Hold the LLVMContext by reference rather than by pointer.
[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/Support/ManagedStatic.h"
19 #include "LLVMContextImpl.h"
20
21 using namespace llvm;
22
23 static ManagedStatic<LLVMContext> GlobalContext;
24
25 const LLVMContext& llvm::getGlobalContext() {
26   return *GlobalContext;
27 }
28
29 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl()) { }
30 LLVMContext::~LLVMContext() { delete pImpl; }
31
32 // ConstantInt accessors.
33 ConstantInt* LLVMContext::getConstantIntTrue() {
34   return ConstantInt::getTrue();
35 }
36
37 ConstantInt* LLVMContext::getConstantIntFalse() {
38   return ConstantInt::getFalse();
39 }
40
41 ConstantInt* LLVMContext::getConstantInt(const IntegerType* Ty, uint64_t V,
42                                          bool isSigned) {
43   return ConstantInt::get(Ty, V, isSigned);
44 }
45
46 ConstantInt* LLVMContext::getConstantIntSigned(const IntegerType* Ty,
47                                                int64_t V) {
48   return ConstantInt::getSigned(Ty, V);
49 }
50
51 ConstantInt* LLVMContext::getConstantInt(const APInt& V) {
52   return ConstantInt::get(V);
53 }
54
55 Constant* LLVMContext::getConstantInt(const Type* Ty, const APInt& V) {
56   return ConstantInt::get(Ty, V);
57 }
58
59 ConstantInt* LLVMContext::getAllOnesConstantInt(const Type* Ty) {
60   return ConstantInt::getAllOnesValue(Ty);
61 }
62
63
64 // ConstantPointerNull accessors.
65 ConstantPointerNull* LLVMContext::getConstantPointerNull(const PointerType* T) {
66   return ConstantPointerNull::get(T);
67 }
68
69
70 // ConstantStruct accessors.
71 Constant* LLVMContext::getConstantStruct(const StructType* T,
72                                          const std::vector<Constant*>& V) {
73   return ConstantStruct::get(T, V);
74 }
75
76 Constant* LLVMContext::getConstantStruct(const std::vector<Constant*>& V,
77                                          bool Packed) {
78   return ConstantStruct::get(V, Packed);
79 }
80
81 Constant* LLVMContext::getConstantStruct(Constant* const *Vals,
82                                          unsigned NumVals, bool Packed) {
83   return ConstantStruct::get(Vals, NumVals, Packed);
84 }
85
86
87 // ConstantAggregateZero accessors.
88 ConstantAggregateZero* LLVMContext::getConstantAggregateZero(const Type* Ty) {
89   return ConstantAggregateZero::get(Ty);
90 }
91
92
93 // ConstantArray accessors.
94 Constant* LLVMContext::getConstantArray(const ArrayType* T,
95                                         const std::vector<Constant*>& V) {
96   return ConstantArray::get(T, V);
97 }
98
99 Constant* LLVMContext::getConstantArray(const ArrayType* T,
100                                         Constant* const* Vals,
101                                         unsigned NumVals) {
102   return ConstantArray::get(T, Vals, NumVals);
103 }
104
105 Constant* LLVMContext::getConstantArray(const std::string& Initializer,
106                                         bool AddNull) {
107   return ConstantArray::get(Initializer, AddNull);
108 }
109
110
111 // ConstantExpr accessors.
112 Constant* LLVMContext::getConstantExpr(unsigned Opcode, Constant* C1,
113                                        Constant* C2) {
114   return ConstantExpr::get(Opcode, C1, C2);
115 }
116
117 Constant* LLVMContext::getConstantExprTrunc(Constant* C, const Type* Ty) {
118   return ConstantExpr::getTrunc(C, Ty);
119 }
120
121 Constant* LLVMContext::getConstantExprSExt(Constant* C, const Type* Ty) {
122   return ConstantExpr::getSExt(C, Ty);
123 }
124
125 Constant* LLVMContext::getConstantExprZExt(Constant* C, const Type* Ty) {
126   return ConstantExpr::getZExt(C, Ty);  
127 }
128
129 Constant* LLVMContext::getConstantExprFPTrunc(Constant* C, const Type* Ty) {
130   return ConstantExpr::getFPTrunc(C, Ty);
131 }
132
133 Constant* LLVMContext::getConstantExprFPExtend(Constant* C, const Type* Ty) {
134   return ConstantExpr::getFPExtend(C, Ty);
135 }
136
137 Constant* LLVMContext::getConstantExprUIToFP(Constant* C, const Type* Ty) {
138   return ConstantExpr::getUIToFP(C, Ty);
139 }
140
141 Constant* LLVMContext::getConstantExprSIToFP(Constant* C, const Type* Ty) {
142   return ConstantExpr::getSIToFP(C, Ty);
143 }
144
145 Constant* LLVMContext::getConstantExprFPToUI(Constant* C, const Type* Ty) {
146   return ConstantExpr::getFPToUI(C, Ty);
147 }
148
149 Constant* LLVMContext::getConstantExprFPToSI(Constant* C, const Type* Ty) {
150   return ConstantExpr::getFPToSI(C, Ty);
151 }
152
153 Constant* LLVMContext::getConstantExprPtrToInt(Constant* C, const Type* Ty) {
154   return ConstantExpr::getPtrToInt(C, Ty);
155 }
156
157 Constant* LLVMContext::getConstantExprIntToPtr(Constant* C, const Type* Ty) {
158   return ConstantExpr::getIntToPtr(C, Ty);
159 }
160
161 Constant* LLVMContext::getConstantExprBitCast(Constant* C, const Type* Ty) {
162   return ConstantExpr::getBitCast(C, Ty);
163 }
164
165 Constant* LLVMContext::getConstantExprCast(unsigned ops, Constant* C,
166                                            const Type* Ty) {
167   return ConstantExpr::getCast(ops, C, Ty);
168 }
169
170 Constant* LLVMContext::getConstantExprZExtOrBitCast(Constant* C,
171                                                     const Type* Ty) {
172   return ConstantExpr::getZExtOrBitCast(C, Ty);
173 }
174
175 Constant* LLVMContext::getConstantExprSExtOrBitCast(Constant* C,
176                                                     const Type* Ty) {
177   return ConstantExpr::getSExtOrBitCast(C, Ty);
178 }
179
180 Constant* LLVMContext::getConstantExprTruncOrBitCast(Constant* C,
181                                                      const Type* Ty) {
182   return ConstantExpr::getTruncOrBitCast(C, Ty);  
183 }
184
185 Constant* LLVMContext::getConstantExprPointerCast(Constant* C, const Type* Ty) {
186   return ConstantExpr::getPointerCast(C, Ty);
187 }
188
189 Constant* LLVMContext::getConstantExprIntegerCast(Constant* C, const Type* Ty,
190                                                   bool isSigned) {
191   return ConstantExpr::getIntegerCast(C, Ty, isSigned);
192 }
193
194 Constant* LLVMContext::getConstantExprFPCast(Constant* C, const Type* Ty) {
195   return ConstantExpr::getFPCast(C, Ty);
196 }
197
198 Constant* LLVMContext::getConstantExprSelect(Constant* C, Constant* V1,
199                                              Constant* V2) {
200   return ConstantExpr::getSelect(C, V1, V2);
201 }
202
203 Constant* LLVMContext::getConstantExprAlignOf(const Type* Ty) {
204   return ConstantExpr::getAlignOf(Ty);
205 }
206
207 Constant* LLVMContext::getConstantExprCompare(unsigned short pred,
208                                  Constant* C1, Constant* C2) {
209   return ConstantExpr::getCompare(pred, C1, C2);
210 }
211
212 Constant* LLVMContext::getConstantExprNeg(Constant* C) {
213   return ConstantExpr::getNeg(C);
214 }
215
216 Constant* LLVMContext::getConstantExprFNeg(Constant* C) {
217   return ConstantExpr::getFNeg(C);
218 }
219
220 Constant* LLVMContext::getConstantExprNot(Constant* C) {
221   return ConstantExpr::getNot(C);
222 }
223
224 Constant* LLVMContext::getConstantExprAdd(Constant* C1, Constant* C2) {
225   return ConstantExpr::getAdd(C1, C2);
226 }
227
228 Constant* LLVMContext::getConstantExprFAdd(Constant* C1, Constant* C2) {
229   return ConstantExpr::getFAdd(C1, C2);
230 }
231
232 Constant* LLVMContext::getConstantExprSub(Constant* C1, Constant* C2) {
233   return ConstantExpr::getSub(C1, C2);
234 }
235
236 Constant* LLVMContext::getConstantExprFSub(Constant* C1, Constant* C2) {
237   return ConstantExpr::getFSub(C1, C2);
238 }
239
240 Constant* LLVMContext::getConstantExprMul(Constant* C1, Constant* C2) {
241   return ConstantExpr::getMul(C1, C2);
242 }
243
244 Constant* LLVMContext::getConstantExprFMul(Constant* C1, Constant* C2) {
245   return ConstantExpr::getFMul(C1, C2);
246 }
247
248 Constant* LLVMContext::getConstantExprUDiv(Constant* C1, Constant* C2) {
249   return ConstantExpr::getUDiv(C1, C2);
250 }
251
252 Constant* LLVMContext::getConstantExprSDiv(Constant* C1, Constant* C2) {
253   return ConstantExpr::getSDiv(C1, C2);
254 }
255
256 Constant* LLVMContext::getConstantExprFDiv(Constant* C1, Constant* C2) {
257   return ConstantExpr::getFDiv(C1, C2);
258 }
259
260 Constant* LLVMContext::getConstantExprURem(Constant* C1, Constant* C2) {
261   return ConstantExpr::getURem(C1, C2);
262 }
263
264 Constant* LLVMContext::getConstantExprSRem(Constant* C1, Constant* C2) {
265   return ConstantExpr::getSRem(C1, C2);
266 }
267
268 Constant* LLVMContext::getConstantExprFRem(Constant* C1, Constant* C2) {
269   return ConstantExpr::getFRem(C1, C2);
270 }
271
272 Constant* LLVMContext::getConstantExprAnd(Constant* C1, Constant* C2) {
273   return ConstantExpr::getAnd(C1, C2);
274 }
275
276 Constant* LLVMContext::getConstantExprOr(Constant* C1, Constant* C2) {
277   return ConstantExpr::getOr(C1, C2);
278 }
279
280 Constant* LLVMContext::getConstantExprXor(Constant* C1, Constant* C2) {
281   return ConstantExpr::getXor(C1, C2);
282 }
283
284 Constant* LLVMContext::getConstantExprICmp(unsigned short pred, Constant* LHS,
285                               Constant* RHS) {
286   return ConstantExpr::getICmp(pred, LHS, RHS);
287 }
288
289 Constant* LLVMContext::getConstantExprFCmp(unsigned short pred, Constant* LHS,
290                               Constant* RHS) {
291   return ConstantExpr::getFCmp(pred, LHS, RHS);
292 }
293
294 Constant* LLVMContext::getConstantExprVICmp(unsigned short pred, Constant* LHS,
295                                Constant* RHS) {
296   return ConstantExpr::getVICmp(pred, LHS, RHS);
297 }
298
299 Constant* LLVMContext::getConstantExprVFCmp(unsigned short pred, Constant* LHS,
300                                Constant* RHS) {
301   return ConstantExpr::getVFCmp(pred, LHS, RHS);
302 }
303
304 Constant* LLVMContext::getConstantExprShl(Constant* C1, Constant* C2) {
305   return ConstantExpr::getShl(C1, C2);
306 }
307
308 Constant* LLVMContext::getConstantExprLShr(Constant* C1, Constant* C2) {
309   return ConstantExpr::getLShr(C1, C2);
310 }
311
312 Constant* LLVMContext::getConstantExprAShr(Constant* C1, Constant* C2) {
313   return ConstantExpr::getAShr(C1, C2);
314 }
315
316 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
317                                                     Constant* const* IdxList, 
318                                                     unsigned NumIdx) {
319   return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
320 }
321
322 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
323                                                     Value* const* IdxList, 
324                                                     unsigned NumIdx) {
325   return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
326 }
327
328 Constant* LLVMContext::getConstantExprExtractElement(Constant* Vec,
329                                                      Constant* Idx) {
330   return ConstantExpr::getExtractElement(Vec, Idx);
331 }
332
333 Constant* LLVMContext::getConstantExprInsertElement(Constant* Vec,
334                                                     Constant* Elt,
335                                                     Constant* Idx) {
336   return ConstantExpr::getInsertElement(Vec, Elt, Idx);
337 }
338
339 Constant* LLVMContext::getConstantExprShuffleVector(Constant* V1, Constant* V2,
340                                                     Constant* Mask) {
341   return ConstantExpr::getShuffleVector(V1, V2, Mask);
342 }
343
344 Constant* LLVMContext::getConstantExprExtractValue(Constant* Agg,
345                                                    const unsigned* IdxList, 
346                                                    unsigned NumIdx) {
347   return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
348 }
349
350 Constant* LLVMContext::getConstantExprInsertValue(Constant* Agg, Constant* Val,
351                                                   const unsigned* IdxList,
352                                                   unsigned NumIdx) {
353   return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);
354 }
355
356 Constant* LLVMContext::getZeroValueForNegation(const Type* Ty) {
357   return ConstantExpr::getZeroValueForNegationExpr(Ty);
358 }
359
360
361 // ConstantFP accessors.
362 ConstantFP* LLVMContext::getConstantFP(const APFloat& V) {
363   return ConstantFP::get(V);
364 }
365
366 Constant* LLVMContext::getConstantFP(const Type* Ty, double V) {
367   return ConstantFP::get(Ty, V);
368 }
369
370 ConstantFP* LLVMContext::getConstantFPNegativeZero(const Type* Ty) {
371   return ConstantFP::getNegativeZero(Ty);
372 }
373
374
375 // ConstantVector accessors.
376 Constant* LLVMContext::getConstantVector(const VectorType* T,
377                             const std::vector<Constant*>& V) {
378   return ConstantVector::get(T, V);
379 }
380
381 Constant* LLVMContext::getConstantVector(const std::vector<Constant*>& V) {
382   return ConstantVector::get(V);
383 }
384
385 Constant* LLVMContext::getConstantVector(Constant* const* Vals,
386                                          unsigned NumVals) {
387   return ConstantVector::get(Vals, NumVals);
388 }
389
390 ConstantVector* LLVMContext::getConstantVectorAllOnes(const VectorType* Ty) {
391   return ConstantVector::getAllOnesValue(Ty);
392 }
393
394 // FunctionType accessors
395 FunctionType* LLVMContext::getFunctionType(const Type* Result,
396                                          const std::vector<const Type*>& Params,
397                                          bool isVarArg) {
398   return FunctionType::get(Result, Params, isVarArg);
399 }
400                                 
401 // IntegerType accessors
402 const IntegerType* LLVMContext::getIntegerType(unsigned NumBits) {
403   return IntegerType::get(NumBits);
404 }
405   
406 // OpaqueType accessors
407 OpaqueType* LLVMContext::getOpaqueType() {
408   return OpaqueType::get();
409 }
410
411 // StructType accessors
412 StructType* LLVMContext::getStructType(const std::vector<const Type*>& Params,
413                                        bool isPacked) {
414   return StructType::get(Params, isPacked);
415 }
416
417 // ArrayType accessors
418 ArrayType* LLVMContext::getArrayType(const Type* ElementType,
419                                      uint64_t NumElements) {
420   return ArrayType::get(ElementType, NumElements);
421 }
422   
423 // PointerType accessors
424 PointerType* LLVMContext::getPointerType(const Type* ElementType,
425                                          unsigned AddressSpace) {
426   return PointerType::get(ElementType, AddressSpace);
427 }
428
429 PointerType* LLVMContext::getPointerTypeUnqualified(const Type* ElementType) {
430   return PointerType::getUnqual(ElementType);
431 }
432   
433 // VectorType accessors
434 VectorType* LLVMContext::getVectorType(const Type* ElementType,
435                                        unsigned NumElements) {
436   return VectorType::get(ElementType, NumElements);
437 }
438
439 VectorType* LLVMContext::getVectorTypeInteger(const VectorType* VTy) {
440   return VectorType::getInteger(VTy);  
441 }
442
443 VectorType* LLVMContext::getVectorTypeExtendedElement(const VectorType* VTy) {
444   return VectorType::getExtendedElementVectorType(VTy);
445 }
446
447 VectorType* LLVMContext::getVectorTypeTruncatedElement(const VectorType* VTy) {
448   return VectorType::getTruncatedElementVectorType(VTy);
449 }