12
12
13
13
#include " swift/Parse/ASTGen.h"
14
14
15
- #include " DebuggerContextChange.h"
16
15
#include " swift/Basic/SourceManager.h"
17
16
#include " swift/Parse/CodeCompletionCallbacks.h"
18
17
#include " swift/Parse/Parser.h"
@@ -24,132 +23,6 @@ SourceLoc ASTGen::generate(const TokenSyntax &Tok, const SourceLoc Loc) {
24
23
return advanceLocBegin (Loc, Tok);
25
24
}
26
25
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
-
153
26
Expr *ASTGen::generate (const IntegerLiteralExprSyntax &Expr,
154
27
const SourceLoc Loc) {
155
28
auto Digits = Expr.getDigits ();
@@ -251,8 +124,6 @@ TypeRepr *ASTGen::generate(const TypeSyntax &Type, const SourceLoc Loc) {
251
124
TypeAST = generate (*Unwrapped, Loc);
252
125
else if (auto Attributed = Type.getAs <AttributedTypeSyntax>())
253
126
TypeAST = generate (*Attributed, Loc);
254
- else if (auto ClassRestriction = Type.getAs <ClassRestrictionTypeSyntax>())
255
- TypeAST = generate (*ClassRestriction, Loc);
256
127
else if (auto CompletionTy = Type.getAs <CodeCompletionTypeSyntax>())
257
128
TypeAST = generate (*CompletionTy, Loc);
258
129
else if (auto Unknown = Type.getAs <UnknownTypeSyntax>())
@@ -513,6 +384,11 @@ TypeRepr *ASTGen::generate(const SimpleTypeIdentifierSyntax &Type,
513
384
auto AnyLoc = advanceLocBegin (Loc, Type.getName ());
514
385
return CompositionTypeRepr::createEmptyComposition (Context, AnyLoc);
515
386
}
387
+ if (Type.getName ().getText () == " class" ) {
388
+ auto classLoc = advanceLocBegin (Loc, Type.getName ());
389
+ return new (Context)
390
+ SimpleIdentTypeRepr (classLoc, Context.getIdentifier (" AnyObject" ));
391
+ }
516
392
517
393
return generateSimpleOrMemberIdentifier (Type, Loc);
518
394
}
@@ -572,13 +448,6 @@ TypeRepr *ASTGen::generate(const ImplicitlyUnwrappedOptionalTypeSyntax &Type,
572
448
ImplicitlyUnwrappedOptionalTypeRepr (WrappedType, ExclamationLoc);
573
449
}
574
450
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
-
582
451
TypeRepr *ASTGen::generate (const CodeCompletionTypeSyntax &Type,
583
452
const SourceLoc Loc) {
584
453
auto base = Type.getBase ();
@@ -720,7 +589,7 @@ GenericParamList *ASTGen::generate(const GenericParameterClauseSyntax &clause,
720
589
721
590
if (auto inherited = elem.getInheritedType ()) {
722
591
if (auto ty = generate (*inherited, Loc)) {
723
- SmallVector<TypeLoc, 1 > constraints = {ty };
592
+ SmallVector<TypeLoc, 1 > constraints = {generate (*inherited, Loc) };
724
593
param->setInherited (Context.AllocateCopy (constraints));
725
594
}
726
595
}
0 commit comments