Skip to content

Commit b45ed36

Browse files
authored
Merge pull request swiftlang#10423 from CodaFi/in-how-to-basic
[NFC] Miscellaneous cleanups for Parameters/Tuple Types
2 parents 7bebc84 + 777e1f9 commit b45ed36

File tree

7 files changed

+55
-67
lines changed

7 files changed

+55
-67
lines changed

include/swift/AST/Types.h

Lines changed: 27 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,12 +1385,13 @@ class NameAliasType : public TypeBase {
13851385
/// escaping.
13861386
class ParameterTypeFlags {
13871387
enum ParameterFlags : uint8_t {
1388-
None = 0,
1389-
Variadic = 1 << 0,
1388+
None = 0,
1389+
Variadic = 1 << 0,
13901390
AutoClosure = 1 << 1,
1391-
Escaping = 1 << 2,
1392-
1393-
NumBits = 3
1391+
Escaping = 1 << 2,
1392+
InOut = 1 << 3,
1393+
1394+
NumBits = 4
13941395
};
13951396
OptionSet<ParameterFlags> value;
13961397
static_assert(NumBits < 8*sizeof(OptionSet<ParameterFlags>), "overflowed");
@@ -1400,10 +1401,11 @@ class ParameterTypeFlags {
14001401
public:
14011402
ParameterTypeFlags() = default;
14021403

1403-
ParameterTypeFlags(bool variadic, bool autoclosure, bool escaping)
1404+
ParameterTypeFlags(bool variadic, bool autoclosure, bool escaping, bool inOut)
14041405
: value((variadic ? Variadic : 0) |
14051406
(autoclosure ? AutoClosure : 0) |
1406-
(escaping ? Escaping : 0)) {}
1407+
(escaping ? Escaping : 0) |
1408+
(inOut ? InOut : 0)) {}
14071409

14081410
/// Create one from what's present in the parameter type
14091411
inline static ParameterTypeFlags fromParameterType(Type paramTy,
@@ -1413,7 +1415,8 @@ class ParameterTypeFlags {
14131415
bool isVariadic() const { return value.contains(Variadic); }
14141416
bool isAutoClosure() const { return value.contains(AutoClosure); }
14151417
bool isEscaping() const { return value.contains(Escaping); }
1416-
1418+
bool isInOut() const { return value.contains(InOut); }
1419+
14171420
ParameterTypeFlags withEscaping(bool escaping) const {
14181421
return ParameterTypeFlags(escaping ? value | ParameterTypeFlags::Escaping
14191422
: value - ParameterTypeFlags::Escaping);
@@ -1467,24 +1470,16 @@ class TupleTypeElt {
14671470
ParameterTypeFlags Flags;
14681471

14691472
friend class TupleType;
1470-
1473+
14711474
public:
14721475
TupleTypeElt() = default;
1473-
inline /*implicit*/ TupleTypeElt(Type ty, Identifier name,
1474-
bool isVariadic, bool isAutoClosure,
1475-
bool isEscaping);
1476-
14771476
TupleTypeElt(Type ty, Identifier name = Identifier(),
1478-
ParameterTypeFlags PTFlags = {})
1479-
: Name(name), ElementType(ty), Flags(PTFlags) {}
1480-
1481-
/*implicit*/ TupleTypeElt(TypeBase *Ty)
1482-
: Name(Identifier()), ElementType(Ty), Flags() { }
1483-
1477+
ParameterTypeFlags fl = {});
1478+
14841479
bool hasName() const { return !Name.empty(); }
14851480
Identifier getName() const { return Name; }
14861481

1487-
Type getType() const { return ElementType.getPointer(); }
1482+
Type getType() const { return ElementType; }
14881483

14891484
ParameterTypeFlags getParameterFlags() const { return Flags; }
14901485

@@ -1496,16 +1491,14 @@ class TupleTypeElt {
14961491

14971492
/// Determine whether this field is an escaping parameter closure.
14981493
bool isEscaping() const { return Flags.isEscaping(); }
1499-
1500-
static inline Type getVarargBaseTy(Type VarArgT);
1501-
1494+
1495+
/// Determine whether this field is marked 'inout'.
1496+
bool isInOut() const { return Flags.isInOut(); }
1497+
15021498
/// Remove the type of this varargs element designator, without the array
15031499
/// type wrapping it.
1504-
Type getVarargBaseTy() const {
1505-
assert(isVararg());
1506-
return getVarargBaseTy(getType());
1507-
}
1508-
1500+
Type getVarargBaseTy() const;
1501+
15091502
/// Retrieve a copy of this tuple type element with the type replaced.
15101503
TupleTypeElt getWithType(Type T) const {
15111504
return TupleTypeElt(T, getName(), getParameterFlags());
@@ -2597,10 +2590,7 @@ struct CallArgParam {
25972590

25982591
// The label associated with the argument or parameter, if any.
25992592
Identifier Label;
2600-
2601-
/// Whether the parameter has a default argument. Not valid for arguments.
2602-
bool HasDefaultArgument = false;
2603-
2593+
26042594
/// Parameter specific flags, not valid for arguments
26052595
ParameterTypeFlags parameterFlags = {};
26062596

@@ -4661,24 +4651,9 @@ inline CanType CanType::getNominalParent() const {
46614651
return cast<BoundGenericType>(*this).getParent();
46624652
}
46634653
}
4664-
4665-
inline TupleTypeElt::TupleTypeElt(Type ty, Identifier name, bool isVariadic,
4666-
bool isAutoClosure, bool isEscaping)
4667-
: Name(name), ElementType(ty),
4668-
Flags(isVariadic, isAutoClosure, isEscaping) {
4669-
assert(!isVariadic ||
4670-
ty->hasError() ||
4671-
isa<ArraySliceType>(ty.getPointer()) ||
4672-
(isa<BoundGenericType>(ty.getPointer()) &&
4673-
ty->castTo<BoundGenericType>()->getGenericArgs().size() == 1));
4674-
assert(!isAutoClosure || (ty->is<AnyFunctionType>() &&
4675-
ty->castTo<AnyFunctionType>()->isAutoClosure()));
4676-
assert(!isEscaping || (ty->is<AnyFunctionType>() &&
4677-
!ty->castTo<AnyFunctionType>()->isNoEscape()));
4678-
}
4679-
4680-
inline Type TupleTypeElt::getVarargBaseTy(Type VarArgT) {
4681-
TypeBase *T = VarArgT.getPointer();
4654+
4655+
inline Type TupleTypeElt::getVarargBaseTy() const {
4656+
TypeBase *T = getType().getPointer();
46824657
if (auto *AT = dyn_cast<ArraySliceType>(T))
46834658
return AT->getBaseType();
46844659
if (auto *BGT = dyn_cast<BoundGenericType>(T)) {
@@ -4696,7 +4671,8 @@ ParameterTypeFlags::fromParameterType(Type paramTy, bool isVariadic) {
46964671
paramTy->castTo<AnyFunctionType>()->isAutoClosure();
46974672
bool escaping = paramTy->is<AnyFunctionType>() &&
46984673
!paramTy->castTo<AnyFunctionType>()->isNoEscape();
4699-
return {isVariadic, autoclosure, escaping};
4674+
bool inOut = paramTy->is<InOutType>();
4675+
return {isVariadic, autoclosure, escaping, inOut};
47004676
}
47014677

47024678
#define TYPE(id, parent)

include/swift/Serialization/ModuleFormat.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const uint16_t VERSION_MAJOR = 0;
5454
/// in source control, you should also update the comment to briefly
5555
/// describe what change you made. The content of this comment isn't important;
5656
/// it just ensures a conflict if two people change the module format.
57-
const uint16_t VERSION_MINOR = 346; // Last change: dependency types for enums
57+
const uint16_t VERSION_MINOR = 347; // Last change: 'inout' on parameter types
5858

5959
using DeclID = PointerEmbeddedInt<unsigned, 31>;
6060
using DeclIDField = BCFixed<31>;
@@ -598,7 +598,8 @@ namespace decls_block {
598598
TypeIDField, // inner type
599599
BCFixed<1>, // vararg?
600600
BCFixed<1>, // autoclosure?
601-
BCFixed<1> // escaping?
601+
BCFixed<1>, // escaping?
602+
BCFixed<1> // inout?
602603
>;
603604

604605
using TupleTypeLayout = BCRecordLayout<
@@ -611,7 +612,8 @@ namespace decls_block {
611612
TypeIDField, // type
612613
BCFixed<1>, // vararg?
613614
BCFixed<1>, // autoclosure?
614-
BCFixed<1> // escaping?
615+
BCFixed<1>, // escaping?
616+
BCFixed<1> // inout?
615617
>;
616618

617619
using FunctionTypeLayout = BCRecordLayout<

lib/AST/ASTContext.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2737,6 +2737,14 @@ Type TupleType::get(ArrayRef<TupleTypeElt> Fields, const ASTContext &C) {
27372737
return New;
27382738
}
27392739

2740+
TupleTypeElt::TupleTypeElt(Type ty, Identifier name,
2741+
ParameterTypeFlags fl)
2742+
: Name(name), ElementType(ty), Flags(fl) {
2743+
// FIXME: Re-enable this assertion and hunt down the callers that aren't
2744+
// setting parameter bits correctly.
2745+
// assert((ty->is<InOutType>() && fl.isInOut()) && "caller did not set flags");
2746+
}
2747+
27402748
void UnboundGenericType::Profile(llvm::FoldingSetNodeID &ID,
27412749
GenericTypeDecl *TheDecl, Type Parent) {
27422750
ID.AddPointer(TheDecl);

lib/AST/Builtins.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,15 +1028,15 @@ static ValueDecl *getCheckedTruncOperation(ASTContext &Context,
10281028
return nullptr;
10291029

10301030
Type OverflowBitTy = BuiltinIntegerType::get(1, Context);
1031-
TupleTypeElt ResultElts[] = { OutTy, OverflowBitTy };
1031+
TupleTypeElt ResultElts[] = { Type(OutTy), OverflowBitTy };
10321032
Type ResultTy = TupleType::get(ResultElts, Context);
10331033
return getBuiltinFunction(Id, { InTy }, ResultTy);
10341034
}
10351035

10361036
static ValueDecl *getCheckedConversionOperation(ASTContext &Context,
10371037
Identifier Id,
10381038
Type Ty) {
1039-
auto BuiltinTy = Ty->getAs<BuiltinIntegerType>();
1039+
Type BuiltinTy = Ty->getAs<BuiltinIntegerType>();
10401040
if (!BuiltinTy)
10411041
return nullptr;
10421042

lib/AST/Type.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,8 @@ swift::decomposeArgType(Type type, ArrayRef<Identifier> argumentLabels) {
727727

728728
for (auto i : range(0, tupleTy->getNumElements())) {
729729
const auto &elt = tupleTy->getElement(i);
730-
assert(elt.getParameterFlags().isNone() &&
730+
assert((elt.getParameterFlags().isNone() ||
731+
elt.getParameterFlags().isInOut()) &&
731732
"Vararg, autoclosure, or escaping argument tuple"
732733
"doesn't make sense");
733734
CallArgParam argParam;

lib/Serialization/Deserialization.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3805,17 +3805,17 @@ Expected<Type> ModuleFile::getTypeChecked(TypeID TID) {
38053805

38063806
case decls_block::PAREN_TYPE: {
38073807
TypeID underlyingID;
3808-
bool isVariadic, isAutoClosure, isEscaping;
3808+
bool isVariadic, isAutoClosure, isEscaping, isInOut;
38093809
decls_block::ParenTypeLayout::readRecord(scratch, underlyingID, isVariadic,
3810-
isAutoClosure, isEscaping);
3810+
isAutoClosure, isEscaping, isInOut);
38113811

38123812
auto underlyingTy = getTypeChecked(underlyingID);
38133813
if (!underlyingTy)
38143814
return underlyingTy.takeError();
3815-
3815+
38163816
typeOrOffset = ParenType::get(
38173817
ctx, underlyingTy.get(),
3818-
ParameterTypeFlags(isVariadic, isAutoClosure, isEscaping));
3818+
ParameterTypeFlags(isVariadic, isAutoClosure, isEscaping, isInOut));
38193819
break;
38203820
}
38213821

@@ -3835,17 +3835,17 @@ Expected<Type> ModuleFile::getTypeChecked(TypeID TID) {
38353835

38363836
IdentifierID nameID;
38373837
TypeID typeID;
3838-
bool isVariadic, isAutoClosure, isEscaping;
3838+
bool isVariadic, isAutoClosure, isEscaping, isInOut;
38393839
decls_block::TupleTypeEltLayout::readRecord(
3840-
scratch, nameID, typeID, isVariadic, isAutoClosure, isEscaping);
3840+
scratch, nameID, typeID, isVariadic, isAutoClosure, isEscaping, isInOut);
38413841

38423842
auto elementTy = getTypeChecked(typeID);
38433843
if (!elementTy)
38443844
return elementTy.takeError();
3845-
3845+
38463846
elements.emplace_back(
38473847
elementTy.get(), getIdentifier(nameID),
3848-
ParameterTypeFlags(isVariadic, isAutoClosure, isEscaping));
3848+
ParameterTypeFlags(isVariadic, isAutoClosure, isEscaping, isInOut));
38493849
}
38503850

38513851
typeOrOffset = TupleType::get(elements, ctx);

lib/Serialization/Serialization.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3248,7 +3248,7 @@ void Serializer::writeType(Type ty) {
32483248
ParenTypeLayout::emitRecord(
32493249
Out, ScratchRecord, abbrCode, addTypeRef(parenTy->getUnderlyingType()),
32503250
paramFlags.isVariadic(), paramFlags.isAutoClosure(),
3251-
paramFlags.isEscaping());
3251+
paramFlags.isEscaping(), paramFlags.isInOut());
32523252
break;
32533253
}
32543254

@@ -3264,7 +3264,8 @@ void Serializer::writeType(Type ty) {
32643264
TupleTypeEltLayout::emitRecord(
32653265
Out, ScratchRecord, abbrCode, addDeclBaseNameRef(elt.getName()),
32663266
addTypeRef(elt.getType()), paramFlags.isVariadic(),
3267-
paramFlags.isAutoClosure(), paramFlags.isEscaping());
3267+
paramFlags.isAutoClosure(), paramFlags.isEscaping(),
3268+
paramFlags.isInOut());
32683269
}
32693270

32703271
break;

0 commit comments

Comments
 (0)