Commit Graph

1443 Commits

Author SHA1 Message Date
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
Slava Pestov
485e4b0926 Sema: Set TVO_CanBindToPack on type variables when generating constraints for PackExpansionExpr 2022-10-27 15:22:17 -04:00
Robert Widmann
394d0645d9 Tidy Up The C AST Bridging File A Bit 2022-10-27 11:11:12 -07:00
Robert Widmann
822fd47b8a Add Utilities to Retrieve the (Opened) Type of a Macro Reference 2022-10-27 11:11:11 -07:00
Holly Borla
032c511754 [ConstraintSystem] Add a PackElementOf constraint to delay mapping opened
element pattern types to pattern archetypes until after type variables are
resolved.
2022-10-24 19:46:44 -07:00
Holly Borla
7354ad30c3 [ConstraintSystem] Use opened element types for opaque values in pack expansions. 2022-10-24 18:11:00 -07:00
Holly Borla
dc35db6aa0 [GenericEnvironment] Map shape types into context in PackArchetypeType::getShape. 2022-10-23 23:49:05 -07:00
Holly Borla
13a17ef918 [ConstraintSystem] Use reduced shape types when generating constraints for pack
expansion expressions.
2022-10-23 20:25:38 -07:00
Holly Borla
dab3b64c99 [ConstraintSystem] Implement basic type checking for pack expressions.
During prechecking, postfix '...' expressions are rewritten to pack
expansion expressions if the operand contains pack references. References
to packs are replaced with opaque values, and the pack expansion stores
the opaque value bindings to decl refs.
2022-10-22 13:41:48 -07:00
Richard Wei
56e7cce809 [Macros] Parse MacroExpansionExpr and MacroExpansionDecl
Introduce `MacroExpansionExpr` and `MacroExpansionDecl` and plumb it through. Parse them in roughly the same way we parse `ObjectLiteralExpr`.

The syntax is gated under `-enable-experimental-feature Macros`.
2022-10-21 01:50:35 -07:00
Slava Pestov
da8ae1d36d Sema: Remove PackExpr 2022-10-16 21:37:25 -04:00
Holly Borla
c4b946195e [AST] Replace the "type sequence" terminology with "parameter pack". 2022-10-10 16:28:13 -07:00
Holly Borla
19688cc2b1 [AST] Add the skeleton for PackExpansionExpr. 2022-10-07 20:13:18 -07:00
Pavel Yaskevich
4442d1c3c9 Merge pull request #61452 from xedin/rework-bool-pattern-cond-handling-in-closures
[CSGen/CSApply] A couple of cleanups to statement condition handling
2022-10-06 12:01:07 -07:00
Suyash Srijan
d124b3581b [Sema] Don't ignore implicit AST nodes in diagnoseUnhandledThrowSite (#61392) 2022-10-06 09:47:50 +01:00
Pavel Yaskevich
e696b759c3 [CSGen/CSApply] Use targets to handle boolean conditions
Remove adhoc constraint generation and solution application
logic from `CK_Boolean` handling and use `SolutionApplicationTarget`
instead.
2022-10-05 10:12:49 -07:00
Pavel Yaskevich
6c518bd311 Merge pull request #61388 from xedin/support-closures-with-implicitly-wrapped-params
[CSClosure] Don't try to infer types for implicitly wrapped parameters
2022-10-03 09:14:17 -07:00
Pavel Yaskevich
77f9c01b77 [CSClosure] Don't try to infer types for implicitly wrapped parameters
Update `applyPropertyWrapperToParameter` to set types to projected
and wrapped values and allow `TypeVariableRefFinder` to skip decls
with implicit property wrappers that are not yet resolved.
2022-09-30 12:53:01 -07:00
Allan Shortlidge
034e53ad20 Sema: Accept if #_hasSymbol() conditions in closure contexts.
Resolves rdar://100129165
2022-09-29 10:55:08 -07:00
Hamish Knight
bca941b152 [AST] NFC: Rename IfExpr -> TernaryExpr
This matches what we call it in SwiftSyntax, and
is just generally clearer.
2022-09-28 10:33:31 +01:00
Allan Shortlidge
9c628904d9 Sema: Temporarily reject if #_hasSymbol in a closure context.
We'll need to implement appropriate constraint generation to support them in such a context.
2022-09-19 11:27:08 -07:00
Pavel Yaskevich
c610e2bd9c [CSSimplify] Member ref decays into value witness for next in for-in
Reference to `$geneator.next` in for-in loop context needs to be
treated as a reference to a witness of `IteratorProtocol#next`
requirement, otherwise it could lead to problems with retroactive
conformances.
2022-09-14 11:35:27 -07:00
Hamish Knight
4716f61fba [AST] Introduce explicit actions for ASTWalker
Replace the use of bool and pointer returns for
`walkToXXXPre`/`walkToXXXPost`, and instead use
explicit actions such as `Action::Continue(E)`,
`Action::SkipChildren(E)`, and `Action::Stop()`.
There are also conditional variants, e.g
`Action::SkipChildrenIf`, `Action::VisitChildrenIf`,
and `Action::StopIf`.

There is still more work that can be done here, in
particular:

- SourceEntityWalker still needs to be migrated.
- Some uses of `return false` in pre-visitation
methods can likely now be replaced by
`Action::Stop`.
- We still use bool and pointer returns internally
within the ASTWalker traversal, which could likely
be improved.

But I'm leaving those as future work for now as
this patch is already large enough.
2022-09-13 10:35:29 +01:00
Allan Shortlidge
f5f1d3c028 AST: Parse #_hasSymbol
Introduce the compiler directive `#_hasSymbol` which will be used to detect whether weakly linked symbols are present at runtime. It is intended for use in combination with `@_weakLinked import` or `-weak-link-at-target`.

```
if #_hasSymbol(foo(_:)) {
  foo(42)
}
```

Parsing only; SILGen is coming in a later commit.

Resolves rdar://99342017
2022-09-08 17:15:29 -07:00
Slava Pestov
79ed990728 AST: Replace TupleTypeRepr's ellipsis with PackExpansionTypeRepr 2022-09-07 12:35:54 -04:00
Amritpan Kaur
4a02d51cfb [CSGen] Add space after initial constraints.
Visually separate initial constraints printed for the first time from solver steps.
2022-09-01 20:25:18 -07:00
Pavel Yaskevich
f5456bd2ac [CSGen] Adjust locators for sub-patterns of parens
In cases like `.<name>(_)`, the `_` sub-pattern should not assume
locator of the parent paren, just like it doesn't in case of tuple
i.e. `.<name>(_, ...)` where each element gets a `PatternMatch(<elt>)`
locator.
2022-08-03 17:30:06 -07:00
Pavel Yaskevich
e66aa61092 [CSGen] Allow _ pattern to assume type of initializer expression
`_` pattern doesn't have a type, it's always contextual, so let's
allow it to assume a type of initializer expression instead of
creating a new type variable. This helps to makes sure that initializer
would never get a placeholder type from `_` pattern it's associated
with.
2022-08-03 15:50:33 -07:00
Pavel Yaskevich
a4fbb3ec40 [CSGen] Allow _ pattern to be a hole
In cases like `case .test(_)` it might not be possible to
establish a type of `_` and hole cannot be propagated to
it from context if condition of switch is incorrect, so
`_` just like a named pattern should be allowed to become
a hole.

Resolves: rdar://96997534
2022-08-03 15:50:33 -07:00
Pavel Yaskevich
3e65a7cab0 Merge pull request #60065 from xedin/result-builder-ast-transform-under-flag
[TypeChecker] Implement result builder transform via AST modification under a flag
2022-08-02 16:22:01 -07:00
Pavel Yaskevich
65aeb4ad89 [CSSolver] Add support for TypeJoinExpr 2022-08-02 11:41:35 -07:00
Hamish Knight
9da53193da [AST] Remove ParameterTypeFlags from ParenType and TupleType
The last clients that relied on stashing parameter
type flags on these types are now gone.
2022-08-02 13:56:32 +01:00
Alex Hoppen
eba95bacbd [CS] Delay type resolution of variables for variables in named pattern 2022-07-20 09:46:14 +02:00
Alex Hoppen
e14fa7291f [CS] Don’t fail constraint generation for ErrorExpr or if type fails to resolve
Instead of failing constraint generation by returning `nullptr` for an `ErrorExpr` or returning a null type when a type fails to be resolved, return a fresh type variable. This allows the constraint solver to continue further and produce more meaningful diagnostics.

Most importantly, it allows us to produce a solution where previously constraint generation for a syntactic element had failed, which is required to type check multi-statement closures in result builders inside the constraint system.
2022-07-20 09:46:12 +02:00