Skip to content

Commit a91becc

Browse files
committed
Reflection: Move some code from TypeRefBuilder.h to TypeRefBuilder.cpp, NFC
Now that we no longer need the template parameter everywhere we can start moving code out of header files.
1 parent 3936cd2 commit a91becc

File tree

3 files changed

+214
-164
lines changed

3 files changed

+214
-164
lines changed

include/swift/Reflection/TypeRefBuilder.h

Lines changed: 9 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -215,181 +215,26 @@ class TypeRefBuilder {
215215

216216
const AssociatedTypeDescriptor *
217217
lookupAssociatedTypes(const std::string &MangledTypeName,
218-
const DependentMemberTypeRef *DependentMember) {
219-
// Cache missed - we need to look through all of the assocty sections
220-
// for all images that we've been notified about.
221-
for (auto &Info : ReflectionInfos) {
222-
for (const auto &AssocTyDescriptor : Info.assocty) {
223-
std::string ConformingTypeName(AssocTyDescriptor.ConformingTypeName);
224-
if (ConformingTypeName.compare(MangledTypeName) != 0)
225-
continue;
226-
std::string ProtocolMangledName(AssocTyDescriptor.ProtocolTypeName);
227-
auto DemangledProto = Demangle::demangleTypeAsNode(ProtocolMangledName);
228-
auto TR = swift::remote::decodeMangledType(*this, DemangledProto);
229-
230-
auto &Conformance = *DependentMember->getProtocol();
231-
if (auto Protocol = dyn_cast<ProtocolTypeRef>(TR)) {
232-
if (*Protocol != Conformance)
233-
continue;
234-
return &AssocTyDescriptor;
235-
}
236-
}
237-
}
238-
return nullptr;
239-
}
218+
const DependentMemberTypeRef *DependentMember);
240219

241-
public:
220+
public:
242221
const TypeRef *
243222
getDependentMemberTypeRef(const std::string &MangledTypeName,
244-
const DependentMemberTypeRef *DependentMember) {
245-
246-
if (auto AssocTys = lookupAssociatedTypes(MangledTypeName, DependentMember)) {
247-
for (auto &AssocTy : *AssocTys) {
248-
if (DependentMember->getMember().compare(AssocTy.getName()) != 0)
249-
continue;
250-
251-
auto SubstitutedTypeName = AssocTy.getMangledSubstitutedTypeName();
252-
auto Demangled = Demangle::demangleTypeAsNode(SubstitutedTypeName);
253-
return swift::remote::decodeMangledType(*this, Demangled);
254-
}
255-
}
256-
return nullptr;
257-
}
223+
const DependentMemberTypeRef *DependentMember);
258224

259225
std::vector<std::pair<std::string, const TypeRef *>>
260-
getFieldTypeRefs(const TypeRef *TR) {
261-
std::string MangledName;
262-
if (auto N = dyn_cast<NominalTypeRef>(TR))
263-
MangledName = N->getMangledName();
264-
else if (auto BG = dyn_cast<BoundGenericTypeRef>(TR))
265-
MangledName = BG->getMangledName();
266-
else
267-
return {};
268-
269-
auto Subs = TR->getSubstMap();
270-
271-
std::vector<std::pair<std::string, const TypeRef *>> Fields;
272-
for (auto Info : ReflectionInfos) {
273-
for (auto &FieldDescriptor : Info.fieldmd) {
274-
auto CandidateMangledName = FieldDescriptor.MangledTypeName.get();
275-
if (!CandidateMangledName)
276-
continue;
277-
if (MangledName.compare(CandidateMangledName) != 0)
278-
continue;
279-
for (auto &Field : FieldDescriptor) {
280-
auto FieldName = Field.getFieldName();
281-
282-
// Empty cases of enums do not have a type
283-
if (!Field.hasMangledTypeName()) {
284-
Fields.push_back({FieldName, nullptr});
285-
continue;
286-
}
287-
288-
auto Demangled
289-
= Demangle::demangleTypeAsNode(Field.getMangledTypeName());
290-
auto Unsubstituted = swift::remote::decodeMangledType(*this, Demangled);
291-
if (!Unsubstituted)
292-
return {};
293-
294-
auto Substituted = Unsubstituted->subst(*this, Subs);
295-
if (FieldName.empty())
296-
FieldName = "<Redacted Field Name>";
297-
Fields.push_back({FieldName, Substituted});
298-
}
299-
}
300-
}
301-
return Fields;
302-
}
226+
getFieldTypeRefs(const TypeRef *TR);
303227

304228
///
305229
/// Dumping typerefs, field declarations, associated types
306230
///
307231

308232
void dumpTypeRef(const std::string &MangledName,
309-
std::ostream &OS, bool printTypeName = false) {
310-
auto TypeName = Demangle::demangleTypeAsString(MangledName);
311-
OS << TypeName << '\n';
312-
313-
auto DemangleTree = Demangle::demangleTypeAsNode(MangledName);
314-
auto TR = swift::remote::decodeMangledType(*this, DemangleTree);
315-
if (!TR) {
316-
OS << "!!! Invalid typeref: " << MangledName << '\n';
317-
return;
318-
}
319-
TR->dump(OS);
320-
OS << '\n';
321-
}
322-
323-
void dumpFieldSection(std::ostream &OS) {
324-
for (const auto &sections : ReflectionInfos) {
325-
for (const auto &descriptor : sections.fieldmd) {
326-
auto TypeName
327-
= Demangle::demangleTypeAsString(descriptor.getMangledTypeName());
328-
OS << TypeName << '\n';
329-
for (size_t i = 0; i < TypeName.size(); ++i)
330-
OS << '-';
331-
OS << '\n';
332-
for (auto &field : descriptor) {
333-
OS << field.getFieldName();
334-
if (field.hasMangledTypeName()) {
335-
OS << ": ";
336-
dumpTypeRef(field.getMangledTypeName(), OS);
337-
} else {
338-
OS << "\n\n";
339-
}
340-
}
341-
}
342-
}
343-
}
344-
345-
void dumpAssociatedTypeSection(std::ostream &OS) {
346-
for (const auto &sections : ReflectionInfos) {
347-
for (const auto &descriptor : sections.assocty) {
348-
auto conformingTypeName = Demangle::demangleTypeAsString(
349-
descriptor.getMangledConformingTypeName());
350-
auto protocolName = Demangle::demangleTypeAsString(
351-
descriptor.getMangledProtocolTypeName());
352-
353-
OS << "- " << conformingTypeName << " : " << protocolName;
354-
OS << '\n';
355-
356-
for (const auto &associatedType : descriptor) {
357-
OS << "typealias " << associatedType.getName() << " = ";
358-
dumpTypeRef(associatedType.getMangledSubstitutedTypeName(), OS);
359-
}
360-
}
361-
}
362-
}
363-
364-
void dumpBuiltinTypeSection(std::ostream &OS) {
365-
for (const auto &sections : ReflectionInfos) {
366-
for (const auto &descriptor : sections.builtin) {
367-
auto typeName = Demangle::demangleTypeAsString(
368-
descriptor.getMangledTypeName());
369-
370-
OS << "\n- " << typeName << ":\n";
371-
OS << "Size: " << descriptor.Size << "\n";
372-
OS << "Alignment: " << descriptor.Alignment << "\n";
373-
OS << "Stride: " << descriptor.Stride << "\n";
374-
OS << "NumExtraInhabitants: " << descriptor.NumExtraInhabitants << "\n";
375-
}
376-
}
377-
}
378-
379-
void dumpAllSections(std::ostream &OS) {
380-
OS << "FIELDS:\n";
381-
OS << "=======\n";
382-
dumpFieldSection(OS);
383-
OS << '\n';
384-
OS << "ASSOCIATED TYPES:\n";
385-
OS << "=================\n";
386-
dumpAssociatedTypeSection(OS);
387-
OS << '\n';
388-
OS << "BUILTIN TYPES:\n";
389-
OS << "==============\n";
390-
dumpBuiltinTypeSection(OS);
391-
OS << '\n';
392-
}
233+
std::ostream &OS, bool printTypeName = false);
234+
void dumpFieldSection(std::ostream &OS);
235+
void dumpAssociatedTypeSection(std::ostream &OS);
236+
void dumpBuiltinTypeSection(std::ostream &OS);
237+
void dumpAllSections(std::ostream &OS);
393238
};
394239

395240

stdlib/public/Reflection/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ add_swift_library(swiftReflection IS_STDLIB IS_HOST
33
MetadataSource.cpp
44
Remangle.cpp
55
TypeRef.cpp
6+
TypeRefBuilder.cpp
67
INSTALL_IN_COMPONENT dev)

0 commit comments

Comments
 (0)