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
),
29 X86_MMXTy(C
, Type::X86_MMXTyID
),
35 AlwaysOpaqueTy(new OpaqueType(C
)) {
36 InlineAsmDiagHandler
= 0;
37 InlineAsmDiagContext
= 0;
39 // Make sure the AlwaysOpaqueTy stays alive as long as the Context.
40 AlwaysOpaqueTy
->addRef();
41 OpaqueTypes
.insert(AlwaysOpaqueTy
);
45 struct DropReferences
{
46 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
48 template<typename PairT
>
49 void operator()(const PairT
&P
) {
50 P
.second
->dropAllReferences();
55 LLVMContextImpl::~LLVMContextImpl() {
56 // NOTE: We need to delete the contents of OwnedModules, but we have to
57 // duplicate it into a temporary vector, because the destructor of Module
58 // will try to remove itself from OwnedModules set. This would cause
59 // iterator invalidation if we iterated on the set directly.
60 std::vector
<Module
*> Modules(OwnedModules
.begin(), OwnedModules
.end());
61 for (std::vector
<Module
*>::iterator I
= Modules
.begin(), E
= Modules
.end();
65 std::for_each(ExprConstants
.map_begin(), ExprConstants
.map_end(),
67 std::for_each(ArrayConstants
.map_begin(), ArrayConstants
.map_end(),
69 std::for_each(StructConstants
.map_begin(), StructConstants
.map_end(),
71 std::for_each(VectorConstants
.map_begin(), VectorConstants
.map_end(),
73 ExprConstants
.freeConstants();
74 ArrayConstants
.freeConstants();
75 StructConstants
.freeConstants();
76 VectorConstants
.freeConstants();
77 AggZeroConstants
.freeConstants();
78 NullPtrConstants
.freeConstants();
79 UndefValueConstants
.freeConstants();
80 InlineAsms
.freeConstants();
81 for (IntMapTy::iterator I
= IntConstants
.begin(), E
= IntConstants
.end();
85 for (FPMapTy::iterator I
= FPConstants
.begin(), E
= FPConstants
.end();
89 AlwaysOpaqueTy
->dropRef();
90 for (OpaqueTypesTy::iterator I
= OpaqueTypes
.begin(), E
= OpaqueTypes
.end();
92 (*I
)->AbstractTypeUsers
.clear();
95 // Destroy MDNodes. ~MDNode can move and remove nodes between the MDNodeSet
96 // and the NonUniquedMDNodes sets, so copy the values out first.
97 SmallVector
<MDNode
*, 8> MDNodes
;
98 MDNodes
.reserve(MDNodeSet
.size() + NonUniquedMDNodes
.size());
99 for (FoldingSetIterator
<MDNode
> I
= MDNodeSet
.begin(), E
= MDNodeSet
.end();
101 MDNodes
.push_back(&*I
);
103 MDNodes
.append(NonUniquedMDNodes
.begin(), NonUniquedMDNodes
.end());
104 for (SmallVectorImpl
<MDNode
*>::iterator I
= MDNodes
.begin(),
105 E
= MDNodes
.end(); I
!= E
; ++I
) {
108 assert(MDNodeSet
.empty() && NonUniquedMDNodes
.empty() &&
109 "Destroying all MDNodes didn't empty the Context's sets.");
110 // Destroy MDStrings.
111 for (StringMap
<MDString
*>::iterator I
= MDStringCache
.begin(),
112 E
= MDStringCache
.end(); I
!= E
; ++I
) {