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" 15 #include "llvm/ADT/STLExtras.h" 16 #include "llvm/IR/Attributes.h" 17 #include "llvm/IR/DiagnosticInfo.h" 18 #include "llvm/IR/Module.h" 19 #include <algorithm> 20 using namespace llvm; 21 22 LLVMContextImpl::LLVMContextImpl(LLVMContext &C) 23 : TheTrueVal(nullptr), TheFalseVal(nullptr), 24 VoidTy(C, Type::VoidTyID), 25 LabelTy(C, Type::LabelTyID), 26 HalfTy(C, Type::HalfTyID), 27 FloatTy(C, Type::FloatTyID), 28 DoubleTy(C, Type::DoubleTyID), 29 MetadataTy(C, Type::MetadataTyID), 30 X86_FP80Ty(C, Type::X86_FP80TyID), 31 FP128Ty(C, Type::FP128TyID), 32 PPC_FP128Ty(C, Type::PPC_FP128TyID), 33 X86_MMXTy(C, Type::X86_MMXTyID), 34 Int1Ty(C, 1), 35 Int8Ty(C, 8), 36 Int16Ty(C, 16), 37 Int32Ty(C, 32), 38 Int64Ty(C, 64) { 39 InlineAsmDiagHandler = nullptr; 40 InlineAsmDiagContext = nullptr; 41 DiagnosticHandler = nullptr; 42 DiagnosticContext = nullptr; 43 RespectDiagnosticFilters = false; 44 YieldCallback = nullptr; 45 YieldOpaqueHandle = nullptr; 46 NamedStructTypesUniqueID = 0; 47 } 48 49 namespace { 50 struct DropReferences { 51 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second' 52 // is a Constant*. 53 template <typename PairT> void operator()(const PairT &P) { 54 P.second->dropAllReferences(); 55 } 56 }; 57 58 // Temporary - drops pair.first instead of second. 59 struct DropFirst { 60 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second' 61 // is a Constant*. 62 template<typename PairT> 63 void operator()(const PairT &P) { 64 P.first->dropAllReferences(); 65 } 66 }; 67 } 68 69 LLVMContextImpl::~LLVMContextImpl() { 70 // NOTE: We need to delete the contents of OwnedModules, but Module's dtor 71 // will call LLVMContextImpl::removeModule, thus invalidating iterators into 72 // the container. Avoid iterators during this operation: 73 while (!OwnedModules.empty()) 74 delete *OwnedModules.begin(); 75 76 // Free the constants. This is important to do here to ensure that they are 77 // freed before the LeakDetector is torn down. 78 std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(), 79 DropFirst()); 80 std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(), 81 DropFirst()); 82 std::for_each(StructConstants.map_begin(), StructConstants.map_end(), 83 DropFirst()); 84 std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(), 85 DropFirst()); 86 ExprConstants.freeConstants(); 87 ArrayConstants.freeConstants(); 88 StructConstants.freeConstants(); 89 VectorConstants.freeConstants(); 90 DeleteContainerSeconds(CAZConstants); 91 DeleteContainerSeconds(CPNConstants); 92 DeleteContainerSeconds(UVConstants); 93 InlineAsms.freeConstants(); 94 DeleteContainerSeconds(IntConstants); 95 DeleteContainerSeconds(FPConstants); 96 97 for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(), 98 E = CDSConstants.end(); I != E; ++I) 99 delete I->second; 100 CDSConstants.clear(); 101 102 // Destroy attributes. 103 for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(), 104 E = AttrsSet.end(); I != E; ) { 105 FoldingSetIterator<AttributeImpl> Elem = I++; 106 delete &*Elem; 107 } 108 109 // Destroy attribute lists. 110 for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(), 111 E = AttrsLists.end(); I != E; ) { 112 FoldingSetIterator<AttributeSetImpl> Elem = I++; 113 delete &*Elem; 114 } 115 116 // Destroy attribute node lists. 117 for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(), 118 E = AttrsSetNodes.end(); I != E; ) { 119 FoldingSetIterator<AttributeSetNode> Elem = I++; 120 delete &*Elem; 121 } 122 123 // Destroy MDNodes. ~MDNode can move and remove nodes between the MDNodeSet 124 // and the NonUniquedMDNodes sets, so copy the values out first. 125 SmallVector<MDNode*, 8> MDNodes; 126 MDNodes.reserve(MDNodeSet.size() + NonUniquedMDNodes.size()); 127 for (FoldingSetIterator<MDNode> I = MDNodeSet.begin(), E = MDNodeSet.end(); 128 I != E; ++I) 129 MDNodes.push_back(&*I); 130 MDNodes.append(NonUniquedMDNodes.begin(), NonUniquedMDNodes.end()); 131 for (SmallVectorImpl<MDNode *>::iterator I = MDNodes.begin(), 132 E = MDNodes.end(); I != E; ++I) 133 (*I)->destroy(); 134 assert(MDNodeSet.empty() && NonUniquedMDNodes.empty() && 135 "Destroying all MDNodes didn't empty the Context's sets."); 136 137 // Destroy MDStrings. 138 DeleteContainerSeconds(MDStringCache); 139 } 140 141 // ConstantsContext anchors 142 void UnaryConstantExpr::anchor() { } 143 144 void BinaryConstantExpr::anchor() { } 145 146 void SelectConstantExpr::anchor() { } 147 148 void ExtractElementConstantExpr::anchor() { } 149 150 void InsertElementConstantExpr::anchor() { } 151 152 void ShuffleVectorConstantExpr::anchor() { } 153 154 void ExtractValueConstantExpr::anchor() { } 155 156 void InsertValueConstantExpr::anchor() { } 157 158 void GetElementPtrConstantExpr::anchor() { } 159 160 void CompareConstantExpr::anchor() { } 161