code cleanups only.
[oota-llvm.git] / lib / VMCore / LLVMContextImpl.cpp
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 <algorithm>
17 using namespace llvm;
18
19 LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
20   : TheTrueVal(0), TheFalseVal(0),
21     VoidTy(C, Type::VoidTyID),
22     LabelTy(C, Type::LabelTyID),
23     FloatTy(C, Type::FloatTyID),
24     DoubleTy(C, Type::DoubleTyID),
25     MetadataTy(C, Type::MetadataTyID),
26     X86_FP80Ty(C, Type::X86_FP80TyID),
27     FP128Ty(C, Type::FP128TyID),
28     PPC_FP128Ty(C, Type::PPC_FP128TyID),
29     X86_MMXTy(C, Type::X86_MMXTyID),
30     Int1Ty(C, 1),
31     Int8Ty(C, 8),
32     Int16Ty(C, 16),
33     Int32Ty(C, 32),
34     Int64Ty(C, 64),
35     AlwaysOpaqueTy(new OpaqueType(C)) {
36   InlineAsmDiagHandler = 0;
37   InlineAsmDiagContext = 0;
38       
39   // Make sure the AlwaysOpaqueTy stays alive as long as the Context.
40   AlwaysOpaqueTy->addRef();
41   OpaqueTypes.insert(AlwaysOpaqueTy);
42 }
43
44 namespace {
45 struct DropReferences {
46   // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
47   // is a Constant*.
48   template<typename PairT>
49   void operator()(const PairT &P) {
50     P.second->dropAllReferences();
51   }
52 };
53 }
54
55 LLVMContextImpl::~LLVMContextImpl() {
56   // NOTE: We need to delete the contents of OwnedModules, but we have to
57   // duplicate it into a temporary vector, because the destructor of Module
58   // will try to remove itself from OwnedModules set.  This would cause
59   // iterator invalidation if we iterated on the set directly.
60   std::vector<Module*> Modules(OwnedModules.begin(), OwnedModules.end());
61   for (std::vector<Module*>::iterator I = Modules.begin(), E = Modules.end();
62        I != E; ++I)
63     delete *I;
64   
65   std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(),
66                 DropReferences());
67   std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
68                 DropReferences());
69   std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
70                 DropReferences());
71   std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
72                 DropReferences());
73   ExprConstants.freeConstants();
74   ArrayConstants.freeConstants();
75   StructConstants.freeConstants();
76   VectorConstants.freeConstants();
77   AggZeroConstants.freeConstants();
78   NullPtrConstants.freeConstants();
79   UndefValueConstants.freeConstants();
80   InlineAsms.freeConstants();
81   for (IntMapTy::iterator I = IntConstants.begin(), E = IntConstants.end(); 
82        I != E; ++I) {
83     delete I->second;
84   }
85   for (FPMapTy::iterator I = FPConstants.begin(), E = FPConstants.end(); 
86        I != E; ++I) {
87     delete I->second;
88   }
89   AlwaysOpaqueTy->dropRef();
90   for (OpaqueTypesTy::iterator I = OpaqueTypes.begin(), E = OpaqueTypes.end();
91        I != E; ++I) {
92     (*I)->AbstractTypeUsers.clear();
93     delete *I;
94   }
95   // Destroy MDNodes.  ~MDNode can move and remove nodes between the MDNodeSet
96   // and the NonUniquedMDNodes sets, so copy the values out first.
97   SmallVector<MDNode*, 8> MDNodes;
98   MDNodes.reserve(MDNodeSet.size() + NonUniquedMDNodes.size());
99   for (FoldingSetIterator<MDNode> I = MDNodeSet.begin(), E = MDNodeSet.end();
100        I != E; ++I) {
101     MDNodes.push_back(&*I);
102   }
103   MDNodes.append(NonUniquedMDNodes.begin(), NonUniquedMDNodes.end());
104   for (SmallVectorImpl<MDNode *>::iterator I = MDNodes.begin(),
105          E = MDNodes.end(); I != E; ++I) {
106     (*I)->destroy();
107   }
108   assert(MDNodeSet.empty() && NonUniquedMDNodes.empty() &&
109          "Destroying all MDNodes didn't empty the Context's sets.");
110   // Destroy MDStrings.
111   for (StringMap<MDString*>::iterator I = MDStringCache.begin(),
112          E = MDStringCache.end(); I != E; ++I) {
113     delete I->second;
114   }
115 }