Debug Info Verifier: simplify DIxxx::Verify
[oota-llvm.git] / include / llvm / DebugInfo.h
index 0c8cb5d9e7c5baa95a89e33f396a069fca65b241..5c74b172e9ff4bc06f79e1ac022be385df0249e2 100644 (file)
@@ -44,7 +44,6 @@ namespace llvm {
   class DIVariable;
   class DIType;
   class DIObjCProperty;
-  class DIImportedModule;
 
   /// DIDescriptor - A thin wraper around MDNode to access encoded debug info.
   /// This should not be stored in a container, because the underlying MDNode
@@ -64,7 +63,8 @@ namespace llvm {
       FlagObjcClassComplete  = 1 << 9,
       FlagObjectPointer      = 1 << 10,
       FlagVector             = 1 << 11,
-      FlagStaticMember       = 1 << 12
+      FlagStaticMember       = 1 << 12,
+      FlagIndirectVariable   = 1 << 13
     };
   protected:
     const MDNode *DbgNode;
@@ -102,6 +102,20 @@ namespace llvm {
     operator MDNode *() const { return const_cast<MDNode*>(DbgNode); }
     MDNode *operator ->() const { return const_cast<MDNode*>(DbgNode); }
 
+    // An explicit operator bool so that we can do testing of DI values
+    // easily.
+    // FIXME: This operator bool isn't actually protecting anything at the
+    // moment due to the conversion operator above making DIDescriptor nodes
+    // implicitly convertable to bool.
+    LLVM_EXPLICIT operator bool() const { return DbgNode != 0; }
+
+    bool operator==(DIDescriptor Other) const {
+      return DbgNode == Other.DbgNode;
+    }
+    bool operator!=(DIDescriptor Other) const {
+      return !operator==(Other);
+    }
+
     unsigned getTag() const {
       return getUnsignedField(0) & ~LLVMDebugVersionMask;
     }
@@ -126,7 +140,7 @@ namespace llvm {
     bool isTemplateTypeParameter() const;
     bool isTemplateValueParameter() const;
     bool isObjCProperty() const;
-    bool isImportedModule() const;
+    bool isImportedEntity() const;
 
     /// print - print descriptor.
     void print(raw_ostream &OS) const;
@@ -167,9 +181,11 @@ namespace llvm {
   public:
     explicit DIScope(const MDNode *N = 0) : DIDescriptor (N) {}
 
+    /// Set the filename by allocating a new string MDNode for
+    /// it and attaching it to the underlying node.
+    void setFilename(StringRef Name, LLVMContext &Context);
     StringRef getFilename() const;
     StringRef getDirectory() const;
-    DIArray getImportedModules() const { return getFieldAs<DIArray>(2); }
   };
 
   /// DIFile - This is a wrapper for a file.
@@ -191,17 +207,18 @@ namespace llvm {
   public:
     explicit DICompileUnit(const MDNode *N = 0) : DIScope(N) {}
 
-    unsigned getLanguage() const { return getUnsignedField(3); }
-    StringRef getProducer() const { return getStringField(4); }
+    unsigned getLanguage() const { return getUnsignedField(2); }
+    StringRef getProducer() const { return getStringField(3); }
 
-    bool isOptimized() const { return getUnsignedField(5) != 0; }
-    StringRef getFlags() const { return getStringField(6); }
-    unsigned getRunTimeVersion() const { return getUnsignedField(7); }
+    bool isOptimized() const { return getUnsignedField(4) != 0; }
+    StringRef getFlags() const { return getStringField(5); }
+    unsigned getRunTimeVersion() const { return getUnsignedField(6); }
 
     DIArray getEnumTypes() const;
     DIArray getRetainedTypes() const;
     DIArray getSubprograms() const;
     DIArray getGlobalVariables() const;
+    DIArray getImportedEntities() const;
 
     StringRef getSplitDebugFilename() const { return getStringField(12); }
 
@@ -219,7 +236,7 @@ namespace llvm {
     explicit DIEnumerator(const MDNode *N = 0) : DIDescriptor(N) {}
 
     StringRef getName() const        { return getStringField(1); }
-    uint64_t getEnumValue() const      { return getUInt64Field(2); }
+    int64_t getEnumValue() const      { return getInt64Field(2); }
     bool Verify() const;
   };
 
@@ -239,15 +256,15 @@ namespace llvm {
     explicit DIType(const MDNode *N);
     explicit DIType() {}
 
-    DIScope getContext() const          { return getFieldAs<DIScope>(3); }
-    StringRef getName() const           { return getStringField(4);     }
-    unsigned getLineNumber() const      { return getUnsignedField(5); }
-    uint64_t getSizeInBits() const      { return getUInt64Field(6); }
-    uint64_t getAlignInBits() const     { return getUInt64Field(7); }
+    DIScope getContext() const          { return getFieldAs<DIScope>(2); }
+    StringRef getName() const           { return getStringField(3);     }
+    unsigned getLineNumber() const      { return getUnsignedField(4); }
+    uint64_t getSizeInBits() const      { return getUInt64Field(5); }
+    uint64_t getAlignInBits() const     { return getUInt64Field(6); }
     // FIXME: Offset is only used for DW_TAG_member nodes.  Making every type
     // carry this is just plain insane.
-    uint64_t getOffsetInBits() const    { return getUInt64Field(8); }
-    unsigned getFlags() const           { return getUnsignedField(9); }
+    uint64_t getOffsetInBits() const    { return getUInt64Field(7); }
+    unsigned getFlags() const           { return getUnsignedField(8); }
     bool isPrivate() const {
       return (getFlags() & FlagPrivate) != 0;
     }
@@ -283,7 +300,7 @@ namespace llvm {
       return (getFlags() & FlagStaticMember) != 0;
     }
     bool isValid() const {
-      return DbgNode && (isBasicType() || isDerivedType() || isCompositeType());
+      return DbgNode && isType();
     }
 
     /// isUnsignedDIType - Return true if type encoding is unsigned.
@@ -300,7 +317,7 @@ namespace llvm {
   public:
     explicit DIBasicType(const MDNode *N = 0) : DIType(N) {}
 
-    unsigned getEncoding() const { return getUnsignedField(10); }
+    unsigned getEncoding() const { return getUnsignedField(9); }
 
     /// Verify - Verify that a basic type descriptor is well formed.
     bool Verify() const;
@@ -319,7 +336,7 @@ namespace llvm {
     explicit DIDerivedType(const MDNode *N = 0)
       : DIType(N, true, true) {}
 
-    DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(10); }
+    DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
 
     /// getOriginalTypeSize - If this type is derived from a base type then
     /// return base type size.
@@ -331,12 +348,12 @@ namespace llvm {
 
     DIType getClassType() const {
       assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
-      return getFieldAs<DIType>(11);
+      return getFieldAs<DIType>(10);
     }
 
     Constant *getConstant() const {
       assert((getTag() == dwarf::DW_TAG_member) && isStaticMember());
-      return getConstantField(11);
+      return getConstantField(10);
     }
 
     /// Verify - Verify that a derived type descriptor is well formed.
@@ -345,7 +362,10 @@ namespace llvm {
 
   /// DICompositeType - This descriptor holds a type that can refer to multiple
   /// other types, like a function or struct.
-  /// FIXME: Why is this a DIDerivedType??
+  /// DICompositeType is derived from DIDerivedType because some
+  /// composite types (such as enums) can be derived from basic types
+  // FIXME: Make this derive from DIType directly & just store the
+  // base type in a single DIType field.
   class DICompositeType : public DIDerivedType {
     friend class DIDescriptor;
     void printInternal(raw_ostream &OS) const;
@@ -356,14 +376,14 @@ namespace llvm {
         DbgNode = 0;
     }
 
-    DIArray getTypeArray() const { return getFieldAs<DIArray>(11); }
+    DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
     void setTypeArray(DIArray Elements, DIArray TParams = DIArray());
-    unsigned getRunTimeLang() const { return getUnsignedField(12); }
+    unsigned getRunTimeLang() const { return getUnsignedField(11); }
     DICompositeType getContainingType() const {
-      return getFieldAs<DICompositeType>(13);
+      return getFieldAs<DICompositeType>(12);
     }
     void setContainingType(DICompositeType ContainingType);
-    DIArray getTemplateParams() const { return getFieldAs<DIArray>(14); }
+    DIArray getTemplateParams() const { return getFieldAs<DIArray>(13); }
 
     /// Verify - Verify that a composite type descriptor is well formed.
     bool Verify() const;
@@ -396,7 +416,7 @@ namespace llvm {
     DIScope getContext() const       { return getFieldAs<DIScope>(1); }
     StringRef getName() const        { return getStringField(2); }
     DIType getType() const           { return getFieldAs<DIType>(3); }
-    uint64_t getValue() const         { return getUInt64Field(4); }
+    Value *getValue() const;
     StringRef getFilename() const    {
       return getFieldAs<DIFile>(5).getFilename();
     }
@@ -415,71 +435,53 @@ namespace llvm {
   public:
     explicit DISubprogram(const MDNode *N = 0) : DIScope(N) {}
 
-    DIScope getContext() const          { return getFieldAs<DIScope>(3); }
-    StringRef getName() const         { return getStringField(4); }
-    StringRef getDisplayName() const  { return getStringField(5); }
-    StringRef getLinkageName() const  { return getStringField(6); }
-    unsigned getLineNumber() const      { return getUnsignedField(7); }
-    DICompositeType getType() const { return getFieldAs<DICompositeType>(8); }
-
-    /// getReturnTypeName - Subprogram return types are encoded either as
-    /// DIType or as DICompositeType.
-    StringRef getReturnTypeName() const {
-      DICompositeType DCT(getFieldAs<DICompositeType>(8));
-      if (DCT.Verify()) {
-        DIArray A = DCT.getTypeArray();
-        DIType T(A.getElement(0));
-        return T.getName();
-      }
-      DIType T(getFieldAs<DIType>(8));
-      return T.getName();
-    }
+    DIScope getContext() const          { return getFieldAs<DIScope>(2); }
+    StringRef getName() const         { return getStringField(3); }
+    StringRef getDisplayName() const  { return getStringField(4); }
+    StringRef getLinkageName() const  { return getStringField(5); }
+    unsigned getLineNumber() const      { return getUnsignedField(6); }
+    DICompositeType getType() const { return getFieldAs<DICompositeType>(7); }
 
     /// isLocalToUnit - Return true if this subprogram is local to the current
     /// compile unit, like 'static' in C.
-    unsigned isLocalToUnit() const     { return getUnsignedField(9); }
-    unsigned isDefinition() const      { return getUnsignedField(10); }
+    unsigned isLocalToUnit() const     { return getUnsignedField(8); }
+    unsigned isDefinition() const      { return getUnsignedField(9); }
 
-    unsigned getVirtuality() const { return getUnsignedField(11); }
-    unsigned getVirtualIndex() const { return getUnsignedField(12); }
+    unsigned getVirtuality() const { return getUnsignedField(10); }
+    unsigned getVirtualIndex() const { return getUnsignedField(11); }
 
     DICompositeType getContainingType() const {
-      return getFieldAs<DICompositeType>(13);
+      return getFieldAs<DICompositeType>(12);
     }
 
     unsigned getFlags() const {
-      return getUnsignedField(14);
+      return getUnsignedField(13);
     }
 
     unsigned isArtificial() const    {
-      return (getFlags() & FlagArtificial) != 0;
+      return (getUnsignedField(13) & FlagArtificial) != 0;
     }
     /// isPrivate - Return true if this subprogram has "private"
     /// access specifier.
     bool isPrivate() const    {
-      return (getFlags() & FlagPrivate) != 0;
+      return (getUnsignedField(13) & FlagPrivate) != 0;
     }
     /// isProtected - Return true if this subprogram has "protected"
     /// access specifier.
     bool isProtected() const    {
-      return (getFlags() & FlagProtected) != 0;
+      return (getUnsignedField(13) & FlagProtected) != 0;
     }
     /// isExplicit - Return true if this subprogram is marked as explicit.
     bool isExplicit() const    {
-      return (getFlags() & FlagExplicit) != 0;
+      return (getUnsignedField(13) & FlagExplicit) != 0;
     }
     /// isPrototyped - Return true if this subprogram is prototyped.
     bool isPrototyped() const    {
-      return (getFlags() & FlagPrototyped) != 0;
+      return (getUnsignedField(13) & FlagPrototyped) != 0;
     }
 
     unsigned isOptimized() const;
 
-    /// getScopeLineNumber - Get the beginning of the scope of the
-    /// function, not necessarily where the name of the program
-    /// starts.
-    unsigned getScopeLineNumber() const { return getUnsignedField(20); }
-
     /// Verify - Verify that a subprogram descriptor is well formed.
     bool Verify() const;
 
@@ -487,14 +489,19 @@ namespace llvm {
     /// information for the function F.
     bool describes(const Function *F);
 
-    Function *getFunction() const { return getFunctionField(16); }
-    void replaceFunction(Function *F) { replaceFunctionField(16, F); }
-    DIArray getTemplateParams() const { return getFieldAs<DIArray>(17); }
+    Function *getFunction() const { return getFunctionField(15); }
+    void replaceFunction(Function *F) { replaceFunctionField(15, F); }
+    DIArray getTemplateParams() const { return getFieldAs<DIArray>(16); }
     DISubprogram getFunctionDeclaration() const {
-      return getFieldAs<DISubprogram>(18);
+      return getFieldAs<DISubprogram>(17);
     }
     MDNode *getVariablesNodes() const;
     DIArray getVariables() const;
+
+    /// getScopeLineNumber - Get the beginning of the scope of the
+    /// function, not necessarily where the name of the program
+    /// starts.
+    unsigned getScopeLineNumber() const { return getUnsignedField(19); }
   };
 
   /// DIGlobalVariable - This is a wrapper for a global variable.
@@ -561,6 +568,11 @@ namespace llvm {
       return (getUnsignedField(6) & FlagObjectPointer) != 0;
     }
 
+    /// \brief Return true if this variable is represented as a pointer.
+    bool isIndirect() const {
+      return (getUnsignedField(6) & FlagIndirectVariable) != 0;
+    }
+
     /// getInlinedAt - If this variable is inlined then return inline location.
     MDNode *getInlinedAt() const;
 
@@ -595,9 +607,9 @@ namespace llvm {
   class DILexicalBlock : public DIScope {
   public:
     explicit DILexicalBlock(const MDNode *N = 0) : DIScope(N) {}
-    DIScope getContext() const       { return getFieldAs<DIScope>(3);      }
-    unsigned getLineNumber() const   { return getUnsignedField(4);         }
-    unsigned getColumnNumber() const { return getUnsignedField(5);         }
+    DIScope getContext() const       { return getFieldAs<DIScope>(2);      }
+    unsigned getLineNumber() const   { return getUnsignedField(3);         }
+    unsigned getColumnNumber() const { return getUnsignedField(4);         }
     bool Verify() const;
   };
 
@@ -609,7 +621,7 @@ namespace llvm {
     DIScope getContext() const { if (getScope().isSubprogram()) return getScope(); return getScope().getContext(); }
     unsigned getLineNumber() const { return getScope().getLineNumber(); }
     unsigned getColumnNumber() const { return getScope().getColumnNumber(); }
-    DILexicalBlock getScope() const { return getFieldAs<DILexicalBlock>(3); }
+    DILexicalBlock getScope() const { return getFieldAs<DILexicalBlock>(2); }
     bool Verify() const;
   };
 
@@ -619,9 +631,9 @@ namespace llvm {
     void printInternal(raw_ostream &OS) const;
   public:
     explicit DINameSpace(const MDNode *N = 0) : DIScope(N) {}
-    DIScope getContext() const     { return getFieldAs<DIScope>(3);      }
-    StringRef getName() const      { return getStringField(4);           }
-    unsigned getLineNumber() const { return getUnsignedField(5);         }
+    DIScope getContext() const     { return getFieldAs<DIScope>(2);      }
+    StringRef getName() const      { return getStringField(3);           }
+    unsigned getLineNumber() const { return getUnsignedField(4);         }
     bool Verify() const;
   };
 
@@ -656,22 +668,22 @@ namespace llvm {
     StringRef getObjCPropertySetterName() const {
       return getStringField(5);
     }
-    bool isReadOnlyObjCProperty() {
+    bool isReadOnlyObjCProperty() const {
       return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
     }
-    bool isReadWriteObjCProperty() {
+    bool isReadWriteObjCProperty() const {
       return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
     }
-    bool isAssignObjCProperty() {
+    bool isAssignObjCProperty() const {
       return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_assign) != 0;
     }
-    bool isRetainObjCProperty() {
+    bool isRetainObjCProperty() const {
       return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_retain) != 0;
     }
-    bool isCopyObjCProperty() {
+    bool isCopyObjCProperty() const {
       return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_copy) != 0;
     }
-    bool isNonAtomicObjCProperty() {
+    bool isNonAtomicObjCProperty() const {
       return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
     }
 
@@ -681,12 +693,16 @@ namespace llvm {
     bool Verify() const;
   };
 
-  class DIImportedModule : public DIDescriptor {
+  /// \brief An imported module (C++ using directive or similar).
+  class DIImportedEntity : public DIDescriptor {
     friend class DIDescriptor;
     void printInternal(raw_ostream &OS) const;
   public:
-    explicit DIImportedModule(const MDNode *N) : DIDescriptor(N) { }
-    DINameSpace getNameSpace() const { return getFieldAs<DINameSpace>(1); }
+    explicit DIImportedEntity(const MDNode *N) : DIDescriptor(N) { }
+    DIScope getContext() const { return getFieldAs<DIScope>(1); }
+    DIDescriptor getEntity() const { return getFieldAs<DIDescriptor>(2); }
+    unsigned getLineNumber() const { return getUnsignedField(3); }
+    StringRef getName() const { return getStringField(4); }
     bool Verify() const;
   };
 
@@ -752,8 +768,10 @@ namespace llvm {
     /// addType - Add type into Tys.
     bool addType(DIType DT);
 
+    bool addScope(DIScope Scope);
+
   public:
-    typedef SmallVector<MDNode *, 8>::const_iterator iterator;
+    typedef SmallVectorImpl<MDNode *>::const_iterator iterator;
     iterator compile_unit_begin()    const { return CUs.begin(); }
     iterator compile_unit_end()      const { return CUs.end(); }
     iterator subprogram_begin()      const { return SPs.begin(); }
@@ -762,17 +780,21 @@ namespace llvm {
     iterator global_variable_end()   const { return GVs.end(); }
     iterator type_begin()            const { return TYs.begin(); }
     iterator type_end()              const { return TYs.end(); }
+    iterator scope_begin()           const { return Scopes.begin(); }
+    iterator scope_end()             const { return Scopes.end(); }
 
     unsigned compile_unit_count()    const { return CUs.size(); }
     unsigned global_variable_count() const { return GVs.size(); }
     unsigned subprogram_count()      const { return SPs.size(); }
     unsigned type_count()            const { return TYs.size(); }
+    unsigned scope_count()           const { return Scopes.size(); }
 
   private:
     SmallVector<MDNode *, 8> CUs;  // Compile Units
     SmallVector<MDNode *, 8> SPs;  // Subprograms
     SmallVector<MDNode *, 8> GVs;  // Global Variables;
     SmallVector<MDNode *, 8> TYs;  // Types
+    SmallVector<MDNode *, 8> Scopes; // Scopes
     SmallPtrSet<MDNode *, 64> NodesSeen;
   };
 } // end namespace llvm