Skip to content

Commit 59b00d1

Browse files
committed
[NFC] Make EnumRawTypeRequest Cached
Drop the extra bit of state in the AST for the semantic type.
1 parent ff8d5bc commit 59b00d1

File tree

8 files changed

+34
-50
lines changed

8 files changed

+34
-50
lines changed

include/swift/AST/Decl.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3304,11 +3304,7 @@ class EnumDecl final : public NominalTypeDecl {
33043304
Type getRawType() const;
33053305

33063306
/// Set the raw type of the enum from its inheritance clause.
3307-
void setRawType(Type rawType) {
3308-
auto flags = LazySemanticInfo.RawTypeAndFlags.getInt();
3309-
LazySemanticInfo.RawTypeAndFlags.setPointerAndInt(
3310-
rawType, flags | HasComputedRawType);
3311-
}
3307+
void setRawType(Type rawType);
33123308

33133309
/// True if none of the enum cases have associated values.
33143310
///

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: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3928,9 +3928,13 @@ 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,

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/Sema/DerivedConformanceRawRepresentable.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "swift/AST/Types.h"
2525
#include "llvm/ADT/APInt.h"
2626
#include "DerivedConformances.h"
27+
#include "TypeCheckDecl.h"
2728

2829
using namespace swift;
2930

@@ -448,7 +449,10 @@ bool DerivedConformance::canDeriveRawRepresentable(DeclContext *DC,
448449
return false;
449450

450451
Type rawType = enumDecl->getRawType();
451-
if (!rawType)
452+
if (!rawType || rawType->hasError())
453+
return false;
454+
455+
if (!computeAutomaticEnumValueKind(enumDecl))
452456
return false;
453457

454458
rawType = DC->mapTypeIntoContext(rawType);

lib/Sema/TypeCheckDecl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,7 +1162,7 @@ EnumRawValuesRequest::evaluate(Evaluator &eval, EnumDecl *ED,
11621162
if (uncheckedRawValueOf(elt)) {
11631163
if (!uncheckedRawValueOf(elt)->isImplicit())
11641164
lastExplicitValueElt = elt;
1165-
} else if (!ED->LazySemanticInfo.hasFixedRawValues()) {
1165+
} else if (!ED->SemanticFlags.contains(EnumDecl::HasFixedRawValues)) {
11661166
// Try to pull out the automatic enum value kind. If that fails, bail.
11671167
if (!valueKind) {
11681168
valueKind = computeAutomaticEnumValueKind(ED);
@@ -1217,7 +1217,7 @@ EnumRawValuesRequest::evaluate(Evaluator &eval, EnumDecl *ED,
12171217
// to have set things up correctly. This comes up with imported enums
12181218
// and deserialized @objc enums which always have their raw values setup
12191219
// beforehand.
1220-
if (ED->LazySemanticInfo.hasFixedRawValues())
1220+
if (ED->SemanticFlags.contains(EnumDecl::HasFixedRawValues))
12211221
continue;
12221222

12231223
// Using magic literals like #file as raw value is not supported right now.

lib/Sema/TypeCheckRequestFunctions.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,12 @@ SuperclassTypeRequest::evaluate(Evaluator &evaluator,
136136
return Type();
137137
}
138138

139-
Type
140-
EnumRawTypeRequest::evaluate(Evaluator &evaluator, EnumDecl *enumDecl,
141-
TypeResolutionStage stage) const {
139+
Type EnumRawTypeRequest::evaluate(Evaluator &evaluator,
140+
EnumDecl *enumDecl) const {
142141
for (unsigned int idx : indices(enumDecl->getInherited())) {
143-
auto inheritedTypeResult =
144-
evaluator(InheritedTypeRequest{enumDecl, idx, stage});
145-
142+
auto inheritedTypeResult = evaluator(
143+
InheritedTypeRequest{enumDecl, idx, TypeResolutionStage::Interface});
144+
146145
if (auto err = inheritedTypeResult.takeError()) {
147146
llvm::handleAllErrors(std::move(err),
148147
[](const CyclicalRequestError<InheritedTypeRequest> &E) {

0 commit comments

Comments
 (0)