Skip to content

Commit f588672

Browse files
authored
Merge pull request #62090 from DougGregor/macros-import
[Macros] Import owning and supplemental modules when processing macro signatures
2 parents 51fcde7 + 60eb0fb commit f588672

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

lib/Sema/TypeCheckMacros.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ static Optional<std::pair<GenericSignature, Type>>
102102
getMacroSignature(
103103
ModuleDecl *mod, Identifier macroName,
104104
Optional<StringRef> genericSignature,
105-
StringRef typeSignature
105+
StringRef typeSignature,
106+
ModuleDecl *owningModule,
107+
ArrayRef<ModuleDecl *> supplementalImportModules
106108
) {
107109
// Form a buffer containing the macro signature context.
108110
ASTContext &ctx = mod->getASTContext();
@@ -140,6 +142,21 @@ getMacroSignature(
140142
if (!decl)
141143
return None;
142144

145+
/// Add an import to the module.
146+
auto addImport = [&](ModuleDecl *importedModule) {
147+
ImportPath::Builder importPath;
148+
importPath.push_back(importedModule->getName(), SourceLoc());
149+
auto importDecl = ImportDecl::create(
150+
ctx, macroSourceFile, SourceLoc(), ImportKind::Module,
151+
SourceLoc(), importPath.get());
152+
importDecl->setImplicit();
153+
macroSourceFile->addTopLevelDecl(importDecl);
154+
};
155+
addImport(owningModule);
156+
std::for_each(supplementalImportModules.begin(),
157+
supplementalImportModules.end(),
158+
addImport);
159+
143160
// Make sure imports are resolved in this file.
144161
performImportResolution(*macroSourceFile);
145162

@@ -183,7 +200,8 @@ static MacroDecl *createMacro(
183200

184201
// Get the type signature of the macro.
185202
auto signature = getMacroSignature(
186-
mod, macroName, genericSignature, typeSignature);
203+
mod, macroName, genericSignature, typeSignature, owningModule,
204+
supplementalImportModules);
187205
if (!signature) {
188206
// FIXME: Swap in ErrorTypes, perhaps?
189207
return nullptr;

test/Macros/Inputs/ColorLib.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public struct MyColor: _ExpressibleByColorLiteral {
2+
public init(_colorLiteralRed red: Float, green: Float, blue: Float, alpha: Float) { }
3+
public init(_colorLiteralHue hue: Float, saturation: Float, value: Float) { }
4+
}

test/Macros/Inputs/macro_definition.swift

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ struct ColorLiteralMacro: _CompilerPlugin {
130130
}
131131

132132
static func _owningModule() -> (UnsafePointer<UInt8>, count: Int) {
133-
var swiftModule = "Swift"
133+
var swiftModule = "ColorLib"
134134
return swiftModule.withUTF8 { buffer in
135135
let result = UnsafeMutablePointer<UInt8>.allocate(capacity: buffer.count)
136136
result.initialize(from: buffer.baseAddress!, count: buffer.count)
@@ -191,20 +191,15 @@ struct HSVColorLiteralMacro: _CompilerPlugin {
191191
}
192192

193193
static func _genericSignature() -> (UnsafePointer<UInt8>?, count: Int) {
194-
var genSig = "<T>"
195-
return genSig.withUTF8 { buffer in
196-
let result = UnsafeMutablePointer<UInt8>.allocate(capacity: buffer.count)
197-
result.initialize(from: buffer.baseAddress!, count: buffer.count)
198-
return (UnsafePointer(result), count: buffer.count)
199-
}
194+
return (nil, count: 0)
200195
}
201196

202197
static func _typeSignature() -> (UnsafePointer<UInt8>, count: Int) {
203198
var typeSig =
204199
"""
205200
(
206201
hue hue: Float, saturation saturation: Float, value value: Float
207-
) -> T
202+
) -> MyColor
208203
"""
209204
return typeSig.withUTF8 { buffer in
210205
let result = UnsafeMutablePointer<UInt8>.allocate(capacity: buffer.count)
@@ -214,7 +209,7 @@ struct HSVColorLiteralMacro: _CompilerPlugin {
214209
}
215210

216211
static func _owningModule() -> (UnsafePointer<UInt8>, count: Int) {
217-
var swiftModule = "Swift"
212+
var swiftModule = "ColorLib"
218213
return swiftModule.withUTF8 { buffer in
219214
let result = UnsafeMutablePointer<UInt8>.allocate(capacity: buffer.count)
220215
result.initialize(from: buffer.baseAddress!, count: buffer.count)

test/Macros/macro_plugin.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
// RUN: %empty-directory(%t)
22
// RUN: %target-build-swift -Xfrontend -disable-availability-checking -I%platform-module-dir/../.. -L%platform-dylib-dir/../.. -emit-library -emit-library-path=%t/%target-library-name(MacroDefinition) -working-directory=%t -module-name=MacroDefinition %S/Inputs/macro_definition.swift
3-
// RUN: %target-swift-frontend -L%platform-dylib-dir/../.. -enable-experimental-feature Macros -load-plugin-library %t/%target-library-name(MacroDefinition) -disable-availability-checking -dump-ast -primary-file %s | %FileCheck %s
3+
// RUN: %target-swift-frontend -emit-module -o %t %S/Inputs/ColorLib.swift
4+
// RUN: %target-swift-frontend -L%platform-dylib-dir/../.. -enable-experimental-feature Macros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %t -disable-availability-checking -dump-ast -primary-file %s | %FileCheck %s
45

56
// FIXME: Swift parser is not enabled on Linux CI yet.
67
// REQUIRES: OS=macosx
78

89
// rdar://102160067
910
// UNSUPPORTED: CPU=arm64e
11+
import ColorLib
1012

1113
let _ = #customStringify(1.byteSwapped + 2.advanced(by: 10))
1214

@@ -35,11 +37,6 @@ let _ = #customStringify(["a", "b", "c"] + ["d", "e", "f"])
3537
// CHECK: (binary_expr type='[String]'
3638
// CHECK: (string_literal_expr type='String'
3739

38-
struct MyColor: _ExpressibleByColorLiteral {
39-
init(_colorLiteralRed red: Float, green: Float, blue: Float, alpha: Float) { }
40-
init(_colorLiteralHue hue: Float, saturation: Float, value: Float) { }
41-
}
42-
4340
// CHECK: (macro_expansion_expr type='MyColor' {{.*}} name=customColorLiteral
4441
let _: MyColor = #customColorLiteral(red: 0.5, green: 0.5, blue: 0.2, alpha: 0.9)
4542

0 commit comments

Comments
 (0)