Home | History | Annotate | Download | only in VMCore
      1 //===-- LLVMContextImpl.cpp - Implement LLVMContextImpl -------------------===//
      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 implements the opaque LLVMContextImpl.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "LLVMContextImpl.h"
     15 #include "llvm/Module.h"
     16 #include "llvm/ADT/STLExtras.h"
     17 #include <algorithm>
     18 using namespace llvm;
     19 
     20 LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
     21   : TheTrueVal(0), TheFalseVal(0),
     22     VoidTy(C, Type::VoidTyID),
     23     LabelTy(C, Type::LabelTyID),
     24     FloatTy(C, Type::FloatTyID),
     25     DoubleTy(C, Type::DoubleTyID),
     26     MetadataTy(C, Type::MetadataTyID),
     27     X86_FP80Ty(C, Type::X86_FP80TyID),
     28     FP128Ty(C, Type::FP128TyID),
     29     PPC_FP128Ty(C, Type::PPC_FP128TyID),
     30     X86_MMXTy(C, Type::X86_MMXTyID),
     31     Int1Ty(C, 1),
     32     Int8Ty(C, 8),
     33     Int16Ty(C, 16),
     34     Int32Ty(C, 32),
     35     Int64Ty(C, 64) {
     36   InlineAsmDiagHandler = 0;
     37   InlineAsmDiagContext = 0;
     38   NamedStructTypesUniqueID = 0;
     39 }
     40 
     41 namespace {
     42 struct DropReferences {
     43   // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
     44   // is a Constant*.
     45   template<typename PairT>
     46   void operator()(const PairT &P) {
     47     P.second->dropAllReferences();
     48   }
     49 };
     50 }
     51 
     52 LLVMContextImpl::~LLVMContextImpl() {
     53   // NOTE: We need to delete the contents of OwnedModules, but we have to
     54   // duplicate it into a temporary vector, because the destructor of Module
     55   // will try to remove itself from OwnedModules set.  This would cause
     56   // iterator invalidation if we iterated on the set directly.
     57   std::vector<Module*> Modules(OwnedModules.begin(), OwnedModules.end());
     58   DeleteContainerPointers(Modules);
     59 
     60   std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(),
     61                 DropReferences());
     62   std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
     63                 DropReferences());
     64   std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
     65                 DropReferences());
     66   std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
     67                 DropReferences());
     68   ExprConstants.freeConstants();
     69   ArrayConstants.freeConstants();
     70   StructConstants.freeConstants();
     71   VectorConstants.freeConstants();
     72   AggZeroConstants.freeConstants();
     73   NullPtrConstants.freeConstants();
     74   UndefValueConstants.freeConstants();
     75   InlineAsms.freeConstants();
     76   DeleteContainerSeconds(IntConstants);
     77   DeleteContainerSeconds(FPConstants);
     78 
     79   // Destroy MDNodes.  ~MDNode can move and remove nodes between the MDNodeSet
     80   // and the NonUniquedMDNodes sets, so copy the values out first.
     81   SmallVector<MDNode*, 8> MDNodes;
     82   MDNodes.reserve(MDNodeSet.size() + NonUniquedMDNodes.size());
     83   for (FoldingSetIterator<MDNode> I = MDNodeSet.begin(), E = MDNodeSet.end();
     84        I != E; ++I)
     85     MDNodes.push_back(&*I);
     86   MDNodes.append(NonUniquedMDNodes.begin(), NonUniquedMDNodes.end());
     87   for (SmallVectorImpl<MDNode *>::iterator I = MDNodes.begin(),
     88          E = MDNodes.end(); I != E; ++I)
     89     (*I)->destroy();
     90   assert(MDNodeSet.empty() && NonUniquedMDNodes.empty() &&
     91          "Destroying all MDNodes didn't empty the Context's sets.");
     92   // Destroy MDStrings.
     93   DeleteContainerSeconds(MDStringCache);
     94 }
     95