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