@@ -226,7 +226,8 @@ Expr *ASTGen::generate(const UnknownExprSyntax &Expr, const SourceLoc Loc) {
226
226
return nullptr ;
227
227
}
228
228
229
- TypeRepr *ASTGen::generate (const TypeSyntax &Type, const SourceLoc Loc) {
229
+ TypeRepr *ASTGen::generate (const TypeSyntax &Type, const SourceLoc Loc,
230
+ bool IsSILFuncDecl) {
230
231
TypeRepr *TypeAST = nullptr ;
231
232
232
233
if (auto SimpleIdentifier = Type.getAs <SimpleTypeIdentifierSyntax>())
@@ -255,6 +256,10 @@ TypeRepr *ASTGen::generate(const TypeSyntax &Type, const SourceLoc Loc) {
255
256
TypeAST = generate (*Attributed, Loc);
256
257
else if (auto ClassRestriction = Type.getAs <ClassRestrictionTypeSyntax>())
257
258
TypeAST = generate (*ClassRestriction, Loc);
259
+ else if (auto SILBoxType = Type.getAs <SILBoxTypeSyntax>())
260
+ TypeAST = generate (*SILBoxType, Loc, IsSILFuncDecl);
261
+ else if (auto SILFunctionType = Type.getAs <SILFunctionTypeSyntax>())
262
+ TypeAST = generate (*SILFunctionType, Loc, IsSILFuncDecl);
258
263
else if (auto CompletionTy = Type.getAs <CodeCompletionTypeSyntax>())
259
264
TypeAST = generate (*CompletionTy, Loc);
260
265
else if (auto Unknown = Type.getAs <UnknownTypeSyntax>())
@@ -270,16 +275,37 @@ TypeRepr *ASTGen::generate(const TypeSyntax &Type, const SourceLoc Loc) {
270
275
271
276
TypeRepr *ASTGen::generate (const FunctionTypeSyntax &Type,
272
277
const SourceLoc Loc) {
273
- auto ArgumentTypes = generateTuple (Type.getLeftParen (), Type.getArguments (),
274
- Type.getRightParen (), Loc,
275
- /* IsFunction=*/ true );
278
+ TupleTypeRepr *ArgumentTypes = nullptr ;
279
+
280
+ SourceLoc VoidLoc;
281
+ if (Type.getLeftParen ().isMissing () && Type.getArguments ().size () == 1 ) {
282
+ if (auto ident = Type.getArguments ()[0 ]
283
+ .getType ()
284
+ .getAs <SimpleTypeIdentifierSyntax>()) {
285
+ if (!ident->getGenericArgumentClause ().hasValue () &&
286
+ ident->getName ().getText () == " Void" )
287
+ VoidLoc = advanceLocBegin (Loc, ident->getName ());
288
+ }
289
+ }
290
+
291
+ if (VoidLoc.isValid ())
292
+ ArgumentTypes = TupleTypeRepr::createEmpty (Context, VoidLoc);
293
+ else {
294
+ ArgumentTypes = generateTuple (Type.getLeftParen (), Type.getArguments (),
295
+ Type.getRightParen (), Loc,
296
+ /* IsFunction=*/ true );
297
+ }
298
+ if (!ArgumentTypes)
299
+ return nullptr ;
276
300
277
301
auto ThrowsLoc = Type.getThrowsOrRethrowsKeyword ()
278
302
? generate (*Type.getThrowsOrRethrowsKeyword (), Loc)
279
303
: SourceLoc ();
280
304
281
305
auto ArrowLoc = generate (Type.getArrow (), Loc);
282
306
auto ReturnType = generate (Type.getReturnType (), Loc);
307
+ if (!ReturnType)
308
+ return nullptr ;
283
309
284
310
return new (Context)
285
311
FunctionTypeRepr (nullptr , ArgumentTypes, ThrowsLoc, ArrowLoc, ReturnType);
@@ -345,42 +371,109 @@ TupleTypeRepr *ASTGen::generateTuple(const TokenSyntax &LParen,
345
371
EllipsisLoc, EllipsisIdx);
346
372
}
347
373
348
- TypeRepr * ASTGen::generate (const AttributedTypeSyntax &Type ,
349
- const SourceLoc Loc) {
350
- // todo [gsoc]: improve this after refactoring attribute parsing
374
+ TypeAttributes ASTGen::generateTypeAttributes (const AttributeListSyntax &syntax ,
375
+ const SourceLoc Loc) {
376
+ TypeAttributes attrs;
351
377
352
- auto TypeAST = generate (Type.getBaseType (), Loc);
378
+ for (auto elem : syntax) {
379
+ auto attrSyntax = elem.castTo <AttributeSyntax>();
380
+ if (attrSyntax.getAttributeName ().isMissing ())
381
+ continue ;
353
382
354
- if (auto Attributes = Type.getAttributes ()) {
355
- TypeAttributes TypeAttrs;
383
+ auto attrName = attrSyntax.getAttributeName ().getText ();
356
384
357
- for ( auto Attribute : *Attributes) {
358
- auto Attr = Attribute. castTo <AttributeSyntax>();
359
- auto AttrNameStr = Attr. getAttributeName (). getText () ;
385
+ auto atLoc = advanceLocBegin (Loc, attrSyntax. getAtSignToken ());
386
+ if (attrs. AtLoc . isInvalid ())
387
+ attrs. AtLoc = atLoc ;
360
388
361
- auto AtLoc = advanceLocBegin (Loc, Attr.getAtSignToken ());
362
- auto AttrKind = TypeAttributes::getAttrKindFromString (AttrNameStr);
389
+ auto attr = TypeAttributes::getAttrKindFromString (attrName);
390
+ if (attr == TAK_Count)
391
+ continue ;
392
+
393
+ if (attrs.has (attr)) {
394
+ P.diagnose (atLoc, diag::duplicate_attribute, /* isModifier=*/ false );
395
+ continue ;
396
+ }
363
397
364
- TypeAttrs. setAttr (AttrKind, AtLoc );
398
+ auto arg = attrSyntax. getArgument ( );
365
399
366
- if (AttrKind == TAK_convention) {
367
- auto Argument = Attr.getArgument ()->castTo <TokenSyntax>();
368
- auto Convention = Context.getIdentifier (Argument.getIdentifierText ());
369
- TypeAttrs.convention = Convention.str ();
400
+ if (attr == TAK_sil_weak || attr == TAK_sil_unowned) {
401
+ if (attrs.hasOwnership ()) {
402
+ P.diagnose (atLoc, diag::duplicate_attribute, /* isModifier*/ false );
370
403
}
404
+ } else if (attr == TAK_convention) {
405
+ // @convention(block)
406
+ // @convention(witness_method: ProtocolName)
407
+ if (!arg)
408
+ continue ;
371
409
372
- if (AttrKind == TAK_opened) {
373
- auto AttrText = Attr.getArgument ()->castTo <TokenSyntax>().getText ();
374
- auto LiteralText = AttrText.slice (1 , AttrText.size () - 1 );
375
- TypeAttrs.OpenedID = UUID::fromString (LiteralText.str ().c_str ());
410
+ if (auto conventionNameTok = arg->getAs <TokenSyntax>()) {
411
+ assert (conventionNameTok->getTokenKind () == tok::identifier);
412
+ auto convention =
413
+ Context.getIdentifier (conventionNameTok->getIdentifierText ());
414
+ attrs.convention = convention.str ();
415
+ } else if (auto witness =
416
+ arg->getAs <NamedAttributeStringArgumentSyntax>()) {
417
+ assert (witness->getNameTok ().getIdentifierText () == " witness_method" );
418
+ if (witness->getStringOrDeclname ().isMissing ())
419
+ continue ;
420
+ auto protocolName =
421
+ witness->getStringOrDeclname ().castTo <DeclNameSyntax>();
422
+ auto protocol = Context.getIdentifier (
423
+ protocolName.getDeclBaseName ().getIdentifierText ());
424
+ attrs.convention = " witness_method" ;
425
+ attrs.conventionWitnessMethodProtocol = protocol.str ();
426
+ } else {
427
+ continue ;
376
428
}
429
+ } else if (attr == TAK_opened) {
430
+ // @opened("01234567-89ab-cdef-0123-111111111111")
431
+ if (!arg)
432
+ continue ;
377
433
378
- if (TypeAttrs.AtLoc .isInvalid ())
379
- TypeAttrs.AtLoc = AtLoc;
434
+ assert (arg->castTo <TokenSyntax>().getTokenKind () ==
435
+ tok::string_literal);
436
+ auto tokText = arg->castTo <TokenSyntax>().getText ();
437
+ auto literalText = tokText.slice (1 , tokText.size () - 1 );
438
+ attrs.OpenedID = UUID::fromString (literalText.str ().c_str ());
439
+ } else if (attr == TAK__opaqueReturnTypeOf) {
440
+ // @_opaqueReturnTypeOf("$sMangledName", 0)
441
+ if (!arg)
442
+ continue ;
443
+
444
+ auto opaqueArg =
445
+ arg->castTo <OpaqueReturnTypeOfAttributeArgumentsSyntax>();
446
+
447
+ auto manglingTok = opaqueArg.getMangledName ();
448
+ auto indexTok = opaqueArg.getIndex ();
449
+ if (manglingTok.isMissing () || indexTok.isMissing ())
450
+ continue ;
451
+
452
+ auto tokText = manglingTok.getText ();
453
+ auto mangling =
454
+ Context.getIdentifier (tokText.slice (1 , tokText.size () - 1 ));
455
+ unsigned index;
456
+ if (indexTok.getText ().getAsInteger (10 , index))
457
+ continue ;
458
+ attrs.setOpaqueReturnTypeOf (mangling.str (), index);
380
459
}
381
460
382
- if (!TypeAttrs.empty ())
383
- TypeAST = new (Context) AttributedTypeRepr (TypeAttrs, TypeAST);
461
+ attrs.setAttr (attr, atLoc);
462
+ }
463
+
464
+ return attrs;
465
+ }
466
+
467
+ TypeRepr *ASTGen::generate (const AttributedTypeSyntax &Type,
468
+ const SourceLoc Loc) {
469
+ auto TypeAST = generate (Type.getBaseType (), Loc);
470
+ if (!TypeAST)
471
+ return nullptr ;
472
+
473
+ if (auto Attributes = Type.getAttributes ()) {
474
+ TypeAttributes attrs = generateTypeAttributes (*Attributes, Loc);
475
+ if (!attrs.empty ())
476
+ TypeAST = new (Context) AttributedTypeRepr (attrs, TypeAST);
384
477
}
385
478
386
479
if (auto Specifier = Type.getSpecifier ()) {
@@ -581,18 +674,92 @@ ASTGen::generate(const ClassRestrictionTypeSyntax &Type, const SourceLoc Loc) {
581
674
SimpleIdentTypeRepr (classLoc, Context.getIdentifier (" AnyObject" ));
582
675
}
583
676
677
+ TypeRepr *ASTGen::generate (const SILBoxTypeSyntax &Type, const SourceLoc Loc,
678
+ bool IsSILFuncDecl) {
679
+ if (Type.getRightBrace ().isMissing ())
680
+ return nullptr ;
681
+
682
+ // If this is part of a sil function decl, generic parameters are visible in
683
+ // the function body; otherwise, they are visible when parsing the type.
684
+ Optional<Scope> GenericsScope;
685
+ if (!IsSILFuncDecl)
686
+ GenericsScope.emplace (&P, ScopeKind::Generics);
687
+
688
+ GenericParamList *generics = nullptr ;
689
+ if (auto genericParamsSyntax = Type.getGenericParameterClauses ())
690
+ generics = generate (*genericParamsSyntax, Loc);
691
+
692
+ SmallVector<SILBoxTypeRepr::Field, 4 > Fields;
693
+ for (auto field : Type.getFields ()) {
694
+ auto specifier = field.getSpecifier ();
695
+ if (specifier.isMissing ())
696
+ return nullptr ;
697
+ bool Mutable;
698
+ if (specifier.getTokenKind () == tok::kw_let)
699
+ Mutable = false ;
700
+ else if (specifier.getTokenKind () == tok::kw_var)
701
+ Mutable = true ;
702
+ else
703
+ return nullptr ;
704
+ SourceLoc VarOrLetLoc = advanceLocBegin (Loc, specifier);;
705
+ auto fieldTy = generate (field.getType (), Loc);
706
+ if (!fieldTy)
707
+ return nullptr ;
708
+
709
+ Fields.emplace_back (VarOrLetLoc, Mutable, fieldTy);
710
+ }
711
+ GenericsScope.reset ();
712
+
713
+ auto LBraceLoc = advanceLocBegin (Loc, Type.getLeftBrace ());
714
+ auto RBraceLoc = advanceLocBegin (Loc, Type.getRightBrace ());
715
+
716
+ SourceLoc LAngleLoc, RAngleLoc;
717
+ SmallVector<TypeRepr*, 4 > Args;
718
+ if (auto genericArgs = Type.getGenericArgumentClause ()) {
719
+ if (genericArgs->getRightAngleBracket ().isMissing ())
720
+ return nullptr ;
721
+ LAngleLoc = advanceLocBegin (Loc, genericArgs->getLeftAngleBracket ());
722
+ RAngleLoc = advanceLocBegin (Loc, genericArgs->getRightAngleBracket ());
723
+ Args = generate (genericArgs->getArguments (), Loc);
724
+ }
725
+
726
+ auto SILType = SILBoxTypeRepr::create (Context, generics, LBraceLoc, Fields,
727
+ RBraceLoc, LAngleLoc, Args, RAngleLoc);
728
+ return SILType;
729
+ }
730
+
731
+ TypeRepr *ASTGen::generate (const SILFunctionTypeSyntax &Type,
732
+ const SourceLoc Loc, bool IsSILFuncDecl) {
733
+ // If this is part of a sil function decl, generic parameters are visible in
734
+ // the function body; otherwise, they are visible when parsing the type.
735
+ Optional<Scope> GenericsScope;
736
+ if (!IsSILFuncDecl)
737
+ GenericsScope.emplace (&P, ScopeKind::Generics);
738
+
739
+ GenericParamList *generics = nullptr ;
740
+ if (auto genericParamsSyntax = Type.getGenericParameterClauses ())
741
+ generics = generate (*genericParamsSyntax, Loc);
742
+
743
+ auto tyR = cast<FunctionTypeRepr>(generate (Type.getFunction (), Loc));
744
+ return new (Context)
745
+ FunctionTypeRepr (generics, tyR->getArgsTypeRepr (), tyR->getThrowsLoc (),
746
+ tyR->getArrowLoc (), tyR->getResultTypeRepr ());
747
+ }
748
+
584
749
TypeRepr *ASTGen::generate (const CodeCompletionTypeSyntax &Type,
585
750
const SourceLoc Loc) {
586
751
auto base = Type.getBase ();
587
752
if (!base)
588
753
return nullptr ;
589
754
590
- TypeRepr *parsedTyR = generate (*base, Loc);
591
- if ( parsedTyR) {
592
- if (P. CodeCompletion )
755
+ if (P. CodeCompletion ) {
756
+ TypeRepr * parsedTyR = generate (*base, Loc);
757
+ if (parsedTyR )
593
758
P.CodeCompletion ->setParsedTypeLoc (parsedTyR);
594
759
}
595
- return parsedTyR;
760
+
761
+ // Return nullptr to typecheck this TypeRepr independently in code completion.
762
+ return nullptr ;
596
763
}
597
764
598
765
TypeRepr *ASTGen::generate (const UnknownTypeSyntax &Type, const SourceLoc Loc) {
@@ -650,12 +817,11 @@ TypeRepr *ASTGen::generate(const UnknownTypeSyntax &Type, const SourceLoc Loc) {
650
817
SmallVector<TypeRepr *, 4 >
651
818
ASTGen::generate (const GenericArgumentListSyntax &Args, const SourceLoc Loc) {
652
819
SmallVector<TypeRepr *, 4 > Types;
653
- Types.resize (Args.size ());
654
-
655
- for (int i = Args.size () - 1 ; i >= 0 ; i--) {
656
- auto Arg = Args.getChild (i).getValue ().castTo <GenericArgumentSyntax>();
657
- auto Type = generate (Arg, Loc);
658
- Types[i] = Type;
820
+ for (auto Arg : Args) {
821
+ auto tyR = generate (Arg, Loc);
822
+ if (!tyR)
823
+ tyR = new (Context) ErrorTypeRepr (advanceLocBegin (Loc, Arg));
824
+ Types.push_back (tyR);
659
825
}
660
826
661
827
return Types;
0 commit comments