@@ -238,166 +238,6 @@ TrailingWhereClause *ASTGen::generate(const GenericWhereClauseSyntax &syntax,
238
238
return TrailingWhereClause::create (Context, whereLoc, requirements);
239
239
}
240
240
241
- Expr *ASTGen::generate (const ExprSyntax &E, const SourceLoc Loc) {
242
- Expr *result = nullptr ;
243
-
244
- if (auto identifierExpr = E.getAs <IdentifierExprSyntax>())
245
- result = generate (*identifierExpr, Loc);
246
- else if (auto specializeExpr = E.getAs <SpecializeExprSyntax>())
247
- result = generate (*specializeExpr, Loc);
248
- else if (auto editorPlaceHolderExpr = E.getAs <EditorPlaceholderExprSyntax>())
249
- result = generate (*editorPlaceHolderExpr, Loc);
250
- else if (auto integerLiteralExpr = E.getAs <IntegerLiteralExprSyntax>())
251
- result = generate (*integerLiteralExpr, Loc);
252
- else if (auto floatLiteralExpr = E.getAs <FloatLiteralExprSyntax>())
253
- result = generate (*floatLiteralExpr, Loc);
254
- else if (auto nilLiteral = E.getAs <NilLiteralExprSyntax>())
255
- result = generate (*nilLiteral, Loc);
256
- else if (auto boolLiteral = E.getAs <BooleanLiteralExprSyntax>())
257
- result = generate (*boolLiteral, Loc);
258
- else if (auto poundFileExpr = E.getAs <PoundFileExprSyntax>())
259
- result = generate (*poundFileExpr, Loc);
260
- else if (auto poundLineExpr = E.getAs <PoundLineExprSyntax>())
261
- result = generate (*poundLineExpr, Loc);
262
- else if (auto poundColumnExpr = E.getAs <PoundColumnExprSyntax>())
263
- result = generate (*poundColumnExpr, Loc);
264
- else if (auto poundFunctionExpr = E.getAs <PoundFunctionExprSyntax>())
265
- result = generate (*poundFunctionExpr, Loc);
266
- else if (auto poundDsohandleExpr = E.getAs <PoundDsohandleExprSyntax>())
267
- result = generate (*poundDsohandleExpr, Loc);
268
- else
269
- llvm_unreachable (" unsupported expression" );
270
-
271
- return result;
272
- }
273
-
274
- std::pair<DeclName, DeclNameLoc> ASTGen::generateUnqualifiedDeclName (
275
- const TokenSyntax &idTok, const Optional<DeclNameArgumentsSyntax> &args,
276
- const SourceLoc Loc) {
277
- SourceLoc baseNameLoc = advanceLocBegin (Loc, idTok);
278
-
279
- DeclBaseName baseName;
280
- if (idTok.getTokenKind () == tok::kw_init)
281
- baseName = DeclBaseName::createConstructor ();
282
- else if (idTok.getTokenKind () == tok::kw_deinit)
283
- baseName = DeclBaseName::createDestructor ();
284
- else if (idTok.getTokenKind () == tok::kw_subscript)
285
- baseName = DeclBaseName::createSubscript ();
286
- else
287
- baseName = Context.getIdentifier (idTok.getIdentifierText ());
288
-
289
- if (!args)
290
- return {DeclName (baseName), DeclNameLoc (baseNameLoc)};
291
-
292
- // FIXME: Remove this block and use 'Loc'.
293
- // This is needed for the case 'idTok' and 'args' are not in the same tree.
294
- // i.e. Call from parseUnqualifiedDeclName().
295
- SourceLoc argsLeadingLoc = Loc;
296
- if (!args->getParent ()) {
297
- argsLeadingLoc = Loc.getAdvancedLoc (idTok.getTextLength ());
298
- } else {
299
- assert (idTok.getData ().getParent () == args->getData ().getParent () &&
300
- idTok.getIndexInParent () + 1 == args->getIndexInParent () &&
301
- " 'idTok' must be immediately followed by 'args'" );
302
- }
303
-
304
- SmallVector<Identifier, 2 > argumentLabels;
305
- SmallVector<SourceLoc, 2 > argumentLabelLocs;
306
- for (auto arg : args->getArguments ()) {
307
- Identifier label;
308
- if (!arg.getName ().isMissing () &&
309
- arg.getName ().getTokenKind () != tok::kw__) {
310
- label = Context.getIdentifier (arg.getName ().getIdentifierText ());
311
- }
312
- argumentLabels.push_back (label);
313
- argumentLabelLocs.push_back (advanceLocBegin (argsLeadingLoc,
314
- *arg.getFirstToken ()));
315
- }
316
- SourceLoc lParenLoc = advanceLocBegin (argsLeadingLoc, args->getLeftParen ());
317
- SourceLoc rParenLoc = advanceLocBegin (argsLeadingLoc, args->getRightParen ());
318
-
319
- DeclName name (Context, baseName, argumentLabels);
320
- DeclNameLoc nameLoc;
321
- if (argumentLabelLocs.empty ())
322
- nameLoc = DeclNameLoc (baseNameLoc);
323
- else
324
- nameLoc = DeclNameLoc (Context, baseNameLoc, lParenLoc, argumentLabelLocs,
325
- rParenLoc);
326
- return {name, nameLoc};
327
- }
328
-
329
- Expr *ASTGen::generate (const IdentifierExprSyntax &E, const SourceLoc Loc) {
330
- auto idTok = E.getIdentifier ();
331
- DeclName name;
332
- DeclNameLoc nameLoc;
333
- std::tie (name, nameLoc) = generateUnqualifiedDeclName (
334
- E.getIdentifier (), E.getDeclNameArguments (), Loc);
335
-
336
- ValueDecl *D = nullptr ;
337
- if (!P.InPoundIfEnvironment ) {
338
- D = lookupInScope (name);
339
- // FIXME: We want this to work: "var x = { x() }", but for now it's better
340
- // to disallow it than to crash.
341
- if (D) {
342
- for (auto activeVar : P.DisabledVars ) {
343
- if (activeVar != D)
344
- continue ;
345
- P.diagnose (nameLoc.getBaseNameLoc (), P.DisabledVarReason );
346
- return new (Context) ErrorExpr (nameLoc.getSourceRange ());
347
- }
348
- } else {
349
- for (auto activeVar : P.DisabledVars ) {
350
- if (activeVar->getFullName () != name)
351
- continue ;
352
- P.diagnose (nameLoc.getBaseNameLoc (), P.DisabledVarReason );
353
- return new (Context) ErrorExpr (nameLoc.getSourceRange ());
354
- }
355
- }
356
- }
357
-
358
- if (!D) {
359
- return new (Context)
360
- UnresolvedDeclRefExpr (name, DeclRefKind::Ordinary, nameLoc);
361
- }
362
-
363
- if (auto TD = dyn_cast<TypeDecl>(D)) {
364
- // When parsing default argument expressions for generic functions,
365
- // we haven't built a FuncDecl or re-parented the GenericTypeParamDecls
366
- // to the FuncDecl yet. Other than that, we should only ever find
367
- // global or local declarations here.
368
- assert (!TD->getDeclContext ()->isTypeContext () ||
369
- isa<GenericTypeParamDecl>(TD));
370
- return TypeExpr::createForDecl (nameLoc.getBaseNameLoc (), TD,
371
- /* DeclContext=*/ nullptr ,
372
- /* inplicit=*/ false );
373
- }
374
-
375
- return new (Context) DeclRefExpr (D, nameLoc, /* implicit=*/ false );
376
- }
377
-
378
- Expr *ASTGen::generate (const EditorPlaceholderExprSyntax &E, const SourceLoc Loc) {
379
- assert (!E.getIdentifier ().isMissing ());
380
-
381
- auto text = E.getIdentifier ().getText ();
382
- auto tokLoc = advanceLocBegin (Loc, E.getIdentifier ());
383
- return P.parseExprEditorPlaceholder (tokLoc, text);
384
- }
385
-
386
- Expr *ASTGen::generate (const SpecializeExprSyntax &E, const SourceLoc Loc) {
387
- auto base = generate (E.getExpression (), Loc);
388
-
389
- SourceLoc lAngleLoc, rAngleLoc;
390
- SmallVector<TypeRepr *, 4 > argTyRs;
391
- generate (E.getGenericArgumentClause (), Loc, lAngleLoc, rAngleLoc, argTyRs);
392
- if (argTyRs.empty ())
393
- return base;
394
-
395
- SmallVector<TypeLoc, 4 > args;
396
- args.assign (argTyRs.begin (), argTyRs.end ());
397
- return UnresolvedSpecializeExpr::create (Context, base, lAngleLoc, args,
398
- rAngleLoc);
399
- }
400
-
401
241
Expr *ASTGen::generate (const IntegerLiteralExprSyntax &Expr,
402
242
const SourceLoc Loc) {
403
243
auto Digits = Expr.getDigits ();
@@ -830,12 +670,15 @@ ComponentIdentTypeRepr *ASTGen::generateIdentifier(const T &Type,
830
670
auto IdentifierLoc = advanceLocBegin (Loc, Type.getName ());
831
671
auto Identifier = Context.getIdentifier (Type.getName ().getIdentifierText ());
832
672
if (auto Clause = Type.getGenericArgumentClause ()) {
833
- SourceLoc lAngleLoc, rAngleLoc;
834
- SmallVector<TypeRepr *, 4 > args;
835
- generate (*Clause, Loc, lAngleLoc, rAngleLoc, args);
836
- if (!args.empty ())
673
+ auto Args = Clause->getArguments ();
674
+ if (!Args.empty ()) {
675
+ auto LAngleLoc = advanceLocBegin (Loc, Clause->getLeftAngleBracket ());
676
+ auto RAngleLoc = advanceLocBegin (Loc, Clause->getRightAngleBracket ());
677
+ SourceRange Range{LAngleLoc, RAngleLoc};
678
+ auto ArgsAST = generate (Args, Loc);
837
679
return GenericIdentTypeRepr::create (Context, IdentifierLoc, Identifier,
838
- args, {lAngleLoc, rAngleLoc});
680
+ ArgsAST, Range);
681
+ }
839
682
}
840
683
return new (Context) SimpleIdentTypeRepr (IdentifierLoc, Identifier);
841
684
}
@@ -964,11 +807,13 @@ TypeRepr *ASTGen::generate(const SILBoxTypeSyntax &Type, const SourceLoc Loc,
964
807
auto RBraceLoc = advanceLocBegin (Loc, Type.getRightBrace ());
965
808
966
809
SourceLoc LAngleLoc, RAngleLoc;
967
- SmallVector<TypeRepr *, 4 > Args;
810
+ SmallVector<TypeRepr*, 4 > Args;
968
811
if (auto genericArgs = Type.getGenericArgumentClause ()) {
969
812
if (genericArgs->getRightAngleBracket ().isMissing ())
970
813
return nullptr ;
971
- generate (*genericArgs, Loc, LAngleLoc, RAngleLoc, Args);
814
+ LAngleLoc = advanceLocBegin (Loc, genericArgs->getLeftAngleBracket ());
815
+ RAngleLoc = advanceLocBegin (Loc, genericArgs->getRightAngleBracket ());
816
+ Args = generate (genericArgs->getArguments (), Loc);
972
817
}
973
818
974
819
auto SILType = SILBoxTypeRepr::create (Context, generics, LBraceLoc, Fields,
@@ -1084,20 +929,22 @@ TypeRepr *ASTGen::generate(const UnknownTypeSyntax &Type, const SourceLoc Loc) {
1084
929
return nullptr ;
1085
930
}
1086
931
1087
- void
1088
- ASTGen::generate (const GenericArgumentClauseSyntax &clause, const SourceLoc Loc,
1089
- SourceLoc &lAngleLoc, SourceLoc &rAngleLoc,
1090
- SmallVectorImpl<TypeRepr *> &args) {
1091
- lAngleLoc = advanceLocBegin (Loc, clause.getLeftAngleBracket ());
1092
- rAngleLoc = advanceLocBegin (Loc, clause.getRightAngleBracket ());
1093
-
1094
- assert (args.empty ());
1095
- for (auto Arg : clause.getArguments ()) {
1096
- auto tyR = generate (Arg.getArgumentType (), Loc);
932
+ SmallVector<TypeRepr *, 4 >
933
+ ASTGen::generate (const GenericArgumentListSyntax &Args, const SourceLoc Loc) {
934
+ SmallVector<TypeRepr *, 4 > Types;
935
+ for (auto Arg : Args) {
936
+ auto tyR = generate (Arg, Loc);
1097
937
if (!tyR)
1098
938
tyR = new (Context) ErrorTypeRepr (advanceLocBegin (Loc, Arg));
1099
- args .push_back (tyR);
939
+ Types .push_back (tyR);
1100
940
}
941
+
942
+ return Types;
943
+ }
944
+
945
+ TypeRepr *ASTGen::generate (const GenericArgumentSyntax &Arg,
946
+ const SourceLoc Loc) {
947
+ return generate (Arg.getArgumentType (), Loc);
1101
948
}
1102
949
1103
950
StringRef ASTGen::copyAndStripUnderscores (StringRef Orig, ASTContext &Context) {
0 commit comments