1//===--- DIBuilder.cpp - Debug Information Builder ------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the DIBuilder.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/IR/DIBuilder.h"
14#include "LLVMContextImpl.h"
15#include "llvm/ADT/APInt.h"
16#include "llvm/ADT/APSInt.h"
17#include "llvm/BinaryFormat/Dwarf.h"
18#include "llvm/IR/Constants.h"
19#include "llvm/IR/DebugInfo.h"
20#include "llvm/IR/IRBuilder.h"
21#include "llvm/IR/Module.h"
22#include <optional>
23
24using namespace llvm;
25using namespace llvm::dwarf;
26
27DIBuilder::DIBuilder(Module &m, bool AllowUnresolvedNodes, DICompileUnit *CU)
28 : M(m), VMContext(M.getContext()), CUNode(CU),
29 AllowUnresolvedNodes(AllowUnresolvedNodes) {
30 if (CUNode) {
31 if (const auto &ETs = CUNode->getEnumTypes())
32 EnumTypes.assign(in_start: ETs.begin(), in_end: ETs.end());
33 if (const auto &RTs = CUNode->getRetainedTypes())
34 AllRetainTypes.assign(in_start: RTs.begin(), in_end: RTs.end());
35 if (const auto &GVs = CUNode->getGlobalVariables())
36 AllGVs.assign(in_start: GVs.begin(), in_end: GVs.end());
37 if (const auto &IMs = CUNode->getImportedEntities())
38 ImportedModules.assign(in_start: IMs.begin(), in_end: IMs.end());
39 if (const auto &MNs = CUNode->getMacros())
40 AllMacrosPerParent.insert(KV: {nullptr, {llvm::from_range, MNs}});
41 }
42}
43
44void DIBuilder::trackIfUnresolved(MDNode *N) {
45 if (!N)
46 return;
47 if (N->isResolved())
48 return;
49
50 assert(AllowUnresolvedNodes && "Cannot handle unresolved nodes");
51 UnresolvedNodes.emplace_back(Args&: N);
52}
53
54void DIBuilder::finalizeSubprogram(DISubprogram *SP) {
55 auto PN = SubprogramTrackedNodes.find(Val: SP);
56 if (PN == SubprogramTrackedNodes.end())
57 return;
58
59 SmallVector<Metadata *, 16> RetainedNodes;
60 for (MDNode *N : PN->second) {
61 // If the tracked node N was temporary, and the DIBuilder user replaced it
62 // with a node that does not belong to SP or is non-local, do not add N to
63 // SP's retainedNodes list.
64 DILocalScope *Scope = dyn_cast_or_null<DILocalScope>(
65 Val: DISubprogram::getRawRetainedNodeScope(N));
66 if (!Scope || Scope->getSubprogram() != SP)
67 continue;
68
69 RetainedNodes.push_back(Elt: N);
70 }
71
72 SP->replaceRetainedNodes(N: MDTuple::get(Context&: VMContext, MDs: RetainedNodes));
73}
74
75void DIBuilder::finalize() {
76 if (!CUNode) {
77 assert(!AllowUnresolvedNodes &&
78 "creating type nodes without a CU is not supported");
79 return;
80 }
81
82 if (!EnumTypes.empty())
83 CUNode->replaceEnumTypes(
84 N: MDTuple::get(Context&: VMContext, MDs: SmallVector<Metadata *, 16>(EnumTypes.begin(),
85 EnumTypes.end())));
86
87 SmallVector<Metadata *, 16> RetainValues;
88 // Declarations and definitions of the same type may be retained. Some
89 // clients RAUW these pairs, leaving duplicates in the retained types
90 // list. Use a set to remove the duplicates while we transform the
91 // TrackingVHs back into Values.
92 SmallPtrSet<Metadata *, 16> RetainSet;
93 for (const TrackingMDNodeRef &N : AllRetainTypes)
94 if (RetainSet.insert(Ptr: N).second)
95 RetainValues.push_back(Elt: N);
96
97 if (!RetainValues.empty())
98 CUNode->replaceRetainedTypes(N: MDTuple::get(Context&: VMContext, MDs: RetainValues));
99
100 for (auto *SP : AllSubprograms)
101 finalizeSubprogram(SP);
102 for (auto *N : RetainValues)
103 if (auto *SP = dyn_cast<DISubprogram>(Val: N))
104 finalizeSubprogram(SP);
105
106 if (!AllGVs.empty())
107 CUNode->replaceGlobalVariables(N: MDTuple::get(Context&: VMContext, MDs: AllGVs));
108
109 if (!ImportedModules.empty())
110 CUNode->replaceImportedEntities(N: MDTuple::get(
111 Context&: VMContext, MDs: SmallVector<Metadata *, 16>(ImportedModules.begin(),
112 ImportedModules.end())));
113
114 for (const auto &I : AllMacrosPerParent) {
115 // DIMacroNode's with nullptr parent are DICompileUnit direct children.
116 if (!I.first) {
117 CUNode->replaceMacros(N: MDTuple::get(Context&: VMContext, MDs: I.second.getArrayRef()));
118 continue;
119 }
120 // Otherwise, it must be a temporary DIMacroFile that need to be resolved.
121 auto *TMF = cast<DIMacroFile>(Val: I.first);
122 auto *MF = DIMacroFile::get(Context&: VMContext, MIType: dwarf::DW_MACINFO_start_file,
123 Line: TMF->getLine(), File: TMF->getFile(),
124 Elements: getOrCreateMacroArray(Elements: I.second.getArrayRef()));
125 replaceTemporary(N: llvm::TempDIMacroNode(TMF), Replacement: MF);
126 }
127
128 // Now that all temp nodes have been replaced or deleted, resolve remaining
129 // cycles.
130 for (const auto &N : UnresolvedNodes)
131 if (N && !N->isResolved())
132 N->resolveCycles();
133 UnresolvedNodes.clear();
134
135 // Can't handle unresolved nodes anymore.
136 AllowUnresolvedNodes = false;
137}
138
139/// If N is compile unit return NULL otherwise return N.
140static DIScope *getNonCompileUnitScope(DIScope *N) {
141 if (!N || isa<DICompileUnit>(Val: N))
142 return nullptr;
143 return cast<DIScope>(Val: N);
144}
145
146DICompileUnit *DIBuilder::createCompileUnit(
147 DISourceLanguageName Lang, DIFile *File, StringRef Producer,
148 bool isOptimized, StringRef Flags, unsigned RunTimeVer, StringRef SplitName,
149 DICompileUnit::DebugEmissionKind Kind, uint64_t DWOId,
150 bool SplitDebugInlining, bool DebugInfoForProfiling,
151 DICompileUnit::DebugNameTableKind NameTableKind, bool RangesBaseAddress,
152 StringRef SysRoot, StringRef SDK) {
153
154 assert(!CUNode && "Can only make one compile unit per DIBuilder instance");
155 CUNode = DICompileUnit::getDistinct(
156 Context&: VMContext, SourceLanguage: Lang, File, Producer, IsOptimized: isOptimized, Flags, RuntimeVersion: RunTimeVer,
157 SplitDebugFilename: SplitName, EmissionKind: Kind, EnumTypes: nullptr, RetainedTypes: nullptr, GlobalVariables: nullptr, ImportedEntities: nullptr, Macros: nullptr, DWOId,
158 SplitDebugInlining, DebugInfoForProfiling, NameTableKind,
159 RangesBaseAddress, SysRoot, SDK);
160
161 // Create a named metadata so that it is easier to find cu in a module.
162 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name: "llvm.dbg.cu");
163 NMD->addOperand(M: CUNode);
164 trackIfUnresolved(N: CUNode);
165 return CUNode;
166}
167
168static DIImportedEntity *
169createImportedModule(LLVMContext &C, dwarf::Tag Tag, DIScope *Context,
170 Metadata *NS, DIFile *File, unsigned Line, StringRef Name,
171 DINodeArray Elements,
172 SmallVectorImpl<TrackingMDNodeRef> &ImportedModules) {
173 if (Line)
174 assert(File && "Source location has line number but no file");
175 unsigned EntitiesCount = C.pImpl->DIImportedEntitys.size();
176 auto *M = DIImportedEntity::get(Context&: C, Tag, Scope: Context, Entity: cast_or_null<DINode>(Val: NS),
177 File, Line, Name, Elements);
178 if (EntitiesCount < C.pImpl->DIImportedEntitys.size())
179 // A new Imported Entity was just added to the context.
180 // Add it to the Imported Modules list.
181 ImportedModules.emplace_back(Args&: M);
182 return M;
183}
184
185DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context,
186 DINamespace *NS, DIFile *File,
187 unsigned Line,
188 DINodeArray Elements) {
189 return ::createImportedModule(C&: VMContext, Tag: dwarf::DW_TAG_imported_module,
190 Context, NS, File, Line, Name: StringRef(), Elements,
191 ImportedModules&: getImportTrackingVector(S: Context));
192}
193
194DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context,
195 DIImportedEntity *NS,
196 DIFile *File, unsigned Line,
197 DINodeArray Elements) {
198 return ::createImportedModule(C&: VMContext, Tag: dwarf::DW_TAG_imported_module,
199 Context, NS, File, Line, Name: StringRef(), Elements,
200 ImportedModules&: getImportTrackingVector(S: Context));
201}
202
203DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context, DIModule *M,
204 DIFile *File, unsigned Line,
205 DINodeArray Elements) {
206 return ::createImportedModule(C&: VMContext, Tag: dwarf::DW_TAG_imported_module,
207 Context, NS: M, File, Line, Name: StringRef(), Elements,
208 ImportedModules&: getImportTrackingVector(S: Context));
209}
210
211DIImportedEntity *
212DIBuilder::createImportedDeclaration(DIScope *Context, DINode *Decl,
213 DIFile *File, unsigned Line,
214 StringRef Name, DINodeArray Elements) {
215 // Make sure to use the unique identifier based metadata reference for
216 // types that have one.
217 return ::createImportedModule(C&: VMContext, Tag: dwarf::DW_TAG_imported_declaration,
218 Context, NS: Decl, File, Line, Name, Elements,
219 ImportedModules&: getImportTrackingVector(S: Context));
220}
221
222DIFile *DIBuilder::createFile(StringRef Filename, StringRef Directory,
223 std::optional<DIFile::ChecksumInfo<StringRef>> CS,
224 std::optional<StringRef> Source) {
225 return DIFile::get(Context&: VMContext, Filename, Directory, CS, Source);
226}
227
228DIMacro *DIBuilder::createMacro(DIMacroFile *Parent, unsigned LineNumber,
229 unsigned MacroType, StringRef Name,
230 StringRef Value) {
231 assert(!Name.empty() && "Unable to create macro without name");
232 assert((MacroType == dwarf::DW_MACINFO_undef ||
233 MacroType == dwarf::DW_MACINFO_define) &&
234 "Unexpected macro type");
235 auto *M = DIMacro::get(Context&: VMContext, MIType: MacroType, Line: LineNumber, Name, Value);
236 AllMacrosPerParent[Parent].insert(X: M);
237 return M;
238}
239
240DIMacroFile *DIBuilder::createTempMacroFile(DIMacroFile *Parent,
241 unsigned LineNumber, DIFile *File) {
242 auto *MF = DIMacroFile::getTemporary(Context&: VMContext, MIType: dwarf::DW_MACINFO_start_file,
243 Line: LineNumber, File, Elements: DIMacroNodeArray())
244 .release();
245 AllMacrosPerParent[Parent].insert(X: MF);
246 // Add the new temporary DIMacroFile to the macro per parent map as a parent.
247 // This is needed to assure DIMacroFile with no children to have an entry in
248 // the map. Otherwise, it will not be resolved in DIBuilder::finalize().
249 AllMacrosPerParent.insert(KV: {MF, {}});
250 return MF;
251}
252
253DIEnumerator *DIBuilder::createEnumerator(StringRef Name, uint64_t Val,
254 bool IsUnsigned) {
255 assert(!Name.empty() && "Unable to create enumerator without name");
256 return DIEnumerator::get(Context&: VMContext, Value: APInt(64, Val, !IsUnsigned), IsUnsigned,
257 Name);
258}
259
260DIEnumerator *DIBuilder::createEnumerator(StringRef Name, const APSInt &Value) {
261 assert(!Name.empty() && "Unable to create enumerator without name");
262 return DIEnumerator::get(Context&: VMContext, Value: APInt(Value), IsUnsigned: Value.isUnsigned(), Name);
263}
264
265DIBasicType *DIBuilder::createUnspecifiedType(StringRef Name) {
266 assert(!Name.empty() && "Unable to create type without name");
267 return DIBasicType::get(Context&: VMContext, Tag: dwarf::DW_TAG_unspecified_type, Name);
268}
269
270DIBasicType *DIBuilder::createNullPtrType() {
271 return createUnspecifiedType(Name: "decltype(nullptr)");
272}
273
274DIBasicType *DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
275 unsigned Encoding,
276 DINode::DIFlags Flags,
277 uint32_t NumExtraInhabitants,
278 uint32_t DataSizeInBits) {
279 return DIBasicType::get(Context&: VMContext, Tag: dwarf::DW_TAG_base_type, Name, SizeInBits,
280 AlignInBits: 0, Encoding, NumExtraInhabitants, DataSizeInBits,
281 Flags);
282}
283
284DIFixedPointType *
285DIBuilder::createBinaryFixedPointType(StringRef Name, uint64_t SizeInBits,
286 uint32_t AlignInBits, unsigned Encoding,
287 DINode::DIFlags Flags, int Factor) {
288 return DIFixedPointType::get(Context&: VMContext, Tag: dwarf::DW_TAG_base_type, Name,
289 SizeInBits, AlignInBits, Encoding, Flags,
290 Kind: DIFixedPointType::FixedPointBinary, Factor,
291 Numerator: APInt(), Denominator: APInt());
292}
293
294DIFixedPointType *
295DIBuilder::createDecimalFixedPointType(StringRef Name, uint64_t SizeInBits,
296 uint32_t AlignInBits, unsigned Encoding,
297 DINode::DIFlags Flags, int Factor) {
298 return DIFixedPointType::get(Context&: VMContext, Tag: dwarf::DW_TAG_base_type, Name,
299 SizeInBits, AlignInBits, Encoding, Flags,
300 Kind: DIFixedPointType::FixedPointDecimal, Factor,
301 Numerator: APInt(), Denominator: APInt());
302}
303
304DIFixedPointType *
305DIBuilder::createRationalFixedPointType(StringRef Name, uint64_t SizeInBits,
306 uint32_t AlignInBits, unsigned Encoding,
307 DINode::DIFlags Flags, APInt Numerator,
308 APInt Denominator) {
309 return DIFixedPointType::get(Context&: VMContext, Tag: dwarf::DW_TAG_base_type, Name,
310 SizeInBits, AlignInBits, Encoding, Flags,
311 Kind: DIFixedPointType::FixedPointRational, Factor: 0,
312 Numerator, Denominator);
313}
314
315DIStringType *DIBuilder::createStringType(StringRef Name, uint64_t SizeInBits) {
316 assert(!Name.empty() && "Unable to create type without name");
317 return DIStringType::get(Context&: VMContext, Tag: dwarf::DW_TAG_string_type, Name,
318 SizeInBits, AlignInBits: 0);
319}
320
321DIStringType *DIBuilder::createStringType(StringRef Name,
322 DIVariable *StringLength,
323 DIExpression *StrLocationExp) {
324 assert(!Name.empty() && "Unable to create type without name");
325 return DIStringType::get(Context&: VMContext, Tag: dwarf::DW_TAG_string_type, Name,
326 StringLength, StringLengthExp: nullptr, StringLocationExp: StrLocationExp, SizeInBits: 0, AlignInBits: 0, Encoding: 0);
327}
328
329DIStringType *DIBuilder::createStringType(StringRef Name,
330 DIExpression *StringLengthExp,
331 DIExpression *StrLocationExp) {
332 assert(!Name.empty() && "Unable to create type without name");
333 return DIStringType::get(Context&: VMContext, Tag: dwarf::DW_TAG_string_type, Name, StringLength: nullptr,
334 StringLengthExp, StringLocationExp: StrLocationExp, SizeInBits: 0, AlignInBits: 0, Encoding: 0);
335}
336
337DIDerivedType *DIBuilder::createQualifiedType(unsigned Tag, DIType *FromTy) {
338 return DIDerivedType::get(Context&: VMContext, Tag, Name: "", File: nullptr, Line: 0, Scope: nullptr, BaseType: FromTy,
339 SizeInBits: (uint64_t)0, AlignInBits: 0, OffsetInBits: (uint64_t)0, DWARFAddressSpace: std::nullopt,
340 PtrAuthData: std::nullopt, Flags: DINode::FlagZero);
341}
342
343DIDerivedType *DIBuilder::createPtrAuthQualifiedType(
344 DIType *FromTy, unsigned Key, bool IsAddressDiscriminated,
345 unsigned ExtraDiscriminator, bool IsaPointer,
346 bool AuthenticatesNullValues) {
347 return DIDerivedType::get(
348 Context&: VMContext, Tag: dwarf::DW_TAG_LLVM_ptrauth_type, Name: "", File: nullptr, Line: 0, Scope: nullptr,
349 BaseType: FromTy, SizeInBits: (uint64_t)0, AlignInBits: 0, OffsetInBits: (uint64_t)0, DWARFAddressSpace: std::nullopt,
350 PtrAuthData: std::optional<DIDerivedType::PtrAuthData>(
351 std::in_place, Key, IsAddressDiscriminated, ExtraDiscriminator,
352 IsaPointer, AuthenticatesNullValues),
353 Flags: DINode::FlagZero);
354}
355
356DIDerivedType *
357DIBuilder::createPointerType(DIType *PointeeTy, uint64_t SizeInBits,
358 uint32_t AlignInBits,
359 std::optional<unsigned> DWARFAddressSpace,
360 StringRef Name, DINodeArray Annotations) {
361 // FIXME: Why is there a name here?
362 return DIDerivedType::get(Context&: VMContext, Tag: dwarf::DW_TAG_pointer_type, Name,
363 File: nullptr, Line: 0, Scope: nullptr, BaseType: PointeeTy, SizeInBits,
364 AlignInBits, OffsetInBits: 0, DWARFAddressSpace, PtrAuthData: std::nullopt,
365 Flags: DINode::FlagZero, ExtraData: nullptr, Annotations);
366}
367
368DIDerivedType *DIBuilder::createMemberPointerType(DIType *PointeeTy,
369 DIType *Base,
370 uint64_t SizeInBits,
371 uint32_t AlignInBits,
372 DINode::DIFlags Flags) {
373 return DIDerivedType::get(Context&: VMContext, Tag: dwarf::DW_TAG_ptr_to_member_type, Name: "",
374 File: nullptr, Line: 0, Scope: nullptr, BaseType: PointeeTy, SizeInBits,
375 AlignInBits, OffsetInBits: 0, DWARFAddressSpace: std::nullopt, PtrAuthData: std::nullopt, Flags,
376 ExtraData: Base);
377}
378
379DIDerivedType *
380DIBuilder::createReferenceType(unsigned Tag, DIType *RTy, uint64_t SizeInBits,
381 uint32_t AlignInBits,
382 std::optional<unsigned> DWARFAddressSpace) {
383 assert(RTy && "Unable to create reference type");
384 return DIDerivedType::get(Context&: VMContext, Tag, Name: "", File: nullptr, Line: 0, Scope: nullptr, BaseType: RTy,
385 SizeInBits, AlignInBits, OffsetInBits: 0, DWARFAddressSpace, PtrAuthData: {},
386 Flags: DINode::FlagZero);
387}
388
389DIDerivedType *DIBuilder::createTypedef(DIType *Ty, StringRef Name,
390 DIFile *File, unsigned LineNo,
391 DIScope *Context, uint32_t AlignInBits,
392 DINode::DIFlags Flags,
393 DINodeArray Annotations) {
394 auto *T = DIDerivedType::get(
395 Context&: VMContext, Tag: dwarf::DW_TAG_typedef, Name, File, Line: LineNo,
396 Scope: getNonCompileUnitScope(N: Context), BaseType: Ty, SizeInBits: (uint64_t)0, AlignInBits,
397 OffsetInBits: (uint64_t)0, DWARFAddressSpace: std::nullopt, PtrAuthData: std::nullopt, Flags, ExtraData: nullptr, Annotations);
398 if (isa_and_nonnull<DILocalScope>(Val: Context))
399 getSubprogramNodesTrackingVector(S: Context).emplace_back(Args&: T);
400 return T;
401}
402
403DIDerivedType *
404DIBuilder::createTemplateAlias(DIType *Ty, StringRef Name, DIFile *File,
405 unsigned LineNo, DIScope *Context,
406 DINodeArray TParams, uint32_t AlignInBits,
407 DINode::DIFlags Flags, DINodeArray Annotations) {
408 auto *T =
409 DIDerivedType::get(Context&: VMContext, Tag: dwarf::DW_TAG_template_alias, Name, File,
410 Line: LineNo, Scope: getNonCompileUnitScope(N: Context), BaseType: Ty,
411 SizeInBits: (uint64_t)0, AlignInBits, OffsetInBits: (uint64_t)0, DWARFAddressSpace: std::nullopt,
412 PtrAuthData: std::nullopt, Flags, ExtraData: TParams.get(), Annotations);
413 if (isa_and_nonnull<DILocalScope>(Val: Context))
414 getSubprogramNodesTrackingVector(S: Context).emplace_back(Args&: T);
415 return T;
416}
417
418DIDerivedType *DIBuilder::createFriend(DIType *Ty, DIType *FriendTy) {
419 assert(Ty && "Invalid type!");
420 assert(FriendTy && "Invalid friend type!");
421 return DIDerivedType::get(Context&: VMContext, Tag: dwarf::DW_TAG_friend, Name: "", File: nullptr, Line: 0, Scope: Ty,
422 BaseType: FriendTy, SizeInBits: (uint64_t)0, AlignInBits: 0, OffsetInBits: (uint64_t)0, DWARFAddressSpace: std::nullopt,
423 PtrAuthData: std::nullopt, Flags: DINode::FlagZero);
424}
425
426DIDerivedType *DIBuilder::createInheritance(DIType *Ty, DIType *BaseTy,
427 uint64_t BaseOffset,
428 uint32_t VBPtrOffset,
429 DINode::DIFlags Flags) {
430 assert(Ty && "Unable to create inheritance");
431 Metadata *ExtraData = ConstantAsMetadata::get(
432 C: ConstantInt::get(Ty: IntegerType::get(C&: VMContext, NumBits: 32), V: VBPtrOffset));
433 return DIDerivedType::get(Context&: VMContext, Tag: dwarf::DW_TAG_inheritance, Name: "", File: nullptr,
434 Line: 0, Scope: Ty, BaseType: BaseTy, SizeInBits: 0, AlignInBits: 0, OffsetInBits: BaseOffset, DWARFAddressSpace: std::nullopt,
435 PtrAuthData: std::nullopt, Flags, ExtraData);
436}
437
438DIDerivedType *DIBuilder::createMemberType(
439 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
440 uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
441 DINode::DIFlags Flags, DIType *Ty, DINodeArray Annotations) {
442 return DIDerivedType::get(Context&: VMContext, Tag: dwarf::DW_TAG_member, Name, File,
443 Line: LineNumber, Scope: getNonCompileUnitScope(N: Scope), BaseType: Ty,
444 SizeInBits, AlignInBits, OffsetInBits, DWARFAddressSpace: std::nullopt,
445 PtrAuthData: std::nullopt, Flags, ExtraData: nullptr, Annotations);
446}
447
448DIDerivedType *DIBuilder::createMemberType(
449 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
450 Metadata *SizeInBits, uint32_t AlignInBits, Metadata *OffsetInBits,
451 DINode::DIFlags Flags, DIType *Ty, DINodeArray Annotations) {
452 return DIDerivedType::get(Context&: VMContext, Tag: dwarf::DW_TAG_member, Name, File,
453 Line: LineNumber, Scope: getNonCompileUnitScope(N: Scope), BaseType: Ty,
454 SizeInBits, AlignInBits, OffsetInBits, DWARFAddressSpace: std::nullopt,
455 PtrAuthData: std::nullopt, Flags, ExtraData: nullptr, Annotations);
456}
457
458static ConstantAsMetadata *getConstantOrNull(Constant *C) {
459 if (C)
460 return ConstantAsMetadata::get(C);
461 return nullptr;
462}
463
464DIDerivedType *DIBuilder::createVariantMemberType(
465 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
466 uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
467 Constant *Discriminant, DINode::DIFlags Flags, DIType *Ty) {
468 // "ExtraData" is overloaded for bit fields and for variants, so
469 // make sure to disallow this.
470 assert((Flags & DINode::FlagBitField) == 0);
471 return DIDerivedType::get(
472 Context&: VMContext, Tag: dwarf::DW_TAG_member, Name, File, Line: LineNumber,
473 Scope: getNonCompileUnitScope(N: Scope), BaseType: Ty, SizeInBits, AlignInBits, OffsetInBits,
474 DWARFAddressSpace: std::nullopt, PtrAuthData: std::nullopt, Flags, ExtraData: getConstantOrNull(C: Discriminant));
475}
476
477DIDerivedType *DIBuilder::createVariantMemberType(DIScope *Scope,
478 DINodeArray Elements,
479 Constant *Discriminant,
480 DIType *Ty) {
481 auto *V = DICompositeType::get(Context&: VMContext, Tag: dwarf::DW_TAG_variant, Name: {}, File: nullptr,
482 Line: 0, Scope: getNonCompileUnitScope(N: Scope), BaseType: {},
483 SizeInBits: (uint64_t)0, AlignInBits: 0, OffsetInBits: (uint64_t)0, Flags: DINode::FlagZero,
484 Elements, RuntimeLang: 0, EnumKind: {}, VTableHolder: nullptr);
485
486 trackIfUnresolved(N: V);
487 return createVariantMemberType(Scope, Name: {}, File: nullptr, LineNumber: 0, SizeInBits: 0, AlignInBits: 0, OffsetInBits: 0, Discriminant,
488 Flags: DINode::FlagZero, Ty: V);
489}
490
491DIDerivedType *DIBuilder::createBitFieldMemberType(
492 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
493 Metadata *SizeInBits, Metadata *OffsetInBits, uint64_t StorageOffsetInBits,
494 DINode::DIFlags Flags, DIType *Ty, DINodeArray Annotations) {
495 Flags |= DINode::FlagBitField;
496 return DIDerivedType::get(
497 Context&: VMContext, Tag: dwarf::DW_TAG_member, Name, File, Line: LineNumber,
498 Scope: getNonCompileUnitScope(N: Scope), BaseType: Ty, SizeInBits, /*AlignInBits=*/0,
499 OffsetInBits, DWARFAddressSpace: std::nullopt, PtrAuthData: std::nullopt, Flags,
500 ExtraData: ConstantAsMetadata::get(C: ConstantInt::get(Ty: IntegerType::get(C&: VMContext, NumBits: 64),
501 V: StorageOffsetInBits)),
502 Annotations);
503}
504
505DIDerivedType *DIBuilder::createBitFieldMemberType(
506 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
507 uint64_t SizeInBits, uint64_t OffsetInBits, uint64_t StorageOffsetInBits,
508 DINode::DIFlags Flags, DIType *Ty, DINodeArray Annotations) {
509 Flags |= DINode::FlagBitField;
510 return DIDerivedType::get(
511 Context&: VMContext, Tag: dwarf::DW_TAG_member, Name, File, Line: LineNumber,
512 Scope: getNonCompileUnitScope(N: Scope), BaseType: Ty, SizeInBits, /*AlignInBits=*/0,
513 OffsetInBits, DWARFAddressSpace: std::nullopt, PtrAuthData: std::nullopt, Flags,
514 ExtraData: ConstantAsMetadata::get(C: ConstantInt::get(Ty: IntegerType::get(C&: VMContext, NumBits: 64),
515 V: StorageOffsetInBits)),
516 Annotations);
517}
518
519DIDerivedType *
520DIBuilder::createStaticMemberType(DIScope *Scope, StringRef Name, DIFile *File,
521 unsigned LineNumber, DIType *Ty,
522 DINode::DIFlags Flags, llvm::Constant *Val,
523 unsigned Tag, uint32_t AlignInBits) {
524 Flags |= DINode::FlagStaticMember;
525 return DIDerivedType::get(Context&: VMContext, Tag, Name, File, Line: LineNumber,
526 Scope: getNonCompileUnitScope(N: Scope), BaseType: Ty, SizeInBits: (uint64_t)0,
527 AlignInBits, OffsetInBits: (uint64_t)0, DWARFAddressSpace: std::nullopt,
528 PtrAuthData: std::nullopt, Flags, ExtraData: getConstantOrNull(C: Val));
529}
530
531DIDerivedType *
532DIBuilder::createObjCIVar(StringRef Name, DIFile *File, unsigned LineNumber,
533 uint64_t SizeInBits, uint32_t AlignInBits,
534 uint64_t OffsetInBits, DINode::DIFlags Flags,
535 DIType *Ty, MDNode *PropertyNode) {
536 return DIDerivedType::get(Context&: VMContext, Tag: dwarf::DW_TAG_member, Name, File,
537 Line: LineNumber, Scope: getNonCompileUnitScope(N: File), BaseType: Ty,
538 SizeInBits, AlignInBits, OffsetInBits, DWARFAddressSpace: std::nullopt,
539 PtrAuthData: std::nullopt, Flags, ExtraData: PropertyNode);
540}
541
542DIObjCProperty *
543DIBuilder::createObjCProperty(StringRef Name, DIFile *File, unsigned LineNumber,
544 StringRef GetterName, StringRef SetterName,
545 unsigned PropertyAttributes, DIType *Ty) {
546 return DIObjCProperty::get(Context&: VMContext, Name, File, Line: LineNumber, GetterName,
547 SetterName, Attributes: PropertyAttributes, Type: Ty);
548}
549
550DITemplateTypeParameter *
551DIBuilder::createTemplateTypeParameter(DIScope *Context, StringRef Name,
552 DIType *Ty, bool isDefault) {
553 assert((!Context || isa<DICompileUnit>(Context)) && "Expected compile unit");
554 return DITemplateTypeParameter::get(Context&: VMContext, Name, Type: Ty, IsDefault: isDefault);
555}
556
557static DITemplateValueParameter *
558createTemplateValueParameterHelper(LLVMContext &VMContext, unsigned Tag,
559 DIScope *Context, StringRef Name, DIType *Ty,
560 bool IsDefault, Metadata *MD) {
561 assert((!Context || isa<DICompileUnit>(Context)) && "Expected compile unit");
562 return DITemplateValueParameter::get(Context&: VMContext, Tag, Name, Type: Ty, IsDefault, Value: MD);
563}
564
565DITemplateValueParameter *
566DIBuilder::createTemplateValueParameter(DIScope *Context, StringRef Name,
567 DIType *Ty, bool isDefault,
568 Constant *Val) {
569 return createTemplateValueParameterHelper(
570 VMContext, Tag: dwarf::DW_TAG_template_value_parameter, Context, Name, Ty,
571 IsDefault: isDefault, MD: getConstantOrNull(C: Val));
572}
573
574DITemplateValueParameter *
575DIBuilder::createTemplateTemplateParameter(DIScope *Context, StringRef Name,
576 DIType *Ty, StringRef Val,
577 bool IsDefault) {
578 return createTemplateValueParameterHelper(
579 VMContext, Tag: dwarf::DW_TAG_GNU_template_template_param, Context, Name, Ty,
580 IsDefault, MD: MDString::get(Context&: VMContext, Str: Val));
581}
582
583DITemplateValueParameter *
584DIBuilder::createTemplateParameterPack(DIScope *Context, StringRef Name,
585 DIType *Ty, DINodeArray Val) {
586 return createTemplateValueParameterHelper(
587 VMContext, Tag: dwarf::DW_TAG_GNU_template_parameter_pack, Context, Name, Ty,
588 IsDefault: false, MD: Val.get());
589}
590
591DICompositeType *DIBuilder::createClassType(
592 DIScope *Context, StringRef Name, DIFile *File, unsigned LineNumber,
593 uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
594 DINode::DIFlags Flags, DIType *DerivedFrom, DINodeArray Elements,
595 unsigned RunTimeLang, DIType *VTableHolder, MDNode *TemplateParams,
596 StringRef UniqueIdentifier) {
597 assert((!Context || isa<DIScope>(Context)) &&
598 "createClassType should be called with a valid Context");
599
600 auto *R = DICompositeType::get(
601 Context&: VMContext, Tag: dwarf::DW_TAG_class_type, Name, File, Line: LineNumber,
602 Scope: getNonCompileUnitScope(N: Context), BaseType: DerivedFrom, SizeInBits, AlignInBits,
603 OffsetInBits, Flags, Elements, RuntimeLang: RunTimeLang, /*EnumKind=*/std::nullopt,
604 VTableHolder, TemplateParams: cast_or_null<MDTuple>(Val: TemplateParams), Identifier: UniqueIdentifier);
605 trackIfUnresolved(N: R);
606 if (isa_and_nonnull<DILocalScope>(Val: Context))
607 getSubprogramNodesTrackingVector(S: Context).emplace_back(Args&: R);
608 return R;
609}
610
611DICompositeType *DIBuilder::createStructType(
612 DIScope *Context, StringRef Name, DIFile *File, unsigned LineNumber,
613 Metadata *SizeInBits, uint32_t AlignInBits, DINode::DIFlags Flags,
614 DIType *DerivedFrom, DINodeArray Elements, unsigned RunTimeLang,
615 DIType *VTableHolder, StringRef UniqueIdentifier, DIType *Specification,
616 uint32_t NumExtraInhabitants) {
617 auto *R = DICompositeType::get(
618 Context&: VMContext, Tag: dwarf::DW_TAG_structure_type, Name, File, Line: LineNumber,
619 Scope: getNonCompileUnitScope(N: Context), BaseType: DerivedFrom, SizeInBits, AlignInBits, OffsetInBits: 0,
620 Flags, Elements, RuntimeLang: RunTimeLang, /*EnumKind=*/std::nullopt, VTableHolder,
621 TemplateParams: nullptr, Identifier: UniqueIdentifier, Discriminator: nullptr, DataLocation: nullptr, Associated: nullptr, Allocated: nullptr, Rank: nullptr,
622 Annotations: nullptr, Specification, NumExtraInhabitants);
623 trackIfUnresolved(N: R);
624 if (isa_and_nonnull<DILocalScope>(Val: Context))
625 getSubprogramNodesTrackingVector(S: Context).emplace_back(Args&: R);
626 return R;
627}
628
629DICompositeType *DIBuilder::createStructType(
630 DIScope *Context, StringRef Name, DIFile *File, unsigned LineNumber,
631 uint64_t SizeInBits, uint32_t AlignInBits, DINode::DIFlags Flags,
632 DIType *DerivedFrom, DINodeArray Elements, unsigned RunTimeLang,
633 DIType *VTableHolder, StringRef UniqueIdentifier, DIType *Specification,
634 uint32_t NumExtraInhabitants) {
635 auto *R = DICompositeType::get(
636 Context&: VMContext, Tag: dwarf::DW_TAG_structure_type, Name, File, Line: LineNumber,
637 Scope: getNonCompileUnitScope(N: Context), BaseType: DerivedFrom, SizeInBits, AlignInBits, OffsetInBits: 0,
638 Flags, Elements, RuntimeLang: RunTimeLang, /*EnumKind=*/std::nullopt, VTableHolder,
639 TemplateParams: nullptr, Identifier: UniqueIdentifier, Discriminator: nullptr, DataLocation: nullptr, Associated: nullptr, Allocated: nullptr, Rank: nullptr,
640 Annotations: nullptr, Specification, NumExtraInhabitants);
641 trackIfUnresolved(N: R);
642 if (isa_and_nonnull<DILocalScope>(Val: Context))
643 getSubprogramNodesTrackingVector(S: Context).emplace_back(Args&: R);
644 return R;
645}
646
647DICompositeType *DIBuilder::createUnionType(
648 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
649 uint64_t SizeInBits, uint32_t AlignInBits, DINode::DIFlags Flags,
650 DINodeArray Elements, unsigned RunTimeLang, StringRef UniqueIdentifier) {
651 auto *R = DICompositeType::get(
652 Context&: VMContext, Tag: dwarf::DW_TAG_union_type, Name, File, Line: LineNumber,
653 Scope: getNonCompileUnitScope(N: Scope), BaseType: nullptr, SizeInBits, AlignInBits, OffsetInBits: 0, Flags,
654 Elements, RuntimeLang: RunTimeLang, /*EnumKind=*/std::nullopt, VTableHolder: nullptr, TemplateParams: nullptr,
655 Identifier: UniqueIdentifier);
656 trackIfUnresolved(N: R);
657 if (isa_and_nonnull<DILocalScope>(Val: Scope))
658 getSubprogramNodesTrackingVector(S: Scope).emplace_back(Args&: R);
659 return R;
660}
661
662DICompositeType *
663DIBuilder::createVariantPart(DIScope *Scope, StringRef Name, DIFile *File,
664 unsigned LineNumber, uint64_t SizeInBits,
665 uint32_t AlignInBits, DINode::DIFlags Flags,
666 DIDerivedType *Discriminator, DINodeArray Elements,
667 StringRef UniqueIdentifier) {
668 auto *R = DICompositeType::get(
669 Context&: VMContext, Tag: dwarf::DW_TAG_variant_part, Name, File, Line: LineNumber,
670 Scope: getNonCompileUnitScope(N: Scope), BaseType: nullptr, SizeInBits, AlignInBits, OffsetInBits: 0, Flags,
671 Elements, RuntimeLang: 0, /*EnumKind=*/std::nullopt, VTableHolder: nullptr, TemplateParams: nullptr,
672 Identifier: UniqueIdentifier, Discriminator);
673 trackIfUnresolved(N: R);
674 return R;
675}
676
677DISubroutineType *DIBuilder::createSubroutineType(DITypeArray ParameterTypes,
678 DINode::DIFlags Flags,
679 unsigned CC) {
680 return DISubroutineType::get(Context&: VMContext, Flags, CC, TypeArray: ParameterTypes);
681}
682
683DICompositeType *DIBuilder::createEnumerationType(
684 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
685 uint64_t SizeInBits, uint32_t AlignInBits, DINodeArray Elements,
686 DIType *UnderlyingType, unsigned RunTimeLang, StringRef UniqueIdentifier,
687 bool IsScoped, std::optional<uint32_t> EnumKind) {
688 auto *CTy = DICompositeType::get(
689 Context&: VMContext, Tag: dwarf::DW_TAG_enumeration_type, Name, File, Line: LineNumber,
690 Scope: getNonCompileUnitScope(N: Scope), BaseType: UnderlyingType, SizeInBits, AlignInBits, OffsetInBits: 0,
691 Flags: IsScoped ? DINode::FlagEnumClass : DINode::FlagZero, Elements,
692 RuntimeLang: RunTimeLang, EnumKind, VTableHolder: nullptr, TemplateParams: nullptr, Identifier: UniqueIdentifier);
693 if (isa_and_nonnull<DILocalScope>(Val: Scope))
694 getSubprogramNodesTrackingVector(S: Scope).emplace_back(Args&: CTy);
695 else
696 EnumTypes.emplace_back(Args&: CTy);
697 trackIfUnresolved(N: CTy);
698 return CTy;
699}
700
701DIDerivedType *DIBuilder::createSetType(DIScope *Scope, StringRef Name,
702 DIFile *File, unsigned LineNo,
703 uint64_t SizeInBits,
704 uint32_t AlignInBits, DIType *Ty) {
705 auto *R = DIDerivedType::get(Context&: VMContext, Tag: dwarf::DW_TAG_set_type, Name, File,
706 Line: LineNo, Scope: getNonCompileUnitScope(N: Scope), BaseType: Ty,
707 SizeInBits, AlignInBits, OffsetInBits: 0, DWARFAddressSpace: std::nullopt,
708 PtrAuthData: std::nullopt, Flags: DINode::FlagZero);
709 trackIfUnresolved(N: R);
710 if (isa_and_nonnull<DILocalScope>(Val: Scope))
711 getSubprogramNodesTrackingVector(S: Scope).emplace_back(Args&: R);
712 return R;
713}
714
715DICompositeType *
716DIBuilder::createArrayType(uint64_t Size, uint32_t AlignInBits, DIType *Ty,
717 DINodeArray Subscripts,
718 PointerUnion<DIExpression *, DIVariable *> DL,
719 PointerUnion<DIExpression *, DIVariable *> AS,
720 PointerUnion<DIExpression *, DIVariable *> AL,
721 PointerUnion<DIExpression *, DIVariable *> RK) {
722 return createArrayType(Scope: nullptr, Name: StringRef(), File: nullptr, LineNumber: 0, Size, AlignInBits,
723 Ty, Subscripts, DataLocation: DL, Associated: AS, Allocated: AL, Rank: RK);
724}
725
726DICompositeType *DIBuilder::createArrayType(
727 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
728 uint64_t Size, uint32_t AlignInBits, DIType *Ty, DINodeArray Subscripts,
729 PointerUnion<DIExpression *, DIVariable *> DL,
730 PointerUnion<DIExpression *, DIVariable *> AS,
731 PointerUnion<DIExpression *, DIVariable *> AL,
732 PointerUnion<DIExpression *, DIVariable *> RK, Metadata *BitStride) {
733 auto *R = DICompositeType::get(
734 Context&: VMContext, Tag: dwarf::DW_TAG_array_type, Name, File, Line: LineNumber,
735 Scope: getNonCompileUnitScope(N: Scope), BaseType: Ty, SizeInBits: Size, AlignInBits, OffsetInBits: 0, Flags: DINode::FlagZero,
736 Elements: Subscripts, RuntimeLang: 0, /*EnumKind=*/std::nullopt, VTableHolder: nullptr, TemplateParams: nullptr, Identifier: "", Discriminator: nullptr,
737 DataLocation: isa<DIExpression *>(Val: DL) ? (Metadata *)cast<DIExpression *>(Val&: DL)
738 : (Metadata *)cast<DIVariable *>(Val&: DL),
739 Associated: isa<DIExpression *>(Val: AS) ? (Metadata *)cast<DIExpression *>(Val&: AS)
740 : (Metadata *)cast<DIVariable *>(Val&: AS),
741 Allocated: isa<DIExpression *>(Val: AL) ? (Metadata *)cast<DIExpression *>(Val&: AL)
742 : (Metadata *)cast<DIVariable *>(Val&: AL),
743 Rank: isa<DIExpression *>(Val: RK) ? (Metadata *)cast<DIExpression *>(Val&: RK)
744 : (Metadata *)cast<DIVariable *>(Val&: RK),
745 Annotations: nullptr, Specification: nullptr, NumExtraInhabitants: 0, BitStride);
746 trackIfUnresolved(N: R);
747 if (isa_and_nonnull<DILocalScope>(Val: Scope))
748 getSubprogramNodesTrackingVector(S: Scope).emplace_back(Args&: R);
749 return R;
750}
751
752DICompositeType *DIBuilder::createVectorType(uint64_t Size,
753 uint32_t AlignInBits, DIType *Ty,
754 DINodeArray Subscripts,
755 Metadata *BitStride) {
756 auto *R = DICompositeType::get(
757 Context&: VMContext, Tag: dwarf::DW_TAG_array_type, /*Name=*/"",
758 /*File=*/nullptr, /*Line=*/0, /*Scope=*/nullptr, /*BaseType=*/Ty,
759 /*SizeInBits=*/Size, /*AlignInBits=*/AlignInBits, /*OffsetInBits=*/0,
760 /*Flags=*/DINode::FlagVector, /*Elements=*/Subscripts,
761 /*RuntimeLang=*/0, /*EnumKind=*/std::nullopt, /*VTableHolder=*/nullptr,
762 /*TemplateParams=*/nullptr, /*Identifier=*/"",
763 /*Discriminator=*/nullptr, /*DataLocation=*/nullptr,
764 /*Associated=*/nullptr, /*Allocated=*/nullptr, /*Rank=*/nullptr,
765 /*Annotations=*/nullptr, /*Specification=*/nullptr,
766 /*NumExtraInhabitants=*/0,
767 /*BitStride=*/BitStride);
768 trackIfUnresolved(N: R);
769 return R;
770}
771
772DISubprogram *DIBuilder::createArtificialSubprogram(DISubprogram *SP) {
773 auto NewSP = SP->cloneWithFlags(NewFlags: SP->getFlags() | DINode::FlagArtificial);
774 return MDNode::replaceWithDistinct(N: std::move(NewSP));
775}
776
777static DIType *createTypeWithFlags(const DIType *Ty,
778 DINode::DIFlags FlagsToSet) {
779 auto NewTy = Ty->cloneWithFlags(NewFlags: Ty->getFlags() | FlagsToSet);
780 return MDNode::replaceWithUniqued(N: std::move(NewTy));
781}
782
783DIType *DIBuilder::createArtificialType(DIType *Ty) {
784 // FIXME: Restrict this to the nodes where it's valid.
785 if (Ty->isArtificial())
786 return Ty;
787 return createTypeWithFlags(Ty, FlagsToSet: DINode::FlagArtificial);
788}
789
790DIType *DIBuilder::createObjectPointerType(DIType *Ty, bool Implicit) {
791 // FIXME: Restrict this to the nodes where it's valid.
792 if (Ty->isObjectPointer())
793 return Ty;
794 DINode::DIFlags Flags = DINode::FlagObjectPointer;
795
796 if (Implicit)
797 Flags |= DINode::FlagArtificial;
798
799 return createTypeWithFlags(Ty, FlagsToSet: Flags);
800}
801
802void DIBuilder::retainType(DIScope *T) {
803 assert(T && "Expected non-null type");
804 assert((isa<DIType>(T) || (isa<DISubprogram>(T) &&
805 cast<DISubprogram>(T)->isDefinition() == false)) &&
806 "Expected type or subprogram declaration");
807 if (!isa_and_nonnull<DILocalScope>(Val: T->getScope()))
808 AllRetainTypes.emplace_back(Args&: T);
809}
810
811DIBasicType *DIBuilder::createUnspecifiedParameter() { return nullptr; }
812
813DICompositeType *DIBuilder::createForwardDecl(
814 unsigned Tag, StringRef Name, DIScope *Scope, DIFile *F, unsigned Line,
815 unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits,
816 StringRef UniqueIdentifier, std::optional<uint32_t> EnumKind) {
817 // FIXME: Define in terms of createReplaceableForwardDecl() by calling
818 // replaceWithUniqued().
819 auto *RetTy = DICompositeType::get(
820 Context&: VMContext, Tag, Name, File: F, Line, Scope: getNonCompileUnitScope(N: Scope), BaseType: nullptr,
821 SizeInBits, AlignInBits, OffsetInBits: 0, Flags: DINode::FlagFwdDecl, Elements: nullptr, RuntimeLang,
822 /*EnumKind=*/EnumKind, VTableHolder: nullptr, TemplateParams: nullptr, Identifier: UniqueIdentifier);
823 trackIfUnresolved(N: RetTy);
824 if (isa_and_nonnull<DILocalScope>(Val: Scope))
825 getSubprogramNodesTrackingVector(S: Scope).emplace_back(Args&: RetTy);
826 return RetTy;
827}
828
829DICompositeType *DIBuilder::createReplaceableCompositeType(
830 unsigned Tag, StringRef Name, DIScope *Scope, DIFile *F, unsigned Line,
831 unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits,
832 DINode::DIFlags Flags, StringRef UniqueIdentifier, DINodeArray Annotations,
833 std::optional<uint32_t> EnumKind) {
834 auto *RetTy =
835 DICompositeType::getTemporary(
836 Context&: VMContext, Tag, Name, File: F, Line, Scope: getNonCompileUnitScope(N: Scope), BaseType: nullptr,
837 SizeInBits, AlignInBits, OffsetInBits: 0, Flags, Elements: nullptr, RuntimeLang, EnumKind,
838 VTableHolder: nullptr, TemplateParams: nullptr, Identifier: UniqueIdentifier, Discriminator: nullptr, DataLocation: nullptr, Associated: nullptr,
839 Allocated: nullptr, Rank: nullptr, Annotations)
840 .release();
841 trackIfUnresolved(N: RetTy);
842 if (isa_and_nonnull<DILocalScope>(Val: Scope))
843 getSubprogramNodesTrackingVector(S: Scope).emplace_back(Args&: RetTy);
844 return RetTy;
845}
846
847DINodeArray DIBuilder::getOrCreateArray(ArrayRef<Metadata *> Elements) {
848 return MDTuple::get(Context&: VMContext, MDs: Elements);
849}
850
851DIMacroNodeArray
852DIBuilder::getOrCreateMacroArray(ArrayRef<Metadata *> Elements) {
853 return MDTuple::get(Context&: VMContext, MDs: Elements);
854}
855
856DITypeArray DIBuilder::getOrCreateTypeArray(ArrayRef<Metadata *> Elements) {
857 SmallVector<llvm::Metadata *, 16> Elts;
858 for (Metadata *E : Elements) {
859 if (isa_and_nonnull<MDNode>(Val: E))
860 Elts.push_back(Elt: cast<DIType>(Val: E));
861 else
862 Elts.push_back(Elt: E);
863 }
864 return DITypeArray(MDNode::get(Context&: VMContext, MDs: Elts));
865}
866
867DISubrange *DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
868 auto *LB = ConstantAsMetadata::get(
869 C: ConstantInt::getSigned(Ty: Type::getInt64Ty(C&: VMContext), V: Lo));
870 auto *CountNode = ConstantAsMetadata::get(
871 C: ConstantInt::getSigned(Ty: Type::getInt64Ty(C&: VMContext), V: Count));
872 return DISubrange::get(Context&: VMContext, CountNode, LowerBound: LB, UpperBound: nullptr, Stride: nullptr);
873}
874
875DISubrange *DIBuilder::getOrCreateSubrange(int64_t Lo, Metadata *CountNode) {
876 auto *LB = ConstantAsMetadata::get(
877 C: ConstantInt::getSigned(Ty: Type::getInt64Ty(C&: VMContext), V: Lo));
878 return DISubrange::get(Context&: VMContext, CountNode, LowerBound: LB, UpperBound: nullptr, Stride: nullptr);
879}
880
881DISubrange *DIBuilder::getOrCreateSubrange(Metadata *CountNode, Metadata *LB,
882 Metadata *UB, Metadata *Stride) {
883 return DISubrange::get(Context&: VMContext, CountNode, LowerBound: LB, UpperBound: UB, Stride);
884}
885
886DIGenericSubrange *DIBuilder::getOrCreateGenericSubrange(
887 DIGenericSubrange::BoundType CountNode, DIGenericSubrange::BoundType LB,
888 DIGenericSubrange::BoundType UB, DIGenericSubrange::BoundType Stride) {
889 auto ConvToMetadata = [&](DIGenericSubrange::BoundType Bound) -> Metadata * {
890 return isa<DIExpression *>(Val: Bound) ? (Metadata *)cast<DIExpression *>(Val&: Bound)
891 : (Metadata *)cast<DIVariable *>(Val&: Bound);
892 };
893 return DIGenericSubrange::get(Context&: VMContext, CountNode: ConvToMetadata(CountNode),
894 LowerBound: ConvToMetadata(LB), UpperBound: ConvToMetadata(UB),
895 Stride: ConvToMetadata(Stride));
896}
897
898DISubrangeType *DIBuilder::createSubrangeType(
899 StringRef Name, DIFile *File, unsigned LineNo, DIScope *Scope,
900 uint64_t SizeInBits, uint32_t AlignInBits, DINode::DIFlags Flags,
901 DIType *Ty, Metadata *LowerBound, Metadata *UpperBound, Metadata *Stride,
902 Metadata *Bias) {
903 auto *T = DISubrangeType::get(Context&: VMContext, Name, File, Line: LineNo, Scope,
904 SizeInBits, AlignInBits, Flags, BaseType: Ty, LowerBound,
905 UpperBound, Stride, Bias);
906 if (isa_and_nonnull<DILocalScope>(Val: Scope))
907 getSubprogramNodesTrackingVector(S: Scope).emplace_back(Args&: T);
908 return T;
909}
910
911static void checkGlobalVariableScope(DIScope *Context) {
912#ifndef NDEBUG
913 if (auto *CT =
914 dyn_cast_or_null<DICompositeType>(getNonCompileUnitScope(Context)))
915 assert(CT->getIdentifier().empty() &&
916 "Context of a global variable should not be a type with identifier");
917#endif
918}
919
920DIGlobalVariableExpression *DIBuilder::createGlobalVariableExpression(
921 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F,
922 unsigned LineNumber, DIType *Ty, bool IsLocalToUnit, bool isDefined,
923 DIExpression *Expr, MDNode *Decl, MDTuple *TemplateParams,
924 uint32_t AlignInBits, DINodeArray Annotations) {
925 checkGlobalVariableScope(Context);
926
927 auto *GV = DIGlobalVariable::getDistinct(
928 Context&: VMContext, Scope: cast_or_null<DIScope>(Val: Context), Name, LinkageName, File: F,
929 Line: LineNumber, Type: Ty, IsLocalToUnit, IsDefinition: isDefined,
930 StaticDataMemberDeclaration: cast_or_null<DIDerivedType>(Val: Decl), TemplateParams, AlignInBits,
931 Annotations);
932 if (!Expr)
933 Expr = createExpression();
934 auto *N = DIGlobalVariableExpression::get(Context&: VMContext, Variable: GV, Expression: Expr);
935 AllGVs.push_back(Elt: N);
936 return N;
937}
938
939DIGlobalVariable *DIBuilder::createTempGlobalVariableFwdDecl(
940 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F,
941 unsigned LineNumber, DIType *Ty, bool IsLocalToUnit, MDNode *Decl,
942 MDTuple *TemplateParams, uint32_t AlignInBits) {
943 checkGlobalVariableScope(Context);
944
945 return DIGlobalVariable::getTemporary(
946 Context&: VMContext, Scope: cast_or_null<DIScope>(Val: Context), Name, LinkageName, File: F,
947 Line: LineNumber, Type: Ty, IsLocalToUnit, IsDefinition: false,
948 StaticDataMemberDeclaration: cast_or_null<DIDerivedType>(Val: Decl), TemplateParams, AlignInBits,
949 Annotations: nullptr)
950 .release();
951}
952
953static DILocalVariable *createLocalVariable(
954 LLVMContext &VMContext,
955 SmallVectorImpl<TrackingMDNodeRef> &PreservedNodes,
956 DIScope *Context, StringRef Name, unsigned ArgNo, DIFile *File,
957 unsigned LineNo, DIType *Ty, bool AlwaysPreserve, DINode::DIFlags Flags,
958 uint32_t AlignInBits, DINodeArray Annotations = nullptr) {
959 // FIXME: Why doesn't this check for a subprogram or lexical block (AFAICT
960 // the only valid scopes)?
961 auto *Scope = cast<DILocalScope>(Val: Context);
962 auto *Node = DILocalVariable::get(Context&: VMContext, Scope, Name, File, Line: LineNo, Type: Ty,
963 Arg: ArgNo, Flags, AlignInBits, Annotations);
964 if (AlwaysPreserve) {
965 // The optimizer may remove local variables. If there is an interest
966 // to preserve variable info in such situation then stash it in a
967 // named mdnode.
968 PreservedNodes.emplace_back(Args&: Node);
969 }
970 return Node;
971}
972
973DILocalVariable *DIBuilder::createAutoVariable(DIScope *Scope, StringRef Name,
974 DIFile *File, unsigned LineNo,
975 DIType *Ty, bool AlwaysPreserve,
976 DINode::DIFlags Flags,
977 uint32_t AlignInBits) {
978 assert(Scope && isa<DILocalScope>(Scope) &&
979 "Unexpected scope for a local variable.");
980 return createLocalVariable(
981 VMContext, PreservedNodes&: getSubprogramNodesTrackingVector(S: Scope), Context: Scope, Name,
982 /* ArgNo */ 0, File, LineNo, Ty, AlwaysPreserve, Flags, AlignInBits);
983}
984
985DILocalVariable *DIBuilder::createParameterVariable(
986 DIScope *Scope, StringRef Name, unsigned ArgNo, DIFile *File,
987 unsigned LineNo, DIType *Ty, bool AlwaysPreserve, DINode::DIFlags Flags,
988 DINodeArray Annotations) {
989 assert(ArgNo && "Expected non-zero argument number for parameter");
990 assert(Scope && isa<DILocalScope>(Scope) &&
991 "Unexpected scope for a local variable.");
992 return createLocalVariable(
993 VMContext, PreservedNodes&: getSubprogramNodesTrackingVector(S: Scope), Context: Scope, Name, ArgNo,
994 File, LineNo, Ty, AlwaysPreserve, Flags, /*AlignInBits=*/0, Annotations);
995}
996
997DILabel *DIBuilder::createLabel(DIScope *Context, StringRef Name, DIFile *File,
998 unsigned LineNo, unsigned Column,
999 bool IsArtificial,
1000 std::optional<unsigned> CoroSuspendIdx,
1001 bool AlwaysPreserve) {
1002 auto *Scope = cast<DILocalScope>(Val: Context);
1003 auto *Node = DILabel::get(Context&: VMContext, Scope, Name, File, Line: LineNo, Column,
1004 IsArtificial, CoroSuspendIdx);
1005
1006 if (AlwaysPreserve) {
1007 /// The optimizer may remove labels. If there is an interest
1008 /// to preserve label info in such situation then append it to
1009 /// the list of retained nodes of the DISubprogram.
1010 getSubprogramNodesTrackingVector(S: Scope).emplace_back(Args&: Node);
1011 }
1012 return Node;
1013}
1014
1015DIExpression *DIBuilder::createExpression(ArrayRef<uint64_t> Addr) {
1016 return DIExpression::get(Context&: VMContext, Elements: Addr);
1017}
1018
1019template <class... Ts>
1020static DISubprogram *getSubprogram(bool IsDistinct, Ts &&...Args) {
1021 if (IsDistinct)
1022 return DISubprogram::getDistinct(std::forward<Ts>(Args)...);
1023 return DISubprogram::get(std::forward<Ts>(Args)...);
1024}
1025
1026DISubprogram *DIBuilder::createFunction(
1027 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *File,
1028 unsigned LineNo, DISubroutineType *Ty, unsigned ScopeLine,
1029 DINode::DIFlags Flags, DISubprogram::DISPFlags SPFlags,
1030 DITemplateParameterArray TParams, DISubprogram *Decl,
1031 DITypeArray ThrownTypes, DINodeArray Annotations, StringRef TargetFuncName,
1032 bool UseKeyInstructions) {
1033 bool IsDefinition = SPFlags & DISubprogram::SPFlagDefinition;
1034 auto *Node = getSubprogram(
1035 /*IsDistinct=*/IsDefinition, Args&: VMContext, Args: getNonCompileUnitScope(N: Context),
1036 Args&: Name, Args&: LinkageName, Args&: File, Args&: LineNo, Args&: Ty, Args&: ScopeLine, Args: nullptr, Args: 0, Args: 0, Args&: Flags,
1037 Args&: SPFlags, Args: IsDefinition ? CUNode : nullptr, Args&: TParams, Args&: Decl, Args: nullptr,
1038 Args&: ThrownTypes, Args&: Annotations, Args&: TargetFuncName, Args&: UseKeyInstructions);
1039
1040 AllSubprograms.push_back(Elt: Node);
1041 trackIfUnresolved(N: Node);
1042 return Node;
1043}
1044
1045DISubprogram *DIBuilder::createTempFunctionFwdDecl(
1046 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *File,
1047 unsigned LineNo, DISubroutineType *Ty, unsigned ScopeLine,
1048 DINode::DIFlags Flags, DISubprogram::DISPFlags SPFlags,
1049 DITemplateParameterArray TParams, DISubprogram *Decl,
1050 DITypeArray ThrownTypes) {
1051 bool IsDefinition = SPFlags & DISubprogram::SPFlagDefinition;
1052 return DISubprogram::getTemporary(Context&: VMContext, Scope: getNonCompileUnitScope(N: Context),
1053 Name, LinkageName, File, Line: LineNo, Type: Ty,
1054 ScopeLine, ContainingType: nullptr, VirtualIndex: 0, ThisAdjustment: 0, Flags, SPFlags,
1055 Unit: IsDefinition ? CUNode : nullptr, TemplateParams: TParams,
1056 Declaration: Decl, RetainedNodes: nullptr, ThrownTypes)
1057 .release();
1058}
1059
1060DISubprogram *DIBuilder::createMethod(
1061 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F,
1062 unsigned LineNo, DISubroutineType *Ty, unsigned VIndex, int ThisAdjustment,
1063 DIType *VTableHolder, DINode::DIFlags Flags,
1064 DISubprogram::DISPFlags SPFlags, DITemplateParameterArray TParams,
1065 DITypeArray ThrownTypes, bool UseKeyInstructions) {
1066 assert(getNonCompileUnitScope(Context) &&
1067 "Methods should have both a Context and a context that isn't "
1068 "the compile unit.");
1069 // FIXME: Do we want to use different scope/lines?
1070 bool IsDefinition = SPFlags & DISubprogram::SPFlagDefinition;
1071 auto *SP = getSubprogram(
1072 /*IsDistinct=*/IsDefinition, Args&: VMContext, Args: cast<DIScope>(Val: Context), Args&: Name,
1073 Args&: LinkageName, Args&: F, Args&: LineNo, Args&: Ty, Args&: LineNo, Args&: VTableHolder, Args&: VIndex, Args&: ThisAdjustment,
1074 Args&: Flags, Args&: SPFlags, Args: IsDefinition ? CUNode : nullptr, Args&: TParams, Args: nullptr,
1075 Args: nullptr, Args&: ThrownTypes, Args: nullptr, Args: "", Args: IsDefinition && UseKeyInstructions);
1076
1077 AllSubprograms.push_back(Elt: SP);
1078 trackIfUnresolved(N: SP);
1079 return SP;
1080}
1081
1082DICommonBlock *DIBuilder::createCommonBlock(DIScope *Scope,
1083 DIGlobalVariable *Decl,
1084 StringRef Name, DIFile *File,
1085 unsigned LineNo) {
1086 return DICommonBlock::get(Context&: VMContext, Scope, Decl, Name, File, LineNo);
1087}
1088
1089DINamespace *DIBuilder::createNameSpace(DIScope *Scope, StringRef Name,
1090 bool ExportSymbols) {
1091
1092 // It is okay to *not* make anonymous top-level namespaces distinct, because
1093 // all nodes that have an anonymous namespace as their parent scope are
1094 // guaranteed to be unique and/or are linked to their containing
1095 // DICompileUnit. This decision is an explicit tradeoff of link time versus
1096 // memory usage versus code simplicity and may get revisited in the future.
1097 return DINamespace::get(Context&: VMContext, Scope: getNonCompileUnitScope(N: Scope), Name,
1098 ExportSymbols);
1099}
1100
1101DIModule *DIBuilder::createModule(DIScope *Scope, StringRef Name,
1102 StringRef ConfigurationMacros,
1103 StringRef IncludePath, StringRef APINotesFile,
1104 DIFile *File, unsigned LineNo, bool IsDecl) {
1105 return DIModule::get(Context&: VMContext, File, Scope: getNonCompileUnitScope(N: Scope), Name,
1106 ConfigurationMacros, IncludePath, APINotesFile, LineNo,
1107 IsDecl);
1108}
1109
1110DILexicalBlockFile *DIBuilder::createLexicalBlockFile(DIScope *Scope,
1111 DIFile *File,
1112 unsigned Discriminator) {
1113 return DILexicalBlockFile::get(Context&: VMContext, Scope, File, Discriminator);
1114}
1115
1116DILexicalBlock *DIBuilder::createLexicalBlock(DIScope *Scope, DIFile *File,
1117 unsigned Line, unsigned Col) {
1118 // Make these distinct, to avoid merging two lexical blocks on the same
1119 // file/line/column.
1120 return DILexicalBlock::getDistinct(Context&: VMContext, Scope: getNonCompileUnitScope(N: Scope),
1121 File, Line, Column: Col);
1122}
1123
1124DbgInstPtr DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
1125 DIExpression *Expr, const DILocation *DL,
1126 BasicBlock *InsertAtEnd) {
1127 // If this block already has a terminator then insert this intrinsic before
1128 // the terminator. Otherwise, put it at the end of the block.
1129 Instruction *InsertBefore = InsertAtEnd->getTerminator();
1130 return insertDeclare(Storage, VarInfo, Expr, DL,
1131 InsertPt: InsertBefore ? InsertBefore->getIterator()
1132 : InsertAtEnd->end());
1133}
1134
1135DbgInstPtr DIBuilder::insertDbgAssign(Instruction *LinkedInstr, Value *Val,
1136 DILocalVariable *SrcVar,
1137 DIExpression *ValExpr, Value *Addr,
1138 DIExpression *AddrExpr,
1139 const DILocation *DL) {
1140 auto *Link = cast_or_null<DIAssignID>(
1141 Val: LinkedInstr->getMetadata(KindID: LLVMContext::MD_DIAssignID));
1142 assert(Link && "Linked instruction must have DIAssign metadata attached");
1143
1144 DbgVariableRecord *DVR = DbgVariableRecord::createDVRAssign(
1145 Val, Variable: SrcVar, Expression: ValExpr, AssignID: Link, Address: Addr, AddressExpression: AddrExpr, DI: DL);
1146 // Insert after LinkedInstr.
1147 BasicBlock::iterator NextIt = std::next(x: LinkedInstr->getIterator());
1148 NextIt.setHeadBit(true);
1149 insertDbgVariableRecord(DVR, InsertPt: NextIt);
1150 return DVR;
1151}
1152
1153/// Initialize IRBuilder for inserting dbg.declare and dbg.value intrinsics.
1154/// This abstracts over the various ways to specify an insert position.
1155static void initIRBuilder(IRBuilder<> &Builder, const DILocation *DL,
1156 InsertPosition InsertPt) {
1157 Builder.SetInsertPoint(TheBB: InsertPt.getBasicBlock(), IP: InsertPt);
1158 Builder.SetCurrentDebugLocation(DL);
1159}
1160
1161static Value *getDbgIntrinsicValueImpl(LLVMContext &VMContext, Value *V) {
1162 assert(V && "no value passed to dbg intrinsic");
1163 return MetadataAsValue::get(Context&: VMContext, MD: ValueAsMetadata::get(V));
1164}
1165
1166DbgInstPtr DIBuilder::insertDbgValueIntrinsic(llvm::Value *Val,
1167 DILocalVariable *VarInfo,
1168 DIExpression *Expr,
1169 const DILocation *DL,
1170 InsertPosition InsertPt) {
1171 DbgVariableRecord *DVR =
1172 DbgVariableRecord::createDbgVariableRecord(Location: Val, DV: VarInfo, Expr, DI: DL);
1173 insertDbgVariableRecord(DVR, InsertPt);
1174 return DVR;
1175}
1176
1177DbgInstPtr DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
1178 DIExpression *Expr, const DILocation *DL,
1179 InsertPosition InsertPt) {
1180 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.declare");
1181 assert(DL && "Expected debug loc");
1182 assert(DL->getScope()->getSubprogram() ==
1183 VarInfo->getScope()->getSubprogram() &&
1184 "Expected matching subprograms");
1185
1186 DbgVariableRecord *DVR =
1187 DbgVariableRecord::createDVRDeclare(Address: Storage, DV: VarInfo, Expr, DI: DL);
1188 insertDbgVariableRecord(DVR, InsertPt);
1189 return DVR;
1190}
1191
1192DbgInstPtr DIBuilder::insertDeclareValue(Value *Storage,
1193 DILocalVariable *VarInfo,
1194 DIExpression *Expr,
1195 const DILocation *DL,
1196 InsertPosition InsertPt) {
1197 assert(VarInfo &&
1198 "empty or invalid DILocalVariable* passed to dbg.declare_value");
1199 assert(DL && "Expected debug loc");
1200 assert(DL->getScope()->getSubprogram() ==
1201 VarInfo->getScope()->getSubprogram() &&
1202 "Expected matching subprograms");
1203
1204 DbgVariableRecord *DVR =
1205 DbgVariableRecord::createDVRDeclareValue(Address: Storage, DV: VarInfo, Expr, DI: DL);
1206 insertDbgVariableRecord(DVR, InsertPt);
1207 return DVR;
1208}
1209
1210void DIBuilder::insertDbgVariableRecord(DbgVariableRecord *DVR,
1211 InsertPosition InsertPt) {
1212 assert(InsertPt.isValid());
1213 trackIfUnresolved(N: DVR->getVariable());
1214 trackIfUnresolved(N: DVR->getExpression());
1215 if (DVR->isDbgAssign())
1216 trackIfUnresolved(N: DVR->getAddressExpression());
1217
1218 auto *BB = InsertPt.getBasicBlock();
1219 BB->insertDbgRecordBefore(DR: DVR, Here: InsertPt);
1220}
1221
1222Instruction *DIBuilder::insertDbgIntrinsic(llvm::Function *IntrinsicFn,
1223 Value *V, DILocalVariable *VarInfo,
1224 DIExpression *Expr,
1225 const DILocation *DL,
1226 InsertPosition InsertPt) {
1227 assert(IntrinsicFn && "must pass a non-null intrinsic function");
1228 assert(V && "must pass a value to a dbg intrinsic");
1229 assert(VarInfo &&
1230 "empty or invalid DILocalVariable* passed to debug intrinsic");
1231 assert(DL && "Expected debug loc");
1232 assert(DL->getScope()->getSubprogram() ==
1233 VarInfo->getScope()->getSubprogram() &&
1234 "Expected matching subprograms");
1235
1236 trackIfUnresolved(N: VarInfo);
1237 trackIfUnresolved(N: Expr);
1238 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, V),
1239 MetadataAsValue::get(Context&: VMContext, MD: VarInfo),
1240 MetadataAsValue::get(Context&: VMContext, MD: Expr)};
1241
1242 IRBuilder<> B(DL->getContext());
1243 initIRBuilder(Builder&: B, DL, InsertPt);
1244 return B.CreateCall(Callee: IntrinsicFn, Args);
1245}
1246
1247DbgInstPtr DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL,
1248 InsertPosition InsertPt) {
1249 assert(LabelInfo && "empty or invalid DILabel* passed to dbg.label");
1250 assert(DL && "Expected debug loc");
1251 assert(DL->getScope()->getSubprogram() ==
1252 LabelInfo->getScope()->getSubprogram() &&
1253 "Expected matching subprograms");
1254
1255 trackIfUnresolved(N: LabelInfo);
1256 DbgLabelRecord *DLR = new DbgLabelRecord(LabelInfo, DL);
1257 if (InsertPt.isValid()) {
1258 auto *BB = InsertPt.getBasicBlock();
1259 BB->insertDbgRecordBefore(DR: DLR, Here: InsertPt);
1260 }
1261 return DLR;
1262}
1263
1264void DIBuilder::replaceVTableHolder(DICompositeType *&T, DIType *VTableHolder) {
1265 {
1266 TypedTrackingMDRef<DICompositeType> N(T);
1267 N->replaceVTableHolder(VTableHolder);
1268 T = N.get();
1269 }
1270
1271 // If this didn't create a self-reference, just return.
1272 if (T != VTableHolder)
1273 return;
1274
1275 // Look for unresolved operands. T will drop RAUW support, orphaning any
1276 // cycles underneath it.
1277 if (T->isResolved())
1278 for (const MDOperand &O : T->operands())
1279 if (auto *N = dyn_cast_or_null<MDNode>(Val: O))
1280 trackIfUnresolved(N);
1281}
1282
1283void DIBuilder::replaceArrays(DICompositeType *&T, DINodeArray Elements,
1284 DINodeArray TParams) {
1285 {
1286 TypedTrackingMDRef<DICompositeType> N(T);
1287 if (Elements)
1288 N->replaceElements(Elements);
1289 if (TParams)
1290 N->replaceTemplateParams(TemplateParams: DITemplateParameterArray(TParams));
1291 T = N.get();
1292 }
1293
1294 // If T isn't resolved, there's no problem.
1295 if (!T->isResolved())
1296 return;
1297
1298 // If T is resolved, it may be due to a self-reference cycle. Track the
1299 // arrays explicitly if they're unresolved, or else the cycles will be
1300 // orphaned.
1301 if (Elements)
1302 trackIfUnresolved(N: Elements.get());
1303 if (TParams)
1304 trackIfUnresolved(N: TParams.get());
1305}
1306