Skip to content

Commit 05d6229

Browse files
committed
Swift SIL: add NominalFieldsArray.getNameOfField(withIndex:)
1 parent 3367fdd commit 05d6229

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

SwiftCompilerSources/Sources/SIL/Type.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ public struct NominalFieldsArray : RandomAccessCollection, FormattedLikeArray {
8080
}
8181
return idx >= 0 ? idx : nil
8282
}
83+
84+
public func getNameOfField(withIndex idx: Int) -> StringRef {
85+
StringRef(bridged: SILType_getNominalFieldName(type.bridged, idx))
86+
}
8387
}
8488

8589
public struct TupleElementArray : RandomAccessCollection, FormattedLikeArray {

include/swift/SIL/SILBridging.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ BridgedType SILType_getNominalFieldType(BridgedType type, SwiftInt index,
274274
BridgedFunction function);
275275
SwiftInt SILType_getFieldIdxOfNominalType(BridgedType type,
276276
llvm::StringRef fieldName);
277+
llvm::StringRef SILType_getNominalFieldName(BridgedType type, SwiftInt index);
277278
SwiftInt SILType_getCaseIdxOfEnumType(BridgedType type,
278279
llvm::StringRef caseName);
279280

lib/SIL/Utils/SILBridging.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,14 @@ BridgedType SILType_getNominalFieldType(BridgedType type, SwiftInt index,
461461
return {fieldType.getOpaqueValue()};
462462
}
463463

464+
StringRef SILType_getNominalFieldName(BridgedType type, SwiftInt index) {
465+
SILType silType = castToSILType(type);
466+
467+
NominalTypeDecl *decl = silType.getNominalOrBoundGenericNominal();
468+
VarDecl *field = getIndexedField(decl, (unsigned)index);
469+
return field->getName().str();
470+
}
471+
464472
SwiftInt SILType_getFieldIdxOfNominalType(BridgedType type,
465473
StringRef fieldName) {
466474
SILType ty = castToSILType(type);

0 commit comments

Comments
 (0)