Commit Graph

220 Commits

Author SHA1 Message Date
Alex Hoppen
5ab33b44b1 Merge pull request #67836 from ahoppen/ahoppen/no-optional-collections
[ASTGen/Macros] Update for the fact that syntax collections are always non-optional in SwiftSyntax now
2023-08-11 15:27:27 -07:00
Alex Hoppen
8231b272ba [ASTGen/Macros] Update for the fact that syntax collections are always non-optional in SwiftSyntax now 2023-08-11 07:46:59 -07:00
Hamish Knight
35a98f3828 [ASTGen] Allow querying of enabled features
For now this is using C interop, we ought to be
using C++ interop, but that's a much bigger
change.
2023-08-09 19:35:50 +01:00
Hamish Knight
33028a7cc4 Update for SyntaxTransformVisitor SPI 2023-08-09 19:35:49 +01:00
Alex Hoppen
2890e292bd [ASTGen] Fix swift-syntax deprecation warnings 2023-08-03 10:44:09 -07:00
John McCall
1a7d44a11d Diagnose attempts to reabstract variadic function types in unimplementable ways. 2023-07-18 13:22:35 -04:00
Holly Borla
48637c67d9 [ASTGen] Fix a silly typo that caused a bogus protocol conformance list to be
passed to extension macro expansion on the plugin server code path.
2023-07-07 19:18:33 -07:00
Rintaro Ishizaki
928c94ff13 [Macros] PluginMessages aren't @_spi anymore
rdar://111748087
2023-07-05 15:05:07 -07:00
Holly Borla
0bd898eb12 [Macros] Allow extension macros to suppress conformances that are already
stated in the original source.

If an extension macro can introduce protocol conformances, macro expansion
will check which of those protocols already have a stated conformance in the
original source. The protocols that don't will be passed as arguments to
extension macro expansion, indicating to the macro that it should only add
conformances to those protocols.
2023-06-30 16:01:15 -07:00
Rintaro Ishizaki
3e9c2536f6 [Macros] Unify PluginMessages.swift
Use PluginMessages.swift in swift-syntax consistently.
2023-06-29 13:04:10 -07:00
Holly Borla
43e2026a6e [ASTGen] The 'extendedType' argument for attached macros does not have a
source location.
2023-06-28 22:56:42 -07:00
Holly Borla
c3bdae6a30 [ASTGen] Sync PluginMessages.swift with the SwiftSyntax changes for extension
macros.
2023-06-28 13:56:50 -07:00
Holly Borla
d3e2562cd9 [Macros] Pass a qualified extended type to extension macro expansion. 2023-06-27 21:22:12 -07:00
Holly Borla
725374e0d8 [Macros] Implement attached extension macros. 2023-06-27 21:22:12 -07:00
Rintaro Ishizaki
eefe9dc7ef [Macros] Attached macro expansions return single string
* Move collapse(expansions:for:attachedTo:) to SwiftSyntaxMacroExpansion
* SwiftSyntaxMacroExpansion.expandAttachedMacro() now perform collapsing
* SwiftSyntaxMacroExpansion.expandAttachedMacroWithoutCollapsing()
  to keep old behavior
* IPC request 'getCapability' now sends the host protocol version
* Unified IPC response 'macroExpansionResult' that returns single string
  for both 'expandFreestandingMacro' and 'expandAttachedMacro'
* Compiler accepts old 'expandFreestandingMacroResult' and
  'expandAttachedMacroResult' to keep compatibility
* Plugins check the compiler's protcol version to see if it suppports
  'macroExpansionResult', and fall back to old behavior if necessary
2023-06-26 11:04:59 -07:00
Doug Gregor
3c04cff8dd [Macros] Provide the freestanding macro role for expansion operations.
The compiler knows (from a macro declaration) what freestanding macro
role a macro implementation is expected to implement. Pass that through
to the macro expansion code itself, rather than guessing based on the
protocol conformances of the implementation type. We already use this
approach with attached macros, so this is more of the same.

Eliminates a crash and improves diagnostics when the freestanding macro
role and its implementation are out of sync, fixing rdar://110418969.
2023-06-09 12:59:56 -07:00
Ben Barham
18f56c9de5 [NFC][ASTGen] Add wrapping structs for commonly bridged types
Adds wrapping structs for a bunch of commonly bridged types (but not all
the AST nodes yet).
2023-06-02 10:11:46 -07:00
Doug Gregor
4188dca07a [ASTGen] Handle composition types parsed through parseType().
Protocol composition types such as `P & Q` are similar to nested types
like `A.B` because the innermost type syntax node at the given position
doesn't cover the whole type. Adjust by looking back up the tree.
This all feels like a hack, and there should be a better way.

While here, fix the source ranges passed in to create
`CompositionTypeRepr`. We were tripping assertions due to missing
source-location information.
2023-05-22 21:39:59 -07:00
Doug Gregor
e935ebb682 [ASTGen] Handle simple type attributes.
Bridge the simple type attributes like `@autoclosure` into an
`AttributedTypeRepr`. We don't validate the arguments, and we don't
handle more complicated attributes like `@convention(c)` just yet.
2023-05-22 21:39:59 -07:00
Doug Gregor
3dad575e89 [ASTGen] Handle variadic parameters in functions and function types
There is a modeling difference between the swift-syntax tree and the
C++ type representation (TypeRepr) that is a little odd here, so we
end up parsing the ellipsis on the C++ side rather than looking "up"
the syntax tree to find it.
2023-05-22 21:39:59 -07:00
Doug Gregor
b50d5fe438 [ASTGen] Handle the 'Any' type 2023-05-22 21:39:59 -07:00
Doug Gregor
1340124a96 [ASTGen] Support specifiers on types such as inout and consuming 2023-05-22 21:39:59 -07:00
Doug Gregor
4031d7cd1b [ASTGen] Make sure we provide proper TypeReprs for generic arguments
The pointers we were vending to C++ had low bits set because they were
AST node entries rather than the raw TypeReprs.
2023-05-22 21:39:59 -07:00
Doug Gregor
dba94bdbd4 [ASTGen] Add experimental feature to use ASTGen in lieu of parsing types
Introduce a new experimental feature `ASTGenTypes` that uses ASTGen to
translate the Swift syntax tree (produced by the new Swift parser)
into C++ `TypeRepr` nodes instead of having the C++ parser create the
nodes.

The approach here is to intercept the C++ parser's `parseType`
operation to find the Swift syntax node at the given position (where
the lexer currently is) and have ASTGen translate that into the
corresponding C++ AST node. Then, we spin the lexer forward to the
token immediately following the end of the syntax node and continue
parsing.
2023-05-22 21:39:59 -07:00
Rintaro Ishizaki
5fe2ead4e2 [Madros] Update for SwiftSyntaxMacroExpansion module
Share the same expansion logic between ASTGen and
SwiftCompilerMessageHandling
2023-05-11 19:45:14 -07:00
Rintaro Ishizaki
e181a4d39c [Macros] Improve error handling for plugin errors 2023-05-04 14:40:28 -07:00
Rintaro Ishizaki
e93e4ba2cd [Macro] Handle error during initializing executable plugins
Previously, the compiler fails to read the result from 'getCapability'
IPC message, it used to just emit a fatalError and crashed.
Instead, propagate the error status and diagnose it.

rdar://108022847
2023-05-04 09:03:21 -07:00
Ben Barham
fe2104ca99 [Macros] Automatically format expanded macros
Rather than requiring macro implementations to add required whitespace
and indentation, basic format all macro expansions. Right now this uses
the default four space indentation, we can consider having that inferred
later. Macros can opt-out of automatic formatting by implementing
`formatMode` and setting it to `.disabled`.

Also moves the extra newlines before/after expansions to a new "Inline
Macro" refactoring.

Resolves rdar://107731047.
2023-04-27 21:24:46 -07:00
Alex Hoppen
3b0dc8d3a9 [Macros] Remove usages of deprecated methods in SwiftSyntaxMacros 2023-04-22 09:52:10 -07:00
Kim de Vos
b5698798d1 Remove force unwrapping for source location
Companion of https://github.com/apple/swift-syntax/pull/1532
2023-04-20 08:17:32 +02:00
Alex Hoppen
39c9bc8746 Merge pull request #65126 from ahoppen/ahoppen/memberblock
Adjustment for SwiftSyntax rename `members` -> `memberBlock`
2023-04-15 08:00:38 -07:00
Alex Hoppen
767ec056c9 Merge pull request #65118 from ahoppen/ahoppen/better-debug-description
[SwiftSyntax] Adjustments because recursiveDescription is removed in favor of debugDescription
2023-04-13 16:30:49 -07:00
Alex Hoppen
42cb41ded9 Adjustment for SwiftSyntax rename members -> memberBlock
Companion of https://github.com/apple/swift-syntax/pull/1524
2023-04-13 16:28:54 -07:00
Rintaro Ishizaki
fe1eb469e4 [ASTGen] Avoid including C standard libary headers in brigdging headers
C stdlib headers are part of "Darwin"/"Glibc" clang module.
If a Swift file imports a bridging headers and that has '#include'
C stdlib headers, Swift compiler implicitly imports "Darwin"/"Glibc"
overlay modules. That violates dependency layering. I.e. Compiler
depends on Darwin overlay, Darwin overlay is created by the compiler.

rdar://107957117
2023-04-12 17:22:32 -07:00
Alex Hoppen
ccccc33b01 [SwiftSyntax] Adjustments because recursiveDescription is removed in favor of debugDescription
Companion of https://github.com/apple/swift-syntax/pull/1503
2023-04-12 14:39:14 -07:00
Alex Hoppen
9b814218b8 Merge pull request #64954 from ahoppen/ahoppen/swiftsyntax-public-api
[ASTGen] Remove usage of methods that shouldn’t be part of SwiftSynax’s public API
2023-04-11 16:04:08 -07:00
Alex Hoppen
32ecb2858d [ASTGen] Remove usage of methods that shouldn’t be part of SwiftSynax’s public API 2023-04-05 16:47:02 -07:00
Alex Hoppen
3765410245 Merge pull request #64747 from ahoppen/ahoppen/fixits
Adjustments for how Fix-It changes are represented by SwiftSyntax
2023-04-05 15:57:03 -07:00
Alex Hoppen
f3f1a5ed81 Merge pull request #64697 from ahoppen/ahoppen/split-function-parameter
[ASTGen/Macros] Adjustments to split `FunctionParameterSyntax` into multiple nodes for function parameters, closure parameters and enum parameters
2023-04-05 13:33:14 -07:00
Richard Wei
01e6fe2936 [Macros] Code item macros
Add support for declaring and expanding code item macros.  Add experimental feature flag `CodeItemMacros`.
2023-04-04 09:54:57 -07:00
Doug Gregor
e7fcee698c [Macros] Use MacroExpansionContext.addDiagnostics for thrown errors
Rather than open-coding the mapping of thrown errors to diagnostics,
use `MacroExpansionContext.addDiagnostics`. It already does the right
thing.
2023-03-30 13:27:16 -07:00
Alex Hoppen
962664a2e5 [ASTGen/Macros] Adjustments to split FunctionParameterSyntax into multiple nodes for function parameters, closure parameters and enum parameters
Companion of https://github.com/apple/swift-syntax/pull/1455
2023-03-30 08:31:26 -07:00
Doug Gregor
7c7459ac6b Merge pull request #64641 from DougGregor/macro-replacement-expansion 2023-03-29 20:47:30 -07:00
Doug Gregor
9292231e1f [Macros] Start recording expanded macro definitions and replacements
Handle a trivial macro defined in terms of another macro.
2023-03-29 16:32:28 -07:00
Doug Gregor
49277f7e89 [Macros] Move most macro definition checking into ASTGen
In preparation for supporting macros that are defined in terms of other
macros, adopt macro definition checking provided by the
`MacroDeclSyntax.checkDefinition` operation (implemented in
swift-syntax). Use this in lieu of the checking on the C++ side.

This required me to finally fix an issue with the source ranges for
Fix-Its, where we were replacing the leading/trailing trivia of nodes
along with the node itself, even though that's incorrect: we should
only replce the node itself, and there are other Fix-It kinds for
replacing leading or trailing trivia.
2023-03-29 16:30:42 -07:00
Alex Hoppen
95d9684827 Adjustments for how Fix-It changes are represented by SwiftSyntax 2023-03-29 15:39:35 -07:00
Doug Gregor
a2737df55a [Macros] Unpack DiagnosticsError into separate diagnostics
Fixes rdar://107289985.
2023-03-29 15:28:52 -07:00
Rintaro Ishizaki
02c72cd41c Merge pull request #64407 from rintaro/macros-dump-plugin-messaging
[Macros] Add env variable option to dump exectuable plugin messagings
2023-03-27 09:55:17 -07:00
Rintaro Ishizaki
bd9af096d8 [Macro] Add env variable option to dump exectuable plugin messagings
Set 'SWIFT_DUMP_PLUGIN_MESSAGING' env variable to enable it.
2023-03-24 16:39:30 -07:00
Kim de Vos
8fb787dc27 Downgrade placeholder error to warning for swiftparser 2023-03-24 20:18:02 +01:00