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