Skip to content

Commit 940988e

Browse files
committed
Revert "Revert "Merge pull request swiftlang#27291 from rintaro/syntaxparses-astgen-signature""
This reverts commit 0165b4f.
1 parent 27d6e18 commit 940988e

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
@@ -34,87 +34,130 @@ class ASTGen {
3434

3535
Parser &P;
3636

37-
// FIXME: remove when Syntax can represent all types and ASTGen can handle them
37+
// FIXME: remove when Syntax can represent all types and ASTGen can handle
38+
// them
3839
/// Types that cannot be represented by Syntax or generated by ASTGen.
3940
llvm::DenseMap<SourceLoc, TypeRepr *> Types;
4041

4142
llvm::DenseMap<SourceLoc, DeclAttributes> ParsedDeclAttrs;
4243

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

79-
GenericParamList *
80-
generate(syntax::GenericParameterClauseListSyntax clause, SourceLoc &Loc);
81-
GenericParamList *
82-
generate(syntax::GenericParameterClauseSyntax clause, SourceLoc &Loc);
83-
Optional<RequirementRepr>
84-
generate(syntax::GenericRequirementSyntax req, SourceLoc &Loc);
85-
LayoutConstraint
86-
generate(syntax::LayoutConstraintSyntax req, SourceLoc &Loc);
47+
SourceLoc generate(const syntax::TokenSyntax &Tok, const SourceLoc Loc);
8748

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

9270
private:
93-
Expr *generateMagicIdentifierLiteralExpression(syntax::TokenSyntax PoundToken,
94-
SourceLoc &Loc);
71+
Expr *generateMagicIdentifierLiteralExpression(
72+
const syntax::TokenSyntax &PoundToken, const SourceLoc Loc);
9573

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

101113
void gatherTypeIdentifierComponents(
102-
syntax::TypeSyntax Component, SourceLoc &Loc,
114+
const syntax::TypeSyntax &Component, const SourceLoc Loc,
103115
llvm::SmallVectorImpl<ComponentIdentTypeRepr *> &Components);
104116

105117
template <typename T>
106-
TypeRepr *generateSimpleOrMemberIdentifier(T Type, SourceLoc &Loc);
118+
TypeRepr *generateSimpleOrMemberIdentifier(const T &Type,
119+
const SourceLoc Loc);
107120

108121
template <typename T>
109-
ComponentIdentTypeRepr *generateIdentifier(T Type, SourceLoc &Loc);
122+
ComponentIdentTypeRepr *generateIdentifier(const T &Type,
123+
const SourceLoc Loc);
110124

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

155+
/// Advance \p Loc to the first token of the \p Node.
156+
/// \p Loc must be the leading trivia of the first token in the tree in which
157+
/// \p Node resides.
113158
static SourceLoc advanceLocBegin(const SourceLoc &Loc,
114159
const syntax::Syntax &Node);
115160

116-
static MagicIdentifierLiteralExpr::Kind getMagicIdentifierLiteralKind(tok Kind);
117-
118161
ValueDecl *lookupInScope(DeclName Name);
119162

120163
void addToScope(ValueDecl *D, bool diagnoseRedefinitions = true);
@@ -124,15 +167,13 @@ class ASTGen {
124167
TypeRepr *lookupType(syntax::TypeSyntax Type);
125168

126169
public:
127-
TypeRepr *addType(TypeRepr *Type, const SourceLoc &Loc);
128-
129-
bool hasType(const SourceLoc &Loc) const;
130-
131-
TypeRepr *getType(const SourceLoc &Loc) const;
170+
void addType(TypeRepr *Type, const SourceLoc Loc);
171+
bool hasType(const SourceLoc Loc) const;
172+
TypeRepr *getType(const SourceLoc Loc) const;
132173

133-
void addDeclAttributes(DeclAttributes attrs, SourceLoc Loc);
174+
void addDeclAttributes(DeclAttributes attrs, const SourceLoc Loc);
134175
bool hasDeclAttributes(SourceLoc Loc) const;
135-
DeclAttributes getDeclAttributes(SourceLoc Loc) const;
176+
DeclAttributes getDeclAttributes(const SourceLoc Loc) const;
136177
};
137178
} // namespace swift
138179

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)