Skip to content

Commit f25104f

Browse files
committed
[ASTGen] Generate MagicIdentifierLiteralExpr
1 parent 04b2174 commit f25104f

File tree

4 files changed

+89
-4
lines changed

4 files changed

+89
-4
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,6 +1765,23 @@ BridgedMacroExpansionExpr BridgedMacroExpansionExpr_createParsed(
17651765
BridgedSourceLoc cLeftAngleLoc, BridgedArrayRef cGenericArgs,
17661766
BridgedSourceLoc cRightAngleLoc, BridgedNullableArgumentList cArgList);
17671767

1768+
enum ENUM_EXTENSIBILITY_ATTR(open) BridgedMagicIdentifierLiteralKind : uint8_t {
1769+
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
1770+
BridgedMagicIdentifierLiteralKind##NAME,
1771+
#include "swift/AST/MagicIdentifierKinds.def"
1772+
BridgedMagicIdentifierLiteralKindNone,
1773+
};
1774+
1775+
SWIFT_NAME("BridgedMagicIdentifierLiteralKind.init(from:)")
1776+
BridgedMagicIdentifierLiteralKind
1777+
BridgedMagicIdentifierLiteralKind_fromString(BridgedStringRef cStr);
1778+
1779+
SWIFT_NAME("BridgedMagicIdentifierLiteralExpr.createParsed(_:kind:loc:)")
1780+
BridgedMagicIdentifierLiteralExpr
1781+
BridgedMagicIdentifierLiteralExpr_createParsed(
1782+
BridgedASTContext cContext, BridgedMagicIdentifierLiteralKind cKind,
1783+
BridgedSourceLoc cLoc);
1784+
17681785
SWIFT_NAME("BridgedNilLiteralExpr.createParsed(_:nilKeywordLoc:)")
17691786
BridgedNilLiteralExpr
17701787
BridgedNilLiteralExpr_createParsed(BridgedASTContext cContext,

lib/AST/Bridging/ExprBridging.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,36 @@ BridgedMacroExpansionExpr BridgedMacroExpansionExpr_createParsed(
381381
: getFreestandingMacroRoles());
382382
}
383383

384+
BridgedMagicIdentifierLiteralKind
385+
BridgedMagicIdentifierLiteralKind_fromString(BridgedStringRef cStr) {
386+
StringRef str = cStr.unbridged();
387+
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
388+
if (str == StringRef(STRING).drop_front()) \
389+
return BridgedMagicIdentifierLiteralKind##NAME;
390+
#include "swift/AST/MagicIdentifierKinds.def"
391+
return BridgedMagicIdentifierLiteralKindNone;
392+
}
393+
394+
static std::optional<MagicIdentifierLiteralExpr::Kind>
395+
unbridge(BridgedMagicIdentifierLiteralKind cKind) {
396+
switch (cKind) {
397+
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
398+
case BridgedMagicIdentifierLiteralKind##NAME: \
399+
return MagicIdentifierLiteralExpr::Kind::NAME;
400+
#include "swift/AST/MagicIdentifierKinds.def"
401+
case BridgedMagicIdentifierLiteralKindNone:
402+
return std::nullopt;
403+
}
404+
}
405+
406+
BridgedMagicIdentifierLiteralExpr
407+
BridgedMagicIdentifierLiteralExpr_createParsed(
408+
BridgedASTContext cContext, BridgedMagicIdentifierLiteralKind cKind,
409+
BridgedSourceLoc cLoc) {
410+
return new (cContext.unbridged())
411+
MagicIdentifierLiteralExpr(*unbridge(cKind), cLoc.unbridged());
412+
}
413+
384414
BridgedNilLiteralExpr
385415
BridgedNilLiteralExpr_createParsed(BridgedASTContext cContext,
386416
BridgedSourceLoc cNilKeywordLoc) {

lib/ASTGen/Sources/ASTGen/Exprs.swift

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ extension ASTGenVisitor {
7171
case .keyPathExpr(let node):
7272
return self.generate(keyPathExpr: node)
7373
case .macroExpansionExpr(let node):
74-
return self.generate(macroExpansionExpr: node).asExpr
74+
return self.generate(macroExpansionExpr: node)
7575
case .memberAccessExpr(let node):
7676
return self.generate(memberAccessExpr: node)
7777
case .missingExpr:
@@ -797,9 +797,43 @@ extension ASTGenVisitor {
797797
)
798798
}
799799

800-
func generate(macroExpansionExpr node: MacroExpansionExprSyntax) -> BridgedMacroExpansionExpr {
800+
func generateObjCSelectorExpr(macroExpansionExpr node: MacroExpansionExprSyntax) -> BridgedObjCSelectorExpr {
801+
fatalError("unimplemented")
802+
}
803+
804+
func generateObjCKeyPathExpr(macroExpansionExpr node: MacroExpansionExprSyntax) -> BridgedKeyPathExpr {
805+
fatalError("unimplemented")
806+
}
807+
808+
func generate(macroExpansionExpr node: MacroExpansionExprSyntax) -> BridgedExpr {
809+
// '#file', '#line' etc.
810+
let magicIdentifierKind = BridgedMagicIdentifierLiteralKind(from: node.macroName.rawText.bridged)
811+
if magicIdentifierKind != .none {
812+
guard node.lastToken(viewMode: .sourceAccurate) == node.macroName else {
813+
// TODO: Diagnose
814+
fatalError("magic identifier token with arguments")
815+
}
816+
817+
return BridgedMagicIdentifierLiteralExpr.createParsed(
818+
self.ctx,
819+
kind: magicIdentifierKind,
820+
loc: self.generateSourceLoc(node.macroName)
821+
).asExpr
822+
}
823+
824+
// Other built-in pound expressions.
825+
switch node.macroName.rawText {
826+
case "selector":
827+
return self.generateObjCSelectorExpr(macroExpansionExpr: node).asExpr
828+
case "keyPath":
829+
return self.generateObjCKeyPathExpr(macroExpansionExpr: node).asExpr
830+
default:
831+
// Fallback to MacroExpansionExpr.
832+
break
833+
}
834+
801835
let info = self.generate(freestandingMacroExpansion: node)
802-
return .createParsed(
836+
return BridgedMacroExpansionExpr.createParsed(
803837
self.declContext,
804838
poundLoc: info.poundLoc,
805839
macroNameRef: info.macroNameRef,
@@ -808,7 +842,7 @@ extension ASTGenVisitor {
808842
genericArgs: info.genericArgs,
809843
rightAngleLoc: info.rightAngleLoc,
810844
args: info.arguments
811-
)
845+
).asExpr
812846
}
813847

814848
func generate(memberAccessExpr node: MemberAccessExprSyntax, postfixIfConfigBaseExpr: BridgedExpr? = nil) -> BridgedExpr {

test/ASTGen/decls.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,7 @@ extension ValueStruct where 123 == N {}
299299
extension ValueStruct where N == -123 {}
300300
extension ValueStruct where -123 == N {}
301301

302+
func testMagicIdentifier(file: String = #file, line: Int = #line) {
303+
let _: String = file
304+
let _: Int = line
305+
}

0 commit comments

Comments
 (0)