blob: 5c9a3c405755b0d9468cd98696525165938ae321 [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 Smith176b6912014-10-03 20:01:09 +000027namespace {
28class HeaderBuilder {
Duncan P. N. Exon Smithaa687a32015-01-20 05:02:42 +000029 /// \brief Whether there are any fields yet.
30 ///
31 /// Note that this is not equivalent to \c Chars.empty(), since \a concat()
32 /// may have been called already with an empty string.
33 bool IsEmpty;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +000034 SmallVector<char, 256> Chars;
35
36public:
Duncan P. N. Exon Smithaa687a32015-01-20 05:02:42 +000037 HeaderBuilder() : IsEmpty(true) {}
38 HeaderBuilder(const HeaderBuilder &X) : IsEmpty(X.IsEmpty), Chars(X.Chars) {}
39 HeaderBuilder(HeaderBuilder &&X)
40 : IsEmpty(X.IsEmpty), Chars(std::move(X.Chars)) {}
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +000041
42 template <class Twineable> HeaderBuilder &concat(Twineable &&X) {
Duncan P. N. Exon Smithaa687a32015-01-20 05:02:42 +000043 if (IsEmpty)
44 IsEmpty = false;
45 else
46 Chars.push_back(0);
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +000047 Twine(X).toVector(Chars);
48 return *this;
49 }
50
51 MDString *get(LLVMContext &Context) const {
52 return MDString::get(Context, StringRef(Chars.begin(), Chars.size()));
53 }
54
55 static HeaderBuilder get(unsigned Tag) {
Duncan P. N. Exon Smithaa687a32015-01-20 05:02:42 +000056 return HeaderBuilder().concat("0x" + Twine::utohexstr(Tag));
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +000057 }
58};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000059}
Devang Patel63f83cd2010-12-07 23:58:00 +000060
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000061DIBuilder::DIBuilder(Module &m, bool AllowUnresolvedNodes)
Adrian Prantl18c073a2015-07-02 22:32:52 +000062 : M(m), VMContext(M.getContext()), CUNode(nullptr),
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000063 DeclareFn(nullptr), ValueFn(nullptr),
64 AllowUnresolvedNodes(AllowUnresolvedNodes) {}
65
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000066void DIBuilder::trackIfUnresolved(MDNode *N) {
Duncan P. N. Exon Smith9b1c6d32015-01-19 19:09:14 +000067 if (!N)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000068 return;
Duncan P. N. Exon Smith9b1c6d32015-01-19 19:09:14 +000069 if (N->isResolved())
70 return;
71
72 assert(AllowUnresolvedNodes && "Cannot handle unresolved nodes");
73 UnresolvedNodes.emplace_back(N);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000074}
Devang Patel57c5a202010-11-04 15:01:38 +000075
Devang Patel2b8acaf2011-08-15 23:00:00 +000076void DIBuilder::finalize() {
Adrian Prantl0e224a62015-07-06 16:22:12 +000077 if (!CUNode) {
78 assert(!AllowUnresolvedNodes &&
79 "creating type nodes without a CU is not supported");
80 return;
Devang Patel59e27c52011-08-19 23:28:12 +000081 }
Devang Pateleb1bb4e2011-08-16 22:09:43 +000082
Adrian Prantl0e224a62015-07-06 16:22:12 +000083 CUNode->replaceEnumTypes(MDTuple::get(VMContext, AllEnumTypes));
84
85 SmallVector<Metadata *, 16> RetainValues;
86 // Declarations and definitions of the same type may be retained. Some
87 // clients RAUW these pairs, leaving duplicates in the retained types
88 // list. Use a set to remove the duplicates while we transform the
89 // TrackingVHs back into Values.
90 SmallPtrSet<Metadata *, 16> RetainSet;
91 for (unsigned I = 0, E = AllRetainTypes.size(); I < E; I++)
92 if (RetainSet.insert(AllRetainTypes[I]).second)
93 RetainValues.push_back(AllRetainTypes[I]);
Adrian Prantl4276d4a2015-07-06 16:36:02 +000094
95 if (!RetainValues.empty())
96 CUNode->replaceRetainedTypes(MDTuple::get(VMContext, RetainValues));
Adrian Prantl0e224a62015-07-06 16:22:12 +000097
98 DISubprogramArray SPs = MDTuple::get(VMContext, AllSubprograms);
Adrian Prantl75819ae2016-04-15 15:57:41 +000099 auto resolveVariables = [&](DISubprogram *SP) {
Adrian Prantl0e224a62015-07-06 16:22:12 +0000100 if (MDTuple *Temp = SP->getVariables().get()) {
101 const auto &PV = PreservedVariables.lookup(SP);
102 SmallVector<Metadata *, 4> Variables(PV.begin(), PV.end());
103 DINodeArray AV = getOrCreateArray(Variables);
104 TempMDTuple(Temp)->replaceAllUsesWith(AV.get());
105 }
Adrian Prantl75819ae2016-04-15 15:57:41 +0000106 };
107 for (auto *SP : SPs)
108 resolveVariables(SP);
109 for (auto *N : RetainValues)
110 if (auto *SP = dyn_cast<DISubprogram>(N))
111 resolveVariables(SP);
Adrian Prantl0e224a62015-07-06 16:22:12 +0000112
Adrian Prantl4276d4a2015-07-06 16:36:02 +0000113 if (!AllGVs.empty())
114 CUNode->replaceGlobalVariables(MDTuple::get(VMContext, AllGVs));
Adrian Prantl0e224a62015-07-06 16:22:12 +0000115
Adrian Prantl4276d4a2015-07-06 16:36:02 +0000116 if (!AllImportedModules.empty())
117 CUNode->replaceImportedEntities(MDTuple::get(
118 VMContext, SmallVector<Metadata *, 16>(AllImportedModules.begin(),
119 AllImportedModules.end())));
Adrian Prantl0e224a62015-07-06 16:22:12 +0000120
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000121 // Now that all temp nodes have been replaced or deleted, resolve remaining
122 // cycles.
123 for (const auto &N : UnresolvedNodes)
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +0000124 if (N && !N->isResolved())
125 N->resolveCycles();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000126 UnresolvedNodes.clear();
127
128 // Can't handle unresolved nodes anymore.
129 AllowUnresolvedNodes = false;
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000130}
131
Duncan P. N. Exon Smith379e3752014-10-01 21:32:15 +0000132/// If N is compile unit return NULL otherwise return N.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000133static DIScope *getNonCompileUnitScope(DIScope *N) {
134 if (!N || isa<DICompileUnit>(N))
Craig Topperc6207612014-04-09 06:08:46 +0000135 return nullptr;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000136 return cast<DIScope>(N);
Devang Patel2b8acaf2011-08-15 23:00:00 +0000137}
138
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000139DICompileUnit *DIBuilder::createCompileUnit(
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000140 unsigned Lang, StringRef Filename, StringRef Directory, StringRef Producer,
141 bool isOptimized, StringRef Flags, unsigned RunTimeVer, StringRef SplitName,
Adrian Prantl5992a722016-04-08 22:43:03 +0000142 DICompileUnit::DebugEmissionKind Kind, uint64_t DWOId) {
Eric Christopher75d49db2014-02-27 01:24:56 +0000143
Bruce Mitchener7e575ed2015-02-07 06:35:30 +0000144 assert(((Lang <= dwarf::DW_LANG_Fortran08 && Lang >= dwarf::DW_LANG_C89) ||
Chandler Carruth4c0ee742012-01-10 18:18:52 +0000145 (Lang <= dwarf::DW_LANG_hi_user && Lang >= dwarf::DW_LANG_lo_user)) &&
146 "Invalid Language tag");
147 assert(!Filename.empty() &&
148 "Unable to create compile unit without filename");
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000149
Adrian Prantl18c073a2015-07-02 22:32:52 +0000150 assert(!CUNode && "Can only make one compile unit per DIBuilder instance");
151 CUNode = DICompileUnit::getDistinct(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000152 VMContext, Lang, DIFile::get(VMContext, Filename, Directory), Producer,
Adrian Prantl75819ae2016-04-15 15:57:41 +0000153 isOptimized, Flags, RunTimeVer, SplitName, Kind, nullptr, nullptr,
154 nullptr, nullptr, nullptr, DWOId);
Devang Patel09fa69e2011-05-03 16:18:28 +0000155
156 // Create a named metadata so that it is easier to find cu in a module.
Adrian Prantl5992a722016-04-08 22:43:03 +0000157 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
158 NMD->addOperand(CUNode);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000159 trackIfUnresolved(CUNode);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000160 return CUNode;
Devang Patel57c5a202010-11-04 15:01:38 +0000161}
162
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000163static DIImportedEntity *
164createImportedModule(LLVMContext &C, dwarf::Tag Tag, DIScope *Context,
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000165 Metadata *NS, unsigned Line, StringRef Name,
166 SmallVectorImpl<TrackingMDNodeRef> &AllImportedModules) {
Amjad Aboud62f6f5c2016-03-13 11:11:39 +0000167 unsigned EntitiesCount = C.pImpl->DIImportedEntitys.size();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000168 auto *M = DIImportedEntity::get(C, Tag, Context, DINodeRef(NS), Line, Name);
Amjad Aboud62f6f5c2016-03-13 11:11:39 +0000169 if (EntitiesCount < C.pImpl->DIImportedEntitys.size())
170 // A new Imported Entity was just added to the context.
171 // Add it to the Imported Modules list.
172 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
Keno Fischerb011c632015-11-16 07:57:32 +0000258DIDerivedType *DIBuilder::createReferenceType(unsigned Tag, DIType *RTy,
259 uint64_t SizeInBits,
260 uint64_t AlignInBits) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000261 assert(RTy && "Unable to create reference type");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000262 return DIDerivedType::get(VMContext, Tag, "", nullptr, 0, nullptr,
Keno Fischerb011c632015-11-16 07:57:32 +0000263 DITypeRef::get(RTy), SizeInBits, AlignInBits, 0, 0);
Devang Patel57c5a202010-11-04 15:01:38 +0000264}
265
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000266DIDerivedType *DIBuilder::createTypedef(DIType *Ty, StringRef Name,
267 DIFile *File, unsigned LineNo,
268 DIScope *Context) {
269 return DIDerivedType::get(VMContext, dwarf::DW_TAG_typedef, Name, File,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000270 LineNo,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000271 DIScopeRef::get(getNonCompileUnitScope(Context)),
272 DITypeRef::get(Ty), 0, 0, 0, 0);
Devang Patel57c5a202010-11-04 15:01:38 +0000273}
274
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000275DIDerivedType *DIBuilder::createFriend(DIType *Ty, DIType *FriendTy) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000276 assert(Ty && "Invalid type!");
277 assert(FriendTy && "Invalid friend type!");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000278 return DIDerivedType::get(VMContext, dwarf::DW_TAG_friend, "", nullptr, 0,
279 DITypeRef::get(Ty), DITypeRef::get(FriendTy), 0, 0,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000280 0, 0);
Devang Patel57c5a202010-11-04 15:01:38 +0000281}
282
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000283DIDerivedType *DIBuilder::createInheritance(DIType *Ty, DIType *BaseTy,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000284 uint64_t BaseOffset,
285 unsigned Flags) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000286 assert(Ty && "Unable to create inheritance");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000287 return DIDerivedType::get(VMContext, dwarf::DW_TAG_inheritance, "", nullptr,
288 0, DITypeRef::get(Ty), DITypeRef::get(BaseTy), 0, 0,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000289 BaseOffset, Flags);
Devang Patel57c5a202010-11-04 15:01:38 +0000290}
291
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000292DIDerivedType *DIBuilder::createMemberType(DIScope *Scope, StringRef Name,
293 DIFile *File, unsigned LineNumber,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000294 uint64_t SizeInBits,
295 uint64_t AlignInBits,
296 uint64_t OffsetInBits,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000297 unsigned Flags, DIType *Ty) {
298 return DIDerivedType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000299 VMContext, dwarf::DW_TAG_member, Name, File, LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000300 DIScopeRef::get(getNonCompileUnitScope(Scope)), DITypeRef::get(Ty),
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000301 SizeInBits, AlignInBits, OffsetInBits, Flags);
Devang Patel57c5a202010-11-04 15:01:38 +0000302}
303
Duncan P. N. Exon Smith3cd2cab2015-03-27 00:34:10 +0000304static ConstantAsMetadata *getConstantOrNull(Constant *C) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000305 if (C)
306 return ConstantAsMetadata::get(C);
307 return nullptr;
308}
309
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000310DIDerivedType *DIBuilder::createStaticMemberType(DIScope *Scope, StringRef Name,
311 DIFile *File,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000312 unsigned LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000313 DIType *Ty, unsigned Flags,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000314 llvm::Constant *Val) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000315 Flags |= DINode::FlagStaticMember;
316 return DIDerivedType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000317 VMContext, dwarf::DW_TAG_member, Name, File, LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000318 DIScopeRef::get(getNonCompileUnitScope(Scope)), DITypeRef::get(Ty), 0, 0,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000319 0, Flags, getConstantOrNull(Val));
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000320}
321
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000322DIDerivedType *DIBuilder::createObjCIVar(StringRef Name, DIFile *File,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000323 unsigned LineNumber,
324 uint64_t SizeInBits,
325 uint64_t AlignInBits,
326 uint64_t OffsetInBits, unsigned Flags,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000327 DIType *Ty, MDNode *PropertyNode) {
328 return DIDerivedType::get(
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000329 VMContext, dwarf::DW_TAG_member, Name, File, LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000330 DIScopeRef::get(getNonCompileUnitScope(File)), DITypeRef::get(Ty),
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000331 SizeInBits, AlignInBits, OffsetInBits, Flags, PropertyNode);
Devang Patel44882172012-02-06 17:49:43 +0000332}
333
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000334DIObjCProperty *
335DIBuilder::createObjCProperty(StringRef Name, DIFile *File, unsigned LineNumber,
Eric Christopher98f9c232013-10-15 23:31:31 +0000336 StringRef GetterName, StringRef SetterName,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000337 unsigned PropertyAttributes, DIType *Ty) {
338 return DIObjCProperty::get(VMContext, Name, File, LineNumber, GetterName,
Adrian Prantl8ff53b32015-06-15 23:18:03 +0000339 SetterName, PropertyAttributes,
340 DITypeRef::get(Ty));
Devang Patelcc481592012-02-04 00:59:25 +0000341}
342
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000343DITemplateTypeParameter *
344DIBuilder::createTemplateTypeParameter(DIScope *Context, StringRef Name,
345 DIType *Ty) {
346 assert((!Context || isa<DICompileUnit>(Context)) && "Expected compile unit");
347 return DITemplateTypeParameter::get(VMContext, Name, DITypeRef::get(Ty));
Devang Patel3a9e65e2011-02-02 21:38:25 +0000348}
349
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000350static DITemplateValueParameter *
Duncan P. N. Exon Smithb4aa16f2015-02-13 03:35:29 +0000351createTemplateValueParameterHelper(LLVMContext &VMContext, unsigned Tag,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000352 DIScope *Context, StringRef Name, DIType *Ty,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000353 Metadata *MD) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000354 assert((!Context || isa<DICompileUnit>(Context)) && "Expected compile unit");
355 return DITemplateValueParameter::get(VMContext, Tag, Name, DITypeRef::get(Ty),
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000356 MD);
Devang Patelbe933b42011-02-02 22:35:53 +0000357}
358
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000359DITemplateValueParameter *
360DIBuilder::createTemplateValueParameter(DIScope *Context, StringRef Name,
361 DIType *Ty, Constant *Val) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000362 return createTemplateValueParameterHelper(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000363 VMContext, dwarf::DW_TAG_template_value_parameter, Context, Name, Ty,
Duncan P. N. Exon Smithb4aa16f2015-02-13 03:35:29 +0000364 getConstantOrNull(Val));
David Blaikie2b380232013-06-22 18:59:11 +0000365}
366
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000367DITemplateValueParameter *
368DIBuilder::createTemplateTemplateParameter(DIScope *Context, StringRef Name,
369 DIType *Ty, StringRef Val) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000370 return createTemplateValueParameterHelper(
371 VMContext, dwarf::DW_TAG_GNU_template_template_param, Context, Name, Ty,
Duncan P. N. Exon Smithb4aa16f2015-02-13 03:35:29 +0000372 MDString::get(VMContext, Val));
David Blaikie2b380232013-06-22 18:59:11 +0000373}
374
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000375DITemplateValueParameter *
376DIBuilder::createTemplateParameterPack(DIScope *Context, StringRef Name,
377 DIType *Ty, DINodeArray Val) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000378 return createTemplateValueParameterHelper(
379 VMContext, dwarf::DW_TAG_GNU_template_parameter_pack, Context, Name, Ty,
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +0000380 Val.get());
David Blaikie2b380232013-06-22 18:59:11 +0000381}
382
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000383DICompositeType *DIBuilder::createClassType(
384 DIScope *Context, StringRef Name, DIFile *File, unsigned LineNumber,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000385 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000386 unsigned Flags, DIType *DerivedFrom, DINodeArray Elements,
387 DIType *VTableHolder, MDNode *TemplateParams, StringRef UniqueIdentifier) {
388 assert((!Context || isa<DIScope>(Context)) &&
David Blaikie085abe32013-03-11 23:21:19 +0000389 "createClassType should be called with a valid Context");
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000390
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000391 auto *R = DICompositeType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000392 VMContext, dwarf::DW_TAG_structure_type, Name, File, LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000393 DIScopeRef::get(getNonCompileUnitScope(Context)),
394 DITypeRef::get(DerivedFrom), SizeInBits, AlignInBits, OffsetInBits, Flags,
395 Elements, 0, DITypeRef::get(VTableHolder),
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000396 cast_or_null<MDTuple>(TemplateParams), UniqueIdentifier);
Manman Ren0b410402013-08-29 23:17:54 +0000397 if (!UniqueIdentifier.empty())
398 retainType(R);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000399 trackIfUnresolved(R);
David Blaikie085abe32013-03-11 23:21:19 +0000400 return R;
Eric Christopher17426692012-07-06 02:35:57 +0000401}
402
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000403DICompositeType *DIBuilder::createStructType(
404 DIScope *Context, StringRef Name, DIFile *File, unsigned LineNumber,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000405 uint64_t SizeInBits, uint64_t AlignInBits, unsigned Flags,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000406 DIType *DerivedFrom, DINodeArray Elements, unsigned RunTimeLang,
407 DIType *VTableHolder, StringRef UniqueIdentifier) {
408 auto *R = DICompositeType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000409 VMContext, dwarf::DW_TAG_structure_type, Name, File, LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000410 DIScopeRef::get(getNonCompileUnitScope(Context)),
411 DITypeRef::get(DerivedFrom), SizeInBits, AlignInBits, 0, Flags, Elements,
412 RunTimeLang, DITypeRef::get(VTableHolder), nullptr, UniqueIdentifier);
Manman Ren0b410402013-08-29 23:17:54 +0000413 if (!UniqueIdentifier.empty())
414 retainType(R);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000415 trackIfUnresolved(R);
David Blaikie085abe32013-03-11 23:21:19 +0000416 return R;
Devang Patel746660f2010-12-07 23:25:47 +0000417}
418
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000419DICompositeType *DIBuilder::createUnionType(
420 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
Duncan P. N. Exon Smithaa861aa2015-04-21 20:07:38 +0000421 uint64_t SizeInBits, uint64_t AlignInBits, unsigned Flags,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000422 DINodeArray Elements, unsigned RunTimeLang, StringRef UniqueIdentifier) {
423 auto *R = DICompositeType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000424 VMContext, dwarf::DW_TAG_union_type, Name, File, LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000425 DIScopeRef::get(getNonCompileUnitScope(Scope)), nullptr, SizeInBits,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000426 AlignInBits, 0, Flags, Elements, RunTimeLang, nullptr, nullptr,
427 UniqueIdentifier);
Manman Ren0b410402013-08-29 23:17:54 +0000428 if (!UniqueIdentifier.empty())
429 retainType(R);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000430 trackIfUnresolved(R);
Manman Ren0b410402013-08-29 23:17:54 +0000431 return R;
Devang Patel89ea4f22010-12-08 01:50:15 +0000432}
433
Eric Christopherbdafb3c2015-10-15 06:56:10 +0000434DISubroutineType *DIBuilder::createSubroutineType(DITypeRefArray ParameterTypes,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000435 unsigned Flags) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000436 return DISubroutineType::get(VMContext, Flags, ParameterTypes);
Devang Patel89ea4f22010-12-08 01:50:15 +0000437}
438
Adrian Prantlee5feaf2015-07-15 17:01:41 +0000439DICompositeType *DIBuilder::createExternalTypeRef(unsigned Tag, DIFile *File,
440 StringRef UniqueIdentifier) {
441 assert(!UniqueIdentifier.empty() && "external type ref without uid");
442 auto *CTy =
443 DICompositeType::get(VMContext, Tag, "", nullptr, 0, nullptr, nullptr, 0,
444 0, 0, DINode::FlagExternalTypeRef, nullptr, 0,
445 nullptr, nullptr, UniqueIdentifier);
446 // Types with unique IDs need to be in the type map.
447 retainType(CTy);
448 return CTy;
449}
450
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000451DICompositeType *DIBuilder::createEnumerationType(
452 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
453 uint64_t SizeInBits, uint64_t AlignInBits, DINodeArray Elements,
454 DIType *UnderlyingType, StringRef UniqueIdentifier) {
455 auto *CTy = DICompositeType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000456 VMContext, dwarf::DW_TAG_enumeration_type, Name, File, LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000457 DIScopeRef::get(getNonCompileUnitScope(Scope)),
458 DITypeRef::get(UnderlyingType), SizeInBits, AlignInBits, 0, 0, Elements,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000459 0, nullptr, nullptr, UniqueIdentifier);
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000460 AllEnumTypes.push_back(CTy);
Manman Ren0b410402013-08-29 23:17:54 +0000461 if (!UniqueIdentifier.empty())
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000462 retainType(CTy);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000463 trackIfUnresolved(CTy);
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000464 return CTy;
Devang Patel89ea4f22010-12-08 01:50:15 +0000465}
466
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000467DICompositeType *DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
468 DIType *Ty,
469 DINodeArray Subscripts) {
470 auto *R = DICompositeType::get(VMContext, dwarf::DW_TAG_array_type, "",
471 nullptr, 0, nullptr, DITypeRef::get(Ty), Size,
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000472 AlignInBits, 0, 0, Subscripts, 0, nullptr);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000473 trackIfUnresolved(R);
474 return R;
Devang Patel89ea4f22010-12-08 01:50:15 +0000475}
476
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000477DICompositeType *DIBuilder::createVectorType(uint64_t Size,
478 uint64_t AlignInBits, DIType *Ty,
479 DINodeArray Subscripts) {
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000480 auto *R =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000481 DICompositeType::get(VMContext, dwarf::DW_TAG_array_type, "", nullptr, 0,
482 nullptr, DITypeRef::get(Ty), Size, AlignInBits, 0,
483 DINode::FlagVector, Subscripts, 0, nullptr);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000484 trackIfUnresolved(R);
485 return R;
Devang Patel89ea4f22010-12-08 01:50:15 +0000486}
Devang Patel746660f2010-12-07 23:25:47 +0000487
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000488static DIType *createTypeWithFlags(LLVMContext &Context, DIType *Ty,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000489 unsigned FlagsToSet) {
490 auto NewTy = Ty->clone();
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000491 NewTy->setFlags(NewTy->getFlags() | FlagsToSet);
492 return MDNode::replaceWithUniqued(std::move(NewTy));
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000493}
494
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000495DIType *DIBuilder::createArtificialType(DIType *Ty) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000496 // FIXME: Restrict this to the nodes where it's valid.
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000497 if (Ty->isArtificial())
Devang Patel57c5a202010-11-04 15:01:38 +0000498 return Ty;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000499 return createTypeWithFlags(VMContext, Ty, DINode::FlagArtificial);
Devang Patel57c5a202010-11-04 15:01:38 +0000500}
Devang Patel746660f2010-12-07 23:25:47 +0000501
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000502DIType *DIBuilder::createObjectPointerType(DIType *Ty) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000503 // FIXME: Restrict this to the nodes where it's valid.
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000504 if (Ty->isObjectPointer())
Eric Christophere3417762012-09-12 23:36:19 +0000505 return Ty;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000506 unsigned Flags = DINode::FlagObjectPointer | DINode::FlagArtificial;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000507 return createTypeWithFlags(VMContext, Ty, Flags);
Eric Christophere3417762012-09-12 23:36:19 +0000508}
509
Adrian Prantl75819ae2016-04-15 15:57:41 +0000510void DIBuilder::retainType(DIScope *T) {
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000511 assert(T && "Expected non-null type");
Adrian Prantl75819ae2016-04-15 15:57:41 +0000512 assert((isa<DIType>(T) || (isa<DISubprogram>(T) &&
513 cast<DISubprogram>(T)->isDefinition() == false)) &&
514 "Expected type or subprogram declaration");
Duncan P. N. Exon Smithd9ccfb92015-03-27 23:00:49 +0000515 AllRetainTypes.emplace_back(T);
516}
Devang Patel89ea4f22010-12-08 01:50:15 +0000517
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000518DIBasicType *DIBuilder::createUnspecifiedParameter() { return nullptr; }
Devang Patel89ea4f22010-12-08 01:50:15 +0000519
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000520DICompositeType *
521DIBuilder::createForwardDecl(unsigned Tag, StringRef Name, DIScope *Scope,
522 DIFile *F, unsigned Line, unsigned RuntimeLang,
Eric Christopher98f9c232013-10-15 23:31:31 +0000523 uint64_t SizeInBits, uint64_t AlignInBits,
524 StringRef UniqueIdentifier) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000525 // FIXME: Define in terms of createReplaceableForwardDecl() by calling
526 // replaceWithUniqued().
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000527 auto *RetTy = DICompositeType::get(
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000528 VMContext, Tag, Name, F, Line,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000529 DIScopeRef::get(getNonCompileUnitScope(Scope)), nullptr, SizeInBits,
530 AlignInBits, 0, DINode::FlagFwdDecl, nullptr, RuntimeLang, nullptr,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000531 nullptr, UniqueIdentifier);
David Blaikied3f094a2014-05-06 03:41:57 +0000532 if (!UniqueIdentifier.empty())
533 retainType(RetTy);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000534 trackIfUnresolved(RetTy);
David Blaikied3f094a2014-05-06 03:41:57 +0000535 return RetTy;
536}
537
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000538DICompositeType *DIBuilder::createReplaceableCompositeType(
539 unsigned Tag, StringRef Name, DIScope *Scope, DIFile *F, unsigned Line,
David Blaikied3f094a2014-05-06 03:41:57 +0000540 unsigned RuntimeLang, uint64_t SizeInBits, uint64_t AlignInBits,
Adrian Prantl534a81a2015-02-11 17:45:05 +0000541 unsigned Flags, StringRef UniqueIdentifier) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000542 auto *RetTy = DICompositeType::getTemporary(
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000543 VMContext, Tag, Name, F, Line,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000544 DIScopeRef::get(getNonCompileUnitScope(Scope)), nullptr,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000545 SizeInBits, AlignInBits, 0, Flags, nullptr, RuntimeLang,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000546 nullptr, nullptr, UniqueIdentifier)
547 .release();
Manman Ren0b410402013-08-29 23:17:54 +0000548 if (!UniqueIdentifier.empty())
549 retainType(RetTy);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000550 trackIfUnresolved(RetTy);
Manman Rend0e67aa2013-07-02 18:37:35 +0000551 return RetTy;
Eric Christopherae56eec2012-02-08 00:22:26 +0000552}
553
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000554DINodeArray DIBuilder::getOrCreateArray(ArrayRef<Metadata *> Elements) {
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000555 return MDTuple::get(VMContext, Elements);
Devang Patel746660f2010-12-07 23:25:47 +0000556}
557
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000558DITypeRefArray DIBuilder::getOrCreateTypeArray(ArrayRef<Metadata *> Elements) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000559 SmallVector<llvm::Metadata *, 16> Elts;
Manman Ren1a125c92014-07-28 19:33:20 +0000560 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
561 if (Elements[i] && isa<MDNode>(Elements[i]))
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000562 Elts.push_back(DITypeRef::get(cast<DIType>(Elements[i])));
Manman Ren1a125c92014-07-28 19:33:20 +0000563 else
564 Elts.push_back(Elements[i]);
565 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000566 return DITypeRefArray(MDNode::get(VMContext, Elts));
Manman Ren1a125c92014-07-28 19:33:20 +0000567}
568
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000569DISubrange *DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
570 return DISubrange::get(VMContext, Count, Lo);
Devang Patel89ea4f22010-12-08 01:50:15 +0000571}
572
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000573static void checkGlobalVariableScope(DIScope *Context) {
Duncan P. N. Exon Smith430220f2015-04-06 23:34:41 +0000574#ifndef NDEBUG
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000575 if (auto *CT =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000576 dyn_cast_or_null<DICompositeType>(getNonCompileUnitScope(Context)))
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000577 assert(CT->getIdentifier().empty() &&
Manman Renbfd2b8292014-11-21 19:47:48 +0000578 "Context of a global variable should not be a type with identifier");
Duncan P. N. Exon Smith430220f2015-04-06 23:34:41 +0000579#endif
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000580}
581
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000582DIGlobalVariable *DIBuilder::createGlobalVariable(
583 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F,
584 unsigned LineNumber, DIType *Ty, bool isLocalToUnit, Constant *Val,
Duncan P. N. Exon Smithdbf64acd2014-11-15 00:23:49 +0000585 MDNode *Decl) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000586 checkGlobalVariableScope(Context);
587
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000588 auto *N = DIGlobalVariable::get(VMContext, cast_or_null<DIScope>(Context),
Duncan P. N. Exon Smithb273d062015-04-16 01:37:00 +0000589 Name, LinkageName, F, LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000590 DITypeRef::get(Ty), isLocalToUnit, true, Val,
591 cast_or_null<DIDerivedType>(Decl));
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000592 AllGVs.push_back(N);
593 return N;
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000594}
595
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000596DIGlobalVariable *DIBuilder::createTempGlobalVariableFwdDecl(
597 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F,
598 unsigned LineNumber, DIType *Ty, bool isLocalToUnit, Constant *Val,
Duncan P. N. Exon Smithdbf64acd2014-11-15 00:23:49 +0000599 MDNode *Decl) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000600 checkGlobalVariableScope(Context);
601
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000602 return DIGlobalVariable::getTemporary(
603 VMContext, cast_or_null<DIScope>(Context), Name, LinkageName, F,
604 LineNumber, DITypeRef::get(Ty), isLocalToUnit, false, Val,
605 cast_or_null<DIDerivedType>(Decl))
Duncan P. N. Exon Smithb273d062015-04-16 01:37:00 +0000606 .release();
Devang Patel746660f2010-12-07 23:25:47 +0000607}
608
Duncan P. N. Exon Smith1e40dc42015-07-31 17:55:53 +0000609static DILocalVariable *createLocalVariable(
610 LLVMContext &VMContext,
611 DenseMap<MDNode *, std::vector<TrackingMDNodeRef>> &PreservedVariables,
612 DIScope *Scope, StringRef Name, unsigned ArgNo, DIFile *File,
613 unsigned LineNo, DIType *Ty, bool AlwaysPreserve, unsigned Flags) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000614 // FIXME: Why getNonCompileUnitScope()?
615 // FIXME: Why is "!Context" okay here?
Adrian Prantl12d52842015-07-10 23:26:02 +0000616 // FIXME: Why doesn't this check for a subprogram or lexical block (AFAICT
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000617 // the only valid scopes)?
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000618 DIScope *Context = getNonCompileUnitScope(Scope);
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000619
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +0000620 auto *Node =
621 DILocalVariable::get(VMContext, cast_or_null<DILocalScope>(Context), Name,
622 File, LineNo, DITypeRef::get(Ty), ArgNo, Flags);
Devang Patel63f83cd2010-12-07 23:58:00 +0000623 if (AlwaysPreserve) {
Adrian Prantl12d52842015-07-10 23:26:02 +0000624 // The optimizer may remove local variables. If there is an interest
Devang Patel63f83cd2010-12-07 23:58:00 +0000625 // to preserve variable info in such situation then stash it in a
626 // named mdnode.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000627 DISubprogram *Fn = getDISubprogram(Scope);
Duncan P. N. Exon Smith3bfffde2014-10-15 16:11:41 +0000628 assert(Fn && "Missing subprogram for local variable");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000629 PreservedVariables[Fn].emplace_back(Node);
Devang Patel63f83cd2010-12-07 23:58:00 +0000630 }
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000631 return Node;
Devang Patel63f83cd2010-12-07 23:58:00 +0000632}
633
Duncan P. N. Exon Smith1e40dc42015-07-31 17:55:53 +0000634DILocalVariable *DIBuilder::createAutoVariable(DIScope *Scope, StringRef Name,
635 DIFile *File, unsigned LineNo,
636 DIType *Ty, bool AlwaysPreserve,
637 unsigned Flags) {
638 return createLocalVariable(VMContext, PreservedVariables, Scope, Name,
639 /* ArgNo */ 0, File, LineNo, Ty, AlwaysPreserve,
640 Flags);
641}
642
643DILocalVariable *DIBuilder::createParameterVariable(
644 DIScope *Scope, StringRef Name, unsigned ArgNo, DIFile *File,
645 unsigned LineNo, DIType *Ty, bool AlwaysPreserve, unsigned Flags) {
646 assert(ArgNo && "Expected non-zero argument number for parameter");
647 return createLocalVariable(VMContext, PreservedVariables, Scope, Name, ArgNo,
648 File, LineNo, Ty, AlwaysPreserve, Flags);
649}
650
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000651DIExpression *DIBuilder::createExpression(ArrayRef<uint64_t> Addr) {
652 return DIExpression::get(VMContext, Addr);
Devang Patel746660f2010-12-07 23:25:47 +0000653}
654
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000655DIExpression *DIBuilder::createExpression(ArrayRef<int64_t> Signed) {
Duncan P. N. Exon Smithbd75ad42015-02-09 22:13:27 +0000656 // TODO: Remove the callers of this signed version and delete.
657 SmallVector<uint64_t, 8> Addr(Signed.begin(), Signed.end());
658 return createExpression(Addr);
659}
660
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000661DIExpression *DIBuilder::createBitPieceExpression(unsigned OffsetInBytes,
662 unsigned SizeInBytes) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000663 uint64_t Addr[] = {dwarf::DW_OP_bit_piece, OffsetInBytes, SizeInBytes};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000664 return DIExpression::get(VMContext, Addr);
Duncan P. N. Exon Smith9affbba2014-10-01 21:32:12 +0000665}
666
Peter Collingbourned4bff302015-11-05 22:03:56 +0000667DISubprogram *DIBuilder::createFunction(
668 DIScopeRef Context, StringRef Name, StringRef LinkageName, DIFile *File,
669 unsigned LineNo, DISubroutineType *Ty, bool isLocalToUnit,
670 bool isDefinition, unsigned ScopeLine, unsigned Flags, bool isOptimized,
671 DITemplateParameterArray TParams, DISubprogram *Decl) {
Manman Renc50fa112013-10-10 18:40:01 +0000672 // dragonegg does not generate identifier for types, so using an empty map
673 // to resolve the context should be fine.
674 DITypeIdentifierMap EmptyMap;
675 return createFunction(Context.resolve(EmptyMap), Name, LinkageName, File,
676 LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
Peter Collingbourned4bff302015-11-05 22:03:56 +0000677 Flags, isOptimized, TParams, Decl);
Manman Renc50fa112013-10-10 18:40:01 +0000678}
679
Duncan P. N. Exon Smithb2df6472015-08-26 22:50:16 +0000680template <class... Ts>
681static DISubprogram *getSubprogram(bool IsDistinct, Ts &&... Args) {
682 if (IsDistinct)
683 return DISubprogram::getDistinct(std::forward<Ts>(Args)...);
684 return DISubprogram::get(std::forward<Ts>(Args)...);
685}
686
Peter Collingbourned4bff302015-11-05 22:03:56 +0000687DISubprogram *DIBuilder::createFunction(
688 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *File,
689 unsigned LineNo, DISubroutineType *Ty, bool isLocalToUnit,
690 bool isDefinition, unsigned ScopeLine, unsigned Flags, bool isOptimized,
691 DITemplateParameterArray TParams, DISubprogram *Decl) {
Adrian Prantl75819ae2016-04-15 15:57:41 +0000692 auto *Node = getSubprogram(
693 /* IsDistinct = */ isDefinition, VMContext,
694 DIScopeRef::get(getNonCompileUnitScope(Context)), Name, LinkageName, File,
695 LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine, nullptr, 0, 0, Flags,
696 isOptimized, isDefinition ? CUNode : nullptr, TParams, Decl,
697 MDTuple::getTemporary(VMContext, None).release());
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000698
699 if (isDefinition)
700 AllSubprograms.push_back(Node);
701 trackIfUnresolved(Node);
702 return Node;
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000703}
704
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000705DISubprogram *DIBuilder::createTempFunctionFwdDecl(
706 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *File,
707 unsigned LineNo, DISubroutineType *Ty, bool isLocalToUnit,
708 bool isDefinition, unsigned ScopeLine, unsigned Flags, bool isOptimized,
Peter Collingbourned4bff302015-11-05 22:03:56 +0000709 DITemplateParameterArray TParams, DISubprogram *Decl) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000710 return DISubprogram::getTemporary(
711 VMContext, DIScopeRef::get(getNonCompileUnitScope(Context)), Name,
712 LinkageName, File, LineNo, Ty, isLocalToUnit, isDefinition,
Adrian Prantl75819ae2016-04-15 15:57:41 +0000713 ScopeLine, nullptr, 0, 0, Flags, isOptimized,
714 isDefinition ? CUNode : nullptr, TParams, Decl, nullptr)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000715 .release();
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000716}
717
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000718DISubprogram *
719DIBuilder::createMethod(DIScope *Context, StringRef Name, StringRef LinkageName,
720 DIFile *F, unsigned LineNo, DISubroutineType *Ty,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000721 bool isLocalToUnit, bool isDefinition, unsigned VK,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000722 unsigned VIndex, DIType *VTableHolder, unsigned Flags,
Peter Collingbourned4bff302015-11-05 22:03:56 +0000723 bool isOptimized, DITemplateParameterArray TParams) {
Eric Christopher5cb56322013-10-15 23:31:36 +0000724 assert(getNonCompileUnitScope(Context) &&
725 "Methods should have both a Context and a context that isn't "
726 "the compile unit.");
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000727 // FIXME: Do we want to use different scope/lines?
Peter Collingbourned4bff302015-11-05 22:03:56 +0000728 auto *SP = getSubprogram(
729 /* IsDistinct = */ isDefinition, VMContext,
730 DIScopeRef::get(cast<DIScope>(Context)), Name, LinkageName, F, LineNo, Ty,
731 isLocalToUnit, isDefinition, LineNo, DITypeRef::get(VTableHolder), VK,
Adrian Prantl75819ae2016-04-15 15:57:41 +0000732 VIndex, Flags, isOptimized, isDefinition ? CUNode : nullptr, TParams,
733 nullptr, nullptr);
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000734
David Blaikie595eb442013-02-18 07:10:22 +0000735 if (isDefinition)
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000736 AllSubprograms.push_back(SP);
737 trackIfUnresolved(SP);
738 return SP;
Devang Patelb68c6232010-12-08 20:42:44 +0000739}
740
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000741DINamespace *DIBuilder::createNameSpace(DIScope *Scope, StringRef Name,
742 DIFile *File, unsigned LineNo) {
743 return DINamespace::get(VMContext, getNonCompileUnitScope(Scope), File, Name,
Duncan P. N. Exon Smitha5099dc2015-04-06 19:49:39 +0000744 LineNo);
Devang Patel746660f2010-12-07 23:25:47 +0000745}
746
Adrian Prantlab1243f2015-06-29 23:03:47 +0000747DIModule *DIBuilder::createModule(DIScope *Scope, StringRef Name,
748 StringRef ConfigurationMacros,
749 StringRef IncludePath,
750 StringRef ISysRoot) {
751 return DIModule::get(VMContext, getNonCompileUnitScope(Scope), Name,
752 ConfigurationMacros, IncludePath, ISysRoot);
753}
754
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000755DILexicalBlockFile *DIBuilder::createLexicalBlockFile(DIScope *Scope,
756 DIFile *File,
757 unsigned Discriminator) {
758 return DILexicalBlockFile::get(VMContext, Scope, File, Discriminator);
Eric Christopher6647b832011-10-11 22:59:11 +0000759}
760
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000761DILexicalBlock *DIBuilder::createLexicalBlock(DIScope *Scope, DIFile *File,
762 unsigned Line, unsigned Col) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000763 // Make these distinct, to avoid merging two lexical blocks on the same
764 // file/line/column.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000765 return DILexicalBlock::getDistinct(VMContext, getNonCompileUnitScope(Scope),
Duncan P. N. Exon Smith35ef22c2015-04-15 23:19:27 +0000766 File, Line, Col);
Devang Patel89ea4f22010-12-08 01:50:15 +0000767}
768
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000769static Value *getDbgIntrinsicValueImpl(LLVMContext &VMContext, Value *V) {
770 assert(V && "no value passed to dbg intrinsic");
771 return MetadataAsValue::get(VMContext, ValueAsMetadata::get(V));
772}
773
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000774static Instruction *withDebugLoc(Instruction *I, const DILocation *DL) {
775 I->setDebugLoc(const_cast<DILocation *>(DL));
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000776 return I;
777}
778
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000779Instruction *DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
780 DIExpression *Expr, const DILocation *DL,
Devang Patel746660f2010-12-07 23:25:47 +0000781 Instruction *InsertBefore) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000782 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.declare");
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000783 assert(DL && "Expected debug loc");
784 assert(DL->getScope()->getSubprogram() ==
785 VarInfo->getScope()->getSubprogram() &&
786 "Expected matching subprograms");
Devang Patel746660f2010-12-07 23:25:47 +0000787 if (!DeclareFn)
788 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
789
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000790 trackIfUnresolved(VarInfo);
791 trackIfUnresolved(Expr);
792 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, Storage),
793 MetadataAsValue::get(VMContext, VarInfo),
794 MetadataAsValue::get(VMContext, Expr)};
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000795 return withDebugLoc(CallInst::Create(DeclareFn, Args, "", InsertBefore), DL);
Devang Patel746660f2010-12-07 23:25:47 +0000796}
797
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000798Instruction *DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
799 DIExpression *Expr, const DILocation *DL,
Devang Patel746660f2010-12-07 23:25:47 +0000800 BasicBlock *InsertAtEnd) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000801 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.declare");
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000802 assert(DL && "Expected debug loc");
803 assert(DL->getScope()->getSubprogram() ==
804 VarInfo->getScope()->getSubprogram() &&
805 "Expected matching subprograms");
Devang Patel746660f2010-12-07 23:25:47 +0000806 if (!DeclareFn)
807 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
808
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000809 trackIfUnresolved(VarInfo);
810 trackIfUnresolved(Expr);
811 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, Storage),
812 MetadataAsValue::get(VMContext, VarInfo),
813 MetadataAsValue::get(VMContext, Expr)};
Devang Patel746660f2010-12-07 23:25:47 +0000814
815 // If this block already has a terminator then insert this intrinsic
816 // before the terminator.
817 if (TerminatorInst *T = InsertAtEnd->getTerminator())
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000818 return withDebugLoc(CallInst::Create(DeclareFn, Args, "", T), DL);
819 return withDebugLoc(CallInst::Create(DeclareFn, Args, "", InsertAtEnd), DL);
Devang Patel746660f2010-12-07 23:25:47 +0000820}
821
Devang Patel9b412732011-02-22 18:56:12 +0000822Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000823 DILocalVariable *VarInfo,
824 DIExpression *Expr,
825 const DILocation *DL,
Devang Patel746660f2010-12-07 23:25:47 +0000826 Instruction *InsertBefore) {
827 assert(V && "no value passed to dbg.value");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000828 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.value");
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000829 assert(DL && "Expected debug loc");
830 assert(DL->getScope()->getSubprogram() ==
831 VarInfo->getScope()->getSubprogram() &&
832 "Expected matching subprograms");
Devang Patel746660f2010-12-07 23:25:47 +0000833 if (!ValueFn)
834 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
835
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000836 trackIfUnresolved(VarInfo);
837 trackIfUnresolved(Expr);
838 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, V),
839 ConstantInt::get(Type::getInt64Ty(VMContext), Offset),
840 MetadataAsValue::get(VMContext, VarInfo),
841 MetadataAsValue::get(VMContext, Expr)};
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000842 return withDebugLoc(CallInst::Create(ValueFn, Args, "", InsertBefore), DL);
Devang Patel746660f2010-12-07 23:25:47 +0000843}
844
Devang Patel9b412732011-02-22 18:56:12 +0000845Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000846 DILocalVariable *VarInfo,
847 DIExpression *Expr,
848 const DILocation *DL,
Devang Patel746660f2010-12-07 23:25:47 +0000849 BasicBlock *InsertAtEnd) {
850 assert(V && "no value passed to dbg.value");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000851 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.value");
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000852 assert(DL && "Expected debug loc");
853 assert(DL->getScope()->getSubprogram() ==
854 VarInfo->getScope()->getSubprogram() &&
855 "Expected matching subprograms");
Devang Patel746660f2010-12-07 23:25:47 +0000856 if (!ValueFn)
857 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
858
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000859 trackIfUnresolved(VarInfo);
860 trackIfUnresolved(Expr);
861 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, V),
862 ConstantInt::get(Type::getInt64Ty(VMContext), Offset),
863 MetadataAsValue::get(VMContext, VarInfo),
864 MetadataAsValue::get(VMContext, Expr)};
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000865
866 return withDebugLoc(CallInst::Create(ValueFn, Args, "", InsertAtEnd), DL);
Devang Patel746660f2010-12-07 23:25:47 +0000867}
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000868
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000869void DIBuilder::replaceVTableHolder(DICompositeType *&T,
870 DICompositeType *VTableHolder) {
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000871 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000872 TypedTrackingMDRef<DICompositeType> N(T);
873 N->replaceVTableHolder(DITypeRef::get(VTableHolder));
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000874 T = N.get();
875 }
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000876
877 // If this didn't create a self-reference, just return.
878 if (T != VTableHolder)
879 return;
880
Adrian Prantl18a25b02015-02-11 17:45:10 +0000881 // Look for unresolved operands. T will drop RAUW support, orphaning any
882 // cycles underneath it.
883 if (T->isResolved())
884 for (const MDOperand &O : T->operands())
885 if (auto *N = dyn_cast_or_null<MDNode>(O))
886 trackIfUnresolved(N);
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000887}
888
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000889void DIBuilder::replaceArrays(DICompositeType *&T, DINodeArray Elements,
890 DINodeArray TParams) {
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000891 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000892 TypedTrackingMDRef<DICompositeType> N(T);
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000893 if (Elements)
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000894 N->replaceElements(Elements);
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000895 if (TParams)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000896 N->replaceTemplateParams(DITemplateParameterArray(TParams));
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000897 T = N.get();
898 }
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000899
900 // If T isn't resolved, there's no problem.
901 if (!T->isResolved())
902 return;
903
Adrian Prantl12d52842015-07-10 23:26:02 +0000904 // 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 +0000905 // arrays explicitly if they're unresolved, or else the cycles will be
906 // orphaned.
907 if (Elements)
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +0000908 trackIfUnresolved(Elements.get());
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000909 if (TParams)
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +0000910 trackIfUnresolved(TParams.get());
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000911}