LLVMContextImpl.cpp revision 36b56886974eae4f9c5ebc96befd3e7bfe5de338
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/ADT/STLExtras.h"
16#include "llvm/IR/Attributes.h"
17#include "llvm/IR/Module.h"
18#include <algorithm>
19using namespace llvm;
20
21LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
22  : TheTrueVal(0), TheFalseVal(0),
23    VoidTy(C, Type::VoidTyID),
24    LabelTy(C, Type::LabelTyID),
25    HalfTy(C, Type::HalfTyID),
26    FloatTy(C, Type::FloatTyID),
27    DoubleTy(C, Type::DoubleTyID),
28    MetadataTy(C, Type::MetadataTyID),
29    X86_FP80Ty(C, Type::X86_FP80TyID),
30    FP128Ty(C, Type::FP128TyID),
31    PPC_FP128Ty(C, Type::PPC_FP128TyID),
32    X86_MMXTy(C, Type::X86_MMXTyID),
33    Int1Ty(C, 1),
34    Int8Ty(C, 8),
35    Int16Ty(C, 16),
36    Int32Ty(C, 32),
37    Int64Ty(C, 64) {
38  InlineAsmDiagHandler = 0;
39  InlineAsmDiagContext = 0;
40  DiagnosticHandler = 0;
41  DiagnosticContext = 0;
42  NamedStructTypesUniqueID = 0;
43}
44
45namespace {
46struct DropReferences {
47  // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
48  // is a Constant*.
49  template<typename PairT>
50  void operator()(const PairT &P) {
51    P.second->dropAllReferences();
52  }
53};
54
55// Temporary - drops pair.first instead of second.
56struct DropFirst {
57  // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
58  // is a Constant*.
59  template<typename PairT>
60  void operator()(const PairT &P) {
61    P.first->dropAllReferences();
62  }
63};
64}
65
66LLVMContextImpl::~LLVMContextImpl() {
67  // NOTE: We need to delete the contents of OwnedModules, but we have to
68  // duplicate it into a temporary vector, because the destructor of Module
69  // will try to remove itself from OwnedModules set.  This would cause
70  // iterator invalidation if we iterated on the set directly.
71  std::vector<Module*> Modules(OwnedModules.begin(), OwnedModules.end());
72  DeleteContainerPointers(Modules);
73
74  // Free the constants.  This is important to do here to ensure that they are
75  // freed before the LeakDetector is torn down.
76  std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(),
77                DropReferences());
78  std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
79                DropFirst());
80  std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
81                DropFirst());
82  std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
83                DropFirst());
84  ExprConstants.freeConstants();
85  ArrayConstants.freeConstants();
86  StructConstants.freeConstants();
87  VectorConstants.freeConstants();
88  DeleteContainerSeconds(CAZConstants);
89  DeleteContainerSeconds(CPNConstants);
90  DeleteContainerSeconds(UVConstants);
91  InlineAsms.freeConstants();
92  DeleteContainerSeconds(IntConstants);
93  DeleteContainerSeconds(FPConstants);
94
95  for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(),
96       E = CDSConstants.end(); I != E; ++I)
97    delete I->second;
98  CDSConstants.clear();
99
100  // Destroy attributes.
101  for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
102         E = AttrsSet.end(); I != E; ) {
103    FoldingSetIterator<AttributeImpl> Elem = I++;
104    delete &*Elem;
105  }
106
107  // Destroy attribute lists.
108  for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(),
109         E = AttrsLists.end(); I != E; ) {
110    FoldingSetIterator<AttributeSetImpl> Elem = I++;
111    delete &*Elem;
112  }
113
114  // Destroy attribute node lists.
115  for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
116         E = AttrsSetNodes.end(); I != E; ) {
117    FoldingSetIterator<AttributeSetNode> Elem = I++;
118    delete &*Elem;
119  }
120
121  // Destroy MDNodes.  ~MDNode can move and remove nodes between the MDNodeSet
122  // and the NonUniquedMDNodes sets, so copy the values out first.
123  SmallVector<MDNode*, 8> MDNodes;
124  MDNodes.reserve(MDNodeSet.size() + NonUniquedMDNodes.size());
125  for (FoldingSetIterator<MDNode> I = MDNodeSet.begin(), E = MDNodeSet.end();
126       I != E; ++I)
127    MDNodes.push_back(&*I);
128  MDNodes.append(NonUniquedMDNodes.begin(), NonUniquedMDNodes.end());
129  for (SmallVectorImpl<MDNode *>::iterator I = MDNodes.begin(),
130         E = MDNodes.end(); I != E; ++I)
131    (*I)->destroy();
132  assert(MDNodeSet.empty() && NonUniquedMDNodes.empty() &&
133         "Destroying all MDNodes didn't empty the Context's sets.");
134
135  // Destroy MDStrings.
136  DeleteContainerSeconds(MDStringCache);
137}
138
139// ConstantsContext anchors
140void UnaryConstantExpr::anchor() { }
141
142void BinaryConstantExpr::anchor() { }
143
144void SelectConstantExpr::anchor() { }
145
146void ExtractElementConstantExpr::anchor() { }
147
148void InsertElementConstantExpr::anchor() { }
149
150void ShuffleVectorConstantExpr::anchor() { }
151
152void ExtractValueConstantExpr::anchor() { }
153
154void InsertValueConstantExpr::anchor() { }
155
156void GetElementPtrConstantExpr::anchor() { }
157
158void CompareConstantExpr::anchor() { }
159