Skip to content

Commit a6dd630

Browse files
authored
Eliminate Builtin.UnknownObject as an AST type (#27378)
This removes it from the AST and largely replaces it with AnyObject at the SIL and IRGen layers. Some notes: - Reflection still uses the notion of "unknown object" to mean an object with unknown refcounting. There's no real reason to make this different from AnyObject (an existential containing a single object with unknown refcounting), but this way nothing changes for clients of Reflection, and it's consistent with how native objects are represented. - The value witness table and reflection descriptor for AnyObject use the mangling "BO" instead of "yXl". - The demangler and remangler continue to support "BO" because it's still in use as a type encoding, even if it's not an AST-level Type anymore. - Type-based alias analysis for Builtin.UnknownObject was incorrect, so it's a good thing we weren't using it. - Same with enum layout. (This one assumed UnknownObject never referred to an Objective-C tagged pointer. That certainly wasn't how we were using it!)
1 parent 777d557 commit a6dd630

Some content is hidden

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

49 files changed

+208
-338
lines changed

docs/ABI/Mangling.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ Types
481481
type ::= 'Bf' NATURAL '_' // Builtin.Float<n>
482482
type ::= 'Bi' NATURAL '_' // Builtin.Int<n>
483483
type ::= 'BI' // Builtin.IntLiteral
484-
type ::= 'BO' // Builtin.UnknownObject
484+
type ::= 'BO' // Builtin.UnknownObject (no longer a distinct type, but still used for AnyObject)
485485
type ::= 'Bo' // Builtin.NativeObject
486486
type ::= 'Bp' // Builtin.RawPointer
487487
type ::= 'Bt' // Builtin.SILToken

docs/ARCOptimization.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ is_unique performs depends on the argument type:
335335

336336
- Objective-C object types require an additional check that the
337337
dynamic object type uses native Swift reference counting:
338-
(Builtin.UnknownObject, unknown class reference, class existential)
338+
(unknown class reference, class existential)
339339

340340
- Bridged object types allow the dynamic object type check to be
341341
bypassed based on the pointer encoding:

include/swift/AST/ASTContext.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,6 @@ class ASTContext final {
605605
const CanType TheAnyType; /// This is 'Any', the empty protocol composition
606606
const CanType TheNativeObjectType; /// Builtin.NativeObject
607607
const CanType TheBridgeObjectType; /// Builtin.BridgeObject
608-
const CanType TheUnknownObjectType; /// Builtin.UnknownObject
609608
const CanType TheRawPointerType; /// Builtin.RawPointer
610609
const CanType TheUnsafeValueBufferType; /// Builtin.UnsafeValueBuffer
611610
const CanType TheSILTokenType; /// Builtin.SILToken

include/swift/AST/TypeMatcher.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ class TypeMatcher {
107107
TRIVIAL_CASE(BuiltinRawPointerType)
108108
TRIVIAL_CASE(BuiltinNativeObjectType)
109109
TRIVIAL_CASE(BuiltinBridgeObjectType)
110-
TRIVIAL_CASE(BuiltinUnknownObjectType)
111110
TRIVIAL_CASE(BuiltinUnsafeValueBufferType)
112111
TRIVIAL_CASE(BuiltinVectorType)
113112
TRIVIAL_CASE(SILTokenType)

include/swift/AST/TypeNodes.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ ABSTRACT_TYPE(Builtin, Type)
9898
BUILTIN_TYPE(BuiltinRawPointer, BuiltinType)
9999
BUILTIN_TYPE(BuiltinNativeObject, BuiltinType)
100100
BUILTIN_TYPE(BuiltinBridgeObject, BuiltinType)
101-
BUILTIN_TYPE(BuiltinUnknownObject, BuiltinType)
102101
BUILTIN_TYPE(BuiltinUnsafeValueBuffer, BuiltinType)
103102
BUILTIN_TYPE(BuiltinVector, BuiltinType)
104103
TYPE_RANGE(Builtin, BuiltinInteger, BuiltinVector)

include/swift/AST/Types.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,19 +1311,6 @@ class BuiltinBridgeObjectType : public BuiltinType {
13111311
};
13121312
DEFINE_EMPTY_CAN_TYPE_WRAPPER(BuiltinBridgeObjectType, BuiltinType);
13131313

1314-
/// BuiltinUnknownObjectType - The builtin opaque Objective-C pointer type.
1315-
/// Useful for pushing an Objective-C type through swift.
1316-
class BuiltinUnknownObjectType : public BuiltinType {
1317-
friend class ASTContext;
1318-
BuiltinUnknownObjectType(const ASTContext &C)
1319-
: BuiltinType(TypeKind::BuiltinUnknownObject, C) {}
1320-
public:
1321-
static bool classof(const TypeBase *T) {
1322-
return T->getKind() == TypeKind::BuiltinUnknownObject;
1323-
}
1324-
};
1325-
DEFINE_EMPTY_CAN_TYPE_WRAPPER(BuiltinUnknownObjectType, BuiltinType);
1326-
13271314
/// BuiltinUnsafeValueBufferType - The builtin opaque fixed-size value
13281315
/// buffer type, into which storage for an arbitrary value can be
13291316
/// allocated using Builtin.allocateValueBuffer.

include/swift/Reflection/ReflectionContext.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ class ReflectionContext
671671
// Class existentials have trivial layout.
672672
// It is itself the pointer to the instance followed by the witness tables.
673673
case RecordKind::ClassExistential:
674-
// This is just Builtin.UnknownObject
674+
// This is just AnyObject.
675675
*OutInstanceTR = ExistentialRecordTI->getFields()[0].TR;
676676
*OutInstanceAddress = ExistentialAddress;
677677
return true;

include/swift/Runtime/BuiltinTypes.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ BUILTIN_POINTER_TYPE(Bb, "Builtin.BridgeObject")
5656
BUILTIN_POINTER_TYPE(Bp, "Builtin.RawPointer")
5757
BUILTIN_TYPE(BB, "Builtin.UnsafeValueBuffer")
5858

59+
// No longer used in the compiler as an AST type, but still used for fields 
60+
// shaped like AnyObject (normal mangling yXl).
5961
BUILTIN_POINTER_TYPE(BO, "Builtin.UnknownObject")
6062

6163
// Int8 vector types

include/swift/SIL/SILType.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,6 @@ class SILType {
512512

513513
/// Get the NativeObject type as a SILType.
514514
static SILType getNativeObjectType(const ASTContext &C);
515-
/// Get the UnknownObject type as a SILType.
516-
static SILType getUnknownObjectType(const ASTContext &C);
517515
/// Get the BridgeObject type as a SILType.
518516
static SILType getBridgeObjectType(const ASTContext &C);
519517
/// Get the RawPointer type as a SILType.

include/swift/Strings.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ constexpr static BuiltinNameStringLiteral BUILTIN_TYPE_NAME_RAWPOINTER = {
106106
constexpr static BuiltinNameStringLiteral BUILTIN_TYPE_NAME_UNSAFEVALUEBUFFER =
107107
{"Builtin.UnsafeValueBuffer"};
108108
/// The name of the Builtin type for UnknownObject
109+
///
110+
/// This no longer exists as an AST-accessible type, but it's still used for 
111+
/// fields shaped like AnyObject when ObjC interop is enabled.
109112
constexpr static BuiltinNameStringLiteral BUILTIN_TYPE_NAME_UNKNOWNOBJECT = {
110113
"Builtin.UnknownObject"};
111114
/// The name of the Builtin type for Vector

lib/AST/ASTContext.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,8 +551,6 @@ ASTContext::ASTContext(LangOptions &langOpts, SearchPathOptions &SearchPathOpts,
551551
BuiltinNativeObjectType(*this)),
552552
TheBridgeObjectType(new (*this, AllocationArena::Permanent)
553553
BuiltinBridgeObjectType(*this)),
554-
TheUnknownObjectType(new (*this, AllocationArena::Permanent)
555-
BuiltinUnknownObjectType(*this)),
556554
TheRawPointerType(new (*this, AllocationArena::Permanent)
557555
BuiltinRawPointerType(*this)),
558556
TheUnsafeValueBufferType(new (*this, AllocationArena::Permanent)

lib/AST/ASTDumper.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3373,7 +3373,6 @@ namespace {
33733373
TRIVIAL_TYPE_PRINTER(BuiltinRawPointer, builtin_raw_pointer)
33743374
TRIVIAL_TYPE_PRINTER(BuiltinNativeObject, builtin_native_object)
33753375
TRIVIAL_TYPE_PRINTER(BuiltinBridgeObject, builtin_bridge_object)
3376-
TRIVIAL_TYPE_PRINTER(BuiltinUnknownObject, builtin_unknown_object)
33773376
TRIVIAL_TYPE_PRINTER(BuiltinUnsafeValueBuffer, builtin_unsafe_value_buffer)
33783377
TRIVIAL_TYPE_PRINTER(SILToken, sil_token)
33793378

lib/AST/ASTMangler.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -802,8 +802,6 @@ void ASTMangler::appendType(Type type, const ValueDecl *forDecl) {
802802
return appendOperator("Bo");
803803
case TypeKind::BuiltinBridgeObject:
804804
return appendOperator("Bb");
805-
case TypeKind::BuiltinUnknownObject:
806-
return appendOperator("BO");
807805
case TypeKind::BuiltinUnsafeValueBuffer:
808806
return appendOperator("BB");
809807
case TypeKind::SILToken:

lib/AST/ASTPrinter.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3601,7 +3601,6 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
36013601
}
36023602
ASTPRINTER_PRINT_BUILTINTYPE(BuiltinRawPointerType)
36033603
ASTPRINTER_PRINT_BUILTINTYPE(BuiltinNativeObjectType)
3604-
ASTPRINTER_PRINT_BUILTINTYPE(BuiltinUnknownObjectType)
36053604
ASTPRINTER_PRINT_BUILTINTYPE(BuiltinBridgeObjectType)
36063605
ASTPRINTER_PRINT_BUILTINTYPE(BuiltinUnsafeValueBufferType)
36073606
ASTPRINTER_PRINT_BUILTINTYPE(BuiltinIntegerLiteralType)

lib/AST/Builtins.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ Type swift::getBuiltinType(ASTContext &Context, StringRef Name) {
8080
return Context.TheRawPointerType;
8181
if (Name == "NativeObject")
8282
return Context.TheNativeObjectType;
83-
if (Name == "UnknownObject")
84-
return Context.TheUnknownObjectType;
8583
if (Name == "BridgeObject")
8684
return Context.TheBridgeObjectType;
8785
if (Name == "SILToken")
@@ -2100,9 +2098,6 @@ StringRef BuiltinType::getTypeName(SmallVectorImpl<char> &result,
21002098
case BuiltinTypeKind::BuiltinNativeObject:
21012099
printer << MAYBE_GET_NAMESPACED_BUILTIN(BUILTIN_TYPE_NAME_NATIVEOBJECT);
21022100
break;
2103-
case BuiltinTypeKind::BuiltinUnknownObject:
2104-
printer << MAYBE_GET_NAMESPACED_BUILTIN(BUILTIN_TYPE_NAME_UNKNOWNOBJECT);
2105-
break;
21062101
case BuiltinTypeKind::BuiltinBridgeObject:
21072102
printer << MAYBE_GET_NAMESPACED_BUILTIN(BUILTIN_TYPE_NAME_BRIDGEOBJECT);
21082103
break;

lib/AST/Type.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ bool CanType::isReferenceTypeImpl(CanType type, GenericSignature *sig,
170170
llvm_unreachable("sugared canonical type?");
171171

172172
// These types are always class references.
173-
case TypeKind::BuiltinUnknownObject:
174173
case TypeKind::BuiltinNativeObject:
175174
case TypeKind::BuiltinBridgeObject:
176175
case TypeKind::Class:
@@ -4348,9 +4347,6 @@ ReferenceCounting TypeBase::getReferenceCounting() {
43484347
case TypeKind::BuiltinBridgeObject:
43494348
return ReferenceCounting::Bridge;
43504349

4351-
case TypeKind::BuiltinUnknownObject:
4352-
return ReferenceCounting::Unknown;
4353-
43544350
case TypeKind::Class:
43554351
return getClassReferenceCounting(cast<ClassType>(type)->getDecl());
43564352
case TypeKind::BoundGenericClass:

lib/IRGen/GenClangType.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,6 @@ class GenClangType : public CanTypeVisitor<GenClangType, clang::CanQualType> {
156156
clang::CanQualType visitBuiltinRawPointerType(CanBuiltinRawPointerType type);
157157
clang::CanQualType visitBuiltinIntegerType(CanBuiltinIntegerType type);
158158
clang::CanQualType visitBuiltinFloatType(CanBuiltinFloatType type);
159-
clang::CanQualType visitBuiltinUnknownObjectType(
160-
CanBuiltinUnknownObjectType type);
161159
clang::CanQualType visitArchetypeType(CanArchetypeType type);
162160
clang::CanQualType visitSILFunctionType(CanSILFunctionType type);
163161
clang::CanQualType visitGenericTypeParamType(CanGenericTypeParamType type);
@@ -703,12 +701,6 @@ clang::CanQualType GenClangType::visitBuiltinFloatType(
703701
llvm_unreachable("cannot translate floating-point format to C");
704702
}
705703

706-
clang::CanQualType GenClangType::visitBuiltinUnknownObjectType(
707-
CanBuiltinUnknownObjectType type) {
708-
// Builtin.UnknownObject == AnyObject, so it is also translated to 'id'.
709-
return getClangIdType(getClangASTContext());
710-
}
711-
712704
clang::CanQualType GenClangType::visitArchetypeType(CanArchetypeType type) {
713705
// We see these in the case where we invoke an @objc function
714706
// through a protocol.

lib/IRGen/GenMeta.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2614,7 +2614,7 @@ namespace {
26142614
case ClassMetadataStrategy::Fixed: {
26152615
// FIXME: Should this check HasImported instead?
26162616
auto type = (Target->checkAncestry(AncestryFlags::ObjC)
2617-
? IGM.Context.TheUnknownObjectType
2617+
? IGM.Context.getAnyObjectType()
26182618
: IGM.Context.TheNativeObjectType);
26192619
auto wtable = IGM.getAddrOfValueWitnessTable(type);
26202620
B.add(wtable);
@@ -4011,7 +4011,7 @@ namespace {
40114011
// Without Objective-C interop, foreign classes must still use
40124012
// Swift native reference counting.
40134013
auto type = (IGM.ObjCInterop
4014-
? IGM.Context.TheUnknownObjectType
4014+
? IGM.Context.getAnyObjectType()
40154015
: IGM.Context.TheNativeObjectType);
40164016
auto wtable = IGM.getAddrOfValueWitnessTable(type);
40174017
B.add(wtable);

lib/IRGen/GenObjC.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,16 @@ llvm::Value *irgen::emitObjCAutoreleaseReturnValue(IRGenFunction &IGF,
184184
}
185185

186186
namespace {
187-
/// A type-info implementation suitable for Builtin.UnknownObject.
187+
/// A type-info implementation suitable for AnyObject on platforms with ObjC
188+
/// interop.
188189
class UnknownTypeInfo : public HeapTypeInfo<UnknownTypeInfo> {
189190
public:
190191
UnknownTypeInfo(llvm::PointerType *storageType, Size size,
191192
SpareBitVector spareBits, Alignment align)
192193
: HeapTypeInfo(storageType, size, spareBits, align) {
193194
}
194195

195-
/// Builtin.UnknownObject requires ObjC reference-counting.
196+
/// AnyObject requires ObjC reference-counting.
196197
ReferenceCounting getReferenceCounting() const {
197198
return ReferenceCounting::Unknown;
198199
}

lib/IRGen/GenReflection.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,14 @@ class FixedTypeMetadataBuilder : public ReflectionMetadataBuilder {
847847
}
848848

849849
void layout() override {
850-
addTypeRef(type, CanGenericSignature());
850+
if (type->isAnyObject()) {
851+
// AnyObject isn't actually a builtin type; we're emitting it as the old
852+
// Builtin.UnknownObject type for ABI compatibility.
853+
B.addRelativeAddress(
854+
IGM.getAddrOfStringForTypeRef("BO", MangledTypeRefRole::Reflection));
855+
} else {
856+
addTypeRef(type, CanGenericSignature());
857+
}
851858

852859
B.addInt32(ti->getFixedSize().getValue());
853860

@@ -1251,7 +1258,7 @@ emitAssociatedTypeMetadataRecord(const RootProtocolConformance *conformance) {
12511258
void IRGenModule::emitBuiltinReflectionMetadata() {
12521259
if (getSwiftModule()->isStdlibModule()) {
12531260
BuiltinTypes.insert(Context.TheNativeObjectType);
1254-
BuiltinTypes.insert(Context.TheUnknownObjectType);
1261+
BuiltinTypes.insert(Context.getAnyObjectType());
12551262
BuiltinTypes.insert(Context.TheBridgeObjectType);
12561263
BuiltinTypes.insert(Context.TheRawPointerType);
12571264
BuiltinTypes.insert(Context.TheUnsafeValueBufferType);

lib/IRGen/GenType.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,8 +1788,6 @@ const TypeInfo *TypeConverter::convertType(CanType ty) {
17881788
}
17891789
case TypeKind::BuiltinNativeObject:
17901790
return &getNativeObjectTypeInfo();
1791-
case TypeKind::BuiltinUnknownObject:
1792-
return &getUnknownObjectTypeInfo();
17931791
case TypeKind::BuiltinBridgeObject:
17941792
return &getBridgeObjectTypeInfo();
17951793
case TypeKind::BuiltinUnsafeValueBuffer:

lib/IRGen/GenValueWitness.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ getAddrOfKnownValueWitnessTable(IRGenModule &IGM, CanType type,
10011001
case ReferenceCounting::ObjC:
10021002
case ReferenceCounting::Block:
10031003
case ReferenceCounting::Unknown:
1004-
witnessSurrogate = C.TheUnknownObjectType;
1004+
witnessSurrogate = C.getAnyObjectType();
10051005
break;
10061006
case ReferenceCounting::Bridge:
10071007
witnessSurrogate = C.TheBridgeObjectType;

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,18 +1207,6 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
12071207
break;
12081208
}
12091209

1210-
case TypeKind::BuiltinUnknownObject: {
1211-
// The builtin opaque Objective-C pointer type. Useful for pushing
1212-
// an Objective-C type through swift.
1213-
unsigned PtrSize = CI.getTargetInfo().getPointerWidth(0);
1214-
auto IdTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
1215-
MangledName, Scope, File, 0,
1216-
llvm::dwarf::DW_LANG_ObjC, 0, 0);
1217-
return DBuilder.createPointerType(IdTy, PtrSize, 0,
1218-
/* DWARFAddressSpace */ None,
1219-
MangledName);
1220-
}
1221-
12221210
case TypeKind::BuiltinNativeObject: {
12231211
unsigned PtrSize = CI.getTargetInfo().getPointerWidth(0);
12241212
auto PTy =

lib/IRGen/IRGenMangler.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,16 @@ class IRGenMangler : public Mangle::ASTMangler {
8080
std::string mangleValueWitness(Type type, ValueWitness witness);
8181

8282
std::string mangleValueWitnessTable(Type type) {
83-
return mangleTypeSymbol(type, "WV");
83+
const char * const witnessTableOp = "WV";
84+
if (type->isAnyObject()) {
85+
// Special-case for AnyObject, whose witness table is under the old name
86+
// Builtin.UnknownObject, even though we don't use that as a Type anymore.
87+
beginMangling();
88+
appendOperator("BO");
89+
appendOperator(witnessTableOp);
90+
return finalize();
91+
}
92+
return mangleTypeSymbol(type, witnessTableOp);
8493
}
8594

8695
std::string mangleTypeMetadataAccessFunction(Type type) {
@@ -382,7 +391,17 @@ class IRGenMangler : public Mangle::ASTMangler {
382391
}
383392

384393
std::string mangleReflectionBuiltinDescriptor(Type type) {
385-
return mangleTypeSymbol(type, "MB");
394+
const char * const reflectionDescriptorOp = "MB";
395+
if (type->isAnyObject()) {
396+
// Special-case for AnyObject, whose reflection descriptor is under the
397+
// old name Builtin.UnknownObject, even though we don't use that as a Type
398+
// anymore.
399+
beginMangling();
400+
appendOperator("BO");
401+
appendOperator(reflectionDescriptorOp);
402+
return finalize();
403+
}
404+
return mangleTypeSymbol(type, reflectionDescriptorOp);
386405
}
387406

388407
std::string mangleReflectionFieldDescriptor(Type type) {

lib/IRGen/IRGenSIL.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3913,8 +3913,7 @@ static bool hasReferenceSemantics(IRGenSILFunction &IGF,
39133913
return (objType->mayHaveSuperclass()
39143914
|| objType->isClassExistentialType()
39153915
|| objType->is<BuiltinNativeObjectType>()
3916-
|| objType->is<BuiltinBridgeObjectType>()
3917-
|| objType->is<BuiltinUnknownObjectType>());
3916+
|| objType->is<BuiltinBridgeObjectType>());
39183917
}
39193918

39203919
static llvm::Value *emitIsUnique(IRGenSILFunction &IGF, SILValue operand,

0 commit comments

Comments
 (0)