Files
swift-mirror/lib/CompilerPluginSupport/CompilerPluginSupport.swift
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

61 lines
2.0 KiB
Swift

//===--- CompilerPlugin.swift - Compiler plugin library support -----------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// Do not modify without modifying swift::CompilerPlugin::Kind in
// include/swift/AST/CompilerPlugin.h.
@frozen
public struct _CompilerPluginKind {
@usableFromInline
enum RawValue: UInt32 {
case expressionMacro
}
@usableFromInline var rawValue: RawValue
public static var expressionMacro: Self {
Self(rawValue: .expressionMacro)
}
}
/// A compiler plugin that can be loaded by the Swift compiler for code rewrite
/// and custom compilation.
public protocol _CompilerPlugin {
// Note:
// - Do not modify the protocol layout without updating
// `CompilerPlugin::WitnessTableEntry`.
// - Use C-compatible types and tuples thereof to ensure that we can invoke
// these methods from C.
static func _name() -> (UnsafePointer<UInt8>, count: Int)
static func _kind() -> _CompilerPluginKind
/// Returns new source code by transforming the given source code, or nil if
/// there's no transform.
///
/// - Parameters:
/// - code: A buffer containing the source code in UTF-8.
/// - context: The compiler context.
/// - Returns: A newly allocated, null-terminated buffer containing the
/// transformed source code. The caller is responsible for managing the
/// memory.
static func _rewrite(
targetModuleName: UnsafePointer<UInt8>,
targetModuleNameCount: Int,
filePath: UnsafePointer<UInt8>,
filePathCount: Int,
sourceFileText: UnsafePointer<UInt8>,
sourceFileTextCount: Int,
localSourceText: UnsafePointer<UInt8>,
localSourceTextCount: Int
) -> (UnsafePointer<UInt8>?, count: Int)
}