Skip to content

AST: Rename VarDecl::getType() to VarDecl::getTypeInContext() #67739

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
merged 1 commit into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5666,9 +5666,10 @@ class VarDecl : public AbstractStorageDecl {
return hasName() ? getBaseIdentifier().str() : "_";
}

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

/// Retrieve the source range of the variable type, or an invalid range if the
/// variable's type is not explicitly written in the source.
Expand Down
13 changes: 0 additions & 13 deletions lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -701,15 +701,6 @@ namespace {
if (auto *MD = dyn_cast<MacroDecl>(VD))
printGenericParameters(OS, MD->getParsedGenericParams());

if (auto *var = dyn_cast<VarDecl>(VD)) {
PrintWithColorRAII(OS, TypeColor) << " type='";
if (var->hasInterfaceType())
var->getType().print(PrintWithColorRAII(OS, TypeColor).getOS());
else
PrintWithColorRAII(OS, TypeColor) << "<null type>";
PrintWithColorRAII(OS, TypeColor) << "'";
}

if (VD->hasInterfaceType()) {
PrintWithColorRAII(OS, InterfaceTypeColor) << " interface type='";
VD->getInterfaceType()->print(
Expand Down Expand Up @@ -1004,10 +995,6 @@ namespace {
<< " apiName=" << P->getArgumentName();

if (P->hasInterfaceType()) {
PrintWithColorRAII(OS, TypeColor) << " type='";
P->getType().print(PrintWithColorRAII(OS, TypeColor).getOS());
PrintWithColorRAII(OS, TypeColor) << "'";

PrintWithColorRAII(OS, InterfaceTypeColor) << " interface type='";
P->getInterfaceType().print(
PrintWithColorRAII(OS, InterfaceTypeColor).getOS());
Expand Down
20 changes: 10 additions & 10 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5484,7 +5484,7 @@ NominalTypeDecl::getExecutorLegacyOwnedEnqueueFunction() const {

if ((params->get(0)->getSpecifier() == ParamSpecifier::LegacyOwned ||
params->get(0)->getSpecifier() == ParamSpecifier::Consuming) &&
params->get(0)->getType()->isEqual(legacyJobDecl->getDeclaredInterfaceType())) {
params->get(0)->getInterfaceType()->isEqual(legacyJobDecl->getDeclaredInterfaceType())) {
return funcDecl;
}
}
Expand Down Expand Up @@ -6820,7 +6820,7 @@ VarDecl::VarDecl(DeclKind kind, bool isStatic, VarDecl::Introducer introducer,
Bits.VarDecl.IsTopLevelGlobal = false;
}

Type VarDecl::getType() const {
Type VarDecl::getTypeInContext() const {
return getDeclContext()->mapTypeIntoContext(getInterfaceType());
}

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

static bool isPotentialCompletionHandler(const ParamDecl *param) {
if (!param->getType())
if (!param->getInterfaceType())
return false;

const AnyFunctionType *paramType = param->getType()->getAs<AnyFunctionType>();
auto *paramType = param->getInterfaceType()->getAs<AnyFunctionType>();
return paramType && paramType->getResult()->isVoid() &&
!paramType->isNoEscape() && !param->isAutoClosure();
}
Expand Down Expand Up @@ -8710,11 +8710,11 @@ AbstractFunctionDecl::findPotentialCompletionHandlerParam(
}

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

paramMatches = param->getType()->matchesParameter(asyncParam->getType(),
TypeMatchOptions());
paramMatches = param->getInterfaceType()->matchesParameter(
asyncParam->getInterfaceType(), TypeMatchOptions());
}

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

return {};
}
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1887,7 +1887,7 @@ ActorIsolation ClosureActorIsolation::getActorIsolation() const {
case ClosureActorIsolation::ActorInstance: {
auto selfDecl = getActorInstance();
auto actor =
selfDecl->getType()->getReferenceStorageReferent()->getAnyActor();
selfDecl->getTypeInContext()->getReferenceStorageReferent()->getAnyActor();
assert(actor && "Bad closure actor isolation?");
// FIXME: This could be a parameter... or a capture... hmmm.
return ActorIsolation::forActorInstanceSelf(actor).withPreconcurrency(
Expand Down
18 changes: 9 additions & 9 deletions lib/ClangImporter/ClangDerivedConformances.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ static FuncDecl *getInsertFunc(NominalTypeDecl *decl,
if (params->size() != 1)
continue;
auto param = params->front();
if (param->getType()->getCanonicalType() !=
if (param->getTypeInContext()->getCanonicalType() !=
valueType->getUnderlyingType()->getCanonicalType())
continue;
insert = candidateMethod;
Expand Down Expand Up @@ -167,8 +167,8 @@ static ValueDecl *getEqualEqualOperator(NominalTypeDecl *decl) {
auto rhs = params->get(1);
if (lhs->isInOut() || rhs->isInOut())
return false;
auto lhsTy = lhs->getType();
auto rhsTy = rhs->getType();
auto lhsTy = lhs->getTypeInContext();
auto rhsTy = rhs->getTypeInContext();
if (!lhsTy || !rhsTy)
return false;
auto lhsNominal = lhsTy->getAnyNominal();
Expand Down Expand Up @@ -197,8 +197,8 @@ static FuncDecl *getMinusOperator(NominalTypeDecl *decl) {
auto rhs = params->get(1);
if (lhs->isInOut() || rhs->isInOut())
return false;
auto lhsTy = lhs->getType();
auto rhsTy = rhs->getType();
auto lhsTy = lhs->getTypeInContext();
auto rhsTy = rhs->getTypeInContext();
if (!lhsTy || !rhsTy)
return false;
auto lhsNominal = lhsTy->getAnyNominal();
Expand Down Expand Up @@ -230,8 +230,8 @@ static FuncDecl *getPlusEqualOperator(NominalTypeDecl *decl, Type distanceTy) {
auto rhs = params->get(1);
if (rhs->isInOut())
return false;
auto lhsTy = lhs->getType();
auto rhsTy = rhs->getType();
auto lhsTy = lhs->getTypeInContext();
auto rhsTy = rhs->getTypeInContext();
if (!lhsTy || !rhsTy)
return false;
if (rhsTy->getCanonicalType() != distanceTy->getCanonicalType())
Expand Down Expand Up @@ -454,7 +454,7 @@ void swift::conformToCxxIteratorIfNeeded(
// Check if present: `var pointee: Pointee { get }`
auto pointeeId = ctx.getIdentifier("pointee");
auto pointee = lookupDirectSingleWithoutExtensions<VarDecl>(decl, pointeeId);
if (!pointee || pointee->isGetterMutating() || pointee->getType()->hasError())
if (!pointee || pointee->isGetterMutating() || pointee->getTypeInContext()->hasError())
return;

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

impl.addSynthesizedTypealias(decl, ctx.getIdentifier("Pointee"),
pointee->getType());
pointee->getTypeInContext());
if (pointeeSettable)
impl.addSynthesizedProtocolAttrs(
decl, {KnownProtocolKind::UnsafeCxxMutableInputIterator});
Expand Down
32 changes: 17 additions & 15 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4779,7 +4779,7 @@ synthesizeBaseClassMethodBody(AbstractFunctionDecl *afd, void *context) {
for (auto param : *funcDecl->getParameters()) {
auto paramRefExpr = new (ctx) DeclRefExpr(param, DeclNameLoc(),
/*Implicit=*/true);
paramRefExpr->setType(param->getType());
paramRefExpr->setType(param->getTypeInContext());
forwardingParams.push_back(paramRefExpr);
}

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

auto staticCastRefExpr = getInteropStaticCastDeclRefExpr(
ctx, baseStruct->getClangDecl()->getOwningModule(), baseType,
Expand Down Expand Up @@ -4845,7 +4845,7 @@ synthesizeBaseClassFieldGetterBody(AbstractFunctionDecl *afd, void *context) {
auto selfDecl = getterDecl->getImplicitSelfDecl();
auto selfExpr = new (ctx) DeclRefExpr(selfDecl, DeclNameLoc(),
/*implicit*/ true);
selfExpr->setType(selfDecl->getType());
selfExpr->setType(selfDecl->getTypeInContext());

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

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

auto ret = new (ctx) ReturnStmt(SourceLoc(), baseMember);
Expand Down Expand Up @@ -4914,7 +4914,7 @@ synthesizeBaseClassFieldSetterBody(AbstractFunctionDecl *afd, void *context) {
auto paramRefExpr = new (ctx) DeclRefExpr(paramDecl,
DeclNameLoc(),
/*Implicit=*/ true);
paramRefExpr->setType(paramDecl->getType());
paramRefExpr->setType(paramDecl->getTypeInContext());

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

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

auto assignExpr =
new (ctx) AssignExpr(storedRef, SourceLoc(), newValueParamRefExpr,
Expand Down Expand Up @@ -5998,14 +5998,15 @@ synthesizeDependentTypeThunkParamForwarding(AbstractFunctionDecl *afd, void *con
SmallVector<Argument, 8> forwardingParams;
unsigned paramIndex = 0;
for (auto param : *thunkDecl->getParameters()) {
if (isa<MetatypeType>(param->getType().getPointer())) {
if (isa<MetatypeType>(param->getInterfaceType().getPointer())) {
paramIndex++;
continue;
}
auto paramTy = param->getType();
auto paramTy = param->getTypeInContext();
auto isInOut = param->isInOut();
auto specParamTy =
specializedFuncDecl->getParameters()->get(paramIndex)->getType();
specializedFuncDecl->getParameters()->get(paramIndex)
->getTypeInContext();

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

Expr *resultExpr = nullptr;
if (specializedFuncCallExpr->getType()->isEqual(thunkDecl->getResultInterfaceType())) {
if (specializedFuncCallExpr->getType()->isEqual(
thunkDecl->getResultInterfaceType())) {
resultExpr = specializedFuncCallExpr;
} else {
resultExpr = ForcedCheckedCastExpr::createImplicit(
Expand All @@ -6085,7 +6087,7 @@ static ValueDecl *addThunkForDependentTypes(FuncDecl *oldDecl,
for (auto *newFnParam : *newDecl->getParameters()) {
// If the un-specialized function had a parameter with type "Any" preserve
// that parameter. Otherwise, use the new function parameter.
auto oldParamType = oldDecl->getParameters()->get(parameterIndex)->getType();
auto oldParamType = oldDecl->getParameters()->get(parameterIndex)->getInterfaceType();
if (oldParamType->isEqual(newDecl->getASTContext().getAnyExistentialType())) {
updatedAnyParams = true;
auto newParam =
Expand Down Expand Up @@ -6144,10 +6146,10 @@ synthesizeForwardingThunkBody(AbstractFunctionDecl *afd, void *context) {

SmallVector<Argument, 8> forwardingParams;
for (auto param : *thunkDecl->getParameters()) {
if (isa<MetatypeType>(param->getType().getPointer())) {
if (isa<MetatypeType>(param->getInterfaceType().getPointer())) {
continue;
}
auto paramTy = param->getType();
auto paramTy = param->getTypeInContext();
auto isInOut = param->isInOut();

Expr *paramRefExpr = new (ctx) DeclRefExpr(param, DeclNameLoc(),
Expand Down
7 changes: 4 additions & 3 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2492,7 +2492,7 @@ namespace {

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

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

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