blob: 51e296383e021805e519f124728e475fad7f759e [file] [log] [blame]
Devang Patel35fcd652010-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
Bill Wendling16eeb6f2012-06-29 08:32:07 +000014#include "llvm/DIBuilder.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000015#include "llvm/ADT/STLExtras.h"
Bill Wendling0bcbd1d2012-06-28 00:05:13 +000016#include "llvm/DebugInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000017#include "llvm/IR/Constants.h"
18#include "llvm/IR/IntrinsicInst.h"
19#include "llvm/IR/Module.h"
Eric Christopher6126a1e2012-04-03 00:43:49 +000020#include "llvm/Support/Debug.h"
Devang Patel35fcd652010-11-04 15:01:38 +000021#include "llvm/Support/Dwarf.h"
22
23using namespace llvm;
24using namespace llvm::dwarf;
25
26static Constant *GetTagConstant(LLVMContext &VMContext, unsigned Tag) {
27 assert((Tag & LLVMDebugVersionMask) == 0 &&
28 "Tag too large for debug encoding!");
29 return ConstantInt::get(Type::getInt32Ty(VMContext), Tag | LLVMDebugVersion);
30}
Devang Patel48f17ba2010-12-07 23:58:00 +000031
Devang Patel35fcd652010-11-04 15:01:38 +000032DIBuilder::DIBuilder(Module &m)
Eric Christopher1fe3f9a2013-07-19 00:51:47 +000033 : M(m), VMContext(M.getContext()), TempEnumTypes(0),
Eric Christopher6c0046f2011-08-26 21:02:40 +000034 TempRetainTypes(0), TempSubprograms(0), TempGVs(0), DeclareFn(0),
35 ValueFn(0)
Devang Patel94c7ddb2011-08-16 22:09:43 +000036{}
Devang Patel35fcd652010-11-04 15:01:38 +000037
Devang Patel6326a422011-08-15 23:00:00 +000038/// finalize - Construct any deferred debug info descriptors.
39void DIBuilder::finalize() {
Devang Patel94c7ddb2011-08-16 22:09:43 +000040 DIArray Enums = getOrCreateArray(AllEnumTypes);
41 DIType(TempEnumTypes).replaceAllUsesWith(Enums);
42
Manman Ren3a0e9b52013-08-29 23:17:54 +000043 SmallVector<Value *, 16> RetainValues;
44 // Declarations and definitions of the same type may be retained. Some
45 // clients RAUW these pairs, leaving duplicates in the retained types
46 // list. Use a set to remove the duplicates while we transform the
47 // TrackingVHs back into Values.
48 SmallPtrSet<Value *, 16> RetainSet;
49 for (unsigned I = 0, E = AllRetainTypes.size(); I < E; I++)
50 if (RetainSet.insert(AllRetainTypes[I]))
51 RetainValues.push_back(AllRetainTypes[I]);
52 DIArray RetainTypes = getOrCreateArray(RetainValues);
Devang Patel94c7ddb2011-08-16 22:09:43 +000053 DIType(TempRetainTypes).replaceAllUsesWith(RetainTypes);
54
55 DIArray SPs = getOrCreateArray(AllSubprograms);
56 DIType(TempSubprograms).replaceAllUsesWith(SPs);
Devang Patel93d39be2011-08-19 23:28:12 +000057 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
58 DISubprogram SP(SPs.getElement(i));
Eric Christopher216432d2012-04-23 19:00:11 +000059 SmallVector<Value *, 4> Variables;
Devang Patel93d39be2011-08-19 23:28:12 +000060 if (NamedMDNode *NMD = getFnSpecificMDNode(M, SP)) {
Devang Patel93d39be2011-08-19 23:28:12 +000061 for (unsigned ii = 0, ee = NMD->getNumOperands(); ii != ee; ++ii)
62 Variables.push_back(NMD->getOperand(ii));
Devang Patel93d39be2011-08-19 23:28:12 +000063 NMD->eraseFromParent();
64 }
Eric Christopher216432d2012-04-23 19:00:11 +000065 if (MDNode *Temp = SP.getVariablesNodes()) {
66 DIArray AV = getOrCreateArray(Variables);
67 DIType(Temp).replaceAllUsesWith(AV);
68 }
Devang Patel93d39be2011-08-19 23:28:12 +000069 }
Devang Patel94c7ddb2011-08-16 22:09:43 +000070
71 DIArray GVs = getOrCreateArray(AllGVs);
72 DIType(TempGVs).replaceAllUsesWith(GVs);
David Blaikiec462db62013-04-22 06:12:31 +000073
74 DIArray IMs = getOrCreateArray(AllImportedModules);
75 DIType(TempImportedModules).replaceAllUsesWith(IMs);
Devang Patel94c7ddb2011-08-16 22:09:43 +000076}
77
Eric Christopher6c0046f2011-08-26 21:02:40 +000078/// getNonCompileUnitScope - If N is compile unit return NULL otherwise return
79/// N.
Devang Patel94c7ddb2011-08-16 22:09:43 +000080static MDNode *getNonCompileUnitScope(MDNode *N) {
81 if (DIDescriptor(N).isCompileUnit())
82 return NULL;
83 return N;
Devang Patel6326a422011-08-15 23:00:00 +000084}
85
David Blaikie00c5c5d2013-03-20 23:58:12 +000086static MDNode *createFilePathPair(LLVMContext &VMContext, StringRef Filename,
87 StringRef Directory) {
88 assert(!Filename.empty() && "Unable to create file without name");
89 Value *Pair[] = {
90 MDString::get(VMContext, Filename),
91 MDString::get(VMContext, Directory),
92 };
93 return MDNode::get(VMContext, Pair);
94}
95
Devang Patel50d280c2011-02-22 18:56:12 +000096/// createCompileUnit - A CompileUnit provides an anchor for all debugging
Devang Patel35fcd652010-11-04 15:01:38 +000097/// information generated during this instance of compilation.
Eric Christopher1fe3f9a2013-07-19 00:51:47 +000098DICompileUnit DIBuilder::createCompileUnit(unsigned Lang, StringRef Filename,
99 StringRef Directory,
100 StringRef Producer, bool isOptimized,
101 StringRef Flags, unsigned RunTimeVer,
102 StringRef SplitName) {
Chandler Carruthb0dc4d92012-01-10 18:18:52 +0000103 assert(((Lang <= dwarf::DW_LANG_Python && Lang >= dwarf::DW_LANG_C89) ||
104 (Lang <= dwarf::DW_LANG_hi_user && Lang >= dwarf::DW_LANG_lo_user)) &&
105 "Invalid Language tag");
106 assert(!Filename.empty() &&
107 "Unable to create compile unit without filename");
Devang Patel94c7ddb2011-08-16 22:09:43 +0000108 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
109 TempEnumTypes = MDNode::getTemporary(VMContext, TElts);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000110
111 TempRetainTypes = MDNode::getTemporary(VMContext, TElts);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000112
113 TempSubprograms = MDNode::getTemporary(VMContext, TElts);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000114
115 TempGVs = MDNode::getTemporary(VMContext, TElts);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000116
David Blaikiec462db62013-04-22 06:12:31 +0000117 TempImportedModules = MDNode::getTemporary(VMContext, TElts);
118
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000119 Value *Elts[] = {
120 GetTagConstant(VMContext, dwarf::DW_TAG_compile_unit),
David Blaikie00c5c5d2013-03-20 23:58:12 +0000121 createFilePathPair(VMContext, Filename, Directory),
David Blaikie162c8002013-03-20 22:52:54 +0000122 ConstantInt::get(Type::getInt32Ty(VMContext), Lang),
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000123 MDString::get(VMContext, Producer),
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000124 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
125 MDString::get(VMContext, Flags),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000126 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer),
David Blaikiea8eefc72013-02-02 05:56:24 +0000127 TempEnumTypes,
128 TempRetainTypes,
129 TempSubprograms,
Eric Christophere4b67902013-02-22 23:50:04 +0000130 TempGVs,
David Blaikiec462db62013-04-22 06:12:31 +0000131 TempImportedModules,
Eric Christophere4b67902013-02-22 23:50:04 +0000132 MDString::get(VMContext, SplitName)
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000133 };
Eric Christopher1fe3f9a2013-07-19 00:51:47 +0000134
135 MDNode *CUNode = MDNode::get(VMContext, Elts);
Devang Patel464f4ef2011-05-03 16:18:28 +0000136
137 // Create a named metadata so that it is easier to find cu in a module.
138 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
Eric Christopher1fe3f9a2013-07-19 00:51:47 +0000139 NMD->addOperand(CUNode);
140
141 return DICompileUnit(CUNode);
Devang Patel35fcd652010-11-04 15:01:38 +0000142}
143
David Blaikie7b72cc72013-05-20 22:50:35 +0000144static DIImportedEntity
145createImportedModule(LLVMContext &C, DIScope Context, DIDescriptor NS,
146 unsigned Line, StringRef Name,
147 SmallVectorImpl<Value *> &AllImportedModules) {
148 const MDNode *R;
149 if (Name.empty()) {
150 Value *Elts[] = {
151 GetTagConstant(C, dwarf::DW_TAG_imported_module),
152 Context,
153 NS,
154 ConstantInt::get(Type::getInt32Ty(C), Line),
155 };
156 R = MDNode::get(C, Elts);
157 } else {
158 Value *Elts[] = {
159 GetTagConstant(C, dwarf::DW_TAG_imported_module),
160 Context,
161 NS,
162 ConstantInt::get(Type::getInt32Ty(C), Line),
163 MDString::get(C, Name)
164 };
165 R = MDNode::get(C, Elts);
166 }
167 DIImportedEntity M(R);
David Blaikie20d9e412013-05-07 21:35:53 +0000168 assert(M.Verify() && "Imported module should be valid");
169 AllImportedModules.push_back(M);
170 return M;
171}
172
David Blaikie7b72cc72013-05-20 22:50:35 +0000173DIImportedEntity DIBuilder::createImportedModule(DIScope Context,
174 DINameSpace NS, unsigned Line,
175 StringRef Name) {
176 return ::createImportedModule(VMContext, Context, NS, Line, Name,
177 AllImportedModules);
178}
179
180DIImportedEntity DIBuilder::createImportedModule(DIScope Context,
181 DIImportedEntity NS,
182 unsigned Line,
183 StringRef Name) {
184 return ::createImportedModule(VMContext, Context, NS, Line, Name,
185 AllImportedModules);
186}
187
David Blaikie20d9e412013-05-07 21:35:53 +0000188DIImportedEntity DIBuilder::createImportedDeclaration(DIScope Context,
189 DIDescriptor Decl,
190 unsigned Line) {
191 Value *Elts[] = {
192 GetTagConstant(VMContext, dwarf::DW_TAG_imported_declaration),
193 Context,
194 Decl,
195 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
196 };
197 DIImportedEntity M(MDNode::get(VMContext, Elts));
David Blaikiec462db62013-04-22 06:12:31 +0000198 assert(M.Verify() && "Imported module should be valid");
199 AllImportedModules.push_back(M);
200 return M;
201}
202
Devang Patel50d280c2011-02-22 18:56:12 +0000203/// createFile - Create a file descriptor to hold debugging information
Devang Patel35fcd652010-11-04 15:01:38 +0000204/// for a file.
Devang Patel50d280c2011-02-22 18:56:12 +0000205DIFile DIBuilder::createFile(StringRef Filename, StringRef Directory) {
David Blaikieb4cf0ab2013-03-17 21:13:55 +0000206 Value *Elts[] = {
207 GetTagConstant(VMContext, dwarf::DW_TAG_file_type),
David Blaikie72dfb052013-03-28 02:44:59 +0000208 createFilePathPair(VMContext, Filename, Directory)
David Blaikieb4cf0ab2013-03-17 21:13:55 +0000209 };
David Blaikie72dfb052013-03-28 02:44:59 +0000210 return DIFile(MDNode::get(VMContext, Elts));
Devang Patel35fcd652010-11-04 15:01:38 +0000211}
212
Devang Patel50d280c2011-02-22 18:56:12 +0000213/// createEnumerator - Create a single enumerator value.
David Blaikie8de0a462013-06-24 17:34:33 +0000214DIEnumerator DIBuilder::createEnumerator(StringRef Name, int64_t Val) {
Devang Patel811ae5b2011-09-12 18:26:08 +0000215 assert(!Name.empty() && "Unable to create enumerator without name");
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000216 Value *Elts[] = {
217 GetTagConstant(VMContext, dwarf::DW_TAG_enumerator),
218 MDString::get(VMContext, Name),
219 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
220 };
David Blaikie72dfb052013-03-28 02:44:59 +0000221 return DIEnumerator(MDNode::get(VMContext, Elts));
Devang Patel35fcd652010-11-04 15:01:38 +0000222}
223
Peter Collingbourne03ccdb52013-06-27 22:50:59 +0000224/// \brief Create a DWARF unspecified type.
225DIBasicType DIBuilder::createUnspecifiedType(StringRef Name) {
Devang Patel734a67c2011-09-14 23:13:28 +0000226 assert(!Name.empty() && "Unable to create type without name");
Peter Collingbourne03ccdb52013-06-27 22:50:59 +0000227 // Unspecified types are encoded in DIBasicType format. Line number, filename,
228 // size, alignment, offset and flags are always empty here.
Devang Patel734a67c2011-09-14 23:13:28 +0000229 Value *Elts[] = {
230 GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_type),
David Blaikiea13f3cd2013-03-19 23:25:22 +0000231 NULL, // Filename
Eric Christopher1fe3f9a2013-07-19 00:51:47 +0000232 NULL, // Unused
Devang Patel734a67c2011-09-14 23:13:28 +0000233 MDString::get(VMContext, Name),
Devang Patel734a67c2011-09-14 23:13:28 +0000234 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
235 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
236 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
237 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
238 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
Bill Wendling9fdb7c02012-07-06 17:49:19 +0000239 ConstantInt::get(Type::getInt32Ty(VMContext), 0) // Encoding
Devang Patel734a67c2011-09-14 23:13:28 +0000240 };
Manman Ren576d49a2013-06-07 18:35:53 +0000241 return DIBasicType(MDNode::get(VMContext, Elts));
Devang Patel734a67c2011-09-14 23:13:28 +0000242}
243
Peter Collingbourne03ccdb52013-06-27 22:50:59 +0000244/// \brief Create C++11 nullptr type.
245DIBasicType DIBuilder::createNullPtrType() {
246 return createUnspecifiedType("decltype(nullptr)");
247}
248
Eric Christopher6c0046f2011-08-26 21:02:40 +0000249/// createBasicType - Create debugging information entry for a basic
Devang Patel35fcd652010-11-04 15:01:38 +0000250/// type, e.g 'char'.
David Blaikie2ce067a2013-02-12 00:40:41 +0000251DIBasicType
252DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
253 uint64_t AlignInBits, unsigned Encoding) {
Devang Patel811ae5b2011-09-12 18:26:08 +0000254 assert(!Name.empty() && "Unable to create type without name");
Devang Patel35fcd652010-11-04 15:01:38 +0000255 // Basic types are encoded in DIBasicType format. Line number, filename,
256 // offset and flags are always empty here.
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000257 Value *Elts[] = {
258 GetTagConstant(VMContext, dwarf::DW_TAG_base_type),
David Blaikiea13f3cd2013-03-19 23:25:22 +0000259 NULL, // File/directory name
Eric Christopher1fe3f9a2013-07-19 00:51:47 +0000260 NULL, // Unused
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000261 MDString::get(VMContext, Name),
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000262 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
263 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
264 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
265 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
266 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
267 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
268 };
David Blaikie72dfb052013-03-28 02:44:59 +0000269 return DIBasicType(MDNode::get(VMContext, Elts));
Devang Patel35fcd652010-11-04 15:01:38 +0000270}
271
Nick Lewyckyffab7d02011-11-09 22:45:04 +0000272/// createQualifiedType - Create debugging information entry for a qualified
Devang Patel35fcd652010-11-04 15:01:38 +0000273/// type, e.g. 'const int'.
David Blaikied67c5ca2013-02-18 06:41:57 +0000274DIDerivedType DIBuilder::createQualifiedType(unsigned Tag, DIType FromTy) {
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000275 // Qualified types are encoded in DIDerivedType format.
276 Value *Elts[] = {
277 GetTagConstant(VMContext, Tag),
David Blaikiea13f3cd2013-03-19 23:25:22 +0000278 NULL, // Filename
Eric Christopher1fe3f9a2013-07-19 00:51:47 +0000279 NULL, // Unused
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000280 MDString::get(VMContext, StringRef()), // Empty name.
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000281 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
282 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
283 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
284 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
285 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
286 FromTy
287 };
David Blaikie72dfb052013-03-28 02:44:59 +0000288 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel35fcd652010-11-04 15:01:38 +0000289}
290
Devang Patel50d280c2011-02-22 18:56:12 +0000291/// createPointerType - Create debugging information entry for a pointer.
David Blaikied67c5ca2013-02-18 06:41:57 +0000292DIDerivedType
293DIBuilder::createPointerType(DIType PointeeTy, uint64_t SizeInBits,
294 uint64_t AlignInBits, StringRef Name) {
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000295 // Pointer types are encoded in DIDerivedType format.
296 Value *Elts[] = {
297 GetTagConstant(VMContext, dwarf::DW_TAG_pointer_type),
David Blaikiea13f3cd2013-03-19 23:25:22 +0000298 NULL, // Filename
Eric Christopher1fe3f9a2013-07-19 00:51:47 +0000299 NULL, // Unused
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000300 MDString::get(VMContext, Name),
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000301 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
302 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
303 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
304 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
305 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
306 PointeeTy
307 };
David Blaikie72dfb052013-03-28 02:44:59 +0000308 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel35fcd652010-11-04 15:01:38 +0000309}
310
Eric Christopher09b79812013-04-19 20:37:12 +0000311DIDerivedType DIBuilder::createMemberPointerType(DIType PointeeTy,
312 DIType Base) {
David Blaikie62fdfb52013-01-07 05:51:15 +0000313 // Pointer types are encoded in DIDerivedType format.
314 Value *Elts[] = {
315 GetTagConstant(VMContext, dwarf::DW_TAG_ptr_to_member_type),
David Blaikiea13f3cd2013-03-19 23:25:22 +0000316 NULL, // Filename
Eric Christopher1fe3f9a2013-07-19 00:51:47 +0000317 NULL, // Unused
David Blaikie62fdfb52013-01-07 05:51:15 +0000318 NULL,
David Blaikie62fdfb52013-01-07 05:51:15 +0000319 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
320 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
321 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
322 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
323 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
324 PointeeTy,
325 Base
326 };
David Blaikie72dfb052013-03-28 02:44:59 +0000327 return DIDerivedType(MDNode::get(VMContext, Elts));
David Blaikie62fdfb52013-01-07 05:51:15 +0000328}
329
Eric Christopher791e6292012-05-19 01:36:37 +0000330/// createReferenceType - Create debugging information entry for a reference
331/// type.
David Blaikied67c5ca2013-02-18 06:41:57 +0000332DIDerivedType DIBuilder::createReferenceType(unsigned Tag, DIType RTy) {
Manman Ren89c83b72013-07-01 21:02:01 +0000333 assert(RTy.isType() && "Unable to create reference type");
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000334 // References are encoded in DIDerivedType format.
335 Value *Elts[] = {
Eric Christopher791e6292012-05-19 01:36:37 +0000336 GetTagConstant(VMContext, Tag),
David Blaikiea13f3cd2013-03-19 23:25:22 +0000337 NULL, // Filename
Eric Christopher6c0046f2011-08-26 21:02:40 +0000338 NULL, // TheCU,
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000339 NULL, // Name
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000340 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
341 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
342 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
343 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
344 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
345 RTy
346 };
David Blaikie72dfb052013-03-28 02:44:59 +0000347 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel35fcd652010-11-04 15:01:38 +0000348}
349
Devang Patel50d280c2011-02-22 18:56:12 +0000350/// createTypedef - Create debugging information entry for a typedef.
David Blaikied67c5ca2013-02-18 06:41:57 +0000351DIDerivedType DIBuilder::createTypedef(DIType Ty, StringRef Name, DIFile File,
352 unsigned LineNo, DIDescriptor Context) {
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000353 // typedefs are encoded in DIDerivedType format.
Manman Ren89c83b72013-07-01 21:02:01 +0000354 assert(Ty.isType() && "Invalid typedef type!");
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000355 Value *Elts[] = {
356 GetTagConstant(VMContext, dwarf::DW_TAG_typedef),
David Blaikie4776bce2013-03-20 00:26:26 +0000357 File.getFileNode(),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000358 getNonCompileUnitScope(Context),
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000359 MDString::get(VMContext, Name),
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000360 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
361 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
362 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
363 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
364 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
365 Ty
366 };
David Blaikie72dfb052013-03-28 02:44:59 +0000367 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel35fcd652010-11-04 15:01:38 +0000368}
369
Devang Patel50d280c2011-02-22 18:56:12 +0000370/// createFriend - Create debugging information entry for a 'friend'.
Manman Ren576d49a2013-06-07 18:35:53 +0000371DIDerivedType DIBuilder::createFriend(DIType Ty, DIType FriendTy) {
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000372 // typedefs are encoded in DIDerivedType format.
Manman Ren89c83b72013-07-01 21:02:01 +0000373 assert(Ty.isType() && "Invalid type!");
374 assert(FriendTy.isType() && "Invalid friend type!");
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000375 Value *Elts[] = {
376 GetTagConstant(VMContext, dwarf::DW_TAG_friend),
David Blaikiea13f3cd2013-03-19 23:25:22 +0000377 NULL,
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000378 Ty,
379 NULL, // Name
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000380 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
381 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
382 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
383 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
384 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
385 FriendTy
386 };
Manman Ren576d49a2013-06-07 18:35:53 +0000387 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel35fcd652010-11-04 15:01:38 +0000388}
389
Devang Patel50d280c2011-02-22 18:56:12 +0000390/// createInheritance - Create debugging information entry to establish
Eric Christopherc27c7342011-09-12 19:58:22 +0000391/// inheritance relationship between two types.
David Blaikied67c5ca2013-02-18 06:41:57 +0000392DIDerivedType DIBuilder::createInheritance(
393 DIType Ty, DIType BaseTy, uint64_t BaseOffset, unsigned Flags) {
Manman Ren89c83b72013-07-01 21:02:01 +0000394 assert(Ty.isType() && "Unable to create inheritance");
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000395 // TAG_inheritance is encoded in DIDerivedType format.
396 Value *Elts[] = {
397 GetTagConstant(VMContext, dwarf::DW_TAG_inheritance),
David Blaikiea13f3cd2013-03-19 23:25:22 +0000398 NULL,
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000399 Ty,
400 NULL, // Name
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000401 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
402 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
403 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
404 ConstantInt::get(Type::getInt64Ty(VMContext), BaseOffset),
405 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
406 BaseTy
407 };
David Blaikie72dfb052013-03-28 02:44:59 +0000408 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel35fcd652010-11-04 15:01:38 +0000409}
410
Devang Patel50d280c2011-02-22 18:56:12 +0000411/// createMemberType - Create debugging information entry for a member.
David Blaikied67c5ca2013-02-18 06:41:57 +0000412DIDerivedType DIBuilder::createMemberType(
413 DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
414 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
415 unsigned Flags, DIType Ty) {
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000416 // TAG_member is encoded in DIDerivedType format.
417 Value *Elts[] = {
418 GetTagConstant(VMContext, dwarf::DW_TAG_member),
David Blaikie4776bce2013-03-20 00:26:26 +0000419 File.getFileNode(),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000420 getNonCompileUnitScope(Scope),
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000421 MDString::get(VMContext, Name),
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000422 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
423 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
424 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
425 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
426 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
427 Ty
428 };
David Blaikie72dfb052013-03-28 02:44:59 +0000429 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel35fcd652010-11-04 15:01:38 +0000430}
431
Eric Christopher6b6061f2013-01-16 01:22:23 +0000432/// createStaticMemberType - Create debugging information entry for a
433/// C++ static data member.
Manman Ren576d49a2013-06-07 18:35:53 +0000434DIDerivedType
435DIBuilder::createStaticMemberType(DIDescriptor Scope, StringRef Name,
436 DIFile File, unsigned LineNumber,
437 DIType Ty, unsigned Flags,
438 llvm::Value *Val) {
Eric Christopher6b6061f2013-01-16 01:22:23 +0000439 // TAG_member is encoded in DIDerivedType format.
440 Flags |= DIDescriptor::FlagStaticMember;
441 Value *Elts[] = {
442 GetTagConstant(VMContext, dwarf::DW_TAG_member),
David Blaikie4776bce2013-03-20 00:26:26 +0000443 File.getFileNode(),
Eric Christopher6b6061f2013-01-16 01:22:23 +0000444 getNonCompileUnitScope(Scope),
445 MDString::get(VMContext, Name),
Eric Christopher6b6061f2013-01-16 01:22:23 +0000446 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
447 ConstantInt::get(Type::getInt64Ty(VMContext), 0/*SizeInBits*/),
448 ConstantInt::get(Type::getInt64Ty(VMContext), 0/*AlignInBits*/),
449 ConstantInt::get(Type::getInt64Ty(VMContext), 0/*OffsetInBits*/),
450 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
451 Ty,
452 Val
453 };
Manman Ren576d49a2013-06-07 18:35:53 +0000454 return DIDerivedType(MDNode::get(VMContext, Elts));
Eric Christopher6b6061f2013-01-16 01:22:23 +0000455}
456
Devang Patele9db5e22011-04-16 00:11:51 +0000457/// createObjCIVar - Create debugging information entry for Objective-C
458/// instance variable.
Manman Ren576d49a2013-06-07 18:35:53 +0000459DIDerivedType
460DIBuilder::createObjCIVar(StringRef Name,
461 DIFile File, unsigned LineNumber,
462 uint64_t SizeInBits, uint64_t AlignInBits,
463 uint64_t OffsetInBits, unsigned Flags,
464 DIType Ty, StringRef PropertyName,
465 StringRef GetterName, StringRef SetterName,
466 unsigned PropertyAttributes) {
Devang Patele9db5e22011-04-16 00:11:51 +0000467 // TAG_member is encoded in DIDerivedType format.
468 Value *Elts[] = {
469 GetTagConstant(VMContext, dwarf::DW_TAG_member),
David Blaikie4776bce2013-03-20 00:26:26 +0000470 File.getFileNode(),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000471 getNonCompileUnitScope(File),
Devang Patele9db5e22011-04-16 00:11:51 +0000472 MDString::get(VMContext, Name),
Devang Patele9db5e22011-04-16 00:11:51 +0000473 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
474 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
475 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
476 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
477 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
478 Ty,
479 MDString::get(VMContext, PropertyName),
480 MDString::get(VMContext, GetterName),
481 MDString::get(VMContext, SetterName),
482 ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes)
483 };
Manman Ren576d49a2013-06-07 18:35:53 +0000484 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patele9db5e22011-04-16 00:11:51 +0000485}
486
Devang Patel6588abf2012-02-06 17:49:43 +0000487/// createObjCIVar - Create debugging information entry for Objective-C
488/// instance variable.
Manman Ren576d49a2013-06-07 18:35:53 +0000489DIDerivedType
490DIBuilder::createObjCIVar(StringRef Name,
491 DIFile File, unsigned LineNumber,
492 uint64_t SizeInBits, uint64_t AlignInBits,
493 uint64_t OffsetInBits, unsigned Flags,
494 DIType Ty, MDNode *PropertyNode) {
Devang Patel6588abf2012-02-06 17:49:43 +0000495 // TAG_member is encoded in DIDerivedType format.
496 Value *Elts[] = {
497 GetTagConstant(VMContext, dwarf::DW_TAG_member),
David Blaikie4776bce2013-03-20 00:26:26 +0000498 File.getFileNode(),
Devang Patel6588abf2012-02-06 17:49:43 +0000499 getNonCompileUnitScope(File),
500 MDString::get(VMContext, Name),
Devang Patel6588abf2012-02-06 17:49:43 +0000501 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
502 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
503 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
504 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
505 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
506 Ty,
507 PropertyNode
508 };
Manman Ren576d49a2013-06-07 18:35:53 +0000509 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel6588abf2012-02-06 17:49:43 +0000510}
511
Devang Patel1ea02d42012-02-04 00:59:25 +0000512/// createObjCProperty - Create debugging information entry for Objective-C
513/// property.
Eric Christopherb8ca9882012-03-29 08:42:56 +0000514DIObjCProperty DIBuilder::createObjCProperty(StringRef Name,
Eric Christopher87c06a82012-07-06 02:35:57 +0000515 DIFile File, unsigned LineNumber,
Devang Patel1ea02d42012-02-04 00:59:25 +0000516 StringRef GetterName,
Adrian Prantl2f445be2013-04-19 19:56:02 +0000517 StringRef SetterName,
Eric Christopherb8ca9882012-03-29 08:42:56 +0000518 unsigned PropertyAttributes,
Eric Christopher87c06a82012-07-06 02:35:57 +0000519 DIType Ty) {
Devang Patel1ea02d42012-02-04 00:59:25 +0000520 Value *Elts[] = {
Eric Christopher6c31ee22012-03-29 21:35:05 +0000521 GetTagConstant(VMContext, dwarf::DW_TAG_APPLE_property),
Devang Patel1ea02d42012-02-04 00:59:25 +0000522 MDString::get(VMContext, Name),
Eric Christopherb8ca9882012-03-29 08:42:56 +0000523 File,
524 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
Devang Patel1ea02d42012-02-04 00:59:25 +0000525 MDString::get(VMContext, GetterName),
526 MDString::get(VMContext, SetterName),
Eric Christopherb8ca9882012-03-29 08:42:56 +0000527 ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes),
528 Ty
Devang Patel1ea02d42012-02-04 00:59:25 +0000529 };
David Blaikie72dfb052013-03-28 02:44:59 +0000530 return DIObjCProperty(MDNode::get(VMContext, Elts));
Devang Patel1ea02d42012-02-04 00:59:25 +0000531}
532
Devang Patel50d280c2011-02-22 18:56:12 +0000533/// createTemplateTypeParameter - Create debugging information for template
Devang Patel7e2cb112011-02-02 21:38:25 +0000534/// type parameter.
Eric Christopher6c0046f2011-08-26 21:02:40 +0000535DITemplateTypeParameter
Devang Patel50d280c2011-02-22 18:56:12 +0000536DIBuilder::createTemplateTypeParameter(DIDescriptor Context, StringRef Name,
Devang Patel7e2cb112011-02-02 21:38:25 +0000537 DIType Ty, MDNode *File, unsigned LineNo,
538 unsigned ColumnNo) {
539 Value *Elts[] = {
540 GetTagConstant(VMContext, dwarf::DW_TAG_template_type_parameter),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000541 getNonCompileUnitScope(Context),
Devang Patel7e2cb112011-02-02 21:38:25 +0000542 MDString::get(VMContext, Name),
543 Ty,
544 File,
545 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
546 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
547 };
David Blaikie72dfb052013-03-28 02:44:59 +0000548 return DITemplateTypeParameter(MDNode::get(VMContext, Elts));
Devang Patel7e2cb112011-02-02 21:38:25 +0000549}
550
Eric Christopher6c0046f2011-08-26 21:02:40 +0000551DITemplateValueParameter
David Blaikiee88939c2013-06-22 18:59:11 +0000552DIBuilder::createTemplateValueParameter(unsigned Tag, DIDescriptor Context,
553 StringRef Name, DIType Ty,
554 Value *Val, MDNode *File,
555 unsigned LineNo,
Devang Patele7d93872011-02-02 22:35:53 +0000556 unsigned ColumnNo) {
557 Value *Elts[] = {
David Blaikiee88939c2013-06-22 18:59:11 +0000558 GetTagConstant(VMContext, Tag),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000559 getNonCompileUnitScope(Context),
Devang Patele7d93872011-02-02 22:35:53 +0000560 MDString::get(VMContext, Name),
561 Ty,
David Blaikie4de9d722013-05-10 21:52:07 +0000562 Val,
Devang Patele7d93872011-02-02 22:35:53 +0000563 File,
564 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
565 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
566 };
David Blaikie72dfb052013-03-28 02:44:59 +0000567 return DITemplateValueParameter(MDNode::get(VMContext, Elts));
Devang Patele7d93872011-02-02 22:35:53 +0000568}
569
David Blaikiee88939c2013-06-22 18:59:11 +0000570/// createTemplateValueParameter - Create debugging information for template
571/// value parameter.
572DITemplateValueParameter
573DIBuilder::createTemplateValueParameter(DIDescriptor Context, StringRef Name,
574 DIType Ty, Value *Val,
575 MDNode *File, unsigned LineNo,
576 unsigned ColumnNo) {
577 return createTemplateValueParameter(dwarf::DW_TAG_template_value_parameter,
578 Context, Name, Ty, Val, File, LineNo,
579 ColumnNo);
580}
581
582DITemplateValueParameter
583DIBuilder::createTemplateTemplateParameter(DIDescriptor Context, StringRef Name,
584 DIType Ty, StringRef Val,
585 MDNode *File, unsigned LineNo,
586 unsigned ColumnNo) {
587 return createTemplateValueParameter(
588 dwarf::DW_TAG_GNU_template_template_param, Context, Name, Ty,
589 MDString::get(VMContext, Val), File, LineNo, ColumnNo);
590}
591
592DITemplateValueParameter
593DIBuilder::createTemplateParameterPack(DIDescriptor Context, StringRef Name,
594 DIType Ty, DIArray Val,
595 MDNode *File, unsigned LineNo,
596 unsigned ColumnNo) {
597 return createTemplateValueParameter(dwarf::DW_TAG_GNU_template_parameter_pack,
598 Context, Name, Ty, Val, File, LineNo,
599 ColumnNo);
600}
601
Eric Christopher87c06a82012-07-06 02:35:57 +0000602/// createClassType - Create debugging information entry for a class.
David Blaikieca442a42013-03-26 23:46:39 +0000603DICompositeType DIBuilder::createClassType(DIDescriptor Context, StringRef Name,
604 DIFile File, unsigned LineNumber,
605 uint64_t SizeInBits,
606 uint64_t AlignInBits,
607 uint64_t OffsetInBits,
608 unsigned Flags, DIType DerivedFrom,
609 DIArray Elements,
610 MDNode *VTableHolder,
Manman Ren23f84cb2013-08-27 23:06:40 +0000611 MDNode *TemplateParams,
612 StringRef UniqueIdentifier) {
Manman Ren89c83b72013-07-01 21:02:01 +0000613 assert((!Context || Context.isScope() || Context.isType()) &&
David Blaikie66438682013-03-11 23:21:19 +0000614 "createClassType should be called with a valid Context");
615 // TAG_class_type is encoded in DICompositeType format.
Eric Christopher87c06a82012-07-06 02:35:57 +0000616 Value *Elts[] = {
617 GetTagConstant(VMContext, dwarf::DW_TAG_class_type),
David Blaikie4776bce2013-03-20 00:26:26 +0000618 File.getFileNode(),
Eric Christopher87c06a82012-07-06 02:35:57 +0000619 getNonCompileUnitScope(Context),
620 MDString::get(VMContext, Name),
Eric Christopher87c06a82012-07-06 02:35:57 +0000621 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
622 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
623 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
624 ConstantInt::get(Type::getInt32Ty(VMContext), OffsetInBits),
625 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
626 DerivedFrom,
627 Elements,
628 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
629 VTableHolder,
Manman Ren6e3cd0e2013-08-26 22:39:55 +0000630 TemplateParams,
Manman Ren23f84cb2013-08-27 23:06:40 +0000631 UniqueIdentifier.empty() ? NULL : MDString::get(VMContext, UniqueIdentifier)
Eric Christopher87c06a82012-07-06 02:35:57 +0000632 };
David Blaikieca442a42013-03-26 23:46:39 +0000633 DICompositeType R(MDNode::get(VMContext, Elts));
Manman Ren89c83b72013-07-01 21:02:01 +0000634 assert(R.isCompositeType() &&
635 "createClassType should return a DICompositeType");
Manman Ren3a0e9b52013-08-29 23:17:54 +0000636 if (!UniqueIdentifier.empty())
637 retainType(R);
David Blaikie66438682013-03-11 23:21:19 +0000638 return R;
Eric Christopher87c06a82012-07-06 02:35:57 +0000639}
640
Devang Patel50d280c2011-02-22 18:56:12 +0000641/// createStructType - Create debugging information entry for a struct.
David Blaikie6172f022013-02-25 01:07:18 +0000642DICompositeType DIBuilder::createStructType(DIDescriptor Context,
643 StringRef Name, DIFile File,
644 unsigned LineNumber,
645 uint64_t SizeInBits,
646 uint64_t AlignInBits,
647 unsigned Flags, DIType DerivedFrom,
648 DIArray Elements,
649 unsigned RunTimeLang,
Manman Ren23f84cb2013-08-27 23:06:40 +0000650 MDNode *VTableHolder,
651 StringRef UniqueIdentifier) {
Devang Patelfe58f952010-12-07 23:25:47 +0000652 // TAG_structure_type is encoded in DICompositeType format.
653 Value *Elts[] = {
654 GetTagConstant(VMContext, dwarf::DW_TAG_structure_type),
David Blaikie4776bce2013-03-20 00:26:26 +0000655 File.getFileNode(),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000656 getNonCompileUnitScope(Context),
Devang Patelfe58f952010-12-07 23:25:47 +0000657 MDString::get(VMContext, Name),
Devang Patelfe58f952010-12-07 23:25:47 +0000658 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
659 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
660 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
Devang Patel43c249c2010-12-08 01:50:15 +0000661 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
Devang Patelfe58f952010-12-07 23:25:47 +0000662 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
David Blaikie6172f022013-02-25 01:07:18 +0000663 DerivedFrom,
Devang Patelfe58f952010-12-07 23:25:47 +0000664 Elements,
665 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
David Blaikie6172f022013-02-25 01:07:18 +0000666 VTableHolder,
David Blaikied4f92fd2013-02-18 07:27:30 +0000667 NULL,
Manman Ren23f84cb2013-08-27 23:06:40 +0000668 UniqueIdentifier.empty() ? NULL : MDString::get(VMContext, UniqueIdentifier)
Devang Patelfe58f952010-12-07 23:25:47 +0000669 };
David Blaikie66438682013-03-11 23:21:19 +0000670 DICompositeType R(MDNode::get(VMContext, Elts));
Manman Ren89c83b72013-07-01 21:02:01 +0000671 assert(R.isCompositeType() &&
672 "createStructType should return a DICompositeType");
Manman Ren3a0e9b52013-08-29 23:17:54 +0000673 if (!UniqueIdentifier.empty())
674 retainType(R);
David Blaikie66438682013-03-11 23:21:19 +0000675 return R;
Devang Patelfe58f952010-12-07 23:25:47 +0000676}
677
Devang Patel50d280c2011-02-22 18:56:12 +0000678/// createUnionType - Create debugging information entry for an union.
Eric Christophercf0623b2013-04-02 22:55:52 +0000679DICompositeType DIBuilder::createUnionType(DIDescriptor Scope, StringRef Name,
680 DIFile File, unsigned LineNumber,
681 uint64_t SizeInBits,
682 uint64_t AlignInBits, unsigned Flags,
683 DIArray Elements,
Manman Ren23f84cb2013-08-27 23:06:40 +0000684 unsigned RunTimeLang,
685 StringRef UniqueIdentifier) {
Devang Patel43c249c2010-12-08 01:50:15 +0000686 // TAG_union_type is encoded in DICompositeType format.
687 Value *Elts[] = {
688 GetTagConstant(VMContext, dwarf::DW_TAG_union_type),
David Blaikie4776bce2013-03-20 00:26:26 +0000689 File.getFileNode(),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000690 getNonCompileUnitScope(Scope),
Devang Patel43c249c2010-12-08 01:50:15 +0000691 MDString::get(VMContext, Name),
Devang Patel43c249c2010-12-08 01:50:15 +0000692 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
693 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
694 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
695 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
696 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patelc104cf22011-12-16 17:51:31 +0000697 NULL,
Devang Patel43c249c2010-12-08 01:50:15 +0000698 Elements,
699 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
Manman Ren6e3cd0e2013-08-26 22:39:55 +0000700 NULL,
701 NULL,
Manman Ren23f84cb2013-08-27 23:06:40 +0000702 UniqueIdentifier.empty() ? NULL : MDString::get(VMContext, UniqueIdentifier)
Devang Patel43c249c2010-12-08 01:50:15 +0000703 };
Manman Ren3a0e9b52013-08-29 23:17:54 +0000704 DICompositeType R(MDNode::get(VMContext, Elts));
705 if (!UniqueIdentifier.empty())
706 retainType(R);
707 return R;
Devang Patel43c249c2010-12-08 01:50:15 +0000708}
709
Devang Patel50d280c2011-02-22 18:56:12 +0000710/// createSubroutineType - Create subroutine type.
David Blaikied67c5ca2013-02-18 06:41:57 +0000711DICompositeType
712DIBuilder::createSubroutineType(DIFile File, DIArray ParameterTypes) {
Devang Patel43c249c2010-12-08 01:50:15 +0000713 // TAG_subroutine_type is encoded in DICompositeType format.
714 Value *Elts[] = {
715 GetTagConstant(VMContext, dwarf::DW_TAG_subroutine_type),
Bill Wendlingf46b4972012-07-06 17:47:36 +0000716 Constant::getNullValue(Type::getInt32Ty(VMContext)),
Bill Wendlingf46b4972012-07-06 17:47:36 +0000717 Constant::getNullValue(Type::getInt32Ty(VMContext)),
David Blaikiea13f3cd2013-03-19 23:25:22 +0000718 MDString::get(VMContext, ""),
Devang Patel43c249c2010-12-08 01:50:15 +0000719 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
720 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
721 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
Devang Patelc104cf22011-12-16 17:51:31 +0000722 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
Devang Patel43c249c2010-12-08 01:50:15 +0000723 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
Devang Patelc104cf22011-12-16 17:51:31 +0000724 NULL,
Devang Patel43c249c2010-12-08 01:50:15 +0000725 ParameterTypes,
726 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
Manman Ren6e3cd0e2013-08-26 22:39:55 +0000727 NULL,
728 NULL,
729 NULL // Type Identifer
Devang Patel43c249c2010-12-08 01:50:15 +0000730 };
David Blaikie72dfb052013-03-28 02:44:59 +0000731 return DICompositeType(MDNode::get(VMContext, Elts));
Devang Patel43c249c2010-12-08 01:50:15 +0000732}
733
Eric Christopher6c0046f2011-08-26 21:02:40 +0000734/// createEnumerationType - Create debugging information entry for an
Devang Patel43c249c2010-12-08 01:50:15 +0000735/// enumeration.
David Blaikied67c5ca2013-02-18 06:41:57 +0000736DICompositeType DIBuilder::createEnumerationType(
737 DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
738 uint64_t SizeInBits, uint64_t AlignInBits, DIArray Elements,
Manman Ren23f84cb2013-08-27 23:06:40 +0000739 DIType UnderlyingType, StringRef UniqueIdentifier) {
Devang Patel43c249c2010-12-08 01:50:15 +0000740 // TAG_enumeration_type is encoded in DICompositeType format.
741 Value *Elts[] = {
742 GetTagConstant(VMContext, dwarf::DW_TAG_enumeration_type),
David Blaikie4776bce2013-03-20 00:26:26 +0000743 File.getFileNode(),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000744 getNonCompileUnitScope(Scope),
Devang Patel43c249c2010-12-08 01:50:15 +0000745 MDString::get(VMContext, Name),
Devang Patel43c249c2010-12-08 01:50:15 +0000746 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
747 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
748 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
749 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
Eli Friedmance3da6f2012-10-05 01:49:14 +0000750 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
Adrian Prantl2f445be2013-04-19 19:56:02 +0000751 UnderlyingType,
Devang Patel43c249c2010-12-08 01:50:15 +0000752 Elements,
753 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
Manman Ren6e3cd0e2013-08-26 22:39:55 +0000754 NULL,
755 NULL,
Manman Ren23f84cb2013-08-27 23:06:40 +0000756 UniqueIdentifier.empty() ? NULL : MDString::get(VMContext, UniqueIdentifier)
Devang Patel43c249c2010-12-08 01:50:15 +0000757 };
Devang Patel1f48a952011-04-18 23:51:03 +0000758 MDNode *Node = MDNode::get(VMContext, Elts);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000759 AllEnumTypes.push_back(Node);
Manman Ren3a0e9b52013-08-29 23:17:54 +0000760 if (!UniqueIdentifier.empty())
761 retainType(Node);
David Blaikie72dfb052013-03-28 02:44:59 +0000762 return DICompositeType(Node);
Devang Patel43c249c2010-12-08 01:50:15 +0000763}
764
Devang Patel50d280c2011-02-22 18:56:12 +0000765/// createArrayType - Create debugging information entry for an array.
David Blaikied67c5ca2013-02-18 06:41:57 +0000766DICompositeType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
767 DIType Ty, DIArray Subscripts) {
Devang Patel43c249c2010-12-08 01:50:15 +0000768 // TAG_array_type is encoded in DICompositeType format.
769 Value *Elts[] = {
770 GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
David Blaikiea13f3cd2013-03-19 23:25:22 +0000771 NULL, // Filename/Directory,
Eric Christopher1fe3f9a2013-07-19 00:51:47 +0000772 NULL, // Unused
Devang Patel43c249c2010-12-08 01:50:15 +0000773 MDString::get(VMContext, ""),
Devang Patel43c249c2010-12-08 01:50:15 +0000774 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
775 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
776 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
777 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
778 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
779 Ty,
780 Subscripts,
781 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
Manman Ren6e3cd0e2013-08-26 22:39:55 +0000782 NULL,
783 NULL,
784 NULL // Type Identifer
Devang Patel43c249c2010-12-08 01:50:15 +0000785 };
David Blaikie72dfb052013-03-28 02:44:59 +0000786 return DICompositeType(MDNode::get(VMContext, Elts));
Devang Patel43c249c2010-12-08 01:50:15 +0000787}
788
Devang Patel50d280c2011-02-22 18:56:12 +0000789/// createVectorType - Create debugging information entry for a vector.
Manman Ren37bfb182013-06-07 03:13:46 +0000790DICompositeType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits,
791 DIType Ty, DIArray Subscripts) {
Eric Christopher9a1e0e22013-01-08 01:53:52 +0000792 // A vector is an array type with the FlagVector flag applied.
Devang Patel43c249c2010-12-08 01:50:15 +0000793 Value *Elts[] = {
Eric Christopher9a1e0e22013-01-08 01:53:52 +0000794 GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
David Blaikiea13f3cd2013-03-19 23:25:22 +0000795 NULL, // Filename/Directory,
Eric Christopher1fe3f9a2013-07-19 00:51:47 +0000796 NULL, // Unused
Devang Patel43c249c2010-12-08 01:50:15 +0000797 MDString::get(VMContext, ""),
Devang Patel43c249c2010-12-08 01:50:15 +0000798 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
799 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
800 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
801 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
Eric Christopher9a1e0e22013-01-08 01:53:52 +0000802 ConstantInt::get(Type::getInt32Ty(VMContext), DIType::FlagVector),
Devang Patel43c249c2010-12-08 01:50:15 +0000803 Ty,
804 Subscripts,
805 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
Manman Ren6e3cd0e2013-08-26 22:39:55 +0000806 NULL,
807 NULL,
808 NULL // Type Identifer
Devang Patel43c249c2010-12-08 01:50:15 +0000809 };
Manman Ren37bfb182013-06-07 03:13:46 +0000810 return DICompositeType(MDNode::get(VMContext, Elts));
Devang Patel43c249c2010-12-08 01:50:15 +0000811}
Devang Patelfe58f952010-12-07 23:25:47 +0000812
Devang Patel50d280c2011-02-22 18:56:12 +0000813/// createArtificialType - Create a new DIType with "artificial" flag set.
814DIType DIBuilder::createArtificialType(DIType Ty) {
Devang Patel35fcd652010-11-04 15:01:38 +0000815 if (Ty.isArtificial())
816 return Ty;
817
818 SmallVector<Value *, 9> Elts;
819 MDNode *N = Ty;
820 assert (N && "Unexpected input DIType!");
821 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
822 if (Value *V = N->getOperand(i))
823 Elts.push_back(V);
824 else
825 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
826 }
827
828 unsigned CurFlags = Ty.getFlags();
829 CurFlags = CurFlags | DIType::FlagArtificial;
830
831 // Flags are stored at this slot.
David Blaikie72dfb052013-03-28 02:44:59 +0000832 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
Devang Patel35fcd652010-11-04 15:01:38 +0000833
David Blaikie72dfb052013-03-28 02:44:59 +0000834 return DIType(MDNode::get(VMContext, Elts));
Devang Patel35fcd652010-11-04 15:01:38 +0000835}
Devang Patelfe58f952010-12-07 23:25:47 +0000836
Eric Christopher1f55eb42013-01-08 01:53:42 +0000837/// createObjectPointerType - Create a new type with both the object pointer
838/// and artificial flags set.
Eric Christophere5212782012-09-12 23:36:19 +0000839DIType DIBuilder::createObjectPointerType(DIType Ty) {
840 if (Ty.isObjectPointer())
841 return Ty;
842
843 SmallVector<Value *, 9> Elts;
844 MDNode *N = Ty;
845 assert (N && "Unexpected input DIType!");
846 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
847 if (Value *V = N->getOperand(i))
848 Elts.push_back(V);
849 else
850 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
851 }
852
853 unsigned CurFlags = Ty.getFlags();
854 CurFlags = CurFlags | (DIType::FlagObjectPointer | DIType::FlagArtificial);
855
856 // Flags are stored at this slot.
David Blaikie72dfb052013-03-28 02:44:59 +0000857 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
Eric Christophere5212782012-09-12 23:36:19 +0000858
David Blaikie72dfb052013-03-28 02:44:59 +0000859 return DIType(MDNode::get(VMContext, Elts));
Eric Christophere5212782012-09-12 23:36:19 +0000860}
861
Eric Christopher6c0046f2011-08-26 21:02:40 +0000862/// retainType - Retain DIType in a module even if it is not referenced
Devang Patel43c249c2010-12-08 01:50:15 +0000863/// through debug info anchors.
Devang Patel50d280c2011-02-22 18:56:12 +0000864void DIBuilder::retainType(DIType T) {
Manman Ren3a0e9b52013-08-29 23:17:54 +0000865 AllRetainTypes.push_back(TrackingVH<MDNode>(T));
Devang Patel43c249c2010-12-08 01:50:15 +0000866}
867
Devang Patel50d280c2011-02-22 18:56:12 +0000868/// createUnspecifiedParameter - Create unspeicified type descriptor
Devang Patel43c249c2010-12-08 01:50:15 +0000869/// for the subroutine type.
Devang Patel50d280c2011-02-22 18:56:12 +0000870DIDescriptor DIBuilder::createUnspecifiedParameter() {
Eric Christopher6c0046f2011-08-26 21:02:40 +0000871 Value *Elts[] = {
872 GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_parameters)
Devang Patel43c249c2010-12-08 01:50:15 +0000873 };
Devang Patel1f48a952011-04-18 23:51:03 +0000874 return DIDescriptor(MDNode::get(VMContext, Elts));
Devang Patel43c249c2010-12-08 01:50:15 +0000875}
876
Eric Christopher4fe34572012-02-08 00:22:26 +0000877/// createForwardDecl - Create a temporary forward-declared type that
878/// can be RAUW'd if the full type is seen.
David Blaikie692062f2013-08-16 20:42:14 +0000879DICompositeType DIBuilder::createForwardDecl(unsigned Tag, StringRef Name,
Eric Christopher216432d2012-04-23 19:00:11 +0000880 DIDescriptor Scope, DIFile F,
Eli Friedmance3da6f2012-10-05 01:49:14 +0000881 unsigned Line, unsigned RuntimeLang,
882 uint64_t SizeInBits,
Manman Ren23f84cb2013-08-27 23:06:40 +0000883 uint64_t AlignInBits,
884 StringRef UniqueIdentifier) {
Eric Christopher4fe34572012-02-08 00:22:26 +0000885 // Create a temporary MDNode.
886 Value *Elts[] = {
887 GetTagConstant(VMContext, Tag),
David Blaikie4776bce2013-03-20 00:26:26 +0000888 F.getFileNode(),
Eric Christopher216432d2012-04-23 19:00:11 +0000889 getNonCompileUnitScope(Scope),
Eric Christopher4fe34572012-02-08 00:22:26 +0000890 MDString::get(VMContext, Name),
Eric Christopher4fe34572012-02-08 00:22:26 +0000891 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
Eli Friedmance3da6f2012-10-05 01:49:14 +0000892 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
893 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
Eric Christopher4fe34572012-02-08 00:22:26 +0000894 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
895 ConstantInt::get(Type::getInt32Ty(VMContext),
Eric Christopher9f90e872012-02-20 18:04:14 +0000896 DIDescriptor::FlagFwdDecl),
897 NULL,
898 DIArray(),
David Blaikie692062f2013-08-16 20:42:14 +0000899 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
Manman Ren6e3cd0e2013-08-26 22:39:55 +0000900 NULL,
901 NULL, //TemplateParams
Manman Ren23f84cb2013-08-27 23:06:40 +0000902 UniqueIdentifier.empty() ? NULL : MDString::get(VMContext, UniqueIdentifier)
Eric Christopher4fe34572012-02-08 00:22:26 +0000903 };
904 MDNode *Node = MDNode::getTemporary(VMContext, Elts);
David Blaikie692062f2013-08-16 20:42:14 +0000905 DICompositeType RetTy(Node);
906 assert(RetTy.isCompositeType() &&
Manman Ren89c83b72013-07-01 21:02:01 +0000907 "createForwardDecl result should be a DIType");
Manman Ren3a0e9b52013-08-29 23:17:54 +0000908 if (!UniqueIdentifier.empty())
909 retainType(RetTy);
Manman Ren88328d22013-07-02 18:37:35 +0000910 return RetTy;
Eric Christopher4fe34572012-02-08 00:22:26 +0000911}
912
Devang Patel50d280c2011-02-22 18:56:12 +0000913/// getOrCreateArray - Get a DIArray, create one if required.
Jay Foad68550182011-04-24 10:11:03 +0000914DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
915 if (Elements.empty()) {
Bill Wendlingf46b4972012-07-06 17:47:36 +0000916 Value *Null = Constant::getNullValue(Type::getInt32Ty(VMContext));
Jay Foadec9186b2011-04-21 19:59:31 +0000917 return DIArray(MDNode::get(VMContext, Null));
Devang Patelfe58f952010-12-07 23:25:47 +0000918 }
Jay Foad68550182011-04-24 10:11:03 +0000919 return DIArray(MDNode::get(VMContext, Elements));
Devang Patelfe58f952010-12-07 23:25:47 +0000920}
921
Devang Patel50d280c2011-02-22 18:56:12 +0000922/// getOrCreateSubrange - Create a descriptor for a value range. This
Devang Patel43c249c2010-12-08 01:50:15 +0000923/// implicitly uniques the values returned.
Bill Wendling9493dae2012-12-04 21:34:03 +0000924DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
Devang Patel43c249c2010-12-08 01:50:15 +0000925 Value *Elts[] = {
926 GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
927 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
Bill Wendlinga7645a32012-12-04 06:20:49 +0000928 ConstantInt::get(Type::getInt64Ty(VMContext), Count)
Devang Patel43c249c2010-12-08 01:50:15 +0000929 };
930
Devang Patel1f48a952011-04-18 23:51:03 +0000931 return DISubrange(MDNode::get(VMContext, Elts));
Devang Patel43c249c2010-12-08 01:50:15 +0000932}
933
David Blaikie3fac43d2013-03-20 17:49:48 +0000934/// \brief Create a new descriptor for the specified global.
Devang Patelfe58f952010-12-07 23:25:47 +0000935DIGlobalVariable DIBuilder::
David Blaikie3fac43d2013-03-20 17:49:48 +0000936createGlobalVariable(StringRef Name, StringRef LinkageName, DIFile F,
937 unsigned LineNumber, DIType Ty, bool isLocalToUnit,
938 Value *Val) {
Devang Patelfe58f952010-12-07 23:25:47 +0000939 Value *Elts[] = {
940 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
Bill Wendlingf46b4972012-07-06 17:47:36 +0000941 Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000942 NULL, // TheCU,
Devang Patelfe58f952010-12-07 23:25:47 +0000943 MDString::get(VMContext, Name),
944 MDString::get(VMContext, Name),
David Blaikie3fac43d2013-03-20 17:49:48 +0000945 MDString::get(VMContext, LinkageName),
Devang Patelfe58f952010-12-07 23:25:47 +0000946 F,
947 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
948 Ty,
949 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
950 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
Eric Christopher6b6061f2013-01-16 01:22:23 +0000951 Val,
952 DIDescriptor()
Devang Patelfe58f952010-12-07 23:25:47 +0000953 };
Devang Patel1f48a952011-04-18 23:51:03 +0000954 MDNode *Node = MDNode::get(VMContext, Elts);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000955 AllGVs.push_back(Node);
Devang Patelfe58f952010-12-07 23:25:47 +0000956 return DIGlobalVariable(Node);
957}
958
David Blaikie3fac43d2013-03-20 17:49:48 +0000959/// \brief Create a new descriptor for the specified global.
960DIGlobalVariable DIBuilder::
961createGlobalVariable(StringRef Name, DIFile F, unsigned LineNumber,
962 DIType Ty, bool isLocalToUnit, Value *Val) {
963 return createGlobalVariable(Name, Name, F, LineNumber, Ty, isLocalToUnit,
964 Val);
965}
966
Devang Patel50d280c2011-02-22 18:56:12 +0000967/// createStaticVariable - Create a new descriptor for the specified static
Devang Patelfe58f952010-12-07 23:25:47 +0000968/// variable.
969DIGlobalVariable DIBuilder::
Eric Christopher6c0046f2011-08-26 21:02:40 +0000970createStaticVariable(DIDescriptor Context, StringRef Name,
971 StringRef LinkageName, DIFile F, unsigned LineNumber,
Eric Christopher6b6061f2013-01-16 01:22:23 +0000972 DIType Ty, bool isLocalToUnit, Value *Val, MDNode *Decl) {
Devang Patelfe58f952010-12-07 23:25:47 +0000973 Value *Elts[] = {
974 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
Bill Wendlingf46b4972012-07-06 17:47:36 +0000975 Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000976 getNonCompileUnitScope(Context),
Devang Patelfe58f952010-12-07 23:25:47 +0000977 MDString::get(VMContext, Name),
978 MDString::get(VMContext, Name),
979 MDString::get(VMContext, LinkageName),
980 F,
981 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
982 Ty,
983 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
984 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
Eric Christopher6b6061f2013-01-16 01:22:23 +0000985 Val,
986 DIDescriptor(Decl)
Devang Patelfe58f952010-12-07 23:25:47 +0000987 };
Devang Patel1f48a952011-04-18 23:51:03 +0000988 MDNode *Node = MDNode::get(VMContext, Elts);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000989 AllGVs.push_back(Node);
Devang Patelfe58f952010-12-07 23:25:47 +0000990 return DIGlobalVariable(Node);
991}
992
Devang Patel50d280c2011-02-22 18:56:12 +0000993/// createVariable - Create a new descriptor for the specified variable.
994DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
Devang Patel48f17ba2010-12-07 23:58:00 +0000995 StringRef Name, DIFile File,
Eric Christopher6c0046f2011-08-26 21:02:40 +0000996 unsigned LineNo, DIType Ty,
Devang Patele9e16c52011-03-01 22:58:13 +0000997 bool AlwaysPreserve, unsigned Flags,
998 unsigned ArgNo) {
David Blaikie66438682013-03-11 23:21:19 +0000999 DIDescriptor Context(getNonCompileUnitScope(Scope));
Manman Ren89c83b72013-07-01 21:02:01 +00001000 assert((!Context || Context.isScope()) &&
David Blaikie66438682013-03-11 23:21:19 +00001001 "createLocalVariable should be called with a valid Context");
Manman Ren89c83b72013-07-01 21:02:01 +00001002 assert(Ty.isType() &&
David Blaikie66438682013-03-11 23:21:19 +00001003 "createLocalVariable should be called with a valid type");
Devang Patel48f17ba2010-12-07 23:58:00 +00001004 Value *Elts[] = {
1005 GetTagConstant(VMContext, Tag),
Devang Patel94c7ddb2011-08-16 22:09:43 +00001006 getNonCompileUnitScope(Scope),
Devang Patel48f17ba2010-12-07 23:58:00 +00001007 MDString::get(VMContext, Name),
1008 File,
Devang Patele9e16c52011-03-01 22:58:13 +00001009 ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))),
Devang Patel48f17ba2010-12-07 23:58:00 +00001010 Ty,
Devang Patel23336b42011-07-19 19:41:54 +00001011 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Bill Wendling9fdb7c02012-07-06 17:49:19 +00001012 Constant::getNullValue(Type::getInt32Ty(VMContext))
Devang Patel48f17ba2010-12-07 23:58:00 +00001013 };
Devang Patel1f48a952011-04-18 23:51:03 +00001014 MDNode *Node = MDNode::get(VMContext, Elts);
Devang Patel48f17ba2010-12-07 23:58:00 +00001015 if (AlwaysPreserve) {
1016 // The optimizer may remove local variable. If there is an interest
1017 // to preserve variable info in such situation then stash it in a
1018 // named mdnode.
1019 DISubprogram Fn(getDISubprogram(Scope));
Devang Patel93d39be2011-08-19 23:28:12 +00001020 NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, Fn);
Devang Patel48f17ba2010-12-07 23:58:00 +00001021 FnLocals->addOperand(Node);
1022 }
Manman Ren88328d22013-07-02 18:37:35 +00001023 DIVariable RetVar(Node);
1024 assert(RetVar.isVariable() &&
Manman Ren89c83b72013-07-01 21:02:01 +00001025 "createLocalVariable should return a valid DIVariable");
Manman Ren88328d22013-07-02 18:37:35 +00001026 return RetVar;
Devang Patel48f17ba2010-12-07 23:58:00 +00001027}
1028
Devang Patel50d280c2011-02-22 18:56:12 +00001029/// createComplexVariable - Create a new descriptor for the specified variable
Devang Patelfe58f952010-12-07 23:25:47 +00001030/// which has a complex address expression for its address.
Devang Patel50d280c2011-02-22 18:56:12 +00001031DIVariable DIBuilder::createComplexVariable(unsigned Tag, DIDescriptor Scope,
Devang Patelfe58f952010-12-07 23:25:47 +00001032 StringRef Name, DIFile F,
1033 unsigned LineNo,
Jay Foad68550182011-04-24 10:11:03 +00001034 DIType Ty, ArrayRef<Value *> Addr,
1035 unsigned ArgNo) {
Devang Patelfe58f952010-12-07 23:25:47 +00001036 SmallVector<Value *, 15> Elts;
1037 Elts.push_back(GetTagConstant(VMContext, Tag));
Devang Patel94c7ddb2011-08-16 22:09:43 +00001038 Elts.push_back(getNonCompileUnitScope(Scope)),
Devang Patelfe58f952010-12-07 23:25:47 +00001039 Elts.push_back(MDString::get(VMContext, Name));
1040 Elts.push_back(F);
Eric Christopher6c0046f2011-08-26 21:02:40 +00001041 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext),
Devang Patel9f997212012-02-08 00:17:07 +00001042 (LineNo | (ArgNo << 24))));
Devang Patelfe58f952010-12-07 23:25:47 +00001043 Elts.push_back(Ty);
Bill Wendlingf46b4972012-07-06 17:47:36 +00001044 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
1045 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
Jay Foad68550182011-04-24 10:11:03 +00001046 Elts.append(Addr.begin(), Addr.end());
Devang Patelfe58f952010-12-07 23:25:47 +00001047
Devang Patel1f48a952011-04-18 23:51:03 +00001048 return DIVariable(MDNode::get(VMContext, Elts));
Devang Patelfe58f952010-12-07 23:25:47 +00001049}
1050
Devang Patel50d280c2011-02-22 18:56:12 +00001051/// createFunction - Create a new descriptor for the specified function.
1052DISubprogram DIBuilder::createFunction(DIDescriptor Context,
Devang Patel44498a62010-12-08 20:42:44 +00001053 StringRef Name,
1054 StringRef LinkageName,
1055 DIFile File, unsigned LineNo,
David Blaikie3d331842013-05-22 23:22:18 +00001056 DICompositeType Ty,
Devang Patel44498a62010-12-08 20:42:44 +00001057 bool isLocalToUnit, bool isDefinition,
Eric Christopher6126a1e2012-04-03 00:43:49 +00001058 unsigned ScopeLine,
Devang Patel44498a62010-12-08 20:42:44 +00001059 unsigned Flags, bool isOptimized,
Devang Patelda194752011-04-05 22:52:06 +00001060 Function *Fn,
Devang Patel5e06bb82011-04-22 23:10:17 +00001061 MDNode *TParams,
1062 MDNode *Decl) {
David Blaikie3d331842013-05-22 23:22:18 +00001063 assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
1064 "function types should be subroutines");
Devang Patel93d39be2011-08-19 23:28:12 +00001065 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
Devang Patel44498a62010-12-08 20:42:44 +00001066 Value *Elts[] = {
1067 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
David Blaikiebb4e6192013-03-21 23:08:34 +00001068 File.getFileNode(),
Devang Patel94c7ddb2011-08-16 22:09:43 +00001069 getNonCompileUnitScope(Context),
Devang Patel44498a62010-12-08 20:42:44 +00001070 MDString::get(VMContext, Name),
1071 MDString::get(VMContext, Name),
1072 MDString::get(VMContext, LinkageName),
Devang Patel44498a62010-12-08 20:42:44 +00001073 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1074 Ty,
1075 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1076 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1077 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
1078 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
Devang Patel9642c572011-12-15 17:55:56 +00001079 NULL,
Devang Patel44498a62010-12-08 20:42:44 +00001080 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1081 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patelda194752011-04-05 22:52:06 +00001082 Fn,
Devang Patel5e06bb82011-04-22 23:10:17 +00001083 TParams,
Devang Patel93d39be2011-08-19 23:28:12 +00001084 Decl,
David Blaikief839eed2013-02-04 05:56:36 +00001085 MDNode::getTemporary(VMContext, TElts),
Eric Christopher6126a1e2012-04-03 00:43:49 +00001086 ConstantInt::get(Type::getInt32Ty(VMContext), ScopeLine)
Devang Patel44498a62010-12-08 20:42:44 +00001087 };
Devang Patel1f48a952011-04-18 23:51:03 +00001088 MDNode *Node = MDNode::get(VMContext, Elts);
Devang Patel44498a62010-12-08 20:42:44 +00001089
1090 // Create a named metadata so that we do not lose this mdnode.
David Blaikie139f7e52013-02-18 07:10:22 +00001091 if (isDefinition)
1092 AllSubprograms.push_back(Node);
David Blaikieebb51832013-03-21 20:28:52 +00001093 DISubprogram S(Node);
Manman Ren89c83b72013-07-01 21:02:01 +00001094 assert(S.isSubprogram() && "createFunction should return a valid DISubprogram");
David Blaikieebb51832013-03-21 20:28:52 +00001095 return S;
Devang Patel44498a62010-12-08 20:42:44 +00001096}
1097
Devang Patel50d280c2011-02-22 18:56:12 +00001098/// createMethod - Create a new descriptor for the specified C++ method.
1099DISubprogram DIBuilder::createMethod(DIDescriptor Context,
Devang Patel44498a62010-12-08 20:42:44 +00001100 StringRef Name,
1101 StringRef LinkageName,
1102 DIFile F,
David Blaikie3d331842013-05-22 23:22:18 +00001103 unsigned LineNo, DICompositeType Ty,
Devang Patel44498a62010-12-08 20:42:44 +00001104 bool isLocalToUnit,
1105 bool isDefinition,
1106 unsigned VK, unsigned VIndex,
1107 MDNode *VTableHolder,
1108 unsigned Flags,
1109 bool isOptimized,
Devang Patelda194752011-04-05 22:52:06 +00001110 Function *Fn,
1111 MDNode *TParam) {
David Blaikie3d331842013-05-22 23:22:18 +00001112 assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
1113 "function types should be subroutines");
Devang Patel93d39be2011-08-19 23:28:12 +00001114 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
Devang Patel44498a62010-12-08 20:42:44 +00001115 Value *Elts[] = {
1116 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
David Blaikiebb4e6192013-03-21 23:08:34 +00001117 F.getFileNode(),
Devang Patel94c7ddb2011-08-16 22:09:43 +00001118 getNonCompileUnitScope(Context),
Devang Patel44498a62010-12-08 20:42:44 +00001119 MDString::get(VMContext, Name),
1120 MDString::get(VMContext, Name),
1121 MDString::get(VMContext, LinkageName),
Devang Patel44498a62010-12-08 20:42:44 +00001122 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1123 Ty,
1124 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1125 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1126 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
1127 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
1128 VTableHolder,
1129 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1130 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patelda194752011-04-05 22:52:06 +00001131 Fn,
1132 TParam,
Bill Wendlingf46b4972012-07-06 17:47:36 +00001133 Constant::getNullValue(Type::getInt32Ty(VMContext)),
David Blaikief839eed2013-02-04 05:56:36 +00001134 MDNode::getTemporary(VMContext, TElts),
Eric Christopher25016522012-05-18 00:16:22 +00001135 // FIXME: Do we want to use different scope/lines?
Eric Christopher6126a1e2012-04-03 00:43:49 +00001136 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
Devang Patel44498a62010-12-08 20:42:44 +00001137 };
Devang Patel1f48a952011-04-18 23:51:03 +00001138 MDNode *Node = MDNode::get(VMContext, Elts);
David Blaikie139f7e52013-02-18 07:10:22 +00001139 if (isDefinition)
1140 AllSubprograms.push_back(Node);
David Blaikieebb51832013-03-21 20:28:52 +00001141 DISubprogram S(Node);
Manman Ren89c83b72013-07-01 21:02:01 +00001142 assert(S.isSubprogram() && "createMethod should return a valid DISubprogram");
David Blaikieebb51832013-03-21 20:28:52 +00001143 return S;
Devang Patel44498a62010-12-08 20:42:44 +00001144}
1145
Devang Patel50d280c2011-02-22 18:56:12 +00001146/// createNameSpace - This creates new descriptor for a namespace
Devang Patelfe58f952010-12-07 23:25:47 +00001147/// with the specified parent scope.
Devang Patel50d280c2011-02-22 18:56:12 +00001148DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
Devang Patelfe58f952010-12-07 23:25:47 +00001149 DIFile File, unsigned LineNo) {
1150 Value *Elts[] = {
1151 GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
David Blaikie6115ed02013-03-20 19:39:15 +00001152 File.getFileNode(),
Devang Patel94c7ddb2011-08-16 22:09:43 +00001153 getNonCompileUnitScope(Scope),
Devang Patelfe58f952010-12-07 23:25:47 +00001154 MDString::get(VMContext, Name),
Devang Patelfe58f952010-12-07 23:25:47 +00001155 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1156 };
David Blaikie66438682013-03-11 23:21:19 +00001157 DINameSpace R(MDNode::get(VMContext, Elts));
1158 assert(R.Verify() &&
1159 "createNameSpace should return a verifiable DINameSpace");
1160 return R;
Devang Patelfe58f952010-12-07 23:25:47 +00001161}
1162
Eric Christopher6618a242011-10-11 22:59:11 +00001163/// createLexicalBlockFile - This creates a new MDNode that encapsulates
1164/// an existing scope with a new filename.
1165DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
Devang Patel9f997212012-02-08 00:17:07 +00001166 DIFile File) {
Eric Christopher6618a242011-10-11 22:59:11 +00001167 Value *Elts[] = {
1168 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
David Blaikie8faed272013-03-22 20:18:46 +00001169 File.getFileNode(),
David Blaikie7b246862013-03-22 19:13:22 +00001170 Scope
Eric Christopher6618a242011-10-11 22:59:11 +00001171 };
David Blaikie66438682013-03-11 23:21:19 +00001172 DILexicalBlockFile R(MDNode::get(VMContext, Elts));
1173 assert(
1174 R.Verify() &&
1175 "createLexicalBlockFile should return a verifiable DILexicalBlockFile");
1176 return R;
Eric Christopher6618a242011-10-11 22:59:11 +00001177}
1178
Devang Patel50d280c2011-02-22 18:56:12 +00001179DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
Devang Patel43c249c2010-12-08 01:50:15 +00001180 unsigned Line, unsigned Col) {
Adrian Prantle06db0c2013-06-24 21:19:43 +00001181 // Defeat MDNode uniquing for lexical blocks by using unique id.
Devang Patel43c249c2010-12-08 01:50:15 +00001182 static unsigned int unique_id = 0;
1183 Value *Elts[] = {
1184 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
David Blaikie4b52a882013-03-22 17:33:20 +00001185 File.getFileNode(),
Devang Patel94c7ddb2011-08-16 22:09:43 +00001186 getNonCompileUnitScope(Scope),
Devang Patel43c249c2010-12-08 01:50:15 +00001187 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
1188 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
Devang Patel43c249c2010-12-08 01:50:15 +00001189 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
1190 };
David Blaikie66438682013-03-11 23:21:19 +00001191 DILexicalBlock R(MDNode::get(VMContext, Elts));
1192 assert(R.Verify() &&
1193 "createLexicalBlock should return a verifiable DILexicalBlock");
1194 return R;
Devang Patel43c249c2010-12-08 01:50:15 +00001195}
1196
Devang Patel50d280c2011-02-22 18:56:12 +00001197/// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1198Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
Devang Patelfe58f952010-12-07 23:25:47 +00001199 Instruction *InsertBefore) {
1200 assert(Storage && "no storage passed to dbg.declare");
Manman Renda918172013-06-29 05:01:19 +00001201 assert(VarInfo.isVariable() &&
1202 "empty or invalid DIVariable passed to dbg.declare");
Devang Patelfe58f952010-12-07 23:25:47 +00001203 if (!DeclareFn)
1204 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1205
Jay Foadec9186b2011-04-21 19:59:31 +00001206 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
Jay Foada3efbb12011-07-15 08:37:34 +00001207 return CallInst::Create(DeclareFn, Args, "", InsertBefore);
Devang Patelfe58f952010-12-07 23:25:47 +00001208}
1209
Devang Patel50d280c2011-02-22 18:56:12 +00001210/// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1211Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
Devang Patelfe58f952010-12-07 23:25:47 +00001212 BasicBlock *InsertAtEnd) {
1213 assert(Storage && "no storage passed to dbg.declare");
Manman Renda918172013-06-29 05:01:19 +00001214 assert(VarInfo.isVariable() &&
1215 "empty or invalid DIVariable passed to dbg.declare");
Devang Patelfe58f952010-12-07 23:25:47 +00001216 if (!DeclareFn)
1217 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1218
Jay Foadec9186b2011-04-21 19:59:31 +00001219 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
Devang Patelfe58f952010-12-07 23:25:47 +00001220
1221 // If this block already has a terminator then insert this intrinsic
1222 // before the terminator.
1223 if (TerminatorInst *T = InsertAtEnd->getTerminator())
Jay Foada3efbb12011-07-15 08:37:34 +00001224 return CallInst::Create(DeclareFn, Args, "", T);
Devang Patelfe58f952010-12-07 23:25:47 +00001225 else
Jay Foada3efbb12011-07-15 08:37:34 +00001226 return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
Devang Patelfe58f952010-12-07 23:25:47 +00001227}
1228
Devang Patel50d280c2011-02-22 18:56:12 +00001229/// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1230Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
Devang Patelfe58f952010-12-07 23:25:47 +00001231 DIVariable VarInfo,
1232 Instruction *InsertBefore) {
1233 assert(V && "no value passed to dbg.value");
Manman Renda918172013-06-29 05:01:19 +00001234 assert(VarInfo.isVariable() &&
1235 "empty or invalid DIVariable passed to dbg.value");
Devang Patelfe58f952010-12-07 23:25:47 +00001236 if (!ValueFn)
1237 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1238
Jay Foadec9186b2011-04-21 19:59:31 +00001239 Value *Args[] = { MDNode::get(V->getContext(), V),
Devang Patelfe58f952010-12-07 23:25:47 +00001240 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1241 VarInfo };
Jay Foada3efbb12011-07-15 08:37:34 +00001242 return CallInst::Create(ValueFn, Args, "", InsertBefore);
Devang Patelfe58f952010-12-07 23:25:47 +00001243}
1244
Devang Patel50d280c2011-02-22 18:56:12 +00001245/// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1246Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
Devang Patelfe58f952010-12-07 23:25:47 +00001247 DIVariable VarInfo,
1248 BasicBlock *InsertAtEnd) {
1249 assert(V && "no value passed to dbg.value");
Manman Renda918172013-06-29 05:01:19 +00001250 assert(VarInfo.isVariable() &&
1251 "empty or invalid DIVariable passed to dbg.value");
Devang Patelfe58f952010-12-07 23:25:47 +00001252 if (!ValueFn)
1253 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1254
Jay Foadec9186b2011-04-21 19:59:31 +00001255 Value *Args[] = { MDNode::get(V->getContext(), V),
Devang Patelfe58f952010-12-07 23:25:47 +00001256 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1257 VarInfo };
Jay Foada3efbb12011-07-15 08:37:34 +00001258 return CallInst::Create(ValueFn, Args, "", InsertAtEnd);
Devang Patelfe58f952010-12-07 23:25:47 +00001259}