|
| 1 | +//===--- CompilerPlugin.h - Compiler Plugin Support -------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// This file defines supporting data structures for compiler plugins. |
| 14 | +// |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +#include "swift/AST/Type.h" |
| 18 | +#include "swift/Basic/LLVM.h" |
| 19 | +#include "swift/Basic/StringExtras.h" |
| 20 | +#include "llvm/Support/DynamicLibrary.h" |
| 21 | + |
| 22 | +#ifndef SWIFT_AST_COMPILER_PLUGIN_H |
| 23 | +#define SWIFT_AST_COMPILER_PLUGIN_H |
| 24 | + |
| 25 | +namespace swift { |
| 26 | + |
| 27 | +class ASTContext; |
| 28 | + |
| 29 | +/// A compiler plugin that corresponds to a dynamically loaded Swift type that |
| 30 | +/// conforms to the `_CompilerPluginSupport._CompilerPlugin` protocol. |
| 31 | +class CompilerPlugin { |
| 32 | + friend class ASTContext; |
| 33 | + |
| 34 | +public: |
| 35 | + // Must be modified together with `_CompilerPluginKind` in |
| 36 | + // stdlib/toolchain/CompilerPluginSupport.swift. |
| 37 | + enum class Kind: uint32_t { |
| 38 | + ExpressionMacro, |
| 39 | + }; |
| 40 | + |
| 41 | +private: |
| 42 | + // Must be modified together with `_CompilerPlugin` in |
| 43 | + // stdlib/toolchain/CompilerPluginSupport.swift. |
| 44 | + enum class WitnessTableEntry: unsigned { |
| 45 | + ConformanceDescriptor = 0, |
| 46 | + // static func _name() -> (UnsafePointer<UInt8>, count: Int8) |
| 47 | + Name = 1, |
| 48 | + // static func _kind() -> _CompilerPluginKind |
| 49 | + Kind = 2, |
| 50 | + // static func _rewrite(...) -> (UnsafePointer<UInt8>?, count: Int) |
| 51 | + Rewrite = 3, |
| 52 | + }; |
| 53 | + |
| 54 | + /// The plugin type metadata. |
| 55 | + const void *metadata; |
| 56 | + /// The parent dynamic library containing the plugin. |
| 57 | + llvm::sys::DynamicLibrary parentLibrary; |
| 58 | + /// The witness table proving that the plugin conforms to `_CompilerPlugin`. |
| 59 | + const void *witnessTable; |
| 60 | + /// The plugin name, aka. result of the `_name()` method. |
| 61 | + StringRef name; |
| 62 | + /// The plugin's kind, aka. result of the `_kind()` method. |
| 63 | + Kind kind; |
| 64 | + |
| 65 | + template<typename Func> |
| 66 | + const Func *getWitnessMethodUnsafe(WitnessTableEntry entry) const { |
| 67 | + return reinterpret_cast<const Func *const *>(witnessTable)[(unsigned)entry]; |
| 68 | + } |
| 69 | + |
| 70 | +protected: |
| 71 | + CompilerPlugin(const void *metadata, llvm::sys::DynamicLibrary parentLibrary, |
| 72 | + ASTContext &ctx); |
| 73 | + |
| 74 | +private: |
| 75 | + /// Invoke the `_name` method. The caller assumes ownership of the result |
| 76 | + /// string buffer. |
| 77 | + StringRef invokeName() const; |
| 78 | + |
| 79 | + /// Invoke the `_kind` method. |
| 80 | + Kind invokeKind() const; |
| 81 | + |
| 82 | +public: |
| 83 | + /// Invoke the `_rewrite` method. Invoke the `_name` method. The caller |
| 84 | + /// assumes ownership of the result string buffer. |
| 85 | + Optional<NullTerminatedStringRef> invokeRewrite( |
| 86 | + StringRef targetModuleName, StringRef filePath, StringRef sourceFileText, |
| 87 | + CharSourceRange range, ASTContext &ctx) const; |
| 88 | + |
| 89 | + StringRef getName() const { |
| 90 | + return name; |
| 91 | + } |
| 92 | + |
| 93 | + Kind getKind() const { |
| 94 | + return kind; |
| 95 | + } |
| 96 | +}; |
| 97 | + |
| 98 | +} // end namespace swift |
| 99 | + |
| 100 | +#endif // SWIFT_AST_COMPILER_PLUGIN_H |
0 commit comments