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