16
16
#include " swift/AST/ASTContext.h"
17
17
#include " swift/AST/Decl.h"
18
18
#include " swift/AST/Expr.h"
19
+ #include " swift/AST/TypeRepr.h"
19
20
#include " swift/Parse/PersistentParserState.h"
20
21
#include " swift/Syntax/SyntaxNodes.h"
21
22
#include " llvm/ADT/DenseMap.h"
@@ -31,87 +32,158 @@ class ASTGen {
31
32
32
33
Parser &P;
33
34
34
- // FIXME: remove when Syntax can represent all types and ASTGen can handle them
35
+ // FIXME: remove when Syntax can represent all types and ASTGen can handle
36
+ // them
35
37
// / Types that cannot be represented by Syntax or generated by ASTGen.
36
38
llvm::DenseMap<SourceLoc, TypeRepr *> Types;
37
39
38
40
llvm::DenseMap<SourceLoc, DeclAttributes> ParsedDeclAttrs;
39
41
40
42
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);
43
+ ASTGen (ASTContext &Context, Parser &P) : Context(Context), P(P) {}
75
44
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);
45
+ SourceLoc generate (const syntax::TokenSyntax &Tok, const SourceLoc Loc);
84
46
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);
47
+ SourceLoc generateIdentifierDeclName (const syntax::TokenSyntax &Tok,
48
+ const SourceLoc, Identifier &Identifier);
49
+
50
+ public:
51
+ // ===--------------------------------------------------------------------===//
52
+ // Decls.
53
+
54
+ Decl *generate (const syntax::DeclSyntax &Decl, const SourceLoc Loc);
55
+ TypeDecl *generate (const syntax::AssociatedtypeDeclSyntax &Decl,
56
+ const SourceLoc Loc);
57
+
58
+ TrailingWhereClause *generate (const syntax::GenericWhereClauseSyntax &syntax,
59
+ const SourceLoc Loc);
60
+ MutableArrayRef<TypeLoc>
61
+ generate (const syntax::TypeInheritanceClauseSyntax &syntax,
62
+ const SourceLoc Loc, bool allowClassRequirement);
88
63
89
64
private:
90
- Expr *generateMagicIdentifierLiteralExpression (syntax::TokenSyntax PoundToken,
91
- SourceLoc &Loc);
65
+ DeclAttributes
66
+ generateDeclAttributes (const syntax::DeclSyntax &D,
67
+ const Optional<syntax::AttributeListSyntax> &attrs,
68
+ const Optional<syntax::ModifierListSyntax> &modifiers,
69
+ SourceLoc Loc, bool includeComments);
70
+
71
+ public:
72
+ // ===--------------------------------------------------------------------===//
73
+ // Expressions.
74
+
75
+ Expr *generate (const syntax::IntegerLiteralExprSyntax &Expr,
76
+ const SourceLoc Loc);
77
+ Expr *generate (const syntax::FloatLiteralExprSyntax &Expr,
78
+ const SourceLoc Loc);
79
+ Expr *generate (const syntax::NilLiteralExprSyntax &Expr, const SourceLoc Loc);
80
+ Expr *generate (const syntax::BooleanLiteralExprSyntax &Expr,
81
+ const SourceLoc Loc);
82
+ Expr *generate (const syntax::PoundFileExprSyntax &Expr, const SourceLoc Loc);
83
+ Expr *generate (const syntax::PoundLineExprSyntax &Expr, const SourceLoc Loc);
84
+ Expr *generate (const syntax::PoundColumnExprSyntax &Expr,
85
+ const SourceLoc Loc);
86
+ Expr *generate (const syntax::PoundFunctionExprSyntax &Expr,
87
+ const SourceLoc Loc);
88
+ Expr *generate (const syntax::PoundDsohandleExprSyntax &Expr,
89
+ const SourceLoc Loc);
90
+ Expr *generate (const syntax::UnknownExprSyntax &Expr, const SourceLoc Loc);
91
+
92
+ private:
93
+ Expr *generateMagicIdentifierLiteralExpression (
94
+ const syntax::TokenSyntax &PoundToken, const SourceLoc Loc);
95
+
96
+ static MagicIdentifierLiteralExpr::Kind
97
+ getMagicIdentifierLiteralKind (tok Kind);
98
+
99
+ public:
100
+ // ===--------------------------------------------------------------------===//
101
+ // Types.
102
+
103
+ TypeRepr *generate (const syntax::TypeSyntax &Type, const SourceLoc Loc);
104
+ TypeRepr *generate (const syntax::SomeTypeSyntax &Type, const SourceLoc Loc);
105
+ TypeRepr *generate (const syntax::CompositionTypeSyntax &Type,
106
+ const SourceLoc Loc);
107
+ TypeRepr *generate (const syntax::SimpleTypeIdentifierSyntax &Type,
108
+ const SourceLoc Loc);
109
+ TypeRepr *generate (const syntax::MemberTypeIdentifierSyntax &Type,
110
+ const SourceLoc Loc);
111
+ TypeRepr *generate (const syntax::DictionaryTypeSyntax &Type,
112
+ const SourceLoc Loc);
113
+ TypeRepr *generate (const syntax::ArrayTypeSyntax &Type, const SourceLoc Loc);
114
+ TypeRepr *generate (const syntax::TupleTypeSyntax &Type, const SourceLoc Loc);
115
+ TypeRepr *generate (const syntax::AttributedTypeSyntax &Type,
116
+ const SourceLoc Loc);
117
+ TypeRepr *generate (const syntax::FunctionTypeSyntax &Type,
118
+ const SourceLoc Loc);
119
+ TypeRepr *generate (const syntax::MetatypeTypeSyntax &Type,
120
+ const SourceLoc Loc);
121
+ TypeRepr *generate (const syntax::OptionalTypeSyntax &Type,
122
+ const SourceLoc Loc);
123
+ TypeRepr *generate (const syntax::ImplicitlyUnwrappedOptionalTypeSyntax &Type,
124
+ const SourceLoc Loc);
125
+ TypeRepr *generate (const syntax::ClassRestrictionTypeSyntax &Type,
126
+ const SourceLoc Loc);
127
+ TypeRepr *generate (const syntax::CodeCompletionTypeSyntax &Type,
128
+ const SourceLoc Loc);
129
+ TypeRepr *generate (const syntax::UnknownTypeSyntax &Type,
130
+ const SourceLoc Loc);
92
131
93
- TupleTypeRepr *generateTuple (syntax::TokenSyntax LParen,
94
- syntax::TupleTypeElementListSyntax Elements,
95
- syntax::TokenSyntax RParen, SourceLoc &Loc,
96
- bool IsFunction = false );
132
+ private:
133
+ TupleTypeRepr *
134
+ generateTuple (const syntax::TokenSyntax &LParen,
135
+ const syntax::TupleTypeElementListSyntax &Elements,
136
+ const syntax::TokenSyntax &RParen, const SourceLoc Loc,
137
+ bool IsFunction = false );
97
138
98
139
void gatherTypeIdentifierComponents (
99
- syntax::TypeSyntax Component, SourceLoc & Loc,
140
+ const syntax::TypeSyntax & Component, const SourceLoc Loc,
100
141
llvm::SmallVectorImpl<ComponentIdentTypeRepr *> &Components);
101
142
102
143
template <typename T>
103
- TypeRepr *generateSimpleOrMemberIdentifier (T Type, SourceLoc &Loc);
144
+ TypeRepr *generateSimpleOrMemberIdentifier (const T &Type,
145
+ const SourceLoc Loc);
104
146
105
147
template <typename T>
106
- ComponentIdentTypeRepr *generateIdentifier (T Type, SourceLoc &Loc);
148
+ ComponentIdentTypeRepr *generateIdentifier (const T &Type,
149
+ const SourceLoc Loc);
150
+
151
+ public:
152
+ // ===--------------------------------------------------------------------===//
153
+ // Generics.
154
+
155
+ TypeRepr *generate (const syntax::GenericArgumentSyntax &Arg,
156
+ const SourceLoc Loc);
157
+ llvm::SmallVector<TypeRepr *, 4 >
158
+ generate (const syntax::GenericArgumentListSyntax &Args, const SourceLoc Loc);
159
+
160
+ GenericParamList *
161
+ generate (const syntax::GenericParameterClauseListSyntax &clause,
162
+ const SourceLoc Loc);
163
+ GenericParamList *generate (const syntax::GenericParameterClauseSyntax &clause,
164
+ const SourceLoc Loc);
165
+ Optional<RequirementRepr>
166
+ generate (const syntax::GenericRequirementSyntax &req, const SourceLoc Loc);
167
+ LayoutConstraint generate (const syntax::LayoutConstraintSyntax &req,
168
+ const SourceLoc Loc);
169
+
170
+ public:
171
+ // ===--------------------------------------------------------------------===//
172
+ // Utilities.
173
+
174
+ // / Copy a numeric literal value into AST-owned memory, stripping underscores
175
+ // / so the semantic part of the value can be parsed by APInt/APFloat parsers.
176
+ static StringRef copyAndStripUnderscores (StringRef Orig, ASTContext &Context);
107
177
178
+ private:
108
179
StringRef copyAndStripUnderscores (StringRef Orig);
109
180
181
+ // / Advance \p Loc to the first token of the \p Node.
182
+ // / \p Loc must be the leading trivia of the first token in the tree in which
183
+ // / \p Node resides.
110
184
static SourceLoc advanceLocBegin (const SourceLoc &Loc,
111
185
const syntax::Syntax &Node);
112
186
113
- static MagicIdentifierLiteralExpr::Kind getMagicIdentifierLiteralKind (tok Kind);
114
-
115
187
ValueDecl *lookupInScope (DeclName Name);
116
188
117
189
void addToScope (ValueDecl *D, bool diagnoseRedefinitions = true );
@@ -121,15 +193,13 @@ class ASTGen {
121
193
TypeRepr *lookupType (syntax::TypeSyntax Type);
122
194
123
195
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 ;
196
+ void addType (TypeRepr *Type, const SourceLoc Loc);
197
+ bool hasType (const SourceLoc Loc) const ;
198
+ TypeRepr *getType (const SourceLoc Loc) const ;
129
199
130
- void addDeclAttributes (DeclAttributes attrs, SourceLoc Loc);
200
+ void addDeclAttributes (DeclAttributes attrs, const SourceLoc Loc);
131
201
bool hasDeclAttributes (SourceLoc Loc) const ;
132
- DeclAttributes getDeclAttributes (SourceLoc Loc) const ;
202
+ DeclAttributes getDeclAttributes (const SourceLoc Loc) const ;
133
203
};
134
204
} // namespace swift
135
205
0 commit comments