Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 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" |
Owen Anderson | 8e89e41 | 2010-09-08 18:03:32 +0000 | [diff] [blame] | 15 | #include "llvm/Module.h" |
Jeffrey Yasskin | a6eedc3 | 2010-03-22 05:23:37 +0000 | [diff] [blame] | 16 | #include <algorithm> |
Dan Gohman | b29cda9 | 2010-04-15 17:08:50 +0000 | [diff] [blame] | 17 | using namespace llvm; |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 18 | |
| 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), |
Dale Johannesen | baa5d04 | 2010-09-10 20:55:01 +0000 | [diff] [blame] | 29 | X86_MMXTy(C, Type::X86_MMXTyID), |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 30 | 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 Lattner | 60955d4 | 2010-04-06 00:44:45 +0000 | [diff] [blame] | 36 | InlineAsmDiagHandler = 0; |
| 37 | InlineAsmDiagContext = 0; |
| 38 | |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 39 | // Make sure the AlwaysOpaqueTy stays alive as long as the Context. |
| 40 | AlwaysOpaqueTy->addRef(); |
| 41 | OpaqueTypes.insert(AlwaysOpaqueTy); |
| 42 | } |
| 43 | |
Jeffrey Yasskin | a6eedc3 | 2010-03-22 05:23:37 +0000 | [diff] [blame] | 44 | namespace { |
| 45 | struct 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 Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 55 | LLVMContextImpl::~LLVMContextImpl() { |
Owen Anderson | 8e89e41 | 2010-09-08 18:03:32 +0000 | [diff] [blame] | 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(); |
| 62 | I != E; ++I) |
| 63 | delete *I; |
| 64 | |
Jeffrey Yasskin | a6eedc3 | 2010-03-22 05:23:37 +0000 | [diff] [blame] | 65 | 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 Yasskin | a6eedc3 | 2010-03-22 05:23:37 +0000 | [diff] [blame] | 71 | std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(), |
| 72 | DropReferences()); |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 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(); |
| 82 | I != E; ++I) { |
Jeffrey Yasskin | a6eedc3 | 2010-03-22 05:23:37 +0000 | [diff] [blame] | 83 | delete I->second; |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 84 | } |
| 85 | for (FPMapTy::iterator I = FPConstants.begin(), E = FPConstants.end(); |
| 86 | I != E; ++I) { |
Jeffrey Yasskin | a6eedc3 | 2010-03-22 05:23:37 +0000 | [diff] [blame] | 87 | delete I->second; |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 88 | } |
| 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 Gohman | 060d5ba | 2010-10-12 00:15:27 +0000 | [diff] [blame] | 104 | for (SmallVectorImpl<MDNode *>::iterator I = MDNodes.begin(), |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 105 | 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 | } |