Skip to content

Commit 0474387

Browse files
authored
Merge pull request #30355 from mikeash/remote-mirror-type-name-api
2 parents 94fdce7 + 625768e commit 0474387

File tree

8 files changed

+145
-50
lines changed

8 files changed

+145
-50
lines changed

include/swift/SwiftRemoteMirror/SwiftRemoteMirror.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,16 @@ swift_reflection_typeRefForMangledTypeName(SwiftReflectionContextRef ContextRef,
149149
const char *MangledName,
150150
uint64_t Length);
151151

152+
/// Returns the demangled name for a typeref, or NULL if the name couldn't be
153+
/// created.
154+
///
155+
/// The returned string is heap allocated and the caller must free() it when
156+
/// done.
157+
SWIFT_REMOTE_MIRROR_LINKAGE
158+
char *
159+
swift_reflection_copyDemangledNameForTypeRef(
160+
SwiftReflectionContextRef ContextRef, swift_typeref_t OpaqueTypeRef);
161+
152162
/// Returns a structure describing the layout of a value of a typeref.
153163
/// For classes, this returns the reference value itself.
154164
SWIFT_REMOTE_MIRROR_LINKAGE

include/swift/SwiftRemoteMirror/SwiftRemoteMirrorLegacyInterop.h

Lines changed: 55 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ swift_reflection_interop_typeRefForMangledTypeName(
101101
const char *MangledName,
102102
uint64_t Length);
103103

104+
static inline char *
105+
swift_reflection_interop_copyDemangledNameForTypeRef(
106+
SwiftReflectionInteropContextRef ContextRef,
107+
swift_typeref_interop_t OpaqueTypeRef);
108+
104109
static inline swift_typeinfo_t
105110
swift_reflection_interop_infoForTypeRef(SwiftReflectionInteropContextRef ContextRef,
106111
swift_typeref_interop_t OpaqueTypeRef);
@@ -250,6 +255,9 @@ struct SwiftReflectionFunctions {
250255
const char *MangledName,
251256
uint64_t Length);
252257

258+
char * (*copyDemangledNameForTypeRef)(
259+
SwiftReflectionContextRef ContextRef, swift_typeref_t OpaqueTypeRef);
260+
253261
swift_typeinfo_t (*infoForTypeRef)(SwiftReflectionContextRef ContextRef,
254262
swift_typeref_t OpaqueTypeRef);
255263

@@ -354,7 +362,7 @@ swift_reflection_interop_libraryOwnsAddress(
354362
// Search the images list to see if the address is in one of them.
355363
struct SwiftReflectionInteropContextLegacyImageRangeList *Node =
356364
ContextRef->LegacyImageRangeList;
357-
while (Node != nullptr) {
365+
while (Node != NULL) {
358366
if (Node->Start <= Address && Address < Node->End)
359367
return 1;
360368
Node = Node->Next;
@@ -381,7 +389,7 @@ swift_reflection_interop_libraryForAddress(
381389
return Library;
382390
}
383391
}
384-
return nullptr;
392+
return NULL;
385393
}
386394

387395
static inline uintptr_t
@@ -409,7 +417,7 @@ swift_reflection_interop_libraryForObject(
409417
if (Library->IsLegacy)
410418
return Library;
411419
}
412-
return nullptr;
420+
return NULL;
413421
}
414422

415423
return swift_reflection_interop_libraryForAddress(ContextRef, Metadata);
@@ -418,7 +426,7 @@ swift_reflection_interop_libraryForObject(
418426
static inline int
419427
swift_reflection_interop_loadFunctions(struct SwiftReflectionInteropContext *Context,
420428
void *Handle) {
421-
if (Handle == nullptr)
429+
if (Handle == NULL)
422430
return 0;
423431

424432
struct SwiftReflectionInteropContextLibrary *Library = &Context
@@ -430,7 +438,7 @@ swift_reflection_interop_loadFunctions(struct SwiftReflectionInteropContext *Con
430438
#endif
431439
#define LOAD_NAMED(field, symbol, required) do { \
432440
Functions->field = (decltype(Functions->field))dlsym(Handle, symbol); \
433-
if (required && Functions->field == nullptr) return 0; \
441+
if (required && Functions->field == NULL) return 0; \
434442
} while (0)
435443
#define LOAD(name) LOAD_NAMED(name, "swift_reflection_" #name, 1)
436444
#define LOAD_OPT(name) LOAD_NAMED(name, "swift_reflection_" #name, 0)
@@ -442,7 +450,7 @@ swift_reflection_interop_loadFunctions(struct SwiftReflectionInteropContext *Con
442450
if (version < SWIFT_LEGACY_METADATA_MIN_VERSION)
443451
return 0;
444452

445-
int IsLegacy = dlsym(Handle, "swift_reflection_addImage") == nullptr;
453+
int IsLegacy = dlsym(Handle, "swift_reflection_addImage") == NULL;
446454

447455
if (IsLegacy) {
448456
LOAD_NAMED(createReflectionContextLegacy, "swift_reflection_createReflectionContext", 1);
@@ -464,6 +472,7 @@ swift_reflection_interop_loadFunctions(struct SwiftReflectionInteropContext *Con
464472
LOAD(typeRefForMetadata);
465473
LOAD(typeRefForInstance);
466474
LOAD(typeRefForMangledTypeName);
475+
LOAD_OPT(copyDemangledNameForTypeRef);
467476
LOAD(infoForTypeRef);
468477
LOAD(childOfTypeRef);
469478
LOAD(infoForMetadata);
@@ -499,11 +508,11 @@ swift_reflection_interop_readBytesAdapter(void *reader_context,
499508
void *FreeContext;
500509
const void *ptr = Context->ReadBytes(Context->ReaderContext, address, size,
501510
&FreeContext);
502-
if (ptr == nullptr)
511+
if (ptr == NULL)
503512
return 0;
504513

505514
memcpy(dest, ptr, size);
506-
if (Context->FreeBytes != nullptr)
515+
if (Context->FreeBytes != NULL)
507516
Context->FreeBytes(Context->ReaderContext, ptr, FreeContext);
508517
return 1;
509518
}
@@ -536,6 +545,8 @@ swift_reflection_interop_minimalDataLayoutQueryFunction4(
536545
void *ReaderContext,
537546
DataLayoutQueryType type,
538547
void *inBuffer, void *outBuffer) {
548+
(void)ReaderContext;
549+
(void)inBuffer;
539550
switch (type) {
540551
case DLQ_GetPointerSize:
541552
case DLQ_GetSizeSize: {
@@ -565,17 +576,19 @@ swift_reflection_interop_minimalDataLayoutQueryFunction8(
565576
void *ReaderContext,
566577
DataLayoutQueryType type,
567578
void *inBuffer, void *outBuffer) {
579+
(void)ReaderContext;
580+
(void)inBuffer;
568581
// Caveat: This assumes the process being examined is
569582
// running in the same kind of environment as this host code.
570583
#if defined(__APPLE__) && __APPLE__
571-
auto applePlatform = true;
584+
int applePlatform = 1;
572585
#else
573-
auto applePlatform = false;
586+
int applePlatform = 0;
574587
#endif
575588
#if defined(__APPLE__) && __APPLE__ && ((defined(TARGET_OS_IOS) && TARGET_OS_IOS) || (defined(TARGET_OS_IOS) && TARGET_OS_WATCH) || (defined(TARGET_OS_TV) && TARGET_OS_TV))
576-
auto iosDerivedPlatform = true;
589+
int iosDerivedPlatform = 1;
577590
#else
578-
auto iosDerivedPlatform = false;
591+
int iosDerivedPlatform = 0;
579592
#endif
580593

581594
switch (type) {
@@ -628,7 +641,7 @@ swift_reflection_interop_createReflectionContext(
628641

629642
return swift_reflection_interop_createReflectionContextWithDataLayout(
630643
ReaderContext,
631-
nullptr,
644+
DataLayout,
632645
FreeBytes,
633646
ReadBytes,
634647
GetStringLength,
@@ -654,7 +667,7 @@ swift_reflection_interop_createReflectionContextWithDataLayout(
654667
ContextRef->GetStringLength = GetStringLength;
655668
ContextRef->GetSymbolAddress = GetSymbolAddress;
656669

657-
ContextRef->AddressToLibraryCache = CFDictionaryCreateMutable(nullptr, 0, nullptr, nullptr);
670+
ContextRef->AddressToLibraryCache = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
658671

659672
return ContextRef;
660673
}
@@ -687,7 +700,7 @@ swift_reflection_interop_addLibrary(
687700
} else {
688701
uint8_t PointerSize;
689702
int result = ContextRef->DataLayout(
690-
ContextRef->ReaderContext, DLQ_GetPointerSize, nullptr, &PointerSize);
703+
ContextRef->ReaderContext, DLQ_GetPointerSize, NULL, &PointerSize);
691704
if (!result)
692705
abort(); // We need the pointer size, can't proceed without it.
693706

@@ -712,14 +725,14 @@ swift_reflection_interop_destroyReflectionContext(
712725
free(ContextRef->Libraries);
713726
struct SwiftReflectionInteropContextLegacyImageRangeList *LegacyImageRangeList
714727
= ContextRef->LegacyImageRangeList;
715-
while (LegacyImageRangeList != nullptr) {
728+
while (LegacyImageRangeList != NULL) {
716729
struct SwiftReflectionInteropContextLegacyImageRangeList *Next
717730
= LegacyImageRangeList->Next;
718731
free(LegacyImageRangeList);
719732
LegacyImageRangeList = Next;
720733
}
721734
struct SwiftReflectionInteropContextFreeList *FreeList = ContextRef->FreeList;
722-
while (FreeList != nullptr) {
735+
while (FreeList != NULL) {
723736
ContextRef->FreeBytes(ContextRef->ReaderContext,
724737
FreeList->Pointer, FreeList->Context);
725738
struct SwiftReflectionInteropContextFreeList *Next = FreeList->Next;
@@ -771,37 +784,37 @@ swift_reflection_interop_addImageLegacy(
771784
ImageStart,
772785
sizeof(MachHeader),
773786
&FreeContext);
774-
if (Buf == nullptr)
787+
if (Buf == NULL)
775788
return 0;
776789

777790
MachHeader *Header = (MachHeader *)Buf;
778791

779792
if (Header->magic != MH_MAGIC && Header->magic != MH_MAGIC_64) {
780-
if (ContextRef->FreeBytes != nullptr)
793+
if (ContextRef->FreeBytes != NULL)
781794
ContextRef->FreeBytes(ContextRef->ReaderContext, Buf, FreeContext);
782795
return 0;
783796
}
784797

785798
// Read the commands.
786799
uint32_t Length = Header->sizeofcmds;
787-
if (ContextRef->FreeBytes != nullptr)
800+
if (ContextRef->FreeBytes != NULL)
788801
ContextRef->FreeBytes(ContextRef->ReaderContext, Buf, FreeContext);
789802

790803
Buf = ContextRef->ReadBytes(ContextRef->ReaderContext,
791804
ImageStart,
792805
Length,
793806
&FreeContext);
794-
if (Buf == nullptr)
807+
if (Buf == NULL)
795808
return 0;
796809
Header = (MachHeader *)Buf;
797810

798811
// Find the TEXT segment and figure out where the end is.
799812
unsigned long TextSize;
800813
uint8_t *TextSegment = getsegmentdata(Header, "__TEXT", &TextSize);
801-
if (ContextRef->FreeBytes != nullptr)
814+
if (ContextRef->FreeBytes != NULL)
802815
ContextRef->FreeBytes(ContextRef->ReaderContext, Buf, FreeContext);
803816

804-
if (TextSegment == nullptr) {
817+
if (TextSegment == NULL) {
805818
return 0;
806819
}
807820
unsigned long TextEnd = TextSegment - (uint8_t *)Buf + TextSize;
@@ -811,7 +824,7 @@ swift_reflection_interop_addImageLegacy(
811824
ImageStart,
812825
TextEnd,
813826
&FreeContext);
814-
if (Buf == nullptr)
827+
if (Buf == NULL)
815828
return 0;
816829
Header = (MachHeader *)Buf;
817830

@@ -839,7 +852,7 @@ swift_reflection_interop_addImageLegacy(
839852
&info.reflstr) || success;
840853

841854
if (!success) {
842-
if (ContextRef->FreeBytes != nullptr)
855+
if (ContextRef->FreeBytes != NULL)
843856
ContextRef->FreeBytes(ContextRef->ReaderContext, Buf, FreeContext);
844857
return 0;
845858
}
@@ -863,7 +876,7 @@ swift_reflection_interop_addImageLegacy(
863876

864877
// If the buffer needs to be freed, save buffer and free context to free it when the
865878
// reflection context is destroyed.
866-
if (ContextRef->FreeBytes != nullptr) {
879+
if (ContextRef->FreeBytes != NULL) {
867880
struct SwiftReflectionInteropContextFreeList *FreeListNode =
868881
(struct SwiftReflectionInteropContextFreeList *)malloc(sizeof(*FreeListNode));
869882
FreeListNode->Next = ContextRef->FreeList;
@@ -911,7 +924,7 @@ swift_reflection_interop_lookupMetadata(SwiftReflectionInteropContextRef Context
911924
swift_metadata_interop_t Result = {};
912925
struct SwiftReflectionInteropContextLibrary *Library =
913926
swift_reflection_interop_libraryForAddress(ContextRef, Metadata);
914-
if (Library != nullptr) {
927+
if (Library != NULL) {
915928
Result.Metadata = Metadata;
916929
Result.Library = (int)LIBRARY_INDEX;
917930
}
@@ -935,7 +948,7 @@ swift_reflection_interop_typeRefForInstance(SwiftReflectionInteropContextRef Con
935948
swift_typeref_interop_t Result = {};
936949
struct SwiftReflectionInteropContextLibrary *Library
937950
= swift_reflection_interop_libraryForObject(ContextRef, Object);
938-
if (Library != nullptr) {
951+
if (Library != NULL) {
939952
swift_typeref_t Typeref = Library->Functions.typeRefForInstance(Library->Context,
940953
Object);
941954
Result.Typeref = Typeref;
@@ -966,6 +979,17 @@ swift_reflection_interop_typeRefForMangledTypeName(
966979
return Result;
967980
}
968981

982+
static inline char *
983+
swift_reflection_interop_copyDemangledNameForTypeRef(
984+
SwiftReflectionInteropContextRef ContextRef,
985+
swift_typeref_interop_t OpaqueTypeRef) {
986+
DECLARE_LIBRARY(OpaqueTypeRef.Library);
987+
if (Library->Functions.copyDemangledNameForTypeRef)
988+
return Library->Functions.copyDemangledNameForTypeRef(Library->Context,
989+
OpaqueTypeRef.Typeref);
990+
return NULL;
991+
}
992+
969993
static inline swift_typeinfo_t
970994
swift_reflection_interop_infoForTypeRef(SwiftReflectionInteropContextRef ContextRef,
971995
swift_typeref_interop_t OpaqueTypeRef) {
@@ -1021,7 +1045,7 @@ swift_reflection_interop_infoForInstance(SwiftReflectionInteropContextRef Contex
10211045
struct SwiftReflectionInteropContextLibrary *Library
10221046
= swift_reflection_interop_libraryForObject(ContextRef, Object);
10231047

1024-
if (Library != nullptr) {
1048+
if (Library != NULL) {
10251049
Result = Library->Functions.infoForInstance(Library->Context, Object);
10261050
} else {
10271051
Result.Kind = SWIFT_UNKNOWN;
@@ -1037,7 +1061,7 @@ swift_reflection_interop_childOfInstance(SwiftReflectionInteropContextRef Contex
10371061
swift_childinfo_interop_t Result = {};
10381062
struct SwiftReflectionInteropContextLibrary *Library
10391063
= swift_reflection_interop_libraryForObject(ContextRef, Object);
1040-
if (Library != nullptr) {
1064+
if (Library != NULL) {
10411065
swift_childinfo_t LibResult = Library->Functions.childOfInstance(Library->Context,
10421066
Object, Index);
10431067
Result.Name = LibResult.Name;
@@ -1115,7 +1139,7 @@ swift_reflection_interop_dumpInfoForInstance(SwiftReflectionInteropContextRef Co
11151139
uintptr_t Object) {
11161140
struct SwiftReflectionInteropContextLibrary *Library
11171141
= swift_reflection_interop_libraryForObject(ContextRef, Object);
1118-
if (Library != nullptr) {
1142+
if (Library != NULL) {
11191143
Library->Functions.dumpInfoForInstance(Library->Context, Object);
11201144
}
11211145
}

stdlib/public/Reflection/TypeRef.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,16 @@ class DemanglingForTypeRef
383383
public:
384384
DemanglingForTypeRef(Demangle::Demangler &Dem) : Dem(Dem) {}
385385

386+
Demangle::NodePointer visit(const TypeRef *typeRef) {
387+
auto node = TypeRefVisitor<DemanglingForTypeRef,
388+
Demangle::NodePointer>::visit(typeRef);
389+
390+
// Wrap all nodes in a Type node, as consumers generally expect.
391+
auto typeNode = Dem.createNode(Node::Kind::Type);
392+
typeNode->addChild(node, Dem);
393+
return typeNode;
394+
}
395+
386396
Demangle::NodePointer visitBuiltinTypeRef(const BuiltinTypeRef *B) {
387397
return Dem.demangleType(B->getMangledName());
388398
}
@@ -423,9 +433,7 @@ class DemanglingForTypeRef
423433
if (auto parent = BG->getParent())
424434
assert(false && "not implemented");
425435

426-
auto top = Dem.createNode(Node::Kind::Type);
427-
top->addChild(genericNode, Dem);
428-
return top;
436+
return genericNode;
429437
}
430438

431439
Demangle::NodePointer visitTupleTypeRef(const TupleTypeRef *T) {
@@ -576,18 +584,14 @@ class DemanglingForTypeRef
576584
node = Dem.createNode(Node::Kind::ProtocolListWithAnyObject);
577585
node->addChild(proto_list, Dem);
578586
}
579-
auto typeNode = Dem.createNode(Node::Kind::Type);
580-
typeNode->addChild(node, Dem);
581-
return typeNode;
587+
return node;
582588
}
583589

584590
Demangle::NodePointer visitMetatypeTypeRef(const MetatypeTypeRef *M) {
585591
auto node = Dem.createNode(Node::Kind::Metatype);
586592
assert(!M->wasAbstract() && "not implemented");
587593
node->addChild(visit(M->getInstanceType()), Dem);
588-
auto typeNode = Dem.createNode(Node::Kind::Type);
589-
typeNode->addChild(node, Dem);
590-
return typeNode;
594+
return node;
591595
}
592596

593597
Demangle::NodePointer
@@ -618,8 +622,7 @@ class DemanglingForTypeRef
618622
}
619623

620624
Demangle::NodePointer visitForeignClassTypeRef(const ForeignClassTypeRef *F) {
621-
assert(false && "not implemented");
622-
return nullptr;
625+
return Dem.demangleType(F->getName());
623626
}
624627

625628
Demangle::NodePointer visitObjCClassTypeRef(const ObjCClassTypeRef *OC) {
@@ -636,9 +639,7 @@ class DemanglingForTypeRef
636639
auto node = Dem.createNode(Node::Kind::Protocol);
637640
node->addChild(module, Dem);
638641
node->addChild(Dem.createNode(Node::Kind::Identifier, OC->getName()), Dem);
639-
auto typeNode = Dem.createNode(Node::Kind::Type);
640-
typeNode->addChild(node, Dem);
641-
return typeNode;
642+
return node;
642643
}
643644

644645
#define REF_STORAGE(Name, name, ...) \

0 commit comments

Comments
 (0)