12
12
13
13
#include " swift/Parse/ASTGen.h"
14
14
15
+ #include " DebuggerContextChange.h"
15
16
#include " swift/Basic/SourceManager.h"
16
17
#include " swift/Parse/CodeCompletionCallbacks.h"
17
18
#include " swift/Parse/Parser.h"
@@ -23,6 +24,132 @@ SourceLoc ASTGen::generate(const TokenSyntax &Tok, const SourceLoc Loc) {
23
24
return advanceLocBegin (Loc, Tok);
24
25
}
25
26
27
+ SourceLoc ASTGen::generateIdentifierDeclName (const syntax::TokenSyntax &Tok,
28
+ const SourceLoc Loc,
29
+ Identifier &Id) {
30
+ StringRef text;
31
+ if (Tok.getText () == " Any" )
32
+ // Special handle 'Any' because we don't want to accidantaly declare 'Any'
33
+ // type in any way.
34
+ text = " #Any" ;
35
+ else
36
+ text = Tok.getIdentifierText ();
37
+
38
+ Id = Context.getIdentifier (text);
39
+ return advanceLocBegin (Loc, Tok);
40
+ }
41
+
42
+ Decl *ASTGen::generate (const DeclSyntax &D, const SourceLoc Loc) {
43
+ Decl *DeclAST = nullptr ;
44
+
45
+ if (auto associatedTypeDecl = D.getAs <AssociatedtypeDeclSyntax>()) {
46
+ DeclAST = generate (*associatedTypeDecl, Loc);
47
+ } else {
48
+ llvm_unreachable (" unsupported decl kind" );
49
+ }
50
+
51
+ return DeclAST;
52
+ }
53
+
54
+ DeclAttributes
55
+ ASTGen::generateDeclAttributes (const DeclSyntax &D,
56
+ const Optional<AttributeListSyntax> &attrs,
57
+ const Optional<ModifierListSyntax> &modifiers,
58
+ SourceLoc Loc, bool includeComments) {
59
+ SourceLoc attrsLoc;
60
+ if (attrs) {
61
+ attrsLoc = advanceLocBegin (Loc, *attrs->getFirstToken ());
62
+ } else if (modifiers) {
63
+ attrsLoc = advanceLocBegin (Loc, *modifiers->getFirstToken ());
64
+ } else {
65
+ // We might have comment attributes.
66
+ attrsLoc = advanceLocBegin (Loc, *D.getFirstToken ());
67
+ }
68
+ if (hasDeclAttributes (attrsLoc))
69
+ return getDeclAttributes (attrsLoc);
70
+ return DeclAttributes ();
71
+ }
72
+
73
+ MutableArrayRef<TypeLoc>
74
+ ASTGen::generate (const TypeInheritanceClauseSyntax &clause, SourceLoc Loc,
75
+ bool allowClassRequirement) {
76
+ SmallVector<TypeLoc, 2 > inherited;
77
+
78
+ bool hasClass = false ;
79
+ for (const auto elem : clause.getInheritedTypeCollection ()) {
80
+ const auto &tySyntax = elem.getTypeName ();
81
+ if (tySyntax.is <ClassRestrictionTypeSyntax>()) {
82
+ // Accept 'class' only if it's allowed and it's the first one.
83
+ if (!allowClassRequirement || hasClass)
84
+ continue ;
85
+ hasClass = true ;
86
+ }
87
+ if (auto ty = generate (tySyntax, Loc))
88
+ inherited.emplace_back (ty);
89
+ }
90
+
91
+ return Context.AllocateCopy (inherited);
92
+ }
93
+
94
+ TypeDecl *ASTGen::generate (const AssociatedtypeDeclSyntax &D,
95
+ const SourceLoc Loc) {
96
+ if (!isa<ProtocolDecl>(P.CurDeclContext )) {
97
+ // This is already diagnosed in Parser.
98
+ return nullptr ;
99
+ }
100
+
101
+ auto idToken = D.getIdentifier ();
102
+ if (idToken.isMissing ())
103
+ return nullptr ;
104
+
105
+ auto keywordLoc = advanceLocBegin (Loc, D.getAssociatedtypeKeyword ());
106
+ auto name = Context.getIdentifier (idToken.getIdentifierText ());
107
+ auto nameLoc = advanceLocBegin (Loc, idToken);
108
+
109
+ DeclAttributes attrs =
110
+ generateDeclAttributes (D, D.getAttributes (), D.getModifiers (), Loc, true );
111
+
112
+ DebuggerContextChange DCC (P, name, DeclKind::AssociatedType);
113
+
114
+ ArrayRef<TypeLoc> inherited;
115
+ if (const auto inheritanceClause = D.getInheritanceClause ())
116
+ inherited =
117
+ generate (*inheritanceClause, Loc, /* allowClassRequirement=*/ true );
118
+
119
+ TypeRepr *defaultTy = nullptr ;
120
+ if (const auto init = D.getInitializer ())
121
+ defaultTy = generate (init->getValue (), Loc);
122
+
123
+ TrailingWhereClause *trailingWhere = nullptr ;
124
+ if (auto whereClause = D.getGenericWhereClause ())
125
+ trailingWhere = generate (*whereClause, Loc);
126
+
127
+ auto assocType = new (Context)
128
+ AssociatedTypeDecl (P.CurDeclContext , keywordLoc, name, nameLoc, defaultTy,
129
+ trailingWhere);
130
+ assocType->getAttrs () = attrs;
131
+ if (!inherited.empty ())
132
+ assocType->setInherited (Context.AllocateCopy (inherited));
133
+ addToScope (assocType);
134
+ return assocType;
135
+ }
136
+
137
+ TrailingWhereClause *ASTGen::generate (const GenericWhereClauseSyntax &syntax,
138
+ const SourceLoc Loc) {
139
+ SourceLoc whereLoc = advanceLocBegin (Loc, syntax.getWhereKeyword ());
140
+
141
+ SmallVector<RequirementRepr, 4 > requirements;
142
+ requirements.reserve (syntax.getRequirementList ().size ());
143
+ for (auto elem : syntax.getRequirementList ()) {
144
+ if (auto req = generate (elem, Loc))
145
+ requirements.push_back (*req);
146
+ }
147
+
148
+ if (requirements.empty ())
149
+ return nullptr ;
150
+ return TrailingWhereClause::create (Context, whereLoc, requirements);
151
+ }
152
+
26
153
Expr *ASTGen::generate (const IntegerLiteralExprSyntax &Expr,
27
154
const SourceLoc Loc) {
28
155
auto Digits = Expr.getDigits ();
@@ -124,6 +251,8 @@ TypeRepr *ASTGen::generate(const TypeSyntax &Type, const SourceLoc Loc) {
124
251
TypeAST = generate (*Unwrapped, Loc);
125
252
else if (auto Attributed = Type.getAs <AttributedTypeSyntax>())
126
253
TypeAST = generate (*Attributed, Loc);
254
+ else if (auto ClassRestriction = Type.getAs <ClassRestrictionTypeSyntax>())
255
+ TypeAST = generate (*ClassRestriction, Loc);
127
256
else if (auto CompletionTy = Type.getAs <CodeCompletionTypeSyntax>())
128
257
TypeAST = generate (*CompletionTy, Loc);
129
258
else if (auto Unknown = Type.getAs <UnknownTypeSyntax>())
@@ -384,11 +513,6 @@ TypeRepr *ASTGen::generate(const SimpleTypeIdentifierSyntax &Type,
384
513
auto AnyLoc = advanceLocBegin (Loc, Type.getName ());
385
514
return CompositionTypeRepr::createEmptyComposition (Context, AnyLoc);
386
515
}
387
- if (Type.getName ().getText () == " class" ) {
388
- auto classLoc = advanceLocBegin (Loc, Type.getName ());
389
- return new (Context)
390
- SimpleIdentTypeRepr (classLoc, Context.getIdentifier (" AnyObject" ));
391
- }
392
516
393
517
return generateSimpleOrMemberIdentifier (Type, Loc);
394
518
}
@@ -448,6 +572,13 @@ TypeRepr *ASTGen::generate(const ImplicitlyUnwrappedOptionalTypeSyntax &Type,
448
572
ImplicitlyUnwrappedOptionalTypeRepr (WrappedType, ExclamationLoc);
449
573
}
450
574
575
+ TypeRepr *
576
+ ASTGen::generate (const ClassRestrictionTypeSyntax &Type, const SourceLoc Loc) {
577
+ auto classLoc = advanceLocBegin (Loc, Type);
578
+ return new (Context)
579
+ SimpleIdentTypeRepr (classLoc, Context.getIdentifier (" AnyObject" ));
580
+ }
581
+
451
582
TypeRepr *ASTGen::generate (const CodeCompletionTypeSyntax &Type,
452
583
const SourceLoc Loc) {
453
584
auto base = Type.getBase ();
@@ -589,7 +720,7 @@ GenericParamList *ASTGen::generate(const GenericParameterClauseSyntax &clause,
589
720
590
721
if (auto inherited = elem.getInheritedType ()) {
591
722
if (auto ty = generate (*inherited, Loc)) {
592
- SmallVector<TypeLoc, 1 > constraints = {generate (*inherited, Loc) };
723
+ SmallVector<TypeLoc, 1 > constraints = {ty };
593
724
param->setInherited (Context.AllocateCopy (constraints));
594
725
}
595
726
}
0 commit comments