Commit Graph

428 Commits

Author SHA1 Message Date
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
Rintaro Ishizaki
a49ab25ae8 [Macros] Recovery after executable plugin crash
When executable plugins crashed or somehow decided to exit, the compiler
should relaunch the plugin executable before sending another message.
2023-03-23 22:26:42 -07:00
Rintaro Ishizaki
54884f05e5 [Macros/Plugin] Make 'features' a set of enum values 2023-03-17 10:08:18 -07:00
Rintaro Ishizaki
1851ba2d8f [Macros] swift-plugin-server cosmetic tweaks 2023-03-16 14:00:45 -07:00
Rintaro Ishizaki
c4b3edd6df [Macros] Add swift-plugin-server executable
This executable is intended to be installed in the toolchain and act as
an executable compiler plugin just like other 'macro' plugins.

This plugin server has an optional method 'loadPluginLibrary' that
dynamically loads dylib plugins.
The compiler has a newly added option '-external-plugin-path'. This
option receives a pair of the plugin library search path (just like
'-plugin-path') and the corresponding "plugin server" path, separated
by '#'. i.e.

  -external-plugin-path
    <plugin library search path>#<plugin server executable path>

For exmaple, when there's a macro decl:

  @freestanding(expression)
  macro stringify<T>(T) -> (T, String) =
      #externalMacro(module: "BasicMacro", type: "StringifyMacro")

The compiler look for 'libBasicMacro.dylib' in '-plugin-path' paths,
if not found, it falls back to '-external-plugin-path' and tries to find
'libBasicMacro.dylib' in them. If it's found, the "plugin server" path
is launched just like an executable plugin, then 'loadPluginLibrary'
method is invoked via IPC, which 'dlopen' the library path in the plugin
server. At the actual macro expansion, the mangled name for
'BasicMacro.StringifyMacro' is used to resolve the macro  just like
dylib plugins in the compiler.

This is useful for
 * Isolating the plugin process, so the plugin crashes doesn't result
   the compiler crash
 * Being able to use library plugins linked with other `swift-syntax`
   versions

rdar://105104850
2023-03-16 14:00:45 -07:00
Rintaro Ishizaki
9004fd24d4 [ASTGen] Separate JSON serialization module from ASTGen
For future usage from other host libraries written in Swift

For CMake:
 * Explicitly specify LINKER_LANGAGE to CXX in existing components so
   that 'swiftc' is not used when linking with 'swiftASTGen'
 * Add 'EMIT_MODULE' argument to 'add_pure_swift_host_library' to emit
   .swiftmodule usable from other Swift libraries.
2023-03-09 19:15:45 -08:00
Doug Gregor
70d91b95da Merge pull request #64196 from DougGregor/macros-dont-look-up 2023-03-07 20:37:51 -08:00
Doug Gregor
136f6f262f [Macros] Look further up the syntax tree to find the type we need 2023-03-07 17:54:25 -08:00
Richard Wei
aea93fc8ef Merge pull request #63962 from rxwei/rename-createuniquename
[Macros] Rename `createUniqueName` to `makeUniqueName`
2023-03-07 15:34:03 -08:00
Ben Barham
edb980eb55 Merge pull request #64095 from bnbarham/do-not-change-refactoring
[Macros] Do not edit macro buffer or position when refactoring
2023-03-06 08:43:30 -08:00
Richard Wei
833338f9ce [Macros] Top-level freestanding macros (#63553)
Allow freestanding macros to be used at top-level.
- Parse top-level `#…` as `MacroExpansionDecl` when we are not in scripting mode.
- Add macro expansion decls to the source lookup cache with name-driven lazy expansion. Not supporting arbitrary name yet.
- Experimental support for script mode and brace-level declaration macro expansions: When type-checking a `MacroExpansionExpr`, assign it a substitute `MacroExpansionDecl` if the macro reference resolves to a declaration macro. This doesn’t work quite fully yet and will be enabled in a future fix.
2023-03-06 07:15:20 -08:00