@@ -152,6 +152,166 @@ TrailingWhereClause *ASTGen::generate(const GenericWhereClauseSyntax &syntax,
152
152
return TrailingWhereClause::create (Context, whereLoc, requirements);
153
153
}
154
154
155
+ Expr *ASTGen::generate (const ExprSyntax &E, const SourceLoc Loc) {
156
+ Expr *result = nullptr ;
157
+
158
+ if (auto identifierExpr = E.getAs <IdentifierExprSyntax>())
159
+ result = generate (*identifierExpr, Loc);
160
+ else if (auto specializeExpr = E.getAs <SpecializeExprSyntax>())
161
+ result = generate (*specializeExpr, Loc);
162
+ else if (auto editorPlaceHolderExpr = E.getAs <EditorPlaceholderExprSyntax>())
163
+ result = generate (*editorPlaceHolderExpr, Loc);
164
+ else if (auto integerLiteralExpr = E.getAs <IntegerLiteralExprSyntax>())
165
+ result = generate (*integerLiteralExpr, Loc);
166
+ else if (auto floatLiteralExpr = E.getAs <FloatLiteralExprSyntax>())
167
+ result = generate (*floatLiteralExpr, Loc);
168
+ else if (auto nilLiteral = E.getAs <NilLiteralExprSyntax>())
169
+ result = generate (*nilLiteral, Loc);
170
+ else if (auto boolLiteral = E.getAs <BooleanLiteralExprSyntax>())
171
+ result = generate (*boolLiteral, Loc);
172
+ else if (auto poundFileExpr = E.getAs <PoundFileExprSyntax>())
173
+ result = generate (*poundFileExpr, Loc);
174
+ else if (auto poundLineExpr = E.getAs <PoundLineExprSyntax>())
175
+ result = generate (*poundLineExpr, Loc);
176
+ else if (auto poundColumnExpr = E.getAs <PoundColumnExprSyntax>())
177
+ result = generate (*poundColumnExpr, Loc);
178
+ else if (auto poundFunctionExpr = E.getAs <PoundFunctionExprSyntax>())
179
+ result = generate (*poundFunctionExpr, Loc);
180
+ else if (auto poundDsohandleExpr = E.getAs <PoundDsohandleExprSyntax>())
181
+ result = generate (*poundDsohandleExpr, Loc);
182
+ else
183
+ llvm_unreachable (" unsupported expression" );
184
+
185
+ return result;
186
+ }
187
+
188
+ std::pair<DeclName, DeclNameLoc> ASTGen::generateUnqualifiedDeclName (
189
+ const TokenSyntax &idTok, const Optional<DeclNameArgumentsSyntax> &args,
190
+ const SourceLoc Loc) {
191
+ SourceLoc baseNameLoc = advanceLocBegin (Loc, idTok);
192
+
193
+ DeclBaseName baseName;
194
+ if (idTok.getTokenKind () == tok::kw_init)
195
+ baseName = DeclBaseName::createConstructor ();
196
+ else if (idTok.getTokenKind () == tok::kw_deinit)
197
+ baseName = DeclBaseName::createDestructor ();
198
+ else if (idTok.getTokenKind () == tok::kw_subscript)
199
+ baseName = DeclBaseName::createSubscript ();
200
+ else
201
+ baseName = Context.getIdentifier (idTok.getIdentifierText ());
202
+
203
+ if (!args)
204
+ return {DeclName (baseName), DeclNameLoc (baseNameLoc)};
205
+
206
+ // FIXME: Remove this block and use 'Loc'.
207
+ // This is needed for the case 'idTok' and 'args' are not in the same tree.
208
+ // i.e. Call from parseUnqualifiedDeclName().
209
+ SourceLoc argsLeadingLoc = Loc;
210
+ if (!args->getParent ()) {
211
+ argsLeadingLoc = Loc.getAdvancedLoc (idTok.getTextLength ());
212
+ } else {
213
+ assert (idTok.getData ().getParent () == args->getData ().getParent () &&
214
+ idTok.getIndexInParent () + 1 == args->getIndexInParent () &&
215
+ " 'idTok' must be immediately followed by 'args'" );
216
+ }
217
+
218
+ SmallVector<Identifier, 2 > argumentLabels;
219
+ SmallVector<SourceLoc, 2 > argumentLabelLocs;
220
+ for (auto arg : args->getArguments ()) {
221
+ Identifier label;
222
+ if (!arg.getName ().isMissing () &&
223
+ arg.getName ().getTokenKind () != tok::kw__) {
224
+ label = Context.getIdentifier (arg.getName ().getIdentifierText ());
225
+ }
226
+ argumentLabels.push_back (label);
227
+ argumentLabelLocs.push_back (advanceLocBegin (argsLeadingLoc,
228
+ *arg.getFirstToken ()));
229
+ }
230
+ SourceLoc lParenLoc = advanceLocBegin (argsLeadingLoc, args->getLeftParen ());
231
+ SourceLoc rParenLoc = advanceLocBegin (argsLeadingLoc, args->getRightParen ());
232
+
233
+ DeclName name (Context, baseName, argumentLabels);
234
+ DeclNameLoc nameLoc;
235
+ if (argumentLabelLocs.empty ())
236
+ nameLoc = DeclNameLoc (baseNameLoc);
237
+ else
238
+ nameLoc = DeclNameLoc (Context, baseNameLoc, lParenLoc, argumentLabelLocs,
239
+ rParenLoc);
240
+ return {name, nameLoc};
241
+ }
242
+
243
+ Expr *ASTGen::generate (const IdentifierExprSyntax &E, const SourceLoc Loc) {
244
+ auto idTok = E.getIdentifier ();
245
+ DeclName name;
246
+ DeclNameLoc nameLoc;
247
+ std::tie (name, nameLoc) = generateUnqualifiedDeclName (
248
+ E.getIdentifier (), E.getDeclNameArguments (), Loc);
249
+
250
+ ValueDecl *D = nullptr ;
251
+ if (!P.InPoundIfEnvironment ) {
252
+ D = lookupInScope (name);
253
+ // FIXME: We want this to work: "var x = { x() }", but for now it's better
254
+ // to disallow it than to crash.
255
+ if (D) {
256
+ for (auto activeVar : P.DisabledVars ) {
257
+ if (activeVar != D)
258
+ continue ;
259
+ P.diagnose (nameLoc.getBaseNameLoc (), P.DisabledVarReason );
260
+ return new (Context) ErrorExpr (nameLoc.getSourceRange ());
261
+ }
262
+ } else {
263
+ for (auto activeVar : P.DisabledVars ) {
264
+ if (activeVar->getFullName () != name)
265
+ continue ;
266
+ P.diagnose (nameLoc.getBaseNameLoc (), P.DisabledVarReason );
267
+ return new (Context) ErrorExpr (nameLoc.getSourceRange ());
268
+ }
269
+ }
270
+ }
271
+
272
+ if (!D) {
273
+ return new (Context)
274
+ UnresolvedDeclRefExpr (name, DeclRefKind::Ordinary, nameLoc);
275
+ }
276
+
277
+ if (auto TD = dyn_cast<TypeDecl>(D)) {
278
+ // When parsing default argument expressions for generic functions,
279
+ // we haven't built a FuncDecl or re-parented the GenericTypeParamDecls
280
+ // to the FuncDecl yet. Other than that, we should only ever find
281
+ // global or local declarations here.
282
+ assert (!TD->getDeclContext ()->isTypeContext () ||
283
+ isa<GenericTypeParamDecl>(TD));
284
+ return TypeExpr::createForDecl (nameLoc.getBaseNameLoc (), TD,
285
+ /* DeclContext=*/ nullptr ,
286
+ /* inplicit=*/ false );
287
+ }
288
+
289
+ return new (Context) DeclRefExpr (D, nameLoc, /* implicit=*/ false );
290
+ }
291
+
292
+ Expr *ASTGen::generate (const EditorPlaceholderExprSyntax &E, const SourceLoc Loc) {
293
+ assert (!E.getIdentifier ().isMissing ());
294
+
295
+ auto text = E.getIdentifier ().getText ();
296
+ auto tokLoc = advanceLocBegin (Loc, E.getIdentifier ());
297
+ return P.parseExprEditorPlaceholder (tokLoc, text);
298
+ }
299
+
300
+ Expr *ASTGen::generate (const SpecializeExprSyntax &E, const SourceLoc Loc) {
301
+ auto base = generate (E.getExpression (), Loc);
302
+
303
+ SourceLoc lAngleLoc, rAngleLoc;
304
+ SmallVector<TypeRepr *, 4 > argTyRs;
305
+ generate (E.getGenericArgumentClause (), Loc, lAngleLoc, rAngleLoc, argTyRs);
306
+ if (argTyRs.empty ())
307
+ return base;
308
+
309
+ SmallVector<TypeLoc, 4 > args;
310
+ args.assign (argTyRs.begin (), argTyRs.end ());
311
+ return UnresolvedSpecializeExpr::create (Context, base, lAngleLoc, args,
312
+ rAngleLoc);
313
+ }
314
+
155
315
Expr *ASTGen::generate (const IntegerLiteralExprSyntax &Expr,
156
316
const SourceLoc Loc) {
157
317
auto Digits = Expr.getDigits ();
@@ -584,15 +744,12 @@ ComponentIdentTypeRepr *ASTGen::generateIdentifier(const T &Type,
584
744
auto IdentifierLoc = advanceLocBegin (Loc, Type.getName ());
585
745
auto Identifier = Context.getIdentifier (Type.getName ().getIdentifierText ());
586
746
if (auto Clause = Type.getGenericArgumentClause ()) {
587
- auto Args = Clause->getArguments ();
588
- if (!Args.empty ()) {
589
- auto LAngleLoc = advanceLocBegin (Loc, Clause->getLeftAngleBracket ());
590
- auto RAngleLoc = advanceLocBegin (Loc, Clause->getRightAngleBracket ());
591
- SourceRange Range{LAngleLoc, RAngleLoc};
592
- auto ArgsAST = generate (Args, Loc);
747
+ SourceLoc lAngleLoc, rAngleLoc;
748
+ SmallVector<TypeRepr *, 4 > args;
749
+ generate (*Clause, Loc, lAngleLoc, rAngleLoc, args);
750
+ if (!args.empty ())
593
751
return GenericIdentTypeRepr::create (Context, IdentifierLoc, Identifier,
594
- ArgsAST, Range);
595
- }
752
+ args, {lAngleLoc, rAngleLoc});
596
753
}
597
754
return new (Context) SimpleIdentTypeRepr (IdentifierLoc, Identifier);
598
755
}
@@ -721,13 +878,11 @@ TypeRepr *ASTGen::generate(const SILBoxTypeSyntax &Type, const SourceLoc Loc,
721
878
auto RBraceLoc = advanceLocBegin (Loc, Type.getRightBrace ());
722
879
723
880
SourceLoc LAngleLoc, RAngleLoc;
724
- SmallVector<TypeRepr*, 4 > Args;
881
+ SmallVector<TypeRepr *, 4 > Args;
725
882
if (auto genericArgs = Type.getGenericArgumentClause ()) {
726
883
if (genericArgs->getRightAngleBracket ().isMissing ())
727
884
return nullptr ;
728
- LAngleLoc = advanceLocBegin (Loc, genericArgs->getLeftAngleBracket ());
729
- RAngleLoc = advanceLocBegin (Loc, genericArgs->getRightAngleBracket ());
730
- Args = generate (genericArgs->getArguments (), Loc);
885
+ generate (*genericArgs, Loc, LAngleLoc, RAngleLoc, Args);
731
886
}
732
887
733
888
auto SILType = SILBoxTypeRepr::create (Context, generics, LBraceLoc, Fields,
@@ -843,22 +998,20 @@ TypeRepr *ASTGen::generate(const UnknownTypeSyntax &Type, const SourceLoc Loc) {
843
998
return nullptr ;
844
999
}
845
1000
846
- SmallVector<TypeRepr *, 4 >
847
- ASTGen::generate (const GenericArgumentListSyntax &Args, const SourceLoc Loc) {
848
- SmallVector<TypeRepr *, 4 > Types;
849
- for (auto Arg : Args) {
850
- auto tyR = generate (Arg, Loc);
1001
+ void
1002
+ ASTGen::generate (const GenericArgumentClauseSyntax &clause, const SourceLoc Loc,
1003
+ SourceLoc &lAngleLoc, SourceLoc &rAngleLoc,
1004
+ SmallVectorImpl<TypeRepr *> &args) {
1005
+ lAngleLoc = advanceLocBegin (Loc, clause.getLeftAngleBracket ());
1006
+ rAngleLoc = advanceLocBegin (Loc, clause.getRightAngleBracket ());
1007
+
1008
+ assert (args.empty ());
1009
+ for (auto Arg : clause.getArguments ()) {
1010
+ auto tyR = generate (Arg.getArgumentType (), Loc);
851
1011
if (!tyR)
852
1012
tyR = new (Context) ErrorTypeRepr (advanceLocBegin (Loc, Arg));
853
- Types .push_back (tyR);
1013
+ args .push_back (tyR);
854
1014
}
855
-
856
- return Types;
857
- }
858
-
859
- TypeRepr *ASTGen::generate (const GenericArgumentSyntax &Arg,
860
- const SourceLoc Loc) {
861
- return generate (Arg.getArgumentType (), Loc);
862
1015
}
863
1016
864
1017
StringRef ASTGen::copyAndStripUnderscores (StringRef Orig, ASTContext &Context) {
0 commit comments