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/ADT/STLExtras.h"
16 #include "llvm/IR/Attributes.h"
17 #include "llvm/IR/DiagnosticInfo.h"
18 #include "llvm/IR/Module.h"
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),
39 InlineAsmDiagHandler = nullptr;
40 InlineAsmDiagContext = nullptr;
41 DiagnosticHandler = nullptr;
42 DiagnosticContext = nullptr;
43 RespectDiagnosticFilters = false;
44 YieldCallback = nullptr;
45 YieldOpaqueHandle = nullptr;
46 NamedStructTypesUniqueID = 0;
50 struct DropReferences {
51 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
53 template <typename PairT> void operator()(const PairT &P) {
54 P.second->dropAllReferences();
58 // Temporary - drops pair.first instead of second.
60 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
62 template<typename PairT>
63 void operator()(const PairT &P) {
64 P.first->dropAllReferences();
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();
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(),
80 std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
82 std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
84 std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
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);
97 for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(),
98 E = CDSConstants.end(); I != E; ++I)
100 CDSConstants.clear();
102 // Destroy attributes.
103 for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
104 E = AttrsSet.end(); I != E; ) {
105 FoldingSetIterator<AttributeImpl> Elem = I++;
109 // Destroy attribute lists.
110 for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(),
111 E = AttrsLists.end(); I != E; ) {
112 FoldingSetIterator<AttributeSetImpl> Elem = I++;
116 // Destroy attribute node lists.
117 for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
118 E = AttrsSetNodes.end(); I != E; ) {
119 FoldingSetIterator<AttributeSetNode> Elem = I++;
123 // Destroy MetadataAsValues.
125 SmallVector<MetadataAsValue *, 8> MDVs;
126 MDVs.reserve(MetadataAsValues.size());
127 for (auto &Pair : MetadataAsValues)
128 MDVs.push_back(Pair.second);
129 MetadataAsValues.clear();
134 // Destroy ValuesAsMetadata.
135 for (auto &Pair : ValuesAsMetadata)
138 // Destroy MDNodes. ~MDNode can move and remove nodes between the MDTuples
139 // and the DistinctMDNodes sets, so copy the values out first.
140 SmallVector<UniquableMDNode *, 8> Uniquables;
141 Uniquables.reserve(MDTuples.size() + DistinctMDNodes.size());
142 Uniquables.append(MDTuples.begin(), MDTuples.end());
143 Uniquables.append(DistinctMDNodes.begin(), DistinctMDNodes.end());
144 for (UniquableMDNode *I : Uniquables)
145 I->dropAllReferences();
146 for (UniquableMDNode *I : Uniquables)
147 delete cast<MDTuple>(I);
148 assert(MDTuples.empty() && DistinctMDNodes.empty() &&
149 "Destroying all MDNodes didn't empty the Context's sets.");
151 // Destroy MDStrings.
152 MDStringCache.clear();
155 // ConstantsContext anchors
156 void UnaryConstantExpr::anchor() { }
158 void BinaryConstantExpr::anchor() { }
160 void SelectConstantExpr::anchor() { }
162 void ExtractElementConstantExpr::anchor() { }
164 void InsertElementConstantExpr::anchor() { }
166 void ShuffleVectorConstantExpr::anchor() { }
168 void ExtractValueConstantExpr::anchor() { }
170 void InsertValueConstantExpr::anchor() { }
172 void GetElementPtrConstantExpr::anchor() { }
174 void CompareConstantExpr::anchor() { }