blob: f193e5331dd1fae0e90dd518500a7acca7f8c2c2 [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()) {
Benjamin Kramer6cd780f2015-02-17 15:29:18 +000097 const auto &PV = PreservedVariables.lookup(SP);
98 SmallVector<Metadata *, 4> Variables(PV.begin(), PV.end());
Eric Christopher27deb262012-04-23 19:00:11 +000099 DIArray AV = getOrCreateArray(Variables);
100 DIType(Temp).replaceAllUsesWith(AV);
101 }
Devang Patel59e27c52011-08-19 23:28:12 +0000102 }
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000103
104 DIArray GVs = getOrCreateArray(AllGVs);
105 DIType(TempGVs).replaceAllUsesWith(GVs);
David Blaikief55abea2013-04-22 06:12:31 +0000106
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000107 SmallVector<Metadata *, 16> RetainValuesI(AllImportedModules.begin(),
108 AllImportedModules.end());
Eric Christopher2c3a6dc2014-02-28 21:27:57 +0000109 DIArray IMs = getOrCreateArray(RetainValuesI);
David Blaikief55abea2013-04-22 06:12:31 +0000110 DIType(TempImportedModules).replaceAllUsesWith(IMs);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000111
112 // Now that all temp nodes have been replaced or deleted, resolve remaining
113 // cycles.
114 for (const auto &N : UnresolvedNodes)
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +0000115 if (N && !N->isResolved())
116 N->resolveCycles();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000117 UnresolvedNodes.clear();
118
119 // Can't handle unresolved nodes anymore.
120 AllowUnresolvedNodes = false;
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000121}
122
Duncan P. N. Exon Smith379e3752014-10-01 21:32:15 +0000123/// If N is compile unit return NULL otherwise return N.
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000124static MDNode *getNonCompileUnitScope(MDNode *N) {
125 if (DIDescriptor(N).isCompileUnit())
Craig Topperc6207612014-04-09 06:08:46 +0000126 return nullptr;
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000127 return N;
Devang Patel2b8acaf2011-08-15 23:00:00 +0000128}
129
David Blaikieefb0d652013-03-20 23:58:12 +0000130static MDNode *createFilePathPair(LLVMContext &VMContext, StringRef Filename,
131 StringRef Directory) {
132 assert(!Filename.empty() && "Unable to create file without name");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000133 Metadata *Pair[] = {MDString::get(VMContext, Filename),
134 MDString::get(VMContext, Directory)};
David Blaikieefb0d652013-03-20 23:58:12 +0000135 return MDNode::get(VMContext, Pair);
136}
137
Eric Christopher03b3e112013-07-19 00:51:47 +0000138DICompileUnit DIBuilder::createCompileUnit(unsigned Lang, StringRef Filename,
139 StringRef Directory,
140 StringRef Producer, bool isOptimized,
141 StringRef Flags, unsigned RunTimeVer,
Eric Christopher75d49db2014-02-27 01:24:56 +0000142 StringRef SplitName,
Diego Novillo56653fd2014-06-24 17:02:03 +0000143 DebugEmissionKind Kind,
144 bool EmitDebugInfo) {
Eric Christopher75d49db2014-02-27 01:24:56 +0000145
Bruce Mitchener7e575ed2015-02-07 06:35:30 +0000146 assert(((Lang <= dwarf::DW_LANG_Fortran08 && Lang >= dwarf::DW_LANG_C89) ||
Chandler Carruth4c0ee742012-01-10 18:18:52 +0000147 (Lang <= dwarf::DW_LANG_hi_user && Lang >= dwarf::DW_LANG_lo_user)) &&
148 "Invalid Language tag");
149 assert(!Filename.empty() &&
150 "Unable to create compile unit without filename");
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000151
Duncan P. N. Exon Smithb93569d2015-02-12 21:52:11 +0000152 // TODO: Once we make MDCompileUnit distinct, stop using temporaries here
153 // (just start with operands assigned to nullptr).
154 TempEnumTypes = MDTuple::getTemporary(VMContext, None).release();
155 TempRetainTypes = MDTuple::getTemporary(VMContext, None).release();
156 TempSubprograms = MDTuple::getTemporary(VMContext, None).release();
157 TempGVs = MDTuple::getTemporary(VMContext, None).release();
158 TempImportedModules = MDTuple::getTemporary(VMContext, None).release();
David Blaikief55abea2013-04-22 06:12:31 +0000159
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000160 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_compile_unit)
161 .concat(Lang)
162 .concat(Producer)
163 .concat(isOptimized)
164 .concat(Flags)
165 .concat(RunTimeVer)
166 .concat(SplitName)
167 .concat(Kind)
168 .get(VMContext),
169 createFilePathPair(VMContext, Filename, Directory),
170 TempEnumTypes, TempRetainTypes, TempSubprograms, TempGVs,
171 TempImportedModules};
Eric Christopher03b3e112013-07-19 00:51:47 +0000172
Duncan P. N. Exon Smithb93569d2015-02-12 21:52:11 +0000173 // TODO: Switch to getDistinct(). We never want to merge compile units based
174 // on contents.
Eric Christopher03b3e112013-07-19 00:51:47 +0000175 MDNode *CUNode = MDNode::get(VMContext, Elts);
Devang Patel09fa69e2011-05-03 16:18:28 +0000176
177 // Create a named metadata so that it is easier to find cu in a module.
Diego Novillo56653fd2014-06-24 17:02:03 +0000178 // Note that we only generate this when the caller wants to actually
179 // emit debug information. When we are only interested in tracking
180 // source line locations throughout the backend, we prevent codegen from
181 // emitting debug info in the final output by not generating llvm.dbg.cu.
182 if (EmitDebugInfo) {
183 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
184 NMD->addOperand(CUNode);
185 }
Eric Christopher03b3e112013-07-19 00:51:47 +0000186
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000187 trackIfUnresolved(CUNode);
Eric Christopher03b3e112013-07-19 00:51:47 +0000188 return DICompileUnit(CUNode);
Devang Patel57c5a202010-11-04 15:01:38 +0000189}
190
David Blaikiee63d5d12013-05-20 22:50:35 +0000191static DIImportedEntity
David Blaikie2a40c142014-04-06 06:29:01 +0000192createImportedModule(LLVMContext &C, dwarf::Tag Tag, DIScope Context,
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000193 Metadata *NS, unsigned Line, StringRef Name,
194 SmallVectorImpl<TrackingMDNodeRef> &AllImportedModules) {
David Blaikiee63d5d12013-05-20 22:50:35 +0000195 const MDNode *R;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000196 Metadata *Elts[] = {HeaderBuilder::get(Tag).concat(Line).concat(Name).get(C),
197 Context, NS};
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000198 R = MDNode::get(C, Elts);
David Blaikiee63d5d12013-05-20 22:50:35 +0000199 DIImportedEntity M(R);
David Blaikie1fd43652013-05-07 21:35:53 +0000200 assert(M.Verify() && "Imported module should be valid");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000201 AllImportedModules.emplace_back(M.get());
David Blaikie1fd43652013-05-07 21:35:53 +0000202 return M;
203}
204
David Blaikiee63d5d12013-05-20 22:50:35 +0000205DIImportedEntity DIBuilder::createImportedModule(DIScope Context,
David Blaikie2a40c142014-04-06 06:29:01 +0000206 DINameSpace NS,
207 unsigned Line) {
208 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
209 Context, NS, Line, StringRef(), AllImportedModules);
David Blaikiee63d5d12013-05-20 22:50:35 +0000210}
211
212DIImportedEntity DIBuilder::createImportedModule(DIScope Context,
213 DIImportedEntity NS,
David Blaikie2a40c142014-04-06 06:29:01 +0000214 unsigned Line) {
215 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
216 Context, NS, Line, StringRef(), AllImportedModules);
David Blaikiee63d5d12013-05-20 22:50:35 +0000217}
218
David Blaikie1fd43652013-05-07 21:35:53 +0000219DIImportedEntity DIBuilder::createImportedDeclaration(DIScope Context,
Frederic Riss4aa51ae2014-11-06 17:46:55 +0000220 DIDescriptor Decl,
David Blaikie2a40c142014-04-06 06:29:01 +0000221 unsigned Line, StringRef Name) {
Frederic Riss4aa51ae2014-11-06 17:46:55 +0000222 // Make sure to use the unique identifier based metadata reference for
223 // types that have one.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000224 Metadata *V =
225 Decl.isType() ? static_cast<Metadata *>(DIType(Decl).getRef()) : Decl;
David Blaikie2a40c142014-04-06 06:29:01 +0000226 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_declaration,
Frederic Riss4aa51ae2014-11-06 17:46:55 +0000227 Context, V, Line, Name,
David Blaikie2a40c142014-04-06 06:29:01 +0000228 AllImportedModules);
229}
230
231DIImportedEntity DIBuilder::createImportedDeclaration(DIScope Context,
232 DIImportedEntity Imp,
233 unsigned Line, StringRef Name) {
234 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_declaration,
235 Context, Imp, Line, Name, AllImportedModules);
David Blaikief55abea2013-04-22 06:12:31 +0000236}
237
Devang Patel9b412732011-02-22 18:56:12 +0000238DIFile DIBuilder::createFile(StringRef Filename, StringRef Directory) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000239 Metadata *Elts[] = {
240 HeaderBuilder::get(dwarf::DW_TAG_file_type).get(VMContext),
241 createFilePathPair(VMContext, Filename, Directory)};
David Blaikie5692e722013-03-28 02:44:59 +0000242 return DIFile(MDNode::get(VMContext, Elts));
Devang Patel57c5a202010-11-04 15:01:38 +0000243}
244
David Blaikieb7619002013-06-24 17:34:33 +0000245DIEnumerator DIBuilder::createEnumerator(StringRef Name, int64_t Val) {
Devang Patel1ad1abe2011-09-12 18:26:08 +0000246 assert(!Name.empty() && "Unable to create enumerator without name");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000247 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_enumerator)
248 .concat(Name)
249 .concat(Val)
250 .get(VMContext)};
David Blaikie5692e722013-03-28 02:44:59 +0000251 return DIEnumerator(MDNode::get(VMContext, Elts));
Devang Patel57c5a202010-11-04 15:01:38 +0000252}
253
Peter Collingbournea4a47cb2013-06-27 22:50:59 +0000254DIBasicType DIBuilder::createUnspecifiedType(StringRef Name) {
Devang Patel04d6d472011-09-14 23:13:28 +0000255 assert(!Name.empty() && "Unable to create type without name");
Peter Collingbournea4a47cb2013-06-27 22:50:59 +0000256 // Unspecified types are encoded in DIBasicType format. Line number, filename,
257 // size, alignment, offset and flags are always empty here.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000258 Metadata *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000259 HeaderBuilder::get(dwarf::DW_TAG_unspecified_type)
260 .concat(Name)
261 .concat(0)
262 .concat(0)
263 .concat(0)
264 .concat(0)
265 .concat(0)
266 .concat(0)
267 .get(VMContext),
268 nullptr, // Filename
269 nullptr // Unused
Devang Patel04d6d472011-09-14 23:13:28 +0000270 };
Manman Ren3c6acec2013-06-07 18:35:53 +0000271 return DIBasicType(MDNode::get(VMContext, Elts));
Devang Patel04d6d472011-09-14 23:13:28 +0000272}
273
Peter Collingbournea4a47cb2013-06-27 22:50:59 +0000274DIBasicType DIBuilder::createNullPtrType() {
275 return createUnspecifiedType("decltype(nullptr)");
276}
277
David Blaikie209d63a2013-02-12 00:40:41 +0000278DIBasicType
279DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
280 uint64_t AlignInBits, unsigned Encoding) {
Devang Patel1ad1abe2011-09-12 18:26:08 +0000281 assert(!Name.empty() && "Unable to create type without name");
Devang Patel57c5a202010-11-04 15:01:38 +0000282 // Basic types are encoded in DIBasicType format. Line number, filename,
283 // offset and flags are always empty here.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000284 Metadata *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000285 HeaderBuilder::get(dwarf::DW_TAG_base_type)
286 .concat(Name)
287 .concat(0) // Line
288 .concat(SizeInBits)
289 .concat(AlignInBits)
290 .concat(0) // Offset
291 .concat(0) // Flags
292 .concat(Encoding)
293 .get(VMContext),
294 nullptr, // Filename
295 nullptr // Unused
Benjamin Kramered8b7bf2010-11-04 18:45:27 +0000296 };
David Blaikie5692e722013-03-28 02:44:59 +0000297 return DIBasicType(MDNode::get(VMContext, Elts));
Devang Patel57c5a202010-11-04 15:01:38 +0000298}
299
David Blaikief11de2f2013-02-18 06:41:57 +0000300DIDerivedType DIBuilder::createQualifiedType(unsigned Tag, DIType FromTy) {
Benjamin Kramered8b7bf2010-11-04 18:45:27 +0000301 // Qualified types are encoded in DIDerivedType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000302 Metadata *Elts[] = {HeaderBuilder::get(Tag)
303 .concat(StringRef()) // Name
304 .concat(0) // Line
305 .concat(0) // Size
306 .concat(0) // Align
307 .concat(0) // Offset
308 .concat(0) // Flags
309 .get(VMContext),
310 nullptr, // Filename
311 nullptr, // Unused
312 FromTy.getRef()};
David Blaikie5692e722013-03-28 02:44:59 +0000313 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel57c5a202010-11-04 15:01:38 +0000314}
315
David Blaikief11de2f2013-02-18 06:41:57 +0000316DIDerivedType
317DIBuilder::createPointerType(DIType PointeeTy, uint64_t SizeInBits,
318 uint64_t AlignInBits, StringRef Name) {
Benjamin Kramered8b7bf2010-11-04 18:45:27 +0000319 // Pointer types are encoded in DIDerivedType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000320 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_pointer_type)
321 .concat(Name)
322 .concat(0) // Line
323 .concat(SizeInBits)
324 .concat(AlignInBits)
325 .concat(0) // Offset
326 .concat(0) // Flags
327 .get(VMContext),
328 nullptr, // Filename
329 nullptr, // Unused
330 PointeeTy.getRef()};
David Blaikie5692e722013-03-28 02:44:59 +0000331 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel57c5a202010-11-04 15:01:38 +0000332}
333
Adrian Prantl48af2ef2014-12-23 19:11:47 +0000334DIDerivedType
335DIBuilder::createMemberPointerType(DIType PointeeTy, DIType Base,
336 uint64_t SizeInBits, uint64_t AlignInBits) {
David Blaikie5d3249b2013-01-07 05:51:15 +0000337 // Pointer types are encoded in DIDerivedType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000338 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_ptr_to_member_type)
339 .concat(StringRef())
340 .concat(0) // Line
Adrian Prantl48af2ef2014-12-23 19:11:47 +0000341 .concat(SizeInBits) // Size
342 .concat(AlignInBits) // Align
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000343 .concat(0) // Offset
344 .concat(0) // Flags
345 .get(VMContext),
346 nullptr, // Filename
347 nullptr, // Unused
348 PointeeTy.getRef(), Base.getRef()};
David Blaikie5692e722013-03-28 02:44:59 +0000349 return DIDerivedType(MDNode::get(VMContext, Elts));
David Blaikie5d3249b2013-01-07 05:51:15 +0000350}
351
David Blaikief11de2f2013-02-18 06:41:57 +0000352DIDerivedType DIBuilder::createReferenceType(unsigned Tag, DIType RTy) {
Manman Ren74c188f2013-07-01 21:02:01 +0000353 assert(RTy.isType() && "Unable to create reference type");
Benjamin Kramered8b7bf2010-11-04 18:45:27 +0000354 // References are encoded in DIDerivedType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000355 Metadata *Elts[] = {HeaderBuilder::get(Tag)
356 .concat(StringRef()) // Name
357 .concat(0) // Line
358 .concat(0) // Size
359 .concat(0) // Align
360 .concat(0) // Offset
361 .concat(0) // Flags
362 .get(VMContext),
363 nullptr, // Filename
364 nullptr, // TheCU,
365 RTy.getRef()};
David Blaikie5692e722013-03-28 02:44:59 +0000366 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel57c5a202010-11-04 15:01:38 +0000367}
368
David Blaikief11de2f2013-02-18 06:41:57 +0000369DIDerivedType DIBuilder::createTypedef(DIType Ty, StringRef Name, DIFile File,
370 unsigned LineNo, DIDescriptor Context) {
Benjamin Kramered8b7bf2010-11-04 18:45:27 +0000371 // typedefs are encoded in DIDerivedType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000372 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_typedef)
373 .concat(Name)
374 .concat(LineNo)
375 .concat(0) // Size
376 .concat(0) // Align
377 .concat(0) // Offset
378 .concat(0) // Flags
379 .get(VMContext),
380 File.getFileNode(),
381 DIScope(getNonCompileUnitScope(Context)).getRef(),
382 Ty.getRef()};
David Blaikie5692e722013-03-28 02:44:59 +0000383 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel57c5a202010-11-04 15:01:38 +0000384}
385
Manman Ren3c6acec2013-06-07 18:35:53 +0000386DIDerivedType DIBuilder::createFriend(DIType Ty, DIType FriendTy) {
Benjamin Kramered8b7bf2010-11-04 18:45:27 +0000387 // typedefs are encoded in DIDerivedType format.
Manman Ren74c188f2013-07-01 21:02:01 +0000388 assert(Ty.isType() && "Invalid type!");
389 assert(FriendTy.isType() && "Invalid friend type!");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000390 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_friend)
391 .concat(StringRef()) // Name
392 .concat(0) // Line
393 .concat(0) // Size
394 .concat(0) // Align
395 .concat(0) // Offset
396 .concat(0) // Flags
397 .get(VMContext),
398 nullptr, Ty.getRef(), FriendTy.getRef()};
Manman Ren3c6acec2013-06-07 18:35:53 +0000399 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel57c5a202010-11-04 15:01:38 +0000400}
401
Eric Christopher98f9c232013-10-15 23:31:31 +0000402DIDerivedType DIBuilder::createInheritance(DIType Ty, DIType BaseTy,
403 uint64_t BaseOffset,
404 unsigned Flags) {
Manman Ren74c188f2013-07-01 21:02:01 +0000405 assert(Ty.isType() && "Unable to create inheritance");
Benjamin Kramered8b7bf2010-11-04 18:45:27 +0000406 // TAG_inheritance is encoded in DIDerivedType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000407 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_inheritance)
408 .concat(StringRef()) // Name
409 .concat(0) // Line
410 .concat(0) // Size
411 .concat(0) // Align
412 .concat(BaseOffset)
413 .concat(Flags)
414 .get(VMContext),
415 nullptr, Ty.getRef(), BaseTy.getRef()};
Adrian Prantl9a804922015-02-11 17:45:08 +0000416 auto R = DIDerivedType(MDNode::get(VMContext, Elts));
Adrian Prantl9a804922015-02-11 17:45:08 +0000417 return R;
Devang Patel57c5a202010-11-04 15:01:38 +0000418}
419
Eric Christopher98f9c232013-10-15 23:31:31 +0000420DIDerivedType DIBuilder::createMemberType(DIDescriptor Scope, StringRef Name,
421 DIFile File, unsigned LineNumber,
422 uint64_t SizeInBits,
423 uint64_t AlignInBits,
424 uint64_t OffsetInBits, unsigned Flags,
425 DIType Ty) {
Benjamin Kramered8b7bf2010-11-04 18:45:27 +0000426 // TAG_member is encoded in DIDerivedType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000427 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_member)
428 .concat(Name)
429 .concat(LineNumber)
430 .concat(SizeInBits)
431 .concat(AlignInBits)
432 .concat(OffsetInBits)
433 .concat(Flags)
434 .get(VMContext),
435 File.getFileNode(),
436 DIScope(getNonCompileUnitScope(Scope)).getRef(),
437 Ty.getRef()};
David Blaikie5692e722013-03-28 02:44:59 +0000438 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel57c5a202010-11-04 15:01:38 +0000439}
440
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000441static Metadata *getConstantOrNull(Constant *C) {
442 if (C)
443 return ConstantAsMetadata::get(C);
444 return nullptr;
445}
446
Duncan P. N. Exon Smithdbf64acd2014-11-15 00:23:49 +0000447DIDerivedType DIBuilder::createStaticMemberType(DIDescriptor Scope,
448 StringRef Name, DIFile File,
449 unsigned LineNumber, DIType Ty,
450 unsigned Flags,
451 llvm::Constant *Val) {
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000452 // TAG_member is encoded in DIDerivedType format.
453 Flags |= DIDescriptor::FlagStaticMember;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000454 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_member)
455 .concat(Name)
456 .concat(LineNumber)
457 .concat(0) // Size
458 .concat(0) // Align
459 .concat(0) // Offset
460 .concat(Flags)
461 .get(VMContext),
462 File.getFileNode(),
463 DIScope(getNonCompileUnitScope(Scope)).getRef(),
464 Ty.getRef(), getConstantOrNull(Val)};
Manman Ren3c6acec2013-06-07 18:35:53 +0000465 return DIDerivedType(MDNode::get(VMContext, Elts));
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000466}
467
Eric Christopher98f9c232013-10-15 23:31:31 +0000468DIDerivedType DIBuilder::createObjCIVar(StringRef Name, DIFile File,
469 unsigned LineNumber,
470 uint64_t SizeInBits,
471 uint64_t AlignInBits,
472 uint64_t OffsetInBits, unsigned Flags,
473 DIType Ty, MDNode *PropertyNode) {
Devang Patel44882172012-02-06 17:49:43 +0000474 // TAG_member is encoded in DIDerivedType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000475 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_member)
476 .concat(Name)
477 .concat(LineNumber)
478 .concat(SizeInBits)
479 .concat(AlignInBits)
480 .concat(OffsetInBits)
481 .concat(Flags)
482 .get(VMContext),
483 File.getFileNode(), getNonCompileUnitScope(File), Ty,
484 PropertyNode};
Manman Ren3c6acec2013-06-07 18:35:53 +0000485 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel44882172012-02-06 17:49:43 +0000486}
487
Eric Christopher98f9c232013-10-15 23:31:31 +0000488DIObjCProperty
489DIBuilder::createObjCProperty(StringRef Name, DIFile File, unsigned LineNumber,
490 StringRef GetterName, StringRef SetterName,
491 unsigned PropertyAttributes, DIType Ty) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000492 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_APPLE_property)
493 .concat(Name)
494 .concat(LineNumber)
495 .concat(GetterName)
496 .concat(SetterName)
497 .concat(PropertyAttributes)
498 .get(VMContext),
499 File, Ty};
David Blaikie5692e722013-03-28 02:44:59 +0000500 return DIObjCProperty(MDNode::get(VMContext, Elts));
Devang Patelcc481592012-02-04 00:59:25 +0000501}
502
Eric Christopher3cc90fe2011-08-26 21:02:40 +0000503DITemplateTypeParameter
Devang Patel9b412732011-02-22 18:56:12 +0000504DIBuilder::createTemplateTypeParameter(DIDescriptor Context, StringRef Name,
Duncan P. N. Exon Smithb4aa16f2015-02-13 03:35:29 +0000505 DIType Ty) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000506 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_template_type_parameter)
507 .concat(Name)
Duncan P. N. Exon Smithb4aa16f2015-02-13 03:35:29 +0000508 .concat(0)
509 .concat(0)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000510 .get(VMContext),
511 DIScope(getNonCompileUnitScope(Context)).getRef(),
Duncan P. N. Exon Smithb4aa16f2015-02-13 03:35:29 +0000512 Ty.getRef(), nullptr};
David Blaikie5692e722013-03-28 02:44:59 +0000513 return DITemplateTypeParameter(MDNode::get(VMContext, Elts));
Devang Patel3a9e65e2011-02-02 21:38:25 +0000514}
515
Duncan P. N. Exon Smithb4aa16f2015-02-13 03:35:29 +0000516static DITemplateValueParameter
517createTemplateValueParameterHelper(LLVMContext &VMContext, unsigned Tag,
518 DIDescriptor Context, StringRef Name,
519 DIType Ty, Metadata *MD) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000520 Metadata *Elts[] = {
Duncan P. N. Exon Smithb4aa16f2015-02-13 03:35:29 +0000521 HeaderBuilder::get(Tag).concat(Name).concat(0).concat(0).get(VMContext),
522 DIScope(getNonCompileUnitScope(Context)).getRef(), Ty.getRef(), MD,
523 nullptr};
David Blaikie5692e722013-03-28 02:44:59 +0000524 return DITemplateValueParameter(MDNode::get(VMContext, Elts));
Devang Patelbe933b42011-02-02 22:35:53 +0000525}
526
David Blaikie2b380232013-06-22 18:59:11 +0000527DITemplateValueParameter
528DIBuilder::createTemplateValueParameter(DIDescriptor Context, StringRef Name,
Duncan P. N. Exon Smithb4aa16f2015-02-13 03:35:29 +0000529 DIType Ty, Constant *Val) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000530 return createTemplateValueParameterHelper(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000531 VMContext, dwarf::DW_TAG_template_value_parameter, Context, Name, Ty,
Duncan P. N. Exon Smithb4aa16f2015-02-13 03:35:29 +0000532 getConstantOrNull(Val));
David Blaikie2b380232013-06-22 18:59:11 +0000533}
534
535DITemplateValueParameter
536DIBuilder::createTemplateTemplateParameter(DIDescriptor Context, StringRef Name,
Duncan P. N. Exon Smithb4aa16f2015-02-13 03:35:29 +0000537 DIType Ty, StringRef Val) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000538 return createTemplateValueParameterHelper(
539 VMContext, dwarf::DW_TAG_GNU_template_template_param, Context, Name, Ty,
Duncan P. N. Exon Smithb4aa16f2015-02-13 03:35:29 +0000540 MDString::get(VMContext, Val));
David Blaikie2b380232013-06-22 18:59:11 +0000541}
542
543DITemplateValueParameter
544DIBuilder::createTemplateParameterPack(DIDescriptor Context, StringRef Name,
Duncan P. N. Exon Smithb4aa16f2015-02-13 03:35:29 +0000545 DIType Ty, DIArray Val) {
Duncan P. N. Exon Smith774951f2014-11-15 00:05:04 +0000546 return createTemplateValueParameterHelper(
547 VMContext, dwarf::DW_TAG_GNU_template_parameter_pack, Context, Name, Ty,
Duncan P. N. Exon Smithb4aa16f2015-02-13 03:35:29 +0000548 Val);
David Blaikie2b380232013-06-22 18:59:11 +0000549}
550
David Blaikiea7310a32013-03-26 23:46:39 +0000551DICompositeType DIBuilder::createClassType(DIDescriptor Context, StringRef Name,
552 DIFile File, unsigned LineNumber,
553 uint64_t SizeInBits,
554 uint64_t AlignInBits,
555 uint64_t OffsetInBits,
556 unsigned Flags, DIType DerivedFrom,
557 DIArray Elements,
Manman Ren27552062013-09-06 23:54:23 +0000558 DIType VTableHolder,
Manman Ren547467b2013-08-27 23:06:40 +0000559 MDNode *TemplateParams,
560 StringRef UniqueIdentifier) {
Manman Ren74c188f2013-07-01 21:02:01 +0000561 assert((!Context || Context.isScope() || Context.isType()) &&
David Blaikie085abe32013-03-11 23:21:19 +0000562 "createClassType should be called with a valid Context");
563 // TAG_class_type is encoded in DICompositeType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000564 Metadata *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000565 HeaderBuilder::get(dwarf::DW_TAG_class_type)
566 .concat(Name)
567 .concat(LineNumber)
568 .concat(SizeInBits)
569 .concat(AlignInBits)
570 .concat(OffsetInBits)
571 .concat(Flags)
572 .concat(0)
573 .get(VMContext),
574 File.getFileNode(), DIScope(getNonCompileUnitScope(Context)).getRef(),
575 DerivedFrom.getRef(), Elements, VTableHolder.getRef(), TemplateParams,
576 UniqueIdentifier.empty() ? nullptr
577 : MDString::get(VMContext, UniqueIdentifier)};
David Blaikiea7310a32013-03-26 23:46:39 +0000578 DICompositeType R(MDNode::get(VMContext, Elts));
Manman Ren74c188f2013-07-01 21:02:01 +0000579 assert(R.isCompositeType() &&
580 "createClassType should return a DICompositeType");
Manman Ren0b410402013-08-29 23:17:54 +0000581 if (!UniqueIdentifier.empty())
582 retainType(R);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000583 trackIfUnresolved(R);
David Blaikie085abe32013-03-11 23:21:19 +0000584 return R;
Eric Christopher17426692012-07-06 02:35:57 +0000585}
586
David Blaikiebbe0e1a2013-02-25 01:07:18 +0000587DICompositeType DIBuilder::createStructType(DIDescriptor Context,
588 StringRef Name, DIFile File,
589 unsigned LineNumber,
590 uint64_t SizeInBits,
591 uint64_t AlignInBits,
592 unsigned Flags, DIType DerivedFrom,
593 DIArray Elements,
594 unsigned RunTimeLang,
Manman Ren27552062013-09-06 23:54:23 +0000595 DIType VTableHolder,
Manman Ren547467b2013-08-27 23:06:40 +0000596 StringRef UniqueIdentifier) {
Devang Patel746660f2010-12-07 23:25:47 +0000597 // TAG_structure_type is encoded in DICompositeType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000598 Metadata *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000599 HeaderBuilder::get(dwarf::DW_TAG_structure_type)
600 .concat(Name)
601 .concat(LineNumber)
602 .concat(SizeInBits)
603 .concat(AlignInBits)
604 .concat(0)
605 .concat(Flags)
606 .concat(RunTimeLang)
607 .get(VMContext),
608 File.getFileNode(), DIScope(getNonCompileUnitScope(Context)).getRef(),
609 DerivedFrom.getRef(), Elements, VTableHolder.getRef(), nullptr,
610 UniqueIdentifier.empty() ? nullptr
611 : MDString::get(VMContext, UniqueIdentifier)};
David Blaikie085abe32013-03-11 23:21:19 +0000612 DICompositeType R(MDNode::get(VMContext, Elts));
Manman Ren74c188f2013-07-01 21:02:01 +0000613 assert(R.isCompositeType() &&
614 "createStructType should return a DICompositeType");
Manman Ren0b410402013-08-29 23:17:54 +0000615 if (!UniqueIdentifier.empty())
616 retainType(R);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000617 trackIfUnresolved(R);
David Blaikie085abe32013-03-11 23:21:19 +0000618 return R;
Devang Patel746660f2010-12-07 23:25:47 +0000619}
620
Eric Christopher17dd8f02013-04-02 22:55:52 +0000621DICompositeType DIBuilder::createUnionType(DIDescriptor Scope, StringRef Name,
622 DIFile File, unsigned LineNumber,
623 uint64_t SizeInBits,
624 uint64_t AlignInBits, unsigned Flags,
625 DIArray Elements,
Manman Ren547467b2013-08-27 23:06:40 +0000626 unsigned RunTimeLang,
627 StringRef UniqueIdentifier) {
Devang Patel89ea4f22010-12-08 01:50:15 +0000628 // TAG_union_type is encoded in DICompositeType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000629 Metadata *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000630 HeaderBuilder::get(dwarf::DW_TAG_union_type)
631 .concat(Name)
632 .concat(LineNumber)
633 .concat(SizeInBits)
634 .concat(AlignInBits)
635 .concat(0) // Offset
636 .concat(Flags)
637 .concat(RunTimeLang)
638 .get(VMContext),
639 File.getFileNode(), DIScope(getNonCompileUnitScope(Scope)).getRef(),
640 nullptr, Elements, nullptr, nullptr,
641 UniqueIdentifier.empty() ? nullptr
642 : MDString::get(VMContext, UniqueIdentifier)};
Manman Ren0b410402013-08-29 23:17:54 +0000643 DICompositeType R(MDNode::get(VMContext, Elts));
644 if (!UniqueIdentifier.empty())
645 retainType(R);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000646 trackIfUnresolved(R);
Manman Ren0b410402013-08-29 23:17:54 +0000647 return R;
Devang Patel89ea4f22010-12-08 01:50:15 +0000648}
649
Manman Renf8a19672014-07-28 22:24:06 +0000650DISubroutineType DIBuilder::createSubroutineType(DIFile File,
651 DITypeArray ParameterTypes,
652 unsigned Flags) {
Devang Patel89ea4f22010-12-08 01:50:15 +0000653 // TAG_subroutine_type is encoded in DICompositeType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000654 Metadata *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000655 HeaderBuilder::get(dwarf::DW_TAG_subroutine_type)
656 .concat(StringRef())
657 .concat(0) // Line
658 .concat(0) // Size
659 .concat(0) // Align
660 .concat(0) // Offset
661 .concat(Flags) // Flags
662 .concat(0)
663 .get(VMContext),
664 nullptr, nullptr, nullptr, ParameterTypes, nullptr, nullptr,
665 nullptr // Type Identifer
Devang Patel89ea4f22010-12-08 01:50:15 +0000666 };
Manman Renf8a19672014-07-28 22:24:06 +0000667 return DISubroutineType(MDNode::get(VMContext, Elts));
Devang Patel89ea4f22010-12-08 01:50:15 +0000668}
669
David Blaikief11de2f2013-02-18 06:41:57 +0000670DICompositeType DIBuilder::createEnumerationType(
671 DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
672 uint64_t SizeInBits, uint64_t AlignInBits, DIArray Elements,
Manman Ren547467b2013-08-27 23:06:40 +0000673 DIType UnderlyingType, StringRef UniqueIdentifier) {
Devang Patel89ea4f22010-12-08 01:50:15 +0000674 // TAG_enumeration_type is encoded in DICompositeType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000675 Metadata *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000676 HeaderBuilder::get(dwarf::DW_TAG_enumeration_type)
677 .concat(Name)
678 .concat(LineNumber)
679 .concat(SizeInBits)
680 .concat(AlignInBits)
681 .concat(0) // Offset
682 .concat(0) // Flags
683 .concat(0)
684 .get(VMContext),
685 File.getFileNode(), DIScope(getNonCompileUnitScope(Scope)).getRef(),
686 UnderlyingType.getRef(), Elements, nullptr, nullptr,
687 UniqueIdentifier.empty() ? nullptr
688 : MDString::get(VMContext, UniqueIdentifier)};
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000689 DICompositeType CTy(MDNode::get(VMContext, Elts));
690 AllEnumTypes.push_back(CTy);
Manman Ren0b410402013-08-29 23:17:54 +0000691 if (!UniqueIdentifier.empty())
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000692 retainType(CTy);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000693 trackIfUnresolved(CTy);
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000694 return CTy;
Devang Patel89ea4f22010-12-08 01:50:15 +0000695}
696
David Blaikief11de2f2013-02-18 06:41:57 +0000697DICompositeType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
698 DIType Ty, DIArray Subscripts) {
Devang Patel89ea4f22010-12-08 01:50:15 +0000699 // TAG_array_type is encoded in DICompositeType format.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000700 Metadata *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000701 HeaderBuilder::get(dwarf::DW_TAG_array_type)
702 .concat(StringRef())
703 .concat(0) // Line
704 .concat(Size)
705 .concat(AlignInBits)
706 .concat(0) // Offset
707 .concat(0) // Flags
708 .concat(0)
709 .get(VMContext),
710 nullptr, // Filename/Directory,
711 nullptr, // Unused
712 Ty.getRef(), Subscripts, nullptr, nullptr,
713 nullptr // Type Identifer
Devang Patel89ea4f22010-12-08 01:50:15 +0000714 };
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000715 DICompositeType R(MDNode::get(VMContext, Elts));
716 trackIfUnresolved(R);
717 return R;
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 };
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000738 DICompositeType R(MDNode::get(VMContext, Elts));
739 trackIfUnresolved(R);
740 return R;
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));
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000766 Elts.append(N->op_begin() + 1, N->op_end());
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000767
768 return DIType(MDNode::get(Context, Elts));
769}
770
Devang Patel9b412732011-02-22 18:56:12 +0000771DIType DIBuilder::createArtificialType(DIType Ty) {
Devang Patel57c5a202010-11-04 15:01:38 +0000772 if (Ty.isArtificial())
773 return Ty;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000774 return createTypeWithFlags(VMContext, Ty, DIType::FlagArtificial);
Devang Patel57c5a202010-11-04 15:01:38 +0000775}
Devang Patel746660f2010-12-07 23:25:47 +0000776
Eric Christophere3417762012-09-12 23:36:19 +0000777DIType DIBuilder::createObjectPointerType(DIType Ty) {
778 if (Ty.isObjectPointer())
779 return Ty;
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000780 unsigned Flags = DIType::FlagObjectPointer | DIType::FlagArtificial;
781 return createTypeWithFlags(VMContext, Ty, Flags);
Eric Christophere3417762012-09-12 23:36:19 +0000782}
783
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000784void DIBuilder::retainType(DIType T) { AllRetainTypes.emplace_back(T); }
Devang Patel89ea4f22010-12-08 01:50:15 +0000785
Manman Renf93ac4b2014-07-29 18:20:39 +0000786DIBasicType DIBuilder::createUnspecifiedParameter() {
Manman Ren72b07e82014-07-29 22:58:13 +0000787 return DIBasicType();
Devang Patel89ea4f22010-12-08 01:50:15 +0000788}
789
Eric Christopher98f9c232013-10-15 23:31:31 +0000790DICompositeType
791DIBuilder::createForwardDecl(unsigned Tag, StringRef Name, DIDescriptor Scope,
792 DIFile F, unsigned Line, unsigned RuntimeLang,
793 uint64_t SizeInBits, uint64_t AlignInBits,
794 StringRef UniqueIdentifier) {
Eric Christopherae56eec2012-02-08 00:22:26 +0000795 // Create a temporary MDNode.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000796 Metadata *Elts[] = {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000797 HeaderBuilder::get(Tag)
798 .concat(Name)
799 .concat(Line)
800 .concat(SizeInBits)
801 .concat(AlignInBits)
802 .concat(0) // Offset
803 .concat(DIDescriptor::FlagFwdDecl)
804 .concat(RuntimeLang)
805 .get(VMContext),
806 F.getFileNode(), DIScope(getNonCompileUnitScope(Scope)).getRef(), nullptr,
807 DIArray(), nullptr,
808 nullptr, // TemplateParams
809 UniqueIdentifier.empty() ? nullptr
810 : MDString::get(VMContext, UniqueIdentifier)};
David Blaikied3f094a2014-05-06 03:41:57 +0000811 MDNode *Node = MDNode::get(VMContext, Elts);
812 DICompositeType RetTy(Node);
813 assert(RetTy.isCompositeType() &&
814 "createForwardDecl result should be a DIType");
815 if (!UniqueIdentifier.empty())
816 retainType(RetTy);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000817 trackIfUnresolved(RetTy);
David Blaikied3f094a2014-05-06 03:41:57 +0000818 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);
Adrian Prantlea7f1c22015-02-17 19:17:39 +0000846 trackIfUnresolved(RetTy);
Manman Rend0e67aa2013-07-02 18:37:35 +0000847 return RetTy;
Eric Christopherae56eec2012-02-08 00:22:26 +0000848}
849
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000850DIArray DIBuilder::getOrCreateArray(ArrayRef<Metadata *> Elements) {
Jay Foaddbf81d82011-04-24 10:11:03 +0000851 return DIArray(MDNode::get(VMContext, Elements));
Devang Patel746660f2010-12-07 23:25:47 +0000852}
853
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000854DITypeArray DIBuilder::getOrCreateTypeArray(ArrayRef<Metadata *> Elements) {
855 SmallVector<llvm::Metadata *, 16> Elts;
Manman Ren1a125c92014-07-28 19:33:20 +0000856 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
857 if (Elements[i] && isa<MDNode>(Elements[i]))
858 Elts.push_back(DIType(cast<MDNode>(Elements[i])).getRef());
859 else
860 Elts.push_back(Elements[i]);
861 }
862 return DITypeArray(MDNode::get(VMContext, Elts));
863}
864
Bill Wendlingd7767122012-12-04 21:34:03 +0000865DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000866 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_subrange_type)
867 .concat(Lo)
868 .concat(Count)
869 .get(VMContext)};
Devang Patel89ea4f22010-12-08 01:50:15 +0000870
Devang Patel0c773242011-04-18 23:51:03 +0000871 return DISubrange(MDNode::get(VMContext, Elts));
Devang Patel89ea4f22010-12-08 01:50:15 +0000872}
873
Duncan P. N. Exon Smithdbf64acd2014-11-15 00:23:49 +0000874static DIGlobalVariable createGlobalVariableHelper(
875 LLVMContext &VMContext, DIDescriptor Context, StringRef Name,
876 StringRef LinkageName, DIFile F, unsigned LineNumber, DITypeRef Ty,
877 bool isLocalToUnit, Constant *Val, MDNode *Decl, bool isDefinition,
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000878 std::function<MDNode *(ArrayRef<Metadata *>)> CreateFunc) {
Manman Renbfd2b8292014-11-21 19:47:48 +0000879
880 MDNode *TheCtx = getNonCompileUnitScope(Context);
881 if (DIScope(TheCtx).isCompositeType()) {
882 assert(!DICompositeType(TheCtx).getIdentifier() &&
883 "Context of a global variable should not be a type with identifier");
884 }
885
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000886 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_variable)
887 .concat(Name)
888 .concat(Name)
889 .concat(LinkageName)
890 .concat(LineNumber)
891 .concat(isLocalToUnit)
892 .concat(isDefinition)
893 .get(VMContext),
894 TheCtx, F, Ty, getConstantOrNull(Val),
895 DIDescriptor(Decl)};
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000896
897 return DIGlobalVariable(CreateFunc(Elts));
898}
899
Duncan P. N. Exon Smithdbf64acd2014-11-15 00:23:49 +0000900DIGlobalVariable DIBuilder::createGlobalVariable(
901 DIDescriptor Context, StringRef Name, StringRef LinkageName, DIFile F,
902 unsigned LineNumber, DITypeRef Ty, bool isLocalToUnit, Constant *Val,
903 MDNode *Decl) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000904 return createGlobalVariableHelper(
905 VMContext, Context, Name, LinkageName, F, LineNumber, Ty, isLocalToUnit,
906 Val, Decl, true, [&](ArrayRef<Metadata *> Elts) -> MDNode *{
907 MDNode *Node = MDNode::get(VMContext, Elts);
908 AllGVs.push_back(Node);
909 return Node;
910 });
Frederic Riss5e6bc9e2014-09-17 09:28:34 +0000911}
912
Duncan P. N. Exon Smithdbf64acd2014-11-15 00:23:49 +0000913DIGlobalVariable DIBuilder::createTempGlobalVariableFwdDecl(
914 DIDescriptor Context, StringRef Name, StringRef LinkageName, DIFile F,
915 unsigned LineNumber, DITypeRef Ty, bool isLocalToUnit, Constant *Val,
916 MDNode *Decl) {
Jyoti Allurb76b57f2014-09-29 06:32:54 +0000917 return createGlobalVariableHelper(VMContext, Context, Name, LinkageName, F,
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000918 LineNumber, Ty, isLocalToUnit, Val, Decl,
919 false, [&](ArrayRef<Metadata *> Elts) {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000920 return MDNode::getTemporary(VMContext, Elts).release();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000921 });
Devang Patel746660f2010-12-07 23:25:47 +0000922}
923
Devang Patel9b412732011-02-22 18:56:12 +0000924DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
Devang Patel63f83cd2010-12-07 23:58:00 +0000925 StringRef Name, DIFile File,
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000926 unsigned LineNo, DITypeRef Ty,
Devang Patel40eee1e2011-03-01 22:58:13 +0000927 bool AlwaysPreserve, unsigned Flags,
928 unsigned ArgNo) {
David Blaikie085abe32013-03-11 23:21:19 +0000929 DIDescriptor Context(getNonCompileUnitScope(Scope));
Manman Ren74c188f2013-07-01 21:02:01 +0000930 assert((!Context || Context.isScope()) &&
David Blaikie085abe32013-03-11 23:21:19 +0000931 "createLocalVariable should be called with a valid Context");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000932 Metadata *Elts[] = {HeaderBuilder::get(Tag)
933 .concat(Name)
934 .concat(LineNo | (ArgNo << 24))
935 .concat(Flags)
936 .get(VMContext),
937 getNonCompileUnitScope(Scope), File, Ty};
Devang Patel0c773242011-04-18 23:51:03 +0000938 MDNode *Node = MDNode::get(VMContext, Elts);
Devang Patel63f83cd2010-12-07 23:58:00 +0000939 if (AlwaysPreserve) {
940 // The optimizer may remove local variable. If there is an interest
941 // to preserve variable info in such situation then stash it in a
942 // named mdnode.
943 DISubprogram Fn(getDISubprogram(Scope));
Duncan P. N. Exon Smith3bfffde2014-10-15 16:11:41 +0000944 assert(Fn && "Missing subprogram for local variable");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000945 PreservedVariables[Fn].emplace_back(Node);
Devang Patel63f83cd2010-12-07 23:58:00 +0000946 }
Manman Rend0e67aa2013-07-02 18:37:35 +0000947 DIVariable RetVar(Node);
948 assert(RetVar.isVariable() &&
Manman Ren74c188f2013-07-01 21:02:01 +0000949 "createLocalVariable should return a valid DIVariable");
Manman Rend0e67aa2013-07-02 18:37:35 +0000950 return RetVar;
Devang Patel63f83cd2010-12-07 23:58:00 +0000951}
952
Duncan P. N. Exon Smithbd75ad42015-02-09 22:13:27 +0000953DIExpression DIBuilder::createExpression(ArrayRef<uint64_t> Addr) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000954 auto Header = HeaderBuilder::get(DW_TAG_expression);
Duncan P. N. Exon Smithbd75ad42015-02-09 22:13:27 +0000955 for (uint64_t I : Addr)
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000956 Header.concat(I);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000957 Metadata *Elts[] = {Header.get(VMContext)};
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000958 return DIExpression(MDNode::get(VMContext, Elts));
Devang Patel746660f2010-12-07 23:25:47 +0000959}
960
Duncan P. N. Exon Smithbd75ad42015-02-09 22:13:27 +0000961DIExpression DIBuilder::createExpression(ArrayRef<int64_t> Signed) {
962 // TODO: Remove the callers of this signed version and delete.
963 SmallVector<uint64_t, 8> Addr(Signed.begin(), Signed.end());
964 return createExpression(Addr);
965}
966
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000967DIExpression DIBuilder::createBitPieceExpression(unsigned OffsetInBits,
968 unsigned SizeInBits) {
969 int64_t Addr[] = {dwarf::DW_OP_bit_piece, OffsetInBits, SizeInBits};
Duncan P. N. Exon Smith9affbba2014-10-01 21:32:12 +0000970 return createExpression(Addr);
971}
972
Eric Christopher98f9c232013-10-15 23:31:31 +0000973DISubprogram DIBuilder::createFunction(DIScopeRef Context, StringRef Name,
974 StringRef LinkageName, DIFile File,
975 unsigned LineNo, DICompositeType Ty,
Manman Renc50fa112013-10-10 18:40:01 +0000976 bool isLocalToUnit, bool isDefinition,
Eric Christopher98f9c232013-10-15 23:31:31 +0000977 unsigned ScopeLine, unsigned Flags,
978 bool isOptimized, Function *Fn,
979 MDNode *TParams, MDNode *Decl) {
Manman Renc50fa112013-10-10 18:40:01 +0000980 // dragonegg does not generate identifier for types, so using an empty map
981 // to resolve the context should be fine.
982 DITypeIdentifierMap EmptyMap;
983 return createFunction(Context.resolve(EmptyMap), Name, LinkageName, File,
984 LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
985 Flags, isOptimized, Fn, TParams, Decl);
986}
987
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000988static DISubprogram createFunctionHelper(
989 LLVMContext &VMContext, DIDescriptor Context, StringRef Name,
990 StringRef LinkageName, DIFile File, unsigned LineNo, DICompositeType Ty,
991 bool isLocalToUnit, bool isDefinition, unsigned ScopeLine, unsigned Flags,
992 bool isOptimized, Function *Fn, MDNode *TParams, MDNode *Decl, MDNode *Vars,
993 std::function<MDNode *(ArrayRef<Metadata *>)> CreateFunc) {
David Blaikie5174c842013-05-22 23:22:18 +0000994 assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
995 "function types should be subroutines");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000996 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_subprogram)
997 .concat(Name)
998 .concat(Name)
999 .concat(LinkageName)
1000 .concat(LineNo)
1001 .concat(isLocalToUnit)
1002 .concat(isDefinition)
1003 .concat(0)
1004 .concat(0)
1005 .concat(Flags)
1006 .concat(isOptimized)
1007 .concat(ScopeLine)
1008 .get(VMContext),
1009 File.getFileNode(),
1010 DIScope(getNonCompileUnitScope(Context)).getRef(), Ty,
1011 nullptr, getConstantOrNull(Fn), TParams, Decl, Vars};
Devang Patelb68c6232010-12-08 20:42:44 +00001012
Frederic Riss5e6bc9e2014-09-17 09:28:34 +00001013 DISubprogram S(CreateFunc(Elts));
Eric Christopher961959f2014-02-28 21:27:59 +00001014 assert(S.isSubprogram() &&
1015 "createFunction should return a valid DISubprogram");
David Blaikiecc8d0902013-03-21 20:28:52 +00001016 return S;
Devang Patelb68c6232010-12-08 20:42:44 +00001017}
1018
Frederic Riss5e6bc9e2014-09-17 09:28:34 +00001019
Frederic Riss5e6bc9e2014-09-17 09:28:34 +00001020DISubprogram DIBuilder::createFunction(DIDescriptor Context, StringRef Name,
1021 StringRef LinkageName, DIFile File,
1022 unsigned LineNo, DICompositeType Ty,
1023 bool isLocalToUnit, bool isDefinition,
1024 unsigned ScopeLine, unsigned Flags,
1025 bool isOptimized, Function *Fn,
1026 MDNode *TParams, MDNode *Decl) {
1027 return createFunctionHelper(VMContext, Context, Name, LinkageName, File,
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001028 LineNo, Ty, isLocalToUnit, isDefinition,
1029 ScopeLine, Flags, isOptimized, Fn, TParams, Decl,
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00001030 MDNode::getTemporary(VMContext, None).release(),
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001031 [&](ArrayRef<Metadata *> Elts) -> MDNode *{
1032 MDNode *Node = MDNode::get(VMContext, Elts);
1033 // Create a named metadata so that we
1034 // do not lose this mdnode.
1035 if (isDefinition)
1036 AllSubprograms.push_back(Node);
Adrian Prantlea7f1c22015-02-17 19:17:39 +00001037 trackIfUnresolved(Node);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001038 return Node;
1039 });
Frederic Riss5e6bc9e2014-09-17 09:28:34 +00001040}
1041
Frederic Riss5e6bc9e2014-09-17 09:28:34 +00001042DISubprogram
1043DIBuilder::createTempFunctionFwdDecl(DIDescriptor Context, StringRef Name,
1044 StringRef LinkageName, DIFile File,
1045 unsigned LineNo, DICompositeType Ty,
1046 bool isLocalToUnit, bool isDefinition,
1047 unsigned ScopeLine, unsigned Flags,
1048 bool isOptimized, Function *Fn,
1049 MDNode *TParams, MDNode *Decl) {
1050 return createFunctionHelper(VMContext, Context, Name, LinkageName, File,
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001051 LineNo, Ty, isLocalToUnit, isDefinition,
1052 ScopeLine, Flags, isOptimized, Fn, TParams, Decl,
1053 nullptr, [&](ArrayRef<Metadata *> Elts) {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00001054 return MDNode::getTemporary(VMContext, Elts).release();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001055 });
Frederic Riss5e6bc9e2014-09-17 09:28:34 +00001056}
1057
Eric Christopher98f9c232013-10-15 23:31:31 +00001058DISubprogram DIBuilder::createMethod(DIDescriptor Context, StringRef Name,
1059 StringRef LinkageName, DIFile F,
David Blaikie5174c842013-05-22 23:22:18 +00001060 unsigned LineNo, DICompositeType Ty,
Eric Christopher98f9c232013-10-15 23:31:31 +00001061 bool isLocalToUnit, bool isDefinition,
Devang Patelb68c6232010-12-08 20:42:44 +00001062 unsigned VK, unsigned VIndex,
Eric Christopher98f9c232013-10-15 23:31:31 +00001063 DIType VTableHolder, unsigned Flags,
1064 bool isOptimized, Function *Fn,
Devang Patel9f738842011-04-05 22:52:06 +00001065 MDNode *TParam) {
David Blaikie5174c842013-05-22 23:22:18 +00001066 assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
1067 "function types should be subroutines");
Eric Christopher5cb56322013-10-15 23:31:36 +00001068 assert(getNonCompileUnitScope(Context) &&
1069 "Methods should have both a Context and a context that isn't "
1070 "the compile unit.");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001071 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_subprogram)
1072 .concat(Name)
1073 .concat(Name)
1074 .concat(LinkageName)
1075 .concat(LineNo)
1076 .concat(isLocalToUnit)
1077 .concat(isDefinition)
1078 .concat(VK)
1079 .concat(VIndex)
1080 .concat(Flags)
1081 .concat(isOptimized)
1082 .concat(LineNo)
1083 // FIXME: Do we want to use different scope/lines?
1084 .get(VMContext),
1085 F.getFileNode(), DIScope(Context).getRef(), Ty,
1086 VTableHolder.getRef(), getConstantOrNull(Fn), TParam,
1087 nullptr, nullptr};
Devang Patel0c773242011-04-18 23:51:03 +00001088 MDNode *Node = MDNode::get(VMContext, Elts);
David Blaikie595eb442013-02-18 07:10:22 +00001089 if (isDefinition)
1090 AllSubprograms.push_back(Node);
David Blaikiecc8d0902013-03-21 20:28:52 +00001091 DISubprogram S(Node);
Manman Ren74c188f2013-07-01 21:02:01 +00001092 assert(S.isSubprogram() && "createMethod should return a valid DISubprogram");
Adrian Prantlea7f1c22015-02-17 19:17:39 +00001093 trackIfUnresolved(S);
David Blaikiecc8d0902013-03-21 20:28:52 +00001094 return S;
Devang Patelb68c6232010-12-08 20:42:44 +00001095}
1096
Devang Patel9b412732011-02-22 18:56:12 +00001097DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
Devang Patel746660f2010-12-07 23:25:47 +00001098 DIFile File, unsigned LineNo) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001099 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_namespace)
1100 .concat(Name)
1101 .concat(LineNo)
1102 .get(VMContext),
1103 File.getFileNode(), getNonCompileUnitScope(Scope)};
David Blaikie085abe32013-03-11 23:21:19 +00001104 DINameSpace R(MDNode::get(VMContext, Elts));
1105 assert(R.Verify() &&
1106 "createNameSpace should return a verifiable DINameSpace");
1107 return R;
Devang Patel746660f2010-12-07 23:25:47 +00001108}
1109
Eric Christopher6647b832011-10-11 22:59:11 +00001110DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
David Blaikie2f3f76f2014-08-21 22:45:21 +00001111 DIFile File,
1112 unsigned Discriminator) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001113 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_lexical_block)
1114 .concat(Discriminator)
1115 .get(VMContext),
1116 File.getFileNode(), Scope};
David Blaikie085abe32013-03-11 23:21:19 +00001117 DILexicalBlockFile R(MDNode::get(VMContext, Elts));
1118 assert(
1119 R.Verify() &&
1120 "createLexicalBlockFile should return a verifiable DILexicalBlockFile");
1121 return R;
Eric Christopher6647b832011-10-11 22:59:11 +00001122}
1123
Devang Patel9b412732011-02-22 18:56:12 +00001124DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
David Blaikie2f3f76f2014-08-21 22:45:21 +00001125 unsigned Line, unsigned Col) {
David Blaikie6c217162014-05-15 20:09:55 +00001126 // FIXME: This isn't thread safe nor the right way to defeat MDNode uniquing.
1127 // I believe the right way is to have a self-referential element in the node.
1128 // Also: why do we bother with line/column - they're not used and the
1129 // documentation (SourceLevelDebugging.rst) claims the line/col are necessary
1130 // for uniquing, yet then we have this other solution (because line/col were
1131 // inadequate) anyway. Remove all 3 and replace them with a self-reference.
1132
Adrian Prantl21e8d4a2013-06-24 21:19:43 +00001133 // Defeat MDNode uniquing for lexical blocks by using unique id.
Devang Patel89ea4f22010-12-08 01:50:15 +00001134 static unsigned int unique_id = 0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001135 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_lexical_block)
1136 .concat(Line)
1137 .concat(Col)
1138 .concat(unique_id++)
1139 .get(VMContext),
1140 File.getFileNode(), getNonCompileUnitScope(Scope)};
David Blaikie085abe32013-03-11 23:21:19 +00001141 DILexicalBlock R(MDNode::get(VMContext, Elts));
1142 assert(R.Verify() &&
1143 "createLexicalBlock should return a verifiable DILexicalBlock");
1144 return R;
Devang Patel89ea4f22010-12-08 01:50:15 +00001145}
1146
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001147static Value *getDbgIntrinsicValueImpl(LLVMContext &VMContext, Value *V) {
1148 assert(V && "no value passed to dbg intrinsic");
1149 return MetadataAsValue::get(VMContext, ValueAsMetadata::get(V));
1150}
1151
Devang Patel9b412732011-02-22 18:56:12 +00001152Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001153 DIExpression Expr,
Devang Patel746660f2010-12-07 23:25:47 +00001154 Instruction *InsertBefore) {
Manman Ren9822a112013-06-29 05:01:19 +00001155 assert(VarInfo.isVariable() &&
1156 "empty or invalid DIVariable passed to dbg.declare");
Devang Patel746660f2010-12-07 23:25:47 +00001157 if (!DeclareFn)
1158 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1159
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001160 trackIfUnresolved(VarInfo);
1161 trackIfUnresolved(Expr);
1162 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, Storage),
1163 MetadataAsValue::get(VMContext, VarInfo),
1164 MetadataAsValue::get(VMContext, Expr)};
Jay Foad5bd375a2011-07-15 08:37:34 +00001165 return CallInst::Create(DeclareFn, Args, "", InsertBefore);
Devang Patel746660f2010-12-07 23:25:47 +00001166}
1167
Devang Patel9b412732011-02-22 18:56:12 +00001168Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001169 DIExpression Expr,
Devang Patel746660f2010-12-07 23:25:47 +00001170 BasicBlock *InsertAtEnd) {
Manman Ren9822a112013-06-29 05:01:19 +00001171 assert(VarInfo.isVariable() &&
1172 "empty or invalid DIVariable passed to dbg.declare");
Devang Patel746660f2010-12-07 23:25:47 +00001173 if (!DeclareFn)
1174 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1175
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001176 trackIfUnresolved(VarInfo);
1177 trackIfUnresolved(Expr);
1178 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, Storage),
1179 MetadataAsValue::get(VMContext, VarInfo),
1180 MetadataAsValue::get(VMContext, Expr)};
Devang Patel746660f2010-12-07 23:25:47 +00001181
1182 // If this block already has a terminator then insert this intrinsic
1183 // before the terminator.
1184 if (TerminatorInst *T = InsertAtEnd->getTerminator())
Jay Foad5bd375a2011-07-15 08:37:34 +00001185 return CallInst::Create(DeclareFn, Args, "", T);
Devang Patel746660f2010-12-07 23:25:47 +00001186 else
Jay Foad5bd375a2011-07-15 08:37:34 +00001187 return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
Devang Patel746660f2010-12-07 23:25:47 +00001188}
1189
Devang Patel9b412732011-02-22 18:56:12 +00001190Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
Devang Patel746660f2010-12-07 23:25:47 +00001191 DIVariable VarInfo,
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001192 DIExpression Expr,
Devang Patel746660f2010-12-07 23:25:47 +00001193 Instruction *InsertBefore) {
1194 assert(V && "no value passed to dbg.value");
Manman Ren9822a112013-06-29 05:01:19 +00001195 assert(VarInfo.isVariable() &&
1196 "empty or invalid DIVariable passed to dbg.value");
Devang Patel746660f2010-12-07 23:25:47 +00001197 if (!ValueFn)
1198 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1199
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001200 trackIfUnresolved(VarInfo);
1201 trackIfUnresolved(Expr);
1202 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, V),
1203 ConstantInt::get(Type::getInt64Ty(VMContext), Offset),
1204 MetadataAsValue::get(VMContext, VarInfo),
1205 MetadataAsValue::get(VMContext, Expr)};
Jay Foad5bd375a2011-07-15 08:37:34 +00001206 return CallInst::Create(ValueFn, Args, "", InsertBefore);
Devang Patel746660f2010-12-07 23:25:47 +00001207}
1208
Devang Patel9b412732011-02-22 18:56:12 +00001209Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
Devang Patel746660f2010-12-07 23:25:47 +00001210 DIVariable VarInfo,
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001211 DIExpression Expr,
Devang Patel746660f2010-12-07 23:25:47 +00001212 BasicBlock *InsertAtEnd) {
1213 assert(V && "no value passed to dbg.value");
Manman Ren9822a112013-06-29 05:01:19 +00001214 assert(VarInfo.isVariable() &&
1215 "empty or invalid DIVariable passed to dbg.value");
Devang Patel746660f2010-12-07 23:25:47 +00001216 if (!ValueFn)
1217 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1218
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001219 trackIfUnresolved(VarInfo);
1220 trackIfUnresolved(Expr);
1221 Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, V),
1222 ConstantInt::get(Type::getInt64Ty(VMContext), Offset),
1223 MetadataAsValue::get(VMContext, VarInfo),
1224 MetadataAsValue::get(VMContext, Expr)};
Jay Foad5bd375a2011-07-15 08:37:34 +00001225 return CallInst::Create(ValueFn, Args, "", InsertAtEnd);
Devang Patel746660f2010-12-07 23:25:47 +00001226}
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +00001227
1228void DIBuilder::replaceVTableHolder(DICompositeType &T, DICompositeType VTableHolder) {
1229 T.setContainingType(VTableHolder);
1230
1231 // If this didn't create a self-reference, just return.
1232 if (T != VTableHolder)
1233 return;
1234
Adrian Prantl18a25b02015-02-11 17:45:10 +00001235 // Look for unresolved operands. T will drop RAUW support, orphaning any
1236 // cycles underneath it.
1237 if (T->isResolved())
1238 for (const MDOperand &O : T->operands())
1239 if (auto *N = dyn_cast_or_null<MDNode>(O))
1240 trackIfUnresolved(N);
Duncan P. N. Exon Smith97f07c22014-12-18 00:46:16 +00001241}
1242
1243void DIBuilder::replaceArrays(DICompositeType &T, DIArray Elements,
1244 DIArray TParams) {
1245 T.setArrays(Elements, TParams);
1246
1247 // If T isn't resolved, there's no problem.
1248 if (!T->isResolved())
1249 return;
1250
1251 // If "T" is resolved, it may be due to a self-reference cycle. Track the
1252 // arrays explicitly if they're unresolved, or else the cycles will be
1253 // orphaned.
1254 if (Elements)
1255 trackIfUnresolved(Elements);
1256 if (TParams)
1257 trackIfUnresolved(TParams);
1258}