blob: e46de241dfcd6e7335739e986cb52787d455a903 [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) {
Duncan P. N. Exon Smitha2495d92016-04-20 20:03:59 +0000100 MDTuple *Temp = SP->getVariables().get();
101 if (!Temp)
102 return;
103
104 SmallVector<Metadata *, 4> Variables;
105
106 auto PV = PreservedVariables.find(SP);
107 if (PV != PreservedVariables.end())
108 Variables.append(PV->second.begin(), PV->second.end());
109
110 DINodeArray AV = getOrCreateArray(Variables);
111 TempMDTuple(Temp)->replaceAllUsesWith(AV.get());
Adrian Prantl75819ae2016-04-15 15:57:41 +0000112 };
113 for (auto *SP : SPs)
114 resolveVariables(SP);
115 for (auto *N : RetainValues)
116 if (auto *SP = dyn_cast<DISubprogram>(N))
117 resolveVariables(SP);
Adrian Prantl0e224a62015-07-06 16:22:12 +0000118
Adrian Prantl4276d4a2015-07-06 16:36:02 +0000119 if (!AllGVs.empty())
120 CUNode->replaceGlobalVariables(MDTuple::get(VMContext, AllGVs));
Adrian Prantl0e224a62015-07-06 16:22:12 +0000121
Adrian Prantl4276d4a2015-07-06 16:36:02 +0000122 if (!AllImportedModules.empty())
123 CUNode->replaceImportedEntities(MDTuple::get(
124 VMContext, SmallVector<Metadata *, 16>(AllImportedModules.begin(),
125 AllImportedModules.end())));
Adrian Prantl0e224a62015-07-06 16:22:12 +0000126
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000127 // Now that all temp nodes have been replaced or deleted, resolve remaining
128 // cycles.
129 for (const auto &N : UnresolvedNodes)
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +0000130 if (N && !N->isResolved())
131 N->resolveCycles();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000132 UnresolvedNodes.clear();
133
134 // Can't handle unresolved nodes anymore.
135 AllowUnresolvedNodes = false;
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000136}
137
Duncan P. N. Exon Smith379e3752014-10-01 21:32:15 +0000138/// If N is compile unit return NULL otherwise return N.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000139static DIScope *getNonCompileUnitScope(DIScope *N) {
140 if (!N || isa<DICompileUnit>(N))
Craig Topperc6207612014-04-09 06:08:46 +0000141 return nullptr;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000142 return cast<DIScope>(N);
Devang Patel2b8acaf2011-08-15 23:00:00 +0000143}
144
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000145DICompileUnit *DIBuilder::createCompileUnit(
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000146 unsigned Lang, StringRef Filename, StringRef Directory, StringRef Producer,
147 bool isOptimized, StringRef Flags, unsigned RunTimeVer, StringRef SplitName,
Adrian Prantl5992a722016-04-08 22:43:03 +0000148 DICompileUnit::DebugEmissionKind Kind, uint64_t DWOId) {
Eric Christopher75d49db2014-02-27 01:24:56 +0000149
Bruce Mitchener7e575ed2015-02-07 06:35:30 +0000150 assert(((Lang <= dwarf::DW_LANG_Fortran08 && Lang >= dwarf::DW_LANG_C89) ||
Chandler Carruth4c0ee742012-01-10 18:18:52 +0000151 (Lang <= dwarf::DW_LANG_hi_user && Lang >= dwarf::DW_LANG_lo_user)) &&
152 "Invalid Language tag");
153 assert(!Filename.empty() &&
154 "Unable to create compile unit without filename");
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000155
Adrian Prantl18c073a2015-07-02 22:32:52 +0000156 assert(!CUNode && "Can only make one compile unit per DIBuilder instance");
157 CUNode = DICompileUnit::getDistinct(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000158 VMContext, Lang, DIFile::get(VMContext, Filename, Directory), Producer,
Adrian Prantl75819ae2016-04-15 15:57:41 +0000159 isOptimized, Flags, RunTimeVer, SplitName, Kind, nullptr, nullptr,
160 nullptr, nullptr, nullptr, DWOId);
Devang Patel09fa69e2011-05-03 16:18:28 +0000161
162 // Create a named metadata so that it is easier to find cu in a module.
Adrian Prantl5992a722016-04-08 22:43:03 +0000163 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
164 NMD->addOperand(CUNode);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000165 trackIfUnresolved(CUNode);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000166 return CUNode;
Devang Patel57c5a202010-11-04 15:01:38 +0000167}
168
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000169static DIImportedEntity *
170createImportedModule(LLVMContext &C, dwarf::Tag Tag, DIScope *Context,
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000171 Metadata *NS, unsigned Line, StringRef Name,
172 SmallVectorImpl<TrackingMDNodeRef> &AllImportedModules) {
Amjad Aboud62f6f5c2016-03-13 11:11:39 +0000173 unsigned EntitiesCount = C.pImpl->DIImportedEntitys.size();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000174 auto *M = DIImportedEntity::get(C, Tag, Context, DINodeRef(NS), Line, Name);
Amjad Aboud62f6f5c2016-03-13 11:11:39 +0000175 if (EntitiesCount < C.pImpl->DIImportedEntitys.size())
176 // A new Imported Entity was just added to the context.
177 // Add it to the Imported Modules list.
178 AllImportedModules.emplace_back(M);
David Blaikie1fd43652013-05-07 21:35:53 +0000179 return M;
180}
181
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000182DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context,
183 DINamespace *NS,
184 unsigned Line) {
David Blaikie2a40c142014-04-06 06:29:01 +0000185 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
186 Context, NS, Line, StringRef(), AllImportedModules);
David Blaikiee63d5d12013-05-20 22:50:35 +0000187}
188
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000189DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context,
190 DIImportedEntity *NS,
191 unsigned Line) {
David Blaikie2a40c142014-04-06 06:29:01 +0000192 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
193 Context, NS, Line, StringRef(), AllImportedModules);
David Blaikiee63d5d12013-05-20 22:50:35 +0000194}
195
Adrian Prantlab1243f2015-06-29 23:03:47 +0000196DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context, DIModule *M,
197 unsigned Line) {
198 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
199 Context, M, Line, StringRef(), AllImportedModules);
200}
201
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000202DIImportedEntity *DIBuilder::createImportedDeclaration(DIScope *Context,
203 DINode *Decl,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000204 unsigned Line,
205 StringRef Name) {
Frederic Riss4aa51ae2014-11-06 17:46:55 +0000206 // Make sure to use the unique identifier based metadata reference for
207 // types that have one.
David Blaikie2a40c142014-04-06 06:29:01 +0000208 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_declaration,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000209 Context, DINodeRef::get(Decl), Line, Name,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000210 AllImportedModules);
David Blaikief55abea2013-04-22 06:12:31 +0000211}
212
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000213DIFile *DIBuilder::createFile(StringRef Filename, StringRef Directory) {
214 return DIFile::get(VMContext, Filename, Directory);
Devang Patel57c5a202010-11-04 15:01:38 +0000215}
216
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000217DIEnumerator *DIBuilder::createEnumerator(StringRef Name, int64_t Val) {
Devang Patel1ad1abe2011-09-12 18:26:08 +0000218 assert(!Name.empty() && "Unable to create enumerator without name");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000219 return DIEnumerator::get(VMContext, Val, Name);
Devang Patel57c5a202010-11-04 15:01:38 +0000220}
221
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000222DIBasicType *DIBuilder::createUnspecifiedType(StringRef Name) {
Devang Patel04d6d472011-09-14 23:13:28 +0000223 assert(!Name.empty() && "Unable to create type without name");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000224 return DIBasicType::get(VMContext, dwarf::DW_TAG_unspecified_type, Name);
Devang Patel04d6d472011-09-14 23:13:28 +0000225}
226
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000227DIBasicType *DIBuilder::createNullPtrType() {
Peter Collingbournea4a47cb2013-06-27 22:50:59 +0000228 return createUnspecifiedType("decltype(nullptr)");
229}
230
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000231DIBasicType *DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000232 uint64_t AlignInBits,
233 unsigned Encoding) {
Devang Patel1ad1abe2011-09-12 18:26:08 +0000234 assert(!Name.empty() && "Unable to create type without name");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000235 return DIBasicType::get(VMContext, dwarf::DW_TAG_base_type, Name, SizeInBits,
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000236 AlignInBits, Encoding);
Devang Patel57c5a202010-11-04 15:01:38 +0000237}
238
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000239DIDerivedType *DIBuilder::createQualifiedType(unsigned Tag, DIType *FromTy) {
240 return DIDerivedType::get(VMContext, Tag, "", nullptr, 0, nullptr,
241 DITypeRef::get(FromTy), 0, 0, 0, 0);
Devang Patel57c5a202010-11-04 15:01:38 +0000242}
243
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000244DIDerivedType *DIBuilder::createPointerType(DIType *PointeeTy,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000245 uint64_t SizeInBits,
246 uint64_t AlignInBits,
247 StringRef Name) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000248 // FIXME: Why is there a name here?
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000249 return DIDerivedType::get(VMContext, dwarf::DW_TAG_pointer_type, Name,
250 nullptr, 0, nullptr, DITypeRef::get(PointeeTy),
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000251 SizeInBits, AlignInBits, 0, 0);
Devang Patel57c5a202010-11-04 15:01:38 +0000252}
253
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000254DIDerivedType *DIBuilder::createMemberPointerType(DIType *PointeeTy,
255 DIType *Base,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000256 uint64_t SizeInBits,
257 uint64_t AlignInBits) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000258 return DIDerivedType::get(VMContext, dwarf::DW_TAG_ptr_to_member_type, "",
259 nullptr, 0, nullptr, DITypeRef::get(PointeeTy),
260 SizeInBits, AlignInBits, 0, 0,
261 DITypeRef::get(Base));
David Blaikie5d3249b2013-01-07 05:51:15 +0000262}
263
Keno Fischerb011c632015-11-16 07:57:32 +0000264DIDerivedType *DIBuilder::createReferenceType(unsigned Tag, DIType *RTy,
265 uint64_t SizeInBits,
266 uint64_t AlignInBits) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000267 assert(RTy && "Unable to create reference type");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000268 return DIDerivedType::get(VMContext, Tag, "", nullptr, 0, nullptr,
Keno Fischerb011c632015-11-16 07:57:32 +0000269 DITypeRef::get(RTy), SizeInBits, AlignInBits, 0, 0);
Devang Patel57c5a202010-11-04 15:01:38 +0000270}
271
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000272DIDerivedType *DIBuilder::createTypedef(DIType *Ty, StringRef Name,
273 DIFile *File, unsigned LineNo,
274 DIScope *Context) {
275 return DIDerivedType::get(VMContext, dwarf::DW_TAG_typedef, Name, File,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000276 LineNo,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000277 DIScopeRef::get(getNonCompileUnitScope(Context)),
278 DITypeRef::get(Ty), 0, 0, 0, 0);
Devang Patel57c5a202010-11-04 15:01:38 +0000279}
280
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000281DIDerivedType *DIBuilder::createFriend(DIType *Ty, DIType *FriendTy) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000282 assert(Ty && "Invalid type!");
283 assert(FriendTy && "Invalid friend type!");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000284 return DIDerivedType::get(VMContext, dwarf::DW_TAG_friend, "", nullptr, 0,
285 DITypeRef::get(Ty), DITypeRef::get(FriendTy), 0, 0,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000286 0, 0);
Devang Patel57c5a202010-11-04 15:01:38 +0000287}
288
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000289DIDerivedType *DIBuilder::createInheritance(DIType *Ty, DIType *BaseTy,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000290 uint64_t BaseOffset,
291 unsigned Flags) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000292 assert(Ty && "Unable to create inheritance");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000293 return DIDerivedType::get(VMContext, dwarf::DW_TAG_inheritance, "", nullptr,
294 0, DITypeRef::get(Ty), DITypeRef::get(BaseTy), 0, 0,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000295 BaseOffset, Flags);
Devang Patel57c5a202010-11-04 15:01:38 +0000296}
297
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000298DIDerivedType *DIBuilder::createMemberType(DIScope *Scope, StringRef Name,
299 DIFile *File, unsigned LineNumber,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000300 uint64_t SizeInBits,
301 uint64_t AlignInBits,
302 uint64_t OffsetInBits,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000303 unsigned Flags, DIType *Ty) {
304 return DIDerivedType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000305 VMContext, dwarf::DW_TAG_member, Name, File, LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000306 DIScopeRef::get(getNonCompileUnitScope(Scope)), DITypeRef::get(Ty),
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000307 SizeInBits, AlignInBits, OffsetInBits, Flags);
Devang Patel57c5a202010-11-04 15:01:38 +0000308}
309
Duncan P. N. Exon Smith3cd2cab2015-03-27 00:34:10 +0000310static ConstantAsMetadata *getConstantOrNull(Constant *C) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000311 if (C)
312 return ConstantAsMetadata::get(C);
313 return nullptr;
314}
315
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000316DIDerivedType *DIBuilder::createStaticMemberType(DIScope *Scope, StringRef Name,
317 DIFile *File,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000318 unsigned LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000319 DIType *Ty, unsigned Flags,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000320 llvm::Constant *Val) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000321 Flags |= DINode::FlagStaticMember;
322 return DIDerivedType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000323 VMContext, dwarf::DW_TAG_member, Name, File, LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000324 DIScopeRef::get(getNonCompileUnitScope(Scope)), DITypeRef::get(Ty), 0, 0,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000325 0, Flags, getConstantOrNull(Val));
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000326}
327
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000328DIDerivedType *DIBuilder::createObjCIVar(StringRef Name, DIFile *File,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000329 unsigned LineNumber,
330 uint64_t SizeInBits,
331 uint64_t AlignInBits,
332 uint64_t OffsetInBits, unsigned Flags,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000333 DIType *Ty, MDNode *PropertyNode) {
334 return DIDerivedType::get(
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000335 VMContext, dwarf::DW_TAG_member, Name, File, LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000336 DIScopeRef::get(getNonCompileUnitScope(File)), DITypeRef::get(Ty),
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000337 SizeInBits, AlignInBits, OffsetInBits, Flags, PropertyNode);
Devang Patel44882172012-02-06 17:49:43 +0000338}
339
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000340DIObjCProperty *
341DIBuilder::createObjCProperty(StringRef Name, DIFile *File, unsigned LineNumber,
Eric Christopher98f9c232013-10-15 23:31:31 +0000342 StringRef GetterName, StringRef SetterName,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000343 unsigned PropertyAttributes, DIType *Ty) {
344 return DIObjCProperty::get(VMContext, Name, File, LineNumber, GetterName,
Adrian Prantl8ff53b32015-06-15 23:18:03 +0000345 SetterName, PropertyAttributes,
346 DITypeRef::get(Ty));
Devang Patelcc481592012-02-04 00:59:25 +0000347}
348
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000349DITemplateTypeParameter *
350DIBuilder::createTemplateTypeParameter(DIScope *Context, StringRef Name,
351 DIType *Ty) {
352 assert((!Context || isa<DICompileUnit>(Context)) && "Expected compile unit");
353 return DITemplateTypeParameter::get(VMContext, Name, DITypeRef::get(Ty));
Devang Patel3a9e65e2011-02-02 21:38:25 +0000354}
355
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000356static DITemplateValueParameter *
Duncan P. N. Exon Smithb4aa16f2015-02-13 03:35:29 +0000357createTemplateValueParameterHelper(LLVMContext &VMContext, unsigned Tag,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000358 DIScope *Context, StringRef Name, DIType *Ty,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000359 Metadata *MD) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000360 assert((!Context || isa<DICompileUnit>(Context)) && "Expected compile unit");
361 return DITemplateValueParameter::get(VMContext, Tag, Name, DITypeRef::get(Ty),
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000362 MD);
Devang Patelbe933b42011-02-02 22:35:53 +0000363}
364
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000365DITemplateValueParameter *
366DIBuilder::createTemplateValueParameter(DIScope *Context, StringRef Name,
367 DIType *Ty, Constant *Val) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000368 return createTemplateValueParameterHelper(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000369 VMContext, dwarf::DW_TAG_template_value_parameter, Context, Name, Ty,
Duncan P. N. Exon Smithb4aa16f2015-02-13 03:35:29 +0000370 getConstantOrNull(Val));
David Blaikie2b380232013-06-22 18:59:11 +0000371}
372
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000373DITemplateValueParameter *
374DIBuilder::createTemplateTemplateParameter(DIScope *Context, StringRef Name,
375 DIType *Ty, StringRef Val) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000376 return createTemplateValueParameterHelper(
377 VMContext, dwarf::DW_TAG_GNU_template_template_param, Context, Name, Ty,
Duncan P. N. Exon Smithb4aa16f2015-02-13 03:35:29 +0000378 MDString::get(VMContext, Val));
David Blaikie2b380232013-06-22 18:59:11 +0000379}
380
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000381DITemplateValueParameter *
382DIBuilder::createTemplateParameterPack(DIScope *Context, StringRef Name,
383 DIType *Ty, DINodeArray Val) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000384 return createTemplateValueParameterHelper(
385 VMContext, dwarf::DW_TAG_GNU_template_parameter_pack, Context, Name, Ty,
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +0000386 Val.get());
David Blaikie2b380232013-06-22 18:59:11 +0000387}
388
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000389DICompositeType *DIBuilder::createClassType(
390 DIScope *Context, StringRef Name, DIFile *File, unsigned LineNumber,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000391 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000392 unsigned Flags, DIType *DerivedFrom, DINodeArray Elements,
393 DIType *VTableHolder, MDNode *TemplateParams, StringRef UniqueIdentifier) {
394 assert((!Context || isa<DIScope>(Context)) &&
David Blaikie085abe32013-03-11 23:21:19 +0000395 "createClassType should be called with a valid Context");
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000396
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000397 auto *R = DICompositeType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000398 VMContext, dwarf::DW_TAG_structure_type, Name, File, LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000399 DIScopeRef::get(getNonCompileUnitScope(Context)),
400 DITypeRef::get(DerivedFrom), SizeInBits, AlignInBits, OffsetInBits, Flags,
401 Elements, 0, DITypeRef::get(VTableHolder),
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000402 cast_or_null<MDTuple>(TemplateParams), UniqueIdentifier);
Manman Ren0b410402013-08-29 23:17:54 +0000403 if (!UniqueIdentifier.empty())
404 retainType(R);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000405 trackIfUnresolved(R);
David Blaikie085abe32013-03-11 23:21:19 +0000406 return R;
Eric Christopher17426692012-07-06 02:35:57 +0000407}
408
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000409DICompositeType *DIBuilder::createStructType(
410 DIScope *Context, StringRef Name, DIFile *File, unsigned LineNumber,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000411 uint64_t SizeInBits, uint64_t AlignInBits, unsigned Flags,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000412 DIType *DerivedFrom, DINodeArray Elements, unsigned RunTimeLang,
413 DIType *VTableHolder, StringRef UniqueIdentifier) {
414 auto *R = DICompositeType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000415 VMContext, dwarf::DW_TAG_structure_type, Name, File, LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000416 DIScopeRef::get(getNonCompileUnitScope(Context)),
417 DITypeRef::get(DerivedFrom), SizeInBits, AlignInBits, 0, Flags, Elements,
418 RunTimeLang, DITypeRef::get(VTableHolder), nullptr, UniqueIdentifier);
Manman Ren0b410402013-08-29 23:17:54 +0000419 if (!UniqueIdentifier.empty())
420 retainType(R);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000421 trackIfUnresolved(R);
David Blaikie085abe32013-03-11 23:21:19 +0000422 return R;
Devang Patel746660f2010-12-07 23:25:47 +0000423}
424
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000425DICompositeType *DIBuilder::createUnionType(
426 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
Duncan P. N. Exon Smithaa861aa2015-04-21 20:07:38 +0000427 uint64_t SizeInBits, uint64_t AlignInBits, unsigned Flags,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000428 DINodeArray Elements, unsigned RunTimeLang, StringRef UniqueIdentifier) {
429 auto *R = DICompositeType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000430 VMContext, dwarf::DW_TAG_union_type, Name, File, LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000431 DIScopeRef::get(getNonCompileUnitScope(Scope)), nullptr, SizeInBits,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000432 AlignInBits, 0, Flags, Elements, RunTimeLang, nullptr, nullptr,
433 UniqueIdentifier);
Manman Ren0b410402013-08-29 23:17:54 +0000434 if (!UniqueIdentifier.empty())
435 retainType(R);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000436 trackIfUnresolved(R);
Manman Ren0b410402013-08-29 23:17:54 +0000437 return R;
Devang Patel89ea4f22010-12-08 01:50:15 +0000438}
439
Eric Christopherbdafb3c2015-10-15 06:56:10 +0000440DISubroutineType *DIBuilder::createSubroutineType(DITypeRefArray ParameterTypes,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000441 unsigned Flags) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000442 return DISubroutineType::get(VMContext, Flags, ParameterTypes);
Devang Patel89ea4f22010-12-08 01:50:15 +0000443}
444
Adrian Prantlee5feaf2015-07-15 17:01:41 +0000445DICompositeType *DIBuilder::createExternalTypeRef(unsigned Tag, DIFile *File,
446 StringRef UniqueIdentifier) {
447 assert(!UniqueIdentifier.empty() && "external type ref without uid");
448 auto *CTy =
449 DICompositeType::get(VMContext, Tag, "", nullptr, 0, nullptr, nullptr, 0,
450 0, 0, DINode::FlagExternalTypeRef, nullptr, 0,
451 nullptr, nullptr, UniqueIdentifier);
452 // Types with unique IDs need to be in the type map.
453 retainType(CTy);
454 return CTy;
455}
456
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000457DICompositeType *DIBuilder::createEnumerationType(
458 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
459 uint64_t SizeInBits, uint64_t AlignInBits, DINodeArray Elements,
460 DIType *UnderlyingType, StringRef UniqueIdentifier) {
461 auto *CTy = DICompositeType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000462 VMContext, dwarf::DW_TAG_enumeration_type, Name, File, LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000463 DIScopeRef::get(getNonCompileUnitScope(Scope)),
464 DITypeRef::get(UnderlyingType), SizeInBits, AlignInBits, 0, 0, Elements,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000465 0, nullptr, nullptr, UniqueIdentifier);
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000466 AllEnumTypes.push_back(CTy);
Manman Ren0b410402013-08-29 23:17:54 +0000467 if (!UniqueIdentifier.empty())
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000468 retainType(CTy);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000469 trackIfUnresolved(CTy);
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000470 return CTy;
Devang Patel89ea4f22010-12-08 01:50:15 +0000471}
472
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000473DICompositeType *DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
474 DIType *Ty,
475 DINodeArray Subscripts) {
476 auto *R = DICompositeType::get(VMContext, dwarf::DW_TAG_array_type, "",
477 nullptr, 0, nullptr, DITypeRef::get(Ty), Size,
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000478 AlignInBits, 0, 0, Subscripts, 0, nullptr);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000479 trackIfUnresolved(R);
480 return R;
Devang Patel89ea4f22010-12-08 01:50:15 +0000481}
482
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000483DICompositeType *DIBuilder::createVectorType(uint64_t Size,
484 uint64_t AlignInBits, DIType *Ty,
485 DINodeArray Subscripts) {
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000486 auto *R =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000487 DICompositeType::get(VMContext, dwarf::DW_TAG_array_type, "", nullptr, 0,
488 nullptr, DITypeRef::get(Ty), Size, AlignInBits, 0,
489 DINode::FlagVector, Subscripts, 0, nullptr);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000490 trackIfUnresolved(R);
491 return R;
Devang Patel89ea4f22010-12-08 01:50:15 +0000492}
Devang Patel746660f2010-12-07 23:25:47 +0000493
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000494static DIType *createTypeWithFlags(LLVMContext &Context, DIType *Ty,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000495 unsigned FlagsToSet) {
496 auto NewTy = Ty->clone();
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000497 NewTy->setFlags(NewTy->getFlags() | FlagsToSet);
498 return MDNode::replaceWithUniqued(std::move(NewTy));
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000499}
500
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000501DIType *DIBuilder::createArtificialType(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->isArtificial())
Devang Patel57c5a202010-11-04 15:01:38 +0000504 return Ty;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000505 return createTypeWithFlags(VMContext, Ty, DINode::FlagArtificial);
Devang Patel57c5a202010-11-04 15:01:38 +0000506}
Devang Patel746660f2010-12-07 23:25:47 +0000507
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000508DIType *DIBuilder::createObjectPointerType(DIType *Ty) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000509 // FIXME: Restrict this to the nodes where it's valid.
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000510 if (Ty->isObjectPointer())
Eric Christophere3417762012-09-12 23:36:19 +0000511 return Ty;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000512 unsigned Flags = DINode::FlagObjectPointer | DINode::FlagArtificial;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000513 return createTypeWithFlags(VMContext, Ty, Flags);
Eric Christophere3417762012-09-12 23:36:19 +0000514}
515
Adrian Prantl75819ae2016-04-15 15:57:41 +0000516void DIBuilder::retainType(DIScope *T) {
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000517 assert(T && "Expected non-null type");
Adrian Prantl75819ae2016-04-15 15:57:41 +0000518 assert((isa<DIType>(T) || (isa<DISubprogram>(T) &&
519 cast<DISubprogram>(T)->isDefinition() == false)) &&
520 "Expected type or subprogram declaration");
Duncan P. N. Exon Smithd9ccfb92015-03-27 23:00:49 +0000521 AllRetainTypes.emplace_back(T);
522}
Devang Patel89ea4f22010-12-08 01:50:15 +0000523
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000524DIBasicType *DIBuilder::createUnspecifiedParameter() { return nullptr; }
Devang Patel89ea4f22010-12-08 01:50:15 +0000525
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000526DICompositeType *
527DIBuilder::createForwardDecl(unsigned Tag, StringRef Name, DIScope *Scope,
528 DIFile *F, unsigned Line, unsigned RuntimeLang,
Eric Christopher98f9c232013-10-15 23:31:31 +0000529 uint64_t SizeInBits, uint64_t AlignInBits,
530 StringRef UniqueIdentifier) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000531 // FIXME: Define in terms of createReplaceableForwardDecl() by calling
532 // replaceWithUniqued().
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000533 auto *RetTy = DICompositeType::get(
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000534 VMContext, Tag, Name, F, Line,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000535 DIScopeRef::get(getNonCompileUnitScope(Scope)), nullptr, SizeInBits,
536 AlignInBits, 0, DINode::FlagFwdDecl, nullptr, RuntimeLang, nullptr,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000537 nullptr, UniqueIdentifier);
David Blaikied3f094a2014-05-06 03:41:57 +0000538 if (!UniqueIdentifier.empty())
539 retainType(RetTy);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000540 trackIfUnresolved(RetTy);
David Blaikied3f094a2014-05-06 03:41:57 +0000541 return RetTy;
542}
543
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000544DICompositeType *DIBuilder::createReplaceableCompositeType(
545 unsigned Tag, StringRef Name, DIScope *Scope, DIFile *F, unsigned Line,
David Blaikied3f094a2014-05-06 03:41:57 +0000546 unsigned RuntimeLang, uint64_t SizeInBits, uint64_t AlignInBits,
Adrian Prantl534a81a2015-02-11 17:45:05 +0000547 unsigned Flags, StringRef UniqueIdentifier) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000548 auto *RetTy = DICompositeType::getTemporary(
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000549 VMContext, Tag, Name, F, Line,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000550 DIScopeRef::get(getNonCompileUnitScope(Scope)), nullptr,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000551 SizeInBits, AlignInBits, 0, Flags, nullptr, RuntimeLang,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000552 nullptr, nullptr, UniqueIdentifier)
553 .release();
Manman Ren0b410402013-08-29 23:17:54 +0000554 if (!UniqueIdentifier.empty())
555 retainType(RetTy);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000556 trackIfUnresolved(RetTy);
Manman Rend0e67aa2013-07-02 18:37:35 +0000557 return RetTy;
Eric Christopherae56eec2012-02-08 00:22:26 +0000558}
559
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000560DINodeArray DIBuilder::getOrCreateArray(ArrayRef<Metadata *> Elements) {
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000561 return MDTuple::get(VMContext, Elements);
Devang Patel746660f2010-12-07 23:25:47 +0000562}
563
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000564DITypeRefArray DIBuilder::getOrCreateTypeArray(ArrayRef<Metadata *> Elements) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000565 SmallVector<llvm::Metadata *, 16> Elts;
Manman Ren1a125c92014-07-28 19:33:20 +0000566 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
567 if (Elements[i] && isa<MDNode>(Elements[i]))
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000568 Elts.push_back(DITypeRef::get(cast<DIType>(Elements[i])));
Manman Ren1a125c92014-07-28 19:33:20 +0000569 else
570 Elts.push_back(Elements[i]);
571 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000572 return DITypeRefArray(MDNode::get(VMContext, Elts));
Manman Ren1a125c92014-07-28 19:33:20 +0000573}
574
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000575DISubrange *DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
576 return DISubrange::get(VMContext, Count, Lo);
Devang Patel89ea4f22010-12-08 01:50:15 +0000577}
578
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000579static void checkGlobalVariableScope(DIScope *Context) {
Duncan P. N. Exon Smith430220f2015-04-06 23:34:41 +0000580#ifndef NDEBUG
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000581 if (auto *CT =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000582 dyn_cast_or_null<DICompositeType>(getNonCompileUnitScope(Context)))
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000583 assert(CT->getIdentifier().empty() &&
Manman Renbfd2b8292014-11-21 19:47:48 +0000584 "Context of a global variable should not be a type with identifier");
Duncan P. N. Exon Smith430220f2015-04-06 23:34:41 +0000585#endif
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000586}
587
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000588DIGlobalVariable *DIBuilder::createGlobalVariable(
589 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F,
590 unsigned LineNumber, DIType *Ty, bool isLocalToUnit, Constant *Val,
Duncan P. N. Exon Smithdbf64acd2014-11-15 00:23:49 +0000591 MDNode *Decl) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000592 checkGlobalVariableScope(Context);
593
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000594 auto *N = DIGlobalVariable::get(VMContext, cast_or_null<DIScope>(Context),
Duncan P. N. Exon Smithb273d062015-04-16 01:37:00 +0000595 Name, LinkageName, F, LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000596 DITypeRef::get(Ty), isLocalToUnit, true, Val,
597 cast_or_null<DIDerivedType>(Decl));
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000598 AllGVs.push_back(N);
599 return N;
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000600}
601
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000602DIGlobalVariable *DIBuilder::createTempGlobalVariableFwdDecl(
603 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F,
604 unsigned LineNumber, DIType *Ty, bool isLocalToUnit, Constant *Val,
Duncan P. N. Exon Smithdbf64acd2014-11-15 00:23:49 +0000605 MDNode *Decl) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000606 checkGlobalVariableScope(Context);
607
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000608 return DIGlobalVariable::getTemporary(
609 VMContext, cast_or_null<DIScope>(Context), Name, LinkageName, F,
610 LineNumber, DITypeRef::get(Ty), isLocalToUnit, false, Val,
611 cast_or_null<DIDerivedType>(Decl))
Duncan P. N. Exon Smithb273d062015-04-16 01:37:00 +0000612 .release();
Devang Patel746660f2010-12-07 23:25:47 +0000613}
614
Duncan P. N. Exon Smith1e40dc42015-07-31 17:55:53 +0000615static DILocalVariable *createLocalVariable(
616 LLVMContext &VMContext,
Duncan P. N. Exon Smith3c406c22016-04-20 20:14:09 +0000617 DenseMap<MDNode *, SmallVector<TrackingMDNodeRef, 1>> &PreservedVariables,
Duncan P. N. Exon Smith1e40dc42015-07-31 17:55:53 +0000618 DIScope *Scope, StringRef Name, unsigned ArgNo, DIFile *File,
619 unsigned LineNo, DIType *Ty, bool AlwaysPreserve, unsigned Flags) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000620 // FIXME: Why getNonCompileUnitScope()?
621 // FIXME: Why is "!Context" okay here?
Adrian Prantl12d52842015-07-10 23:26:02 +0000622 // FIXME: Why doesn't this check for a subprogram or lexical block (AFAICT
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000623 // the only valid scopes)?
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000624 DIScope *Context = getNonCompileUnitScope(Scope);
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000625
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +0000626 auto *Node =
627 DILocalVariable::get(VMContext, cast_or_null<DILocalScope>(Context), Name,
628 File, LineNo, DITypeRef::get(Ty), ArgNo, Flags);
Devang Patel63f83cd2010-12-07 23:58:00 +0000629 if (AlwaysPreserve) {
Adrian Prantl12d52842015-07-10 23:26:02 +0000630 // The optimizer may remove local variables. If there is an interest
Devang Patel63f83cd2010-12-07 23:58:00 +0000631 // to preserve variable info in such situation then stash it in a
632 // named mdnode.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000633 DISubprogram *Fn = getDISubprogram(Scope);
Duncan P. N. Exon Smith3bfffde2014-10-15 16:11:41 +0000634 assert(Fn && "Missing subprogram for local variable");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000635 PreservedVariables[Fn].emplace_back(Node);
Devang Patel63f83cd2010-12-07 23:58:00 +0000636 }
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000637 return Node;
Devang Patel63f83cd2010-12-07 23:58:00 +0000638}
639
Duncan P. N. Exon Smith1e40dc42015-07-31 17:55:53 +0000640DILocalVariable *DIBuilder::createAutoVariable(DIScope *Scope, StringRef Name,
641 DIFile *File, unsigned LineNo,
642 DIType *Ty, bool AlwaysPreserve,
643 unsigned Flags) {
644 return createLocalVariable(VMContext, PreservedVariables, Scope, Name,
645 /* ArgNo */ 0, File, LineNo, Ty, AlwaysPreserve,
646 Flags);
647}
648
649DILocalVariable *DIBuilder::createParameterVariable(
650 DIScope *Scope, StringRef Name, unsigned ArgNo, DIFile *File,
651 unsigned LineNo, DIType *Ty, bool AlwaysPreserve, unsigned Flags) {
652 assert(ArgNo && "Expected non-zero argument number for parameter");
653 return createLocalVariable(VMContext, PreservedVariables, Scope, Name, ArgNo,
654 File, LineNo, Ty, AlwaysPreserve, Flags);
655}
656
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000657DIExpression *DIBuilder::createExpression(ArrayRef<uint64_t> Addr) {
658 return DIExpression::get(VMContext, Addr);
Devang Patel746660f2010-12-07 23:25:47 +0000659}
660
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000661DIExpression *DIBuilder::createExpression(ArrayRef<int64_t> Signed) {
Duncan P. N. Exon Smithbd75ad42015-02-09 22:13:27 +0000662 // TODO: Remove the callers of this signed version and delete.
663 SmallVector<uint64_t, 8> Addr(Signed.begin(), Signed.end());
664 return createExpression(Addr);
665}
666
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000667DIExpression *DIBuilder::createBitPieceExpression(unsigned OffsetInBytes,
668 unsigned SizeInBytes) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000669 uint64_t Addr[] = {dwarf::DW_OP_bit_piece, OffsetInBytes, SizeInBytes};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000670 return DIExpression::get(VMContext, Addr);
Duncan P. N. Exon Smith9affbba2014-10-01 21:32:12 +0000671}
672
Duncan P. N. Exon Smithb2df6472015-08-26 22:50:16 +0000673template <class... Ts>
674static DISubprogram *getSubprogram(bool IsDistinct, Ts &&... Args) {
675 if (IsDistinct)
676 return DISubprogram::getDistinct(std::forward<Ts>(Args)...);
677 return DISubprogram::get(std::forward<Ts>(Args)...);
678}
679
Peter Collingbourned4bff302015-11-05 22:03:56 +0000680DISubprogram *DIBuilder::createFunction(
681 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *File,
682 unsigned LineNo, DISubroutineType *Ty, bool isLocalToUnit,
683 bool isDefinition, unsigned ScopeLine, unsigned Flags, bool isOptimized,
684 DITemplateParameterArray TParams, DISubprogram *Decl) {
Adrian Prantl75819ae2016-04-15 15:57:41 +0000685 auto *Node = getSubprogram(
686 /* IsDistinct = */ isDefinition, VMContext,
687 DIScopeRef::get(getNonCompileUnitScope(Context)), Name, LinkageName, File,
688 LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine, nullptr, 0, 0, Flags,
689 isOptimized, isDefinition ? CUNode : nullptr, TParams, Decl,
690 MDTuple::getTemporary(VMContext, None).release());
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000691
692 if (isDefinition)
693 AllSubprograms.push_back(Node);
694 trackIfUnresolved(Node);
695 return Node;
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000696}
697
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000698DISubprogram *DIBuilder::createTempFunctionFwdDecl(
699 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *File,
700 unsigned LineNo, DISubroutineType *Ty, bool isLocalToUnit,
701 bool isDefinition, unsigned ScopeLine, unsigned Flags, bool isOptimized,
Peter Collingbourned4bff302015-11-05 22:03:56 +0000702 DITemplateParameterArray TParams, DISubprogram *Decl) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000703 return DISubprogram::getTemporary(
704 VMContext, DIScopeRef::get(getNonCompileUnitScope(Context)), Name,
705 LinkageName, File, LineNo, Ty, isLocalToUnit, isDefinition,
Adrian Prantl75819ae2016-04-15 15:57:41 +0000706 ScopeLine, nullptr, 0, 0, Flags, isOptimized,
707 isDefinition ? CUNode : nullptr, TParams, Decl, nullptr)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000708 .release();
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000709}
710
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000711DISubprogram *
712DIBuilder::createMethod(DIScope *Context, StringRef Name, StringRef LinkageName,
713 DIFile *F, unsigned LineNo, DISubroutineType *Ty,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000714 bool isLocalToUnit, bool isDefinition, unsigned VK,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000715 unsigned VIndex, DIType *VTableHolder, unsigned Flags,
Peter Collingbourned4bff302015-11-05 22:03:56 +0000716 bool isOptimized, DITemplateParameterArray TParams) {
Eric Christopher5cb56322013-10-15 23:31:36 +0000717 assert(getNonCompileUnitScope(Context) &&
718 "Methods should have both a Context and a context that isn't "
719 "the compile unit.");
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000720 // FIXME: Do we want to use different scope/lines?
Peter Collingbourned4bff302015-11-05 22:03:56 +0000721 auto *SP = getSubprogram(
722 /* IsDistinct = */ isDefinition, VMContext,
723 DIScopeRef::get(cast<DIScope>(Context)), Name, LinkageName, F, LineNo, Ty,
724 isLocalToUnit, isDefinition, LineNo, DITypeRef::get(VTableHolder), VK,
Adrian Prantl75819ae2016-04-15 15:57:41 +0000725 VIndex, Flags, isOptimized, isDefinition ? CUNode : nullptr, TParams,
726 nullptr, nullptr);
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000727
David Blaikie595eb442013-02-18 07:10:22 +0000728 if (isDefinition)
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000729 AllSubprograms.push_back(SP);
730 trackIfUnresolved(SP);
731 return SP;
Devang Patelb68c6232010-12-08 20:42:44 +0000732}
733
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000734DINamespace *DIBuilder::createNameSpace(DIScope *Scope, StringRef Name,
735 DIFile *File, unsigned LineNo) {
736 return DINamespace::get(VMContext, getNonCompileUnitScope(Scope), File, Name,
Duncan P. N. Exon Smitha5099dc2015-04-06 19:49:39 +0000737 LineNo);
Devang Patel746660f2010-12-07 23:25:47 +0000738}
739
Adrian Prantlab1243f2015-06-29 23:03:47 +0000740DIModule *DIBuilder::createModule(DIScope *Scope, StringRef Name,
741 StringRef ConfigurationMacros,
742 StringRef IncludePath,
743 StringRef ISysRoot) {
744 return DIModule::get(VMContext, getNonCompileUnitScope(Scope), Name,
745 ConfigurationMacros, IncludePath, ISysRoot);
746}
747
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000748DILexicalBlockFile *DIBuilder::createLexicalBlockFile(DIScope *Scope,
749 DIFile *File,
750 unsigned Discriminator) {
751 return DILexicalBlockFile::get(VMContext, Scope, File, Discriminator);
Eric Christopher6647b832011-10-11 22:59:11 +0000752}
753
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000754DILexicalBlock *DIBuilder::createLexicalBlock(DIScope *Scope, DIFile *File,
755 unsigned Line, unsigned Col) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000756 // Make these distinct, to avoid merging two lexical blocks on the same
757 // file/line/column.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000758 return DILexicalBlock::getDistinct(VMContext, getNonCompileUnitScope(Scope),
Duncan P. N. Exon Smith35ef22c2015-04-15 23:19:27 +0000759 File, Line, Col);
Devang Patel89ea4f22010-12-08 01:50:15 +0000760}
761
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000762static Value *getDbgIntrinsicValueImpl(LLVMContext &VMContext, Value *V) {
763 assert(V && "no value passed to dbg intrinsic");
764 return MetadataAsValue::get(VMContext, ValueAsMetadata::get(V));
765}
766
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000767static Instruction *withDebugLoc(Instruction *I, const DILocation *DL) {
768 I->setDebugLoc(const_cast<DILocation *>(DL));
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000769 return I;
770}
771
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000772Instruction *DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
773 DIExpression *Expr, const DILocation *DL,
Devang Patel746660f2010-12-07 23:25:47 +0000774 Instruction *InsertBefore) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000775 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.declare");
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000776 assert(DL && "Expected debug loc");
777 assert(DL->getScope()->getSubprogram() ==
778 VarInfo->getScope()->getSubprogram() &&
779 "Expected matching subprograms");
Devang Patel746660f2010-12-07 23:25:47 +0000780 if (!DeclareFn)
781 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
782
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000783 trackIfUnresolved(VarInfo);
784 trackIfUnresolved(Expr);
785 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, Storage),
786 MetadataAsValue::get(VMContext, VarInfo),
787 MetadataAsValue::get(VMContext, Expr)};
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000788 return withDebugLoc(CallInst::Create(DeclareFn, Args, "", InsertBefore), DL);
Devang Patel746660f2010-12-07 23:25:47 +0000789}
790
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000791Instruction *DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
792 DIExpression *Expr, const DILocation *DL,
Devang Patel746660f2010-12-07 23:25:47 +0000793 BasicBlock *InsertAtEnd) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000794 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.declare");
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000795 assert(DL && "Expected debug loc");
796 assert(DL->getScope()->getSubprogram() ==
797 VarInfo->getScope()->getSubprogram() &&
798 "Expected matching subprograms");
Devang Patel746660f2010-12-07 23:25:47 +0000799 if (!DeclareFn)
800 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
801
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000802 trackIfUnresolved(VarInfo);
803 trackIfUnresolved(Expr);
804 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, Storage),
805 MetadataAsValue::get(VMContext, VarInfo),
806 MetadataAsValue::get(VMContext, Expr)};
Devang Patel746660f2010-12-07 23:25:47 +0000807
808 // If this block already has a terminator then insert this intrinsic
809 // before the terminator.
810 if (TerminatorInst *T = InsertAtEnd->getTerminator())
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000811 return withDebugLoc(CallInst::Create(DeclareFn, Args, "", T), DL);
812 return withDebugLoc(CallInst::Create(DeclareFn, Args, "", InsertAtEnd), DL);
Devang Patel746660f2010-12-07 23:25:47 +0000813}
814
Devang Patel9b412732011-02-22 18:56:12 +0000815Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000816 DILocalVariable *VarInfo,
817 DIExpression *Expr,
818 const DILocation *DL,
Devang Patel746660f2010-12-07 23:25:47 +0000819 Instruction *InsertBefore) {
820 assert(V && "no value passed to dbg.value");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000821 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.value");
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000822 assert(DL && "Expected debug loc");
823 assert(DL->getScope()->getSubprogram() ==
824 VarInfo->getScope()->getSubprogram() &&
825 "Expected matching subprograms");
Devang Patel746660f2010-12-07 23:25:47 +0000826 if (!ValueFn)
827 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
828
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000829 trackIfUnresolved(VarInfo);
830 trackIfUnresolved(Expr);
831 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, V),
832 ConstantInt::get(Type::getInt64Ty(VMContext), Offset),
833 MetadataAsValue::get(VMContext, VarInfo),
834 MetadataAsValue::get(VMContext, Expr)};
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000835 return withDebugLoc(CallInst::Create(ValueFn, Args, "", InsertBefore), DL);
Devang Patel746660f2010-12-07 23:25:47 +0000836}
837
Devang Patel9b412732011-02-22 18:56:12 +0000838Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000839 DILocalVariable *VarInfo,
840 DIExpression *Expr,
841 const DILocation *DL,
Devang Patel746660f2010-12-07 23:25:47 +0000842 BasicBlock *InsertAtEnd) {
843 assert(V && "no value passed to dbg.value");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000844 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.value");
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000845 assert(DL && "Expected debug loc");
846 assert(DL->getScope()->getSubprogram() ==
847 VarInfo->getScope()->getSubprogram() &&
848 "Expected matching subprograms");
Devang Patel746660f2010-12-07 23:25:47 +0000849 if (!ValueFn)
850 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
851
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000852 trackIfUnresolved(VarInfo);
853 trackIfUnresolved(Expr);
854 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, V),
855 ConstantInt::get(Type::getInt64Ty(VMContext), Offset),
856 MetadataAsValue::get(VMContext, VarInfo),
857 MetadataAsValue::get(VMContext, Expr)};
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000858
859 return withDebugLoc(CallInst::Create(ValueFn, Args, "", InsertAtEnd), DL);
Devang Patel746660f2010-12-07 23:25:47 +0000860}
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000861
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000862void DIBuilder::replaceVTableHolder(DICompositeType *&T,
863 DICompositeType *VTableHolder) {
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000864 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000865 TypedTrackingMDRef<DICompositeType> N(T);
866 N->replaceVTableHolder(DITypeRef::get(VTableHolder));
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000867 T = N.get();
868 }
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000869
870 // If this didn't create a self-reference, just return.
871 if (T != VTableHolder)
872 return;
873
Adrian Prantl18a25b02015-02-11 17:45:10 +0000874 // Look for unresolved operands. T will drop RAUW support, orphaning any
875 // cycles underneath it.
876 if (T->isResolved())
877 for (const MDOperand &O : T->operands())
878 if (auto *N = dyn_cast_or_null<MDNode>(O))
879 trackIfUnresolved(N);
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000880}
881
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000882void DIBuilder::replaceArrays(DICompositeType *&T, DINodeArray Elements,
883 DINodeArray TParams) {
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000884 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000885 TypedTrackingMDRef<DICompositeType> N(T);
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000886 if (Elements)
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000887 N->replaceElements(Elements);
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000888 if (TParams)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000889 N->replaceTemplateParams(DITemplateParameterArray(TParams));
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000890 T = N.get();
891 }
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000892
893 // If T isn't resolved, there's no problem.
894 if (!T->isResolved())
895 return;
896
Adrian Prantl12d52842015-07-10 23:26:02 +0000897 // 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 +0000898 // arrays explicitly if they're unresolved, or else the cycles will be
899 // orphaned.
900 if (Elements)
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +0000901 trackIfUnresolved(Elements.get());
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000902 if (TParams)
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +0000903 trackIfUnresolved(TParams.get());
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000904}