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