blob: 09b540350c28a24a99273145fecf9e69a750f56f [file] [log] [blame]
Devang Patel57c5a202010-11-04 15:01:38 +00001//===--- DIBuilder.cpp - Debug Information Builder ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the DIBuilder.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth12664a02014-03-06 00:22:06 +000014#include "llvm/IR/DIBuilder.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/STLExtras.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/Constants.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000017#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/IntrinsicInst.h"
19#include "llvm/IR/Module.h"
Eric Christopher34164192012-04-03 00:43:49 +000020#include "llvm/Support/Debug.h"
Devang Patel57c5a202010-11-04 15:01:38 +000021#include "llvm/Support/Dwarf.h"
22
23using namespace llvm;
24using namespace llvm::dwarf;
25
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +000026namespace {
27class HeaderBuilder {
Duncan P. N. Exon Smithaa687a32015-01-20 05:02:42 +000028 /// \brief Whether there are any fields yet.
29 ///
30 /// Note that this is not equivalent to \c Chars.empty(), since \a concat()
31 /// may have been called already with an empty string.
32 bool IsEmpty;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +000033 SmallVector<char, 256> Chars;
34
35public:
Duncan P. N. Exon Smithaa687a32015-01-20 05:02:42 +000036 HeaderBuilder() : IsEmpty(true) {}
37 HeaderBuilder(const HeaderBuilder &X) : IsEmpty(X.IsEmpty), Chars(X.Chars) {}
38 HeaderBuilder(HeaderBuilder &&X)
39 : IsEmpty(X.IsEmpty), Chars(std::move(X.Chars)) {}
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +000040
41 template <class Twineable> HeaderBuilder &concat(Twineable &&X) {
Duncan P. N. Exon Smithaa687a32015-01-20 05:02:42 +000042 if (IsEmpty)
43 IsEmpty = false;
44 else
45 Chars.push_back(0);
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +000046 Twine(X).toVector(Chars);
47 return *this;
48 }
49
50 MDString *get(LLVMContext &Context) const {
51 return MDString::get(Context, StringRef(Chars.begin(), Chars.size()));
52 }
53
54 static HeaderBuilder get(unsigned Tag) {
Duncan P. N. Exon Smithaa687a32015-01-20 05:02:42 +000055 return HeaderBuilder().concat("0x" + Twine::utohexstr(Tag));
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +000056 }
57};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000058}
Devang Patel63f83cd2010-12-07 23:58:00 +000059
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000060DIBuilder::DIBuilder(Module &m, bool AllowUnresolvedNodes)
Adrian Prantl18c073a2015-07-02 22:32:52 +000061 : M(m), VMContext(M.getContext()), CUNode(nullptr),
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000062 DeclareFn(nullptr), ValueFn(nullptr),
63 AllowUnresolvedNodes(AllowUnresolvedNodes) {}
64
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000065void DIBuilder::trackIfUnresolved(MDNode *N) {
Duncan P. N. Exon Smith9b1c6d32015-01-19 19:09:14 +000066 if (!N)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000067 return;
Duncan P. N. Exon Smith9b1c6d32015-01-19 19:09:14 +000068 if (N->isResolved())
69 return;
70
71 assert(AllowUnresolvedNodes && "Cannot handle unresolved nodes");
72 UnresolvedNodes.emplace_back(N);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000073}
Devang Patel57c5a202010-11-04 15:01:38 +000074
Devang Patel2b8acaf2011-08-15 23:00:00 +000075void DIBuilder::finalize() {
Adrian Prantl0e224a62015-07-06 16:22:12 +000076 if (!CUNode) {
77 assert(!AllowUnresolvedNodes &&
78 "creating type nodes without a CU is not supported");
79 return;
Devang Patel59e27c52011-08-19 23:28:12 +000080 }
Devang Pateleb1bb4e2011-08-16 22:09:43 +000081
Adrian Prantl0e224a62015-07-06 16:22:12 +000082 CUNode->replaceEnumTypes(MDTuple::get(VMContext, AllEnumTypes));
83
84 SmallVector<Metadata *, 16> RetainValues;
85 // Declarations and definitions of the same type may be retained. Some
86 // clients RAUW these pairs, leaving duplicates in the retained types
87 // list. Use a set to remove the duplicates while we transform the
88 // TrackingVHs back into Values.
89 SmallPtrSet<Metadata *, 16> RetainSet;
90 for (unsigned I = 0, E = AllRetainTypes.size(); I < E; I++)
91 if (RetainSet.insert(AllRetainTypes[I]).second)
92 RetainValues.push_back(AllRetainTypes[I]);
Adrian Prantl4276d4a2015-07-06 16:36:02 +000093
94 if (!RetainValues.empty())
95 CUNode->replaceRetainedTypes(MDTuple::get(VMContext, RetainValues));
Adrian Prantl0e224a62015-07-06 16:22:12 +000096
97 DISubprogramArray SPs = MDTuple::get(VMContext, AllSubprograms);
Adrian Prantl4276d4a2015-07-06 16:36:02 +000098 if (!AllSubprograms.empty())
99 CUNode->replaceSubprograms(SPs.get());
100
Adrian Prantl0e224a62015-07-06 16:22:12 +0000101 for (auto *SP : SPs) {
102 if (MDTuple *Temp = SP->getVariables().get()) {
103 const auto &PV = PreservedVariables.lookup(SP);
104 SmallVector<Metadata *, 4> Variables(PV.begin(), PV.end());
105 DINodeArray AV = getOrCreateArray(Variables);
106 TempMDTuple(Temp)->replaceAllUsesWith(AV.get());
107 }
108 }
109
Adrian Prantl4276d4a2015-07-06 16:36:02 +0000110 if (!AllGVs.empty())
111 CUNode->replaceGlobalVariables(MDTuple::get(VMContext, AllGVs));
Adrian Prantl0e224a62015-07-06 16:22:12 +0000112
Adrian Prantl4276d4a2015-07-06 16:36:02 +0000113 if (!AllImportedModules.empty())
114 CUNode->replaceImportedEntities(MDTuple::get(
115 VMContext, SmallVector<Metadata *, 16>(AllImportedModules.begin(),
116 AllImportedModules.end())));
Adrian Prantl0e224a62015-07-06 16:22:12 +0000117
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000118 // Now that all temp nodes have been replaced or deleted, resolve remaining
119 // cycles.
120 for (const auto &N : UnresolvedNodes)
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +0000121 if (N && !N->isResolved())
122 N->resolveCycles();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000123 UnresolvedNodes.clear();
124
125 // Can't handle unresolved nodes anymore.
126 AllowUnresolvedNodes = false;
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000127}
128
Duncan P. N. Exon Smith379e3752014-10-01 21:32:15 +0000129/// If N is compile unit return NULL otherwise return N.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000130static DIScope *getNonCompileUnitScope(DIScope *N) {
131 if (!N || isa<DICompileUnit>(N))
Craig Topperc6207612014-04-09 06:08:46 +0000132 return nullptr;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000133 return cast<DIScope>(N);
Devang Patel2b8acaf2011-08-15 23:00:00 +0000134}
135
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000136DICompileUnit *DIBuilder::createCompileUnit(
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000137 unsigned Lang, StringRef Filename, StringRef Directory, StringRef Producer,
138 bool isOptimized, StringRef Flags, unsigned RunTimeVer, StringRef SplitName,
Adrian Prantl1f599f92015-05-21 20:37:30 +0000139 DebugEmissionKind Kind, uint64_t DWOId, bool EmitDebugInfo) {
Eric Christopher75d49db2014-02-27 01:24:56 +0000140
Bruce Mitchener7e575ed2015-02-07 06:35:30 +0000141 assert(((Lang <= dwarf::DW_LANG_Fortran08 && Lang >= dwarf::DW_LANG_C89) ||
Chandler Carruth4c0ee742012-01-10 18:18:52 +0000142 (Lang <= dwarf::DW_LANG_hi_user && Lang >= dwarf::DW_LANG_lo_user)) &&
143 "Invalid Language tag");
144 assert(!Filename.empty() &&
145 "Unable to create compile unit without filename");
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000146
Adrian Prantl18c073a2015-07-02 22:32:52 +0000147 assert(!CUNode && "Can only make one compile unit per DIBuilder instance");
148 CUNode = DICompileUnit::getDistinct(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000149 VMContext, Lang, DIFile::get(VMContext, Filename, Directory), Producer,
Adrian Prantl18c073a2015-07-02 22:32:52 +0000150 isOptimized, Flags, RunTimeVer, SplitName, Kind, nullptr,
151 nullptr, nullptr, nullptr, nullptr, DWOId);
Devang Patel09fa69e2011-05-03 16:18:28 +0000152
153 // Create a named metadata so that it is easier to find cu in a module.
Diego Novillo56653fd2014-06-24 17:02:03 +0000154 // Note that we only generate this when the caller wants to actually
155 // emit debug information. When we are only interested in tracking
156 // source line locations throughout the backend, we prevent codegen from
157 // emitting debug info in the final output by not generating llvm.dbg.cu.
158 if (EmitDebugInfo) {
159 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
160 NMD->addOperand(CUNode);
161 }
Eric Christopher03b3e112013-07-19 00:51:47 +0000162
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000163 trackIfUnresolved(CUNode);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000164 return CUNode;
Devang Patel57c5a202010-11-04 15:01:38 +0000165}
166
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000167static DIImportedEntity *
168createImportedModule(LLVMContext &C, dwarf::Tag Tag, DIScope *Context,
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000169 Metadata *NS, unsigned Line, StringRef Name,
170 SmallVectorImpl<TrackingMDNodeRef> &AllImportedModules) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000171 auto *M = DIImportedEntity::get(C, Tag, Context, DINodeRef(NS), Line, Name);
Duncan P. N. Exon Smithde8e4272015-04-14 01:46:44 +0000172 AllImportedModules.emplace_back(M);
David Blaikie1fd43652013-05-07 21:35:53 +0000173 return M;
174}
175
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000176DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context,
177 DINamespace *NS,
178 unsigned Line) {
David Blaikie2a40c142014-04-06 06:29:01 +0000179 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
180 Context, NS, Line, StringRef(), AllImportedModules);
David Blaikiee63d5d12013-05-20 22:50:35 +0000181}
182
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000183DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context,
184 DIImportedEntity *NS,
185 unsigned Line) {
David Blaikie2a40c142014-04-06 06:29:01 +0000186 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
187 Context, NS, Line, StringRef(), AllImportedModules);
David Blaikiee63d5d12013-05-20 22:50:35 +0000188}
189
Adrian Prantlab1243f2015-06-29 23:03:47 +0000190DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context, DIModule *M,
191 unsigned Line) {
192 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
193 Context, M, Line, StringRef(), AllImportedModules);
194}
195
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000196DIImportedEntity *DIBuilder::createImportedDeclaration(DIScope *Context,
197 DINode *Decl,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000198 unsigned Line,
199 StringRef Name) {
Frederic Riss4aa51ae2014-11-06 17:46:55 +0000200 // Make sure to use the unique identifier based metadata reference for
201 // types that have one.
David Blaikie2a40c142014-04-06 06:29:01 +0000202 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_declaration,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000203 Context, DINodeRef::get(Decl), Line, Name,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000204 AllImportedModules);
David Blaikief55abea2013-04-22 06:12:31 +0000205}
206
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000207DIFile *DIBuilder::createFile(StringRef Filename, StringRef Directory) {
208 return DIFile::get(VMContext, Filename, Directory);
Devang Patel57c5a202010-11-04 15:01:38 +0000209}
210
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000211DIEnumerator *DIBuilder::createEnumerator(StringRef Name, int64_t Val) {
Devang Patel1ad1abe2011-09-12 18:26:08 +0000212 assert(!Name.empty() && "Unable to create enumerator without name");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000213 return DIEnumerator::get(VMContext, Val, Name);
Devang Patel57c5a202010-11-04 15:01:38 +0000214}
215
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000216DIBasicType *DIBuilder::createUnspecifiedType(StringRef Name) {
Devang Patel04d6d472011-09-14 23:13:28 +0000217 assert(!Name.empty() && "Unable to create type without name");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000218 return DIBasicType::get(VMContext, dwarf::DW_TAG_unspecified_type, Name);
Devang Patel04d6d472011-09-14 23:13:28 +0000219}
220
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000221DIBasicType *DIBuilder::createNullPtrType() {
Peter Collingbournea4a47cb2013-06-27 22:50:59 +0000222 return createUnspecifiedType("decltype(nullptr)");
223}
224
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000225DIBasicType *DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000226 uint64_t AlignInBits,
227 unsigned Encoding) {
Devang Patel1ad1abe2011-09-12 18:26:08 +0000228 assert(!Name.empty() && "Unable to create type without name");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000229 return DIBasicType::get(VMContext, dwarf::DW_TAG_base_type, Name, SizeInBits,
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000230 AlignInBits, Encoding);
Devang Patel57c5a202010-11-04 15:01:38 +0000231}
232
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000233DIDerivedType *DIBuilder::createQualifiedType(unsigned Tag, DIType *FromTy) {
234 return DIDerivedType::get(VMContext, Tag, "", nullptr, 0, nullptr,
235 DITypeRef::get(FromTy), 0, 0, 0, 0);
Devang Patel57c5a202010-11-04 15:01:38 +0000236}
237
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000238DIDerivedType *DIBuilder::createPointerType(DIType *PointeeTy,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000239 uint64_t SizeInBits,
240 uint64_t AlignInBits,
241 StringRef Name) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000242 // FIXME: Why is there a name here?
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000243 return DIDerivedType::get(VMContext, dwarf::DW_TAG_pointer_type, Name,
244 nullptr, 0, nullptr, DITypeRef::get(PointeeTy),
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000245 SizeInBits, AlignInBits, 0, 0);
Devang Patel57c5a202010-11-04 15:01:38 +0000246}
247
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000248DIDerivedType *DIBuilder::createMemberPointerType(DIType *PointeeTy,
249 DIType *Base,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000250 uint64_t SizeInBits,
251 uint64_t AlignInBits) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000252 return DIDerivedType::get(VMContext, dwarf::DW_TAG_ptr_to_member_type, "",
253 nullptr, 0, nullptr, DITypeRef::get(PointeeTy),
254 SizeInBits, AlignInBits, 0, 0,
255 DITypeRef::get(Base));
David Blaikie5d3249b2013-01-07 05:51:15 +0000256}
257
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
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000510void DIBuilder::retainType(DIType *T) {
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000511 assert(T && "Expected non-null type");
Duncan P. N. Exon Smithd9ccfb92015-03-27 23:00:49 +0000512 AllRetainTypes.emplace_back(T);
513}
Devang Patel89ea4f22010-12-08 01:50:15 +0000514
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000515DIBasicType *DIBuilder::createUnspecifiedParameter() { return nullptr; }
Devang Patel89ea4f22010-12-08 01:50:15 +0000516
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000517DICompositeType *
518DIBuilder::createForwardDecl(unsigned Tag, StringRef Name, DIScope *Scope,
519 DIFile *F, unsigned Line, unsigned RuntimeLang,
Eric Christopher98f9c232013-10-15 23:31:31 +0000520 uint64_t SizeInBits, uint64_t AlignInBits,
521 StringRef UniqueIdentifier) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000522 // FIXME: Define in terms of createReplaceableForwardDecl() by calling
523 // replaceWithUniqued().
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000524 auto *RetTy = DICompositeType::get(
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000525 VMContext, Tag, Name, F, Line,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000526 DIScopeRef::get(getNonCompileUnitScope(Scope)), nullptr, SizeInBits,
527 AlignInBits, 0, DINode::FlagFwdDecl, nullptr, RuntimeLang, nullptr,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000528 nullptr, UniqueIdentifier);
David Blaikied3f094a2014-05-06 03:41:57 +0000529 if (!UniqueIdentifier.empty())
530 retainType(RetTy);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000531 trackIfUnresolved(RetTy);
David Blaikied3f094a2014-05-06 03:41:57 +0000532 return RetTy;
533}
534
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000535DICompositeType *DIBuilder::createReplaceableCompositeType(
536 unsigned Tag, StringRef Name, DIScope *Scope, DIFile *F, unsigned Line,
David Blaikied3f094a2014-05-06 03:41:57 +0000537 unsigned RuntimeLang, uint64_t SizeInBits, uint64_t AlignInBits,
Adrian Prantl534a81a2015-02-11 17:45:05 +0000538 unsigned Flags, StringRef UniqueIdentifier) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000539 auto *RetTy = DICompositeType::getTemporary(
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000540 VMContext, Tag, Name, F, Line,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000541 DIScopeRef::get(getNonCompileUnitScope(Scope)), nullptr,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000542 SizeInBits, AlignInBits, 0, Flags, nullptr, RuntimeLang,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000543 nullptr, nullptr, UniqueIdentifier)
544 .release();
Manman Ren0b410402013-08-29 23:17:54 +0000545 if (!UniqueIdentifier.empty())
546 retainType(RetTy);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000547 trackIfUnresolved(RetTy);
Manman Rend0e67aa2013-07-02 18:37:35 +0000548 return RetTy;
Eric Christopherae56eec2012-02-08 00:22:26 +0000549}
550
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000551DINodeArray DIBuilder::getOrCreateArray(ArrayRef<Metadata *> Elements) {
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000552 return MDTuple::get(VMContext, Elements);
Devang Patel746660f2010-12-07 23:25:47 +0000553}
554
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000555DITypeRefArray DIBuilder::getOrCreateTypeArray(ArrayRef<Metadata *> Elements) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000556 SmallVector<llvm::Metadata *, 16> Elts;
Manman Ren1a125c92014-07-28 19:33:20 +0000557 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
558 if (Elements[i] && isa<MDNode>(Elements[i]))
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000559 Elts.push_back(DITypeRef::get(cast<DIType>(Elements[i])));
Manman Ren1a125c92014-07-28 19:33:20 +0000560 else
561 Elts.push_back(Elements[i]);
562 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000563 return DITypeRefArray(MDNode::get(VMContext, Elts));
Manman Ren1a125c92014-07-28 19:33:20 +0000564}
565
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000566DISubrange *DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
567 return DISubrange::get(VMContext, Count, Lo);
Devang Patel89ea4f22010-12-08 01:50:15 +0000568}
569
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000570static void checkGlobalVariableScope(DIScope *Context) {
Duncan P. N. Exon Smith430220f2015-04-06 23:34:41 +0000571#ifndef NDEBUG
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000572 if (auto *CT =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000573 dyn_cast_or_null<DICompositeType>(getNonCompileUnitScope(Context)))
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000574 assert(CT->getIdentifier().empty() &&
Manman Renbfd2b8292014-11-21 19:47:48 +0000575 "Context of a global variable should not be a type with identifier");
Duncan P. N. Exon Smith430220f2015-04-06 23:34:41 +0000576#endif
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000577}
578
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000579DIGlobalVariable *DIBuilder::createGlobalVariable(
580 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F,
581 unsigned LineNumber, DIType *Ty, bool isLocalToUnit, Constant *Val,
Duncan P. N. Exon Smithdbf64acd2014-11-15 00:23:49 +0000582 MDNode *Decl) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000583 checkGlobalVariableScope(Context);
584
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000585 auto *N = DIGlobalVariable::get(VMContext, cast_or_null<DIScope>(Context),
Duncan P. N. Exon Smithb273d062015-04-16 01:37:00 +0000586 Name, LinkageName, F, LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000587 DITypeRef::get(Ty), isLocalToUnit, true, Val,
588 cast_or_null<DIDerivedType>(Decl));
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000589 AllGVs.push_back(N);
590 return N;
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000591}
592
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000593DIGlobalVariable *DIBuilder::createTempGlobalVariableFwdDecl(
594 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F,
595 unsigned LineNumber, DIType *Ty, bool isLocalToUnit, Constant *Val,
Duncan P. N. Exon Smithdbf64acd2014-11-15 00:23:49 +0000596 MDNode *Decl) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000597 checkGlobalVariableScope(Context);
598
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000599 return DIGlobalVariable::getTemporary(
600 VMContext, cast_or_null<DIScope>(Context), Name, LinkageName, F,
601 LineNumber, DITypeRef::get(Ty), isLocalToUnit, false, Val,
602 cast_or_null<DIDerivedType>(Decl))
Duncan P. N. Exon Smithb273d062015-04-16 01:37:00 +0000603 .release();
Devang Patel746660f2010-12-07 23:25:47 +0000604}
605
Duncan P. N. Exon Smith1e40dc42015-07-31 17:55:53 +0000606static DILocalVariable *createLocalVariable(
607 LLVMContext &VMContext,
608 DenseMap<MDNode *, std::vector<TrackingMDNodeRef>> &PreservedVariables,
609 DIScope *Scope, StringRef Name, unsigned ArgNo, DIFile *File,
610 unsigned LineNo, DIType *Ty, bool AlwaysPreserve, unsigned Flags) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000611 // FIXME: Why getNonCompileUnitScope()?
612 // FIXME: Why is "!Context" okay here?
Adrian Prantl12d52842015-07-10 23:26:02 +0000613 // FIXME: Why doesn't this check for a subprogram or lexical block (AFAICT
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000614 // the only valid scopes)?
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000615 DIScope *Context = getNonCompileUnitScope(Scope);
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000616
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +0000617 auto *Node =
618 DILocalVariable::get(VMContext, cast_or_null<DILocalScope>(Context), Name,
619 File, LineNo, DITypeRef::get(Ty), ArgNo, Flags);
Devang Patel63f83cd2010-12-07 23:58:00 +0000620 if (AlwaysPreserve) {
Adrian Prantl12d52842015-07-10 23:26:02 +0000621 // The optimizer may remove local variables. If there is an interest
Devang Patel63f83cd2010-12-07 23:58:00 +0000622 // to preserve variable info in such situation then stash it in a
623 // named mdnode.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000624 DISubprogram *Fn = getDISubprogram(Scope);
Duncan P. N. Exon Smith3bfffde2014-10-15 16:11:41 +0000625 assert(Fn && "Missing subprogram for local variable");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000626 PreservedVariables[Fn].emplace_back(Node);
Devang Patel63f83cd2010-12-07 23:58:00 +0000627 }
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000628 return Node;
Devang Patel63f83cd2010-12-07 23:58:00 +0000629}
630
Duncan P. N. Exon Smith1e40dc42015-07-31 17:55:53 +0000631DILocalVariable *DIBuilder::createAutoVariable(DIScope *Scope, StringRef Name,
632 DIFile *File, unsigned LineNo,
633 DIType *Ty, bool AlwaysPreserve,
634 unsigned Flags) {
635 return createLocalVariable(VMContext, PreservedVariables, Scope, Name,
636 /* ArgNo */ 0, File, LineNo, Ty, AlwaysPreserve,
637 Flags);
638}
639
640DILocalVariable *DIBuilder::createParameterVariable(
641 DIScope *Scope, StringRef Name, unsigned ArgNo, DIFile *File,
642 unsigned LineNo, DIType *Ty, bool AlwaysPreserve, unsigned Flags) {
643 assert(ArgNo && "Expected non-zero argument number for parameter");
644 return createLocalVariable(VMContext, PreservedVariables, Scope, Name, ArgNo,
645 File, LineNo, Ty, AlwaysPreserve, Flags);
646}
647
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000648DIExpression *DIBuilder::createExpression(ArrayRef<uint64_t> Addr) {
649 return DIExpression::get(VMContext, Addr);
Devang Patel746660f2010-12-07 23:25:47 +0000650}
651
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000652DIExpression *DIBuilder::createExpression(ArrayRef<int64_t> Signed) {
Duncan P. N. Exon Smithbd75ad42015-02-09 22:13:27 +0000653 // TODO: Remove the callers of this signed version and delete.
654 SmallVector<uint64_t, 8> Addr(Signed.begin(), Signed.end());
655 return createExpression(Addr);
656}
657
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000658DIExpression *DIBuilder::createBitPieceExpression(unsigned OffsetInBytes,
659 unsigned SizeInBytes) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000660 uint64_t Addr[] = {dwarf::DW_OP_bit_piece, OffsetInBytes, SizeInBytes};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000661 return DIExpression::get(VMContext, Addr);
Duncan P. N. Exon Smith9affbba2014-10-01 21:32:12 +0000662}
663
Peter Collingbourned4bff302015-11-05 22:03:56 +0000664DISubprogram *DIBuilder::createFunction(
665 DIScopeRef Context, StringRef Name, StringRef LinkageName, DIFile *File,
666 unsigned LineNo, DISubroutineType *Ty, bool isLocalToUnit,
667 bool isDefinition, unsigned ScopeLine, unsigned Flags, bool isOptimized,
668 DITemplateParameterArray TParams, DISubprogram *Decl) {
Manman Renc50fa112013-10-10 18:40:01 +0000669 // dragonegg does not generate identifier for types, so using an empty map
670 // to resolve the context should be fine.
671 DITypeIdentifierMap EmptyMap;
672 return createFunction(Context.resolve(EmptyMap), Name, LinkageName, File,
673 LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
Peter Collingbourned4bff302015-11-05 22:03:56 +0000674 Flags, isOptimized, TParams, Decl);
Manman Renc50fa112013-10-10 18:40:01 +0000675}
676
Duncan P. N. Exon Smithb2df6472015-08-26 22:50:16 +0000677template <class... Ts>
678static DISubprogram *getSubprogram(bool IsDistinct, Ts &&... Args) {
679 if (IsDistinct)
680 return DISubprogram::getDistinct(std::forward<Ts>(Args)...);
681 return DISubprogram::get(std::forward<Ts>(Args)...);
682}
683
Peter Collingbourned4bff302015-11-05 22:03:56 +0000684DISubprogram *DIBuilder::createFunction(
685 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *File,
686 unsigned LineNo, DISubroutineType *Ty, bool isLocalToUnit,
687 bool isDefinition, unsigned ScopeLine, unsigned Flags, bool isOptimized,
688 DITemplateParameterArray TParams, DISubprogram *Decl) {
689 auto *Node =
690 getSubprogram(/* IsDistinct = */ isDefinition, VMContext,
691 DIScopeRef::get(getNonCompileUnitScope(Context)), Name,
692 LinkageName, File, LineNo, Ty, isLocalToUnit, isDefinition,
693 ScopeLine, nullptr, 0, 0, Flags, isOptimized, TParams, Decl,
694 MDTuple::getTemporary(VMContext, None).release());
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000695
696 if (isDefinition)
697 AllSubprograms.push_back(Node);
698 trackIfUnresolved(Node);
699 return Node;
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000700}
701
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000702DISubprogram *DIBuilder::createTempFunctionFwdDecl(
703 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *File,
704 unsigned LineNo, DISubroutineType *Ty, bool isLocalToUnit,
705 bool isDefinition, unsigned ScopeLine, unsigned Flags, bool isOptimized,
Peter Collingbourned4bff302015-11-05 22:03:56 +0000706 DITemplateParameterArray TParams, DISubprogram *Decl) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000707 return DISubprogram::getTemporary(
708 VMContext, DIScopeRef::get(getNonCompileUnitScope(Context)), Name,
709 LinkageName, File, LineNo, Ty, isLocalToUnit, isDefinition,
Peter Collingbourned4bff302015-11-05 22:03:56 +0000710 ScopeLine, nullptr, 0, 0, Flags, isOptimized, TParams, Decl,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000711 nullptr)
712 .release();
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000713}
714
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000715DISubprogram *
716DIBuilder::createMethod(DIScope *Context, StringRef Name, StringRef LinkageName,
717 DIFile *F, unsigned LineNo, DISubroutineType *Ty,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000718 bool isLocalToUnit, bool isDefinition, unsigned VK,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000719 unsigned VIndex, DIType *VTableHolder, unsigned Flags,
Peter Collingbourned4bff302015-11-05 22:03:56 +0000720 bool isOptimized, DITemplateParameterArray TParams) {
Eric Christopher5cb56322013-10-15 23:31:36 +0000721 assert(getNonCompileUnitScope(Context) &&
722 "Methods should have both a Context and a context that isn't "
723 "the compile unit.");
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000724 // FIXME: Do we want to use different scope/lines?
Peter Collingbourned4bff302015-11-05 22:03:56 +0000725 auto *SP = getSubprogram(
726 /* IsDistinct = */ isDefinition, VMContext,
727 DIScopeRef::get(cast<DIScope>(Context)), Name, LinkageName, F, LineNo, Ty,
728 isLocalToUnit, isDefinition, LineNo, DITypeRef::get(VTableHolder), VK,
729 VIndex, Flags, isOptimized, TParams, nullptr, nullptr);
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000730
David Blaikie595eb442013-02-18 07:10:22 +0000731 if (isDefinition)
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000732 AllSubprograms.push_back(SP);
733 trackIfUnresolved(SP);
734 return SP;
Devang Patelb68c6232010-12-08 20:42:44 +0000735}
736
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000737DINamespace *DIBuilder::createNameSpace(DIScope *Scope, StringRef Name,
738 DIFile *File, unsigned LineNo) {
739 return DINamespace::get(VMContext, getNonCompileUnitScope(Scope), File, Name,
Duncan P. N. Exon Smitha5099dc2015-04-06 19:49:39 +0000740 LineNo);
Devang Patel746660f2010-12-07 23:25:47 +0000741}
742
Adrian Prantlab1243f2015-06-29 23:03:47 +0000743DIModule *DIBuilder::createModule(DIScope *Scope, StringRef Name,
744 StringRef ConfigurationMacros,
745 StringRef IncludePath,
746 StringRef ISysRoot) {
747 return DIModule::get(VMContext, getNonCompileUnitScope(Scope), Name,
748 ConfigurationMacros, IncludePath, ISysRoot);
749}
750
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000751DILexicalBlockFile *DIBuilder::createLexicalBlockFile(DIScope *Scope,
752 DIFile *File,
753 unsigned Discriminator) {
754 return DILexicalBlockFile::get(VMContext, Scope, File, Discriminator);
Eric Christopher6647b832011-10-11 22:59:11 +0000755}
756
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000757DILexicalBlock *DIBuilder::createLexicalBlock(DIScope *Scope, DIFile *File,
758 unsigned Line, unsigned Col) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000759 // Make these distinct, to avoid merging two lexical blocks on the same
760 // file/line/column.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000761 return DILexicalBlock::getDistinct(VMContext, getNonCompileUnitScope(Scope),
Duncan P. N. Exon Smith35ef22c2015-04-15 23:19:27 +0000762 File, Line, Col);
Devang Patel89ea4f22010-12-08 01:50:15 +0000763}
764
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000765static Value *getDbgIntrinsicValueImpl(LLVMContext &VMContext, Value *V) {
766 assert(V && "no value passed to dbg intrinsic");
767 return MetadataAsValue::get(VMContext, ValueAsMetadata::get(V));
768}
769
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000770static Instruction *withDebugLoc(Instruction *I, const DILocation *DL) {
771 I->setDebugLoc(const_cast<DILocation *>(DL));
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000772 return I;
773}
774
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000775Instruction *DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
776 DIExpression *Expr, const DILocation *DL,
Devang Patel746660f2010-12-07 23:25:47 +0000777 Instruction *InsertBefore) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000778 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.declare");
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000779 assert(DL && "Expected debug loc");
780 assert(DL->getScope()->getSubprogram() ==
781 VarInfo->getScope()->getSubprogram() &&
782 "Expected matching subprograms");
Devang Patel746660f2010-12-07 23:25:47 +0000783 if (!DeclareFn)
784 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
785
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000786 trackIfUnresolved(VarInfo);
787 trackIfUnresolved(Expr);
788 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, Storage),
789 MetadataAsValue::get(VMContext, VarInfo),
790 MetadataAsValue::get(VMContext, Expr)};
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000791 return withDebugLoc(CallInst::Create(DeclareFn, Args, "", InsertBefore), DL);
Devang Patel746660f2010-12-07 23:25:47 +0000792}
793
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000794Instruction *DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
795 DIExpression *Expr, const DILocation *DL,
Devang Patel746660f2010-12-07 23:25:47 +0000796 BasicBlock *InsertAtEnd) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000797 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.declare");
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000798 assert(DL && "Expected debug loc");
799 assert(DL->getScope()->getSubprogram() ==
800 VarInfo->getScope()->getSubprogram() &&
801 "Expected matching subprograms");
Devang Patel746660f2010-12-07 23:25:47 +0000802 if (!DeclareFn)
803 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
804
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000805 trackIfUnresolved(VarInfo);
806 trackIfUnresolved(Expr);
807 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, Storage),
808 MetadataAsValue::get(VMContext, VarInfo),
809 MetadataAsValue::get(VMContext, Expr)};
Devang Patel746660f2010-12-07 23:25:47 +0000810
811 // If this block already has a terminator then insert this intrinsic
812 // before the terminator.
813 if (TerminatorInst *T = InsertAtEnd->getTerminator())
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000814 return withDebugLoc(CallInst::Create(DeclareFn, Args, "", T), DL);
815 return withDebugLoc(CallInst::Create(DeclareFn, Args, "", InsertAtEnd), DL);
Devang Patel746660f2010-12-07 23:25:47 +0000816}
817
Devang Patel9b412732011-02-22 18:56:12 +0000818Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000819 DILocalVariable *VarInfo,
820 DIExpression *Expr,
821 const DILocation *DL,
Devang Patel746660f2010-12-07 23:25:47 +0000822 Instruction *InsertBefore) {
823 assert(V && "no value passed to dbg.value");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000824 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.value");
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000825 assert(DL && "Expected debug loc");
826 assert(DL->getScope()->getSubprogram() ==
827 VarInfo->getScope()->getSubprogram() &&
828 "Expected matching subprograms");
Devang Patel746660f2010-12-07 23:25:47 +0000829 if (!ValueFn)
830 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
831
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000832 trackIfUnresolved(VarInfo);
833 trackIfUnresolved(Expr);
834 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, V),
835 ConstantInt::get(Type::getInt64Ty(VMContext), Offset),
836 MetadataAsValue::get(VMContext, VarInfo),
837 MetadataAsValue::get(VMContext, Expr)};
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000838 return withDebugLoc(CallInst::Create(ValueFn, Args, "", InsertBefore), DL);
Devang Patel746660f2010-12-07 23:25:47 +0000839}
840
Devang Patel9b412732011-02-22 18:56:12 +0000841Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000842 DILocalVariable *VarInfo,
843 DIExpression *Expr,
844 const DILocation *DL,
Devang Patel746660f2010-12-07 23:25:47 +0000845 BasicBlock *InsertAtEnd) {
846 assert(V && "no value passed to dbg.value");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000847 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.value");
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000848 assert(DL && "Expected debug loc");
849 assert(DL->getScope()->getSubprogram() ==
850 VarInfo->getScope()->getSubprogram() &&
851 "Expected matching subprograms");
Devang Patel746660f2010-12-07 23:25:47 +0000852 if (!ValueFn)
853 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
854
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000855 trackIfUnresolved(VarInfo);
856 trackIfUnresolved(Expr);
857 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, V),
858 ConstantInt::get(Type::getInt64Ty(VMContext), Offset),
859 MetadataAsValue::get(VMContext, VarInfo),
860 MetadataAsValue::get(VMContext, Expr)};
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000861
862 return withDebugLoc(CallInst::Create(ValueFn, Args, "", InsertAtEnd), DL);
Devang Patel746660f2010-12-07 23:25:47 +0000863}
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000864
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000865void DIBuilder::replaceVTableHolder(DICompositeType *&T,
866 DICompositeType *VTableHolder) {
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000867 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000868 TypedTrackingMDRef<DICompositeType> N(T);
869 N->replaceVTableHolder(DITypeRef::get(VTableHolder));
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000870 T = N.get();
871 }
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000872
873 // If this didn't create a self-reference, just return.
874 if (T != VTableHolder)
875 return;
876
Adrian Prantl18a25b02015-02-11 17:45:10 +0000877 // Look for unresolved operands. T will drop RAUW support, orphaning any
878 // cycles underneath it.
879 if (T->isResolved())
880 for (const MDOperand &O : T->operands())
881 if (auto *N = dyn_cast_or_null<MDNode>(O))
882 trackIfUnresolved(N);
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000883}
884
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000885void DIBuilder::replaceArrays(DICompositeType *&T, DINodeArray Elements,
886 DINodeArray TParams) {
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000887 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000888 TypedTrackingMDRef<DICompositeType> N(T);
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000889 if (Elements)
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000890 N->replaceElements(Elements);
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000891 if (TParams)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000892 N->replaceTemplateParams(DITemplateParameterArray(TParams));
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000893 T = N.get();
894 }
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000895
896 // If T isn't resolved, there's no problem.
897 if (!T->isResolved())
898 return;
899
Adrian Prantl12d52842015-07-10 23:26:02 +0000900 // 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 +0000901 // arrays explicitly if they're unresolved, or else the cycles will be
902 // orphaned.
903 if (Elements)
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +0000904 trackIfUnresolved(Elements.get());
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000905 if (TParams)
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +0000906 trackIfUnresolved(TParams.get());
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000907}