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"
16 #include "llvm/ADT/STLExtras.h"
20 LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
21 : TheTrueVal(0), TheFalseVal(0),
22 VoidTy(C, Type::VoidTyID),
23 LabelTy(C, Type::LabelTyID),
24 FloatTy(C, Type::FloatTyID),
25 DoubleTy(C, Type::DoubleTyID),
26 MetadataTy(C, Type::MetadataTyID),
27 X86_FP80Ty(C, Type::X86_FP80TyID),
28 FP128Ty(C, Type::FP128TyID),
29 PPC_FP128Ty(C, Type::PPC_FP128TyID),
30 X86_MMXTy(C, Type::X86_MMXTyID),
36 InlineAsmDiagHandler = 0;
37 InlineAsmDiagContext = 0;
38 NamedStructTypesUniqueID = 0;
42 struct DropReferences {
43 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
45 template<typename PairT>
46 void operator()(const PairT &P) {
47 P.second->dropAllReferences();
52 LLVMContextImpl::~LLVMContextImpl() {
53 // NOTE: We need to delete the contents of OwnedModules, but we have to
54 // duplicate it into a temporary vector, because the destructor of Module
55 // will try to remove itself from OwnedModules set. This would cause
56 // iterator invalidation if we iterated on the set directly.
57 std::vector<Module*> Modules(OwnedModules.begin(), OwnedModules.end());
58 DeleteContainerPointers(Modules);
60 std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(),
62 std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
64 std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
66 std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
68 ExprConstants.freeConstants();
69 ArrayConstants.freeConstants();
70 StructConstants.freeConstants();
71 VectorConstants.freeConstants();
72 AggZeroConstants.freeConstants();
73 NullPtrConstants.freeConstants();
74 UndefValueConstants.freeConstants();
75 InlineAsms.freeConstants();
76 DeleteContainerSeconds(IntConstants);
77 DeleteContainerSeconds(FPConstants);
79 // Destroy MDNodes. ~MDNode can move and remove nodes between the MDNodeSet
80 // and the NonUniquedMDNodes sets, so copy the values out first.
81 SmallVector<MDNode*, 8> MDNodes;
82 MDNodes.reserve(MDNodeSet.size() + NonUniquedMDNodes.size());
83 for (FoldingSetIterator<MDNode> I = MDNodeSet.begin(), E = MDNodeSet.end();
85 MDNodes.push_back(&*I);
86 MDNodes.append(NonUniquedMDNodes.begin(), NonUniquedMDNodes.end());
87 for (SmallVectorImpl<MDNode *>::iterator I = MDNodes.begin(),
88 E = MDNodes.end(); I != E; ++I)
90 assert(MDNodeSet.empty() && NonUniquedMDNodes.empty() &&
91 "Destroying all MDNodes didn't empty the Context's sets.");
93 DeleteContainerSeconds(MDStringCache);