Skip to content

Commit 9caaad4

Browse files
committed
AST: Don't call hasType()/getType()/setType() on TypeDecls
1 parent 07780bd commit 9caaad4

18 files changed

+103
-108
lines changed

lib/AST/ASTDumper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ namespace {
517517
if (!isa<AbstractFunctionDecl>(VD) &&
518518
!isa<EnumElementDecl>(VD) &&
519519
!isa<SubscriptDecl>(VD) &&
520-
!isa<AbstractTypeParamDecl>(VD)) {
520+
!isa<TypeDecl>(VD)) {
521521
OS << " type='";
522522
if (VD->hasType())
523523
VD->getType().print(OS);
@@ -569,7 +569,7 @@ namespace {
569569
llvm::Optional<llvm::raw_ostream::Colors>()) {
570570
printCommon((ValueDecl *)NTD, Name, Color);
571571

572-
if (NTD->hasType()) {
572+
if (NTD->hasInterfaceType()) {
573573
if (NTD->hasFixedLayout())
574574
OS << " @_fixed_layout";
575575
else

lib/AST/Decl.cpp

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,7 +1648,7 @@ bool ValueDecl::hasType() const {
16481648
assert(!isa<AbstractFunctionDecl>(this) &&
16491649
!isa<EnumElementDecl>(this) &&
16501650
!isa<SubscriptDecl>(this) &&
1651-
!isa<AbstractTypeParamDecl>(this) &&
1651+
!isa<TypeDecl>(this) &&
16521652
"functions and enum case constructors only have an interface type");
16531653
return !TypeAndAccess.getPointer().isNull();
16541654
}
@@ -1657,7 +1657,7 @@ Type ValueDecl::getType() const {
16571657
assert(!isa<AbstractFunctionDecl>(this) &&
16581658
!isa<EnumElementDecl>(this) &&
16591659
!isa<SubscriptDecl>(this) &&
1660-
!isa<AbstractTypeParamDecl>(this) &&
1660+
!isa<TypeDecl>(this) &&
16611661
"functions and enum case constructors only have an interface type");
16621662
assert(hasType() && "declaration has no type set yet");
16631663
return TypeAndAccess.getPointer();
@@ -1672,7 +1672,7 @@ void ValueDecl::overwriteType(Type T) {
16721672
assert(!isa<AbstractFunctionDecl>(this) &&
16731673
!isa<EnumElementDecl>(this) &&
16741674
!isa<SubscriptDecl>(this) &&
1675-
!isa<AbstractTypeParamDecl>(this) &&
1675+
!isa<TypeDecl>(this) &&
16761676
"functions and enum case constructors only have an interface type");
16771677

16781678
TypeAndAccess.setPointer(T);
@@ -2031,15 +2031,8 @@ void NominalTypeDecl::computeType() {
20312031
Type declaredInterfaceTy = getDeclaredInterfaceType();
20322032
setInterfaceType(MetatypeType::get(declaredInterfaceTy, ctx));
20332033

2034-
// If we still don't have a declared type, don't crash -- there's a weird
2035-
// circular declaration issue in the code.
2036-
Type declaredTy = getDeclaredType();
2037-
if (!declaredTy || declaredTy->hasError()) {
2038-
setType(ErrorType::get(ctx));
2039-
return;
2040-
}
2041-
2042-
setType(MetatypeType::get(declaredTy, ctx));
2034+
if (declaredInterfaceTy->hasError())
2035+
setInvalid();
20432036
}
20442037

20452038
enum class DeclTypeKind : unsigned {
@@ -2201,19 +2194,14 @@ TypeAliasDecl::TypeAliasDecl(SourceLoc TypeAliasLoc, Identifier Name,
22012194
SourceLoc NameLoc, TypeLoc UnderlyingTy,
22022195
GenericParamList *GenericParams, DeclContext *DC)
22032196
: GenericTypeDecl(DeclKind::TypeAlias, DC, Name, NameLoc, {}, GenericParams),
2204-
TypeAliasLoc(TypeAliasLoc), UnderlyingTy(UnderlyingTy)
2205-
{
2197+
AliasTy(nullptr), TypeAliasLoc(TypeAliasLoc), UnderlyingTy(UnderlyingTy) {}
22062198

2207-
// Set the type of the TypeAlias to the right MetatypeType.
2199+
void TypeAliasDecl::computeType() {
22082200
ASTContext &Ctx = getASTContext();
2201+
assert(!AliasTy && "already called computeType()");
22092202
AliasTy = new (Ctx, AllocationArena::Permanent) NameAliasType(this);
22102203
}
22112204

2212-
void TypeAliasDecl::computeType() {
2213-
ASTContext &ctx = getASTContext();
2214-
setType(MetatypeType::get(AliasTy, ctx));
2215-
}
2216-
22172205
SourceRange TypeAliasDecl::getSourceRange() const {
22182206
if (UnderlyingTy.hasLocation())
22192207
return { TypeAliasLoc, UnderlyingTy.getSourceRange().End };
@@ -2869,7 +2857,7 @@ bool ProtocolDecl::existentialTypeSupportedSlow(LazyResolver *resolver) {
28692857
ProtocolDeclBits.ExistentialTypeSupported = true;
28702858

28712859
// Resolve the protocol's type.
2872-
if (resolver && !hasType())
2860+
if (resolver && !hasInterfaceType())
28732861
resolver->resolveDeclSignature(this);
28742862

28752863
for (auto member : getMembers()) {

lib/AST/DocComment.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "swift/AST/Comment.h"
2020
#include "swift/AST/Decl.h"
21+
#include "swift/AST/Types.h"
2122
#include "swift/AST/PrettyStackTrace.h"
2223
#include "swift/AST/RawComment.h"
2324
#include "swift/Markup/Markup.h"
@@ -401,7 +402,7 @@ getProtocolRequirementDocComment(swift::markup::MarkupContext &MC,
401402
const ValueDecl *VD)
402403
-> const ValueDecl * {
403404
SmallVector<ValueDecl *, 2> Members;
404-
P->lookupQualified(P->getType(), VD->getFullName(),
405+
P->lookupQualified(P->getDeclaredType(), VD->getFullName(),
405406
NLOptions::NL_ProtocolMembers,
406407
/*resolver=*/nullptr, Members);
407408
SmallVector<const ValueDecl *, 1> ProtocolRequirements;

lib/AST/Module.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void BuiltinUnit::LookupCache::lookupValue(
8484
/*genericparams*/nullptr,
8585
const_cast<BuiltinUnit*>(&M));
8686
TAD->computeType();
87-
TAD->setInterfaceType(TAD->getType());
87+
TAD->setInterfaceType(MetatypeType::get(TAD->getAliasType(), Ctx));
8888
TAD->setAccessibility(Accessibility::Public);
8989
Entry = TAD;
9090
}
@@ -346,7 +346,6 @@ ModuleDecl::ModuleDecl(Identifier name, ASTContext &ctx)
346346
Flags({0, 0, 0}), DSOHandle(nullptr) {
347347
ctx.addDestructorCleanup(*this);
348348
setImplicit();
349-
setType(ModuleType::get(this));
350349
setInterfaceType(ModuleType::get(this));
351350
setAccessibility(Accessibility::Public);
352351
}

lib/ClangImporter/ImportDecl.cpp

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ makeUnionFieldAccessors(ClangImporter::Implementation &Impl,
660660
auto inoutSelfRef = new (C) DeclRefExpr(inoutSelfDecl, DeclNameLoc(),
661661
/*implicit*/ true);
662662
auto inoutSelf = new (C) InOutExpr(SourceLoc(), inoutSelfRef,
663-
InOutType::get(importedUnionDecl->getType()), /*implicit*/ true);
663+
InOutType::get(importedUnionDecl->getDeclaredType()), /*implicit*/ true);
664664

665665
auto newValueDecl = setterDecl->getParameterList(1)->get(0);
666666

@@ -1908,7 +1908,9 @@ namespace {
19081908
underlying->getDeclaredInterfaceType()),
19091909
/*genericparams*/nullptr, DC);
19101910
typealias->computeType();
1911-
typealias->setInterfaceType(typealias->getType());
1911+
typealias->setInterfaceType(
1912+
MetatypeType::get(typealias->getAliasType(),
1913+
Impl.SwiftContext));
19121914

19131915
Impl.SpecialTypedefNames[Decl->getCanonicalDecl()] =
19141916
MappedTypeNameKind::DefineAndUse;
@@ -1934,7 +1936,9 @@ namespace {
19341936
proto->getDeclaredInterfaceType()),
19351937
/*genericparams*/nullptr, DC);
19361938
typealias->computeType();
1937-
typealias->setInterfaceType(typealias->getType());
1939+
typealias->setInterfaceType(
1940+
MetatypeType::get(typealias->getAliasType(),
1941+
Impl.SwiftContext));
19381942

19391943
Impl.SpecialTypedefNames[Decl->getCanonicalDecl()] =
19401944
MappedTypeNameKind::DefineAndUse;
@@ -2003,13 +2007,14 @@ namespace {
20032007
TypeLoc::withoutLoc(SwiftType),
20042008
/*genericparams*/nullptr, DC);
20052009
Result->computeType();
2006-
Result->setInterfaceType(Result->getType());
2010+
Result->setInterfaceType(
2011+
MetatypeType::get(Result->getAliasType(),
2012+
Impl.SwiftContext));
20072013

20082014
// Make Objective-C's 'id' unavailable.
2009-
ASTContext &ctx = DC->getASTContext();
2010-
if (ctx.LangOpts.EnableObjCInterop && isObjCId(Decl)) {
2015+
if (Impl.SwiftContext.LangOpts.EnableObjCInterop && isObjCId(Decl)) {
20112016
auto attr = AvailableAttr::createPlatformAgnostic(
2012-
ctx,
2017+
Impl.SwiftContext,
20132018
"'id' is not available in Swift; use 'Any'", "",
20142019
PlatformAgnosticAvailabilityKind::UnavailableInSwift);
20152020
Result->getAttrs().add(attr);
@@ -2274,7 +2279,9 @@ namespace {
22742279
errorWrapper->getDeclaredInterfaceType()),
22752280
/*genericSignature=*/nullptr, enumDecl);
22762281
alias->computeType();
2277-
alias->setInterfaceType(alias->getType());
2282+
alias->setInterfaceType(
2283+
MetatypeType::get(alias->getAliasType(),
2284+
Impl.SwiftContext));
22782285
enumDecl->addMember(alias);
22792286

22802287
// Add the 'Code' enum to the error wrapper.
@@ -4105,7 +4112,9 @@ namespace {
41054112
/*genericparams=*/nullptr, dc);
41064113

41074114
typealias->computeType();
4108-
typealias->setInterfaceType(typealias->getType());
4115+
typealias->setInterfaceType(
4116+
MetatypeType::get(typealias->getAliasType(),
4117+
Impl.SwiftContext));
41094118

41104119
return typealias;
41114120
}
@@ -4342,7 +4351,9 @@ Decl *SwiftDeclConverter::importSwift2TypeAlias(const clang::NamedDecl *decl,
43424351
Impl.importSourceLoc(decl->getLocation()),
43434352
TypeLoc::withoutLoc(underlyingType), genericParams, dc);
43444353
alias->computeType();
4345-
alias->setInterfaceType(alias->getType());
4354+
alias->setInterfaceType(
4355+
MetatypeType::get(alias->getAliasType(),
4356+
Impl.SwiftContext));
43464357
alias->setGenericEnvironment(genericEnv);
43474358

43484359
// Record that this is the Swift 2 version of this declaration.

lib/IDE/CodeCompletion.cpp

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -949,8 +949,9 @@ calculateTypeRelationForDecl(const Decl *D, Type ExpectedType,
949949
}
950950
}
951951
if (auto NTD = dyn_cast<NominalTypeDecl>(VD)) {
952-
return std::max(calculateTypeRelation(NTD->getType(), ExpectedType, DC),
953-
calculateTypeRelation(NTD->getDeclaredType(), ExpectedType, DC));
952+
return std::max(
953+
calculateTypeRelation(NTD->getInterfaceType(), ExpectedType, DC),
954+
calculateTypeRelation(NTD->getDeclaredInterfaceType(), ExpectedType, DC));
954955
}
955956
return calculateTypeRelation(VD->getInterfaceType(), ExpectedType, DC);
956957
}
@@ -1341,7 +1342,7 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks {
13411342
if (auto *NTD = dyn_cast<NominalTypeDecl>(DC)) {
13421343
// First, type check the parent DeclContext.
13431344
typecheckContextImpl(DC->getParent());
1344-
if (NTD->hasType())
1345+
if (NTD->hasInterfaceType())
13451346
return true;
13461347
return typeCheckCompletionDecl(cast<NominalTypeDecl>(DC));
13471348
}
@@ -2855,7 +2856,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
28552856

28562857
if (auto *NTD = dyn_cast<NominalTypeDecl>(D)) {
28572858
addNominalTypeRef(NTD, Reason);
2858-
addConstructorCallsForType(NTD->getType(), NTD->getName(), Reason);
2859+
addConstructorCallsForType(NTD->getInterfaceType(), NTD->getName(),
2860+
Reason);
28592861
return;
28602862
}
28612863

@@ -2869,7 +2871,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
28692871
if (auto *GP = dyn_cast<GenericTypeParamDecl>(D)) {
28702872
addGenericTypeParamRef(GP, Reason);
28712873
for (auto *protocol : GP->getConformingProtocols())
2872-
addConstructorCallsForType(protocol->getType(), GP->getName(),
2874+
addConstructorCallsForType(protocol->getInterfaceType(), GP->getName(),
28732875
Reason);
28742876
return;
28752877
}
@@ -2923,7 +2925,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
29232925

29242926
if (auto *NTD = dyn_cast<NominalTypeDecl>(D)) {
29252927
addNominalTypeRef(NTD, Reason);
2926-
addConstructorCallsForType(NTD->getType(), NTD->getName(), Reason);
2928+
addConstructorCallsForType(NTD->getInterfaceType(), NTD->getName(),
2929+
Reason);
29272930
return;
29282931
}
29292932

@@ -2937,7 +2940,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
29372940
if (auto *GP = dyn_cast<GenericTypeParamDecl>(D)) {
29382941
addGenericTypeParamRef(GP, Reason);
29392942
for (auto *protocol : GP->getConformingProtocols())
2940-
addConstructorCallsForType(protocol->getType(), GP->getName(),
2943+
addConstructorCallsForType(protocol->getInterfaceType(), GP->getName(),
29412944
Reason);
29422945
return;
29432946
}
@@ -3630,12 +3633,6 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
36303633
return std::binary_search(SortedNames.begin(), SortedNames.end(), Name);
36313634
}
36323635

3633-
void collectEnumElementTypes(EnumElementDecl *EED) {
3634-
if (isNameHit(EED->getNameStr()) && EED->getType()) {
3635-
unboxType(EED->getType());
3636-
}
3637-
}
3638-
36393636
void unboxType(Type T) {
36403637
if (T->getKind() == TypeKind::Paren) {
36413638
unboxType(T->getDesugaredType());

lib/IRGen/DebugTypeInfo.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ DebugTypeInfo::DebugTypeInfo(TypeDecl *Decl, const TypeInfo &Info)
7070
if (auto AliasDecl = dyn_cast<TypeAliasDecl>(Decl))
7171
Type = AliasDecl->getAliasType();
7272
else
73-
Type = Decl->getType().getPointer();
73+
Type = Decl->getInterfaceType().getPointer();
7474

7575
initFromTypeInfo(size, align, StorageType, Info);
7676
}
@@ -83,7 +83,7 @@ DebugTypeInfo::DebugTypeInfo(ValueDecl *Decl, llvm::Type *StorageTy, Size size,
8383
if (auto AliasDecl = dyn_cast<TypeAliasDecl>(Decl))
8484
Type = AliasDecl->getAliasType();
8585
else
86-
Type = Decl->getType().getPointer();
86+
Type = Decl->getInterfaceType().getPointer();
8787

8888
assert(StorageType && "StorageType is a nullptr");
8989
assert(align.getValue() != 0);

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1597,7 +1597,7 @@ llvm::DIType *IRGenDebugInfo::createType(DebugTypeInfo DbgTy,
15971597
// Emit the protocols the archetypes conform to.
15981598
SmallVector<llvm::Metadata *, 4> Protocols;
15991599
for (auto *ProtocolDecl : Archetype->getConformsTo()) {
1600-
auto PTy = IGM.getLoweredType(ProtocolDecl->getType())
1600+
auto PTy = IGM.getLoweredType(ProtocolDecl->getInterfaceType())
16011601
.getSwiftRValueType();
16021602
auto PDbgTy = DebugTypeInfo(ProtocolDecl, IGM.getTypeInfoForLowered(PTy));
16031603
auto PDITy = getOrCreateType(PDbgTy);

lib/Parse/ParseSIL.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ static llvm::PointerUnion<ValueDecl*, Module*> lookupTopDecl(Parser &P,
794794
return VD;
795795
}
796796

797-
/// Find the ValueDecl given a type and a member name.
797+
/// Find the ValueDecl given an interface type and a member name.
798798
static ValueDecl *lookupMember(Parser &P, Type Ty, Identifier Name,
799799
SourceLoc Loc,
800800
SmallVectorImpl<ValueDecl *> &Lookup,
@@ -934,14 +934,14 @@ bool SILParser::parseSILDottedPathWithoutPound(ValueDecl *&Decl,
934934
FullName.size() == 2/*ExpectMultipleResults*/);
935935
for (unsigned I = 2, E = FullName.size(); I < E; I++) {
936936
values.clear();
937-
VD = lookupMember(P, VD->getType(), FullName[I], Locs[I], values,
937+
VD = lookupMember(P, VD->getInterfaceType(), FullName[I], Locs[I], values,
938938
I == FullName.size() - 1/*ExpectMultipleResults*/);
939939
}
940940
} else {
941941
VD = Res.get<ValueDecl*>();
942942
for (unsigned I = 1, E = FullName.size(); I < E; I++) {
943943
values.clear();
944-
VD = lookupMember(P, VD->getType(), FullName[I], Locs[I], values,
944+
VD = lookupMember(P, VD->getInterfaceType(), FullName[I], Locs[I], values,
945945
I == FullName.size() - 1/*ExpectMultipleResults*/);
946946
}
947947
}
@@ -4331,7 +4331,7 @@ static AssociatedTypeDecl *parseAssociatedTypeDecl(Parser &P, SILParser &SP,
43314331
// One example is two decls when searching for Generator of Sequence:
43324332
// one from Sequence, the other from _Sequence_Type.
43334333
SmallVector<ValueDecl *, 4> values;
4334-
auto VD = lookupMember(P, proto->getType(), DeclName, DeclLoc,
4334+
auto VD = lookupMember(P, proto->getInterfaceType(), DeclName, DeclLoc,
43354335
values, true/*ExpectMultipleResults*/);
43364336
if (!VD) {
43374337
P.diagnose(DeclLoc, diag::sil_witness_assoc_not_found, DeclName);

lib/Sema/ConstraintSystem.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,10 +1134,8 @@ ConstraintSystem::getTypeOfMemberReference(
11341134
// We want to track if the generic context is represented by a
11351135
// class-bound existential so we won't inappropriately wrap the
11361136
// self type in an inout later on.
1137-
if (auto metatype = nominal->getType()->getAs<AnyMetatypeType>()) {
1138-
isClassBoundExistential = metatype->getInstanceType()->
1139-
isClassExistentialType();
1140-
}
1137+
isClassBoundExistential = nominal->getDeclaredType()
1138+
->isClassExistentialType();
11411139

11421140
if (outerDC->getAsProtocolOrProtocolExtensionContext()) {
11431141
// Retrieve the type variable for 'Self'.

lib/Sema/ITCDecl.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ bool IterativeTypeChecker::isResolveTypeDeclSatisfied(TypeDecl *typeDecl) {
295295

296296
// Nominal types.
297297
auto nominal = cast<NominalTypeDecl>(typeDecl);
298-
return nominal->hasType();
298+
return nominal->hasInterfaceType();
299299
}
300300

301301
void IterativeTypeChecker::processResolveTypeDecl(
@@ -304,7 +304,7 @@ void IterativeTypeChecker::processResolveTypeDecl(
304304
if (auto typeAliasDecl = dyn_cast<TypeAliasDecl>(typeDecl)) {
305305
if (typeAliasDecl->getDeclContext()->isModuleScopeContext()) {
306306
// FIXME: This is silly.
307-
if (!typeAliasDecl->hasType())
307+
if (!typeAliasDecl->getAliasType())
308308
typeAliasDecl->computeType();
309309

310310
TypeResolutionOptions options;
@@ -318,7 +318,7 @@ void IterativeTypeChecker::processResolveTypeDecl(
318318
typeAliasDecl->getDeclContext(),
319319
options, nullptr, &unsatisfiedDependency)) {
320320
typeAliasDecl->setInvalid();
321-
typeAliasDecl->overwriteType(ErrorType::get(getASTContext()));
321+
typeAliasDecl->setInterfaceType(ErrorType::get(getASTContext()));
322322
typeAliasDecl->getUnderlyingTypeLoc().setInvalidType(getASTContext());
323323
}
324324

@@ -335,7 +335,7 @@ void IterativeTypeChecker::processResolveTypeDecl(
335335
bool IterativeTypeChecker::breakCycleForResolveTypeDecl(TypeDecl *typeDecl) {
336336
if (auto typeAliasDecl = dyn_cast<TypeAliasDecl>(typeDecl)) {
337337
typeAliasDecl->setInvalid();
338-
typeAliasDecl->overwriteType(ErrorType::get(getASTContext()));
338+
typeAliasDecl->setInterfaceType(ErrorType::get(getASTContext()));
339339
typeAliasDecl->getUnderlyingTypeLoc().setInvalidType(getASTContext());
340340
return true;
341341
}

0 commit comments

Comments
 (0)