Introduce DILocation.
[oota-llvm.git] / include / llvm / Analysis / DebugInfo.h
index 53934d8e9af0c9cb1c2259653a1625eb4b900e82..7c6b421ca35c878408d7d536cf9f60155843b354 100644 (file)
@@ -17,6 +17,7 @@
 #ifndef LLVM_ANALYSIS_DEBUGINFO_H
 #define LLVM_ANALYSIS_DEBUGINFO_H
 
+#include "llvm/Metadata.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/DenseMap.h"
@@ -40,16 +41,16 @@ namespace llvm {
   class DebugLoc;
   struct DebugLocTracker;
   class Instruction;
-  struct LLVMContext;
+  class LLVMContext;
 
   class DIDescriptor {
   protected:    
-    GlobalVariable *DbgGV;
+    MDNode *DbgNode;
 
-    /// DIDescriptor constructor.  If the specified GV is non-null, this checks
+    /// DIDescriptor constructor.  If the specified node is non-null, check
     /// to make sure that the tag in the descriptor matches 'RequiredTag'.  If
     /// not, the debug info is corrupt and we ignore it.
-    DIDescriptor(GlobalVariable *GV, unsigned RequiredTag);
+    DIDescriptor(MDNode *N, unsigned RequiredTag);
 
     const std::string &getStringField(unsigned Elt, std::string &Result) const;
     unsigned getUnsignedField(unsigned Elt) const {
@@ -60,18 +61,18 @@ namespace llvm {
 
     template <typename DescTy>
     DescTy getFieldAs(unsigned Elt) const {
-      return DescTy(getDescriptorField(Elt).getGV());
+      return DescTy(getDescriptorField(Elt).getNode());
     }
 
     GlobalVariable *getGlobalVariableField(unsigned Elt) const;
 
   public:
-    explicit DIDescriptor() : DbgGV(0) {}
-    explicit DIDescriptor(GlobalVariable *GV) : DbgGV(GV) {}
+    explicit DIDescriptor() : DbgNode(0) {}
+    explicit DIDescriptor(MDNode *N) : DbgNode(N) {}
 
-    bool isNull() const { return DbgGV == 0; }
+    bool isNull() const { return DbgNode == 0; }
 
-    GlobalVariable *getGV() const { return DbgGV; }
+    MDNode *getNode() const { return DbgNode; }
 
     unsigned getVersion() const {
       return getUnsignedField(0) & LLVMDebugVersionMask;
@@ -81,18 +82,28 @@ namespace llvm {
       return getUnsignedField(0) & ~LLVMDebugVersionMask;
     }
 
-    /// ValidDebugInfo - Return true if V represents valid debug info value.
-    static bool ValidDebugInfo(Value *V, CodeGenOpt::Level OptLevel);
+    /// ValidDebugInfo - Return true if N represents valid debug info value.
+    static bool ValidDebugInfo(MDNode *N, CodeGenOpt::Level OptLevel);
 
     /// dump - print descriptor.
     void dump() const;
+
+    bool isDerivedType() const;
+    bool isCompositeType() const;
+    bool isBasicType() const;
+    bool isVariable() const;
+    bool isSubprogram() const;
+    bool isGlobalVariable() const;
+    bool isScope() const;
+    bool isCompileUnit() const;
+    bool isLexicalBlock() const;
   };
 
   /// DISubrange - This is used to represent ranges, for array bounds.
   class DISubrange : public DIDescriptor {
   public:
-    explicit DISubrange(GlobalVariable *GV = 0)
-      : DIDescriptor(GV, dwarf::DW_TAG_subrange_type) {}
+    explicit DISubrange(MDNode *N = 0)
+      : DIDescriptor(N, dwarf::DW_TAG_subrange_type) {}
 
     int64_t getLo() const { return (int64_t)getUInt64Field(1); }
     int64_t getHi() const { return (int64_t)getUInt64Field(2); }
@@ -101,7 +112,8 @@ namespace llvm {
   /// DIArray - This descriptor holds an array of descriptors.
   class DIArray : public DIDescriptor {
   public:
-    explicit DIArray(GlobalVariable *GV = 0) : DIDescriptor(GV) {}
+    explicit DIArray(MDNode *N = 0) 
+      : DIDescriptor(N) {}
 
     unsigned getNumElements() const;
     DIDescriptor getElement(unsigned Idx) const {
@@ -109,11 +121,31 @@ namespace llvm {
     }
   };
 
+  /// DIScope - A base class for various scopes.
+  class DIScope : public DIDescriptor {
+  public:
+    explicit DIScope(MDNode *N = 0) : DIDescriptor (N) {
+      if (DbgNode && !isScope())
+        DbgNode = 0;
+    }
+
+    virtual const std::string &getFilename(std::string &F) const {
+      return F;
+    }
+
+    virtual const std::string &getDirectory(std::string &D) const {
+      return D;
+    }
+  };
+
   /// DICompileUnit - A wrapper for a compile unit.
-  class DICompileUnit : public DIDescriptor {
+  class DICompileUnit : public DIScope {
   public:
-    explicit DICompileUnit(GlobalVariable *GV = 0)
-      : DIDescriptor(GV, dwarf::DW_TAG_compile_unit) {}
+    explicit DICompileUnit(MDNode *N = 0) {
+      DbgNode = N;
+      if (DbgNode && !isCompileUnit())
+       DbgNode = 0;
+    }
 
     unsigned getLanguage() const     { return getUnsignedField(2); }
     const std::string &getFilename(std::string &F) const {
@@ -154,8 +186,8 @@ namespace llvm {
   /// type/precision or a file/line pair for location info.
   class DIEnumerator : public DIDescriptor {
   public:
-    explicit DIEnumerator(GlobalVariable *GV = 0)
-      : DIDescriptor(GV, dwarf::DW_TAG_enumerator) {}
+    explicit DIEnumerator(MDNode *N = 0)
+      : DIDescriptor(N, dwarf::DW_TAG_enumerator) {}
 
     const std::string &getName(std::string &F) const {
       return getStringField(1, F);
@@ -169,36 +201,26 @@ namespace llvm {
   class DIType : public DIDescriptor {
   public:
     enum {
-      FlagPrivate   = 1 << 0,
-      FlagProtected = 1 << 1,
-      FlagFwdDecl   = 1 << 2
+      FlagPrivate    = 1 << 0,
+      FlagProtected  = 1 << 1,
+      FlagFwdDecl    = 1 << 2,
+      FlagAppleBlock = 1 << 3,
+      FlagBlockByrefStruct = 1 << 4
     };
 
   protected:
-    DIType(GlobalVariable *GV, unsigned Tag) : DIDescriptor(GV, Tag) {}
+    DIType(MDNode *N, unsigned Tag) 
+      : DIDescriptor(N, Tag) {}
     // This ctor is used when the Tag has already been validated by a derived
     // ctor.
-    DIType(GlobalVariable *GV, bool, bool) : DIDescriptor(GV) {}
+    DIType(MDNode *N, bool, bool) : DIDescriptor(N) {}
 
   public:
-    /// isDerivedType - Return true if the specified tag is legal for
-    /// DIDerivedType.
-    static bool isDerivedType(unsigned TAG);
-
-    /// isCompositeType - Return true if the specified tag is legal for
-    /// DICompositeType.
-    static bool isCompositeType(unsigned TAG);
-
-    /// isBasicType - Return true if the specified tag is legal for
-    /// DIBasicType.
-    static bool isBasicType(unsigned TAG) {
-      return TAG == dwarf::DW_TAG_base_type;
-    }
 
     /// Verify - Verify that a type descriptor is well formed.
     bool Verify() const;
   public:
-    explicit DIType(GlobalVariable *GV);
+    explicit DIType(MDNode *N);
     explicit DIType() {}
     virtual ~DIType() {}
 
@@ -214,14 +236,21 @@ namespace llvm {
     // carry this is just plain insane.
     uint64_t getOffsetInBits() const    { return getUInt64Field(7); }
     unsigned getFlags() const           { return getUnsignedField(8); }
-    bool isPrivate() const              { return 
-        (getFlags() & FlagPrivate) != 0; 
+    bool isPrivate() const {
+      return (getFlags() & FlagPrivate) != 0; 
     }
-    bool isProtected() const            { 
+    bool isProtected() const {
       return (getFlags() & FlagProtected) != 0; 
     }
-    bool isForwardDecl() const          {
-        return (getFlags() & FlagFwdDecl) != 0; 
+    bool isForwardDecl() const {
+      return (getFlags() & FlagFwdDecl) != 0; 
+    }
+    // isAppleBlock - Return true if this is the Apple Blocks extension.
+    bool isAppleBlockExtension() const {
+      return (getFlags() & FlagAppleBlock) != 0; 
+    }
+    bool isBlockByrefStruct() const {
+      return (getFlags() & FlagBlockByrefStruct) != 0;
     }
 
     /// dump - print type.
@@ -231,8 +260,8 @@ namespace llvm {
   /// DIBasicType - A basic type, like 'int' or 'float'.
   class DIBasicType : public DIType {
   public:
-    explicit DIBasicType(GlobalVariable *GV)
-      : DIType(GV, dwarf::DW_TAG_base_type) {}
+    explicit DIBasicType(MDNode *N = 0)
+      : DIType(N, dwarf::DW_TAG_base_type) {}
 
     unsigned getEncoding() const { return getUnsignedField(9); }
 
@@ -244,13 +273,13 @@ namespace llvm {
   /// a typedef, a pointer or reference, etc.
   class DIDerivedType : public DIType {
   protected:
-    explicit DIDerivedType(GlobalVariable *GV, bool, bool)
-      : DIType(GV, true, true) {}
+    explicit DIDerivedType(MDNode *N, bool, bool)
+      : DIType(N, true, true) {}
   public:
-    explicit DIDerivedType(GlobalVariable *GV)
-      : DIType(GV, true, true) {
-      if (GV && !isDerivedType(getTag()))
-        DbgGV = 0;
+    explicit DIDerivedType(MDNode *N = 0)
+      : DIType(N, true, true) {
+      if (DbgNode && !isDerivedType())
+        DbgNode = 0;
     }
 
     DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
@@ -272,10 +301,10 @@ namespace llvm {
   /// FIXME: Why is this a DIDerivedType??
   class DICompositeType : public DIDerivedType {
   public:
-    explicit DICompositeType(GlobalVariable *GV)
-      : DIDerivedType(GV, true, true) {
-      if (GV && !isCompositeType(getTag()))
-        DbgGV = 0;
+    explicit DICompositeType(MDNode *N = 0)
+      : DIDerivedType(N, true, true) {
+      if (N && !isCompositeType())
+        DbgNode = 0;
     }
 
     DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
@@ -291,20 +320,8 @@ namespace llvm {
   /// DIGlobal - This is a common class for global variables and subprograms.
   class DIGlobal : public DIDescriptor {
   protected:
-    explicit DIGlobal(GlobalVariable *GV, unsigned RequiredTag)
-      : DIDescriptor(GV, RequiredTag) {}
-
-    /// isSubprogram - Return true if the specified tag is legal for
-    /// DISubprogram.
-    static bool isSubprogram(unsigned TAG) {
-      return TAG == dwarf::DW_TAG_subprogram;
-    }
-
-    /// isGlobalVariable - Return true if the specified tag is legal for
-    /// DIGlobalVariable.
-    static bool isGlobalVariable(unsigned TAG) {
-      return TAG == dwarf::DW_TAG_variable;
-    }
+    explicit DIGlobal(MDNode *N, unsigned RequiredTag)
+      : DIDescriptor(N, RequiredTag) {}
 
   public:
     virtual ~DIGlobal() {}
@@ -333,11 +350,26 @@ namespace llvm {
   };
 
   /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
-  class DISubprogram : public DIGlobal {
+  class DISubprogram : public DIScope {
   public:
-    explicit DISubprogram(GlobalVariable *GV = 0)
-      : DIGlobal(GV, dwarf::DW_TAG_subprogram) {}
+    explicit DISubprogram(MDNode *N = 0) {
+      DbgNode = N;
+      if (DbgNode && !isSubprogram())
+       DbgNode = 0;
+    }
 
+    DIDescriptor getContext() const     { return getDescriptorField(2); }
+    const std::string &getName(std::string &F) const {
+      return getStringField(3, F);
+    }
+    const std::string &getDisplayName(std::string &F) const {
+      return getStringField(4, F);
+    }
+    const std::string &getLinkageName(std::string &F) const {
+      return getStringField(5, F);
+    }
+    DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(6); }
+    unsigned getLineNumber() const      { return getUnsignedField(7); }
     DICompositeType getType() const { return getFieldAs<DICompositeType>(8); }
 
     /// getReturnTypeName - Subprogram return types are encoded either as
@@ -346,13 +378,25 @@ namespace llvm {
       DICompositeType DCT(getFieldAs<DICompositeType>(8));
       if (!DCT.isNull()) {
         DIArray A = DCT.getTypeArray();
-        DIType T(A.getElement(0).getGV());
+        DIType T(A.getElement(0).getNode());
         return T.getName(F);
       }
       DIType T(getFieldAs<DIType>(8));
       return T.getName(F);
     }
 
+    /// 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); }
+
+    const std::string &getFilename(std::string &F) const {
+      return getCompileUnit().getFilename(F);
+    }
+    const std::string &getDirectory(std::string &F) const {
+      return getCompileUnit().getDirectory(F);
+    }
+
     /// Verify - Verify that a subprogram descriptor is well formed.
     bool Verify() const;
 
@@ -367,8 +411,8 @@ namespace llvm {
   /// DIGlobalVariable - This is a wrapper for a global variable.
   class DIGlobalVariable : public DIGlobal {
   public:
-    explicit DIGlobalVariable(GlobalVariable *GV = 0)
-      : DIGlobal(GV, dwarf::DW_TAG_variable) {}
+    explicit DIGlobalVariable(MDNode *N = 0)
+      : DIGlobal(N, dwarf::DW_TAG_variable) {}
 
     GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
 
@@ -383,10 +427,10 @@ namespace llvm {
   /// global etc).
   class DIVariable : public DIDescriptor {
   public:
-    explicit DIVariable(GlobalVariable *GV = 0)
-      : DIDescriptor(GV) {
-      if (GV && !isVariable(getTag()))
-        DbgGV = 0;
+    explicit DIVariable(MDNode *N = 0)
+      : DIDescriptor(N) {
+      if (DbgNode && !isVariable())
+        DbgNode = 0;
     }
 
     DIDescriptor getContext() const { return getDescriptorField(1); }
@@ -397,23 +441,54 @@ namespace llvm {
     unsigned getLineNumber() const      { return getUnsignedField(4); }
     DIType getType() const              { return getFieldAs<DIType>(5); }
 
-    /// isVariable - Return true if the specified tag is legal for DIVariable.
-    static bool isVariable(unsigned Tag);
 
     /// Verify - Verify that a variable descriptor is well formed.
     bool Verify() const;
 
+    /// isBlockByrefVariable - Return true if the variable was declared as
+    /// a "__block" variable (Apple Blocks).
+    bool isBlockByrefVariable() const { 
+      return getType().isBlockByrefStruct(); 
+    }
+
     /// dump - print variable.
     void dump() const;
   };
 
-  /// DIBlock - This is a wrapper for a block (e.g. a function, scope, etc).
-  class DIBlock : public DIDescriptor {
+  /// DILexicalBlock - This is a wrapper for a lexical block.
+  class DILexicalBlock : public DIScope {
   public:
-    explicit DIBlock(GlobalVariable *GV = 0)
-      : DIDescriptor(GV, dwarf::DW_TAG_lexical_block) {}
+    explicit DILexicalBlock(MDNode *N = 0) {
+      DbgNode = N;
+      if (DbgNode && !isLexicalBlock()) 
+       DbgNode = 0;
+    }
+    DIScope getContext() const { return getFieldAs<DIScope>(1); }
 
-    DIDescriptor getContext() const { return getDescriptorField(1); }
+    const std::string &getFilename(std::string &F) const {
+      return getContext().getFilename(F);
+    }
+    const std::string &getDirectory(std::string &D) const {
+      return getContext().getDirectory(D);
+    }
+  };
+
+  /// DILocation - This object holds location information. This object
+  /// is not associated with any DWARF tag.
+  class DILocation : public DIDescriptor {
+  public:
+    explicit DILocation(MDNode *L) { DbgNode = L; }
+
+    unsigned getLineNumber() const     { return getUnsignedField(0); }
+    unsigned getColumnNumber() const   { return getUnsignedField(1); }
+    DIScope  getScope() const          { return getFieldAs<DIScope>(3); }
+    DILocation getOrigLocation() const { return getFieldAs<DILocation>(4); }
+    std::string getFilename(std::string &F) const  { 
+      return getScope().getFilename(F); 
+    }
+    std::string getDirectory(std::string &D) const { 
+      return getScope().getDirectory(D); 
+    }
   };
 
   /// DIFactory - This object assists with the construction of the various
@@ -512,9 +587,13 @@ namespace llvm {
                               DICompileUnit CompileUnit, unsigned LineNo,
                               DIType Type);
 
-    /// CreateBlock - This creates a descriptor for a lexical block with the
-    /// specified parent context.
-    DIBlock CreateBlock(DIDescriptor Context);
+    /// CreateLexicalBlock - This creates a descriptor for a lexical block 
+    /// with the specified parent context.
+    DILexicalBlock CreateLexicalBlock(DIDescriptor Context);
+
+    /// CreateLocation - Creates a debug info location.
+    DILocation CreateLocation(unsigned LineNo, unsigned ColumnNo, 
+                             DIScope S, DILocation OrigLoc);
 
     /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
     /// inserting it at the end of the specified basic block.
@@ -538,10 +617,6 @@ namespace llvm {
 
   private:
     Constant *GetTagConstant(unsigned TAG);
-    Constant *GetStringConstant(const std::string &String);
-
-    /// getCastToEmpty - Return the descriptor as a Constant* with type '{}*'.
-    Constant *getCastToEmpty(DIDescriptor D);
   };
 
   /// Finds the stoppoint coressponding to this instruction, that is the
@@ -563,12 +638,6 @@ namespace llvm {
                        std::string &Type, unsigned &LineNo, std::string &File,
                        std::string &Dir); 
 
-  /// CollectDebugInfoAnchors - Collect debugging information anchors.
-  void CollectDebugInfoAnchors(Module &M,
-                               SmallVector<GlobalVariable *, 2> &CompileUnits,
-                               SmallVector<GlobalVariable *, 4> &GlobalVars,
-                               SmallVector<GlobalVariable *, 4> &Subprograms);
-
   /// isValidDebugInfoIntrinsic - Return true if SPI is a valid debug 
   /// info intrinsic.
   bool isValidDebugInfoIntrinsic(DbgStopPointInst &SPI, 
@@ -609,9 +678,7 @@ namespace llvm {
 
   /// isInlinedFnEnd - Return true if REI is ending an inlined function.
   bool isInlinedFnEnd(DbgRegionEndInst &REI, const Function *CurrentFn);
-
-  /// DebugInfoEnumrator - This object collects DebugInfo from
-  /// the module.
+  /// DebugInfoFinder - This object collects DebugInfo from a module.
   class DebugInfoFinder {
 
   public:
@@ -650,25 +717,31 @@ namespace llvm {
     // addSubprogram - Add subprgoram into SPs.
     bool addSubprogram(DISubprogram SP);
 
+    /// addType - Add type into Tys.
+    bool addType(DIType DT);
+
   public:
-    typedef SmallVector<GlobalVariable *, 8>::iterator iterator;
+    typedef SmallVector<MDNode *, 8>::iterator iterator;
     iterator compile_unit_begin()    { return CUs.begin(); }
     iterator compile_unit_end()      { return CUs.end(); }
     iterator subprogram_begin()      { return SPs.begin(); }
     iterator subprogram_end()        { return SPs.end(); }
     iterator global_variable_begin() { return GVs.begin(); }
     iterator global_variable_end()   { return GVs.end(); }
+    iterator type_begin()            { return TYs.begin(); }
+    iterator type_end()              { return TYs.end(); }
 
     unsigned compile_unit_count()    { return CUs.size(); }
     unsigned global_variable_count() { return GVs.size(); }
     unsigned subprogram_count()      { return SPs.size(); }
+    unsigned type_count()            { return TYs.size(); }
 
   private:
-    SmallVector<GlobalVariable *, 8> CUs;  // Compile Units
-    SmallVector<GlobalVariable *, 8> SPs;  // Subprograms
-    SmallVector<GlobalVariable *, 8> GVs;  // Global Variables;
-    SmallPtrSet<GlobalVariable *, 64> NodesSeen;
-    
+    SmallVector<MDNode *, 8> CUs;  // Compile Units
+    SmallVector<MDNode *, 8> SPs;  // Subprograms
+    SmallVector<MDNode *, 8> GVs;  // Global Variables;
+    SmallVector<MDNode *, 8> TYs;  // Types
+    SmallPtrSet<MDNode *, 64> NodesSeen;
   };
 } // end namespace llvm