Skip to content

Commit 73164a0

Browse files
authored
Merge pull request #79353 from rintaro/astgen-magic-ident-literal
[ASTGen] Generate MagicIdentifierLiteralExpr
2 parents 1d509c7 + 563ddc4 commit 73164a0

File tree

18 files changed

+145
-58
lines changed

18 files changed

+145
-58
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,6 +1747,23 @@ BridgedMacroExpansionExpr BridgedMacroExpansionExpr_createParsed(
17471747
BridgedSourceLoc cLeftAngleLoc, BridgedArrayRef cGenericArgs,
17481748
BridgedSourceLoc cRightAngleLoc, BridgedNullableArgumentList cArgList);
17491749

1750+
enum ENUM_EXTENSIBILITY_ATTR(open) BridgedMagicIdentifierLiteralKind : uint8_t {
1751+
#define MAGIC_IDENTIFIER(NAME, STRING) \
1752+
BridgedMagicIdentifierLiteralKind##NAME,
1753+
#include "swift/AST/MagicIdentifierKinds.def"
1754+
BridgedMagicIdentifierLiteralKindNone,
1755+
};
1756+
1757+
SWIFT_NAME("BridgedMagicIdentifierLiteralKind.init(from:)")
1758+
BridgedMagicIdentifierLiteralKind
1759+
BridgedMagicIdentifierLiteralKind_fromString(BridgedStringRef cStr);
1760+
1761+
SWIFT_NAME("BridgedMagicIdentifierLiteralExpr.createParsed(_:kind:loc:)")
1762+
BridgedMagicIdentifierLiteralExpr
1763+
BridgedMagicIdentifierLiteralExpr_createParsed(
1764+
BridgedASTContext cContext, BridgedMagicIdentifierLiteralKind cKind,
1765+
BridgedSourceLoc cLoc);
1766+
17501767
SWIFT_NAME("BridgedNilLiteralExpr.createParsed(_:nilKeywordLoc:)")
17511768
BridgedNilLiteralExpr
17521769
BridgedNilLiteralExpr_createParsed(BridgedASTContext cContext,

include/swift/AST/DefaultArgumentKind.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ enum class DefaultArgumentKind : uint8_t {
4747
/// property's initializer.
4848
StoredProperty,
4949
// Magic identifier literals expanded at the call site:
50-
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) NAME,
50+
#define MAGIC_IDENTIFIER(NAME, STRING) NAME,
5151
#include "swift/AST/MagicIdentifierKinds.def"
5252
/// An expression macro.
5353
ExpressionMacro

include/swift/AST/Expr.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,13 +1085,15 @@ class RegexLiteralExpr : public LiteralExpr {
10851085
class MagicIdentifierLiteralExpr : public BuiltinLiteralExpr {
10861086
public:
10871087
enum Kind : unsigned {
1088-
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) NAME,
1088+
#define MAGIC_IDENTIFIER(NAME, STRING) NAME,
10891089
#include "swift/AST/MagicIdentifierKinds.def"
10901090
};
10911091

10921092
static StringRef getKindString(MagicIdentifierLiteralExpr::Kind value) {
10931093
switch (value) {
1094-
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) case NAME: return STRING;
1094+
#define MAGIC_IDENTIFIER(NAME, STRING) \
1095+
case NAME: \
1096+
return STRING;
10951097
#include "swift/AST/MagicIdentifierKinds.def"
10961098
}
10971099

@@ -1116,11 +1118,11 @@ class MagicIdentifierLiteralExpr : public BuiltinLiteralExpr {
11161118

11171119
bool isString() const {
11181120
switch (getKind()) {
1119-
#define MAGIC_STRING_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
1120-
case NAME: \
1121+
#define MAGIC_STRING_IDENTIFIER(NAME, STRING) \
1122+
case NAME: \
11211123
return true;
1122-
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
1123-
case NAME: \
1124+
#define MAGIC_IDENTIFIER(NAME, STRING) \
1125+
case NAME: \
11241126
return false;
11251127
#include "swift/AST/MagicIdentifierKinds.def"
11261128
}

include/swift/AST/MagicIdentifierKinds.def

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@
1717

1818
// Used for any magic identifier.
1919
#ifndef MAGIC_IDENTIFIER
20-
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND)
20+
#define MAGIC_IDENTIFIER(NAME, STRING)
2121
#endif
2222

2323
// Used for magic identifiers which produce string literals.
2424
#ifndef MAGIC_STRING_IDENTIFIER
25-
#define MAGIC_STRING_IDENTIFIER(NAME, STRING, SYNTAX_KIND) MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND)
25+
#define MAGIC_STRING_IDENTIFIER(NAME, STRING) MAGIC_IDENTIFIER(NAME, STRING)
2626
#endif
2727

2828
// Used for magic identifiers which produce integer literals.
2929
#ifndef MAGIC_INT_IDENTIFIER
30-
#define MAGIC_INT_IDENTIFIER(NAME, STRING, SYNTAX_KIND) MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND)
30+
#define MAGIC_INT_IDENTIFIER(NAME, STRING) MAGIC_IDENTIFIER(NAME, STRING)
3131
#endif
3232

3333
// Used for magic identifiers which produce raw pointers.
3434
#ifndef MAGIC_POINTER_IDENTIFIER
35-
#define MAGIC_POINTER_IDENTIFIER(NAME, STRING, SYNTAX_KIND) MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND)
35+
#define MAGIC_POINTER_IDENTIFIER(NAME, STRING) MAGIC_IDENTIFIER(NAME, STRING)
3636
#endif
3737

3838
// Used when a given token always maps to a particular magic identifier kind.
@@ -45,27 +45,27 @@
4545
//
4646

4747
/// The \c #fileID magic identifier literal.
48-
MAGIC_STRING_IDENTIFIER(FileID, "#fileID", PoundFileIDExpr)
48+
MAGIC_STRING_IDENTIFIER(FileID, "#fileID")
4949
MAGIC_IDENTIFIER_TOKEN(FileID, pound_fileID)
5050

5151
/// The \c #file magic identifier literal, written in code where it is
5252
/// a synonym for \c #fileID (i.e. "Swift 6 mode" code).
53-
MAGIC_STRING_IDENTIFIER(FileIDSpelledAsFile, "#file", PoundFileExpr)
53+
MAGIC_STRING_IDENTIFIER(FileIDSpelledAsFile, "#file")
5454
// tok::pound_file is shared with FilePathSpelledAsFile; please write custom
5555
// code paths for it.
5656

5757
/// The \c #filePath magic identifier literal.
58-
MAGIC_STRING_IDENTIFIER(FilePath, "#filePath", PoundFilePathExpr)
58+
MAGIC_STRING_IDENTIFIER(FilePath, "#filePath")
5959
MAGIC_IDENTIFIER_TOKEN(FilePath, pound_filePath)
6060

6161
/// The \c #file magic identifier literal, written in code where it is
6262
/// a synonym for \c #filePath (i.e. Swift 5 mode code).
63-
MAGIC_STRING_IDENTIFIER(FilePathSpelledAsFile, "#file", PoundFileExpr)
63+
MAGIC_STRING_IDENTIFIER(FilePathSpelledAsFile, "#file")
6464
// tok::pound_file is shared with FileIDSpelledAsFile; please write custom
6565
// code paths for it.
6666

6767
/// The \c #function magic identifier literal.
68-
MAGIC_STRING_IDENTIFIER(Function, "#function", PoundFunctionExpr)
68+
MAGIC_STRING_IDENTIFIER(Function, "#function")
6969
MAGIC_IDENTIFIER_TOKEN(Function, pound_function)
7070

7171

@@ -75,11 +75,11 @@ MAGIC_STRING_IDENTIFIER(Function, "#function", PoundFunctionExpr)
7575
//
7676

7777
/// The \c #line magic identifier literal.
78-
MAGIC_INT_IDENTIFIER(Line, "#line", PoundLineExpr)
78+
MAGIC_INT_IDENTIFIER(Line, "#line")
7979
MAGIC_IDENTIFIER_TOKEN(Line, pound_line)
8080

8181
/// The \c #column magic identifier literal.
82-
MAGIC_INT_IDENTIFIER(Column, "#column", PoundColumnExpr)
82+
MAGIC_INT_IDENTIFIER(Column, "#column")
8383
MAGIC_IDENTIFIER_TOKEN(Column, pound_column)
8484

8585

@@ -89,7 +89,7 @@ MAGIC_INT_IDENTIFIER(Column, "#column", PoundColumnExpr)
8989
//
9090

9191
/// The \c #dsohandle magic identifier literal.
92-
MAGIC_POINTER_IDENTIFIER(DSOHandle, "#dsohandle", PoundDsohandleExpr)
92+
MAGIC_POINTER_IDENTIFIER(DSOHandle, "#dsohandle")
9393
MAGIC_IDENTIFIER_TOKEN(DSOHandle, pound_dsohandle)
9494

9595
#undef MAGIC_IDENTIFIER

lib/AST/ASTDumper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ static StringRef getDumpString(ForeignErrorConvention::Kind value) {
382382
static StringRef getDumpString(DefaultArgumentKind value) {
383383
switch (value) {
384384
case DefaultArgumentKind::None: return "none";
385-
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
385+
#define MAGIC_IDENTIFIER(NAME, STRING) \
386386
case DefaultArgumentKind::NAME: return STRING;
387387
#include "swift/AST/MagicIdentifierKinds.def"
388388
case DefaultArgumentKind::Inherited: return "inherited";

lib/AST/ASTPrinter.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4008,8 +4008,7 @@ void PrintAST::printOneParameter(const ParamDecl *param,
40084008
Printer << " = ";
40094009

40104010
switch (param->getDefaultArgumentKind()) {
4011-
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
4012-
case DefaultArgumentKind::NAME:
4011+
#define MAGIC_IDENTIFIER(NAME, STRING) case DefaultArgumentKind::NAME:
40134012
#include "swift/AST/MagicIdentifierKinds.def"
40144013
Printer.printKeyword(defaultArgStr, Options);
40154014
break;

lib/AST/Bridging/ExprBridging.cpp

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

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

lib/AST/Decl.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8647,7 +8647,7 @@ static DefaultArgumentKind computeDefaultArgumentKind(DeclContext *dc,
86478647
return DefaultArgumentKind::Normal;
86488648

86498649
switch (magic->getKind()) {
8650-
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
8650+
#define MAGIC_IDENTIFIER(NAME, STRING) \
86518651
case MagicIdentifierLiteralExpr::NAME: \
86528652
return DefaultArgumentKind::NAME;
86538653
#include "swift/AST/MagicIdentifierKinds.def"
@@ -8867,7 +8867,7 @@ bool ParamDecl::hasDefaultExpr() const {
88678867
case DefaultArgumentKind::StoredProperty:
88688868
return false;
88698869
case DefaultArgumentKind::Normal:
8870-
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
8870+
#define MAGIC_IDENTIFIER(NAME, STRING) \
88718871
case DefaultArgumentKind::NAME:
88728872
#include "swift/AST/MagicIdentifierKinds.def"
88738873
case DefaultArgumentKind::ExpressionMacro:
@@ -8888,7 +8888,7 @@ bool ParamDecl::hasCallerSideDefaultExpr() const {
88888888
case DefaultArgumentKind::StoredProperty:
88898889
case DefaultArgumentKind::Normal:
88908890
return false;
8891-
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
8891+
#define MAGIC_IDENTIFIER(NAME, STRING) \
88928892
case DefaultArgumentKind::NAME:
88938893
#include "swift/AST/MagicIdentifierKinds.def"
88948894
case DefaultArgumentKind::NilLiteral:
@@ -9190,7 +9190,7 @@ ParamDecl::getDefaultValueStringRepresentation(
91909190
return extractInlinableText(getASTContext(), init, scratch);
91919191
}
91929192
case DefaultArgumentKind::Inherited: return "super";
9193-
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
9193+
#define MAGIC_IDENTIFIER(NAME, STRING) \
91949194
case DefaultArgumentKind::NAME: return STRING;
91959195
#include "swift/AST/MagicIdentifierKinds.def"
91969196
case DefaultArgumentKind::NilLiteral: return "nil";

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 {

lib/IDE/CompletionLookup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -999,7 +999,7 @@ bool CompletionLookup::hasInterestingDefaultValue(const ParamDecl *param) {
999999
return true;
10001000

10011001
case DefaultArgumentKind::None:
1002-
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
1002+
#define MAGIC_IDENTIFIER(NAME, STRING) \
10031003
case DefaultArgumentKind::NAME:
10041004
#include "swift/AST/MagicIdentifierKinds.def"
10051005
case DefaultArgumentKind::ExpressionMacro:

0 commit comments

Comments
 (0)