Skip to content

Commit f919b2d

Browse files
committed
[SyntaxParse] Parse generic parameter clause and generic where clause
1 parent b09f875 commit f919b2d

33 files changed

+634
-364
lines changed

cmake/modules/SwiftHandleGybSources.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ function(handle_gyb_sources dependency_out_var_name sources_var_name arch)
121121
"${SWIFT_SOURCE_DIR}/utils/gyb_syntax_support/DeclNodes.py"
122122
"${SWIFT_SOURCE_DIR}/utils/gyb_syntax_support/ExprNodes.py"
123123
"${SWIFT_SOURCE_DIR}/utils/gyb_syntax_support/GenericNodes.py"
124+
"${SWIFT_SOURCE_DIR}/utils/gyb_syntax_support/NodeSerializationCodes.py"
124125
"${SWIFT_SOURCE_DIR}/utils/gyb_syntax_support/PatternNodes.py"
126+
"${SWIFT_SOURCE_DIR}/utils/gyb_syntax_support/SILOnlyNodes.py"
125127
"${SWIFT_SOURCE_DIR}/utils/gyb_syntax_support/StmtNodes.py"
126128
"${SWIFT_SOURCE_DIR}/utils/gyb_syntax_support/TypeNodes.py"
127129
"${SWIFT_SOURCE_DIR}/utils/gyb_syntax_support/Token.py"

include/swift/Parse/ASTGen.h

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,24 @@
2222

2323
namespace swift {
2424
/// Generates AST nodes from Syntax nodes.
25+
class Parser;
2526
class ASTGen {
2627
ASTContext &Context;
2728

2829
/// Type cache to prevent multiple transformations of the same syntax node.
2930
llvm::DenseMap<syntax::SyntaxNodeId, TypeRepr *> TypeCache;
3031

31-
PersistentParserState **ParserState;
32+
Parser &P;
3233

3334
// FIXME: remove when Syntax can represent all types and ASTGen can handle them
3435
/// Types that cannot be represented by Syntax or generated by ASTGen.
3536
llvm::DenseMap<SourceLoc, TypeRepr *> Types;
3637

38+
llvm::DenseMap<SourceLoc, DeclAttributes> ParsedDeclAttrs;
39+
3740
public:
38-
ASTGen(ASTContext &Context, PersistentParserState **ParserState)
39-
: Context(Context), ParserState(ParserState) {}
41+
ASTGen(ASTContext &Context, Parser &P)
42+
: Context(Context), P(P) {}
4043

4144
SourceLoc generate(syntax::TokenSyntax Tok, SourceLoc &Loc);
4245

@@ -70,6 +73,15 @@ class ASTGen {
7073
llvm::SmallVector<TypeRepr *, 4>
7174
generate(syntax::GenericArgumentListSyntax Args, SourceLoc &Loc);
7275

76+
GenericParamList *
77+
generate(syntax::GenericParameterClauseListSyntax clause, SourceLoc &Loc);
78+
GenericParamList *
79+
generate(syntax::GenericParameterClauseSyntax clause, SourceLoc &Loc);
80+
Optional<RequirementRepr>
81+
generate(syntax::GenericRequirementSyntax req, SourceLoc &Loc);
82+
LayoutConstraint
83+
generate(syntax::LayoutConstraintSyntax req, SourceLoc &Loc);
84+
7385
/// Copy a numeric literal value into AST-owned memory, stripping underscores
7486
/// so the semantic part of the value can be parsed by APInt/APFloat parsers.
7587
static StringRef copyAndStripUnderscores(StringRef Orig, ASTContext &Context);
@@ -102,6 +114,8 @@ class ASTGen {
102114

103115
ValueDecl *lookupInScope(DeclName Name);
104116

117+
void addToScope(ValueDecl *D, bool diagnoseRedefinitions = true);
118+
105119
TypeRepr *cacheType(syntax::TypeSyntax Type, TypeRepr *TypeAST);
106120

107121
TypeRepr *lookupType(syntax::TypeSyntax Type);
@@ -112,6 +126,10 @@ class ASTGen {
112126
bool hasType(const SourceLoc &Loc) const;
113127

114128
TypeRepr *getType(const SourceLoc &Loc) const;
129+
130+
void addDeclAttributes(DeclAttributes attrs, SourceLoc Loc);
131+
bool hasDeclAttributes(SourceLoc Loc) const;
132+
DeclAttributes getDeclAttributes(SourceLoc Loc) const;
115133
};
116134
} // namespace swift
117135

include/swift/Parse/Lexer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ class Lexer {
200200
ParsedTrivia &TrailingTriviaResult) {
201201
Result = NextToken;
202202
if (TriviaRetention == TriviaRetentionMode::WithTrivia) {
203-
LeadingTriviaResult = {LeadingTrivia};
204-
TrailingTriviaResult = {TrailingTrivia};
203+
std::swap(LeadingTriviaResult, LeadingTrivia);
204+
std::swap(TrailingTriviaResult, TrailingTrivia);
205205
}
206206
if (Result.isNot(tok::eof))
207207
lexImpl();

include/swift/Parse/ParsedSyntaxBuilders.h.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace swift {
3030
class ParsedRawSyntaxRecorder;
3131
class SyntaxParsingContext;
3232

33-
% for node in SYNTAX_NODES:
33+
% for node in SYNTAX_NODES + SILONLY_NODES:
3434
% if node.is_buildable():
3535
% child_count = len(node.children)
3636
class Parsed${node.name}Builder {

include/swift/Parse/ParsedSyntaxNodes.h.gyb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,20 @@ namespace swift {
2828
% # Emit the non-collection classes first, then emit the collection classes
2929
% # that reference these classes.
3030

31-
% for node in SYNTAX_NODES:
31+
% for node in SYNTAX_NODES + SILONLY_NODES:
3232
% if not node.is_syntax_collection():
3333
class Parsed${node.name};
3434
% end
3535
% end
3636

37-
% for node in SYNTAX_NODES:
37+
% for node in SYNTAX_NODES + SILONLY_NODES:
3838
% if node.is_syntax_collection():
3939
using Parsed${node.name} =
4040
ParsedSyntaxCollection<syntax::SyntaxKind::${node.syntax_kind}>;
4141
% end
4242
% end
4343

44-
% for node in SYNTAX_NODES:
44+
% for node in SYNTAX_NODES + SILONLY_NODES:
4545
% if not node.is_syntax_collection():
4646
% qualifier = "" if node.is_base() else "final"
4747
% for line in dedented_lines(node.description):

include/swift/Parse/ParsedSyntaxRecorder.h.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class SyntaxParsingContext;
3131

3232
struct ParsedSyntaxRecorder {
3333

34-
% for node in SYNTAX_NODES:
34+
% for node in SYNTAX_NODES + SILONLY_NODES:
3535
% if node.children:
3636
% child_params = []
3737
% for child in node.children:

include/swift/Parse/Parser.h

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -695,11 +695,19 @@ class Parser {
695695
/// plain Tok.is(T1) check).
696696
bool skipUntilTokenOrEndOfLine(tok T1);
697697

698+
//-------------------------------------------------------------------------//
699+
// Ignore token APIs.
700+
// This is used when we skip gabage text in the source text.
701+
702+
/// Ignore the current single token.
698703
void ignoreToken();
699704
void ignoreToken(tok Kind) {
705+
/// Ignore the current single token asserting its kind.
700706
assert(Tok.is(Kind));
701707
ignoreToken();
702708
}
709+
/// Conditionally ignore the current single token if it matches with the \p
710+
/// Kind.
703711
bool ignoreIf(tok Kind) {
704712
if (!Tok.is(Kind))
705713
return false;
@@ -1173,7 +1181,8 @@ class Parser {
11731181
using TypeASTResult = ParserResult<TypeRepr>;
11741182
using TypeResult = ParsedSyntaxResult<ParsedTypeSyntax>;
11751183

1176-
LayoutConstraint parseLayoutConstraint(Identifier LayoutConstraintID);
1184+
ParsedSyntaxResult<ParsedLayoutConstraintSyntax>
1185+
parseLayoutConstraintSyntax();
11771186

11781187
TypeResult parseTypeSyntax();
11791188
TypeResult parseTypeSyntax(Diag<> MessageID, bool HandleCodeCompletion = true,
@@ -1597,8 +1606,19 @@ class Parser {
15971606
//===--------------------------------------------------------------------===//
15981607
// Generics Parsing
15991608

1609+
ParserResult<GenericParamList> parseSILGenericParams();
1610+
1611+
ParserStatus parseSILGenericParamsSyntax(
1612+
Optional<ParsedGenericParameterClauseListSyntax> &result);
1613+
1614+
ParsedSyntaxResult<ParsedGenericParameterClauseSyntax>
1615+
parseGenericParameterClauseSyntax();
1616+
1617+
ParsedSyntaxResult<ParsedGenericWhereClauseSyntax>
1618+
parseGenericWhereClauseSyntax(bool &FirstTypeInComplete,
1619+
bool AllowLayoutConstraints = false);
1620+
16001621
ParserResult<GenericParamList> parseGenericParameters();
1601-
ParserResult<GenericParamList> parseGenericParameters(SourceLoc LAngleLoc);
16021622
ParserStatus parseGenericParametersBeforeWhere(SourceLoc LAngleLoc,
16031623
SmallVectorImpl<GenericTypeParamDecl *> &GenericParams);
16041624
ParserResult<GenericParamList> maybeParseGenericParams();

include/swift/Syntax/SyntaxBuilders.h.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace syntax {
2929

3030
class SyntaxArena;
3131

32-
% for node in SYNTAX_NODES:
32+
% for node in SYNTAX_NODES + SILONLY_NODES:
3333
% if node.is_buildable():
3434
% child_count = len(node.children)
3535
class ${node.name}Builder {

include/swift/Syntax/SyntaxFactory.h.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ struct SyntaxFactory {
7171
static Syntax
7272
makeBlankCollectionSyntax(SyntaxKind Kind);
7373

74-
% for node in SYNTAX_NODES:
74+
% for node in SYNTAX_NODES + SILONLY_NODES:
7575
% if node.children:
7676
% child_params = []
7777
% for child in node.children:

include/swift/Syntax/SyntaxKind.h.gyb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from gyb_syntax_support import *
33
from gyb_syntax_support.kinds import SYNTAX_BASE_KINDS
44
grouped_nodes = { kind: [] for kind in SYNTAX_BASE_KINDS }
5-
for node in SYNTAX_NODES:
5+
for node in SYNTAX_NODES + SILONLY_NODES:
66
grouped_nodes[node.base_kind].append(node)
77
# -*- mode: C++ -*-
88
# Ignore the following admonition; it applies to the resulting .h file only
@@ -89,12 +89,14 @@ struct WrapperTypeTraits<syntax::SyntaxKind> {
8989
return 0;
9090
case syntax::SyntaxKind::Unknown:
9191
return 1;
92-
% for name, nodes in grouped_nodes.items():
93-
% for node in nodes:
92+
% for node in SYNTAX_NODES:
9493
case syntax::SyntaxKind::${node.syntax_kind}:
9594
return ${SYNTAX_NODE_SERIALIZATION_CODES[node.syntax_kind]};
96-
% end
9795
% end
96+
% for node in SILONLY_NODES:
97+
case syntax::SyntaxKind::${node.syntax_kind}:
98+
% end
99+
llvm_unreachable("unserializable syntax kind");
98100
}
99101
llvm_unreachable("unhandled kind");
100102
}
@@ -122,6 +124,10 @@ struct ScalarReferenceTraits<syntax::SyntaxKind> {
122124
case syntax::SyntaxKind::${node.syntax_kind}:
123125
return "\"${node.syntax_kind}\"";
124126
% end
127+
% for node in SILONLY_NODES:
128+
case syntax::SyntaxKind::${node.syntax_kind}:
129+
% end
130+
llvm_unreachable("unserializable syntax kind");
125131
}
126132
llvm_unreachable("unhandled kind");
127133
}

include/swift/Syntax/SyntaxNodes.h.gyb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,21 @@ namespace syntax {
3232
% # Emit the non-collection classes first, then emit the collection classes
3333
% # that reference these classes.
3434

35-
% for node in SYNTAX_NODES:
35+
% for node in SYNTAX_NODES + SILONLY_NODES:
3636
% if not node.is_syntax_collection():
3737
class ${node.name};
3838
% end
3939
% end
4040

41-
% for node in SYNTAX_NODES:
41+
% for node in SYNTAX_NODES + SILONLY_NODES:
4242
% if node.is_syntax_collection():
4343
using ${node.name} =
4444
SyntaxCollection<SyntaxKind::${node.syntax_kind},
4545
${node.collection_element_type}>;
4646
% end
4747
% end
4848

49-
% for node in SYNTAX_NODES:
49+
% for node in SYNTAX_NODES + SILONLY_NODES:
5050
% if not node.is_syntax_collection():
5151
% qualifier = "" if node.is_base() else "final"
5252
% for line in dedented_lines(node.description):

include/swift/Syntax/SyntaxVisitor.h.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace syntax {
3232
struct SyntaxVisitor {
3333
virtual ~SyntaxVisitor() {}
3434

35-
% for node in SYNTAX_NODES:
35+
% for node in SYNTAX_NODES + SILONLY_NODES:
3636
% if is_visitable(node):
3737
virtual void visit(${node.name} node);
3838
% end

0 commit comments

Comments
 (0)