blob: 741a3a27f1367546dc3dd844edb59bc5b2963707 [file] [log] [blame]
Devang Patel57c5a202010-11-04 15:01:38 +00001//===--- DIBuilder.cpp - Debug Information Builder ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the DIBuilder.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth12664a02014-03-06 00:22:06 +000014#include "llvm/IR/DIBuilder.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/STLExtras.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/Constants.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000017#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/IntrinsicInst.h"
19#include "llvm/IR/Module.h"
Eric Christopher34164192012-04-03 00:43:49 +000020#include "llvm/Support/Debug.h"
Devang Patel57c5a202010-11-04 15:01:38 +000021#include "llvm/Support/Dwarf.h"
Amjad Aboud62f6f5c2016-03-13 11:11:39 +000022#include "LLVMContextImpl.h"
Devang Patel57c5a202010-11-04 15:01:38 +000023
24using namespace llvm;
25using namespace llvm::dwarf;
26
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000027DIBuilder::DIBuilder(Module &m, bool AllowUnresolvedNodes)
Adrian Prantl18c073a2015-07-02 22:32:52 +000028 : M(m), VMContext(M.getContext()), CUNode(nullptr),
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000029 DeclareFn(nullptr), ValueFn(nullptr),
30 AllowUnresolvedNodes(AllowUnresolvedNodes) {}
31
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000032void DIBuilder::trackIfUnresolved(MDNode *N) {
Duncan P. N. Exon Smith9b1c6d32015-01-19 19:09:14 +000033 if (!N)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000034 return;
Duncan P. N. Exon Smith9b1c6d32015-01-19 19:09:14 +000035 if (N->isResolved())
36 return;
37
38 assert(AllowUnresolvedNodes && "Cannot handle unresolved nodes");
39 UnresolvedNodes.emplace_back(N);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000040}
Devang Patel57c5a202010-11-04 15:01:38 +000041
Devang Patel2b8acaf2011-08-15 23:00:00 +000042void DIBuilder::finalize() {
Adrian Prantl0e224a62015-07-06 16:22:12 +000043 if (!CUNode) {
44 assert(!AllowUnresolvedNodes &&
45 "creating type nodes without a CU is not supported");
46 return;
Devang Patel59e27c52011-08-19 23:28:12 +000047 }
Devang Pateleb1bb4e2011-08-16 22:09:43 +000048
Adrian Prantl0e224a62015-07-06 16:22:12 +000049 CUNode->replaceEnumTypes(MDTuple::get(VMContext, AllEnumTypes));
50
51 SmallVector<Metadata *, 16> RetainValues;
52 // Declarations and definitions of the same type may be retained. Some
53 // clients RAUW these pairs, leaving duplicates in the retained types
54 // list. Use a set to remove the duplicates while we transform the
55 // TrackingVHs back into Values.
56 SmallPtrSet<Metadata *, 16> RetainSet;
57 for (unsigned I = 0, E = AllRetainTypes.size(); I < E; I++)
58 if (RetainSet.insert(AllRetainTypes[I]).second)
59 RetainValues.push_back(AllRetainTypes[I]);
Adrian Prantl4276d4a2015-07-06 16:36:02 +000060
61 if (!RetainValues.empty())
62 CUNode->replaceRetainedTypes(MDTuple::get(VMContext, RetainValues));
Adrian Prantl0e224a62015-07-06 16:22:12 +000063
64 DISubprogramArray SPs = MDTuple::get(VMContext, AllSubprograms);
Adrian Prantl75819ae2016-04-15 15:57:41 +000065 auto resolveVariables = [&](DISubprogram *SP) {
Duncan P. N. Exon Smitha2495d92016-04-20 20:03:59 +000066 MDTuple *Temp = SP->getVariables().get();
67 if (!Temp)
68 return;
69
70 SmallVector<Metadata *, 4> Variables;
71
72 auto PV = PreservedVariables.find(SP);
73 if (PV != PreservedVariables.end())
74 Variables.append(PV->second.begin(), PV->second.end());
75
76 DINodeArray AV = getOrCreateArray(Variables);
77 TempMDTuple(Temp)->replaceAllUsesWith(AV.get());
Adrian Prantl75819ae2016-04-15 15:57:41 +000078 };
79 for (auto *SP : SPs)
80 resolveVariables(SP);
81 for (auto *N : RetainValues)
82 if (auto *SP = dyn_cast<DISubprogram>(N))
83 resolveVariables(SP);
Adrian Prantl0e224a62015-07-06 16:22:12 +000084
Adrian Prantl4276d4a2015-07-06 16:36:02 +000085 if (!AllGVs.empty())
86 CUNode->replaceGlobalVariables(MDTuple::get(VMContext, AllGVs));
Adrian Prantl0e224a62015-07-06 16:22:12 +000087
Adrian Prantl4276d4a2015-07-06 16:36:02 +000088 if (!AllImportedModules.empty())
89 CUNode->replaceImportedEntities(MDTuple::get(
90 VMContext, SmallVector<Metadata *, 16>(AllImportedModules.begin(),
91 AllImportedModules.end())));
Adrian Prantl0e224a62015-07-06 16:22:12 +000092
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000093 // Now that all temp nodes have been replaced or deleted, resolve remaining
94 // cycles.
95 for (const auto &N : UnresolvedNodes)
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +000096 if (N && !N->isResolved())
97 N->resolveCycles();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000098 UnresolvedNodes.clear();
99
100 // Can't handle unresolved nodes anymore.
101 AllowUnresolvedNodes = false;
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000102}
103
Duncan P. N. Exon Smith379e3752014-10-01 21:32:15 +0000104/// If N is compile unit return NULL otherwise return N.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000105static DIScope *getNonCompileUnitScope(DIScope *N) {
106 if (!N || isa<DICompileUnit>(N))
Craig Topperc6207612014-04-09 06:08:46 +0000107 return nullptr;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000108 return cast<DIScope>(N);
Devang Patel2b8acaf2011-08-15 23:00:00 +0000109}
110
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000111DICompileUnit *DIBuilder::createCompileUnit(
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000112 unsigned Lang, StringRef Filename, StringRef Directory, StringRef Producer,
113 bool isOptimized, StringRef Flags, unsigned RunTimeVer, StringRef SplitName,
David Blaikiea01f2952016-08-24 18:29:49 +0000114 DICompileUnit::DebugEmissionKind Kind, uint64_t DWOId,
115 bool SplitDebugInlining) {
Eric Christopher75d49db2014-02-27 01:24:56 +0000116
Bruce Mitchener7e575ed2015-02-07 06:35:30 +0000117 assert(((Lang <= dwarf::DW_LANG_Fortran08 && Lang >= dwarf::DW_LANG_C89) ||
Chandler Carruth4c0ee742012-01-10 18:18:52 +0000118 (Lang <= dwarf::DW_LANG_hi_user && Lang >= dwarf::DW_LANG_lo_user)) &&
119 "Invalid Language tag");
120 assert(!Filename.empty() &&
121 "Unable to create compile unit without filename");
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000122
Adrian Prantl18c073a2015-07-02 22:32:52 +0000123 assert(!CUNode && "Can only make one compile unit per DIBuilder instance");
124 CUNode = DICompileUnit::getDistinct(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000125 VMContext, Lang, DIFile::get(VMContext, Filename, Directory), Producer,
Adrian Prantl75819ae2016-04-15 15:57:41 +0000126 isOptimized, Flags, RunTimeVer, SplitName, Kind, nullptr, nullptr,
David Blaikiea01f2952016-08-24 18:29:49 +0000127 nullptr, nullptr, nullptr, DWOId, SplitDebugInlining);
Devang Patel09fa69e2011-05-03 16:18:28 +0000128
129 // Create a named metadata so that it is easier to find cu in a module.
Adrian Prantl5992a722016-04-08 22:43:03 +0000130 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
131 NMD->addOperand(CUNode);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000132 trackIfUnresolved(CUNode);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000133 return CUNode;
Devang Patel57c5a202010-11-04 15:01:38 +0000134}
135
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000136static DIImportedEntity *
137createImportedModule(LLVMContext &C, dwarf::Tag Tag, DIScope *Context,
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000138 Metadata *NS, unsigned Line, StringRef Name,
139 SmallVectorImpl<TrackingMDNodeRef> &AllImportedModules) {
Amjad Aboud62f6f5c2016-03-13 11:11:39 +0000140 unsigned EntitiesCount = C.pImpl->DIImportedEntitys.size();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000141 auto *M = DIImportedEntity::get(C, Tag, Context, DINodeRef(NS), Line, Name);
Amjad Aboud62f6f5c2016-03-13 11:11:39 +0000142 if (EntitiesCount < C.pImpl->DIImportedEntitys.size())
143 // A new Imported Entity was just added to the context.
144 // Add it to the Imported Modules list.
145 AllImportedModules.emplace_back(M);
David Blaikie1fd43652013-05-07 21:35:53 +0000146 return M;
147}
148
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000149DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context,
150 DINamespace *NS,
151 unsigned Line) {
David Blaikie2a40c142014-04-06 06:29:01 +0000152 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
153 Context, NS, Line, StringRef(), AllImportedModules);
David Blaikiee63d5d12013-05-20 22:50:35 +0000154}
155
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000156DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context,
157 DIImportedEntity *NS,
158 unsigned Line) {
David Blaikie2a40c142014-04-06 06:29:01 +0000159 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
160 Context, NS, Line, StringRef(), AllImportedModules);
David Blaikiee63d5d12013-05-20 22:50:35 +0000161}
162
Adrian Prantlab1243f2015-06-29 23:03:47 +0000163DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context, DIModule *M,
164 unsigned Line) {
165 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
166 Context, M, Line, StringRef(), AllImportedModules);
167}
168
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000169DIImportedEntity *DIBuilder::createImportedDeclaration(DIScope *Context,
170 DINode *Decl,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000171 unsigned Line,
172 StringRef Name) {
Frederic Riss4aa51ae2014-11-06 17:46:55 +0000173 // Make sure to use the unique identifier based metadata reference for
174 // types that have one.
David Blaikie2a40c142014-04-06 06:29:01 +0000175 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_declaration,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000176 Context, Decl, Line, Name, AllImportedModules);
David Blaikief55abea2013-04-22 06:12:31 +0000177}
178
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000179DIFile *DIBuilder::createFile(StringRef Filename, StringRef Directory) {
180 return DIFile::get(VMContext, Filename, Directory);
Devang Patel57c5a202010-11-04 15:01:38 +0000181}
182
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000183DIEnumerator *DIBuilder::createEnumerator(StringRef Name, int64_t Val) {
Devang Patel1ad1abe2011-09-12 18:26:08 +0000184 assert(!Name.empty() && "Unable to create enumerator without name");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000185 return DIEnumerator::get(VMContext, Val, Name);
Devang Patel57c5a202010-11-04 15:01:38 +0000186}
187
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000188DIBasicType *DIBuilder::createUnspecifiedType(StringRef Name) {
Devang Patel04d6d472011-09-14 23:13:28 +0000189 assert(!Name.empty() && "Unable to create type without name");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000190 return DIBasicType::get(VMContext, dwarf::DW_TAG_unspecified_type, Name);
Devang Patel04d6d472011-09-14 23:13:28 +0000191}
192
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000193DIBasicType *DIBuilder::createNullPtrType() {
Peter Collingbournea4a47cb2013-06-27 22:50:59 +0000194 return createUnspecifiedType("decltype(nullptr)");
195}
196
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000197DIBasicType *DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000198 unsigned Encoding) {
Devang Patel1ad1abe2011-09-12 18:26:08 +0000199 assert(!Name.empty() && "Unable to create type without name");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000200 return DIBasicType::get(VMContext, dwarf::DW_TAG_base_type, Name, SizeInBits,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000201 0, Encoding);
Devang Patel57c5a202010-11-04 15:01:38 +0000202}
203
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000204DIDerivedType *DIBuilder::createQualifiedType(unsigned Tag, DIType *FromTy) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000205 return DIDerivedType::get(VMContext, Tag, "", nullptr, 0, nullptr, FromTy, 0,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000206 0, 0, DINode::FlagZero);
Devang Patel57c5a202010-11-04 15:01:38 +0000207}
208
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000209DIDerivedType *DIBuilder::createPointerType(DIType *PointeeTy,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000210 uint64_t SizeInBits,
Victor Leschuk197aa312016-10-18 14:31:22 +0000211 uint32_t AlignInBits,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000212 StringRef Name) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000213 // FIXME: Why is there a name here?
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000214 return DIDerivedType::get(VMContext, dwarf::DW_TAG_pointer_type, Name,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000215 nullptr, 0, nullptr, PointeeTy, SizeInBits,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000216 AlignInBits, 0, DINode::FlagZero);
Devang Patel57c5a202010-11-04 15:01:38 +0000217}
218
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000219DIDerivedType *DIBuilder::createMemberPointerType(DIType *PointeeTy,
220 DIType *Base,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000221 uint64_t SizeInBits,
Victor Leschuk197aa312016-10-18 14:31:22 +0000222 uint32_t AlignInBits,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000223 DINode::DIFlags Flags) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000224 return DIDerivedType::get(VMContext, dwarf::DW_TAG_ptr_to_member_type, "",
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000225 nullptr, 0, nullptr, PointeeTy, SizeInBits,
Reid Kleckner604105b2016-06-17 21:31:33 +0000226 AlignInBits, 0, Flags, Base);
David Blaikie5d3249b2013-01-07 05:51:15 +0000227}
228
Keno Fischerb011c632015-11-16 07:57:32 +0000229DIDerivedType *DIBuilder::createReferenceType(unsigned Tag, DIType *RTy,
230 uint64_t SizeInBits,
Victor Leschuk197aa312016-10-18 14:31:22 +0000231 uint32_t AlignInBits) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000232 assert(RTy && "Unable to create reference type");
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000233 return DIDerivedType::get(VMContext, Tag, "", nullptr, 0, nullptr, RTy,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000234 SizeInBits, AlignInBits, 0, DINode::FlagZero);
Devang Patel57c5a202010-11-04 15:01:38 +0000235}
236
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000237DIDerivedType *DIBuilder::createTypedef(DIType *Ty, StringRef Name,
238 DIFile *File, unsigned LineNo,
239 DIScope *Context) {
240 return DIDerivedType::get(VMContext, dwarf::DW_TAG_typedef, Name, File,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000241 LineNo, getNonCompileUnitScope(Context), Ty, 0, 0,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000242 0, DINode::FlagZero);
Devang Patel57c5a202010-11-04 15:01:38 +0000243}
244
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000245DIDerivedType *DIBuilder::createFriend(DIType *Ty, DIType *FriendTy) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000246 assert(Ty && "Invalid type!");
247 assert(FriendTy && "Invalid friend type!");
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000248 return DIDerivedType::get(VMContext, dwarf::DW_TAG_friend, "", nullptr, 0, Ty,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000249 FriendTy, 0, 0, 0, DINode::FlagZero);
Devang Patel57c5a202010-11-04 15:01:38 +0000250}
251
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000252DIDerivedType *DIBuilder::createInheritance(DIType *Ty, DIType *BaseTy,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000253 uint64_t BaseOffset,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000254 DINode::DIFlags Flags) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000255 assert(Ty && "Unable to create inheritance");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000256 return DIDerivedType::get(VMContext, dwarf::DW_TAG_inheritance, "", nullptr,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000257 0, Ty, BaseTy, 0, 0, BaseOffset, Flags);
Devang Patel57c5a202010-11-04 15:01:38 +0000258}
259
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000260DIDerivedType *DIBuilder::createMemberType(DIScope *Scope, StringRef Name,
261 DIFile *File, unsigned LineNumber,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000262 uint64_t SizeInBits,
Victor Leschuk197aa312016-10-18 14:31:22 +0000263 uint32_t AlignInBits,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000264 uint64_t OffsetInBits,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000265 DINode::DIFlags Flags, DIType *Ty) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000266 return DIDerivedType::get(VMContext, dwarf::DW_TAG_member, Name, File,
267 LineNumber, getNonCompileUnitScope(Scope), Ty,
268 SizeInBits, AlignInBits, OffsetInBits, Flags);
Devang Patel57c5a202010-11-04 15:01:38 +0000269}
270
Duncan P. N. Exon Smith3cd2cab2015-03-27 00:34:10 +0000271static ConstantAsMetadata *getConstantOrNull(Constant *C) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000272 if (C)
273 return ConstantAsMetadata::get(C);
274 return nullptr;
275}
276
David Majnemer9319cbc2016-06-30 03:00:20 +0000277DIDerivedType *DIBuilder::createBitFieldMemberType(
278 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000279 uint64_t SizeInBits, uint64_t OffsetInBits, uint64_t StorageOffsetInBits,
280 DINode::DIFlags Flags, DIType *Ty) {
David Majnemer9319cbc2016-06-30 03:00:20 +0000281 Flags |= DINode::FlagBitField;
282 return DIDerivedType::get(
283 VMContext, dwarf::DW_TAG_member, Name, File, LineNumber,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000284 getNonCompileUnitScope(Scope), Ty, SizeInBits, /* AlignInBits */ 0,
285 OffsetInBits, Flags,
286 ConstantAsMetadata::get(ConstantInt::get(IntegerType::get(VMContext, 64),
287 StorageOffsetInBits)));
David Majnemer9319cbc2016-06-30 03:00:20 +0000288}
289
Leny Kholodov40c62352016-09-06 17:03:02 +0000290DIDerivedType *
291DIBuilder::createStaticMemberType(DIScope *Scope, StringRef Name, DIFile *File,
292 unsigned LineNumber, DIType *Ty,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000293 DINode::DIFlags Flags, llvm::Constant *Val,
294 uint32_t AlignInBits) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000295 Flags |= DINode::FlagStaticMember;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000296 return DIDerivedType::get(VMContext, dwarf::DW_TAG_member, Name, File,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000297 LineNumber, getNonCompileUnitScope(Scope), Ty, 0,
298 AlignInBits, 0, Flags, getConstantOrNull(Val));
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000299}
300
Leny Kholodov40c62352016-09-06 17:03:02 +0000301DIDerivedType *
302DIBuilder::createObjCIVar(StringRef Name, DIFile *File, unsigned LineNumber,
Victor Leschuk197aa312016-10-18 14:31:22 +0000303 uint64_t SizeInBits, uint32_t AlignInBits,
Leny Kholodov40c62352016-09-06 17:03:02 +0000304 uint64_t OffsetInBits, DINode::DIFlags Flags,
305 DIType *Ty, MDNode *PropertyNode) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000306 return DIDerivedType::get(VMContext, dwarf::DW_TAG_member, Name, File,
307 LineNumber, getNonCompileUnitScope(File), Ty,
308 SizeInBits, AlignInBits, OffsetInBits, Flags,
309 PropertyNode);
Devang Patel44882172012-02-06 17:49:43 +0000310}
311
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000312DIObjCProperty *
313DIBuilder::createObjCProperty(StringRef Name, DIFile *File, unsigned LineNumber,
Eric Christopher98f9c232013-10-15 23:31:31 +0000314 StringRef GetterName, StringRef SetterName,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000315 unsigned PropertyAttributes, DIType *Ty) {
316 return DIObjCProperty::get(VMContext, Name, File, LineNumber, GetterName,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000317 SetterName, PropertyAttributes, Ty);
Devang Patelcc481592012-02-04 00:59:25 +0000318}
319
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000320DITemplateTypeParameter *
321DIBuilder::createTemplateTypeParameter(DIScope *Context, StringRef Name,
322 DIType *Ty) {
323 assert((!Context || isa<DICompileUnit>(Context)) && "Expected compile unit");
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000324 return DITemplateTypeParameter::get(VMContext, Name, Ty);
Devang Patel3a9e65e2011-02-02 21:38:25 +0000325}
326
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000327static DITemplateValueParameter *
Duncan P. N. Exon Smithb4aa16f2015-02-13 03:35:29 +0000328createTemplateValueParameterHelper(LLVMContext &VMContext, unsigned Tag,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000329 DIScope *Context, StringRef Name, DIType *Ty,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000330 Metadata *MD) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000331 assert((!Context || isa<DICompileUnit>(Context)) && "Expected compile unit");
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000332 return DITemplateValueParameter::get(VMContext, Tag, Name, Ty, MD);
Devang Patelbe933b42011-02-02 22:35:53 +0000333}
334
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000335DITemplateValueParameter *
336DIBuilder::createTemplateValueParameter(DIScope *Context, StringRef Name,
337 DIType *Ty, Constant *Val) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000338 return createTemplateValueParameterHelper(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000339 VMContext, dwarf::DW_TAG_template_value_parameter, Context, Name, Ty,
Duncan P. N. Exon Smithb4aa16f2015-02-13 03:35:29 +0000340 getConstantOrNull(Val));
David Blaikie2b380232013-06-22 18:59:11 +0000341}
342
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000343DITemplateValueParameter *
344DIBuilder::createTemplateTemplateParameter(DIScope *Context, StringRef Name,
345 DIType *Ty, StringRef Val) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000346 return createTemplateValueParameterHelper(
347 VMContext, dwarf::DW_TAG_GNU_template_template_param, Context, Name, Ty,
Duncan P. N. Exon Smithb4aa16f2015-02-13 03:35:29 +0000348 MDString::get(VMContext, Val));
David Blaikie2b380232013-06-22 18:59:11 +0000349}
350
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000351DITemplateValueParameter *
352DIBuilder::createTemplateParameterPack(DIScope *Context, StringRef Name,
353 DIType *Ty, DINodeArray Val) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000354 return createTemplateValueParameterHelper(
355 VMContext, dwarf::DW_TAG_GNU_template_parameter_pack, Context, Name, Ty,
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +0000356 Val.get());
David Blaikie2b380232013-06-22 18:59:11 +0000357}
358
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000359DICompositeType *DIBuilder::createClassType(
360 DIScope *Context, StringRef Name, DIFile *File, unsigned LineNumber,
Victor Leschuk197aa312016-10-18 14:31:22 +0000361 uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000362 DINode::DIFlags Flags, DIType *DerivedFrom, DINodeArray Elements,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000363 DIType *VTableHolder, MDNode *TemplateParams, StringRef UniqueIdentifier) {
364 assert((!Context || isa<DIScope>(Context)) &&
David Blaikie085abe32013-03-11 23:21:19 +0000365 "createClassType should be called with a valid Context");
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000366
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000367 auto *R = DICompositeType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000368 VMContext, dwarf::DW_TAG_structure_type, Name, File, LineNumber,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000369 getNonCompileUnitScope(Context), DerivedFrom, SizeInBits, AlignInBits,
370 OffsetInBits, Flags, Elements, 0, VTableHolder,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000371 cast_or_null<MDTuple>(TemplateParams), UniqueIdentifier);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000372 trackIfUnresolved(R);
David Blaikie085abe32013-03-11 23:21:19 +0000373 return R;
Eric Christopher17426692012-07-06 02:35:57 +0000374}
375
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000376DICompositeType *DIBuilder::createStructType(
377 DIScope *Context, StringRef Name, DIFile *File, unsigned LineNumber,
Victor Leschuk197aa312016-10-18 14:31:22 +0000378 uint64_t SizeInBits, uint32_t AlignInBits, DINode::DIFlags Flags,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000379 DIType *DerivedFrom, DINodeArray Elements, unsigned RunTimeLang,
380 DIType *VTableHolder, StringRef UniqueIdentifier) {
381 auto *R = DICompositeType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000382 VMContext, dwarf::DW_TAG_structure_type, Name, File, LineNumber,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000383 getNonCompileUnitScope(Context), DerivedFrom, SizeInBits, AlignInBits, 0,
384 Flags, Elements, RunTimeLang, VTableHolder, nullptr, UniqueIdentifier);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000385 trackIfUnresolved(R);
David Blaikie085abe32013-03-11 23:21:19 +0000386 return R;
Devang Patel746660f2010-12-07 23:25:47 +0000387}
388
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000389DICompositeType *DIBuilder::createUnionType(
390 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
Victor Leschuk197aa312016-10-18 14:31:22 +0000391 uint64_t SizeInBits, uint32_t AlignInBits, DINode::DIFlags Flags,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000392 DINodeArray Elements, unsigned RunTimeLang, StringRef UniqueIdentifier) {
393 auto *R = DICompositeType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000394 VMContext, dwarf::DW_TAG_union_type, Name, File, LineNumber,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000395 getNonCompileUnitScope(Scope), nullptr, SizeInBits, AlignInBits, 0, Flags,
396 Elements, RunTimeLang, nullptr, nullptr, UniqueIdentifier);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000397 trackIfUnresolved(R);
Manman Ren0b410402013-08-29 23:17:54 +0000398 return R;
Devang Patel89ea4f22010-12-08 01:50:15 +0000399}
400
Eric Christopherbdafb3c2015-10-15 06:56:10 +0000401DISubroutineType *DIBuilder::createSubroutineType(DITypeRefArray ParameterTypes,
Leny Kholodov40c62352016-09-06 17:03:02 +0000402 DINode::DIFlags Flags,
403 unsigned CC) {
Reid Klecknerde3d8b52016-06-08 20:34:29 +0000404 return DISubroutineType::get(VMContext, Flags, CC, ParameterTypes);
Devang Patel89ea4f22010-12-08 01:50:15 +0000405}
406
Adrian Prantlee5feaf2015-07-15 17:01:41 +0000407DICompositeType *DIBuilder::createExternalTypeRef(unsigned Tag, DIFile *File,
408 StringRef UniqueIdentifier) {
409 assert(!UniqueIdentifier.empty() && "external type ref without uid");
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000410 return DICompositeType::get(VMContext, Tag, "", nullptr, 0, nullptr, nullptr,
411 0, 0, 0, DINode::FlagExternalTypeRef, nullptr, 0,
412 nullptr, nullptr, UniqueIdentifier);
Adrian Prantlee5feaf2015-07-15 17:01:41 +0000413}
414
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000415DICompositeType *DIBuilder::createEnumerationType(
416 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
Victor Leschuk197aa312016-10-18 14:31:22 +0000417 uint64_t SizeInBits, uint32_t AlignInBits, DINodeArray Elements,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000418 DIType *UnderlyingType, StringRef UniqueIdentifier) {
419 auto *CTy = DICompositeType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000420 VMContext, dwarf::DW_TAG_enumeration_type, Name, File, LineNumber,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000421 getNonCompileUnitScope(Scope), UnderlyingType, SizeInBits, AlignInBits, 0,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000422 DINode::FlagZero, Elements, 0, nullptr, nullptr, UniqueIdentifier);
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000423 AllEnumTypes.push_back(CTy);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000424 trackIfUnresolved(CTy);
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000425 return CTy;
Devang Patel89ea4f22010-12-08 01:50:15 +0000426}
427
Victor Leschuk197aa312016-10-18 14:31:22 +0000428DICompositeType *DIBuilder::createArrayType(uint64_t Size,
429 uint32_t AlignInBits, DIType *Ty,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000430 DINodeArray Subscripts) {
431 auto *R = DICompositeType::get(VMContext, dwarf::DW_TAG_array_type, "",
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000432 nullptr, 0, nullptr, Ty, Size, AlignInBits, 0,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000433 DINode::FlagZero, Subscripts, 0, nullptr);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000434 trackIfUnresolved(R);
435 return R;
Devang Patel89ea4f22010-12-08 01:50:15 +0000436}
437
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000438DICompositeType *DIBuilder::createVectorType(uint64_t Size,
Victor Leschuk197aa312016-10-18 14:31:22 +0000439 uint32_t AlignInBits, DIType *Ty,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000440 DINodeArray Subscripts) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000441 auto *R = DICompositeType::get(VMContext, dwarf::DW_TAG_array_type, "",
442 nullptr, 0, nullptr, Ty, Size, AlignInBits, 0,
443 DINode::FlagVector, Subscripts, 0, nullptr);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000444 trackIfUnresolved(R);
445 return R;
Devang Patel89ea4f22010-12-08 01:50:15 +0000446}
Devang Patel746660f2010-12-07 23:25:47 +0000447
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000448static DIType *createTypeWithFlags(LLVMContext &Context, DIType *Ty,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000449 DINode::DIFlags FlagsToSet) {
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000450 auto NewTy = Ty->clone();
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000451 NewTy->setFlags(NewTy->getFlags() | FlagsToSet);
452 return MDNode::replaceWithUniqued(std::move(NewTy));
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000453}
454
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000455DIType *DIBuilder::createArtificialType(DIType *Ty) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000456 // FIXME: Restrict this to the nodes where it's valid.
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000457 if (Ty->isArtificial())
Devang Patel57c5a202010-11-04 15:01:38 +0000458 return Ty;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000459 return createTypeWithFlags(VMContext, Ty, DINode::FlagArtificial);
Devang Patel57c5a202010-11-04 15:01:38 +0000460}
Devang Patel746660f2010-12-07 23:25:47 +0000461
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000462DIType *DIBuilder::createObjectPointerType(DIType *Ty) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000463 // FIXME: Restrict this to the nodes where it's valid.
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000464 if (Ty->isObjectPointer())
Eric Christophere3417762012-09-12 23:36:19 +0000465 return Ty;
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000466 DINode::DIFlags Flags = DINode::FlagObjectPointer | DINode::FlagArtificial;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000467 return createTypeWithFlags(VMContext, Ty, Flags);
Eric Christophere3417762012-09-12 23:36:19 +0000468}
469
Adrian Prantl75819ae2016-04-15 15:57:41 +0000470void DIBuilder::retainType(DIScope *T) {
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000471 assert(T && "Expected non-null type");
Adrian Prantl75819ae2016-04-15 15:57:41 +0000472 assert((isa<DIType>(T) || (isa<DISubprogram>(T) &&
473 cast<DISubprogram>(T)->isDefinition() == false)) &&
474 "Expected type or subprogram declaration");
Duncan P. N. Exon Smithd9ccfb92015-03-27 23:00:49 +0000475 AllRetainTypes.emplace_back(T);
476}
Devang Patel89ea4f22010-12-08 01:50:15 +0000477
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000478DIBasicType *DIBuilder::createUnspecifiedParameter() { return nullptr; }
Devang Patel89ea4f22010-12-08 01:50:15 +0000479
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000480DICompositeType *
481DIBuilder::createForwardDecl(unsigned Tag, StringRef Name, DIScope *Scope,
482 DIFile *F, unsigned Line, unsigned RuntimeLang,
Victor Leschuk197aa312016-10-18 14:31:22 +0000483 uint64_t SizeInBits, uint32_t AlignInBits,
Eric Christopher98f9c232013-10-15 23:31:31 +0000484 StringRef UniqueIdentifier) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000485 // FIXME: Define in terms of createReplaceableForwardDecl() by calling
486 // replaceWithUniqued().
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000487 auto *RetTy = DICompositeType::get(
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000488 VMContext, Tag, Name, F, Line, getNonCompileUnitScope(Scope), nullptr,
489 SizeInBits, AlignInBits, 0, DINode::FlagFwdDecl, nullptr, RuntimeLang,
490 nullptr, nullptr, UniqueIdentifier);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000491 trackIfUnresolved(RetTy);
David Blaikied3f094a2014-05-06 03:41:57 +0000492 return RetTy;
493}
494
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000495DICompositeType *DIBuilder::createReplaceableCompositeType(
496 unsigned Tag, StringRef Name, DIScope *Scope, DIFile *F, unsigned Line,
Victor Leschuk197aa312016-10-18 14:31:22 +0000497 unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000498 DINode::DIFlags Flags, StringRef UniqueIdentifier) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000499 auto *RetTy =
500 DICompositeType::getTemporary(
501 VMContext, Tag, Name, F, Line, getNonCompileUnitScope(Scope), nullptr,
502 SizeInBits, AlignInBits, 0, Flags, nullptr, RuntimeLang, nullptr,
503 nullptr, UniqueIdentifier)
504 .release();
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000505 trackIfUnresolved(RetTy);
Manman Rend0e67aa2013-07-02 18:37:35 +0000506 return RetTy;
Eric Christopherae56eec2012-02-08 00:22:26 +0000507}
508
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000509DINodeArray DIBuilder::getOrCreateArray(ArrayRef<Metadata *> Elements) {
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000510 return MDTuple::get(VMContext, Elements);
Devang Patel746660f2010-12-07 23:25:47 +0000511}
512
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000513DITypeRefArray DIBuilder::getOrCreateTypeArray(ArrayRef<Metadata *> Elements) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000514 SmallVector<llvm::Metadata *, 16> Elts;
Manman Ren1a125c92014-07-28 19:33:20 +0000515 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
516 if (Elements[i] && isa<MDNode>(Elements[i]))
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000517 Elts.push_back(cast<DIType>(Elements[i]));
Manman Ren1a125c92014-07-28 19:33:20 +0000518 else
519 Elts.push_back(Elements[i]);
520 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000521 return DITypeRefArray(MDNode::get(VMContext, Elts));
Manman Ren1a125c92014-07-28 19:33:20 +0000522}
523
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000524DISubrange *DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
525 return DISubrange::get(VMContext, Count, Lo);
Devang Patel89ea4f22010-12-08 01:50:15 +0000526}
527
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000528static void checkGlobalVariableScope(DIScope *Context) {
Duncan P. N. Exon Smith430220f2015-04-06 23:34:41 +0000529#ifndef NDEBUG
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000530 if (auto *CT =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000531 dyn_cast_or_null<DICompositeType>(getNonCompileUnitScope(Context)))
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000532 assert(CT->getIdentifier().empty() &&
Manman Renbfd2b8292014-11-21 19:47:48 +0000533 "Context of a global variable should not be a type with identifier");
Duncan P. N. Exon Smith430220f2015-04-06 23:34:41 +0000534#endif
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000535}
536
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000537DIGlobalVariable *DIBuilder::createGlobalVariable(
538 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000539 unsigned LineNumber, DIType *Ty, bool isLocalToUnit,
540 DIExpression *Expr, MDNode *Decl, uint32_t AlignInBits) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000541 checkGlobalVariableScope(Context);
542
Duncan P. N. Exon Smithc5225df2016-04-23 22:29:09 +0000543 auto *N = DIGlobalVariable::getDistinct(
544 VMContext, cast_or_null<DIScope>(Context), Name, LinkageName, F,
Peter Collingbourned4135bb2016-09-13 01:12:59 +0000545 LineNumber, Ty, isLocalToUnit, true, Expr,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000546 cast_or_null<DIDerivedType>(Decl), AlignInBits);
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000547 AllGVs.push_back(N);
548 return N;
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000549}
550
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000551DIGlobalVariable *DIBuilder::createTempGlobalVariableFwdDecl(
552 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000553 unsigned LineNumber, DIType *Ty, bool isLocalToUnit,
554 DIExpression *Expr, MDNode *Decl, uint32_t AlignInBits) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000555 checkGlobalVariableScope(Context);
556
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000557 return DIGlobalVariable::getTemporary(
558 VMContext, cast_or_null<DIScope>(Context), Name, LinkageName, F,
Peter Collingbourned4135bb2016-09-13 01:12:59 +0000559 LineNumber, Ty, isLocalToUnit, false, Expr,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000560 cast_or_null<DIDerivedType>(Decl), AlignInBits)
Duncan P. N. Exon Smithb273d062015-04-16 01:37:00 +0000561 .release();
Devang Patel746660f2010-12-07 23:25:47 +0000562}
563
Duncan P. N. Exon Smith1e40dc42015-07-31 17:55:53 +0000564static DILocalVariable *createLocalVariable(
565 LLVMContext &VMContext,
Duncan P. N. Exon Smith3c406c22016-04-20 20:14:09 +0000566 DenseMap<MDNode *, SmallVector<TrackingMDNodeRef, 1>> &PreservedVariables,
Duncan P. N. Exon Smith1e40dc42015-07-31 17:55:53 +0000567 DIScope *Scope, StringRef Name, unsigned ArgNo, DIFile *File,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000568 unsigned LineNo, DIType *Ty, bool AlwaysPreserve, DINode::DIFlags Flags,
569 uint32_t AlignInBits) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000570 // FIXME: Why getNonCompileUnitScope()?
571 // FIXME: Why is "!Context" okay here?
Adrian Prantl12d52842015-07-10 23:26:02 +0000572 // FIXME: Why doesn't this check for a subprogram or lexical block (AFAICT
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000573 // the only valid scopes)?
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000574 DIScope *Context = getNonCompileUnitScope(Scope);
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000575
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +0000576 auto *Node =
577 DILocalVariable::get(VMContext, cast_or_null<DILocalScope>(Context), Name,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000578 File, LineNo, Ty, ArgNo, Flags, AlignInBits);
Devang Patel63f83cd2010-12-07 23:58:00 +0000579 if (AlwaysPreserve) {
Adrian Prantl12d52842015-07-10 23:26:02 +0000580 // The optimizer may remove local variables. If there is an interest
Devang Patel63f83cd2010-12-07 23:58:00 +0000581 // to preserve variable info in such situation then stash it in a
582 // named mdnode.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000583 DISubprogram *Fn = getDISubprogram(Scope);
Duncan P. N. Exon Smith3bfffde2014-10-15 16:11:41 +0000584 assert(Fn && "Missing subprogram for local variable");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000585 PreservedVariables[Fn].emplace_back(Node);
Devang Patel63f83cd2010-12-07 23:58:00 +0000586 }
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000587 return Node;
Devang Patel63f83cd2010-12-07 23:58:00 +0000588}
589
Duncan P. N. Exon Smith1e40dc42015-07-31 17:55:53 +0000590DILocalVariable *DIBuilder::createAutoVariable(DIScope *Scope, StringRef Name,
591 DIFile *File, unsigned LineNo,
592 DIType *Ty, bool AlwaysPreserve,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000593 DINode::DIFlags Flags,
594 uint32_t AlignInBits) {
Duncan P. N. Exon Smith1e40dc42015-07-31 17:55:53 +0000595 return createLocalVariable(VMContext, PreservedVariables, Scope, Name,
596 /* ArgNo */ 0, File, LineNo, Ty, AlwaysPreserve,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000597 Flags, AlignInBits);
Duncan P. N. Exon Smith1e40dc42015-07-31 17:55:53 +0000598}
599
600DILocalVariable *DIBuilder::createParameterVariable(
601 DIScope *Scope, StringRef Name, unsigned ArgNo, DIFile *File,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000602 unsigned LineNo, DIType *Ty, bool AlwaysPreserve, DINode::DIFlags Flags) {
Duncan P. N. Exon Smith1e40dc42015-07-31 17:55:53 +0000603 assert(ArgNo && "Expected non-zero argument number for parameter");
604 return createLocalVariable(VMContext, PreservedVariables, Scope, Name, ArgNo,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000605 File, LineNo, Ty, AlwaysPreserve, Flags,
606 /* AlignInBits */0);
Duncan P. N. Exon Smith1e40dc42015-07-31 17:55:53 +0000607}
608
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000609DIExpression *DIBuilder::createExpression(ArrayRef<uint64_t> Addr) {
610 return DIExpression::get(VMContext, Addr);
Devang Patel746660f2010-12-07 23:25:47 +0000611}
612
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000613DIExpression *DIBuilder::createExpression(ArrayRef<int64_t> Signed) {
Duncan P. N. Exon Smithbd75ad42015-02-09 22:13:27 +0000614 // TODO: Remove the callers of this signed version and delete.
615 SmallVector<uint64_t, 8> Addr(Signed.begin(), Signed.end());
616 return createExpression(Addr);
617}
618
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000619DIExpression *DIBuilder::createBitPieceExpression(unsigned OffsetInBytes,
620 unsigned SizeInBytes) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000621 uint64_t Addr[] = {dwarf::DW_OP_bit_piece, OffsetInBytes, SizeInBytes};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000622 return DIExpression::get(VMContext, Addr);
Duncan P. N. Exon Smith9affbba2014-10-01 21:32:12 +0000623}
624
Duncan P. N. Exon Smithb2df6472015-08-26 22:50:16 +0000625template <class... Ts>
626static DISubprogram *getSubprogram(bool IsDistinct, Ts &&... Args) {
627 if (IsDistinct)
628 return DISubprogram::getDistinct(std::forward<Ts>(Args)...);
629 return DISubprogram::get(std::forward<Ts>(Args)...);
630}
631
Peter Collingbourned4bff302015-11-05 22:03:56 +0000632DISubprogram *DIBuilder::createFunction(
633 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *File,
634 unsigned LineNo, DISubroutineType *Ty, bool isLocalToUnit,
Leny Kholodov40c62352016-09-06 17:03:02 +0000635 bool isDefinition, unsigned ScopeLine, DINode::DIFlags Flags,
636 bool isOptimized, DITemplateParameterArray TParams, DISubprogram *Decl) {
Adrian Prantl75819ae2016-04-15 15:57:41 +0000637 auto *Node = getSubprogram(
638 /* IsDistinct = */ isDefinition, VMContext,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000639 getNonCompileUnitScope(Context), Name, LinkageName, File, LineNo, Ty,
Reid Klecknerb5af11d2016-07-01 02:41:21 +0000640 isLocalToUnit, isDefinition, ScopeLine, nullptr, 0, 0, 0, Flags,
641 isOptimized, isDefinition ? CUNode : nullptr, TParams, Decl,
Adrian Prantl75819ae2016-04-15 15:57:41 +0000642 MDTuple::getTemporary(VMContext, None).release());
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000643
644 if (isDefinition)
645 AllSubprograms.push_back(Node);
646 trackIfUnresolved(Node);
647 return Node;
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000648}
649
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000650DISubprogram *DIBuilder::createTempFunctionFwdDecl(
651 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *File,
652 unsigned LineNo, DISubroutineType *Ty, bool isLocalToUnit,
Leny Kholodov40c62352016-09-06 17:03:02 +0000653 bool isDefinition, unsigned ScopeLine, DINode::DIFlags Flags,
654 bool isOptimized, DITemplateParameterArray TParams, DISubprogram *Decl) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000655 return DISubprogram::getTemporary(
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000656 VMContext, getNonCompileUnitScope(Context), Name, LinkageName,
657 File, LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine, nullptr,
Reid Klecknerb5af11d2016-07-01 02:41:21 +0000658 0, 0, 0, Flags, isOptimized, isDefinition ? CUNode : nullptr,
659 TParams, Decl, nullptr)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000660 .release();
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000661}
662
Leny Kholodov40c62352016-09-06 17:03:02 +0000663DISubprogram *DIBuilder::createMethod(DIScope *Context, StringRef Name,
664 StringRef LinkageName, DIFile *F,
665 unsigned LineNo, DISubroutineType *Ty,
666 bool isLocalToUnit, bool isDefinition,
667 unsigned VK, unsigned VIndex,
668 int ThisAdjustment, DIType *VTableHolder,
669 DINode::DIFlags Flags, bool isOptimized,
670 DITemplateParameterArray TParams) {
Eric Christopher5cb56322013-10-15 23:31:36 +0000671 assert(getNonCompileUnitScope(Context) &&
672 "Methods should have both a Context and a context that isn't "
673 "the compile unit.");
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000674 // FIXME: Do we want to use different scope/lines?
Peter Collingbourned4bff302015-11-05 22:03:56 +0000675 auto *SP = getSubprogram(
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000676 /* IsDistinct = */ isDefinition, VMContext, cast<DIScope>(Context), Name,
677 LinkageName, F, LineNo, Ty, isLocalToUnit, isDefinition, LineNo,
Reid Klecknerb5af11d2016-07-01 02:41:21 +0000678 VTableHolder, VK, VIndex, ThisAdjustment, Flags, isOptimized,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000679 isDefinition ? CUNode : nullptr, TParams, nullptr, nullptr);
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000680
David Blaikie595eb442013-02-18 07:10:22 +0000681 if (isDefinition)
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000682 AllSubprograms.push_back(SP);
683 trackIfUnresolved(SP);
684 return SP;
Devang Patelb68c6232010-12-08 20:42:44 +0000685}
686
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000687DINamespace *DIBuilder::createNameSpace(DIScope *Scope, StringRef Name,
688 DIFile *File, unsigned LineNo) {
689 return DINamespace::get(VMContext, getNonCompileUnitScope(Scope), File, Name,
Duncan P. N. Exon Smitha5099dc2015-04-06 19:49:39 +0000690 LineNo);
Devang Patel746660f2010-12-07 23:25:47 +0000691}
692
Adrian Prantlab1243f2015-06-29 23:03:47 +0000693DIModule *DIBuilder::createModule(DIScope *Scope, StringRef Name,
694 StringRef ConfigurationMacros,
695 StringRef IncludePath,
696 StringRef ISysRoot) {
697 return DIModule::get(VMContext, getNonCompileUnitScope(Scope), Name,
698 ConfigurationMacros, IncludePath, ISysRoot);
699}
700
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000701DILexicalBlockFile *DIBuilder::createLexicalBlockFile(DIScope *Scope,
702 DIFile *File,
703 unsigned Discriminator) {
704 return DILexicalBlockFile::get(VMContext, Scope, File, Discriminator);
Eric Christopher6647b832011-10-11 22:59:11 +0000705}
706
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000707DILexicalBlock *DIBuilder::createLexicalBlock(DIScope *Scope, DIFile *File,
708 unsigned Line, unsigned Col) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000709 // Make these distinct, to avoid merging two lexical blocks on the same
710 // file/line/column.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000711 return DILexicalBlock::getDistinct(VMContext, getNonCompileUnitScope(Scope),
Duncan P. N. Exon Smith35ef22c2015-04-15 23:19:27 +0000712 File, Line, Col);
Devang Patel89ea4f22010-12-08 01:50:15 +0000713}
714
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000715static Value *getDbgIntrinsicValueImpl(LLVMContext &VMContext, Value *V) {
716 assert(V && "no value passed to dbg intrinsic");
717 return MetadataAsValue::get(VMContext, ValueAsMetadata::get(V));
718}
719
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000720static Instruction *withDebugLoc(Instruction *I, const DILocation *DL) {
721 I->setDebugLoc(const_cast<DILocation *>(DL));
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000722 return I;
723}
724
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000725Instruction *DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
726 DIExpression *Expr, const DILocation *DL,
Devang Patel746660f2010-12-07 23:25:47 +0000727 Instruction *InsertBefore) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000728 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.declare");
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000729 assert(DL && "Expected debug loc");
730 assert(DL->getScope()->getSubprogram() ==
731 VarInfo->getScope()->getSubprogram() &&
732 "Expected matching subprograms");
Devang Patel746660f2010-12-07 23:25:47 +0000733 if (!DeclareFn)
734 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
735
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000736 trackIfUnresolved(VarInfo);
737 trackIfUnresolved(Expr);
738 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, Storage),
739 MetadataAsValue::get(VMContext, VarInfo),
740 MetadataAsValue::get(VMContext, Expr)};
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000741 return withDebugLoc(CallInst::Create(DeclareFn, Args, "", InsertBefore), DL);
Devang Patel746660f2010-12-07 23:25:47 +0000742}
743
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000744Instruction *DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
745 DIExpression *Expr, const DILocation *DL,
Devang Patel746660f2010-12-07 23:25:47 +0000746 BasicBlock *InsertAtEnd) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000747 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.declare");
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000748 assert(DL && "Expected debug loc");
749 assert(DL->getScope()->getSubprogram() ==
750 VarInfo->getScope()->getSubprogram() &&
751 "Expected matching subprograms");
Devang Patel746660f2010-12-07 23:25:47 +0000752 if (!DeclareFn)
753 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
754
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000755 trackIfUnresolved(VarInfo);
756 trackIfUnresolved(Expr);
757 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, Storage),
758 MetadataAsValue::get(VMContext, VarInfo),
759 MetadataAsValue::get(VMContext, Expr)};
Devang Patel746660f2010-12-07 23:25:47 +0000760
761 // If this block already has a terminator then insert this intrinsic
762 // before the terminator.
763 if (TerminatorInst *T = InsertAtEnd->getTerminator())
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000764 return withDebugLoc(CallInst::Create(DeclareFn, Args, "", T), DL);
765 return withDebugLoc(CallInst::Create(DeclareFn, Args, "", InsertAtEnd), DL);
Devang Patel746660f2010-12-07 23:25:47 +0000766}
767
Devang Patel9b412732011-02-22 18:56:12 +0000768Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000769 DILocalVariable *VarInfo,
770 DIExpression *Expr,
771 const DILocation *DL,
Devang Patel746660f2010-12-07 23:25:47 +0000772 Instruction *InsertBefore) {
773 assert(V && "no value passed to dbg.value");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000774 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.value");
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000775 assert(DL && "Expected debug loc");
776 assert(DL->getScope()->getSubprogram() ==
777 VarInfo->getScope()->getSubprogram() &&
778 "Expected matching subprograms");
Devang Patel746660f2010-12-07 23:25:47 +0000779 if (!ValueFn)
780 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
781
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000782 trackIfUnresolved(VarInfo);
783 trackIfUnresolved(Expr);
784 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, V),
785 ConstantInt::get(Type::getInt64Ty(VMContext), Offset),
786 MetadataAsValue::get(VMContext, VarInfo),
787 MetadataAsValue::get(VMContext, Expr)};
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000788 return withDebugLoc(CallInst::Create(ValueFn, Args, "", InsertBefore), DL);
Devang Patel746660f2010-12-07 23:25:47 +0000789}
790
Devang Patel9b412732011-02-22 18:56:12 +0000791Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000792 DILocalVariable *VarInfo,
793 DIExpression *Expr,
794 const DILocation *DL,
Devang Patel746660f2010-12-07 23:25:47 +0000795 BasicBlock *InsertAtEnd) {
796 assert(V && "no value passed to dbg.value");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000797 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.value");
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000798 assert(DL && "Expected debug loc");
799 assert(DL->getScope()->getSubprogram() ==
800 VarInfo->getScope()->getSubprogram() &&
801 "Expected matching subprograms");
Devang Patel746660f2010-12-07 23:25:47 +0000802 if (!ValueFn)
803 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
804
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000805 trackIfUnresolved(VarInfo);
806 trackIfUnresolved(Expr);
807 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, V),
808 ConstantInt::get(Type::getInt64Ty(VMContext), Offset),
809 MetadataAsValue::get(VMContext, VarInfo),
810 MetadataAsValue::get(VMContext, Expr)};
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000811
812 return withDebugLoc(CallInst::Create(ValueFn, Args, "", InsertAtEnd), DL);
Devang Patel746660f2010-12-07 23:25:47 +0000813}
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000814
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000815void DIBuilder::replaceVTableHolder(DICompositeType *&T,
816 DICompositeType *VTableHolder) {
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000817 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000818 TypedTrackingMDRef<DICompositeType> N(T);
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000819 N->replaceVTableHolder(VTableHolder);
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000820 T = N.get();
821 }
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000822
823 // If this didn't create a self-reference, just return.
824 if (T != VTableHolder)
825 return;
826
Adrian Prantl18a25b02015-02-11 17:45:10 +0000827 // Look for unresolved operands. T will drop RAUW support, orphaning any
828 // cycles underneath it.
829 if (T->isResolved())
830 for (const MDOperand &O : T->operands())
831 if (auto *N = dyn_cast_or_null<MDNode>(O))
832 trackIfUnresolved(N);
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000833}
834
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000835void DIBuilder::replaceArrays(DICompositeType *&T, DINodeArray Elements,
836 DINodeArray TParams) {
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000837 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000838 TypedTrackingMDRef<DICompositeType> N(T);
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000839 if (Elements)
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000840 N->replaceElements(Elements);
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000841 if (TParams)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000842 N->replaceTemplateParams(DITemplateParameterArray(TParams));
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000843 T = N.get();
844 }
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000845
846 // If T isn't resolved, there's no problem.
847 if (!T->isResolved())
848 return;
849
Adrian Prantl12d52842015-07-10 23:26:02 +0000850 // If T is resolved, it may be due to a self-reference cycle. Track the
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000851 // arrays explicitly if they're unresolved, or else the cycles will be
852 // orphaned.
853 if (Elements)
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +0000854 trackIfUnresolved(Elements.get());
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000855 if (TParams)
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +0000856 trackIfUnresolved(TParams.get());
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000857}