@@ -41,54 +41,10 @@ class ReflectionContext
41
41
using super = remote::MetadataReader<Runtime, TypeRefBuilder>;
42
42
43
43
public:
44
- using super::decodeMangledType ;
44
+ using super::getBuilder ;
45
45
using super::readTypeFromMetadata;
46
46
using typename super::StoredPointer;
47
47
48
- private:
49
-
50
- std::vector<ReflectionInfo> ReflectionInfos;
51
-
52
- void dumpTypeRef (const std::string &MangledName,
53
- std::ostream &OS, bool printTypeName = false ) {
54
- auto TypeName = Demangle::demangleTypeAsString (MangledName);
55
- OS << TypeName << ' \n ' ;
56
-
57
- auto DemangleTree = Demangle::demangleTypeAsNode (MangledName);
58
- auto TR = decodeMangledType (DemangleTree);
59
- if (!TR) {
60
- OS << " !!! Invalid typeref: " << MangledName << ' \n ' ;
61
- return ;
62
- }
63
- TR->dump (OS);
64
- OS << ' \n ' ;
65
- }
66
-
67
- const AssociatedTypeDescriptor *
68
- lookupAssociatedTypes (const std::string &MangledTypeName,
69
- const DependentMemberTypeRef *DependentMember) {
70
- // Cache missed - we need to look through all of the assocty sections
71
- // for all images that we've been notified about.
72
- for (auto &Info : ReflectionInfos) {
73
- for (const auto &AssocTyDescriptor : Info.assocty ) {
74
- std::string ConformingTypeName (AssocTyDescriptor.ConformingTypeName );
75
- if (ConformingTypeName.compare (MangledTypeName) != 0 )
76
- continue ;
77
- std::string ProtocolMangledName (AssocTyDescriptor.ProtocolTypeName );
78
- auto DemangledProto = Demangle::demangleTypeAsNode (ProtocolMangledName);
79
- auto TR = decodeMangledType (DemangledProto);
80
-
81
- auto &Conformance = *DependentMember->getProtocol ();
82
- if (auto Protocol = dyn_cast<ProtocolTypeRef>(TR)) {
83
- if (*Protocol != Conformance)
84
- continue ;
85
- return &AssocTyDescriptor;
86
- }
87
- }
88
- }
89
- return nullptr ;
90
- }
91
-
92
48
public:
93
49
94
50
explicit ReflectionContext (std::shared_ptr<MemoryReader> reader)
@@ -101,137 +57,13 @@ class ReflectionContext
101
57
return *this ->Reader ;
102
58
}
103
59
104
- void dumpFieldSection (std::ostream &OS) {
105
- for (const auto §ions : ReflectionInfos) {
106
- for (const auto &descriptor : sections.fieldmd ) {
107
- auto TypeName
108
- = Demangle::demangleTypeAsString (descriptor.getMangledTypeName ());
109
- OS << TypeName << ' \n ' ;
110
- for (size_t i = 0 ; i < TypeName.size (); ++i)
111
- OS << ' -' ;
112
- OS << ' \n ' ;
113
- for (auto &field : descriptor) {
114
- OS << field.getFieldName ();
115
- if (field.hasMangledTypeName ()) {
116
- OS << " : " ;
117
- dumpTypeRef (field.getMangledTypeName (), OS);
118
- } else {
119
- OS << " \n\n " ;
120
- }
121
- }
122
- }
123
- }
124
- }
125
-
126
- void dumpAssociatedTypeSection (std::ostream &OS) {
127
- for (const auto §ions : ReflectionInfos) {
128
- for (const auto &descriptor : sections.assocty ) {
129
- auto conformingTypeName = Demangle::demangleTypeAsString (
130
- descriptor.getMangledConformingTypeName ());
131
- auto protocolName = Demangle::demangleTypeAsString (
132
- descriptor.getMangledProtocolTypeName ());
133
-
134
- OS << " - " << conformingTypeName << " : " << protocolName;
135
- OS << ' \n ' ;
136
-
137
- for (const auto &associatedType : descriptor) {
138
- OS << " typealias " << associatedType.getName () << " = " ;
139
- dumpTypeRef (associatedType.getMangledSubstitutedTypeName (), OS);
140
- }
141
- }
142
- }
143
- }
144
-
145
- void dumpBuiltinTypeSection (std::ostream &OS) {
146
- for (const auto §ions : ReflectionInfos) {
147
- for (const auto &descriptor : sections.builtin ) {
148
- auto typeName = Demangle::demangleTypeAsString (
149
- descriptor.getMangledTypeName ());
150
-
151
- OS << " \n - " << typeName << " :\n " ;
152
- OS << " Size: " << descriptor.Size << " \n " ;
153
- OS << " Alignment: " << descriptor.Alignment << " \n " ;
154
- OS << " Stride: " << descriptor.Stride << " \n " ;
155
- OS << " NumExtraInhabitants: " << descriptor.NumExtraInhabitants << " \n " ;
156
- }
157
- }
158
- }
159
-
160
60
void dumpAllSections (std::ostream &OS) {
161
- OS << " FIELDS:\n " ;
162
- OS << " =======\n " ;
163
- dumpFieldSection (OS);
164
- OS << ' \n ' ;
165
- OS << " ASSOCIATED TYPES:\n " ;
166
- OS << " =================\n " ;
167
- dumpAssociatedTypeSection (OS);
168
- OS << ' \n ' ;
169
- OS << " BUILTIN TYPES:\n " ;
170
- OS << " ==============\n " ;
171
- dumpBuiltinTypeSection (OS);
172
- OS << ' \n ' ;
173
- }
174
-
175
- const TypeRef *
176
- getDependentMemberTypeRef (const std::string &MangledTypeName,
177
- const DependentMemberTypeRef *DependentMember) {
178
-
179
- if (auto AssocTys = lookupAssociatedTypes (MangledTypeName, DependentMember)) {
180
- for (auto &AssocTy : *AssocTys) {
181
- if (DependentMember->getMember ().compare (AssocTy.getName ()) != 0 )
182
- continue ;
183
-
184
- auto SubstitutedTypeName = AssocTy.getMangledSubstitutedTypeName ();
185
- auto Demangled = Demangle::demangleTypeAsNode (SubstitutedTypeName);
186
- return decodeMangledType (Demangled);
187
- }
188
- }
189
- return nullptr ;
61
+ getBuilder ().dumpAllSections ();
190
62
}
191
63
192
64
std::vector<std::pair<std::string, const TypeRef *>>
193
65
getFieldTypeRefs (const TypeRef *TR) {
194
- std::string MangledName;
195
- if (auto N = dyn_cast<NominalTypeRef>(TR))
196
- MangledName = N->getMangledName ();
197
- else if (auto BG = dyn_cast<BoundGenericTypeRef>(TR))
198
- MangledName = BG->getMangledName ();
199
- else
200
- return {};
201
-
202
- auto Subs = TR->getSubstMap ();
203
-
204
- std::vector<std::pair<std::string, const TypeRef *>> Fields;
205
- for (auto Info : ReflectionInfos) {
206
- for (auto &FieldDescriptor : Info.fieldmd ) {
207
- auto CandidateMangledName = FieldDescriptor.MangledTypeName .get ();
208
- if (!CandidateMangledName)
209
- continue ;
210
- if (MangledName.compare (CandidateMangledName) != 0 )
211
- continue ;
212
- for (auto &Field : FieldDescriptor) {
213
- auto FieldName = Field.getFieldName ();
214
-
215
- // Empty cases of enums do not have a type
216
- if (!Field.hasMangledTypeName ()) {
217
- Fields.push_back ({FieldName, nullptr });
218
- continue ;
219
- }
220
-
221
- auto Demangled
222
- = Demangle::demangleTypeAsNode (Field.getMangledTypeName ());
223
- auto Unsubstituted = decodeMangledType (Demangled);
224
- if (!Unsubstituted)
225
- return {};
226
-
227
- auto Substituted = Unsubstituted->subst (*this , Subs);
228
- if (FieldName.empty ())
229
- FieldName = " <Redacted Field Name>" ;
230
- Fields.push_back ({FieldName, Substituted});
231
- }
232
- }
233
- }
234
- return Fields;
66
+ return getBuilder ().getFieldTypeRefs (TR);
235
67
}
236
68
237
69
std::vector<std::pair<std::string, const TypeRef *>>
@@ -241,7 +73,7 @@ class ReflectionContext
241
73
}
242
74
243
75
void addReflectionInfo (ReflectionInfo I) {
244
- ReflectionInfos. push_back (I);
76
+ getBuilder (). addReflectionInfo (I);
245
77
}
246
78
247
79
swift_typeinfo_t getInfoForTypeRef (const TypeRef *TR) {
0 commit comments