Skip to content

Commit acc01ae

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 672bd40 commit acc01ae

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
@@ -323,13 +323,10 @@ getForeignErrorConventionKindString(ForeignErrorConvention::Kind value) {
323323
static StringRef getDefaultArgumentKindString(DefaultArgumentKind value) {
324324
switch (value) {
325325
case DefaultArgumentKind::None: return "none";
326-
case DefaultArgumentKind::Column: return "#column";
327-
case DefaultArgumentKind::DSOHandle: return "#dsohandle";
328-
case DefaultArgumentKind::File: return "#file";
329-
case DefaultArgumentKind::FilePath: return "#filePath";
330-
case DefaultArgumentKind::Function: return "#function";
326+
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
327+
case DefaultArgumentKind::NAME: return STRING;
328+
#include "swift/AST/MagicIdentifierKinds.def"
331329
case DefaultArgumentKind::Inherited: return "inherited";
332-
case DefaultArgumentKind::Line: return "#line";
333330
case DefaultArgumentKind::NilLiteral: return "nil";
334331
case DefaultArgumentKind::EmptyArray: return "[]";
335332
case DefaultArgumentKind::EmptyDictionary: return "[:]";

lib/AST/ASTPrinter.cpp

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

28132813
switch (param->getDefaultArgumentKind()) {
2814-
case DefaultArgumentKind::File:
2815-
case DefaultArgumentKind::Line:
2816-
case DefaultArgumentKind::Column:
2817-
case DefaultArgumentKind::Function:
2818-
case DefaultArgumentKind::DSOHandle:
2819-
case DefaultArgumentKind::NilLiteral:
2814+
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
2815+
case DefaultArgumentKind::NAME:
2816+
#include "swift/AST/MagicIdentifierKinds.def"
28202817
Printer.printKeyword(defaultArgStr, Options);
28212818
break;
28222819
default:

lib/AST/Decl.cpp

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6348,12 +6348,9 @@ bool ParamDecl::hasDefaultExpr() const {
63486348
case DefaultArgumentKind::StoredProperty:
63496349
return false;
63506350
case DefaultArgumentKind::Normal:
6351-
case DefaultArgumentKind::File:
6352-
case DefaultArgumentKind::FilePath:
6353-
case DefaultArgumentKind::Line:
6354-
case DefaultArgumentKind::Column:
6355-
case DefaultArgumentKind::Function:
6356-
case DefaultArgumentKind::DSOHandle:
6351+
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
6352+
case DefaultArgumentKind::NAME:
6353+
#include "swift/AST/MagicIdentifierKinds.def"
63576354
case DefaultArgumentKind::NilLiteral:
63586355
case DefaultArgumentKind::EmptyArray:
63596356
case DefaultArgumentKind::EmptyDictionary:
@@ -6371,12 +6368,9 @@ bool ParamDecl::hasCallerSideDefaultExpr() const {
63716368
case DefaultArgumentKind::StoredProperty:
63726369
case DefaultArgumentKind::Normal:
63736370
return false;
6374-
case DefaultArgumentKind::File:
6375-
case DefaultArgumentKind::FilePath:
6376-
case DefaultArgumentKind::Line:
6377-
case DefaultArgumentKind::Column:
6378-
case DefaultArgumentKind::Function:
6379-
case DefaultArgumentKind::DSOHandle:
6371+
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
6372+
case DefaultArgumentKind::NAME:
6373+
#include "swift/AST/MagicIdentifierKinds.def"
63806374
case DefaultArgumentKind::NilLiteral:
63816375
case DefaultArgumentKind::EmptyArray:
63826376
case DefaultArgumentKind::EmptyDictionary:
@@ -6613,12 +6607,9 @@ ParamDecl::getDefaultValueStringRepresentation(
66136607
scratch);
66146608
}
66156609
case DefaultArgumentKind::Inherited: return "super";
6616-
case DefaultArgumentKind::File: return "#file";
6617-
case DefaultArgumentKind::FilePath: return "#filePath";
6618-
case DefaultArgumentKind::Line: return "#line";
6619-
case DefaultArgumentKind::Column: return "#column";
6620-
case DefaultArgumentKind::Function: return "#function";
6621-
case DefaultArgumentKind::DSOHandle: return "#dsohandle";
6610+
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
6611+
case DefaultArgumentKind::NAME: return STRING;
6612+
#include "swift/AST/MagicIdentifierKinds.def"
66226613
case DefaultArgumentKind::NilLiteral: return "nil";
66236614
case DefaultArgumentKind::EmptyArray: return "[]";
66246615
case DefaultArgumentKind::EmptyDictionary: return "[:]";

lib/IDE/CodeCompletion.cpp

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

2597-
case DefaultArgumentKind::File:
2598-
case DefaultArgumentKind::FilePath:
2599-
case DefaultArgumentKind::Line:
2600-
case DefaultArgumentKind::Column:
2601-
case DefaultArgumentKind::Function:
2602-
case DefaultArgumentKind::DSOHandle:
2597+
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
2598+
case DefaultArgumentKind::NAME:
2599+
#include "swift/AST/MagicIdentifierKinds.def"
26032600
// Skip parameters that are defaulted to source location or other
26042601
// caller context information. Users typically don't want to specify
26052602
// these parameters.
@@ -4032,33 +4029,47 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
40324029

40334030
/// Add '#file', '#line', et at.
40344031
void addPoundLiteralCompletions(bool needPound) {
4035-
auto addFromProto = [&](StringRef name, CodeCompletionKeywordKind kwKind,
4036-
CodeCompletionLiteralKind literalKind) {
4032+
auto addFromProto = [&](MagicIdentifierLiteralExpr::Kind magicKind,
4033+
Optional<CodeCompletionLiteralKind> literalKind) {
4034+
CodeCompletionKeywordKind kwKind;
4035+
switch (magicKind) {
4036+
#define MAGIC_IDENTIFIER_TOKEN(NAME, TOKEN) \
4037+
case MagicIdentifierLiteralExpr::NAME: \
4038+
kwKind = CodeCompletionKeywordKind::TOKEN; \
4039+
break;
4040+
#define MAGIC_IDENTIFIER_DEPRECATED_TOKEN(NAME, TOKEN)
4041+
#include "swift/AST/MagicIdentifierKinds.def"
4042+
}
4043+
4044+
StringRef name = MagicIdentifierLiteralExpr::getKindString(magicKind);
40374045
if (!needPound)
40384046
name = name.substr(1);
40394047

4048+
if (!literalKind) {
4049+
// Pointer type
4050+
addKeyword(name, "UnsafeRawPointer", kwKind);
4051+
return;
4052+
}
4053+
40404054
CodeCompletionResultBuilder builder(
40414055
Sink, CodeCompletionResult::ResultKind::Keyword,
40424056
SemanticContextKind::None, {});
4043-
builder.setLiteralKind(literalKind);
4057+
builder.setLiteralKind(literalKind.getValue());
40444058
builder.setKeywordKind(kwKind);
40454059
builder.addBaseName(name);
4046-
addTypeRelationFromProtocol(builder, literalKind);
4060+
addTypeRelationFromProtocol(builder, literalKind.getValue());
40474061
};
40484062

4049-
addFromProto("#function", CodeCompletionKeywordKind::pound_function,
4050-
CodeCompletionLiteralKind::StringLiteral);
4051-
addFromProto("#file", CodeCompletionKeywordKind::pound_file,
4052-
CodeCompletionLiteralKind::StringLiteral);
4053-
addFromProto("#filePath", CodeCompletionKeywordKind::pound_filePath,
4063+
#define MAGIC_STRING_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
4064+
addFromProto(MagicIdentifierLiteralExpr::NAME, \
40544065
CodeCompletionLiteralKind::StringLiteral);
4055-
addFromProto("#line", CodeCompletionKeywordKind::pound_line,
4066+
#define MAGIC_INT_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
4067+
addFromProto(MagicIdentifierLiteralExpr::NAME, \
40564068
CodeCompletionLiteralKind::IntegerLiteral);
4057-
addFromProto("#column", CodeCompletionKeywordKind::pound_column,
4058-
CodeCompletionLiteralKind::IntegerLiteral);
4059-
4060-
addKeyword(needPound ? "#dsohandle" : "dsohandle", "UnsafeRawPointer",
4061-
CodeCompletionKeywordKind::pound_dsohandle);
4069+
#define MAGIC_POINTER_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
4070+
addFromProto(MagicIdentifierLiteralExpr::NAME, \
4071+
None);
4072+
#include "swift/AST/MagicIdentifierKinds.def"
40624073
}
40634074

40644075
void addValueLiteralCompletions() {

0 commit comments

Comments
 (0)