Skip to content

Commit 41368d0

Browse files
committed
[NFC] Add MagicIdentifierKinds.def
Extracts the list of magic identifier literal kinds into a separate file and updates a lot of code to use macro metaprogramming instead of naming half a dozen cases manually. This is a complicated change, but it should be NFC.
1 parent ab232fe commit 41368d0

17 files changed

+260
-248
lines changed

include/swift/AST/DefaultArgumentKind.h

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,6 @@ enum class DefaultArgumentKind : uint8_t {
3636
/// The default argument is inherited from the corresponding argument of the
3737
/// overridden declaration.
3838
Inherited,
39-
/// The #file default argument, which is expanded at the call site.
40-
File,
41-
/// The #filePath default argument, which is expanded at the call site.
42-
FilePath,
43-
/// The #line default argument, which is expanded at the call site.
44-
Line,
45-
/// The #column default argument, which is expanded at the call site.
46-
Column,
47-
/// The #function default argument, which is expanded at the call site.
48-
Function,
49-
/// The #dsohandle default argument, which is expanded at the call site.
50-
DSOHandle,
5139
/// The "nil" literal.
5240
NilLiteral,
5341
/// An empty array literal.
@@ -56,8 +44,11 @@ enum class DefaultArgumentKind : uint8_t {
5644
EmptyDictionary,
5745
/// A reference to the stored property. This is a special default argument
5846
/// kind for the synthesized memberwise constructor to emit a call to the
59-
// property's initializer.
47+
/// property's initializer.
6048
StoredProperty,
49+
// Magic identifier literals expanded at the call site:
50+
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) NAME,
51+
#include "swift/AST/MagicIdentifierKinds.def"
6152
};
6253
enum { NumDefaultArgumentKindBits = 4 };
6354

include/swift/AST/Expr.h

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,17 +1074,14 @@ class InterpolatedStringLiteralExpr : public LiteralExpr {
10741074
class MagicIdentifierLiteralExpr : public LiteralExpr {
10751075
public:
10761076
enum Kind : unsigned {
1077-
File, FilePath, Line, Column, Function, DSOHandle
1077+
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) NAME,
1078+
#include "swift/AST/MagicIdentifierKinds.def"
10781079
};
10791080

10801081
static StringRef getKindString(MagicIdentifierLiteralExpr::Kind value) {
10811082
switch (value) {
1082-
case File: return "#file";
1083-
case FilePath: return "#filePath";
1084-
case Function: return "#function";
1085-
case Line: return "#line";
1086-
case Column: return "#column";
1087-
case DSOHandle: return "#dsohandle";
1083+
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) case NAME: return STRING;
1084+
#include "swift/AST/MagicIdentifierKinds.def"
10881085
}
10891086

10901087
llvm_unreachable("Unhandled MagicIdentifierLiteralExpr in getKindString.");
@@ -1107,21 +1104,15 @@ class MagicIdentifierLiteralExpr : public LiteralExpr {
11071104
return static_cast<Kind>(Bits.MagicIdentifierLiteralExpr.Kind);
11081105
}
11091106

1110-
bool isFile() const { return getKind() == File; }
1111-
bool isFunction() const { return getKind() == Function; }
1112-
bool isLine() const { return getKind() == Line; }
1113-
bool isColumn() const { return getKind() == Column; }
1114-
11151107
bool isString() const {
11161108
switch (getKind()) {
1117-
case File:
1118-
case FilePath:
1119-
case Function:
1109+
#define MAGIC_STRING_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
1110+
case NAME: \
11201111
return true;
1121-
case Line:
1122-
case Column:
1123-
case DSOHandle:
1112+
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
1113+
case NAME: \
11241114
return false;
1115+
#include "swift/AST/MagicIdentifierKinds.def"
11251116
}
11261117
llvm_unreachable("bad Kind");
11271118
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
//===--- MagicIdentifierKinds.def - Swift #ident metaprogramming -*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This file defines macros used for macro-metaprogramming with magic
14+
// identifier literals.
15+
//
16+
//===----------------------------------------------------------------------===//
17+
18+
// Used for any magic identifier.
19+
#ifndef MAGIC_IDENTIFIER
20+
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND)
21+
#endif
22+
23+
// Used for magic identifiers which produce string literals.
24+
#ifndef MAGIC_STRING_IDENTIFIER
25+
#define MAGIC_STRING_IDENTIFIER(NAME, STRING, SYNTAX_KIND) MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND)
26+
#endif
27+
28+
// Used for magic identifiers which produce integer literals.
29+
#ifndef MAGIC_INT_IDENTIFIER
30+
#define MAGIC_INT_IDENTIFIER(NAME, STRING, SYNTAX_KIND) MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND)
31+
#endif
32+
33+
// Used for magic identifiers which produce raw pointers.
34+
#ifndef MAGIC_POINTER_IDENTIFIER
35+
#define MAGIC_POINTER_IDENTIFIER(NAME, STRING, SYNTAX_KIND) MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND)
36+
#endif
37+
38+
// Used when a given token always maps to a particular magic identifier kind.
39+
#ifndef MAGIC_IDENTIFIER_TOKEN
40+
#define MAGIC_IDENTIFIER_TOKEN(NAME, TOKEN)
41+
#endif
42+
43+
// Used when a given token always maps to a particular magic identifier kind,
44+
// but that token is deprecated.
45+
#ifndef MAGIC_IDENTIFIER_DEPRECATED_TOKEN
46+
#define MAGIC_IDENTIFIER_DEPRECATED_TOKEN(NAME, TOKEN) MAGIC_IDENTIFIER_TOKEN(NAME, TOKEN)
47+
#endif
48+
49+
50+
51+
//
52+
// Magic string literals
53+
//
54+
55+
/// The \c #file magic identifier literal.
56+
MAGIC_STRING_IDENTIFIER(File, "#file", PoundFileExpr)
57+
MAGIC_IDENTIFIER_TOKEN(File, pound_file)
58+
MAGIC_IDENTIFIER_DEPRECATED_TOKEN(File, kw___FILE__)
59+
60+
/// The \c #filePath magic identifier literal.
61+
MAGIC_STRING_IDENTIFIER(FilePath, "#filePath", PoundFilePathExpr)
62+
MAGIC_IDENTIFIER_TOKEN(FilePath, pound_filePath)
63+
64+
/// The \c #function magic identifier literal.
65+
MAGIC_STRING_IDENTIFIER(Function, "#function", PoundFunctionExpr)
66+
MAGIC_IDENTIFIER_TOKEN(Function, pound_function)
67+
MAGIC_IDENTIFIER_DEPRECATED_TOKEN(Function, kw___FUNCTION__)
68+
69+
70+
71+
//
72+
// Magic integer literals
73+
//
74+
75+
/// The \c #line magic identifier literal.
76+
MAGIC_INT_IDENTIFIER(Line, "#line", PoundLineExpr)
77+
MAGIC_IDENTIFIER_TOKEN(Line, pound_line)
78+
MAGIC_IDENTIFIER_DEPRECATED_TOKEN(Line, kw___LINE__)
79+
80+
/// The \c #column magic identifier literal.
81+
MAGIC_INT_IDENTIFIER(Column, "#column", PoundColumnExpr)
82+
MAGIC_IDENTIFIER_TOKEN(Column, pound_column)
83+
MAGIC_IDENTIFIER_DEPRECATED_TOKEN(Column, kw___COLUMN__)
84+
85+
86+
87+
//
88+
// Magic raw pointer literals
89+
//
90+
91+
/// The \c #dsohandle magic identifier literal.
92+
MAGIC_POINTER_IDENTIFIER(DSOHandle, "#dsohandle", PoundDsohandleExpr)
93+
MAGIC_IDENTIFIER_TOKEN(DSOHandle, pound_dsohandle)
94+
MAGIC_IDENTIFIER_DEPRECATED_TOKEN(DSOHandle, kw___DSO_HANDLE__)
95+
96+
97+
98+
99+
#undef MAGIC_IDENTIFIER
100+
#undef MAGIC_STRING_IDENTIFIER
101+
#undef MAGIC_INT_IDENTIFIER
102+
#undef MAGIC_POINTER_IDENTIFIER
103+
#undef MAGIC_IDENTIFIER_TOKEN
104+
#undef MAGIC_IDENTIFIER_DEPRECATED_TOKEN

lib/AST/ASTDumper.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -321,13 +321,10 @@ getForeignErrorConventionKindString(ForeignErrorConvention::Kind value) {
321321
static StringRef getDefaultArgumentKindString(DefaultArgumentKind value) {
322322
switch (value) {
323323
case DefaultArgumentKind::None: return "none";
324-
case DefaultArgumentKind::Column: return "#column";
325-
case DefaultArgumentKind::DSOHandle: return "#dsohandle";
326-
case DefaultArgumentKind::File: return "#file";
327-
case DefaultArgumentKind::FilePath: return "#filePath";
328-
case DefaultArgumentKind::Function: return "#function";
324+
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
325+
case DefaultArgumentKind::NAME: return STRING;
326+
#include "swift/AST/MagicIdentifierKinds.def"
329327
case DefaultArgumentKind::Inherited: return "inherited";
330-
case DefaultArgumentKind::Line: return "#line";
331328
case DefaultArgumentKind::NilLiteral: return "nil";
332329
case DefaultArgumentKind::EmptyArray: return "[]";
333330
case DefaultArgumentKind::EmptyDictionary: return "[:]";

lib/AST/ASTPrinter.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2739,12 +2739,9 @@ void PrintAST::printOneParameter(const ParamDecl *param,
27392739
Printer << " = ";
27402740

27412741
switch (param->getDefaultArgumentKind()) {
2742-
case DefaultArgumentKind::File:
2743-
case DefaultArgumentKind::Line:
2744-
case DefaultArgumentKind::Column:
2745-
case DefaultArgumentKind::Function:
2746-
case DefaultArgumentKind::DSOHandle:
2747-
case DefaultArgumentKind::NilLiteral:
2742+
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
2743+
case DefaultArgumentKind::NAME:
2744+
#include "swift/AST/MagicIdentifierKinds.def"
27482745
Printer.printKeyword(defaultArgStr, Options);
27492746
break;
27502747
default:

lib/AST/Decl.cpp

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6321,12 +6321,9 @@ bool ParamDecl::hasDefaultExpr() const {
63216321
case DefaultArgumentKind::StoredProperty:
63226322
return false;
63236323
case DefaultArgumentKind::Normal:
6324-
case DefaultArgumentKind::File:
6325-
case DefaultArgumentKind::FilePath:
6326-
case DefaultArgumentKind::Line:
6327-
case DefaultArgumentKind::Column:
6328-
case DefaultArgumentKind::Function:
6329-
case DefaultArgumentKind::DSOHandle:
6324+
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
6325+
case DefaultArgumentKind::NAME:
6326+
#include "swift/AST/MagicIdentifierKinds.def"
63306327
case DefaultArgumentKind::NilLiteral:
63316328
case DefaultArgumentKind::EmptyArray:
63326329
case DefaultArgumentKind::EmptyDictionary:
@@ -6344,12 +6341,9 @@ bool ParamDecl::hasCallerSideDefaultExpr() const {
63446341
case DefaultArgumentKind::StoredProperty:
63456342
case DefaultArgumentKind::Normal:
63466343
return false;
6347-
case DefaultArgumentKind::File:
6348-
case DefaultArgumentKind::FilePath:
6349-
case DefaultArgumentKind::Line:
6350-
case DefaultArgumentKind::Column:
6351-
case DefaultArgumentKind::Function:
6352-
case DefaultArgumentKind::DSOHandle:
6344+
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
6345+
case DefaultArgumentKind::NAME:
6346+
#include "swift/AST/MagicIdentifierKinds.def"
63536347
case DefaultArgumentKind::NilLiteral:
63546348
case DefaultArgumentKind::EmptyArray:
63556349
case DefaultArgumentKind::EmptyDictionary:
@@ -6585,12 +6579,9 @@ ParamDecl::getDefaultValueStringRepresentation(
65856579
scratch);
65866580
}
65876581
case DefaultArgumentKind::Inherited: return "super";
6588-
case DefaultArgumentKind::File: return "#file";
6589-
case DefaultArgumentKind::FilePath: return "#filePath";
6590-
case DefaultArgumentKind::Line: return "#line";
6591-
case DefaultArgumentKind::Column: return "#column";
6592-
case DefaultArgumentKind::Function: return "#function";
6593-
case DefaultArgumentKind::DSOHandle: return "#dsohandle";
6582+
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
6583+
case DefaultArgumentKind::NAME: return STRING;
6584+
#include "swift/AST/MagicIdentifierKinds.def"
65946585
case DefaultArgumentKind::NilLiteral: return "nil";
65956586
case DefaultArgumentKind::EmptyArray: return "[]";
65966587
case DefaultArgumentKind::EmptyDictionary: return "[:]";

lib/IDE/CodeCompletion.cpp

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2604,12 +2604,9 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
26042604
case DefaultArgumentKind::EmptyDictionary:
26052605
return !includeDefaultArgs;
26062606

2607-
case DefaultArgumentKind::File:
2608-
case DefaultArgumentKind::FilePath:
2609-
case DefaultArgumentKind::Line:
2610-
case DefaultArgumentKind::Column:
2611-
case DefaultArgumentKind::Function:
2612-
case DefaultArgumentKind::DSOHandle:
2607+
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
2608+
case DefaultArgumentKind::NAME:
2609+
#include "swift/AST/MagicIdentifierKinds.def"
26132610
// Skip parameters that are defaulted to source location or other
26142611
// caller context information. Users typically don't want to specify
26152612
// these parameters.
@@ -4042,33 +4039,47 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
40424039

40434040
/// Add '#file', '#line', et at.
40444041
void addPoundLiteralCompletions(bool needPound) {
4045-
auto addFromProto = [&](StringRef name, CodeCompletionKeywordKind kwKind,
4046-
CodeCompletionLiteralKind literalKind) {
4042+
auto addFromProto = [&](MagicIdentifierLiteralExpr::Kind magicKind,
4043+
Optional<CodeCompletionLiteralKind> literalKind) {
4044+
CodeCompletionKeywordKind kwKind;
4045+
switch (magicKind) {
4046+
#define MAGIC_IDENTIFIER_TOKEN(NAME, TOKEN) \
4047+
case MagicIdentifierLiteralExpr::NAME: \
4048+
kwKind = CodeCompletionKeywordKind::TOKEN; \
4049+
break;
4050+
#define MAGIC_IDENTIFIER_DEPRECATED_TOKEN(NAME, TOKEN)
4051+
#include "swift/AST/MagicIdentifierKinds.def"
4052+
}
4053+
4054+
StringRef name = MagicIdentifierLiteralExpr::getKindString(magicKind);
40474055
if (!needPound)
40484056
name = name.substr(1);
40494057

4058+
if (!literalKind) {
4059+
// Pointer type
4060+
addKeyword(name, "UnsafeRawPointer", kwKind);
4061+
return;
4062+
}
4063+
40504064
CodeCompletionResultBuilder builder(
40514065
Sink, CodeCompletionResult::ResultKind::Keyword,
40524066
SemanticContextKind::None, {});
4053-
builder.setLiteralKind(literalKind);
4067+
builder.setLiteralKind(literalKind.getValue());
40544068
builder.setKeywordKind(kwKind);
40554069
builder.addBaseName(name);
4056-
addTypeRelationFromProtocol(builder, literalKind);
4070+
addTypeRelationFromProtocol(builder, literalKind.getValue());
40574071
};
40584072

4059-
addFromProto("#function", CodeCompletionKeywordKind::pound_function,
4060-
CodeCompletionLiteralKind::StringLiteral);
4061-
addFromProto("#file", CodeCompletionKeywordKind::pound_file,
4062-
CodeCompletionLiteralKind::StringLiteral);
4063-
addFromProto("#filePath", CodeCompletionKeywordKind::pound_filePath,
4073+
#define MAGIC_STRING_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
4074+
addFromProto(MagicIdentifierLiteralExpr::NAME, \
40644075
CodeCompletionLiteralKind::StringLiteral);
4065-
addFromProto("#line", CodeCompletionKeywordKind::pound_line,
4076+
#define MAGIC_INT_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
4077+
addFromProto(MagicIdentifierLiteralExpr::NAME, \
40664078
CodeCompletionLiteralKind::IntegerLiteral);
4067-
addFromProto("#column", CodeCompletionKeywordKind::pound_column,
4068-
CodeCompletionLiteralKind::IntegerLiteral);
4069-
4070-
addKeyword(needPound ? "#dsohandle" : "dsohandle", "UnsafeRawPointer",
4071-
CodeCompletionKeywordKind::pound_dsohandle);
4079+
#define MAGIC_POINTER_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
4080+
addFromProto(MagicIdentifierLiteralExpr::NAME, \
4081+
None);
4082+
#include "swift/AST/MagicIdentifierKinds.def"
40724083
}
40734084

40744085
void addValueLiteralCompletions() {

0 commit comments

Comments
 (0)