blob: ab03365d508856c01147849d247f3be54f73ec02 [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 Smitha59d3e52016-04-23 21:08:00 +0000209 Context, Decl, Line, Name, AllImportedModules);
David Blaikief55abea2013-04-22 06:12:31 +0000210}
211
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000212DIFile *DIBuilder::createFile(StringRef Filename, StringRef Directory) {
213 return DIFile::get(VMContext, Filename, Directory);
Devang Patel57c5a202010-11-04 15:01:38 +0000214}
215
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000216DIEnumerator *DIBuilder::createEnumerator(StringRef Name, int64_t Val) {
Devang Patel1ad1abe2011-09-12 18:26:08 +0000217 assert(!Name.empty() && "Unable to create enumerator without name");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000218 return DIEnumerator::get(VMContext, Val, Name);
Devang Patel57c5a202010-11-04 15:01:38 +0000219}
220
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000221DIBasicType *DIBuilder::createUnspecifiedType(StringRef Name) {
Devang Patel04d6d472011-09-14 23:13:28 +0000222 assert(!Name.empty() && "Unable to create type without name");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000223 return DIBasicType::get(VMContext, dwarf::DW_TAG_unspecified_type, Name);
Devang Patel04d6d472011-09-14 23:13:28 +0000224}
225
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000226DIBasicType *DIBuilder::createNullPtrType() {
Peter Collingbournea4a47cb2013-06-27 22:50:59 +0000227 return createUnspecifiedType("decltype(nullptr)");
228}
229
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000230DIBasicType *DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000231 uint64_t AlignInBits,
232 unsigned Encoding) {
Devang Patel1ad1abe2011-09-12 18:26:08 +0000233 assert(!Name.empty() && "Unable to create type without name");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000234 return DIBasicType::get(VMContext, dwarf::DW_TAG_base_type, Name, SizeInBits,
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000235 AlignInBits, Encoding);
Devang Patel57c5a202010-11-04 15:01:38 +0000236}
237
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000238DIDerivedType *DIBuilder::createQualifiedType(unsigned Tag, DIType *FromTy) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000239 return DIDerivedType::get(VMContext, Tag, "", nullptr, 0, nullptr, FromTy, 0,
240 0, 0, 0);
Devang Patel57c5a202010-11-04 15:01:38 +0000241}
242
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000243DIDerivedType *DIBuilder::createPointerType(DIType *PointeeTy,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000244 uint64_t SizeInBits,
245 uint64_t AlignInBits,
246 StringRef Name) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000247 // FIXME: Why is there a name here?
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000248 return DIDerivedType::get(VMContext, dwarf::DW_TAG_pointer_type, Name,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000249 nullptr, 0, nullptr, PointeeTy, SizeInBits,
250 AlignInBits, 0, 0);
Devang Patel57c5a202010-11-04 15:01:38 +0000251}
252
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000253DIDerivedType *DIBuilder::createMemberPointerType(DIType *PointeeTy,
254 DIType *Base,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000255 uint64_t SizeInBits,
Reid Kleckner604105b2016-06-17 21:31:33 +0000256 uint64_t AlignInBits,
257 unsigned Flags) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000258 return DIDerivedType::get(VMContext, dwarf::DW_TAG_ptr_to_member_type, "",
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000259 nullptr, 0, nullptr, PointeeTy, SizeInBits,
Reid Kleckner604105b2016-06-17 21:31:33 +0000260 AlignInBits, 0, Flags, Base);
David Blaikie5d3249b2013-01-07 05:51:15 +0000261}
262
Keno Fischerb011c632015-11-16 07:57:32 +0000263DIDerivedType *DIBuilder::createReferenceType(unsigned Tag, DIType *RTy,
264 uint64_t SizeInBits,
265 uint64_t AlignInBits) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000266 assert(RTy && "Unable to create reference type");
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000267 return DIDerivedType::get(VMContext, Tag, "", nullptr, 0, nullptr, RTy,
268 SizeInBits, AlignInBits, 0, 0);
Devang Patel57c5a202010-11-04 15:01:38 +0000269}
270
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000271DIDerivedType *DIBuilder::createTypedef(DIType *Ty, StringRef Name,
272 DIFile *File, unsigned LineNo,
273 DIScope *Context) {
274 return DIDerivedType::get(VMContext, dwarf::DW_TAG_typedef, Name, File,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000275 LineNo, getNonCompileUnitScope(Context), Ty, 0, 0,
276 0, 0);
Devang Patel57c5a202010-11-04 15:01:38 +0000277}
278
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000279DIDerivedType *DIBuilder::createFriend(DIType *Ty, DIType *FriendTy) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000280 assert(Ty && "Invalid type!");
281 assert(FriendTy && "Invalid friend type!");
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000282 return DIDerivedType::get(VMContext, dwarf::DW_TAG_friend, "", nullptr, 0, Ty,
283 FriendTy, 0, 0, 0, 0);
Devang Patel57c5a202010-11-04 15:01:38 +0000284}
285
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000286DIDerivedType *DIBuilder::createInheritance(DIType *Ty, DIType *BaseTy,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000287 uint64_t BaseOffset,
288 unsigned Flags) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000289 assert(Ty && "Unable to create inheritance");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000290 return DIDerivedType::get(VMContext, dwarf::DW_TAG_inheritance, "", nullptr,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000291 0, Ty, BaseTy, 0, 0, BaseOffset, Flags);
Devang Patel57c5a202010-11-04 15:01:38 +0000292}
293
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000294DIDerivedType *DIBuilder::createMemberType(DIScope *Scope, StringRef Name,
295 DIFile *File, unsigned LineNumber,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000296 uint64_t SizeInBits,
297 uint64_t AlignInBits,
298 uint64_t OffsetInBits,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000299 unsigned Flags, DIType *Ty) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000300 return DIDerivedType::get(VMContext, dwarf::DW_TAG_member, Name, File,
301 LineNumber, getNonCompileUnitScope(Scope), Ty,
302 SizeInBits, AlignInBits, OffsetInBits, Flags);
Devang Patel57c5a202010-11-04 15:01:38 +0000303}
304
Duncan P. N. Exon Smith3cd2cab2015-03-27 00:34:10 +0000305static ConstantAsMetadata *getConstantOrNull(Constant *C) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000306 if (C)
307 return ConstantAsMetadata::get(C);
308 return nullptr;
309}
310
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000311DIDerivedType *DIBuilder::createStaticMemberType(DIScope *Scope, StringRef Name,
312 DIFile *File,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000313 unsigned LineNumber,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000314 DIType *Ty, unsigned Flags,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000315 llvm::Constant *Val) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000316 Flags |= DINode::FlagStaticMember;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000317 return DIDerivedType::get(VMContext, dwarf::DW_TAG_member, Name, File,
318 LineNumber, getNonCompileUnitScope(Scope), Ty, 0, 0,
319 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) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000328 return DIDerivedType::get(VMContext, dwarf::DW_TAG_member, Name, File,
329 LineNumber, getNonCompileUnitScope(File), Ty,
330 SizeInBits, AlignInBits, OffsetInBits, Flags,
331 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,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000339 SetterName, PropertyAttributes, Ty);
Devang Patelcc481592012-02-04 00:59:25 +0000340}
341
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000342DITemplateTypeParameter *
343DIBuilder::createTemplateTypeParameter(DIScope *Context, StringRef Name,
344 DIType *Ty) {
345 assert((!Context || isa<DICompileUnit>(Context)) && "Expected compile unit");
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000346 return DITemplateTypeParameter::get(VMContext, Name, Ty);
Devang Patel3a9e65e2011-02-02 21:38:25 +0000347}
348
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000349static DITemplateValueParameter *
Duncan P. N. Exon Smithb4aa16f2015-02-13 03:35:29 +0000350createTemplateValueParameterHelper(LLVMContext &VMContext, unsigned Tag,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000351 DIScope *Context, StringRef Name, DIType *Ty,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000352 Metadata *MD) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000353 assert((!Context || isa<DICompileUnit>(Context)) && "Expected compile unit");
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000354 return DITemplateValueParameter::get(VMContext, Tag, Name, Ty, MD);
Devang Patelbe933b42011-02-02 22:35:53 +0000355}
356
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000357DITemplateValueParameter *
358DIBuilder::createTemplateValueParameter(DIScope *Context, StringRef Name,
359 DIType *Ty, Constant *Val) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000360 return createTemplateValueParameterHelper(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000361 VMContext, dwarf::DW_TAG_template_value_parameter, Context, Name, Ty,
Duncan P. N. Exon Smithb4aa16f2015-02-13 03:35:29 +0000362 getConstantOrNull(Val));
David Blaikie2b380232013-06-22 18:59:11 +0000363}
364
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000365DITemplateValueParameter *
366DIBuilder::createTemplateTemplateParameter(DIScope *Context, StringRef Name,
367 DIType *Ty, StringRef Val) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000368 return createTemplateValueParameterHelper(
369 VMContext, dwarf::DW_TAG_GNU_template_template_param, Context, Name, Ty,
Duncan P. N. Exon Smithb4aa16f2015-02-13 03:35:29 +0000370 MDString::get(VMContext, Val));
David Blaikie2b380232013-06-22 18:59:11 +0000371}
372
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000373DITemplateValueParameter *
374DIBuilder::createTemplateParameterPack(DIScope *Context, StringRef Name,
375 DIType *Ty, DINodeArray Val) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000376 return createTemplateValueParameterHelper(
377 VMContext, dwarf::DW_TAG_GNU_template_parameter_pack, Context, Name, Ty,
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +0000378 Val.get());
David Blaikie2b380232013-06-22 18:59:11 +0000379}
380
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000381DICompositeType *DIBuilder::createClassType(
382 DIScope *Context, StringRef Name, DIFile *File, unsigned LineNumber,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000383 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000384 unsigned Flags, DIType *DerivedFrom, DINodeArray Elements,
385 DIType *VTableHolder, MDNode *TemplateParams, StringRef UniqueIdentifier) {
386 assert((!Context || isa<DIScope>(Context)) &&
David Blaikie085abe32013-03-11 23:21:19 +0000387 "createClassType should be called with a valid Context");
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000388
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000389 auto *R = DICompositeType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000390 VMContext, dwarf::DW_TAG_structure_type, Name, File, LineNumber,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000391 getNonCompileUnitScope(Context), DerivedFrom, SizeInBits, AlignInBits,
392 OffsetInBits, Flags, Elements, 0, VTableHolder,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000393 cast_or_null<MDTuple>(TemplateParams), UniqueIdentifier);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000394 trackIfUnresolved(R);
David Blaikie085abe32013-03-11 23:21:19 +0000395 return R;
Eric Christopher17426692012-07-06 02:35:57 +0000396}
397
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000398DICompositeType *DIBuilder::createStructType(
399 DIScope *Context, StringRef Name, DIFile *File, unsigned LineNumber,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000400 uint64_t SizeInBits, uint64_t AlignInBits, unsigned Flags,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000401 DIType *DerivedFrom, DINodeArray Elements, unsigned RunTimeLang,
402 DIType *VTableHolder, StringRef UniqueIdentifier) {
403 auto *R = DICompositeType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000404 VMContext, dwarf::DW_TAG_structure_type, Name, File, LineNumber,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000405 getNonCompileUnitScope(Context), DerivedFrom, SizeInBits, AlignInBits, 0,
406 Flags, Elements, RunTimeLang, VTableHolder, nullptr, UniqueIdentifier);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000407 trackIfUnresolved(R);
David Blaikie085abe32013-03-11 23:21:19 +0000408 return R;
Devang Patel746660f2010-12-07 23:25:47 +0000409}
410
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000411DICompositeType *DIBuilder::createUnionType(
412 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
Duncan P. N. Exon Smithaa861aa2015-04-21 20:07:38 +0000413 uint64_t SizeInBits, uint64_t AlignInBits, unsigned Flags,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000414 DINodeArray Elements, unsigned RunTimeLang, StringRef UniqueIdentifier) {
415 auto *R = DICompositeType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000416 VMContext, dwarf::DW_TAG_union_type, Name, File, LineNumber,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000417 getNonCompileUnitScope(Scope), nullptr, SizeInBits, AlignInBits, 0, Flags,
418 Elements, RunTimeLang, nullptr, nullptr, UniqueIdentifier);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000419 trackIfUnresolved(R);
Manman Ren0b410402013-08-29 23:17:54 +0000420 return R;
Devang Patel89ea4f22010-12-08 01:50:15 +0000421}
422
Eric Christopherbdafb3c2015-10-15 06:56:10 +0000423DISubroutineType *DIBuilder::createSubroutineType(DITypeRefArray ParameterTypes,
Reid Klecknerde3d8b52016-06-08 20:34:29 +0000424 unsigned Flags, unsigned CC) {
425 return DISubroutineType::get(VMContext, Flags, CC, ParameterTypes);
Devang Patel89ea4f22010-12-08 01:50:15 +0000426}
427
Adrian Prantlee5feaf2015-07-15 17:01:41 +0000428DICompositeType *DIBuilder::createExternalTypeRef(unsigned Tag, DIFile *File,
429 StringRef UniqueIdentifier) {
430 assert(!UniqueIdentifier.empty() && "external type ref without uid");
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000431 return DICompositeType::get(VMContext, Tag, "", nullptr, 0, nullptr, nullptr,
432 0, 0, 0, DINode::FlagExternalTypeRef, nullptr, 0,
433 nullptr, nullptr, UniqueIdentifier);
Adrian Prantlee5feaf2015-07-15 17:01:41 +0000434}
435
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000436DICompositeType *DIBuilder::createEnumerationType(
437 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
438 uint64_t SizeInBits, uint64_t AlignInBits, DINodeArray Elements,
439 DIType *UnderlyingType, StringRef UniqueIdentifier) {
440 auto *CTy = DICompositeType::get(
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000441 VMContext, dwarf::DW_TAG_enumeration_type, Name, File, LineNumber,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000442 getNonCompileUnitScope(Scope), UnderlyingType, SizeInBits, AlignInBits, 0,
443 0, Elements, 0, nullptr, nullptr, UniqueIdentifier);
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000444 AllEnumTypes.push_back(CTy);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000445 trackIfUnresolved(CTy);
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000446 return CTy;
Devang Patel89ea4f22010-12-08 01:50:15 +0000447}
448
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000449DICompositeType *DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
450 DIType *Ty,
451 DINodeArray Subscripts) {
452 auto *R = DICompositeType::get(VMContext, dwarf::DW_TAG_array_type, "",
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000453 nullptr, 0, nullptr, Ty, Size, AlignInBits, 0,
454 0, Subscripts, 0, nullptr);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000455 trackIfUnresolved(R);
456 return R;
Devang Patel89ea4f22010-12-08 01:50:15 +0000457}
458
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000459DICompositeType *DIBuilder::createVectorType(uint64_t Size,
460 uint64_t AlignInBits, DIType *Ty,
461 DINodeArray Subscripts) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000462 auto *R = DICompositeType::get(VMContext, dwarf::DW_TAG_array_type, "",
463 nullptr, 0, nullptr, Ty, Size, AlignInBits, 0,
464 DINode::FlagVector, Subscripts, 0, nullptr);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000465 trackIfUnresolved(R);
466 return R;
Devang Patel89ea4f22010-12-08 01:50:15 +0000467}
Devang Patel746660f2010-12-07 23:25:47 +0000468
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000469static DIType *createTypeWithFlags(LLVMContext &Context, DIType *Ty,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000470 unsigned FlagsToSet) {
471 auto NewTy = Ty->clone();
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000472 NewTy->setFlags(NewTy->getFlags() | FlagsToSet);
473 return MDNode::replaceWithUniqued(std::move(NewTy));
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000474}
475
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000476DIType *DIBuilder::createArtificialType(DIType *Ty) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000477 // FIXME: Restrict this to the nodes where it's valid.
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000478 if (Ty->isArtificial())
Devang Patel57c5a202010-11-04 15:01:38 +0000479 return Ty;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000480 return createTypeWithFlags(VMContext, Ty, DINode::FlagArtificial);
Devang Patel57c5a202010-11-04 15:01:38 +0000481}
Devang Patel746660f2010-12-07 23:25:47 +0000482
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000483DIType *DIBuilder::createObjectPointerType(DIType *Ty) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000484 // FIXME: Restrict this to the nodes where it's valid.
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000485 if (Ty->isObjectPointer())
Eric Christophere3417762012-09-12 23:36:19 +0000486 return Ty;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000487 unsigned Flags = DINode::FlagObjectPointer | DINode::FlagArtificial;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000488 return createTypeWithFlags(VMContext, Ty, Flags);
Eric Christophere3417762012-09-12 23:36:19 +0000489}
490
Adrian Prantl75819ae2016-04-15 15:57:41 +0000491void DIBuilder::retainType(DIScope *T) {
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000492 assert(T && "Expected non-null type");
Adrian Prantl75819ae2016-04-15 15:57:41 +0000493 assert((isa<DIType>(T) || (isa<DISubprogram>(T) &&
494 cast<DISubprogram>(T)->isDefinition() == false)) &&
495 "Expected type or subprogram declaration");
Duncan P. N. Exon Smithd9ccfb92015-03-27 23:00:49 +0000496 AllRetainTypes.emplace_back(T);
497}
Devang Patel89ea4f22010-12-08 01:50:15 +0000498
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000499DIBasicType *DIBuilder::createUnspecifiedParameter() { return nullptr; }
Devang Patel89ea4f22010-12-08 01:50:15 +0000500
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000501DICompositeType *
502DIBuilder::createForwardDecl(unsigned Tag, StringRef Name, DIScope *Scope,
503 DIFile *F, unsigned Line, unsigned RuntimeLang,
Eric Christopher98f9c232013-10-15 23:31:31 +0000504 uint64_t SizeInBits, uint64_t AlignInBits,
505 StringRef UniqueIdentifier) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000506 // FIXME: Define in terms of createReplaceableForwardDecl() by calling
507 // replaceWithUniqued().
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000508 auto *RetTy = DICompositeType::get(
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000509 VMContext, Tag, Name, F, Line, getNonCompileUnitScope(Scope), nullptr,
510 SizeInBits, AlignInBits, 0, DINode::FlagFwdDecl, nullptr, RuntimeLang,
511 nullptr, nullptr, UniqueIdentifier);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000512 trackIfUnresolved(RetTy);
David Blaikied3f094a2014-05-06 03:41:57 +0000513 return RetTy;
514}
515
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000516DICompositeType *DIBuilder::createReplaceableCompositeType(
517 unsigned Tag, StringRef Name, DIScope *Scope, DIFile *F, unsigned Line,
David Blaikied3f094a2014-05-06 03:41:57 +0000518 unsigned RuntimeLang, uint64_t SizeInBits, uint64_t AlignInBits,
Adrian Prantl534a81a2015-02-11 17:45:05 +0000519 unsigned Flags, StringRef UniqueIdentifier) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000520 auto *RetTy =
521 DICompositeType::getTemporary(
522 VMContext, Tag, Name, F, Line, getNonCompileUnitScope(Scope), nullptr,
523 SizeInBits, AlignInBits, 0, Flags, nullptr, RuntimeLang, nullptr,
524 nullptr, UniqueIdentifier)
525 .release();
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000526 trackIfUnresolved(RetTy);
Manman Rend0e67aa2013-07-02 18:37:35 +0000527 return RetTy;
Eric Christopherae56eec2012-02-08 00:22:26 +0000528}
529
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000530DINodeArray DIBuilder::getOrCreateArray(ArrayRef<Metadata *> Elements) {
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000531 return MDTuple::get(VMContext, Elements);
Devang Patel746660f2010-12-07 23:25:47 +0000532}
533
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000534DITypeRefArray DIBuilder::getOrCreateTypeArray(ArrayRef<Metadata *> Elements) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000535 SmallVector<llvm::Metadata *, 16> Elts;
Manman Ren1a125c92014-07-28 19:33:20 +0000536 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
537 if (Elements[i] && isa<MDNode>(Elements[i]))
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000538 Elts.push_back(cast<DIType>(Elements[i]));
Manman Ren1a125c92014-07-28 19:33:20 +0000539 else
540 Elts.push_back(Elements[i]);
541 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000542 return DITypeRefArray(MDNode::get(VMContext, Elts));
Manman Ren1a125c92014-07-28 19:33:20 +0000543}
544
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000545DISubrange *DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
546 return DISubrange::get(VMContext, Count, Lo);
Devang Patel89ea4f22010-12-08 01:50:15 +0000547}
548
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000549static void checkGlobalVariableScope(DIScope *Context) {
Duncan P. N. Exon Smith430220f2015-04-06 23:34:41 +0000550#ifndef NDEBUG
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000551 if (auto *CT =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000552 dyn_cast_or_null<DICompositeType>(getNonCompileUnitScope(Context)))
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000553 assert(CT->getIdentifier().empty() &&
Manman Renbfd2b8292014-11-21 19:47:48 +0000554 "Context of a global variable should not be a type with identifier");
Duncan P. N. Exon Smith430220f2015-04-06 23:34:41 +0000555#endif
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000556}
557
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000558DIGlobalVariable *DIBuilder::createGlobalVariable(
559 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F,
560 unsigned LineNumber, DIType *Ty, bool isLocalToUnit, Constant *Val,
Duncan P. N. Exon Smithdbf64acd2014-11-15 00:23:49 +0000561 MDNode *Decl) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000562 checkGlobalVariableScope(Context);
563
Duncan P. N. Exon Smithc5225df2016-04-23 22:29:09 +0000564 auto *N = DIGlobalVariable::getDistinct(
565 VMContext, cast_or_null<DIScope>(Context), Name, LinkageName, F,
566 LineNumber, Ty, isLocalToUnit, true, Val,
567 cast_or_null<DIDerivedType>(Decl));
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000568 AllGVs.push_back(N);
569 return N;
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000570}
571
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000572DIGlobalVariable *DIBuilder::createTempGlobalVariableFwdDecl(
573 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F,
574 unsigned LineNumber, DIType *Ty, bool isLocalToUnit, Constant *Val,
Duncan P. N. Exon Smithdbf64acd2014-11-15 00:23:49 +0000575 MDNode *Decl) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000576 checkGlobalVariableScope(Context);
577
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000578 return DIGlobalVariable::getTemporary(
579 VMContext, cast_or_null<DIScope>(Context), Name, LinkageName, F,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000580 LineNumber, Ty, isLocalToUnit, false, Val,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000581 cast_or_null<DIDerivedType>(Decl))
Duncan P. N. Exon Smithb273d062015-04-16 01:37:00 +0000582 .release();
Devang Patel746660f2010-12-07 23:25:47 +0000583}
584
Duncan P. N. Exon Smith1e40dc42015-07-31 17:55:53 +0000585static DILocalVariable *createLocalVariable(
586 LLVMContext &VMContext,
Duncan P. N. Exon Smith3c406c22016-04-20 20:14:09 +0000587 DenseMap<MDNode *, SmallVector<TrackingMDNodeRef, 1>> &PreservedVariables,
Duncan P. N. Exon Smith1e40dc42015-07-31 17:55:53 +0000588 DIScope *Scope, StringRef Name, unsigned ArgNo, DIFile *File,
589 unsigned LineNo, DIType *Ty, bool AlwaysPreserve, unsigned Flags) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000590 // FIXME: Why getNonCompileUnitScope()?
591 // FIXME: Why is "!Context" okay here?
Adrian Prantl12d52842015-07-10 23:26:02 +0000592 // FIXME: Why doesn't this check for a subprogram or lexical block (AFAICT
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000593 // the only valid scopes)?
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000594 DIScope *Context = getNonCompileUnitScope(Scope);
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000595
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +0000596 auto *Node =
597 DILocalVariable::get(VMContext, cast_or_null<DILocalScope>(Context), Name,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000598 File, LineNo, Ty, ArgNo, Flags);
Devang Patel63f83cd2010-12-07 23:58:00 +0000599 if (AlwaysPreserve) {
Adrian Prantl12d52842015-07-10 23:26:02 +0000600 // The optimizer may remove local variables. If there is an interest
Devang Patel63f83cd2010-12-07 23:58:00 +0000601 // to preserve variable info in such situation then stash it in a
602 // named mdnode.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000603 DISubprogram *Fn = getDISubprogram(Scope);
Duncan P. N. Exon Smith3bfffde2014-10-15 16:11:41 +0000604 assert(Fn && "Missing subprogram for local variable");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000605 PreservedVariables[Fn].emplace_back(Node);
Devang Patel63f83cd2010-12-07 23:58:00 +0000606 }
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000607 return Node;
Devang Patel63f83cd2010-12-07 23:58:00 +0000608}
609
Duncan P. N. Exon Smith1e40dc42015-07-31 17:55:53 +0000610DILocalVariable *DIBuilder::createAutoVariable(DIScope *Scope, StringRef Name,
611 DIFile *File, unsigned LineNo,
612 DIType *Ty, bool AlwaysPreserve,
613 unsigned Flags) {
614 return createLocalVariable(VMContext, PreservedVariables, Scope, Name,
615 /* ArgNo */ 0, File, LineNo, Ty, AlwaysPreserve,
616 Flags);
617}
618
619DILocalVariable *DIBuilder::createParameterVariable(
620 DIScope *Scope, StringRef Name, unsigned ArgNo, DIFile *File,
621 unsigned LineNo, DIType *Ty, bool AlwaysPreserve, unsigned Flags) {
622 assert(ArgNo && "Expected non-zero argument number for parameter");
623 return createLocalVariable(VMContext, PreservedVariables, Scope, Name, ArgNo,
624 File, LineNo, Ty, AlwaysPreserve, Flags);
625}
626
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000627DIExpression *DIBuilder::createExpression(ArrayRef<uint64_t> Addr) {
628 return DIExpression::get(VMContext, Addr);
Devang Patel746660f2010-12-07 23:25:47 +0000629}
630
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000631DIExpression *DIBuilder::createExpression(ArrayRef<int64_t> Signed) {
Duncan P. N. Exon Smithbd75ad42015-02-09 22:13:27 +0000632 // TODO: Remove the callers of this signed version and delete.
633 SmallVector<uint64_t, 8> Addr(Signed.begin(), Signed.end());
634 return createExpression(Addr);
635}
636
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000637DIExpression *DIBuilder::createBitPieceExpression(unsigned OffsetInBytes,
638 unsigned SizeInBytes) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000639 uint64_t Addr[] = {dwarf::DW_OP_bit_piece, OffsetInBytes, SizeInBytes};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000640 return DIExpression::get(VMContext, Addr);
Duncan P. N. Exon Smith9affbba2014-10-01 21:32:12 +0000641}
642
Duncan P. N. Exon Smithb2df6472015-08-26 22:50:16 +0000643template <class... Ts>
644static DISubprogram *getSubprogram(bool IsDistinct, Ts &&... Args) {
645 if (IsDistinct)
646 return DISubprogram::getDistinct(std::forward<Ts>(Args)...);
647 return DISubprogram::get(std::forward<Ts>(Args)...);
648}
649
Peter Collingbourned4bff302015-11-05 22:03:56 +0000650DISubprogram *DIBuilder::createFunction(
651 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *File,
652 unsigned LineNo, DISubroutineType *Ty, bool isLocalToUnit,
653 bool isDefinition, unsigned ScopeLine, unsigned Flags, bool isOptimized,
654 DITemplateParameterArray TParams, DISubprogram *Decl) {
Adrian Prantl75819ae2016-04-15 15:57:41 +0000655 auto *Node = getSubprogram(
656 /* IsDistinct = */ isDefinition, VMContext,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000657 getNonCompileUnitScope(Context), Name, LinkageName, File, LineNo, Ty,
658 isLocalToUnit, isDefinition, ScopeLine, nullptr, 0, 0, Flags, isOptimized,
659 isDefinition ? CUNode : nullptr, TParams, Decl,
Adrian Prantl75819ae2016-04-15 15:57:41 +0000660 MDTuple::getTemporary(VMContext, None).release());
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000661
662 if (isDefinition)
663 AllSubprograms.push_back(Node);
664 trackIfUnresolved(Node);
665 return Node;
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000666}
667
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000668DISubprogram *DIBuilder::createTempFunctionFwdDecl(
669 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *File,
670 unsigned LineNo, DISubroutineType *Ty, bool isLocalToUnit,
671 bool isDefinition, unsigned ScopeLine, unsigned Flags, bool isOptimized,
Peter Collingbourned4bff302015-11-05 22:03:56 +0000672 DITemplateParameterArray TParams, DISubprogram *Decl) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000673 return DISubprogram::getTemporary(
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000674 VMContext, getNonCompileUnitScope(Context), Name, LinkageName,
675 File, LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine, nullptr,
676 0, 0, Flags, isOptimized, isDefinition ? CUNode : nullptr, TParams,
677 Decl, nullptr)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000678 .release();
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000679}
680
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000681DISubprogram *
682DIBuilder::createMethod(DIScope *Context, StringRef Name, StringRef LinkageName,
683 DIFile *F, unsigned LineNo, DISubroutineType *Ty,
Duncan P. N. Exon Smith02083532015-04-16 16:36:23 +0000684 bool isLocalToUnit, bool isDefinition, unsigned VK,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000685 unsigned VIndex, DIType *VTableHolder, unsigned Flags,
Peter Collingbourned4bff302015-11-05 22:03:56 +0000686 bool isOptimized, DITemplateParameterArray TParams) {
Eric Christopher5cb56322013-10-15 23:31:36 +0000687 assert(getNonCompileUnitScope(Context) &&
688 "Methods should have both a Context and a context that isn't "
689 "the compile unit.");
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000690 // FIXME: Do we want to use different scope/lines?
Peter Collingbourned4bff302015-11-05 22:03:56 +0000691 auto *SP = getSubprogram(
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000692 /* IsDistinct = */ isDefinition, VMContext, cast<DIScope>(Context), Name,
693 LinkageName, F, LineNo, Ty, isLocalToUnit, isDefinition, LineNo,
694 VTableHolder, VK, VIndex, Flags, isOptimized,
695 isDefinition ? CUNode : nullptr, TParams, nullptr, nullptr);
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000696
David Blaikie595eb442013-02-18 07:10:22 +0000697 if (isDefinition)
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000698 AllSubprograms.push_back(SP);
699 trackIfUnresolved(SP);
700 return SP;
Devang Patelb68c6232010-12-08 20:42:44 +0000701}
702
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000703DINamespace *DIBuilder::createNameSpace(DIScope *Scope, StringRef Name,
704 DIFile *File, unsigned LineNo) {
705 return DINamespace::get(VMContext, getNonCompileUnitScope(Scope), File, Name,
Duncan P. N. Exon Smitha5099dc2015-04-06 19:49:39 +0000706 LineNo);
Devang Patel746660f2010-12-07 23:25:47 +0000707}
708
Adrian Prantlab1243f2015-06-29 23:03:47 +0000709DIModule *DIBuilder::createModule(DIScope *Scope, StringRef Name,
710 StringRef ConfigurationMacros,
711 StringRef IncludePath,
712 StringRef ISysRoot) {
713 return DIModule::get(VMContext, getNonCompileUnitScope(Scope), Name,
714 ConfigurationMacros, IncludePath, ISysRoot);
715}
716
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000717DILexicalBlockFile *DIBuilder::createLexicalBlockFile(DIScope *Scope,
718 DIFile *File,
719 unsigned Discriminator) {
720 return DILexicalBlockFile::get(VMContext, Scope, File, Discriminator);
Eric Christopher6647b832011-10-11 22:59:11 +0000721}
722
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000723DILexicalBlock *DIBuilder::createLexicalBlock(DIScope *Scope, DIFile *File,
724 unsigned Line, unsigned Col) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000725 // Make these distinct, to avoid merging two lexical blocks on the same
726 // file/line/column.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000727 return DILexicalBlock::getDistinct(VMContext, getNonCompileUnitScope(Scope),
Duncan P. N. Exon Smith35ef22c2015-04-15 23:19:27 +0000728 File, Line, Col);
Devang Patel89ea4f22010-12-08 01:50:15 +0000729}
730
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000731static Value *getDbgIntrinsicValueImpl(LLVMContext &VMContext, Value *V) {
732 assert(V && "no value passed to dbg intrinsic");
733 return MetadataAsValue::get(VMContext, ValueAsMetadata::get(V));
734}
735
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000736static Instruction *withDebugLoc(Instruction *I, const DILocation *DL) {
737 I->setDebugLoc(const_cast<DILocation *>(DL));
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000738 return I;
739}
740
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000741Instruction *DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
742 DIExpression *Expr, const DILocation *DL,
Devang Patel746660f2010-12-07 23:25:47 +0000743 Instruction *InsertBefore) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000744 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.declare");
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000745 assert(DL && "Expected debug loc");
746 assert(DL->getScope()->getSubprogram() ==
747 VarInfo->getScope()->getSubprogram() &&
748 "Expected matching subprograms");
Devang Patel746660f2010-12-07 23:25:47 +0000749 if (!DeclareFn)
750 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
751
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000752 trackIfUnresolved(VarInfo);
753 trackIfUnresolved(Expr);
754 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, Storage),
755 MetadataAsValue::get(VMContext, VarInfo),
756 MetadataAsValue::get(VMContext, Expr)};
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000757 return withDebugLoc(CallInst::Create(DeclareFn, Args, "", InsertBefore), DL);
Devang Patel746660f2010-12-07 23:25:47 +0000758}
759
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000760Instruction *DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
761 DIExpression *Expr, const DILocation *DL,
Devang Patel746660f2010-12-07 23:25:47 +0000762 BasicBlock *InsertAtEnd) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000763 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.declare");
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000764 assert(DL && "Expected debug loc");
765 assert(DL->getScope()->getSubprogram() ==
766 VarInfo->getScope()->getSubprogram() &&
767 "Expected matching subprograms");
Devang Patel746660f2010-12-07 23:25:47 +0000768 if (!DeclareFn)
769 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
770
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000771 trackIfUnresolved(VarInfo);
772 trackIfUnresolved(Expr);
773 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, Storage),
774 MetadataAsValue::get(VMContext, VarInfo),
775 MetadataAsValue::get(VMContext, Expr)};
Devang Patel746660f2010-12-07 23:25:47 +0000776
777 // If this block already has a terminator then insert this intrinsic
778 // before the terminator.
779 if (TerminatorInst *T = InsertAtEnd->getTerminator())
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000780 return withDebugLoc(CallInst::Create(DeclareFn, Args, "", T), DL);
781 return withDebugLoc(CallInst::Create(DeclareFn, Args, "", InsertAtEnd), DL);
Devang Patel746660f2010-12-07 23:25:47 +0000782}
783
Devang Patel9b412732011-02-22 18:56:12 +0000784Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000785 DILocalVariable *VarInfo,
786 DIExpression *Expr,
787 const DILocation *DL,
Devang Patel746660f2010-12-07 23:25:47 +0000788 Instruction *InsertBefore) {
789 assert(V && "no value passed to dbg.value");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000790 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.value");
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000791 assert(DL && "Expected debug loc");
792 assert(DL->getScope()->getSubprogram() ==
793 VarInfo->getScope()->getSubprogram() &&
794 "Expected matching subprograms");
Devang Patel746660f2010-12-07 23:25:47 +0000795 if (!ValueFn)
796 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
797
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000798 trackIfUnresolved(VarInfo);
799 trackIfUnresolved(Expr);
800 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, V),
801 ConstantInt::get(Type::getInt64Ty(VMContext), Offset),
802 MetadataAsValue::get(VMContext, VarInfo),
803 MetadataAsValue::get(VMContext, Expr)};
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000804 return withDebugLoc(CallInst::Create(ValueFn, Args, "", InsertBefore), DL);
Devang Patel746660f2010-12-07 23:25:47 +0000805}
806
Devang Patel9b412732011-02-22 18:56:12 +0000807Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000808 DILocalVariable *VarInfo,
809 DIExpression *Expr,
810 const DILocation *DL,
Devang Patel746660f2010-12-07 23:25:47 +0000811 BasicBlock *InsertAtEnd) {
812 assert(V && "no value passed to dbg.value");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000813 assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.value");
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000814 assert(DL && "Expected debug loc");
815 assert(DL->getScope()->getSubprogram() ==
816 VarInfo->getScope()->getSubprogram() &&
817 "Expected matching subprograms");
Devang Patel746660f2010-12-07 23:25:47 +0000818 if (!ValueFn)
819 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
820
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000821 trackIfUnresolved(VarInfo);
822 trackIfUnresolved(Expr);
823 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, V),
824 ConstantInt::get(Type::getInt64Ty(VMContext), Offset),
825 MetadataAsValue::get(VMContext, VarInfo),
826 MetadataAsValue::get(VMContext, Expr)};
Duncan P. N. Exon Smithcd1aecf2015-04-15 21:18:07 +0000827
828 return withDebugLoc(CallInst::Create(ValueFn, Args, "", InsertAtEnd), DL);
Devang Patel746660f2010-12-07 23:25:47 +0000829}
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000830
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000831void DIBuilder::replaceVTableHolder(DICompositeType *&T,
832 DICompositeType *VTableHolder) {
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000833 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000834 TypedTrackingMDRef<DICompositeType> N(T);
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000835 N->replaceVTableHolder(VTableHolder);
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000836 T = N.get();
837 }
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000838
839 // If this didn't create a self-reference, just return.
840 if (T != VTableHolder)
841 return;
842
Adrian Prantl18a25b02015-02-11 17:45:10 +0000843 // Look for unresolved operands. T will drop RAUW support, orphaning any
844 // cycles underneath it.
845 if (T->isResolved())
846 for (const MDOperand &O : T->operands())
847 if (auto *N = dyn_cast_or_null<MDNode>(O))
848 trackIfUnresolved(N);
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000849}
850
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000851void DIBuilder::replaceArrays(DICompositeType *&T, DINodeArray Elements,
852 DINodeArray TParams) {
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000853 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000854 TypedTrackingMDRef<DICompositeType> N(T);
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000855 if (Elements)
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000856 N->replaceElements(Elements);
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000857 if (TParams)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000858 N->replaceTemplateParams(DITemplateParameterArray(TParams));
Duncan P. N. Exon Smith8d33fdc2015-04-07 04:12:02 +0000859 T = N.get();
860 }
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000861
862 // If T isn't resolved, there's no problem.
863 if (!T->isResolved())
864 return;
865
Adrian Prantl12d52842015-07-10 23:26:02 +0000866 // 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 +0000867 // arrays explicitly if they're unresolved, or else the cycles will be
868 // orphaned.
869 if (Elements)
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +0000870 trackIfUnresolved(Elements.get());
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000871 if (TParams)
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +0000872 trackIfUnresolved(TParams.get());
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +0000873}