Skip to content

Commit 93f5d9f

Browse files
committed
swift SIL: Type.getStructFields -> Type.getNominalFields
To be able to get class fields as well as struct fields
1 parent 75da7d9 commit 93f5d9f

File tree

6 files changed

+73
-60
lines changed

6 files changed

+73
-60
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ReleaseDevirtualizer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ private extension StructExtractInst {
204204
let structType = operand.type
205205

206206
var nonTrivialFieldsCount = 0
207-
for field in structType.getStructFields(in: function) {
207+
for field in structType.getNominalFields(in: function) {
208208
if field.isTrivial(in: function) {
209209
nonTrivialFieldsCount += 1
210210
}

SwiftCompilerSources/Sources/SIL/Type.swift

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,17 @@ public struct Type : CustomStringConvertible, CustomReflectable {
2121
public func isTrivial(in function: Function) -> Bool {
2222
return SILType_isTrivial(bridged, function.bridged) != 0
2323
}
24-
24+
2525
public var isNominal: Bool { SILType_isNominal(bridged) != 0 }
2626
public var isClass: Bool { SILType_isClass(bridged) != 0 }
2727
public var isStruct: Bool { SILType_isStruct(bridged) != 0 }
2828
public var isTuple: Bool { SILType_isTuple(bridged) != 0 }
2929
public var isEnum: Bool { SILType_isEnum(bridged) != 0 }
3030

3131
public var tupleElements: TupleElementArray { TupleElementArray(type: self) }
32-
33-
public func getFieldIndexOfNominal(fieldName: String) -> Int? {
34-
let idx = fieldName.withBridgedStringRef {
35-
SILType_getFieldIdxOfNominalType(bridged, $0)
36-
}
37-
return idx >= 0 ? idx : nil
38-
}
3932

40-
public func getStructFields(in function: Function) -> StructElementArray {
41-
StructElementArray(type: self, function: function)
33+
public func getNominalFields(in function: Function) -> NominalFieldsArray {
34+
NominalFieldsArray(type: self, function: function)
4235
}
4336

4437
public var description: String { SILType_debugDescription(bridged).takeString() }
@@ -52,29 +45,36 @@ extension Type: Equatable {
5245
}
5346
}
5447

55-
public struct TupleElementArray : RandomAccessCollection, FormattedLikeArray {
56-
public let type: Type
48+
public struct NominalFieldsArray : RandomAccessCollection, FormattedLikeArray {
49+
fileprivate let type: Type
50+
fileprivate let function: Function
5751

5852
public var startIndex: Int { return 0 }
59-
public var endIndex: Int { SILType_getNumTupleElements(type.bridged) }
53+
public var endIndex: Int { SILType_getNumNominalFields(type.bridged) }
6054

6155
public subscript(_ index: Int) -> Type {
62-
SILType_getTupleElementType(type.bridged, index).type
56+
SILType_getNominalFieldType(type.bridged, index, function.bridged).type
6357
}
64-
}
6558

66-
extension BridgedType {
67-
var type: Type { Type(bridged: self) }
59+
public func getIndexOfField(withName name: String) -> Int? {
60+
let idx = name.withBridgedStringRef {
61+
SILType_getFieldIdxOfNominalType(type.bridged, $0)
62+
}
63+
return idx >= 0 ? idx : nil
64+
}
6865
}
6966

70-
public struct StructElementArray : RandomAccessCollection, FormattedLikeArray {
71-
public let type: Type
72-
let function: Function
67+
public struct TupleElementArray : RandomAccessCollection, FormattedLikeArray {
68+
fileprivate let type: Type
7369

7470
public var startIndex: Int { return 0 }
75-
public var endIndex: Int { SILType_getNumStructFields(type.bridged) }
71+
public var endIndex: Int { SILType_getNumTupleElements(type.bridged) }
7672

7773
public subscript(_ index: Int) -> Type {
78-
SILType_getStructFieldType(type.bridged, index, function.bridged).type
74+
SILType_getTupleElementType(type.bridged, index).type
7975
}
8076
}
77+
78+
extension BridgedType {
79+
var type: Type { Type(bridged: self) }
80+
}

include/swift/SIL/SILBridging.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,14 @@ SwiftInt SILType_isClass(BridgedType type);
217217
SwiftInt SILType_isStruct(BridgedType type);
218218
SwiftInt SILType_isTuple(BridgedType type);
219219
SwiftInt SILType_isEnum(BridgedType type);
220-
SwiftInt SILType_getFieldIdxOfNominalType(BridgedType type,
221-
BridgedStringRef fieldName);
222220
SwiftInt SILType_getNumTupleElements(BridgedType type);
223221
BridgedType SILType_getTupleElementType(BridgedType type, SwiftInt elementIdx);
222+
SwiftInt SILType_getNumNominalFields(BridgedType type);
223+
BridgedType SILType_getNominalFieldType(BridgedType type, SwiftInt index,
224+
BridgedFunction function);
225+
SwiftInt SILType_getFieldIdxOfNominalType(BridgedType type,
226+
BridgedStringRef fieldName);
224227
BridgedSubstitutionMap SILType_getContextSubstitutionMap(BridgedType);
225-
SwiftInt SILType_getNumStructFields(BridgedType type);
226-
BridgedType SILType_getStructFieldType(BridgedType type, SwiftInt index,
227-
BridgedFunction function);
228228

229229
BridgedBasicBlock SILArgument_getParent(BridgedArgument argument);
230230

include/swift/SIL/SILInstruction.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6523,6 +6523,8 @@ unsigned getFieldIndex(NominalTypeDecl *decl, VarDecl *property);
65236523

65246524
unsigned getCaseIndex(EnumElementDecl *enumElement);
65256525

6526+
unsigned getNumFieldsInNominal(NominalTypeDecl *decl);
6527+
65266528
/// Get the property for a struct or class by its unique index, or nullptr if
65276529
/// the index does not match a property declared in this struct or class or
65286530
/// one its superclasses.

lib/SIL/IR/SILInstructions.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,6 +1363,17 @@ unsigned swift::getFieldIndex(NominalTypeDecl *decl, VarDecl *field) {
13631363
"property of the operand type");
13641364
}
13651365

1366+
unsigned swift::getNumFieldsInNominal(NominalTypeDecl *decl) {
1367+
unsigned count = 0;
1368+
if (auto *classDecl = dyn_cast<ClassDecl>(decl)) {
1369+
for (auto *superDecl = classDecl->getSuperclassDecl(); superDecl != nullptr;
1370+
superDecl = superDecl->getSuperclassDecl()) {
1371+
count += superDecl->getStoredProperties().size();
1372+
}
1373+
}
1374+
return count + decl->getStoredProperties().size();
1375+
}
1376+
13661377
unsigned swift::getCaseIndex(EnumElementDecl *enumElement) {
13671378
unsigned idx = 0;
13681379
for (EnumElementDecl *e : enumElement->getParentEnum()->getAllElements()) {

lib/SIL/Utils/SILBridging.cpp

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,38 @@ SwiftInt SILType_isEnum(BridgedType type) {
380380
return castToSILType(type).getEnumOrBoundGenericEnum() ? 1 : 0;
381381
}
382382

383+
SwiftInt SILType_getNumTupleElements(BridgedType type) {
384+
TupleType *tupleTy = castToSILType(type).castTo<TupleType>();
385+
return tupleTy->getNumElements();
386+
}
387+
388+
BridgedType SILType_getTupleElementType(BridgedType type, SwiftInt elementIdx) {
389+
SILType ty = castToSILType(type);
390+
SILType elmtTy = ty.getTupleElementType((unsigned)elementIdx);
391+
return {elmtTy.getOpaqueValue()};
392+
}
393+
394+
SwiftInt SILType_getNumNominalFields(BridgedType type) {
395+
SILType silType = castToSILType(type);
396+
auto *nominal = silType.getNominalOrBoundGenericNominal();
397+
assert(nominal && "expected nominal type");
398+
return getNumFieldsInNominal(nominal);
399+
}
400+
401+
BridgedType SILType_getNominalFieldType(BridgedType type, SwiftInt index,
402+
BridgedFunction function) {
403+
SILType silType = castToSILType(type);
404+
SILFunction *silFunction = castToFunction(function);
405+
406+
NominalTypeDecl *decl = silType.getNominalOrBoundGenericNominal();
407+
VarDecl *field = getIndexedField(decl, (unsigned)index);
408+
409+
SILType fieldType = silType.getFieldType(
410+
field, silFunction->getModule(), silFunction->getTypeExpansionContext());
411+
412+
return {fieldType.getOpaqueValue()};
413+
}
414+
383415
SwiftInt SILType_getFieldIdxOfNominalType(BridgedType type,
384416
BridgedStringRef fieldName) {
385417
SILType ty = castToSILType(type);
@@ -408,38 +440,6 @@ SwiftInt SILType_getFieldIdxOfNominalType(BridgedType type,
408440
return -1;
409441
}
410442

411-
SwiftInt SILType_getNumTupleElements(BridgedType type) {
412-
TupleType *tupleTy = castToSILType(type).castTo<TupleType>();
413-
return tupleTy->getNumElements();
414-
}
415-
416-
BridgedType SILType_getTupleElementType(BridgedType type, SwiftInt elementIdx) {
417-
SILType ty = castToSILType(type);
418-
SILType elmtTy = ty.getTupleElementType((unsigned)elementIdx);
419-
return {elmtTy.getOpaqueValue()};
420-
}
421-
422-
SwiftInt SILType_getNumStructFields(BridgedType type) {
423-
SILType silType = castToSILType(type);
424-
StructDecl *decl =
425-
cast<StructDecl>(silType.getNominalOrBoundGenericNominal());
426-
return decl->getStoredProperties().size();
427-
}
428-
429-
BridgedType SILType_getStructFieldType(BridgedType type, SwiftInt index,
430-
BridgedFunction function) {
431-
SILType silType = castToSILType(type);
432-
SILFunction *silFunction = castToFunction(function);
433-
434-
StructDecl *decl = cast<StructDecl>(silType.getNominalOrBoundGenericNominal());
435-
VarDecl *property = decl->getStoredProperties()[index];
436-
437-
SILType propertyType = silType.getFieldType(
438-
property, silFunction->getModule(), TypeExpansionContext(*silFunction));
439-
440-
return {propertyType.getOpaqueValue()};
441-
}
442-
443443
//===----------------------------------------------------------------------===//
444444
// SILGlobalVariable
445445
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)