Skip to content

Commit 9e9acd2

Browse files
committed
Reflection: Add RecordTypeInfoBuilder class to TypeLowering.h, NFC
Closure context layout uses this class.
1 parent e09be3b commit 9e9acd2

File tree

2 files changed

+73
-63
lines changed

2 files changed

+73
-63
lines changed

include/swift/Reflection/TypeLowering.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,29 @@ class TypeConverter {
202202
}
203203
};
204204

205+
/// Utility class for performing universal layout for types such as
206+
/// tuples, structs, thick functions, etc.
207+
class RecordTypeInfoBuilder {
208+
TypeConverter &TC;
209+
unsigned Size, Alignment, Stride, NumExtraInhabitants;
210+
RecordKind Kind;
211+
std::vector<FieldInfo> Fields;
212+
bool Invalid;
213+
214+
public:
215+
RecordTypeInfoBuilder(TypeConverter &TC, RecordKind Kind)
216+
: TC(TC), Size(0), Alignment(1), Stride(0), NumExtraInhabitants(0),
217+
Kind(Kind), Invalid(false) {}
218+
219+
bool isInvalid() const {
220+
return Invalid;
221+
}
222+
223+
unsigned addField(unsigned fieldSize, unsigned fieldAlignment);
224+
void addField(const std::string &Name, const TypeRef *TR);
225+
const RecordTypeInfo *build();
226+
};
227+
205228
}
206229
}
207230

stdlib/public/Reflection/TypeLowering.cpp

Lines changed: 50 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -186,69 +186,6 @@ class BuiltinTypeInfo : public TypeInfo {
186186
}
187187
};
188188

189-
/// Utility class for performing universal layout for types such as
190-
/// tuples, structs, thick functions, etc.
191-
class RecordTypeInfoBuilder {
192-
TypeConverter &TC;
193-
unsigned Size, Alignment, Stride, NumExtraInhabitants;
194-
RecordKind Kind;
195-
std::vector<FieldInfo> Fields;
196-
bool Invalid;
197-
198-
public:
199-
RecordTypeInfoBuilder(TypeConverter &TC, RecordKind Kind)
200-
: TC(TC), Size(0), Alignment(1), Stride(0), NumExtraInhabitants(0),
201-
Kind(Kind), Invalid(false) {}
202-
203-
unsigned addField(unsigned fieldSize, unsigned fieldAlignment) {
204-
// Align the current size appropriately
205-
Size = ((Size + fieldAlignment - 1) & ~(fieldAlignment - 1));
206-
207-
// Record the offset
208-
unsigned offset = Size;
209-
210-
// Update the aggregate size
211-
Size += fieldSize;
212-
213-
// Update the aggregate alignment
214-
Alignment = std::max(Alignment, fieldAlignment);
215-
216-
// Re-calculate the stride
217-
Stride = ((Size + Alignment - 1) & ~(Alignment - 1));
218-
219-
return offset;
220-
}
221-
222-
void addField(const std::string &Name, const TypeRef *TR) {
223-
const TypeInfo *TI = TC.getTypeInfo(TR);
224-
if (TI == nullptr) {
225-
Invalid = true;
226-
return;
227-
}
228-
229-
// FIXME: I just made this up
230-
if (Size == 0)
231-
NumExtraInhabitants = TI->getNumExtraInhabitants();
232-
else
233-
NumExtraInhabitants = 0;
234-
235-
unsigned fieldSize = TI->getSize();
236-
unsigned fieldAlignment = TI->getAlignment();
237-
238-
unsigned fieldOffset = addField(fieldSize, fieldAlignment);
239-
Fields.push_back({Name, fieldOffset, TR, *TI});
240-
}
241-
242-
const RecordTypeInfo *build() {
243-
if (Invalid)
244-
return nullptr;
245-
246-
return TC.makeTypeInfo<RecordTypeInfo>(
247-
Size, Alignment, Stride,
248-
NumExtraInhabitants, Kind, Fields);
249-
}
250-
};
251-
252189
/// Utility class for building values that contain witness tables.
253190
class ExistentialTypeInfoBuilder {
254191
TypeConverter &TC;
@@ -361,6 +298,56 @@ class ExistentialTypeInfoBuilder {
361298

362299
}
363300

301+
unsigned RecordTypeInfoBuilder::addField(unsigned fieldSize,
302+
unsigned fieldAlignment) {
303+
// Align the current size appropriately
304+
Size = ((Size + fieldAlignment - 1) & ~(fieldAlignment - 1));
305+
306+
// Record the offset
307+
unsigned offset = Size;
308+
309+
// Update the aggregate size
310+
Size += fieldSize;
311+
312+
// Update the aggregate alignment
313+
Alignment = std::max(Alignment, fieldAlignment);
314+
315+
// Re-calculate the stride
316+
Stride = ((Size + Alignment - 1) & ~(Alignment - 1));
317+
318+
return offset;
319+
}
320+
321+
void RecordTypeInfoBuilder::addField(const std::string &Name,
322+
const TypeRef *TR) {
323+
const TypeInfo *TI = TC.getTypeInfo(TR);
324+
if (TI == nullptr) {
325+
Invalid = true;
326+
return;
327+
}
328+
329+
// FIXME: I just made this up
330+
if (Size == 0)
331+
NumExtraInhabitants = TI->getNumExtraInhabitants();
332+
else
333+
NumExtraInhabitants = 0;
334+
335+
unsigned fieldSize = TI->getSize();
336+
unsigned fieldAlignment = TI->getAlignment();
337+
338+
unsigned fieldOffset = addField(fieldSize, fieldAlignment);
339+
Fields.push_back({Name, fieldOffset, TR, *TI});
340+
}
341+
342+
const RecordTypeInfo *RecordTypeInfoBuilder::build() {
343+
if (Invalid)
344+
return nullptr;
345+
346+
return TC.makeTypeInfo<RecordTypeInfo>(
347+
Size, Alignment, Stride,
348+
NumExtraInhabitants, Kind, Fields);
349+
}
350+
364351
const ReferenceTypeInfo *
365352
TypeConverter::getReferenceTypeInfo(ReferenceKind Kind,
366353
ReferenceCounting Refcounting) {

0 commit comments

Comments
 (0)