blob: 504b37267f7074292426fc6ae7686930b0d6ad3f [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"
Nick Lewyckye9bb9a02011-07-12 00:26:08 +000016#include "llvm/ADT/STLExtras.h"
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000017#include <algorithm>
Dan Gohmanb29cda92010-04-15 17:08:50 +000018using namespace llvm;
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000019
20LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
21 : TheTrueVal(0), TheFalseVal(0),
22 VoidTy(C, Type::VoidTyID),
23 LabelTy(C, Type::LabelTyID),
24 FloatTy(C, Type::FloatTyID),
25 DoubleTy(C, Type::DoubleTyID),
26 MetadataTy(C, Type::MetadataTyID),
27 X86_FP80Ty(C, Type::X86_FP80TyID),
28 FP128Ty(C, Type::FP128TyID),
29 PPC_FP128Ty(C, Type::PPC_FP128TyID),
Dale Johannesenbaa5d042010-09-10 20:55:01 +000030 X86_MMXTy(C, Type::X86_MMXTyID),
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000031 Int1Ty(C, 1),
32 Int8Ty(C, 8),
33 Int16Ty(C, 16),
34 Int32Ty(C, 32),
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000035 Int64Ty(C, 64) {
Chris Lattner60955d42010-04-06 00:44:45 +000036 InlineAsmDiagHandler = 0;
37 InlineAsmDiagContext = 0;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000038 NamedStructTypesUniqueID = 0;
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000039}
40
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000041namespace {
42struct DropReferences {
43 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
44 // is a Constant*.
45 template<typename PairT>
46 void operator()(const PairT &P) {
47 P.second->dropAllReferences();
48 }
49};
50}
51
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000052LLVMContextImpl::~LLVMContextImpl() {
Owen Anderson8e89e412010-09-08 18:03:32 +000053 // NOTE: We need to delete the contents of OwnedModules, but we have to
54 // duplicate it into a temporary vector, because the destructor of Module
55 // will try to remove itself from OwnedModules set. This would cause
56 // iterator invalidation if we iterated on the set directly.
57 std::vector<Module*> Modules(OwnedModules.begin(), OwnedModules.end());
Nick Lewyckye9bb9a02011-07-12 00:26:08 +000058 DeleteContainerPointers(Modules);
Owen Anderson8e89e412010-09-08 18:03:32 +000059
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000060 std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(),
61 DropReferences());
62 std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
63 DropReferences());
64 std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
65 DropReferences());
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000066 std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
67 DropReferences());
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000068 ExprConstants.freeConstants();
69 ArrayConstants.freeConstants();
70 StructConstants.freeConstants();
71 VectorConstants.freeConstants();
72 AggZeroConstants.freeConstants();
73 NullPtrConstants.freeConstants();
74 UndefValueConstants.freeConstants();
75 InlineAsms.freeConstants();
Nick Lewyckye9bb9a02011-07-12 00:26:08 +000076 DeleteContainerSeconds(IntConstants);
77 DeleteContainerSeconds(FPConstants);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000078
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000079 // Destroy MDNodes. ~MDNode can move and remove nodes between the MDNodeSet
80 // and the NonUniquedMDNodes sets, so copy the values out first.
81 SmallVector<MDNode*, 8> MDNodes;
82 MDNodes.reserve(MDNodeSet.size() + NonUniquedMDNodes.size());
83 for (FoldingSetIterator<MDNode> I = MDNodeSet.begin(), E = MDNodeSet.end();
Chris Lattner68ef6942011-07-13 04:22:39 +000084 I != E; ++I)
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000085 MDNodes.push_back(&*I);
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000086 MDNodes.append(NonUniquedMDNodes.begin(), NonUniquedMDNodes.end());
Dan Gohman060d5ba2010-10-12 00:15:27 +000087 for (SmallVectorImpl<MDNode *>::iterator I = MDNodes.begin(),
Chris Lattner68ef6942011-07-13 04:22:39 +000088 E = MDNodes.end(); I != E; ++I)
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000089 (*I)->destroy();
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000090 assert(MDNodeSet.empty() && NonUniquedMDNodes.empty() &&
91 "Destroying all MDNodes didn't empty the Context's sets.");
92 // Destroy MDStrings.
Nick Lewyckye9bb9a02011-07-12 00:26:08 +000093 DeleteContainerSeconds(MDStringCache);
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000094}