Commit Graph

1476 Commits

Author SHA1 Message Date
Holly Borla
26786c1e09 [Macros] Make macro expansions always have an argument list. If an explicit
argument list isn't written, an implicit, empty list is created.
2023-01-31 17:04:35 -08:00
Holly Borla
7c50f95e1b [Macros] Use MacroExpansionExpr when resolving attached and freestanding macros. 2023-01-31 09:27:06 -08:00
Alex Hoppen
17d47bb691 [Sema] Don’t fail constraint generation if a closure contains an ErrorExpr
It appears like this was missed in #60062.
2023-01-30 17:36:36 +01:00
Pavel Yaskevich
6ea82b21e3 Merge pull request #63196 from xedin/connect-captures-to-closure
[CSGen] Captures should always be connected to their closure
2023-01-26 11:03:06 -08:00
Pavel Yaskevich
516e616bdc Merge pull request #63149 from Vespen/fix-non-api-wrapper-parameter
[Sema] Fix for non-API level property wrappers with closure.
2023-01-26 09:45:27 -08:00
Doug Gregor
72ddbebc08 [Macros] Assign macro expansion discriminators more lazily.
This is a punt. They'll be unique, and they'll be per-context, but
they'll be lazy and therefore not stable.
2023-01-25 22:03:38 -08:00
Anton Lagutin
a28776e847 [Sema] applyPropertyWrapperToParameter fails when wrapper is not applied 2023-01-25 13:46:27 +10:00
Pavel Yaskevich
60720f4e07 [CSGen] Captures should always be connected to their closure
Otherwise it might be possible (once references are not resolved
eagerly for example) to get a situation where closure is solved
before captures are.
2023-01-24 10:43:29 -08:00
Anton Lagutin
f1c6c1ba41 [Sema] Fix for non-API level property wrappers with closure.
[Sema] Added parameter tests for non-API-level wrappers.
[Sema] Added solution for non-API level property wrappers with closure.
2023-01-23 14:22:25 +10:00
Pavel Yaskevich
c2ad27ccc6 Merge pull request #62899 from xedin/closure-analyzer-shouldnt-walk-local-decls
[CSGen] Closure analyzer shouldn't walk into local some decls/patterns
2023-01-19 10:25:58 -08:00
Doug Gregor
de16b47875 [Macros] Introduce the @attached attribute for declaring attached macros.
Describe attached macros with the `@attached` attribute, providing the
macro role and affected names as arguments to the macro. The form of
this macro will remain the same as it gains other kinds of attached
macro roles beyond "accessor".

Remove the "accessors" role from `@declaration`, which will be going
away.
2023-01-13 22:47:59 -08:00
Richard Wei
f17b7c48bf [Macros] Freestanding declaration macros
Add support for freestanding declaration macros.

- Parse `@declaration` attribute.
- Type check and expand `MacroExpansionDecl`.

Known issues:
- Generic macros are not yet handled.
- Expansion does not work when the parent decl context is `BraceStmt`. Need to parse freestanding declaration macro expansions in `BraceStmt` as `MacroExpansionDecl`, and add expanded decls to name lookup.
2023-01-10 19:09:11 -08:00
Holly Borla
6e28f996ce [ConstraintSystem] Create opened pack element generic environments lazily
in the constraint system.
2023-01-07 09:50:14 -08:00
Holly Borla
74bf2f3e7a [AST] Introduce a syntactic distinction betweek vararg type reprs and
pack expansion type reprs.

Classic variadic parameters still use the postfix ellipsis syntax, and
pack expansion types now use a prefix 'repeat' keyword.
2023-01-07 09:50:11 -08:00
Pavel Yaskevich
ca126bc433 [CSGen] Closure analyzer shouldn't walk into local some decls/patterns
While trying to gather all references to external variables,
analyzer shouldn't walk into patterns and any local declarations
except to pattern bindings, because decls do not participate
in inference and patterns cannot contain references analyzer
is looking for.
2023-01-06 15:46:35 -08:00
Doug Gregor
6bb9cb8b5d [Macros] Always parse macro expansions, diagnose later
Always parse macro expansions, regardless of language mode, and
eliminate the fallback path for very, very, very old object literals
like `#Color`. Instead, check for the feature flag for macro
declaration and at macro expansion time, since this is a semantic
restriction.

While here, refactor things so the vast majority of the macro-handling
logic still applies even if the Swift Swift parser is disabled. Only
attempts to expand the macro will fail. This allows us to enable the
macro-diagnostics test everywhere.
2023-01-02 21:22:04 -08:00
Doug Gregor
71ca9c86e6 [Macros] Diagnose when we forget to provide macro arguments.
Unlike functions, you can't curry macros; diagnose when one omits the
arguments in a macro expansion of a macro that has a parameter list.
2023-01-02 21:22:04 -08:00
Doug Gregor
806b5a8777 [Macros] Diagnose errors where a macro is used without the '#'. 2023-01-02 21:22:04 -08:00
Holly Borla
e966b4ef7d [CSDiagnostics] Add an error message for pack expansion expressions over
packs that don't have the same shape.
2022-12-21 08:25:10 -05:00
Holly Borla
58953567b3 [CSGen] Use PackExpansionExpr::getExpandedPacks to generate ShapeOf constraints
rather than relying on PackElementExprs collected by preCheck.

This handles pack element expressions and pack element type reprs, and enforces that
all packs expanded by a given pack expansion expression all have the same shape.

Once the parser creates PackExpansionExpr directly (based on a dedicated syntax instead
of postfix '...'), the code in preCheck for identifying and creating pack expansions
can simply be deleted.
2022-12-20 22:34:16 -05:00
Holly Borla
2df72f6bcc [ConstraintSystem] Strip pack expansions off of pack reference types in
getUnopenedTypeOfReference.
2022-12-20 22:13:40 -05:00
Holly Borla
334efb9975 [ConstraintSystem] Always pass in a direct instance of OpenPackElementType instead
of assigning it to std::function_ref.

std::function_ref does not own the function reference, so assigning an OpenPackElementType
to a local function_ref lead to stale constraint system references when invoking
operator () later on.
2022-12-20 15:44:42 -05:00
Holly Borla
7b35e85d31 [PreCheckExpr] Consider postfix ellipsis expressions containing 'each' type
reprs to be pack expansion expressions.
2022-12-20 11:36:58 -05:00
Holly Borla
27aa3651e8 [ConstraintSystem] Open pack element types using a PackElementOf constraint
when resolving pack reference type reprs inside pack expansion expressions.
2022-12-20 11:36:58 -05:00
Holly Borla
454913aeb5 [TypeResolution] Plumb a pack element opener function through type resolution. 2022-12-20 11:36:58 -05:00
Holly Borla
42020792c6 [Sema] Represent pack element references in pack expansion patterns as
PackElementExpr.
2022-12-14 20:45:52 -08:00
Holly Borla
48fdafe08d [ConstraintSystem] Implement constraint generation and solution application for
PackElementExpr.
2022-12-14 20:45:51 -08:00
Holly Borla
0d4e487e06 [CSGen] Store a stack of pack element generic environments in constraint
generation.
2022-12-14 20:45:51 -08:00
Holly Borla
ea68720e12 [CSBindings] Instead of breaking a PackElementOf constraint down into Bind
constraints, vend potential bindings through PotentialBindings::infer.

This allows for bidirectional binding inference from the pack type to the
element type and vice versa.
2022-12-14 20:45:51 -08:00
Holly Borla
f1fd9acb23 [AST] Add 'PackElementExpr' for explicit pack elements spelled with the 'each'
keyword.
2022-12-14 20:45:51 -08:00
Pavel Yaskevich
9a22bac825 Merge pull request #62315 from hank121314/fix/issue-60806
[CSGen]: `getTypeForPattern` should perform member lookup into the external pattern metatype
2022-12-10 00:22:38 -05:00
hank121314
9ddf49738f [CSGen]: getTypeForPattern should perform member lookup into the external pattern metatype if enum element has externalPatternType 2022-11-30 21:45:15 +08:00
Doug Gregor
64f2c8ef0c Address a few comments about the specialization arguments constraint. 2022-11-28 18:33:11 -08:00
Doug Gregor
a9ee5dd142 [Macros] Don't allow local names to shadow macro names. 2022-11-28 18:33:10 -08:00
Doug Gregor
b29fcb4e58 [Macros] Parse macro declarations fully, and treat them as normal declarations 2022-11-28 18:32:43 -08:00
Doug Gregor
8adee85cb6 [Macros] Add support for explicit generic arguments of macros.
Enable type checking support for explicitly specifying generic arguments to
a macro, e.g., `#stringify<Double>(1 + 2)`. To do so, introduce a new
kind of constraint that performs explicit argument matching against the
generic parameters of a macro only after the overload is chosen.
2022-11-28 18:32:43 -08:00
Holly Borla
c225fc428b Merge pull request #62228 from hborla/pack-element-generic-environment
[GenericEnvironment] Include original parameter packs in opened pack element signatures.
2022-11-28 15:34:35 -05:00
Holly Borla
10adbddddd [ConstraintSystem] Correct the first type of the ShapeOf constraint when
generating constraints for PackExpansionExpr.

The result pattern type of a pack expansion doesn't always contain pack
references. Instead, use the type of the first pack reference binding.
2022-11-24 00:01:58 -05:00
Erik Eckstein
ab1b343dad use new llvm::Optional API
`getValue` -> `value`
`getValueOr` -> `value_or`
`hasValue` -> `has_value`
`map` -> `transform`

The old API will be deprecated in the rebranch.
To avoid merge conflicts, use the new API already in the main branch.

rdar://102362022
2022-11-21 19:44:24 +01:00
Holly Borla
72d87b5ca5 [ConstraintSystem] Add a locator path element for the pattern of a pack expansion. 2022-11-16 09:06:58 -08:00
Holly Borla
9eccfbd3ca [CSGen] Use a ShapeOf constraint when generating constraints for a pack
expansion expression.
2022-11-15 21:33:17 -08:00
Doug Gregor
51fcde7b76 Merge pull request #62086 from DougGregor/macro-decl 2022-11-13 20:25:48 -08:00
Doug Gregor
1aa907500f [Macros] Use the macro recorded in the constraint system for expansion.
Rather than lookup up the macro by name again during constraint
application, use the overload choice for the macro as recorded in the
constraint system to apply the correct macro.
2022-11-13 13:46:42 -08:00
Slava Pestov
386a11f2c1 Merge pull request #62016 from slavapestov/shape-requirements-misc
Mangling for shape requirements, and related fixes
2022-11-12 13:27:00 -05:00
Slava Pestov
30a1211a3b AST: Move getReducedShape() from CSSimplify.cpp to a method on TypeBase 2022-11-12 02:13:54 -05:00
Doug Gregor
16f81fbad5 Introduce a Macro AST type and use it to describe the macros we find 2022-11-11 15:24:37 -08:00
Richard Wei
d059735260 [Macros] Type check user-defined macro plugins (#61861)
Type check user-defined macros plugins with user-provided type signatures.

Also, load plugin libraries with `RTLD_LOCAL` instead of `RTLD_GLOBAL` to prevent symbol collision between plugins. `llvm::sys::DynamicLibrary` only supports `RTLD_GLOBAL` so we use the plain `dlopen` instead. This does not work on Windows and needs to be fixed.

Friend PR: apple/swift-syntax#1042
2022-11-02 18:22:31 -07:00
Richard Wei
4ce1ebb120 [Macros] Support user-defined macros as compiler plugins (#61734)
Allow user-defined macros to be loaded from dynamic libraries and evaluated.

- Introduce a _CompilerPluginSupport module installed into the toolchain. Its `_CompilerPlugin` protocol acts as a stable interface between the compiler and user-defined macros.
- Introduce a `-load-plugin-library <path>` attribute which allows users to specify dynamic libraries to be loaded into the compiler.

A macro library must declare a public top-level computed property `public var allMacros: [Any.Type]` and be compiled to a dynamic library. The compiler will call the getter of this property to obtain and register all macros.

Known issues:
- We current do not have a way to strip out unnecessary symbols from the plugin dylib, i.e. produce a plugin library that does not contain SwiftSyntax symbols that will collide with the compiler itself.
- `MacroExpansionExpr`'s type is hard-coded as `(Int, String)`. It should instead be specified by the macro via protocol requirements such as `signature` and `genericSignature`. We need more protocol requirements in `_CompilerPlugin` to handle this.
- `dlopen` is not secure and is only for prototyping use here.

Friend PR: apple/swift-syntax#1022
2022-10-31 14:03:25 -07:00
Slava Pestov
8a05768fd9 Merge pull request #61728 from slavapestov/pack-expansion-matching-pattern-instantiation
Instantiate pattern type when matching PackExpansionTypes
2022-10-28 12:12:17 -04:00
Doug Gregor
d6d2318dc9 [Macros] Handle macro expansion for function-like macros. 2022-10-27 17:15:30 -07:00