Commit Graph

740 Commits

Author SHA1 Message Date
Doug Gregor
f0fc1c51bb Remove compiler plugin support library
The functionality of the compiler plugin support library has been
subsumed into parts of the compiler. Remove the functionality and its
last test.
2022-12-05 12:54:36 -08:00
Doug Gregor
14aebcab1b Bridge swift-syntax diagnostics to the C++ diagnostic engine's diagnostics.
Implement an ASTGen operation to bridge swift-syntax diagnostics, as
produced by the parser, operator folding, and macros, over to the C++
diagnostic engine infrastructure. Use this to wire up macro expansion
diagnostics.
2022-12-05 11:49:42 -08:00
Doug Gregor
728ba4eada [Macros] Adapt to rename of ExpressionMacro.expansion(of:in:). 2022-12-05 09:34:54 -08:00
Doug Gregor
bc06bb08ba Submit the macros "builtin" tests 2022-12-02 22:43:49 -08:00
Doug Gregor
c9573309a6 Subsume two _CompilerPluginSupport-based tests into macro plugin tests 2022-12-02 16:45:39 -08:00
Doug Gregor
5bd94d680e [Macros] Adapt to macro API changes and switch more testing to use it.
The API provided by macro evaluation has changed a bit. Adapt ASTGen to
the new API, and use this as an opportunity to start moving more tests
to using the shared libraries built into the toolchain.
2022-12-02 16:35:14 -08:00
Doug Gregor
811595394f [Macros] Build and test a macro plugin based on SwiftSyntax 2022-12-01 23:20:56 -08:00
Doug Gregor
9c5357e82c [Macros] Fix path to host libraries in macro tests 2022-11-30 13:54:57 -08:00
Doug Gregor
a9ee5dd142 [Macros] Don't allow local names to shadow macro names. 2022-11-28 18:33:10 -08:00
Doug Gregor
1a10009cae [Macro] Diagnose macros declared in nested scopes 2022-11-28 18:33:10 -08:00
Doug Gregor
ab9608cd78 [Macros] Add tests for redeclarations of macros 2022-11-28 18:33:10 -08:00
Doug Gregor
e2c66cb67d [Macros] Check access control and availability 2022-11-28 18:33:10 -08:00
Doug Gregor
4f823c60be [Macros] Ensure that macros cannot return opaque result types 2022-11-28 18:33:10 -08:00
Doug Gregor
3c80b714bf [Macros] Fix end location of macro declarations with where clauses 2022-11-28 18:33:10 -08:00
Doug Gregor
e67c546a77 [Macros] Resolve in-process macros via external type name.
This approach works for both macros built into the compiler (e.g., the
builtin macros) as well as those that are loaded via plugin but don't
conform to the _CompilerPluginSupport protocol.

This eliminates all uses of the `MacroSystem` itself in ASTGen, and
pushes more of the implementation through ASTGen.
2022-11-28 18:32:43 -08:00
Doug Gregor
813dc12ad3 [Macros] Use the external module/type name in lieu of macro registration.
A macro declaration contains the external module and type name of the
macro's implementation. Use that information to find the macro type
(via its type metadata accessor) in a loaded plugin, so we no longer
require the "allMacros" array. Instead, each macro implementation type
must be a public struct.

Since we are now fully dependent on the macro declaration for
everything about a macro except its kind, remove most of the query
infrastructure for compiler plugins.

Replace the macro registration scheme based on the allMacros array with
2022-11-28 18:32:43 -08:00
Doug Gregor
b29fcb4e58 [Macros] Parse macro declarations fully, and treat them as normal declarations 2022-11-28 18:32:43 -08:00
Doug Gregor
8adee85cb6 [Macros] Add support for explicit generic arguments of macros.
Enable type checking support for explicitly specifying generic arguments to
a macro, e.g., `#stringify<Double>(1 + 2)`. To do so, introduce a new
kind of constraint that performs explicit argument matching against the
generic parameters of a macro only after the overload is chosen.
2022-11-28 18:32:43 -08:00
Doug Gregor
787af41765 [Macros] Parse generic arguments in macro expansions. 2022-11-17 16:46:43 -08:00
Richard Wei
1930f86a1a [Macros] Emit diagnostics from plugins
Make `_rewrite` return an buffer containing diagnostics and emit them.
2022-11-17 03:17:57 -08:00
Doug Gregor
34b62080b6 [Macro tests] Pass appropriate linker flags for macro plugin tests. 2022-11-15 19:32:20 -08:00
Doug Gregor
66b7031843 [Macros] Use appropriate substitutions in tests 2022-11-15 19:32:20 -08:00
Doug Gregor
60eb0fb3f5 [Macros] Add missing test file 2022-11-13 20:47:17 -08:00
Doug Gregor
25d324fb20 [Macros] Import owning and supplemental modules when processing macro signature.
The macro signature can depend on both the owning module and any
supplemental signature modules. Create import declarations for each of these
prior to import resolution of the macro signature buffer.
2022-11-13 19:09:10 -08:00
Doug Gregor
19d1588d13 [Macros] Handle macro overloading.
Allow more than one macro plugin to introduce a macro with the same
name, and let the constraint solver figure out which one to call. Also
eliminates a potential use-after-free if we somehow find additional
compiler plugins to load after having expanded a macro.
2022-11-13 16:29:48 -08:00
Doug Gregor
bdf7762f55 [Macros] Start threading argument labels through macros. 2022-11-13 14:38:07 -08:00
Doug Gregor
4b87cb7b14 [Macros] Plumb the owning module and supplemental modules through to Macro.
Plumb the information about the owning module and supplemental
signature modules through to the Macro data structure, for both
built-in and plugin macros.

We're resolving the given module names into module declarations, but
otherwise performing no checking and not emitting any diagnostics.
This information is not yet used.
2022-11-11 15:24:37 -08:00
Allan Shortlidge
1b4b7f0d60 Test: Temporarily disable some Macros tests for arm64e. 2022-11-10 18:25:07 -08:00
swift-ci
4b23a9f990 Merge pull request #61961 from rxwei/fix-macro
[Macros] Fix memory leak in `expandMacroExpr`
2022-11-07 22:36:25 -08:00
Doug Gregor
0b78b9a0b0 [Macros] Ensure that macro expansion parsing has appropriate context.
The parser needs context to do completely reasonable things like... parse
closures.
2022-11-07 16:34:18 -08:00
Richard Wei
331372872c [Macros] Fix memory leak in expandMacroExpr
The result of `swift_ASTGen_lookupMacro` needs to be deallocated.
2022-11-07 15:31:43 -08:00
Andrew Trick
9daffe0d73 macro_plugin tests requires asserts
Experimental features require an asserts build.

Fixes rdar://101790243 (Swift CI: experimental feature 'Macros'
cannot be enabled in a production compiler!
2022-11-04 17:22:24 -07:00
Richard Wei
d059735260 [Macros] Type check user-defined macro plugins (#61861)
Type check user-defined macros plugins with user-provided type signatures.

Also, load plugin libraries with `RTLD_LOCAL` instead of `RTLD_GLOBAL` to prevent symbol collision between plugins. `llvm::sys::DynamicLibrary` only supports `RTLD_GLOBAL` so we use the plain `dlopen` instead. This does not work on Windows and needs to be fixed.

Friend PR: apple/swift-syntax#1042
2022-11-02 18:22:31 -07:00
Doug Gregor
b129d7ec60 [Macros] Teach name lookup in macro expansions to look in expansion context
Teach ASTScope how to reason about macro expansions. When we create an
ASTScope for a source file that represents a macro expansion, its
parent scope node is the macro expansion itself. When performing
unqualified lookup (of any form), find the starting source file based
on the location, not on the source file provided---this ensures that
we start lookups within the macro expansion (for example).

The effect of this is to enable macro expansions to work in nested
contexts, where they refer to names in the scope in which the macro is
expanded.

This is decidedly unhiegenic, but it fits with our syntactic model.
2022-11-01 08:04:16 -07:00
Richard Wei
4ce1ebb120 [Macros] Support user-defined macros as compiler plugins (#61734)
Allow user-defined macros to be loaded from dynamic libraries and evaluated.

- Introduce a _CompilerPluginSupport module installed into the toolchain. Its `_CompilerPlugin` protocol acts as a stable interface between the compiler and user-defined macros.
- Introduce a `-load-plugin-library <path>` attribute which allows users to specify dynamic libraries to be loaded into the compiler.

A macro library must declare a public top-level computed property `public var allMacros: [Any.Type]` and be compiled to a dynamic library. The compiler will call the getter of this property to obtain and register all macros.

Known issues:
- We current do not have a way to strip out unnecessary symbols from the plugin dylib, i.e. produce a plugin library that does not contain SwiftSyntax symbols that will collide with the compiler itself.
- `MacroExpansionExpr`'s type is hard-coded as `(Int, String)`. It should instead be specified by the macro via protocol requirements such as `signature` and `genericSignature`. We need more protocol requirements in `_CompilerPlugin` to handle this.
- `dlopen` is not secure and is only for prototyping use here.

Friend PR: apple/swift-syntax#1022
2022-10-31 14:03:25 -07:00
Doug Gregor
d6d2318dc9 [Macros] Handle macro expansion for function-like macros. 2022-10-27 17:15:30 -07:00
Doug Gregor
c1e8a0c3d9 Fix line number in test 2022-10-24 11:23:36 -07:00
Doug Gregor
e4d51be1a2 Enable this test only when assertions are available.
Experimental features are generally not available in non-asserts build,
so narrow this test. Fixes rdar://101494565.
2022-10-24 10:12:42 -07:00
Doug Gregor
414ef860a6 [Macros] Type-check and use the result of macro-expanding an expression macro.
Once we have expanded an expression macro, parse and type-check the
result given a priori knowledge of the expanded type. Then, create an
implicit macro-expansion expression to capture the result of the
rewrite.
2022-10-23 15:36:17 -07:00
Doug Gregor
636e6d1524 [Macros] "Expand" builtin macros for magic literals.
Introduce an experimental option `BuiltinMacros` that takes the magic
literals (`#file`, `#line`, `#function`, etc.) after type checking and
processes the original source for the expression using the build
syntactic macro system in the swift-syntax library. At present, the
result of expansion is printed to standard output, but it's enough to
verify that we're able to find the corresponding syntax node based on
the C++ AST.
2022-10-21 06:41:05 -07:00