]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - opencl/llvm.git/blob - lib/IR/DebugInfo.cpp
Refactor filename/directory in DICompileUnit into a DIFile
[opencl/llvm.git] / lib / IR / DebugInfo.cpp
1 //===--- 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 //===----------------------------------------------------------------------===//
15 #include "llvm/DebugInfo.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/SmallPtrSet.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/Analysis/ValueTracking.h"
20 #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"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/Dwarf.h"
28 #include "llvm/Support/raw_ostream.h"
29 using namespace llvm;
30 using namespace llvm::dwarf;
32 //===----------------------------------------------------------------------===//
33 // DIDescriptor
34 //===----------------------------------------------------------------------===//
36 DIDescriptor::DIDescriptor(const DIFile F) : DbgNode(F.DbgNode) {
37 }
39 DIDescriptor::DIDescriptor(const DISubprogram F) : DbgNode(F.DbgNode) {
40 }
42 DIDescriptor::DIDescriptor(const DILexicalBlockFile F) : DbgNode(F.DbgNode) {
43 }
45 DIDescriptor::DIDescriptor(const DILexicalBlock F) : DbgNode(F.DbgNode) {
46 }
48 DIDescriptor::DIDescriptor(const DIVariable F) : DbgNode(F.DbgNode) {
49 }
51 DIDescriptor::DIDescriptor(const DIType F) : DbgNode(F.DbgNode) {
52 }
54 bool 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 }
69 StringRef
70 DIDescriptor::getStringField(unsigned Elt) const {
71   if (DbgNode == 0)
72     return StringRef();
74   if (Elt < DbgNode->getNumOperands())
75     if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getOperand(Elt)))
76       return MDS->getString();
78   return StringRef();
79 }
81 uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
82   if (DbgNode == 0)
83     return 0;
85   if (Elt < DbgNode->getNumOperands())
86     if (ConstantInt *CI
87         = dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
88       return CI->getZExtValue();
90   return 0;
91 }
93 int64_t DIDescriptor::getInt64Field(unsigned Elt) const {
94   if (DbgNode == 0)
95     return 0;
97   if (Elt < DbgNode->getNumOperands())
98     if (ConstantInt *CI
99         = dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
100       return CI->getSExtValue();
102   return 0;
105 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
106   if (DbgNode == 0)
107     return DIDescriptor();
109   if (Elt < DbgNode->getNumOperands())
110     return
111       DIDescriptor(dyn_cast_or_null<const MDNode>(DbgNode->getOperand(Elt)));
112   return DIDescriptor();
115 GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
116   if (DbgNode == 0)
117     return 0;
119   if (Elt < DbgNode->getNumOperands())
120       return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
121   return 0;
124 Constant *DIDescriptor::getConstantField(unsigned Elt) const {
125   if (DbgNode == 0)
126     return 0;
128   if (Elt < DbgNode->getNumOperands())
129       return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt));
130   return 0;
133 Function *DIDescriptor::getFunctionField(unsigned Elt) const {
134   if (DbgNode == 0)
135     return 0;
137   if (Elt < DbgNode->getNumOperands())
138       return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
139   return 0;
142 void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) {
143   if (DbgNode == 0)
144     return;
146   if (Elt < DbgNode->getNumOperands()) {
147     MDNode *Node = const_cast<MDNode*>(DbgNode);
148     Node->replaceOperandWith(Elt, F);
149   }
152 unsigned DIVariable::getNumAddrElements() const {
153   return DbgNode->getNumOperands()-8;
156 /// getInlinedAt - If this variable is inlined then return inline location.
157 MDNode *DIVariable::getInlinedAt() const {
158   return dyn_cast_or_null<MDNode>(DbgNode->getOperand(7));
161 //===----------------------------------------------------------------------===//
162 // Predicates
163 //===----------------------------------------------------------------------===//
165 /// isBasicType - Return true if the specified tag is legal for
166 /// DIBasicType.
167 bool DIDescriptor::isBasicType() const {
168   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   }
178 /// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
179 bool DIDescriptor::isDerivedType() const {
180   if (!DbgNode) return false;
181   switch (getTag()) {
182   case dwarf::DW_TAG_typedef:
183   case dwarf::DW_TAG_pointer_type:
184   case dwarf::DW_TAG_ptr_to_member_type:
185   case dwarf::DW_TAG_reference_type:
186   case dwarf::DW_TAG_rvalue_reference_type:
187   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:
192   case dwarf::DW_TAG_friend:
193     return true;
194   default:
195     // CompositeTypes are currently modelled as DerivedTypes.
196     return isCompositeType();
197   }
200 /// isCompositeType - Return true if the specified tag is legal for
201 /// DICompositeType.
202 bool DIDescriptor::isCompositeType() const {
203   if (!DbgNode) return false;
204   switch (getTag()) {
205   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:
209   case dwarf::DW_TAG_subroutine_type:
210   case dwarf::DW_TAG_class_type:
211     return true;
212   default:
213     return false;
214   }
217 /// isVariable - Return true if the specified tag is legal for DIVariable.
218 bool DIDescriptor::isVariable() const {
219   if (!DbgNode) return false;
220   switch (getTag()) {
221   case dwarf::DW_TAG_auto_variable:
222   case dwarf::DW_TAG_arg_variable:
223     return true;
224   default:
225     return false;
226   }
229 /// isType - Return true if the specified tag is legal for DIType.
230 bool DIDescriptor::isType() const {
231   return isBasicType() || isCompositeType() || isDerivedType();
234 /// isSubprogram - Return true if the specified tag is legal for
235 /// DISubprogram.
236 bool DIDescriptor::isSubprogram() const {
237   return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
240 /// isGlobalVariable - Return true if the specified tag is legal for
241 /// DIGlobalVariable.
242 bool DIDescriptor::isGlobalVariable() const {
243   return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
244                      getTag() == dwarf::DW_TAG_constant);
247 /// isGlobal - Return true if the specified tag is legal for DIGlobal.
248 bool DIDescriptor::isGlobal() const {
249   return isGlobalVariable();
252 /// isUnspecifiedParmeter - Return true if the specified tag is
253 /// DW_TAG_unspecified_parameters.
254 bool DIDescriptor::isUnspecifiedParameter() const {
255   return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters;
258 /// isScope - Return true if the specified tag is one of the scope
259 /// related tag.
260 bool DIDescriptor::isScope() const {
261   if (!DbgNode) return false;
262   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;
270   }
271   return false;
274 /// isTemplateTypeParameter - Return true if the specified tag is
275 /// DW_TAG_template_type_parameter.
276 bool DIDescriptor::isTemplateTypeParameter() const {
277   return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
280 /// isTemplateValueParameter - Return true if the specified tag is
281 /// DW_TAG_template_value_parameter.
282 bool DIDescriptor::isTemplateValueParameter() const {
283   return DbgNode && getTag() == dwarf::DW_TAG_template_value_parameter;
286 /// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
287 bool DIDescriptor::isCompileUnit() const {
288   return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
291 /// isFile - Return true if the specified tag is DW_TAG_file_type.
292 bool DIDescriptor::isFile() const {
293   return DbgNode && getTag() == dwarf::DW_TAG_file_type;
296 /// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
297 bool DIDescriptor::isNameSpace() const {
298   return DbgNode && getTag() == dwarf::DW_TAG_namespace;
301 /// isLexicalBlockFile - Return true if the specified descriptor is a
302 /// lexical block with an extra file.
303 bool DIDescriptor::isLexicalBlockFile() const {
304   return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
305     (DbgNode->getNumOperands() == 3);
308 /// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
309 bool DIDescriptor::isLexicalBlock() const {
310   return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
311     (DbgNode->getNumOperands() > 3);
314 /// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
315 bool DIDescriptor::isSubrange() const {
316   return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
319 /// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
320 bool DIDescriptor::isEnumerator() const {
321   return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
324 /// isObjCProperty - Return true if the specified tag is DW_TAG
325 bool DIDescriptor::isObjCProperty() const {
326   return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
328 //===----------------------------------------------------------------------===//
329 // Simple Descriptor Constructors and other Methods
330 //===----------------------------------------------------------------------===//
332 DIType::DIType(const MDNode *N) : DIScope(N) {
333   if (!N) return;
334   if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
335     DbgNode = 0;
336   }
339 unsigned DIArray::getNumElements() const {
340   if (!DbgNode)
341     return 0;
342   return DbgNode->getNumOperands();
345 /// replaceAllUsesWith - Replace all uses of debug info referenced by
346 /// this descriptor.
347 void DIType::replaceAllUsesWith(DIDescriptor &D) {
348   if (!DbgNode)
349     return;
351   // 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.
356   if (DbgNode != D) {
357     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));
361     MDNode::deleteTemporary(Node);
362   }
365 /// replaceAllUsesWith - Replace all uses of debug info referenced by
366 /// this descriptor.
367 void DIType::replaceAllUsesWith(MDNode *D) {
368   if (!DbgNode)
369     return;
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   }
385 /// isUnsignedDIType - Return true if type encoding is unsigned.
386 bool DIType::isUnsignedDIType() {
387   DIDerivedType DTy(DbgNode);
388   if (DTy.Verify())
389     return DTy.getTypeDerivedFrom().isUnsignedDIType();
391   DIBasicType BTy(DbgNode);
392   if (BTy.Verify()) {
393     unsigned Encoding = BTy.getEncoding();
394     if (Encoding == dwarf::DW_ATE_unsigned ||
395         Encoding == dwarf::DW_ATE_unsigned_char ||
396         Encoding == dwarf::DW_ATE_boolean)
397       return true;
398   }
399   return false;
402 /// Verify - Verify that a compile unit is well formed.
403 bool DICompileUnit::Verify() const {
404   if (!isCompileUnit())
405     return false;
406   StringRef N = getFilename();
407   if (N.empty())
408     return false;
409   // It is possible that directory and produce string is empty.
410   return DbgNode->getNumOperands() == 13;
413 /// Verify - Verify that an ObjC property is well formed.
414 bool DIObjCProperty::Verify() const {
415   if (!isObjCProperty())
416     return false;
418   DIType Ty = getType();
419   if (!Ty.Verify()) return false;
421   // Don't worry about the rest of the strings for now.
422   return DbgNode->getNumOperands() == 8;
425 /// Verify - Verify that a type descriptor is well formed.
426 bool DIType::Verify() const {
427   if (!isType())
428     return false;
429   if (getContext() && !getContext().Verify())
430     return false;
431   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 &&
434       Tag != dwarf::DW_TAG_ptr_to_member_type &&
435       Tag != dwarf::DW_TAG_reference_type &&
436       Tag != dwarf::DW_TAG_rvalue_reference_type &&
437       Tag != dwarf::DW_TAG_restrict_type &&
438       Tag != dwarf::DW_TAG_array_type &&
439       Tag != dwarf::DW_TAG_enumeration_type &&
440       Tag != dwarf::DW_TAG_subroutine_type &&
441       getFilename().empty())
442     return false;
443   return true;
446 /// Verify - Verify that a basic type descriptor is well formed.
447 bool DIBasicType::Verify() const {
448   return isBasicType() && DbgNode->getNumOperands() == 10;
451 /// Verify - Verify that a derived type descriptor is well formed.
452 bool DIDerivedType::Verify() const {
453   return isDerivedType() && DbgNode->getNumOperands() >= 10 &&
454          DbgNode->getNumOperands() <= 14;
457 /// Verify - Verify that a composite type descriptor is well formed.
458 bool DICompositeType::Verify() const {
459   if (!isCompositeType())
460     return false;
461   if (getContext() && !getContext().Verify())
462     return false;
464   return DbgNode->getNumOperands() >= 10 && DbgNode->getNumOperands() <= 14;
467 /// Verify - Verify that a subprogram descriptor is well formed.
468 bool DISubprogram::Verify() const {
469   if (!isSubprogram())
470     return false;
472   if (getContext() && !getContext().Verify())
473     return false;
475   DICompositeType Ty = getType();
476   if (!Ty.Verify())
477     return false;
478   return DbgNode->getNumOperands() == 21;
481 /// Verify - Verify that a global variable descriptor is well formed.
482 bool DIGlobalVariable::Verify() const {
483   if (!isGlobalVariable())
484     return false;
486   if (getDisplayName().empty())
487     return false;
489   if (getContext() && !getContext().Verify())
490     return false;
492   DIType Ty = getType();
493   if (!Ty.Verify())
494     return false;
496   if (!getGlobal() && !getConstant())
497     return false;
499   return DbgNode->getNumOperands() == 13;
502 /// Verify - Verify that a variable descriptor is well formed.
503 bool DIVariable::Verify() const {
504   if (!isVariable())
505     return false;
507   if (getContext() && !getContext().Verify())
508     return false;
510   DIType Ty = getType();
511   if (!Ty.Verify())
512     return false;
514   return DbgNode->getNumOperands() >= 8;
517 /// Verify - Verify that a location descriptor is well formed.
518 bool DILocation::Verify() const {
519   if (!DbgNode)
520     return false;
522   return DbgNode->getNumOperands() == 4;
525 /// Verify - Verify that a namespace descriptor is well formed.
526 bool DINameSpace::Verify() const {
527   if (!isNameSpace())
528     return false;
529   return DbgNode->getNumOperands() == 5;
532 /// \brief Verify that the file descriptor is well formed.
533 bool DIFile::Verify() const {
534   return isFile() && DbgNode->getNumOperands() == 4;
537 /// \brief Verify that the enumerator descriptor is well formed.
538 bool DIEnumerator::Verify() const {
539   return isEnumerator() && DbgNode->getNumOperands() == 3;
542 /// \brief Verify that the subrange descriptor is well formed.
543 bool DISubrange::Verify() const {
544   return isSubrange() && DbgNode->getNumOperands() == 3;
547 /// \brief Verify that the lexical block descriptor is well formed.
548 bool DILexicalBlock::Verify() const {
549   return isLexicalBlock() && DbgNode->getNumOperands() == 6;
552 /// \brief Verify that the file-scoped lexical block descriptor is well formed.
553 bool DILexicalBlockFile::Verify() const {
554   return isLexicalBlockFile() && DbgNode->getNumOperands() == 3;
557 /// \brief Verify that the template type parameter descriptor is well formed.
558 bool DITemplateTypeParameter::Verify() const {
559   return isTemplateTypeParameter() && DbgNode->getNumOperands() == 7;
562 /// \brief Verify that the template value parameter descriptor is well formed.
563 bool DITemplateValueParameter::Verify() const {
564   return isTemplateValueParameter() && DbgNode->getNumOperands() == 8;
567 /// getOriginalTypeSize - If this type is derived from a base type then
568 /// return base type size.
569 uint64_t DIDerivedType::getOriginalTypeSize() const {
570   unsigned Tag = getTag();
572   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();
577   DIType BaseType = getTypeDerivedFrom();
579   // If this type is not derived from any type then take conservative approach.
580   if (!BaseType.isValid())
581     return getSizeInBits();
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();
590   if (BaseType.isDerivedType())
591     return DIDerivedType(BaseType).getOriginalTypeSize();
593   return BaseType.getSizeInBits();
596 /// getObjCProperty - Return property node, if this ivar is associated with one.
597 MDNode *DIDerivedType::getObjCProperty() const {
598   if (DbgNode->getNumOperands() <= 10)
599     return NULL;
600   return dyn_cast_or_null<MDNode>(DbgNode->getOperand(10));
603 /// isInlinedFnArgument - Return true if this variable provides debugging
604 /// information for an inlined function arguments.
605 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
606   assert(CurFn && "Invalid function");
607   if (!getContext().isSubprogram())
608     return false;
609   // This variable is not inlined function argument if its scope
610   // does not describe current function.
611   return !DISubprogram(getContext()).describes(CurFn);
614 /// describes - Return true if this subprogram provides debugging
615 /// information for the function F.
616 bool DISubprogram::describes(const Function *F) {
617   assert(F && "Invalid function");
618   if (F == getFunction())
619     return true;
620   StringRef Name = getLinkageName();
621   if (Name.empty())
622     Name = getName();
623   if (F->getName() == Name)
624     return true;
625   return false;
628 unsigned DISubprogram::isOptimized() const {
629   assert (DbgNode && "Invalid subprogram descriptor!");
630   if (DbgNode->getNumOperands() == 16)
631     return getUnsignedField(15);
632   return 0;
635 MDNode *DISubprogram::getVariablesNodes() const {
636   if (!DbgNode || DbgNode->getNumOperands() <= 19)
637     return NULL;
638   return dyn_cast_or_null<MDNode>(DbgNode->getOperand(19));
641 DIArray DISubprogram::getVariables() const {
642   if (!DbgNode || DbgNode->getNumOperands() <= 19)
643     return DIArray();
644   if (MDNode *T = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19)))
645     return DIArray(T);
646   return DIArray();
649 StringRef DIScope::getFilename() const {
650   if (!DbgNode)
651     return StringRef();
652   if (isLexicalBlockFile())
653     return DILexicalBlockFile(DbgNode).getFilename();
654   if (isLexicalBlock())
655     return DILexicalBlock(DbgNode).getFilename();
656   if (isSubprogram())
657     return DISubprogram(DbgNode).getFilename();
658   if (isCompileUnit())
659     return DICompileUnit(DbgNode).getFilename();
660   if (isNameSpace())
661     return DINameSpace(DbgNode).getFilename();
662   if (isType())
663     return DIType(DbgNode).getFilename();
664   if (isFile())
665     return DIFile(DbgNode).getFilename();
666   llvm_unreachable("Invalid DIScope!");
669 StringRef DIScope::getDirectory() const {
670   if (!DbgNode)
671     return StringRef();
672   if (isLexicalBlockFile())
673     return DILexicalBlockFile(DbgNode).getDirectory();
674   if (isLexicalBlock())
675     return DILexicalBlock(DbgNode).getDirectory();
676   if (isSubprogram())
677     return DISubprogram(DbgNode).getDirectory();
678   if (isCompileUnit())
679     return DICompileUnit(DbgNode).getDirectory();
680   if (isNameSpace())
681     return DINameSpace(DbgNode).getDirectory();
682   if (isType())
683     return DIType(DbgNode).getDirectory();
684   if (isFile())
685     return DIFile(DbgNode).getDirectory();
686   llvm_unreachable("Invalid DIScope!");
689 DIArray DICompileUnit::getEnumTypes() const {
690   if (!DbgNode || DbgNode->getNumOperands() < 13)
691     return DIArray();
693   if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(8)))
694     return DIArray(N);
695   return DIArray();
698 DIArray DICompileUnit::getRetainedTypes() const {
699   if (!DbgNode || DbgNode->getNumOperands() < 13)
700     return DIArray();
702   if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(9)))
703     return DIArray(N);
704   return DIArray();
707 DIArray DICompileUnit::getSubprograms() const {
708   if (!DbgNode || DbgNode->getNumOperands() < 13)
709     return DIArray();
711   if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(10)))
712     return DIArray(N);
713   return DIArray();
717 DIArray DICompileUnit::getGlobalVariables() const {
718   if (!DbgNode || DbgNode->getNumOperands() < 13)
719     return DIArray();
721   if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(11)))
722     return DIArray(N);
723   return DIArray();
726 /// fixupObjcLikeName - Replace contains special characters used
727 /// in a typical Objective-C names with '.' in a given string.
728 static void fixupObjcLikeName(StringRef Str, SmallVectorImpl<char> &Out) {
729   bool isObjCLike = false;
730   for (size_t i = 0, e = Str.size(); i < e; ++i) {
731     char C = Str[i];
732     if (C == '[')
733       isObjCLike = true;
735     if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
736                        C == '+' || C == '(' || C == ')'))
737       Out.push_back('.');
738     else
739       Out.push_back(C);
740   }
743 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
744 /// suitable to hold function specific information.
745 NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) {
746   SmallString<32> Name = StringRef("llvm.dbg.lv.");
747   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);
756   return M.getNamedMetadata(Name.str());
759 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
760 /// to hold function specific information.
761 NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) {
762   SmallString<32> Name = StringRef("llvm.dbg.lv.");
763   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);
773   return M.getOrInsertNamedMetadata(Name.str());
776 /// 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.
780 DIVariable 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));
790 /// cleanseInlinedVariable - Remove inlined scope from the variable.
791 DIVariable 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)
795     i == 7 ?
796       Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext))):
797       Elts.push_back(DV->getOperand(i));
798   return DIVariable(MDNode::get(VMContext, Elts));
801 /// getDISubprogram - Find subprogram that is enclosing this scope.
802 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
803   DIDescriptor D(Scope);
804   if (D.isSubprogram())
805     return DISubprogram(Scope);
807   if (D.isLexicalBlockFile())
808     return getDISubprogram(DILexicalBlockFile(Scope).getContext());
810   if (D.isLexicalBlock())
811     return getDISubprogram(DILexicalBlock(Scope).getContext());
813   return DISubprogram();
816 /// getDICompositeType - Find underlying composite type.
817 DICompositeType llvm::getDICompositeType(DIType T) {
818   if (T.isCompositeType())
819     return DICompositeType(T);
821   if (T.isDerivedType())
822     return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
824   return DICompositeType();
827 /// isSubprogramContext - Return true if Context is either a subprogram
828 /// or another context nested inside a subprogram.
829 bool 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;
840 //===----------------------------------------------------------------------===//
841 // DebugInfoFinder implementations.
842 //===----------------------------------------------------------------------===//
844 /// processModule - Process entire module and collect debug info.
845 void DebugInfoFinder::processModule(const Module &M) {
846   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);
850       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());
855       }
856       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;
867     }
868   }
871 /// processLocation - Process DILocation.
872 void DebugInfoFinder::processLocation(DILocation Loc) {
873   if (!Loc.Verify()) return;
874   DIDescriptor S(Loc.getScope());
875   if (S.isCompileUnit())
876     addCompileUnit(DICompileUnit(S));
877   else if (S.isSubprogram())
878     processSubprogram(DISubprogram(S));
879   else if (S.isLexicalBlock())
880     processLexicalBlock(DILexicalBlock(S));
881   else if (S.isLexicalBlockFile()) {
882     DILexicalBlockFile DBF = DILexicalBlockFile(S);
883     processLexicalBlock(DILexicalBlock(DBF.getScope()));
884   }
885   processLocation(Loc.getOrigLocation());
888 /// processType - Process DIType.
889 void DebugInfoFinder::processType(DIType DT) {
890   if (!addType(DT))
891     return;
892   if (DT.isCompositeType()) {
893     DICompositeType DCT(DT);
894     processType(DCT.getTypeDerivedFrom());
895     DIArray DA = DCT.getTypeArray();
896     for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
897       DIDescriptor D = DA.getElement(i);
898       if (D.isType())
899         processType(DIType(D));
900       else if (D.isSubprogram())
901         processSubprogram(DISubprogram(D));
902     }
903   } else if (DT.isDerivedType()) {
904     DIDerivedType DDT(DT);
905     processType(DDT.getTypeDerivedFrom());
906   }
909 /// processLexicalBlock
910 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
911   DIScope Context = LB.getContext();
912   if (Context.isLexicalBlock())
913     return processLexicalBlock(DILexicalBlock(Context));
914   else if (Context.isLexicalBlockFile()) {
915     DILexicalBlockFile DBF = DILexicalBlockFile(Context);
916     return processLexicalBlock(DILexicalBlock(DBF.getScope()));
917   }
918   else
919     return processSubprogram(DISubprogram(Context));
922 /// processSubprogram - Process DISubprogram.
923 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
924   if (!addSubprogram(SP))
925     return;
926   processType(SP.getType());
929 /// processDeclare - Process DbgDeclareInst.
930 void DebugInfoFinder::processDeclare(const DbgDeclareInst *DDI) {
931   MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
932   if (!N) return;
934   DIDescriptor DV(N);
935   if (!DV.isVariable())
936     return;
938   if (!NodesSeen.insert(DV))
939     return;
940   processType(DIVariable(N).getType());
943 /// addType - Add type into Tys.
944 bool DebugInfoFinder::addType(DIType DT) {
945   if (!DT.isValid())
946     return false;
948   if (!NodesSeen.insert(DT))
949     return false;
951   TYs.push_back(DT);
952   return true;
955 /// addCompileUnit - Add compile unit into CUs.
956 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
957   if (!CU.Verify())
958     return false;
960   if (!NodesSeen.insert(CU))
961     return false;
963   CUs.push_back(CU);
964   return true;
967 /// addGlobalVariable - Add global variable into GVs.
968 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
969   if (!DIDescriptor(DIG).isGlobalVariable())
970     return false;
972   if (!NodesSeen.insert(DIG))
973     return false;
975   GVs.push_back(DIG);
976   return true;
979 // addSubprogram - Add subprgoram into SPs.
980 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
981   if (!DIDescriptor(SP).isSubprogram())
982     return false;
984   if (!NodesSeen.insert(SP))
985     return false;
987   SPs.push_back(SP);
988   return true;
991 //===----------------------------------------------------------------------===//
992 // DIDescriptor: dump routines for all descriptors.
993 //===----------------------------------------------------------------------===//
995 /// dump - Print descriptor to dbgs() with a newline.
996 void DIDescriptor::dump() const {
997   print(dbgs()); dbgs() << '\n';
1000 /// print - Print descriptor.
1001 void DIDescriptor::print(raw_ostream &OS) const {
1002   if (!DbgNode) return;
1004   if (const char *Tag = dwarf::TagString(getTag()))
1005     OS << "[ " << Tag << " ]";
1007   if (this->isSubrange()) {
1008     DISubrange(DbgNode).printInternal(OS);
1009   } 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);
1027   } else if (this->isObjCProperty()) {
1028     DIObjCProperty(DbgNode).printInternal(OS);
1029   } else if (this->isScope()) {
1030     DIScope(DbgNode).printInternal(OS);
1031   }
1034 void DISubrange::printInternal(raw_ostream &OS) const {
1035   int64_t Count = getCount();
1036   if (Count != -1)
1037     OS << " [" << getLo() << ", " << Count - 1 << ']';
1038   else
1039     OS << " [unbounded]";
1042 void DIScope::printInternal(raw_ostream &OS) const {
1043   OS << " [" << getDirectory() << "/" << getFilename() << ']';
1046 void DICompileUnit::printInternal(raw_ostream &OS) const {
1047   DIScope::printInternal(OS);
1048   if (const char *Lang = dwarf::LanguageString(getLanguage()))
1049     OS << " [" << Lang << ']';
1052 void DIEnumerator::printInternal(raw_ostream &OS) const {
1053   OS << " [" << getName() << " :: " << getEnumValue() << ']';
1056 void DIType::printInternal(raw_ostream &OS) const {
1057   if (!DbgNode) return;
1059   StringRef Res = getName();
1060   if (!Res.empty())
1061     OS << " [" << Res << "]";
1063   // TODO: Print context?
1065   OS << " [line " << getLineNumber()
1066      << ", size " << getSizeInBits()
1067      << ", align " << getAlignInBits()
1068      << ", offset " << getOffsetInBits();
1069   if (isBasicType())
1070     if (const char *Enc =
1071         dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
1072       OS << ", enc " << Enc;
1073   OS << "]";
1075   if (isPrivate())
1076     OS << " [private]";
1077   else if (isProtected())
1078     OS << " [protected]";
1080   if (isArtificial())
1081     OS << " [artificial]";
1083   if (isForwardDecl())
1084     OS << " [fwd]";
1085   if (isVector())
1086     OS << " [vector]";
1087   if (isStaticMember())
1088     OS << " [static]";
1091 void DIDerivedType::printInternal(raw_ostream &OS) const {
1092   DIType::printInternal(OS);
1093   OS << " [from " << getTypeDerivedFrom().getName() << ']';
1096 void DICompositeType::printInternal(raw_ostream &OS) const {
1097   DIType::printInternal(OS);
1098   DIArray A = getTypeArray();
1099   OS << " [" << A.getNumElements() << " elements]";
1102 void DISubprogram::printInternal(raw_ostream &OS) const {
1103   // TODO : Print context
1104   OS << " [line " << getLineNumber() << ']';
1106   if (isLocalToUnit())
1107     OS << " [local]";
1109   if (isDefinition())
1110     OS << " [def]";
1112   if (getScopeLineNumber() != getLineNumber())
1113     OS << " [scope " << getScopeLineNumber() << "]";
1115   if (isPrivate())
1116     OS << " [private]";
1117   else if (isProtected())
1118     OS << " [protected]";
1120   StringRef Res = getName();
1121   if (!Res.empty())
1122     OS << " [" << Res << ']';
1125 void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1126   StringRef Res = getName();
1127   if (!Res.empty())
1128     OS << " [" << Res << ']';
1130   OS << " [line " << getLineNumber() << ']';
1132   // TODO : Print context
1134   if (isLocalToUnit())
1135     OS << " [local]";
1137   if (isDefinition())
1138     OS << " [def]";
1141 void DIVariable::printInternal(raw_ostream &OS) const {
1142   StringRef Res = getName();
1143   if (!Res.empty())
1144     OS << " [" << Res << ']';
1146   OS << " [line " << getLineNumber() << ']';
1149 void DIObjCProperty::printInternal(raw_ostream &OS) const {
1150   StringRef Name = getObjCPropertyName();
1151   if (!Name.empty())
1152     OS << " [" << Name << ']';
1154   OS << " [line " << getLineNumber()
1155      << ", properties " << getUnsignedField(6) << ']';
1158 static 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   }
1179 void 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   }