Skip to content

Commit 02b5e56

Browse files
authored
Merge pull request swiftlang#27369 from jckarter/type-ref-builder-remote-ref
Reflection: Traffic in RemoteRefs.
2 parents b9b4196 + 8c4df3b commit 02b5e56

File tree

11 files changed

+371
-394
lines changed

11 files changed

+371
-394
lines changed

include/swift/Reflection/Records.h

Lines changed: 10 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,11 @@ class FieldRecordFlags {
7070

7171
class FieldRecord {
7272
const FieldRecordFlags Flags;
73+
74+
public:
7375
const RelativeDirectPointer<const char> MangledTypeName;
7476
const RelativeDirectPointer<const char> FieldName;
7577

76-
public:
7778
FieldRecord() = delete;
7879

7980
bool hasMangledTypeName() const {
@@ -84,11 +85,8 @@ class FieldRecord {
8485
return Demangle::makeSymbolicMangledNameStringRef(MangledTypeName.get());
8586
}
8687

87-
StringRef getFieldName(uintptr_t Low, uintptr_t High) const {
88-
uintptr_t nameAddr = (uintptr_t)FieldName.get();
89-
if (nameAddr < Low || nameAddr > High)
90-
return "";
91-
return (const char *)nameAddr;
88+
StringRef getFieldName() const {
89+
return FieldName.get();
9290
}
9391

9492
bool isIndirectCase() const {
@@ -162,10 +160,10 @@ class FieldDescriptor {
162160
return reinterpret_cast<const FieldRecord *>(this + 1);
163161
}
164162

163+
public:
165164
const RelativeDirectPointer<const char> MangledTypeName;
166165
const RelativeDirectPointer<const char> Superclass;
167166

168-
public:
169167
FieldDescriptor() = delete;
170168

171169
const FieldDescriptorKind Kind;
@@ -227,46 +225,13 @@ class FieldDescriptor {
227225
}
228226
};
229227

230-
class FieldDescriptorIterator
231-
: public std::iterator<std::forward_iterator_tag, FieldDescriptor> {
232-
public:
233-
const void *Cur;
234-
const void * const End;
235-
FieldDescriptorIterator(const void *Cur, const void * const End)
236-
: Cur(Cur), End(End) {}
237-
238-
const FieldDescriptor &operator*() const {
239-
return *reinterpret_cast<const FieldDescriptor *>(Cur);
240-
}
241-
242-
const FieldDescriptor *operator->() const {
243-
return reinterpret_cast<const FieldDescriptor *>(Cur);
244-
}
245-
246-
FieldDescriptorIterator &operator++() {
247-
const auto &FR = this->operator*();
248-
const void *Next = reinterpret_cast<const char *>(Cur)
249-
+ sizeof(FieldDescriptor) + FR.NumFields * FR.FieldRecordSize;
250-
Cur = Next;
251-
return *this;
252-
}
253-
254-
bool operator==(FieldDescriptorIterator const &other) const {
255-
return Cur == other.Cur && End == other.End;
256-
}
257-
258-
bool operator!=(FieldDescriptorIterator const &other) const {
259-
return !(*this == other);
260-
}
261-
};
262-
263228
// Associated type records describe the mapping from an associated
264229
// type to the type witness of a conformance.
265230
class AssociatedTypeRecord {
231+
public:
266232
const RelativeDirectPointer<const char> Name;
267233
const RelativeDirectPointer<const char> SubstitutedTypeName;
268234

269-
public:
270235
StringRef getName() const {
271236
return Name.get();
272237
}
@@ -322,10 +287,9 @@ struct AssociatedTypeRecordIterator {
322287
// An associated type descriptor contains a collection of associated
323288
// type records for a conformance.
324289
struct AssociatedTypeDescriptor {
325-
private:
290+
public:
326291
const RelativeDirectPointer<const char> ConformingTypeName;
327292
const RelativeDirectPointer<const char> ProtocolTypeName;
328-
public:
329293

330294
uint32_t NumAssociatedTypes;
331295
uint32_t AssociatedTypeRecordSize;
@@ -357,46 +321,12 @@ struct AssociatedTypeDescriptor {
357321
}
358322
};
359323

360-
class AssociatedTypeIterator
361-
: public std::iterator<std::forward_iterator_tag, AssociatedTypeDescriptor> {
362-
public:
363-
const void *Cur;
364-
const void * const End;
365-
AssociatedTypeIterator(const void *Cur, const void * const End)
366-
: Cur(Cur), End(End) {}
367-
368-
const AssociatedTypeDescriptor &operator*() const {
369-
return *reinterpret_cast<const AssociatedTypeDescriptor *>(Cur);
370-
}
371-
372-
const AssociatedTypeDescriptor *operator->() const {
373-
return reinterpret_cast<const AssociatedTypeDescriptor *>(Cur);
374-
}
375-
376-
AssociatedTypeIterator &operator++() {
377-
const auto &ATR = this->operator*();
378-
size_t Size = sizeof(AssociatedTypeDescriptor) +
379-
ATR.NumAssociatedTypes * ATR.AssociatedTypeRecordSize;
380-
const void *Next = reinterpret_cast<const char *>(Cur) + Size;
381-
Cur = Next;
382-
return *this;
383-
}
384-
385-
bool operator==(AssociatedTypeIterator const &other) const {
386-
return Cur == other.Cur && End == other.End;
387-
}
388-
389-
bool operator!=(AssociatedTypeIterator const &other) const {
390-
return !(*this == other);
391-
}
392-
};
393-
394324
// Builtin type records describe basic layout information about
395325
// any builtin types referenced from the other sections.
396326
class BuiltinTypeDescriptor {
327+
public:
397328
const RelativeDirectPointer<const char> TypeName;
398329

399-
public:
400330
uint32_t Size;
401331

402332
// - Least significant 16 bits are the alignment.
@@ -424,42 +354,10 @@ class BuiltinTypeDescriptor {
424354
}
425355
};
426356

427-
class BuiltinTypeDescriptorIterator
428-
: public std::iterator<std::forward_iterator_tag, BuiltinTypeDescriptor> {
429-
public:
430-
const void *Cur;
431-
const void * const End;
432-
BuiltinTypeDescriptorIterator(const void *Cur, const void * const End)
433-
: Cur(Cur), End(End) {}
434-
435-
const BuiltinTypeDescriptor &operator*() const {
436-
return *reinterpret_cast<const BuiltinTypeDescriptor *>(Cur);
437-
}
438-
439-
const BuiltinTypeDescriptor *operator->() const {
440-
return reinterpret_cast<const BuiltinTypeDescriptor *>(Cur);;
441-
}
442-
443-
BuiltinTypeDescriptorIterator &operator++() {
444-
const void *Next = reinterpret_cast<const char *>(Cur)
445-
+ sizeof(BuiltinTypeDescriptor);
446-
Cur = Next;
447-
return *this;
448-
}
449-
450-
bool operator==(BuiltinTypeDescriptorIterator const &other) const {
451-
return Cur == other.Cur && End == other.End;
452-
}
453-
454-
bool operator!=(BuiltinTypeDescriptorIterator const &other) const {
455-
return !(*this == other);
456-
}
457-
};
458-
459357
class CaptureTypeRecord {
358+
public:
460359
const RelativeDirectPointer<const char> MangledTypeName;
461360

462-
public:
463361
CaptureTypeRecord() = delete;
464362

465363
bool hasMangledTypeName() const {
@@ -502,10 +400,10 @@ struct CaptureTypeRecordIterator {
502400
};
503401

504402
class MetadataSourceRecord {
403+
public:
505404
const RelativeDirectPointer<const char> MangledTypeName;
506405
const RelativeDirectPointer<const char> MangledMetadataSource;
507406

508-
public:
509407
MetadataSourceRecord() = delete;
510408

511409
bool hasMangledTypeName() const {
@@ -608,41 +506,6 @@ class CaptureDescriptor {
608506
}
609507
};
610508

611-
class CaptureDescriptorIterator
612-
: public std::iterator<std::forward_iterator_tag, CaptureDescriptor> {
613-
public:
614-
const void *Cur;
615-
const void * const End;
616-
CaptureDescriptorIterator(const void *Cur, const void * const End)
617-
: Cur(Cur), End(End) {}
618-
619-
const CaptureDescriptor &operator*() const {
620-
return *reinterpret_cast<const CaptureDescriptor *>(Cur);
621-
}
622-
623-
const CaptureDescriptor *operator->() const {
624-
return reinterpret_cast<const CaptureDescriptor *>(Cur);
625-
}
626-
627-
CaptureDescriptorIterator &operator++() {
628-
const auto &CR = this->operator*();
629-
const void *Next = reinterpret_cast<const char *>(Cur)
630-
+ sizeof(CaptureDescriptor)
631-
+ CR.NumCaptureTypes * sizeof(CaptureTypeRecord)
632-
+ CR.NumMetadataSources * sizeof(MetadataSourceRecord);
633-
Cur = Next;
634-
return *this;
635-
}
636-
637-
bool operator==(CaptureDescriptorIterator const &other) const {
638-
return Cur == other.Cur && End == other.End;
639-
}
640-
641-
bool operator!=(CaptureDescriptorIterator const &other) const {
642-
return !(*this == other);
643-
}
644-
};
645-
646509
} // end namespace reflection
647510
} // end namespace swift
648511

include/swift/Reflection/ReflectionContext.h

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ class ReflectionContext
206206
RangeEnd - RangeStart);
207207

208208
auto findMachOSectionByName = [&](std::string Name)
209-
-> std::pair<const char *, const char *> {
209+
-> std::pair<RemoteRef<void>, uint64_t> {
210210
for (unsigned I = 0; I < NumSect; ++I) {
211211
auto S = reinterpret_cast<typename T::Section *>(
212212
SectionsBuf + (I * sizeof(typename T::Section)));
@@ -216,10 +216,11 @@ class ReflectionContext
216216
auto SectBufData = reinterpret_cast<const char *>(SectBuf.get());
217217
auto LocalSectStart =
218218
reinterpret_cast<const char *>(SectBufData + RemoteSecStart - RangeStart);
219-
auto LocalSectEnd = reinterpret_cast<const char *>(LocalSectStart + S->size);
220-
return {LocalSectStart, LocalSectEnd};
219+
220+
auto StartRef = RemoteRef<void>(RemoteSecStart, LocalSectStart);
221+
return {StartRef, S->size};
221222
}
222-
return {nullptr, nullptr};
223+
return {nullptr, 0};
223224
};
224225

225226
auto FieldMdSec = findMachOSectionByName("__swift5_fieldmd");
@@ -298,7 +299,7 @@ class ReflectionContext
298299
sizeof(llvm::object::coff_section) * COFFFileHdr->NumberOfSections);
299300

300301
auto findCOFFSectionByName = [&](llvm::StringRef Name)
301-
-> std::pair<const char *, const char *> {
302+
-> std::pair<RemoteRef<void>, uint64_t> {
302303
for (size_t i = 0; i < COFFFileHdr->NumberOfSections; ++i) {
303304
const llvm::object::coff_section *COFFSec =
304305
reinterpret_cast<const llvm::object::coff_section *>(
@@ -313,34 +314,30 @@ class ReflectionContext
313314
auto Addr = ImageStart.getAddressData() + COFFSec->VirtualAddress;
314315
auto Buf = this->getReader().readBytes(RemoteAddress(Addr),
315316
COFFSec->VirtualSize);
316-
const char *Begin = reinterpret_cast<const char *>(Buf.get());
317-
const char *End = Begin + COFFSec->VirtualSize;
317+
auto BufStart = Buf.get();
318318
savedBuffers.push_back(std::move(Buf));
319319

320+
auto Begin = RemoteRef<void>(Addr, BufStart);
321+
auto Size = COFFSec->VirtualSize;
322+
320323
// FIXME: This code needs to be cleaned up and updated
321324
// to make it work for 32 bit platforms.
322325
if (SectionName != ".sw5cptr" && SectionName != ".sw5bltn") {
323-
Begin += 8;
324-
End -= 8;
326+
Begin = Begin.atByteOffset(8);
327+
Size -= 16;
325328
}
326329

327-
return {Begin, End};
330+
return {Begin, Size};
328331
}
329-
return {nullptr, nullptr};
332+
return {nullptr, 0};
330333
};
331334

332-
std::pair<const char *, const char *> CaptureSec =
333-
findCOFFSectionByName(".sw5cptr");
334-
std::pair<const char *, const char *> TypeRefMdSec =
335-
findCOFFSectionByName(".sw5tyrf");
336-
std::pair<const char *, const char *> FieldMdSec =
337-
findCOFFSectionByName(".sw5flmd");
338-
std::pair<const char *, const char *> AssocTySec =
339-
findCOFFSectionByName(".sw5asty");
340-
std::pair<const char *, const char *> BuiltinTySec =
341-
findCOFFSectionByName(".sw5bltn");
342-
std::pair<const char *, const char *> ReflStrMdSec =
343-
findCOFFSectionByName(".sw5rfst");
335+
auto CaptureSec = findCOFFSectionByName(".sw5cptr");
336+
auto TypeRefMdSec = findCOFFSectionByName(".sw5tyrf");
337+
auto FieldMdSec = findCOFFSectionByName(".sw5flmd");
338+
auto AssocTySec = findCOFFSectionByName(".sw5asty");
339+
auto BuiltinTySec = findCOFFSectionByName(".sw5bltn");
340+
auto ReflStrMdSec = findCOFFSectionByName(".sw5rfst");
344341

345342
if (FieldMdSec.first == nullptr &&
346343
AssocTySec.first == nullptr &&
@@ -434,7 +431,7 @@ class ReflectionContext
434431
auto StrTab = reinterpret_cast<const char *>(StrTabBuf.get());
435432

436433
auto findELFSectionByName = [&](std::string Name)
437-
-> std::pair<const char *, const char *> {
434+
-> std::pair<RemoteRef<void>, uint64_t> {
438435
// Now for all the sections, find their name.
439436
for (const typename T::Section *Hdr : SecHdrVec) {
440437
uint32_t Offset = Hdr->sh_name;
@@ -445,10 +442,12 @@ class ReflectionContext
445442
RemoteAddress(ImageStart.getAddressData() + Hdr->sh_addr);
446443
auto SecSize = Hdr->sh_size;
447444
auto SecBuf = this->getReader().readBytes(SecStart, SecSize);
448-
auto SecContents = reinterpret_cast<const char *>(SecBuf.get());
449-
return {SecContents, SecContents + SecSize};
445+
auto SecContents = RemoteRef<void>(SecStart.getAddressData(),
446+
SecBuf.get());
447+
savedBuffers.push_back(std::move(SecBuf));
448+
return {SecContents, SecSize};
450449
}
451-
return {nullptr, nullptr};
450+
return {nullptr, 0};
452451
};
453452

454453
auto FieldMdSec = findELFSectionByName("swift5_fieldmd");
@@ -634,11 +633,11 @@ class ReflectionContext
634633
//
635634
// Non-generic SIL boxes share metadata among types with compatible
636635
// layout, but we need some way to get an outgoing pointer map for them.
637-
auto *CD = getBuilder().getCaptureDescriptor(*CDAddr);
636+
auto CD = getBuilder().getCaptureDescriptor(*CDAddr);
638637
if (CD == nullptr)
639638
return nullptr;
640639

641-
auto Info = getBuilder().getClosureContextInfo(*CD);
640+
auto Info = getBuilder().getClosureContextInfo(CD);
642641

643642
return getClosureContextInfo(ObjectAddress, Info);
644643
}

0 commit comments

Comments
 (0)