blob: 3394cff35206be8d6ea4add21b1beaf688fe2ed8 [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};
Devang Patel57c5a202010-11-04 15:01:38 +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)
Craig Topperc6207612014-04-09 06:08:46 +000061 : M(m), VMContext(M.getContext()), TempEnumTypes(nullptr),
62 TempRetainTypes(nullptr), TempSubprograms(nullptr), TempGVs(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() {
Devang Pateleb1bb4e2011-08-16 22:09:43 +000077 DIArray Enums = getOrCreateArray(AllEnumTypes);
78 DIType(TempEnumTypes).replaceAllUsesWith(Enums);
79
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000080 SmallVector<Metadata *, 16> RetainValues;
Manman Ren0b410402013-08-29 23:17:54 +000081 // Declarations and definitions of the same type may be retained. Some
82 // clients RAUW these pairs, leaving duplicates in the retained types
83 // list. Use a set to remove the duplicates while we transform the
84 // TrackingVHs back into Values.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000085 SmallPtrSet<Metadata *, 16> RetainSet;
Manman Ren0b410402013-08-29 23:17:54 +000086 for (unsigned I = 0, E = AllRetainTypes.size(); I < E; I++)
David Blaikie70573dc2014-11-19 07:49:26 +000087 if (RetainSet.insert(AllRetainTypes[I]).second)
Manman Ren0b410402013-08-29 23:17:54 +000088 RetainValues.push_back(AllRetainTypes[I]);
89 DIArray RetainTypes = getOrCreateArray(RetainValues);
Devang Pateleb1bb4e2011-08-16 22:09:43 +000090 DIType(TempRetainTypes).replaceAllUsesWith(RetainTypes);
91
92 DIArray SPs = getOrCreateArray(AllSubprograms);
93 DIType(TempSubprograms).replaceAllUsesWith(SPs);
Devang Patel59e27c52011-08-19 23:28:12 +000094 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
95 DISubprogram SP(SPs.getElement(i));
Eric Christopher27deb262012-04-23 19:00:11 +000096 if (MDNode *Temp = SP.getVariablesNodes()) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000097 SmallVector<Metadata *, 4> Variables;
98 for (Metadata *V : PreservedVariables.lookup(SP))
Duncan P. N. Exon Smith3bfffde2014-10-15 16:11:41 +000099 Variables.push_back(V);
Eric Christopher27deb262012-04-23 19:00:11 +0000100 DIArray AV = getOrCreateArray(Variables);
101 DIType(Temp).replaceAllUsesWith(AV);
102 }
Devang Patel59e27c52011-08-19 23:28:12 +0000103 }
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000104
105 DIArray GVs = getOrCreateArray(AllGVs);
106 DIType(TempGVs).replaceAllUsesWith(GVs);
David Blaikief55abea2013-04-22 06:12:31 +0000107
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000108 SmallVector<Metadata *, 16> RetainValuesI;
Eric Christopher2c3a6dc2014-02-28 21:27:57 +0000109 for (unsigned I = 0, E = AllImportedModules.size(); I < E; I++)
110 RetainValuesI.push_back(AllImportedModules[I]);
111 DIArray IMs = getOrCreateArray(RetainValuesI);
David Blaikief55abea2013-04-22 06:12:31 +0000112 DIType(TempImportedModules).replaceAllUsesWith(IMs);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000113
114 // Now that all temp nodes have been replaced or deleted, resolve remaining
115 // cycles.
116 for (const auto &N : UnresolvedNodes)
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +0000117 if (N && !N->isResolved())
118 N->resolveCycles();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000119 UnresolvedNodes.clear();
120
121 // Can't handle unresolved nodes anymore.
122 AllowUnresolvedNodes = false;
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000123}
124
Duncan P. N. Exon Smith379e3752014-10-01 21:32:15 +0000125/// If N is compile unit return NULL otherwise return N.
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000126static MDNode *getNonCompileUnitScope(MDNode *N) {
127 if (DIDescriptor(N).isCompileUnit())
Craig Topperc6207612014-04-09 06:08:46 +0000128 return nullptr;
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000129 return N;
Devang Patel2b8acaf2011-08-15 23:00:00 +0000130}
131
David Blaikieefb0d652013-03-20 23:58:12 +0000132static MDNode *createFilePathPair(LLVMContext &VMContext, StringRef Filename,
133 StringRef Directory) {
134 assert(!Filename.empty() && "Unable to create file without name");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000135 Metadata *Pair[] = {MDString::get(VMContext, Filename),
136 MDString::get(VMContext, Directory)};
David Blaikieefb0d652013-03-20 23:58:12 +0000137 return MDNode::get(VMContext, Pair);
138}
139
Eric Christopher03b3e112013-07-19 00:51:47 +0000140DICompileUnit DIBuilder::createCompileUnit(unsigned Lang, StringRef Filename,
141 StringRef Directory,
142 StringRef Producer, bool isOptimized,
143 StringRef Flags, unsigned RunTimeVer,
Eric Christopher75d49db2014-02-27 01:24:56 +0000144 StringRef SplitName,
Diego Novillo56653fd2014-06-24 17:02:03 +0000145 DebugEmissionKind Kind,
146 bool EmitDebugInfo) {
Eric Christopher75d49db2014-02-27 01:24:56 +0000147
Bruce Mitchener7e575ed2015-02-07 06:35:30 +0000148 assert(((Lang <= dwarf::DW_LANG_Fortran08 && Lang >= dwarf::DW_LANG_C89) ||
Chandler Carruth4c0ee742012-01-10 18:18:52 +0000149 (Lang <= dwarf::DW_LANG_hi_user && Lang >= dwarf::DW_LANG_lo_user)) &&
150 "Invalid Language tag");
151 assert(!Filename.empty() &&
152 "Unable to create compile unit without filename");
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000153
Duncan P. N. Exon Smithb93569d2015-02-12 21:52:11 +0000154 // TODO: Once we make MDCompileUnit distinct, stop using temporaries here
155 // (just start with operands assigned to nullptr).
156 TempEnumTypes = MDTuple::getTemporary(VMContext, None).release();
157 TempRetainTypes = MDTuple::getTemporary(VMContext, None).release();
158 TempSubprograms = MDTuple::getTemporary(VMContext, None).release();
159 TempGVs = MDTuple::getTemporary(VMContext, None).release();
160 TempImportedModules = MDTuple::getTemporary(VMContext, None).release();
David Blaikief55abea2013-04-22 06:12:31 +0000161
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000162 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_compile_unit)
163 .concat(Lang)
164 .concat(Producer)
165 .concat(isOptimized)
166 .concat(Flags)
167 .concat(RunTimeVer)
168 .concat(SplitName)
169 .concat(Kind)
170 .get(VMContext),
171 createFilePathPair(VMContext, Filename, Directory),
172 TempEnumTypes, TempRetainTypes, TempSubprograms, TempGVs,
173 TempImportedModules};
Eric Christopher03b3e112013-07-19 00:51:47 +0000174
Duncan P. N. Exon Smithb93569d2015-02-12 21:52:11 +0000175 // TODO: Switch to getDistinct(). We never want to merge compile units based
176 // on contents.
Eric Christopher03b3e112013-07-19 00:51:47 +0000177 MDNode *CUNode = MDNode::get(VMContext, Elts);
Devang Patel09fa69e2011-05-03 16:18:28 +0000178
179 // Create a named metadata so that it is easier to find cu in a module.
Diego Novillo56653fd2014-06-24 17:02:03 +0000180 // Note that we only generate this when the caller wants to actually
181 // emit debug information. When we are only interested in tracking
182 // source line locations throughout the backend, we prevent codegen from
183 // emitting debug info in the final output by not generating llvm.dbg.cu.
184 if (EmitDebugInfo) {
185 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
186 NMD->addOperand(CUNode);
187 }
Eric Christopher03b3e112013-07-19 00:51:47 +0000188
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000189 trackIfUnresolved(CUNode);
Eric Christopher03b3e112013-07-19 00:51:47 +0000190 return DICompileUnit(CUNode);
Devang Patel57c5a202010-11-04 15:01:38 +0000191}
192
David Blaikiee63d5d12013-05-20 22:50:35 +0000193static DIImportedEntity
David Blaikie2a40c142014-04-06 06:29:01 +0000194createImportedModule(LLVMContext &C, dwarf::Tag Tag, DIScope Context,
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000195 Metadata *NS, unsigned Line, StringRef Name,
196 SmallVectorImpl<TrackingMDNodeRef> &AllImportedModules) {
David Blaikiee63d5d12013-05-20 22:50:35 +0000197 const MDNode *R;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000198 Metadata *Elts[] = {HeaderBuilder::get(Tag).concat(Line).concat(Name).get(C),
199 Context, NS};
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000200 R = MDNode::get(C, Elts);
David Blaikiee63d5d12013-05-20 22:50:35 +0000201 DIImportedEntity M(R);
David Blaikie1fd43652013-05-07 21:35:53 +0000202 assert(M.Verify() && "Imported module should be valid");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000203 AllImportedModules.emplace_back(M.get());
David Blaikie1fd43652013-05-07 21:35:53 +0000204 return M;
205}
206
David Blaikiee63d5d12013-05-20 22:50:35 +0000207DIImportedEntity DIBuilder::createImportedModule(DIScope Context,
David Blaikie2a40c142014-04-06 06:29:01 +0000208 DINameSpace NS,
209 unsigned Line) {
210 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
211 Context, NS, Line, StringRef(), AllImportedModules);
David Blaikiee63d5d12013-05-20 22:50:35 +0000212}
213
214DIImportedEntity DIBuilder::createImportedModule(DIScope Context,
215 DIImportedEntity NS,
David Blaikie2a40c142014-04-06 06:29:01 +0000216 unsigned Line) {
217 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
218 Context, NS, Line, StringRef(), AllImportedModules);
David Blaikiee63d5d12013-05-20 22:50:35 +0000219}
220
David Blaikie1fd43652013-05-07 21:35:53 +0000221DIImportedEntity DIBuilder::createImportedDeclaration(DIScope Context,
Frederic Riss4aa51ae2014-11-06 17:46:55 +0000222 DIDescriptor Decl,
David Blaikie2a40c142014-04-06 06:29:01 +0000223 unsigned Line, StringRef Name) {
Frederic Riss4aa51ae2014-11-06 17:46:55 +0000224 // Make sure to use the unique identifier based metadata reference for
225 // types that have one.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000226 Metadata *V =
227 Decl.isType() ? static_cast<Metadata *>(DIType(Decl).getRef()) : Decl;
David Blaikie2a40c142014-04-06 06:29:01 +0000228 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_declaration,
Frederic Riss4aa51ae2014-11-06 17:46:55 +0000229 Context, V, Line, Name,
David Blaikie2a40c142014-04-06 06:29:01 +0000230 AllImportedModules);
231}
232
233DIImportedEntity DIBuilder::createImportedDeclaration(DIScope Context,
234 DIImportedEntity Imp,
235 unsigned Line, StringRef Name) {
236 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_declaration,
237 Context, Imp, Line, Name, AllImportedModules);
David Blaikief55abea2013-04-22 06:12:31 +0000238}
239
Devang Patel9b412732011-02-22 18:56:12 +0000240DIFile DIBuilder::createFile(StringRef Filename, StringRef Directory) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000241 Metadata *Elts[] = {
242 HeaderBuilder::get(dwarf::DW_TAG_file_type).get(VMContext),
243 createFilePathPair(VMContext, Filename, Directory)};
David Blaikie5692e722013-03-28 02:44:59 +0000244 return DIFile(MDNode::get(VMContext, Elts));
Devang Patel57c5a202010-11-04 15:01:38 +0000245}
246
David Blaikieb7619002013-06-24 17:34:33 +0000247DIEnumerator DIBuilder::createEnumerator(StringRef Name, int64_t Val) {
Devang Patel1ad1abe2011-09-12 18:26:08 +0000248 assert(!Name.empty() && "Unable to create enumerator without name");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000249 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_enumerator)
250 .concat(Name)
251 .concat(Val)
252 .get(VMContext)};
David Blaikie5692e722013-03-28 02:44:59 +0000253 return DIEnumerator(MDNode::get(VMContext, Elts));
Devang Patel57c5a202010-11-04 15:01:38 +0000254}
255
Peter Collingbournea4a47cb2013-06-27 22:50:59 +0000256DIBasicType DIBuilder::createUnspecifiedType(StringRef Name) {
Devang Patel04d6d472011-09-14 23:13:28 +0000257 assert(!Name.empty() && "Unable to create type without name");
Peter Collingbournea4a47cb2013-06-27 22:50:59 +0000258 // Unspecified types are encoded in DIBasicType format. Line number, filename,
259 // size, alignment, offset and flags are always empty here.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000260 Metadata *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000261 HeaderBuilder::get(dwarf::DW_TAG_unspecified_type)
262 .concat(Name)
263 .concat(0)
264 .concat(0)
265 .concat(0)
266 .concat(0)
267 .concat(0)
268 .concat(0)
269 .get(VMContext),
270 nullptr, // Filename
271 nullptr // Unused
Devang Patel04d6d472011-09-14 23:13:28 +0000272 };
Manman Ren3c6acec2013-06-07 18:35:53 +0000273 return DIBasicType(MDNode::get(VMContext, Elts));
Devang Patel04d6d472011-09-14 23:13:28 +0000274}
275
Peter Collingbournea4a47cb2013-06-27 22:50:59 +0000276DIBasicType DIBuilder::createNullPtrType() {
277 return createUnspecifiedType("decltype(nullptr)");
278}
279
David Blaikie209d63a2013-02-12 00:40:41 +0000280DIBasicType
281DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
282 uint64_t AlignInBits, unsigned Encoding) {
Devang Patel1ad1abe2011-09-12 18:26:08 +0000283 assert(!Name.empty() && "Unable to create type without name");
Devang Patel57c5a202010-11-04 15:01:38 +0000284 // Basic types are encoded in DIBasicType format. Line number, filename,
285 // offset and flags are always empty here.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000286 Metadata *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000287 HeaderBuilder::get(dwarf::DW_TAG_base_type)
288 .concat(Name)
289 .concat(0) // Line
290 .concat(SizeInBits)
291 .concat(AlignInBits)
292 .concat(0) // Offset
293 .concat(0) // Flags
294 .concat(Encoding)
295 .get(VMContext),
296 nullptr, // Filename
297 nullptr // Unused
Benjamin Kramered8b7bf2010-11-04 18:45:27 +0000298 };
David Blaikie5692e722013-03-28 02:44:59 +0000299 return DIBasicType(MDNode::get(VMContext, Elts));
Devang Patel57c5a202010-11-04 15:01:38 +0000300}
301
David Blaikief11de2f2013-02-18 06:41:57 +0000302DIDerivedType DIBuilder::createQualifiedType(unsigned Tag, DIType FromTy) {
Benjamin Kramered8b7bf2010-11-04 18:45:27 +0000303 // Qualified types are encoded in DIDerivedType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000304 Metadata *Elts[] = {HeaderBuilder::get(Tag)
305 .concat(StringRef()) // Name
306 .concat(0) // Line
307 .concat(0) // Size
308 .concat(0) // Align
309 .concat(0) // Offset
310 .concat(0) // Flags
311 .get(VMContext),
312 nullptr, // Filename
313 nullptr, // Unused
314 FromTy.getRef()};
David Blaikie5692e722013-03-28 02:44:59 +0000315 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel57c5a202010-11-04 15:01:38 +0000316}
317
David Blaikief11de2f2013-02-18 06:41:57 +0000318DIDerivedType
319DIBuilder::createPointerType(DIType PointeeTy, uint64_t SizeInBits,
320 uint64_t AlignInBits, StringRef Name) {
Benjamin Kramered8b7bf2010-11-04 18:45:27 +0000321 // Pointer types are encoded in DIDerivedType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000322 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_pointer_type)
323 .concat(Name)
324 .concat(0) // Line
325 .concat(SizeInBits)
326 .concat(AlignInBits)
327 .concat(0) // Offset
328 .concat(0) // Flags
329 .get(VMContext),
330 nullptr, // Filename
331 nullptr, // Unused
332 PointeeTy.getRef()};
David Blaikie5692e722013-03-28 02:44:59 +0000333 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel57c5a202010-11-04 15:01:38 +0000334}
335
Adrian Prantl48af2ef2014-12-23 19:11:47 +0000336DIDerivedType
337DIBuilder::createMemberPointerType(DIType PointeeTy, DIType Base,
338 uint64_t SizeInBits, uint64_t AlignInBits) {
David Blaikie5d3249b2013-01-07 05:51:15 +0000339 // Pointer types are encoded in DIDerivedType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000340 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_ptr_to_member_type)
341 .concat(StringRef())
342 .concat(0) // Line
Adrian Prantl48af2ef2014-12-23 19:11:47 +0000343 .concat(SizeInBits) // Size
344 .concat(AlignInBits) // Align
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000345 .concat(0) // Offset
346 .concat(0) // Flags
347 .get(VMContext),
348 nullptr, // Filename
349 nullptr, // Unused
350 PointeeTy.getRef(), Base.getRef()};
David Blaikie5692e722013-03-28 02:44:59 +0000351 return DIDerivedType(MDNode::get(VMContext, Elts));
David Blaikie5d3249b2013-01-07 05:51:15 +0000352}
353
David Blaikief11de2f2013-02-18 06:41:57 +0000354DIDerivedType DIBuilder::createReferenceType(unsigned Tag, DIType RTy) {
Manman Ren74c188f2013-07-01 21:02:01 +0000355 assert(RTy.isType() && "Unable to create reference type");
Benjamin Kramered8b7bf2010-11-04 18:45:27 +0000356 // References are encoded in DIDerivedType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000357 Metadata *Elts[] = {HeaderBuilder::get(Tag)
358 .concat(StringRef()) // Name
359 .concat(0) // Line
360 .concat(0) // Size
361 .concat(0) // Align
362 .concat(0) // Offset
363 .concat(0) // Flags
364 .get(VMContext),
365 nullptr, // Filename
366 nullptr, // TheCU,
367 RTy.getRef()};
David Blaikie5692e722013-03-28 02:44:59 +0000368 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel57c5a202010-11-04 15:01:38 +0000369}
370
David Blaikief11de2f2013-02-18 06:41:57 +0000371DIDerivedType DIBuilder::createTypedef(DIType Ty, StringRef Name, DIFile File,
372 unsigned LineNo, DIDescriptor Context) {
Benjamin Kramered8b7bf2010-11-04 18:45:27 +0000373 // typedefs are encoded in DIDerivedType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000374 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_typedef)
375 .concat(Name)
376 .concat(LineNo)
377 .concat(0) // Size
378 .concat(0) // Align
379 .concat(0) // Offset
380 .concat(0) // Flags
381 .get(VMContext),
382 File.getFileNode(),
383 DIScope(getNonCompileUnitScope(Context)).getRef(),
384 Ty.getRef()};
David Blaikie5692e722013-03-28 02:44:59 +0000385 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel57c5a202010-11-04 15:01:38 +0000386}
387
Manman Ren3c6acec2013-06-07 18:35:53 +0000388DIDerivedType DIBuilder::createFriend(DIType Ty, DIType FriendTy) {
Benjamin Kramered8b7bf2010-11-04 18:45:27 +0000389 // typedefs are encoded in DIDerivedType format.
Manman Ren74c188f2013-07-01 21:02:01 +0000390 assert(Ty.isType() && "Invalid type!");
391 assert(FriendTy.isType() && "Invalid friend type!");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000392 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_friend)
393 .concat(StringRef()) // Name
394 .concat(0) // Line
395 .concat(0) // Size
396 .concat(0) // Align
397 .concat(0) // Offset
398 .concat(0) // Flags
399 .get(VMContext),
400 nullptr, Ty.getRef(), FriendTy.getRef()};
Manman Ren3c6acec2013-06-07 18:35:53 +0000401 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel57c5a202010-11-04 15:01:38 +0000402}
403
Eric Christopher98f9c232013-10-15 23:31:31 +0000404DIDerivedType DIBuilder::createInheritance(DIType Ty, DIType BaseTy,
405 uint64_t BaseOffset,
406 unsigned Flags) {
Manman Ren74c188f2013-07-01 21:02:01 +0000407 assert(Ty.isType() && "Unable to create inheritance");
Benjamin Kramered8b7bf2010-11-04 18:45:27 +0000408 // TAG_inheritance is encoded in DIDerivedType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000409 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_inheritance)
410 .concat(StringRef()) // Name
411 .concat(0) // Line
412 .concat(0) // Size
413 .concat(0) // Align
414 .concat(BaseOffset)
415 .concat(Flags)
416 .get(VMContext),
417 nullptr, Ty.getRef(), BaseTy.getRef()};
Adrian Prantl9a804922015-02-11 17:45:08 +0000418 auto R = DIDerivedType(MDNode::get(VMContext, Elts));
419 trackIfUnresolved(R);
420 return R;
Devang Patel57c5a202010-11-04 15:01:38 +0000421}
422
Eric Christopher98f9c232013-10-15 23:31:31 +0000423DIDerivedType DIBuilder::createMemberType(DIDescriptor Scope, StringRef Name,
424 DIFile File, unsigned LineNumber,
425 uint64_t SizeInBits,
426 uint64_t AlignInBits,
427 uint64_t OffsetInBits, unsigned Flags,
428 DIType Ty) {
Benjamin Kramered8b7bf2010-11-04 18:45:27 +0000429 // TAG_member is encoded in DIDerivedType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000430 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_member)
431 .concat(Name)
432 .concat(LineNumber)
433 .concat(SizeInBits)
434 .concat(AlignInBits)
435 .concat(OffsetInBits)
436 .concat(Flags)
437 .get(VMContext),
438 File.getFileNode(),
439 DIScope(getNonCompileUnitScope(Scope)).getRef(),
440 Ty.getRef()};
David Blaikie5692e722013-03-28 02:44:59 +0000441 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel57c5a202010-11-04 15:01:38 +0000442}
443
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000444static Metadata *getConstantOrNull(Constant *C) {
445 if (C)
446 return ConstantAsMetadata::get(C);
447 return nullptr;
448}
449
Duncan P. N. Exon Smithdbf64acd2014-11-15 00:23:49 +0000450DIDerivedType DIBuilder::createStaticMemberType(DIDescriptor Scope,
451 StringRef Name, DIFile File,
452 unsigned LineNumber, DIType Ty,
453 unsigned Flags,
454 llvm::Constant *Val) {
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000455 // TAG_member is encoded in DIDerivedType format.
456 Flags |= DIDescriptor::FlagStaticMember;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000457 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_member)
458 .concat(Name)
459 .concat(LineNumber)
460 .concat(0) // Size
461 .concat(0) // Align
462 .concat(0) // Offset
463 .concat(Flags)
464 .get(VMContext),
465 File.getFileNode(),
466 DIScope(getNonCompileUnitScope(Scope)).getRef(),
467 Ty.getRef(), getConstantOrNull(Val)};
Manman Ren3c6acec2013-06-07 18:35:53 +0000468 return DIDerivedType(MDNode::get(VMContext, Elts));
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000469}
470
Eric Christopher98f9c232013-10-15 23:31:31 +0000471DIDerivedType DIBuilder::createObjCIVar(StringRef Name, DIFile File,
472 unsigned LineNumber,
473 uint64_t SizeInBits,
474 uint64_t AlignInBits,
475 uint64_t OffsetInBits, unsigned Flags,
476 DIType Ty, MDNode *PropertyNode) {
Devang Patel44882172012-02-06 17:49:43 +0000477 // TAG_member is encoded in DIDerivedType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000478 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_member)
479 .concat(Name)
480 .concat(LineNumber)
481 .concat(SizeInBits)
482 .concat(AlignInBits)
483 .concat(OffsetInBits)
484 .concat(Flags)
485 .get(VMContext),
486 File.getFileNode(), getNonCompileUnitScope(File), Ty,
487 PropertyNode};
Manman Ren3c6acec2013-06-07 18:35:53 +0000488 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel44882172012-02-06 17:49:43 +0000489}
490
Eric Christopher98f9c232013-10-15 23:31:31 +0000491DIObjCProperty
492DIBuilder::createObjCProperty(StringRef Name, DIFile File, unsigned LineNumber,
493 StringRef GetterName, StringRef SetterName,
494 unsigned PropertyAttributes, DIType Ty) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000495 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_APPLE_property)
496 .concat(Name)
497 .concat(LineNumber)
498 .concat(GetterName)
499 .concat(SetterName)
500 .concat(PropertyAttributes)
501 .get(VMContext),
502 File, Ty};
David Blaikie5692e722013-03-28 02:44:59 +0000503 return DIObjCProperty(MDNode::get(VMContext, Elts));
Devang Patelcc481592012-02-04 00:59:25 +0000504}
505
Eric Christopher3cc90fe2011-08-26 21:02:40 +0000506DITemplateTypeParameter
Devang Patel9b412732011-02-22 18:56:12 +0000507DIBuilder::createTemplateTypeParameter(DIDescriptor Context, StringRef Name,
Devang Patel3a9e65e2011-02-02 21:38:25 +0000508 DIType Ty, MDNode *File, unsigned LineNo,
509 unsigned ColumnNo) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000510 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_template_type_parameter)
511 .concat(Name)
512 .concat(LineNo)
513 .concat(ColumnNo)
514 .get(VMContext),
515 DIScope(getNonCompileUnitScope(Context)).getRef(),
516 Ty.getRef(), File};
David Blaikie5692e722013-03-28 02:44:59 +0000517 return DITemplateTypeParameter(MDNode::get(VMContext, Elts));
Devang Patel3a9e65e2011-02-02 21:38:25 +0000518}
519
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000520static DITemplateValueParameter createTemplateValueParameterHelper(
521 LLVMContext &VMContext, unsigned Tag, DIDescriptor Context, StringRef Name,
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000522 DIType Ty, Metadata *MD, MDNode *File, unsigned LineNo, unsigned ColumnNo) {
523 Metadata *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000524 HeaderBuilder::get(Tag).concat(Name).concat(LineNo).concat(ColumnNo).get(
525 VMContext),
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000526 DIScope(getNonCompileUnitScope(Context)).getRef(), Ty.getRef(), MD, File};
David Blaikie5692e722013-03-28 02:44:59 +0000527 return DITemplateValueParameter(MDNode::get(VMContext, Elts));
Devang Patelbe933b42011-02-02 22:35:53 +0000528}
529
David Blaikie2b380232013-06-22 18:59:11 +0000530DITemplateValueParameter
531DIBuilder::createTemplateValueParameter(DIDescriptor Context, StringRef Name,
Duncan P. N. Exon Smithdbf64acd2014-11-15 00:23:49 +0000532 DIType Ty, Constant *Val, MDNode *File,
533 unsigned LineNo, unsigned ColumnNo) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000534 return createTemplateValueParameterHelper(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000535 VMContext, dwarf::DW_TAG_template_value_parameter, Context, Name, Ty,
536 getConstantOrNull(Val), File, LineNo, ColumnNo);
David Blaikie2b380232013-06-22 18:59:11 +0000537}
538
539DITemplateValueParameter
540DIBuilder::createTemplateTemplateParameter(DIDescriptor Context, StringRef Name,
541 DIType Ty, StringRef Val,
542 MDNode *File, unsigned LineNo,
543 unsigned ColumnNo) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000544 return createTemplateValueParameterHelper(
545 VMContext, dwarf::DW_TAG_GNU_template_template_param, Context, Name, Ty,
David Blaikie2b380232013-06-22 18:59:11 +0000546 MDString::get(VMContext, Val), File, LineNo, ColumnNo);
547}
548
549DITemplateValueParameter
550DIBuilder::createTemplateParameterPack(DIDescriptor Context, StringRef Name,
551 DIType Ty, DIArray Val,
552 MDNode *File, unsigned LineNo,
553 unsigned ColumnNo) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000554 return createTemplateValueParameterHelper(
555 VMContext, dwarf::DW_TAG_GNU_template_parameter_pack, Context, Name, Ty,
556 Val, File, LineNo, ColumnNo);
David Blaikie2b380232013-06-22 18:59:11 +0000557}
558
David Blaikiea7310a32013-03-26 23:46:39 +0000559DICompositeType DIBuilder::createClassType(DIDescriptor Context, StringRef Name,
560 DIFile File, unsigned LineNumber,
561 uint64_t SizeInBits,
562 uint64_t AlignInBits,
563 uint64_t OffsetInBits,
564 unsigned Flags, DIType DerivedFrom,
565 DIArray Elements,
Manman Ren27552062013-09-06 23:54:23 +0000566 DIType VTableHolder,
Manman Ren547467b2013-08-27 23:06:40 +0000567 MDNode *TemplateParams,
568 StringRef UniqueIdentifier) {
Manman Ren74c188f2013-07-01 21:02:01 +0000569 assert((!Context || Context.isScope() || Context.isType()) &&
David Blaikie085abe32013-03-11 23:21:19 +0000570 "createClassType should be called with a valid Context");
571 // TAG_class_type is encoded in DICompositeType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000572 Metadata *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000573 HeaderBuilder::get(dwarf::DW_TAG_class_type)
574 .concat(Name)
575 .concat(LineNumber)
576 .concat(SizeInBits)
577 .concat(AlignInBits)
578 .concat(OffsetInBits)
579 .concat(Flags)
580 .concat(0)
581 .get(VMContext),
582 File.getFileNode(), DIScope(getNonCompileUnitScope(Context)).getRef(),
583 DerivedFrom.getRef(), Elements, VTableHolder.getRef(), TemplateParams,
584 UniqueIdentifier.empty() ? nullptr
585 : MDString::get(VMContext, UniqueIdentifier)};
David Blaikiea7310a32013-03-26 23:46:39 +0000586 DICompositeType R(MDNode::get(VMContext, Elts));
Manman Ren74c188f2013-07-01 21:02:01 +0000587 assert(R.isCompositeType() &&
588 "createClassType should return a DICompositeType");
Manman Ren0b410402013-08-29 23:17:54 +0000589 if (!UniqueIdentifier.empty())
590 retainType(R);
David Blaikie085abe32013-03-11 23:21:19 +0000591 return R;
Eric Christopher17426692012-07-06 02:35:57 +0000592}
593
David Blaikiebbe0e1a2013-02-25 01:07:18 +0000594DICompositeType DIBuilder::createStructType(DIDescriptor Context,
595 StringRef Name, DIFile File,
596 unsigned LineNumber,
597 uint64_t SizeInBits,
598 uint64_t AlignInBits,
599 unsigned Flags, DIType DerivedFrom,
600 DIArray Elements,
601 unsigned RunTimeLang,
Manman Ren27552062013-09-06 23:54:23 +0000602 DIType VTableHolder,
Manman Ren547467b2013-08-27 23:06:40 +0000603 StringRef UniqueIdentifier) {
Devang Patel746660f2010-12-07 23:25:47 +0000604 // TAG_structure_type is encoded in DICompositeType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000605 Metadata *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000606 HeaderBuilder::get(dwarf::DW_TAG_structure_type)
607 .concat(Name)
608 .concat(LineNumber)
609 .concat(SizeInBits)
610 .concat(AlignInBits)
611 .concat(0)
612 .concat(Flags)
613 .concat(RunTimeLang)
614 .get(VMContext),
615 File.getFileNode(), DIScope(getNonCompileUnitScope(Context)).getRef(),
616 DerivedFrom.getRef(), Elements, VTableHolder.getRef(), nullptr,
617 UniqueIdentifier.empty() ? nullptr
618 : MDString::get(VMContext, UniqueIdentifier)};
David Blaikie085abe32013-03-11 23:21:19 +0000619 DICompositeType R(MDNode::get(VMContext, Elts));
Manman Ren74c188f2013-07-01 21:02:01 +0000620 assert(R.isCompositeType() &&
621 "createStructType should return a DICompositeType");
Manman Ren0b410402013-08-29 23:17:54 +0000622 if (!UniqueIdentifier.empty())
623 retainType(R);
David Blaikie085abe32013-03-11 23:21:19 +0000624 return R;
Devang Patel746660f2010-12-07 23:25:47 +0000625}
626
Eric Christopher17dd8f02013-04-02 22:55:52 +0000627DICompositeType DIBuilder::createUnionType(DIDescriptor Scope, StringRef Name,
628 DIFile File, unsigned LineNumber,
629 uint64_t SizeInBits,
630 uint64_t AlignInBits, unsigned Flags,
631 DIArray Elements,
Manman Ren547467b2013-08-27 23:06:40 +0000632 unsigned RunTimeLang,
633 StringRef UniqueIdentifier) {
Devang Patel89ea4f22010-12-08 01:50:15 +0000634 // TAG_union_type is encoded in DICompositeType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000635 Metadata *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000636 HeaderBuilder::get(dwarf::DW_TAG_union_type)
637 .concat(Name)
638 .concat(LineNumber)
639 .concat(SizeInBits)
640 .concat(AlignInBits)
641 .concat(0) // Offset
642 .concat(Flags)
643 .concat(RunTimeLang)
644 .get(VMContext),
645 File.getFileNode(), DIScope(getNonCompileUnitScope(Scope)).getRef(),
646 nullptr, Elements, nullptr, nullptr,
647 UniqueIdentifier.empty() ? nullptr
648 : MDString::get(VMContext, UniqueIdentifier)};
Manman Ren0b410402013-08-29 23:17:54 +0000649 DICompositeType R(MDNode::get(VMContext, Elts));
650 if (!UniqueIdentifier.empty())
651 retainType(R);
652 return R;
Devang Patel89ea4f22010-12-08 01:50:15 +0000653}
654
Manman Renf8a19672014-07-28 22:24:06 +0000655DISubroutineType DIBuilder::createSubroutineType(DIFile File,
656 DITypeArray ParameterTypes,
657 unsigned Flags) {
Devang Patel89ea4f22010-12-08 01:50:15 +0000658 // TAG_subroutine_type is encoded in DICompositeType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000659 Metadata *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000660 HeaderBuilder::get(dwarf::DW_TAG_subroutine_type)
661 .concat(StringRef())
662 .concat(0) // Line
663 .concat(0) // Size
664 .concat(0) // Align
665 .concat(0) // Offset
666 .concat(Flags) // Flags
667 .concat(0)
668 .get(VMContext),
669 nullptr, nullptr, nullptr, ParameterTypes, nullptr, nullptr,
670 nullptr // Type Identifer
Devang Patel89ea4f22010-12-08 01:50:15 +0000671 };
Manman Renf8a19672014-07-28 22:24:06 +0000672 return DISubroutineType(MDNode::get(VMContext, Elts));
Devang Patel89ea4f22010-12-08 01:50:15 +0000673}
674
David Blaikief11de2f2013-02-18 06:41:57 +0000675DICompositeType DIBuilder::createEnumerationType(
676 DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
677 uint64_t SizeInBits, uint64_t AlignInBits, DIArray Elements,
Manman Ren547467b2013-08-27 23:06:40 +0000678 DIType UnderlyingType, StringRef UniqueIdentifier) {
Devang Patel89ea4f22010-12-08 01:50:15 +0000679 // TAG_enumeration_type is encoded in DICompositeType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000680 Metadata *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000681 HeaderBuilder::get(dwarf::DW_TAG_enumeration_type)
682 .concat(Name)
683 .concat(LineNumber)
684 .concat(SizeInBits)
685 .concat(AlignInBits)
686 .concat(0) // Offset
687 .concat(0) // Flags
688 .concat(0)
689 .get(VMContext),
690 File.getFileNode(), DIScope(getNonCompileUnitScope(Scope)).getRef(),
691 UnderlyingType.getRef(), Elements, nullptr, nullptr,
692 UniqueIdentifier.empty() ? nullptr
693 : MDString::get(VMContext, UniqueIdentifier)};
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000694 DICompositeType CTy(MDNode::get(VMContext, Elts));
695 AllEnumTypes.push_back(CTy);
Manman Ren0b410402013-08-29 23:17:54 +0000696 if (!UniqueIdentifier.empty())
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000697 retainType(CTy);
698 return CTy;
Devang Patel89ea4f22010-12-08 01:50:15 +0000699}
700
David Blaikief11de2f2013-02-18 06:41:57 +0000701DICompositeType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
702 DIType Ty, DIArray Subscripts) {
Devang Patel89ea4f22010-12-08 01:50:15 +0000703 // TAG_array_type is encoded in DICompositeType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000704 Metadata *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000705 HeaderBuilder::get(dwarf::DW_TAG_array_type)
706 .concat(StringRef())
707 .concat(0) // Line
708 .concat(Size)
709 .concat(AlignInBits)
710 .concat(0) // Offset
711 .concat(0) // Flags
712 .concat(0)
713 .get(VMContext),
714 nullptr, // Filename/Directory,
715 nullptr, // Unused
716 Ty.getRef(), Subscripts, nullptr, nullptr,
717 nullptr // Type Identifer
Devang Patel89ea4f22010-12-08 01:50:15 +0000718 };
David Blaikie5692e722013-03-28 02:44:59 +0000719 return DICompositeType(MDNode::get(VMContext, Elts));
Devang Patel89ea4f22010-12-08 01:50:15 +0000720}
721
Manman Ren60711602013-06-07 03:13:46 +0000722DICompositeType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits,
723 DIType Ty, DIArray Subscripts) {
Eric Christopher72a52952013-01-08 01:53:52 +0000724 // A vector is an array type with the FlagVector flag applied.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000725 Metadata *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000726 HeaderBuilder::get(dwarf::DW_TAG_array_type)
727 .concat("")
728 .concat(0) // Line
729 .concat(Size)
730 .concat(AlignInBits)
731 .concat(0) // Offset
732 .concat(DIType::FlagVector)
733 .concat(0)
734 .get(VMContext),
735 nullptr, // Filename/Directory,
736 nullptr, // Unused
737 Ty.getRef(), Subscripts, nullptr, nullptr,
738 nullptr // Type Identifer
Devang Patel89ea4f22010-12-08 01:50:15 +0000739 };
Manman Ren60711602013-06-07 03:13:46 +0000740 return DICompositeType(MDNode::get(VMContext, Elts));
Devang Patel89ea4f22010-12-08 01:50:15 +0000741}
Devang Patel746660f2010-12-07 23:25:47 +0000742
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000743static HeaderBuilder setTypeFlagsInHeader(StringRef Header,
744 unsigned FlagsToSet) {
745 DIHeaderFieldIterator I(Header);
746 std::advance(I, 6);
747
748 unsigned Flags;
749 if (I->getAsInteger(0, Flags))
750 Flags = 0;
751 Flags |= FlagsToSet;
752
Duncan P. N. Exon Smithaa687a32015-01-20 05:02:42 +0000753 return HeaderBuilder()
754 .concat(I.getPrefix())
755 .concat(Flags)
756 .concat(I.getSuffix());
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000757}
758
759static DIType createTypeWithFlags(LLVMContext &Context, DIType Ty,
760 unsigned FlagsToSet) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000761 SmallVector<Metadata *, 9> Elts;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000762 MDNode *N = Ty;
763 assert(N && "Unexpected input DIType!");
764 // Update header field.
765 Elts.push_back(setTypeFlagsInHeader(Ty.getHeader(), FlagsToSet).get(Context));
766 for (unsigned I = 1, E = N->getNumOperands(); I != E; ++I)
767 Elts.push_back(N->getOperand(I));
768
769 return DIType(MDNode::get(Context, Elts));
770}
771
Devang Patel9b412732011-02-22 18:56:12 +0000772DIType DIBuilder::createArtificialType(DIType Ty) {
Devang Patel57c5a202010-11-04 15:01:38 +0000773 if (Ty.isArtificial())
774 return Ty;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000775 return createTypeWithFlags(VMContext, Ty, DIType::FlagArtificial);
Devang Patel57c5a202010-11-04 15:01:38 +0000776}
Devang Patel746660f2010-12-07 23:25:47 +0000777
Eric Christophere3417762012-09-12 23:36:19 +0000778DIType DIBuilder::createObjectPointerType(DIType Ty) {
779 if (Ty.isObjectPointer())
780 return Ty;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000781 unsigned Flags = DIType::FlagObjectPointer | DIType::FlagArtificial;
782 return createTypeWithFlags(VMContext, Ty, Flags);
Eric Christophere3417762012-09-12 23:36:19 +0000783}
784
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000785void DIBuilder::retainType(DIType T) { AllRetainTypes.emplace_back(T); }
Devang Patel89ea4f22010-12-08 01:50:15 +0000786
Manman Renf93ac4b2014-07-29 18:20:39 +0000787DIBasicType DIBuilder::createUnspecifiedParameter() {
Manman Ren72b07e82014-07-29 22:58:13 +0000788 return DIBasicType();
Devang Patel89ea4f22010-12-08 01:50:15 +0000789}
790
Eric Christopher98f9c232013-10-15 23:31:31 +0000791DICompositeType
792DIBuilder::createForwardDecl(unsigned Tag, StringRef Name, DIDescriptor Scope,
793 DIFile F, unsigned Line, unsigned RuntimeLang,
794 uint64_t SizeInBits, uint64_t AlignInBits,
795 StringRef UniqueIdentifier) {
Eric Christopherae56eec2012-02-08 00:22:26 +0000796 // Create a temporary MDNode.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000797 Metadata *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000798 HeaderBuilder::get(Tag)
799 .concat(Name)
800 .concat(Line)
801 .concat(SizeInBits)
802 .concat(AlignInBits)
803 .concat(0) // Offset
804 .concat(DIDescriptor::FlagFwdDecl)
805 .concat(RuntimeLang)
806 .get(VMContext),
807 F.getFileNode(), DIScope(getNonCompileUnitScope(Scope)).getRef(), nullptr,
808 DIArray(), nullptr,
809 nullptr, // TemplateParams
810 UniqueIdentifier.empty() ? nullptr
811 : MDString::get(VMContext, UniqueIdentifier)};
David Blaikied3f094a2014-05-06 03:41:57 +0000812 MDNode *Node = MDNode::get(VMContext, Elts);
813 DICompositeType RetTy(Node);
814 assert(RetTy.isCompositeType() &&
815 "createForwardDecl result should be a DIType");
816 if (!UniqueIdentifier.empty())
817 retainType(RetTy);
818 return RetTy;
819}
820
Adrian Prantl534a81a2015-02-11 17:45:05 +0000821DICompositeType DIBuilder::createReplaceableCompositeType(
David Blaikied3f094a2014-05-06 03:41:57 +0000822 unsigned Tag, StringRef Name, DIDescriptor Scope, DIFile F, unsigned Line,
823 unsigned RuntimeLang, uint64_t SizeInBits, uint64_t AlignInBits,
Adrian Prantl534a81a2015-02-11 17:45:05 +0000824 unsigned Flags, StringRef UniqueIdentifier) {
David Blaikied3f094a2014-05-06 03:41:57 +0000825 // Create a temporary MDNode.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000826 Metadata *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000827 HeaderBuilder::get(Tag)
828 .concat(Name)
829 .concat(Line)
830 .concat(SizeInBits)
831 .concat(AlignInBits)
832 .concat(0) // Offset
Adrian Prantl534a81a2015-02-11 17:45:05 +0000833 .concat(Flags)
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000834 .concat(RuntimeLang)
835 .get(VMContext),
836 F.getFileNode(), DIScope(getNonCompileUnitScope(Scope)).getRef(), nullptr,
837 DIArray(), nullptr,
838 nullptr, // TemplateParams
839 UniqueIdentifier.empty() ? nullptr
840 : MDString::get(VMContext, UniqueIdentifier)};
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000841 DICompositeType RetTy(MDNode::getTemporary(VMContext, Elts).release());
David Blaikied4e106e2013-08-16 20:42:14 +0000842 assert(RetTy.isCompositeType() &&
Frederic Rissa8734142014-09-10 16:03:14 +0000843 "createReplaceableForwardDecl result should be a DIType");
Manman Ren0b410402013-08-29 23:17:54 +0000844 if (!UniqueIdentifier.empty())
845 retainType(RetTy);
Manman Rend0e67aa2013-07-02 18:37:35 +0000846 return RetTy;
Eric Christopherae56eec2012-02-08 00:22:26 +0000847}
848
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000849DIArray DIBuilder::getOrCreateArray(ArrayRef<Metadata *> Elements) {
Jay Foaddbf81d82011-04-24 10:11:03 +0000850 return DIArray(MDNode::get(VMContext, Elements));
Devang Patel746660f2010-12-07 23:25:47 +0000851}
852
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000853DITypeArray DIBuilder::getOrCreateTypeArray(ArrayRef<Metadata *> Elements) {
854 SmallVector<llvm::Metadata *, 16> Elts;
Manman Ren1a125c92014-07-28 19:33:20 +0000855 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
856 if (Elements[i] && isa<MDNode>(Elements[i]))
857 Elts.push_back(DIType(cast<MDNode>(Elements[i])).getRef());
858 else
859 Elts.push_back(Elements[i]);
860 }
861 return DITypeArray(MDNode::get(VMContext, Elts));
862}
863
Bill Wendlingd7767122012-12-04 21:34:03 +0000864DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000865 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_subrange_type)
866 .concat(Lo)
867 .concat(Count)
868 .get(VMContext)};
Devang Patel89ea4f22010-12-08 01:50:15 +0000869
Devang Patel0c773242011-04-18 23:51:03 +0000870 return DISubrange(MDNode::get(VMContext, Elts));
Devang Patel89ea4f22010-12-08 01:50:15 +0000871}
872
Duncan P. N. Exon Smithdbf64acd2014-11-15 00:23:49 +0000873static DIGlobalVariable createGlobalVariableHelper(
874 LLVMContext &VMContext, DIDescriptor Context, StringRef Name,
875 StringRef LinkageName, DIFile F, unsigned LineNumber, DITypeRef Ty,
876 bool isLocalToUnit, Constant *Val, MDNode *Decl, bool isDefinition,
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000877 std::function<MDNode *(ArrayRef<Metadata *>)> CreateFunc) {
Manman Renbfd2b8292014-11-21 19:47:48 +0000878
879 MDNode *TheCtx = getNonCompileUnitScope(Context);
880 if (DIScope(TheCtx).isCompositeType()) {
881 assert(!DICompositeType(TheCtx).getIdentifier() &&
882 "Context of a global variable should not be a type with identifier");
883 }
884
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000885 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_variable)
886 .concat(Name)
887 .concat(Name)
888 .concat(LinkageName)
889 .concat(LineNumber)
890 .concat(isLocalToUnit)
891 .concat(isDefinition)
892 .get(VMContext),
893 TheCtx, F, Ty, getConstantOrNull(Val),
894 DIDescriptor(Decl)};
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000895
896 return DIGlobalVariable(CreateFunc(Elts));
897}
898
Duncan P. N. Exon Smithdbf64acd2014-11-15 00:23:49 +0000899DIGlobalVariable DIBuilder::createGlobalVariable(
900 DIDescriptor Context, StringRef Name, StringRef LinkageName, DIFile F,
901 unsigned LineNumber, DITypeRef Ty, bool isLocalToUnit, Constant *Val,
902 MDNode *Decl) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000903 return createGlobalVariableHelper(
904 VMContext, Context, Name, LinkageName, F, LineNumber, Ty, isLocalToUnit,
905 Val, Decl, true, [&](ArrayRef<Metadata *> Elts) -> MDNode *{
906 MDNode *Node = MDNode::get(VMContext, Elts);
907 AllGVs.push_back(Node);
908 return Node;
909 });
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000910}
911
Duncan P. N. Exon Smithdbf64acd2014-11-15 00:23:49 +0000912DIGlobalVariable DIBuilder::createTempGlobalVariableFwdDecl(
913 DIDescriptor Context, StringRef Name, StringRef LinkageName, DIFile F,
914 unsigned LineNumber, DITypeRef Ty, bool isLocalToUnit, Constant *Val,
915 MDNode *Decl) {
Jyoti Allurb76b57f2014-09-29 06:32:54 +0000916 return createGlobalVariableHelper(VMContext, Context, Name, LinkageName, F,
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000917 LineNumber, Ty, isLocalToUnit, Val, Decl,
918 false, [&](ArrayRef<Metadata *> Elts) {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000919 return MDNode::getTemporary(VMContext, Elts).release();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000920 });
Devang Patel746660f2010-12-07 23:25:47 +0000921}
922
Devang Patel9b412732011-02-22 18:56:12 +0000923DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
Devang Patel63f83cd2010-12-07 23:58:00 +0000924 StringRef Name, DIFile File,
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000925 unsigned LineNo, DITypeRef Ty,
Devang Patel40eee1e2011-03-01 22:58:13 +0000926 bool AlwaysPreserve, unsigned Flags,
927 unsigned ArgNo) {
David Blaikie085abe32013-03-11 23:21:19 +0000928 DIDescriptor Context(getNonCompileUnitScope(Scope));
Manman Ren74c188f2013-07-01 21:02:01 +0000929 assert((!Context || Context.isScope()) &&
David Blaikie085abe32013-03-11 23:21:19 +0000930 "createLocalVariable should be called with a valid Context");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000931 Metadata *Elts[] = {HeaderBuilder::get(Tag)
932 .concat(Name)
933 .concat(LineNo | (ArgNo << 24))
934 .concat(Flags)
935 .get(VMContext),
936 getNonCompileUnitScope(Scope), File, Ty};
Devang Patel0c773242011-04-18 23:51:03 +0000937 MDNode *Node = MDNode::get(VMContext, Elts);
Devang Patel63f83cd2010-12-07 23:58:00 +0000938 if (AlwaysPreserve) {
939 // The optimizer may remove local variable. If there is an interest
940 // to preserve variable info in such situation then stash it in a
941 // named mdnode.
942 DISubprogram Fn(getDISubprogram(Scope));
Duncan P. N. Exon Smith3bfffde2014-10-15 16:11:41 +0000943 assert(Fn && "Missing subprogram for local variable");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000944 PreservedVariables[Fn].emplace_back(Node);
Devang Patel63f83cd2010-12-07 23:58:00 +0000945 }
Manman Rend0e67aa2013-07-02 18:37:35 +0000946 DIVariable RetVar(Node);
947 assert(RetVar.isVariable() &&
Manman Ren74c188f2013-07-01 21:02:01 +0000948 "createLocalVariable should return a valid DIVariable");
Manman Rend0e67aa2013-07-02 18:37:35 +0000949 return RetVar;
Devang Patel63f83cd2010-12-07 23:58:00 +0000950}
951
Duncan P. N. Exon Smithbd75ad42015-02-09 22:13:27 +0000952DIExpression DIBuilder::createExpression(ArrayRef<uint64_t> Addr) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000953 auto Header = HeaderBuilder::get(DW_TAG_expression);
Duncan P. N. Exon Smithbd75ad42015-02-09 22:13:27 +0000954 for (uint64_t I : Addr)
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000955 Header.concat(I);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000956 Metadata *Elts[] = {Header.get(VMContext)};
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000957 return DIExpression(MDNode::get(VMContext, Elts));
Devang Patel746660f2010-12-07 23:25:47 +0000958}
959
Duncan P. N. Exon Smithbd75ad42015-02-09 22:13:27 +0000960DIExpression DIBuilder::createExpression(ArrayRef<int64_t> Signed) {
961 // TODO: Remove the callers of this signed version and delete.
962 SmallVector<uint64_t, 8> Addr(Signed.begin(), Signed.end());
963 return createExpression(Addr);
964}
965
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000966DIExpression DIBuilder::createBitPieceExpression(unsigned OffsetInBits,
967 unsigned SizeInBits) {
968 int64_t Addr[] = {dwarf::DW_OP_bit_piece, OffsetInBits, SizeInBits};
Duncan P. N. Exon Smith9affbba2014-10-01 21:32:12 +0000969 return createExpression(Addr);
970}
971
Eric Christopher98f9c232013-10-15 23:31:31 +0000972DISubprogram DIBuilder::createFunction(DIScopeRef Context, StringRef Name,
973 StringRef LinkageName, DIFile File,
974 unsigned LineNo, DICompositeType Ty,
Manman Renc50fa112013-10-10 18:40:01 +0000975 bool isLocalToUnit, bool isDefinition,
Eric Christopher98f9c232013-10-15 23:31:31 +0000976 unsigned ScopeLine, unsigned Flags,
977 bool isOptimized, Function *Fn,
978 MDNode *TParams, MDNode *Decl) {
Manman Renc50fa112013-10-10 18:40:01 +0000979 // dragonegg does not generate identifier for types, so using an empty map
980 // to resolve the context should be fine.
981 DITypeIdentifierMap EmptyMap;
982 return createFunction(Context.resolve(EmptyMap), Name, LinkageName, File,
983 LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
984 Flags, isOptimized, Fn, TParams, Decl);
985}
986
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000987static DISubprogram createFunctionHelper(
988 LLVMContext &VMContext, DIDescriptor Context, StringRef Name,
989 StringRef LinkageName, DIFile File, unsigned LineNo, DICompositeType Ty,
990 bool isLocalToUnit, bool isDefinition, unsigned ScopeLine, unsigned Flags,
991 bool isOptimized, Function *Fn, MDNode *TParams, MDNode *Decl, MDNode *Vars,
992 std::function<MDNode *(ArrayRef<Metadata *>)> CreateFunc) {
David Blaikie5174c842013-05-22 23:22:18 +0000993 assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
994 "function types should be subroutines");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000995 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_subprogram)
996 .concat(Name)
997 .concat(Name)
998 .concat(LinkageName)
999 .concat(LineNo)
1000 .concat(isLocalToUnit)
1001 .concat(isDefinition)
1002 .concat(0)
1003 .concat(0)
1004 .concat(Flags)
1005 .concat(isOptimized)
1006 .concat(ScopeLine)
1007 .get(VMContext),
1008 File.getFileNode(),
1009 DIScope(getNonCompileUnitScope(Context)).getRef(), Ty,
1010 nullptr, getConstantOrNull(Fn), TParams, Decl, Vars};
Devang Patelb68c6232010-12-08 20:42:44 +00001011
Frederic Riss5e6bc9e2014-09-17 09:28:34 +00001012 DISubprogram S(CreateFunc(Elts));
Eric Christopher961959f2014-02-28 21:27:59 +00001013 assert(S.isSubprogram() &&
1014 "createFunction should return a valid DISubprogram");
David Blaikiecc8d0902013-03-21 20:28:52 +00001015 return S;
Devang Patelb68c6232010-12-08 20:42:44 +00001016}
1017
Frederic Riss5e6bc9e2014-09-17 09:28:34 +00001018
Frederic Riss5e6bc9e2014-09-17 09:28:34 +00001019DISubprogram DIBuilder::createFunction(DIDescriptor Context, StringRef Name,
1020 StringRef LinkageName, DIFile File,
1021 unsigned LineNo, DICompositeType Ty,
1022 bool isLocalToUnit, bool isDefinition,
1023 unsigned ScopeLine, unsigned Flags,
1024 bool isOptimized, Function *Fn,
1025 MDNode *TParams, MDNode *Decl) {
1026 return createFunctionHelper(VMContext, Context, Name, LinkageName, File,
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001027 LineNo, Ty, isLocalToUnit, isDefinition,
1028 ScopeLine, Flags, isOptimized, Fn, TParams, Decl,
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00001029 MDNode::getTemporary(VMContext, None).release(),
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001030 [&](ArrayRef<Metadata *> Elts) -> MDNode *{
1031 MDNode *Node = MDNode::get(VMContext, Elts);
1032 // Create a named metadata so that we
1033 // do not lose this mdnode.
1034 if (isDefinition)
1035 AllSubprograms.push_back(Node);
1036 return Node;
1037 });
Frederic Riss5e6bc9e2014-09-17 09:28:34 +00001038}
1039
Frederic Riss5e6bc9e2014-09-17 09:28:34 +00001040DISubprogram
1041DIBuilder::createTempFunctionFwdDecl(DIDescriptor Context, StringRef Name,
1042 StringRef LinkageName, DIFile File,
1043 unsigned LineNo, DICompositeType Ty,
1044 bool isLocalToUnit, bool isDefinition,
1045 unsigned ScopeLine, unsigned Flags,
1046 bool isOptimized, Function *Fn,
1047 MDNode *TParams, MDNode *Decl) {
1048 return createFunctionHelper(VMContext, Context, Name, LinkageName, File,
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001049 LineNo, Ty, isLocalToUnit, isDefinition,
1050 ScopeLine, Flags, isOptimized, Fn, TParams, Decl,
1051 nullptr, [&](ArrayRef<Metadata *> Elts) {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00001052 return MDNode::getTemporary(VMContext, Elts).release();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001053 });
Frederic Riss5e6bc9e2014-09-17 09:28:34 +00001054}
1055
Eric Christopher98f9c232013-10-15 23:31:31 +00001056DISubprogram DIBuilder::createMethod(DIDescriptor Context, StringRef Name,
1057 StringRef LinkageName, DIFile F,
David Blaikie5174c842013-05-22 23:22:18 +00001058 unsigned LineNo, DICompositeType Ty,
Eric Christopher98f9c232013-10-15 23:31:31 +00001059 bool isLocalToUnit, bool isDefinition,
Devang Patelb68c6232010-12-08 20:42:44 +00001060 unsigned VK, unsigned VIndex,
Eric Christopher98f9c232013-10-15 23:31:31 +00001061 DIType VTableHolder, unsigned Flags,
1062 bool isOptimized, Function *Fn,
Devang Patel9f738842011-04-05 22:52:06 +00001063 MDNode *TParam) {
David Blaikie5174c842013-05-22 23:22:18 +00001064 assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
1065 "function types should be subroutines");
Eric Christopher5cb56322013-10-15 23:31:36 +00001066 assert(getNonCompileUnitScope(Context) &&
1067 "Methods should have both a Context and a context that isn't "
1068 "the compile unit.");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001069 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_subprogram)
1070 .concat(Name)
1071 .concat(Name)
1072 .concat(LinkageName)
1073 .concat(LineNo)
1074 .concat(isLocalToUnit)
1075 .concat(isDefinition)
1076 .concat(VK)
1077 .concat(VIndex)
1078 .concat(Flags)
1079 .concat(isOptimized)
1080 .concat(LineNo)
1081 // FIXME: Do we want to use different scope/lines?
1082 .get(VMContext),
1083 F.getFileNode(), DIScope(Context).getRef(), Ty,
1084 VTableHolder.getRef(), getConstantOrNull(Fn), TParam,
1085 nullptr, nullptr};
Devang Patel0c773242011-04-18 23:51:03 +00001086 MDNode *Node = MDNode::get(VMContext, Elts);
David Blaikie595eb442013-02-18 07:10:22 +00001087 if (isDefinition)
1088 AllSubprograms.push_back(Node);
David Blaikiecc8d0902013-03-21 20:28:52 +00001089 DISubprogram S(Node);
Manman Ren74c188f2013-07-01 21:02:01 +00001090 assert(S.isSubprogram() && "createMethod should return a valid DISubprogram");
David Blaikiecc8d0902013-03-21 20:28:52 +00001091 return S;
Devang Patelb68c6232010-12-08 20:42:44 +00001092}
1093
Devang Patel9b412732011-02-22 18:56:12 +00001094DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
Devang Patel746660f2010-12-07 23:25:47 +00001095 DIFile File, unsigned LineNo) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001096 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_namespace)
1097 .concat(Name)
1098 .concat(LineNo)
1099 .get(VMContext),
1100 File.getFileNode(), getNonCompileUnitScope(Scope)};
David Blaikie085abe32013-03-11 23:21:19 +00001101 DINameSpace R(MDNode::get(VMContext, Elts));
1102 assert(R.Verify() &&
1103 "createNameSpace should return a verifiable DINameSpace");
1104 return R;
Devang Patel746660f2010-12-07 23:25:47 +00001105}
1106
Eric Christopher6647b832011-10-11 22:59:11 +00001107DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
David Blaikie2f3f76f2014-08-21 22:45:21 +00001108 DIFile File,
1109 unsigned Discriminator) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001110 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_lexical_block)
1111 .concat(Discriminator)
1112 .get(VMContext),
1113 File.getFileNode(), Scope};
David Blaikie085abe32013-03-11 23:21:19 +00001114 DILexicalBlockFile R(MDNode::get(VMContext, Elts));
1115 assert(
1116 R.Verify() &&
1117 "createLexicalBlockFile should return a verifiable DILexicalBlockFile");
1118 return R;
Eric Christopher6647b832011-10-11 22:59:11 +00001119}
1120
Devang Patel9b412732011-02-22 18:56:12 +00001121DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
David Blaikie2f3f76f2014-08-21 22:45:21 +00001122 unsigned Line, unsigned Col) {
David Blaikie6c217162014-05-15 20:09:55 +00001123 // FIXME: This isn't thread safe nor the right way to defeat MDNode uniquing.
1124 // I believe the right way is to have a self-referential element in the node.
1125 // Also: why do we bother with line/column - they're not used and the
1126 // documentation (SourceLevelDebugging.rst) claims the line/col are necessary
1127 // for uniquing, yet then we have this other solution (because line/col were
1128 // inadequate) anyway. Remove all 3 and replace them with a self-reference.
1129
Adrian Prantl21e8d4a2013-06-24 21:19:43 +00001130 // Defeat MDNode uniquing for lexical blocks by using unique id.
Devang Patel89ea4f22010-12-08 01:50:15 +00001131 static unsigned int unique_id = 0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001132 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_lexical_block)
1133 .concat(Line)
1134 .concat(Col)
1135 .concat(unique_id++)
1136 .get(VMContext),
1137 File.getFileNode(), getNonCompileUnitScope(Scope)};
David Blaikie085abe32013-03-11 23:21:19 +00001138 DILexicalBlock R(MDNode::get(VMContext, Elts));
1139 assert(R.Verify() &&
1140 "createLexicalBlock should return a verifiable DILexicalBlock");
1141 return R;
Devang Patel89ea4f22010-12-08 01:50:15 +00001142}
1143
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001144static Value *getDbgIntrinsicValueImpl(LLVMContext &VMContext, Value *V) {
1145 assert(V && "no value passed to dbg intrinsic");
1146 return MetadataAsValue::get(VMContext, ValueAsMetadata::get(V));
1147}
1148
Devang Patel9b412732011-02-22 18:56:12 +00001149Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001150 DIExpression Expr,
Devang Patel746660f2010-12-07 23:25:47 +00001151 Instruction *InsertBefore) {
Manman Ren9822a112013-06-29 05:01:19 +00001152 assert(VarInfo.isVariable() &&
1153 "empty or invalid DIVariable passed to dbg.declare");
Devang Patel746660f2010-12-07 23:25:47 +00001154 if (!DeclareFn)
1155 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1156
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001157 trackIfUnresolved(VarInfo);
1158 trackIfUnresolved(Expr);
1159 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, Storage),
1160 MetadataAsValue::get(VMContext, VarInfo),
1161 MetadataAsValue::get(VMContext, Expr)};
Jay Foad5bd375a2011-07-15 08:37:34 +00001162 return CallInst::Create(DeclareFn, Args, "", InsertBefore);
Devang Patel746660f2010-12-07 23:25:47 +00001163}
1164
Devang Patel9b412732011-02-22 18:56:12 +00001165Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001166 DIExpression Expr,
Devang Patel746660f2010-12-07 23:25:47 +00001167 BasicBlock *InsertAtEnd) {
Manman Ren9822a112013-06-29 05:01:19 +00001168 assert(VarInfo.isVariable() &&
1169 "empty or invalid DIVariable passed to dbg.declare");
Devang Patel746660f2010-12-07 23:25:47 +00001170 if (!DeclareFn)
1171 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1172
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001173 trackIfUnresolved(VarInfo);
1174 trackIfUnresolved(Expr);
1175 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, Storage),
1176 MetadataAsValue::get(VMContext, VarInfo),
1177 MetadataAsValue::get(VMContext, Expr)};
Devang Patel746660f2010-12-07 23:25:47 +00001178
1179 // If this block already has a terminator then insert this intrinsic
1180 // before the terminator.
1181 if (TerminatorInst *T = InsertAtEnd->getTerminator())
Jay Foad5bd375a2011-07-15 08:37:34 +00001182 return CallInst::Create(DeclareFn, Args, "", T);
Devang Patel746660f2010-12-07 23:25:47 +00001183 else
Jay Foad5bd375a2011-07-15 08:37:34 +00001184 return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
Devang Patel746660f2010-12-07 23:25:47 +00001185}
1186
Devang Patel9b412732011-02-22 18:56:12 +00001187Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
Devang Patel746660f2010-12-07 23:25:47 +00001188 DIVariable VarInfo,
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001189 DIExpression Expr,
Devang Patel746660f2010-12-07 23:25:47 +00001190 Instruction *InsertBefore) {
1191 assert(V && "no value passed to dbg.value");
Manman Ren9822a112013-06-29 05:01:19 +00001192 assert(VarInfo.isVariable() &&
1193 "empty or invalid DIVariable passed to dbg.value");
Devang Patel746660f2010-12-07 23:25:47 +00001194 if (!ValueFn)
1195 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1196
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001197 trackIfUnresolved(VarInfo);
1198 trackIfUnresolved(Expr);
1199 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, V),
1200 ConstantInt::get(Type::getInt64Ty(VMContext), Offset),
1201 MetadataAsValue::get(VMContext, VarInfo),
1202 MetadataAsValue::get(VMContext, Expr)};
Jay Foad5bd375a2011-07-15 08:37:34 +00001203 return CallInst::Create(ValueFn, Args, "", InsertBefore);
Devang Patel746660f2010-12-07 23:25:47 +00001204}
1205
Devang Patel9b412732011-02-22 18:56:12 +00001206Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
Devang Patel746660f2010-12-07 23:25:47 +00001207 DIVariable VarInfo,
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001208 DIExpression Expr,
Devang Patel746660f2010-12-07 23:25:47 +00001209 BasicBlock *InsertAtEnd) {
1210 assert(V && "no value passed to dbg.value");
Manman Ren9822a112013-06-29 05:01:19 +00001211 assert(VarInfo.isVariable() &&
1212 "empty or invalid DIVariable passed to dbg.value");
Devang Patel746660f2010-12-07 23:25:47 +00001213 if (!ValueFn)
1214 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1215
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001216 trackIfUnresolved(VarInfo);
1217 trackIfUnresolved(Expr);
1218 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, V),
1219 ConstantInt::get(Type::getInt64Ty(VMContext), Offset),
1220 MetadataAsValue::get(VMContext, VarInfo),
1221 MetadataAsValue::get(VMContext, Expr)};
Jay Foad5bd375a2011-07-15 08:37:34 +00001222 return CallInst::Create(ValueFn, Args, "", InsertAtEnd);
Devang Patel746660f2010-12-07 23:25:47 +00001223}
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +00001224
1225void DIBuilder::replaceVTableHolder(DICompositeType &T, DICompositeType VTableHolder) {
1226 T.setContainingType(VTableHolder);
1227
1228 // If this didn't create a self-reference, just return.
1229 if (T != VTableHolder)
1230 return;
1231
Adrian Prantl18a25b02015-02-11 17:45:10 +00001232 // Look for unresolved operands. T will drop RAUW support, orphaning any
1233 // cycles underneath it.
1234 if (T->isResolved())
1235 for (const MDOperand &O : T->operands())
1236 if (auto *N = dyn_cast_or_null<MDNode>(O))
1237 trackIfUnresolved(N);
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +00001238}
1239
1240void DIBuilder::replaceArrays(DICompositeType &T, DIArray Elements,
1241 DIArray TParams) {
1242 T.setArrays(Elements, TParams);
1243
1244 // If T isn't resolved, there's no problem.
1245 if (!T->isResolved())
1246 return;
1247
1248 // If "T" is resolved, it may be due to a self-reference cycle. Track the
1249 // arrays explicitly if they're unresolved, or else the cycles will be
1250 // orphaned.
1251 if (Elements)
1252 trackIfUnresolved(Elements);
1253 if (TParams)
1254 trackIfUnresolved(TParams);
1255}