Skip to content

Commit 301fa01

Browse files
authored
Merge pull request swiftlang#34258 from CodaFi/innumerable-enumerables
Some Light Refactoring of EnumRawTypeRequest
2 parents cf34fa5 + 59b00d1 commit 301fa01

11 files changed

+67
-106
lines changed

include/swift/AST/Decl.h

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,7 +1193,7 @@ class ExtensionDecl final : public GenericContext, public Decl,
11931193
/// extended nominal.
11941194
llvm::PointerIntPair<NominalTypeDecl *, 1, bool> ExtendedNominal;
11951195

1196-
MutableArrayRef<TypeLoc> Inherited;
1196+
ArrayRef<TypeLoc> Inherited;
11971197

11981198
/// The next extension in the linked list of extensions.
11991199
///
@@ -1212,7 +1212,7 @@ class ExtensionDecl final : public GenericContext, public Decl,
12121212
friend class IterableDeclContext;
12131213

12141214
ExtensionDecl(SourceLoc extensionLoc, TypeRepr *extendedType,
1215-
MutableArrayRef<TypeLoc> inherited,
1215+
ArrayRef<TypeLoc> inherited,
12161216
DeclContext *parent,
12171217
TrailingWhereClause *trailingWhereClause);
12181218

@@ -1237,7 +1237,7 @@ class ExtensionDecl final : public GenericContext, public Decl,
12371237
/// Create a new extension declaration.
12381238
static ExtensionDecl *create(ASTContext &ctx, SourceLoc extensionLoc,
12391239
TypeRepr *extendedType,
1240-
MutableArrayRef<TypeLoc> inherited,
1240+
ArrayRef<TypeLoc> inherited,
12411241
DeclContext *parent,
12421242
TrailingWhereClause *trailingWhereClause,
12431243
ClangNode clangNode = ClangNode());
@@ -1289,10 +1289,9 @@ class ExtensionDecl final : public GenericContext, public Decl,
12891289

12901290
/// Retrieve the set of protocols that this type inherits (i.e,
12911291
/// explicitly conforms to).
1292-
MutableArrayRef<TypeLoc> getInherited() { return Inherited; }
12931292
ArrayRef<TypeLoc> getInherited() const { return Inherited; }
12941293

1295-
void setInherited(MutableArrayRef<TypeLoc> i) { Inherited = i; }
1294+
void setInherited(ArrayRef<TypeLoc> i) { Inherited = i; }
12961295

12971296
bool hasDefaultAccessLevel() const {
12981297
return Bits.ExtensionDecl.DefaultAndMaxAccessLevel != 0;
@@ -2405,12 +2404,12 @@ class ValueDecl : public Decl {
24052404

24062405
/// This is a common base class for declarations which declare a type.
24072406
class TypeDecl : public ValueDecl {
2408-
MutableArrayRef<TypeLoc> Inherited;
2407+
ArrayRef<TypeLoc> Inherited;
24092408

24102409
protected:
24112410
TypeDecl(DeclKind K, llvm::PointerUnion<DeclContext *, ASTContext *> context,
24122411
Identifier name, SourceLoc NameLoc,
2413-
MutableArrayRef<TypeLoc> inherited) :
2412+
ArrayRef<TypeLoc> inherited) :
24142413
ValueDecl(K, context, name, NameLoc), Inherited(inherited) {}
24152414

24162415
public:
@@ -2428,10 +2427,9 @@ class TypeDecl : public ValueDecl {
24282427

24292428
/// Retrieve the set of protocols that this type inherits (i.e,
24302429
/// explicitly conforms to).
2431-
MutableArrayRef<TypeLoc> getInherited() { return Inherited; }
24322430
ArrayRef<TypeLoc> getInherited() const { return Inherited; }
24332431

2434-
void setInherited(MutableArrayRef<TypeLoc> i) { Inherited = i; }
2432+
void setInherited(ArrayRef<TypeLoc> i) { Inherited = i; }
24352433

24362434
static bool classof(const Decl *D) {
24372435
return D->getKind() >= DeclKind::First_TypeDecl &&
@@ -2456,7 +2454,7 @@ class GenericTypeDecl : public GenericContext, public TypeDecl {
24562454
public:
24572455
GenericTypeDecl(DeclKind K, DeclContext *DC,
24582456
Identifier name, SourceLoc nameLoc,
2459-
MutableArrayRef<TypeLoc> inherited,
2457+
ArrayRef<TypeLoc> inherited,
24602458
GenericParamList *GenericParams);
24612459

24622460
// Resolve ambiguity due to multiple base classes.
@@ -2982,7 +2980,7 @@ class NominalTypeDecl : public GenericTypeDecl, public IterableDeclContext {
29822980

29832981
NominalTypeDecl(DeclKind K, DeclContext *DC, Identifier name,
29842982
SourceLoc NameLoc,
2985-
MutableArrayRef<TypeLoc> inherited,
2983+
ArrayRef<TypeLoc> inherited,
29862984
GenericParamList *GenericParams) :
29872985
GenericTypeDecl(K, DC, name, NameLoc, inherited, GenericParams),
29882986
IterableDeclContext(IterableDeclContextKind::NominalTypeDecl)
@@ -3224,34 +3222,14 @@ class EnumDecl final : public NominalTypeDecl {
32243222
// Is the complete set of raw values type checked?
32253223
HasFixedRawValuesAndTypes = 1 << 2,
32263224
};
3227-
3228-
struct {
3229-
/// The raw type and a bit to indicate whether the
3230-
/// raw was computed yet or not.
3231-
llvm::PointerIntPair<Type, 3, OptionSet<SemanticInfoFlags>> RawTypeAndFlags;
3232-
3233-
bool hasRawType() const {
3234-
return RawTypeAndFlags.getInt().contains(HasComputedRawType);
3235-
}
3236-
void cacheRawType(Type ty) {
3237-
auto flags = RawTypeAndFlags.getInt() | HasComputedRawType;
3238-
RawTypeAndFlags.setPointerAndInt(ty, flags);
3239-
}
3240-
3241-
bool hasFixedRawValues() const {
3242-
return RawTypeAndFlags.getInt().contains(HasFixedRawValues);
3243-
}
3244-
bool hasCheckedRawValues() const {
3245-
return RawTypeAndFlags.getInt().contains(HasFixedRawValuesAndTypes);
3246-
}
3247-
} LazySemanticInfo;
3225+
OptionSet<SemanticInfoFlags> SemanticFlags;
32483226

32493227
friend class EnumRawValuesRequest;
32503228
friend class EnumRawTypeRequest;
32513229

32523230
public:
32533231
EnumDecl(SourceLoc EnumLoc, Identifier Name, SourceLoc NameLoc,
3254-
MutableArrayRef<TypeLoc> Inherited,
3232+
ArrayRef<TypeLoc> Inherited,
32553233
GenericParamList *GenericParams, DeclContext *DC);
32563234

32573235
SourceLoc getStartLoc() const { return EnumLoc; }
@@ -3326,11 +3304,7 @@ class EnumDecl final : public NominalTypeDecl {
33263304
Type getRawType() const;
33273305

33283306
/// Set the raw type of the enum from its inheritance clause.
3329-
void setRawType(Type rawType) {
3330-
auto flags = LazySemanticInfo.RawTypeAndFlags.getInt();
3331-
LazySemanticInfo.RawTypeAndFlags.setPointerAndInt(
3332-
rawType, flags | HasComputedRawType);
3333-
}
3307+
void setRawType(Type rawType);
33343308

33353309
/// True if none of the enum cases have associated values.
33363310
///
@@ -3387,7 +3361,7 @@ class StructDecl final : public NominalTypeDecl {
33873361

33883362
public:
33893363
StructDecl(SourceLoc StructLoc, Identifier Name, SourceLoc NameLoc,
3390-
MutableArrayRef<TypeLoc> Inherited,
3364+
ArrayRef<TypeLoc> Inherited,
33913365
GenericParamList *GenericParams, DeclContext *DC);
33923366

33933367
SourceLoc getStartLoc() const { return StructLoc; }
@@ -3526,7 +3500,7 @@ class ClassDecl final : public NominalTypeDecl {
35263500

35273501
public:
35283502
ClassDecl(SourceLoc ClassLoc, Identifier Name, SourceLoc NameLoc,
3529-
MutableArrayRef<TypeLoc> Inherited,
3503+
ArrayRef<TypeLoc> Inherited,
35303504
GenericParamList *GenericParams, DeclContext *DC);
35313505

35323506
SourceLoc getStartLoc() const { return ClassLoc; }
@@ -3907,7 +3881,7 @@ class ProtocolDecl final : public NominalTypeDecl {
39073881

39083882
public:
39093883
ProtocolDecl(DeclContext *DC, SourceLoc ProtocolLoc, SourceLoc NameLoc,
3910-
Identifier Name, MutableArrayRef<TypeLoc> Inherited,
3884+
Identifier Name, ArrayRef<TypeLoc> Inherited,
39113885
TrailingWhereClause *TrailingWhere);
39123886

39133887
using Decl::getASTContext;

include/swift/AST/TypeCheckRequests.h

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,29 +126,24 @@ class SuperclassTypeRequest
126126
};
127127

128128
/// Request the raw type of the given enum.
129-
class EnumRawTypeRequest :
130-
public SimpleRequest<EnumRawTypeRequest,
131-
Type(EnumDecl *, TypeResolutionStage),
132-
RequestFlags::SeparatelyCached> {
129+
class EnumRawTypeRequest
130+
: public SimpleRequest<EnumRawTypeRequest, Type(EnumDecl *),
131+
RequestFlags::Cached> {
133132
public:
134133
using SimpleRequest::SimpleRequest;
135134

136135
private:
137136
friend SimpleRequest;
138137

139138
// Evaluation.
140-
Type
141-
evaluate(Evaluator &evaluator, EnumDecl *enumDecl,
142-
TypeResolutionStage stage) const;
139+
Type evaluate(Evaluator &evaluator, EnumDecl *enumDecl) const;
143140

144141
public:
145142
// Cycle handling
146143
void diagnoseCycle(DiagnosticEngine &diags) const;
144+
void noteCycleStep(DiagnosticEngine &diags) const;
147145

148-
// Separate caching.
149-
bool isCached() const;
150-
Optional<Type> getCachedResult() const;
151-
void cacheResult(Type value) const;
146+
bool isCached() const { return true; }
152147
};
153148

154149
/// Request to determine the set of declarations that were are overridden

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ SWIFT_REQUEST(TypeChecker, EnumRawValuesRequest,
7171
evaluator::SideEffect (EnumDecl *, TypeResolutionStage),
7272
SeparatelyCached, NoLocationInfo)
7373
SWIFT_REQUEST(TypeChecker, EnumRawTypeRequest,
74-
Type(EnumDecl *, TypeResolutionStage), SeparatelyCached,
75-
NoLocationInfo)
74+
Type(EnumDecl *), Cached, NoLocationInfo)
7675
SWIFT_REQUEST(TypeChecker, ExistentialConformsToSelfRequest,
7776
bool(ProtocolDecl *), SeparatelyCached, NoLocationInfo)
7877
SWIFT_REQUEST(TypeChecker, ExistentialTypeSupportedRequest,

lib/AST/Decl.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,7 +1106,7 @@ NominalTypeDecl::takeConformanceLoaderSlow() {
11061106

11071107
ExtensionDecl::ExtensionDecl(SourceLoc extensionLoc,
11081108
TypeRepr *extendedType,
1109-
MutableArrayRef<TypeLoc> inherited,
1109+
ArrayRef<TypeLoc> inherited,
11101110
DeclContext *parent,
11111111
TrailingWhereClause *trailingWhereClause)
11121112
: GenericContext(DeclContextKind::ExtensionDecl, parent, nullptr),
@@ -1123,7 +1123,7 @@ ExtensionDecl::ExtensionDecl(SourceLoc extensionLoc,
11231123

11241124
ExtensionDecl *ExtensionDecl::create(ASTContext &ctx, SourceLoc extensionLoc,
11251125
TypeRepr *extendedType,
1126-
MutableArrayRef<TypeLoc> inherited,
1126+
ArrayRef<TypeLoc> inherited,
11271127
DeclContext *parent,
11281128
TrailingWhereClause *trailingWhereClause,
11291129
ClangNode clangNode) {
@@ -3704,7 +3704,7 @@ PropertyWrapperTypeInfo NominalTypeDecl::getPropertyWrapperTypeInfo() const {
37043704

37053705
GenericTypeDecl::GenericTypeDecl(DeclKind K, DeclContext *DC,
37063706
Identifier name, SourceLoc nameLoc,
3707-
MutableArrayRef<TypeLoc> inherited,
3707+
ArrayRef<TypeLoc> inherited,
37083708
GenericParamList *GenericParams) :
37093709
GenericContext(DeclContextKind::GenericTypeDecl, DC, GenericParams),
37103710
TypeDecl(K, DC, name, nameLoc, inherited) {}
@@ -3914,7 +3914,7 @@ AssociatedTypeDecl *AssociatedTypeDecl::getAssociatedTypeAnchor() const {
39143914

39153915
EnumDecl::EnumDecl(SourceLoc EnumLoc,
39163916
Identifier Name, SourceLoc NameLoc,
3917-
MutableArrayRef<TypeLoc> Inherited,
3917+
ArrayRef<TypeLoc> Inherited,
39183918
GenericParamList *GenericParams, DeclContext *Parent)
39193919
: NominalTypeDecl(DeclKind::Enum, Parent, Name, NameLoc, Inherited,
39203920
GenericParams),
@@ -3928,13 +3928,17 @@ EnumDecl::EnumDecl(SourceLoc EnumLoc,
39283928

39293929
Type EnumDecl::getRawType() const {
39303930
ASTContext &ctx = getASTContext();
3931-
return evaluateOrDefault(ctx.evaluator,
3932-
EnumRawTypeRequest{const_cast<EnumDecl *>(this),
3933-
TypeResolutionStage::Interface}, Type());
3931+
return evaluateOrDefault(
3932+
ctx.evaluator, EnumRawTypeRequest{const_cast<EnumDecl *>(this)}, Type());
3933+
}
3934+
3935+
void EnumDecl::setRawType(Type rawType) {
3936+
getASTContext().evaluator.cacheOutput(EnumRawTypeRequest{this},
3937+
std::move(rawType));
39343938
}
39353939

39363940
StructDecl::StructDecl(SourceLoc StructLoc, Identifier Name, SourceLoc NameLoc,
3937-
MutableArrayRef<TypeLoc> Inherited,
3941+
ArrayRef<TypeLoc> Inherited,
39383942
GenericParamList *GenericParams, DeclContext *Parent)
39393943
: NominalTypeDecl(DeclKind::Struct, Parent, Name, NameLoc, Inherited,
39403944
GenericParams),
@@ -4045,7 +4049,7 @@ void NominalTypeDecl::synthesizeSemanticMembersIfNeeded(DeclName member) {
40454049
}
40464050

40474051
ClassDecl::ClassDecl(SourceLoc ClassLoc, Identifier Name, SourceLoc NameLoc,
4048-
MutableArrayRef<TypeLoc> Inherited,
4052+
ArrayRef<TypeLoc> Inherited,
40494053
GenericParamList *GenericParams, DeclContext *Parent)
40504054
: NominalTypeDecl(DeclKind::Class, Parent, Name, NameLoc, Inherited,
40514055
GenericParams),
@@ -4509,9 +4513,7 @@ bool EnumDecl::isEffectivelyExhaustive(ModuleDecl *M,
45094513
}
45104514

45114515
void EnumDecl::setHasFixedRawValues() {
4512-
auto flags = LazySemanticInfo.RawTypeAndFlags.getInt() |
4513-
EnumDecl::HasFixedRawValues;
4514-
LazySemanticInfo.RawTypeAndFlags.setInt(flags);
4516+
SemanticFlags |= OptionSet<EnumDecl::SemanticInfoFlags>{EnumDecl::HasFixedRawValues};
45154517
}
45164518

45174519
bool EnumDecl::hasCircularRawValue() const {
@@ -4523,7 +4525,7 @@ bool EnumDecl::hasCircularRawValue() const {
45234525

45244526
ProtocolDecl::ProtocolDecl(DeclContext *DC, SourceLoc ProtocolLoc,
45254527
SourceLoc NameLoc, Identifier Name,
4526-
MutableArrayRef<TypeLoc> Inherited,
4528+
ArrayRef<TypeLoc> Inherited,
45274529
TrailingWhereClause *TrailingWhere)
45284530
: NominalTypeDecl(DeclKind::Protocol, DC, Name, NameLoc, Inherited,
45294531
nullptr),

lib/AST/TypeCheckRequests.cpp

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -179,21 +179,10 @@ void EnumRawTypeRequest::diagnoseCycle(DiagnosticEngine &diags) const {
179179
diags.diagnose(enumDecl, diag::circular_enum_inheritance, enumDecl->getName());
180180
}
181181

182-
bool EnumRawTypeRequest::isCached() const {
183-
return std::get<1>(getStorage()) == TypeResolutionStage::Interface;
184-
}
185-
186-
Optional<Type> EnumRawTypeRequest::getCachedResult() const {
187-
auto enumDecl = std::get<0>(getStorage());
188-
if (enumDecl->LazySemanticInfo.hasRawType())
189-
return enumDecl->LazySemanticInfo.RawTypeAndFlags.getPointer();
190-
191-
return None;
192-
}
193-
194-
void EnumRawTypeRequest::cacheResult(Type value) const {
195-
auto enumDecl = std::get<0>(getStorage());
196-
enumDecl->LazySemanticInfo.cacheRawType(value);
182+
void EnumRawTypeRequest::noteCycleStep(DiagnosticEngine &diags) const {
183+
auto *decl = std::get<0>(getStorage());
184+
diags.diagnose(decl, diag::kind_declname_declared_here,
185+
decl->getDescriptiveKind(), decl->getName());
197186
}
198187

199188
//----------------------------------------------------------------------------//
@@ -854,17 +843,15 @@ bool EnumRawValuesRequest::isCached() const {
854843

855844
Optional<evaluator::SideEffect> EnumRawValuesRequest::getCachedResult() const {
856845
auto *ED = std::get<0>(getStorage());
857-
if (ED->LazySemanticInfo.hasCheckedRawValues())
846+
if (ED->SemanticFlags.contains(EnumDecl::HasFixedRawValuesAndTypes))
858847
return std::make_tuple<>();
859848
return None;
860849
}
861850

862851
void EnumRawValuesRequest::cacheResult(evaluator::SideEffect) const {
863852
auto *ED = std::get<0>(getStorage());
864-
auto flags = ED->LazySemanticInfo.RawTypeAndFlags.getInt() |
865-
EnumDecl::HasFixedRawValues |
866-
EnumDecl::HasFixedRawValuesAndTypes;
867-
ED->LazySemanticInfo.RawTypeAndFlags.setInt(flags);
853+
ED->SemanticFlags |= OptionSet<EnumDecl::SemanticInfoFlags>{
854+
EnumDecl::HasFixedRawValues | EnumDecl::HasFixedRawValuesAndTypes};
868855
}
869856

870857
void EnumRawValuesRequest::diagnoseCycle(DiagnosticEngine &diags) const {

lib/IRGen/GenClass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2340,7 +2340,7 @@ ClassDecl *IRGenModule::getObjCRuntimeBaseClass(Identifier name,
23402340

23412341
// Make a really fake-looking class.
23422342
auto SwiftRootClass = new (Context) ClassDecl(SourceLoc(), name, SourceLoc(),
2343-
MutableArrayRef<TypeLoc>(),
2343+
ArrayRef<TypeLoc>(),
23442344
/*generics*/ nullptr,
23452345
Context.TheBuiltinModule);
23462346
SwiftRootClass->setIsObjC(Context.LangOpts.EnableObjCInterop);

lib/Sema/DerivedConformanceCodable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ static EnumDecl *synthesizeCodingKeysEnum(DerivedConformance &derived) {
234234
auto *codingKeyProto = C.getProtocol(KnownProtocolKind::CodingKey);
235235
auto codingKeyType = codingKeyProto->getDeclaredInterfaceType();
236236
TypeLoc protoTypeLoc[1] = {TypeLoc::withoutLoc(codingKeyType)};
237-
MutableArrayRef<TypeLoc> inherited = C.AllocateCopy(protoTypeLoc);
237+
ArrayRef<TypeLoc> inherited = C.AllocateCopy(protoTypeLoc);
238238

239239
auto *enumDecl = new (C) EnumDecl(SourceLoc(), C.Id_CodingKeys, SourceLoc(),
240240
inherited, nullptr, target);

0 commit comments

Comments
 (0)