Skip to content

Commit da506fc

Browse files
committed
[Reflection] NFC: Adopt reference storage type meta-programming macros
1 parent 7d6c696 commit da506fc

File tree

9 files changed

+83
-194
lines changed

9 files changed

+83
-194
lines changed

include/swift/Reflection/TypeLowering.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,8 @@ enum class ReferenceCounting : unsigned {
9797

9898
enum class ReferenceKind : unsigned {
9999
Strong,
100-
Unowned,
101-
Weak,
102-
Unmanaged
100+
#define REF_STORAGE(Name, ...) Name,
101+
#include "swift/AST/ReferenceStorage.def"
103102
};
104103

105104
enum class TypeInfoKind : unsigned {

include/swift/Reflection/TypeRef.h

Lines changed: 24 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -667,63 +667,33 @@ class ReferenceStorageTypeRef : public TypeRef {
667667
}
668668

669669
static bool classof(const TypeRef *TR) {
670-
auto Kind = TR->getKind();
671-
return (Kind == TypeRefKind::UnownedStorage ||
672-
Kind == TypeRefKind::WeakStorage ||
673-
Kind == TypeRefKind::UnmanagedStorage);
674-
}
675-
};
676-
677-
class UnownedStorageTypeRef final : public ReferenceStorageTypeRef {
678-
using ReferenceStorageTypeRef::Profile;
679-
public:
680-
UnownedStorageTypeRef(const TypeRef *Type)
681-
: ReferenceStorageTypeRef(TypeRefKind::UnownedStorage, Type) {}
682-
683-
template <typename Allocator>
684-
static const UnownedStorageTypeRef *create(Allocator &A,
685-
const TypeRef *Type) {
686-
FIND_OR_CREATE_TYPEREF(A, UnownedStorageTypeRef, Type);
687-
}
688-
689-
static bool classof(const TypeRef *TR) {
690-
return TR->getKind() == TypeRefKind::UnownedStorage;
691-
}
692-
};
693-
694-
class WeakStorageTypeRef final : public ReferenceStorageTypeRef {
695-
using ReferenceStorageTypeRef::Profile;
696-
public:
697-
WeakStorageTypeRef(const TypeRef *Type)
698-
: ReferenceStorageTypeRef(TypeRefKind::WeakStorage, Type) {}
699-
700-
template <typename Allocator>
701-
static const WeakStorageTypeRef *create(Allocator &A,
702-
const TypeRef *Type) {
703-
FIND_OR_CREATE_TYPEREF(A, WeakStorageTypeRef, Type);
704-
}
705-
706-
static bool classof(const TypeRef *TR) {
707-
return TR->getKind() == TypeRefKind::WeakStorage;
670+
switch (TR->getKind()) {
671+
#define REF_STORAGE(Name, ...) \
672+
case TypeRefKind::Name##Storage:
673+
#include "swift/AST/ReferenceStorage.def"
674+
return true;
675+
default:
676+
return false;
677+
}
708678
}
709679
};
710680

711-
class UnmanagedStorageTypeRef final : public ReferenceStorageTypeRef {
712-
using ReferenceStorageTypeRef::Profile;
713-
public:
714-
UnmanagedStorageTypeRef(const TypeRef *Type)
715-
: ReferenceStorageTypeRef(TypeRefKind::UnmanagedStorage, Type) {}
716-
717-
template <typename Allocator>
718-
static const UnmanagedStorageTypeRef *create(Allocator &A,
719-
const TypeRef *Type) {
720-
FIND_OR_CREATE_TYPEREF(A, UnmanagedStorageTypeRef, Type);
721-
}
722-
723-
static bool classof(const TypeRef *TR) {
724-
return TR->getKind() == TypeRefKind::UnmanagedStorage;
725-
}
726-
};
681+
#define REF_STORAGE(Name, ...) \
682+
class Name##StorageTypeRef final : public ReferenceStorageTypeRef { \
683+
using ReferenceStorageTypeRef::Profile; \
684+
public: \
685+
Name##StorageTypeRef(const TypeRef *Type) \
686+
: ReferenceStorageTypeRef(TypeRefKind::Name##Storage, Type) {} \
687+
template <typename Allocator> \
688+
static const Name##StorageTypeRef *create(Allocator &A, \
689+
const TypeRef *Type) { \
690+
FIND_OR_CREATE_TYPEREF(A, Name##StorageTypeRef, Type); \
691+
} \
692+
static bool classof(const TypeRef *TR) { \
693+
return TR->getKind() == TypeRefKind::Name##Storage; \
694+
} \
695+
};
696+
#include "swift/AST/ReferenceStorage.def"
727697

728698
class SILBoxTypeRef final : public TypeRef {
729699
const TypeRef *BoxedType;

include/swift/Reflection/TypeRefBuilder.h

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -296,18 +296,11 @@ class TypeRefBuilder {
296296
return DependentMemberTypeRef::create(*this, member, base, *protocol);
297297
}
298298

299-
const UnownedStorageTypeRef *createUnownedStorageType(const TypeRef *base) {
300-
return UnownedStorageTypeRef::create(*this, base);
301-
}
302-
303-
const UnmanagedStorageTypeRef *
304-
createUnmanagedStorageType(const TypeRef *base) {
305-
return UnmanagedStorageTypeRef::create(*this, base);
306-
}
307-
308-
const WeakStorageTypeRef *createWeakStorageType(const TypeRef *base) {
309-
return WeakStorageTypeRef::create(*this, base);
299+
#define REF_STORAGE(Name, ...) \
300+
const Name##StorageTypeRef *create##Name##StorageType(const TypeRef *base) { \
301+
return Name##StorageTypeRef::create(*this, base); \
310302
}
303+
#include "swift/AST/ReferenceStorage.def"
311304

312305
const SILBoxTypeRef *createSILBoxType(const TypeRef *base) {
313306
return SILBoxTypeRef::create(*this, base);

include/swift/Reflection/TypeRefs.def

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ TYPEREF(DependentMember, TypeRef)
2929
TYPEREF(ForeignClass, TypeRef)
3030
TYPEREF(ObjCClass, TypeRef)
3131
TYPEREF(Opaque, TypeRef)
32-
TYPEREF(UnownedStorage, TypeRef)
33-
TYPEREF(UnmanagedStorage, TypeRef)
34-
TYPEREF(WeakStorage, TypeRef)
32+
#define REF_STORAGE(Name, ...) \
33+
TYPEREF(Name##Storage, TypeRef)
34+
#include "swift/AST/ReferenceStorage.def"
3535
TYPEREF(SILBox, TypeRef)
3636

3737
#undef TYPEREF

include/swift/SwiftRemoteMirror/SwiftRemoteMirrorTypes.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ typedef enum swift_layout_kind {
108108

109109
// References to other objects in the heap.
110110
SWIFT_STRONG_REFERENCE,
111-
SWIFT_UNOWNED_REFERENCE,
112-
SWIFT_WEAK_REFERENCE,
113-
SWIFT_UNMANAGED_REFERENCE,
111+
#define REF_STORAGE(Name, name, NAME) \
112+
SWIFT_##NAME##_REFERENCE,
113+
#include "swift/AST/ReferenceStorage.def"
114114

115115
// Layouts of heap objects. These are only ever returned from
116116
// swift_reflection_infoFor{Instance,Metadata}(), and not

lib/RemoteAST/RemoteAST.cpp

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -412,23 +412,13 @@ class RemoteASTTypeBuilder {
412412
return Type();
413413
}
414414

415-
Type createUnownedStorageType(Type base) {
416-
if (!base->allowsOwnership())
417-
return Type();
418-
return UnownedStorageType::get(base, Ctx);
419-
}
420-
421-
Type createUnmanagedStorageType(Type base) {
422-
if (!base->allowsOwnership())
423-
return Type();
424-
return UnmanagedStorageType::get(base, Ctx);
425-
}
426-
427-
Type createWeakStorageType(Type base) {
428-
if (!base->allowsOwnership())
429-
return Type();
430-
return WeakStorageType::get(base, Ctx);
415+
#define REF_STORAGE(Name, ...) \
416+
Type create##Name##StorageType(Type base) { \
417+
if (!base->allowsOwnership()) \
418+
return Type(); \
419+
return Name##StorageType::get(base, Ctx); \
431420
}
421+
#include "swift/AST/ReferenceStorage.def"
432422

433423
Type createSILBoxType(Type base) {
434424
return SILBoxType::get(base->getCanonicalType());

stdlib/public/Reflection/TypeLowering.cpp

Lines changed: 19 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -158,18 +158,10 @@ class PrintTypeInfo {
158158
printHeader("reference");
159159
auto &ReferenceTI = cast<ReferenceTypeInfo>(TI);
160160
switch (ReferenceTI.getReferenceKind()) {
161-
case ReferenceKind::Strong:
162-
printField("kind", "strong");
163-
break;
164-
case ReferenceKind::Unowned:
165-
printField("kind", "unowned");
166-
break;
167-
case ReferenceKind::Weak:
168-
printField("kind", "weak");
169-
break;
170-
case ReferenceKind::Unmanaged:
171-
printField("kind", "unmanaged");
172-
break;
161+
case ReferenceKind::Strong: printField("kind", "strong"); break;
162+
#define REF_STORAGE(Name, name, ...) \
163+
case ReferenceKind::Name: printField("kind", #name); break;
164+
#include "swift/AST/ReferenceStorage.def"
173165
}
174166

175167
switch (ReferenceTI.getReferenceCounting()) {
@@ -706,19 +698,12 @@ class HasFixedSize
706698
return true;
707699
}
708700

709-
bool
710-
visitUnownedStorageTypeRef(const UnownedStorageTypeRef *US) {
711-
return true;
712-
}
713-
714-
bool visitWeakStorageTypeRef(const WeakStorageTypeRef *WS) {
715-
return true;
716-
}
717-
718-
bool
719-
visitUnmanagedStorageTypeRef(const UnmanagedStorageTypeRef *US) {
720-
return true;
701+
#define REF_STORAGE(Name, ...) \
702+
bool \
703+
visit##Name##StorageTypeRef(const Name##StorageTypeRef *US) { \
704+
return true; \
721705
}
706+
#include "swift/AST/ReferenceStorage.def"
722707

723708
bool
724709
visitGenericTypeParameterTypeRef(const GenericTypeParameterTypeRef *GTP) {
@@ -840,19 +825,12 @@ class HasSingletonMetatype
840825
return MetatypeRepresentation::Unknown;
841826
}
842827

843-
MetatypeRepresentation
844-
visitUnownedStorageTypeRef(const UnownedStorageTypeRef *US) {
845-
return MetatypeRepresentation::Unknown;
846-
}
847-
848-
MetatypeRepresentation visitWeakStorageTypeRef(const WeakStorageTypeRef *WS) {
849-
return MetatypeRepresentation::Unknown;
850-
}
851-
852-
MetatypeRepresentation
853-
visitUnmanagedStorageTypeRef(const UnmanagedStorageTypeRef *US) {
854-
return MetatypeRepresentation::Unknown;
828+
#define REF_STORAGE(Name, ...) \
829+
MetatypeRepresentation \
830+
visit##Name##StorageTypeRef(const Name##StorageTypeRef *US) { \
831+
return MetatypeRepresentation::Unknown; \
855832
}
833+
#include "swift/AST/ReferenceStorage.def"
856834

857835
MetatypeRepresentation visitOpaqueTypeRef(const OpaqueTypeRef *O) {
858836
return MetatypeRepresentation::Unknown;
@@ -1255,19 +1233,12 @@ class LowerType
12551233
return rebuildStorageTypeInfo(TC.getTypeInfo(TR), Kind);
12561234
}
12571235

1258-
const TypeInfo *
1259-
visitUnownedStorageTypeRef(const UnownedStorageTypeRef *US) {
1260-
return visitAnyStorageTypeRef(US->getType(), ReferenceKind::Unowned);
1261-
}
1262-
1263-
const TypeInfo *visitWeakStorageTypeRef(const WeakStorageTypeRef *WS) {
1264-
return visitAnyStorageTypeRef(WS->getType(), ReferenceKind::Weak);
1265-
}
1266-
1267-
const TypeInfo *
1268-
visitUnmanagedStorageTypeRef(const UnmanagedStorageTypeRef *US) {
1269-
return visitAnyStorageTypeRef(US->getType(), ReferenceKind::Unmanaged);
1236+
#define REF_STORAGE(Name, name, ...) \
1237+
const TypeInfo * \
1238+
visit##Name##StorageTypeRef(const Name##StorageTypeRef *US) { \
1239+
return visitAnyStorageTypeRef(US->getType(), ReferenceKind::Name); \
12701240
}
1241+
#include "swift/AST/ReferenceStorage.def"
12711242

12721243
const TypeInfo *visitSILBoxTypeRef(const SILBoxTypeRef *SB) {
12731244
return TC.getReferenceTypeInfo(ReferenceKind::Strong,

stdlib/public/Reflection/TypeRef.cpp

Lines changed: 18 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -236,23 +236,13 @@ class PrintTypeRef : public TypeRefVisitor<PrintTypeRef, void> {
236236
OS << ')';
237237
}
238238

239-
void visitUnownedStorageTypeRef(const UnownedStorageTypeRef *US) {
240-
printHeader("unowned_storage");
241-
printRec(US->getType());
242-
OS << ')';
243-
}
244-
245-
void visitWeakStorageTypeRef(const WeakStorageTypeRef *WS) {
246-
printHeader("weak_storage");
247-
printRec(WS->getType());
248-
OS << ')';
249-
}
250-
251-
void visitUnmanagedStorageTypeRef(const UnmanagedStorageTypeRef *US) {
252-
printHeader("unmanaged_storage");
253-
printRec(US->getType());
254-
OS << ')';
239+
#define REF_STORAGE(Name, name, ...) \
240+
void visit##Name##StorageTypeRef(const Name##StorageTypeRef *US) { \
241+
printHeader(#name "_storage"); \
242+
printRec(US->getType()); \
243+
OS << ')'; \
255244
}
245+
#include "swift/AST/ReferenceStorage.def"
256246

257247
void visitSILBoxTypeRef(const SILBoxTypeRef *SB) {
258248
printHeader("sil_box");
@@ -349,17 +339,11 @@ struct TypeRefIsConcrete
349339
return true;
350340
}
351341

352-
bool visitUnownedStorageTypeRef(const UnownedStorageTypeRef *US) {
353-
return visit(US->getType());
354-
}
355-
356-
bool visitWeakStorageTypeRef(const WeakStorageTypeRef *WS) {
357-
return visit(WS->getType());
358-
}
359-
360-
bool visitUnmanagedStorageTypeRef(const UnmanagedStorageTypeRef *US) {
361-
return visit(US->getType());
342+
#define REF_STORAGE(Name, name, ...) \
343+
bool visit##Name##StorageTypeRef(const Name##StorageTypeRef *US) { \
344+
return visit(US->getType()); \
362345
}
346+
#include "swift/AST/ReferenceStorage.def"
363347

364348
bool visitSILBoxTypeRef(const SILBoxTypeRef *SB) {
365349
return visit(SB->getBoxedType());
@@ -525,18 +509,11 @@ class ThickenMetatype
525509
return OC;
526510
}
527511

528-
const TypeRef *visitUnownedStorageTypeRef(const UnownedStorageTypeRef *US) {
529-
return US;
530-
}
531-
532-
const TypeRef *visitWeakStorageTypeRef(const WeakStorageTypeRef *WS) {
533-
return WS;
534-
}
535-
536-
const TypeRef *
537-
visitUnmanagedStorageTypeRef(const UnmanagedStorageTypeRef *US) {
538-
return US;
512+
#define REF_STORAGE(Name, name, ...) \
513+
const TypeRef *visit##Name##StorageTypeRef(const Name##StorageTypeRef *US) { \
514+
return US; \
539515
}
516+
#include "swift/AST/ReferenceStorage.def"
540517

541518
const TypeRef *visitSILBoxTypeRef(const SILBoxTypeRef *SB) {
542519
return SILBoxTypeRef::create(Builder, visit(SB->getBoxedType()));
@@ -698,18 +675,11 @@ class TypeRefSubstitution
698675
return OC;
699676
}
700677

701-
const TypeRef *visitUnownedStorageTypeRef(const UnownedStorageTypeRef *US) {
702-
return UnownedStorageTypeRef::create(Builder, visit(US->getType()));
703-
}
704-
705-
const TypeRef *visitWeakStorageTypeRef(const WeakStorageTypeRef *WS) {
706-
return WeakStorageTypeRef::create(Builder, visit(WS->getType()));
707-
}
708-
709-
const TypeRef *
710-
visitUnmanagedStorageTypeRef(const UnmanagedStorageTypeRef *US) {
711-
return UnmanagedStorageTypeRef::create(Builder, visit(US->getType()));
678+
#define REF_STORAGE(Name, name, ...) \
679+
const TypeRef *visit##Name##StorageTypeRef(const Name##StorageTypeRef *US) { \
680+
return Name##StorageTypeRef::create(Builder, visit(US->getType())); \
712681
}
682+
#include "swift/AST/ReferenceStorage.def"
713683

714684
const TypeRef *visitSILBoxTypeRef(const SILBoxTypeRef *SB) {
715685
return SILBoxTypeRef::create(Builder, visit(SB->getBoxedType()));

stdlib/public/SwiftRemoteMirror/SwiftRemoteMirror.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -278,14 +278,10 @@ swift_layout_kind_t getTypeInfoKind(const TypeInfo &TI) {
278278
case TypeInfoKind::Reference: {
279279
auto &ReferenceTI = cast<ReferenceTypeInfo>(TI);
280280
switch (ReferenceTI.getReferenceKind()) {
281-
case ReferenceKind::Strong:
282-
return SWIFT_STRONG_REFERENCE;
283-
case ReferenceKind::Unowned:
284-
return SWIFT_UNOWNED_REFERENCE;
285-
case ReferenceKind::Weak:
286-
return SWIFT_WEAK_REFERENCE;
287-
case ReferenceKind::Unmanaged:
288-
return SWIFT_UNMANAGED_REFERENCE;
281+
case ReferenceKind::Strong: return SWIFT_STRONG_REFERENCE;
282+
#define REF_STORAGE(Name, name, NAME) \
283+
case ReferenceKind::Name: return SWIFT_##NAME##_REFERENCE;
284+
#include "swift/AST/ReferenceStorage.def"
289285
}
290286
}
291287
}

0 commit comments

Comments
 (0)