Skip to content

Commit e315b6f

Browse files
authored
Merge pull request #27291 from rintaro/syntaxparses-astgen-signature
[ASTGen] Adjust geneate() function signatures
2 parents 079a1a4 + fde58a9 commit e315b6f

File tree

6 files changed

+190
-127
lines changed

6 files changed

+190
-127
lines changed

include/swift/Parse/ASTGen.h

Lines changed: 105 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -31,87 +31,130 @@ class ASTGen {
3131

3232
Parser &P;
3333

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

3839
llvm::DenseMap<SourceLoc, DeclAttributes> ParsedDeclAttrs;
3940

4041
public:
41-
ASTGen(ASTContext &Context, Parser &P)
42-
: Context(Context), P(P) {}
43-
44-
SourceLoc generate(syntax::TokenSyntax Tok, SourceLoc &Loc);
45-
46-
Expr *generate(syntax::IntegerLiteralExprSyntax &Expr, SourceLoc &Loc);
47-
Expr *generate(syntax::FloatLiteralExprSyntax &Expr, SourceLoc &Loc);
48-
Expr *generate(syntax::NilLiteralExprSyntax &Expr, SourceLoc &Loc);
49-
Expr *generate(syntax::BooleanLiteralExprSyntax &Expr, SourceLoc &Loc);
50-
Expr *generate(syntax::PoundFileExprSyntax &Expr, SourceLoc &Loc);
51-
Expr *generate(syntax::PoundLineExprSyntax &Expr, SourceLoc &Loc);
52-
Expr *generate(syntax::PoundColumnExprSyntax &Expr, SourceLoc &Loc);
53-
Expr *generate(syntax::PoundFunctionExprSyntax &Expr, SourceLoc &Loc);
54-
Expr *generate(syntax::PoundDsohandleExprSyntax &Expr, SourceLoc &Loc);
55-
Expr *generate(syntax::UnknownExprSyntax &Expr, SourceLoc &Loc);
56-
57-
TypeRepr *generate(syntax::TypeSyntax Type, SourceLoc &Loc);
58-
TypeRepr *generate(syntax::SomeTypeSyntax Type, SourceLoc &Loc);
59-
TypeRepr *generate(syntax::CompositionTypeSyntax Type, SourceLoc &Loc);
60-
TypeRepr *generate(syntax::SimpleTypeIdentifierSyntax Type, SourceLoc &Loc);
61-
TypeRepr *generate(syntax::MemberTypeIdentifierSyntax Type, SourceLoc &Loc);
62-
TypeRepr *generate(syntax::DictionaryTypeSyntax Type, SourceLoc &Loc);
63-
TypeRepr *generate(syntax::ArrayTypeSyntax Type, SourceLoc &Loc);
64-
TypeRepr *generate(syntax::TupleTypeSyntax Type, SourceLoc &Loc);
65-
TypeRepr *generate(syntax::AttributedTypeSyntax Type, SourceLoc &Loc);
66-
TypeRepr *generate(syntax::FunctionTypeSyntax Type, SourceLoc &Loc);
67-
TypeRepr *generate(syntax::MetatypeTypeSyntax Type, SourceLoc &Loc);
68-
TypeRepr *generate(syntax::OptionalTypeSyntax Type, SourceLoc &Loc);
69-
TypeRepr *generate(syntax::ImplicitlyUnwrappedOptionalTypeSyntax Type, SourceLoc &Loc);
70-
TypeRepr *generate(syntax::UnknownTypeSyntax Type, SourceLoc &Loc);
71-
72-
TypeRepr *generate(syntax::GenericArgumentSyntax Arg, SourceLoc &Loc);
73-
llvm::SmallVector<TypeRepr *, 4>
74-
generate(syntax::GenericArgumentListSyntax Args, SourceLoc &Loc);
42+
ASTGen(ASTContext &Context, Parser &P) : Context(Context), P(P) {}
7543

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);
44+
SourceLoc generate(const syntax::TokenSyntax &Tok, const SourceLoc Loc);
8445

85-
/// Copy a numeric literal value into AST-owned memory, stripping underscores
86-
/// so the semantic part of the value can be parsed by APInt/APFloat parsers.
87-
static StringRef copyAndStripUnderscores(StringRef Orig, ASTContext &Context);
46+
public:
47+
//===--------------------------------------------------------------------===//
48+
// Expressions.
49+
50+
Expr *generate(const syntax::IntegerLiteralExprSyntax &Expr,
51+
const SourceLoc Loc);
52+
Expr *generate(const syntax::FloatLiteralExprSyntax &Expr,
53+
const SourceLoc Loc);
54+
Expr *generate(const syntax::NilLiteralExprSyntax &Expr, const SourceLoc Loc);
55+
Expr *generate(const syntax::BooleanLiteralExprSyntax &Expr,
56+
const SourceLoc Loc);
57+
Expr *generate(const syntax::PoundFileExprSyntax &Expr, const SourceLoc Loc);
58+
Expr *generate(const syntax::PoundLineExprSyntax &Expr, const SourceLoc Loc);
59+
Expr *generate(const syntax::PoundColumnExprSyntax &Expr,
60+
const SourceLoc Loc);
61+
Expr *generate(const syntax::PoundFunctionExprSyntax &Expr,
62+
const SourceLoc Loc);
63+
Expr *generate(const syntax::PoundDsohandleExprSyntax &Expr,
64+
const SourceLoc Loc);
65+
Expr *generate(const syntax::UnknownExprSyntax &Expr, const SourceLoc Loc);
8866

8967
private:
90-
Expr *generateMagicIdentifierLiteralExpression(syntax::TokenSyntax PoundToken,
91-
SourceLoc &Loc);
68+
Expr *generateMagicIdentifierLiteralExpression(
69+
const syntax::TokenSyntax &PoundToken, const SourceLoc Loc);
9270

93-
TupleTypeRepr *generateTuple(syntax::TokenSyntax LParen,
94-
syntax::TupleTypeElementListSyntax Elements,
95-
syntax::TokenSyntax RParen, SourceLoc &Loc,
96-
bool IsFunction = false);
71+
static MagicIdentifierLiteralExpr::Kind
72+
getMagicIdentifierLiteralKind(tok Kind);
73+
74+
public:
75+
//===--------------------------------------------------------------------===//
76+
// Types.
77+
78+
TypeRepr *generate(const syntax::TypeSyntax &Type, const SourceLoc Loc);
79+
TypeRepr *generate(const syntax::SomeTypeSyntax &Type, const SourceLoc Loc);
80+
TypeRepr *generate(const syntax::CompositionTypeSyntax &Type,
81+
const SourceLoc Loc);
82+
TypeRepr *generate(const syntax::SimpleTypeIdentifierSyntax &Type,
83+
const SourceLoc Loc);
84+
TypeRepr *generate(const syntax::MemberTypeIdentifierSyntax &Type,
85+
const SourceLoc Loc);
86+
TypeRepr *generate(const syntax::DictionaryTypeSyntax &Type,
87+
const SourceLoc Loc);
88+
TypeRepr *generate(const syntax::ArrayTypeSyntax &Type, const SourceLoc Loc);
89+
TypeRepr *generate(const syntax::TupleTypeSyntax &Type, const SourceLoc Loc);
90+
TypeRepr *generate(const syntax::AttributedTypeSyntax &Type,
91+
const SourceLoc Loc);
92+
TypeRepr *generate(const syntax::FunctionTypeSyntax &Type,
93+
const SourceLoc Loc);
94+
TypeRepr *generate(const syntax::MetatypeTypeSyntax &Type,
95+
const SourceLoc Loc);
96+
TypeRepr *generate(const syntax::OptionalTypeSyntax &Type,
97+
const SourceLoc Loc);
98+
TypeRepr *generate(const syntax::ImplicitlyUnwrappedOptionalTypeSyntax &Type,
99+
const SourceLoc Loc);
100+
TypeRepr *generate(const syntax::UnknownTypeSyntax &Type,
101+
const SourceLoc Loc);
102+
103+
private:
104+
TupleTypeRepr *
105+
generateTuple(const syntax::TokenSyntax &LParen,
106+
const syntax::TupleTypeElementListSyntax &Elements,
107+
const syntax::TokenSyntax &RParen, const SourceLoc Loc,
108+
bool IsFunction = false);
97109

98110
void gatherTypeIdentifierComponents(
99-
syntax::TypeSyntax Component, SourceLoc &Loc,
111+
const syntax::TypeSyntax &Component, const SourceLoc Loc,
100112
llvm::SmallVectorImpl<ComponentIdentTypeRepr *> &Components);
101113

102114
template <typename T>
103-
TypeRepr *generateSimpleOrMemberIdentifier(T Type, SourceLoc &Loc);
115+
TypeRepr *generateSimpleOrMemberIdentifier(const T &Type,
116+
const SourceLoc Loc);
104117

105118
template <typename T>
106-
ComponentIdentTypeRepr *generateIdentifier(T Type, SourceLoc &Loc);
119+
ComponentIdentTypeRepr *generateIdentifier(const T &Type,
120+
const SourceLoc Loc);
107121

122+
public:
123+
//===--------------------------------------------------------------------===//
124+
// Generics.
125+
126+
TypeRepr *generate(const syntax::GenericArgumentSyntax &Arg,
127+
const SourceLoc Loc);
128+
llvm::SmallVector<TypeRepr *, 4>
129+
generate(const syntax::GenericArgumentListSyntax &Args, const SourceLoc Loc);
130+
131+
GenericParamList *
132+
generate(const syntax::GenericParameterClauseListSyntax &clause,
133+
const SourceLoc Loc);
134+
GenericParamList *generate(const syntax::GenericParameterClauseSyntax &clause,
135+
const SourceLoc Loc);
136+
Optional<RequirementRepr>
137+
generate(const syntax::GenericRequirementSyntax &req, const SourceLoc Loc);
138+
LayoutConstraint generate(const syntax::LayoutConstraintSyntax &req,
139+
const SourceLoc Loc);
140+
141+
public:
142+
//===--------------------------------------------------------------------===//
143+
// Utilities.
144+
145+
/// Copy a numeric literal value into AST-owned memory, stripping underscores
146+
/// so the semantic part of the value can be parsed by APInt/APFloat parsers.
147+
static StringRef copyAndStripUnderscores(StringRef Orig, ASTContext &Context);
148+
149+
private:
108150
StringRef copyAndStripUnderscores(StringRef Orig);
109151

152+
/// Advance \p Loc to the first token of the \p Node.
153+
/// \p Loc must be the leading trivia of the first token in the tree in which
154+
/// \p Node resides.
110155
static SourceLoc advanceLocBegin(const SourceLoc &Loc,
111156
const syntax::Syntax &Node);
112157

113-
static MagicIdentifierLiteralExpr::Kind getMagicIdentifierLiteralKind(tok Kind);
114-
115158
ValueDecl *lookupInScope(DeclName Name);
116159

117160
void addToScope(ValueDecl *D, bool diagnoseRedefinitions = true);
@@ -121,15 +164,13 @@ class ASTGen {
121164
TypeRepr *lookupType(syntax::TypeSyntax Type);
122165

123166
public:
124-
TypeRepr *addType(TypeRepr *Type, const SourceLoc &Loc);
125-
126-
bool hasType(const SourceLoc &Loc) const;
127-
128-
TypeRepr *getType(const SourceLoc &Loc) const;
167+
void addType(TypeRepr *Type, const SourceLoc Loc);
168+
bool hasType(const SourceLoc Loc) const;
169+
TypeRepr *getType(const SourceLoc Loc) const;
129170

130-
void addDeclAttributes(DeclAttributes attrs, SourceLoc Loc);
171+
void addDeclAttributes(DeclAttributes attrs, const SourceLoc Loc);
131172
bool hasDeclAttributes(SourceLoc Loc) const;
132-
DeclAttributes getDeclAttributes(SourceLoc Loc) const;
173+
DeclAttributes getDeclAttributes(const SourceLoc Loc) const;
133174
};
134175
} // namespace swift
135176

include/swift/Syntax/Syntax.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,11 @@ class Syntax {
172172

173173
/// Returns the first non-missing token in this syntax. Returns None if there
174174
/// is no non-missing token.
175-
Optional<TokenSyntax> getFirstToken();
175+
Optional<TokenSyntax> getFirstToken() const;
176176

177177
/// Returns the last non-missing token in this syntax. Returns None if there
178178
/// is no non-missing token.
179-
Optional<TokenSyntax> getLastToken();
179+
Optional<TokenSyntax> getLastToken() const;
180180

181181
/// Print the syntax node with full fidelity to the given output stream.
182182
void print(llvm::raw_ostream &OS, SyntaxPrintOptions Opts = SyntaxPrintOptions()) const;

include/swift/Syntax/SyntaxNodes.h.gyb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ public:
8181
/// ${line}
8282
% end
8383
% if child.is_optional:
84-
llvm::Optional<${child.type_name}> get${child.name}();
84+
llvm::Optional<${child.type_name}> get${child.name}() const;
8585
% else:
86-
${child.type_name} get${child.name}();
86+
${child.type_name} get${child.name}() const;
8787
% end
8888

8989
% child_node = NODE_MAP.get(child.syntax_kind)

0 commit comments

Comments
 (0)