Skip to content

Commit d8d226c

Browse files
authored
Merge pull request #84362 from beccadax/mod-squad-parse
[SE-0491] Parse and ASTGen module selectors
2 parents d59244f + 683ce9d commit d8d226c

27 files changed

+1615
-311
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,19 @@ class BridgedDeclNameRef {
148148
BRIDGED_INLINE swift::DeclNameRef unbridged() const;
149149
};
150150

151-
SWIFT_NAME("BridgedDeclNameRef.createParsed(_:baseName:argumentLabels:)")
152-
BridgedDeclNameRef BridgedDeclNameRef_createParsed(BridgedASTContext cContext,
153-
swift::DeclBaseName baseName,
154-
BridgedArrayRef cLabels);
151+
SWIFT_NAME("BridgedDeclNameRef.createParsed(_:moduleSelector:baseName:"
152+
"argumentLabels:)")
153+
BridgedDeclNameRef
154+
BridgedDeclNameRef_createParsed(BridgedASTContext cContext,
155+
swift::Identifier cModuleSelector,
156+
swift::DeclBaseName cBaseName,
157+
BridgedArrayRef cLabels);
155158

156-
SWIFT_NAME("BridgedDeclNameRef.createParsed(_:)")
159+
SWIFT_NAME("BridgedDeclNameRef.createParsed(_:moduleSelector:baseName:)")
157160
BridgedDeclNameRef
158-
BridgedDeclNameRef_createParsed(swift::DeclBaseName baseName);
161+
BridgedDeclNameRef_createParsed(BridgedASTContext cContext,
162+
swift::Identifier cModuleSelector,
163+
swift::DeclBaseName cBaseName);
159164

160165
class BridgedDeclNameLoc {
161166
const void *_Nullable LocationInfo;
@@ -893,11 +898,12 @@ BridgedDocumentationAttr BridgedDocumentationAttr_createParsed(
893898

894899
SWIFT_NAME(
895900
"BridgedDynamicReplacementAttr.createParsed(_:atLoc:attrNameLoc:lParenLoc:"
896-
"replacedFunction:rParenLoc:)")
901+
"replacedFunction:replacedFunctionLoc:rParenLoc:)")
897902
BridgedDynamicReplacementAttr BridgedDynamicReplacementAttr_createParsed(
898-
BridgedASTContext cContext, swift::SourceLoc atLoc,
899-
swift::SourceLoc attrNameLoc, swift::SourceLoc lParenLoc,
900-
BridgedDeclNameRef cReplacedFunction, swift::SourceLoc rParenLoc);
903+
BridgedASTContext cContext, swift::SourceLoc cAtLoc,
904+
swift::SourceLoc cAttrNameLoc, swift::SourceLoc cLParenLoc,
905+
BridgedDeclNameRef cReplacedFunction,
906+
BridgedDeclNameLoc cReplacedFunctionLoc, swift::SourceLoc cRParenLoc);
901907

902908
SWIFT_NAME("BridgedEffectsAttr.createParsed(_:atLoc:range:effectKind:)")
903909
BridgedEffectsAttr BridgedEffectsAttr_createParsed(
@@ -1228,22 +1234,24 @@ enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedSpecializationKind : uint8_t {
12281234
};
12291235

12301236
SWIFT_NAME("BridgedSpecializeAttr.createParsed(_:atLoc:range:whereClause:"
1231-
"exported:kind:taretFunction:spiGroups:availableAttrs:)")
1237+
"exported:kind:targetFunction:targetFunctionLoc:spiGroups:"
1238+
"availableAttrs:)")
12321239
BridgedSpecializeAttr BridgedSpecializeAttr_createParsed(
12331240
BridgedASTContext cContext, swift::SourceLoc atLoc,
12341241
swift::SourceRange range, BridgedNullableTrailingWhereClause cWhereClause,
12351242
bool exported, BridgedSpecializationKind cKind,
1236-
BridgedDeclNameRef cTargetFunction, BridgedArrayRef cSPIGroups,
1237-
BridgedArrayRef cAvailableAttrs);
1243+
BridgedDeclNameRef cTargetFunction, BridgedDeclNameLoc cTargetFunctionLoc,
1244+
BridgedArrayRef cSPIGroups, BridgedArrayRef cAvailableAttrs);
12381245

12391246
SWIFT_NAME("BridgedSpecializedAttr.createParsed(_:atLoc:range:whereClause:"
1240-
"exported:kind:taretFunction:spiGroups:availableAttrs:)")
1247+
"exported:kind:targetFunction:targetFunctionLoc:spiGroups:"
1248+
"availableAttrs:)")
12411249
BridgedSpecializedAttr BridgedSpecializedAttr_createParsed(
12421250
BridgedASTContext cContext, swift::SourceLoc atLoc,
12431251
swift::SourceRange range, BridgedNullableTrailingWhereClause cWhereClause,
12441252
bool exported, BridgedSpecializationKind cKind,
1245-
BridgedDeclNameRef cTargetFunction, BridgedArrayRef cSPIGroups,
1246-
BridgedArrayRef cAvailableAttrs);
1253+
BridgedDeclNameRef cTargetFunction, BridgedDeclNameLoc cTargetFunctionLoc,
1254+
BridgedArrayRef cSPIGroups, BridgedArrayRef cAvailableAttrs);
12471255

12481256
SWIFT_NAME(
12491257
"BridgedSPIAccessControlAttr.createParsed(_:atLoc:range:spiGroupName:)")
@@ -2496,9 +2504,10 @@ BridgedOpaqueReturnTypeOfTypeAttr_createParsed(
24962504
// MARK: TypeReprs
24972505
//===----------------------------------------------------------------------===//
24982506

2499-
SWIFT_NAME("BridgedUnqualifiedIdentTypeRepr.createParsed(_:loc:name:)")
2507+
SWIFT_NAME("BridgedUnqualifiedIdentTypeRepr.createParsed(_:name:loc:)")
25002508
BridgedUnqualifiedIdentTypeRepr BridgedUnqualifiedIdentTypeRepr_createParsed(
2501-
BridgedASTContext cContext, swift::SourceLoc loc, swift::Identifier id);
2509+
BridgedASTContext cContext, BridgedDeclNameRef cName,
2510+
BridgedDeclNameLoc cLoc);
25022511

25032512
SWIFT_NAME(
25042513
"BridgedArrayTypeRepr.createParsed(_:base:leftSquareLoc:rightSquareLoc:)")
@@ -2532,8 +2541,8 @@ BridgedCompileTimeLiteralTypeRepr_createParsed(BridgedASTContext cContext,
25322541
SWIFT_NAME("BridgedDeclRefTypeRepr.createParsed(_:base:name:nameLoc:"
25332542
"genericArguments:angleRange:)")
25342543
BridgedDeclRefTypeRepr BridgedDeclRefTypeRepr_createParsed(
2535-
BridgedASTContext cContext, BridgedTypeRepr cBase, swift::Identifier name,
2536-
swift::SourceLoc loc, BridgedArrayRef cGenericArguments,
2544+
BridgedASTContext cContext, BridgedTypeRepr cBase, BridgedDeclNameRef cName,
2545+
BridgedDeclNameLoc cLoc, BridgedArrayRef cGenericArguments,
25372546
swift::SourceRange angleRange);
25382547

25392548
SWIFT_NAME("BridgedDictionaryTypeRepr.createParsed(_:leftSquareLoc:keyType:"
@@ -2558,8 +2567,8 @@ BridgedFunctionTypeRepr BridgedFunctionTypeRepr_createParsed(
25582567
SWIFT_NAME("BridgedUnqualifiedIdentTypeRepr.createParsed(_:name:nameLoc:"
25592568
"genericArgs:leftAngleLoc:rightAngleLoc:)")
25602569
BridgedUnqualifiedIdentTypeRepr BridgedUnqualifiedIdentTypeRepr_createParsed(
2561-
BridgedASTContext cContext, swift::Identifier name,
2562-
swift::SourceLoc nameLoc, BridgedArrayRef genericArgs,
2570+
BridgedASTContext cContext, BridgedDeclNameRef cName,
2571+
BridgedDeclNameLoc cNameLoc, BridgedArrayRef genericArgs,
25632572
swift::SourceLoc lAngleLoc, swift::SourceLoc rAngleLoc);
25642573

25652574
SWIFT_NAME("BridgedOptionalTypeRepr.createParsed(_:base:questionLoc:)")

include/swift/AST/Attr.h

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,19 +1263,22 @@ class DynamicReplacementAttr final
12631263
friend class DynamicallyReplacedDeclRequest;
12641264

12651265
DeclNameRef ReplacedFunctionName;
1266+
DeclNameLoc ReplacedFunctionNameLoc;
12661267
LazyMemberLoader *Resolver = nullptr;
12671268
uint64_t ResolverContextData;
12681269

12691270
/// Create an @_dynamicReplacement(for:) attribute written in the source.
12701271
DynamicReplacementAttr(SourceLoc atLoc, SourceRange baseRange,
12711272
DeclNameRef replacedFunctionName,
1273+
DeclNameLoc replacedFunctionNameLoc,
12721274
SourceRange parenRange);
12731275

12741276
DynamicReplacementAttr(DeclNameRef name, AbstractFunctionDecl *f)
12751277
: DeclAttribute(DeclAttrKind::DynamicReplacement, SourceLoc(),
12761278
SourceRange(),
12771279
/*Implicit=*/false),
1278-
ReplacedFunctionName(name), Resolver(nullptr), ResolverContextData(0) {
1280+
ReplacedFunctionName(name), ReplacedFunctionNameLoc(),
1281+
Resolver(nullptr), ResolverContextData(0) {
12791282
Bits.DynamicReplacementAttr.HasTrailingLocationInfo = false;
12801283
}
12811284

@@ -1284,8 +1287,8 @@ class DynamicReplacementAttr final
12841287
: DeclAttribute(DeclAttrKind::DynamicReplacement, SourceLoc(),
12851288
SourceRange(),
12861289
/*Implicit=*/false),
1287-
ReplacedFunctionName(name), Resolver(Resolver),
1288-
ResolverContextData(Data) {
1290+
ReplacedFunctionName(name), ReplacedFunctionNameLoc(),
1291+
Resolver(Resolver), ResolverContextData(Data) {
12891292
Bits.DynamicReplacementAttr.HasTrailingLocationInfo = false;
12901293
}
12911294

@@ -1306,7 +1309,8 @@ class DynamicReplacementAttr final
13061309
public:
13071310
static DynamicReplacementAttr *
13081311
create(ASTContext &Context, SourceLoc AtLoc, SourceLoc DynReplLoc,
1309-
SourceLoc LParenLoc, DeclNameRef replacedFunction, SourceLoc RParenLoc);
1312+
SourceLoc LParenLoc, DeclNameRef replacedFunction,
1313+
DeclNameLoc replacedFunctionNameLoc, SourceLoc RParenLoc);
13101314

13111315
static DynamicReplacementAttr *create(ASTContext &ctx,
13121316
DeclNameRef replacedFunction,
@@ -1321,6 +1325,10 @@ class DynamicReplacementAttr final
13211325
return ReplacedFunctionName;
13221326
}
13231327

1328+
DeclNameLoc getReplacedFunctionNameLoc() const {
1329+
return ReplacedFunctionNameLoc;
1330+
}
1331+
13241332
/// Retrieve the location of the opening parentheses, if there is one.
13251333
SourceLoc getLParenLoc() const;
13261334

@@ -1809,6 +1817,7 @@ class AbstractSpecializeAttr
18091817
GenericSignature specializedSignature;
18101818

18111819
DeclNameRef targetFunctionName;
1820+
DeclNameLoc targetFunctionNameLoc;
18121821
LazyMemberLoader *resolver = nullptr;
18131822
uint64_t resolverContextData;
18141823
size_t numSPIGroups;
@@ -1821,7 +1830,9 @@ class AbstractSpecializeAttr
18211830
TrailingWhereClause *clause,
18221831
bool exported,
18231832
SpecializationKind kind, GenericSignature specializedSignature,
1824-
DeclNameRef targetFunctionName, ArrayRef<Identifier> spiGroups,
1833+
DeclNameRef targetFunctionName,
1834+
DeclNameLoc targetFunctionNameLoc,
1835+
ArrayRef<Identifier> spiGroups,
18251836
ArrayRef<AvailableAttr *> availabilityAttrs,
18261837
size_t typeErasedParamsCount);
18271838

@@ -1905,6 +1916,10 @@ class AbstractSpecializeAttr
19051916
return targetFunctionName;
19061917
}
19071918

1919+
DeclNameLoc getTargetFunctionNameLoc() const {
1920+
return targetFunctionNameLoc;
1921+
}
1922+
19081923
/// \p forDecl is the value decl that the attribute belongs to.
19091924
ValueDecl *getTargetFunctionDecl(const ValueDecl *forDecl) const;
19101925

@@ -1934,19 +1949,23 @@ class SpecializeAttr final : public AbstractSpecializeAttr,
19341949
TrailingWhereClause *clause,
19351950
bool exported,
19361951
SpecializationKind kind, GenericSignature specializedSignature,
1937-
DeclNameRef targetFunctionName, ArrayRef<Identifier> spiGroups,
1952+
DeclNameRef targetFunctionName,
1953+
DeclNameLoc targetFunctionNameLoc,
1954+
ArrayRef<Identifier> spiGroups,
19381955
ArrayRef<AvailableAttr *> availabilityAttrs,
19391956
size_t typeErasedParamsCount) :
19401957
AbstractSpecializeAttr(DeclAttrKind::Specialize, atLoc, Range, clause,
19411958
exported, kind, specializedSignature, targetFunctionName,
1942-
spiGroups, availabilityAttrs, typeErasedParamsCount) {}
1959+
targetFunctionNameLoc, spiGroups, availabilityAttrs,
1960+
typeErasedParamsCount) {}
19431961

19441962
public:
19451963
static SpecializeAttr *
19461964
create(ASTContext &Ctx, SourceLoc atLoc, SourceRange Range,
19471965
TrailingWhereClause *clause, bool exported,
19481966
SpecializationKind kind,
1949-
DeclNameRef targetFunctionName, ArrayRef<Identifier> spiGroups,
1967+
DeclNameRef targetFunctionName, DeclNameLoc targetFunctionNameLoc,
1968+
ArrayRef<Identifier> spiGroups,
19501969
ArrayRef<AvailableAttr *> availabilityAttrs,
19511970
GenericSignature specializedSignature = nullptr);
19521971

@@ -1990,19 +2009,23 @@ class SpecializedAttr final : public AbstractSpecializeAttr ,
19902009
TrailingWhereClause *clause,
19912010
bool exported,
19922011
SpecializationKind kind, GenericSignature specializedSignature,
1993-
DeclNameRef targetFunctionName, ArrayRef<Identifier> spiGroups,
2012+
DeclNameRef targetFunctionName,
2013+
DeclNameLoc targetFunctionNameLoc,
2014+
ArrayRef<Identifier> spiGroups,
19942015
ArrayRef<AvailableAttr *> availabilityAttrs,
19952016
size_t typeErasedParamsCount) :
19962017
AbstractSpecializeAttr(DeclAttrKind::Specialized, atLoc, Range, clause,
19972018
exported, kind, specializedSignature, targetFunctionName,
1998-
spiGroups, availabilityAttrs, typeErasedParamsCount) {}
2019+
targetFunctionNameLoc, spiGroups, availabilityAttrs,
2020+
typeErasedParamsCount) {}
19992021

20002022
public:
20012023
static SpecializedAttr *
20022024
create(ASTContext &Ctx, SourceLoc atLoc, SourceRange Range,
20032025
TrailingWhereClause *clause, bool exported,
20042026
SpecializationKind kind,
2005-
DeclNameRef targetFunctionName, ArrayRef<Identifier> spiGroups,
2027+
DeclNameRef targetFunctionName, DeclNameLoc targetFunctionNameLoc,
2028+
ArrayRef<Identifier> spiGroups,
20062029
ArrayRef<AvailableAttr *> availabilityAttrs,
20072030
GenericSignature specializedSignature = nullptr);
20082031

include/swift/AST/DiagnosticsParse.def

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,17 @@ ERROR(forbidden_interpolated_string,none,
9393
ERROR(forbidden_extended_escaping_string,none,
9494
"%0 cannot be an extended escaping string literal", (StringRef))
9595

96+
ERROR(expected_identifier_in_module_selector,none,
97+
"expected module name in module selector", ())
98+
ERROR(expected_identifier_after_module_selector,none,
99+
"expected identifier after module selector", ())
100+
NOTE(extra_whitespace_module_selector,none,
101+
"remove extraneous whitespace after '::'", ())
102+
ERROR(module_selector_submodule_not_allowed,none,
103+
"module selector cannot specify a submodule", ())
104+
NOTE(replace_module_selector_with_member_lookup,none,
105+
"replace '::' with '.'", ())
106+
96107
//------------------------------------------------------------------------------
97108
// MARK: Lexer diagnostics
98109
//------------------------------------------------------------------------------

include/swift/AST/TokenKinds.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ PUNCTUATOR(period_prefix, ".")
203203
PUNCTUATOR(comma, ",")
204204
PUNCTUATOR(ellipsis, "...")
205205
PUNCTUATOR(colon, ":")
206+
PUNCTUATOR(colon_colon, "::")
206207
PUNCTUATOR(semi, ";")
207208
PUNCTUATOR(equal, "=")
208209
PUNCTUATOR(at_sign, "@")

include/swift/Parse/Parser.h

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -487,9 +487,8 @@ class Parser {
487487
/// \returns the value returned by \c f
488488
/// \note When calling, you may need to specify the \c Val type
489489
/// explicitly as a type parameter.
490-
template <typename Val>
491-
Val lookahead(unsigned char K,
492-
llvm::function_ref<Val(CancellableBacktrackingScope &)> f) {
490+
template <typename Fn>
491+
decltype(auto) lookahead(unsigned char K, Fn f) {
493492
CancellableBacktrackingScope backtrackScope(*this);
494493

495494
for (unsigned char i = 0; i < K; ++i)
@@ -776,6 +775,17 @@ class Parser {
776775
consumeStartingCharacterOfCurrentToken(tok Kind = tok::oper_binary_unspaced,
777776
size_t Len = 1);
778777

778+
/// If the next token is \c tok::colon, consume it; if the next token is
779+
/// \c tok::colon_colon, split it into two \c tok::colons and consume the
780+
/// first; otherwise, do nothing and return false.
781+
bool consumeIfColonSplittingDoubles() {
782+
if (!Tok.isAny(tok::colon, tok::colon_colon))
783+
return false;
784+
785+
consumeStartingCharacterOfCurrentToken(tok::colon);
786+
return true;
787+
}
788+
779789
//===--------------------------------------------------------------------===//
780790
// Primitive Parsing
781791

@@ -1067,7 +1077,7 @@ class Parser {
10671077
std::optional<bool> &Exported,
10681078
std::optional<SpecializeAttr::SpecializationKind> &Kind,
10691079
TrailingWhereClause *&TrailingWhereClause, DeclNameRef &targetFunction,
1070-
AvailabilityRange *SILAvailability,
1080+
DeclNameLoc &targetFunctionLoc, AvailabilityRange *SILAvailability,
10711081
SmallVectorImpl<Identifier> &spiGroups,
10721082
SmallVectorImpl<AvailableAttr *> &availableAttrs,
10731083
llvm::function_ref<bool(Parser &)> parseSILTargetName,
@@ -1810,6 +1820,23 @@ class Parser {
18101820
void parseOptionalArgumentLabel(Identifier &name, SourceLoc &loc,
18111821
bool isAttr = false);
18121822

1823+
/// If a \c module-selector is present, returns a true value (specifically,
1824+
/// 1 or 2 depending on how many tokens should be consumed to skip it).
1825+
unsigned isAtModuleSelector();
1826+
1827+
/// Attempts to parse a \c module-selector if one is present.
1828+
///
1829+
/// \verbatim
1830+
/// module-selector: identifier '::'
1831+
/// \endverbatim
1832+
///
1833+
/// \return \c None if no selector is present or a selector is present but
1834+
/// is not allowed; an instance with an empty \c Identifier if a
1835+
/// selector is present but has no valid identifier; an instance with
1836+
/// a valid \c Identifier if a selector is present and includes a
1837+
/// module name.
1838+
std::optional<Located<Identifier>> parseModuleSelector();
1839+
18131840
enum class DeclNameFlag : uint8_t {
18141841
/// If passed, operator basenames are allowed.
18151842
AllowOperators = 1 << 0,
@@ -1822,6 +1849,9 @@ class Parser {
18221849
/// not ordinary identifiers.
18231850
AllowKeywordsUsingSpecialNames = AllowKeywords | 1 << 2,
18241851

1852+
/// If passed, module selectors are not permitted on this declaration name.
1853+
ModuleSelectorUnsupported = 1 << 3,
1854+
18251855
/// If passed, compound names with argument lists are allowed, unless they
18261856
/// have empty argument lists.
18271857
AllowCompoundNames = 1 << 4,
@@ -1840,6 +1870,9 @@ class Parser {
18401870
return DeclNameOptions(flag1) | flag2;
18411871
}
18421872

1873+
/// Parse a declaration name that results in a `DeclNameRef` in the syntax
1874+
/// tree.
1875+
///
18431876
/// Without \c DeclNameFlag::AllowCompoundNames, parse an
18441877
/// unqualified-decl-base-name.
18451878
///
@@ -1863,7 +1896,8 @@ class Parser {
18631896
SourceLoc &rightAngleLoc, ArgumentList *&argList, bool isExprBasic,
18641897
DiagRef diag);
18651898

1866-
ParserResult<Expr> parseExprIdentifier(bool allowKeyword);
1899+
ParserResult<Expr> parseExprIdentifier(bool allowKeyword,
1900+
bool allowModuleSelector = true);
18671901
Expr *parseExprEditorPlaceholder(Token PlaceholderTok,
18681902
Identifier PlaceholderId);
18691903

0 commit comments

Comments
 (0)