Skip to content

Commit 6edf769

Browse files
committed
Support module selectors in the syntax tree
1 parent edde0a1 commit 6edf769

File tree

11 files changed

+92
-5
lines changed

11 files changed

+92
-5
lines changed

lib/Parse/ParseExpr.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2295,6 +2295,9 @@ parseModuleSelector(ModuleSelectorReason reason, StringRef declKindName) {
22952295
tok::r_paren, tok::r_brace, tok::r_angle, tok::r_square))
22962296
return None;
22972297

2298+
SyntaxParsingContext selectorContext(SyntaxContext,
2299+
SyntaxKind::ModuleSelector);
2300+
22982301
// We will parse the selector whether or not it's allowed, then early return
22992302
// if it's disallowed, then diagnose any other errors in what we parsed. This
23002303
// will make sure we always consume the module selector's tokens, but don't
@@ -2307,6 +2310,7 @@ parseModuleSelector(ModuleSelectorReason reason, StringRef declKindName) {
23072310
// If we are only supposed to consume invalid selectors, leave this for
23082311
// later.
23092312
if (reason == ModuleSelectorReason::InvalidOnly) {
2313+
selectorContext.disable();
23102314
return None;
23112315
}
23122316

lib/Syntax/SyntaxFactory.cpp.gyb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,15 @@ DeclNameRefSyntax SyntaxFactory::makeDeclNameRef(Optional<StringRef> ModuleName,
274274
StringRef BaseName,
275275
StringRef LeadingTrivia,
276276
StringRef TrailingTrivia) {
277+
Optional<ModuleSelectorSyntax> selector;
278+
if (ModuleName) {
279+
auto moduleIdent = makeIdentifier(*ModuleName, LeadingTrivia, "");
280+
auto colonColon = makeColonColonToken("", "");
281+
selector.emplace(makeModuleSelector(moduleIdent, colonColon));
282+
LeadingTrivia = "";
283+
}
277284
auto baseNameIdent = makeIdentifier(BaseName, LeadingTrivia, TrailingTrivia);
278-
return makeDeclNameRef(baseNameIdent, None);
285+
return makeDeclNameRef(selector, baseNameIdent, None);
279286
}
280287

281288
TypeSyntax SyntaxFactory::makeTypeIdentifier(StringRef TypeName,

test/Syntax/Inputs/serialize_main_actor.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
{
132132
"kind": "DeclNameRef",
133133
"layout": [
134+
null,
134135
{
135136
"tokenKind": {
136137
"kind": "identifier",
@@ -197,6 +198,7 @@
197198
{
198199
"kind": "DeclNameRef",
199200
"layout": [
201+
null,
200202
{
201203
"tokenKind": {
202204
"kind": "identifier",
@@ -394,6 +396,7 @@
394396
{
395397
"kind": "DeclNameRef",
396398
"layout": [
399+
null,
397400
{
398401
"tokenKind": {
399402
"kind": "identifier",
@@ -441,6 +444,7 @@
441444
{
442445
"kind": "DeclNameRef",
443446
"layout": [
447+
null,
444448
{
445449
"tokenKind": {
446450
"kind": "identifier",
@@ -498,6 +502,7 @@
498502
{
499503
"kind": "DeclNameRef",
500504
"layout": [
505+
null,
501506
{
502507
"tokenKind": {
503508
"kind": "identifier",
@@ -555,6 +560,7 @@
555560
{
556561
"kind": "DeclNameRef",
557562
"layout": [
563+
null,
558564
{
559565
"tokenKind": {
560566
"kind": "identifier",

test/Syntax/Inputs/serialize_struct_decl.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
{
101101
"kind": "DeclNameRef",
102102
"layout": [
103+
null,
103104
{
104105
"tokenKind": {
105106
"kind": "identifier",
@@ -190,6 +191,7 @@
190191
{
191192
"kind": "DeclNameRef",
192193
"layout": [
194+
null,
193195
{
194196
"tokenKind": {
195197
"kind": "identifier",
@@ -226,6 +228,7 @@
226228
{
227229
"kind": "DeclNameRef",
228230
"layout": [
231+
null,
229232
{
230233
"tokenKind": {
231234
"kind": "identifier",

test/Syntax/Outputs/round_trip_parse_gen.swift.withkinds

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,4 +617,33 @@ foo</DeclNameRef></IdentifierExpr>()</FunctionCallExpr><IfConfigDecl><IfConfigCl
617617
.<DeclNameRef>member</DeclNameRef></MemberAccessExpr></IfConfigClause><IfConfigClause>
618618
#else<FunctionCallExpr><MemberAccessExpr>
619619
.<DeclNameRef>baz</DeclNameRef></MemberAccessExpr>() <ClosureExpr>{}</ClosureExpr></FunctionCallExpr></IfConfigClause>
620-
#endif</IfConfigDecl></PostfixIfConfigExpr>
620+
#endif</IfConfigDecl></PostfixIfConfigExpr><VariableDecl>
621+
622+
let <PatternBinding><IdentifierPattern>magnitude</IdentifierPattern><TypeAnnotation>: <MemberTypeIdentifier><SimpleTypeIdentifier><DeclNameRef><ModuleSelector>Swift::</ModuleSelector>Int</DeclNameRef></SimpleTypeIdentifier>.<DeclNameRef><ModuleSelector>Swift::</ModuleSelector>Magnitude </DeclNameRef></MemberTypeIdentifier></TypeAnnotation><InitializerClause>= <IntegerLiteralExpr>42</IntegerLiteralExpr></InitializerClause></PatternBinding></VariableDecl><ExtensionDecl>
623+
624+
extension <SimpleTypeIdentifier><DeclNameRef><ModuleSelector>ABC::</ModuleSelector>Z</DeclNameRef></SimpleTypeIdentifier><TypeInheritanceClause>: <InheritedType><SimpleTypeIdentifier><DeclNameRef><ModuleSelector>Swift::</ModuleSelector>Equatable </DeclNameRef></SimpleTypeIdentifier></InheritedType></TypeInheritanceClause><MemberDeclBlock>{<MemberDeclListItem><FunctionDecl><Attribute>
625+
@_implements(<ImplementsAttributeArguments><SimpleTypeIdentifier><DeclNameRef><ModuleSelector>Swift::</ModuleSelector>Equatable</DeclNameRef></SimpleTypeIdentifier>, <DeclNameRef>==<DeclNameArguments>(<DeclNameArgument>_:</DeclNameArgument><DeclNameArgument>_:</DeclNameArgument>)</DeclNameArguments></DeclNameRef></ImplementsAttributeArguments>)</Attribute><DeclModifier>
626+
public </DeclModifier><DeclModifier>static </DeclModifier>func equals<FunctionSignature><ParameterClause>(<FunctionParameter>_: <SimpleTypeIdentifier><DeclNameRef><ModuleSelector>ABC::</ModuleSelector>Z</DeclNameRef></SimpleTypeIdentifier>, </FunctionParameter><FunctionParameter>_: <SimpleTypeIdentifier><DeclNameRef>Z</DeclNameRef></SimpleTypeIdentifier></FunctionParameter>) </ParameterClause><ReturnClause>-> <SimpleTypeIdentifier><DeclNameRef><ModuleSelector>Swift::</ModuleSelector>Bool </DeclNameRef></SimpleTypeIdentifier></ReturnClause></FunctionSignature><CodeBlock>{<FunctionCallExpr><IdentifierExpr><DeclNameRef><ModuleSelector>
627+
Swift::</ModuleSelector>fatalError</DeclNameRef></IdentifierExpr>()</FunctionCallExpr>
628+
}</CodeBlock></FunctionDecl></MemberDeclListItem><MemberDeclListItem><FunctionDecl><Attribute>
629+
630+
@_dynamicReplacement(<NamedAttributeStringArgument>for: <DeclNameRef><ModuleSelector>ABC::</ModuleSelector>negate<DeclNameArguments>()</DeclNameArguments></DeclNameRef></NamedAttributeStringArgument>)</Attribute><DeclModifier>
631+
mutating </DeclModifier>func myNegate<FunctionSignature><ParameterClause>() </ParameterClause></FunctionSignature><CodeBlock>{<VariableDecl>
632+
let <PatternBinding><IdentifierPattern>fn</IdentifierPattern><TypeAnnotation>: <FunctionType>(<TupleTypeElement><SimpleTypeIdentifier><DeclNameRef><ModuleSelector>Swift::</ModuleSelector>Int</DeclNameRef></SimpleTypeIdentifier>, </TupleTypeElement><TupleTypeElement><SimpleTypeIdentifier><DeclNameRef><ModuleSelector>Swift::</ModuleSelector>Int</DeclNameRef></SimpleTypeIdentifier></TupleTypeElement>) -> <SimpleTypeIdentifier><DeclNameRef><ModuleSelector>Swift::</ModuleSelector>Int </DeclNameRef></SimpleTypeIdentifier></FunctionType></TypeAnnotation><InitializerClause>= <TupleExpr>(<TupleExprElement><IdentifierExpr><DeclNameRef><ModuleSelector>Swift::</ModuleSelector>+</DeclNameRef></IdentifierExpr></TupleExprElement>)</TupleExpr></InitializerClause></PatternBinding></VariableDecl><VariableDecl>
633+
634+
let <PatternBinding><IdentifierPattern>magnitude</IdentifierPattern><TypeAnnotation>: <MemberTypeIdentifier><SimpleTypeIdentifier><DeclNameRef>Int</DeclNameRef></SimpleTypeIdentifier>.<DeclNameRef><ModuleSelector>Swift::</ModuleSelector>Magnitude </DeclNameRef></MemberTypeIdentifier></TypeAnnotation><InitializerClause>= <IdentifierExpr><DeclNameRef><ModuleSelector>round_trip_parse_gen::</ModuleSelector>magnitude</DeclNameRef></IdentifierExpr></InitializerClause></PatternBinding></VariableDecl><IfStmt>
635+
636+
if <ConditionElement><FunctionCallExpr><MemberAccessExpr><IdentifierExpr><DeclNameRef><ModuleSelector>Swift::</ModuleSelector>Bool</DeclNameRef></IdentifierExpr>.<DeclNameRef><ModuleSelector>Swift::</ModuleSelector>random</DeclNameRef></MemberAccessExpr>() </FunctionCallExpr></ConditionElement><CodeBlock>{<FunctionCallExpr><MemberAccessExpr><IdentifierExpr><DeclNameRef>
637+
self</DeclNameRef></IdentifierExpr>.<DeclNameRef><ModuleSelector>round_trip_parse_gen::</ModuleSelector>negate</DeclNameRef></MemberAccessExpr>()</FunctionCallExpr>
638+
}</CodeBlock>
639+
else <CodeBlock>{<SequenceExpr><IdentifierExpr><DeclNameRef>
640+
self </DeclNameRef></IdentifierExpr><AssignmentExpr>= </AssignmentExpr><FunctionCallExpr><IdentifierExpr><DeclNameRef><ModuleSelector>ABC::</ModuleSelector>Z</DeclNameRef></IdentifierExpr>(<TupleExprElement>value: <MemberAccessExpr>.<DeclNameRef><ModuleSelector>Swift::</ModuleSelector>min</DeclNameRef></MemberAccessExpr></TupleExprElement>)</FunctionCallExpr></SequenceExpr><SequenceExpr><IdentifierExpr><DeclNameRef>
641+
self </DeclNameRef></IdentifierExpr><AssignmentExpr>= </AssignmentExpr><FunctionCallExpr><MemberAccessExpr><IdentifierExpr><DeclNameRef>A</DeclNameRef></IdentifierExpr>.<DeclNameRef><ModuleSelector>ABC::</ModuleSelector>init</DeclNameRef></MemberAccessExpr>(<TupleExprElement>value: <MemberAccessExpr>.<DeclNameRef>min</DeclNameRef></MemberAccessExpr></TupleExprElement>)</FunctionCallExpr></SequenceExpr>
642+
}</CodeBlock></IfStmt><FunctionCallExpr><MemberAccessExpr><IdentifierExpr><DeclNameRef>
643+
644+
self</DeclNameRef></IdentifierExpr>.<DeclNameRef><ModuleSelector>ABC::</ModuleSelector>myNegate</DeclNameRef></MemberAccessExpr>()</FunctionCallExpr><FunctionCallExpr><IdentifierExpr><DeclNameRef><ModuleSelector>
645+
646+
Swift::</ModuleSelector>fatalError</DeclNameRef></IdentifierExpr>()</FunctionCallExpr>
647+
}</CodeBlock></FunctionDecl></MemberDeclListItem>
648+
}</MemberDeclBlock></ExtensionDecl>
649+

test/Syntax/Parser/tree.swift.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
// RUN: diff -u %s.result %t.result
44

55
|func| </t6><t105>|test|</t105><NULL/><s110><s108><t88>|(|</t88><s174></s174><t89>|)| </t89></s108><NULL/><NULL/><NULL/></s110><NULL/><s93><t90>|{|</t90><s163><s92><s48><NULL/><t102>
6-
|"|</t102><s168><s104><t104>|a|</t104></s104><s105><t100>|\|</t100><NULL/><t88>|(|</t88><s165><s97><NULL/><NULL/><s28><s253><t105>|b|</t105><NULL/></s253></s28><NULL/></s97></s165><t101>|)|</t101></s105><s104><t104>|c|</t104></s104></s168><t102>|"|</t102><NULL/></s48><NULL/><NULL/></s92></s163><t91>
6+
|"|</t102><s168><s104><t104>|a|</t104></s104><s105><t100>|\|</t100><NULL/><t88>|(|</t88><s165><s97><NULL/><NULL/><s28><s253><NULL/><t105>|b|</t105><NULL/></s253></s28><NULL/></s97></s165><t101>|)|</t101></s105><s104><t104>|c|</t104></s104></s168><t102>|"|</t102><NULL/></s48><NULL/><NULL/></s92></s163><t91>
77
|}|</t91></s93></s13><NULL/><NULL/></s92></s163><t0>
88
||</t0></s118>

test/Syntax/round_trip_parse_gen.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,3 +618,31 @@ foo()
618618
#else
619619
.baz() {}
620620
#endif
621+
622+
let magnitude: Swift::Int.Swift::Magnitude = 42
623+
624+
extension ABC::Z: Swift::Equatable {
625+
@_implements(Swift::Equatable, ==(_:_:))
626+
public static func equals(_: ABC::Z, _: Z) -> Swift::Bool {
627+
Swift::fatalError()
628+
}
629+
630+
@_dynamicReplacement(for: ABC::negate())
631+
mutating func myNegate() {
632+
let fn: (Swift::Int, Swift::Int) -> Swift::Int = (Swift::+)
633+
634+
let magnitude: Int.Swift::Magnitude = round_trip_parse_gen::magnitude
635+
636+
if Swift::Bool.Swift::random() {
637+
self.round_trip_parse_gen::negate()
638+
}
639+
else {
640+
self = ABC::Z(value: .Swift::min)
641+
self = A.ABC::init(value: .min)
642+
}
643+
644+
self.ABC::myNegate()
645+
646+
Swift::fatalError()
647+
}
648+
}

test/Syntax/serialize_tupletype.swift.result

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
{
9191
"kind": "DeclNameRef",
9292
"layout": [
93+
null,
9394
{
9495
"tokenKind": {
9596
"kind": "identifier",
@@ -147,6 +148,7 @@
147148
{
148149
"kind": "DeclNameRef",
149150
"layout": [
151+
null,
150152
{
151153
"tokenKind": {
152154
"kind": "identifier",

tools/swift-syntax-test/swift-syntax-test.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,7 @@ int parseFile(
581581
Invocation.getLangOptions().ParseForSyntaxTreeOnly = true;
582582
Invocation.getLangOptions().VerifySyntaxTree = options::VerifySyntaxTree;
583583
Invocation.getLangOptions().DisablePoundIfEvaluation = true;
584+
Invocation.getLangOptions().EnableExperimentalModuleSelector = true;
584585

585586
Invocation.getFrontendOptions().InputsAndOutputs.addInputFile(InputFileName);
586587

utils/gyb_syntax_support/CommonNodes.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@
5252
requires_leading_newline=True),
5353
]),
5454

55+
# module-selector -> identifier '::'
56+
Node('ModuleSelector', kind='Syntax',
57+
children=[
58+
Child('ModuleName', kind='IdentifierToken'),
59+
Child('SelectorOperator', kind='ColonColonToken'),
60+
]),
61+
5562
# decl-name-arguments -> '(' declname-argument-list ')'
5663
# decl-name-argument-list -> declname-argument*
5764
# decl-name-argument -> identifier ':'
@@ -77,7 +84,7 @@
7784
# decl-name-ref -> module-selector? token /* usually identifier */ decl-name-arguments?
7885
Node('DeclNameRef', kind='Syntax',
7986
children=[
80-
# Child('Module', kind='ModuleSelector', is_optional=True),
87+
Child('Module', kind='ModuleSelector', is_optional=True),
8188
Child('BaseName', kind='Token'),
8289
Child('ArgLabels', kind='DeclNameArguments', is_optional=True),
8390
]),

0 commit comments

Comments
 (0)