blob: 2ff10b593eded9fd3f0f96b4acb4469643b7e30d [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
Manman Renbc660712013-09-05 18:48:31 +000078/// Use the type identifier instead of the actual MDNode if possible,
79/// to help type uniquing. This function returns the identifier if it
80/// exists for the given type, otherwise returns the MDNode.
81static Value *getTypeIdentifier(DIType T) {
82 if (!T.isCompositeType())
83 return T;
84 DICompositeType DTy(T);
85 if (!DTy.getIdentifier())
86 return T;
87 return DTy.getIdentifier();
88}
89
Eric Christopher6c0046f2011-08-26 21:02:40 +000090/// getNonCompileUnitScope - If N is compile unit return NULL otherwise return
91/// N.
Devang Patel94c7ddb2011-08-16 22:09:43 +000092static MDNode *getNonCompileUnitScope(MDNode *N) {
93 if (DIDescriptor(N).isCompileUnit())
94 return NULL;
95 return N;
Devang Patel6326a422011-08-15 23:00:00 +000096}
97
David Blaikie00c5c5d2013-03-20 23:58:12 +000098static MDNode *createFilePathPair(LLVMContext &VMContext, StringRef Filename,
99 StringRef Directory) {
100 assert(!Filename.empty() && "Unable to create file without name");
101 Value *Pair[] = {
102 MDString::get(VMContext, Filename),
103 MDString::get(VMContext, Directory),
104 };
105 return MDNode::get(VMContext, Pair);
106}
107
Devang Patel50d280c2011-02-22 18:56:12 +0000108/// createCompileUnit - A CompileUnit provides an anchor for all debugging
Devang Patel35fcd652010-11-04 15:01:38 +0000109/// information generated during this instance of compilation.
Eric Christopher1fe3f9a2013-07-19 00:51:47 +0000110DICompileUnit DIBuilder::createCompileUnit(unsigned Lang, StringRef Filename,
111 StringRef Directory,
112 StringRef Producer, bool isOptimized,
113 StringRef Flags, unsigned RunTimeVer,
114 StringRef SplitName) {
Chandler Carruthb0dc4d92012-01-10 18:18:52 +0000115 assert(((Lang <= dwarf::DW_LANG_Python && Lang >= dwarf::DW_LANG_C89) ||
116 (Lang <= dwarf::DW_LANG_hi_user && Lang >= dwarf::DW_LANG_lo_user)) &&
117 "Invalid Language tag");
118 assert(!Filename.empty() &&
119 "Unable to create compile unit without filename");
Devang Patel94c7ddb2011-08-16 22:09:43 +0000120 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
121 TempEnumTypes = MDNode::getTemporary(VMContext, TElts);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000122
123 TempRetainTypes = MDNode::getTemporary(VMContext, TElts);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000124
125 TempSubprograms = MDNode::getTemporary(VMContext, TElts);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000126
127 TempGVs = MDNode::getTemporary(VMContext, TElts);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000128
David Blaikiec462db62013-04-22 06:12:31 +0000129 TempImportedModules = MDNode::getTemporary(VMContext, TElts);
130
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000131 Value *Elts[] = {
132 GetTagConstant(VMContext, dwarf::DW_TAG_compile_unit),
David Blaikie00c5c5d2013-03-20 23:58:12 +0000133 createFilePathPair(VMContext, Filename, Directory),
David Blaikie162c8002013-03-20 22:52:54 +0000134 ConstantInt::get(Type::getInt32Ty(VMContext), Lang),
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000135 MDString::get(VMContext, Producer),
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000136 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
137 MDString::get(VMContext, Flags),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000138 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer),
David Blaikiea8eefc72013-02-02 05:56:24 +0000139 TempEnumTypes,
140 TempRetainTypes,
141 TempSubprograms,
Eric Christophere4b67902013-02-22 23:50:04 +0000142 TempGVs,
David Blaikiec462db62013-04-22 06:12:31 +0000143 TempImportedModules,
Eric Christophere4b67902013-02-22 23:50:04 +0000144 MDString::get(VMContext, SplitName)
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000145 };
Eric Christopher1fe3f9a2013-07-19 00:51:47 +0000146
147 MDNode *CUNode = MDNode::get(VMContext, Elts);
Devang Patel464f4ef2011-05-03 16:18:28 +0000148
149 // Create a named metadata so that it is easier to find cu in a module.
150 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
Eric Christopher1fe3f9a2013-07-19 00:51:47 +0000151 NMD->addOperand(CUNode);
152
153 return DICompileUnit(CUNode);
Devang Patel35fcd652010-11-04 15:01:38 +0000154}
155
David Blaikie7b72cc72013-05-20 22:50:35 +0000156static DIImportedEntity
157createImportedModule(LLVMContext &C, DIScope Context, DIDescriptor NS,
158 unsigned Line, StringRef Name,
159 SmallVectorImpl<Value *> &AllImportedModules) {
160 const MDNode *R;
161 if (Name.empty()) {
162 Value *Elts[] = {
163 GetTagConstant(C, dwarf::DW_TAG_imported_module),
164 Context,
165 NS,
166 ConstantInt::get(Type::getInt32Ty(C), Line),
167 };
168 R = MDNode::get(C, Elts);
169 } else {
170 Value *Elts[] = {
171 GetTagConstant(C, dwarf::DW_TAG_imported_module),
172 Context,
173 NS,
174 ConstantInt::get(Type::getInt32Ty(C), Line),
175 MDString::get(C, Name)
176 };
177 R = MDNode::get(C, Elts);
178 }
179 DIImportedEntity M(R);
David Blaikie20d9e412013-05-07 21:35:53 +0000180 assert(M.Verify() && "Imported module should be valid");
181 AllImportedModules.push_back(M);
182 return M;
183}
184
David Blaikie7b72cc72013-05-20 22:50:35 +0000185DIImportedEntity DIBuilder::createImportedModule(DIScope Context,
186 DINameSpace NS, unsigned Line,
187 StringRef Name) {
188 return ::createImportedModule(VMContext, Context, NS, Line, Name,
189 AllImportedModules);
190}
191
192DIImportedEntity DIBuilder::createImportedModule(DIScope Context,
193 DIImportedEntity NS,
194 unsigned Line,
195 StringRef Name) {
196 return ::createImportedModule(VMContext, Context, NS, Line, Name,
197 AllImportedModules);
198}
199
David Blaikie20d9e412013-05-07 21:35:53 +0000200DIImportedEntity DIBuilder::createImportedDeclaration(DIScope Context,
201 DIDescriptor Decl,
202 unsigned Line) {
203 Value *Elts[] = {
204 GetTagConstant(VMContext, dwarf::DW_TAG_imported_declaration),
205 Context,
206 Decl,
207 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
208 };
209 DIImportedEntity M(MDNode::get(VMContext, Elts));
David Blaikiec462db62013-04-22 06:12:31 +0000210 assert(M.Verify() && "Imported module should be valid");
211 AllImportedModules.push_back(M);
212 return M;
213}
214
Devang Patel50d280c2011-02-22 18:56:12 +0000215/// createFile - Create a file descriptor to hold debugging information
Devang Patel35fcd652010-11-04 15:01:38 +0000216/// for a file.
Devang Patel50d280c2011-02-22 18:56:12 +0000217DIFile DIBuilder::createFile(StringRef Filename, StringRef Directory) {
David Blaikieb4cf0ab2013-03-17 21:13:55 +0000218 Value *Elts[] = {
219 GetTagConstant(VMContext, dwarf::DW_TAG_file_type),
David Blaikie72dfb052013-03-28 02:44:59 +0000220 createFilePathPair(VMContext, Filename, Directory)
David Blaikieb4cf0ab2013-03-17 21:13:55 +0000221 };
David Blaikie72dfb052013-03-28 02:44:59 +0000222 return DIFile(MDNode::get(VMContext, Elts));
Devang Patel35fcd652010-11-04 15:01:38 +0000223}
224
Devang Patel50d280c2011-02-22 18:56:12 +0000225/// createEnumerator - Create a single enumerator value.
David Blaikie8de0a462013-06-24 17:34:33 +0000226DIEnumerator DIBuilder::createEnumerator(StringRef Name, int64_t Val) {
Devang Patel811ae5b2011-09-12 18:26:08 +0000227 assert(!Name.empty() && "Unable to create enumerator without name");
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000228 Value *Elts[] = {
229 GetTagConstant(VMContext, dwarf::DW_TAG_enumerator),
230 MDString::get(VMContext, Name),
231 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
232 };
David Blaikie72dfb052013-03-28 02:44:59 +0000233 return DIEnumerator(MDNode::get(VMContext, Elts));
Devang Patel35fcd652010-11-04 15:01:38 +0000234}
235
Peter Collingbourne03ccdb52013-06-27 22:50:59 +0000236/// \brief Create a DWARF unspecified type.
237DIBasicType DIBuilder::createUnspecifiedType(StringRef Name) {
Devang Patel734a67c2011-09-14 23:13:28 +0000238 assert(!Name.empty() && "Unable to create type without name");
Peter Collingbourne03ccdb52013-06-27 22:50:59 +0000239 // Unspecified types are encoded in DIBasicType format. Line number, filename,
240 // size, alignment, offset and flags are always empty here.
Devang Patel734a67c2011-09-14 23:13:28 +0000241 Value *Elts[] = {
242 GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_type),
David Blaikiea13f3cd2013-03-19 23:25:22 +0000243 NULL, // Filename
Eric Christopher1fe3f9a2013-07-19 00:51:47 +0000244 NULL, // Unused
Devang Patel734a67c2011-09-14 23:13:28 +0000245 MDString::get(VMContext, Name),
Devang Patel734a67c2011-09-14 23:13:28 +0000246 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
247 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
248 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
249 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
250 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
Bill Wendling9fdb7c02012-07-06 17:49:19 +0000251 ConstantInt::get(Type::getInt32Ty(VMContext), 0) // Encoding
Devang Patel734a67c2011-09-14 23:13:28 +0000252 };
Manman Ren576d49a2013-06-07 18:35:53 +0000253 return DIBasicType(MDNode::get(VMContext, Elts));
Devang Patel734a67c2011-09-14 23:13:28 +0000254}
255
Peter Collingbourne03ccdb52013-06-27 22:50:59 +0000256/// \brief Create C++11 nullptr type.
257DIBasicType DIBuilder::createNullPtrType() {
258 return createUnspecifiedType("decltype(nullptr)");
259}
260
Eric Christopher6c0046f2011-08-26 21:02:40 +0000261/// createBasicType - Create debugging information entry for a basic
Devang Patel35fcd652010-11-04 15:01:38 +0000262/// type, e.g 'char'.
David Blaikie2ce067a2013-02-12 00:40:41 +0000263DIBasicType
264DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
265 uint64_t AlignInBits, unsigned Encoding) {
Devang Patel811ae5b2011-09-12 18:26:08 +0000266 assert(!Name.empty() && "Unable to create type without name");
Devang Patel35fcd652010-11-04 15:01:38 +0000267 // Basic types are encoded in DIBasicType format. Line number, filename,
268 // offset and flags are always empty here.
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000269 Value *Elts[] = {
270 GetTagConstant(VMContext, dwarf::DW_TAG_base_type),
David Blaikiea13f3cd2013-03-19 23:25:22 +0000271 NULL, // File/directory name
Eric Christopher1fe3f9a2013-07-19 00:51:47 +0000272 NULL, // Unused
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000273 MDString::get(VMContext, Name),
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000274 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
275 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
276 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
277 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
278 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
279 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
280 };
David Blaikie72dfb052013-03-28 02:44:59 +0000281 return DIBasicType(MDNode::get(VMContext, Elts));
Devang Patel35fcd652010-11-04 15:01:38 +0000282}
283
Nick Lewyckyffab7d02011-11-09 22:45:04 +0000284/// createQualifiedType - Create debugging information entry for a qualified
Devang Patel35fcd652010-11-04 15:01:38 +0000285/// type, e.g. 'const int'.
David Blaikied67c5ca2013-02-18 06:41:57 +0000286DIDerivedType DIBuilder::createQualifiedType(unsigned Tag, DIType FromTy) {
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000287 // Qualified types are encoded in DIDerivedType format.
288 Value *Elts[] = {
289 GetTagConstant(VMContext, Tag),
David Blaikiea13f3cd2013-03-19 23:25:22 +0000290 NULL, // Filename
Eric Christopher1fe3f9a2013-07-19 00:51:47 +0000291 NULL, // Unused
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000292 MDString::get(VMContext, StringRef()), // Empty name.
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000293 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
294 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
295 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
296 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
297 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
298 FromTy
299 };
David Blaikie72dfb052013-03-28 02:44:59 +0000300 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel35fcd652010-11-04 15:01:38 +0000301}
302
Devang Patel50d280c2011-02-22 18:56:12 +0000303/// createPointerType - Create debugging information entry for a pointer.
David Blaikied67c5ca2013-02-18 06:41:57 +0000304DIDerivedType
305DIBuilder::createPointerType(DIType PointeeTy, uint64_t SizeInBits,
306 uint64_t AlignInBits, StringRef Name) {
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000307 // Pointer types are encoded in DIDerivedType format.
308 Value *Elts[] = {
309 GetTagConstant(VMContext, dwarf::DW_TAG_pointer_type),
David Blaikiea13f3cd2013-03-19 23:25:22 +0000310 NULL, // Filename
Eric Christopher1fe3f9a2013-07-19 00:51:47 +0000311 NULL, // Unused
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000312 MDString::get(VMContext, Name),
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000313 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
314 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
315 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
316 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
317 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
318 PointeeTy
319 };
David Blaikie72dfb052013-03-28 02:44:59 +0000320 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel35fcd652010-11-04 15:01:38 +0000321}
322
Eric Christopher09b79812013-04-19 20:37:12 +0000323DIDerivedType DIBuilder::createMemberPointerType(DIType PointeeTy,
324 DIType Base) {
David Blaikie62fdfb52013-01-07 05:51:15 +0000325 // Pointer types are encoded in DIDerivedType format.
326 Value *Elts[] = {
327 GetTagConstant(VMContext, dwarf::DW_TAG_ptr_to_member_type),
David Blaikiea13f3cd2013-03-19 23:25:22 +0000328 NULL, // Filename
Eric Christopher1fe3f9a2013-07-19 00:51:47 +0000329 NULL, // Unused
David Blaikie62fdfb52013-01-07 05:51:15 +0000330 NULL,
David Blaikie62fdfb52013-01-07 05:51:15 +0000331 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
332 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
333 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
334 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
335 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
336 PointeeTy,
Manman Renbc660712013-09-05 18:48:31 +0000337 getTypeIdentifier(Base)
David Blaikie62fdfb52013-01-07 05:51:15 +0000338 };
David Blaikie72dfb052013-03-28 02:44:59 +0000339 return DIDerivedType(MDNode::get(VMContext, Elts));
David Blaikie62fdfb52013-01-07 05:51:15 +0000340}
341
Eric Christopher791e6292012-05-19 01:36:37 +0000342/// createReferenceType - Create debugging information entry for a reference
343/// type.
David Blaikied67c5ca2013-02-18 06:41:57 +0000344DIDerivedType DIBuilder::createReferenceType(unsigned Tag, DIType RTy) {
Manman Ren89c83b72013-07-01 21:02:01 +0000345 assert(RTy.isType() && "Unable to create reference type");
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000346 // References are encoded in DIDerivedType format.
347 Value *Elts[] = {
Eric Christopher791e6292012-05-19 01:36:37 +0000348 GetTagConstant(VMContext, Tag),
David Blaikiea13f3cd2013-03-19 23:25:22 +0000349 NULL, // Filename
Eric Christopher6c0046f2011-08-26 21:02:40 +0000350 NULL, // TheCU,
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000351 NULL, // Name
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000352 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
353 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
354 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
355 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
356 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
357 RTy
358 };
David Blaikie72dfb052013-03-28 02:44:59 +0000359 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel35fcd652010-11-04 15:01:38 +0000360}
361
Devang Patel50d280c2011-02-22 18:56:12 +0000362/// createTypedef - Create debugging information entry for a typedef.
David Blaikied67c5ca2013-02-18 06:41:57 +0000363DIDerivedType DIBuilder::createTypedef(DIType Ty, StringRef Name, DIFile File,
364 unsigned LineNo, DIDescriptor Context) {
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000365 // typedefs are encoded in DIDerivedType format.
Manman Ren89c83b72013-07-01 21:02:01 +0000366 assert(Ty.isType() && "Invalid typedef type!");
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000367 Value *Elts[] = {
368 GetTagConstant(VMContext, dwarf::DW_TAG_typedef),
David Blaikie4776bce2013-03-20 00:26:26 +0000369 File.getFileNode(),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000370 getNonCompileUnitScope(Context),
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000371 MDString::get(VMContext, Name),
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000372 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
373 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
374 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
375 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
376 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
377 Ty
378 };
David Blaikie72dfb052013-03-28 02:44:59 +0000379 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel35fcd652010-11-04 15:01:38 +0000380}
381
Devang Patel50d280c2011-02-22 18:56:12 +0000382/// createFriend - Create debugging information entry for a 'friend'.
Manman Ren576d49a2013-06-07 18:35:53 +0000383DIDerivedType DIBuilder::createFriend(DIType Ty, DIType FriendTy) {
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000384 // typedefs are encoded in DIDerivedType format.
Manman Ren89c83b72013-07-01 21:02:01 +0000385 assert(Ty.isType() && "Invalid type!");
386 assert(FriendTy.isType() && "Invalid friend type!");
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000387 Value *Elts[] = {
388 GetTagConstant(VMContext, dwarf::DW_TAG_friend),
David Blaikiea13f3cd2013-03-19 23:25:22 +0000389 NULL,
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000390 Ty,
391 NULL, // Name
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000392 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
393 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
394 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
395 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
396 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
397 FriendTy
398 };
Manman Ren576d49a2013-06-07 18:35:53 +0000399 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel35fcd652010-11-04 15:01:38 +0000400}
401
Devang Patel50d280c2011-02-22 18:56:12 +0000402/// createInheritance - Create debugging information entry to establish
Eric Christopherc27c7342011-09-12 19:58:22 +0000403/// inheritance relationship between two types.
David Blaikied67c5ca2013-02-18 06:41:57 +0000404DIDerivedType DIBuilder::createInheritance(
405 DIType Ty, DIType BaseTy, uint64_t BaseOffset, unsigned Flags) {
Manman Ren89c83b72013-07-01 21:02:01 +0000406 assert(Ty.isType() && "Unable to create inheritance");
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000407 // TAG_inheritance is encoded in DIDerivedType format.
408 Value *Elts[] = {
409 GetTagConstant(VMContext, dwarf::DW_TAG_inheritance),
David Blaikiea13f3cd2013-03-19 23:25:22 +0000410 NULL,
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000411 Ty,
412 NULL, // Name
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000413 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
414 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
415 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
416 ConstantInt::get(Type::getInt64Ty(VMContext), BaseOffset),
417 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
418 BaseTy
419 };
David Blaikie72dfb052013-03-28 02:44:59 +0000420 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel35fcd652010-11-04 15:01:38 +0000421}
422
Devang Patel50d280c2011-02-22 18:56:12 +0000423/// createMemberType - Create debugging information entry for a member.
David Blaikied67c5ca2013-02-18 06:41:57 +0000424DIDerivedType DIBuilder::createMemberType(
425 DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
426 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
427 unsigned Flags, DIType Ty) {
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000428 // TAG_member is encoded in DIDerivedType format.
429 Value *Elts[] = {
430 GetTagConstant(VMContext, dwarf::DW_TAG_member),
David Blaikie4776bce2013-03-20 00:26:26 +0000431 File.getFileNode(),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000432 getNonCompileUnitScope(Scope),
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000433 MDString::get(VMContext, Name),
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000434 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
435 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
436 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
437 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
438 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
439 Ty
440 };
David Blaikie72dfb052013-03-28 02:44:59 +0000441 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel35fcd652010-11-04 15:01:38 +0000442}
443
Eric Christopher6b6061f2013-01-16 01:22:23 +0000444/// createStaticMemberType - Create debugging information entry for a
445/// C++ static data member.
Manman Ren576d49a2013-06-07 18:35:53 +0000446DIDerivedType
447DIBuilder::createStaticMemberType(DIDescriptor Scope, StringRef Name,
448 DIFile File, unsigned LineNumber,
449 DIType Ty, unsigned Flags,
450 llvm::Value *Val) {
Eric Christopher6b6061f2013-01-16 01:22:23 +0000451 // TAG_member is encoded in DIDerivedType format.
452 Flags |= DIDescriptor::FlagStaticMember;
453 Value *Elts[] = {
454 GetTagConstant(VMContext, dwarf::DW_TAG_member),
David Blaikie4776bce2013-03-20 00:26:26 +0000455 File.getFileNode(),
Eric Christopher6b6061f2013-01-16 01:22:23 +0000456 getNonCompileUnitScope(Scope),
457 MDString::get(VMContext, Name),
Eric Christopher6b6061f2013-01-16 01:22:23 +0000458 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
459 ConstantInt::get(Type::getInt64Ty(VMContext), 0/*SizeInBits*/),
460 ConstantInt::get(Type::getInt64Ty(VMContext), 0/*AlignInBits*/),
461 ConstantInt::get(Type::getInt64Ty(VMContext), 0/*OffsetInBits*/),
462 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
463 Ty,
464 Val
465 };
Manman Ren576d49a2013-06-07 18:35:53 +0000466 return DIDerivedType(MDNode::get(VMContext, Elts));
Eric Christopher6b6061f2013-01-16 01:22:23 +0000467}
468
Devang Patele9db5e22011-04-16 00:11:51 +0000469/// createObjCIVar - Create debugging information entry for Objective-C
470/// instance variable.
Manman Ren576d49a2013-06-07 18:35:53 +0000471DIDerivedType
472DIBuilder::createObjCIVar(StringRef Name,
473 DIFile File, unsigned LineNumber,
474 uint64_t SizeInBits, uint64_t AlignInBits,
475 uint64_t OffsetInBits, unsigned Flags,
476 DIType Ty, StringRef PropertyName,
477 StringRef GetterName, StringRef SetterName,
478 unsigned PropertyAttributes) {
Devang Patele9db5e22011-04-16 00:11:51 +0000479 // TAG_member is encoded in DIDerivedType format.
480 Value *Elts[] = {
481 GetTagConstant(VMContext, dwarf::DW_TAG_member),
David Blaikie4776bce2013-03-20 00:26:26 +0000482 File.getFileNode(),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000483 getNonCompileUnitScope(File),
Devang Patele9db5e22011-04-16 00:11:51 +0000484 MDString::get(VMContext, Name),
Devang Patele9db5e22011-04-16 00:11:51 +0000485 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
486 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
487 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
488 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
489 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
490 Ty,
491 MDString::get(VMContext, PropertyName),
492 MDString::get(VMContext, GetterName),
493 MDString::get(VMContext, SetterName),
494 ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes)
495 };
Manman Ren576d49a2013-06-07 18:35:53 +0000496 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patele9db5e22011-04-16 00:11:51 +0000497}
498
Devang Patel6588abf2012-02-06 17:49:43 +0000499/// createObjCIVar - Create debugging information entry for Objective-C
500/// instance variable.
Manman Ren576d49a2013-06-07 18:35:53 +0000501DIDerivedType
502DIBuilder::createObjCIVar(StringRef Name,
503 DIFile File, unsigned LineNumber,
504 uint64_t SizeInBits, uint64_t AlignInBits,
505 uint64_t OffsetInBits, unsigned Flags,
506 DIType Ty, MDNode *PropertyNode) {
Devang Patel6588abf2012-02-06 17:49:43 +0000507 // TAG_member is encoded in DIDerivedType format.
508 Value *Elts[] = {
509 GetTagConstant(VMContext, dwarf::DW_TAG_member),
David Blaikie4776bce2013-03-20 00:26:26 +0000510 File.getFileNode(),
Devang Patel6588abf2012-02-06 17:49:43 +0000511 getNonCompileUnitScope(File),
512 MDString::get(VMContext, Name),
Devang Patel6588abf2012-02-06 17:49:43 +0000513 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
514 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
515 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
516 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
517 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
518 Ty,
519 PropertyNode
520 };
Manman Ren576d49a2013-06-07 18:35:53 +0000521 return DIDerivedType(MDNode::get(VMContext, Elts));
Devang Patel6588abf2012-02-06 17:49:43 +0000522}
523
Devang Patel1ea02d42012-02-04 00:59:25 +0000524/// createObjCProperty - Create debugging information entry for Objective-C
525/// property.
Eric Christopherb8ca9882012-03-29 08:42:56 +0000526DIObjCProperty DIBuilder::createObjCProperty(StringRef Name,
Eric Christopher87c06a82012-07-06 02:35:57 +0000527 DIFile File, unsigned LineNumber,
Devang Patel1ea02d42012-02-04 00:59:25 +0000528 StringRef GetterName,
Adrian Prantl2f445be2013-04-19 19:56:02 +0000529 StringRef SetterName,
Eric Christopherb8ca9882012-03-29 08:42:56 +0000530 unsigned PropertyAttributes,
Eric Christopher87c06a82012-07-06 02:35:57 +0000531 DIType Ty) {
Devang Patel1ea02d42012-02-04 00:59:25 +0000532 Value *Elts[] = {
Eric Christopher6c31ee22012-03-29 21:35:05 +0000533 GetTagConstant(VMContext, dwarf::DW_TAG_APPLE_property),
Devang Patel1ea02d42012-02-04 00:59:25 +0000534 MDString::get(VMContext, Name),
Eric Christopherb8ca9882012-03-29 08:42:56 +0000535 File,
536 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
Devang Patel1ea02d42012-02-04 00:59:25 +0000537 MDString::get(VMContext, GetterName),
538 MDString::get(VMContext, SetterName),
Eric Christopherb8ca9882012-03-29 08:42:56 +0000539 ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes),
540 Ty
Devang Patel1ea02d42012-02-04 00:59:25 +0000541 };
David Blaikie72dfb052013-03-28 02:44:59 +0000542 return DIObjCProperty(MDNode::get(VMContext, Elts));
Devang Patel1ea02d42012-02-04 00:59:25 +0000543}
544
Devang Patel50d280c2011-02-22 18:56:12 +0000545/// createTemplateTypeParameter - Create debugging information for template
Devang Patel7e2cb112011-02-02 21:38:25 +0000546/// type parameter.
Eric Christopher6c0046f2011-08-26 21:02:40 +0000547DITemplateTypeParameter
Devang Patel50d280c2011-02-22 18:56:12 +0000548DIBuilder::createTemplateTypeParameter(DIDescriptor Context, StringRef Name,
Devang Patel7e2cb112011-02-02 21:38:25 +0000549 DIType Ty, MDNode *File, unsigned LineNo,
550 unsigned ColumnNo) {
551 Value *Elts[] = {
552 GetTagConstant(VMContext, dwarf::DW_TAG_template_type_parameter),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000553 getNonCompileUnitScope(Context),
Devang Patel7e2cb112011-02-02 21:38:25 +0000554 MDString::get(VMContext, Name),
555 Ty,
556 File,
557 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
558 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
559 };
David Blaikie72dfb052013-03-28 02:44:59 +0000560 return DITemplateTypeParameter(MDNode::get(VMContext, Elts));
Devang Patel7e2cb112011-02-02 21:38:25 +0000561}
562
Eric Christopher6c0046f2011-08-26 21:02:40 +0000563DITemplateValueParameter
David Blaikiee88939c2013-06-22 18:59:11 +0000564DIBuilder::createTemplateValueParameter(unsigned Tag, DIDescriptor Context,
565 StringRef Name, DIType Ty,
566 Value *Val, MDNode *File,
567 unsigned LineNo,
Devang Patele7d93872011-02-02 22:35:53 +0000568 unsigned ColumnNo) {
569 Value *Elts[] = {
David Blaikiee88939c2013-06-22 18:59:11 +0000570 GetTagConstant(VMContext, Tag),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000571 getNonCompileUnitScope(Context),
Devang Patele7d93872011-02-02 22:35:53 +0000572 MDString::get(VMContext, Name),
573 Ty,
David Blaikie4de9d722013-05-10 21:52:07 +0000574 Val,
Devang Patele7d93872011-02-02 22:35:53 +0000575 File,
576 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
577 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
578 };
David Blaikie72dfb052013-03-28 02:44:59 +0000579 return DITemplateValueParameter(MDNode::get(VMContext, Elts));
Devang Patele7d93872011-02-02 22:35:53 +0000580}
581
David Blaikiee88939c2013-06-22 18:59:11 +0000582/// createTemplateValueParameter - Create debugging information for template
583/// value parameter.
584DITemplateValueParameter
585DIBuilder::createTemplateValueParameter(DIDescriptor Context, StringRef Name,
586 DIType Ty, Value *Val,
587 MDNode *File, unsigned LineNo,
588 unsigned ColumnNo) {
589 return createTemplateValueParameter(dwarf::DW_TAG_template_value_parameter,
590 Context, Name, Ty, Val, File, LineNo,
591 ColumnNo);
592}
593
594DITemplateValueParameter
595DIBuilder::createTemplateTemplateParameter(DIDescriptor Context, StringRef Name,
596 DIType Ty, StringRef Val,
597 MDNode *File, unsigned LineNo,
598 unsigned ColumnNo) {
599 return createTemplateValueParameter(
600 dwarf::DW_TAG_GNU_template_template_param, Context, Name, Ty,
601 MDString::get(VMContext, Val), File, LineNo, ColumnNo);
602}
603
604DITemplateValueParameter
605DIBuilder::createTemplateParameterPack(DIDescriptor Context, StringRef Name,
606 DIType Ty, DIArray Val,
607 MDNode *File, unsigned LineNo,
608 unsigned ColumnNo) {
609 return createTemplateValueParameter(dwarf::DW_TAG_GNU_template_parameter_pack,
610 Context, Name, Ty, Val, File, LineNo,
611 ColumnNo);
612}
613
Eric Christopher87c06a82012-07-06 02:35:57 +0000614/// createClassType - Create debugging information entry for a class.
David Blaikieca442a42013-03-26 23:46:39 +0000615DICompositeType DIBuilder::createClassType(DIDescriptor Context, StringRef Name,
616 DIFile File, unsigned LineNumber,
617 uint64_t SizeInBits,
618 uint64_t AlignInBits,
619 uint64_t OffsetInBits,
620 unsigned Flags, DIType DerivedFrom,
621 DIArray Elements,
622 MDNode *VTableHolder,
Manman Ren23f84cb2013-08-27 23:06:40 +0000623 MDNode *TemplateParams,
624 StringRef UniqueIdentifier) {
Manman Ren89c83b72013-07-01 21:02:01 +0000625 assert((!Context || Context.isScope() || Context.isType()) &&
David Blaikie66438682013-03-11 23:21:19 +0000626 "createClassType should be called with a valid Context");
627 // TAG_class_type is encoded in DICompositeType format.
Eric Christopher87c06a82012-07-06 02:35:57 +0000628 Value *Elts[] = {
629 GetTagConstant(VMContext, dwarf::DW_TAG_class_type),
David Blaikie4776bce2013-03-20 00:26:26 +0000630 File.getFileNode(),
Eric Christopher87c06a82012-07-06 02:35:57 +0000631 getNonCompileUnitScope(Context),
632 MDString::get(VMContext, Name),
Eric Christopher87c06a82012-07-06 02:35:57 +0000633 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
634 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
635 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
636 ConstantInt::get(Type::getInt32Ty(VMContext), OffsetInBits),
637 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
638 DerivedFrom,
639 Elements,
640 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
641 VTableHolder,
Manman Ren6e3cd0e2013-08-26 22:39:55 +0000642 TemplateParams,
Manman Ren23f84cb2013-08-27 23:06:40 +0000643 UniqueIdentifier.empty() ? NULL : MDString::get(VMContext, UniqueIdentifier)
Eric Christopher87c06a82012-07-06 02:35:57 +0000644 };
David Blaikieca442a42013-03-26 23:46:39 +0000645 DICompositeType R(MDNode::get(VMContext, Elts));
Manman Ren89c83b72013-07-01 21:02:01 +0000646 assert(R.isCompositeType() &&
647 "createClassType should return a DICompositeType");
Manman Ren3a0e9b52013-08-29 23:17:54 +0000648 if (!UniqueIdentifier.empty())
649 retainType(R);
David Blaikie66438682013-03-11 23:21:19 +0000650 return R;
Eric Christopher87c06a82012-07-06 02:35:57 +0000651}
652
Devang Patel50d280c2011-02-22 18:56:12 +0000653/// createStructType - Create debugging information entry for a struct.
David Blaikie6172f022013-02-25 01:07:18 +0000654DICompositeType DIBuilder::createStructType(DIDescriptor Context,
655 StringRef Name, DIFile File,
656 unsigned LineNumber,
657 uint64_t SizeInBits,
658 uint64_t AlignInBits,
659 unsigned Flags, DIType DerivedFrom,
660 DIArray Elements,
661 unsigned RunTimeLang,
Manman Ren23f84cb2013-08-27 23:06:40 +0000662 MDNode *VTableHolder,
663 StringRef UniqueIdentifier) {
Devang Patelfe58f952010-12-07 23:25:47 +0000664 // TAG_structure_type is encoded in DICompositeType format.
665 Value *Elts[] = {
666 GetTagConstant(VMContext, dwarf::DW_TAG_structure_type),
David Blaikie4776bce2013-03-20 00:26:26 +0000667 File.getFileNode(),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000668 getNonCompileUnitScope(Context),
Devang Patelfe58f952010-12-07 23:25:47 +0000669 MDString::get(VMContext, Name),
Devang Patelfe58f952010-12-07 23:25:47 +0000670 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
671 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
672 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
Devang Patel43c249c2010-12-08 01:50:15 +0000673 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
Devang Patelfe58f952010-12-07 23:25:47 +0000674 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
David Blaikie6172f022013-02-25 01:07:18 +0000675 DerivedFrom,
Devang Patelfe58f952010-12-07 23:25:47 +0000676 Elements,
677 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
David Blaikie6172f022013-02-25 01:07:18 +0000678 VTableHolder,
David Blaikied4f92fd2013-02-18 07:27:30 +0000679 NULL,
Manman Ren23f84cb2013-08-27 23:06:40 +0000680 UniqueIdentifier.empty() ? NULL : MDString::get(VMContext, UniqueIdentifier)
Devang Patelfe58f952010-12-07 23:25:47 +0000681 };
David Blaikie66438682013-03-11 23:21:19 +0000682 DICompositeType R(MDNode::get(VMContext, Elts));
Manman Ren89c83b72013-07-01 21:02:01 +0000683 assert(R.isCompositeType() &&
684 "createStructType should return a DICompositeType");
Manman Ren3a0e9b52013-08-29 23:17:54 +0000685 if (!UniqueIdentifier.empty())
686 retainType(R);
David Blaikie66438682013-03-11 23:21:19 +0000687 return R;
Devang Patelfe58f952010-12-07 23:25:47 +0000688}
689
Devang Patel50d280c2011-02-22 18:56:12 +0000690/// createUnionType - Create debugging information entry for an union.
Eric Christophercf0623b2013-04-02 22:55:52 +0000691DICompositeType DIBuilder::createUnionType(DIDescriptor Scope, StringRef Name,
692 DIFile File, unsigned LineNumber,
693 uint64_t SizeInBits,
694 uint64_t AlignInBits, unsigned Flags,
695 DIArray Elements,
Manman Ren23f84cb2013-08-27 23:06:40 +0000696 unsigned RunTimeLang,
697 StringRef UniqueIdentifier) {
Devang Patel43c249c2010-12-08 01:50:15 +0000698 // TAG_union_type is encoded in DICompositeType format.
699 Value *Elts[] = {
700 GetTagConstant(VMContext, dwarf::DW_TAG_union_type),
David Blaikie4776bce2013-03-20 00:26:26 +0000701 File.getFileNode(),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000702 getNonCompileUnitScope(Scope),
Devang Patel43c249c2010-12-08 01:50:15 +0000703 MDString::get(VMContext, Name),
Devang Patel43c249c2010-12-08 01:50:15 +0000704 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
705 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
706 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
707 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
708 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patelc104cf22011-12-16 17:51:31 +0000709 NULL,
Devang Patel43c249c2010-12-08 01:50:15 +0000710 Elements,
711 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
Manman Ren6e3cd0e2013-08-26 22:39:55 +0000712 NULL,
713 NULL,
Manman Ren23f84cb2013-08-27 23:06:40 +0000714 UniqueIdentifier.empty() ? NULL : MDString::get(VMContext, UniqueIdentifier)
Devang Patel43c249c2010-12-08 01:50:15 +0000715 };
Manman Ren3a0e9b52013-08-29 23:17:54 +0000716 DICompositeType R(MDNode::get(VMContext, Elts));
717 if (!UniqueIdentifier.empty())
718 retainType(R);
719 return R;
Devang Patel43c249c2010-12-08 01:50:15 +0000720}
721
Devang Patel50d280c2011-02-22 18:56:12 +0000722/// createSubroutineType - Create subroutine type.
David Blaikied67c5ca2013-02-18 06:41:57 +0000723DICompositeType
724DIBuilder::createSubroutineType(DIFile File, DIArray ParameterTypes) {
Devang Patel43c249c2010-12-08 01:50:15 +0000725 // TAG_subroutine_type is encoded in DICompositeType format.
726 Value *Elts[] = {
727 GetTagConstant(VMContext, dwarf::DW_TAG_subroutine_type),
Bill Wendlingf46b4972012-07-06 17:47:36 +0000728 Constant::getNullValue(Type::getInt32Ty(VMContext)),
Bill Wendlingf46b4972012-07-06 17:47:36 +0000729 Constant::getNullValue(Type::getInt32Ty(VMContext)),
David Blaikiea13f3cd2013-03-19 23:25:22 +0000730 MDString::get(VMContext, ""),
Devang Patel43c249c2010-12-08 01:50:15 +0000731 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
732 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
733 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
Devang Patelc104cf22011-12-16 17:51:31 +0000734 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
Devang Patel43c249c2010-12-08 01:50:15 +0000735 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
Devang Patelc104cf22011-12-16 17:51:31 +0000736 NULL,
Devang Patel43c249c2010-12-08 01:50:15 +0000737 ParameterTypes,
738 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
Manman Ren6e3cd0e2013-08-26 22:39:55 +0000739 NULL,
740 NULL,
741 NULL // Type Identifer
Devang Patel43c249c2010-12-08 01:50:15 +0000742 };
David Blaikie72dfb052013-03-28 02:44:59 +0000743 return DICompositeType(MDNode::get(VMContext, Elts));
Devang Patel43c249c2010-12-08 01:50:15 +0000744}
745
Eric Christopher6c0046f2011-08-26 21:02:40 +0000746/// createEnumerationType - Create debugging information entry for an
Devang Patel43c249c2010-12-08 01:50:15 +0000747/// enumeration.
David Blaikied67c5ca2013-02-18 06:41:57 +0000748DICompositeType DIBuilder::createEnumerationType(
749 DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
750 uint64_t SizeInBits, uint64_t AlignInBits, DIArray Elements,
Manman Ren23f84cb2013-08-27 23:06:40 +0000751 DIType UnderlyingType, StringRef UniqueIdentifier) {
Devang Patel43c249c2010-12-08 01:50:15 +0000752 // TAG_enumeration_type is encoded in DICompositeType format.
753 Value *Elts[] = {
754 GetTagConstant(VMContext, dwarf::DW_TAG_enumeration_type),
David Blaikie4776bce2013-03-20 00:26:26 +0000755 File.getFileNode(),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000756 getNonCompileUnitScope(Scope),
Devang Patel43c249c2010-12-08 01:50:15 +0000757 MDString::get(VMContext, Name),
Devang Patel43c249c2010-12-08 01:50:15 +0000758 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
759 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
760 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
761 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
Eli Friedmance3da6f2012-10-05 01:49:14 +0000762 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
Adrian Prantl2f445be2013-04-19 19:56:02 +0000763 UnderlyingType,
Devang Patel43c249c2010-12-08 01:50:15 +0000764 Elements,
765 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
Manman Ren6e3cd0e2013-08-26 22:39:55 +0000766 NULL,
767 NULL,
Manman Ren23f84cb2013-08-27 23:06:40 +0000768 UniqueIdentifier.empty() ? NULL : MDString::get(VMContext, UniqueIdentifier)
Devang Patel43c249c2010-12-08 01:50:15 +0000769 };
Devang Patel1f48a952011-04-18 23:51:03 +0000770 MDNode *Node = MDNode::get(VMContext, Elts);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000771 AllEnumTypes.push_back(Node);
Manman Ren3a0e9b52013-08-29 23:17:54 +0000772 if (!UniqueIdentifier.empty())
773 retainType(Node);
David Blaikie72dfb052013-03-28 02:44:59 +0000774 return DICompositeType(Node);
Devang Patel43c249c2010-12-08 01:50:15 +0000775}
776
Devang Patel50d280c2011-02-22 18:56:12 +0000777/// createArrayType - Create debugging information entry for an array.
David Blaikied67c5ca2013-02-18 06:41:57 +0000778DICompositeType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
779 DIType Ty, DIArray Subscripts) {
Devang Patel43c249c2010-12-08 01:50:15 +0000780 // TAG_array_type is encoded in DICompositeType format.
781 Value *Elts[] = {
782 GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
David Blaikiea13f3cd2013-03-19 23:25:22 +0000783 NULL, // Filename/Directory,
Eric Christopher1fe3f9a2013-07-19 00:51:47 +0000784 NULL, // Unused
Devang Patel43c249c2010-12-08 01:50:15 +0000785 MDString::get(VMContext, ""),
Devang Patel43c249c2010-12-08 01:50:15 +0000786 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
787 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
788 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
789 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
790 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
791 Ty,
792 Subscripts,
793 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
Manman Ren6e3cd0e2013-08-26 22:39:55 +0000794 NULL,
795 NULL,
796 NULL // Type Identifer
Devang Patel43c249c2010-12-08 01:50:15 +0000797 };
David Blaikie72dfb052013-03-28 02:44:59 +0000798 return DICompositeType(MDNode::get(VMContext, Elts));
Devang Patel43c249c2010-12-08 01:50:15 +0000799}
800
Devang Patel50d280c2011-02-22 18:56:12 +0000801/// createVectorType - Create debugging information entry for a vector.
Manman Ren37bfb182013-06-07 03:13:46 +0000802DICompositeType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits,
803 DIType Ty, DIArray Subscripts) {
Eric Christopher9a1e0e22013-01-08 01:53:52 +0000804 // A vector is an array type with the FlagVector flag applied.
Devang Patel43c249c2010-12-08 01:50:15 +0000805 Value *Elts[] = {
Eric Christopher9a1e0e22013-01-08 01:53:52 +0000806 GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
David Blaikiea13f3cd2013-03-19 23:25:22 +0000807 NULL, // Filename/Directory,
Eric Christopher1fe3f9a2013-07-19 00:51:47 +0000808 NULL, // Unused
Devang Patel43c249c2010-12-08 01:50:15 +0000809 MDString::get(VMContext, ""),
Devang Patel43c249c2010-12-08 01:50:15 +0000810 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
811 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
812 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
813 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
Eric Christopher9a1e0e22013-01-08 01:53:52 +0000814 ConstantInt::get(Type::getInt32Ty(VMContext), DIType::FlagVector),
Devang Patel43c249c2010-12-08 01:50:15 +0000815 Ty,
816 Subscripts,
817 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
Manman Ren6e3cd0e2013-08-26 22:39:55 +0000818 NULL,
819 NULL,
820 NULL // Type Identifer
Devang Patel43c249c2010-12-08 01:50:15 +0000821 };
Manman Ren37bfb182013-06-07 03:13:46 +0000822 return DICompositeType(MDNode::get(VMContext, Elts));
Devang Patel43c249c2010-12-08 01:50:15 +0000823}
Devang Patelfe58f952010-12-07 23:25:47 +0000824
Devang Patel50d280c2011-02-22 18:56:12 +0000825/// createArtificialType - Create a new DIType with "artificial" flag set.
826DIType DIBuilder::createArtificialType(DIType Ty) {
Devang Patel35fcd652010-11-04 15:01:38 +0000827 if (Ty.isArtificial())
828 return Ty;
829
830 SmallVector<Value *, 9> Elts;
831 MDNode *N = Ty;
832 assert (N && "Unexpected input DIType!");
833 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
834 if (Value *V = N->getOperand(i))
835 Elts.push_back(V);
836 else
837 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
838 }
839
840 unsigned CurFlags = Ty.getFlags();
841 CurFlags = CurFlags | DIType::FlagArtificial;
842
843 // Flags are stored at this slot.
David Blaikie72dfb052013-03-28 02:44:59 +0000844 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
Devang Patel35fcd652010-11-04 15:01:38 +0000845
David Blaikie72dfb052013-03-28 02:44:59 +0000846 return DIType(MDNode::get(VMContext, Elts));
Devang Patel35fcd652010-11-04 15:01:38 +0000847}
Devang Patelfe58f952010-12-07 23:25:47 +0000848
Eric Christopher1f55eb42013-01-08 01:53:42 +0000849/// createObjectPointerType - Create a new type with both the object pointer
850/// and artificial flags set.
Eric Christophere5212782012-09-12 23:36:19 +0000851DIType DIBuilder::createObjectPointerType(DIType Ty) {
852 if (Ty.isObjectPointer())
853 return Ty;
854
855 SmallVector<Value *, 9> Elts;
856 MDNode *N = Ty;
857 assert (N && "Unexpected input DIType!");
858 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
859 if (Value *V = N->getOperand(i))
860 Elts.push_back(V);
861 else
862 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
863 }
864
865 unsigned CurFlags = Ty.getFlags();
866 CurFlags = CurFlags | (DIType::FlagObjectPointer | DIType::FlagArtificial);
867
868 // Flags are stored at this slot.
David Blaikie72dfb052013-03-28 02:44:59 +0000869 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
Eric Christophere5212782012-09-12 23:36:19 +0000870
David Blaikie72dfb052013-03-28 02:44:59 +0000871 return DIType(MDNode::get(VMContext, Elts));
Eric Christophere5212782012-09-12 23:36:19 +0000872}
873
Eric Christopher6c0046f2011-08-26 21:02:40 +0000874/// retainType - Retain DIType in a module even if it is not referenced
Devang Patel43c249c2010-12-08 01:50:15 +0000875/// through debug info anchors.
Devang Patel50d280c2011-02-22 18:56:12 +0000876void DIBuilder::retainType(DIType T) {
Manman Ren3a0e9b52013-08-29 23:17:54 +0000877 AllRetainTypes.push_back(TrackingVH<MDNode>(T));
Devang Patel43c249c2010-12-08 01:50:15 +0000878}
879
Devang Patel50d280c2011-02-22 18:56:12 +0000880/// createUnspecifiedParameter - Create unspeicified type descriptor
Devang Patel43c249c2010-12-08 01:50:15 +0000881/// for the subroutine type.
Devang Patel50d280c2011-02-22 18:56:12 +0000882DIDescriptor DIBuilder::createUnspecifiedParameter() {
Eric Christopher6c0046f2011-08-26 21:02:40 +0000883 Value *Elts[] = {
884 GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_parameters)
Devang Patel43c249c2010-12-08 01:50:15 +0000885 };
Devang Patel1f48a952011-04-18 23:51:03 +0000886 return DIDescriptor(MDNode::get(VMContext, Elts));
Devang Patel43c249c2010-12-08 01:50:15 +0000887}
888
Eric Christopher4fe34572012-02-08 00:22:26 +0000889/// createForwardDecl - Create a temporary forward-declared type that
890/// can be RAUW'd if the full type is seen.
David Blaikie692062f2013-08-16 20:42:14 +0000891DICompositeType DIBuilder::createForwardDecl(unsigned Tag, StringRef Name,
Eric Christopher216432d2012-04-23 19:00:11 +0000892 DIDescriptor Scope, DIFile F,
Eli Friedmance3da6f2012-10-05 01:49:14 +0000893 unsigned Line, unsigned RuntimeLang,
894 uint64_t SizeInBits,
Manman Ren23f84cb2013-08-27 23:06:40 +0000895 uint64_t AlignInBits,
896 StringRef UniqueIdentifier) {
Eric Christopher4fe34572012-02-08 00:22:26 +0000897 // Create a temporary MDNode.
898 Value *Elts[] = {
899 GetTagConstant(VMContext, Tag),
David Blaikie4776bce2013-03-20 00:26:26 +0000900 F.getFileNode(),
Eric Christopher216432d2012-04-23 19:00:11 +0000901 getNonCompileUnitScope(Scope),
Eric Christopher4fe34572012-02-08 00:22:26 +0000902 MDString::get(VMContext, Name),
Eric Christopher4fe34572012-02-08 00:22:26 +0000903 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
Eli Friedmance3da6f2012-10-05 01:49:14 +0000904 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
905 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
Eric Christopher4fe34572012-02-08 00:22:26 +0000906 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
907 ConstantInt::get(Type::getInt32Ty(VMContext),
Eric Christopher9f90e872012-02-20 18:04:14 +0000908 DIDescriptor::FlagFwdDecl),
909 NULL,
910 DIArray(),
David Blaikie692062f2013-08-16 20:42:14 +0000911 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
Manman Ren6e3cd0e2013-08-26 22:39:55 +0000912 NULL,
913 NULL, //TemplateParams
Manman Ren23f84cb2013-08-27 23:06:40 +0000914 UniqueIdentifier.empty() ? NULL : MDString::get(VMContext, UniqueIdentifier)
Eric Christopher4fe34572012-02-08 00:22:26 +0000915 };
916 MDNode *Node = MDNode::getTemporary(VMContext, Elts);
David Blaikie692062f2013-08-16 20:42:14 +0000917 DICompositeType RetTy(Node);
918 assert(RetTy.isCompositeType() &&
Manman Ren89c83b72013-07-01 21:02:01 +0000919 "createForwardDecl result should be a DIType");
Manman Ren3a0e9b52013-08-29 23:17:54 +0000920 if (!UniqueIdentifier.empty())
921 retainType(RetTy);
Manman Ren88328d22013-07-02 18:37:35 +0000922 return RetTy;
Eric Christopher4fe34572012-02-08 00:22:26 +0000923}
924
Devang Patel50d280c2011-02-22 18:56:12 +0000925/// getOrCreateArray - Get a DIArray, create one if required.
Jay Foad68550182011-04-24 10:11:03 +0000926DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
927 if (Elements.empty()) {
Bill Wendlingf46b4972012-07-06 17:47:36 +0000928 Value *Null = Constant::getNullValue(Type::getInt32Ty(VMContext));
Jay Foadec9186b2011-04-21 19:59:31 +0000929 return DIArray(MDNode::get(VMContext, Null));
Devang Patelfe58f952010-12-07 23:25:47 +0000930 }
Jay Foad68550182011-04-24 10:11:03 +0000931 return DIArray(MDNode::get(VMContext, Elements));
Devang Patelfe58f952010-12-07 23:25:47 +0000932}
933
Devang Patel50d280c2011-02-22 18:56:12 +0000934/// getOrCreateSubrange - Create a descriptor for a value range. This
Devang Patel43c249c2010-12-08 01:50:15 +0000935/// implicitly uniques the values returned.
Bill Wendling9493dae2012-12-04 21:34:03 +0000936DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
Devang Patel43c249c2010-12-08 01:50:15 +0000937 Value *Elts[] = {
938 GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
939 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
Bill Wendlinga7645a32012-12-04 06:20:49 +0000940 ConstantInt::get(Type::getInt64Ty(VMContext), Count)
Devang Patel43c249c2010-12-08 01:50:15 +0000941 };
942
Devang Patel1f48a952011-04-18 23:51:03 +0000943 return DISubrange(MDNode::get(VMContext, Elts));
Devang Patel43c249c2010-12-08 01:50:15 +0000944}
945
David Blaikie3fac43d2013-03-20 17:49:48 +0000946/// \brief Create a new descriptor for the specified global.
Devang Patelfe58f952010-12-07 23:25:47 +0000947DIGlobalVariable DIBuilder::
David Blaikie3fac43d2013-03-20 17:49:48 +0000948createGlobalVariable(StringRef Name, StringRef LinkageName, DIFile F,
949 unsigned LineNumber, DIType Ty, bool isLocalToUnit,
950 Value *Val) {
Devang Patelfe58f952010-12-07 23:25:47 +0000951 Value *Elts[] = {
952 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
Bill Wendlingf46b4972012-07-06 17:47:36 +0000953 Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000954 NULL, // TheCU,
Devang Patelfe58f952010-12-07 23:25:47 +0000955 MDString::get(VMContext, Name),
956 MDString::get(VMContext, Name),
David Blaikie3fac43d2013-03-20 17:49:48 +0000957 MDString::get(VMContext, LinkageName),
Devang Patelfe58f952010-12-07 23:25:47 +0000958 F,
959 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
960 Ty,
961 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
962 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
Eric Christopher6b6061f2013-01-16 01:22:23 +0000963 Val,
964 DIDescriptor()
Devang Patelfe58f952010-12-07 23:25:47 +0000965 };
Devang Patel1f48a952011-04-18 23:51:03 +0000966 MDNode *Node = MDNode::get(VMContext, Elts);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000967 AllGVs.push_back(Node);
Devang Patelfe58f952010-12-07 23:25:47 +0000968 return DIGlobalVariable(Node);
969}
970
David Blaikie3fac43d2013-03-20 17:49:48 +0000971/// \brief Create a new descriptor for the specified global.
972DIGlobalVariable DIBuilder::
973createGlobalVariable(StringRef Name, DIFile F, unsigned LineNumber,
974 DIType Ty, bool isLocalToUnit, Value *Val) {
975 return createGlobalVariable(Name, Name, F, LineNumber, Ty, isLocalToUnit,
976 Val);
977}
978
Devang Patel50d280c2011-02-22 18:56:12 +0000979/// createStaticVariable - Create a new descriptor for the specified static
Devang Patelfe58f952010-12-07 23:25:47 +0000980/// variable.
981DIGlobalVariable DIBuilder::
Eric Christopher6c0046f2011-08-26 21:02:40 +0000982createStaticVariable(DIDescriptor Context, StringRef Name,
983 StringRef LinkageName, DIFile F, unsigned LineNumber,
Eric Christopher6b6061f2013-01-16 01:22:23 +0000984 DIType Ty, bool isLocalToUnit, Value *Val, MDNode *Decl) {
Devang Patelfe58f952010-12-07 23:25:47 +0000985 Value *Elts[] = {
986 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
Bill Wendlingf46b4972012-07-06 17:47:36 +0000987 Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel94c7ddb2011-08-16 22:09:43 +0000988 getNonCompileUnitScope(Context),
Devang Patelfe58f952010-12-07 23:25:47 +0000989 MDString::get(VMContext, Name),
990 MDString::get(VMContext, Name),
991 MDString::get(VMContext, LinkageName),
992 F,
993 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
994 Ty,
995 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
996 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
Eric Christopher6b6061f2013-01-16 01:22:23 +0000997 Val,
998 DIDescriptor(Decl)
Devang Patelfe58f952010-12-07 23:25:47 +0000999 };
Devang Patel1f48a952011-04-18 23:51:03 +00001000 MDNode *Node = MDNode::get(VMContext, Elts);
Devang Patel94c7ddb2011-08-16 22:09:43 +00001001 AllGVs.push_back(Node);
Devang Patelfe58f952010-12-07 23:25:47 +00001002 return DIGlobalVariable(Node);
1003}
1004
Devang Patel50d280c2011-02-22 18:56:12 +00001005/// createVariable - Create a new descriptor for the specified variable.
1006DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
Devang Patel48f17ba2010-12-07 23:58:00 +00001007 StringRef Name, DIFile File,
Eric Christopher6c0046f2011-08-26 21:02:40 +00001008 unsigned LineNo, DIType Ty,
Devang Patele9e16c52011-03-01 22:58:13 +00001009 bool AlwaysPreserve, unsigned Flags,
1010 unsigned ArgNo) {
David Blaikie66438682013-03-11 23:21:19 +00001011 DIDescriptor Context(getNonCompileUnitScope(Scope));
Manman Ren89c83b72013-07-01 21:02:01 +00001012 assert((!Context || Context.isScope()) &&
David Blaikie66438682013-03-11 23:21:19 +00001013 "createLocalVariable should be called with a valid Context");
Manman Ren89c83b72013-07-01 21:02:01 +00001014 assert(Ty.isType() &&
David Blaikie66438682013-03-11 23:21:19 +00001015 "createLocalVariable should be called with a valid type");
Devang Patel48f17ba2010-12-07 23:58:00 +00001016 Value *Elts[] = {
1017 GetTagConstant(VMContext, Tag),
Devang Patel94c7ddb2011-08-16 22:09:43 +00001018 getNonCompileUnitScope(Scope),
Devang Patel48f17ba2010-12-07 23:58:00 +00001019 MDString::get(VMContext, Name),
1020 File,
Devang Patele9e16c52011-03-01 22:58:13 +00001021 ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))),
Devang Patel48f17ba2010-12-07 23:58:00 +00001022 Ty,
Devang Patel23336b42011-07-19 19:41:54 +00001023 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Bill Wendling9fdb7c02012-07-06 17:49:19 +00001024 Constant::getNullValue(Type::getInt32Ty(VMContext))
Devang Patel48f17ba2010-12-07 23:58:00 +00001025 };
Devang Patel1f48a952011-04-18 23:51:03 +00001026 MDNode *Node = MDNode::get(VMContext, Elts);
Devang Patel48f17ba2010-12-07 23:58:00 +00001027 if (AlwaysPreserve) {
1028 // The optimizer may remove local variable. If there is an interest
1029 // to preserve variable info in such situation then stash it in a
1030 // named mdnode.
1031 DISubprogram Fn(getDISubprogram(Scope));
Devang Patel93d39be2011-08-19 23:28:12 +00001032 NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, Fn);
Devang Patel48f17ba2010-12-07 23:58:00 +00001033 FnLocals->addOperand(Node);
1034 }
Manman Ren88328d22013-07-02 18:37:35 +00001035 DIVariable RetVar(Node);
1036 assert(RetVar.isVariable() &&
Manman Ren89c83b72013-07-01 21:02:01 +00001037 "createLocalVariable should return a valid DIVariable");
Manman Ren88328d22013-07-02 18:37:35 +00001038 return RetVar;
Devang Patel48f17ba2010-12-07 23:58:00 +00001039}
1040
Devang Patel50d280c2011-02-22 18:56:12 +00001041/// createComplexVariable - Create a new descriptor for the specified variable
Devang Patelfe58f952010-12-07 23:25:47 +00001042/// which has a complex address expression for its address.
Devang Patel50d280c2011-02-22 18:56:12 +00001043DIVariable DIBuilder::createComplexVariable(unsigned Tag, DIDescriptor Scope,
Devang Patelfe58f952010-12-07 23:25:47 +00001044 StringRef Name, DIFile F,
1045 unsigned LineNo,
Jay Foad68550182011-04-24 10:11:03 +00001046 DIType Ty, ArrayRef<Value *> Addr,
1047 unsigned ArgNo) {
Devang Patelfe58f952010-12-07 23:25:47 +00001048 SmallVector<Value *, 15> Elts;
1049 Elts.push_back(GetTagConstant(VMContext, Tag));
Devang Patel94c7ddb2011-08-16 22:09:43 +00001050 Elts.push_back(getNonCompileUnitScope(Scope)),
Devang Patelfe58f952010-12-07 23:25:47 +00001051 Elts.push_back(MDString::get(VMContext, Name));
1052 Elts.push_back(F);
Eric Christopher6c0046f2011-08-26 21:02:40 +00001053 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext),
Devang Patel9f997212012-02-08 00:17:07 +00001054 (LineNo | (ArgNo << 24))));
Devang Patelfe58f952010-12-07 23:25:47 +00001055 Elts.push_back(Ty);
Bill Wendlingf46b4972012-07-06 17:47:36 +00001056 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
1057 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
Jay Foad68550182011-04-24 10:11:03 +00001058 Elts.append(Addr.begin(), Addr.end());
Devang Patelfe58f952010-12-07 23:25:47 +00001059
Devang Patel1f48a952011-04-18 23:51:03 +00001060 return DIVariable(MDNode::get(VMContext, Elts));
Devang Patelfe58f952010-12-07 23:25:47 +00001061}
1062
Devang Patel50d280c2011-02-22 18:56:12 +00001063/// createFunction - Create a new descriptor for the specified function.
1064DISubprogram DIBuilder::createFunction(DIDescriptor Context,
Devang Patel44498a62010-12-08 20:42:44 +00001065 StringRef Name,
1066 StringRef LinkageName,
1067 DIFile File, unsigned LineNo,
David Blaikie3d331842013-05-22 23:22:18 +00001068 DICompositeType Ty,
Devang Patel44498a62010-12-08 20:42:44 +00001069 bool isLocalToUnit, bool isDefinition,
Eric Christopher6126a1e2012-04-03 00:43:49 +00001070 unsigned ScopeLine,
Devang Patel44498a62010-12-08 20:42:44 +00001071 unsigned Flags, bool isOptimized,
Devang Patelda194752011-04-05 22:52:06 +00001072 Function *Fn,
Devang Patel5e06bb82011-04-22 23:10:17 +00001073 MDNode *TParams,
1074 MDNode *Decl) {
David Blaikie3d331842013-05-22 23:22:18 +00001075 assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
1076 "function types should be subroutines");
Devang Patel93d39be2011-08-19 23:28:12 +00001077 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
Devang Patel44498a62010-12-08 20:42:44 +00001078 Value *Elts[] = {
1079 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
David Blaikiebb4e6192013-03-21 23:08:34 +00001080 File.getFileNode(),
Devang Patel94c7ddb2011-08-16 22:09:43 +00001081 getNonCompileUnitScope(Context),
Devang Patel44498a62010-12-08 20:42:44 +00001082 MDString::get(VMContext, Name),
1083 MDString::get(VMContext, Name),
1084 MDString::get(VMContext, LinkageName),
Devang Patel44498a62010-12-08 20:42:44 +00001085 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1086 Ty,
1087 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1088 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1089 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
1090 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
Devang Patel9642c572011-12-15 17:55:56 +00001091 NULL,
Devang Patel44498a62010-12-08 20:42:44 +00001092 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1093 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patelda194752011-04-05 22:52:06 +00001094 Fn,
Devang Patel5e06bb82011-04-22 23:10:17 +00001095 TParams,
Devang Patel93d39be2011-08-19 23:28:12 +00001096 Decl,
David Blaikief839eed2013-02-04 05:56:36 +00001097 MDNode::getTemporary(VMContext, TElts),
Eric Christopher6126a1e2012-04-03 00:43:49 +00001098 ConstantInt::get(Type::getInt32Ty(VMContext), ScopeLine)
Devang Patel44498a62010-12-08 20:42:44 +00001099 };
Devang Patel1f48a952011-04-18 23:51:03 +00001100 MDNode *Node = MDNode::get(VMContext, Elts);
Devang Patel44498a62010-12-08 20:42:44 +00001101
1102 // Create a named metadata so that we do not lose this mdnode.
David Blaikie139f7e52013-02-18 07:10:22 +00001103 if (isDefinition)
1104 AllSubprograms.push_back(Node);
David Blaikieebb51832013-03-21 20:28:52 +00001105 DISubprogram S(Node);
Manman Ren89c83b72013-07-01 21:02:01 +00001106 assert(S.isSubprogram() && "createFunction should return a valid DISubprogram");
David Blaikieebb51832013-03-21 20:28:52 +00001107 return S;
Devang Patel44498a62010-12-08 20:42:44 +00001108}
1109
Devang Patel50d280c2011-02-22 18:56:12 +00001110/// createMethod - Create a new descriptor for the specified C++ method.
1111DISubprogram DIBuilder::createMethod(DIDescriptor Context,
Devang Patel44498a62010-12-08 20:42:44 +00001112 StringRef Name,
1113 StringRef LinkageName,
1114 DIFile F,
David Blaikie3d331842013-05-22 23:22:18 +00001115 unsigned LineNo, DICompositeType Ty,
Devang Patel44498a62010-12-08 20:42:44 +00001116 bool isLocalToUnit,
1117 bool isDefinition,
1118 unsigned VK, unsigned VIndex,
1119 MDNode *VTableHolder,
1120 unsigned Flags,
1121 bool isOptimized,
Devang Patelda194752011-04-05 22:52:06 +00001122 Function *Fn,
1123 MDNode *TParam) {
David Blaikie3d331842013-05-22 23:22:18 +00001124 assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
1125 "function types should be subroutines");
Devang Patel93d39be2011-08-19 23:28:12 +00001126 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
Devang Patel44498a62010-12-08 20:42:44 +00001127 Value *Elts[] = {
1128 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
David Blaikiebb4e6192013-03-21 23:08:34 +00001129 F.getFileNode(),
Devang Patel94c7ddb2011-08-16 22:09:43 +00001130 getNonCompileUnitScope(Context),
Devang Patel44498a62010-12-08 20:42:44 +00001131 MDString::get(VMContext, Name),
1132 MDString::get(VMContext, Name),
1133 MDString::get(VMContext, LinkageName),
Devang Patel44498a62010-12-08 20:42:44 +00001134 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1135 Ty,
1136 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1137 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1138 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
1139 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
1140 VTableHolder,
1141 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1142 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patelda194752011-04-05 22:52:06 +00001143 Fn,
1144 TParam,
Bill Wendlingf46b4972012-07-06 17:47:36 +00001145 Constant::getNullValue(Type::getInt32Ty(VMContext)),
David Blaikief839eed2013-02-04 05:56:36 +00001146 MDNode::getTemporary(VMContext, TElts),
Eric Christopher25016522012-05-18 00:16:22 +00001147 // FIXME: Do we want to use different scope/lines?
Eric Christopher6126a1e2012-04-03 00:43:49 +00001148 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
Devang Patel44498a62010-12-08 20:42:44 +00001149 };
Devang Patel1f48a952011-04-18 23:51:03 +00001150 MDNode *Node = MDNode::get(VMContext, Elts);
David Blaikie139f7e52013-02-18 07:10:22 +00001151 if (isDefinition)
1152 AllSubprograms.push_back(Node);
David Blaikieebb51832013-03-21 20:28:52 +00001153 DISubprogram S(Node);
Manman Ren89c83b72013-07-01 21:02:01 +00001154 assert(S.isSubprogram() && "createMethod should return a valid DISubprogram");
David Blaikieebb51832013-03-21 20:28:52 +00001155 return S;
Devang Patel44498a62010-12-08 20:42:44 +00001156}
1157
Devang Patel50d280c2011-02-22 18:56:12 +00001158/// createNameSpace - This creates new descriptor for a namespace
Devang Patelfe58f952010-12-07 23:25:47 +00001159/// with the specified parent scope.
Devang Patel50d280c2011-02-22 18:56:12 +00001160DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
Devang Patelfe58f952010-12-07 23:25:47 +00001161 DIFile File, unsigned LineNo) {
1162 Value *Elts[] = {
1163 GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
David Blaikie6115ed02013-03-20 19:39:15 +00001164 File.getFileNode(),
Devang Patel94c7ddb2011-08-16 22:09:43 +00001165 getNonCompileUnitScope(Scope),
Devang Patelfe58f952010-12-07 23:25:47 +00001166 MDString::get(VMContext, Name),
Devang Patelfe58f952010-12-07 23:25:47 +00001167 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1168 };
David Blaikie66438682013-03-11 23:21:19 +00001169 DINameSpace R(MDNode::get(VMContext, Elts));
1170 assert(R.Verify() &&
1171 "createNameSpace should return a verifiable DINameSpace");
1172 return R;
Devang Patelfe58f952010-12-07 23:25:47 +00001173}
1174
Eric Christopher6618a242011-10-11 22:59:11 +00001175/// createLexicalBlockFile - This creates a new MDNode that encapsulates
1176/// an existing scope with a new filename.
1177DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
Devang Patel9f997212012-02-08 00:17:07 +00001178 DIFile File) {
Eric Christopher6618a242011-10-11 22:59:11 +00001179 Value *Elts[] = {
1180 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
David Blaikie8faed272013-03-22 20:18:46 +00001181 File.getFileNode(),
David Blaikie7b246862013-03-22 19:13:22 +00001182 Scope
Eric Christopher6618a242011-10-11 22:59:11 +00001183 };
David Blaikie66438682013-03-11 23:21:19 +00001184 DILexicalBlockFile R(MDNode::get(VMContext, Elts));
1185 assert(
1186 R.Verify() &&
1187 "createLexicalBlockFile should return a verifiable DILexicalBlockFile");
1188 return R;
Eric Christopher6618a242011-10-11 22:59:11 +00001189}
1190
Devang Patel50d280c2011-02-22 18:56:12 +00001191DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
Devang Patel43c249c2010-12-08 01:50:15 +00001192 unsigned Line, unsigned Col) {
Adrian Prantle06db0c2013-06-24 21:19:43 +00001193 // Defeat MDNode uniquing for lexical blocks by using unique id.
Devang Patel43c249c2010-12-08 01:50:15 +00001194 static unsigned int unique_id = 0;
1195 Value *Elts[] = {
1196 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
David Blaikie4b52a882013-03-22 17:33:20 +00001197 File.getFileNode(),
Devang Patel94c7ddb2011-08-16 22:09:43 +00001198 getNonCompileUnitScope(Scope),
Devang Patel43c249c2010-12-08 01:50:15 +00001199 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
1200 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
Devang Patel43c249c2010-12-08 01:50:15 +00001201 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
1202 };
David Blaikie66438682013-03-11 23:21:19 +00001203 DILexicalBlock R(MDNode::get(VMContext, Elts));
1204 assert(R.Verify() &&
1205 "createLexicalBlock should return a verifiable DILexicalBlock");
1206 return R;
Devang Patel43c249c2010-12-08 01:50:15 +00001207}
1208
Devang Patel50d280c2011-02-22 18:56:12 +00001209/// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1210Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
Devang Patelfe58f952010-12-07 23:25:47 +00001211 Instruction *InsertBefore) {
1212 assert(Storage && "no storage passed to dbg.declare");
Manman Renda918172013-06-29 05:01:19 +00001213 assert(VarInfo.isVariable() &&
1214 "empty or invalid DIVariable passed to dbg.declare");
Devang Patelfe58f952010-12-07 23:25:47 +00001215 if (!DeclareFn)
1216 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1217
Jay Foadec9186b2011-04-21 19:59:31 +00001218 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
Jay Foada3efbb12011-07-15 08:37:34 +00001219 return CallInst::Create(DeclareFn, Args, "", InsertBefore);
Devang Patelfe58f952010-12-07 23:25:47 +00001220}
1221
Devang Patel50d280c2011-02-22 18:56:12 +00001222/// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1223Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
Devang Patelfe58f952010-12-07 23:25:47 +00001224 BasicBlock *InsertAtEnd) {
1225 assert(Storage && "no storage passed to dbg.declare");
Manman Renda918172013-06-29 05:01:19 +00001226 assert(VarInfo.isVariable() &&
1227 "empty or invalid DIVariable passed to dbg.declare");
Devang Patelfe58f952010-12-07 23:25:47 +00001228 if (!DeclareFn)
1229 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1230
Jay Foadec9186b2011-04-21 19:59:31 +00001231 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
Devang Patelfe58f952010-12-07 23:25:47 +00001232
1233 // If this block already has a terminator then insert this intrinsic
1234 // before the terminator.
1235 if (TerminatorInst *T = InsertAtEnd->getTerminator())
Jay Foada3efbb12011-07-15 08:37:34 +00001236 return CallInst::Create(DeclareFn, Args, "", T);
Devang Patelfe58f952010-12-07 23:25:47 +00001237 else
Jay Foada3efbb12011-07-15 08:37:34 +00001238 return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
Devang Patelfe58f952010-12-07 23:25:47 +00001239}
1240
Devang Patel50d280c2011-02-22 18:56:12 +00001241/// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1242Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
Devang Patelfe58f952010-12-07 23:25:47 +00001243 DIVariable VarInfo,
1244 Instruction *InsertBefore) {
1245 assert(V && "no value passed to dbg.value");
Manman Renda918172013-06-29 05:01:19 +00001246 assert(VarInfo.isVariable() &&
1247 "empty or invalid DIVariable passed to dbg.value");
Devang Patelfe58f952010-12-07 23:25:47 +00001248 if (!ValueFn)
1249 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1250
Jay Foadec9186b2011-04-21 19:59:31 +00001251 Value *Args[] = { MDNode::get(V->getContext(), V),
Devang Patelfe58f952010-12-07 23:25:47 +00001252 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1253 VarInfo };
Jay Foada3efbb12011-07-15 08:37:34 +00001254 return CallInst::Create(ValueFn, Args, "", InsertBefore);
Devang Patelfe58f952010-12-07 23:25:47 +00001255}
1256
Devang Patel50d280c2011-02-22 18:56:12 +00001257/// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1258Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
Devang Patelfe58f952010-12-07 23:25:47 +00001259 DIVariable VarInfo,
1260 BasicBlock *InsertAtEnd) {
1261 assert(V && "no value passed to dbg.value");
Manman Renda918172013-06-29 05:01:19 +00001262 assert(VarInfo.isVariable() &&
1263 "empty or invalid DIVariable passed to dbg.value");
Devang Patelfe58f952010-12-07 23:25:47 +00001264 if (!ValueFn)
1265 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1266
Jay Foadec9186b2011-04-21 19:59:31 +00001267 Value *Args[] = { MDNode::get(V->getContext(), V),
Devang Patelfe58f952010-12-07 23:25:47 +00001268 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1269 VarInfo };
Jay Foada3efbb12011-07-15 08:37:34 +00001270 return CallInst::Create(ValueFn, Args, "", InsertAtEnd);
Devang Patelfe58f952010-12-07 23:25:47 +00001271}