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 YieldCallback = nullptr;
44 YieldOpaqueHandle = nullptr;
45 NamedStructTypesUniqueID = 0;
49 struct DropReferences {
50 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
52 template <typename PairT> void operator()(const PairT &P) {
53 P.second->dropAllReferences();
57 // Temporary - drops pair.first instead of second.
59 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
61 template<typename PairT>
62 void operator()(const PairT &P) {
63 P.first->dropAllReferences();
68 LLVMContextImpl::~LLVMContextImpl() {
69 // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
70 // will call LLVMContextImpl::removeModule, thus invalidating iterators into
71 // the container. Avoid iterators during this operation:
72 while (!OwnedModules.empty())
73 delete *OwnedModules.begin();
75 // Free the constants. This is important to do here to ensure that they are
76 // freed before the LeakDetector is torn down.
77 std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(),
79 std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
81 std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
83 std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
85 ExprConstants.freeConstants();
86 ArrayConstants.freeConstants();
87 StructConstants.freeConstants();
88 VectorConstants.freeConstants();
89 DeleteContainerSeconds(CAZConstants);
90 DeleteContainerSeconds(CPNConstants);
91 DeleteContainerSeconds(UVConstants);
92 InlineAsms.freeConstants();
93 DeleteContainerSeconds(IntConstants);
94 DeleteContainerSeconds(FPConstants);
96 for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(),
97 E = CDSConstants.end(); I != E; ++I)
101 // Destroy attributes.
102 for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
103 E = AttrsSet.end(); I != E; ) {
104 FoldingSetIterator<AttributeImpl> Elem = I++;
108 // Destroy attribute lists.
109 for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(),
110 E = AttrsLists.end(); I != E; ) {
111 FoldingSetIterator<AttributeSetImpl> Elem = I++;
115 // Destroy attribute node lists.
116 for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
117 E = AttrsSetNodes.end(); I != E; ) {
118 FoldingSetIterator<AttributeSetNode> Elem = I++;
122 // Destroy MDNodes. ~MDNode can move and remove nodes between the MDNodeSet
123 // and the NonUniquedMDNodes sets, so copy the values out first.
124 SmallVector<MDNode*, 8> MDNodes;
125 MDNodes.reserve(MDNodeSet.size() + NonUniquedMDNodes.size());
126 for (FoldingSetIterator<MDNode> I = MDNodeSet.begin(), E = MDNodeSet.end();
128 MDNodes.push_back(&*I);
129 MDNodes.append(NonUniquedMDNodes.begin(), NonUniquedMDNodes.end());
130 for (SmallVectorImpl<MDNode *>::iterator I = MDNodes.begin(),
131 E = MDNodes.end(); I != E; ++I)
133 assert(MDNodeSet.empty() && NonUniquedMDNodes.empty() &&
134 "Destroying all MDNodes didn't empty the Context's sets.");
136 // Destroy MDStrings.
137 DeleteContainerSeconds(MDStringCache);
140 // ConstantsContext anchors
141 void UnaryConstantExpr::anchor() { }
143 void BinaryConstantExpr::anchor() { }
145 void SelectConstantExpr::anchor() { }
147 void ExtractElementConstantExpr::anchor() { }
149 void InsertElementConstantExpr::anchor() { }
151 void ShuffleVectorConstantExpr::anchor() { }
153 void ExtractValueConstantExpr::anchor() { }
155 void InsertValueConstantExpr::anchor() { }
157 void GetElementPtrConstantExpr::anchor() { }
159 void CompareConstantExpr::anchor() { }