1 //===-- LLVMContextImpl.cpp - Implement LLVMContextImpl -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the opaque LLVMContextImpl.
12 //===----------------------------------------------------------------------===//
14 #include "LLVMContextImpl.h"
15 #include "llvm/Module.h"
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),
34 AlwaysOpaqueTy(new OpaqueType(C)) {
35 InlineAsmDiagHandler = 0;
36 InlineAsmDiagContext = 0;
38 // Make sure the AlwaysOpaqueTy stays alive as long as the Context.
39 AlwaysOpaqueTy->addRef();
40 OpaqueTypes.insert(AlwaysOpaqueTy);
44 struct DropReferences {
45 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
47 template<typename PairT>
48 void operator()(const PairT &P) {
49 P.second->dropAllReferences();
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();
64 std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(),
66 std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
68 std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
70 std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
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();
84 for (FPMapTy::iterator I = FPConstants.begin(), E = FPConstants.end();
88 AlwaysOpaqueTy->dropRef();
89 for (OpaqueTypesTy::iterator I = OpaqueTypes.begin(), E = OpaqueTypes.end();
91 (*I)->AbstractTypeUsers.clear();
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();
100 MDNodes.push_back(&*I);
102 MDNodes.append(NonUniquedMDNodes.begin(), NonUniquedMDNodes.end());
103 for (SmallVector<MDNode*, 8>::iterator I = MDNodes.begin(),
104 E = MDNodes.end(); I != E; ++I) {
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) {