blob: c8d0d22ec2e103450c87a99493b3b86c11a23699 [file] [log] [blame]
Chris Lattnera45664f2008-11-10 02:56:27 +00001//===--- DebugInfo.cpp - Debug Information Helper Classes -----------------===//
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 helper classes used to build and interpret debug
11// information in LLVM IR form.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Analysis/DebugInfo.h"
Chris Lattner784b8502009-12-29 09:15:46 +000016#include "llvm/Target/TargetMachine.h" // FIXME: LAYERING VIOLATION!
Chris Lattnera45664f2008-11-10 02:56:27 +000017#include "llvm/Constants.h"
18#include "llvm/DerivedTypes.h"
19#include "llvm/Intrinsics.h"
Torok Edwin620f2802008-12-16 09:07:36 +000020#include "llvm/IntrinsicInst.h"
Chris Lattnera45664f2008-11-10 02:56:27 +000021#include "llvm/Instructions.h"
22#include "llvm/Module.h"
23#include "llvm/Analysis/ValueTracking.h"
Devang Patele4b27562009-08-28 23:24:31 +000024#include "llvm/ADT/SmallPtrSet.h"
David Greene0eb5b662009-12-23 19:45:49 +000025#include "llvm/Support/Debug.h"
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000026#include "llvm/Support/Dwarf.h"
Chris Lattnera81d29b2009-08-23 07:33:14 +000027#include "llvm/Support/raw_ostream.h"
Chris Lattnera45664f2008-11-10 02:56:27 +000028using namespace llvm;
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000029using namespace llvm::dwarf;
Chris Lattnera45664f2008-11-10 02:56:27 +000030
31//===----------------------------------------------------------------------===//
32// DIDescriptor
33//===----------------------------------------------------------------------===//
34
Devang Patel65dbc902009-11-25 17:36:49 +000035StringRef
Devang Patel5ccdd102009-09-29 18:40:58 +000036DIDescriptor::getStringField(unsigned Elt) const {
Devang Patele4b27562009-08-28 23:24:31 +000037 if (DbgNode == 0)
Devang Patel65dbc902009-11-25 17:36:49 +000038 return StringRef();
Chris Lattnera45664f2008-11-10 02:56:27 +000039
Chris Lattner5d0cacd2009-12-31 01:22:29 +000040 if (Elt < DbgNode->getNumOperands())
41 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getOperand(Elt)))
Devang Patel65dbc902009-11-25 17:36:49 +000042 return MDS->getString();
Daniel Dunbarf612ff62009-09-19 20:40:05 +000043
Devang Patel65dbc902009-11-25 17:36:49 +000044 return StringRef();
Chris Lattnera45664f2008-11-10 02:56:27 +000045}
46
47uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +000048 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +000049 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +000050
Chris Lattner5d0cacd2009-12-31 01:22:29 +000051 if (Elt < DbgNode->getNumOperands())
52 if (ConstantInt *CI = dyn_cast<ConstantInt>(DbgNode->getOperand(Elt)))
Devang Patele4b27562009-08-28 23:24:31 +000053 return CI->getZExtValue();
Daniel Dunbarf612ff62009-09-19 20:40:05 +000054
Chris Lattnera45664f2008-11-10 02:56:27 +000055 return 0;
56}
57
Chris Lattnera45664f2008-11-10 02:56:27 +000058DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +000059 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +000060 return DIDescriptor();
Bill Wendlingdc817b62009-05-14 18:26:15 +000061
Chris Lattner7a2f3e02010-03-31 05:53:47 +000062 if (Elt < DbgNode->getNumOperands())
Devang Patele9f8f5e2010-05-07 20:54:48 +000063 return DIDescriptor(dyn_cast_or_null<const MDNode>(DbgNode->getOperand(Elt)));
Devang Patele4b27562009-08-28 23:24:31 +000064 return DIDescriptor();
Chris Lattnera45664f2008-11-10 02:56:27 +000065}
66
67GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +000068 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +000069 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +000070
Chris Lattner5d0cacd2009-12-31 01:22:29 +000071 if (Elt < DbgNode->getNumOperands())
72 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
Devang Patele4b27562009-08-28 23:24:31 +000073 return 0;
Chris Lattnera45664f2008-11-10 02:56:27 +000074}
75
Stuart Hastings215aa152010-06-11 20:08:44 +000076Function *DIDescriptor::getFunctionField(unsigned Elt) const {
77 if (DbgNode == 0)
78 return 0;
79
80 if (Elt < DbgNode->getNumOperands())
81 return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
82 return 0;
83}
84
Chris Lattnerf0908a32009-12-31 03:02:08 +000085unsigned DIVariable::getNumAddrElements() const {
86 return DbgNode->getNumOperands()-6;
87}
88
89
Chris Lattnera45664f2008-11-10 02:56:27 +000090//===----------------------------------------------------------------------===//
Devang Patel6ceea332009-08-31 18:49:10 +000091// Predicates
Chris Lattnera45664f2008-11-10 02:56:27 +000092//===----------------------------------------------------------------------===//
93
Devang Patel6ceea332009-08-31 18:49:10 +000094/// isBasicType - Return true if the specified tag is legal for
95/// DIBasicType.
96bool DIDescriptor::isBasicType() const {
Devang Patel3c91b052010-03-08 20:52:55 +000097 return DbgNode && getTag() == dwarf::DW_TAG_base_type;
Torok Edwinb07fbd92008-12-13 08:25:29 +000098}
Chris Lattnera45664f2008-11-10 02:56:27 +000099
Devang Patel6ceea332009-08-31 18:49:10 +0000100/// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
101bool DIDescriptor::isDerivedType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000102 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000103 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000104 case dwarf::DW_TAG_typedef:
105 case dwarf::DW_TAG_pointer_type:
106 case dwarf::DW_TAG_reference_type:
107 case dwarf::DW_TAG_const_type:
108 case dwarf::DW_TAG_volatile_type:
109 case dwarf::DW_TAG_restrict_type:
110 case dwarf::DW_TAG_member:
111 case dwarf::DW_TAG_inheritance:
112 return true;
113 default:
Devang Patele4b27562009-08-28 23:24:31 +0000114 // CompositeTypes are currently modelled as DerivedTypes.
Devang Patel6ceea332009-08-31 18:49:10 +0000115 return isCompositeType();
Chris Lattnera45664f2008-11-10 02:56:27 +0000116 }
117}
118
Chris Lattnera45664f2008-11-10 02:56:27 +0000119/// isCompositeType - Return true if the specified tag is legal for
120/// DICompositeType.
Devang Patel6ceea332009-08-31 18:49:10 +0000121bool DIDescriptor::isCompositeType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000122 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000123 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000124 case dwarf::DW_TAG_array_type:
125 case dwarf::DW_TAG_structure_type:
126 case dwarf::DW_TAG_union_type:
127 case dwarf::DW_TAG_enumeration_type:
128 case dwarf::DW_TAG_vector_type:
129 case dwarf::DW_TAG_subroutine_type:
Devang Patel25cb0d72009-03-25 03:52:06 +0000130 case dwarf::DW_TAG_class_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000131 return true;
132 default:
133 return false;
134 }
135}
136
Chris Lattnera45664f2008-11-10 02:56:27 +0000137/// isVariable - Return true if the specified tag is legal for DIVariable.
Devang Patel6ceea332009-08-31 18:49:10 +0000138bool DIDescriptor::isVariable() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000139 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000140 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000141 case dwarf::DW_TAG_auto_variable:
142 case dwarf::DW_TAG_arg_variable:
143 case dwarf::DW_TAG_return_variable:
144 return true;
145 default:
146 return false;
147 }
148}
149
Devang Patelecbeb1a2009-09-30 22:34:41 +0000150/// isType - Return true if the specified tag is legal for DIType.
151bool DIDescriptor::isType() const {
152 return isBasicType() || isCompositeType() || isDerivedType();
153}
154
Devang Patel6ceea332009-08-31 18:49:10 +0000155/// isSubprogram - Return true if the specified tag is legal for
156/// DISubprogram.
157bool DIDescriptor::isSubprogram() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000158 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
Devang Patel6ceea332009-08-31 18:49:10 +0000159}
160
161/// isGlobalVariable - Return true if the specified tag is legal for
162/// DIGlobalVariable.
163bool DIDescriptor::isGlobalVariable() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000164 return DbgNode && getTag() == dwarf::DW_TAG_variable;
Devang Patel6ceea332009-08-31 18:49:10 +0000165}
166
Devang Patelecbeb1a2009-09-30 22:34:41 +0000167/// isGlobal - Return true if the specified tag is legal for DIGlobal.
168bool DIDescriptor::isGlobal() const {
169 return isGlobalVariable();
170}
171
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000172/// isScope - Return true if the specified tag is one of the scope
Devang Patel43d98b32009-08-31 20:44:45 +0000173/// related tag.
174bool DIDescriptor::isScope() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000175 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000176 switch (getTag()) {
177 case dwarf::DW_TAG_compile_unit:
178 case dwarf::DW_TAG_lexical_block:
179 case dwarf::DW_TAG_subprogram:
180 case dwarf::DW_TAG_namespace:
181 return true;
182 default:
183 break;
Devang Patel43d98b32009-08-31 20:44:45 +0000184 }
185 return false;
186}
Devang Patel6ceea332009-08-31 18:49:10 +0000187
Devang Patelc9f322d2009-08-31 21:34:44 +0000188/// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
189bool DIDescriptor::isCompileUnit() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000190 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
Devang Patelc9f322d2009-08-31 21:34:44 +0000191}
192
Devang Patel7aa81892010-03-08 22:27:22 +0000193/// isFile - Return true if the specified tag is DW_TAG_file_type.
194bool DIDescriptor::isFile() const {
195 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
196}
197
Devang Patel6404e4e2009-12-15 19:16:48 +0000198/// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
199bool DIDescriptor::isNameSpace() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000200 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
Devang Patel6404e4e2009-12-15 19:16:48 +0000201}
202
Devang Patel5e005d82009-08-31 22:00:15 +0000203/// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
204bool DIDescriptor::isLexicalBlock() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000205 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block;
Devang Patel5e005d82009-08-31 22:00:15 +0000206}
207
Devang Patelecbeb1a2009-09-30 22:34:41 +0000208/// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
209bool DIDescriptor::isSubrange() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000210 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000211}
212
213/// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
214bool DIDescriptor::isEnumerator() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000215 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000216}
217
Devang Patel6ceea332009-08-31 18:49:10 +0000218//===----------------------------------------------------------------------===//
219// Simple Descriptor Constructors and other Methods
220//===----------------------------------------------------------------------===//
221
Devang Patele9f8f5e2010-05-07 20:54:48 +0000222DIType::DIType(const MDNode *N) : DIScope(N) {
Devang Patel6ceea332009-08-31 18:49:10 +0000223 if (!N) return;
224 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
225 DbgNode = 0;
226 }
227}
228
Devang Patel68afdc32009-01-05 18:33:01 +0000229unsigned DIArray::getNumElements() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000230 if (!DbgNode)
231 return 0;
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000232 return DbgNode->getNumOperands();
Devang Patel68afdc32009-01-05 18:33:01 +0000233}
Chris Lattnera45664f2008-11-10 02:56:27 +0000234
Devang Patelc4999d72009-07-22 18:23:44 +0000235/// replaceAllUsesWith - Replace all uses of debug info referenced by
236/// this descriptor. After this completes, the current debug info value
237/// is erased.
238void DIDerivedType::replaceAllUsesWith(DIDescriptor &D) {
Devang Patel3c91b052010-03-08 20:52:55 +0000239 if (!DbgNode)
Devang Patelc4999d72009-07-22 18:23:44 +0000240 return;
241
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000242 // Since we use a TrackingVH for the node, its easy for clients to manufacture
243 // legitimate situations where they want to replaceAllUsesWith() on something
244 // which, due to uniquing, has merged with the source. We shield clients from
245 // this detail by allowing a value to be replaced with replaceAllUsesWith()
246 // itself.
Devang Pateled66bf52010-05-07 18:19:32 +0000247 if (DbgNode != D) {
Devang Patele9f8f5e2010-05-07 20:54:48 +0000248 MDNode *Node = const_cast<MDNode*>(DbgNode);
249 const MDNode *DN = D;
250 const Value *V = cast_or_null<Value>(DN);
251 Node->replaceAllUsesWith(const_cast<Value*>(V));
Chris Lattnerb76359e2009-12-31 01:05:46 +0000252 Node->destroy();
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000253 }
Devang Patelc4999d72009-07-22 18:23:44 +0000254}
255
Devang Patelb79b5352009-01-19 23:21:49 +0000256/// Verify - Verify that a compile unit is well formed.
257bool DICompileUnit::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000258 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000259 return false;
Devang Patel65dbc902009-11-25 17:36:49 +0000260 StringRef N = getFilename();
261 if (N.empty())
Bill Wendling0582ae92009-03-13 04:39:26 +0000262 return false;
Devang Patelb79b5352009-01-19 23:21:49 +0000263 // It is possible that directory and produce string is empty.
Bill Wendling0582ae92009-03-13 04:39:26 +0000264 return true;
Devang Patelb79b5352009-01-19 23:21:49 +0000265}
266
267/// Verify - Verify that a type descriptor is well formed.
268bool DIType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000269 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000270 return false;
Devang Patel3c91b052010-03-08 20:52:55 +0000271 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000272 return false;
273
274 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000275 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000276 return false;
277 return true;
278}
279
280/// Verify - Verify that a composite type descriptor is well formed.
281bool DICompositeType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000282 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000283 return false;
Devang Patel3c91b052010-03-08 20:52:55 +0000284 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000285 return false;
286
287 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000288 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000289 return false;
290 return true;
291}
292
293/// Verify - Verify that a subprogram descriptor is well formed.
294bool DISubprogram::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000295 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000296 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000297
Devang Patel3c91b052010-03-08 20:52:55 +0000298 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000299 return false;
300
301 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000302 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000303 return false;
304
305 DICompositeType Ty = getType();
Devang Patel3c91b052010-03-08 20:52:55 +0000306 if (!Ty.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000307 return false;
308 return true;
309}
310
311/// Verify - Verify that a global variable descriptor is well formed.
312bool DIGlobalVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000313 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000314 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000315
Devang Patel65dbc902009-11-25 17:36:49 +0000316 if (getDisplayName().empty())
Devang Patel84c73e92009-11-06 17:58:12 +0000317 return false;
318
Devang Patel3c91b052010-03-08 20:52:55 +0000319 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000320 return false;
321
322 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000323 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000324 return false;
325
326 DIType Ty = getType();
327 if (!Ty.Verify())
328 return false;
329
330 if (!getGlobal())
331 return false;
332
333 return true;
334}
335
336/// Verify - Verify that a variable descriptor is well formed.
337bool DIVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000338 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000339 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000340
Devang Patel3c91b052010-03-08 20:52:55 +0000341 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000342 return false;
343
Devang Patel62077af2010-05-07 21:42:24 +0000344 if (!getCompileUnit().Verify())
345 return false;
346
Devang Patelb79b5352009-01-19 23:21:49 +0000347 DIType Ty = getType();
348 if (!Ty.Verify())
349 return false;
350
Devang Patelb79b5352009-01-19 23:21:49 +0000351 return true;
352}
353
Devang Patel3c91b052010-03-08 20:52:55 +0000354/// Verify - Verify that a location descriptor is well formed.
355bool DILocation::Verify() const {
356 if (!DbgNode)
357 return false;
358
359 return DbgNode->getNumOperands() == 4;
360}
361
Devang Patel47e22652010-05-07 23:04:32 +0000362/// Verify - Verify that a namespace descriptor is well formed.
363bool DINameSpace::Verify() const {
364 if (!DbgNode)
365 return false;
366 if (getName().empty())
367 return false;
368 if (!getCompileUnit().Verify())
369 return false;
370 return true;
371}
372
Devang Patel36375ee2009-02-17 21:23:59 +0000373/// getOriginalTypeSize - If this type is derived from a base type then
374/// return base type size.
375uint64_t DIDerivedType::getOriginalTypeSize() const {
Devang Patel61ecbd12009-11-04 23:48:00 +0000376 unsigned Tag = getTag();
377 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
378 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
379 Tag == dwarf::DW_TAG_restrict_type) {
380 DIType BaseType = getTypeDerivedFrom();
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000381 // If this type is not derived from any type then take conservative
382 // approach.
Devang Patel3c91b052010-03-08 20:52:55 +0000383 if (!BaseType.isValid())
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000384 return getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +0000385 if (BaseType.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +0000386 return DIDerivedType(BaseType).getOriginalTypeSize();
Devang Patel61ecbd12009-11-04 23:48:00 +0000387 else
388 return BaseType.getSizeInBits();
389 }
390
391 return getSizeInBits();
Devang Patel36375ee2009-02-17 21:23:59 +0000392}
Devang Patelb79b5352009-01-19 23:21:49 +0000393
Devang Patel719f6a92010-04-29 20:40:36 +0000394/// isInlinedFnArgument - Return trule if this variable provides debugging
395/// information for an inlined function arguments.
396bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
397 assert(CurFn && "Invalid function");
398 if (!getContext().isSubprogram())
399 return false;
400 // This variable is not inlined function argument if its scope
401 // does not describe current function.
Devang Patel2db49d72010-05-07 18:11:54 +0000402 return !(DISubprogram(getContext()).describes(CurFn));
Devang Patel719f6a92010-04-29 20:40:36 +0000403}
404
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000405/// describes - Return true if this subprogram provides debugging
406/// information for the function F.
407bool DISubprogram::describes(const Function *F) {
Chris Lattner099b7792009-12-29 09:22:47 +0000408 assert(F && "Invalid function");
Devang Patelffd33cd2010-06-16 06:42:02 +0000409 if (F == getFunction())
410 return true;
Devang Patel65dbc902009-11-25 17:36:49 +0000411 StringRef Name = getLinkageName();
412 if (Name.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +0000413 Name = getName();
Devang Patel65dbc902009-11-25 17:36:49 +0000414 if (F->getName() == Name)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000415 return true;
416 return false;
417}
418
Devang Patelccff8122010-04-30 19:38:23 +0000419unsigned DISubprogram::isOptimized() const {
420 assert (DbgNode && "Invalid subprogram descriptor!");
421 if (DbgNode->getNumOperands() == 16)
422 return getUnsignedField(15);
423 return 0;
424}
425
Devang Patel65dbc902009-11-25 17:36:49 +0000426StringRef DIScope::getFilename() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000427 if (!DbgNode)
428 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000429 if (isLexicalBlock())
430 return DILexicalBlock(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000431 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000432 return DISubprogram(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000433 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000434 return DICompileUnit(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000435 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000436 return DINameSpace(DbgNode).getFilename();
Devang Patel77bf2952010-03-08 22:02:50 +0000437 if (isType())
438 return DIType(DbgNode).getFilename();
Devang Patel7aa81892010-03-08 22:27:22 +0000439 if (isFile())
440 return DIFile(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000441 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000442 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000443}
444
Devang Patel65dbc902009-11-25 17:36:49 +0000445StringRef DIScope::getDirectory() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000446 if (!DbgNode)
447 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000448 if (isLexicalBlock())
449 return DILexicalBlock(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000450 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000451 return DISubprogram(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000452 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000453 return DICompileUnit(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000454 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000455 return DINameSpace(DbgNode).getDirectory();
Devang Patel77bf2952010-03-08 22:02:50 +0000456 if (isType())
457 return DIType(DbgNode).getDirectory();
Devang Patel7aa81892010-03-08 22:27:22 +0000458 if (isFile())
459 return DIFile(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000460 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000461 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000462}
463
Chris Lattnera45664f2008-11-10 02:56:27 +0000464//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000465// DIDescriptor: dump routines for all descriptors.
466//===----------------------------------------------------------------------===//
467
468
Dan Gohman50404362010-05-07 15:30:29 +0000469/// print - Print descriptor.
470void DIDescriptor::print(raw_ostream &OS) const {
471 OS << "[" << dwarf::TagString(getTag()) << "] ";
472 OS.write_hex((intptr_t) &*DbgNode) << ']';
Devang Patel7136a652009-07-01 22:10:23 +0000473}
474
Dan Gohman50404362010-05-07 15:30:29 +0000475/// print - Print compile unit.
476void DICompileUnit::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000477 if (getLanguage())
Dan Gohman50404362010-05-07 15:30:29 +0000478 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000479
Dan Gohmanfaa19c32010-05-10 20:07:44 +0000480 OS << " [" << getDirectory() << "/" << getFilename() << "]";
Devang Patel7136a652009-07-01 22:10:23 +0000481}
482
Dan Gohman50404362010-05-07 15:30:29 +0000483/// print - Print type.
484void DIType::print(raw_ostream &OS) const {
Devang Patel3c91b052010-03-08 20:52:55 +0000485 if (!DbgNode) return;
Devang Patel7136a652009-07-01 22:10:23 +0000486
Devang Patel65dbc902009-11-25 17:36:49 +0000487 StringRef Res = getName();
488 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000489 OS << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000490
491 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000492 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000493
494 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000495 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000496 OS << " ["
Dan Gohman9a7063e2010-05-07 16:39:27 +0000497 << "line " << getLineNumber() << ", "
498 << getSizeInBits() << " bits, "
499 << getAlignInBits() << " bit alignment, "
500 << getOffsetInBits() << " bit offset"
Chris Lattnera81d29b2009-08-23 07:33:14 +0000501 << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000502
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000503 if (isPrivate())
Dan Gohman50404362010-05-07 15:30:29 +0000504 OS << " [private] ";
Devang Patel7136a652009-07-01 22:10:23 +0000505 else if (isProtected())
Dan Gohman50404362010-05-07 15:30:29 +0000506 OS << " [protected] ";
Devang Patel7136a652009-07-01 22:10:23 +0000507
508 if (isForwardDecl())
Dan Gohman50404362010-05-07 15:30:29 +0000509 OS << " [fwd] ";
Devang Patel7136a652009-07-01 22:10:23 +0000510
Devang Patel6ceea332009-08-31 18:49:10 +0000511 if (isBasicType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000512 DIBasicType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000513 else if (isDerivedType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000514 DIDerivedType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000515 else if (isCompositeType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000516 DICompositeType(DbgNode).print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000517 else {
Dan Gohman50404362010-05-07 15:30:29 +0000518 OS << "Invalid DIType\n";
Devang Patel7136a652009-07-01 22:10:23 +0000519 return;
520 }
521
Dan Gohman50404362010-05-07 15:30:29 +0000522 OS << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000523}
524
Dan Gohman50404362010-05-07 15:30:29 +0000525/// print - Print basic type.
526void DIBasicType::print(raw_ostream &OS) const {
527 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000528}
529
Dan Gohman50404362010-05-07 15:30:29 +0000530/// print - Print derived type.
531void DIDerivedType::print(raw_ostream &OS) const {
Dan Gohmanc014d092010-05-07 16:17:22 +0000532 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000533}
534
Dan Gohman50404362010-05-07 15:30:29 +0000535/// print - Print composite type.
536void DICompositeType::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000537 DIArray A = getTypeArray();
Dan Gohman50404362010-05-07 15:30:29 +0000538 OS << " [" << A.getNumElements() << " elements]";
Devang Patel7136a652009-07-01 22:10:23 +0000539}
540
Dan Gohman50404362010-05-07 15:30:29 +0000541/// print - Print subprogram.
542void DISubprogram::print(raw_ostream &OS) const {
Devang Patel65dbc902009-11-25 17:36:49 +0000543 StringRef Res = getName();
544 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000545 OS << " [" << Res << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000546
547 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000548 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000549
550 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000551 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000552 OS << " [" << getLineNumber() << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000553
554 if (isLocalToUnit())
Dan Gohman50404362010-05-07 15:30:29 +0000555 OS << " [local] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000556
557 if (isDefinition())
Dan Gohman50404362010-05-07 15:30:29 +0000558 OS << " [def] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000559
Dan Gohman50404362010-05-07 15:30:29 +0000560 OS << "\n";
561}
562
563/// print - Print global variable.
564void DIGlobalVariable::print(raw_ostream &OS) const {
565 OS << " [";
Devang Patela49d8772010-05-07 23:19:07 +0000566 StringRef Res = getName();
567 if (!Res.empty())
568 OS << " [" << Res << "] ";
569
570 unsigned Tag = getTag();
571 OS << " [" << dwarf::TagString(Tag) << "] ";
572
573 // TODO : Print context
574 getCompileUnit().print(OS);
575 OS << " [" << getLineNumber() << "] ";
576
577 if (isLocalToUnit())
578 OS << " [local] ";
579
580 if (isDefinition())
581 OS << " [def] ";
582
583 if (isGlobalVariable())
584 DIGlobalVariable(DbgNode).print(OS);
585 OS << "]\n";
Dan Gohman50404362010-05-07 15:30:29 +0000586}
587
588/// print - Print variable.
589void DIVariable::print(raw_ostream &OS) const {
590 StringRef Res = getName();
591 if (!Res.empty())
592 OS << " [" << Res << "] ";
593
Dan Gohmanc014d092010-05-07 16:17:22 +0000594 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000595 OS << " [" << getLineNumber() << "] ";
Dan Gohmanc014d092010-05-07 16:17:22 +0000596 getType().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000597 OS << "\n";
598
599 // FIXME: Dump complex addresses
600}
601
602/// dump - Print descriptor to dbgs() with a newline.
603void DIDescriptor::dump() const {
604 print(dbgs()); dbgs() << '\n';
605}
606
607/// dump - Print compile unit to dbgs() with a newline.
608void DICompileUnit::dump() const {
609 print(dbgs()); dbgs() << '\n';
610}
611
612/// dump - Print type to dbgs() with a newline.
613void DIType::dump() const {
614 print(dbgs()); dbgs() << '\n';
615}
616
617/// dump - Print basic type to dbgs() with a newline.
618void DIBasicType::dump() const {
619 print(dbgs()); dbgs() << '\n';
620}
621
622/// dump - Print derived type to dbgs() with a newline.
623void DIDerivedType::dump() const {
624 print(dbgs()); dbgs() << '\n';
625}
626
627/// dump - Print composite type to dbgs() with a newline.
628void DICompositeType::dump() const {
629 print(dbgs()); dbgs() << '\n';
630}
631
Dan Gohman50404362010-05-07 15:30:29 +0000632/// dump - Print subprogram to dbgs() with a newline.
633void DISubprogram::dump() const {
634 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000635}
636
637/// dump - Print global variable.
638void DIGlobalVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000639 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000640}
641
642/// dump - Print variable.
643void DIVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000644 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000645}
646
647//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000648// DIFactory: Basic Helpers
649//===----------------------------------------------------------------------===//
650
Bill Wendlingdc817b62009-05-14 18:26:15 +0000651DIFactory::DIFactory(Module &m)
Victor Hernandez06203122010-01-23 00:03:28 +0000652 : M(m), VMContext(M.getContext()), DeclareFn(0), ValueFn(0) {}
Chris Lattner497a7a82008-11-10 04:10:34 +0000653
Chris Lattnera45664f2008-11-10 02:56:27 +0000654Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000655 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000656 "Tag too large for debug encoding!");
Owen Anderson1d0be152009-08-13 21:58:54 +0000657 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000658}
659
Chris Lattnera45664f2008-11-10 02:56:27 +0000660//===----------------------------------------------------------------------===//
661// DIFactory: Primary Constructors
662//===----------------------------------------------------------------------===//
663
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000664/// GetOrCreateArray - Create an descriptor for an array of descriptors.
Chris Lattnera45664f2008-11-10 02:56:27 +0000665/// This implicitly uniques the arrays created.
666DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
Devang Patele4b27562009-08-28 23:24:31 +0000667 SmallVector<Value*, 16> Elts;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000668
Devang Patele4b27562009-08-28 23:24:31 +0000669 if (NumTys == 0)
670 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
671 else
672 for (unsigned i = 0; i != NumTys; ++i)
Devang Patel2db49d72010-05-07 18:11:54 +0000673 Elts.push_back(Tys[i]);
Devang Patel82459882009-08-26 05:01:18 +0000674
Devang Patele4b27562009-08-28 23:24:31 +0000675 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
Chris Lattnera45664f2008-11-10 02:56:27 +0000676}
677
678/// GetOrCreateSubrange - Create a descriptor for a value range. This
679/// implicitly uniques the values returned.
680DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
Devang Patele4b27562009-08-28 23:24:31 +0000681 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000682 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Anderson1d0be152009-08-13 21:58:54 +0000683 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
684 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000685 };
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000686
Devang Patele4b27562009-08-28 23:24:31 +0000687 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000688}
689
690
691
692/// CreateCompileUnit - Create a new descriptor for the specified compile
693/// unit. Note that this does not unique compile units within the module.
694DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
Devang Patel65dbc902009-11-25 17:36:49 +0000695 StringRef Filename,
696 StringRef Directory,
697 StringRef Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000698 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000699 bool isOptimized,
Devang Patel65dbc902009-11-25 17:36:49 +0000700 StringRef Flags,
Devang Patel13319ce2009-02-17 22:43:44 +0000701 unsigned RunTimeVer) {
Devang Patele4b27562009-08-28 23:24:31 +0000702 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000703 GetTagConstant(dwarf::DW_TAG_compile_unit),
Devang Patele4b27562009-08-28 23:24:31 +0000704 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Owen Anderson1d0be152009-08-13 21:58:54 +0000705 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
Devang Patele4b27562009-08-28 23:24:31 +0000706 MDString::get(VMContext, Filename),
707 MDString::get(VMContext, Directory),
708 MDString::get(VMContext, Producer),
Owen Anderson1d0be152009-08-13 21:58:54 +0000709 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
710 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patele4b27562009-08-28 23:24:31 +0000711 MDString::get(VMContext, Flags),
Owen Anderson1d0be152009-08-13 21:58:54 +0000712 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000713 };
Devang Patele4b27562009-08-28 23:24:31 +0000714
715 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000716}
717
Devang Patel7aa81892010-03-08 22:27:22 +0000718/// CreateFile - Create a new descriptor for the specified file.
719DIFile DIFactory::CreateFile(StringRef Filename,
720 StringRef Directory,
721 DICompileUnit CU) {
722 Value *Elts[] = {
723 GetTagConstant(dwarf::DW_TAG_file_type),
724 MDString::get(VMContext, Filename),
725 MDString::get(VMContext, Directory),
Devang Patel2db49d72010-05-07 18:11:54 +0000726 CU
Devang Patel7aa81892010-03-08 22:27:22 +0000727 };
728
729 return DIFile(MDNode::get(VMContext, &Elts[0], 4));
730}
731
Chris Lattnera45664f2008-11-10 02:56:27 +0000732/// CreateEnumerator - Create a single enumerator value.
Devang Patel65dbc902009-11-25 17:36:49 +0000733DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
Devang Patele4b27562009-08-28 23:24:31 +0000734 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000735 GetTagConstant(dwarf::DW_TAG_enumerator),
Devang Patele4b27562009-08-28 23:24:31 +0000736 MDString::get(VMContext, Name),
Owen Anderson1d0be152009-08-13 21:58:54 +0000737 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000738 };
Devang Patele4b27562009-08-28 23:24:31 +0000739 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000740}
741
742
743/// CreateBasicType - Create a basic type like int, float, etc.
744DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000745 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000746 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000747 unsigned LineNumber,
748 uint64_t SizeInBits,
749 uint64_t AlignInBits,
750 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000751 unsigned Encoding) {
Devang Patele4b27562009-08-28 23:24:31 +0000752 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000753 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patel2db49d72010-05-07 18:11:54 +0000754 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000755 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000756 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000757 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
758 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
759 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
760 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
761 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
762 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000763 };
Devang Patele4b27562009-08-28 23:24:31 +0000764 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000765}
766
Devang Patelac16d442009-10-26 16:54:35 +0000767
768/// CreateBasicType - Create a basic type like int, float, etc.
769DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000770 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000771 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000772 unsigned LineNumber,
773 Constant *SizeInBits,
774 Constant *AlignInBits,
775 Constant *OffsetInBits, unsigned Flags,
776 unsigned Encoding) {
777 Value *Elts[] = {
778 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patel2db49d72010-05-07 18:11:54 +0000779 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000780 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000781 F,
Devang Patelac16d442009-10-26 16:54:35 +0000782 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
783 SizeInBits,
784 AlignInBits,
785 OffsetInBits,
786 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
787 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
788 };
789 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
790}
791
Devang Patelb4645642010-02-06 01:02:37 +0000792/// CreateArtificialType - Create a new DIType with "artificial" flag set.
793DIType DIFactory::CreateArtificialType(DIType Ty) {
794 if (Ty.isArtificial())
795 return Ty;
796
797 SmallVector<Value *, 9> Elts;
Devang Patel2db49d72010-05-07 18:11:54 +0000798 MDNode *N = Ty;
Devang Patelb4645642010-02-06 01:02:37 +0000799 assert (N && "Unexpected input DIType!");
800 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
801 if (Value *V = N->getOperand(i))
802 Elts.push_back(V);
803 else
804 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
805 }
806
807 unsigned CurFlags = Ty.getFlags();
808 CurFlags = CurFlags | DIType::FlagArtificial;
809
810 // Flags are stored at this slot.
811 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
812
813 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
814}
Devang Patelac16d442009-10-26 16:54:35 +0000815
Chris Lattnera45664f2008-11-10 02:56:27 +0000816/// CreateDerivedType - Create a derived type like const qualified type,
817/// pointer, typedef, etc.
818DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
819 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000820 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000821 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000822 unsigned LineNumber,
823 uint64_t SizeInBits,
824 uint64_t AlignInBits,
825 uint64_t OffsetInBits,
826 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000827 DIType DerivedFrom) {
Devang Patele4b27562009-08-28 23:24:31 +0000828 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000829 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000830 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000831 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000832 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000833 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
834 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
835 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
836 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
837 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000838 DerivedFrom,
Chris Lattnera45664f2008-11-10 02:56:27 +0000839 };
Devang Patele4b27562009-08-28 23:24:31 +0000840 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000841}
842
Devang Patelac16d442009-10-26 16:54:35 +0000843
844/// CreateDerivedType - Create a derived type like const qualified type,
845/// pointer, typedef, etc.
846DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
847 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000848 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000849 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000850 unsigned LineNumber,
851 Constant *SizeInBits,
852 Constant *AlignInBits,
853 Constant *OffsetInBits,
854 unsigned Flags,
855 DIType DerivedFrom) {
856 Value *Elts[] = {
857 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000858 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000859 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000860 F,
Devang Patelac16d442009-10-26 16:54:35 +0000861 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
862 SizeInBits,
863 AlignInBits,
864 OffsetInBits,
865 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000866 DerivedFrom,
Devang Patelac16d442009-10-26 16:54:35 +0000867 };
868 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
869}
870
871
Chris Lattnera45664f2008-11-10 02:56:27 +0000872/// CreateCompositeType - Create a composite type like array, struct, etc.
873DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
874 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000875 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000876 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000877 unsigned LineNumber,
878 uint64_t SizeInBits,
879 uint64_t AlignInBits,
880 uint64_t OffsetInBits,
881 unsigned Flags,
882 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000883 DIArray Elements,
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000884 unsigned RuntimeLang,
885 MDNode *ContainingType) {
Owen Andersone277fed2009-07-07 16:31:25 +0000886
Devang Patele4b27562009-08-28 23:24:31 +0000887 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000888 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000889 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000890 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000891 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000892 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
893 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
894 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
895 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
896 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000897 DerivedFrom,
898 Elements,
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000899 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
900 ContainingType
Chris Lattnera45664f2008-11-10 02:56:27 +0000901 };
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000902 return DICompositeType(MDNode::get(VMContext, &Elts[0], 13));
Chris Lattnera45664f2008-11-10 02:56:27 +0000903}
904
905
Devang Patelac16d442009-10-26 16:54:35 +0000906/// CreateCompositeType - Create a composite type like array, struct, etc.
907DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
908 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000909 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000910 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000911 unsigned LineNumber,
912 Constant *SizeInBits,
913 Constant *AlignInBits,
914 Constant *OffsetInBits,
915 unsigned Flags,
916 DIType DerivedFrom,
917 DIArray Elements,
918 unsigned RuntimeLang) {
919
920 Value *Elts[] = {
921 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +0000922 Context,
Devang Patelac16d442009-10-26 16:54:35 +0000923 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +0000924 F,
Devang Patelac16d442009-10-26 16:54:35 +0000925 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
926 SizeInBits,
927 AlignInBits,
928 OffsetInBits,
929 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patel2db49d72010-05-07 18:11:54 +0000930 DerivedFrom,
931 Elements,
Devang Patelac16d442009-10-26 16:54:35 +0000932 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
933 };
934 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
935}
936
937
Chris Lattnera45664f2008-11-10 02:56:27 +0000938/// CreateSubprogram - Create a new descriptor for the specified subprogram.
939/// See comments in DISubprogram for descriptions of these fields. This
940/// method does not unique the generated descriptors.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000941DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000942 StringRef Name,
943 StringRef DisplayName,
944 StringRef LinkageName,
Devang Patel4b945502010-03-09 00:44:10 +0000945 DIFile F,
Devang Patel2e369932010-01-23 00:26:28 +0000946 unsigned LineNo, DIType Ty,
Chris Lattnera45664f2008-11-10 02:56:27 +0000947 bool isLocalToUnit,
Devang Patel5d11eb02009-12-03 19:11:07 +0000948 bool isDefinition,
949 unsigned VK, unsigned VIndex,
Devang Patel4e0d19d2010-02-03 19:57:19 +0000950 DIType ContainingType,
Devang Patelccff8122010-04-30 19:38:23 +0000951 bool isArtificial,
Stuart Hastings215aa152010-06-11 20:08:44 +0000952 bool isOptimized,
953 Function *Fn) {
Devang Patel854967e2008-12-17 22:39:29 +0000954
Devang Patele4b27562009-08-28 23:24:31 +0000955 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000956 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patele4b27562009-08-28 23:24:31 +0000957 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel2db49d72010-05-07 18:11:54 +0000958 Context,
Devang Patele4b27562009-08-28 23:24:31 +0000959 MDString::get(VMContext, Name),
960 MDString::get(VMContext, DisplayName),
961 MDString::get(VMContext, LinkageName),
Devang Patel2db49d72010-05-07 18:11:54 +0000962 F,
Owen Anderson1d0be152009-08-13 21:58:54 +0000963 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +0000964 Ty,
Owen Anderson1d0be152009-08-13 21:58:54 +0000965 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
Devang Patel5d11eb02009-12-03 19:11:07 +0000966 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
967 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
968 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
Devang Patel2db49d72010-05-07 18:11:54 +0000969 ContainingType,
Devang Patelccff8122010-04-30 19:38:23 +0000970 ConstantInt::get(Type::getInt1Ty(VMContext), isArtificial),
Stuart Hastings215aa152010-06-11 20:08:44 +0000971 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
972 Fn
Chris Lattnera45664f2008-11-10 02:56:27 +0000973 };
Devang Patelfd5fdc32010-06-28 05:53:08 +0000974 MDNode *Node = MDNode::get(VMContext, &Elts[0], 17);
975
976 // Create a named metadata so that we do not lose this mdnode.
977 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
978 NMD->addOperand(Node);
979 return DISubprogram(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +0000980}
981
Devang Patele3a18de2009-12-01 23:09:02 +0000982/// CreateSubprogramDefinition - Create new subprogram descriptor for the
983/// given declaration.
984DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration) {
985 if (SPDeclaration.isDefinition())
Devang Patel2db49d72010-05-07 18:11:54 +0000986 return DISubprogram(SPDeclaration);
Devang Patele3a18de2009-12-01 23:09:02 +0000987
Devang Patel2db49d72010-05-07 18:11:54 +0000988 MDNode *DeclNode = SPDeclaration;
Devang Patele3a18de2009-12-01 23:09:02 +0000989 Value *Elts[] = {
990 GetTagConstant(dwarf::DW_TAG_subprogram),
991 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000992 DeclNode->getOperand(2), // Context
993 DeclNode->getOperand(3), // Name
994 DeclNode->getOperand(4), // DisplayName
995 DeclNode->getOperand(5), // LinkageName
996 DeclNode->getOperand(6), // CompileUnit
997 DeclNode->getOperand(7), // LineNo
998 DeclNode->getOperand(8), // Type
999 DeclNode->getOperand(9), // isLocalToUnit
Stuart Hastings6d56b9f2010-06-05 00:39:29 +00001000 ConstantInt::get(Type::getInt1Ty(VMContext), true),
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001001 DeclNode->getOperand(11), // Virtuality
1002 DeclNode->getOperand(12), // VIndex
Devang Patel4e0d19d2010-02-03 19:57:19 +00001003 DeclNode->getOperand(13), // Containting Type
Devang Patelccff8122010-04-30 19:38:23 +00001004 DeclNode->getOperand(14), // isArtificial
Devang Patelacc6efa2010-06-27 21:04:31 +00001005 DeclNode->getOperand(15), // isOptimized
1006 SPDeclaration.getFunction()
Devang Patele3a18de2009-12-01 23:09:02 +00001007 };
Devang Patelfd5fdc32010-06-28 05:53:08 +00001008 MDNode *Node =MDNode::get(VMContext, &Elts[0], 16);
1009
1010 // Create a named metadata so that we do not lose this mdnode.
1011 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
1012 NMD->addOperand(Node);
1013 return DISubprogram(Node);
Devang Patele3a18de2009-12-01 23:09:02 +00001014}
1015
Chris Lattnera45664f2008-11-10 02:56:27 +00001016/// CreateGlobalVariable - Create a new descriptor for the specified global.
1017DIGlobalVariable
Devang Patel65dbc902009-11-25 17:36:49 +00001018DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1019 StringRef DisplayName,
1020 StringRef LinkageName,
Devang Patel4b945502010-03-09 00:44:10 +00001021 DIFile F,
Devang Patel2e369932010-01-23 00:26:28 +00001022 unsigned LineNo, DIType Ty,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +00001023 bool isDefinition, llvm::GlobalVariable *Val) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001024 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001025 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patele4b27562009-08-28 23:24:31 +00001026 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Devang Patel2db49d72010-05-07 18:11:54 +00001027 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001028 MDString::get(VMContext, Name),
1029 MDString::get(VMContext, DisplayName),
1030 MDString::get(VMContext, LinkageName),
Devang Patel2db49d72010-05-07 18:11:54 +00001031 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001032 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001033 Ty,
Owen Anderson1d0be152009-08-13 21:58:54 +00001034 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1035 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
Devang Patele4b27562009-08-28 23:24:31 +00001036 Val
Chris Lattnera45664f2008-11-10 02:56:27 +00001037 };
Devang Patele4b27562009-08-28 23:24:31 +00001038
1039 Value *const *Vs = &Elts[0];
1040 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1041
1042 // Create a named metadata so that we do not lose this mdnode.
1043 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001044 NMD->addOperand(Node);
Devang Patele4b27562009-08-28 23:24:31 +00001045
1046 return DIGlobalVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001047}
1048
1049
1050/// CreateVariable - Create a new descriptor for the specified variable.
1051DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +00001052 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +00001053 DIFile F,
1054 unsigned LineNo,
Devang Patel6ed0ce32010-05-20 20:35:24 +00001055 DIType Ty, bool AlwaysPreserve) {
Devang Patele4b27562009-08-28 23:24:31 +00001056 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001057 GetTagConstant(Tag),
Devang Patel2db49d72010-05-07 18:11:54 +00001058 Context,
Devang Patele4b27562009-08-28 23:24:31 +00001059 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +00001060 F,
Owen Anderson1d0be152009-08-13 21:58:54 +00001061 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001062 Ty,
Chris Lattnera45664f2008-11-10 02:56:27 +00001063 };
Devang Patel98e1cac2010-05-14 21:01:35 +00001064 MDNode *Node = MDNode::get(VMContext, &Elts[0], 6);
Devang Patel6ed0ce32010-05-20 20:35:24 +00001065 if (AlwaysPreserve) {
1066 // The optimizer may remove local variable. If there is an interest
1067 // to preserve variable info in such situation then stash it in a
1068 // named mdnode.
Devang Patel2f7d5292010-06-16 00:53:55 +00001069 DISubprogram Fn(getDISubprogram(Context));
Devang Patel10de3bb2010-06-21 18:36:58 +00001070 StringRef FName = "fn";
1071 if (Fn.getFunction())
1072 FName = Fn.getFunction()->getName();
Devang Patel10de3bb2010-06-21 18:36:58 +00001073 char One = '\1';
1074 if (FName.startswith(StringRef(&One, 1)))
1075 FName = FName.substr(1);
Devang Patela762b092010-06-22 01:19:38 +00001076 NamedMDNode *FnLocals = M.getNamedMetadata(Twine("llvm.dbg.lv.", FName));
Devang Patel2f7d5292010-06-16 00:53:55 +00001077 if (!FnLocals)
Devang Pateld1bbc6b2010-06-22 01:01:58 +00001078 FnLocals = NamedMDNode::Create(VMContext, Twine("llvm.dbg.lv.", FName),
1079 NULL, 0, &M);
Devang Patel2f7d5292010-06-16 00:53:55 +00001080 FnLocals->addOperand(Node);
Devang Patel98e1cac2010-05-14 21:01:35 +00001081 }
1082 return DIVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001083}
1084
1085
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001086/// CreateComplexVariable - Create a new descriptor for the specified variable
1087/// which has a complex address expression for its address.
1088DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
1089 const std::string &Name,
Devang Patel4b945502010-03-09 00:44:10 +00001090 DIFile F,
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001091 unsigned LineNo,
Devang Patel2e369932010-01-23 00:26:28 +00001092 DIType Ty,
1093 SmallVector<Value *, 9> &addr) {
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001094 SmallVector<Value *, 9> Elts;
1095 Elts.push_back(GetTagConstant(Tag));
Devang Patel2db49d72010-05-07 18:11:54 +00001096 Elts.push_back(Context);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001097 Elts.push_back(MDString::get(VMContext, Name));
Devang Patel2db49d72010-05-07 18:11:54 +00001098 Elts.push_back(F);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001099 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
Devang Patel2db49d72010-05-07 18:11:54 +00001100 Elts.push_back(Ty);
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001101 Elts.insert(Elts.end(), addr.begin(), addr.end());
1102
1103 return DIVariable(MDNode::get(VMContext, &Elts[0], 6+addr.size()));
1104}
1105
1106
Chris Lattnera45664f2008-11-10 02:56:27 +00001107/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +00001108/// specified parent VMContext.
Devang Patel3d821aa2010-02-16 21:39:34 +00001109DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context,
Stuart Hastings99cfb692010-07-08 23:25:39 +00001110 unsigned LineNo, unsigned Col) {
Devang Patele4b27562009-08-28 23:24:31 +00001111 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001112 GetTagConstant(dwarf::DW_TAG_lexical_block),
Devang Patel2db49d72010-05-07 18:11:54 +00001113 Context,
Devang Patel3d821aa2010-02-16 21:39:34 +00001114 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Stuart Hastings99cfb692010-07-08 23:25:39 +00001115 ConstantInt::get(Type::getInt32Ty(VMContext), Col)
Chris Lattnera45664f2008-11-10 02:56:27 +00001116 };
Stuart Hastings99cfb692010-07-08 23:25:39 +00001117 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 4));
Chris Lattnera45664f2008-11-10 02:56:27 +00001118}
1119
Devang Patel6404e4e2009-12-15 19:16:48 +00001120/// CreateNameSpace - This creates new descriptor for a namespace
1121/// with the specified parent context.
1122DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +00001123 DIFile F,
Devang Patel6404e4e2009-12-15 19:16:48 +00001124 unsigned LineNo) {
1125 Value *Elts[] = {
1126 GetTagConstant(dwarf::DW_TAG_namespace),
Devang Patel2db49d72010-05-07 18:11:54 +00001127 Context,
Devang Patel6404e4e2009-12-15 19:16:48 +00001128 MDString::get(VMContext, Name),
Devang Patel2db49d72010-05-07 18:11:54 +00001129 F,
Devang Patel6404e4e2009-12-15 19:16:48 +00001130 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1131 };
1132 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1133}
1134
Devang Patelf98d8fe2009-09-01 01:14:15 +00001135/// CreateLocation - Creates a debug info location.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001136DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
Daniel Dunbara279bc32009-09-20 02:20:51 +00001137 DIScope S, DILocation OrigLoc) {
Devang Patelf98d8fe2009-09-01 01:14:15 +00001138 Value *Elts[] = {
1139 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1140 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
Devang Patel2db49d72010-05-07 18:11:54 +00001141 S,
1142 OrigLoc,
Devang Patelf98d8fe2009-09-01 01:14:15 +00001143 };
1144 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1145}
1146
Chris Lattnera45664f2008-11-10 02:56:27 +00001147//===----------------------------------------------------------------------===//
1148// DIFactory: Routines for inserting code into a function
1149//===----------------------------------------------------------------------===//
1150
Chris Lattnera45664f2008-11-10 02:56:27 +00001151/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001152Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001153 Instruction *InsertBefore) {
Victor Hernandez4cf292a2010-01-26 02:07:38 +00001154 assert(Storage && "no storage passed to dbg.declare");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001155 assert(D.Verify() && "empty DIVariable passed to dbg.declare");
Chris Lattnera45664f2008-11-10 02:56:27 +00001156 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001157 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1158
Victor Hernandez756462b2010-01-18 20:42:09 +00001159 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
Devang Patel2db49d72010-05-07 18:11:54 +00001160 D };
Devang Patel6daf99b2009-11-10 22:05:35 +00001161 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
Mike Stumpe4250392009-10-01 22:08:58 +00001162}
1163
1164/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001165Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001166 BasicBlock *InsertAtEnd) {
Victor Hernandez4cf292a2010-01-26 02:07:38 +00001167 assert(Storage && "no storage passed to dbg.declare");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001168 assert(D.Verify() && "invalid DIVariable passed to dbg.declare");
Mike Stumpe4250392009-10-01 22:08:58 +00001169 if (!DeclareFn)
1170 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1171
Victor Hernandez756462b2010-01-18 20:42:09 +00001172 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
Devang Patel2db49d72010-05-07 18:11:54 +00001173 D };
Devang Patel27a53de2010-01-29 18:30:57 +00001174
1175 // If this block already has a terminator then insert this intrinsic
1176 // before the terminator.
1177 if (TerminatorInst *T = InsertAtEnd->getTerminator())
1178 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1179 else
1180 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
Torok Edwin620f2802008-12-16 09:07:36 +00001181
Victor Hernandezc59b3352009-12-07 21:54:43 +00001182/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001183Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
Victor Hernandezc59b3352009-12-07 21:54:43 +00001184 DIVariable D,
1185 Instruction *InsertBefore) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001186 assert(V && "no value passed to dbg.value");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001187 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001188 if (!ValueFn)
1189 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1190
Victor Hernandezc8b7cd02010-01-20 05:44:11 +00001191 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001192 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
Devang Patel2db49d72010-05-07 18:11:54 +00001193 D };
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001194 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1195}
1196
Victor Hernandezc59b3352009-12-07 21:54:43 +00001197/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001198Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
Victor Hernandezc59b3352009-12-07 21:54:43 +00001199 DIVariable D,
1200 BasicBlock *InsertAtEnd) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001201 assert(V && "no value passed to dbg.value");
Devang Patele9f8f5e2010-05-07 20:54:48 +00001202 assert(D.Verify() && "invalid DIVariable passed to dbg.value");
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001203 if (!ValueFn)
1204 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1205
Victor Hernandezc8b7cd02010-01-20 05:44:11 +00001206 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001207 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
Devang Patel2db49d72010-05-07 18:11:54 +00001208 D };
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001209 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1210}
Devang Patele4b27562009-08-28 23:24:31 +00001211
Devang Pateld2f79a12009-07-28 19:55:13 +00001212//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +00001213// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +00001214//===----------------------------------------------------------------------===//
1215
Devang Patel98c65172009-07-30 18:25:15 +00001216/// processModule - Process entire module and collect debug info.
1217void DebugInfoFinder::processModule(Module &M) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001218 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1219 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1220 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1221 ++BI) {
Devang Patel01c5ff62010-05-04 01:05:02 +00001222 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
Devang Patelb4d31302009-07-31 18:18:52 +00001223 processDeclare(DDI);
Chris Lattner28a9bf62010-04-02 20:44:29 +00001224
1225 DebugLoc Loc = BI->getDebugLoc();
1226 if (Loc.isUnknown())
1227 continue;
1228
1229 LLVMContext &Ctx = BI->getContext();
1230 DIDescriptor Scope(Loc.getScope(Ctx));
1231
1232 if (Scope.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +00001233 addCompileUnit(DICompileUnit(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +00001234 else if (Scope.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001235 processSubprogram(DISubprogram(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +00001236 else if (Scope.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001237 processLexicalBlock(DILexicalBlock(Scope));
Chris Lattner28a9bf62010-04-02 20:44:29 +00001238
1239 if (MDNode *IA = Loc.getInlinedAt(Ctx))
1240 processLocation(DILocation(IA));
Devang Pateld2f79a12009-07-28 19:55:13 +00001241 }
Devang Patele4b27562009-08-28 23:24:31 +00001242
Devang Patelfd5fdc32010-06-28 05:53:08 +00001243 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
1244 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1245 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
1246 if (addGlobalVariable(DIG)) {
1247 addCompileUnit(DIG.getCompileUnit());
1248 processType(DIG.getType());
1249 }
Devang Pateld2f79a12009-07-28 19:55:13 +00001250 }
1251 }
Devang Patelfd5fdc32010-06-28 05:53:08 +00001252
1253 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
1254 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
1255 processSubprogram(DISubprogram(NMD->getOperand(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +00001256}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001257
Devang Patel6daf99b2009-11-10 22:05:35 +00001258/// processLocation - Process DILocation.
1259void DebugInfoFinder::processLocation(DILocation Loc) {
Devang Patel3c91b052010-03-08 20:52:55 +00001260 if (!Loc.Verify()) return;
Devang Patel2db49d72010-05-07 18:11:54 +00001261 DIDescriptor S(Loc.getScope());
Devang Patel6daf99b2009-11-10 22:05:35 +00001262 if (S.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +00001263 addCompileUnit(DICompileUnit(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001264 else if (S.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001265 processSubprogram(DISubprogram(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001266 else if (S.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001267 processLexicalBlock(DILexicalBlock(S));
Devang Patel6daf99b2009-11-10 22:05:35 +00001268 processLocation(Loc.getOrigLocation());
1269}
1270
Devang Patel98c65172009-07-30 18:25:15 +00001271/// processType - Process DIType.
1272void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +00001273 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +00001274 return;
1275
1276 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +00001277 if (DT.isCompositeType()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001278 DICompositeType DCT(DT);
Devang Patel98c65172009-07-30 18:25:15 +00001279 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001280 DIArray DA = DCT.getTypeArray();
Devang Patel3c91b052010-03-08 20:52:55 +00001281 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1282 DIDescriptor D = DA.getElement(i);
1283 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +00001284 processType(DIType(D));
Devang Patel3c91b052010-03-08 20:52:55 +00001285 else if (D.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +00001286 processSubprogram(DISubprogram(D));
Devang Patel3c91b052010-03-08 20:52:55 +00001287 }
Devang Patel6ceea332009-08-31 18:49:10 +00001288 } else if (DT.isDerivedType()) {
Devang Patel2db49d72010-05-07 18:11:54 +00001289 DIDerivedType DDT(DT);
Devang Patel3c91b052010-03-08 20:52:55 +00001290 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001291 }
1292}
1293
Devang Patelbeab41b2009-10-07 22:04:08 +00001294/// processLexicalBlock
1295void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
Devang Patelbeab41b2009-10-07 22:04:08 +00001296 DIScope Context = LB.getContext();
1297 if (Context.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001298 return processLexicalBlock(DILexicalBlock(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +00001299 else
Devang Patel2db49d72010-05-07 18:11:54 +00001300 return processSubprogram(DISubprogram(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +00001301}
1302
Devang Patel98c65172009-07-30 18:25:15 +00001303/// processSubprogram - Process DISubprogram.
1304void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001305 if (!addSubprogram(SP))
1306 return;
1307 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001308 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001309}
1310
Devang Patelb4d31302009-07-31 18:18:52 +00001311/// processDeclare - Process DbgDeclareInst.
1312void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patel3c91b052010-03-08 20:52:55 +00001313 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1314 if (!N) return;
1315
1316 DIDescriptor DV(N);
1317 if (!DV.isVariable())
Devang Patelb4d31302009-07-31 18:18:52 +00001318 return;
1319
Devang Patel2db49d72010-05-07 18:11:54 +00001320 if (!NodesSeen.insert(DV))
Devang Patelb4d31302009-07-31 18:18:52 +00001321 return;
1322
Devang Patel3c91b052010-03-08 20:52:55 +00001323 addCompileUnit(DIVariable(N).getCompileUnit());
1324 processType(DIVariable(N).getType());
Devang Patelb4d31302009-07-31 18:18:52 +00001325}
1326
Devang Patel72bcdb62009-08-10 22:09:58 +00001327/// addType - Add type into Tys.
1328bool DebugInfoFinder::addType(DIType DT) {
Devang Patel3c91b052010-03-08 20:52:55 +00001329 if (!DT.isValid())
Devang Patel72bcdb62009-08-10 22:09:58 +00001330 return false;
1331
Devang Patel2db49d72010-05-07 18:11:54 +00001332 if (!NodesSeen.insert(DT))
Devang Patel72bcdb62009-08-10 22:09:58 +00001333 return false;
1334
Devang Patel2db49d72010-05-07 18:11:54 +00001335 TYs.push_back(DT);
Devang Patel72bcdb62009-08-10 22:09:58 +00001336 return true;
1337}
1338
Devang Pateld2f79a12009-07-28 19:55:13 +00001339/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +00001340bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Patel3c91b052010-03-08 20:52:55 +00001341 if (!CU.Verify())
Devang Pateld2f79a12009-07-28 19:55:13 +00001342 return false;
1343
Devang Patel2db49d72010-05-07 18:11:54 +00001344 if (!NodesSeen.insert(CU))
Devang Pateld2f79a12009-07-28 19:55:13 +00001345 return false;
1346
Devang Patel2db49d72010-05-07 18:11:54 +00001347 CUs.push_back(CU);
Devang Pateld2f79a12009-07-28 19:55:13 +00001348 return true;
1349}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001350
Devang Pateld2f79a12009-07-28 19:55:13 +00001351/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +00001352bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Patel2db49d72010-05-07 18:11:54 +00001353 if (!DIDescriptor(DIG).isGlobalVariable())
Devang Pateld2f79a12009-07-28 19:55:13 +00001354 return false;
1355
Devang Patel2db49d72010-05-07 18:11:54 +00001356 if (!NodesSeen.insert(DIG))
Devang Pateld2f79a12009-07-28 19:55:13 +00001357 return false;
1358
Devang Patel2db49d72010-05-07 18:11:54 +00001359 GVs.push_back(DIG);
Devang Pateld2f79a12009-07-28 19:55:13 +00001360 return true;
1361}
1362
1363// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +00001364bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Patel2db49d72010-05-07 18:11:54 +00001365 if (!DIDescriptor(SP).isSubprogram())
Devang Pateld2f79a12009-07-28 19:55:13 +00001366 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001367
Devang Patel2db49d72010-05-07 18:11:54 +00001368 if (!NodesSeen.insert(SP))
Devang Pateld2f79a12009-07-28 19:55:13 +00001369 return false;
1370
Devang Patel2db49d72010-05-07 18:11:54 +00001371 SPs.push_back(SP);
Devang Pateld2f79a12009-07-28 19:55:13 +00001372 return true;
1373}
1374
Victor Hernandez756462b2010-01-18 20:42:09 +00001375/// Find the debug info descriptor corresponding to this global variable.
1376static Value *findDbgGlobalDeclare(GlobalVariable *V) {
Chris Lattner099b7792009-12-29 09:22:47 +00001377 const Module *M = V->getParent();
1378 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1379 if (!NMD)
Torok Edwinff7d0e92009-03-10 13:41:26 +00001380 return 0;
Chris Lattner099b7792009-12-29 09:22:47 +00001381
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001382 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Devang Patel3c91b052010-03-08 20:52:55 +00001383 DIDescriptor DIG(cast_or_null<MDNode>(NMD->getOperand(i)));
1384 if (!DIG.isGlobalVariable())
Chris Lattner099b7792009-12-29 09:22:47 +00001385 continue;
Devang Patel2db49d72010-05-07 18:11:54 +00001386 if (DIGlobalVariable(DIG).getGlobal() == V)
1387 return DIG;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001388 }
Chris Lattner099b7792009-12-29 09:22:47 +00001389 return 0;
1390}
Torok Edwinff7d0e92009-03-10 13:41:26 +00001391
Chris Lattner099b7792009-12-29 09:22:47 +00001392/// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1393/// It looks through pointer casts too.
Victor Hernandez756462b2010-01-18 20:42:09 +00001394static const DbgDeclareInst *findDbgDeclare(const Value *V) {
Victor Hernandez3a328652010-01-15 19:04:09 +00001395 V = V->stripPointerCasts();
1396
1397 if (!isa<Instruction>(V) && !isa<Argument>(V))
Torok Edwin620f2802008-12-16 09:07:36 +00001398 return 0;
Victor Hernandez3a328652010-01-15 19:04:09 +00001399
1400 const Function *F = NULL;
1401 if (const Instruction *I = dyn_cast<Instruction>(V))
1402 F = I->getParent()->getParent();
1403 else if (const Argument *A = dyn_cast<Argument>(V))
1404 F = A->getParent();
1405
1406 for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
1407 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
1408 BI != BE; ++BI)
1409 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1410 if (DDI->getAddress() == V)
1411 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001412
Chris Lattner099b7792009-12-29 09:22:47 +00001413 return 0;
1414}
Bill Wendlingdc817b62009-05-14 18:26:15 +00001415
Chris Lattner099b7792009-12-29 09:22:47 +00001416bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1417 std::string &Type, unsigned &LineNo,
1418 std::string &File, std::string &Dir) {
1419 DICompileUnit Unit;
1420 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001421
Chris Lattner099b7792009-12-29 09:22:47 +00001422 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1423 Value *DIGV = findDbgGlobalDeclare(GV);
1424 if (!DIGV) return false;
1425 DIGlobalVariable Var(cast<MDNode>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001426
Chris Lattner099b7792009-12-29 09:22:47 +00001427 StringRef D = Var.getDisplayName();
Devang Patel65dbc902009-11-25 17:36:49 +00001428 if (!D.empty())
Chris Lattner099b7792009-12-29 09:22:47 +00001429 DisplayName = D;
1430 LineNo = Var.getLineNumber();
1431 Unit = Var.getCompileUnit();
1432 TypeD = Var.getType();
1433 } else {
1434 const DbgDeclareInst *DDI = findDbgDeclare(V);
1435 if (!DDI) return false;
1436 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1437
1438 StringRef D = Var.getName();
1439 if (!D.empty())
1440 DisplayName = D;
1441 LineNo = Var.getLineNumber();
1442 Unit = Var.getCompileUnit();
1443 TypeD = Var.getType();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001444 }
Devang Patel13e16b62009-06-26 01:49:18 +00001445
Chris Lattner099b7792009-12-29 09:22:47 +00001446 StringRef T = TypeD.getName();
1447 if (!T.empty())
1448 Type = T;
1449 StringRef F = Unit.getFilename();
1450 if (!F.empty())
1451 File = F;
1452 StringRef D = Unit.getDirectory();
1453 if (!D.empty())
1454 Dir = D;
1455 return true;
1456}
Devang Patel9e529c32009-07-02 01:15:24 +00001457
Chris Lattner099b7792009-12-29 09:22:47 +00001458/// getDISubprogram - Find subprogram that is enclosing this scope.
Devang Patele9f8f5e2010-05-07 20:54:48 +00001459DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
Chris Lattner099b7792009-12-29 09:22:47 +00001460 DIDescriptor D(Scope);
Chris Lattner099b7792009-12-29 09:22:47 +00001461 if (D.isSubprogram())
1462 return DISubprogram(Scope);
1463
1464 if (D.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +00001465 return getDISubprogram(DILexicalBlock(Scope).getContext());
Chris Lattner099b7792009-12-29 09:22:47 +00001466
1467 return DISubprogram();
1468}
Devang Patel193f7202009-11-24 01:14:22 +00001469
Chris Lattner099b7792009-12-29 09:22:47 +00001470/// getDICompositeType - Find underlying composite type.
1471DICompositeType llvm::getDICompositeType(DIType T) {
Chris Lattner099b7792009-12-29 09:22:47 +00001472 if (T.isCompositeType())
Devang Patel2db49d72010-05-07 18:11:54 +00001473 return DICompositeType(T);
Chris Lattner099b7792009-12-29 09:22:47 +00001474
1475 if (T.isDerivedType())
Devang Patel2db49d72010-05-07 18:11:54 +00001476 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
Chris Lattner099b7792009-12-29 09:22:47 +00001477
1478 return DICompositeType();
Torok Edwin620f2802008-12-16 09:07:36 +00001479}