15
15
//
16
16
// ===----------------------------------------------------------------------===//
17
17
#include " swift/AST/ASTContext.h"
18
+ #include " swift/AST/CanTypeVisitor.h"
18
19
#include " swift/AST/Decl.h"
19
20
#include " swift/AST/Type.h"
20
- #include " swift/AST/TypeVisitor.h"
21
21
#include " swift/AST/Types.h"
22
22
#include " llvm/ADT/SmallPtrSet.h"
23
23
using namespace swift ;
@@ -26,43 +26,33 @@ using namespace swift;
26
26
// used for optimizing away extra exploratory work in the constraint
27
27
// solver. It should eventually encompass all of the subtyping rules
28
28
// of the language.
29
- struct TypeJoin : TypeVisitor <TypeJoin, Type > {
30
- Type First;
29
+ struct TypeJoin : CanTypeVisitor <TypeJoin, CanType > {
30
+ CanType First;
31
31
32
- TypeJoin (Type First) : First(First) {
32
+ TypeJoin (CanType First) : First(First) {
33
33
assert (First && " Unexpected null type!" );
34
34
}
35
35
36
- static Type getSuperclassJoin (Type first, Type second);
36
+ static CanType getSuperclassJoin (CanType first, CanType second);
37
37
38
- Type visitClassType (Type second);
39
- Type visitBoundGenericClassType (Type second);
40
- Type visitArchetypeType (Type second);
41
- Type visitDynamicSelfType (Type second);
42
- Type visitMetatypeType (Type second);
43
- Type visitExistentialMetatypeType (Type second);
44
- Type visitBoundGenericEnumType (Type second);
38
+ CanType visitClassType (CanType second);
39
+ CanType visitBoundGenericClassType (CanType second);
40
+ CanType visitArchetypeType (CanType second);
41
+ CanType visitDynamicSelfType (CanType second);
42
+ CanType visitMetatypeType (CanType second);
43
+ CanType visitExistentialMetatypeType (CanType second);
44
+ CanType visitBoundGenericEnumType (CanType second);
45
45
46
- Type visitOptionalType (Type second);
46
+ CanType visitOptionalType (CanType second);
47
47
48
- Type visitType (Type second) {
48
+ CanType visitType (CanType second) {
49
49
// FIXME: Implement all the visitors.
50
50
// llvm_unreachable("Unimplemented type visitor!");
51
51
return First->getASTContext ().TheAnyType ;
52
52
}
53
53
54
54
public:
55
- static Type join (Type first, Type second) {
56
- if (!first || !second) {
57
- if (first)
58
- return ErrorType::get (first->getASTContext ());
59
-
60
- if (second)
61
- return ErrorType::get (second->getASTContext ());
62
-
63
- return Type ();
64
- }
65
-
55
+ static CanType join (CanType first, CanType second) {
66
56
assert (!first->hasTypeVariable () && !second->hasTypeVariable () &&
67
57
" Cannot compute join of types involving type variables" );
68
58
@@ -72,7 +62,7 @@ struct TypeJoin : TypeVisitor<TypeJoin, Type> {
72
62
" Expected simple type!" );
73
63
74
64
// If the types are equivalent, the join is obvious.
75
- if (first-> isEqual ( second) )
65
+ if (first == second)
76
66
return first;
77
67
78
68
// Until we handle all the combinations of joins, we need to make
@@ -85,118 +75,124 @@ struct TypeJoin : TypeVisitor<TypeJoin, Type> {
85
75
}
86
76
};
87
77
88
- Type TypeJoin::getSuperclassJoin (Type first, Type second) {
89
- if (!first || !second)
90
- return TypeJoin::join (first, second);
91
-
78
+ CanType TypeJoin::getSuperclassJoin (CanType first, CanType second) {
92
79
if (!first->mayHaveSuperclass () || !second->mayHaveSuperclass ())
93
80
return first->getASTContext ().TheAnyType ;
94
81
95
82
// / Walk the superclasses of `first` looking for `second`. Record them
96
83
// / for our second step.
97
84
llvm::SmallPtrSet<CanType, 8 > superclassesOfFirst;
98
- CanType canSecond = second->getCanonicalType ();
99
85
for (Type super = first; super; super = super->getSuperclass ()) {
100
- CanType canSuper = super->getCanonicalType ();
86
+ auto canSuper = super->getCanonicalType ();
101
87
102
88
// If we have found the second type, we're done.
103
- if (canSuper == canSecond )
104
- return super ;
89
+ if (canSuper == second )
90
+ return canSuper ;
105
91
106
92
superclassesOfFirst.insert (canSuper);
107
93
}
108
94
109
95
// Look through the superclasses of second to determine if any were also
110
96
// superclasses of first.
111
97
for (Type super = second; super; super = super->getSuperclass ()) {
112
- CanType canSuper = super->getCanonicalType ();
98
+ auto canSuper = super->getCanonicalType ();
113
99
114
100
// If we found the first type, we're done.
115
101
if (superclassesOfFirst.count (canSuper))
116
- return super ;
102
+ return canSuper ;
117
103
}
118
104
119
105
// There is no common superclass; we're done.
120
106
return first->getASTContext ().TheAnyType ;
121
107
}
122
108
123
- Type TypeJoin::visitClassType (Type second) {
109
+ CanType TypeJoin::visitClassType (CanType second) {
124
110
return getSuperclassJoin (First, second);
125
111
}
126
112
127
- Type TypeJoin::visitBoundGenericClassType (Type second) {
113
+ CanType TypeJoin::visitBoundGenericClassType (CanType second) {
128
114
return getSuperclassJoin (First, second);
129
115
}
130
116
131
- Type TypeJoin::visitArchetypeType (Type second) {
117
+ CanType TypeJoin::visitArchetypeType (CanType second) {
132
118
return getSuperclassJoin (First, second);
133
119
}
134
120
135
- Type TypeJoin::visitDynamicSelfType (Type second) {
121
+ CanType TypeJoin::visitDynamicSelfType (CanType second) {
136
122
return getSuperclassJoin (First, second);
137
123
}
138
124
139
- Type TypeJoin::visitMetatypeType (Type second) {
125
+ CanType TypeJoin::visitMetatypeType (CanType second) {
140
126
if (First->getKind () != second->getKind ())
141
127
return First->getASTContext ().TheAnyType ;
142
128
143
- auto firstInstance = First->castTo <AnyMetatypeType>()->getInstanceType ();
144
- auto secondInstance = second->castTo <AnyMetatypeType>()->getInstanceType ();
129
+ auto firstInstance =
130
+ First->castTo <AnyMetatypeType>()->getInstanceType ()->getCanonicalType ();
131
+ auto secondInstance =
132
+ second->castTo <AnyMetatypeType>()->getInstanceType ()->getCanonicalType ();
145
133
146
134
auto joinInstance = join (firstInstance, secondInstance);
147
135
148
136
if (!joinInstance)
149
137
return First->getASTContext ().TheAnyType ;
150
138
151
- return MetatypeType::get (joinInstance);
139
+ return MetatypeType::get (joinInstance)-> getCanonicalType () ;
152
140
}
153
141
154
- Type TypeJoin::visitExistentialMetatypeType (Type second) {
142
+ CanType TypeJoin::visitExistentialMetatypeType (CanType second) {
155
143
if (First->getKind () != second->getKind ())
156
144
return First->getASTContext ().TheAnyType ;
157
145
158
- auto firstInstance = First->castTo <AnyMetatypeType>()->getInstanceType ();
159
- auto secondInstance = second->castTo <AnyMetatypeType>()->getInstanceType ();
146
+ auto firstInstance =
147
+ First->castTo <AnyMetatypeType>()->getInstanceType ()->getCanonicalType ();
148
+ auto secondInstance =
149
+ second->castTo <AnyMetatypeType>()->getInstanceType ()->getCanonicalType ();
160
150
161
151
auto joinInstance = join (firstInstance, secondInstance);
162
152
163
153
if (!joinInstance)
164
154
return First->getASTContext ().TheAnyType ;
165
155
166
- return ExistentialMetatypeType::get (joinInstance);
156
+ return ExistentialMetatypeType::get (joinInstance)-> getCanonicalType () ;
167
157
}
168
158
169
- Type TypeJoin::visitBoundGenericEnumType (Type second) {
159
+ CanType TypeJoin::visitBoundGenericEnumType (CanType second) {
170
160
if (First->getKind () != second->getKind ())
171
161
return First->getASTContext ().TheAnyType ;
172
162
173
163
OptionalTypeKind otk1, otk2;
174
- Type objectType1 = First->getAnyOptionalObjectType (otk1);
175
- Type objectType2 = second->getAnyOptionalObjectType (otk2);
164
+ auto firstObject = First->getAnyOptionalObjectType (otk1);
165
+ auto secondObject = second->getAnyOptionalObjectType (otk2);
176
166
if (otk1 == OTK_Optional || otk2 == OTK_Optional) {
167
+ auto canFirst = firstObject->getCanonicalType ();
168
+ auto canSecond = secondObject->getCanonicalType ();
169
+
177
170
// Compute the join of the unwrapped type. If there is none, we're done.
178
- Type unwrappedJoin = join (objectType1 ? objectType1 : First,
179
- objectType2 ? objectType2 : second);
171
+ auto unwrappedJoin =
172
+ join (canFirst ? canFirst : First, canSecond ? canSecond : second);
180
173
// FIXME: More general joins of enums need to be handled.
181
174
if (!unwrappedJoin)
182
175
return First->getASTContext ().TheAnyType ;
183
176
184
- return OptionalType::get (unwrappedJoin);
177
+ return OptionalType::get (unwrappedJoin)-> getCanonicalType () ;
185
178
}
186
179
187
180
// FIXME: More general joins of enums need to be handled.
188
181
return First->getASTContext ().TheAnyType ;
189
182
}
190
183
191
- Type TypeJoin::visitOptionalType (Type second) {
192
- auto canFirst = First->getCanonicalType ();
193
- auto canSecond = second->getCanonicalType ();
184
+ Type Type::join (Type first, Type second) {
185
+ assert (first && second && " Unexpected null type!" );
194
186
195
- return TypeJoin::join (canFirst, canSecond);
196
- }
187
+ if (!first || !second) {
188
+ if (first)
189
+ return Type (ErrorType::get (first->getASTContext ()));
197
190
198
- Type Type::join (Type type1, Type type2) {
199
- assert (type1 && type2 && " Unexpected null type!" );
191
+ if (second)
192
+ return Type (ErrorType::get (second->getASTContext ()));
193
+
194
+ return Type ();
195
+ }
200
196
201
- return TypeJoin::join (type1, type2 );
197
+ return TypeJoin::join (first-> getCanonicalType (), second-> getCanonicalType () );
202
198
}
0 commit comments