blob: 6c38197cf14e9ad393bddf3f564bae333a9ea42f [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");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000153 Metadata *TElts[] = {HeaderBuilder::get(DW_TAG_base_type).get(VMContext)};
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000154 TempEnumTypes = MDNode::getTemporary(VMContext, TElts).release();
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000155
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000156 TempRetainTypes = MDNode::getTemporary(VMContext, TElts).release();
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000157
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000158 TempSubprograms = MDNode::getTemporary(VMContext, TElts).release();
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000159
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000160 TempGVs = MDNode::getTemporary(VMContext, TElts).release();
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000161
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000162 TempImportedModules = MDNode::getTemporary(VMContext, TElts).release();
David Blaikief55abea2013-04-22 06:12:31 +0000163
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000164 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_compile_unit)
165 .concat(Lang)
166 .concat(Producer)
167 .concat(isOptimized)
168 .concat(Flags)
169 .concat(RunTimeVer)
170 .concat(SplitName)
171 .concat(Kind)
172 .get(VMContext),
173 createFilePathPair(VMContext, Filename, Directory),
174 TempEnumTypes, TempRetainTypes, TempSubprograms, TempGVs,
175 TempImportedModules};
Eric Christopher03b3e112013-07-19 00:51:47 +0000176
177 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()};
David Blaikie5692e722013-03-28 02:44:59 +0000418 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel57c5a202010-11-04 15:01:38 +0000419}
420
Eric Christopher98f9c232013-10-15 23:31:31 +0000421DIDerivedType DIBuilder::createMemberType(DIDescriptor Scope, StringRef Name,
422 DIFile File, unsigned LineNumber,
423 uint64_t SizeInBits,
424 uint64_t AlignInBits,
425 uint64_t OffsetInBits, unsigned Flags,
426 DIType Ty) {
Benjamin Kramered8b7bf2010-11-04 18:45:27 +0000427 // TAG_member is encoded in DIDerivedType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000428 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_member)
429 .concat(Name)
430 .concat(LineNumber)
431 .concat(SizeInBits)
432 .concat(AlignInBits)
433 .concat(OffsetInBits)
434 .concat(Flags)
435 .get(VMContext),
436 File.getFileNode(),
437 DIScope(getNonCompileUnitScope(Scope)).getRef(),
438 Ty.getRef()};
David Blaikie5692e722013-03-28 02:44:59 +0000439 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel57c5a202010-11-04 15:01:38 +0000440}
441
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000442static Metadata *getConstantOrNull(Constant *C) {
443 if (C)
444 return ConstantAsMetadata::get(C);
445 return nullptr;
446}
447
Duncan P. N. Exon Smithdbf64acd2014-11-15 00:23:49 +0000448DIDerivedType DIBuilder::createStaticMemberType(DIDescriptor Scope,
449 StringRef Name, DIFile File,
450 unsigned LineNumber, DIType Ty,
451 unsigned Flags,
452 llvm::Constant *Val) {
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000453 // TAG_member is encoded in DIDerivedType format.
454 Flags |= DIDescriptor::FlagStaticMember;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000455 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_member)
456 .concat(Name)
457 .concat(LineNumber)
458 .concat(0) // Size
459 .concat(0) // Align
460 .concat(0) // Offset
461 .concat(Flags)
462 .get(VMContext),
463 File.getFileNode(),
464 DIScope(getNonCompileUnitScope(Scope)).getRef(),
465 Ty.getRef(), getConstantOrNull(Val)};
Manman Ren3c6acec2013-06-07 18:35:53 +0000466 return DIDerivedType(MDNode::get(VMContext, Elts));
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000467}
468
Eric Christopher98f9c232013-10-15 23:31:31 +0000469DIDerivedType DIBuilder::createObjCIVar(StringRef Name, DIFile File,
470 unsigned LineNumber,
471 uint64_t SizeInBits,
472 uint64_t AlignInBits,
473 uint64_t OffsetInBits, unsigned Flags,
474 DIType Ty, MDNode *PropertyNode) {
Devang Patel44882172012-02-06 17:49:43 +0000475 // TAG_member is encoded in DIDerivedType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000476 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_member)
477 .concat(Name)
478 .concat(LineNumber)
479 .concat(SizeInBits)
480 .concat(AlignInBits)
481 .concat(OffsetInBits)
482 .concat(Flags)
483 .get(VMContext),
484 File.getFileNode(), getNonCompileUnitScope(File), Ty,
485 PropertyNode};
Manman Ren3c6acec2013-06-07 18:35:53 +0000486 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel44882172012-02-06 17:49:43 +0000487}
488
Eric Christopher98f9c232013-10-15 23:31:31 +0000489DIObjCProperty
490DIBuilder::createObjCProperty(StringRef Name, DIFile File, unsigned LineNumber,
491 StringRef GetterName, StringRef SetterName,
492 unsigned PropertyAttributes, DIType Ty) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000493 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_APPLE_property)
494 .concat(Name)
495 .concat(LineNumber)
496 .concat(GetterName)
497 .concat(SetterName)
498 .concat(PropertyAttributes)
499 .get(VMContext),
500 File, Ty};
David Blaikie5692e722013-03-28 02:44:59 +0000501 return DIObjCProperty(MDNode::get(VMContext, Elts));
Devang Patelcc481592012-02-04 00:59:25 +0000502}
503
Eric Christopher3cc90fe2011-08-26 21:02:40 +0000504DITemplateTypeParameter
Devang Patel9b412732011-02-22 18:56:12 +0000505DIBuilder::createTemplateTypeParameter(DIDescriptor Context, StringRef Name,
Devang Patel3a9e65e2011-02-02 21:38:25 +0000506 DIType Ty, MDNode *File, unsigned LineNo,
507 unsigned ColumnNo) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000508 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_template_type_parameter)
509 .concat(Name)
510 .concat(LineNo)
511 .concat(ColumnNo)
512 .get(VMContext),
513 DIScope(getNonCompileUnitScope(Context)).getRef(),
514 Ty.getRef(), File};
David Blaikie5692e722013-03-28 02:44:59 +0000515 return DITemplateTypeParameter(MDNode::get(VMContext, Elts));
Devang Patel3a9e65e2011-02-02 21:38:25 +0000516}
517
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000518static DITemplateValueParameter createTemplateValueParameterHelper(
519 LLVMContext &VMContext, unsigned Tag, DIDescriptor Context, StringRef Name,
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000520 DIType Ty, Metadata *MD, MDNode *File, unsigned LineNo, unsigned ColumnNo) {
521 Metadata *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000522 HeaderBuilder::get(Tag).concat(Name).concat(LineNo).concat(ColumnNo).get(
523 VMContext),
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000524 DIScope(getNonCompileUnitScope(Context)).getRef(), Ty.getRef(), MD, File};
David Blaikie5692e722013-03-28 02:44:59 +0000525 return DITemplateValueParameter(MDNode::get(VMContext, Elts));
Devang Patelbe933b42011-02-02 22:35:53 +0000526}
527
David Blaikie2b380232013-06-22 18:59:11 +0000528DITemplateValueParameter
529DIBuilder::createTemplateValueParameter(DIDescriptor Context, StringRef Name,
Duncan P. N. Exon Smithdbf64acd2014-11-15 00:23:49 +0000530 DIType Ty, Constant *Val, MDNode *File,
531 unsigned LineNo, unsigned ColumnNo) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000532 return createTemplateValueParameterHelper(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000533 VMContext, dwarf::DW_TAG_template_value_parameter, Context, Name, Ty,
534 getConstantOrNull(Val), File, LineNo, ColumnNo);
David Blaikie2b380232013-06-22 18:59:11 +0000535}
536
537DITemplateValueParameter
538DIBuilder::createTemplateTemplateParameter(DIDescriptor Context, StringRef Name,
539 DIType Ty, StringRef Val,
540 MDNode *File, unsigned LineNo,
541 unsigned ColumnNo) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000542 return createTemplateValueParameterHelper(
543 VMContext, dwarf::DW_TAG_GNU_template_template_param, Context, Name, Ty,
David Blaikie2b380232013-06-22 18:59:11 +0000544 MDString::get(VMContext, Val), File, LineNo, ColumnNo);
545}
546
547DITemplateValueParameter
548DIBuilder::createTemplateParameterPack(DIDescriptor Context, StringRef Name,
549 DIType Ty, DIArray Val,
550 MDNode *File, unsigned LineNo,
551 unsigned ColumnNo) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000552 return createTemplateValueParameterHelper(
553 VMContext, dwarf::DW_TAG_GNU_template_parameter_pack, Context, Name, Ty,
554 Val, File, LineNo, ColumnNo);
David Blaikie2b380232013-06-22 18:59:11 +0000555}
556
David Blaikiea7310a32013-03-26 23:46:39 +0000557DICompositeType DIBuilder::createClassType(DIDescriptor Context, StringRef Name,
558 DIFile File, unsigned LineNumber,
559 uint64_t SizeInBits,
560 uint64_t AlignInBits,
561 uint64_t OffsetInBits,
562 unsigned Flags, DIType DerivedFrom,
563 DIArray Elements,
Manman Ren27552062013-09-06 23:54:23 +0000564 DIType VTableHolder,
Manman Ren547467b2013-08-27 23:06:40 +0000565 MDNode *TemplateParams,
566 StringRef UniqueIdentifier) {
Manman Ren74c188f2013-07-01 21:02:01 +0000567 assert((!Context || Context.isScope() || Context.isType()) &&
David Blaikie085abe32013-03-11 23:21:19 +0000568 "createClassType should be called with a valid Context");
569 // TAG_class_type is encoded in DICompositeType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000570 Metadata *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000571 HeaderBuilder::get(dwarf::DW_TAG_class_type)
572 .concat(Name)
573 .concat(LineNumber)
574 .concat(SizeInBits)
575 .concat(AlignInBits)
576 .concat(OffsetInBits)
577 .concat(Flags)
578 .concat(0)
579 .get(VMContext),
580 File.getFileNode(), DIScope(getNonCompileUnitScope(Context)).getRef(),
581 DerivedFrom.getRef(), Elements, VTableHolder.getRef(), TemplateParams,
582 UniqueIdentifier.empty() ? nullptr
583 : MDString::get(VMContext, UniqueIdentifier)};
David Blaikiea7310a32013-03-26 23:46:39 +0000584 DICompositeType R(MDNode::get(VMContext, Elts));
Manman Ren74c188f2013-07-01 21:02:01 +0000585 assert(R.isCompositeType() &&
586 "createClassType should return a DICompositeType");
Manman Ren0b410402013-08-29 23:17:54 +0000587 if (!UniqueIdentifier.empty())
588 retainType(R);
David Blaikie085abe32013-03-11 23:21:19 +0000589 return R;
Eric Christopher17426692012-07-06 02:35:57 +0000590}
591
David Blaikiebbe0e1a2013-02-25 01:07:18 +0000592DICompositeType DIBuilder::createStructType(DIDescriptor Context,
593 StringRef Name, DIFile File,
594 unsigned LineNumber,
595 uint64_t SizeInBits,
596 uint64_t AlignInBits,
597 unsigned Flags, DIType DerivedFrom,
598 DIArray Elements,
599 unsigned RunTimeLang,
Manman Ren27552062013-09-06 23:54:23 +0000600 DIType VTableHolder,
Manman Ren547467b2013-08-27 23:06:40 +0000601 StringRef UniqueIdentifier) {
Devang Patel746660f2010-12-07 23:25:47 +0000602 // TAG_structure_type is encoded in DICompositeType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000603 Metadata *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000604 HeaderBuilder::get(dwarf::DW_TAG_structure_type)
605 .concat(Name)
606 .concat(LineNumber)
607 .concat(SizeInBits)
608 .concat(AlignInBits)
609 .concat(0)
610 .concat(Flags)
611 .concat(RunTimeLang)
612 .get(VMContext),
613 File.getFileNode(), DIScope(getNonCompileUnitScope(Context)).getRef(),
614 DerivedFrom.getRef(), Elements, VTableHolder.getRef(), nullptr,
615 UniqueIdentifier.empty() ? nullptr
616 : MDString::get(VMContext, UniqueIdentifier)};
David Blaikie085abe32013-03-11 23:21:19 +0000617 DICompositeType R(MDNode::get(VMContext, Elts));
Manman Ren74c188f2013-07-01 21:02:01 +0000618 assert(R.isCompositeType() &&
619 "createStructType should return a DICompositeType");
Manman Ren0b410402013-08-29 23:17:54 +0000620 if (!UniqueIdentifier.empty())
621 retainType(R);
David Blaikie085abe32013-03-11 23:21:19 +0000622 return R;
Devang Patel746660f2010-12-07 23:25:47 +0000623}
624
Eric Christopher17dd8f02013-04-02 22:55:52 +0000625DICompositeType DIBuilder::createUnionType(DIDescriptor Scope, StringRef Name,
626 DIFile File, unsigned LineNumber,
627 uint64_t SizeInBits,
628 uint64_t AlignInBits, unsigned Flags,
629 DIArray Elements,
Manman Ren547467b2013-08-27 23:06:40 +0000630 unsigned RunTimeLang,
631 StringRef UniqueIdentifier) {
Devang Patel89ea4f22010-12-08 01:50:15 +0000632 // TAG_union_type is encoded in DICompositeType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000633 Metadata *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000634 HeaderBuilder::get(dwarf::DW_TAG_union_type)
635 .concat(Name)
636 .concat(LineNumber)
637 .concat(SizeInBits)
638 .concat(AlignInBits)
639 .concat(0) // Offset
640 .concat(Flags)
641 .concat(RunTimeLang)
642 .get(VMContext),
643 File.getFileNode(), DIScope(getNonCompileUnitScope(Scope)).getRef(),
644 nullptr, Elements, nullptr, nullptr,
645 UniqueIdentifier.empty() ? nullptr
646 : MDString::get(VMContext, UniqueIdentifier)};
Manman Ren0b410402013-08-29 23:17:54 +0000647 DICompositeType R(MDNode::get(VMContext, Elts));
648 if (!UniqueIdentifier.empty())
649 retainType(R);
650 return R;
Devang Patel89ea4f22010-12-08 01:50:15 +0000651}
652
Manman Renf8a19672014-07-28 22:24:06 +0000653DISubroutineType DIBuilder::createSubroutineType(DIFile File,
654 DITypeArray ParameterTypes,
655 unsigned Flags) {
Devang Patel89ea4f22010-12-08 01:50:15 +0000656 // TAG_subroutine_type is encoded in DICompositeType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000657 Metadata *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000658 HeaderBuilder::get(dwarf::DW_TAG_subroutine_type)
659 .concat(StringRef())
660 .concat(0) // Line
661 .concat(0) // Size
662 .concat(0) // Align
663 .concat(0) // Offset
664 .concat(Flags) // Flags
665 .concat(0)
666 .get(VMContext),
667 nullptr, nullptr, nullptr, ParameterTypes, nullptr, nullptr,
668 nullptr // Type Identifer
Devang Patel89ea4f22010-12-08 01:50:15 +0000669 };
Manman Renf8a19672014-07-28 22:24:06 +0000670 return DISubroutineType(MDNode::get(VMContext, Elts));
Devang Patel89ea4f22010-12-08 01:50:15 +0000671}
672
David Blaikief11de2f2013-02-18 06:41:57 +0000673DICompositeType DIBuilder::createEnumerationType(
674 DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
675 uint64_t SizeInBits, uint64_t AlignInBits, DIArray Elements,
Manman Ren547467b2013-08-27 23:06:40 +0000676 DIType UnderlyingType, StringRef UniqueIdentifier) {
Devang Patel89ea4f22010-12-08 01:50:15 +0000677 // TAG_enumeration_type is encoded in DICompositeType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000678 Metadata *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000679 HeaderBuilder::get(dwarf::DW_TAG_enumeration_type)
680 .concat(Name)
681 .concat(LineNumber)
682 .concat(SizeInBits)
683 .concat(AlignInBits)
684 .concat(0) // Offset
685 .concat(0) // Flags
686 .concat(0)
687 .get(VMContext),
688 File.getFileNode(), DIScope(getNonCompileUnitScope(Scope)).getRef(),
689 UnderlyingType.getRef(), Elements, nullptr, nullptr,
690 UniqueIdentifier.empty() ? nullptr
691 : MDString::get(VMContext, UniqueIdentifier)};
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000692 DICompositeType CTy(MDNode::get(VMContext, Elts));
693 AllEnumTypes.push_back(CTy);
Manman Ren0b410402013-08-29 23:17:54 +0000694 if (!UniqueIdentifier.empty())
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000695 retainType(CTy);
696 return CTy;
Devang Patel89ea4f22010-12-08 01:50:15 +0000697}
698
David Blaikief11de2f2013-02-18 06:41:57 +0000699DICompositeType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
700 DIType Ty, DIArray Subscripts) {
Devang Patel89ea4f22010-12-08 01:50:15 +0000701 // TAG_array_type is encoded in DICompositeType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000702 Metadata *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000703 HeaderBuilder::get(dwarf::DW_TAG_array_type)
704 .concat(StringRef())
705 .concat(0) // Line
706 .concat(Size)
707 .concat(AlignInBits)
708 .concat(0) // Offset
709 .concat(0) // Flags
710 .concat(0)
711 .get(VMContext),
712 nullptr, // Filename/Directory,
713 nullptr, // Unused
714 Ty.getRef(), Subscripts, nullptr, nullptr,
715 nullptr // Type Identifer
Devang Patel89ea4f22010-12-08 01:50:15 +0000716 };
David Blaikie5692e722013-03-28 02:44:59 +0000717 return DICompositeType(MDNode::get(VMContext, Elts));
Devang Patel89ea4f22010-12-08 01:50:15 +0000718}
719
Manman Ren60711602013-06-07 03:13:46 +0000720DICompositeType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits,
721 DIType Ty, DIArray Subscripts) {
Eric Christopher72a52952013-01-08 01:53:52 +0000722 // A vector is an array type with the FlagVector flag applied.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000723 Metadata *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000724 HeaderBuilder::get(dwarf::DW_TAG_array_type)
725 .concat("")
726 .concat(0) // Line
727 .concat(Size)
728 .concat(AlignInBits)
729 .concat(0) // Offset
730 .concat(DIType::FlagVector)
731 .concat(0)
732 .get(VMContext),
733 nullptr, // Filename/Directory,
734 nullptr, // Unused
735 Ty.getRef(), Subscripts, nullptr, nullptr,
736 nullptr // Type Identifer
Devang Patel89ea4f22010-12-08 01:50:15 +0000737 };
Manman Ren60711602013-06-07 03:13:46 +0000738 return DICompositeType(MDNode::get(VMContext, Elts));
Devang Patel89ea4f22010-12-08 01:50:15 +0000739}
Devang Patel746660f2010-12-07 23:25:47 +0000740
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000741static HeaderBuilder setTypeFlagsInHeader(StringRef Header,
742 unsigned FlagsToSet) {
743 DIHeaderFieldIterator I(Header);
744 std::advance(I, 6);
745
746 unsigned Flags;
747 if (I->getAsInteger(0, Flags))
748 Flags = 0;
749 Flags |= FlagsToSet;
750
Duncan P. N. Exon Smithaa687a32015-01-20 05:02:42 +0000751 return HeaderBuilder()
752 .concat(I.getPrefix())
753 .concat(Flags)
754 .concat(I.getSuffix());
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000755}
756
757static DIType createTypeWithFlags(LLVMContext &Context, DIType Ty,
758 unsigned FlagsToSet) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000759 SmallVector<Metadata *, 9> Elts;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000760 MDNode *N = Ty;
761 assert(N && "Unexpected input DIType!");
762 // Update header field.
763 Elts.push_back(setTypeFlagsInHeader(Ty.getHeader(), FlagsToSet).get(Context));
764 for (unsigned I = 1, E = N->getNumOperands(); I != E; ++I)
765 Elts.push_back(N->getOperand(I));
766
767 return DIType(MDNode::get(Context, Elts));
768}
769
Devang Patel9b412732011-02-22 18:56:12 +0000770DIType DIBuilder::createArtificialType(DIType Ty) {
Devang Patel57c5a202010-11-04 15:01:38 +0000771 if (Ty.isArtificial())
772 return Ty;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000773 return createTypeWithFlags(VMContext, Ty, DIType::FlagArtificial);
Devang Patel57c5a202010-11-04 15:01:38 +0000774}
Devang Patel746660f2010-12-07 23:25:47 +0000775
Eric Christophere3417762012-09-12 23:36:19 +0000776DIType DIBuilder::createObjectPointerType(DIType Ty) {
777 if (Ty.isObjectPointer())
778 return Ty;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000779 unsigned Flags = DIType::FlagObjectPointer | DIType::FlagArtificial;
780 return createTypeWithFlags(VMContext, Ty, Flags);
Eric Christophere3417762012-09-12 23:36:19 +0000781}
782
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000783void DIBuilder::retainType(DIType T) { AllRetainTypes.emplace_back(T); }
Devang Patel89ea4f22010-12-08 01:50:15 +0000784
Manman Renf93ac4b2014-07-29 18:20:39 +0000785DIBasicType DIBuilder::createUnspecifiedParameter() {
Manman Ren72b07e82014-07-29 22:58:13 +0000786 return DIBasicType();
Devang Patel89ea4f22010-12-08 01:50:15 +0000787}
788
Eric Christopher98f9c232013-10-15 23:31:31 +0000789DICompositeType
790DIBuilder::createForwardDecl(unsigned Tag, StringRef Name, DIDescriptor Scope,
791 DIFile F, unsigned Line, unsigned RuntimeLang,
792 uint64_t SizeInBits, uint64_t AlignInBits,
793 StringRef UniqueIdentifier) {
Eric Christopherae56eec2012-02-08 00:22:26 +0000794 // Create a temporary MDNode.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000795 Metadata *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000796 HeaderBuilder::get(Tag)
797 .concat(Name)
798 .concat(Line)
799 .concat(SizeInBits)
800 .concat(AlignInBits)
801 .concat(0) // Offset
802 .concat(DIDescriptor::FlagFwdDecl)
803 .concat(RuntimeLang)
804 .get(VMContext),
805 F.getFileNode(), DIScope(getNonCompileUnitScope(Scope)).getRef(), nullptr,
806 DIArray(), nullptr,
807 nullptr, // TemplateParams
808 UniqueIdentifier.empty() ? nullptr
809 : MDString::get(VMContext, UniqueIdentifier)};
David Blaikied3f094a2014-05-06 03:41:57 +0000810 MDNode *Node = MDNode::get(VMContext, Elts);
811 DICompositeType RetTy(Node);
812 assert(RetTy.isCompositeType() &&
813 "createForwardDecl result should be a DIType");
814 if (!UniqueIdentifier.empty())
815 retainType(RetTy);
816 return RetTy;
817}
818
Adrian Prantl534a81a2015-02-11 17:45:05 +0000819DICompositeType DIBuilder::createReplaceableCompositeType(
David Blaikied3f094a2014-05-06 03:41:57 +0000820 unsigned Tag, StringRef Name, DIDescriptor Scope, DIFile F, unsigned Line,
821 unsigned RuntimeLang, uint64_t SizeInBits, uint64_t AlignInBits,
Adrian Prantl534a81a2015-02-11 17:45:05 +0000822 unsigned Flags, StringRef UniqueIdentifier) {
David Blaikied3f094a2014-05-06 03:41:57 +0000823 // Create a temporary MDNode.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000824 Metadata *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000825 HeaderBuilder::get(Tag)
826 .concat(Name)
827 .concat(Line)
828 .concat(SizeInBits)
829 .concat(AlignInBits)
830 .concat(0) // Offset
Adrian Prantl534a81a2015-02-11 17:45:05 +0000831 .concat(Flags)
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000832 .concat(RuntimeLang)
833 .get(VMContext),
834 F.getFileNode(), DIScope(getNonCompileUnitScope(Scope)).getRef(), nullptr,
835 DIArray(), nullptr,
836 nullptr, // TemplateParams
837 UniqueIdentifier.empty() ? nullptr
838 : MDString::get(VMContext, UniqueIdentifier)};
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000839 DICompositeType RetTy(MDNode::getTemporary(VMContext, Elts).release());
David Blaikied4e106e2013-08-16 20:42:14 +0000840 assert(RetTy.isCompositeType() &&
Frederic Rissa8734142014-09-10 16:03:14 +0000841 "createReplaceableForwardDecl result should be a DIType");
Manman Ren0b410402013-08-29 23:17:54 +0000842 if (!UniqueIdentifier.empty())
843 retainType(RetTy);
Manman Rend0e67aa2013-07-02 18:37:35 +0000844 return RetTy;
Eric Christopherae56eec2012-02-08 00:22:26 +0000845}
846
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000847DIArray DIBuilder::getOrCreateArray(ArrayRef<Metadata *> Elements) {
Jay Foaddbf81d82011-04-24 10:11:03 +0000848 return DIArray(MDNode::get(VMContext, Elements));
Devang Patel746660f2010-12-07 23:25:47 +0000849}
850
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000851DITypeArray DIBuilder::getOrCreateTypeArray(ArrayRef<Metadata *> Elements) {
852 SmallVector<llvm::Metadata *, 16> Elts;
Manman Ren1a125c92014-07-28 19:33:20 +0000853 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
854 if (Elements[i] && isa<MDNode>(Elements[i]))
855 Elts.push_back(DIType(cast<MDNode>(Elements[i])).getRef());
856 else
857 Elts.push_back(Elements[i]);
858 }
859 return DITypeArray(MDNode::get(VMContext, Elts));
860}
861
Bill Wendlingd7767122012-12-04 21:34:03 +0000862DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000863 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_subrange_type)
864 .concat(Lo)
865 .concat(Count)
866 .get(VMContext)};
Devang Patel89ea4f22010-12-08 01:50:15 +0000867
Devang Patel0c773242011-04-18 23:51:03 +0000868 return DISubrange(MDNode::get(VMContext, Elts));
Devang Patel89ea4f22010-12-08 01:50:15 +0000869}
870
Duncan P. N. Exon Smithdbf64acd2014-11-15 00:23:49 +0000871static DIGlobalVariable createGlobalVariableHelper(
872 LLVMContext &VMContext, DIDescriptor Context, StringRef Name,
873 StringRef LinkageName, DIFile F, unsigned LineNumber, DITypeRef Ty,
874 bool isLocalToUnit, Constant *Val, MDNode *Decl, bool isDefinition,
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000875 std::function<MDNode *(ArrayRef<Metadata *>)> CreateFunc) {
Manman Renbfd2b8292014-11-21 19:47:48 +0000876
877 MDNode *TheCtx = getNonCompileUnitScope(Context);
878 if (DIScope(TheCtx).isCompositeType()) {
879 assert(!DICompositeType(TheCtx).getIdentifier() &&
880 "Context of a global variable should not be a type with identifier");
881 }
882
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000883 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_variable)
884 .concat(Name)
885 .concat(Name)
886 .concat(LinkageName)
887 .concat(LineNumber)
888 .concat(isLocalToUnit)
889 .concat(isDefinition)
890 .get(VMContext),
891 TheCtx, F, Ty, getConstantOrNull(Val),
892 DIDescriptor(Decl)};
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000893
894 return DIGlobalVariable(CreateFunc(Elts));
895}
896
Duncan P. N. Exon Smithdbf64acd2014-11-15 00:23:49 +0000897DIGlobalVariable DIBuilder::createGlobalVariable(
898 DIDescriptor Context, StringRef Name, StringRef LinkageName, DIFile F,
899 unsigned LineNumber, DITypeRef Ty, bool isLocalToUnit, Constant *Val,
900 MDNode *Decl) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000901 return createGlobalVariableHelper(
902 VMContext, Context, Name, LinkageName, F, LineNumber, Ty, isLocalToUnit,
903 Val, Decl, true, [&](ArrayRef<Metadata *> Elts) -> MDNode *{
904 MDNode *Node = MDNode::get(VMContext, Elts);
905 AllGVs.push_back(Node);
906 return Node;
907 });
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000908}
909
Duncan P. N. Exon Smithdbf64acd2014-11-15 00:23:49 +0000910DIGlobalVariable DIBuilder::createTempGlobalVariableFwdDecl(
911 DIDescriptor Context, StringRef Name, StringRef LinkageName, DIFile F,
912 unsigned LineNumber, DITypeRef Ty, bool isLocalToUnit, Constant *Val,
913 MDNode *Decl) {
Jyoti Allurb76b57f2014-09-29 06:32:54 +0000914 return createGlobalVariableHelper(VMContext, Context, Name, LinkageName, F,
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000915 LineNumber, Ty, isLocalToUnit, Val, Decl,
916 false, [&](ArrayRef<Metadata *> Elts) {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000917 return MDNode::getTemporary(VMContext, Elts).release();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000918 });
Devang Patel746660f2010-12-07 23:25:47 +0000919}
920
Devang Patel9b412732011-02-22 18:56:12 +0000921DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
Devang Patel63f83cd2010-12-07 23:58:00 +0000922 StringRef Name, DIFile File,
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000923 unsigned LineNo, DITypeRef Ty,
Devang Patel40eee1e2011-03-01 22:58:13 +0000924 bool AlwaysPreserve, unsigned Flags,
925 unsigned ArgNo) {
David Blaikie085abe32013-03-11 23:21:19 +0000926 DIDescriptor Context(getNonCompileUnitScope(Scope));
Manman Ren74c188f2013-07-01 21:02:01 +0000927 assert((!Context || Context.isScope()) &&
David Blaikie085abe32013-03-11 23:21:19 +0000928 "createLocalVariable should be called with a valid Context");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000929 Metadata *Elts[] = {HeaderBuilder::get(Tag)
930 .concat(Name)
931 .concat(LineNo | (ArgNo << 24))
932 .concat(Flags)
933 .get(VMContext),
934 getNonCompileUnitScope(Scope), File, Ty};
Devang Patel0c773242011-04-18 23:51:03 +0000935 MDNode *Node = MDNode::get(VMContext, Elts);
Devang Patel63f83cd2010-12-07 23:58:00 +0000936 if (AlwaysPreserve) {
937 // The optimizer may remove local variable. If there is an interest
938 // to preserve variable info in such situation then stash it in a
939 // named mdnode.
940 DISubprogram Fn(getDISubprogram(Scope));
Duncan P. N. Exon Smith3bfffde2014-10-15 16:11:41 +0000941 assert(Fn && "Missing subprogram for local variable");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000942 PreservedVariables[Fn].emplace_back(Node);
Devang Patel63f83cd2010-12-07 23:58:00 +0000943 }
Manman Rend0e67aa2013-07-02 18:37:35 +0000944 DIVariable RetVar(Node);
945 assert(RetVar.isVariable() &&
Manman Ren74c188f2013-07-01 21:02:01 +0000946 "createLocalVariable should return a valid DIVariable");
Manman Rend0e67aa2013-07-02 18:37:35 +0000947 return RetVar;
Devang Patel63f83cd2010-12-07 23:58:00 +0000948}
949
Duncan P. N. Exon Smithbd75ad42015-02-09 22:13:27 +0000950DIExpression DIBuilder::createExpression(ArrayRef<uint64_t> Addr) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000951 auto Header = HeaderBuilder::get(DW_TAG_expression);
Duncan P. N. Exon Smithbd75ad42015-02-09 22:13:27 +0000952 for (uint64_t I : Addr)
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000953 Header.concat(I);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000954 Metadata *Elts[] = {Header.get(VMContext)};
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000955 return DIExpression(MDNode::get(VMContext, Elts));
Devang Patel746660f2010-12-07 23:25:47 +0000956}
957
Duncan P. N. Exon Smithbd75ad42015-02-09 22:13:27 +0000958DIExpression DIBuilder::createExpression(ArrayRef<int64_t> Signed) {
959 // TODO: Remove the callers of this signed version and delete.
960 SmallVector<uint64_t, 8> Addr(Signed.begin(), Signed.end());
961 return createExpression(Addr);
962}
963
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000964DIExpression DIBuilder::createBitPieceExpression(unsigned OffsetInBits,
965 unsigned SizeInBits) {
966 int64_t Addr[] = {dwarf::DW_OP_bit_piece, OffsetInBits, SizeInBits};
Duncan P. N. Exon Smith9affbba2014-10-01 21:32:12 +0000967 return createExpression(Addr);
968}
969
Eric Christopher98f9c232013-10-15 23:31:31 +0000970DISubprogram DIBuilder::createFunction(DIScopeRef Context, StringRef Name,
971 StringRef LinkageName, DIFile File,
972 unsigned LineNo, DICompositeType Ty,
Manman Renc50fa112013-10-10 18:40:01 +0000973 bool isLocalToUnit, bool isDefinition,
Eric Christopher98f9c232013-10-15 23:31:31 +0000974 unsigned ScopeLine, unsigned Flags,
975 bool isOptimized, Function *Fn,
976 MDNode *TParams, MDNode *Decl) {
Manman Renc50fa112013-10-10 18:40:01 +0000977 // dragonegg does not generate identifier for types, so using an empty map
978 // to resolve the context should be fine.
979 DITypeIdentifierMap EmptyMap;
980 return createFunction(Context.resolve(EmptyMap), Name, LinkageName, File,
981 LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
982 Flags, isOptimized, Fn, TParams, Decl);
983}
984
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000985static DISubprogram createFunctionHelper(
986 LLVMContext &VMContext, DIDescriptor Context, StringRef Name,
987 StringRef LinkageName, DIFile File, unsigned LineNo, DICompositeType Ty,
988 bool isLocalToUnit, bool isDefinition, unsigned ScopeLine, unsigned Flags,
989 bool isOptimized, Function *Fn, MDNode *TParams, MDNode *Decl, MDNode *Vars,
990 std::function<MDNode *(ArrayRef<Metadata *>)> CreateFunc) {
David Blaikie5174c842013-05-22 23:22:18 +0000991 assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
992 "function types should be subroutines");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000993 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_subprogram)
994 .concat(Name)
995 .concat(Name)
996 .concat(LinkageName)
997 .concat(LineNo)
998 .concat(isLocalToUnit)
999 .concat(isDefinition)
1000 .concat(0)
1001 .concat(0)
1002 .concat(Flags)
1003 .concat(isOptimized)
1004 .concat(ScopeLine)
1005 .get(VMContext),
1006 File.getFileNode(),
1007 DIScope(getNonCompileUnitScope(Context)).getRef(), Ty,
1008 nullptr, getConstantOrNull(Fn), TParams, Decl, Vars};
Devang Patelb68c6232010-12-08 20:42:44 +00001009
Frederic Riss5e6bc9e2014-09-17 09:28:34 +00001010 DISubprogram S(CreateFunc(Elts));
Eric Christopher961959f2014-02-28 21:27:59 +00001011 assert(S.isSubprogram() &&
1012 "createFunction should return a valid DISubprogram");
David Blaikiecc8d0902013-03-21 20:28:52 +00001013 return S;
Devang Patelb68c6232010-12-08 20:42:44 +00001014}
1015
Frederic Riss5e6bc9e2014-09-17 09:28:34 +00001016
Frederic Riss5e6bc9e2014-09-17 09:28:34 +00001017DISubprogram DIBuilder::createFunction(DIDescriptor Context, StringRef Name,
1018 StringRef LinkageName, DIFile File,
1019 unsigned LineNo, DICompositeType Ty,
1020 bool isLocalToUnit, bool isDefinition,
1021 unsigned ScopeLine, unsigned Flags,
1022 bool isOptimized, Function *Fn,
1023 MDNode *TParams, MDNode *Decl) {
1024 return createFunctionHelper(VMContext, Context, Name, LinkageName, File,
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001025 LineNo, Ty, isLocalToUnit, isDefinition,
1026 ScopeLine, Flags, isOptimized, Fn, TParams, Decl,
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00001027 MDNode::getTemporary(VMContext, None).release(),
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001028 [&](ArrayRef<Metadata *> Elts) -> MDNode *{
1029 MDNode *Node = MDNode::get(VMContext, Elts);
1030 // Create a named metadata so that we
1031 // do not lose this mdnode.
1032 if (isDefinition)
1033 AllSubprograms.push_back(Node);
1034 return Node;
1035 });
Frederic Riss5e6bc9e2014-09-17 09:28:34 +00001036}
1037
Frederic Riss5e6bc9e2014-09-17 09:28:34 +00001038DISubprogram
1039DIBuilder::createTempFunctionFwdDecl(DIDescriptor Context, StringRef Name,
1040 StringRef LinkageName, DIFile File,
1041 unsigned LineNo, DICompositeType Ty,
1042 bool isLocalToUnit, bool isDefinition,
1043 unsigned ScopeLine, unsigned Flags,
1044 bool isOptimized, Function *Fn,
1045 MDNode *TParams, MDNode *Decl) {
1046 return createFunctionHelper(VMContext, Context, Name, LinkageName, File,
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001047 LineNo, Ty, isLocalToUnit, isDefinition,
1048 ScopeLine, Flags, isOptimized, Fn, TParams, Decl,
1049 nullptr, [&](ArrayRef<Metadata *> Elts) {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00001050 return MDNode::getTemporary(VMContext, Elts).release();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001051 });
Frederic Riss5e6bc9e2014-09-17 09:28:34 +00001052}
1053
Eric Christopher98f9c232013-10-15 23:31:31 +00001054DISubprogram DIBuilder::createMethod(DIDescriptor Context, StringRef Name,
1055 StringRef LinkageName, DIFile F,
David Blaikie5174c842013-05-22 23:22:18 +00001056 unsigned LineNo, DICompositeType Ty,
Eric Christopher98f9c232013-10-15 23:31:31 +00001057 bool isLocalToUnit, bool isDefinition,
Devang Patelb68c6232010-12-08 20:42:44 +00001058 unsigned VK, unsigned VIndex,
Eric Christopher98f9c232013-10-15 23:31:31 +00001059 DIType VTableHolder, unsigned Flags,
1060 bool isOptimized, Function *Fn,
Devang Patel9f738842011-04-05 22:52:06 +00001061 MDNode *TParam) {
David Blaikie5174c842013-05-22 23:22:18 +00001062 assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
1063 "function types should be subroutines");
Eric Christopher5cb56322013-10-15 23:31:36 +00001064 assert(getNonCompileUnitScope(Context) &&
1065 "Methods should have both a Context and a context that isn't "
1066 "the compile unit.");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001067 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_subprogram)
1068 .concat(Name)
1069 .concat(Name)
1070 .concat(LinkageName)
1071 .concat(LineNo)
1072 .concat(isLocalToUnit)
1073 .concat(isDefinition)
1074 .concat(VK)
1075 .concat(VIndex)
1076 .concat(Flags)
1077 .concat(isOptimized)
1078 .concat(LineNo)
1079 // FIXME: Do we want to use different scope/lines?
1080 .get(VMContext),
1081 F.getFileNode(), DIScope(Context).getRef(), Ty,
1082 VTableHolder.getRef(), getConstantOrNull(Fn), TParam,
1083 nullptr, nullptr};
Devang Patel0c773242011-04-18 23:51:03 +00001084 MDNode *Node = MDNode::get(VMContext, Elts);
David Blaikie595eb442013-02-18 07:10:22 +00001085 if (isDefinition)
1086 AllSubprograms.push_back(Node);
David Blaikiecc8d0902013-03-21 20:28:52 +00001087 DISubprogram S(Node);
Manman Ren74c188f2013-07-01 21:02:01 +00001088 assert(S.isSubprogram() && "createMethod should return a valid DISubprogram");
David Blaikiecc8d0902013-03-21 20:28:52 +00001089 return S;
Devang Patelb68c6232010-12-08 20:42:44 +00001090}
1091
Devang Patel9b412732011-02-22 18:56:12 +00001092DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
Devang Patel746660f2010-12-07 23:25:47 +00001093 DIFile File, unsigned LineNo) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001094 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_namespace)
1095 .concat(Name)
1096 .concat(LineNo)
1097 .get(VMContext),
1098 File.getFileNode(), getNonCompileUnitScope(Scope)};
David Blaikie085abe32013-03-11 23:21:19 +00001099 DINameSpace R(MDNode::get(VMContext, Elts));
1100 assert(R.Verify() &&
1101 "createNameSpace should return a verifiable DINameSpace");
1102 return R;
Devang Patel746660f2010-12-07 23:25:47 +00001103}
1104
Eric Christopher6647b832011-10-11 22:59:11 +00001105DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
David Blaikie2f3f76f2014-08-21 22:45:21 +00001106 DIFile File,
1107 unsigned Discriminator) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001108 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_lexical_block)
1109 .concat(Discriminator)
1110 .get(VMContext),
1111 File.getFileNode(), Scope};
David Blaikie085abe32013-03-11 23:21:19 +00001112 DILexicalBlockFile R(MDNode::get(VMContext, Elts));
1113 assert(
1114 R.Verify() &&
1115 "createLexicalBlockFile should return a verifiable DILexicalBlockFile");
1116 return R;
Eric Christopher6647b832011-10-11 22:59:11 +00001117}
1118
Devang Patel9b412732011-02-22 18:56:12 +00001119DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
David Blaikie2f3f76f2014-08-21 22:45:21 +00001120 unsigned Line, unsigned Col) {
David Blaikie6c217162014-05-15 20:09:55 +00001121 // FIXME: This isn't thread safe nor the right way to defeat MDNode uniquing.
1122 // I believe the right way is to have a self-referential element in the node.
1123 // Also: why do we bother with line/column - they're not used and the
1124 // documentation (SourceLevelDebugging.rst) claims the line/col are necessary
1125 // for uniquing, yet then we have this other solution (because line/col were
1126 // inadequate) anyway. Remove all 3 and replace them with a self-reference.
1127
Adrian Prantl21e8d4a2013-06-24 21:19:43 +00001128 // Defeat MDNode uniquing for lexical blocks by using unique id.
Devang Patel89ea4f22010-12-08 01:50:15 +00001129 static unsigned int unique_id = 0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001130 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_lexical_block)
1131 .concat(Line)
1132 .concat(Col)
1133 .concat(unique_id++)
1134 .get(VMContext),
1135 File.getFileNode(), getNonCompileUnitScope(Scope)};
David Blaikie085abe32013-03-11 23:21:19 +00001136 DILexicalBlock R(MDNode::get(VMContext, Elts));
1137 assert(R.Verify() &&
1138 "createLexicalBlock should return a verifiable DILexicalBlock");
1139 return R;
Devang Patel89ea4f22010-12-08 01:50:15 +00001140}
1141
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001142static Value *getDbgIntrinsicValueImpl(LLVMContext &VMContext, Value *V) {
1143 assert(V && "no value passed to dbg intrinsic");
1144 return MetadataAsValue::get(VMContext, ValueAsMetadata::get(V));
1145}
1146
Devang Patel9b412732011-02-22 18:56:12 +00001147Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001148 DIExpression Expr,
Devang Patel746660f2010-12-07 23:25:47 +00001149 Instruction *InsertBefore) {
Manman Ren9822a112013-06-29 05:01:19 +00001150 assert(VarInfo.isVariable() &&
1151 "empty or invalid DIVariable passed to dbg.declare");
Devang Patel746660f2010-12-07 23:25:47 +00001152 if (!DeclareFn)
1153 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1154
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001155 trackIfUnresolved(VarInfo);
1156 trackIfUnresolved(Expr);
1157 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, Storage),
1158 MetadataAsValue::get(VMContext, VarInfo),
1159 MetadataAsValue::get(VMContext, Expr)};
Jay Foad5bd375a2011-07-15 08:37:34 +00001160 return CallInst::Create(DeclareFn, Args, "", InsertBefore);
Devang Patel746660f2010-12-07 23:25:47 +00001161}
1162
Devang Patel9b412732011-02-22 18:56:12 +00001163Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001164 DIExpression Expr,
Devang Patel746660f2010-12-07 23:25:47 +00001165 BasicBlock *InsertAtEnd) {
Manman Ren9822a112013-06-29 05:01:19 +00001166 assert(VarInfo.isVariable() &&
1167 "empty or invalid DIVariable passed to dbg.declare");
Devang Patel746660f2010-12-07 23:25:47 +00001168 if (!DeclareFn)
1169 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1170
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001171 trackIfUnresolved(VarInfo);
1172 trackIfUnresolved(Expr);
1173 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, Storage),
1174 MetadataAsValue::get(VMContext, VarInfo),
1175 MetadataAsValue::get(VMContext, Expr)};
Devang Patel746660f2010-12-07 23:25:47 +00001176
1177 // If this block already has a terminator then insert this intrinsic
1178 // before the terminator.
1179 if (TerminatorInst *T = InsertAtEnd->getTerminator())
Jay Foad5bd375a2011-07-15 08:37:34 +00001180 return CallInst::Create(DeclareFn, Args, "", T);
Devang Patel746660f2010-12-07 23:25:47 +00001181 else
Jay Foad5bd375a2011-07-15 08:37:34 +00001182 return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
Devang Patel746660f2010-12-07 23:25:47 +00001183}
1184
Devang Patel9b412732011-02-22 18:56:12 +00001185Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
Devang Patel746660f2010-12-07 23:25:47 +00001186 DIVariable VarInfo,
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001187 DIExpression Expr,
Devang Patel746660f2010-12-07 23:25:47 +00001188 Instruction *InsertBefore) {
1189 assert(V && "no value passed to dbg.value");
Manman Ren9822a112013-06-29 05:01:19 +00001190 assert(VarInfo.isVariable() &&
1191 "empty or invalid DIVariable passed to dbg.value");
Devang Patel746660f2010-12-07 23:25:47 +00001192 if (!ValueFn)
1193 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1194
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001195 trackIfUnresolved(VarInfo);
1196 trackIfUnresolved(Expr);
1197 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, V),
1198 ConstantInt::get(Type::getInt64Ty(VMContext), Offset),
1199 MetadataAsValue::get(VMContext, VarInfo),
1200 MetadataAsValue::get(VMContext, Expr)};
Jay Foad5bd375a2011-07-15 08:37:34 +00001201 return CallInst::Create(ValueFn, Args, "", InsertBefore);
Devang Patel746660f2010-12-07 23:25:47 +00001202}
1203
Devang Patel9b412732011-02-22 18:56:12 +00001204Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
Devang Patel746660f2010-12-07 23:25:47 +00001205 DIVariable VarInfo,
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001206 DIExpression Expr,
Devang Patel746660f2010-12-07 23:25:47 +00001207 BasicBlock *InsertAtEnd) {
1208 assert(V && "no value passed to dbg.value");
Manman Ren9822a112013-06-29 05:01:19 +00001209 assert(VarInfo.isVariable() &&
1210 "empty or invalid DIVariable passed to dbg.value");
Devang Patel746660f2010-12-07 23:25:47 +00001211 if (!ValueFn)
1212 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1213
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001214 trackIfUnresolved(VarInfo);
1215 trackIfUnresolved(Expr);
1216 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, V),
1217 ConstantInt::get(Type::getInt64Ty(VMContext), Offset),
1218 MetadataAsValue::get(VMContext, VarInfo),
1219 MetadataAsValue::get(VMContext, Expr)};
Jay Foad5bd375a2011-07-15 08:37:34 +00001220 return CallInst::Create(ValueFn, Args, "", InsertAtEnd);
Devang Patel746660f2010-12-07 23:25:47 +00001221}
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +00001222
1223void DIBuilder::replaceVTableHolder(DICompositeType &T, DICompositeType VTableHolder) {
1224 T.setContainingType(VTableHolder);
1225
1226 // If this didn't create a self-reference, just return.
1227 if (T != VTableHolder)
1228 return;
1229
1230 // Look for unresolved operands. T has dropped RAUW support and is already
1231 // marked resolved, orphaning any cycles underneath it.
1232 assert(T->isResolved() && "Expected self-reference to be resolved");
1233 for (const MDOperand &O : T->operands())
1234 if (auto *N = dyn_cast_or_null<MDNode>(O))
1235 trackIfUnresolved(N);
1236}
1237
1238void DIBuilder::replaceArrays(DICompositeType &T, DIArray Elements,
1239 DIArray TParams) {
1240 T.setArrays(Elements, TParams);
1241
1242 // If T isn't resolved, there's no problem.
1243 if (!T->isResolved())
1244 return;
1245
1246 // If "T" is resolved, it may be due to a self-reference cycle. Track the
1247 // arrays explicitly if they're unresolved, or else the cycles will be
1248 // orphaned.
1249 if (Elements)
1250 trackIfUnresolved(Elements);
1251 if (TParams)
1252 trackIfUnresolved(TParams);
1253}