Skip to content

Commit 4394e92

Browse files
authored
Merge pull request #32700 from brentdax/magical-and-evolutionary
Revise #file changes from SE-0274
2 parents 1b3cf44 + 87d3d3e commit 4394e92

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+601
-352
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/DiagnosticsCommon.def

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,22 @@ ERROR(sdk_node_unrecognized_decl_kind,none,
118118
ERROR(sdk_node_unrecognized_accessor_kind,none,
119119
"unrecognized accessor kind '%0' in SDK node", (StringRef))
120120

121-
// Emitted from ModuleDecl::computeMagicFileStringMap()
122-
WARNING(pound_source_location_creates_pound_file_conflicts,none,
123-
"'#sourceLocation' directive produces '#file' string of '%0', which "
124-
"conflicts with '#file' strings produced by other paths in the module",
125-
(StringRef))
121+
// Emitted from ModuleDecl::computeFileIDMap()
122+
WARNING(source_location_creates_file_id_conflicts,none,
123+
"'#sourceLocation' directive produces '#fileID' string of '%0', which "
124+
"conflicts with '#fileID' strings produced by other paths in the "
125+
"module", (StringRef))
126126
NOTE(fixit_correct_source_location_file,none,
127127
"change file in '#sourceLocation' to '%0'", (StringRef))
128128

129+
// Usually, but not always, emitted from the driver
130+
ERROR(error_two_files_same_name,none,
131+
"filename \"%0\" used twice: '%1' and '%2'",
132+
(StringRef, StringRef, StringRef))
133+
NOTE(note_explain_two_files_same_name,none,
134+
"filenames are used to distinguish private declarations with the same "
135+
"name", ())
136+
129137
//------------------------------------------------------------------------------
130138
// MARK: Circular reference diagnostics
131139
//------------------------------------------------------------------------------

include/swift/AST/DiagnosticsDriver.def

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,6 @@ WARNING(warn_arclite_not_found_when_link_objc_runtime,none,
104104
"unable to find Objective-C runtime support library 'arclite'; "
105105
"pass '-no-link-objc-runtime' to silence this warning", ())
106106

107-
ERROR(error_two_files_same_name,none,
108-
"filename \"%0\" used twice: '%1' and '%2'",
109-
(StringRef, StringRef, StringRef))
110-
NOTE(note_explain_two_files_same_name,none,
111-
"filenames are used to distinguish private declarations with the same "
112-
"name", ())
113-
114107
WARNING(warn_cannot_stat_input,none,
115108
"unable to determine when '%0' was last modified: %1",
116109
(StringRef, StringRef))

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: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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 #fileID magic identifier literal.
56+
MAGIC_STRING_IDENTIFIER(FileID, "#fileID", PoundFileIDExpr)
57+
MAGIC_IDENTIFIER_TOKEN(FileID, pound_fileID)
58+
59+
/// The \c #file magic identifier literal, written in code where it is
60+
/// a synonym for \c #fileID (i.e. "Swift 6 mode" code).
61+
MAGIC_STRING_IDENTIFIER(FileIDSpelledAsFile, "#file", PoundFileExpr)
62+
// tok::pound_file is shared with FilePathSpelledAsFile; please write custom
63+
// code paths for it.
64+
65+
/// The \c #filePath magic identifier literal.
66+
MAGIC_STRING_IDENTIFIER(FilePath, "#filePath", PoundFilePathExpr)
67+
MAGIC_IDENTIFIER_TOKEN(FilePath, pound_filePath)
68+
69+
/// The \c #file magic identifier literal, written in code where it is
70+
/// a synonym for \c #filePath (i.e. Swift 5 mode code).
71+
MAGIC_STRING_IDENTIFIER(FilePathSpelledAsFile, "#file", PoundFileExpr)
72+
// tok::pound_file is shared with FileIDSpelledAsFile; please write custom
73+
// code paths for it.
74+
MAGIC_IDENTIFIER_DEPRECATED_TOKEN(FilePathSpelledAsFile, kw___FILE__)
75+
76+
/// The \c #function magic identifier literal.
77+
MAGIC_STRING_IDENTIFIER(Function, "#function", PoundFunctionExpr)
78+
MAGIC_IDENTIFIER_TOKEN(Function, pound_function)
79+
MAGIC_IDENTIFIER_DEPRECATED_TOKEN(Function, kw___FUNCTION__)
80+
81+
82+
83+
//
84+
// Magic integer literals
85+
//
86+
87+
/// The \c #line magic identifier literal.
88+
MAGIC_INT_IDENTIFIER(Line, "#line", PoundLineExpr)
89+
MAGIC_IDENTIFIER_TOKEN(Line, pound_line)
90+
MAGIC_IDENTIFIER_DEPRECATED_TOKEN(Line, kw___LINE__)
91+
92+
/// The \c #column magic identifier literal.
93+
MAGIC_INT_IDENTIFIER(Column, "#column", PoundColumnExpr)
94+
MAGIC_IDENTIFIER_TOKEN(Column, pound_column)
95+
MAGIC_IDENTIFIER_DEPRECATED_TOKEN(Column, kw___COLUMN__)
96+
97+
98+
99+
//
100+
// Magic raw pointer literals
101+
//
102+
103+
/// The \c #dsohandle magic identifier literal.
104+
MAGIC_POINTER_IDENTIFIER(DSOHandle, "#dsohandle", PoundDsohandleExpr)
105+
MAGIC_IDENTIFIER_TOKEN(DSOHandle, pound_dsohandle)
106+
MAGIC_IDENTIFIER_DEPRECATED_TOKEN(DSOHandle, kw___DSO_HANDLE__)
107+
108+
109+
110+
111+
#undef MAGIC_IDENTIFIER
112+
#undef MAGIC_STRING_IDENTIFIER
113+
#undef MAGIC_INT_IDENTIFIER
114+
#undef MAGIC_POINTER_IDENTIFIER
115+
#undef MAGIC_IDENTIFIER_TOKEN
116+
#undef MAGIC_IDENTIFIER_DEPRECATED_TOKEN

include/swift/AST/Module.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -370,23 +370,20 @@ class ModuleDecl : public DeclContext, public TypeDecl {
370370
bool isClangModule() const;
371371
void addFile(FileUnit &newFile);
372372

373-
/// Creates a map from \c #filePath strings to corresponding \c #file
373+
/// Creates a map from \c #filePath strings to corresponding \c #fileID
374374
/// strings, diagnosing any conflicts.
375375
///
376-
/// A given \c #filePath string always maps to exactly one \c #file string,
376+
/// A given \c #filePath string always maps to exactly one \c #fileID string,
377377
/// but it is possible for \c #sourceLocation directives to introduce
378378
/// duplicates in the opposite direction. If there are such conflicts, this
379379
/// method will diagnose the conflict and choose a "winner" among the paths
380-
/// in a reproducible way. The \c bool paired with the \c #file string is
380+
/// in a reproducible way. The \c bool paired with the \c #fileID string is
381381
/// \c true for paths which did not have a conflict or won a conflict, and
382382
/// \c false for paths which lost a conflict. Thus, if you want to generate a
383-
/// reverse mapping, you should drop or special-case the \c #file strings that
384-
/// are paired with \c false.
385-
///
386-
/// Note that this returns an empty StringMap if concise \c #file strings are
387-
/// disabled. Users should fall back to using the file path in this case.
383+
/// reverse mapping, you should drop or special-case the \c #fileID strings
384+
/// that are paired with \c false.
388385
llvm::StringMap<std::pair<std::string, /*isWinner=*/bool>>
389-
computeMagicFileStringMap(bool shouldDiagnose) const;
386+
computeFileIDMap(bool shouldDiagnose) const;
390387

391388
/// Add a file declaring a cross-import overlay.
392389
void addCrossImportOverlayFile(StringRef file);

include/swift/Option/Options.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ def enable_experimental_additive_arithmetic_derivation :
543543

544544
def enable_experimental_concise_pound_file : Flag<["-"],
545545
"enable-experimental-concise-pound-file">,
546-
Flags<[FrontendOption]>,
546+
Flags<[FrontendOption, ModuleInterfaceOption]>,
547547
HelpText<"Enable experimental concise '#file' identifier">;
548548

549549
def enable_direct_intramodule_dependencies : Flag<["-"],

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 "[:]";

0 commit comments

Comments
 (0)