@@ -171,83 +171,67 @@ static bool hasEnumElementOrStaticVarMember(DeclContext *DC, Type ty,
171
171
});
172
172
}
173
173
174
- namespace {
175
- // / Build up an \c DeclRefTypeRepr and see what it resolves to .
176
- // / FIXME: Support DeclRefTypeRepr nodes with non-identifier base components .
177
- struct ExprToDeclRefTypeRepr : public ASTVisitor < ExprToDeclRefTypeRepr, bool > {
178
- SmallVectorImpl<IdentTypeRepr *> &components;
179
- ASTContext &C;
174
+ static DeclRefTypeRepr * translateExprToDeclRefTypeRepr (Expr *E, ASTContext &C) {
175
+ // FIXME: Support MemberTypeRepr nodes with non- DeclRefTypeRepr bases .
176
+ // / Translates an expression to a \c DeclRefTypeRepr .
177
+ class ExprToDeclRefTypeRepr
178
+ : public ExprVisitor<ExprToDeclRefTypeRepr, DeclRefTypeRepr *> {
179
+ ASTContext &C;
180
180
181
- ExprToDeclRefTypeRepr ( decltype (components) &components, ASTContext &C)
182
- : components(components), C(C) {}
181
+ public:
182
+ ExprToDeclRefTypeRepr (ASTContext &C) : C(C) {}
183
183
184
- bool visitExpr (Expr *e) {
185
- return false ;
186
- }
187
-
188
- bool visitTypeExpr (TypeExpr *te) {
189
- if (auto *TR = te->getTypeRepr ())
190
- if (auto *ITR = dyn_cast<IdentTypeRepr>(TR)) {
191
- components.push_back (ITR);
192
- return true ;
184
+ DeclRefTypeRepr *visitExpr (Expr *e) { return nullptr ; }
185
+
186
+ DeclRefTypeRepr *visitTypeExpr (TypeExpr *te) {
187
+ return dyn_cast_or_null<IdentTypeRepr>(te->getTypeRepr ());
188
+ }
189
+
190
+ DeclRefTypeRepr *visitDeclRefExpr (DeclRefExpr *dre) {
191
+ // Get the declared type.
192
+ auto *td = dyn_cast<TypeDecl>(dre->getDecl ());
193
+ if (!td) {
194
+ return nullptr ;
193
195
}
194
- return false ;
195
- }
196
196
197
- bool visitDeclRefExpr (DeclRefExpr *dre) {
198
- assert (components.empty () && " decl ref should be root element of expr" );
199
-
200
- // Get the declared type.
201
- if (auto *td = dyn_cast<TypeDecl>(dre->getDecl ())) {
202
- components.push_back (
203
- new (C) SimpleIdentTypeRepr (dre->getNameLoc (), td->createNameRef ()));
204
- components.back ()->setValue (td, nullptr );
205
- return true ;
197
+ auto *repr =
198
+ new (C) SimpleIdentTypeRepr (dre->getNameLoc (), td->createNameRef ());
199
+ repr->setValue (td, nullptr );
200
+
201
+ return repr;
206
202
}
207
- return false ;
208
- }
209
-
210
- bool visitUnresolvedDeclRefExpr (UnresolvedDeclRefExpr *udre) {
211
- assert (components.empty () && " decl ref should be root element of expr" );
212
- // Track the AST location of the component.
213
- components.push_back (
214
- new (C) SimpleIdentTypeRepr (udre->getNameLoc (),
215
- udre->getName ()));
216
- return true ;
217
- }
218
-
219
- bool visitUnresolvedDotExpr (UnresolvedDotExpr *ude) {
220
- if (!visit (ude->getBase ()))
221
- return false ;
222
-
223
- assert (!components.empty () && " no components before dot expr?!" );
224
203
225
- // Track the AST location of the new component.
226
- components.push_back (
227
- new (C) SimpleIdentTypeRepr (ude->getNameLoc (),
228
- ude->getName ()));
229
- return true ;
230
- }
231
-
232
- bool visitUnresolvedSpecializeExpr (UnresolvedSpecializeExpr *use) {
233
- if (!visit (use->getSubExpr ()))
234
- return false ;
235
-
236
- assert (!components.empty () && " no components before generic args?!" );
237
-
238
- // Track the AST location of the generic arguments.
239
- auto origComponent = components.back ();
240
- components.back () =
241
- GenericIdentTypeRepr::create (C, origComponent->getNameLoc (),
242
- origComponent->getNameRef (),
243
- use->getUnresolvedParams (),
244
- SourceRange (use->getLAngleLoc (),
245
- use->getRAngleLoc ()));
246
-
247
- return true ;
248
- }
249
- };
250
- } // end anonymous namespace
204
+ DeclRefTypeRepr *visitUnresolvedDeclRefExpr (UnresolvedDeclRefExpr *udre) {
205
+ return new (C) SimpleIdentTypeRepr (udre->getNameLoc (), udre->getName ());
206
+ }
207
+
208
+ DeclRefTypeRepr *visitUnresolvedDotExpr (UnresolvedDotExpr *ude) {
209
+ auto *base = visit (ude->getBase ());
210
+ if (!base) {
211
+ return nullptr ;
212
+ }
213
+
214
+ return MemberTypeRepr::create (C, base, ude->getNameLoc (), ude->getName ());
215
+ }
216
+
217
+ DeclRefTypeRepr *
218
+ visitUnresolvedSpecializeExpr (UnresolvedSpecializeExpr *use) {
219
+ auto *base = visit (use->getSubExpr ());
220
+ if (!base) {
221
+ return nullptr ;
222
+ }
223
+
224
+ assert (!base->hasGenericArgList () && " Already has generic arguments" );
225
+
226
+ return DeclRefTypeRepr::create (
227
+ C, base->getBase (), base->getNameLoc (), base->getNameRef (),
228
+ use->getUnresolvedParams (),
229
+ SourceRange (use->getLAngleLoc (), use->getRAngleLoc ()));
230
+ }
231
+ } translator (C);
232
+
233
+ return translator.visit (E);
234
+ }
251
235
252
236
namespace {
253
237
class ResolvePattern : public ASTVisitor <ResolvePattern,
@@ -470,15 +454,15 @@ class ResolvePattern : public ASTVisitor<ResolvePattern,
470
454
// Member syntax 'T.Element' forms a pattern if 'T' is an enum and the
471
455
// member name is a member of the enum.
472
456
Pattern *visitUnresolvedDotExpr (UnresolvedDotExpr *ude) {
473
- SmallVector<IdentTypeRepr *, 2 > components;
474
- if (!ExprToDeclRefTypeRepr (components, Context).visit (ude->getBase ()))
457
+ DeclRefTypeRepr *repr =
458
+ translateExprToDeclRefTypeRepr (ude->getBase (), Context);
459
+ if (!repr) {
475
460
return nullptr ;
461
+ }
476
462
477
463
const auto options =
478
464
TypeResolutionOptions (llvm::None) | TypeResolutionFlags::SilenceErrors;
479
465
480
- DeclRefTypeRepr *repr = MemberTypeRepr::create (Context, components);
481
-
482
466
// See if the repr resolves to a type.
483
467
const auto ty = TypeResolution::resolveContextualType (
484
468
repr, DC, options,
@@ -572,39 +556,36 @@ class ResolvePattern : public ASTVisitor<ResolvePattern,
572
556
return P;
573
557
}
574
558
575
- SmallVector<IdentTypeRepr *, 2 > components;
576
- if (!ExprToDeclRefTypeRepr (components, Context).visit (ce->getFn ()))
577
- return nullptr ;
578
-
579
- if (components.empty ())
559
+ DeclRefTypeRepr *repr =
560
+ translateExprToDeclRefTypeRepr (ce->getFn (), Context);
561
+ if (!repr) {
580
562
return nullptr ;
563
+ }
581
564
582
- auto tailComponent = components.pop_back_val ();
583
565
EnumElementDecl *referencedElement = nullptr ;
584
566
TypeExpr *baseTE = nullptr ;
585
567
586
- if (components.empty ()) {
587
- // Only one component. Try looking up an enum element in context.
588
- referencedElement
589
- = lookupUnqualifiedEnumMemberElement (DC, tailComponent->getNameRef (),
590
- tailComponent->getLoc ());
568
+ if (isa<IdentTypeRepr>(repr)) {
569
+ // Not qualified. Try looking up an enum element in context.
570
+ referencedElement = lookupUnqualifiedEnumMemberElement (
571
+ DC, repr->getNameRef (), repr->getLoc ());
591
572
if (!referencedElement)
592
573
return nullptr ;
593
574
594
575
auto *enumDecl = referencedElement->getParentEnum ();
595
576
baseTE = TypeExpr::createImplicit (enumDecl->getDeclaredTypeInContext (),
596
577
Context);
597
578
} else {
598
- const auto options = TypeResolutionOptions (llvm::None) |
599
- TypeResolutionFlags::SilenceErrors;
600
-
601
579
// Otherwise, see whether we had an enum type as the penultimate
602
580
// component, and look up an element inside it.
603
- DeclRefTypeRepr *prefixRepr = MemberTypeRepr::create (Context, components);
581
+ auto *qualIdentTR = cast<MemberTypeRepr>(repr);
582
+
583
+ const auto options = TypeResolutionOptions (llvm::None) |
584
+ TypeResolutionFlags::SilenceErrors;
604
585
605
586
// See first if the entire repr resolves to a type.
606
587
const Type enumTy = TypeResolution::resolveContextualType (
607
- prefixRepr , DC, options,
588
+ qualIdentTR-> getBase () , DC, options,
608
589
[](auto unboundTy) {
609
590
// FIXME: Don't let unbound generic types escape type
610
591
// resolution. For now, just return the unbound generic type.
@@ -619,26 +600,23 @@ class ResolvePattern : public ASTVisitor<ResolvePattern,
619
600
if (!enumDecl)
620
601
return nullptr ;
621
602
622
- referencedElement
623
- = lookupEnumMemberElement (DC, enumTy,
624
- tailComponent->getNameRef (),
625
- tailComponent->getLoc ());
603
+ referencedElement = lookupEnumMemberElement (
604
+ DC, enumTy, qualIdentTR->getNameRef (), qualIdentTR->getLoc ());
626
605
if (!referencedElement)
627
606
return nullptr ;
628
607
629
608
baseTE = TypeExpr::createForMemberDecl (
630
- prefixRepr, tailComponent ->getNameLoc (), enumDecl);
609
+ qualIdentTR-> getBase (), qualIdentTR ->getNameLoc (), enumDecl);
631
610
baseTE->setType (MetatypeType::get (enumTy));
632
611
}
633
612
634
613
assert (baseTE && baseTE->getType () && " Didn't initialize base expression?" );
635
- assert (!isa<GenericIdentTypeRepr>(tailComponent) &&
636
- " should be handled above" );
614
+ assert (!repr->hasGenericArgList () && " should be handled above" );
637
615
638
616
auto *subPattern = composeTupleOrParenPattern (ce->getArgs ());
639
617
return new (Context) EnumElementPattern (
640
- baseTE, SourceLoc (), tailComponent ->getNameLoc (),
641
- tailComponent-> getNameRef (), referencedElement, subPattern, DC);
618
+ baseTE, SourceLoc (), repr ->getNameLoc (), repr-> getNameRef (),
619
+ referencedElement, subPattern, DC);
642
620
}
643
621
};
644
622
0 commit comments