Commit Graph

783 Commits

Author SHA1 Message Date
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
Ben Barham
2562afa74e Merge pull request #63959 from bnbarham/more-workarounds
[CMake] Workaround stale object files causing rebuilds
2023-02-28 09:31:44 -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
Rintaro Ishizaki
b41a44d259 [Macros] Update PluginMessages for 'customAttrbuteSyntax' rename.
Rename 'customAttributeSyntax' -> 'attributeSyntax'
2023-02-27 23:54:30 -08:00
Ben Barham
50fb165d61 [CMake] Workaround stale object files causing rebuilds
Consider the case of A -> B. The previous workaround will add a
forced-B-dep.swift to A that is touched if B rebuilds. So if B is
rebuilt, A is also rebuilt. But A itself has no real changes, and so
none of its object files are built and none of the mtimes are updated.

Thus, A is then rebuilt again on the next run because its input
(forced-B-dep.swift) is newer than the corresponding object file. To
prevent this, update the mtime for the library and object files after the
build (which is actually a link step that both compiles and links).
2023-02-27 19:29:02 -08:00
Doug Gregor
0d6de9e4a1 [Diagnostics] Switch swift-syntax diagnostic style to grouped diagnostics
Use the new "grouped diagnostics" feature of the swift-syntax
diagnostic rendering to emit printed diagnostics under the
swift-syntax diagnostic style. This emits macro expansion buffers as
text to the terminal, inset in a box where the macro was expanded, so
that there is more context for understanding how the macro was
expanded and what went wrong inside it.
2023-02-27 08:56:21 -08:00
Rintaro Ishizaki
d6cf55bf82 [ASTGen] Update PluginMessage for conformance macro 2023-02-24 15:16:38 -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
Rintaro Ishizaki
4190cb7978 Merge pull request #63793 from rintaro/macro-plugin-executable
[Macros] Add executable plugin support
2023-02-23 14:02:33 -08:00
Rintaro Ishizaki
c6387a5450 [Macros] Small changes for review 2023-02-22 17:41:43 -08:00
Rintaro Ishizaki
e840409d03 [ASTGen] Remove unnecessary parameters from the expansion funcs 2023-02-22 15:32:25 -08:00
Michael Gottesman
8ee97e1eb3 [reference-bindings] Updates to ASTGen for swift-syntax changes 2023-02-22 13:57:51 -08:00
Rintaro Ishizaki
0e31393024 [Macros] Add executable plugin support
Executable compiler plugins are programs invoked by the host compiler
and communicate with the host with IPC via standard IO (stdin/stdout.)
Each message is serialized in JSON, prefixed with a header which is a
64bit little-endian integer indicating the size of the message.

* Basic/ExecuteWithPipe: External program invocation. Lik
  llvm::sys::ExecuteNoWait() but establishing pipes to the child's
  stdin/stdout
* Basic/Sandbox: Sandboxed execution helper. Create command line
  arguments to be executed in sandbox environment (similar to SwiftPM's
  pluging sandbox)
* AST/PluginRepository: ASTContext independent plugin manager
* ASTGen/PluginHost: Communication with the plugin. Messages are
  serialized by ASTGen/LLVMJSON

rdar://101508815
2023-02-22 10:22:14 -08:00
swift-ci
f68647ac39 Merge pull request #63693 from DougGregor/trim-attr-name
Trim the attribute name when grabbing it from a syntax tree
2023-02-15 18:01:41 -08:00
Doug Gregor
9950ea06ef Trim the attribute name when grabbing it from a syntax tree 2023-02-15 12:46:59 -08:00
Rintaro Ishizaki
66aba1bc67 [ASTGen] Add Encoder/Decoder implementation backed by llvm::json::Value
Preparation for a future use.
2023-02-15 11:55:16 -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
3ddd25b4f1 [Macros] Fold the declaration node before expanding a peer macro. 2023-02-10 14:43:17 -08:00
Holly Borla
f04f512184 [Macros] Add a new macro role for attached peer macros. 2023-02-10 14:38:22 -08:00
Evan Wilde
c67e22e6aa Merge pull request #63192 from etcwilde/ewilde/fix-backdeploy-compat56-crash
Fixes for the compat56 library
2023-02-07 10:22:50 -08:00