blob: 1d83c8ca4955c44fd15ef0c493146cf225b1f08a [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
Bill Wendling0bcbd1d2012-06-28 00:05:13 +000015#include "llvm/DebugInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "llvm/ADT/STLExtras.h"
Devang Patele4b27562009-08-28 23:24:31 +000017#include "llvm/ADT/SmallPtrSet.h"
Dan Gohman17aa92c2010-07-21 23:38:33 +000018#include "llvm/ADT/SmallString.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000020#include "llvm/IR/Constants.h"
21#include "llvm/IR/DerivedTypes.h"
22#include "llvm/IR/Instructions.h"
23#include "llvm/IR/IntrinsicInst.h"
24#include "llvm/IR/Intrinsics.h"
25#include "llvm/IR/Module.h"
David Greene0eb5b662009-12-23 19:45:49 +000026#include "llvm/Support/Debug.h"
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000027#include "llvm/Support/Dwarf.h"
Chris Lattnera81d29b2009-08-23 07:33:14 +000028#include "llvm/Support/raw_ostream.h"
Chris Lattnera45664f2008-11-10 02:56:27 +000029using namespace llvm;
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000030using namespace llvm::dwarf;
Chris Lattnera45664f2008-11-10 02:56:27 +000031
32//===----------------------------------------------------------------------===//
33// DIDescriptor
34//===----------------------------------------------------------------------===//
35
Devang Patel5b164b52010-08-02 22:51:46 +000036DIDescriptor::DIDescriptor(const DIFile F) : DbgNode(F.DbgNode) {
37}
38
39DIDescriptor::DIDescriptor(const DISubprogram F) : DbgNode(F.DbgNode) {
40}
41
Eric Christopher6618a242011-10-11 22:59:11 +000042DIDescriptor::DIDescriptor(const DILexicalBlockFile F) : DbgNode(F.DbgNode) {
43}
44
Devang Patel5b164b52010-08-02 22:51:46 +000045DIDescriptor::DIDescriptor(const DILexicalBlock F) : DbgNode(F.DbgNode) {
46}
47
48DIDescriptor::DIDescriptor(const DIVariable F) : DbgNode(F.DbgNode) {
49}
50
51DIDescriptor::DIDescriptor(const DIType F) : DbgNode(F.DbgNode) {
52}
53
David Blaikiec0ec8a42013-03-11 23:39:23 +000054bool DIDescriptor::Verify() const {
55 return DbgNode &&
56 (DIDerivedType(DbgNode).Verify() ||
57 DICompositeType(DbgNode).Verify() || DIBasicType(DbgNode).Verify() ||
58 DIVariable(DbgNode).Verify() || DISubprogram(DbgNode).Verify() ||
59 DIGlobalVariable(DbgNode).Verify() || DIFile(DbgNode).Verify() ||
60 DICompileUnit(DbgNode).Verify() || DINameSpace(DbgNode).Verify() ||
61 DILexicalBlock(DbgNode).Verify() ||
62 DILexicalBlockFile(DbgNode).Verify() ||
63 DISubrange(DbgNode).Verify() || DIEnumerator(DbgNode).Verify() ||
64 DIObjCProperty(DbgNode).Verify() ||
65 DITemplateTypeParameter(DbgNode).Verify() ||
66 DITemplateValueParameter(DbgNode).Verify());
67}
68
Jim Grosbache62b6902010-07-21 21:36:25 +000069StringRef
Devang Patel5ccdd102009-09-29 18:40:58 +000070DIDescriptor::getStringField(unsigned Elt) const {
Devang Patele4b27562009-08-28 23:24:31 +000071 if (DbgNode == 0)
Devang Patel65dbc902009-11-25 17:36:49 +000072 return StringRef();
Chris Lattnera45664f2008-11-10 02:56:27 +000073
Chris Lattner5d0cacd2009-12-31 01:22:29 +000074 if (Elt < DbgNode->getNumOperands())
75 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getOperand(Elt)))
Devang Patel65dbc902009-11-25 17:36:49 +000076 return MDS->getString();
Daniel Dunbarf612ff62009-09-19 20:40:05 +000077
Devang Patel65dbc902009-11-25 17:36:49 +000078 return StringRef();
Chris Lattnera45664f2008-11-10 02:56:27 +000079}
80
81uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +000082 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +000083 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +000084
Chris Lattner5d0cacd2009-12-31 01:22:29 +000085 if (Elt < DbgNode->getNumOperands())
Eric Christopher5f214ae2012-11-20 00:15:36 +000086 if (ConstantInt *CI
87 = dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
Devang Patele4b27562009-08-28 23:24:31 +000088 return CI->getZExtValue();
Daniel Dunbarf612ff62009-09-19 20:40:05 +000089
Chris Lattnera45664f2008-11-10 02:56:27 +000090 return 0;
91}
92
Bill Wendling9127be82012-12-03 19:44:25 +000093int64_t DIDescriptor::getInt64Field(unsigned Elt) const {
94 if (DbgNode == 0)
95 return 0;
96
97 if (Elt < DbgNode->getNumOperands())
98 if (ConstantInt *CI
99 = dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
100 return CI->getSExtValue();
101
102 return 0;
103}
104
Chris Lattnera45664f2008-11-10 02:56:27 +0000105DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000106 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +0000107 return DIDescriptor();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000108
Chris Lattner7a2f3e02010-03-31 05:53:47 +0000109 if (Elt < DbgNode->getNumOperands())
Jim Grosbache62b6902010-07-21 21:36:25 +0000110 return
111 DIDescriptor(dyn_cast_or_null<const MDNode>(DbgNode->getOperand(Elt)));
Devang Patele4b27562009-08-28 23:24:31 +0000112 return DIDescriptor();
Chris Lattnera45664f2008-11-10 02:56:27 +0000113}
114
115GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000116 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +0000117 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000118
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000119 if (Elt < DbgNode->getNumOperands())
120 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
Devang Patele4b27562009-08-28 23:24:31 +0000121 return 0;
Chris Lattnera45664f2008-11-10 02:56:27 +0000122}
123
Devang Patel27398962010-08-09 21:39:24 +0000124Constant *DIDescriptor::getConstantField(unsigned Elt) const {
125 if (DbgNode == 0)
126 return 0;
127
128 if (Elt < DbgNode->getNumOperands())
129 return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt));
130 return 0;
131}
132
Stuart Hastings215aa152010-06-11 20:08:44 +0000133Function *DIDescriptor::getFunctionField(unsigned Elt) const {
134 if (DbgNode == 0)
135 return 0;
136
137 if (Elt < DbgNode->getNumOperands())
138 return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
139 return 0;
140}
141
Alexey Samsonove97a3a42012-10-09 08:13:15 +0000142void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) {
143 if (DbgNode == 0)
144 return;
145
146 if (Elt < DbgNode->getNumOperands()) {
147 MDNode *Node = const_cast<MDNode*>(DbgNode);
148 Node->replaceOperandWith(Elt, F);
149 }
150}
151
Chris Lattnerf0908a32009-12-31 03:02:08 +0000152unsigned DIVariable::getNumAddrElements() const {
Devang Patel23336b42011-07-19 19:41:54 +0000153 return DbgNode->getNumOperands()-8;
Chris Lattnerf0908a32009-12-31 03:02:08 +0000154}
155
Devang Patel40c7e412011-07-20 22:18:50 +0000156/// getInlinedAt - If this variable is inlined then return inline location.
Devang Patel48d726f2011-08-09 01:03:14 +0000157MDNode *DIVariable::getInlinedAt() const {
Devang Patel40c7e412011-07-20 22:18:50 +0000158 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(7));
159}
Chris Lattnerf0908a32009-12-31 03:02:08 +0000160
Chris Lattnera45664f2008-11-10 02:56:27 +0000161//===----------------------------------------------------------------------===//
Devang Patel6ceea332009-08-31 18:49:10 +0000162// Predicates
Chris Lattnera45664f2008-11-10 02:56:27 +0000163//===----------------------------------------------------------------------===//
164
Devang Patel6ceea332009-08-31 18:49:10 +0000165/// isBasicType - Return true if the specified tag is legal for
166/// DIBasicType.
167bool DIDescriptor::isBasicType() const {
Devang Patel734a67c2011-09-14 23:13:28 +0000168 if (!DbgNode) return false;
169 switch (getTag()) {
170 case dwarf::DW_TAG_base_type:
171 case dwarf::DW_TAG_unspecified_type:
172 return true;
173 default:
174 return false;
175 }
Torok Edwinb07fbd92008-12-13 08:25:29 +0000176}
Chris Lattnera45664f2008-11-10 02:56:27 +0000177
Devang Patel6ceea332009-08-31 18:49:10 +0000178/// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
179bool DIDescriptor::isDerivedType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000180 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000181 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000182 case dwarf::DW_TAG_typedef:
183 case dwarf::DW_TAG_pointer_type:
David Blaikie62fdfb52013-01-07 05:51:15 +0000184 case dwarf::DW_TAG_ptr_to_member_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000185 case dwarf::DW_TAG_reference_type:
Eric Christopher791e6292012-05-19 01:36:37 +0000186 case dwarf::DW_TAG_rvalue_reference_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000187 case dwarf::DW_TAG_const_type:
188 case dwarf::DW_TAG_volatile_type:
189 case dwarf::DW_TAG_restrict_type:
190 case dwarf::DW_TAG_member:
191 case dwarf::DW_TAG_inheritance:
Devang Patel49d96382010-08-23 23:16:25 +0000192 case dwarf::DW_TAG_friend:
Chris Lattnera45664f2008-11-10 02:56:27 +0000193 return true;
194 default:
Devang Patele4b27562009-08-28 23:24:31 +0000195 // CompositeTypes are currently modelled as DerivedTypes.
Devang Patel6ceea332009-08-31 18:49:10 +0000196 return isCompositeType();
Chris Lattnera45664f2008-11-10 02:56:27 +0000197 }
198}
199
Chris Lattnera45664f2008-11-10 02:56:27 +0000200/// isCompositeType - Return true if the specified tag is legal for
201/// DICompositeType.
Devang Patel6ceea332009-08-31 18:49:10 +0000202bool DIDescriptor::isCompositeType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000203 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000204 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000205 case dwarf::DW_TAG_array_type:
206 case dwarf::DW_TAG_structure_type:
207 case dwarf::DW_TAG_union_type:
208 case dwarf::DW_TAG_enumeration_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000209 case dwarf::DW_TAG_subroutine_type:
Devang Patel25cb0d72009-03-25 03:52:06 +0000210 case dwarf::DW_TAG_class_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000211 return true;
212 default:
213 return false;
214 }
215}
216
Chris Lattnera45664f2008-11-10 02:56:27 +0000217/// isVariable - Return true if the specified tag is legal for DIVariable.
Devang Patel6ceea332009-08-31 18:49:10 +0000218bool DIDescriptor::isVariable() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000219 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000220 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000221 case dwarf::DW_TAG_auto_variable:
222 case dwarf::DW_TAG_arg_variable:
Chris Lattnera45664f2008-11-10 02:56:27 +0000223 return true;
224 default:
225 return false;
226 }
227}
228
Devang Patelecbeb1a2009-09-30 22:34:41 +0000229/// isType - Return true if the specified tag is legal for DIType.
230bool DIDescriptor::isType() const {
231 return isBasicType() || isCompositeType() || isDerivedType();
232}
233
Devang Patel6ceea332009-08-31 18:49:10 +0000234/// isSubprogram - Return true if the specified tag is legal for
235/// DISubprogram.
236bool DIDescriptor::isSubprogram() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000237 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
Devang Patel6ceea332009-08-31 18:49:10 +0000238}
239
240/// isGlobalVariable - Return true if the specified tag is legal for
241/// DIGlobalVariable.
242bool DIDescriptor::isGlobalVariable() const {
Devang Patel29368072010-08-10 07:11:13 +0000243 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
244 getTag() == dwarf::DW_TAG_constant);
Devang Patel6ceea332009-08-31 18:49:10 +0000245}
246
Devang Patelecbeb1a2009-09-30 22:34:41 +0000247/// isGlobal - Return true if the specified tag is legal for DIGlobal.
248bool DIDescriptor::isGlobal() const {
249 return isGlobalVariable();
250}
251
Devang Patel716a67f2011-02-03 00:13:47 +0000252/// isUnspecifiedParmeter - Return true if the specified tag is
Devang Pateld6747df2010-10-06 20:50:40 +0000253/// DW_TAG_unspecified_parameters.
254bool DIDescriptor::isUnspecifiedParameter() const {
255 return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters;
256}
257
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000258/// isScope - Return true if the specified tag is one of the scope
Devang Patel43d98b32009-08-31 20:44:45 +0000259/// related tag.
260bool DIDescriptor::isScope() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000261 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000262 switch (getTag()) {
263 case dwarf::DW_TAG_compile_unit:
264 case dwarf::DW_TAG_lexical_block:
265 case dwarf::DW_TAG_subprogram:
266 case dwarf::DW_TAG_namespace:
267 return true;
268 default:
269 break;
Devang Patel43d98b32009-08-31 20:44:45 +0000270 }
271 return false;
272}
Devang Patel6ceea332009-08-31 18:49:10 +0000273
Devang Patel7e2cb112011-02-02 21:38:25 +0000274/// isTemplateTypeParameter - Return true if the specified tag is
275/// DW_TAG_template_type_parameter.
276bool DIDescriptor::isTemplateTypeParameter() const {
277 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
278}
279
Devang Patele7d93872011-02-02 22:35:53 +0000280/// isTemplateValueParameter - Return true if the specified tag is
281/// DW_TAG_template_value_parameter.
282bool DIDescriptor::isTemplateValueParameter() const {
283 return DbgNode && getTag() == dwarf::DW_TAG_template_value_parameter;
284}
285
Devang Patelc9f322d2009-08-31 21:34:44 +0000286/// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
287bool DIDescriptor::isCompileUnit() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000288 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
Devang Patelc9f322d2009-08-31 21:34:44 +0000289}
290
Devang Patel7aa81892010-03-08 22:27:22 +0000291/// isFile - Return true if the specified tag is DW_TAG_file_type.
292bool DIDescriptor::isFile() const {
293 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
294}
295
Devang Patel6404e4e2009-12-15 19:16:48 +0000296/// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
297bool DIDescriptor::isNameSpace() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000298 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
Devang Patel6404e4e2009-12-15 19:16:48 +0000299}
300
Eric Christopher6618a242011-10-11 22:59:11 +0000301/// isLexicalBlockFile - Return true if the specified descriptor is a
302/// lexical block with an extra file.
303bool DIDescriptor::isLexicalBlockFile() const {
304 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
305 (DbgNode->getNumOperands() == 3);
306}
307
Devang Patel5e005d82009-08-31 22:00:15 +0000308/// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
309bool DIDescriptor::isLexicalBlock() const {
Eric Christopher6618a242011-10-11 22:59:11 +0000310 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
311 (DbgNode->getNumOperands() > 3);
Devang Patel5e005d82009-08-31 22:00:15 +0000312}
313
Devang Patelecbeb1a2009-09-30 22:34:41 +0000314/// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
315bool DIDescriptor::isSubrange() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000316 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000317}
318
319/// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
320bool DIDescriptor::isEnumerator() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000321 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000322}
323
Devang Patel1ea02d42012-02-04 00:59:25 +0000324/// isObjCProperty - Return true if the specified tag is DW_TAG
325bool DIDescriptor::isObjCProperty() const {
Eric Christopher6c31ee22012-03-29 21:35:05 +0000326 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
Devang Patel1ea02d42012-02-04 00:59:25 +0000327}
Devang Patel6ceea332009-08-31 18:49:10 +0000328//===----------------------------------------------------------------------===//
329// Simple Descriptor Constructors and other Methods
330//===----------------------------------------------------------------------===//
331
Devang Patele9f8f5e2010-05-07 20:54:48 +0000332DIType::DIType(const MDNode *N) : DIScope(N) {
Devang Patel6ceea332009-08-31 18:49:10 +0000333 if (!N) return;
334 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
335 DbgNode = 0;
336 }
337}
338
Devang Patel68afdc32009-01-05 18:33:01 +0000339unsigned DIArray::getNumElements() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000340 if (!DbgNode)
341 return 0;
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000342 return DbgNode->getNumOperands();
Devang Patel68afdc32009-01-05 18:33:01 +0000343}
Chris Lattnera45664f2008-11-10 02:56:27 +0000344
Devang Patelc4999d72009-07-22 18:23:44 +0000345/// replaceAllUsesWith - Replace all uses of debug info referenced by
Dan Gohman872814a2010-07-21 18:54:18 +0000346/// this descriptor.
Dan Gohman489b29b2010-08-20 22:02:26 +0000347void DIType::replaceAllUsesWith(DIDescriptor &D) {
Devang Patel3c91b052010-03-08 20:52:55 +0000348 if (!DbgNode)
Devang Patelc4999d72009-07-22 18:23:44 +0000349 return;
350
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000351 // Since we use a TrackingVH for the node, its easy for clients to manufacture
352 // legitimate situations where they want to replaceAllUsesWith() on something
353 // which, due to uniquing, has merged with the source. We shield clients from
354 // this detail by allowing a value to be replaced with replaceAllUsesWith()
355 // itself.
Devang Pateled66bf52010-05-07 18:19:32 +0000356 if (DbgNode != D) {
Devang Patele9f8f5e2010-05-07 20:54:48 +0000357 MDNode *Node = const_cast<MDNode*>(DbgNode);
358 const MDNode *DN = D;
359 const Value *V = cast_or_null<Value>(DN);
360 Node->replaceAllUsesWith(const_cast<Value*>(V));
Dan Gohman489b29b2010-08-20 22:02:26 +0000361 MDNode::deleteTemporary(Node);
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000362 }
Devang Patelc4999d72009-07-22 18:23:44 +0000363}
364
Devang Patel0a2551d2010-12-08 20:18:20 +0000365/// replaceAllUsesWith - Replace all uses of debug info referenced by
366/// this descriptor.
367void DIType::replaceAllUsesWith(MDNode *D) {
368 if (!DbgNode)
369 return;
370
371 // Since we use a TrackingVH for the node, its easy for clients to manufacture
372 // legitimate situations where they want to replaceAllUsesWith() on something
373 // which, due to uniquing, has merged with the source. We shield clients from
374 // this detail by allowing a value to be replaced with replaceAllUsesWith()
375 // itself.
376 if (DbgNode != D) {
377 MDNode *Node = const_cast<MDNode*>(DbgNode);
378 const MDNode *DN = D;
379 const Value *V = cast_or_null<Value>(DN);
380 Node->replaceAllUsesWith(const_cast<Value*>(V));
381 MDNode::deleteTemporary(Node);
382 }
383}
384
Devang Patel6f9d8ff2011-08-15 17:57:41 +0000385/// isUnsignedDIType - Return true if type encoding is unsigned.
386bool DIType::isUnsignedDIType() {
387 DIDerivedType DTy(DbgNode);
388 if (DTy.Verify())
389 return DTy.getTypeDerivedFrom().isUnsignedDIType();
390
391 DIBasicType BTy(DbgNode);
392 if (BTy.Verify()) {
393 unsigned Encoding = BTy.getEncoding();
394 if (Encoding == dwarf::DW_ATE_unsigned ||
David Blaikiefe07db32013-01-19 23:00:25 +0000395 Encoding == dwarf::DW_ATE_unsigned_char ||
396 Encoding == dwarf::DW_ATE_boolean)
Devang Patel6f9d8ff2011-08-15 17:57:41 +0000397 return true;
398 }
399 return false;
400}
401
Devang Patelb79b5352009-01-19 23:21:49 +0000402/// Verify - Verify that a compile unit is well formed.
403bool DICompileUnit::Verify() const {
David Blaikiec0ec8a42013-03-11 23:39:23 +0000404 if (!isCompileUnit())
Devang Patelb79b5352009-01-19 23:21:49 +0000405 return false;
Devang Patel65dbc902009-11-25 17:36:49 +0000406 StringRef N = getFilename();
407 if (N.empty())
Bill Wendling0582ae92009-03-13 04:39:26 +0000408 return false;
Devang Patelb79b5352009-01-19 23:21:49 +0000409 // It is possible that directory and produce string is empty.
David Blaikie46561ce2013-03-12 22:43:04 +0000410 return DbgNode->getNumOperands() == 14;
Devang Patelb79b5352009-01-19 23:21:49 +0000411}
412
Eric Christopherb8ca9882012-03-29 08:42:56 +0000413/// Verify - Verify that an ObjC property is well formed.
414bool DIObjCProperty::Verify() const {
David Blaikiec0ec8a42013-03-11 23:39:23 +0000415 if (!isObjCProperty())
Eric Christopherb8ca9882012-03-29 08:42:56 +0000416 return false;
David Blaikiec0ec8a42013-03-11 23:39:23 +0000417
Eric Christopherb8ca9882012-03-29 08:42:56 +0000418 DIType Ty = getType();
419 if (!Ty.Verify()) return false;
420
421 // Don't worry about the rest of the strings for now.
David Blaikiec0ec8a42013-03-11 23:39:23 +0000422 return DbgNode->getNumOperands() == 8;
Eric Christopherb8ca9882012-03-29 08:42:56 +0000423}
424
Devang Patelb79b5352009-01-19 23:21:49 +0000425/// Verify - Verify that a type descriptor is well formed.
426bool DIType::Verify() const {
David Blaikiec0ec8a42013-03-11 23:39:23 +0000427 if (!isType())
Devang Patelb79b5352009-01-19 23:21:49 +0000428 return false;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000429 if (getContext() && !getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000430 return false;
Devang Patelb71bbf92010-11-02 20:41:13 +0000431 unsigned Tag = getTag();
432 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
433 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
David Blaikie62fdfb52013-01-07 05:51:15 +0000434 Tag != dwarf::DW_TAG_ptr_to_member_type &&
Eric Christopher791e6292012-05-19 01:36:37 +0000435 Tag != dwarf::DW_TAG_reference_type &&
436 Tag != dwarf::DW_TAG_rvalue_reference_type &&
Eric Christopher9a1e0e22013-01-08 01:53:52 +0000437 Tag != dwarf::DW_TAG_restrict_type &&
Eric Christopher791e6292012-05-19 01:36:37 +0000438 Tag != dwarf::DW_TAG_array_type &&
439 Tag != dwarf::DW_TAG_enumeration_type &&
440 Tag != dwarf::DW_TAG_subroutine_type &&
441 getFilename().empty())
Devang Patelb79b5352009-01-19 23:21:49 +0000442 return false;
443 return true;
444}
445
Devang Patel0c4720c2010-08-23 18:25:56 +0000446/// Verify - Verify that a basic type descriptor is well formed.
447bool DIBasicType::Verify() const {
David Blaikiec0ec8a42013-03-11 23:39:23 +0000448 return isBasicType() && DbgNode->getNumOperands() == 10;
Devang Patel0c4720c2010-08-23 18:25:56 +0000449}
450
451/// Verify - Verify that a derived type descriptor is well formed.
452bool DIDerivedType::Verify() const {
David Blaikiec0ec8a42013-03-11 23:39:23 +0000453 return isDerivedType() && DbgNode->getNumOperands() >= 10 &&
454 DbgNode->getNumOperands() <= 14;
Devang Patel0c4720c2010-08-23 18:25:56 +0000455}
456
Devang Patelb79b5352009-01-19 23:21:49 +0000457/// Verify - Verify that a composite type descriptor is well formed.
458bool DICompositeType::Verify() const {
David Blaikiec0ec8a42013-03-11 23:39:23 +0000459 if (!isCompositeType())
Devang Patelb79b5352009-01-19 23:21:49 +0000460 return false;
Devang Patel94c7ddb2011-08-16 22:09:43 +0000461 if (getContext() && !getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000462 return false;
463
David Blaikiec0ec8a42013-03-11 23:39:23 +0000464 return DbgNode->getNumOperands() >= 10 && DbgNode->getNumOperands() <= 14;
Devang Patelb79b5352009-01-19 23:21:49 +0000465}
466
467/// Verify - Verify that a subprogram descriptor is well formed.
468bool DISubprogram::Verify() const {
David Blaikiec0ec8a42013-03-11 23:39:23 +0000469 if (!isSubprogram())
Devang Patelb79b5352009-01-19 23:21:49 +0000470 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000471
Devang Patel94c7ddb2011-08-16 22:09:43 +0000472 if (getContext() && !getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000473 return false;
474
475 DICompositeType Ty = getType();
Devang Patel3c91b052010-03-08 20:52:55 +0000476 if (!Ty.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000477 return false;
David Blaikiec0ec8a42013-03-11 23:39:23 +0000478 return DbgNode->getNumOperands() == 21;
Devang Patelb79b5352009-01-19 23:21:49 +0000479}
480
481/// Verify - Verify that a global variable descriptor is well formed.
482bool DIGlobalVariable::Verify() const {
David Blaikiec0ec8a42013-03-11 23:39:23 +0000483 if (!isGlobalVariable())
Devang Patelb79b5352009-01-19 23:21:49 +0000484 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000485
Devang Patel65dbc902009-11-25 17:36:49 +0000486 if (getDisplayName().empty())
Devang Patel84c73e92009-11-06 17:58:12 +0000487 return false;
488
Devang Patel94c7ddb2011-08-16 22:09:43 +0000489 if (getContext() && !getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000490 return false;
491
492 DIType Ty = getType();
493 if (!Ty.Verify())
494 return false;
495
Devang Patel27398962010-08-09 21:39:24 +0000496 if (!getGlobal() && !getConstant())
Devang Patelb79b5352009-01-19 23:21:49 +0000497 return false;
498
David Blaikiec0ec8a42013-03-11 23:39:23 +0000499 return DbgNode->getNumOperands() == 13;
Devang Patelb79b5352009-01-19 23:21:49 +0000500}
501
502/// Verify - Verify that a variable descriptor is well formed.
503bool DIVariable::Verify() const {
David Blaikiec0ec8a42013-03-11 23:39:23 +0000504 if (!isVariable())
Devang Patelb79b5352009-01-19 23:21:49 +0000505 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000506
Devang Patel94c7ddb2011-08-16 22:09:43 +0000507 if (getContext() && !getContext().Verify())
Devang Patel62077af2010-05-07 21:42:24 +0000508 return false;
509
Devang Patelb79b5352009-01-19 23:21:49 +0000510 DIType Ty = getType();
511 if (!Ty.Verify())
512 return false;
513
David Blaikiec0ec8a42013-03-11 23:39:23 +0000514 return DbgNode->getNumOperands() >= 8;
Devang Patelb79b5352009-01-19 23:21:49 +0000515}
516
Devang Patel3c91b052010-03-08 20:52:55 +0000517/// Verify - Verify that a location descriptor is well formed.
518bool DILocation::Verify() const {
519 if (!DbgNode)
520 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000521
Devang Patel3c91b052010-03-08 20:52:55 +0000522 return DbgNode->getNumOperands() == 4;
523}
524
Devang Patel47e22652010-05-07 23:04:32 +0000525/// Verify - Verify that a namespace descriptor is well formed.
526bool DINameSpace::Verify() const {
David Blaikiec0ec8a42013-03-11 23:39:23 +0000527 if (!isNameSpace())
Devang Patel47e22652010-05-07 23:04:32 +0000528 return false;
David Blaikiec0ec8a42013-03-11 23:39:23 +0000529 return DbgNode->getNumOperands() == 5;
530}
531
532/// \brief Verify that the file descriptor is well formed.
533bool DIFile::Verify() const {
534 return isFile() && DbgNode->getNumOperands() == 4;
535}
536
537/// \brief Verify that the enumerator descriptor is well formed.
538bool DIEnumerator::Verify() const {
539 return isEnumerator() && DbgNode->getNumOperands() == 3;
540}
541
542/// \brief Verify that the subrange descriptor is well formed.
543bool DISubrange::Verify() const {
544 return isSubrange() && DbgNode->getNumOperands() == 3;
545}
546
547/// \brief Verify that the lexical block descriptor is well formed.
548bool DILexicalBlock::Verify() const {
549 return isLexicalBlock() && DbgNode->getNumOperands() == 6;
550}
551
552/// \brief Verify that the file-scoped lexical block descriptor is well formed.
553bool DILexicalBlockFile::Verify() const {
554 return isLexicalBlockFile() && DbgNode->getNumOperands() == 3;
555}
556
557/// \brief Verify that the template type parameter descriptor is well formed.
558bool DITemplateTypeParameter::Verify() const {
559 return isTemplateTypeParameter() && DbgNode->getNumOperands() == 7;
560}
561
562/// \brief Verify that the template value parameter descriptor is well formed.
563bool DITemplateValueParameter::Verify() const {
564 return isTemplateValueParameter() && DbgNode->getNumOperands() == 8;
Devang Patel47e22652010-05-07 23:04:32 +0000565}
566
Devang Patel36375ee2009-02-17 21:23:59 +0000567/// getOriginalTypeSize - If this type is derived from a base type then
568/// return base type size.
569uint64_t DIDerivedType::getOriginalTypeSize() const {
Devang Patel61ecbd12009-11-04 23:48:00 +0000570 unsigned Tag = getTag();
Eric Christopher2e1b0c02011-12-16 23:42:45 +0000571
Bill Wendlinge8778242012-06-26 23:22:18 +0000572 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
573 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
574 Tag != dwarf::DW_TAG_restrict_type)
575 return getSizeInBits();
Jim Grosbache62b6902010-07-21 21:36:25 +0000576
Bill Wendlinge8778242012-06-26 23:22:18 +0000577 DIType BaseType = getTypeDerivedFrom();
578
579 // If this type is not derived from any type then take conservative approach.
580 if (!BaseType.isValid())
581 return getSizeInBits();
582
583 // If this is a derived type, go ahead and get the base type, unless it's a
584 // reference then it's just the size of the field. Pointer types have no need
585 // of this since they're a different type of qualification on the type.
586 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
587 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
588 return getSizeInBits();
589
590 if (BaseType.isDerivedType())
591 return DIDerivedType(BaseType).getOriginalTypeSize();
592
593 return BaseType.getSizeInBits();
Devang Patel36375ee2009-02-17 21:23:59 +0000594}
Devang Patelb79b5352009-01-19 23:21:49 +0000595
Devang Patel6588abf2012-02-06 17:49:43 +0000596/// getObjCProperty - Return property node, if this ivar is associated with one.
597MDNode *DIDerivedType::getObjCProperty() const {
David Blaikiec0ec8a42013-03-11 23:39:23 +0000598 if (DbgNode->getNumOperands() <= 10)
Devang Patel6588abf2012-02-06 17:49:43 +0000599 return NULL;
600 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(10));
601}
602
Jim Grosbache62b6902010-07-21 21:36:25 +0000603/// isInlinedFnArgument - Return true if this variable provides debugging
Devang Patel719f6a92010-04-29 20:40:36 +0000604/// information for an inlined function arguments.
605bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
606 assert(CurFn && "Invalid function");
607 if (!getContext().isSubprogram())
608 return false;
Jim Grosbache62b6902010-07-21 21:36:25 +0000609 // This variable is not inlined function argument if its scope
Devang Patel719f6a92010-04-29 20:40:36 +0000610 // does not describe current function.
Bill Wendlinge8778242012-06-26 23:22:18 +0000611 return !DISubprogram(getContext()).describes(CurFn);
Devang Patel719f6a92010-04-29 20:40:36 +0000612}
613
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000614/// describes - Return true if this subprogram provides debugging
615/// information for the function F.
616bool DISubprogram::describes(const Function *F) {
Chris Lattner099b7792009-12-29 09:22:47 +0000617 assert(F && "Invalid function");
Devang Patelffd33cd2010-06-16 06:42:02 +0000618 if (F == getFunction())
619 return true;
Devang Patel65dbc902009-11-25 17:36:49 +0000620 StringRef Name = getLinkageName();
621 if (Name.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +0000622 Name = getName();
Devang Patel65dbc902009-11-25 17:36:49 +0000623 if (F->getName() == Name)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000624 return true;
625 return false;
626}
627
Jim Grosbache62b6902010-07-21 21:36:25 +0000628unsigned DISubprogram::isOptimized() const {
Devang Patelccff8122010-04-30 19:38:23 +0000629 assert (DbgNode && "Invalid subprogram descriptor!");
630 if (DbgNode->getNumOperands() == 16)
631 return getUnsignedField(15);
632 return 0;
633}
634
Devang Patel93d39be2011-08-19 23:28:12 +0000635MDNode *DISubprogram::getVariablesNodes() const {
636 if (!DbgNode || DbgNode->getNumOperands() <= 19)
637 return NULL;
David Blaikief839eed2013-02-04 05:56:36 +0000638 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(19));
Devang Patel93d39be2011-08-19 23:28:12 +0000639}
640
641DIArray DISubprogram::getVariables() const {
642 if (!DbgNode || DbgNode->getNumOperands() <= 19)
643 return DIArray();
644 if (MDNode *T = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
David Blaikief839eed2013-02-04 05:56:36 +0000645 return DIArray(T);
Devang Patel93d39be2011-08-19 23:28:12 +0000646 return DIArray();
647}
648
Devang Patel65dbc902009-11-25 17:36:49 +0000649StringRef DIScope::getFilename() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000650 if (!DbgNode)
651 return StringRef();
Eric Christopher6618a242011-10-11 22:59:11 +0000652 if (isLexicalBlockFile())
653 return DILexicalBlockFile(DbgNode).getFilename();
Jim Grosbache62b6902010-07-21 21:36:25 +0000654 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000655 return DILexicalBlock(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000656 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000657 return DISubprogram(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000658 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000659 return DICompileUnit(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000660 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000661 return DINameSpace(DbgNode).getFilename();
Devang Patel77bf2952010-03-08 22:02:50 +0000662 if (isType())
663 return DIType(DbgNode).getFilename();
Devang Patel7aa81892010-03-08 22:27:22 +0000664 if (isFile())
665 return DIFile(DbgNode).getFilename();
Craig Topper85814382012-02-07 05:05:23 +0000666 llvm_unreachable("Invalid DIScope!");
Devang Patelecbeb1a2009-09-30 22:34:41 +0000667}
668
Devang Patel65dbc902009-11-25 17:36:49 +0000669StringRef DIScope::getDirectory() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000670 if (!DbgNode)
671 return StringRef();
Eric Christopher6618a242011-10-11 22:59:11 +0000672 if (isLexicalBlockFile())
673 return DILexicalBlockFile(DbgNode).getDirectory();
Jim Grosbache62b6902010-07-21 21:36:25 +0000674 if (isLexicalBlock())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000675 return DILexicalBlock(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000676 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000677 return DISubprogram(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000678 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000679 return DICompileUnit(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000680 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000681 return DINameSpace(DbgNode).getDirectory();
Devang Patel77bf2952010-03-08 22:02:50 +0000682 if (isType())
683 return DIType(DbgNode).getDirectory();
Devang Patel7aa81892010-03-08 22:27:22 +0000684 if (isFile())
685 return DIFile(DbgNode).getDirectory();
Craig Topper85814382012-02-07 05:05:23 +0000686 llvm_unreachable("Invalid DIScope!");
Devang Patelecbeb1a2009-09-30 22:34:41 +0000687}
688
Devang Patel94c7ddb2011-08-16 22:09:43 +0000689DIArray DICompileUnit::getEnumTypes() const {
David Blaikie46561ce2013-03-12 22:43:04 +0000690 if (!DbgNode || DbgNode->getNumOperands() < 14)
691 return DIArray();
692
693 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(9)))
694 return DIArray(N);
695 return DIArray();
696}
697
698DIArray DICompileUnit::getRetainedTypes() const {
699 if (!DbgNode || DbgNode->getNumOperands() < 14)
Devang Patel94c7ddb2011-08-16 22:09:43 +0000700 return DIArray();
701
702 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(10)))
David Blaikiea8eefc72013-02-02 05:56:24 +0000703 return DIArray(N);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000704 return DIArray();
705}
706
David Blaikie46561ce2013-03-12 22:43:04 +0000707DIArray DICompileUnit::getSubprograms() const {
708 if (!DbgNode || DbgNode->getNumOperands() < 14)
Devang Patel94c7ddb2011-08-16 22:09:43 +0000709 return DIArray();
710
711 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(11)))
David Blaikiea8eefc72013-02-02 05:56:24 +0000712 return DIArray(N);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000713 return DIArray();
714}
715
David Blaikie46561ce2013-03-12 22:43:04 +0000716
717DIArray DICompileUnit::getGlobalVariables() const {
718 if (!DbgNode || DbgNode->getNumOperands() < 14)
Devang Patel94c7ddb2011-08-16 22:09:43 +0000719 return DIArray();
720
721 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(12)))
David Blaikiea8eefc72013-02-02 05:56:24 +0000722 return DIArray(N);
Devang Patel94c7ddb2011-08-16 22:09:43 +0000723 return DIArray();
724}
725
Devang Patel62367042010-11-10 22:19:21 +0000726/// fixupObjcLikeName - Replace contains special characters used
727/// in a typical Objective-C names with '.' in a given string.
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000728static void fixupObjcLikeName(StringRef Str, SmallVectorImpl<char> &Out) {
729 bool isObjCLike = false;
Devang Patel62367042010-11-10 22:19:21 +0000730 for (size_t i = 0, e = Str.size(); i < e; ++i) {
731 char C = Str[i];
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000732 if (C == '[')
733 isObjCLike = true;
734
735 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
736 C == '+' || C == '(' || C == ')'))
737 Out.push_back('.');
738 else
739 Out.push_back(C);
Devang Patel62367042010-11-10 22:19:21 +0000740 }
741}
742
Eric Christopher5f214ae2012-11-20 00:15:36 +0000743/// getFnSpecificMDNode - Return a NameMDNode, if available, that is
Devang Patel62367042010-11-10 22:19:21 +0000744/// suitable to hold function specific information.
Devang Patel93d39be2011-08-19 23:28:12 +0000745NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) {
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000746 SmallString<32> Name = StringRef("llvm.dbg.lv.");
Devang Patel93d39be2011-08-19 23:28:12 +0000747 StringRef FName = "fn";
748 if (Fn.getFunction())
749 FName = Fn.getFunction()->getName();
750 else
751 FName = Fn.getName();
752 char One = '\1';
753 if (FName.startswith(StringRef(&One, 1)))
754 FName = FName.substr(1);
755 fixupObjcLikeName(FName, Name);
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000756 return M.getNamedMetadata(Name.str());
Devang Patel62367042010-11-10 22:19:21 +0000757}
758
Duncan Sands291bb702011-03-02 20:30:37 +0000759/// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
760/// to hold function specific information.
Devang Patel93d39be2011-08-19 23:28:12 +0000761NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) {
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000762 SmallString<32> Name = StringRef("llvm.dbg.lv.");
Devang Patel93d39be2011-08-19 23:28:12 +0000763 StringRef FName = "fn";
764 if (Fn.getFunction())
765 FName = Fn.getFunction()->getName();
766 else
767 FName = Fn.getName();
768 char One = '\1';
769 if (FName.startswith(StringRef(&One, 1)))
770 FName = FName.substr(1);
771 fixupObjcLikeName(FName, Name);
Eric Christopher5f214ae2012-11-20 00:15:36 +0000772
Benjamin Kramer1a81d482011-06-18 14:42:42 +0000773 return M.getOrInsertNamedMetadata(Name.str());
Devang Patel1a7ca032010-09-28 18:08:20 +0000774}
775
Devang Patel23336b42011-07-19 19:41:54 +0000776/// createInlinedVariable - Create a new inlined variable based on current
777/// variable.
778/// @param DV Current Variable.
779/// @param InlinedScope Location at current variable is inlined.
780DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
781 LLVMContext &VMContext) {
782 SmallVector<Value *, 16> Elts;
783 // Insert inlined scope as 7th element.
784 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
785 i == 7 ? Elts.push_back(InlinedScope) :
786 Elts.push_back(DV->getOperand(i));
787 return DIVariable(MDNode::get(VMContext, Elts));
788}
Devang Patel1a7ca032010-09-28 18:08:20 +0000789
Devang Patelb549bcf2011-08-10 21:50:54 +0000790/// cleanseInlinedVariable - Remove inlined scope from the variable.
791DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
792 SmallVector<Value *, 16> Elts;
793 // Insert inlined scope as 7th element.
794 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
Eric Christopher5f214ae2012-11-20 00:15:36 +0000795 i == 7 ?
Bill Wendling0735e812012-07-06 17:46:28 +0000796 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext))):
Devang Patelb549bcf2011-08-10 21:50:54 +0000797 Elts.push_back(DV->getOperand(i));
798 return DIVariable(MDNode::get(VMContext, Elts));
799}
800
Bill Wendling0bcbd1d2012-06-28 00:05:13 +0000801/// getDISubprogram - Find subprogram that is enclosing this scope.
802DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
803 DIDescriptor D(Scope);
804 if (D.isSubprogram())
805 return DISubprogram(Scope);
806
807 if (D.isLexicalBlockFile())
808 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
Eric Christopher5f214ae2012-11-20 00:15:36 +0000809
Bill Wendling0bcbd1d2012-06-28 00:05:13 +0000810 if (D.isLexicalBlock())
811 return getDISubprogram(DILexicalBlock(Scope).getContext());
812
813 return DISubprogram();
814}
815
816/// getDICompositeType - Find underlying composite type.
817DICompositeType llvm::getDICompositeType(DIType T) {
818 if (T.isCompositeType())
819 return DICompositeType(T);
820
821 if (T.isDerivedType())
822 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
823
824 return DICompositeType();
825}
826
827/// isSubprogramContext - Return true if Context is either a subprogram
828/// or another context nested inside a subprogram.
829bool llvm::isSubprogramContext(const MDNode *Context) {
830 if (!Context)
831 return false;
832 DIDescriptor D(Context);
833 if (D.isSubprogram())
834 return true;
835 if (D.isType())
836 return isSubprogramContext(DIType(Context).getContext());
837 return false;
838}
839
Devang Pateld2f79a12009-07-28 19:55:13 +0000840//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +0000841// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +0000842//===----------------------------------------------------------------------===//
843
Devang Patel98c65172009-07-30 18:25:15 +0000844/// processModule - Process entire module and collect debug info.
Eric Christopherc4639d62012-11-19 22:42:15 +0000845void DebugInfoFinder::processModule(const Module &M) {
Devang Patelcd2db162011-10-17 22:30:34 +0000846 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
847 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
848 DICompileUnit CU(CU_Nodes->getOperand(i));
849 addCompileUnit(CU);
David Blaikiec0ec8a42013-03-11 23:39:23 +0000850 DIArray GVs = CU.getGlobalVariables();
851 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
852 DIGlobalVariable DIG(GVs.getElement(i));
853 if (addGlobalVariable(DIG))
854 processType(DIG.getType());
Devang Patelcd2db162011-10-17 22:30:34 +0000855 }
David Blaikiec0ec8a42013-03-11 23:39:23 +0000856 DIArray SPs = CU.getSubprograms();
857 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
858 processSubprogram(DISubprogram(SPs.getElement(i)));
859 DIArray EnumTypes = CU.getEnumTypes();
860 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
861 processType(DIType(EnumTypes.getElement(i)));
862 DIArray RetainedTypes = CU.getRetainedTypes();
863 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
864 processType(DIType(RetainedTypes.getElement(i)));
865 // FIXME: We really shouldn't be bailing out after visiting just one CU
866 return;
Devang Patelcd2db162011-10-17 22:30:34 +0000867 }
868 }
Devang Pateld2f79a12009-07-28 19:55:13 +0000869}
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000870
Devang Patel6daf99b2009-11-10 22:05:35 +0000871/// processLocation - Process DILocation.
872void DebugInfoFinder::processLocation(DILocation Loc) {
Devang Patel3c91b052010-03-08 20:52:55 +0000873 if (!Loc.Verify()) return;
Devang Patel2db49d72010-05-07 18:11:54 +0000874 DIDescriptor S(Loc.getScope());
Devang Patel6daf99b2009-11-10 22:05:35 +0000875 if (S.isCompileUnit())
Devang Patel2db49d72010-05-07 18:11:54 +0000876 addCompileUnit(DICompileUnit(S));
Devang Patel6daf99b2009-11-10 22:05:35 +0000877 else if (S.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +0000878 processSubprogram(DISubprogram(S));
Devang Patel6daf99b2009-11-10 22:05:35 +0000879 else if (S.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +0000880 processLexicalBlock(DILexicalBlock(S));
Eric Christopher6618a242011-10-11 22:59:11 +0000881 else if (S.isLexicalBlockFile()) {
882 DILexicalBlockFile DBF = DILexicalBlockFile(S);
883 processLexicalBlock(DILexicalBlock(DBF.getScope()));
884 }
Devang Patel6daf99b2009-11-10 22:05:35 +0000885 processLocation(Loc.getOrigLocation());
886}
887
Devang Patel98c65172009-07-30 18:25:15 +0000888/// processType - Process DIType.
889void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +0000890 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +0000891 return;
Devang Patel6ceea332009-08-31 18:49:10 +0000892 if (DT.isCompositeType()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000893 DICompositeType DCT(DT);
Devang Patel98c65172009-07-30 18:25:15 +0000894 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +0000895 DIArray DA = DCT.getTypeArray();
Devang Patel3c91b052010-03-08 20:52:55 +0000896 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
897 DIDescriptor D = DA.getElement(i);
898 if (D.isType())
Devang Patel2db49d72010-05-07 18:11:54 +0000899 processType(DIType(D));
Devang Patel3c91b052010-03-08 20:52:55 +0000900 else if (D.isSubprogram())
Devang Patel2db49d72010-05-07 18:11:54 +0000901 processSubprogram(DISubprogram(D));
Devang Patel3c91b052010-03-08 20:52:55 +0000902 }
Devang Patel6ceea332009-08-31 18:49:10 +0000903 } else if (DT.isDerivedType()) {
Devang Patel2db49d72010-05-07 18:11:54 +0000904 DIDerivedType DDT(DT);
Devang Patel3c91b052010-03-08 20:52:55 +0000905 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +0000906 }
907}
908
Devang Patelbeab41b2009-10-07 22:04:08 +0000909/// processLexicalBlock
910void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
Devang Patelbeab41b2009-10-07 22:04:08 +0000911 DIScope Context = LB.getContext();
912 if (Context.isLexicalBlock())
Devang Patel2db49d72010-05-07 18:11:54 +0000913 return processLexicalBlock(DILexicalBlock(Context));
Eric Christopher6618a242011-10-11 22:59:11 +0000914 else if (Context.isLexicalBlockFile()) {
915 DILexicalBlockFile DBF = DILexicalBlockFile(Context);
916 return processLexicalBlock(DILexicalBlock(DBF.getScope()));
917 }
Devang Patelbeab41b2009-10-07 22:04:08 +0000918 else
Devang Patel2db49d72010-05-07 18:11:54 +0000919 return processSubprogram(DISubprogram(Context));
Devang Patelbeab41b2009-10-07 22:04:08 +0000920}
921
Devang Patel98c65172009-07-30 18:25:15 +0000922/// processSubprogram - Process DISubprogram.
923void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +0000924 if (!addSubprogram(SP))
925 return;
Devang Patel98c65172009-07-30 18:25:15 +0000926 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +0000927}
928
Devang Patelb4d31302009-07-31 18:18:52 +0000929/// processDeclare - Process DbgDeclareInst.
Eric Christopherc4639d62012-11-19 22:42:15 +0000930void DebugInfoFinder::processDeclare(const DbgDeclareInst *DDI) {
Devang Patel3c91b052010-03-08 20:52:55 +0000931 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
932 if (!N) return;
933
934 DIDescriptor DV(N);
935 if (!DV.isVariable())
Devang Patelb4d31302009-07-31 18:18:52 +0000936 return;
937
Devang Patel2db49d72010-05-07 18:11:54 +0000938 if (!NodesSeen.insert(DV))
Devang Patelb4d31302009-07-31 18:18:52 +0000939 return;
Devang Patel3c91b052010-03-08 20:52:55 +0000940 processType(DIVariable(N).getType());
Devang Patelb4d31302009-07-31 18:18:52 +0000941}
942
Devang Patel72bcdb62009-08-10 22:09:58 +0000943/// addType - Add type into Tys.
944bool DebugInfoFinder::addType(DIType DT) {
Devang Patel3c91b052010-03-08 20:52:55 +0000945 if (!DT.isValid())
Devang Patel72bcdb62009-08-10 22:09:58 +0000946 return false;
947
Devang Patel2db49d72010-05-07 18:11:54 +0000948 if (!NodesSeen.insert(DT))
Devang Patel72bcdb62009-08-10 22:09:58 +0000949 return false;
950
Devang Patel2db49d72010-05-07 18:11:54 +0000951 TYs.push_back(DT);
Devang Patel72bcdb62009-08-10 22:09:58 +0000952 return true;
953}
954
Devang Pateld2f79a12009-07-28 19:55:13 +0000955/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +0000956bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Patel3c91b052010-03-08 20:52:55 +0000957 if (!CU.Verify())
Devang Pateld2f79a12009-07-28 19:55:13 +0000958 return false;
959
Devang Patel2db49d72010-05-07 18:11:54 +0000960 if (!NodesSeen.insert(CU))
Devang Pateld2f79a12009-07-28 19:55:13 +0000961 return false;
962
Devang Patel2db49d72010-05-07 18:11:54 +0000963 CUs.push_back(CU);
Devang Pateld2f79a12009-07-28 19:55:13 +0000964 return true;
965}
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000966
Devang Pateld2f79a12009-07-28 19:55:13 +0000967/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +0000968bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Patel2db49d72010-05-07 18:11:54 +0000969 if (!DIDescriptor(DIG).isGlobalVariable())
Devang Pateld2f79a12009-07-28 19:55:13 +0000970 return false;
971
Devang Patel2db49d72010-05-07 18:11:54 +0000972 if (!NodesSeen.insert(DIG))
Devang Pateld2f79a12009-07-28 19:55:13 +0000973 return false;
974
Devang Patel2db49d72010-05-07 18:11:54 +0000975 GVs.push_back(DIG);
Devang Pateld2f79a12009-07-28 19:55:13 +0000976 return true;
977}
978
979// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +0000980bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Patel2db49d72010-05-07 18:11:54 +0000981 if (!DIDescriptor(SP).isSubprogram())
Devang Pateld2f79a12009-07-28 19:55:13 +0000982 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000983
Devang Patel2db49d72010-05-07 18:11:54 +0000984 if (!NodesSeen.insert(SP))
Devang Pateld2f79a12009-07-28 19:55:13 +0000985 return false;
986
Devang Patel2db49d72010-05-07 18:11:54 +0000987 SPs.push_back(SP);
Devang Pateld2f79a12009-07-28 19:55:13 +0000988 return true;
989}
990
Bill Wendlinge8778242012-06-26 23:22:18 +0000991//===----------------------------------------------------------------------===//
992// DIDescriptor: dump routines for all descriptors.
993//===----------------------------------------------------------------------===//
994
995/// dump - Print descriptor to dbgs() with a newline.
996void DIDescriptor::dump() const {
997 print(dbgs()); dbgs() << '\n';
998}
999
1000/// print - Print descriptor.
1001void DIDescriptor::print(raw_ostream &OS) const {
1002 if (!DbgNode) return;
1003
Bill Wendling97a75922012-06-28 02:17:58 +00001004 if (const char *Tag = dwarf::TagString(getTag()))
1005 OS << "[ " << Tag << " ]";
Bill Wendlinge8778242012-06-26 23:22:18 +00001006
1007 if (this->isSubrange()) {
1008 DISubrange(DbgNode).printInternal(OS);
Bill Wendlinge8778242012-06-26 23:22:18 +00001009 } else if (this->isCompileUnit()) {
1010 DICompileUnit(DbgNode).printInternal(OS);
1011 } else if (this->isFile()) {
1012 DIFile(DbgNode).printInternal(OS);
1013 } else if (this->isEnumerator()) {
1014 DIEnumerator(DbgNode).printInternal(OS);
1015 } else if (this->isBasicType()) {
1016 DIType(DbgNode).printInternal(OS);
1017 } else if (this->isDerivedType()) {
1018 DIDerivedType(DbgNode).printInternal(OS);
1019 } else if (this->isCompositeType()) {
1020 DICompositeType(DbgNode).printInternal(OS);
1021 } else if (this->isSubprogram()) {
1022 DISubprogram(DbgNode).printInternal(OS);
1023 } else if (this->isGlobalVariable()) {
1024 DIGlobalVariable(DbgNode).printInternal(OS);
1025 } else if (this->isVariable()) {
1026 DIVariable(DbgNode).printInternal(OS);
Bill Wendling2da1a162012-07-06 19:12:31 +00001027 } else if (this->isObjCProperty()) {
1028 DIObjCProperty(DbgNode).printInternal(OS);
Bill Wendling625252f2012-07-06 23:06:16 +00001029 } else if (this->isScope()) {
1030 DIScope(DbgNode).printInternal(OS);
Bill Wendlinge8778242012-06-26 23:22:18 +00001031 }
1032}
1033
1034void DISubrange::printInternal(raw_ostream &OS) const {
Bill Wendling9493dae2012-12-04 21:34:03 +00001035 int64_t Count = getCount();
1036 if (Count != -1)
1037 OS << " [" << getLo() << ", " << Count - 1 << ']';
1038 else
Bill Wendling46defa52012-12-05 21:43:30 +00001039 OS << " [unbounded]";
Bill Wendlinge8778242012-06-26 23:22:18 +00001040}
1041
1042void DIScope::printInternal(raw_ostream &OS) const {
1043 OS << " [" << getDirectory() << "/" << getFilename() << ']';
1044}
1045
1046void DICompileUnit::printInternal(raw_ostream &OS) const {
1047 DIScope::printInternal(OS);
David Blaikiea9b13172013-02-04 05:31:37 +00001048 if (const char *Lang = dwarf::LanguageString(getLanguage()))
1049 OS << " [" << Lang << ']';
Bill Wendlinge8778242012-06-26 23:22:18 +00001050}
1051
1052void DIEnumerator::printInternal(raw_ostream &OS) const {
1053 OS << " [" << getName() << " :: " << getEnumValue() << ']';
1054}
1055
1056void DIType::printInternal(raw_ostream &OS) const {
1057 if (!DbgNode) return;
1058
1059 StringRef Res = getName();
1060 if (!Res.empty())
1061 OS << " [" << Res << "]";
1062
1063 // TODO: Print context?
1064
1065 OS << " [line " << getLineNumber()
1066 << ", size " << getSizeInBits()
1067 << ", align " << getAlignInBits()
1068 << ", offset " << getOffsetInBits();
1069 if (isBasicType())
Eric Christopher5f214ae2012-11-20 00:15:36 +00001070 if (const char *Enc =
Bill Wendlingfc1c70a2012-06-28 02:12:20 +00001071 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
1072 OS << ", enc " << Enc;
Bill Wendlinge8778242012-06-26 23:22:18 +00001073 OS << "]";
1074
1075 if (isPrivate())
1076 OS << " [private]";
1077 else if (isProtected())
1078 OS << " [protected]";
1079
David Blaikie0fecdfb2013-01-08 00:31:02 +00001080 if (isArtificial())
1081 OS << " [artificial]";
1082
Bill Wendlinge8778242012-06-26 23:22:18 +00001083 if (isForwardDecl())
1084 OS << " [fwd]";
Eric Christopher9a1e0e22013-01-08 01:53:52 +00001085 if (isVector())
1086 OS << " [vector]";
Eric Christopher6b6061f2013-01-16 01:22:23 +00001087 if (isStaticMember())
1088 OS << " [static]";
Bill Wendlinge8778242012-06-26 23:22:18 +00001089}
1090
1091void DIDerivedType::printInternal(raw_ostream &OS) const {
1092 DIType::printInternal(OS);
1093 OS << " [from " << getTypeDerivedFrom().getName() << ']';
1094}
1095
1096void DICompositeType::printInternal(raw_ostream &OS) const {
1097 DIType::printInternal(OS);
1098 DIArray A = getTypeArray();
1099 OS << " [" << A.getNumElements() << " elements]";
1100}
1101
1102void DISubprogram::printInternal(raw_ostream &OS) const {
Bill Wendlinge8778242012-06-26 23:22:18 +00001103 // TODO : Print context
Bill Wendlinge8778242012-06-26 23:22:18 +00001104 OS << " [line " << getLineNumber() << ']';
1105
1106 if (isLocalToUnit())
1107 OS << " [local]";
1108
1109 if (isDefinition())
1110 OS << " [def]";
1111
1112 if (getScopeLineNumber() != getLineNumber())
1113 OS << " [scope " << getScopeLineNumber() << "]";
Bill Wendlingee17cee2012-07-06 23:43:12 +00001114
David Blaikiee302b6c2013-01-05 21:39:33 +00001115 if (isPrivate())
1116 OS << " [private]";
1117 else if (isProtected())
1118 OS << " [protected]";
1119
Bill Wendlingee17cee2012-07-06 23:43:12 +00001120 StringRef Res = getName();
1121 if (!Res.empty())
1122 OS << " [" << Res << ']';
Bill Wendlinge8778242012-06-26 23:22:18 +00001123}
1124
1125void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1126 StringRef Res = getName();
1127 if (!Res.empty())
1128 OS << " [" << Res << ']';
1129
1130 OS << " [line " << getLineNumber() << ']';
1131
1132 // TODO : Print context
1133
1134 if (isLocalToUnit())
1135 OS << " [local]";
1136
1137 if (isDefinition())
1138 OS << " [def]";
1139}
1140
1141void DIVariable::printInternal(raw_ostream &OS) const {
1142 StringRef Res = getName();
1143 if (!Res.empty())
1144 OS << " [" << Res << ']';
1145
1146 OS << " [line " << getLineNumber() << ']';
1147}
1148
Bill Wendling2da1a162012-07-06 19:12:31 +00001149void DIObjCProperty::printInternal(raw_ostream &OS) const {
1150 StringRef Name = getObjCPropertyName();
1151 if (!Name.empty())
1152 OS << " [" << Name << ']';
1153
1154 OS << " [line " << getLineNumber()
1155 << ", properties " << getUnsignedField(6) << ']';
1156}
1157
Bill Wendlinge8778242012-06-26 23:22:18 +00001158static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
1159 const LLVMContext &Ctx) {
1160 if (!DL.isUnknown()) { // Print source line info.
1161 DIScope Scope(DL.getScope(Ctx));
1162 // Omit the directory, because it's likely to be long and uninteresting.
1163 if (Scope.Verify())
1164 CommentOS << Scope.getFilename();
1165 else
1166 CommentOS << "<unknown>";
1167 CommentOS << ':' << DL.getLine();
1168 if (DL.getCol() != 0)
1169 CommentOS << ':' << DL.getCol();
1170 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
1171 if (!InlinedAtDL.isUnknown()) {
1172 CommentOS << " @[ ";
1173 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
1174 CommentOS << " ]";
1175 }
1176 }
1177}
1178
1179void DIVariable::printExtendedName(raw_ostream &OS) const {
1180 const LLVMContext &Ctx = DbgNode->getContext();
1181 StringRef Res = getName();
1182 if (!Res.empty())
1183 OS << Res << "," << getLineNumber();
1184 if (MDNode *InlinedAt = getInlinedAt()) {
1185 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
1186 if (!InlinedAtDL.isUnknown()) {
1187 OS << " @[";
1188 printDebugLoc(InlinedAtDL, OS, Ctx);
1189 OS << "]";
1190 }
1191 }
1192}