Commit Graph

214 Commits

Author SHA1 Message Date
Holly Borla
e4172055c3 [Macros] Allow any decl names in macro expansions when the macro covers arbitrary
names.
2023-03-04 15:31:24 -08:00
Holly Borla
c9a86ac4fa [Macros] Diagnose macro expansions containing invalid declarations, such as
new macro decls or value decls whose names are not covered by the macro.
2023-03-04 15:31:24 -08:00
Doug Gregor
d1bfe72a45 [Macros] Register the extension for a conformance macro fully.
When we form an extension for a conformance macro, make sure to
register that extension as being part of the nominal type to which the
conformance macro is attached. This ensures that the conformance is
seen everywhere. Crucially, this ensures that that protocol
conformance table handles conformances that are implied by the
conformance macro. For example, a conformance macro that introduces a
`Hashable` conformance will also imply an `Equatable` conformance;
that wasn't happening before this change.

This is needed to make the `OptionSet` macro work fully.
2023-03-01 21:45:04 -08:00
Doug Gregor
200f2340d9 [Macros] Be deliberate about walking macro arguments vs. expansions
Provide ASTWalker with a customization point to specify whether to
check macro arguments (which are type checked but never emitted), the
macro expansion (which is the result of applying the macro and is
actually emitted into the source), or both. Provide answers for the
~115 different ASTWalker visitors throughout the code base.

Fixes rdar://104042945, which concerns checking of effects in
macro arguments---which we shouldn't do.
2023-02-28 17:48:23 -08:00
Richard Wei
77c886c861 [Macros] Rename createUniqueName to makeUniqueName
As per Evolution comment: https://forums.swift.org/t/se-0382-second-review-expression-macros/63064/20

To be merged with https://github.com/apple/swift-syntax/pull/1367
2023-02-28 16:14:15 +08:00
Holly Borla
4b2571aa9c [Macros] Add tests for conditional conformance expansion. 2023-02-23 21:33:03 -08:00
Holly Borla
02a53d070f [Macros] Correct the where clause syntax type in ConformanceMacro. 2023-02-23 20:58:22 -08:00
Holly Borla
a3caacd309 [Macros] Initial implementation of conformance macros. 2023-02-23 20:43:11 -08:00
Holly Borla
c33c925e81 [Macros] Remove the macro role argument to ResolveMacroRequest.
The macro role argument presented an opportunity for callers to accidentally
invoke this request twice for the same macro with slightly different macro
roles passed in, which resulted in re-typechecking the macro arguments.
Instead, derive the corresponding macro roles from the macro reference syntax.
2023-02-14 22:38:53 -08:00
Holly Borla
46db62b5b5 [Macros] Pass the attached macro mangling to ASTGen to use as the discriminator
when creating the macro expansion context.
2023-02-14 16:24:27 -08:00
Holly Borla
f4b2b60446 [Macros] Enable global peer macros.
Global peer macro expansions are not injected into the AST. Instead, they
are visited as "auxiliary declarations" when needed, such as in the decl
checker and during SILGen. This is the same mechanism used for local property
wrappers and local lazy variables.
2023-02-14 16:24:25 -08:00
Holly Borla
56a9e17863 [Macros] Expand peer macros. 2023-02-10 14:43:17 -08:00
Holly Borla
205313b1eb Merge pull request #63484 from hborla/attached-macro-generic-args 2023-02-06 22:39:09 -08:00
Richard Wei
01e4c8df26 [Macros] Use name lookup for lazy declaration macro expansion (#63411)
- Use the name lookup table instead of adding members from a macro expansion to the parent decl context.
- Require declaration macros to specify introduced names and used the declared names to guide macro expansions lazily.
2023-02-07 11:24:42 +08:00
Holly Borla
09e34f274d [Macros] Apply generic arguments from attached macro custom attributes when
resolving the macro reference.
2023-02-06 18:45:26 -08:00
Holly Borla
d6e4b707db [Macros] Update member attribute macro test cases to account for a change in
the MemberAttributeMacro protocol in SwiftSyntax.
2023-02-06 10:15:09 -08:00
Holly Borla
5c3872748a [Macros] Invoke member macro expansion during qualified lookup.
Otherwise, members that are added via macro expansion will not be visible
in other source files.
2023-02-05 21:23:30 -08:00
Doug Gregor
8ad7601ade [Macros] Create macro expansion contexts based on the SourceManager
Rather than trying to patch up the "basic" macro expansion context
that comes from the swift-syntax package, implement our own based
on the new SourceManager. Fixes the `location(of:)` operation.
2023-02-02 23:24:56 -08:00
Doug Gregor
69edc787cf [Macros] Walk semantic attributes when checking a primary file 2023-02-01 23:09:36 -08:00
Doug Gregor
cbdb18097e [Macros] Fix emission of thrown errors.
Errors that were thrown out of a macro implementation were being
emitted and then lost. The result of this is a SILGen assertion later
on, if there were no other errors in the code.

Make sure we properly emit these diagnostics through the source
manager, which required fixing an issue in the offset computation used
for diagnostics.
2023-02-01 15:01:44 -08:00
Doug Gregor
ac4aa41d0f [Macros] Use macro expansion mangling for unique names in macros
Use the name mangling scheme we've devised for macro expansions to
back the implementation of the macro expansion context's
`getUniqueName` operation. This way, we guarantee that the names
provided by macro expansions don't conflict, as well as making them
demangleable so we can determine what introduced the names.
2023-01-31 09:40:48 -08:00
Doug Gregor
3fd895a13d Merge pull request #63284 from DougGregor/swift-syntax-macros-module-update 2023-01-29 15:38:27 -08:00
Doug Gregor
027ce8d21c [ASTGen/Macros] Introduce a Swift-side SourceManager into ASTGen.
Add SourceManager that can keep track of multiple source file syntax
nodes along with their external representations. The source manager can
emit diagnostics into any of those files, including tracking any
explicitly "detached" syntax nodes used for macro expansion.

Make sure we detach syntax nodes before passing them to macro
implementations, so they cannot see more of the source file than they
are permitted. We hadn't been doing this before (by accident), and
doing so motivated the introduction of the SourceManager.

Additionally, perform operator folding on macro arguments as part of
detaching them. Macro clients shouldn't have to do this, and moreover,
when clients do this, they lose the ability to easily emit diagnostics
on the now-folded nodes.
2023-01-28 22:23:52 -08:00
Doug Gregor
182950bacb Update to account for SwiftSyntaxMacros module rename and overhaul 2023-01-28 09:00:54 -08:00
Holly Borla
e6d7ea53ed [Parser] Use proper ParseDeclOptions when parsing the result of a member macro
expansion.
2023-01-27 22:37:22 -08:00
Alex Hoppen
b5ca122a94 Merge pull request #63251 from ahoppen/ahoppen/only-base-nodes-expressible-by-string
[SwiftSyntax] Adjustments because `VariableDeclSyntax` etc. are no longer expressible by string literals
2023-01-27 07:45:16 +01:00
Doug Gregor
976060e29c Merge pull request #63220 from AtariDreams/deprecations
Fix DeclarationMacro deprecations
2023-01-26 17:04:08 -08:00
Alex Hoppen
d0f0652bc6 [SwiftSyntax] Adjustments because VariableDeclSyntax etc. are no longer expressible by string literals 2023-01-27 00:56:24 +01:00
Alex Hoppen
cbdaa25d89 Adjustment for removed with<childName> functions in SwiftSyntax 2023-01-26 17:09:55 +01:00
Holly Borla
07319d0fdd [ASTScope] Teach ASTScope lookup to find source locations inside macro-expanded
scopes.

ASTScope lookup can no longer assume that all scopes are within the same source
file. Scope trees can now contain scopes that are in macro expansion buffers, which
live in different source files with independent source ranges from the root of a
given scope tree. To handle this when searching for a scope containing a given source
location, ASTScope lookup needs to walk up the chain of macro expansion buffers to find
the lowest common buffer in which to compare source locations.

This fixes a number of issues with unqualified lookup into and from within macro-expanded
code.
2023-01-25 23:19:33 -08:00
Rose
0a380cbba3 Fix DeclarationMacro deprecations
These deprecations are simple renames, but this PR should get rid of the warnings.
2023-01-25 13:50:02 -05:00
Holly Borla
14f11727f2 [ASTScopeCreation] Teach ASTScope to find the enclosing type context for
synthesized member macro expansion buffers.
2023-01-24 19:45:22 -08:00
Holly Borla
e04a791ad7 [Macros] Allow synthesized member macros to add member type and method decls. 2023-01-24 19:45:22 -08:00
Holly Borla
e5ba38925a [Macros] Allow one attached macro declaration to fulfill multiple roles. 2023-01-23 17:56:48 -08:00
Holly Borla
f1742fd721 [Macros] Initial implementation for synthesized member macro expansion. 2023-01-23 17:56:48 -08:00
Holly Borla
6fcd3f6557 [Macros] Enable composition of member attribute macros and accessor macros. 2023-01-17 18:54:16 -08:00
Erik Eckstein
bfa14e64dd Revert "[Macros] Enable composition of member attribute macros and accessor macros."
It causes crashes in some Sema/SwiftUI tests in some configurations.

rdar://104297642

This reverts commit b8d65e1ed9.
2023-01-17 13:30:27 +01:00
Holly Borla
0d7109710f [Macros] Pass the parent declaration of the member-to-expand to member
attribute macro expansions.
2023-01-15 11:36:47 -08:00
Holly Borla
b8d65e1ed9 [Macros] Enable composition of member attribute macros and accessor macros. 2023-01-15 10:11:00 -08:00
Holly Borla
4f8e1daa26 [Macros] Implement the skeleton of attribute macro expansion.
Attribute macros are expanded during AttachedSemanticAttrsRequest. This
change invokves the expansion, but it does nothing with the result yet.
The next step is to parse back in the attribute list, type check it, and
return the resulting attributes back to the request to add to the semantic
attribute list.
2023-01-15 10:11:00 -08:00
Doug Gregor
91ee109c2f [Macros] Start expanding accessor macros attached to a storage declaration.
Accessor macros are attached macros (written with attribute syntax)
that can generate accessors for a property or subscript. Recognize
custom attributes that are accessor macros when written on a storage
declaration, and expand those macros.

This is very much a work in progress, and the result of the expansion
isn't yet parsed or wired into the AST.
2023-01-13 10:12:25 -08:00
Alex Hoppen
e2842c7022 Merge pull request #62961 from ahoppen/ahoppen/merge-binary-operators
[Macros] Adjustments for merging `spacedBinaryOperator` and `unspacedBinaryOperator` in SwiftSyntax
2023-01-11 19:32:08 +01:00
Alex Hoppen
ee9033b41d [Macros] Adjustments for merging spacedBinaryOperator and unspacedBinaryOperator in SwiftSyntax 2023-01-11 08:07:53 +01: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
Doug Gregor
dfa3ed910d Teach DeclContext::getParentSourceFile() to return the innermost source file
With macro expansions, and really any code generation that produces
proper source locations, the parent source file of a declaration
context will be a generated source file rather than the source file
near the top of the declaration-context stack. Adjust the
implementation of `getParentSourceFile()` to return that innermost
source file by doing location-based lookup.
2023-01-08 11:40:02 -08:00
Doug Gregor
bbb5c7a690 [Macros] Allow enums and classes/actors to be macro definitions, too 2023-01-02 21:22:04 -08:00
Doug Gregor
222bc03c0a [Macros] Prevent recursive expansion of macros. 2023-01-02 21:22:04 -08:00
Doug Gregor
4ed393db46 [Macros] Add a test of a variadic "print" macro.
Macros can use variadic generics, and don't need runtime support for
variadic generics to do so. Add an example "print".
2023-01-02 21:22:04 -08:00
Doug Gregor
921e61b2fa Emit "in expansion of macro" notes for diagnostics within macro expansion buffers 2022-12-15 13:37:29 -08:00
Doug Gregor
387a79c47b [Macro test] Handle source locations in macro diagnostics better.
"Relink" the folded syntax node back into the primary syntax node. When
we do this, we get consistent source locations that do not require any
adjustment.

Test this by adding a Fix-It to the silly AddBlocker macro, replacing
the `+` with a `-`.
2022-12-14 16:13:49 -08:00