blob: 28a8d567c22316a444a82dfc9202b24ca77a53a2 [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"
22
23using namespace llvm;
24using namespace llvm::dwarf;
25
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +000026namespace {
27class HeaderBuilder {
Duncan P. N. Exon Smithaa687a32015-01-20 05:02:42 +000028 /// \brief Whether there are any fields yet.
29 ///
30 /// Note that this is not equivalent to \c Chars.empty(), since \a concat()
31 /// may have been called already with an empty string.
32 bool IsEmpty;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +000033 SmallVector<char, 256> Chars;
34
35public:
Duncan P. N. Exon Smithaa687a32015-01-20 05:02:42 +000036 HeaderBuilder() : IsEmpty(true) {}
37 HeaderBuilder(const HeaderBuilder &X) : IsEmpty(X.IsEmpty), Chars(X.Chars) {}
38 HeaderBuilder(HeaderBuilder &&X)
39 : IsEmpty(X.IsEmpty), Chars(std::move(X.Chars)) {}
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +000040
41 template <class Twineable> HeaderBuilder &concat(Twineable &&X) {
Duncan P. N. Exon Smithaa687a32015-01-20 05:02:42 +000042 if (IsEmpty)
43 IsEmpty = false;
44 else
45 Chars.push_back(0);
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +000046 Twine(X).toVector(Chars);
47 return *this;
48 }
49
50 MDString *get(LLVMContext &Context) const {
51 return MDString::get(Context, StringRef(Chars.begin(), Chars.size()));
52 }
53
54 static HeaderBuilder get(unsigned Tag) {
Duncan P. N. Exon Smithaa687a32015-01-20 05:02:42 +000055 return HeaderBuilder().concat("0x" + Twine::utohexstr(Tag));
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +000056 }
57};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000058}
Devang Patel63f83cd2010-12-07 23:58:00 +000059
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000060DIBuilder::DIBuilder(Module &m, bool AllowUnresolvedNodes)
Adrian Prantl18c073a2015-07-02 22:32:52 +000061 : M(m), VMContext(M.getContext()), CUNode(nullptr),
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000062 DeclareFn(nullptr), ValueFn(nullptr),
63 AllowUnresolvedNodes(AllowUnresolvedNodes) {}
64
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000065void DIBuilder::trackIfUnresolved(MDNode *N) {
Duncan P. N. Exon Smith9b1c6d32015-01-19 19:09:14 +000066 if (!N)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000067 return;
Duncan P. N. Exon Smith9b1c6d32015-01-19 19:09:14 +000068 if (N->isResolved())
69 return;
70
71 assert(AllowUnresolvedNodes && "Cannot handle unresolved nodes");
72 UnresolvedNodes.emplace_back(N);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000073}
Devang Patel57c5a202010-11-04 15:01:38 +000074
Devang Patel2b8acaf2011-08-15 23:00:00 +000075void DIBuilder::finalize() {
Adrian Prantl0e224a62015-07-06 16:22:12 +000076 if (!CUNode) {
77 assert(!AllowUnresolvedNodes &&
78 "creating type nodes without a CU is not supported");
79 return;
Devang Patel59e27c52011-08-19 23:28:12 +000080 }
Devang Pateleb1bb4e2011-08-16 22:09:43 +000081
Adrian Prantl0e224a62015-07-06 16:22:12 +000082 CUNode->replaceEnumTypes(MDTuple::get(VMContext, AllEnumTypes));
83
84 SmallVector<Metadata *, 16> RetainValues;
85 // Declarations and definitions of the same type may be retained. Some
86 // clients RAUW these pairs, leaving duplicates in the retained types
87 // list. Use a set to remove the duplicates while we transform the
88 // TrackingVHs back into Values.
89 SmallPtrSet<Metadata *, 16> RetainSet;
90 for (unsigned I = 0, E = AllRetainTypes.size(); I < E; I++)
91 if (RetainSet.insert(AllRetainTypes[I]).second)
92 RetainValues.push_back(AllRetainTypes[I]);
Adrian Prantl4276d4a2015-07-06 16:36:02 +000093
94 if (!RetainValues.empty())
95 CUNode->replaceRetainedTypes(MDTuple::get(VMContext, RetainValues));
Adrian Prantl0e224a62015-07-06 16:22:12 +000096
97 DISubprogramArray SPs = MDTuple::get(VMContext, AllSubprograms);
Adrian Prantl4276d4a2015-07-06 16:36:02 +000098 if (!AllSubprograms.empty())
99 CUNode->replaceSubprograms(SPs.get());
100
Adrian Prantl0e224a62015-07-06 16:22:12 +0000101 for (auto *SP : SPs) {
102 if (MDTuple *Temp = SP->getVariables().get()) {
103 const auto &PV = PreservedVariables.lookup(SP);
104 SmallVector<Metadata *, 4> Variables(PV.begin(), PV.end());
105 DINodeArray AV = getOrCreateArray(Variables);
106 TempMDTuple(Temp)->replaceAllUsesWith(AV.get());
107 }
108 }
109
Adrian Prantl4276d4a2015-07-06 16:36:02 +0000110 if (!AllGVs.empty())
111 CUNode->replaceGlobalVariables(MDTuple::get(VMContext, AllGVs));
Adrian Prantl0e224a62015-07-06 16:22:12 +0000112
Adrian Prantl4276d4a2015-07-06 16:36:02 +0000113 if (!AllImportedModules.empty())
114 CUNode->replaceImportedEntities(MDTuple::get(
115 VMContext, SmallVector<Metadata *, 16>(AllImportedModules.begin(),
116 AllImportedModules.end())));
Adrian Prantl0e224a62015-07-06 16:22:12 +0000117
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000118 // Now that all temp nodes have been replaced or deleted, resolve remaining
119 // cycles.
120 for (const auto &N : UnresolvedNodes)
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +0000121 if (N && !N->isResolved())
122 N->resolveCycles();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000123 UnresolvedNodes.clear();
124
125 // Can't handle unresolved nodes anymore.
126 AllowUnresolvedNodes = false;
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000127}
128
Duncan P. N. Exon Smith379e3752014-10-01 21:32:15 +0000129/// If N is compile unit return NULL otherwise return N.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000130static DIScope *getNonCompileUnitScope(DIScope *N) {
131 if (!N || isa<DICompileUnit>(N))
Craig Topperc6207612014-04-09 06:08:46 +0000132 return nullptr;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000133 return cast<DIScope>(N);
Devang Patel2b8acaf2011-08-15 23:00:00 +0000134}
135
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000136DICompileUnit *DIBuilder::createCompileUnit(
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000137 unsigned Lang, StringRef Filename, StringRef Directory, StringRef Producer,
138 bool isOptimized, StringRef Flags, unsigned RunTimeVer, StringRef SplitName,
Adrian Prantl1f599f92015-05-21 20:37:30 +0000139 DebugEmissionKind Kind, uint64_t DWOId, bool EmitDebugInfo) {
Eric Christopher75d49db2014-02-27 01:24:56 +0000140
Bruce Mitchener7e575ed2015-02-07 06:35:30 +0000141 assert(((Lang <= dwarf::DW_LANG_Fortran08 && Lang >= dwarf::DW_LANG_C89) ||
Chandler Carruth4c0ee742012-01-10 18:18:52 +0000142 (Lang <= dwarf::DW_LANG_hi_user && Lang >= dwarf::DW_LANG_lo_user)) &&
143 "Invalid Language tag");
144 assert(!Filename.empty() &&
145 "Unable to create compile unit without filename");
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000146
Adrian Prantl18c073a2015-07-02 22:32:52 +0000147 assert(!CUNode && "Can only make one compile unit per DIBuilder instance");
148 CUNode = DICompileUnit::getDistinct(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000149 VMContext, Lang, DIFile::get(VMContext, Filename, Directory), Producer,
Adrian Prantl18c073a2015-07-02 22:32:52 +0000150 isOptimized, Flags, RunTimeVer, SplitName, Kind, nullptr,
151 nullptr, nullptr, nullptr, nullptr, DWOId);
Devang Patel09fa69e2011-05-03 16:18:28 +0000152
153 // Create a named metadata so that it is easier to find cu in a module.
Diego Novillo56653fd2014-06-24 17:02:03 +0000154 // Note that we only generate this when the caller wants to actually
155 // emit debug information. When we are only interested in tracking
156 // source line locations throughout the backend, we prevent codegen from
157 // emitting debug info in the final output by not generating llvm.dbg.cu.
158 if (EmitDebugInfo) {
159 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
160 NMD->addOperand(CUNode);
161 }
Eric Christopher03b3e112013-07-19 00:51:47 +0000162
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000163 trackIfUnresolved(CUNode);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000164 return CUNode;
Devang Patel57c5a202010-11-04 15:01:38 +0000165}
166
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000167static DIImportedEntity *
168createImportedModule(LLVMContext &C, dwarf::Tag Tag, DIScope *Context,
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000169 Metadata *NS, unsigned Line, StringRef Name,
170 SmallVectorImpl<TrackingMDNodeRef> &AllImportedModules) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000171 auto *M = DIImportedEntity::get(C, Tag, Context, DINodeRef(NS), Line, Name);
Duncan P. N. Exon Smithde8e4272015-04-14 01:46:44 +0000172 AllImportedModules.emplace_back(M);
David Blaikie1fd43652013-05-07 21:35:53 +0000173 return M;
174}
175
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000176DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context,
177 DINamespace *NS,
178 unsigned Line) {
David Blaikie2a40c142014-04-06 06:29:01 +0000179 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
180 Context, NS, Line, StringRef(), AllImportedModules);
David Blaikiee63d5d12013-05-20 22:50:35 +0000181}
182
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000183DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context,
184 DIImportedEntity *NS,
185 unsigned Line) {
David Blaikie2a40c142014-04-06 06:29:01 +0000186 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
187 Context, NS, Line, StringRef(), AllImportedModules);
David Blaikiee63d5d12013-05-20 22:50:35 +0000188}
189
Adrian Prantlab1243f2015-06-29 23:03:47 +0000190DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context, DIModule *M,
191 unsigned Line) {
192 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
193 Context, M, Line, StringRef(), AllImportedModules);
194}
195
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000196DIImportedEntity *DIBuilder::createImportedDeclaration(DIScope *Context,
197 DINode *Decl,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000198 unsigned Line,
199 StringRef Name) {
Frederic Riss4aa51ae2014-11-06 17:46:55 +0000200 // Make sure to use the unique identifier based metadata reference for
201 // types that have one.
David Blaikie2a40c142014-04-06 06:29:01 +0000202 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_declaration,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000203 Context, DINodeRef::get(Decl), Line, Name,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000204 AllImportedModules);
David Blaikief55abea2013-04-22 06:12:31 +0000205}
206
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000207DIFile *DIBuilder::createFile(StringRef Filename, StringRef Directory) {
208 return DIFile::get(VMContext, Filename, Directory);
Devang Patel57c5a202010-11-04 15:01:38 +0000209}
210
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000211DIEnumerator *DIBuilder::createEnumerator(StringRef Name, int64_t Val) {
Devang Patel1ad1abe2011-09-12 18:26:08 +0000212 assert(!Name.empty() && "Unable to create enumerator without name");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000213 return DIEnumerator::get(VMContext, Val, Name);
Devang Patel57c5a202010-11-04 15:01:38 +0000214}
215
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000216DIBasicType *DIBuilder::createUnspecifiedType(StringRef Name) {
Devang Patel04d6d472011-09-14 23:13:28 +0000217 assert(!Name.empty() && "Unable to create type without name");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000218 return DIBasicType::get(VMContext, dwarf::DW_TAG_unspecified_type, Name);
Devang Patel04d6d472011-09-14 23:13:28 +0000219}
220
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000221DIBasicType *DIBuilder::createNullPtrType() {
Peter Collingbournea4a47cb2013-06-27 22:50:59 +0000222 return createUnspecifiedType("decltype(nullptr)");
223}
224
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000225DIBasicType *DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000226 uint64_t AlignInBits,
227 unsigned Encoding) {
Devang Patel1ad1abe2011-09-12 18:26:08 +0000228 assert(!Name.empty() && "Unable to create type without name");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000229 return DIBasicType::get(VMContext, dwarf::DW_TAG_base_type, Name, SizeInBits,
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000230 AlignInBits, Encoding);
Devang Patel57c5a202010-11-04 15:01:38 +0000231}
232
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000233DIDerivedType *DIBuilder::createQualifiedType(unsigned Tag, DIType *FromTy) {
234 return DIDerivedType::get(VMContext, Tag, "", nullptr, 0, nullptr,
235 DITypeRef::get(FromTy), 0, 0, 0, 0);
Devang Patel57c5a202010-11-04 15:01:38 +0000236}
237
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000238DIDerivedType *DIBuilder::createPointerType(DIType *PointeeTy,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000239 uint64_t SizeInBits,
240 uint64_t AlignInBits,
241 StringRef Name) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000242 // FIXME: Why is there a name here?
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000243 return DIDerivedType::get(VMContext, dwarf::DW_TAG_pointer_type, Name,
244 nullptr, 0, nullptr, DITypeRef::get(PointeeTy),
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000245 SizeInBits, AlignInBits, 0, 0);
Devang Patel57c5a202010-11-04 15:01:38 +0000246}
247
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000248DIDerivedType *DIBuilder::createMemberPointerType(DIType *PointeeTy,
249 DIType *Base,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000250 uint64_t SizeInBits,
251 uint64_t AlignInBits) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000252 return DIDerivedType::get(VMContext, dwarf::DW_TAG_ptr_to_member_type, "",
253 nullptr, 0, nullptr, DITypeRef::get(PointeeTy),
254 SizeInBits, AlignInBits, 0, 0,
255 DITypeRef::get(Base));
David Blaikie5d3249b2013-01-07 05:51:15 +0000256}
257
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000258DIDerivedType *DIBuilder::createReferenceType(unsigned Tag, DIType *RTy) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000259 assert(RTy && "Unable to create reference type");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000260 return DIDerivedType::get(VMContext, Tag, "", nullptr, 0, nullptr,
261 DITypeRef::get(RTy), 0, 0, 0, 0);
Devang Patel57c5a202010-11-04 15:01:38 +0000262}
263
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000264DIDerivedType *DIBuilder::createTypedef(DIType *Ty, StringRef Name,
265 DIFile *File, unsigned LineNo,
266 DIScope *Context) {
267 return DIDerivedType::get(VMContext, dwarf::DW_TAG_typedef, Name, File,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000268 LineNo,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000269 DIScopeRef::get(getNonCompileUnitScope(Context)),
270 DITypeRef::get(Ty), 0, 0, 0, 0);
Devang Patel57c5a202010-11-04 15:01:38 +0000271}
272
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000273DIDerivedType *DIBuilder::createFriend(DIType *Ty, DIType *FriendTy) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000274 assert(Ty && "Invalid type!");
275 assert(FriendTy && "Invalid friend type!");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000276 return DIDerivedType::get(VMContext, dwarf::DW_TAG_friend, "", nullptr, 0,
277 DITypeRef::get(Ty), DITypeRef::get(FriendTy), 0, 0,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000278 0, 0);
Devang Patel57c5a202010-11-04 15:01:38 +0000279}
280
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000281DIDerivedType *DIBuilder::createInheritance(DIType *Ty, DIType *BaseTy,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000282 uint64_t BaseOffset,
283 unsigned Flags) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000284 assert(Ty && "Unable to create inheritance");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000285 return DIDerivedType::get(VMContext, dwarf::DW_TAG_inheritance, "", nullptr,
286 0, DITypeRef::get(Ty), DITypeRef::get(BaseTy), 0, 0,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000287 BaseOffset, Flags);
Devang Patel57c5a202010-11-04 15:01:38 +0000288}
289
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000290DIDerivedType *DIBuilder::createMemberType(DIScope *Scope, StringRef Name,
291 DIFile *File, unsigned LineNumber,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000292 uint64_t SizeInBits,
293 uint64_t AlignInBits,
294 uint64_t OffsetInBits,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000295 unsigned Flags, DIType *Ty) {
296 return DIDerivedType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000297 VMContext, dwarf::DW_TAG_member, Name, File, LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000298 DIScopeRef::get(getNonCompileUnitScope(Scope)), DITypeRef::get(Ty),
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000299 SizeInBits, AlignInBits, OffsetInBits, Flags);
Devang Patel57c5a202010-11-04 15:01:38 +0000300}
301
Duncan P. N. Exon Smith3cd2cab2015-03-27 00:34:10 +0000302static ConstantAsMetadata *getConstantOrNull(Constant *C) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000303 if (C)
304 return ConstantAsMetadata::get(C);
305 return nullptr;
306}
307
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000308DIDerivedType *DIBuilder::createStaticMemberType(DIScope *Scope, StringRef Name,
309 DIFile *File,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000310 unsigned LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000311 DIType *Ty, unsigned Flags,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000312 llvm::Constant *Val) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000313 Flags |= DINode::FlagStaticMember;
314 return DIDerivedType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000315 VMContext, dwarf::DW_TAG_member, Name, File, LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000316 DIScopeRef::get(getNonCompileUnitScope(Scope)), DITypeRef::get(Ty), 0, 0,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000317 0, Flags, getConstantOrNull(Val));
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000318}
319
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000320DIDerivedType *DIBuilder::createObjCIVar(StringRef Name, DIFile *File,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000321 unsigned LineNumber,
322 uint64_t SizeInBits,
323 uint64_t AlignInBits,
324 uint64_t OffsetInBits, unsigned Flags,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000325 DIType *Ty, MDNode *PropertyNode) {
326 return DIDerivedType::get(
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000327 VMContext, dwarf::DW_TAG_member, Name, File, LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000328 DIScopeRef::get(getNonCompileUnitScope(File)), DITypeRef::get(Ty),
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000329 SizeInBits, AlignInBits, OffsetInBits, Flags, PropertyNode);
Devang Patel44882172012-02-06 17:49:43 +0000330}
331
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000332DIObjCProperty *
333DIBuilder::createObjCProperty(StringRef Name, DIFile *File, unsigned LineNumber,
Eric Christopher98f9c232013-10-15 23:31:31 +0000334 StringRef GetterName, StringRef SetterName,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000335 unsigned PropertyAttributes, DIType *Ty) {
336 return DIObjCProperty::get(VMContext, Name, File, LineNumber, GetterName,
Adrian Prantl8ff53b32015-06-15 23:18:03 +0000337 SetterName, PropertyAttributes,
338 DITypeRef::get(Ty));
Devang Patelcc481592012-02-04 00:59:25 +0000339}
340
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000341DITemplateTypeParameter *
342DIBuilder::createTemplateTypeParameter(DIScope *Context, StringRef Name,
343 DIType *Ty) {
344 assert((!Context || isa<DICompileUnit>(Context)) && "Expected compile unit");
345 return DITemplateTypeParameter::get(VMContext, Name, DITypeRef::get(Ty));
Devang Patel3a9e65e2011-02-02 21:38:25 +0000346}
347
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000348static DITemplateValueParameter *
Duncan P. N. Exon Smithb4aa16f2015-02-13 03:35:29 +0000349createTemplateValueParameterHelper(LLVMContext &VMContext, unsigned Tag,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000350 DIScope *Context, StringRef Name, DIType *Ty,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000351 Metadata *MD) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000352 assert((!Context || isa<DICompileUnit>(Context)) && "Expected compile unit");
353 return DITemplateValueParameter::get(VMContext, Tag, Name, DITypeRef::get(Ty),
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000354 MD);
Devang Patelbe933b42011-02-02 22:35:53 +0000355}
356
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000357DITemplateValueParameter *
358DIBuilder::createTemplateValueParameter(DIScope *Context, StringRef Name,
359 DIType *Ty, Constant *Val) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000360 return createTemplateValueParameterHelper(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000361 VMContext, dwarf::DW_TAG_template_value_parameter, Context, Name, Ty,
Duncan P. N. Exon Smithb4aa16f2015-02-13 03:35:29 +0000362 getConstantOrNull(Val));
David Blaikie2b380232013-06-22 18:59:11 +0000363}
364
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000365DITemplateValueParameter *
366DIBuilder::createTemplateTemplateParameter(DIScope *Context, StringRef Name,
367 DIType *Ty, StringRef Val) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000368 return createTemplateValueParameterHelper(
369 VMContext, dwarf::DW_TAG_GNU_template_template_param, Context, Name, Ty,
Duncan P. N. Exon Smithb4aa16f2015-02-13 03:35:29 +0000370 MDString::get(VMContext, Val));
David Blaikie2b380232013-06-22 18:59:11 +0000371}
372
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000373DITemplateValueParameter *
374DIBuilder::createTemplateParameterPack(DIScope *Context, StringRef Name,
375 DIType *Ty, DINodeArray Val) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000376 return createTemplateValueParameterHelper(
377 VMContext, dwarf::DW_TAG_GNU_template_parameter_pack, Context, Name, Ty,
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +0000378 Val.get());
David Blaikie2b380232013-06-22 18:59:11 +0000379}
380
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000381DICompositeType *DIBuilder::createClassType(
382 DIScope *Context, StringRef Name, DIFile *File, unsigned LineNumber,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000383 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000384 unsigned Flags, DIType *DerivedFrom, DINodeArray Elements,
385 DIType *VTableHolder, MDNode *TemplateParams, StringRef UniqueIdentifier) {
386 assert((!Context || isa<DIScope>(Context)) &&
David Blaikie085abe32013-03-11 23:21:19 +0000387 "createClassType should be called with a valid Context");
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000388
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000389 auto *R = DICompositeType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000390 VMContext, dwarf::DW_TAG_structure_type, Name, File, LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000391 DIScopeRef::get(getNonCompileUnitScope(Context)),
392 DITypeRef::get(DerivedFrom), SizeInBits, AlignInBits, OffsetInBits, Flags,
393 Elements, 0, DITypeRef::get(VTableHolder),
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000394 cast_or_null<MDTuple>(TemplateParams), UniqueIdentifier);
Manman Ren0b410402013-08-29 23:17:54 +0000395 if (!UniqueIdentifier.empty())
396 retainType(R);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000397 trackIfUnresolved(R);
David Blaikie085abe32013-03-11 23:21:19 +0000398 return R;
Eric Christopher17426692012-07-06 02:35:57 +0000399}
400
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000401DICompositeType *DIBuilder::createStructType(
402 DIScope *Context, StringRef Name, DIFile *File, unsigned LineNumber,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000403 uint64_t SizeInBits, uint64_t AlignInBits, unsigned Flags,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000404 DIType *DerivedFrom, DINodeArray Elements, unsigned RunTimeLang,
405 DIType *VTableHolder, StringRef UniqueIdentifier) {
406 auto *R = DICompositeType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000407 VMContext, dwarf::DW_TAG_structure_type, Name, File, LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000408 DIScopeRef::get(getNonCompileUnitScope(Context)),
409 DITypeRef::get(DerivedFrom), SizeInBits, AlignInBits, 0, Flags, Elements,
410 RunTimeLang, DITypeRef::get(VTableHolder), nullptr, UniqueIdentifier);
Manman Ren0b410402013-08-29 23:17:54 +0000411 if (!UniqueIdentifier.empty())
412 retainType(R);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000413 trackIfUnresolved(R);
David Blaikie085abe32013-03-11 23:21:19 +0000414 return R;
Devang Patel746660f2010-12-07 23:25:47 +0000415}
416
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000417DICompositeType *DIBuilder::createUnionType(
418 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
Duncan P. N. Exon Smithaa861aa2015-04-21 20:07:38 +0000419 uint64_t SizeInBits, uint64_t AlignInBits, unsigned Flags,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000420 DINodeArray Elements, unsigned RunTimeLang, StringRef UniqueIdentifier) {
421 auto *R = DICompositeType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000422 VMContext, dwarf::DW_TAG_union_type, Name, File, LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000423 DIScopeRef::get(getNonCompileUnitScope(Scope)), nullptr, SizeInBits,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000424 AlignInBits, 0, Flags, Elements, RunTimeLang, nullptr, nullptr,
425 UniqueIdentifier);
Manman Ren0b410402013-08-29 23:17:54 +0000426 if (!UniqueIdentifier.empty())
427 retainType(R);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000428 trackIfUnresolved(R);
Manman Ren0b410402013-08-29 23:17:54 +0000429 return R;
Devang Patel89ea4f22010-12-08 01:50:15 +0000430}
431
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000432DISubroutineType *DIBuilder::createSubroutineType(DIFile *File,
433 DITypeRefArray ParameterTypes,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000434 unsigned Flags) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000435 return DISubroutineType::get(VMContext, Flags, ParameterTypes);
Devang Patel89ea4f22010-12-08 01:50:15 +0000436}
437
Adrian Prantlee5feaf2015-07-15 17:01:41 +0000438DICompositeType *DIBuilder::createExternalTypeRef(unsigned Tag, DIFile *File,
439 StringRef UniqueIdentifier) {
440 assert(!UniqueIdentifier.empty() && "external type ref without uid");
441 auto *CTy =
442 DICompositeType::get(VMContext, Tag, "", nullptr, 0, nullptr, nullptr, 0,
443 0, 0, DINode::FlagExternalTypeRef, nullptr, 0,
444 nullptr, nullptr, UniqueIdentifier);
445 // Types with unique IDs need to be in the type map.
446 retainType(CTy);
447 return CTy;
448}
449
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000450DICompositeType *DIBuilder::createEnumerationType(
451 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
452 uint64_t SizeInBits, uint64_t AlignInBits, DINodeArray Elements,
453 DIType *UnderlyingType, StringRef UniqueIdentifier) {
454 auto *CTy = DICompositeType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000455 VMContext, dwarf::DW_TAG_enumeration_type, Name, File, LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000456 DIScopeRef::get(getNonCompileUnitScope(Scope)),
457 DITypeRef::get(UnderlyingType), SizeInBits, AlignInBits, 0, 0, Elements,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000458 0, nullptr, nullptr, UniqueIdentifier);
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000459 AllEnumTypes.push_back(CTy);
Manman Ren0b410402013-08-29 23:17:54 +0000460 if (!UniqueIdentifier.empty())
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000461 retainType(CTy);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000462 trackIfUnresolved(CTy);
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000463 return CTy;
Devang Patel89ea4f22010-12-08 01:50:15 +0000464}
465
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000466DICompositeType *DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
467 DIType *Ty,
468 DINodeArray Subscripts) {
469 auto *R = DICompositeType::get(VMContext, dwarf::DW_TAG_array_type, "",
470 nullptr, 0, nullptr, DITypeRef::get(Ty), Size,
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000471 AlignInBits, 0, 0, Subscripts, 0, nullptr);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000472 trackIfUnresolved(R);
473 return R;
Devang Patel89ea4f22010-12-08 01:50:15 +0000474}
475
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000476DICompositeType *DIBuilder::createVectorType(uint64_t Size,
477 uint64_t AlignInBits, DIType *Ty,
478 DINodeArray Subscripts) {
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000479 auto *R =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000480 DICompositeType::get(VMContext, dwarf::DW_TAG_array_type, "", nullptr, 0,
481 nullptr, DITypeRef::get(Ty), Size, AlignInBits, 0,
482 DINode::FlagVector, Subscripts, 0, nullptr);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000483 trackIfUnresolved(R);
484 return R;
Devang Patel89ea4f22010-12-08 01:50:15 +0000485}
Devang Patel746660f2010-12-07 23:25:47 +0000486
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000487static DIType *createTypeWithFlags(LLVMContext &Context, DIType *Ty,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000488 unsigned FlagsToSet) {
489 auto NewTy = Ty->clone();
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000490 NewTy->setFlags(NewTy->getFlags() | FlagsToSet);
491 return MDNode::replaceWithUniqued(std::move(NewTy));
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000492}
493
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000494DIType *DIBuilder::createArtificialType(DIType *Ty) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000495 // FIXME: Restrict this to the nodes where it's valid.
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000496 if (Ty->isArtificial())
Devang Patel57c5a202010-11-04 15:01:38 +0000497 return Ty;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000498 return createTypeWithFlags(VMContext, Ty, DINode::FlagArtificial);
Devang Patel57c5a202010-11-04 15:01:38 +0000499}
Devang Patel746660f2010-12-07 23:25:47 +0000500
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000501DIType *DIBuilder::createObjectPointerType(DIType *Ty) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000502 // FIXME: Restrict this to the nodes where it's valid.
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000503 if (Ty->isObjectPointer())
Eric Christophere3417762012-09-12 23:36:19 +0000504 return Ty;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000505 unsigned Flags = DINode::FlagObjectPointer | DINode::FlagArtificial;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000506 return createTypeWithFlags(VMContext, Ty, Flags);
Eric Christophere3417762012-09-12 23:36:19 +0000507}
508
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000509void DIBuilder::retainType(DIType *T) {
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000510 assert(T && "Expected non-null type");
Duncan P. N. Exon Smithd9ccfb92015-03-27 23:00:49 +0000511 AllRetainTypes.emplace_back(T);
512}
Devang Patel89ea4f22010-12-08 01:50:15 +0000513
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000514DIBasicType *DIBuilder::createUnspecifiedParameter() { return nullptr; }
Devang Patel89ea4f22010-12-08 01:50:15 +0000515
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000516DICompositeType *
517DIBuilder::createForwardDecl(unsigned Tag, StringRef Name, DIScope *Scope,
518 DIFile *F, unsigned Line, unsigned RuntimeLang,
Eric Christopher98f9c232013-10-15 23:31:31 +0000519 uint64_t SizeInBits, uint64_t AlignInBits,
520 StringRef UniqueIdentifier) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000521 // FIXME: Define in terms of createReplaceableForwardDecl() by calling
522 // replaceWithUniqued().
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000523 auto *RetTy = DICompositeType::get(
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000524 VMContext, Tag, Name, F, Line,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000525 DIScopeRef::get(getNonCompileUnitScope(Scope)), nullptr, SizeInBits,
526 AlignInBits, 0, DINode::FlagFwdDecl, nullptr, RuntimeLang, nullptr,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000527 nullptr, UniqueIdentifier);
David Blaikied3f094a2014-05-06 03:41:57 +0000528 if (!UniqueIdentifier.empty())
529 retainType(RetTy);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000530 trackIfUnresolved(RetTy);
David Blaikied3f094a2014-05-06 03:41:57 +0000531 return RetTy;
532}
533
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000534DICompositeType *DIBuilder::createReplaceableCompositeType(
535 unsigned Tag, StringRef Name, DIScope *Scope, DIFile *F, unsigned Line,
David Blaikied3f094a2014-05-06 03:41:57 +0000536 unsigned RuntimeLang, uint64_t SizeInBits, uint64_t AlignInBits,
Adrian Prantl534a81a2015-02-11 17:45:05 +0000537 unsigned Flags, StringRef UniqueIdentifier) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000538 auto *RetTy = DICompositeType::getTemporary(
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000539 VMContext, Tag, Name, F, Line,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000540 DIScopeRef::get(getNonCompileUnitScope(Scope)), nullptr,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000541 SizeInBits, AlignInBits, 0, Flags, nullptr, RuntimeLang,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000542 nullptr, nullptr, UniqueIdentifier)
543 .release();
Manman Ren0b410402013-08-29 23:17:54 +0000544 if (!UniqueIdentifier.empty())
545 retainType(RetTy);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000546 trackIfUnresolved(RetTy);
Manman Rend0e67aa2013-07-02 18:37:35 +0000547 return RetTy;
Eric Christopherae56eec2012-02-08 00:22:26 +0000548}
549
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000550DINodeArray DIBuilder::getOrCreateArray(ArrayRef<Metadata *> Elements) {
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000551 return MDTuple::get(VMContext, Elements);
Devang Patel746660f2010-12-07 23:25:47 +0000552}
553
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000554DITypeRefArray DIBuilder::getOrCreateTypeArray(ArrayRef<Metadata *> Elements) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000555 SmallVector<llvm::Metadata *, 16> Elts;
Manman Ren1a125c92014-07-28 19:33:20 +0000556 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
557 if (Elements[i] && isa<MDNode>(Elements[i]))
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000558 Elts.push_back(DITypeRef::get(cast<DIType>(Elements[i])));
Manman Ren1a125c92014-07-28 19:33:20 +0000559 else
560 Elts.push_back(Elements[i]);
561 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000562 return DITypeRefArray(MDNode::get(VMContext, Elts));
Manman Ren1a125c92014-07-28 19:33:20 +0000563}
564
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000565DISubrange *DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
566 return DISubrange::get(VMContext, Count, Lo);
Devang Patel89ea4f22010-12-08 01:50:15 +0000567}
568
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000569static void checkGlobalVariableScope(DIScope *Context) {
Duncan P. N. Exon Smith430220f2015-04-06 23:34:41 +0000570#ifndef NDEBUG
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000571 if (auto *CT =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000572 dyn_cast_or_null<DICompositeType>(getNonCompileUnitScope(Context)))
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000573 assert(CT->getIdentifier().empty() &&
Manman Renbfd2b8292014-11-21 19:47:48 +0000574 "Context of a global variable should not be a type with identifier");
Duncan P. N. Exon Smith430220f2015-04-06 23:34:41 +0000575#endif
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000576}
577
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000578DIGlobalVariable *DIBuilder::createGlobalVariable(
579 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F,
580 unsigned LineNumber, DIType *Ty, bool isLocalToUnit, Constant *Val,
Duncan P. N. Exon Smithdbf64acd2014-11-15 00:23:49 +0000581 MDNode *Decl) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000582 checkGlobalVariableScope(Context);
583
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000584 auto *N = DIGlobalVariable::get(VMContext, cast_or_null<DIScope>(Context),
Duncan P. N. Exon Smithb273d062015-04-16 01:37:00 +0000585 Name, LinkageName, F, LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000586 DITypeRef::get(Ty), isLocalToUnit, true, Val,
587 cast_or_null<DIDerivedType>(Decl));
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000588 AllGVs.push_back(N);
589 return N;
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000590}
591
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000592DIGlobalVariable *DIBuilder::createTempGlobalVariableFwdDecl(
593 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F,
594 unsigned LineNumber, DIType *Ty, bool isLocalToUnit, Constant *Val,
Duncan P. N. Exon Smithdbf64acd2014-11-15 00:23:49 +0000595 MDNode *Decl) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000596 checkGlobalVariableScope(Context);
597
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000598 return DIGlobalVariable::getTemporary(
599 VMContext, cast_or_null<DIScope>(Context), Name, LinkageName, F,
600 LineNumber, DITypeRef::get(Ty), isLocalToUnit, false, Val,
601 cast_or_null<DIDerivedType>(Decl))
Duncan P. N. Exon Smithb273d062015-04-16 01:37:00 +0000602 .release();
Devang Patel746660f2010-12-07 23:25:47 +0000603}
604
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000605DILocalVariable *DIBuilder::createLocalVariable(
606 unsigned Tag, DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNo,
607 DIType *Ty, bool AlwaysPreserve, unsigned Flags, unsigned ArgNo) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000608 // FIXME: Why getNonCompileUnitScope()?
609 // FIXME: Why is "!Context" okay here?
Adrian Prantl12d52842015-07-10 23:26:02 +0000610 // FIXME: Why doesn't this check for a subprogram or lexical block (AFAICT
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000611 // the only valid scopes)?
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000612 DIScope *Context = getNonCompileUnitScope(Scope);
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000613
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000614 auto *Node = DILocalVariable::get(
615 VMContext, Tag, cast_or_null<DILocalScope>(Context), Name, File, LineNo,
616 DITypeRef::get(Ty), ArgNo, Flags);
Devang Patel63f83cd2010-12-07 23:58:00 +0000617 if (AlwaysPreserve) {
Adrian Prantl12d52842015-07-10 23:26:02 +0000618 // The optimizer may remove local variables. If there is an interest
Devang Patel63f83cd2010-12-07 23:58:00 +0000619 // to preserve variable info in such situation then stash it in a
620 // named mdnode.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000621 DISubprogram *Fn = getDISubprogram(Scope);
Duncan P. N. Exon Smith3bfffde2014-10-15 16:11:41 +0000622 assert(Fn && "Missing subprogram for local variable");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000623 PreservedVariables[Fn].emplace_back(Node);
Devang Patel63f83cd2010-12-07 23:58:00 +0000624 }
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000625 return Node;
Devang Patel63f83cd2010-12-07 23:58:00 +0000626}
627
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000628DIExpression *DIBuilder::createExpression(ArrayRef<uint64_t> Addr) {
629 return DIExpression::get(VMContext, Addr);
Devang Patel746660f2010-12-07 23:25:47 +0000630}
631
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000632DIExpression *DIBuilder::createExpression(ArrayRef<int64_t> Signed) {
Duncan P. N. Exon Smithbd75ad42015-02-09 22:13:27 +0000633 // TODO: Remove the callers of this signed version and delete.
634 SmallVector<uint64_t, 8> Addr(Signed.begin(), Signed.end());
635 return createExpression(Addr);
636}
637
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000638DIExpression *DIBuilder::createBitPieceExpression(unsigned OffsetInBytes,
639 unsigned SizeInBytes) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000640 uint64_t Addr[] = {dwarf::DW_OP_bit_piece, OffsetInBytes, SizeInBytes};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000641 return DIExpression::get(VMContext, Addr);
Duncan P. N. Exon Smith9affbba2014-10-01 21:32:12 +0000642}
643
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000644DISubprogram *DIBuilder::createFunction(DIScopeRef Context, StringRef Name,
645 StringRef LinkageName, DIFile *File,
646 unsigned LineNo, DISubroutineType *Ty,
Duncan P. N. Exon Smith848af382015-04-20 18:20:03 +0000647 bool isLocalToUnit, bool isDefinition,
648 unsigned ScopeLine, unsigned Flags,
649 bool isOptimized, Function *Fn,
650 MDNode *TParams, MDNode *Decl) {
Manman Renc50fa112013-10-10 18:40:01 +0000651 // dragonegg does not generate identifier for types, so using an empty map
652 // to resolve the context should be fine.
653 DITypeIdentifierMap EmptyMap;
654 return createFunction(Context.resolve(EmptyMap), Name, LinkageName, File,
655 LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
656 Flags, isOptimized, Fn, TParams, Decl);
657}
658
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000659DISubprogram *DIBuilder::createFunction(DIScope *Context, StringRef Name,
660 StringRef LinkageName, DIFile *File,
661 unsigned LineNo, DISubroutineType *Ty,
662 bool isLocalToUnit, bool isDefinition,
663 unsigned ScopeLine, unsigned Flags,
664 bool isOptimized, Function *Fn,
665 MDNode *TParams, MDNode *Decl) {
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000666 assert(Ty->getTag() == dwarf::DW_TAG_subroutine_type &&
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000667 "function types should be subroutines");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000668 auto *Node = DISubprogram::get(
669 VMContext, DIScopeRef::get(getNonCompileUnitScope(Context)), Name,
670 LinkageName, File, LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
671 nullptr, 0, 0, Flags, isOptimized, Fn, cast_or_null<MDTuple>(TParams),
672 cast_or_null<DISubprogram>(Decl),
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +0000673 MDTuple::getTemporary(VMContext, None).release());
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000674
675 if (isDefinition)
676 AllSubprograms.push_back(Node);
677 trackIfUnresolved(Node);
678 return Node;
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000679}
680
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000681DISubprogram *DIBuilder::createTempFunctionFwdDecl(
682 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *File,
683 unsigned LineNo, DISubroutineType *Ty, bool isLocalToUnit,
684 bool isDefinition, unsigned ScopeLine, unsigned Flags, bool isOptimized,
685 Function *Fn, MDNode *TParams, MDNode *Decl) {
686 return DISubprogram::getTemporary(
687 VMContext, DIScopeRef::get(getNonCompileUnitScope(Context)), Name,
688 LinkageName, File, LineNo, Ty, isLocalToUnit, isDefinition,
689 ScopeLine, nullptr, 0, 0, Flags, isOptimized, Fn,
690 cast_or_null<MDTuple>(TParams), cast_or_null<DISubprogram>(Decl),
691 nullptr)
692 .release();
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000693}
694
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000695DISubprogram *
696DIBuilder::createMethod(DIScope *Context, StringRef Name, StringRef LinkageName,
697 DIFile *F, unsigned LineNo, DISubroutineType *Ty,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000698 bool isLocalToUnit, bool isDefinition, unsigned VK,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000699 unsigned VIndex, DIType *VTableHolder, unsigned Flags,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000700 bool isOptimized, Function *Fn, MDNode *TParam) {
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000701 assert(Ty->getTag() == dwarf::DW_TAG_subroutine_type &&
David Blaikie5174c842013-05-22 23:22:18 +0000702 "function types should be subroutines");
Eric Christopher5cb56322013-10-15 23:31:36 +0000703 assert(getNonCompileUnitScope(Context) &&
704 "Methods should have both a Context and a context that isn't "
705 "the compile unit.");
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000706 // FIXME: Do we want to use different scope/lines?
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000707 auto *SP = DISubprogram::get(
708 VMContext, DIScopeRef::get(cast<DIScope>(Context)), Name, LinkageName, F,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000709 LineNo, Ty, isLocalToUnit, isDefinition, LineNo,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000710 DITypeRef::get(VTableHolder), VK, VIndex, Flags, isOptimized, Fn,
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000711 cast_or_null<MDTuple>(TParam), nullptr, nullptr);
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000712
David Blaikie595eb442013-02-18 07:10:22 +0000713 if (isDefinition)
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000714 AllSubprograms.push_back(SP);
715 trackIfUnresolved(SP);
716 return SP;
Devang Patelb68c6232010-12-08 20:42:44 +0000717}
718
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000719DINamespace *DIBuilder::createNameSpace(DIScope *Scope, StringRef Name,
720 DIFile *File, unsigned LineNo) {
721 return DINamespace::get(VMContext, getNonCompileUnitScope(Scope), File, Name,
Duncan P. N. Exon Smitha5099dc2015-04-06 19:49:39 +0000722 LineNo);
Devang Patel746660f2010-12-07 23:25:47 +0000723}
724
Adrian Prantlab1243f2015-06-29 23:03:47 +0000725DIModule *DIBuilder::createModule(DIScope *Scope, StringRef Name,
726 StringRef ConfigurationMacros,
727 StringRef IncludePath,
728 StringRef ISysRoot) {
729 return DIModule::get(VMContext, getNonCompileUnitScope(Scope), Name,
730 ConfigurationMacros, IncludePath, ISysRoot);
731}
732
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000733DILexicalBlockFile *DIBuilder::createLexicalBlockFile(DIScope *Scope,
734 DIFile *File,
735 unsigned Discriminator) {
736 return DILexicalBlockFile::get(VMContext, Scope, File, Discriminator);
Eric Christopher6647b832011-10-11 22:59:11 +0000737}
738
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000739DILexicalBlock *DIBuilder::createLexicalBlock(DIScope *Scope, DIFile *File,
740 unsigned Line, unsigned Col) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000741 // Make these distinct, to avoid merging two lexical blocks on the same
742 // file/line/column.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000743 return DILexicalBlock::getDistinct(VMContext, getNonCompileUnitScope(Scope),
Duncan P. N. Exon Smith35ef22c2015-04-15 23:19:27 +0000744 File, Line, Col);
Devang Patel89ea4f22010-12-08 01:50:15 +0000745}
746
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000747static Value *getDbgIntrinsicValueImpl(LLVMContext &VMContext, Value *V) {
748 assert(V && "no value passed to dbg intrinsic");
749 return MetadataAsValue::get(VMContext, ValueAsMetadata::get(V));
750}
751
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000752static Instruction *withDebugLoc(Instruction *I, const DILocation *DL) {
753 I->setDebugLoc(const_cast<DILocation *>(DL));
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000754 return I;
755}
756
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000757Instruction *DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
758 DIExpression *Expr, const DILocation *DL,
Devang Patel746660f2010-12-07 23:25:47 +0000759 Instruction *InsertBefore) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000760 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.declare");
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000761 assert(DL && "Expected debug loc");
762 assert(DL->getScope()->getSubprogram() ==
763 VarInfo->getScope()->getSubprogram() &&
764 "Expected matching subprograms");
Devang Patel746660f2010-12-07 23:25:47 +0000765 if (!DeclareFn)
766 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
767
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000768 trackIfUnresolved(VarInfo);
769 trackIfUnresolved(Expr);
770 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, Storage),
771 MetadataAsValue::get(VMContext, VarInfo),
772 MetadataAsValue::get(VMContext, Expr)};
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000773 return withDebugLoc(CallInst::Create(DeclareFn, Args, "", InsertBefore), DL);
Devang Patel746660f2010-12-07 23:25:47 +0000774}
775
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000776Instruction *DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
777 DIExpression *Expr, const DILocation *DL,
Devang Patel746660f2010-12-07 23:25:47 +0000778 BasicBlock *InsertAtEnd) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000779 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.declare");
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000780 assert(DL && "Expected debug loc");
781 assert(DL->getScope()->getSubprogram() ==
782 VarInfo->getScope()->getSubprogram() &&
783 "Expected matching subprograms");
Devang Patel746660f2010-12-07 23:25:47 +0000784 if (!DeclareFn)
785 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
786
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000787 trackIfUnresolved(VarInfo);
788 trackIfUnresolved(Expr);
789 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, Storage),
790 MetadataAsValue::get(VMContext, VarInfo),
791 MetadataAsValue::get(VMContext, Expr)};
Devang Patel746660f2010-12-07 23:25:47 +0000792
793 // If this block already has a terminator then insert this intrinsic
794 // before the terminator.
795 if (TerminatorInst *T = InsertAtEnd->getTerminator())
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000796 return withDebugLoc(CallInst::Create(DeclareFn, Args, "", T), DL);
797 return withDebugLoc(CallInst::Create(DeclareFn, Args, "", InsertAtEnd), DL);
Devang Patel746660f2010-12-07 23:25:47 +0000798}
799
Devang Patel9b412732011-02-22 18:56:12 +0000800Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000801 DILocalVariable *VarInfo,
802 DIExpression *Expr,
803 const DILocation *DL,
Devang Patel746660f2010-12-07 23:25:47 +0000804 Instruction *InsertBefore) {
805 assert(V && "no value passed to dbg.value");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000806 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.value");
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000807 assert(DL && "Expected debug loc");
808 assert(DL->getScope()->getSubprogram() ==
809 VarInfo->getScope()->getSubprogram() &&
810 "Expected matching subprograms");
Devang Patel746660f2010-12-07 23:25:47 +0000811 if (!ValueFn)
812 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
813
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000814 trackIfUnresolved(VarInfo);
815 trackIfUnresolved(Expr);
816 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, V),
817 ConstantInt::get(Type::getInt64Ty(VMContext), Offset),
818 MetadataAsValue::get(VMContext, VarInfo),
819 MetadataAsValue::get(VMContext, Expr)};
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000820 return withDebugLoc(CallInst::Create(ValueFn, Args, "", InsertBefore), DL);
Devang Patel746660f2010-12-07 23:25:47 +0000821}
822
Devang Patel9b412732011-02-22 18:56:12 +0000823Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000824 DILocalVariable *VarInfo,
825 DIExpression *Expr,
826 const DILocation *DL,
Devang Patel746660f2010-12-07 23:25:47 +0000827 BasicBlock *InsertAtEnd) {
828 assert(V && "no value passed to dbg.value");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000829 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.value");
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000830 assert(DL && "Expected debug loc");
831 assert(DL->getScope()->getSubprogram() ==
832 VarInfo->getScope()->getSubprogram() &&
833 "Expected matching subprograms");
Devang Patel746660f2010-12-07 23:25:47 +0000834 if (!ValueFn)
835 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
836
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000837 trackIfUnresolved(VarInfo);
838 trackIfUnresolved(Expr);
839 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, V),
840 ConstantInt::get(Type::getInt64Ty(VMContext), Offset),
841 MetadataAsValue::get(VMContext, VarInfo),
842 MetadataAsValue::get(VMContext, Expr)};
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000843
844 return withDebugLoc(CallInst::Create(ValueFn, Args, "", InsertAtEnd), DL);
Devang Patel746660f2010-12-07 23:25:47 +0000845}
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000846
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000847void DIBuilder::replaceVTableHolder(DICompositeType *&T,
848 DICompositeType *VTableHolder) {
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000849 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000850 TypedTrackingMDRef<DICompositeType> N(T);
851 N->replaceVTableHolder(DITypeRef::get(VTableHolder));
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000852 T = N.get();
853 }
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000854
855 // If this didn't create a self-reference, just return.
856 if (T != VTableHolder)
857 return;
858
Adrian Prantl18a25b02015-02-11 17:45:10 +0000859 // Look for unresolved operands. T will drop RAUW support, orphaning any
860 // cycles underneath it.
861 if (T->isResolved())
862 for (const MDOperand &O : T->operands())
863 if (auto *N = dyn_cast_or_null<MDNode>(O))
864 trackIfUnresolved(N);
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000865}
866
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000867void DIBuilder::replaceArrays(DICompositeType *&T, DINodeArray Elements,
868 DINodeArray TParams) {
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000869 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000870 TypedTrackingMDRef<DICompositeType> N(T);
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000871 if (Elements)
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000872 N->replaceElements(Elements);
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000873 if (TParams)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000874 N->replaceTemplateParams(DITemplateParameterArray(TParams));
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000875 T = N.get();
876 }
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000877
878 // If T isn't resolved, there's no problem.
879 if (!T->isResolved())
880 return;
881
Adrian Prantl12d52842015-07-10 23:26:02 +0000882 // 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 +0000883 // arrays explicitly if they're unresolved, or else the cycles will be
884 // orphaned.
885 if (Elements)
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +0000886 trackIfUnresolved(Elements.get());
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000887 if (TParams)
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +0000888 trackIfUnresolved(TParams.get());
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000889}