Skip to content

Commit 2e558f8

Browse files
authored
Merge pull request #27594 from slavapestov/circular-validation-cleanups-6
Circular validation cleanups, part 6
2 parents deb32b2 + 90fa96d commit 2e558f8

23 files changed

+221
-334
lines changed

include/swift/AST/Decl.h

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2978,9 +2978,6 @@ class TypeAliasDecl : public GenericTypeDecl {
29782978
/// Retrieve a sugared interface type containing the structure of the interface
29792979
/// type before any semantic validation has occured.
29802980
Type getStructuralType() const;
2981-
2982-
/// Set the interface type of this typealias declaration from the underlying type.
2983-
void computeType();
29842981

29852982
bool isCompatibilityAlias() const {
29862983
return Bits.TypeAliasDecl.IsCompatibilityAlias;
@@ -3169,10 +3166,6 @@ class AssociatedTypeDecl : public AbstractTypeParamDecl {
31693166
TrailingWhere = trailingWhereClause;
31703167
}
31713168

3172-
/// Set the interface type of this associated type declaration to a dependent
3173-
/// member type of 'Self'.
3174-
void computeType();
3175-
31763169
/// Retrieve the associated type "anchor", which is the associated type
31773170
/// declaration that will be used to describe this associated type in the
31783171
/// ABI.
@@ -3362,10 +3355,6 @@ class NominalTypeDecl : public GenericTypeDecl, public IterableDeclContext {
33623355
Bits.NominalTypeDecl.AddedImplicitInitializers = true;
33633356
}
33643357

3365-
/// Set the interface type of this nominal type to the metatype of the
3366-
/// declared interface type.
3367-
void computeType();
3368-
33693358
/// getDeclaredType - Retrieve the type declared by this entity, without
33703359
/// any generic parameters bound if this is a generic type.
33713360
Type getDeclaredType() const;
@@ -5484,10 +5473,6 @@ class SubscriptDecl : public GenericContext, public AbstractStorageDecl {
54845473
TypeLoc &getElementTypeLoc() { return ElementTy; }
54855474
const TypeLoc &getElementTypeLoc() const { return ElementTy; }
54865475

5487-
/// Compute the interface type of this subscript from the parameter and
5488-
/// element types.
5489-
void computeType();
5490-
54915476
/// Determine the kind of Objective-C subscripting this declaration
54925477
/// implies.
54935478
ObjCSubscriptKind getObjCSubscriptKind() const;
@@ -6358,10 +6343,6 @@ class EnumElementDecl : public DeclContext, public ValueDecl {
63586343
return hasName() ? getBaseName().getIdentifier().str() : "_";
63596344
}
63606345

6361-
/// Set the interface type of this enum element to the constructor function
6362-
/// type; (Self.Type) -> Self or (Self.Type) -> (Args...) -> Self.
6363-
void computeType();
6364-
63656346
Type getArgumentInterfaceType() const;
63666347

63676348
void setParameterList(ParameterList *params);

include/swift/AST/TypeCheckRequests.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,6 +1362,29 @@ class ParamSpecifierRequest
13621362
void cacheResult(ParamSpecifier value) const;
13631363
};
13641364

1365+
/// Determines the result type of a function or element type of a subscript.
1366+
class ResultTypeRequest
1367+
: public SimpleRequest<ResultTypeRequest,
1368+
Type(ValueDecl *),
1369+
CacheKind::SeparatelyCached> {
1370+
public:
1371+
using SimpleRequest::SimpleRequest;
1372+
1373+
private:
1374+
friend SimpleRequest;
1375+
1376+
TypeLoc &getResultTypeLoc() const;
1377+
1378+
// Evaluation.
1379+
llvm::Expected<Type> evaluate(Evaluator &evaluator, ValueDecl *decl) const;
1380+
1381+
public:
1382+
// Separate caching.
1383+
bool isCached() const { return true; }
1384+
Optional<Type> getCachedResult() const;
1385+
void cacheResult(Type value) const;
1386+
};
1387+
13651388
// Allow AnyValue to compare two Type values, even though Type doesn't
13661389
// support ==.
13671390
template<>

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,5 @@ SWIFT_REQUEST(TypeChecker, NeedsNewVTableEntryRequest,
153153
bool(AbstractFunctionDecl *), SeparatelyCached, NoLocationInfo)
154154
SWIFT_REQUEST(TypeChecker, ParamSpecifierRequest,
155155
ParamDecl::Specifier(ParamDecl *), SeparatelyCached, NoLocationInfo)
156+
SWIFT_REQUEST(TypeChecker, ResultTypeRequest,
157+
Type(ValueDecl *), SeparatelyCached, NoLocationInfo)

include/swift/Sema/IDETypeChecking.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,31 @@
2020
#define SWIFT_SEMA_IDETYPECHECKING_H
2121

2222
#include "llvm/ADT/MapVector.h"
23+
#include "swift/AST/Identifier.h"
2324
#include "swift/Basic/SourceLoc.h"
2425
#include <memory>
2526

2627
namespace swift {
2728
class AbstractFunctionDecl;
29+
class ASTContext;
30+
class ConcreteDeclRef;
2831
class Decl;
32+
class DeclContext;
33+
class DeclName;
34+
enum class DeclRefKind;
2935
class Expr;
3036
class ExtensionDecl;
37+
class FunctionType;
38+
class NominalTypeDecl;
39+
class PatternBindingDecl;
3140
class ProtocolDecl;
41+
class SourceFile;
42+
class SubscriptDecl;
43+
class TopLevelCodeDecl;
3244
class Type;
3345
class TypeChecker;
34-
class DeclContext;
35-
class ConcreteDeclRef;
3646
class ValueDecl;
37-
class DeclName;
47+
struct PrintOptions;
3848

3949
/// Typecheck binding initializer at \p bindingIndex.
4050
void typeCheckPatternBinding(PatternBindingDecl *PBD, unsigned bindingIndex);
@@ -127,12 +137,6 @@ namespace swift {
127137
/// \returns true on success, false on error.
128138
bool typeCheckTopLevelCodeDecl(TopLevelCodeDecl *TLCD);
129139

130-
/// Creates a type checker instance on the given AST context, if it
131-
/// doesn't already have one.
132-
///
133-
/// \returns a reference to the type checker instance.
134-
TypeChecker &createTypeChecker(ASTContext &Ctx);
135-
136140
struct ExtensionInfo {
137141
// The extension with the declarations to apply.
138142
ExtensionDecl *Ext;

include/swift/Subsystems.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ namespace swift {
6565
class SyntaxParsingCache;
6666
class Token;
6767
class TopLevelContext;
68+
class TypeChecker;
6869
struct TypeLoc;
6970
class UnifiedStatsReporter;
7071
enum class SourceFileKind;
@@ -194,6 +195,12 @@ namespace swift {
194195
SkipNonInlinableFunctionBodies = 1 << 4,
195196
};
196197

198+
/// Creates a type checker instance on the given AST context, if it
199+
/// doesn't already have one.
200+
///
201+
/// \returns a reference to the type checker instance.
202+
TypeChecker &createTypeChecker(ASTContext &Ctx);
203+
197204
/// Once parsing and name-binding are complete, this walks the AST to resolve
198205
/// types and diagnose problems therein.
199206
///

lib/AST/Decl.cpp

Lines changed: 29 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -3368,16 +3368,6 @@ bool NominalTypeDecl::isResilient(ModuleDecl *M,
33683368
llvm_unreachable("bad resilience expansion");
33693369
}
33703370

3371-
void NominalTypeDecl::computeType() {
3372-
assert(!hasInterfaceType());
3373-
3374-
Type declaredInterfaceTy = getDeclaredInterfaceType();
3375-
setInterfaceType(MetatypeType::get(declaredInterfaceTy, getASTContext()));
3376-
3377-
if (declaredInterfaceTy->hasError())
3378-
setInvalid();
3379-
}
3380-
33813371
enum class DeclTypeKind : unsigned {
33823372
DeclaredType,
33833373
DeclaredInterfaceType
@@ -3556,25 +3546,6 @@ SourceRange TypeAliasDecl::getSourceRange() const {
35563546
return { TypeAliasLoc, getNameLoc() };
35573547
}
35583548

3559-
void TypeAliasDecl::computeType() {
3560-
assert(!hasInterfaceType());
3561-
3562-
// Set the interface type of this declaration.
3563-
ASTContext &ctx = getASTContext();
3564-
3565-
auto genericSig = getGenericSignature();
3566-
SubstitutionMap subs;
3567-
if (genericSig)
3568-
subs = genericSig->getIdentitySubstitutionMap();
3569-
3570-
Type parent;
3571-
auto parentDC = getDeclContext();
3572-
if (parentDC->isTypeContext())
3573-
parent = parentDC->getSelfInterfaceType();
3574-
auto sugaredType = TypeAliasType::get(this, parent, subs, getUnderlyingType());
3575-
setInterfaceType(MetatypeType::get(sugaredType, ctx));
3576-
}
3577-
35783549
Type TypeAliasDecl::getUnderlyingType() const {
35793550
auto &ctx = getASTContext();
35803551
return evaluateOrDefault(ctx.evaluator,
@@ -3684,14 +3655,6 @@ AssociatedTypeDecl::AssociatedTypeDecl(DeclContext *dc, SourceLoc keywordLoc,
36843655
assert(Resolver && "missing resolver");
36853656
}
36863657

3687-
void AssociatedTypeDecl::computeType() {
3688-
assert(!hasInterfaceType());
3689-
3690-
auto &ctx = getASTContext();
3691-
auto interfaceTy = getDeclaredInterfaceType();
3692-
setInterfaceType(MetatypeType::get(interfaceTy, ctx));
3693-
}
3694-
36953658
Type AssociatedTypeDecl::getDefaultDefinitionType() const {
36963659
return evaluateOrDefault(getASTContext().evaluator,
36973660
DefaultDefinitionTypeRequest{const_cast<AssociatedTypeDecl *>(this)},
@@ -3876,9 +3839,6 @@ GetDestructorRequest::evaluate(Evaluator &evaluator, ClassDecl *CD) const {
38763839
// Propagate access control and versioned-ness.
38773840
DD->copyFormalAccessFrom(CD, /*sourceIsParentContext*/true);
38783841

3879-
// Wire up generic environment of DD.
3880-
DD->setGenericSignature(CD->getGenericSignatureOfContext());
3881-
38823842
// Mark DD as ObjC, as all dtors are.
38833843
DD->setIsObjC(ctx.LangOpts.EnableObjCInterop);
38843844
if (ctx.LangOpts.EnableObjCInterop)
@@ -6168,30 +6128,11 @@ void SubscriptDecl::setIndices(ParameterList *p) {
61686128
}
61696129

61706130
Type SubscriptDecl::getElementInterfaceType() const {
6171-
auto elementTy = getInterfaceType();
6172-
if (elementTy->is<ErrorType>())
6173-
return elementTy;
6174-
return elementTy->castTo<AnyFunctionType>()->getResult();
6175-
}
6176-
6177-
void SubscriptDecl::computeType() {
6178-
auto elementTy = getElementTypeLoc().getType();
6179-
6180-
SmallVector<AnyFunctionType::Param, 2> argTy;
6181-
getIndices()->getParams(argTy);
6182-
6183-
Type funcTy;
6184-
if (auto sig = getGenericSignature())
6185-
funcTy = GenericFunctionType::get(sig, argTy, elementTy);
6186-
else
6187-
funcTy = FunctionType::get(argTy, elementTy);
6188-
6189-
// Record the interface type.
6190-
setInterfaceType(funcTy);
6191-
6192-
// Make sure that there are no unresolved dependent types in the
6193-
// generic signature.
6194-
assert(!funcTy->findUnresolvedDependentMemberType());
6131+
auto &ctx = getASTContext();
6132+
auto mutableThis = const_cast<SubscriptDecl *>(this);
6133+
return evaluateOrDefault(ctx.evaluator,
6134+
ResultTypeRequest{mutableThis},
6135+
ErrorType::get(ctx));
61956136
}
61966137

61976138
ObjCSubscriptKind SubscriptDecl::getObjCSubscriptKind() const {
@@ -6325,7 +6266,9 @@ BraceStmt *AbstractFunctionDecl::getBody(bool canSynthesize) const {
63256266
}
63266267

63276268
auto mutableThis = const_cast<AbstractFunctionDecl *>(this);
6328-
return evaluateOrDefault(ctx.evaluator, ParseAbstractFunctionBodyRequest{mutableThis}, nullptr);
6269+
return evaluateOrDefault(ctx.evaluator,
6270+
ParseAbstractFunctionBodyRequest{mutableThis},
6271+
nullptr);
63296272
}
63306273

63316274
SourceRange AbstractFunctionDecl::getBodySourceRange() const {
@@ -6606,34 +6549,18 @@ Identifier OpaqueTypeDecl::getOpaqueReturnTypeIdentifier() const {
66066549
}
66076550

66086551
void AbstractFunctionDecl::computeType(AnyFunctionType::ExtInfo info) {
6609-
auto &ctx = getASTContext();
66106552
auto sig = getGenericSignature();
66116553
bool hasSelf = hasImplicitSelfDecl();
66126554

66136555
// Result
66146556
Type resultTy;
66156557
if (auto fn = dyn_cast<FuncDecl>(this)) {
6616-
resultTy = fn->getBodyResultTypeLoc().getType();
6617-
if (!resultTy) {
6618-
resultTy = TupleType::getEmpty(ctx);
6619-
}
6620-
6558+
resultTy = fn->getResultInterfaceType();
66216559
} else if (auto ctor = dyn_cast<ConstructorDecl>(this)) {
6622-
auto *dc = ctor->getDeclContext();
6623-
6624-
if (hasSelf) {
6625-
if (!dc->isTypeContext())
6626-
resultTy = ErrorType::get(ctx);
6627-
else
6628-
resultTy = dc->getSelfInterfaceType();
6629-
}
6630-
6631-
// Adjust result type for failability.
6632-
if (ctor->isFailable())
6633-
resultTy = OptionalType::get(resultTy);
6560+
resultTy = ctor->getResultInterfaceType();
66346561
} else {
66356562
assert(isa<DestructorDecl>(this));
6636-
resultTy = TupleType::getEmpty(ctx);
6563+
resultTy = TupleType::getEmpty(getASTContext());
66376564
}
66386565

66396566
// (Args...) -> Result
@@ -6883,14 +6810,11 @@ StaticSpellingKind FuncDecl::getCorrectStaticSpelling() const {
68836810
}
68846811

68856812
Type FuncDecl::getResultInterfaceType() const {
6886-
Type resultTy = getInterfaceType();
6887-
if (resultTy.isNull() || resultTy->is<ErrorType>())
6888-
return resultTy;
6889-
6890-
if (hasImplicitSelfDecl())
6891-
resultTy = resultTy->castTo<AnyFunctionType>()->getResult();
6892-
6893-
return resultTy->castTo<AnyFunctionType>()->getResult();
6813+
auto &ctx = getASTContext();
6814+
auto mutableThis = const_cast<FuncDecl *>(this);
6815+
return evaluateOrDefault(ctx.evaluator,
6816+
ResultTypeRequest{mutableThis},
6817+
ErrorType::get(ctx));
68946818
}
68956819

68966820
bool FuncDecl::isUnaryOperator() const {
@@ -7033,34 +6957,6 @@ SourceRange EnumElementDecl::getSourceRange() const {
70336957
return {getStartLoc(), getNameLoc()};
70346958
}
70356959

7036-
void EnumElementDecl::computeType() {
7037-
assert(!hasInterfaceType());
7038-
7039-
auto &ctx = getASTContext();
7040-
auto *ED = getParentEnum();
7041-
7042-
// The type of the enum element is either (Self.Type) -> Self
7043-
// or (Self.Type) -> (Args...) -> Self.
7044-
auto resultTy = ED->getDeclaredInterfaceType();
7045-
7046-
AnyFunctionType::Param selfTy(MetatypeType::get(resultTy, ctx));
7047-
7048-
if (auto *PL = getParameterList()) {
7049-
SmallVector<AnyFunctionType::Param, 4> argTy;
7050-
PL->getParams(argTy);
7051-
7052-
resultTy = FunctionType::get(argTy, resultTy);
7053-
}
7054-
7055-
if (auto genericSig = ED->getGenericSignature())
7056-
resultTy = GenericFunctionType::get(genericSig, {selfTy}, resultTy);
7057-
else
7058-
resultTy = FunctionType::get({selfTy}, resultTy);
7059-
7060-
// Record the interface type.
7061-
setInterfaceType(resultTy);
7062-
}
7063-
70646960
Type EnumElementDecl::getArgumentInterfaceType() const {
70656961
if (!hasAssociatedValues())
70666962
return nullptr;
@@ -7143,10 +7039,19 @@ SourceRange ConstructorDecl::getSourceRange() const {
71437039
}
71447040

71457041
Type ConstructorDecl::getResultInterfaceType() const {
7146-
Type ArgTy = getInterfaceType();
7147-
ArgTy = ArgTy->castTo<AnyFunctionType>()->getResult();
7148-
ArgTy = ArgTy->castTo<AnyFunctionType>()->getResult();
7149-
return ArgTy;
7042+
Type resultTy;
7043+
7044+
auto *dc = getDeclContext();
7045+
if (!dc->isTypeContext())
7046+
resultTy = ErrorType::get(getASTContext());
7047+
else
7048+
resultTy = dc->getSelfInterfaceType();
7049+
7050+
// Adjust result type for failability.
7051+
if (isFailable())
7052+
return OptionalType::get(resultTy);
7053+
7054+
return resultTy;
71507055
}
71517056

71527057
Type ConstructorDecl::getInitializerInterfaceType() {

lib/AST/Module.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ void BuiltinUnit::LookupCache::lookupValue(
103103
const_cast<BuiltinUnit*>(&M));
104104
TAD->setUnderlyingType(Ty);
105105
TAD->setAccess(AccessLevel::Public);
106-
TAD->computeType();
107106
Entry = TAD;
108107
}
109108
}

0 commit comments

Comments
 (0)