Skip to content

Commit cd5d738

Browse files
authored
Merge pull request #17043 from slavapestov/small-progress-on-killing-inout-type
Remove some usages of InOutType
2 parents 6c33cfa + dbc8d5e commit cd5d738

24 files changed

+130
-144
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2137,7 +2137,7 @@ static void printParameterFlags(ASTPrinter &printer, PrintOptions options,
21372137
/* nothing */
21382138
break;
21392139
case ValueOwnership::InOut:
2140-
/* handled as part of an InOutType */
2140+
printer << "inout ";
21412141
break;
21422142
case ValueOwnership::Shared:
21432143
printer << "__shared ";
@@ -2266,12 +2266,15 @@ void PrintAST::printOneParameter(const ParamDecl *param,
22662266
// through the paren types so that we don't print excessive @escapings.
22672267
unsigned numParens = 0;
22682268
if (!willUseTypeReprPrinting(TheTypeLoc, CurrentType, Options)) {
2269+
auto type = TheTypeLoc.getType()->getInOutObjectType();
2270+
22692271
printParameterFlags(Printer, Options, paramFlags);
2270-
while (auto parenTy =
2271-
dyn_cast<ParenType>(TheTypeLoc.getType().getPointer())) {
2272+
while (auto parenTy = dyn_cast<ParenType>(type.getPointer())) {
22722273
++numParens;
2273-
TheTypeLoc = TypeLoc::withoutLoc(parenTy->getUnderlyingType());
2274+
type = parenTy->getUnderlyingType();
22742275
}
2276+
2277+
TheTypeLoc = TypeLoc::withoutLoc(type);
22752278
}
22762279

22772280
for (unsigned i = 0; i < numParens; ++i)
@@ -3252,7 +3255,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
32523255
void visitParenType(ParenType *T) {
32533256
Printer << "(";
32543257
printParameterFlags(Printer, Options, T->getParameterFlags());
3255-
visit(T->getUnderlyingType());
3258+
visit(T->getUnderlyingType()->getInOutObjectType());
32563259
Printer << ")";
32573260
}
32583261

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

32723275
Printer.callPrintStructurePre(PrintStructureKind::TupleElement);
32733276
SWIFT_DEFER {
@@ -3540,7 +3543,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
35403543
}
35413544

35423545
printParameterFlags(Printer, Options, Param.getParameterFlags());
3543-
visit(Param.getType());
3546+
visit(Param.getPlainType());
35443547
if (Param.isVariadic())
35453548
Printer << "...";
35463549
}

lib/AST/ASTVerifier.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2255,15 +2255,15 @@ class Verifier : public ASTWalker {
22552255

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

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

23412341
if (var->getAttrs().hasAttribute<ImplicitlyUnwrappedOptionalAttr>()) {
23422342
auto varTy = var->getInterfaceType()
2343-
->getReferenceStorageReferent()
2344-
->getWithoutSpecifierType();
2343+
->getReferenceStorageReferent();
23452344

23462345
// FIXME: Update to look for plain Optional once
23472346
// ImplicitlyUnwrappedOptional is removed
@@ -2906,13 +2905,13 @@ class Verifier : public ASTWalker {
29062905
abort();
29072906
}
29082907
const ParamDecl *selfParam = FD->getImplicitSelfDecl();
2909-
if (!selfParam->getInterfaceType()->is<InOutType>()) {
2908+
if (!selfParam->isInOut()) {
29102909
Out << "mutating function does not have inout 'self'\n";
29112910
abort();
29122911
}
29132912
} else {
29142913
const ParamDecl *selfParam = FD->getImplicitSelfDecl();
2915-
if (selfParam && selfParam->getInterfaceType()->is<InOutType>()) {
2914+
if (selfParam && selfParam->isInOut()) {
29162915
Out << "non-mutating function has inout 'self'\n";
29172916
abort();
29182917
}
@@ -2925,7 +2924,7 @@ class Verifier : public ASTWalker {
29252924
abort();
29262925
}
29272926

2928-
auto resultTy = FD->getResultInterfaceType()->getWithoutSpecifierType();
2927+
auto resultTy = FD->getResultInterfaceType();
29292928

29302929
// FIXME: Update to look for plain Optional once
29312930
// ImplicitlyUnwrappedOptional is removed

lib/AST/Decl.cpp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1960,14 +1960,7 @@ bool ValueDecl::hasInterfaceType() const {
19601960

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

19731966
void ValueDecl::setInterfaceType(Type type) {
@@ -4072,13 +4065,9 @@ Type VarDecl::getType() const {
40724065
if (!typeInContext) {
40734066
const_cast<VarDecl *>(this)->typeInContext =
40744067
getDeclContext()->mapTypeIntoContext(
4075-
getInterfaceType())->getInOutObjectType();
4068+
getInterfaceType());
40764069
}
40774070

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

@@ -4380,7 +4369,7 @@ ParamDecl::ParamDecl(ParamDecl *PD, bool withTypes)
43804369
: VarDecl(DeclKind::Param, /*IsStatic*/false, PD->getSpecifier(),
43814370
/*IsCaptureList*/false, PD->getNameLoc(), PD->getName(),
43824371
PD->hasType() && withTypes
4383-
? PD->getType()->getInOutObjectType()
4372+
? PD->getType()
43844373
: Type(),
43854374
PD->getDeclContext()),
43864375
ArgumentName(PD->getArgumentName()),

lib/ClangImporter/ImportDecl.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ makeEnumRawValueConstructor(ClangImporter::Implementation &Impl,
439439
return ctorDecl;
440440

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

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

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

14651465
lhs = new (ctx) MemberRefExpr(lhs, SourceLoc(), storedRawValue,
14661466
DeclNameLoc(), /*Implicit=*/true,
@@ -5663,7 +5663,7 @@ Decl *SwiftDeclConverter::importGlobalAsInitializer(
56635663
Type selfType = selfParam->getType();
56645664
Type initType = FunctionType::get(selfType, fnType);
56655665
result->setInitializerInterfaceType(initType);
5666-
Type selfMetaType = MetatypeType::get(selfType->getInOutObjectType());
5666+
Type selfMetaType = MetatypeType::get(selfType);
56675667
Type allocType = FunctionType::get(selfMetaType, fnType);
56685668
result->setInterfaceType(allocType);
56695669
Impl.recordImplicitUnwrapForDecl(result,

lib/IDE/CodeCompletion.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2104,10 +2104,10 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
21042104
setClangDeclKeywords(VD, Pairs, Builder);
21052105
// Add a type annotation.
21062106
Type VarType = getTypeOfMember(VD);
2107-
if (VD->getName() == Ctx.Id_self) {
2108-
// Strip inout from 'self'. It is useful to show inout for function
2109-
// parameters. But for 'self' it is just noise.
2110-
VarType = VarType->getInOutObjectType();
2107+
if (VD->getName() != Ctx.Id_self && VD->isInOut()) {
2108+
// It is useful to show inout for function parameters.
2109+
// But for 'self' it is just noise.
2110+
VarType = InOutType::get(VarType);
21112111
}
21122112
auto DynamicOrOptional =
21132113
IsDynamicLookup || VD->getAttrs().hasAttribute<OptionalAttr>();

lib/IRGen/DebugTypeInfo.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,11 @@ DebugTypeInfo DebugTypeInfo::getFromTypeInfo(DeclContext *DC,
6363
DebugTypeInfo DebugTypeInfo::getLocalVariable(DeclContext *DC,
6464
GenericEnvironment *GE,
6565
VarDecl *Decl, swift::Type Ty,
66-
const TypeInfo &Info,
67-
bool Unwrap) {
66+
const TypeInfo &Info) {
6867

6968
auto DeclType =
7069
Decl->hasInterfaceType() ? Decl->getInterfaceType() : Decl->getType();
7170
auto RealType = Ty;
72-
if (Unwrap) {
73-
DeclType = DeclType->getInOutObjectType();
74-
RealType = RealType->getInOutObjectType();
75-
}
7671

7772
// DynamicSelfType is also sugar as far as debug info is concerned.
7873
auto Sugared = DeclType;

lib/IRGen/DebugTypeInfo.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ class DebugTypeInfo {
6262
/// Create type for a local variable.
6363
static DebugTypeInfo getLocalVariable(DeclContext *DeclCtx,
6464
GenericEnvironment *GE, VarDecl *Decl,
65-
swift::Type Ty, const TypeInfo &Info,
66-
bool Unwrap);
65+
swift::Type Ty, const TypeInfo &Info);
6766
/// Create type for an artificial metadata variable.
6867
static DebugTypeInfo getMetadata(swift::Type Ty, llvm::Type *StorageTy,
6968
Size size, Alignment align);
@@ -84,10 +83,6 @@ class DebugTypeInfo {
8483
DeclContext *getDeclContext() const { return DeclCtx; }
8584
GenericEnvironment *getGenericEnvironment() const { return GenericEnv; }
8685

87-
void unwrapLValueOrInOutType() {
88-
Type = Type->getWithoutSpecifierType().getPointer();
89-
}
90-
9186
// Determine whether this type is an Archetype itself.
9287
bool isArchetype() const {
9388
return Type->getWithoutSpecifierType()->is<ArchetypeType>();

lib/IRGen/IRGenSIL.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3671,7 +3671,7 @@ void IRGenSILFunction::visitDebugValueInst(DebugValueInst *i) {
36713671
if (VarDecl *Decl = i->getDecl()) {
36723672
DbgTy = DebugTypeInfo::getLocalVariable(
36733673
CurSILFn->getDeclContext(), CurSILFn->getGenericEnvironment(), Decl,
3674-
RealTy, getTypeInfo(SILVal->getType()), /*Unwrap=*/false);
3674+
RealTy, getTypeInfo(SILVal->getType()));
36753675
} else if (i->getFunction()->isBare() &&
36763676
!SILTy.hasArchetype() && !Name.empty()) {
36773677
// Preliminary support for .sil debug information.
@@ -3713,16 +3713,10 @@ void IRGenSILFunction::visitDebugValueAddrInst(DebugValueAddrInst *i) {
37133713
auto Addr = getLoweredAddress(SILVal).getAddress();
37143714
SILType SILTy = SILVal->getType();
37153715
auto RealType = SILTy.getASTType();
3716-
// Unwrap implicitly indirect types and types that are passed by
3717-
// reference only at the SIL level and below.
3718-
//
3719-
// FIXME: Should this check if the lowered SILType is address only
3720-
// instead? Otherwise optionals of archetypes etc will still have
3721-
// 'Unwrap' set to false.
3722-
bool Unwrap = VarInfo->Constant || SILTy.is<ArchetypeType>();
3716+
37233717
auto DbgTy = DebugTypeInfo::getLocalVariable(
37243718
CurSILFn->getDeclContext(), CurSILFn->getGenericEnvironment(), Decl,
3725-
RealType, getTypeInfo(SILVal->getType()), Unwrap);
3719+
RealType, getTypeInfo(SILVal->getType()));
37263720
bindArchetypes(DbgTy.getType());
37273721
if (!IGM.DebugInfo)
37283722
return;
@@ -4011,7 +4005,7 @@ void IRGenSILFunction::emitDebugInfoForAllocStack(AllocStackInst *i,
40114005
auto RealType = SILTy.getASTType();
40124006
auto DbgTy = DebugTypeInfo::getLocalVariable(
40134007
CurSILFn->getDeclContext(), CurSILFn->getGenericEnvironment(), Decl,
4014-
RealType, type, false);
4008+
RealType, type);
40154009

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

42134207
if (isInlinedGeneric(Decl, i->getDebugScope()))
42144208
return;

lib/IRGen/MetadataRequest.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -891,9 +891,7 @@ namespace {
891891
}
892892

893893
llvm::Value *getFunctionParameterRef(AnyFunctionType::CanParam &param) {
894-
auto type = param.getType();
895-
if (param.getParameterFlags().isInOut())
896-
type = type->getInOutObjectType()->getCanonicalType();
894+
auto type = param.getPlainType()->getCanonicalType();
897895
return IGF.emitAbstractTypeMetadataRef(type);
898896
}
899897

lib/SIL/AbstractionPattern.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ AbstractionPattern TypeConverter::getAbstractionPattern(VarDecl *var) {
7373
genericSig = sig->getCanonicalSignature();
7474

7575
CanType swiftType = var->getInterfaceType()
76-
->getInOutObjectType()
7776
->getCanonicalType();
7877

7978
if (auto clangDecl = var->getClangDecl()) {

lib/SIL/SILFunctionType.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,10 +686,16 @@ class DestructureInputs {
686686

687687
unsigned origParamIndex = NextOrigParamIndex++;
688688

689+
bool isInout = false;
690+
if (auto inoutType = dyn_cast<InOutType>(substType)) {
691+
isInout = true;
692+
substType = inoutType.getObjectType();
693+
origType = origType.getWithoutSpecifierType();
694+
}
695+
689696
auto &substTL = M.Types.getTypeLowering(origType, substType);
690697
ParameterConvention convention;
691-
if (isa<InOutType>(substType)) {
692-
assert(origType.isTypeParameter() || origType.getAs<InOutType>());
698+
if (isInout) {
693699
convention = ParameterConvention::Indirect_Inout;
694700
} else if (isFormallyPassedIndirectly(origType, substType, substTL)) {
695701
if (forSelf && rep == SILFunctionTypeRepresentation::WitnessMethod)

lib/SIL/TypeLowering.cpp

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,7 +1301,7 @@ TypeConverter::~TypeConverter() {
13011301
CanType srcType = ti.first.OrigType;
13021302
if (!srcType) continue;
13031303
CanType mappedType = ti.second->getLoweredType().getASTType();
1304-
if (srcType == mappedType || isa<InOutType>(srcType))
1304+
if (srcType == mappedType)
13051305
ti.second->~TypeLowering();
13061306
}
13071307
}
@@ -1440,26 +1440,11 @@ TypeConverter::getTypeLowering(AbstractionPattern origType,
14401440

14411441
assert((!key.isDependent() || getCurGenericContext())
14421442
&& "dependent type outside of generic context?!");
1443-
1443+
assert(!substType->is<InOutType>());
1444+
14441445
if (auto existing = find(key))
14451446
return *existing;
14461447

1447-
// inout types are a special case for lowering, because they get
1448-
// completely removed and represented as 'address' SILTypes.
1449-
if (isa<InOutType>(substType)) {
1450-
// Derive SILType for InOutType from the object type.
1451-
CanType substObjectType = substType.getWithoutSpecifierType();
1452-
AbstractionPattern origObjectType = origType.getWithoutSpecifierType();
1453-
1454-
SILType loweredType = getLoweredType(origObjectType, substObjectType)
1455-
.getAddressType();
1456-
1457-
auto *theInfo = new (*this, key.isDependent())
1458-
TrivialTypeLowering(loweredType);
1459-
insert(key, theInfo);
1460-
return *theInfo;
1461-
}
1462-
14631448
// Lower the type.
14641449
CanType loweredSubstType =
14651450
getLoweredRValueType(origType, substType);

lib/SILGen/RValue.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,6 @@ class RValue {
205205
/// True if this rvalue was emitted into context.
206206
bool isInContext() const & { return elementsToBeAdded == InContext; }
207207

208-
/// True if this represents an lvalue.
209-
bool isLValue() const & {
210-
return isa<InOutType>(type);
211-
}
212-
213208
/// Add an element to the rvalue. The rvalue must not yet be complete.
214209
void addElement(RValue &&element) &;
215210

0 commit comments

Comments
 (0)