Verifier: Check for incompatible bit piece expressions
[oota-llvm.git] / lib / IR / DebugInfo.cpp
index 4c836924d880c926d83596cdac3519abab0d00fd..19b799e0892d1a3e6ebc95283bfd729b3a9c2cd9 100644 (file)
 using namespace llvm;
 using namespace llvm::dwarf;
 
-//===----------------------------------------------------------------------===//
-// DIDescriptor
-//===----------------------------------------------------------------------===//
-
-static Metadata *getField(const MDNode *DbgNode, unsigned Elt) {
-  if (!DbgNode || Elt >= DbgNode->getNumOperands())
-    return nullptr;
-  return DbgNode->getOperand(Elt);
-}
-
-static MDNode *getNodeField(const MDNode *DbgNode, unsigned Elt) {
-  return dyn_cast_or_null<MDNode>(getField(DbgNode, Elt));
-}
-
-DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
-  MDNode *Field = getNodeField(DbgNode, Elt);
-  return DIDescriptor(Field);
-}
-
-/// \brief Return the size reported by the variable's type.
-unsigned DIVariable::getSizeInBits(const DITypeIdentifierMap &Map) {
-  DIType Ty = getType().resolve(Map);
-  // Follow derived types until we reach a type that
-  // reports back a size.
-  while (isa<MDDerivedType>(Ty) && !Ty.getSizeInBits()) {
-    DIDerivedType DT = cast<MDDerivedType>(Ty);
-    Ty = DT.getTypeDerivedFrom().resolve(Map);
-  }
-  assert(Ty.getSizeInBits() && "type with size 0");
-  return Ty.getSizeInBits();
-}
-
 //===----------------------------------------------------------------------===//
 // Simple Descriptor Constructors and other Methods
 //===----------------------------------------------------------------------===//
 
-void DIDescriptor::replaceAllUsesWith(LLVMContext &, DIDescriptor D) {
-  assert(DbgNode && "Trying to replace an unverified type!");
-  assert(DbgNode->isTemporary() && "Expected temporary node");
-  TempMDNode Temp(get());
-
-  // Since we use a TrackingVH for the node, its easy for clients to manufacture
-  // legitimate situations where they want to replaceAllUsesWith() on something
-  // which, due to uniquing, has merged with the source. We shield clients from
-  // this detail by allowing a value to be replaced with replaceAllUsesWith()
-  // itself.
-  if (Temp.get() == D.get()) {
-    DbgNode = MDNode::replaceWithUniqued(std::move(Temp));
-    return;
-  }
-
-  Temp->replaceAllUsesWith(D.get());
-  DbgNode = D.get();
-}
-
-void DIDescriptor::replaceAllUsesWith(MDNode *D) {
-  assert(DbgNode && "Trying to replace an unverified type!");
-  assert(DbgNode != D && "This replacement should always happen");
-  assert(DbgNode->isTemporary() && "Expected temporary node");
-  TempMDNode Node(get());
-  Node->replaceAllUsesWith(D);
-}
-
-#ifndef NDEBUG
-/// \brief Check if a value can be a reference to a type.
-static bool isTypeRef(const Metadata *MD) {
-  if (!MD)
-    return true;
-  if (auto *S = dyn_cast<MDString>(MD))
-    return !S->getString().empty();
-  return isa<MDType>(MD);
-}
-
-/// \brief Check if a value can be a ScopeRef.
-static bool isScopeRef(const Metadata *MD) {
-  if (!MD)
-    return true;
-  if (auto *S = dyn_cast<MDString>(MD))
-    return !S->getString().empty();
-  return isa<MDScope>(MD);
-}
-
-/// \brief Check if a value can be a DescriptorRef.
-static bool isDescriptorRef(const Metadata *MD) {
-  if (!MD)
-    return true;
-  if (auto *S = dyn_cast<MDString>(MD))
-    return !S->getString().empty();
-  return isa<MDNode>(MD);
-}
-#endif
-
 DIScopeRef DIScope::getRef() const { return MDScopeRef::get(get()); }
 
 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
@@ -138,9 +50,8 @@ bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
 }
 
 Function *DISubprogram::getFunction() const {
-  if (auto *N = get())
-    if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(N->getFunction()))
-      return dyn_cast<Function>(C->getValue());
+  if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(get()->getFunction()))
+    return dyn_cast<Function>(C->getValue());
   return nullptr;
 }
 
@@ -160,57 +71,12 @@ GlobalVariable *DIGlobalVariable::getGlobal() const {
   return dyn_cast_or_null<GlobalVariable>(getConstant());
 }
 
-DIScopeRef DIScope::getContext() const {
-  if (DIType T = dyn_cast<MDType>(*this))
-    return T.getContext();
-
-  if (DISubprogram SP = dyn_cast<MDSubprogram>(*this))
-    return DIScopeRef(SP.getContext());
-
-  if (DILexicalBlock LB = dyn_cast<MDLexicalBlockBase>(*this))
-    return DIScopeRef(LB.getContext());
-
-  if (DINameSpace NS = dyn_cast<MDNamespace>(*this))
-    return DIScopeRef(NS.getContext());
-
-  assert((isa<MDFile>(*this) || isa<MDCompileUnit>(*this)) &&
-         "Unhandled type of scope.");
-  return DIScopeRef(nullptr);
-}
-
-StringRef DIScope::getName() const {
-  if (DIType T = dyn_cast<MDType>(*this))
-    return T.getName();
-  if (DISubprogram SP = dyn_cast<MDSubprogram>(*this))
-    return SP.getName();
-  if (DINameSpace NS = dyn_cast<MDNamespace>(*this))
-    return NS.getName();
-  assert((isa<MDLexicalBlockBase>(*this) || isa<MDFile>(*this) ||
-          isa<MDCompileUnit>(*this)) &&
-         "Unhandled type of scope.");
-  return StringRef();
-}
-
-StringRef DIScope::getFilename() const {
-  if (auto *N = get())
-    if (auto *F = N->getFile())
-      return F->getFilename();
-  return "";
-}
-
-StringRef DIScope::getDirectory() const {
-  if (auto *N = get())
-    if (auto *F = N->getFile())
-      return F->getDirectory();
-  return "";
-}
-
 void DICompileUnit::replaceSubprograms(DIArray Subprograms) {
-  get()->replaceSubprograms(cast_or_null<MDTuple>(Subprograms.get()));
+  get()->replaceSubprograms(MDSubprogramArray(Subprograms));
 }
 
 void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) {
-  get()->replaceGlobalVariables(cast_or_null<MDTuple>(GlobalVariables.get()));
+  get()->replaceGlobalVariables(MDGlobalVariableArray(GlobalVariables));
 }
 
 DILocation DILocation::copyWithNewScope(LLVMContext &Ctx,
@@ -282,10 +148,10 @@ llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
   for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
     DICompileUnit CU = cast<MDCompileUnit>(CU_Nodes->getOperand(CUi));
     DIArray Retain = CU.getRetainedTypes();
-    for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) {
-      if (!isa<MDCompositeType>(Retain.getElement(Ti)))
+    for (unsigned Ti = 0, Te = Retain.size(); Ti != Te; ++Ti) {
+      if (!isa<MDCompositeType>(Retain[Ti]))
         continue;
-      DICompositeType Ty = cast<MDCompositeType>(Retain.getElement(Ti));
+      DICompositeType Ty = cast<MDCompositeType>(Retain[Ti]);
       if (MDString *TypeId = Ty.getIdentifier()) {
         // Definition has priority over declaration.
         // Try to insert (TypeId, Ty) to Map.
@@ -330,26 +196,19 @@ void DebugInfoFinder::processModule(const Module &M) {
     for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
       DICompileUnit CU = cast<MDCompileUnit>(CU_Nodes->getOperand(i));
       addCompileUnit(CU);
-      DIArray GVs = CU.getGlobalVariables();
-      for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
-        DIGlobalVariable DIG = cast<MDGlobalVariable>(GVs.getElement(i));
+      for (DIGlobalVariable DIG : CU->getGlobalVariables()) {
         if (addGlobalVariable(DIG)) {
           processScope(DIG.getContext());
           processType(DIG.getType().resolve(TypeIdentifierMap));
         }
       }
-      DIArray SPs = CU.getSubprograms();
-      for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
-        processSubprogram(cast<MDSubprogram>(SPs.getElement(i)));
-      DIArray EnumTypes = CU.getEnumTypes();
-      for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
-        processType(cast<MDType>(EnumTypes.getElement(i)));
-      DIArray RetainedTypes = CU.getRetainedTypes();
-      for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
-        processType(cast<MDType>(RetainedTypes.getElement(i)));
-      DIArray Imports = CU.getImportedEntities();
-      for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) {
-        DIImportedEntity Import = cast<MDImportedEntity>(Imports.getElement(i));
+      for (auto *SP : CU->getSubprograms())
+        processSubprogram(SP);
+      for (auto *ET : CU->getEnumTypes())
+        processType(ET);
+      for (auto *RT : CU->getRetainedTypes())
+        processType(RT);
+      for (DIImportedEntity Import : CU->getImportedEntities()) {
         DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap);
         if (auto *T = dyn_cast<MDType>(Entity))
           processType(T);
@@ -377,14 +236,11 @@ void DebugInfoFinder::processType(DIType DT) {
   if (DICompositeType DCT = dyn_cast<MDCompositeTypeBase>(DT)) {
     processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
     if (DISubroutineType ST = dyn_cast<MDSubroutineType>(DCT)) {
-      DITypeArray DTA = ST.getTypeArray();
-      for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i)
-        processType(DTA.getElement(i).resolve(TypeIdentifierMap));
+      for (MDTypeRef Ref : ST->getTypeArray())
+        processType(Ref.resolve(TypeIdentifierMap));
       return;
     }
-    DIArray DA = DCT.getElements();
-    for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
-      DIDescriptor D = DA.getElement(i);
+    for (Metadata *D : DCT->getElements()->operands()) {
       if (DIType T = dyn_cast<MDType>(D))
         processType(T);
       else if (DISubprogram SP = dyn_cast<MDSubprogram>(D))
@@ -424,9 +280,7 @@ void DebugInfoFinder::processSubprogram(DISubprogram SP) {
     return;
   processScope(SP.getContext().resolve(TypeIdentifierMap));
   processType(SP.getType());
-  DIArray TParams = SP.getTemplateParams();
-  for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) {
-    DIDescriptor Element = TParams.getElement(I);
+  for (auto *Element : SP.getTemplateParams()) {
     if (DITemplateTypeParameter TType =
             dyn_cast<MDTemplateTypeParameter>(Element)) {
       processType(TType.getType().resolve(TypeIdentifierMap));
@@ -576,29 +430,6 @@ void DIVariable::printExtendedName(raw_ostream &OS) const {
   }
 }
 
-template <> DIRef<DIDescriptor>::DIRef(const Metadata *V) : Val(V) {
-  assert(isDescriptorRef(V) &&
-         "DIDescriptorRef should be a MDString or MDNode");
-}
-template <> DIRef<DIScope>::DIRef(const Metadata *V) : Val(V) {
-  assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode");
-}
-template <> DIRef<DIType>::DIRef(const Metadata *V) : Val(V) {
-  assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode");
-}
-
-template <>
-DIDescriptorRef DIDescriptor::getFieldAs<DIDescriptorRef>(unsigned Elt) const {
-  return DIDescriptorRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
-}
-template <>
-DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const {
-  return DIScopeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
-}
-template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const {
-  return DITypeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
-}
-
 template <>
 DIDescriptor
 DIRef<DIDescriptor>::resolve(const DITypeIdentifierMap &Map) const {
@@ -685,9 +516,7 @@ llvm::makeSubprogramMap(const Module &M) {
 
   for (MDNode *N : CU_Nodes->operands()) {
     DICompileUnit CUNode = cast<MDCompileUnit>(N);
-    DIArray SPs = CUNode.getSubprograms();
-    for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
-      DISubprogram SP = cast<MDSubprogram>(SPs.getElement(i));
+    for (DISubprogram SP : CUNode->getSubprograms()) {
       if (Function *F = SP.getFunction())
         R.insert(std::make_pair(F, SP));
     }