Skip to content

Commit bdf7762

Browse files
committed
[Macros] Start threading argument labels through macros.
1 parent 1aa9075 commit bdf7762

File tree

7 files changed

+116
-11
lines changed

7 files changed

+116
-11
lines changed

lib/Parse/ParseType.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "swift/AST/ASTWalker.h"
1919
#include "swift/AST/Attr.h"
2020
#include "swift/AST/GenericParamList.h"
21+
#include "swift/AST/SourceFile.h" // only for isMacroSignatureFile
2122
#include "swift/AST/TypeRepr.h"
2223
#include "swift/Parse/Lexer.h"
2324
#include "swift/Parse/CodeCompletionCallbacks.h"
@@ -1067,6 +1068,11 @@ ParserResult<TypeRepr> Parser::parseOldStyleProtocolComposition() {
10671068
return makeParserResult(Status, composition);
10681069
}
10691070

1071+
/// FIXME: This is an egregious hack.
1072+
static bool isMacroSignatureFile(SourceFile &sf) {
1073+
return sf.getFilename().startswith("Macro signature of");
1074+
}
1075+
10701076
/// parseTypeTupleBody
10711077
/// type-tuple:
10721078
/// '(' type-tuple-body? ')'
@@ -1209,7 +1215,8 @@ ParserResult<TypeRepr> Parser::parseTypeTupleBody() {
12091215

12101216
// If there was a first name, complain; arguments in function types are
12111217
// always unlabeled.
1212-
if (element.NameLoc.isValid() && !element.Name.empty()) {
1218+
if (element.NameLoc.isValid() && !element.Name.empty() &&
1219+
/*FIXME: Gross hack*/!isMacroSignatureFile(SF)) {
12131220
auto diag = diagnose(element.NameLoc, diag::function_type_argument_label,
12141221
element.Name);
12151222
if (element.SecondNameLoc.isInvalid())

lib/Sema/CSApply.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2982,9 +2982,6 @@ namespace {
29822982
auto overload = solution.getOverloadChoice(locator);
29832983

29842984
auto macro = cast<MacroDecl>(overload.choice.getDecl());
2985-
// FIXME: use the macro for expansion!
2986-
// FIXME: form a proper ConcreteDeclRef for the macro expansion!
2987-
29882985
ConcreteDeclRef macroRef = resolveConcreteDeclRef(macro, locator);
29892986
if (auto newExpr = expandMacroExpr(dc, expr, macroRef, expandedType)) {
29902987
auto expansion = new (ctx) MacroExpansionExpr(

lib/Sema/CSSimplify.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ static bool areConservativelyCompatibleArgumentLabels(
178178
if (!varTy->mayBeCallable(cs.DC))
179179
return false;
180180
fnType = varTy->getAs<AnyFunctionType>();
181+
} else if (auto *MD = dyn_cast<MacroDecl>(decl)) {
182+
fnType = MD->getInterfaceType()->getAs<AnyFunctionType>();
181183
}
182184

183185
// Given we want to be conservative with this checking, if there's any case

lib/Sema/ConstraintSystem.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,10 +1610,6 @@ ConstraintSystem::getTypeOfReference(ValueDecl *value,
16101610
if (auto macro = dyn_cast<MacroDecl>(value)) {
16111611
Type macroType = macro->getInterfaceType();
16121612

1613-
// Remove argument labels when we have a function-like macro.
1614-
if (isa<AnyFunctionType>(macroType.getPointer()))
1615-
macroType = macroType->removeArgumentLabels(1);
1616-
16171613
// Open any the generic types.
16181614
OpenedTypeMap replacements;
16191615
openGeneric(macro->getParentModule(), macro->getGenericSignature(),

lib/Sema/TypeCheckType.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3235,11 +3235,19 @@ TypeResolver::resolveASTFunctionTypeParams(TupleTypeRepr *inputRepr,
32353235
}
32363236
}
32373237

3238+
Identifier argumentLabel;
3239+
Identifier parameterName;
3240+
if (inputRepr->getElement(i).SecondNameLoc.isValid()) {
3241+
argumentLabel = inputRepr->getElement(i).Name;
3242+
parameterName = inputRepr->getElement(i).SecondName;
3243+
} else {
3244+
parameterName = inputRepr->getElementName(i);
3245+
}
3246+
32383247
auto paramFlags = ParameterTypeFlags::fromParameterType(
32393248
ty, variadic, autoclosure, /*isNonEphemeral*/ false, ownership,
32403249
isolated, noDerivative, compileTimeConst);
3241-
elements.emplace_back(ty, Identifier(), paramFlags,
3242-
inputRepr->getElementName(i));
3250+
elements.emplace_back(ty, argumentLabel, paramFlags, parameterName);
32433251
}
32443252

32453253
return elements;

test/Macros/Inputs/macro_definition.swift

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,94 @@ struct StringifyMacro: _CompilerPlugin {
9696
}
9797
}
9898

99+
struct ColorLiteralMacro: _CompilerPlugin {
100+
static func _name() -> (UnsafePointer<UInt8>, count: Int) {
101+
var name = "customColorLiteral"
102+
return name.withUTF8 { buffer in
103+
let result = UnsafeMutablePointer<UInt8>.allocate(capacity: buffer.count)
104+
result.initialize(from: buffer.baseAddress!, count: buffer.count)
105+
return (UnsafePointer(result), count: buffer.count)
106+
}
107+
}
108+
109+
static func _genericSignature() -> (UnsafePointer<UInt8>?, count: Int) {
110+
var genSig = "<T: _ExpressibleByColorLiteral>"
111+
return genSig.withUTF8 { buffer in
112+
let result = UnsafeMutablePointer<UInt8>.allocate(capacity: buffer.count)
113+
result.initialize(from: buffer.baseAddress!, count: buffer.count)
114+
return (UnsafePointer(result), count: buffer.count)
115+
}
116+
}
117+
118+
static func _typeSignature() -> (UnsafePointer<UInt8>, count: Int) {
119+
var typeSig =
120+
"""
121+
(
122+
red red: Float, green green: Float, blue blue: Float, alpha alpha: Float
123+
) -> T
124+
"""
125+
return typeSig.withUTF8 { buffer in
126+
let result = UnsafeMutablePointer<UInt8>.allocate(capacity: buffer.count)
127+
result.initialize(from: buffer.baseAddress!, count: buffer.count)
128+
return (UnsafePointer(result), count: buffer.count)
129+
}
130+
}
131+
132+
static func _owningModule() -> (UnsafePointer<UInt8>, count: Int) {
133+
var swiftModule = "Swift"
134+
return swiftModule.withUTF8 { buffer in
135+
let result = UnsafeMutablePointer<UInt8>.allocate(capacity: buffer.count)
136+
result.initialize(from: buffer.baseAddress!, count: buffer.count)
137+
return (UnsafePointer(result), count: buffer.count)
138+
}
139+
}
140+
141+
static func _supplementalSignatureModules() -> (UnsafePointer<UInt8>, count: Int) {
142+
var nothing = ""
143+
return nothing.withUTF8 { buffer in
144+
let result = UnsafeMutablePointer<UInt8>.allocate(capacity: buffer.count)
145+
result.initialize(from: buffer.baseAddress!, count: buffer.count)
146+
return (UnsafePointer(result), count: buffer.count)
147+
}
148+
}
149+
150+
static func _kind() -> _CompilerPluginKind {
151+
.expressionMacro
152+
}
153+
154+
static func _rewrite(
155+
targetModuleName: UnsafePointer<UInt8>,
156+
targetModuleNameCount: Int,
157+
filePath: UnsafePointer<UInt8>,
158+
filePathCount: Int,
159+
sourceFileText: UnsafePointer<UInt8>,
160+
sourceFileTextCount: Int,
161+
localSourceText: UnsafePointer<UInt8>,
162+
localSourceTextCount: Int
163+
) -> (UnsafePointer<UInt8>?, count: Int) {
164+
let meeTextBuffer = UnsafeBufferPointer(
165+
start: localSourceText, count: localSourceTextCount)
166+
let meeText = String(decoding: meeTextBuffer, as: UTF8.self)
167+
let prefix = "#customColorLiteral(red:"
168+
guard meeText.starts(with: prefix), meeText.last == ")" else {
169+
return (nil, 0)
170+
}
171+
let expr = meeText.dropFirst(prefix.count).dropLast()
172+
var resultString = ".init(_colorLiteralRed:\(expr))"
173+
return resultString.withUTF8 { buffer in
174+
let result = UnsafeMutableBufferPointer<UInt8>.allocate(
175+
capacity: buffer.count + 1)
176+
_ = result.initialize(from: buffer)
177+
result[buffer.count] = 0
178+
return (UnsafePointer(result.baseAddress), buffer.count)
179+
}
180+
}
181+
}
182+
183+
99184
public var allMacros: [Any.Type] {
100-
[StringifyMacro.self]
185+
[
186+
StringifyMacro.self,
187+
ColorLiteralMacro.self
188+
]
101189
}

test/Macros/macro_plugin.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,10 @@ let _ = #customStringify(["a", "b", "c"] + ["d", "e", "f"])
3434
// CHECK: (tuple_expr type='([String], String)'
3535
// CHECK: (binary_expr type='[String]'
3636
// CHECK: (string_literal_expr type='String'
37+
38+
struct MyColor: _ExpressibleByColorLiteral {
39+
init(_colorLiteralRed red: Float, green: Float, blue: Float, alpha: Float) { }
40+
}
41+
42+
let _: MyColor = #customColorLiteral(red: 0.5, green: 0.5, blue: 0.2, alpha: 0.9)
43+

0 commit comments

Comments
 (0)