Skip to content

Removing more calls to ValueDecl::getType() #5990

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
44 changes: 7 additions & 37 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -515,13 +515,8 @@ class alignas(1 << DeclAlignInBits) Decl {
class AssociatedTypeDeclBitfields {
friend class AssociatedTypeDecl;
unsigned : NumTypeDeclBits;

unsigned Recursive : 1;

/// Whether or not this declaration is currently being type-checked.
unsigned BeingTypeChecked : 1;
};
enum { NumAssociatedTypeDeclBits = NumTypeDeclBits + 2 };
enum { NumAssociatedTypeDeclBits = NumTypeDeclBits };
static_assert(NumAssociatedTypeDeclBits <= 32, "fits in an unsigned");

class ImportDeclBitfields {
Expand Down Expand Up @@ -2004,20 +1999,8 @@ class ValueDecl : public Decl {
SourceLoc getNameLoc() const { return NameLoc; }
SourceLoc getLoc() const { return NameLoc; }

bool hasType() const {
assert(!isa<AbstractFunctionDecl>(this) &&
!isa<EnumElementDecl>(this) &&
"functions and enum case constructors only have an interface type");
return !TypeAndAccess.getPointer().isNull();
}

Type getType() const {
assert(!isa<AbstractFunctionDecl>(this) &&
!isa<EnumElementDecl>(this) &&
"functions and enum case constructors only have an interface type");
assert(hasType() && "declaration has no type set yet");
return TypeAndAccess.getPointer();
}
bool hasType() const;
Type getType() const;

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

public:
Type getDeclaredType() const;

/// The type of this declaration's values. For the type of the
/// declaration itself, use getInterfaceType(), which returns a
/// metatype.
Type getDeclaredInterfaceType() const;

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

void setIsRecursive();
bool isRecursive() { return AssociatedTypeDeclBits.Recursive; }

/// Whether the declaration is currently being validated.
bool isBeingTypeChecked() { return AssociatedTypeDeclBits.BeingTypeChecked; }

/// Toggle whether or not the declaration is being validated.
void setIsBeingTypeChecked(bool ibt = true) {
AssociatedTypeDeclBits.BeingTypeChecked = ibt;
}

static bool classof(const Decl *D) {
return D->getKind() == DeclKind::AssociatedType;
}
Expand Down Expand Up @@ -4429,15 +4402,12 @@ class SubscriptDecl : public AbstractStorageDecl, public DeclContext {
const ParameterList *getIndices() const { return Indices; }
void setIndices(ParameterList *p);

/// Retrieve the type of the indices.
Type getIndicesType() const;

/// Retrieve the interface type of the indices.
Type getIndicesInterfaceType() const;

/// \brief Retrieve the type of the element referenced by a subscript
/// operation.
Type getElementType() const { return ElementTy.getType(); }
Type getElementInterfaceType() const;
TypeLoc &getElementTypeLoc() { return ElementTy; }
const TypeLoc &getElementTypeLoc() const { return ElementTy; }

Expand Down
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1543,6 +1543,8 @@ ERROR(requires_generic_param_made_equal_to_concrete,none,
ERROR(requires_superclass_conflict,none,
"generic parameter %0 cannot be a subclass of both %1 and %2",
(Identifier, Type, Type))
ERROR(recursive_type_reference,none,
"type %0 references itself", (Identifier))
ERROR(recursive_requirement_reference,none,
"type may not reference itself as a requirement",())
ERROR(recursive_same_type_constraint,none,
Expand Down
4 changes: 1 addition & 3 deletions include/swift/Serialization/ModuleFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const uint16_t VERSION_MAJOR = 0;
/// in source control, you should also update the comment to briefly
/// describe what change you made. The content of this comment isn't important;
/// it just ensures a conflict if two people change the module format.
const uint16_t VERSION_MINOR = 287; // Last change: remove PolymorphicFunctionType
const uint16_t VERSION_MINOR = 288; // Last change: remove context type from subscript

using DeclID = PointerEmbeddedInt<unsigned, 31>;
using DeclIDField = BCFixed<31>;
Expand Down Expand Up @@ -967,8 +967,6 @@ namespace decls_block {
BCFixed<1>, // implicit?
BCFixed<1>, // objc?
StorageKindField, // StorageKind
TypeIDField, // subscript dummy type
TypeIDField, // element type
TypeIDField, // interface type
DeclIDField, // getter
DeclIDField, // setter
Expand Down
9 changes: 5 additions & 4 deletions lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,9 @@ namespace {
printGenericParameters(OS, GTD->getGenericParams());

if (!isa<AbstractFunctionDecl>(VD) &&
!isa<EnumElementDecl>(VD)) {
!isa<EnumElementDecl>(VD) &&
!isa<SubscriptDecl>(VD) &&
!isa<TypeDecl>(VD)) {
OS << " type='";
if (VD->hasType())
VD->getType().print(OS);
Expand All @@ -526,7 +528,7 @@ namespace {

if (VD->hasInterfaceType()) {
OS << " interface type='";
VD->getInterfaceType()->getCanonicalType().print(OS);
VD->getInterfaceType()->print(OS);
OS << '\'';
}

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

if (NTD->hasType()) {
if (NTD->hasInterfaceType()) {
if (NTD->hasFixedLayout())
OS << " @_fixed_layout";
else
Expand Down Expand Up @@ -703,7 +705,6 @@ namespace {
void visitSubscriptDecl(SubscriptDecl *SD) {
printCommon(SD, "subscript_decl");
OS << " storage_kind=" << getStorageKindName(SD->getStorageKind());
OS << " element=" << SD->getElementType()->getCanonicalType();
printAccessors(SD);
OS << ')';
}
Expand Down
7 changes: 5 additions & 2 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2769,14 +2769,17 @@ void PrintAST::visitSubscriptDecl(SubscriptDecl *decl) {
recordDeclLoc(decl, [&]{
Printer << "subscript";
}, [&] { // Parameters
printParameterList(decl->getIndices(), decl->getIndicesType(),
printParameterList(decl->getIndices(), decl->getIndicesInterfaceType(),
/*Curried=*/false,
/*isAPINameByDefault*/[]()->bool{return false;});
});
Printer << " -> ";

Printer.callPrintStructurePre(PrintStructureKind::FunctionReturnType);
printTypeLoc(decl->getElementTypeLoc());
TypeLoc elementTy = decl->getElementTypeLoc();
if (!elementTy.getTypeRepr())
elementTy = TypeLoc::withoutLoc(decl->getElementInterfaceType());
printTypeLoc(elementTy);
Printer.printStructurePost(PrintStructureKind::FunctionReturnType);

printAccessors(decl);
Expand Down
19 changes: 5 additions & 14 deletions lib/AST/ArchetypeBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -935,8 +935,8 @@ auto ArchetypeBuilder::addGenericParameter(GenericTypeParamType *GenericParam,

void ArchetypeBuilder::addGenericParameter(GenericTypeParamDecl *GenericParam) {
addGenericParameter(
GenericParam->getDeclaredType()->castTo<GenericTypeParamType>(),
GenericParam->getName());
GenericParam->getDeclaredInterfaceType()->castTo<GenericTypeParamType>(),
GenericParam->getName());
}

bool ArchetypeBuilder::addGenericParameterRequirements(GenericTypeParamDecl *GenericParam) {
Expand Down Expand Up @@ -1429,18 +1429,9 @@ bool ArchetypeBuilder::addAbstractTypeParamRequirements(
auto markRecursive = [&](AssociatedTypeDecl *assocType,
ProtocolDecl *proto,
SourceLoc loc) {
if (!pa->isRecursive() && !assocType->isRecursive()) {
if (!pa->isRecursive() && !assocType->isInvalid()) {
Diags.diagnose(assocType->getLoc(),
diag::recursive_requirement_reference);

// Mark all associatedtypes in this protocol as recursive (and error-type)
// to avoid later crashes dealing with this invalid protocol in other
// contexts.
auto containingProto =
assocType->getDeclContext()->getAsProtocolOrProtocolExtensionContext();
for (auto member : containingProto->getMembers())
if (auto assocType = dyn_cast<AssociatedTypeDecl>(member))
assocType->setIsRecursive();
}
pa->setIsRecursive();

Expand All @@ -1453,8 +1444,8 @@ bool ArchetypeBuilder::addAbstractTypeParamRequirements(
};

if (isa<AssociatedTypeDecl>(decl) &&
decl->hasType() &&
decl->getType()->is<ErrorType>())
decl->hasInterfaceType() &&
decl->getInterfaceType()->is<ErrorType>())
return false;

// If this is an associated type that already has an archetype assigned,
Expand Down
7 changes: 4 additions & 3 deletions lib/AST/Builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ getBuiltinGenericFunction(Identifier Id,
// Compute the interface type.
SmallVector<GenericTypeParamType *, 1> GenericParamTypes;
for (auto gp : *GenericParams) {
GenericParamTypes.push_back(gp->getDeclaredType()
GenericParamTypes.push_back(gp->getDeclaredInterfaceType()
->castTo<GenericTypeParamType>());
}
GenericSignature *Sig =
Expand Down Expand Up @@ -479,7 +479,7 @@ namespace {
Archetypes, GenericTypeParams);

for (unsigned i = 0, e = GenericTypeParams.size(); i < e; i++) {
auto paramTy = GenericTypeParams[i]->getDeclaredType()
auto paramTy = GenericTypeParams[i]->getDeclaredInterfaceType()
->getCanonicalType()->castTo<GenericTypeParamType>();
InterfaceToArchetypeMap[paramTy] = Archetypes[i];
}
Expand Down Expand Up @@ -517,7 +517,8 @@ namespace {
unsigned Index;
Type build(GenericSignatureBuilder &builder, bool forBody) const {
return (forBody ? builder.Archetypes[Index]
: builder.GenericTypeParams[Index]->getDeclaredType());
: builder.GenericTypeParams[Index]
->getDeclaredInterfaceType());
}
};
struct LambdaGenerator {
Expand Down
Loading