Skip to content

Commit 0eba047

Browse files
authored
Merge pull request #5990 from slavapestov/removing-value-decl-get-type
Removing more calls to ValueDecl::getType()
2 parents c13af38 + 2ff9994 commit 0eba047

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+361
-437
lines changed

include/swift/AST/Decl.h

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -515,13 +515,8 @@ class alignas(1 << DeclAlignInBits) Decl {
515515
class AssociatedTypeDeclBitfields {
516516
friend class AssociatedTypeDecl;
517517
unsigned : NumTypeDeclBits;
518-
519-
unsigned Recursive : 1;
520-
521-
/// Whether or not this declaration is currently being type-checked.
522-
unsigned BeingTypeChecked : 1;
523518
};
524-
enum { NumAssociatedTypeDeclBits = NumTypeDeclBits + 2 };
519+
enum { NumAssociatedTypeDeclBits = NumTypeDeclBits };
525520
static_assert(NumAssociatedTypeDeclBits <= 32, "fits in an unsigned");
526521

527522
class ImportDeclBitfields {
@@ -2004,20 +1999,8 @@ class ValueDecl : public Decl {
20041999
SourceLoc getNameLoc() const { return NameLoc; }
20052000
SourceLoc getLoc() const { return NameLoc; }
20062001

2007-
bool hasType() const {
2008-
assert(!isa<AbstractFunctionDecl>(this) &&
2009-
!isa<EnumElementDecl>(this) &&
2010-
"functions and enum case constructors only have an interface type");
2011-
return !TypeAndAccess.getPointer().isNull();
2012-
}
2013-
2014-
Type getType() const {
2015-
assert(!isa<AbstractFunctionDecl>(this) &&
2016-
!isa<EnumElementDecl>(this) &&
2017-
"functions and enum case constructors only have an interface type");
2018-
assert(hasType() && "declaration has no type set yet");
2019-
return TypeAndAccess.getPointer();
2020-
}
2002+
bool hasType() const;
2003+
Type getType() const;
20212004

20222005
/// Set the type of this declaration for the first time.
20232006
void setType(Type T);
@@ -2217,8 +2200,9 @@ class TypeDecl : public ValueDecl {
22172200
}
22182201

22192202
public:
2220-
Type getDeclaredType() const;
2221-
2203+
/// The type of this declaration's values. For the type of the
2204+
/// declaration itself, use getInterfaceType(), which returns a
2205+
/// metatype.
22222206
Type getDeclaredInterfaceType() const;
22232207

22242208
/// \brief Retrieve the set of protocols that this type inherits (i.e,
@@ -2532,17 +2516,6 @@ class AssociatedTypeDecl : public AbstractTypeParamDecl {
25322516
SourceLoc getStartLoc() const { return KeywordLoc; }
25332517
SourceRange getSourceRange() const;
25342518

2535-
void setIsRecursive();
2536-
bool isRecursive() { return AssociatedTypeDeclBits.Recursive; }
2537-
2538-
/// Whether the declaration is currently being validated.
2539-
bool isBeingTypeChecked() { return AssociatedTypeDeclBits.BeingTypeChecked; }
2540-
2541-
/// Toggle whether or not the declaration is being validated.
2542-
void setIsBeingTypeChecked(bool ibt = true) {
2543-
AssociatedTypeDeclBits.BeingTypeChecked = ibt;
2544-
}
2545-
25462519
static bool classof(const Decl *D) {
25472520
return D->getKind() == DeclKind::AssociatedType;
25482521
}
@@ -4429,15 +4402,12 @@ class SubscriptDecl : public AbstractStorageDecl, public DeclContext {
44294402
const ParameterList *getIndices() const { return Indices; }
44304403
void setIndices(ParameterList *p);
44314404

4432-
/// Retrieve the type of the indices.
4433-
Type getIndicesType() const;
4434-
44354405
/// Retrieve the interface type of the indices.
44364406
Type getIndicesInterfaceType() const;
44374407

44384408
/// \brief Retrieve the type of the element referenced by a subscript
44394409
/// operation.
4440-
Type getElementType() const { return ElementTy.getType(); }
4410+
Type getElementInterfaceType() const;
44414411
TypeLoc &getElementTypeLoc() { return ElementTy; }
44424412
const TypeLoc &getElementTypeLoc() const { return ElementTy; }
44434413

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,6 +1543,8 @@ ERROR(requires_generic_param_made_equal_to_concrete,none,
15431543
ERROR(requires_superclass_conflict,none,
15441544
"generic parameter %0 cannot be a subclass of both %1 and %2",
15451545
(Identifier, Type, Type))
1546+
ERROR(recursive_type_reference,none,
1547+
"type %0 references itself", (Identifier))
15461548
ERROR(recursive_requirement_reference,none,
15471549
"type may not reference itself as a requirement",())
15481550
ERROR(recursive_same_type_constraint,none,

include/swift/Serialization/ModuleFormat.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const uint16_t VERSION_MAJOR = 0;
5454
/// in source control, you should also update the comment to briefly
5555
/// describe what change you made. The content of this comment isn't important;
5656
/// it just ensures a conflict if two people change the module format.
57-
const uint16_t VERSION_MINOR = 287; // Last change: remove PolymorphicFunctionType
57+
const uint16_t VERSION_MINOR = 288; // Last change: remove context type from subscript
5858

5959
using DeclID = PointerEmbeddedInt<unsigned, 31>;
6060
using DeclIDField = BCFixed<31>;
@@ -967,8 +967,6 @@ namespace decls_block {
967967
BCFixed<1>, // implicit?
968968
BCFixed<1>, // objc?
969969
StorageKindField, // StorageKind
970-
TypeIDField, // subscript dummy type
971-
TypeIDField, // element type
972970
TypeIDField, // interface type
973971
DeclIDField, // getter
974972
DeclIDField, // setter

lib/AST/ASTDumper.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,9 @@ namespace {
515515
printGenericParameters(OS, GTD->getGenericParams());
516516

517517
if (!isa<AbstractFunctionDecl>(VD) &&
518-
!isa<EnumElementDecl>(VD)) {
518+
!isa<EnumElementDecl>(VD) &&
519+
!isa<SubscriptDecl>(VD) &&
520+
!isa<TypeDecl>(VD)) {
519521
OS << " type='";
520522
if (VD->hasType())
521523
VD->getType().print(OS);
@@ -526,7 +528,7 @@ namespace {
526528

527529
if (VD->hasInterfaceType()) {
528530
OS << " interface type='";
529-
VD->getInterfaceType()->getCanonicalType().print(OS);
531+
VD->getInterfaceType()->print(OS);
530532
OS << '\'';
531533
}
532534

@@ -567,7 +569,7 @@ namespace {
567569
llvm::Optional<llvm::raw_ostream::Colors>()) {
568570
printCommon((ValueDecl *)NTD, Name, Color);
569571

570-
if (NTD->hasType()) {
572+
if (NTD->hasInterfaceType()) {
571573
if (NTD->hasFixedLayout())
572574
OS << " @_fixed_layout";
573575
else
@@ -703,7 +705,6 @@ namespace {
703705
void visitSubscriptDecl(SubscriptDecl *SD) {
704706
printCommon(SD, "subscript_decl");
705707
OS << " storage_kind=" << getStorageKindName(SD->getStorageKind());
706-
OS << " element=" << SD->getElementType()->getCanonicalType();
707708
printAccessors(SD);
708709
OS << ')';
709710
}

lib/AST/ASTPrinter.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2769,14 +2769,17 @@ void PrintAST::visitSubscriptDecl(SubscriptDecl *decl) {
27692769
recordDeclLoc(decl, [&]{
27702770
Printer << "subscript";
27712771
}, [&] { // Parameters
2772-
printParameterList(decl->getIndices(), decl->getIndicesType(),
2772+
printParameterList(decl->getIndices(), decl->getIndicesInterfaceType(),
27732773
/*Curried=*/false,
27742774
/*isAPINameByDefault*/[]()->bool{return false;});
27752775
});
27762776
Printer << " -> ";
27772777

27782778
Printer.callPrintStructurePre(PrintStructureKind::FunctionReturnType);
2779-
printTypeLoc(decl->getElementTypeLoc());
2779+
TypeLoc elementTy = decl->getElementTypeLoc();
2780+
if (!elementTy.getTypeRepr())
2781+
elementTy = TypeLoc::withoutLoc(decl->getElementInterfaceType());
2782+
printTypeLoc(elementTy);
27802783
Printer.printStructurePost(PrintStructureKind::FunctionReturnType);
27812784

27822785
printAccessors(decl);

lib/AST/ArchetypeBuilder.cpp

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -935,8 +935,8 @@ auto ArchetypeBuilder::addGenericParameter(GenericTypeParamType *GenericParam,
935935

936936
void ArchetypeBuilder::addGenericParameter(GenericTypeParamDecl *GenericParam) {
937937
addGenericParameter(
938-
GenericParam->getDeclaredType()->castTo<GenericTypeParamType>(),
939-
GenericParam->getName());
938+
GenericParam->getDeclaredInterfaceType()->castTo<GenericTypeParamType>(),
939+
GenericParam->getName());
940940
}
941941

942942
bool ArchetypeBuilder::addGenericParameterRequirements(GenericTypeParamDecl *GenericParam) {
@@ -1429,18 +1429,9 @@ bool ArchetypeBuilder::addAbstractTypeParamRequirements(
14291429
auto markRecursive = [&](AssociatedTypeDecl *assocType,
14301430
ProtocolDecl *proto,
14311431
SourceLoc loc) {
1432-
if (!pa->isRecursive() && !assocType->isRecursive()) {
1432+
if (!pa->isRecursive() && !assocType->isInvalid()) {
14331433
Diags.diagnose(assocType->getLoc(),
14341434
diag::recursive_requirement_reference);
1435-
1436-
// Mark all associatedtypes in this protocol as recursive (and error-type)
1437-
// to avoid later crashes dealing with this invalid protocol in other
1438-
// contexts.
1439-
auto containingProto =
1440-
assocType->getDeclContext()->getAsProtocolOrProtocolExtensionContext();
1441-
for (auto member : containingProto->getMembers())
1442-
if (auto assocType = dyn_cast<AssociatedTypeDecl>(member))
1443-
assocType->setIsRecursive();
14441435
}
14451436
pa->setIsRecursive();
14461437

@@ -1453,8 +1444,8 @@ bool ArchetypeBuilder::addAbstractTypeParamRequirements(
14531444
};
14541445

14551446
if (isa<AssociatedTypeDecl>(decl) &&
1456-
decl->hasType() &&
1457-
decl->getType()->is<ErrorType>())
1447+
decl->hasInterfaceType() &&
1448+
decl->getInterfaceType()->is<ErrorType>())
14581449
return false;
14591450

14601451
// If this is an associated type that already has an archetype assigned,

lib/AST/Builtins.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ getBuiltinGenericFunction(Identifier Id,
192192
// Compute the interface type.
193193
SmallVector<GenericTypeParamType *, 1> GenericParamTypes;
194194
for (auto gp : *GenericParams) {
195-
GenericParamTypes.push_back(gp->getDeclaredType()
195+
GenericParamTypes.push_back(gp->getDeclaredInterfaceType()
196196
->castTo<GenericTypeParamType>());
197197
}
198198
GenericSignature *Sig =
@@ -479,7 +479,7 @@ namespace {
479479
Archetypes, GenericTypeParams);
480480

481481
for (unsigned i = 0, e = GenericTypeParams.size(); i < e; i++) {
482-
auto paramTy = GenericTypeParams[i]->getDeclaredType()
482+
auto paramTy = GenericTypeParams[i]->getDeclaredInterfaceType()
483483
->getCanonicalType()->castTo<GenericTypeParamType>();
484484
InterfaceToArchetypeMap[paramTy] = Archetypes[i];
485485
}
@@ -517,7 +517,8 @@ namespace {
517517
unsigned Index;
518518
Type build(GenericSignatureBuilder &builder, bool forBody) const {
519519
return (forBody ? builder.Archetypes[Index]
520-
: builder.GenericTypeParams[Index]->getDeclaredType());
520+
: builder.GenericTypeParams[Index]
521+
->getDeclaredInterfaceType());
521522
}
522523
};
523524
struct LambdaGenerator {

0 commit comments

Comments
 (0)