Skip to content

Commit 8e967ad

Browse files
rintaronkcsgexi
authored andcommitted
[Syntax] Parse attributed type node
1 parent ed58c15 commit 8e967ad

File tree

5 files changed

+27
-2
lines changed

5 files changed

+27
-2
lines changed

lib/Parse/ParseDecl.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,6 +1618,8 @@ bool Parser::parseTypeAttribute(TypeAttributes &Attributes, bool justChecking) {
16181618

16191619
// Recover by eating @foo(...) when foo is not known.
16201620
consumeToken();
1621+
SyntaxParsingContext TokListContext(SyntaxContext, SyntaxKind::TokenList);
1622+
16211623
if (Tok.is(tok::l_paren) && getEndOfPreviousLoc() == Tok.getLoc()) {
16221624
ParserPosition LParenPosition = getParserPosition();
16231625
skipSingle();
@@ -1633,6 +1635,9 @@ bool Parser::parseTypeAttribute(TypeAttributes &Attributes, bool justChecking) {
16331635
// Ok, it is a valid attribute, eat it, and then process it.
16341636
StringRef Text = Tok.getText();
16351637
SourceLoc Loc = consumeToken();
1638+
1639+
// Accumulate attribute argument '( ... )' as a token list.
1640+
SyntaxParsingContext TokListContext(SyntaxContext, SyntaxKind::TokenList);
16361641

16371642
bool isAutoclosureEscaping = false;
16381643
SourceRange autoclosureEscapingParenRange;
@@ -1916,9 +1921,11 @@ bool Parser::parseTypeAttributeListPresent(VarDecl::Specifier &Specifier,
19161921
SpecifierLoc = consumeToken();
19171922
}
19181923

1924+
SyntaxParsingContext AttrListCtx(SyntaxContext, SyntaxKind::AttributeList);
19191925
while (Tok.is(tok::at_sign)) {
19201926
if (Attributes.AtLoc.isInvalid())
19211927
Attributes.AtLoc = Tok.getLoc();
1928+
SyntaxParsingContext AttrCtx(SyntaxContext, SyntaxKind::Attribute);
19221929
consumeToken();
19231930
if (parseTypeAttribute(Attributes))
19241931
return true;

lib/Parse/ParseType.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,8 @@ ParserResult<TypeRepr> Parser::parseType(Diag<> MessageID,
489489
if (tyR)
490490
tyR->walk(walker);
491491
}
492+
if (specifierLoc.isValid() || !attrs.empty())
493+
SyntaxContext->setCreateSyntax(SyntaxKind::AttributedType);
492494

493495
return makeParserResult(applyAttributeToType(tyR, attrs, specifier,
494496
specifierLoc));

test/Syntax/Outputs/round_trip_parse_gen.swift.withkinds

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ typealias E <TypeInitializerClause>= <MetatypeType><OptionalType><SimpleTypeIden
8080
typealias F <TypeInitializerClause>= <MetatypeType><ImplicitlyUnwrappedOptionalType><ArrayType>[<SimpleTypeIdentifier>Int</SimpleTypeIdentifier>]</ArrayType>!</ImplicitlyUnwrappedOptionalType>.Type</MetatypeType></TypeInitializerClause></TypealiasDecl><TypealiasDecl>
8181
typealias G <TypeInitializerClause>= <FunctionType>(<TupleTypeElement>a x: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier>, </TupleTypeElement><TupleTypeElement>_ y: <SimpleTypeIdentifier>Int </SimpleTypeIdentifier>... <InitializerClause>= <IntegerLiteralExpr>1</IntegerLiteralExpr></InitializerClause></TupleTypeElement>) throw -> <FunctionType>() -> <TupleType>()</TupleType></FunctionType></FunctionType></TypeInitializerClause></TypealiasDecl><TypealiasDecl>
8282
typealias H <TypeInitializerClause>= <FunctionType>() rethrows -> <TupleType>()</TupleType></FunctionType></TypeInitializerClause></TypealiasDecl><TypealiasDecl>
83-
typealias I <TypeInitializerClause>= <FunctionType>(<TupleTypeElement><CompositionType><CompositionTypeElement><SimpleTypeIdentifier>A </SimpleTypeIdentifier>& </CompositionTypeElement><CompositionTypeElement><SimpleTypeIdentifier>B<GenericArgumentClause><<GenericArgument><SimpleTypeIdentifier>C</SimpleTypeIdentifier></GenericArgument>></GenericArgumentClause></SimpleTypeIdentifier></CompositionTypeElement></CompositionType></TupleTypeElement>) -> <CompositionType><CompositionTypeElement><SimpleTypeIdentifier>C </SimpleTypeIdentifier>& </CompositionTypeElement><CompositionTypeElement><SimpleTypeIdentifier>D</SimpleTypeIdentifier></CompositionTypeElement></CompositionType></FunctionType></TypeInitializerClause></TypealiasDecl><TypealiasDecl><Attribute>
83+
typealias I <TypeInitializerClause>= <FunctionType>(<TupleTypeElement><CompositionType><CompositionTypeElement><SimpleTypeIdentifier>A </SimpleTypeIdentifier>& </CompositionTypeElement><CompositionTypeElement><SimpleTypeIdentifier>B<GenericArgumentClause><<GenericArgument><SimpleTypeIdentifier>C</SimpleTypeIdentifier></GenericArgument>></GenericArgumentClause></SimpleTypeIdentifier></CompositionTypeElement></CompositionType></TupleTypeElement>) -> <CompositionType><CompositionTypeElement><SimpleTypeIdentifier>C </SimpleTypeIdentifier>& </CompositionTypeElement><CompositionTypeElement><SimpleTypeIdentifier>D</SimpleTypeIdentifier></CompositionTypeElement></CompositionType></FunctionType></TypeInitializerClause></TypealiasDecl><TypealiasDecl>
84+
typealias J <TypeInitializerClause>= <AttributedType>inout <Attribute>@autoclosure </Attribute><FunctionType>() -> <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></FunctionType></AttributedType></TypeInitializerClause></TypealiasDecl><TypealiasDecl>
85+
typealias K <TypeInitializerClause>= <FunctionType>(<TupleTypeElement><Attribute>@invalidAttr </Attribute><SimpleTypeIdentifier>Int</SimpleTypeIdentifier>, </TupleTypeElement><TupleTypeElement><AttributedType>inout <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></AttributedType>, </TupleTypeElement><TupleTypeElement><AttributedType>__shared <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></AttributedType>, </TupleTypeElement><TupleTypeElement><AttributedType>__owned <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></AttributedType></TupleTypeElement>) -> <TupleType>()</TupleType></FunctionType></TypeInitializerClause></TypealiasDecl><TypealiasDecl><Attribute>
8486

8587
@objc </Attribute><DeclModifier>private </DeclModifier>typealias T<GenericParameterClause><<GenericParameter>a,</GenericParameter><GenericParameter>b</GenericParameter>> </GenericParameterClause><TypeInitializerClause>= <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></TypeInitializerClause></TypealiasDecl><TypealiasDecl><Attribute>
8688
@objc </Attribute><DeclModifier>private </DeclModifier>typealias T<GenericParameterClause><<GenericParameter>a,</GenericParameter><GenericParameter>b</GenericParameter>></GenericParameterClause></TypealiasDecl><ClassDecl>
@@ -131,7 +133,7 @@ func foo<FunctionSignature><ParameterClause>(<FunctionParameter>_ _: <SimpleType
131133
_ c: <SimpleTypeIdentifier>Int </SimpleTypeIdentifier><InitializerClause>= <IntegerLiteralExpr>2</IntegerLiteralExpr></InitializerClause>,</FunctionParameter><FunctionParameter>
132134
d _: <SimpleTypeIdentifier>Int </SimpleTypeIdentifier><InitializerClause>= <SequenceExpr><TernaryExpr><BooleanLiteralExpr>true </BooleanLiteralExpr>? <IntegerLiteralExpr>2</IntegerLiteralExpr>: <IntegerLiteralExpr>3</IntegerLiteralExpr></TernaryExpr></SequenceExpr></InitializerClause>,</FunctionParameter><FunctionParameter><Attribute>
133135
@objc </Attribute>e: <SimpleTypeIdentifier>X </SimpleTypeIdentifier><InitializerClause>= <BooleanLiteralExpr>true</BooleanLiteralExpr></InitializerClause>,</FunctionParameter><FunctionParameter>
134-
f: inout <SimpleTypeIdentifier>Int</SimpleTypeIdentifier>,</FunctionParameter><FunctionParameter>
136+
f: <AttributedType>inout <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></AttributedType>,</FunctionParameter><FunctionParameter>
135137
g: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier>...</FunctionParameter>) </ParameterClause>throws <ReturnClause>-> <DictionaryType>[<SimpleTypeIdentifier>Int</SimpleTypeIdentifier>: <SimpleTypeIdentifier>String</SimpleTypeIdentifier>] </DictionaryType></ReturnClause></FunctionSignature><CodeBlock>{}</CodeBlock></FunctionDecl><FunctionDecl>
136138

137139
func foo<FunctionSignature><ParameterClause>(<FunctionParameter>_ a: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></FunctionParameter>) </ParameterClause>throws <ReturnClause>-> <SimpleTypeIdentifier>Int </SimpleTypeIdentifier></ReturnClause></FunctionSignature><CodeBlock>{}</CodeBlock></FunctionDecl><FunctionDecl>

test/Syntax/round_trip_parse_gen.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ typealias F = [Int]!.Type
8181
typealias G = (a x: Int, _ y: Int ... = 1) throw -> () -> ()
8282
typealias H = () rethrows -> ()
8383
typealias I = (A & B<C>) -> C & D
84+
typealias J = inout @autoclosure () -> Int
85+
typealias K = (@invalidAttr Int, inout Int, __shared Int, __owned Int) -> ()
8486

8587
@objc private typealias T<a,b> = Int
8688
@objc private typealias T<a,b>

utils/gyb_syntax_support/TypeNodes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,18 @@
157157
Child('ReturnType', kind='Type'),
158158
]),
159159

160+
# attributed-type -> type-specifier? attribute-list? type
161+
# type-specifiyer -> 'inout' | '__owned' | '__unowned'
162+
Node('AttributedType', kind='Type',
163+
children=[
164+
Child('Specifier', kind='Token',
165+
text_choices=['inout', '__shared', '__owned'],
166+
is_optional=True),
167+
Child('Attributes', kind='AttributeList',
168+
is_optional=True),
169+
Child('BaseType', kind='Type'),
170+
]),
171+
160172
# generic-argument-list -> generic-argument generic-argument-list?
161173
Node('GenericArgumentList', kind='SyntaxCollection',
162174
element='GenericArgument'),

0 commit comments

Comments
 (0)