Skip to content

Commit fdaa886

Browse files
committed
Sema: Rework resolveTypeInContext()
Separate the "find the correct context" part from "substitute in the base type" part. The former can go away completely after a bit of refactoring.
1 parent 3328592 commit fdaa886

10 files changed

+206
-223
lines changed

lib/Sema/GenericTypeResolver.h

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class GenericTypeResolver {
4747
/// \returns The resolved generic type parameter type, which may be \c gp.
4848
virtual Type resolveGenericTypeParamType(GenericTypeParamType *gp) = 0;
4949

50-
/// Resolve a reference to a member within a dependent type.
50+
/// Resolve a qualified reference to a type member within a dependent type.
5151
///
5252
/// \param baseTy The base of the member access.
5353
/// \param baseRange The source range covering the base type.
@@ -60,7 +60,7 @@ class GenericTypeResolver {
6060
SourceRange baseRange,
6161
ComponentIdentTypeRepr *ref) = 0;
6262

63-
/// Resolve a reference to an associated type within the 'Self' type
63+
/// Resolve an unqualified reference to an associated type of the 'Self' type
6464
/// of a protocol.
6565
///
6666
/// \param selfTy The base of the member access.
@@ -69,21 +69,15 @@ class GenericTypeResolver {
6969
/// \returns A type that refers to the dependent member type, or an error
7070
/// type if such a reference is ill-formed.
7171
virtual Type resolveSelfAssociatedType(Type selfTy,
72-
DeclContext *DC,
7372
AssociatedTypeDecl *assocType) = 0;
7473

75-
/// Retrieve the type when referring to the given context.
74+
/// Resolve the self type within the given context.
7675
///
7776
/// \param dc A context in which type checking occurs, which must be a type
7877
/// context (i.e., nominal type or extension thereof).
7978
///
80-
/// \param wantSelf Do we want the type of the context itself (the
81-
/// existential type, for protocols) or the type of 'self' inside the
82-
/// context (the 'Self' generic parameter, for protocols). Has no effect
83-
/// for concrete types.
84-
///
8579
/// \returns the type of context.
86-
virtual Type resolveTypeOfContext(DeclContext *dc, bool wantSelf=false) = 0;
80+
virtual Type resolveTypeOfContext(DeclContext *dc) = 0;
8781

8882
/// Retrieve the type when referring to the given type declaration within
8983
/// its context.
@@ -116,10 +110,9 @@ class DependentGenericTypeResolver : public GenericTypeResolver {
116110
ComponentIdentTypeRepr *ref);
117111

118112
virtual Type resolveSelfAssociatedType(Type selfTy,
119-
DeclContext *DC,
120113
AssociatedTypeDecl *assocType);
121114

122-
virtual Type resolveTypeOfContext(DeclContext *dc, bool wantSelf=false);
115+
virtual Type resolveTypeOfContext(DeclContext *dc);
123116

124117
virtual Type resolveTypeOfDecl(TypeDecl *decl);
125118

@@ -149,10 +142,9 @@ class GenericTypeToArchetypeResolver : public GenericTypeResolver {
149142
ComponentIdentTypeRepr *ref);
150143

151144
virtual Type resolveSelfAssociatedType(Type selfTy,
152-
DeclContext *DC,
153145
AssociatedTypeDecl *assocType);
154146

155-
virtual Type resolveTypeOfContext(DeclContext *dc, bool wantSelf=false);
147+
virtual Type resolveTypeOfContext(DeclContext *dc);
156148

157149
virtual Type resolveTypeOfDecl(TypeDecl *decl);
158150

@@ -182,10 +174,9 @@ class CompleteGenericTypeResolver : public GenericTypeResolver {
182174
ComponentIdentTypeRepr *ref);
183175

184176
virtual Type resolveSelfAssociatedType(Type selfTy,
185-
DeclContext *DC,
186177
AssociatedTypeDecl *assocType);
187178

188-
virtual Type resolveTypeOfContext(DeclContext *dc, bool wantSelf=false);
179+
virtual Type resolveTypeOfContext(DeclContext *dc);
189180

190181
virtual Type resolveTypeOfDecl(TypeDecl *decl);
191182

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ Type DependentGenericTypeResolver::resolveDependentMemberType(
5050
}
5151

5252
Type DependentGenericTypeResolver::resolveSelfAssociatedType(
53-
Type selfTy,
54-
DeclContext *DC,
55-
AssociatedTypeDecl *assocType) {
53+
Type selfTy, AssociatedTypeDecl *assocType) {
5654
auto archetype = Builder.resolveArchetype(selfTy);
5755
assert(archetype && "Bad generic context nesting?");
5856

@@ -61,15 +59,8 @@ Type DependentGenericTypeResolver::resolveSelfAssociatedType(
6159
->getDependentType(/*FIXME: */{ }, /*allowUnresolved=*/true);
6260
}
6361

64-
static Type getTypeOfContext(DeclContext *dc, bool wantSelf) {
65-
if (wantSelf)
66-
return dc->getSelfInterfaceType();
67-
return dc->getDeclaredInterfaceType();
68-
}
69-
70-
Type DependentGenericTypeResolver::resolveTypeOfContext(DeclContext *dc,
71-
bool wantSelf) {
72-
return getTypeOfContext(dc, wantSelf);
62+
Type DependentGenericTypeResolver::resolveTypeOfContext(DeclContext *dc) {
63+
return dc->getSelfInterfaceType();
7364
}
7465

7566
Type DependentGenericTypeResolver::resolveTypeOfDecl(TypeDecl *decl) {
@@ -106,21 +97,20 @@ Type GenericTypeToArchetypeResolver::resolveDependentMemberType(
10697
}
10798

10899
Type GenericTypeToArchetypeResolver::resolveSelfAssociatedType(
109-
Type selfTy,
110-
DeclContext *DC,
111-
AssociatedTypeDecl *assocType) {
100+
Type selfTy, AssociatedTypeDecl *assocType) {
112101
llvm_unreachable("Dependent type after archetype substitution");
113102
}
114103

115-
Type GenericTypeToArchetypeResolver::resolveTypeOfContext(DeclContext *dc,
116-
bool wantSelf) {
104+
Type GenericTypeToArchetypeResolver::resolveTypeOfContext(DeclContext *dc) {
117105
// FIXME: Fallback case.
118-
if (dc->isGenericContext() && !dc->isValidGenericContext())
119-
return getTypeOfContext(dc, wantSelf);
106+
if (!isa<ExtensionDecl>(dc) &&
107+
dc->isGenericContext() &&
108+
!dc->isValidGenericContext())
109+
return dc->getSelfInterfaceType();
120110

121111
return ArchetypeBuilder::mapTypeIntoContext(
122112
dc->getParentModule(), GenericEnv,
123-
getTypeOfContext(dc, wantSelf));
113+
dc->getSelfInterfaceType());
124114
}
125115

126116
Type GenericTypeToArchetypeResolver::resolveTypeOfDecl(TypeDecl *decl) {
@@ -242,17 +232,15 @@ Type CompleteGenericTypeResolver::resolveDependentMemberType(
242232
return ErrorType::get(TC.Context);
243233
}
244234

245-
Type CompleteGenericTypeResolver::resolveSelfAssociatedType(Type selfTy,
246-
DeclContext *DC,
247-
AssociatedTypeDecl *assocType) {
235+
Type CompleteGenericTypeResolver::resolveSelfAssociatedType(
236+
Type selfTy, AssociatedTypeDecl *assocType) {
248237
return Builder.resolveArchetype(selfTy)->getRepresentative()
249238
->getNestedType(assocType->getName(), Builder)
250239
->getDependentType(/*FIXME: */{ }, /*allowUnresolved=*/false);
251240
}
252241

253-
Type CompleteGenericTypeResolver::resolveTypeOfContext(DeclContext *dc,
254-
bool wantSelf) {
255-
return getTypeOfContext(dc, wantSelf);
242+
Type CompleteGenericTypeResolver::resolveTypeOfContext(DeclContext *dc) {
243+
return dc->getSelfInterfaceType();
256244
}
257245

258246
Type CompleteGenericTypeResolver::resolveTypeOfDecl(TypeDecl *decl) {

0 commit comments

Comments
 (0)