Skip to content

Commit 0d90b62

Browse files
committed
AST: Clean up in preparation for non-canonical archetypes
1 parent fe52553 commit 0d90b62

File tree

10 files changed

+101
-92
lines changed

10 files changed

+101
-92
lines changed

include/swift/AST/GenericEnvironment.h

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ struct OpaqueEnvironmentData {
6161
};
6262

6363
/// Extra data in a generic environment for an opened existential.
64-
struct OpenedExistentialEnvironmentData {
64+
struct ExistentialEnvironmentData {
6565
Type existential;
6666
UUID uuid;
6767
};
6868

6969
/// Extra data in a generic environment for an opened pack element.
70-
struct OpenedElementEnvironmentData {
70+
struct ElementEnvironmentData {
7171
UUID uuid;
7272
CanGenericTypeParamType shapeClass;
7373
};
@@ -85,37 +85,38 @@ class alignas(1 << DeclAlignInBits) GenericEnvironment final
8585
GenericEnvironment,
8686
SubstitutionMap,
8787
OpaqueEnvironmentData,
88-
OpenedExistentialEnvironmentData,
89-
OpenedElementEnvironmentData,
88+
ExistentialEnvironmentData,
89+
ElementEnvironmentData,
9090
Type> {
9191
public:
92-
enum class Kind {
92+
enum class Kind: uint8_t {
9393
/// A normal generic environment, determined only by its generic
9494
/// signature.
9595
Primary,
9696
/// A generic environment describing an opaque type archetype.
9797
Opaque,
9898
/// A generic environment describing an opened existential archetype.
99-
OpenedExistential,
99+
Existential,
100100
/// A generic environment describing an opened element type of a
101101
/// pack archetype inside a pack expansion expression.
102-
OpenedElement,
102+
Element,
103103
};
104104

105105
class NestedTypeStorage;
106106

107107
private:
108-
mutable llvm::PointerIntPair<GenericSignature, 2, Kind> SignatureAndKind{
109-
GenericSignature(), Kind::Primary};
108+
GenericSignature sig;
110109
NestedTypeStorage *nestedTypeStorage = nullptr;
110+
Kind kind;
111+
bool canonical;
111112

112113
friend TrailingObjects;
113114
friend OpaqueTypeArchetypeType;
114115

115116
size_t numTrailingObjects(OverloadToken<SubstitutionMap>) const;
116117
size_t numTrailingObjects(OverloadToken<OpaqueEnvironmentData>) const;
117-
size_t numTrailingObjects(OverloadToken<OpenedExistentialEnvironmentData>) const;
118-
size_t numTrailingObjects(OverloadToken<OpenedElementEnvironmentData>) const;
118+
size_t numTrailingObjects(OverloadToken<ExistentialEnvironmentData>) const;
119+
size_t numTrailingObjects(OverloadToken<ElementEnvironmentData>) const;
119120
size_t numTrailingObjects(OverloadToken<Type>) const;
120121

121122
/// Retrieve the array containing the context types associated with the
@@ -168,10 +169,13 @@ class alignas(1 << DeclAlignInBits) GenericEnvironment final
168169

169170
public:
170171
GenericSignature getGenericSignature() const {
171-
return SignatureAndKind.getPointer();
172+
return sig;
172173
}
173174

174-
Kind getKind() const { return SignatureAndKind.getInt(); }
175+
Kind getKind() const { return kind; }
176+
177+
/// Returns if the archetypes from this environment are canonical types.
178+
bool isCanonical() const { return canonical; }
175179

176180
ArrayRef<GenericTypeParamType *> getGenericParams() const;
177181

include/swift/AST/Types.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7009,7 +7009,7 @@ class ArchetypeType : public SubstitutableType,
70097009
}
70107010
protected:
70117011
ArchetypeType(TypeKind Kind,
7012-
const ASTContext &C,
7012+
const ASTContext *C,
70137013
RecursiveTypeProperties properties,
70147014
Type InterfaceType,
70157015
ArrayRef<ProtocolDecl *> ConformsTo,
@@ -7088,7 +7088,8 @@ class OpaqueTypeArchetypeType final : public ArchetypeType,
70887088
}
70897089

70907090
private:
7091-
OpaqueTypeArchetypeType(GenericEnvironment *environment,
7091+
OpaqueTypeArchetypeType(const ASTContext *ctx,
7092+
GenericEnvironment *environment,
70927093
RecursiveTypeProperties properties,
70937094
Type interfaceType,
70947095
ArrayRef<ProtocolDecl*> conformsTo,
@@ -7232,11 +7233,13 @@ class ExistentialArchetypeType final : public LocalArchetypeType,
72327233
}
72337234

72347235
private:
7235-
ExistentialArchetypeType(GenericEnvironment *environment, Type interfaceType,
7236-
ArrayRef<ProtocolDecl *> conformsTo,
7237-
Type superclass,
7238-
LayoutConstraint layout,
7239-
RecursiveTypeProperties properties);
7236+
ExistentialArchetypeType(const ASTContext *ctx,
7237+
GenericEnvironment *environment,
7238+
Type interfaceType,
7239+
ArrayRef<ProtocolDecl *> conformsTo,
7240+
Type superclass,
7241+
LayoutConstraint layout,
7242+
RecursiveTypeProperties properties);
72407243
};
72417244
BEGIN_CAN_TYPE_WRAPPER(ExistentialArchetypeType, LocalArchetypeType)
72427245
END_CAN_TYPE_WRAPPER(ExistentialArchetypeType, LocalArchetypeType)
@@ -7309,7 +7312,7 @@ class ElementArchetypeType final : public LocalArchetypeType,
73097312
}
73107313

73117314
private:
7312-
ElementArchetypeType(const ASTContext &ctx,
7315+
ElementArchetypeType(const ASTContext *ctx,
73137316
GenericEnvironment *environment, Type interfaceType,
73147317
ArrayRef<ProtocolDecl *> conformsTo, Type superclass,
73157318
LayoutConstraint layout);

lib/AST/ASTContext.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5731,7 +5731,8 @@ OpaqueTypeArchetypeType *OpaqueTypeArchetypeType::getNew(
57315731
ASTContext &ctx = interfaceType->getASTContext();
57325732
auto mem = ctx.Allocate(size, alignof(OpaqueTypeArchetypeType), arena);
57335733
return ::new (mem)
5734-
OpaqueTypeArchetypeType(environment, properties, interfaceType,
5734+
OpaqueTypeArchetypeType(environment->isCanonical() ? &ctx : nullptr,
5735+
environment, properties, interfaceType,
57355736
conformsTo, superclass, layout);
57365737
}
57375738

@@ -5759,6 +5760,7 @@ CanTypeWrapper<ExistentialArchetypeType> ExistentialArchetypeType::getNew(
57595760
void *mem = ctx.Allocate(size, alignof(ExistentialArchetypeType), arena);
57605761

57615762
return CanExistentialArchetypeType(::new (mem) ExistentialArchetypeType(
5763+
environment->isCanonical() ? &ctx : nullptr,
57625764
environment, interfaceType, conformsTo, superclass, layout,
57635765
properties));
57645766
}
@@ -6014,8 +6016,8 @@ GenericEnvironment *GenericEnvironment::forPrimary(GenericSignature signature) {
60146016
unsigned numGenericParams = signature.getGenericParams().size();
60156017
size_t bytes = totalSizeToAlloc<SubstitutionMap,
60166018
OpaqueEnvironmentData,
6017-
OpenedExistentialEnvironmentData,
6018-
OpenedElementEnvironmentData, Type>(
6019+
ExistentialEnvironmentData,
6020+
ElementEnvironmentData, Type>(
60196021
0, 0, 0, 0, numGenericParams);
60206022
void *mem = ctx.Allocate(bytes, alignof(GenericEnvironment));
60216023
return new (mem) GenericEnvironment(signature);
@@ -6044,9 +6046,9 @@ GenericEnvironment *GenericEnvironment::forOpaqueType(
60446046
auto signature = opaque->getOpaqueInterfaceGenericSignature();
60456047
unsigned numGenericParams = signature.getGenericParams().size();
60466048
size_t bytes = totalSizeToAlloc<SubstitutionMap,
6047-
OpaqueEnvironmentData,
6048-
OpenedExistentialEnvironmentData,
6049-
OpenedElementEnvironmentData, Type>(
6049+
OpaqueEnvironmentData,
6050+
ExistentialEnvironmentData,
6051+
ElementEnvironmentData, Type>(
60506052
1, 1, 0, 0, numGenericParams);
60516053
void *mem = ctx.Allocate(bytes, alignof(GenericEnvironment), arena);
60526054
env = new (mem) GenericEnvironment(signature, opaque, subs);
@@ -6108,8 +6110,8 @@ GenericEnvironment::forOpenedExistential(
61086110
unsigned numGenericParams = signature.getGenericParams().size();
61096111
size_t bytes = totalSizeToAlloc<SubstitutionMap,
61106112
OpaqueEnvironmentData,
6111-
OpenedExistentialEnvironmentData,
6112-
OpenedElementEnvironmentData, Type>(
6113+
ExistentialEnvironmentData,
6114+
ElementEnvironmentData, Type>(
61136115
1, 0, 1, 0, numGenericParams);
61146116
void *mem = ctx.Allocate(bytes, alignof(GenericEnvironment));
61156117
auto *genericEnv =
@@ -6146,8 +6148,8 @@ GenericEnvironment::forOpenedElement(GenericSignature signature,
61466148
unsigned numOpenedParams = signature.getInnermostGenericParams().size();
61476149
size_t bytes = totalSizeToAlloc<SubstitutionMap,
61486150
OpaqueEnvironmentData,
6149-
OpenedExistentialEnvironmentData,
6150-
OpenedElementEnvironmentData,
6151+
ExistentialEnvironmentData,
6152+
ElementEnvironmentData,
61516153
Type>(
61526154
1, 0, 0, 1, numGenericParams + numOpenedParams);
61536155
void *mem = ctx.Allocate(bytes, alignof(GenericEnvironment));

lib/AST/CaptureInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ CaptureInfo::CaptureInfo(ASTContext &ctx, ArrayRef<CapturedValue> captures,
7777
// This is the only kind of local generic environment we can capture right now.
7878
#ifndef NDEBUG
7979
for (auto *env : genericEnv) {
80-
assert(env->getKind() == GenericEnvironment::Kind::OpenedElement);
80+
assert(env->getKind() == GenericEnvironment::Kind::Element);
8181
}
8282
#endif
8383

0 commit comments

Comments
 (0)