blob: ccb8dc500fcd39e8a8c715ae931778ce83901fff [file] [log] [blame]
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +00001//===-- 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"
Owen Anderson8e89e412010-09-08 18:03:32 +000015#include "llvm/Module.h"
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000016#include <algorithm>
Dan Gohmanb29cda92010-04-15 17:08:50 +000017using namespace llvm;
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000018
19LLVMContextImpl::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),
Dale Johannesenbaa5d042010-09-10 20:55:01 +000029 X86_MMXTy(C, Type::X86_MMXTyID),
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000030 Int1Ty(C, 1),
31 Int8Ty(C, 8),
32 Int16Ty(C, 16),
33 Int32Ty(C, 32),
34 Int64Ty(C, 64),
35 AlwaysOpaqueTy(new OpaqueType(C)) {
Chris Lattner60955d42010-04-06 00:44:45 +000036 InlineAsmDiagHandler = 0;
37 InlineAsmDiagContext = 0;
38
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000039 // Make sure the AlwaysOpaqueTy stays alive as long as the Context.
40 AlwaysOpaqueTy->addRef();
41 OpaqueTypes.insert(AlwaysOpaqueTy);
42}
43
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000044namespace {
45struct DropReferences {
46 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
47 // is a Constant*.
48 template<typename PairT>
49 void operator()(const PairT &P) {
50 P.second->dropAllReferences();
51 }
52};
53}
54
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000055LLVMContextImpl::~LLVMContextImpl() {
Owen Anderson8e89e412010-09-08 18:03:32 +000056 // 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();
62 I != E; ++I)
63 delete *I;
64
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000065 std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(),
66 DropReferences());
67 std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
68 DropReferences());
69 std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
70 DropReferences());
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000071 std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
72 DropReferences());
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000073 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();
82 I != E; ++I) {
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000083 delete I->second;
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000084 }
85 for (FPMapTy::iterator I = FPConstants.begin(), E = FPConstants.end();
86 I != E; ++I) {
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000087 delete I->second;
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000088 }
89 AlwaysOpaqueTy->dropRef();
90 for (OpaqueTypesTy::iterator I = OpaqueTypes.begin(), E = OpaqueTypes.end();
91 I != E; ++I) {
92 (*I)->AbstractTypeUsers.clear();
93 delete *I;
94 }
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();
100 I != E; ++I) {
101 MDNodes.push_back(&*I);
102 }
103 MDNodes.append(NonUniquedMDNodes.begin(), NonUniquedMDNodes.end());
Dan Gohman060d5ba2010-10-12 00:15:27 +0000104 for (SmallVectorImpl<MDNode *>::iterator I = MDNodes.begin(),
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000105 E = MDNodes.end(); I != E; ++I) {
106 (*I)->destroy();
107 }
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) {
113 delete I->second;
114 }
115}