Move types back to the 2.5 API.
[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/DerivedTypes.h"
18 #include "llvm/Instruction.h"
19 #include "llvm/Metadata.h"
20 #include "llvm/Support/ManagedStatic.h"
21 #include "LLVMContextImpl.h"
22 #include <cstdarg>
23
24 using namespace llvm;
25
26 static ManagedStatic<LLVMContext> GlobalContext;
27
28 LLVMContext& llvm::getGlobalContext() {
29   return *GlobalContext;
30 }
31
32 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) { }
33 LLVMContext::~LLVMContext() { delete pImpl; }
34
35 // Constant accessors
36
37 // Constructor to create a '0' constant of arbitrary type...
38 static const uint64_t zero[2] = {0, 0};
39 Constant* LLVMContext::getNullValue(const Type* Ty) {
40   switch (Ty->getTypeID()) {
41   case Type::IntegerTyID:
42     return ConstantInt::get(Ty, 0);
43   case Type::FloatTyID:
44     return ConstantFP::get(Ty->getContext(), APFloat(APInt(32, 0)));
45   case Type::DoubleTyID:
46     return ConstantFP::get(Ty->getContext(), APFloat(APInt(64, 0)));
47   case Type::X86_FP80TyID:
48     return ConstantFP::get(Ty->getContext(), APFloat(APInt(80, 2, zero)));
49   case Type::FP128TyID:
50     return ConstantFP::get(Ty->getContext(),
51                            APFloat(APInt(128, 2, zero), true));
52   case Type::PPC_FP128TyID:
53     return ConstantFP::get(Ty->getContext(), APFloat(APInt(128, 2, zero)));
54   case Type::PointerTyID:
55     return getConstantPointerNull(cast<PointerType>(Ty));
56   case Type::StructTyID:
57   case Type::ArrayTyID:
58   case Type::VectorTyID:
59     return getConstantAggregateZero(Ty);
60   default:
61     // Function, Label, or Opaque type?
62     assert(!"Cannot create a null constant of that type!");
63     return 0;
64   }
65 }
66
67 Constant* LLVMContext::getAllOnesValue(const Type* Ty) {
68   if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty))
69     return ConstantInt::get(*this, APInt::getAllOnesValue(ITy->getBitWidth()));
70   
71   std::vector<Constant*> Elts;
72   const VectorType* VTy = cast<VectorType>(Ty);
73   Elts.resize(VTy->getNumElements(), getAllOnesValue(VTy->getElementType()));
74   assert(Elts[0] && "Not a vector integer type!");
75   return cast<ConstantVector>(ConstantVector::get(Elts));
76 }
77
78 // UndefValue accessors.
79 UndefValue* LLVMContext::getUndef(const Type* Ty) {
80   return UndefValue::get(Ty);
81 }
82
83 // ConstantInt accessors.
84 ConstantInt* LLVMContext::getTrue() {
85   assert(this && "Context not initialized!");
86   assert(pImpl && "Context not initialized!");
87   return pImpl->getTrue();
88 }
89
90 ConstantInt* LLVMContext::getFalse() {
91   assert(this && "Context not initialized!");
92   assert(pImpl && "Context not initialized!");
93   return pImpl->getFalse();
94 }
95
96 // ConstantPointerNull accessors.
97 ConstantPointerNull* LLVMContext::getConstantPointerNull(const PointerType* T) {
98   return ConstantPointerNull::get(T);
99 }
100
101 // ConstantAggregateZero accessors.
102 ConstantAggregateZero* LLVMContext::getConstantAggregateZero(const Type* Ty) {
103   return pImpl->getConstantAggregateZero(Ty);
104 }
105
106 // MDNode accessors
107 MDNode* LLVMContext::getMDNode(Value* const* Vals, unsigned NumVals) {
108   return pImpl->getMDNode(Vals, NumVals);
109 }
110
111 // MDString accessors
112 MDString* LLVMContext::getMDString(const StringRef &Str) {
113   return pImpl->getMDString(Str.data(), Str.size());
114 }
115
116 void LLVMContext::erase(MDString *M) {
117   pImpl->erase(M);
118 }
119
120 void LLVMContext::erase(MDNode *M) {
121   pImpl->erase(M);
122 }
123
124 void LLVMContext::erase(ConstantAggregateZero *Z) {
125   pImpl->erase(Z);
126 }