Skip to content

Commit a17eec5

Browse files
authored
Merge pull request #6018 from slavapestov/finish-removing-value-decl-get-type
Push ValueDecl::{has,get,set}Type() down to VarDecl
2 parents 259eed9 + 26c8042 commit a17eec5

32 files changed

+156
-262
lines changed

include/swift/AST/AnyFunctionRef.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,6 @@ class AnyFunctionRef {
7575
}
7676

7777
Type getType() const {
78-
if (auto *AFD = TheFunction.dyn_cast<AbstractFunctionDecl *>())
79-
return AFD->getType();
80-
return TheFunction.get<AbstractClosureExpr *>()->getType();
81-
}
82-
83-
/// FIXME: This should just be getType() when interface types take over in
84-
/// the AST.
85-
Type getInterfaceType() const {
8678
if (auto *AFD = TheFunction.dyn_cast<AbstractFunctionDecl *>())
8779
return AFD->getInterfaceType();
8880
return TheFunction.get<AbstractClosureExpr *>()->getType();

include/swift/AST/Decl.h

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,15 +1999,6 @@ class ValueDecl : public Decl {
19991999
SourceLoc getNameLoc() const { return NameLoc; }
20002000
SourceLoc getLoc() const { return NameLoc; }
20012001

2002-
bool hasType() const;
2003-
Type getType() const;
2004-
2005-
/// Set the type of this declaration for the first time.
2006-
void setType(Type T);
2007-
2008-
/// Overwrite the type of this declaration.
2009-
void overwriteType(Type T);
2010-
20112002
bool hasAccessibility() const {
20122003
return TypeAndAccess.getInt().hasValue();
20132004
}
@@ -2080,15 +2071,10 @@ class ValueDecl : public Decl {
20802071
/// If \p DC is null, returns true only if this declaration is public.
20812072
bool isAccessibleFrom(const DeclContext *DC) const;
20822073

2083-
/// Retrieve the "interface" type of this value, which is the type used when
2084-
/// the declaration is viewed from the outside. For a generic function,
2085-
/// this will have generic function type using generic parameters rather than
2086-
/// archetypes, while a generic nominal type's interface type will be the
2087-
/// generic type specialized with its generic parameters.
2088-
///
2089-
/// FIXME: Eventually, this will simply become the type of the value, and
2090-
/// we will substitute in the appropriate archetypes within a particular
2091-
/// context.
2074+
/// Retrieve the "interface" type of this value, which uses
2075+
/// GenericTypeParamType if the declaration is generic. For a generic
2076+
/// function, this will have a GenericFunctionType with a
2077+
/// GenericSignature inside the type.
20922078
Type getInterfaceType() const;
20932079
bool hasInterfaceType() const;
20942080

@@ -4080,6 +4066,8 @@ class VarDecl : public AbstractStorageDecl {
40804066
/// This is the type specified, including location information.
40814067
TypeLoc typeLoc;
40824068

4069+
Type typeInContext;
4070+
40834071
public:
40844072
VarDecl(bool IsStatic, bool IsLet, SourceLoc NameLoc, Identifier Name,
40854073
Type Ty, DeclContext *DC)
@@ -4098,6 +4086,22 @@ class VarDecl : public AbstractStorageDecl {
40984086
TypeLoc &getTypeLoc() { return typeLoc; }
40994087
TypeLoc getTypeLoc() const { return typeLoc; }
41004088

4089+
bool hasType() const {
4090+
return !!typeInContext;
4091+
}
4092+
4093+
/// Get the type of the variable within its context. If the context is generic,
4094+
/// this will use archetypes.
4095+
Type getType() const {
4096+
assert(typeInContext && "No context type set yet");
4097+
return typeInContext;
4098+
}
4099+
4100+
/// Set the type of the variable within its context.
4101+
void setType(Type t);
4102+
4103+
void markInvalid();
4104+
41014105
/// Retrieve the source range of the variable type, or an invalid range if the
41024106
/// variable's type is not explicitly written in the source.
41034107
///

lib/AST/ASTDumper.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -514,13 +514,10 @@ namespace {
514514
if (GenericTypeDecl *GTD = dyn_cast<GenericTypeDecl>(VD))
515515
printGenericParameters(OS, GTD->getGenericParams());
516516

517-
if (!isa<AbstractFunctionDecl>(VD) &&
518-
!isa<EnumElementDecl>(VD) &&
519-
!isa<SubscriptDecl>(VD) &&
520-
!isa<TypeDecl>(VD)) {
517+
if (auto *var = dyn_cast<VarDecl>(VD)) {
521518
OS << " type='";
522-
if (VD->hasType())
523-
VD->getType().print(OS);
519+
if (var->hasType())
520+
var->getType().print(OS);
524521
else
525522
OS << "<null type>";
526523
OS << '\'';

lib/AST/ASTPrinter.cpp

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3215,48 +3215,6 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
32153215
ASTPrinter &Printer;
32163216
const PrintOptions &Options;
32173217

3218-
void printDeclContext(DeclContext *DC) {
3219-
switch (DC->getContextKind()) {
3220-
case DeclContextKind::Module: {
3221-
Module *M = cast<Module>(DC);
3222-
3223-
if (auto Parent = M->getParent())
3224-
printDeclContext(Parent);
3225-
Printer.printModuleRef(M, M->getName());
3226-
return;
3227-
}
3228-
3229-
case DeclContextKind::FileUnit:
3230-
printDeclContext(DC->getParent());
3231-
return;
3232-
3233-
case DeclContextKind::AbstractClosureExpr:
3234-
// FIXME: print closures somehow.
3235-
return;
3236-
3237-
case DeclContextKind::GenericTypeDecl:
3238-
visit(cast<GenericTypeDecl>(DC)->getType());
3239-
return;
3240-
3241-
case DeclContextKind::ExtensionDecl:
3242-
visit(cast<ExtensionDecl>(DC)->getExtendedType());
3243-
return;
3244-
3245-
case DeclContextKind::Initializer:
3246-
case DeclContextKind::TopLevelCodeDecl:
3247-
case DeclContextKind::SerializedLocal:
3248-
llvm_unreachable("bad decl context");
3249-
3250-
case DeclContextKind::AbstractFunctionDecl:
3251-
visit(cast<AbstractFunctionDecl>(DC)->getType());
3252-
return;
3253-
3254-
case DeclContextKind::SubscriptDecl:
3255-
visit(cast<SubscriptDecl>(DC)->getType());
3256-
return;
3257-
}
3258-
}
3259-
32603218
void printGenericArgs(ArrayRef<Type> Args) {
32613219
if (Args.empty())
32623220
return;

lib/AST/ASTVerifier.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2410,13 +2410,6 @@ struct ASTNodeBase {};
24102410
abort();
24112411
}
24122412

2413-
void checkIsTypeOfRValue(ValueDecl *D, Type rvalueType, const char *what) {
2414-
auto declType = D->getType();
2415-
if (auto refType = declType->getAs<ReferenceStorageType>())
2416-
declType = refType->getReferentType();
2417-
checkSameType(declType, rvalueType, what);
2418-
}
2419-
24202413
void checkSameType(Type T0, Type T1, const char *what) {
24212414
if (T0->getCanonicalType() == T1->getCanonicalType())
24222415
return;

lib/AST/Decl.cpp

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,47 +1644,11 @@ ValueDecl::getSatisfiedProtocolRequirements(bool Sorted) const {
16441644
return NTD->getSatisfiedProtocolRequirementsForMember(this, Sorted);
16451645
}
16461646

1647-
bool ValueDecl::hasType() const {
1648-
assert(!isa<AbstractFunctionDecl>(this) &&
1649-
!isa<EnumElementDecl>(this) &&
1650-
!isa<SubscriptDecl>(this) &&
1651-
!isa<TypeDecl>(this) &&
1652-
"functions and enum case constructors only have an interface type");
1653-
return !TypeAndAccess.getPointer().isNull();
1654-
}
1655-
1656-
Type ValueDecl::getType() const {
1657-
assert(!isa<AbstractFunctionDecl>(this) &&
1658-
!isa<EnumElementDecl>(this) &&
1659-
!isa<SubscriptDecl>(this) &&
1660-
!isa<TypeDecl>(this) &&
1661-
"functions and enum case constructors only have an interface type");
1662-
assert(hasType() && "declaration has no type set yet");
1663-
return TypeAndAccess.getPointer();
1664-
}
1665-
1666-
void ValueDecl::setType(Type T) {
1667-
assert(!hasType() && "changing type of declaration");
1668-
overwriteType(T);
1669-
}
1670-
1671-
void ValueDecl::overwriteType(Type T) {
1672-
assert(!isa<AbstractFunctionDecl>(this) &&
1673-
!isa<EnumElementDecl>(this) &&
1674-
!isa<SubscriptDecl>(this) &&
1675-
!isa<TypeDecl>(this) &&
1676-
"functions and enum case constructors only have an interface type");
1677-
1678-
TypeAndAccess.setPointer(T);
1679-
if (!T.isNull() && T->hasError())
1680-
setInvalid();
1681-
}
1682-
16831647
bool ValueDecl::hasInterfaceType() const {
16841648
// getInterfaceType() returns the contextual type for ParamDecls which
16851649
// don't have an explicit interface type.
1686-
if (isa<ParamDecl>(this))
1687-
return hasType();
1650+
if (auto *PD = dyn_cast<ParamDecl>(this))
1651+
return PD->hasType();
16881652

16891653
return !!InterfaceTy;
16901654
}
@@ -1693,14 +1657,11 @@ Type ValueDecl::getInterfaceType() const {
16931657
if (InterfaceTy)
16941658
return InterfaceTy;
16951659

1696-
if (!hasType())
1697-
return Type();
1698-
16991660
// FIXME: ParamDecls are funky and don't always have an interface type
1700-
if (isa<ParamDecl>(this))
1701-
return getType();
1661+
if (auto *PD = dyn_cast<ParamDecl>(this))
1662+
return PD->getType();
17021663

1703-
llvm_unreachable("decl has context type but no interface type");
1664+
llvm_unreachable("no interface type was set");
17041665
}
17051666

17061667
void ValueDecl::setInterfaceType(Type type) {
@@ -3407,6 +3368,19 @@ static bool isSettable(const AbstractStorageDecl *decl) {
34073368
llvm_unreachable("bad storage kind");
34083369
}
34093370

3371+
void VarDecl::setType(Type t) {
3372+
typeInContext = t;
3373+
if (t && t->hasError())
3374+
setInvalid();
3375+
}
3376+
3377+
void VarDecl::markInvalid() {
3378+
auto &Ctx = getASTContext();
3379+
setType(ErrorType::get(Ctx));
3380+
setInterfaceType(ErrorType::get(Ctx));
3381+
setInvalid();
3382+
}
3383+
34103384
/// \brief Returns whether the var is settable in the specified context: this
34113385
/// is either because it is a stored var, because it has a custom setter, or
34123386
/// is a let member in an initializer.

lib/AST/DeclContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ unsigned DeclContext::printContext(raw_ostream &OS, unsigned indent) const {
765765
auto *SD = cast<SubscriptDecl>(this);
766766
OS << " name=" << SD->getName();
767767
if (SD->hasInterfaceType())
768-
OS << " : " << SD->getType();
768+
OS << " : " << SD->getInterfaceType();
769769
else
770770
OS << " : (no type set)";
771771
break;

lib/AST/LookupVisibleDecls.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,9 @@ static void doDynamicLookup(VisibleDeclConsumer &Consumer,
357357
}
358358

359359
case DeclKind::Var: {
360+
auto *VD = cast<VarDecl>(D);
360361
auto Signature =
361-
std::make_pair(D->getName(), D->getType()->getCanonicalType());
362+
std::make_pair(VD->getName(), VD->getType()->getCanonicalType());
362363
if (!PropertiesReported.insert(Signature).second)
363364
return;
364365
break;

lib/AST/Mangle.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,7 @@ void Mangler::mangleType(Type type, unsigned uncurryLevel) {
11091109
SmallVector<const void *, 4> SortedSubsts(Substitutions.size());
11101110
for (auto S : Substitutions) SortedSubsts[S.second] = S.first;
11111111
for (auto S : SortedSubsts) ContextMangler.addSubstitution(S);
1112-
while (DC && DC->getGenericParamsOfContext()) {
1112+
while (DC && DC->isGenericContext()) {
11131113
if (DC->isInnermostContextGeneric() &&
11141114
DC->getGenericParamsOfContext()->getDepth() == GTPT->getDepth())
11151115
break;

lib/ClangImporter/ImportDecl.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,7 +1468,7 @@ static bool addErrorDomain(NominalTypeDecl *swiftDecl,
14681468
false));
14691469
auto stringTy = C.getStringDecl()->getDeclaredType();
14701470
assert(stringTy && "no string type available");
1471-
if (!swiftValueDecl || !swiftValueDecl->getType()->isEqual(stringTy)) {
1471+
if (!swiftValueDecl || !swiftValueDecl->getInterfaceType()->isEqual(stringTy)) {
14721472
// Couldn't actually import it as an error enum, fall back to enum
14731473
return false;
14741474
}
@@ -1639,13 +1639,13 @@ applyPropertyOwnership(VarDecl *prop,
16391639
}
16401640
if (attrs & clang::ObjCPropertyDecl::OBJC_PR_weak) {
16411641
prop->getAttrs().add(new (ctx) OwnershipAttr(Ownership::Weak));
1642-
prop->overwriteType(WeakStorageType::get(prop->getType(), ctx));
1642+
prop->setType(WeakStorageType::get(prop->getType(), ctx));
16431643
return;
16441644
}
16451645
if ((attrs & clang::ObjCPropertyDecl::OBJC_PR_assign) ||
16461646
(attrs & clang::ObjCPropertyDecl::OBJC_PR_unsafe_unretained)) {
16471647
prop->getAttrs().add(new (ctx) OwnershipAttr(Ownership::Unmanaged));
1648-
prop->overwriteType(UnmanagedStorageType::get(prop->getType(), ctx));
1648+
prop->setType(UnmanagedStorageType::get(prop->getType(), ctx));
16491649
return;
16501650
}
16511651
}

lib/SIL/SILFunctionType.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -763,8 +763,11 @@ static CanSILFunctionType getSILFunctionType(SILModule &M,
763763
}
764764

765765
auto *VD = capture.getDecl();
766-
auto type = ArchetypeBuilder::mapTypeOutOfContext(
767-
function->getAsDeclContext(), VD->getType());
766+
auto type = VD->getInterfaceType();
767+
// FIXME: Interface types for parameters
768+
if (type->hasArchetype())
769+
type = ArchetypeBuilder::mapTypeOutOfContext(
770+
function->getAsDeclContext(), type);
768771
auto canType = getCanonicalType(type);
769772

770773
auto &loweredTL = Types.getTypeLowering(

lib/SILGen/SILGenFunction.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,14 @@ void SILGenFunction::emitCaptures(SILLocation loc,
266266
// let declarations.
267267
auto Entry = VarLocs[vd];
268268

269-
auto &tl = getTypeLowering(vd->getType()->getReferenceStorageReferent());
269+
auto *var = cast<VarDecl>(vd);
270+
auto &tl = getTypeLowering(var->getType()->getReferenceStorageReferent());
270271
SILValue Val = Entry.value;
271272

272273
if (!Val->getType().isAddress()) {
273274
// Our 'let' binding can guarantee the lifetime for the callee,
274275
// if we don't need to do anything more to it.
275-
if (canGuarantee && !vd->getType()->is<ReferenceStorageType>()) {
276+
if (canGuarantee && !var->getType()->is<ReferenceStorageType>()) {
276277
auto guaranteed = ManagedValue::forUnmanaged(Val);
277278
capturedArgs.push_back(guaranteed);
278279
break;
@@ -289,8 +290,8 @@ void SILGenFunction::emitCaptures(SILLocation loc,
289290
// If we're capturing an unowned pointer by value, we will have just
290291
// loaded it into a normal retained class pointer, but we capture it as
291292
// an unowned pointer. Convert back now.
292-
if (vd->getType()->is<ReferenceStorageType>()) {
293-
auto type = getTypeLowering(vd->getType()).getLoweredType();
293+
if (var->getType()->is<ReferenceStorageType>()) {
294+
auto type = getTypeLowering(var->getType()).getLoweredType();
294295
Val = emitConversionFromSemanticValue(loc, Val, type);
295296
}
296297

@@ -700,12 +701,15 @@ static void forwardCaptureArgs(SILGenFunction &gen,
700701
case CaptureKind::None:
701702
break;
702703

703-
case CaptureKind::Constant:
704-
addSILArgument(gen.getLoweredType(vd->getType()), vd);
704+
case CaptureKind::Constant: {
705+
auto *var = dyn_cast<VarDecl>(vd);
706+
addSILArgument(gen.getLoweredType(var->getType()), vd);
705707
break;
708+
}
706709

707710
case CaptureKind::Box: {
708-
SILType ty = gen.getLoweredType(vd->getType()->getRValueType())
711+
auto *var = dyn_cast<VarDecl>(vd);
712+
SILType ty = gen.getLoweredType(var->getType()->getRValueType())
709713
.getAddressType();
710714
// Forward the captured owning box.
711715
SILType boxTy = SILType::getPrimitiveObjectType(
@@ -715,7 +719,8 @@ static void forwardCaptureArgs(SILGenFunction &gen,
715719
}
716720

717721
case CaptureKind::StorageAddress: {
718-
SILType ty = gen.getLoweredType(vd->getType()->getRValueType())
722+
auto *var = dyn_cast<VarDecl>(vd);
723+
SILType ty = gen.getLoweredType(var->getType()->getRValueType())
719724
.getAddressType();
720725
// Forward the captured value address.
721726
addSILArgument(ty, vd);

0 commit comments

Comments
 (0)