[Debug Info] add DISubroutineType and its creation takes DITypeArray.
[oota-llvm.git] / include / llvm / IR / DebugInfo.h
index 1b97f6254215ad1dcc46c7223cb7560dd0b05fb8..fb70efc7125ca4edbc94f988ddb5dea291b27d19 100644 (file)
@@ -127,7 +127,9 @@ public:
 
   bool isDerivedType() const;
   bool isCompositeType() const;
+  bool isSubroutineType() const;
   bool isBasicType() const;
+  bool isTrivialType() const;
   bool isVariable() const;
   bool isSubprogram() const;
   bool isGlobalVariable() const;
@@ -166,17 +168,20 @@ public:
   bool Verify() const;
 };
 
-/// DIArray - This descriptor holds an array of descriptors.
-class DIArray : public DIDescriptor {
+/// DITypedArray - This descriptor holds an array of nodes with type T.
+template <typename T> class DITypedArray : public DIDescriptor {
 public:
-  explicit DIArray(const MDNode *N = nullptr) : DIDescriptor(N) {}
-
-  unsigned getNumElements() const;
-  DIDescriptor getElement(unsigned Idx) const {
-    return getDescriptorField(Idx);
+  explicit DITypedArray(const MDNode *N = nullptr) : DIDescriptor(N) {}
+  unsigned getNumElements() const {
+    return DbgNode ? DbgNode->getNumOperands() : 0;
+  }
+  T getElement(unsigned Idx) const {
+    return getFieldAs<T>(Idx);
   }
 };
 
+typedef DITypedArray<DIDescriptor> DIArray;
+
 /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
 /// FIXME: it seems strange that this doesn't have either a reference to the
 /// type/precision or a file/line pair for location info.
@@ -195,6 +200,7 @@ public:
 template <typename T> class DIRef;
 typedef DIRef<DIScope> DIScopeRef;
 typedef DIRef<DIType> DITypeRef;
+typedef DITypedArray<DITypeRef> DITypeArray;
 
 /// DIScope - A base class for various scopes.
 ///
@@ -302,15 +308,36 @@ public:
   /// Verify - Verify that a type descriptor is well formed.
   bool Verify() const;
 
-  DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(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); }
+  DIScopeRef getContext() const { 
+    assert(!isTrivialType() && "no context for DITrivialType");
+    return getFieldAs<DIScopeRef>(2);
+  }
+  StringRef getName() const {
+    assert(!isTrivialType() && "no name for DITrivialType");
+    return getStringField(3);
+  }
+  unsigned getLineNumber() const {
+    assert(!isTrivialType() && "no line number for DITrivialType");
+    return getUnsignedField(4);
+  }
+  uint64_t getSizeInBits() const {
+    assert(!isTrivialType() && "no SizeInBits for DITrivialType");
+    return getUInt64Field(5);
+  }
+  uint64_t getAlignInBits() const {
+    assert(!isTrivialType() && "no AlignInBits for DITrivialType");
+    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(7); }
-  unsigned getFlags() const { return getUnsignedField(8); }
+  uint64_t getOffsetInBits() const {
+    assert(!isTrivialType() && "no OffsetInBits for DITrivialType");
+    return getUInt64Field(7);
+  }
+  unsigned getFlags() const {
+    assert(!isTrivialType() && "no flag for DITrivialType");
+    return getUnsignedField(8);
+  }
   bool isPrivate() const { return (getFlags() & FlagPrivate) != 0; }
   bool isProtected() const { return (getFlags() & FlagProtected) != 0; }
   bool isForwardDecl() const { return (getFlags() & FlagFwdDecl) != 0; }
@@ -343,6 +370,12 @@ public:
   void replaceAllUsesWith(MDNode *D);
 };
 
+class DITrivialType : public DIType {
+public:
+  explicit DITrivialType(const MDNode *N = nullptr) : DIType(N) {}
+  bool Verify() const;
+};
+
 /// DIBasicType - A basic type, like 'int' or 'float'.
 class DIBasicType : public DIType {
 public:
@@ -393,12 +426,22 @@ public:
 class DICompositeType : public DIDerivedType {
   friend class DIDescriptor;
   void printInternal(raw_ostream &OS) const;
+  void setArraysHelper(MDNode *Elements, MDNode *TParams);
 
 public:
   explicit DICompositeType(const MDNode *N = nullptr) : DIDerivedType(N) {}
 
-  DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
-  void setTypeArray(DIArray Elements, DIArray TParams = DIArray());
+  DIArray getElements() const {
+    assert(!isSubroutineType() && "no elements for DISubroutineType");
+    return getFieldAs<DIArray>(10);
+  }
+  template <typename T>
+  void setArrays(DITypedArray<T> Elements, DIArray TParams = DIArray()) {
+    assert((!TParams || DbgNode->getNumOperands() == 15) &&
+           "If you're setting the template parameters this should include a slot "
+           "for that!");
+    setArraysHelper(Elements, TParams);
+  }
   unsigned getRunTimeLang() const { return getUnsignedField(11); }
   DITypeRef getContainingType() const { return getFieldAs<DITypeRef>(12); }
   void setContainingType(DICompositeType ContainingType);
@@ -409,6 +452,14 @@ public:
   bool Verify() const;
 };
 
+class DISubroutineType : public DICompositeType {
+public:
+  explicit DISubroutineType(const MDNode *N = nullptr) : DICompositeType(N) {}
+  DITypedArray<DITypeRef> getTypeArray() const {
+    return getFieldAs<DITypedArray<DITypeRef>>(10);
+  }
+};
+
 /// DIFile - This is a wrapper for a file.
 class DIFile : public DIScope {
   friend class DIDescriptor;
@@ -427,7 +478,9 @@ class DICompileUnit : public DIScope {
 public:
   explicit DICompileUnit(const MDNode *N = nullptr) : DIScope(N) {}
 
-  unsigned getLanguage() const { return getUnsignedField(2); }
+  dwarf::SourceLanguage getLanguage() const {
+    return static_cast<dwarf::SourceLanguage>(getUnsignedField(2));
+  }
   StringRef getProducer() const { return getStringField(3); }
 
   bool isOptimized() const { return getUnsignedField(4) != 0; }
@@ -460,7 +513,7 @@ public:
   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); }
+  DISubroutineType getType() const { return getFieldAs<DISubroutineType>(7); }
 
   /// isLocalToUnit - Return true if this subprogram is local to the current
   /// compile unit, like 'static' in C.
@@ -569,14 +622,6 @@ public:
   bool Verify() const;
 };
 
-/// DIUnspecifiedParameter - This is a wrapper for unspecified parameters.
-class DIUnspecifiedParameter : public DIDescriptor {
-public:
-  explicit DIUnspecifiedParameter(const MDNode *N = nullptr)
-    : DIDescriptor(N) {}
-  bool Verify() const;
-};
-
 /// DITemplateTypeParameter - This is a wrapper for template type parameter.
 class DITemplateTypeParameter : public DIDescriptor {
 public:
@@ -688,12 +733,17 @@ public:
   /// HasComplexAddr - Return true if the variable has a complex address.
   bool hasComplexAddress() const { return getNumAddrElements() > 0; }
 
-  unsigned getNumAddrElements() const;
-
-  uint64_t getAddrElement(unsigned Idx) const {
-    return getUInt64Field(Idx + 8);
+  /// \brief Return the size of this variable's complex address or
+  /// zero if there is none.
+  unsigned getNumAddrElements() const {
+    if (DbgNode->getNumOperands() < 9)
+      return 0;
+    return getDescriptorField(8)->getNumOperands();
   }
 
+  /// \brief return the Idx'th complex address element.
+  uint64_t getAddrElement(unsigned Idx) const;
+
   /// isBlockByrefVariable - Return true if the variable was declared as
   /// a "__block" variable (Apple Blocks).
   bool isBlockByrefVariable(const DITypeIdentifierMap &Map) const {
@@ -927,6 +977,9 @@ private:
   /// Specify if TypeIdentifierMap is initialized.
   bool TypeMapInitialized;
 };
+
+DenseMap<const Function *, DISubprogram> makeSubprogramMap(const Module &M);
+
 } // end namespace llvm
 
 #endif