Skip to content

Remove some usages of InOutType #17043

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
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
17 changes: 10 additions & 7 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2137,7 +2137,7 @@ static void printParameterFlags(ASTPrinter &printer, PrintOptions options,
/* nothing */
break;
case ValueOwnership::InOut:
/* handled as part of an InOutType */
printer << "inout ";
break;
case ValueOwnership::Shared:
printer << "__shared ";
Expand Down Expand Up @@ -2266,12 +2266,15 @@ void PrintAST::printOneParameter(const ParamDecl *param,
// through the paren types so that we don't print excessive @escapings.
unsigned numParens = 0;
if (!willUseTypeReprPrinting(TheTypeLoc, CurrentType, Options)) {
auto type = TheTypeLoc.getType()->getInOutObjectType();

printParameterFlags(Printer, Options, paramFlags);
while (auto parenTy =
dyn_cast<ParenType>(TheTypeLoc.getType().getPointer())) {
while (auto parenTy = dyn_cast<ParenType>(type.getPointer())) {
++numParens;
TheTypeLoc = TypeLoc::withoutLoc(parenTy->getUnderlyingType());
type = parenTy->getUnderlyingType();
}

TheTypeLoc = TypeLoc::withoutLoc(type);
}

for (unsigned i = 0; i < numParens; ++i)
Expand Down Expand Up @@ -3252,7 +3255,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
void visitParenType(ParenType *T) {
Printer << "(";
printParameterFlags(Printer, Options, T->getParameterFlags());
visit(T->getUnderlyingType());
visit(T->getUnderlyingType()->getInOutObjectType());
Printer << ")";
}

Expand All @@ -3267,7 +3270,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
if (i)
Printer << ", ";
const TupleTypeElt &TD = Fields[i];
Type EltType = TD.getType();
Type EltType = TD.getType()->getInOutObjectType();

Printer.callPrintStructurePre(PrintStructureKind::TupleElement);
SWIFT_DEFER {
Expand Down Expand Up @@ -3540,7 +3543,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
}

printParameterFlags(Printer, Options, Param.getParameterFlags());
visit(Param.getType());
visit(Param.getPlainType());
if (Param.isVariadic())
Printer << "...";
}
Expand Down
15 changes: 7 additions & 8 deletions lib/AST/ASTVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2255,15 +2255,15 @@ class Verifier : public ASTWalker {

// Variables must have materializable type, unless they are parameters,
// in which case they must either have l-value type or be anonymous.
if (!var->getInterfaceType()->isMaterializable() || var->isInOut()) {
if (!var->getInterfaceType()->isMaterializable()) {
if (!isa<ParamDecl>(var)) {
Out << "Non-parameter VarDecl has non-materializable type: ";
Out << "VarDecl has non-materializable type: ";
var->getType().print(Out);
Out << "\n";
abort();
}

if (!var->getInterfaceType()->is<InOutType>() && var->hasName()) {
if (!var->isInOut() && var->hasName()) {
Out << "ParamDecl may only have non-materializable tuple type "
"when it is anonymous: ";
var->getType().print(Out);
Expand Down Expand Up @@ -2340,8 +2340,7 @@ class Verifier : public ASTWalker {

if (var->getAttrs().hasAttribute<ImplicitlyUnwrappedOptionalAttr>()) {
auto varTy = var->getInterfaceType()
->getReferenceStorageReferent()
->getWithoutSpecifierType();
->getReferenceStorageReferent();

// FIXME: Update to look for plain Optional once
// ImplicitlyUnwrappedOptional is removed
Expand Down Expand Up @@ -2906,13 +2905,13 @@ class Verifier : public ASTWalker {
abort();
}
const ParamDecl *selfParam = FD->getImplicitSelfDecl();
if (!selfParam->getInterfaceType()->is<InOutType>()) {
if (!selfParam->isInOut()) {
Out << "mutating function does not have inout 'self'\n";
abort();
}
} else {
const ParamDecl *selfParam = FD->getImplicitSelfDecl();
if (selfParam && selfParam->getInterfaceType()->is<InOutType>()) {
if (selfParam && selfParam->isInOut()) {
Out << "non-mutating function has inout 'self'\n";
abort();
}
Expand All @@ -2925,7 +2924,7 @@ class Verifier : public ASTWalker {
abort();
}

auto resultTy = FD->getResultInterfaceType()->getWithoutSpecifierType();
auto resultTy = FD->getResultInterfaceType();

// FIXME: Update to look for plain Optional once
// ImplicitlyUnwrappedOptional is removed
Expand Down
17 changes: 3 additions & 14 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1960,14 +1960,7 @@ bool ValueDecl::hasInterfaceType() const {

Type ValueDecl::getInterfaceType() const {
assert(hasInterfaceType() && "No interface type was set");
auto ty = TypeAndAccess.getPointer();
// FIXME(Remove InOutType): This grossness will go away when Sema is weaned
// off of InOutType. Until then we should respect our parameter flags and
// return the type it expects.
if (auto *VD = dyn_cast<ParamDecl>(this)) {
ty = VD->isInOut() ? InOutType::get(ty) : ty;
}
return ty;
return TypeAndAccess.getPointer();
}

void ValueDecl::setInterfaceType(Type type) {
Expand Down Expand Up @@ -4072,13 +4065,9 @@ Type VarDecl::getType() const {
if (!typeInContext) {
const_cast<VarDecl *>(this)->typeInContext =
getDeclContext()->mapTypeIntoContext(
getInterfaceType())->getInOutObjectType();
getInterfaceType());
}

// FIXME(Remove InOutType): This grossness will go away when Sema is weaned
// off of InOutType. Until then we should respect our parameter flags and
// return the type it expects.
if (isInOut()) return InOutType::get(typeInContext);
return typeInContext;
}

Expand Down Expand Up @@ -4380,7 +4369,7 @@ ParamDecl::ParamDecl(ParamDecl *PD, bool withTypes)
: VarDecl(DeclKind::Param, /*IsStatic*/false, PD->getSpecifier(),
/*IsCaptureList*/false, PD->getNameLoc(), PD->getName(),
PD->hasType() && withTypes
? PD->getType()->getInOutObjectType()
? PD->getType()
: Type(),
PD->getDeclContext()),
ArgumentName(PD->getArgumentName()),
Expand Down
8 changes: 4 additions & 4 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ makeEnumRawValueConstructor(ClangImporter::Implementation &Impl,
return ctorDecl;

auto selfRef = new (C) DeclRefExpr(selfDecl, DeclNameLoc(), /*implicit*/true);
selfRef->setType(LValueType::get(selfDecl->getType()->getInOutObjectType()));
selfRef->setType(LValueType::get(selfDecl->getType()));
selfRef->propagateLValueAccessKind(AccessKind::Write);

auto paramRef = new (C) DeclRefExpr(param, DeclNameLoc(),
Expand Down Expand Up @@ -1308,7 +1308,7 @@ createValueConstructor(ClangImporter::Implementation &Impl,
// Construct left-hand side.
Expr *lhs = new (context) DeclRefExpr(selfDecl, DeclNameLoc(),
/*Implicit=*/true);
lhs->setType(LValueType::get(selfDecl->getType()->getInOutObjectType()));
lhs->setType(LValueType::get(selfDecl->getType()));

auto semantics = (var->hasStorage()
? AccessSemantics::DirectToStorage
Expand Down Expand Up @@ -1460,7 +1460,7 @@ static ConstructorDecl *createRawValueBridgingConstructor(
// Construct left-hand side.
Expr *lhs = new (ctx) DeclRefExpr(selfDecl, DeclNameLoc(),
/*Implicit=*/true);
lhs->setType(LValueType::get(selfDecl->getType()->getInOutObjectType()));
lhs->setType(LValueType::get(selfDecl->getType()));

lhs = new (ctx) MemberRefExpr(lhs, SourceLoc(), storedRawValue,
DeclNameLoc(), /*Implicit=*/true,
Expand Down Expand Up @@ -5663,7 +5663,7 @@ Decl *SwiftDeclConverter::importGlobalAsInitializer(
Type selfType = selfParam->getType();
Type initType = FunctionType::get(selfType, fnType);
result->setInitializerInterfaceType(initType);
Type selfMetaType = MetatypeType::get(selfType->getInOutObjectType());
Type selfMetaType = MetatypeType::get(selfType);
Type allocType = FunctionType::get(selfMetaType, fnType);
result->setInterfaceType(allocType);
Impl.recordImplicitUnwrapForDecl(result,
Expand Down
8 changes: 4 additions & 4 deletions lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2104,10 +2104,10 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
setClangDeclKeywords(VD, Pairs, Builder);
// Add a type annotation.
Type VarType = getTypeOfMember(VD);
if (VD->getName() == Ctx.Id_self) {
// Strip inout from 'self'. It is useful to show inout for function
// parameters. But for 'self' it is just noise.
VarType = VarType->getInOutObjectType();
if (VD->getName() != Ctx.Id_self && VD->isInOut()) {
// It is useful to show inout for function parameters.
// But for 'self' it is just noise.
VarType = InOutType::get(VarType);
}
auto DynamicOrOptional =
IsDynamicLookup || VD->getAttrs().hasAttribute<OptionalAttr>();
Expand Down
7 changes: 1 addition & 6 deletions lib/IRGen/DebugTypeInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,11 @@ DebugTypeInfo DebugTypeInfo::getFromTypeInfo(DeclContext *DC,
DebugTypeInfo DebugTypeInfo::getLocalVariable(DeclContext *DC,
GenericEnvironment *GE,
VarDecl *Decl, swift::Type Ty,
const TypeInfo &Info,
bool Unwrap) {
const TypeInfo &Info) {

auto DeclType =
Decl->hasInterfaceType() ? Decl->getInterfaceType() : Decl->getType();
auto RealType = Ty;
if (Unwrap) {
DeclType = DeclType->getInOutObjectType();
RealType = RealType->getInOutObjectType();
}

// DynamicSelfType is also sugar as far as debug info is concerned.
auto Sugared = DeclType;
Expand Down
7 changes: 1 addition & 6 deletions lib/IRGen/DebugTypeInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ class DebugTypeInfo {
/// Create type for a local variable.
static DebugTypeInfo getLocalVariable(DeclContext *DeclCtx,
GenericEnvironment *GE, VarDecl *Decl,
swift::Type Ty, const TypeInfo &Info,
bool Unwrap);
swift::Type Ty, const TypeInfo &Info);
/// Create type for an artificial metadata variable.
static DebugTypeInfo getMetadata(swift::Type Ty, llvm::Type *StorageTy,
Size size, Alignment align);
Expand All @@ -84,10 +83,6 @@ class DebugTypeInfo {
DeclContext *getDeclContext() const { return DeclCtx; }
GenericEnvironment *getGenericEnvironment() const { return GenericEnv; }

void unwrapLValueOrInOutType() {
Type = Type->getWithoutSpecifierType().getPointer();
}

// Determine whether this type is an Archetype itself.
bool isArchetype() const {
return Type->getWithoutSpecifierType()->is<ArchetypeType>();
Expand Down
16 changes: 5 additions & 11 deletions lib/IRGen/IRGenSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3671,7 +3671,7 @@ void IRGenSILFunction::visitDebugValueInst(DebugValueInst *i) {
if (VarDecl *Decl = i->getDecl()) {
DbgTy = DebugTypeInfo::getLocalVariable(
CurSILFn->getDeclContext(), CurSILFn->getGenericEnvironment(), Decl,
RealTy, getTypeInfo(SILVal->getType()), /*Unwrap=*/false);
RealTy, getTypeInfo(SILVal->getType()));
} else if (i->getFunction()->isBare() &&
!SILTy.hasArchetype() && !Name.empty()) {
// Preliminary support for .sil debug information.
Expand Down Expand Up @@ -3713,16 +3713,10 @@ void IRGenSILFunction::visitDebugValueAddrInst(DebugValueAddrInst *i) {
auto Addr = getLoweredAddress(SILVal).getAddress();
SILType SILTy = SILVal->getType();
auto RealType = SILTy.getASTType();
// Unwrap implicitly indirect types and types that are passed by
// reference only at the SIL level and below.
//
// FIXME: Should this check if the lowered SILType is address only
// instead? Otherwise optionals of archetypes etc will still have
// 'Unwrap' set to false.
bool Unwrap = VarInfo->Constant || SILTy.is<ArchetypeType>();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dcci @adrian-prantl It looks like this wasn't doing anything, so I removed it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine to me if it sticks together.


auto DbgTy = DebugTypeInfo::getLocalVariable(
CurSILFn->getDeclContext(), CurSILFn->getGenericEnvironment(), Decl,
RealType, getTypeInfo(SILVal->getType()), Unwrap);
RealType, getTypeInfo(SILVal->getType()));
bindArchetypes(DbgTy.getType());
if (!IGM.DebugInfo)
return;
Expand Down Expand Up @@ -4011,7 +4005,7 @@ void IRGenSILFunction::emitDebugInfoForAllocStack(AllocStackInst *i,
auto RealType = SILTy.getASTType();
auto DbgTy = DebugTypeInfo::getLocalVariable(
CurSILFn->getDeclContext(), CurSILFn->getGenericEnvironment(), Decl,
RealType, type, false);
RealType, type);

// FIXME: This is working around the inverse special case in LLDB.
if (DbgTy.isImplicitlyIndirect())
Expand Down Expand Up @@ -4208,7 +4202,7 @@ void IRGenSILFunction::visitAllocBoxInst(swift::AllocBoxInst *i) {
auto RealType = SILTy.getASTType();
auto DbgTy = DebugTypeInfo::getLocalVariable(
CurSILFn->getDeclContext(), CurSILFn->getGenericEnvironment(), Decl,
RealType, type, /*Unwrap=*/false);
RealType, type);

if (isInlinedGeneric(Decl, i->getDebugScope()))
return;
Expand Down
4 changes: 1 addition & 3 deletions lib/IRGen/MetadataRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -891,9 +891,7 @@ namespace {
}

llvm::Value *getFunctionParameterRef(AnyFunctionType::CanParam &param) {
auto type = param.getType();
if (param.getParameterFlags().isInOut())
type = type->getInOutObjectType()->getCanonicalType();
auto type = param.getPlainType()->getCanonicalType();
return IGF.emitAbstractTypeMetadataRef(type);
}

Expand Down
1 change: 0 additions & 1 deletion lib/SIL/AbstractionPattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ AbstractionPattern TypeConverter::getAbstractionPattern(VarDecl *var) {
genericSig = sig->getCanonicalSignature();

CanType swiftType = var->getInterfaceType()
->getInOutObjectType()
->getCanonicalType();

if (auto clangDecl = var->getClangDecl()) {
Expand Down
10 changes: 8 additions & 2 deletions lib/SIL/SILFunctionType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -686,10 +686,16 @@ class DestructureInputs {

unsigned origParamIndex = NextOrigParamIndex++;

bool isInout = false;
if (auto inoutType = dyn_cast<InOutType>(substType)) {
isInout = true;
substType = inoutType.getObjectType();
origType = origType.getWithoutSpecifierType();
}

auto &substTL = M.Types.getTypeLowering(origType, substType);
ParameterConvention convention;
if (isa<InOutType>(substType)) {
assert(origType.isTypeParameter() || origType.getAs<InOutType>());
if (isInout) {
convention = ParameterConvention::Indirect_Inout;
} else if (isFormallyPassedIndirectly(origType, substType, substTL)) {
if (forSelf && rep == SILFunctionTypeRepresentation::WitnessMethod)
Expand Down
21 changes: 3 additions & 18 deletions lib/SIL/TypeLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,7 @@ TypeConverter::~TypeConverter() {
CanType srcType = ti.first.OrigType;
if (!srcType) continue;
CanType mappedType = ti.second->getLoweredType().getASTType();
if (srcType == mappedType || isa<InOutType>(srcType))
if (srcType == mappedType)
ti.second->~TypeLowering();
}
}
Expand Down Expand Up @@ -1440,26 +1440,11 @@ TypeConverter::getTypeLowering(AbstractionPattern origType,

assert((!key.isDependent() || getCurGenericContext())
&& "dependent type outside of generic context?!");

assert(!substType->is<InOutType>());

if (auto existing = find(key))
return *existing;

// inout types are a special case for lowering, because they get
// completely removed and represented as 'address' SILTypes.
if (isa<InOutType>(substType)) {
// Derive SILType for InOutType from the object type.
CanType substObjectType = substType.getWithoutSpecifierType();
AbstractionPattern origObjectType = origType.getWithoutSpecifierType();

SILType loweredType = getLoweredType(origObjectType, substObjectType)
.getAddressType();

auto *theInfo = new (*this, key.isDependent())
TrivialTypeLowering(loweredType);
insert(key, theInfo);
return *theInfo;
}

// Lower the type.
CanType loweredSubstType =
getLoweredRValueType(origType, substType);
Expand Down
5 changes: 0 additions & 5 deletions lib/SILGen/RValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,6 @@ class RValue {
/// True if this rvalue was emitted into context.
bool isInContext() const & { return elementsToBeAdded == InContext; }

/// True if this represents an lvalue.
bool isLValue() const & {
return isa<InOutType>(type);
}

/// Add an element to the rvalue. The rvalue must not yet be complete.
void addElement(RValue &&element) &;

Expand Down
Loading