Commit Graph

803 Commits

Author SHA1 Message Date
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
f9fba6ce3e [CMake] Move add_pure_swift_host_library to a file
* Move `add_pure_swift_host_library()` from lib/CMakeLists.txt to
  AddPureSwift.cmake so that code outside `lib` can use it
* Add `add_pure_swift_host_tool()` function to make a pure Swift
  host executable target (for future usages)
* Specify depending `SwiftSyntax` modules explicitly because not all
  Swift libraries uses all SwiftSyntax libraries
2023-03-13 19:28:13 -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
Ben Barham
80d27128d8 [Macros] Do not edit macro buffer or position when refactoring
Rather than editing the macro buffer in refactoring, add appropriate
padding and braces when creating the macro.

Don't edit the insertion location - we should update this in a later PR
as well.
2023-03-03 19:48:06 -08:00
Doug Gregor
6b0a0b6555 [CMake] Rework build of ASTGen and ObservationMacros
Refactor the build support for ASTGen and ObservationMacros. Both have
nearly-identical copies of a bunch of messy, workaround-laden CMake
code to build "pure" Swift host libraries using CMake's Swift support.

Instead, introduce `add_pure_swift_host_library` to centralize all of
the hacks to build a host library that's all Swift, using CMake's
support directly (rather than custom commands), and link against the
swift-syntax stack. Switch ASTGen directly over to this.

Add `add_swift_macro_library` on top of this, to make it easy to
create a macro library and install it into the appopriate plugin
directory. Switch ObservationMacros over to this.
2023-03-03 14:01:30 -08:00
Rintaro Ishizaki
93aabfff01 Merge pull request #64006 from rintaro/macros-plugin-errhandling
[Macros] Improve error handling for executable macro plugins
2023-03-02 22:10:51 -08:00
Rintaro Ishizaki
a6e0191bc0 [Macros] Improve error handling for executable macro plugins
* Handle broken pipe
* Handle invalid response (malformed message, unexpected message kind)
2023-03-02 17:40:55 -08:00
Sophia Poirier
70cffb06f3 [Variadic Generics] type parameter pack syntax change: T... -> each T 2023-03-02 14:42:28 -08:00
Rintaro Ishizaki
7ca8241cda Merge pull request #63958 from rintaro/macros-pluginsmessages-3
[Macros] Update PluginMessages for 'customAttrbuteSyntax' rename.
2023-02-28 13:02:02 -08:00