Make LLVMContext and LLVMContextImpl classes instead of structs.
[oota-llvm.git] / lib / VMCore / LLVMContext.cpp
1 //===-- LLVMContext.cpp - Implement LLVMContext -----------------------===//
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 LLVMContext, as a wrapper around the opaque
11 //  class LLVMContextImpl.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/LLVMContext.h"
16 #include "llvm/Constants.h"
17 #include "llvm/Instruction.h"
18 #include "llvm/Support/ManagedStatic.h"
19 #include "LLVMContextImpl.h"
20 #include <set>
21
22 using namespace llvm;
23
24 static ManagedStatic<LLVMContext> GlobalContext;
25
26 LLVMContext& llvm::getGlobalContext() {
27   return *GlobalContext;
28 }
29
30 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl()) { }
31 LLVMContext::~LLVMContext() { delete pImpl; }
32
33 GetElementPtrConstantExpr::GetElementPtrConstantExpr
34   (Constant *C,
35    const std::vector<Constant*> &IdxList,
36    const Type *DestTy)
37     : ConstantExpr(DestTy, Instruction::GetElementPtr,
38                    OperandTraits<GetElementPtrConstantExpr>::op_end(this)
39                    - (IdxList.size()+1),
40                    IdxList.size()+1) {
41   OperandList[0] = C;
42   for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
43     OperandList[i+1] = IdxList[i];
44 }
45
46 bool LLVMContext::RemoveDeadMetadata() {
47   std::vector<const MDNode *> DeadMDNodes;
48   bool Changed = false;
49   while (1) {
50
51     for (LLVMContextImpl::MDNodeMapTy::MapTy::iterator
52            I = pImpl->MDNodes.map_begin(),
53            E = pImpl->MDNodes.map_end(); I != E; ++I) {
54       const MDNode *N = cast<MDNode>(I->second);
55       if (N->use_empty()) 
56         DeadMDNodes.push_back(N);
57     }
58     
59     if (DeadMDNodes.empty())
60       return Changed;
61
62     while (!DeadMDNodes.empty()) {
63       const MDNode *N = DeadMDNodes.back(); DeadMDNodes.pop_back();
64       delete N;
65     }
66   }
67   return Changed;
68 }