Skip to content

Commit 2b6b663

Browse files
committed
AST: Rename VarDecl::getType() to VarDecl::getTypeInContext()
This is a futile attempt to discourage future use of getType() by giving it a "scary" name. We want people to use getInterfaceType() like with the other decl kinds.
1 parent f674e56 commit 2b6b663

File tree

67 files changed

+230
-237
lines changed

Some content is hidden

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

67 files changed

+230
-237
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5666,9 +5666,10 @@ class VarDecl : public AbstractStorageDecl {
56665666
return hasName() ? getBaseIdentifier().str() : "_";
56675667
}
56685668

5669-
/// Get the type of the variable within its context. If the context is generic,
5670-
/// this will use archetypes.
5671-
Type getType() const;
5669+
/// Maps the interface type of the variable declaration into the generic
5670+
/// environment of its parent DeclContext. Make sure this is what you want
5671+
/// and not just getInterfaceType().
5672+
Type getTypeInContext() const;
56725673

56735674
/// Retrieve the source range of the variable type, or an invalid range if the
56745675
/// variable's type is not explicitly written in the source.

lib/AST/ASTDumper.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -701,15 +701,6 @@ namespace {
701701
if (auto *MD = dyn_cast<MacroDecl>(VD))
702702
printGenericParameters(OS, MD->getParsedGenericParams());
703703

704-
if (auto *var = dyn_cast<VarDecl>(VD)) {
705-
PrintWithColorRAII(OS, TypeColor) << " type='";
706-
if (var->hasInterfaceType())
707-
var->getType().print(PrintWithColorRAII(OS, TypeColor).getOS());
708-
else
709-
PrintWithColorRAII(OS, TypeColor) << "<null type>";
710-
PrintWithColorRAII(OS, TypeColor) << "'";
711-
}
712-
713704
if (VD->hasInterfaceType()) {
714705
PrintWithColorRAII(OS, InterfaceTypeColor) << " interface type='";
715706
VD->getInterfaceType()->print(
@@ -1004,10 +995,6 @@ namespace {
1004995
<< " apiName=" << P->getArgumentName();
1005996

1006997
if (P->hasInterfaceType()) {
1007-
PrintWithColorRAII(OS, TypeColor) << " type='";
1008-
P->getType().print(PrintWithColorRAII(OS, TypeColor).getOS());
1009-
PrintWithColorRAII(OS, TypeColor) << "'";
1010-
1011998
PrintWithColorRAII(OS, InterfaceTypeColor) << " interface type='";
1012999
P->getInterfaceType().print(
10131000
PrintWithColorRAII(OS, InterfaceTypeColor).getOS());

lib/AST/Decl.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5484,7 +5484,7 @@ NominalTypeDecl::getExecutorLegacyOwnedEnqueueFunction() const {
54845484

54855485
if ((params->get(0)->getSpecifier() == ParamSpecifier::LegacyOwned ||
54865486
params->get(0)->getSpecifier() == ParamSpecifier::Consuming) &&
5487-
params->get(0)->getType()->isEqual(legacyJobDecl->getDeclaredInterfaceType())) {
5487+
params->get(0)->getInterfaceType()->isEqual(legacyJobDecl->getDeclaredInterfaceType())) {
54885488
return funcDecl;
54895489
}
54905490
}
@@ -6820,7 +6820,7 @@ VarDecl::VarDecl(DeclKind kind, bool isStatic, VarDecl::Introducer introducer,
68206820
Bits.VarDecl.IsTopLevelGlobal = false;
68216821
}
68226822

6823-
Type VarDecl::getType() const {
6823+
Type VarDecl::getTypeInContext() const {
68246824
return getDeclContext()->mapTypeIntoContext(getInterfaceType());
68256825
}
68266826

@@ -7387,7 +7387,7 @@ LifetimeAnnotation ParamDecl::getLifetimeAnnotation() const {
73877387
auto specifier = getSpecifier();
73887388
// Copyable parameters which are consumed have eager-move semantics.
73897389
if (specifier == ParamDecl::Specifier::Consuming &&
7390-
!getType()->isPureMoveOnly()) {
7390+
!getTypeInContext()->isPureMoveOnly()) {
73917391
if (getAttrs().hasAttribute<NoEagerMoveAttr>())
73927392
return LifetimeAnnotation::Lexical;
73937393
return LifetimeAnnotation::EagerMove;
@@ -8637,10 +8637,10 @@ AbstractFunctionDecl *AbstractFunctionDecl::getAsyncAlternative() const {
86378637
}
86388638

86398639
static bool isPotentialCompletionHandler(const ParamDecl *param) {
8640-
if (!param->getType())
8640+
if (!param->getInterfaceType())
86418641
return false;
86428642

8643-
const AnyFunctionType *paramType = param->getType()->getAs<AnyFunctionType>();
8643+
auto *paramType = param->getInterfaceType()->getAs<AnyFunctionType>();
86448644
return paramType && paramType->getResult()->isVoid() &&
86458645
!paramType->isNoEscape() && !param->isAutoClosure();
86468646
}
@@ -8710,11 +8710,11 @@ AbstractFunctionDecl::findPotentialCompletionHandlerParam(
87108710
}
87118711

87128712
// Don't have types for some reason, just return no match
8713-
if (!param->getType() || !asyncParam->getType())
8713+
if (!param->getInterfaceType() || !asyncParam->getInterfaceType())
87148714
return llvm::None;
87158715

8716-
paramMatches = param->getType()->matchesParameter(asyncParam->getType(),
8717-
TypeMatchOptions());
8716+
paramMatches = param->getInterfaceType()->matchesParameter(
8717+
asyncParam->getInterfaceType(), TypeMatchOptions());
87188718
}
87198719

87208720
if (paramMatches) {
@@ -9618,7 +9618,7 @@ LifetimeAnnotation FuncDecl::getLifetimeAnnotation() const {
96189618
// Copyable parameters which are consumed have eager-move semantics.
96199619
if (getSelfAccessKind() == SelfAccessKind::Consuming) {
96209620
auto *selfDecl = getImplicitSelfDecl();
9621-
if (selfDecl && !selfDecl->getType()->isPureMoveOnly()) {
9621+
if (selfDecl && !selfDecl->getTypeInContext()->isPureMoveOnly()) {
96229622
if (getAttrs().hasAttribute<NoEagerMoveAttr>())
96239623
return LifetimeAnnotation::Lexical;
96249624
return LifetimeAnnotation::EagerMove;
@@ -10047,7 +10047,7 @@ Type TypeBase::getSwiftNewtypeUnderlyingType() {
1004710047
for (auto member : structDecl->getMembers())
1004810048
if (auto varDecl = dyn_cast<VarDecl>(member))
1004910049
if (varDecl->getName() == getASTContext().Id_rawValue)
10050-
return varDecl->getType();
10050+
return varDecl->getInterfaceType();
1005110051

1005210052
return {};
1005310053
}

lib/AST/Expr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1887,7 +1887,7 @@ ActorIsolation ClosureActorIsolation::getActorIsolation() const {
18871887
case ClosureActorIsolation::ActorInstance: {
18881888
auto selfDecl = getActorInstance();
18891889
auto actor =
1890-
selfDecl->getType()->getReferenceStorageReferent()->getAnyActor();
1890+
selfDecl->getTypeInContext()->getReferenceStorageReferent()->getAnyActor();
18911891
assert(actor && "Bad closure actor isolation?");
18921892
// FIXME: This could be a parameter... or a capture... hmmm.
18931893
return ActorIsolation::forActorInstanceSelf(actor).withPreconcurrency(

lib/ClangImporter/ClangDerivedConformances.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ static FuncDecl *getInsertFunc(NominalTypeDecl *decl,
101101
if (params->size() != 1)
102102
continue;
103103
auto param = params->front();
104-
if (param->getType()->getCanonicalType() !=
104+
if (param->getTypeInContext()->getCanonicalType() !=
105105
valueType->getUnderlyingType()->getCanonicalType())
106106
continue;
107107
insert = candidateMethod;
@@ -167,8 +167,8 @@ static ValueDecl *getEqualEqualOperator(NominalTypeDecl *decl) {
167167
auto rhs = params->get(1);
168168
if (lhs->isInOut() || rhs->isInOut())
169169
return false;
170-
auto lhsTy = lhs->getType();
171-
auto rhsTy = rhs->getType();
170+
auto lhsTy = lhs->getTypeInContext();
171+
auto rhsTy = rhs->getTypeInContext();
172172
if (!lhsTy || !rhsTy)
173173
return false;
174174
auto lhsNominal = lhsTy->getAnyNominal();
@@ -197,8 +197,8 @@ static FuncDecl *getMinusOperator(NominalTypeDecl *decl) {
197197
auto rhs = params->get(1);
198198
if (lhs->isInOut() || rhs->isInOut())
199199
return false;
200-
auto lhsTy = lhs->getType();
201-
auto rhsTy = rhs->getType();
200+
auto lhsTy = lhs->getTypeInContext();
201+
auto rhsTy = rhs->getTypeInContext();
202202
if (!lhsTy || !rhsTy)
203203
return false;
204204
auto lhsNominal = lhsTy->getAnyNominal();
@@ -230,8 +230,8 @@ static FuncDecl *getPlusEqualOperator(NominalTypeDecl *decl, Type distanceTy) {
230230
auto rhs = params->get(1);
231231
if (rhs->isInOut())
232232
return false;
233-
auto lhsTy = lhs->getType();
234-
auto rhsTy = rhs->getType();
233+
auto lhsTy = lhs->getTypeInContext();
234+
auto rhsTy = rhs->getTypeInContext();
235235
if (!lhsTy || !rhsTy)
236236
return false;
237237
if (rhsTy->getCanonicalType() != distanceTy->getCanonicalType())
@@ -454,7 +454,7 @@ void swift::conformToCxxIteratorIfNeeded(
454454
// Check if present: `var pointee: Pointee { get }`
455455
auto pointeeId = ctx.getIdentifier("pointee");
456456
auto pointee = lookupDirectSingleWithoutExtensions<VarDecl>(decl, pointeeId);
457-
if (!pointee || pointee->isGetterMutating() || pointee->getType()->hasError())
457+
if (!pointee || pointee->isGetterMutating() || pointee->getTypeInContext()->hasError())
458458
return;
459459

460460
// Check if `var pointee: Pointee` is settable. This is required for the
@@ -498,7 +498,7 @@ void swift::conformToCxxIteratorIfNeeded(
498498
return;
499499

500500
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("Pointee"),
501-
pointee->getType());
501+
pointee->getTypeInContext());
502502
if (pointeeSettable)
503503
impl.addSynthesizedProtocolAttrs(
504504
decl, {KnownProtocolKind::UnsafeCxxMutableInputIterator});

lib/ClangImporter/ClangImporter.cpp

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4779,7 +4779,7 @@ synthesizeBaseClassMethodBody(AbstractFunctionDecl *afd, void *context) {
47794779
for (auto param : *funcDecl->getParameters()) {
47804780
auto paramRefExpr = new (ctx) DeclRefExpr(param, DeclNameLoc(),
47814781
/*Implicit=*/true);
4782-
paramRefExpr->setType(param->getType());
4782+
paramRefExpr->setType(param->getTypeInContext());
47834783
forwardingParams.push_back(paramRefExpr);
47844784
}
47854785

@@ -4791,7 +4791,7 @@ synthesizeBaseClassMethodBody(AbstractFunctionDecl *afd, void *context) {
47914791
auto *selfDecl = funcDecl->getImplicitSelfDecl();
47924792
auto selfExpr = new (ctx) DeclRefExpr(selfDecl, DeclNameLoc(),
47934793
/*implicit*/ true);
4794-
selfExpr->setType(selfDecl->getType());
4794+
selfExpr->setType(selfDecl->getTypeInContext());
47954795

47964796
auto staticCastRefExpr = getInteropStaticCastDeclRefExpr(
47974797
ctx, baseStruct->getClangDecl()->getOwningModule(), baseType,
@@ -4845,7 +4845,7 @@ synthesizeBaseClassFieldGetterBody(AbstractFunctionDecl *afd, void *context) {
48454845
auto selfDecl = getterDecl->getImplicitSelfDecl();
48464846
auto selfExpr = new (ctx) DeclRefExpr(selfDecl, DeclNameLoc(),
48474847
/*implicit*/ true);
4848-
selfExpr->setType(selfDecl->getType());
4848+
selfExpr->setType(selfDecl->getTypeInContext());
48494849

48504850
auto staticCastRefExpr = getInteropStaticCastDeclRefExpr(
48514851
ctx, baseStruct->getClangDecl()->getOwningModule(),
@@ -4863,7 +4863,7 @@ synthesizeBaseClassFieldGetterBody(AbstractFunctionDecl *afd, void *context) {
48634863
auto paramRefExpr = new (ctx) DeclRefExpr(paramDecl,
48644864
DeclNameLoc(),
48654865
/*Implicit=*/ true);
4866-
paramRefExpr->setType(paramDecl->getType());
4866+
paramRefExpr->setType(paramDecl->getTypeInContext());
48674867

48684868
auto *argList = ArgumentList::forImplicitUnlabeled(ctx, {paramRefExpr});
48694869
baseMember = SubscriptExpr::create(ctx, casted, argList, subscript);
@@ -4878,7 +4878,7 @@ synthesizeBaseClassFieldGetterBody(AbstractFunctionDecl *afd, void *context) {
48784878
baseMember =
48794879
new (ctx) MemberRefExpr(casted, SourceLoc(), baseClassVar, DeclNameLoc(),
48804880
/*Implicit=*/true, accessKind);
4881-
baseMember->setType(cast<VarDecl>(baseClassVar)->getType());
4881+
baseMember->setType(cast<VarDecl>(baseClassVar)->getTypeInContext());
48824882
}
48834883

48844884
auto ret = new (ctx) ReturnStmt(SourceLoc(), baseMember);
@@ -4914,7 +4914,7 @@ synthesizeBaseClassFieldSetterBody(AbstractFunctionDecl *afd, void *context) {
49144914
auto paramRefExpr = new (ctx) DeclRefExpr(paramDecl,
49154915
DeclNameLoc(),
49164916
/*Implicit=*/ true);
4917-
paramRefExpr->setType(paramDecl->getType());
4917+
paramRefExpr->setType(paramDecl->getTypeInContext());
49184918

49194919
auto *argList = ArgumentList::forImplicitUnlabeled(ctx, {paramRefExpr});
49204920
storedRef = SubscriptExpr::create(ctx, pointeePropertyRefExpr, argList, subscript);
@@ -4930,13 +4930,13 @@ synthesizeBaseClassFieldSetterBody(AbstractFunctionDecl *afd, void *context) {
49304930
storedRef =
49314931
new (ctx) MemberRefExpr(pointeePropertyRefExpr, SourceLoc(), baseClassVar,
49324932
DeclNameLoc(), /*Implicit=*/true, accessKind);
4933-
storedRef->setType(LValueType::get(cast<VarDecl>(baseClassVar)->getType()));
4933+
storedRef->setType(LValueType::get(cast<VarDecl>(baseClassVar)->getTypeInContext()));
49344934
}
49354935

49364936
auto newValueParamRefExpr =
49374937
new (ctx) DeclRefExpr(setterDecl->getParameters()->get(0), DeclNameLoc(),
49384938
/*Implicit=*/true);
4939-
newValueParamRefExpr->setType(setterDecl->getParameters()->get(0)->getType());
4939+
newValueParamRefExpr->setType(setterDecl->getParameters()->get(0)->getTypeInContext());
49404940

49414941
auto assignExpr =
49424942
new (ctx) AssignExpr(storedRef, SourceLoc(), newValueParamRefExpr,
@@ -5998,14 +5998,15 @@ synthesizeDependentTypeThunkParamForwarding(AbstractFunctionDecl *afd, void *con
59985998
SmallVector<Argument, 8> forwardingParams;
59995999
unsigned paramIndex = 0;
60006000
for (auto param : *thunkDecl->getParameters()) {
6001-
if (isa<MetatypeType>(param->getType().getPointer())) {
6001+
if (isa<MetatypeType>(param->getInterfaceType().getPointer())) {
60026002
paramIndex++;
60036003
continue;
60046004
}
6005-
auto paramTy = param->getType();
6005+
auto paramTy = param->getTypeInContext();
60066006
auto isInOut = param->isInOut();
60076007
auto specParamTy =
6008-
specializedFuncDecl->getParameters()->get(paramIndex)->getType();
6008+
specializedFuncDecl->getParameters()->get(paramIndex)
6009+
->getTypeInContext();
60096010

60106011
Expr *paramRefExpr = new (ctx) DeclRefExpr(param, DeclNameLoc(),
60116012
/*Implicit=*/true);
@@ -6059,7 +6060,8 @@ synthesizeDependentTypeThunkParamForwarding(AbstractFunctionDecl *afd, void *con
60596060
specializedFuncCallExpr->setThrows(false);
60606061

60616062
Expr *resultExpr = nullptr;
6062-
if (specializedFuncCallExpr->getType()->isEqual(thunkDecl->getResultInterfaceType())) {
6063+
if (specializedFuncCallExpr->getType()->isEqual(
6064+
thunkDecl->getResultInterfaceType())) {
60636065
resultExpr = specializedFuncCallExpr;
60646066
} else {
60656067
resultExpr = ForcedCheckedCastExpr::createImplicit(
@@ -6085,7 +6087,7 @@ static ValueDecl *addThunkForDependentTypes(FuncDecl *oldDecl,
60856087
for (auto *newFnParam : *newDecl->getParameters()) {
60866088
// If the un-specialized function had a parameter with type "Any" preserve
60876089
// that parameter. Otherwise, use the new function parameter.
6088-
auto oldParamType = oldDecl->getParameters()->get(parameterIndex)->getType();
6090+
auto oldParamType = oldDecl->getParameters()->get(parameterIndex)->getInterfaceType();
60896091
if (oldParamType->isEqual(newDecl->getASTContext().getAnyExistentialType())) {
60906092
updatedAnyParams = true;
60916093
auto newParam =
@@ -6144,10 +6146,10 @@ synthesizeForwardingThunkBody(AbstractFunctionDecl *afd, void *context) {
61446146

61456147
SmallVector<Argument, 8> forwardingParams;
61466148
for (auto param : *thunkDecl->getParameters()) {
6147-
if (isa<MetatypeType>(param->getType().getPointer())) {
6149+
if (isa<MetatypeType>(param->getInterfaceType().getPointer())) {
61486150
continue;
61496151
}
6150-
auto paramTy = param->getType();
6152+
auto paramTy = param->getTypeInContext();
61516153
auto isInOut = param->isInOut();
61526154

61536155
Expr *paramRefExpr = new (ctx) DeclRefExpr(param, DeclNameLoc(),

lib/ClangImporter/ImportDecl.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2492,7 +2492,7 @@ namespace {
24922492

24932493
// If we have a getter and a setter make sure the types line up.
24942494
if (setter && !getter->getResultInterfaceType()->isEqual(
2495-
setter->getParameters()->get(0)->getType()))
2495+
setter->getParameters()->get(0)->getTypeInContext()))
24962496
continue;
24972497

24982498
// If the name that we would import this as already exists, then don't
@@ -3528,7 +3528,7 @@ namespace {
35283528
assert(func->getParameters()->size() == 1);
35293529
auto typeDecl = dc->getSelfNominalTypeDecl();
35303530
auto parameter = func->getParameters()->get(0);
3531-
auto parameterType = parameter->getType();
3531+
auto parameterType = parameter->getTypeInContext();
35323532
if (!typeDecl || !parameterType)
35333533
return nullptr;
35343534
if (parameter->isInOut())
@@ -6913,7 +6913,8 @@ SwiftDeclConverter::importSubscript(Decl *decl,
69136913

69146914
// Make sure that the index types are equivalent.
69156915
// FIXME: Rectify these the same way we do for element types.
6916-
if (!setterIndex->getType()->isEqual(getterIndex->getType())) {
6916+
if (!setterIndex->getInterfaceType()->isEqual(
6917+
getterIndex->getInterfaceType())) {
69176918
// If there is an existing subscript operation, we're done.
69186919
if (existingSubscript)
69196920
return decl == getter ? existingSubscript : nullptr;

0 commit comments

Comments
 (0)