Skip to content

Commit 5990f2e

Browse files
committed
Make it simpler to add metadata to DIType
This patch changes code in DebugInfoMetadata.h to use symbolic constants to refer to metadata slots. This makes it a little simpler (but still not entirely seamless) to add metadata to DIType.
1 parent bb75f65 commit 5990f2e

File tree

1 file changed

+76
-30
lines changed

1 file changed

+76
-30
lines changed

llvm/include/llvm/IR/DebugInfoMetadata.h

Lines changed: 76 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,8 @@ class DIType : public DIScope {
722722
uint32_t NumExtraInhabitants;
723723

724724
protected:
725+
static constexpr unsigned N_OPERANDS = 3;
726+
725727
DIType(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
726728
unsigned Line, uint64_t SizeInBits, uint32_t AlignInBits,
727729
uint64_t OffsetInBits, uint32_t NumExtraInhabitants, DIFlags Flags,
@@ -1050,6 +1052,8 @@ class DIStringType : public DIType {
10501052
friend class LLVMContextImpl;
10511053
friend class MDNode;
10521054

1055+
static constexpr unsigned MY_FIRST_OPERAND = DIType::N_OPERANDS;
1056+
10531057
unsigned Encoding;
10541058

10551059
DIStringType(LLVMContext &C, StorageType Storage, unsigned Tag,
@@ -1124,11 +1128,15 @@ class DIStringType : public DIType {
11241128

11251129
unsigned getEncoding() const { return Encoding; }
11261130

1127-
Metadata *getRawStringLength() const { return getOperand(3); }
1131+
Metadata *getRawStringLength() const { return getOperand(MY_FIRST_OPERAND); }
11281132

1129-
Metadata *getRawStringLengthExp() const { return getOperand(4); }
1133+
Metadata *getRawStringLengthExp() const {
1134+
return getOperand(MY_FIRST_OPERAND + 1);
1135+
}
11301136

1131-
Metadata *getRawStringLocationExp() const { return getOperand(5); }
1137+
Metadata *getRawStringLocationExp() const {
1138+
return getOperand(MY_FIRST_OPERAND + 2);
1139+
}
11321140
};
11331141

11341142
/// Derived types.
@@ -1170,6 +1178,8 @@ class DIDerivedType : public DIType {
11701178
friend class LLVMContextImpl;
11711179
friend class MDNode;
11721180

1181+
static constexpr unsigned MY_FIRST_OPERAND = DIType::N_OPERANDS;
1182+
11731183
/// The DWARF address space of the memory pointed to or referenced by a
11741184
/// pointer or reference type respectively.
11751185
std::optional<unsigned> DWARFAddressSpace;
@@ -1246,7 +1256,7 @@ class DIDerivedType : public DIType {
12461256

12471257
/// Get the base type this is derived from.
12481258
DIType *getBaseType() const { return cast_or_null<DIType>(getRawBaseType()); }
1249-
Metadata *getRawBaseType() const { return getOperand(3); }
1259+
Metadata *getRawBaseType() const { return getOperand(MY_FIRST_OPERAND); }
12501260

12511261
/// \returns The DWARF address space of the memory pointed to or referenced by
12521262
/// a pointer or reference type respectively.
@@ -1266,7 +1276,7 @@ class DIDerivedType : public DIType {
12661276
/// TODO: Separate out types that need this extra operand: pointer-to-member
12671277
/// types and member fields (static members and ivars).
12681278
Metadata *getExtraData() const { return getRawExtraData(); }
1269-
Metadata *getRawExtraData() const { return getOperand(4); }
1279+
Metadata *getRawExtraData() const { return getOperand(MY_FIRST_OPERAND + 1); }
12701280

12711281
/// Get the template parameters from a template alias.
12721282
DITemplateParameterArray getTemplateParams() const {
@@ -1277,7 +1287,9 @@ class DIDerivedType : public DIType {
12771287
DINodeArray getAnnotations() const {
12781288
return cast_or_null<MDTuple>(getRawAnnotations());
12791289
}
1280-
Metadata *getRawAnnotations() const { return getOperand(5); }
1290+
Metadata *getRawAnnotations() const {
1291+
return getOperand(MY_FIRST_OPERAND + 2);
1292+
}
12811293

12821294
/// Get casted version of extra data.
12831295
/// @{
@@ -1321,6 +1333,8 @@ class DISubrangeType : public DIType {
13211333
friend class LLVMContextImpl;
13221334
friend class MDNode;
13231335

1336+
static constexpr unsigned MY_FIRST_OPERAND = DIType::N_OPERANDS;
1337+
13241338
DISubrangeType(LLVMContext &C, StorageType Storage, unsigned Line,
13251339
uint64_t SizeInBits, uint32_t AlignInBits, DIFlags Flags,
13261340
ArrayRef<Metadata *> Ops);
@@ -1374,15 +1388,23 @@ class DISubrangeType : public DIType {
13741388

13751389
/// Get the base type this is derived from.
13761390
DIType *getBaseType() const { return cast_or_null<DIType>(getRawBaseType()); }
1377-
Metadata *getRawBaseType() const { return getOperand(3); }
1391+
Metadata *getRawBaseType() const { return getOperand(MY_FIRST_OPERAND); }
13781392

1379-
Metadata *getRawLowerBound() const { return getOperand(4).get(); }
1393+
Metadata *getRawLowerBound() const {
1394+
return getOperand(MY_FIRST_OPERAND + 1).get();
1395+
}
13801396

1381-
Metadata *getRawUpperBound() const { return getOperand(5).get(); }
1397+
Metadata *getRawUpperBound() const {
1398+
return getOperand(MY_FIRST_OPERAND + 2).get();
1399+
}
13821400

1383-
Metadata *getRawStride() const { return getOperand(6).get(); }
1401+
Metadata *getRawStride() const {
1402+
return getOperand(MY_FIRST_OPERAND + 3).get();
1403+
}
13841404

1385-
Metadata *getRawBias() const { return getOperand(7).get(); }
1405+
Metadata *getRawBias() const {
1406+
return getOperand(MY_FIRST_OPERAND + 4).get();
1407+
}
13861408

13871409
BoundType getLowerBound() const {
13881410
return convertRawToBound(getRawLowerBound());
@@ -1409,6 +1431,8 @@ class DICompositeType : public DIType {
14091431
friend class LLVMContextImpl;
14101432
friend class MDNode;
14111433

1434+
static constexpr unsigned MY_FIRST_OPERAND = DIType::N_OPERANDS;
1435+
14121436
unsigned RuntimeLang;
14131437
std::optional<uint32_t> EnumKind;
14141438

@@ -1570,41 +1594,55 @@ class DICompositeType : public DIType {
15701594
DITemplateParameterArray getTemplateParams() const {
15711595
return cast_or_null<MDTuple>(getRawTemplateParams());
15721596
}
1573-
StringRef getIdentifier() const { return getStringOperand(7); }
1597+
StringRef getIdentifier() const {
1598+
return getStringOperand(MY_FIRST_OPERAND + 4);
1599+
}
15741600
unsigned getRuntimeLang() const { return RuntimeLang; }
15751601
std::optional<uint32_t> getEnumKind() const { return EnumKind; }
15761602

1577-
Metadata *getRawBaseType() const { return getOperand(3); }
1578-
Metadata *getRawElements() const { return getOperand(4); }
1579-
Metadata *getRawVTableHolder() const { return getOperand(5); }
1580-
Metadata *getRawTemplateParams() const { return getOperand(6); }
1581-
MDString *getRawIdentifier() const { return getOperandAs<MDString>(7); }
1582-
Metadata *getRawDiscriminator() const { return getOperand(8); }
1603+
Metadata *getRawBaseType() const { return getOperand(MY_FIRST_OPERAND); }
1604+
Metadata *getRawElements() const { return getOperand(MY_FIRST_OPERAND + 1); }
1605+
Metadata *getRawVTableHolder() const {
1606+
return getOperand(MY_FIRST_OPERAND + 2);
1607+
}
1608+
Metadata *getRawTemplateParams() const {
1609+
return getOperand(MY_FIRST_OPERAND + 3);
1610+
}
1611+
MDString *getRawIdentifier() const {
1612+
return getOperandAs<MDString>(MY_FIRST_OPERAND + 4);
1613+
}
1614+
Metadata *getRawDiscriminator() const {
1615+
return getOperand(MY_FIRST_OPERAND + 5);
1616+
}
15831617
DIDerivedType *getDiscriminator() const {
1584-
return getOperandAs<DIDerivedType>(8);
1618+
return getOperandAs<DIDerivedType>(MY_FIRST_OPERAND + 5);
1619+
}
1620+
Metadata *getRawDataLocation() const {
1621+
return getOperand(MY_FIRST_OPERAND + 6);
15851622
}
1586-
Metadata *getRawDataLocation() const { return getOperand(9); }
15871623
DIVariable *getDataLocation() const {
15881624
return dyn_cast_or_null<DIVariable>(getRawDataLocation());
15891625
}
15901626
DIExpression *getDataLocationExp() const {
15911627
return dyn_cast_or_null<DIExpression>(getRawDataLocation());
15921628
}
1593-
Metadata *getRawAssociated() const { return getOperand(10); }
1629+
Metadata *getRawAssociated() const {
1630+
return getOperand(MY_FIRST_OPERAND + 7);
1631+
}
15941632
DIVariable *getAssociated() const {
15951633
return dyn_cast_or_null<DIVariable>(getRawAssociated());
15961634
}
15971635
DIExpression *getAssociatedExp() const {
15981636
return dyn_cast_or_null<DIExpression>(getRawAssociated());
15991637
}
1600-
Metadata *getRawAllocated() const { return getOperand(11); }
1638+
Metadata *getRawAllocated() const { return getOperand(MY_FIRST_OPERAND + 8); }
16011639
DIVariable *getAllocated() const {
16021640
return dyn_cast_or_null<DIVariable>(getRawAllocated());
16031641
}
16041642
DIExpression *getAllocatedExp() const {
16051643
return dyn_cast_or_null<DIExpression>(getRawAllocated());
16061644
}
1607-
Metadata *getRawRank() const { return getOperand(12); }
1645+
Metadata *getRawRank() const { return getOperand(MY_FIRST_OPERAND + 9); }
16081646
ConstantInt *getRankConst() const {
16091647
if (auto *MD = dyn_cast_or_null<ConstantAsMetadata>(getRawRank()))
16101648
return dyn_cast_or_null<ConstantInt>(MD->getValue());
@@ -1614,17 +1652,23 @@ class DICompositeType : public DIType {
16141652
return dyn_cast_or_null<DIExpression>(getRawRank());
16151653
}
16161654

1617-
Metadata *getRawAnnotations() const { return getOperand(13); }
1655+
Metadata *getRawAnnotations() const {
1656+
return getOperand(MY_FIRST_OPERAND + 10);
1657+
}
16181658
DINodeArray getAnnotations() const {
16191659
return cast_or_null<MDTuple>(getRawAnnotations());
16201660
}
16211661

1622-
Metadata *getRawSpecification() const { return getOperand(14); }
1662+
Metadata *getRawSpecification() const {
1663+
return getOperand(MY_FIRST_OPERAND + 11);
1664+
}
16231665
DIType *getSpecification() const {
16241666
return cast_or_null<DIType>(getRawSpecification());
16251667
}
16261668

1627-
Metadata *getRawBitStride() const { return getOperand(15); }
1669+
Metadata *getRawBitStride() const {
1670+
return getOperand(MY_FIRST_OPERAND + 12);
1671+
}
16281672
ConstantInt *getBitStrideConst() const {
16291673
if (auto *MD = dyn_cast_or_null<ConstantAsMetadata>(getRawBitStride()))
16301674
return dyn_cast_or_null<ConstantInt>(MD->getValue());
@@ -1643,15 +1687,15 @@ class DICompositeType : public DIType {
16431687
assert(is_contained(Elements->operands(), Op) &&
16441688
"Lost a member during member list replacement");
16451689
#endif
1646-
replaceOperandWith(4, Elements.get());
1690+
replaceOperandWith(MY_FIRST_OPERAND + 1, Elements.get());
16471691
}
16481692

16491693
void replaceVTableHolder(DIType *VTableHolder) {
1650-
replaceOperandWith(5, VTableHolder);
1694+
replaceOperandWith(MY_FIRST_OPERAND + 2, VTableHolder);
16511695
}
16521696

16531697
void replaceTemplateParams(DITemplateParameterArray TemplateParams) {
1654-
replaceOperandWith(6, TemplateParams.get());
1698+
replaceOperandWith(MY_FIRST_OPERAND + 3, TemplateParams.get());
16551699
}
16561700
/// @}
16571701

@@ -1667,6 +1711,8 @@ class DISubroutineType : public DIType {
16671711
friend class LLVMContextImpl;
16681712
friend class MDNode;
16691713

1714+
static constexpr unsigned MY_FIRST_OPERAND = DIType::N_OPERANDS;
1715+
16701716
/// The calling convention used with DW_AT_calling_convention. Actually of
16711717
/// type dwarf::CallingConvention.
16721718
uint8_t CC;
@@ -1712,7 +1758,7 @@ class DISubroutineType : public DIType {
17121758
return cast_or_null<MDTuple>(getRawTypeArray());
17131759
}
17141760

1715-
Metadata *getRawTypeArray() const { return getOperand(3); }
1761+
Metadata *getRawTypeArray() const { return getOperand(MY_FIRST_OPERAND); }
17161762

17171763
static bool classof(const Metadata *MD) {
17181764
return MD->getMetadataID() == DISubroutineTypeKind;

0 commit comments

Comments
 (0)