Skip to content

Commit 4b87cb7

Browse files
committed
[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.
1 parent 36613dc commit 4b87cb7

File tree

7 files changed

+210
-5
lines changed

7 files changed

+210
-5
lines changed

include/swift/AST/CompilerPlugin.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ class CompilerPlugin {
5252
GenericSignature = 4,
5353
// static func _typeSignature(...) -> (UnsafePointer<UInt8>, count: Int)
5454
TypeSignature = 5,
55+
// static func _owningModule(...) -> (UnsafePointer<UInt8>, count: Int)
56+
OwningModule = 6,
57+
// static func _supplementalSignatureModules(...)
58+
// -> (UnsafePointer<UInt8>, count: Int)
59+
SupplementalSignatureModules = 7,
5560
};
5661

5762
/// The plugin type metadata.
@@ -100,6 +105,14 @@ class CompilerPlugin {
100105
/// result string buffer.
101106
StringRef invokeTypeSignature() const;
102107

108+
/// Invoke the `_owningModule` method. The caller assumes ownership of the
109+
/// result string buffer.
110+
StringRef invokeOwningModule() const;
111+
112+
/// Invoke the `_supplementalSignatureModules` method. The caller assumes
113+
/// ownership of the result string buffer.
114+
StringRef invokeSupplementalSignatureModules() const;
115+
103116
StringRef getName() const {
104117
return name;
105118
}

lib/AST/CompilerPlugin.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,3 +319,27 @@ StringRef CompilerPlugin::invokeTypeSignature() const {
319319
llvm_unreachable("Incompatible host compiler");
320320
#endif
321321
}
322+
323+
StringRef CompilerPlugin::invokeOwningModule() const {
324+
#if __clang__
325+
using Method = SWIFT_CC CharBuffer(
326+
SWIFT_CONTEXT const void *, const void *, const void *);
327+
auto method = getWitnessMethodUnsafe<Method>(
328+
WitnessTableEntry::OwningModule);
329+
return method(metadata, metadata, witnessTable).str();
330+
#else
331+
llvm_unreachable("Incompatible host compiler");
332+
#endif
333+
}
334+
335+
StringRef CompilerPlugin::invokeSupplementalSignatureModules() const {
336+
#if __clang__
337+
using Method = SWIFT_CC CharBuffer(
338+
SWIFT_CONTEXT const void *, const void *, const void *);
339+
auto method = getWitnessMethodUnsafe<Method>(
340+
WitnessTableEntry::SupplementalSignatureModules);
341+
return method(metadata, metadata, witnessTable).str();
342+
#else
343+
llvm_unreachable("Incompatible host compiler");
344+
#endif
345+
}

lib/ASTGen/Sources/ASTGen/Macros.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,48 @@ public func getMacroTypeSignature(
112112
}
113113
}
114114

115+
/// Query the documentation of the given macro.
116+
@_cdecl("swift_ASTGen_getMacroDocumentation")
117+
public func getMacroDocumentation(
118+
macroPtr: UnsafeMutablePointer<UInt8>,
119+
documentationPtr: UnsafeMutablePointer<UnsafePointer<UInt8>?>,
120+
documentationLengthPtr: UnsafeMutablePointer<Int>
121+
) {
122+
macroPtr.withMemoryRebound(to: ExportedMacro.self, capacity: 1) { macro in
123+
(documentationPtr.pointee, documentationLengthPtr.pointee) =
124+
allocateUTF8String(macro.pointee.macro.documentation)
125+
}
126+
}
127+
128+
/// Query the owning module of the given macro.
129+
@_cdecl("swift_ASTGen_getMacroOwningModule")
130+
public func getMacroOwningModule(
131+
macroPtr: UnsafeMutablePointer<UInt8>,
132+
owningModulePtr: UnsafeMutablePointer<UnsafePointer<UInt8>?>,
133+
owningModuleLengthPtr: UnsafeMutablePointer<Int>
134+
) {
135+
macroPtr.withMemoryRebound(to: ExportedMacro.self, capacity: 1) { macro in
136+
(owningModulePtr.pointee, owningModuleLengthPtr.pointee) =
137+
allocateUTF8String(macro.pointee.macro.owningModule)
138+
}
139+
}
140+
141+
/// Query the supplemental signature modules of the given macro,
142+
/// as a semicolon-separated string
143+
@_cdecl("swift_ASTGen_getMacroSupplementalSignatureModules")
144+
public func getMacroSupplementableSignatureModules(
145+
macroPtr: UnsafeMutablePointer<UInt8>,
146+
modulesPtr: UnsafeMutablePointer<UnsafePointer<UInt8>?>,
147+
modulesLengthPtr: UnsafeMutablePointer<Int>
148+
) {
149+
macroPtr.withMemoryRebound(to: ExportedMacro.self, capacity: 1) { macro in
150+
let modules = macro.pointee.macro.supplementalSignatureModules
151+
.joined(separator: ";")
152+
(modulesPtr.pointee, modulesLengthPtr.pointee) =
153+
allocateUTF8String(modules)
154+
}
155+
}
156+
115157
/// Query the macro evaluation context given the evaluation
116158
/// context sources.
117159
@_cdecl("swift_ASTGen_getMacroEvaluationContext")

lib/CompilerPluginSupport/CompilerPluginSupport.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,20 @@ public protocol _CompilerPlugin {
6969
/// - Returns: A newly allocated buffer containing the type signature. The
7070
/// caller is responsible for managing the memory.
7171
static func _typeSignature() -> (UnsafePointer<UInt8>, count: Int)
72+
73+
74+
/// Returns the module that owns this macro.
75+
///
76+
/// - Returns: A newly allocated buffer containing the owning module name. The
77+
/// caller is responsible for managing the memory.
78+
static func _owningModule() -> (UnsafePointer<UInt8>, count: Int)
79+
80+
/// Returns the set of modules that are needed (beyond the owning module) to
81+
/// process the module signature.
82+
///
83+
/// - Returns: A newly allocated buffer containing a string with all of the
84+
/// supplemental signature module names, separated by semicolons. The caller
85+
/// is responsible for managing the memory.
86+
static func _supplementalSignatureModules()
87+
-> (UnsafePointer<UInt8>, count: Int)
7288
}

lib/Sema/TypeCheckMacros.cpp

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ swift_ASTGen_getMacroTypeSignature(void *macro,
5454
const char **signaturePtr,
5555
ptrdiff_t *signatureLengthPtr);
5656

57+
extern "C" void
58+
swift_ASTGen_getMacroOwningModule(void *macro,
59+
const char **owningModuleNamePtr,
60+
ptrdiff_t *owningModuleNameLengthPtr);
61+
62+
extern "C" void
63+
swift_ASTGen_getMacroSupplementalSignatureModules(
64+
void *macro, const char **moduleNamesPtr, ptrdiff_t *moduleNamesLengthPtr);
65+
5766
/// Create a new macro signature context buffer describing the macro signature.
5867
///
5968
/// The macro signature is a user-defined generic signature and return
@@ -138,13 +147,39 @@ getMacroSignature(
138147
typealias->getGenericSignature(), typealias->getUnderlyingType());
139148
}
140149

150+
151+
141152
/// Create a macro.
142153
static Macro *createMacro(
143154
ModuleDecl *mod, Identifier macroName,
144155
Macro::ImplementationKind implKind,
145156
Optional<StringRef> genericSignature, StringRef typeSignature,
157+
StringRef owningModuleName,
158+
ArrayRef<StringRef> supplementalImportModuleNames,
146159
void* opaqueHandle
147160
) {
161+
ASTContext &ctx = mod->getASTContext();
162+
163+
// Look up the owning module.
164+
ModuleDecl *owningModule = ctx.getLoadedModule(
165+
ctx.getIdentifier(owningModuleName));
166+
if (!owningModule) {
167+
// FIXME: diagnostic here
168+
return nullptr;
169+
}
170+
// FIXME: Check that mod imports owningModule
171+
172+
// Look up all of the supplemental import modules.
173+
SmallVector<ModuleDecl *, 1> supplementalImportModules;
174+
for (auto supplementalModuleName : supplementalImportModuleNames) {
175+
auto supplementalModuleId = ctx.getIdentifier(supplementalModuleName);
176+
if (auto supplementalModule = ctx.getLoadedModule(supplementalModuleId)) {
177+
supplementalImportModules.push_back(supplementalModule);
178+
179+
// FIXME: Check that mod imports supplementalModule somewhere.
180+
}
181+
}
182+
148183
// Get the type signature of the macro.
149184
auto signature = getMacroSignature(
150185
mod, macroName, genericSignature, typeSignature);
@@ -154,18 +189,17 @@ static Macro *createMacro(
154189
}
155190

156191
// FIXME: All macros are expression macros right now
157-
ASTContext &ctx = mod->getASTContext();
158192
return new (ctx) Macro(
159193
Macro::Expression, implKind, macroName,
160194
signature->first, signature->second,
161-
/*FIXME:owningModule*/mod, /*FIXME:supplementalImportModules*/{},
195+
owningModule, supplementalImportModules,
162196
opaqueHandle);
163197
}
164198

165199
/// Create a builtin macro.
166200
static Macro *createBuiltinMacro(
167201
ModuleDecl *mod, Identifier macroName, void *opaqueHandle) {
168-
// Form the macro generic signature.
202+
// Get the macro generic signature.
169203
const char *genericSignaturePtr;
170204
ptrdiff_t genericSignatureLength;
171205
swift_ASTGen_getMacroGenericSignature(opaqueHandle, &genericSignaturePtr,
@@ -178,7 +212,7 @@ static Macro *createBuiltinMacro(
178212
if (genericSignaturePtr && genericSignatureLength)
179213
genericSignature = StringRef(genericSignaturePtr, genericSignatureLength);
180214

181-
// Form the macro type signature.
215+
// Get the macro type signature.
182216
const char *typeSignaturePtr;
183217
ptrdiff_t typeSignatureLength;
184218
swift_ASTGen_getMacroTypeSignature(opaqueHandle, &typeSignaturePtr,
@@ -188,9 +222,34 @@ static Macro *createBuiltinMacro(
188222
};
189223
StringRef typeSignature = StringRef(typeSignaturePtr, typeSignatureLength);
190224

225+
// Get the owning module name.
226+
const char *owningModuleNamePtr;
227+
ptrdiff_t owningModuleNameLength;
228+
swift_ASTGen_getMacroOwningModule(opaqueHandle, &owningModuleNamePtr,
229+
&owningModuleNameLength);
230+
SWIFT_DEFER {
231+
free((void*)owningModuleNamePtr);
232+
};
233+
StringRef owningModuleName = StringRef(
234+
owningModuleNamePtr, owningModuleNameLength);
235+
236+
// Get the supplemental signature module names.
237+
const char *supplementalModuleNamesPtr;
238+
ptrdiff_t supplementalModuleNamesLength;
239+
swift_ASTGen_getMacroSupplementalSignatureModules(
240+
opaqueHandle, &supplementalModuleNamesPtr,
241+
&supplementalModuleNamesLength);
242+
SWIFT_DEFER {
243+
free((void*)supplementalModuleNamesPtr);
244+
};
245+
SmallVector<StringRef, 2> supplementalModuleNames;
246+
StringRef(owningModuleNamePtr, owningModuleNameLength)
247+
.split(supplementalModuleNames, ";", -1, false);
248+
191249
return createMacro(
192250
mod, macroName, Macro::ImplementationKind::Builtin,
193251
genericSignature, typeSignature,
252+
owningModuleName, supplementalModuleNames,
194253
opaqueHandle);
195254
}
196255

@@ -208,9 +267,24 @@ static Macro *createPluginMacro(
208267
free((void*)typeSignature.data());
209268
};
210269

270+
auto owningModuleName = plugin->invokeOwningModule();
271+
SWIFT_DEFER {
272+
free((void*)owningModuleName.data());
273+
};
274+
275+
// Get the supplemental signature module names.
276+
auto supplementalModuleNamesStr =
277+
plugin->invokeSupplementalSignatureModules();
278+
SWIFT_DEFER {
279+
free((void*)supplementalModuleNamesStr.data());
280+
};
281+
SmallVector<StringRef, 2> supplementalModuleNames;
282+
supplementalModuleNamesStr
283+
.split(supplementalModuleNames, ";", -1, false);
284+
211285
return createMacro(
212286
mod, macroName, Macro::ImplementationKind::Plugin, genSignature,
213-
typeSignature, plugin);
287+
typeSignature, owningModuleName, supplementalModuleNames, plugin);
214288
}
215289

216290
ArrayRef<Macro *> MacroLookupRequest::evaluate(

test/Macros/Inputs/macro_definition.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,24 @@ struct StringifyMacro: _CompilerPlugin {
2828
}
2929
}
3030

31+
static func _owningModule() -> (UnsafePointer<UInt8>, count: Int) {
32+
var swiftModule = "Swift"
33+
return swiftModule.withUTF8 { buffer in
34+
let result = UnsafeMutablePointer<UInt8>.allocate(capacity: buffer.count)
35+
result.initialize(from: buffer.baseAddress!, count: buffer.count)
36+
return (UnsafePointer(result), count: buffer.count)
37+
}
38+
}
39+
40+
static func _supplementalSignatureModules() -> (UnsafePointer<UInt8>, count: Int) {
41+
var nothing = ""
42+
return nothing.withUTF8 { buffer in
43+
let result = UnsafeMutablePointer<UInt8>.allocate(capacity: buffer.count)
44+
result.initialize(from: buffer.baseAddress!, count: buffer.count)
45+
return (UnsafePointer(result), count: buffer.count)
46+
}
47+
}
48+
3149
static func _kind() -> _CompilerPluginKind {
3250
.expressionMacro
3351
}

test/Macros/Inputs/macro_definition_missing_allmacros.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,24 @@ struct DummyMacro: _CompilerPlugin {
2929
}
3030
}
3131

32+
static func _owningModule() -> (UnsafePointer<UInt8>, count: Int) {
33+
var swiftModule = "Swift"
34+
return swiftModule.withUTF8 { buffer in
35+
let result = UnsafeMutablePointer<UInt8>.allocate(capacity: buffer.count)
36+
result.initialize(from: buffer.baseAddress!, count: buffer.count)
37+
return (UnsafePointer(result), count: buffer.count)
38+
}
39+
}
40+
41+
static func _supplementalSignatureModules() -> (UnsafePointer<UInt8>, count: Int) {
42+
var nothing = ""
43+
return nothing.withUTF8 { buffer in
44+
let result = UnsafeMutablePointer<UInt8>.allocate(capacity: buffer.count)
45+
result.initialize(from: buffer.baseAddress!, count: buffer.count)
46+
return (UnsafePointer(result), count: buffer.count)
47+
}
48+
}
49+
3250
static func _kind() -> _CompilerPluginKind {
3351
.expressionMacro
3452
}

0 commit comments

Comments
 (0)