Home | History | Annotate | Download | only in Analysis
      1 //===--- llvm/Analysis/DebugInfo.h - Debug Information Helpers --*- C++ -*-===//
      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 defines a bunch of datatypes that are useful for creating and
     11 // walking debug info in LLVM IR form. They essentially provide wrappers around
     12 // the information in the global variables that's needed when constructing the
     13 // DWARF information.
     14 //
     15 //===----------------------------------------------------------------------===//
     16 
     17 #ifndef LLVM_ANALYSIS_DEBUGINFO_H
     18 #define LLVM_ANALYSIS_DEBUGINFO_H
     19 
     20 #include "llvm/ADT/SmallVector.h"
     21 #include "llvm/ADT/SmallPtrSet.h"
     22 #include "llvm/ADT/StringRef.h"
     23 #include "llvm/Support/Dwarf.h"
     24 
     25 namespace llvm {
     26   class BasicBlock;
     27   class Constant;
     28   class Function;
     29   class GlobalVariable;
     30   class Module;
     31   class Type;
     32   class Value;
     33   class DbgDeclareInst;
     34   class Instruction;
     35   class MDNode;
     36   class NamedMDNode;
     37   class LLVMContext;
     38   class raw_ostream;
     39 
     40   class DIFile;
     41   class DISubprogram;
     42   class DILexicalBlock;
     43   class DIVariable;
     44   class DIType;
     45 
     46   /// DIDescriptor - A thin wraper around MDNode to access encoded debug info.
     47   /// This should not be stored in a container, because underly MDNode may
     48   /// change in certain situations.
     49   class DIDescriptor {
     50   public:
     51     enum {
     52       FlagPrivate            = 1 << 0,
     53       FlagProtected          = 1 << 1,
     54       FlagFwdDecl            = 1 << 2,
     55       FlagAppleBlock         = 1 << 3,
     56       FlagBlockByrefStruct   = 1 << 4,
     57       FlagVirtual            = 1 << 5,
     58       FlagArtificial         = 1 << 6,
     59       FlagExplicit           = 1 << 7,
     60       FlagPrototyped         = 1 << 8,
     61       FlagObjcClassComplete  = 1 << 9
     62     };
     63   protected:
     64     const MDNode *DbgNode;
     65 
     66     StringRef getStringField(unsigned Elt) const;
     67     unsigned getUnsignedField(unsigned Elt) const {
     68       return (unsigned)getUInt64Field(Elt);
     69     }
     70     uint64_t getUInt64Field(unsigned Elt) const;
     71     DIDescriptor getDescriptorField(unsigned Elt) const;
     72 
     73     template <typename DescTy>
     74     DescTy getFieldAs(unsigned Elt) const {
     75       return DescTy(getDescriptorField(Elt));
     76     }
     77 
     78     GlobalVariable *getGlobalVariableField(unsigned Elt) const;
     79     Constant *getConstantField(unsigned Elt) const;
     80     Function *getFunctionField(unsigned Elt) const;
     81 
     82   public:
     83     explicit DIDescriptor() : DbgNode(0) {}
     84     explicit DIDescriptor(const MDNode *N) : DbgNode(N) {}
     85     explicit DIDescriptor(const DIFile F);
     86     explicit DIDescriptor(const DISubprogram F);
     87     explicit DIDescriptor(const DILexicalBlock F);
     88     explicit DIDescriptor(const DIVariable F);
     89     explicit DIDescriptor(const DIType F);
     90 
     91     bool Verify() const { return DbgNode != 0; }
     92 
     93     operator MDNode *() const { return const_cast<MDNode*>(DbgNode); }
     94     MDNode *operator ->() const { return const_cast<MDNode*>(DbgNode); }
     95 
     96     unsigned getVersion() const {
     97       return getUnsignedField(0) & LLVMDebugVersionMask;
     98     }
     99 
    100     unsigned getTag() const {
    101       return getUnsignedField(0) & ~LLVMDebugVersionMask;
    102     }
    103 
    104     /// print - print descriptor.
    105     void print(raw_ostream &OS) const;
    106 
    107     /// dump - print descriptor to dbgs() with a newline.
    108     void dump() const;
    109 
    110     bool isDerivedType() const;
    111     bool isCompositeType() const;
    112     bool isBasicType() const;
    113     bool isVariable() const;
    114     bool isSubprogram() const;
    115     bool isGlobalVariable() const;
    116     bool isScope() const;
    117     bool isFile() const;
    118     bool isCompileUnit() const;
    119     bool isNameSpace() const;
    120     bool isLexicalBlock() const;
    121     bool isSubrange() const;
    122     bool isEnumerator() const;
    123     bool isType() const;
    124     bool isGlobal() const;
    125     bool isUnspecifiedParameter() const;
    126     bool isTemplateTypeParameter() const;
    127     bool isTemplateValueParameter() const;
    128   };
    129 
    130   /// DISubrange - This is used to represent ranges, for array bounds.
    131   class DISubrange : public DIDescriptor {
    132   public:
    133     explicit DISubrange(const MDNode *N = 0) : DIDescriptor(N) {}
    134 
    135     int64_t getLo() const { return (int64_t)getUInt64Field(1); }
    136     int64_t getHi() const { return (int64_t)getUInt64Field(2); }
    137   };
    138 
    139   /// DIArray - This descriptor holds an array of descriptors.
    140   class DIArray : public DIDescriptor {
    141   public:
    142     explicit DIArray(const MDNode *N = 0)
    143       : DIDescriptor(N) {}
    144 
    145     unsigned getNumElements() const;
    146     DIDescriptor getElement(unsigned Idx) const {
    147       return getDescriptorField(Idx);
    148     }
    149   };
    150 
    151   /// DIScope - A base class for various scopes.
    152   class DIScope : public DIDescriptor {
    153   public:
    154     explicit DIScope(const MDNode *N = 0) : DIDescriptor (N) {}
    155     virtual ~DIScope() {}
    156 
    157     StringRef getFilename() const;
    158     StringRef getDirectory() const;
    159   };
    160 
    161   /// DICompileUnit - A wrapper for a compile unit.
    162   class DICompileUnit : public DIScope {
    163   public:
    164     explicit DICompileUnit(const MDNode *N = 0) : DIScope(N) {}
    165 
    166     unsigned getLanguage() const   { return getUnsignedField(2); }
    167     StringRef getFilename() const  { return getStringField(3);   }
    168     StringRef getDirectory() const { return getStringField(4);   }
    169     StringRef getProducer() const  { return getStringField(5);   }
    170 
    171     /// isMain - Each input file is encoded as a separate compile unit in LLVM
    172     /// debugging information output. However, many target specific tool chains
    173     /// prefer to encode only one compile unit in an object file. In this
    174     /// situation, the LLVM code generator will include  debugging information
    175     /// entities in the compile unit that is marked as main compile unit. The
    176     /// code generator accepts maximum one main compile unit per module. If a
    177     /// module does not contain any main compile unit then the code generator
    178     /// will emit multiple compile units in the output object file.
    179 
    180     bool isMain() const                { return getUnsignedField(6) != 0; }
    181     bool isOptimized() const           { return getUnsignedField(7) != 0; }
    182     StringRef getFlags() const       { return getStringField(8);   }
    183     unsigned getRunTimeVersion() const { return getUnsignedField(9); }
    184 
    185     /// Verify - Verify that a compile unit is well formed.
    186     bool Verify() const;
    187 
    188     /// print - print compile unit.
    189     void print(raw_ostream &OS) const;
    190 
    191     /// dump - print compile unit to dbgs() with a newline.
    192     void dump() const;
    193   };
    194 
    195   /// DIFile - This is a wrapper for a file.
    196   class DIFile : public DIScope {
    197   public:
    198     explicit DIFile(const MDNode *N = 0) : DIScope(N) {
    199       if (DbgNode && !isFile())
    200         DbgNode = 0;
    201     }
    202     StringRef getFilename() const  { return getStringField(1);   }
    203     StringRef getDirectory() const { return getStringField(2);   }
    204     DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
    205   };
    206 
    207   /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
    208   /// FIXME: it seems strange that this doesn't have either a reference to the
    209   /// type/precision or a file/line pair for location info.
    210   class DIEnumerator : public DIDescriptor {
    211   public:
    212     explicit DIEnumerator(const MDNode *N = 0) : DIDescriptor(N) {}
    213 
    214     StringRef getName() const        { return getStringField(1); }
    215     uint64_t getEnumValue() const      { return getUInt64Field(2); }
    216   };
    217 
    218   /// DIType - This is a wrapper for a type.
    219   /// FIXME: Types should be factored much better so that CV qualifiers and
    220   /// others do not require a huge and empty descriptor full of zeros.
    221   class DIType : public DIScope {
    222   public:
    223   protected:
    224     // This ctor is used when the Tag has already been validated by a derived
    225     // ctor.
    226     DIType(const MDNode *N, bool, bool) : DIScope(N) {}
    227 
    228   public:
    229 
    230     /// Verify - Verify that a type descriptor is well formed.
    231     bool Verify() const;
    232   public:
    233     explicit DIType(const MDNode *N);
    234     explicit DIType() {}
    235     virtual ~DIType() {}
    236 
    237     DIScope getContext() const          { return getFieldAs<DIScope>(1); }
    238     StringRef getName() const           { return getStringField(2);     }
    239     DICompileUnit getCompileUnit() const{
    240      if (getVersion() == llvm::LLVMDebugVersion7)
    241        return getFieldAs<DICompileUnit>(3);
    242 
    243      return getFieldAs<DIFile>(3).getCompileUnit();
    244     }
    245     DIFile getFile() const              { return getFieldAs<DIFile>(3); }
    246     unsigned getLineNumber() const      { return getUnsignedField(4); }
    247     uint64_t getSizeInBits() const      { return getUInt64Field(5); }
    248     uint64_t getAlignInBits() const     { return getUInt64Field(6); }
    249     // FIXME: Offset is only used for DW_TAG_member nodes.  Making every type
    250     // carry this is just plain insane.
    251     uint64_t getOffsetInBits() const    { return getUInt64Field(7); }
    252     unsigned getFlags() const           { return getUnsignedField(8); }
    253     bool isPrivate() const {
    254       return (getFlags() & FlagPrivate) != 0;
    255     }
    256     bool isProtected() const {
    257       return (getFlags() & FlagProtected) != 0;
    258     }
    259     bool isForwardDecl() const {
    260       return (getFlags() & FlagFwdDecl) != 0;
    261     }
    262     // isAppleBlock - Return true if this is the Apple Blocks extension.
    263     bool isAppleBlockExtension() const {
    264       return (getFlags() & FlagAppleBlock) != 0;
    265     }
    266     bool isBlockByrefStruct() const {
    267       return (getFlags() & FlagBlockByrefStruct) != 0;
    268     }
    269     bool isVirtual() const {
    270       return (getFlags() & FlagVirtual) != 0;
    271     }
    272     bool isArtificial() const {
    273       return (getFlags() & FlagArtificial) != 0;
    274     }
    275     bool isObjcClassComplete() const {
    276       return (getFlags() & FlagObjcClassComplete) != 0;
    277     }
    278     bool isValid() const {
    279       return DbgNode && (isBasicType() || isDerivedType() || isCompositeType());
    280     }
    281     StringRef getDirectory() const  {
    282       if (getVersion() == llvm::LLVMDebugVersion7)
    283         return getCompileUnit().getDirectory();
    284 
    285       return getFieldAs<DIFile>(3).getDirectory();
    286     }
    287     StringRef getFilename() const  {
    288       if (getVersion() == llvm::LLVMDebugVersion7)
    289         return getCompileUnit().getFilename();
    290 
    291       return getFieldAs<DIFile>(3).getFilename();
    292     }
    293 
    294     /// replaceAllUsesWith - Replace all uses of debug info referenced by
    295     /// this descriptor.
    296     void replaceAllUsesWith(DIDescriptor &D);
    297     void replaceAllUsesWith(MDNode *D);
    298 
    299     /// print - print type.
    300     void print(raw_ostream &OS) const;
    301 
    302     /// dump - print type to dbgs() with a newline.
    303     void dump() const;
    304   };
    305 
    306   /// DIBasicType - A basic type, like 'int' or 'float'.
    307   class DIBasicType : public DIType {
    308   public:
    309     explicit DIBasicType(const MDNode *N = 0) : DIType(N) {}
    310 
    311     unsigned getEncoding() const { return getUnsignedField(9); }
    312 
    313     /// Verify - Verify that a basic type descriptor is well formed.
    314     bool Verify() const;
    315 
    316     /// print - print basic type.
    317     void print(raw_ostream &OS) const;
    318 
    319     /// dump - print basic type to dbgs() with a newline.
    320     void dump() const;
    321   };
    322 
    323   /// DIDerivedType - A simple derived type, like a const qualified type,
    324   /// a typedef, a pointer or reference, etc.
    325   class DIDerivedType : public DIType {
    326   protected:
    327     explicit DIDerivedType(const MDNode *N, bool, bool)
    328       : DIType(N, true, true) {}
    329   public:
    330     explicit DIDerivedType(const MDNode *N = 0)
    331       : DIType(N, true, true) {}
    332 
    333     DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
    334 
    335     /// getOriginalTypeSize - If this type is derived from a base type then
    336     /// return base type size.
    337     uint64_t getOriginalTypeSize() const;
    338 
    339     StringRef getObjCPropertyName() const { return getStringField(10); }
    340     StringRef getObjCPropertyGetterName() const {
    341       return getStringField(11);
    342     }
    343     StringRef getObjCPropertySetterName() const {
    344       return getStringField(12);
    345     }
    346     bool isReadOnlyObjCProperty() {
    347       return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
    348     }
    349     bool isReadWriteObjCProperty() {
    350       return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
    351     }
    352     bool isAssignObjCProperty() {
    353       return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_assign) != 0;
    354     }
    355     bool isRetainObjCProperty() {
    356       return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_retain) != 0;
    357     }
    358     bool isCopyObjCProperty() {
    359       return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_copy) != 0;
    360     }
    361     bool isNonAtomicObjCProperty() {
    362       return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
    363     }
    364 
    365     /// Verify - Verify that a derived type descriptor is well formed.
    366     bool Verify() const;
    367 
    368     /// print - print derived type.
    369     void print(raw_ostream &OS) const;
    370 
    371     /// dump - print derived type to dbgs() with a newline.
    372     void dump() const;
    373   };
    374 
    375   /// DICompositeType - This descriptor holds a type that can refer to multiple
    376   /// other types, like a function or struct.
    377   /// FIXME: Why is this a DIDerivedType??
    378   class DICompositeType : public DIDerivedType {
    379   public:
    380     explicit DICompositeType(const MDNode *N = 0)
    381       : DIDerivedType(N, true, true) {
    382       if (N && !isCompositeType())
    383         DbgNode = 0;
    384     }
    385 
    386     DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
    387     unsigned getRunTimeLang() const { return getUnsignedField(11); }
    388     DICompositeType getContainingType() const {
    389       return getFieldAs<DICompositeType>(12);
    390     }
    391     DIArray getTemplateParams() const { return getFieldAs<DIArray>(13); }
    392 
    393     /// Verify - Verify that a composite type descriptor is well formed.
    394     bool Verify() const;
    395 
    396     /// print - print composite type.
    397     void print(raw_ostream &OS) const;
    398 
    399     /// dump - print composite type to dbgs() with a newline.
    400     void dump() const;
    401   };
    402 
    403   /// DITemplateTypeParameter - This is a wrapper for template type parameter.
    404   class DITemplateTypeParameter : public DIDescriptor {
    405   public:
    406     explicit DITemplateTypeParameter(const MDNode *N = 0) : DIDescriptor(N) {}
    407 
    408     DIScope getContext() const       { return getFieldAs<DIScope>(1); }
    409     StringRef getName() const        { return getStringField(2); }
    410     DIType getType() const           { return getFieldAs<DIType>(3); }
    411     StringRef getFilename() const    {
    412       return getFieldAs<DIFile>(4).getFilename();
    413     }
    414     StringRef getDirectory() const   {
    415       return getFieldAs<DIFile>(4).getDirectory();
    416     }
    417     unsigned getLineNumber() const   { return getUnsignedField(5); }
    418     unsigned getColumnNumber() const { return getUnsignedField(6); }
    419   };
    420 
    421   /// DITemplateValueParameter - This is a wrapper for template value parameter.
    422   class DITemplateValueParameter : public DIDescriptor {
    423   public:
    424     explicit DITemplateValueParameter(const MDNode *N = 0) : DIDescriptor(N) {}
    425 
    426     DIScope getContext() const       { return getFieldAs<DIScope>(1); }
    427     StringRef getName() const        { return getStringField(2); }
    428     DIType getType() const           { return getFieldAs<DIType>(3); }
    429     uint64_t getValue() const         { return getUInt64Field(4); }
    430     StringRef getFilename() const    {
    431       return getFieldAs<DIFile>(5).getFilename();
    432     }
    433     StringRef getDirectory() const   {
    434       return getFieldAs<DIFile>(5).getDirectory();
    435     }
    436     unsigned getLineNumber() const   { return getUnsignedField(6); }
    437     unsigned getColumnNumber() const { return getUnsignedField(7); }
    438   };
    439 
    440   /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
    441   class DISubprogram : public DIScope {
    442   public:
    443     explicit DISubprogram(const MDNode *N = 0) : DIScope(N) {}
    444 
    445     DIScope getContext() const          { return getFieldAs<DIScope>(2); }
    446     StringRef getName() const         { return getStringField(3); }
    447     StringRef getDisplayName() const  { return getStringField(4); }
    448     StringRef getLinkageName() const  { return getStringField(5); }
    449     DICompileUnit getCompileUnit() const{
    450       if (getVersion() == llvm::LLVMDebugVersion7)
    451         return getFieldAs<DICompileUnit>(6);
    452 
    453       return getFieldAs<DIFile>(6).getCompileUnit();
    454     }
    455     unsigned getLineNumber() const      { return getUnsignedField(7); }
    456     DICompositeType getType() const { return getFieldAs<DICompositeType>(8); }
    457 
    458     /// getReturnTypeName - Subprogram return types are encoded either as
    459     /// DIType or as DICompositeType.
    460     StringRef getReturnTypeName() const {
    461       DICompositeType DCT(getFieldAs<DICompositeType>(8));
    462       if (DCT.Verify()) {
    463         DIArray A = DCT.getTypeArray();
    464         DIType T(A.getElement(0));
    465         return T.getName();
    466       }
    467       DIType T(getFieldAs<DIType>(8));
    468       return T.getName();
    469     }
    470 
    471     /// isLocalToUnit - Return true if this subprogram is local to the current
    472     /// compile unit, like 'static' in C.
    473     unsigned isLocalToUnit() const     { return getUnsignedField(9); }
    474     unsigned isDefinition() const      { return getUnsignedField(10); }
    475 
    476     unsigned getVirtuality() const { return getUnsignedField(11); }
    477     unsigned getVirtualIndex() const { return getUnsignedField(12); }
    478 
    479     DICompositeType getContainingType() const {
    480       return getFieldAs<DICompositeType>(13);
    481     }
    482     unsigned isArtificial() const    {
    483       if (getVersion() <= llvm::LLVMDebugVersion8)
    484         return getUnsignedField(14);
    485       return (getUnsignedField(14) & FlagArtificial) != 0;
    486     }
    487     /// isPrivate - Return true if this subprogram has "private"
    488     /// access specifier.
    489     bool isPrivate() const    {
    490       if (getVersion() <= llvm::LLVMDebugVersion8)
    491         return false;
    492       return (getUnsignedField(14) & FlagPrivate) != 0;
    493     }
    494     /// isProtected - Return true if this subprogram has "protected"
    495     /// access specifier.
    496     bool isProtected() const    {
    497       if (getVersion() <= llvm::LLVMDebugVersion8)
    498         return false;
    499       return (getUnsignedField(14) & FlagProtected) != 0;
    500     }
    501     /// isExplicit - Return true if this subprogram is marked as explicit.
    502     bool isExplicit() const    {
    503       if (getVersion() <= llvm::LLVMDebugVersion8)
    504         return false;
    505       return (getUnsignedField(14) & FlagExplicit) != 0;
    506     }
    507     /// isPrototyped - Return true if this subprogram is prototyped.
    508     bool isPrototyped() const    {
    509       if (getVersion() <= llvm::LLVMDebugVersion8)
    510         return false;
    511       return (getUnsignedField(14) & FlagPrototyped) != 0;
    512     }
    513 
    514     unsigned isOptimized() const;
    515 
    516     StringRef getFilename() const    {
    517       if (getVersion() == llvm::LLVMDebugVersion7)
    518         return getCompileUnit().getFilename();
    519 
    520       return getFieldAs<DIFile>(6).getFilename();
    521     }
    522 
    523     StringRef getDirectory() const   {
    524       if (getVersion() == llvm::LLVMDebugVersion7)
    525         return getCompileUnit().getFilename();
    526 
    527       return getFieldAs<DIFile>(6).getDirectory();
    528     }
    529 
    530     /// Verify - Verify that a subprogram descriptor is well formed.
    531     bool Verify() const;
    532 
    533     /// print - print subprogram.
    534     void print(raw_ostream &OS) const;
    535 
    536     /// dump - print subprogram to dbgs() with a newline.
    537     void dump() const;
    538 
    539     /// describes - Return true if this subprogram provides debugging
    540     /// information for the function F.
    541     bool describes(const Function *F);
    542 
    543     Function *getFunction() const { return getFunctionField(16); }
    544     DIArray getTemplateParams() const { return getFieldAs<DIArray>(17); }
    545     DISubprogram getFunctionDeclaration() const {
    546       return getFieldAs<DISubprogram>(18);
    547     }
    548   };
    549 
    550   /// DIGlobalVariable - This is a wrapper for a global variable.
    551   class DIGlobalVariable : public DIDescriptor {
    552   public:
    553     explicit DIGlobalVariable(const MDNode *N = 0) : DIDescriptor(N) {}
    554 
    555     DIScope getContext() const          { return getFieldAs<DIScope>(2); }
    556     StringRef getName() const         { return getStringField(3); }
    557     StringRef getDisplayName() const  { return getStringField(4); }
    558     StringRef getLinkageName() const  { return getStringField(5); }
    559     DICompileUnit getCompileUnit() const{
    560       if (getVersion() == llvm::LLVMDebugVersion7)
    561         return getFieldAs<DICompileUnit>(6);
    562 
    563       DIFile F = getFieldAs<DIFile>(6);
    564       return F.getCompileUnit();
    565     }
    566 
    567     unsigned getLineNumber() const      { return getUnsignedField(7); }
    568     DIType getType() const              { return getFieldAs<DIType>(8); }
    569     unsigned isLocalToUnit() const      { return getUnsignedField(9); }
    570     unsigned isDefinition() const       { return getUnsignedField(10); }
    571 
    572     GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
    573     Constant *getConstant() const   { return getConstantField(11); }
    574 
    575     /// Verify - Verify that a global variable descriptor is well formed.
    576     bool Verify() const;
    577 
    578     /// print - print global variable.
    579     void print(raw_ostream &OS) const;
    580 
    581     /// dump - print global variable to dbgs() with a newline.
    582     void dump() const;
    583   };
    584 
    585   /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
    586   /// global etc).
    587   class DIVariable : public DIDescriptor {
    588   public:
    589     explicit DIVariable(const MDNode *N = 0)
    590       : DIDescriptor(N) {}
    591 
    592     DIScope getContext() const          { return getFieldAs<DIScope>(1); }
    593     StringRef getName() const           { return getStringField(2);     }
    594     DICompileUnit getCompileUnit() const{
    595       if (getVersion() == llvm::LLVMDebugVersion7)
    596         return getFieldAs<DICompileUnit>(3);
    597 
    598       DIFile F = getFieldAs<DIFile>(3);
    599       return F.getCompileUnit();
    600     }
    601     unsigned getLineNumber() const      {
    602       return (getUnsignedField(4) << 8) >> 8;
    603     }
    604     unsigned getArgNumber() const       {
    605       unsigned L = getUnsignedField(4);
    606       return L >> 24;
    607     }
    608     DIType getType() const              { return getFieldAs<DIType>(5); }
    609 
    610     /// isArtificial - Return true if this variable is marked as "artificial".
    611     bool isArtificial() const    {
    612       if (getVersion() <= llvm::LLVMDebugVersion8)
    613         return false;
    614       return (getUnsignedField(6) & FlagArtificial) != 0;
    615     }
    616 
    617 
    618     /// Verify - Verify that a variable descriptor is well formed.
    619     bool Verify() const;
    620 
    621     /// HasComplexAddr - Return true if the variable has a complex address.
    622     bool hasComplexAddress() const {
    623       return getNumAddrElements() > 0;
    624     }
    625 
    626     unsigned getNumAddrElements() const;
    627 
    628     uint64_t getAddrElement(unsigned Idx) const {
    629       if (getVersion() <= llvm::LLVMDebugVersion8)
    630         return getUInt64Field(Idx+6);
    631       if (getVersion() == llvm::LLVMDebugVersion9)
    632         return getUInt64Field(Idx+7);
    633       return getUInt64Field(Idx+8);
    634     }
    635 
    636     /// isBlockByrefVariable - Return true if the variable was declared as
    637     /// a "__block" variable (Apple Blocks).
    638     bool isBlockByrefVariable() const {
    639       return getType().isBlockByrefStruct();
    640     }
    641 
    642     /// isInlinedFnArgument - Return trule if this variable provides debugging
    643     /// information for an inlined function arguments.
    644     bool isInlinedFnArgument(const Function *CurFn);
    645 
    646     /// print - print variable.
    647     void print(raw_ostream &OS) const;
    648 
    649     /// dump - print variable to dbgs() with a newline.
    650     void dump() const;
    651   };
    652 
    653   /// DILexicalBlock - This is a wrapper for a lexical block.
    654   class DILexicalBlock : public DIScope {
    655   public:
    656     explicit DILexicalBlock(const MDNode *N = 0) : DIScope(N) {}
    657     DIScope getContext() const       { return getFieldAs<DIScope>(1);      }
    658     unsigned getLineNumber() const   { return getUnsignedField(2);         }
    659     unsigned getColumnNumber() const { return getUnsignedField(3);         }
    660     StringRef getDirectory() const {
    661       StringRef dir = getFieldAs<DIFile>(4).getDirectory();
    662       return !dir.empty() ? dir : getContext().getDirectory();
    663     }
    664     StringRef getFilename() const {
    665       StringRef filename = getFieldAs<DIFile>(4).getFilename();
    666       return !filename.empty() ? filename : getContext().getFilename();
    667     }
    668   };
    669 
    670   /// DINameSpace - A wrapper for a C++ style name space.
    671   class DINameSpace : public DIScope {
    672   public:
    673     explicit DINameSpace(const MDNode *N = 0) : DIScope(N) {}
    674     DIScope getContext() const     { return getFieldAs<DIScope>(1);      }
    675     StringRef getName() const      { return getStringField(2);           }
    676     StringRef getDirectory() const  {
    677       return getFieldAs<DIFile>(3).getDirectory();
    678     }
    679     StringRef getFilename() const  {
    680       return getFieldAs<DIFile>(3).getFilename();
    681     }
    682     DICompileUnit getCompileUnit() const{
    683       if (getVersion() == llvm::LLVMDebugVersion7)
    684         return getFieldAs<DICompileUnit>(3);
    685 
    686       return getFieldAs<DIFile>(3).getCompileUnit();
    687     }
    688     unsigned getLineNumber() const { return getUnsignedField(4);         }
    689     bool Verify() const;
    690   };
    691 
    692   /// DILocation - This object holds location information. This object
    693   /// is not associated with any DWARF tag.
    694   class DILocation : public DIDescriptor {
    695   public:
    696     explicit DILocation(const MDNode *N) : DIDescriptor(N) { }
    697 
    698     unsigned getLineNumber() const     { return getUnsignedField(0); }
    699     unsigned getColumnNumber() const   { return getUnsignedField(1); }
    700     DIScope  getScope() const          { return getFieldAs<DIScope>(2); }
    701     DILocation getOrigLocation() const { return getFieldAs<DILocation>(3); }
    702     StringRef getFilename() const    { return getScope().getFilename(); }
    703     StringRef getDirectory() const   { return getScope().getDirectory(); }
    704     bool Verify() const;
    705   };
    706 
    707   /// getDISubprogram - Find subprogram that is enclosing this scope.
    708   DISubprogram getDISubprogram(const MDNode *Scope);
    709 
    710   /// getDICompositeType - Find underlying composite type.
    711   DICompositeType getDICompositeType(DIType T);
    712 
    713   /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
    714   /// to hold function specific information.
    715   NamedMDNode *getOrInsertFnSpecificMDNode(Module &M, StringRef Name);
    716 
    717   /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
    718   /// suitable to hold function specific information.
    719   NamedMDNode *getFnSpecificMDNode(const Module &M, StringRef Name);
    720 
    721   /// createInlinedVariable - Create a new inlined variable based on current
    722   /// variable.
    723   /// @param DV            Current Variable.
    724   /// @param InlinedScope  Location at current variable is inlined.
    725   DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
    726                                    LLVMContext &VMContext);
    727 
    728   /// cleanseInlinedVariable - Remove inlined scope from the variable.
    729   DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
    730 
    731   class DebugInfoFinder {
    732   public:
    733     /// processModule - Process entire module and collect debug info
    734     /// anchors.
    735     void processModule(Module &M);
    736 
    737   private:
    738     /// processType - Process DIType.
    739     void processType(DIType DT);
    740 
    741     /// processLexicalBlock - Process DILexicalBlock.
    742     void processLexicalBlock(DILexicalBlock LB);
    743 
    744     /// processSubprogram - Process DISubprogram.
    745     void processSubprogram(DISubprogram SP);
    746 
    747     /// processDeclare - Process DbgDeclareInst.
    748     void processDeclare(DbgDeclareInst *DDI);
    749 
    750     /// processLocation - Process DILocation.
    751     void processLocation(DILocation Loc);
    752 
    753     /// addCompileUnit - Add compile unit into CUs.
    754     bool addCompileUnit(DICompileUnit CU);
    755 
    756     /// addGlobalVariable - Add global variable into GVs.
    757     bool addGlobalVariable(DIGlobalVariable DIG);
    758 
    759     // addSubprogram - Add subprgoram into SPs.
    760     bool addSubprogram(DISubprogram SP);
    761 
    762     /// addType - Add type into Tys.
    763     bool addType(DIType DT);
    764 
    765   public:
    766     typedef SmallVector<MDNode *, 8>::const_iterator iterator;
    767     iterator compile_unit_begin()    const { return CUs.begin(); }
    768     iterator compile_unit_end()      const { return CUs.end(); }
    769     iterator subprogram_begin()      const { return SPs.begin(); }
    770     iterator subprogram_end()        const { return SPs.end(); }
    771     iterator global_variable_begin() const { return GVs.begin(); }
    772     iterator global_variable_end()   const { return GVs.end(); }
    773     iterator type_begin()            const { return TYs.begin(); }
    774     iterator type_end()              const { return TYs.end(); }
    775 
    776     unsigned compile_unit_count()    const { return CUs.size(); }
    777     unsigned global_variable_count() const { return GVs.size(); }
    778     unsigned subprogram_count()      const { return SPs.size(); }
    779     unsigned type_count()            const { return TYs.size(); }
    780 
    781   private:
    782     SmallVector<MDNode *, 8> CUs;  // Compile Units
    783     SmallVector<MDNode *, 8> SPs;  // Subprograms
    784     SmallVector<MDNode *, 8> GVs;  // Global Variables;
    785     SmallVector<MDNode *, 8> TYs;  // Types
    786     SmallPtrSet<MDNode *, 64> NodesSeen;
    787   };
    788 } // end namespace llvm
    789 
    790 #endif
    791