Support/MemoryBuffer: Replace all uses of std::string *ErrMsg with error_code &ec...
[oota-llvm.git] / lib / VMCore / Core.cpp
1 //===-- Core.cpp ----------------------------------------------------------===//
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 common infrastructure (including the C bindings)
11 // for libLLVMCore.a, which implements the LLVM intermediate representation.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm-c/Core.h"
16 #include "llvm/Bitcode/ReaderWriter.h"
17 #include "llvm/Constants.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/GlobalVariable.h"
20 #include "llvm/GlobalAlias.h"
21 #include "llvm/LLVMContext.h"
22 #include "llvm/TypeSymbolTable.h"
23 #include "llvm/InlineAsm.h"
24 #include "llvm/IntrinsicInst.h"
25 #include "llvm/PassManager.h"
26 #include "llvm/Support/CallSite.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/MemoryBuffer.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include "llvm/Support/system_error.h"
32 #include <cassert>
33 #include <cstdlib>
34 #include <cstring>
35
36 using namespace llvm;
37
38 void llvm::initializeCore(PassRegistry &Registry) {
39   initializeDominatorTreePass(Registry);
40   initializeDominanceFrontierPass(Registry);
41   initializePrintModulePassPass(Registry);
42   initializePrintFunctionPassPass(Registry);
43   initializeVerifierPass(Registry);
44   initializePreVerifierPass(Registry);
45 }
46
47 void LLVMInitializeCore(LLVMPassRegistryRef R) {
48   initializeCore(*unwrap(R));
49 }
50
51 /*===-- Error handling ----------------------------------------------------===*/
52
53 void LLVMDisposeMessage(char *Message) {
54   free(Message);
55 }
56
57
58 /*===-- Operations on contexts --------------------------------------------===*/
59
60 LLVMContextRef LLVMContextCreate() {
61   return wrap(new LLVMContext());
62 }
63
64 LLVMContextRef LLVMGetGlobalContext() {
65   return wrap(&getGlobalContext());
66 }
67
68 void LLVMContextDispose(LLVMContextRef C) {
69   delete unwrap(C);
70 }
71
72 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char* Name,
73                                   unsigned SLen) {
74   return unwrap(C)->getMDKindID(StringRef(Name, SLen));
75 }
76
77 unsigned LLVMGetMDKindID(const char* Name, unsigned SLen) {
78   return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
79 }
80
81
82 /*===-- Operations on modules ---------------------------------------------===*/
83
84 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
85   return wrap(new Module(ModuleID, getGlobalContext()));
86 }
87
88 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID, 
89                                                 LLVMContextRef C) {
90   return wrap(new Module(ModuleID, *unwrap(C)));
91 }
92
93 void LLVMDisposeModule(LLVMModuleRef M) {
94   delete unwrap(M);
95 }
96
97 /*--.. Data layout .........................................................--*/
98 const char * LLVMGetDataLayout(LLVMModuleRef M) {
99   return unwrap(M)->getDataLayout().c_str();
100 }
101
102 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
103   unwrap(M)->setDataLayout(Triple);
104 }
105
106 /*--.. Target triple .......................................................--*/
107 const char * LLVMGetTarget(LLVMModuleRef M) {
108   return unwrap(M)->getTargetTriple().c_str();
109 }
110
111 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
112   unwrap(M)->setTargetTriple(Triple);
113 }
114
115 /*--.. Type names ..........................................................--*/
116 LLVMBool LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
117   return unwrap(M)->addTypeName(Name, unwrap(Ty));
118 }
119
120 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
121   TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
122
123   TypeSymbolTable::iterator I = TST.find(Name);
124   if (I != TST.end())
125     TST.remove(I);
126 }
127
128 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
129   return wrap(unwrap(M)->getTypeByName(Name));
130 }
131
132 void LLVMDumpModule(LLVMModuleRef M) {
133   unwrap(M)->dump();
134 }
135
136 /*--.. Operations on inline assembler ......................................--*/
137 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
138   unwrap(M)->setModuleInlineAsm(StringRef(Asm));
139 }
140
141
142 /*--.. Operations on module contexts ......................................--*/
143 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
144   return wrap(&unwrap(M)->getContext());
145 }
146
147
148 /*===-- Operations on types -----------------------------------------------===*/
149
150 /*--.. Operations on all types (mostly) ....................................--*/
151
152 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
153   switch (unwrap(Ty)->getTypeID()) {
154   default:
155     assert(false && "Unhandled TypeID.");
156   case Type::VoidTyID:
157     return LLVMVoidTypeKind;
158   case Type::FloatTyID:
159     return LLVMFloatTypeKind;
160   case Type::DoubleTyID:
161     return LLVMDoubleTypeKind;
162   case Type::X86_FP80TyID:
163     return LLVMX86_FP80TypeKind;
164   case Type::FP128TyID:
165     return LLVMFP128TypeKind;
166   case Type::PPC_FP128TyID:
167     return LLVMPPC_FP128TypeKind;
168   case Type::LabelTyID:
169     return LLVMLabelTypeKind;
170   case Type::MetadataTyID:
171     return LLVMMetadataTypeKind;
172   case Type::IntegerTyID:
173     return LLVMIntegerTypeKind;
174   case Type::FunctionTyID:
175     return LLVMFunctionTypeKind;
176   case Type::StructTyID:
177     return LLVMStructTypeKind;
178   case Type::ArrayTyID:
179     return LLVMArrayTypeKind;
180   case Type::PointerTyID:
181     return LLVMPointerTypeKind;
182   case Type::OpaqueTyID:
183     return LLVMOpaqueTypeKind;
184   case Type::VectorTyID:
185     return LLVMVectorTypeKind;
186   case Type::X86_MMXTyID:
187     return LLVMX86_MMXTypeKind;
188   }
189 }
190
191 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
192   return wrap(&unwrap(Ty)->getContext());
193 }
194
195 /*--.. Operations on integer types .........................................--*/
196
197 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C)  {
198   return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
199 }
200 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C)  {
201   return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
202 }
203 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
204   return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
205 }
206 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
207   return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
208 }
209 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
210   return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
211 }
212 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
213   return wrap(IntegerType::get(*unwrap(C), NumBits));
214 }
215
216 LLVMTypeRef LLVMInt1Type(void)  {
217   return LLVMInt1TypeInContext(LLVMGetGlobalContext());
218 }
219 LLVMTypeRef LLVMInt8Type(void)  {
220   return LLVMInt8TypeInContext(LLVMGetGlobalContext());
221 }
222 LLVMTypeRef LLVMInt16Type(void) {
223   return LLVMInt16TypeInContext(LLVMGetGlobalContext());
224 }
225 LLVMTypeRef LLVMInt32Type(void) {
226   return LLVMInt32TypeInContext(LLVMGetGlobalContext());
227 }
228 LLVMTypeRef LLVMInt64Type(void) {
229   return LLVMInt64TypeInContext(LLVMGetGlobalContext());
230 }
231 LLVMTypeRef LLVMIntType(unsigned NumBits) {
232   return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
233 }
234
235 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
236   return unwrap<IntegerType>(IntegerTy)->getBitWidth();
237 }
238
239 /*--.. Operations on real types ............................................--*/
240
241 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
242   return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
243 }
244 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
245   return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
246 }
247 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
248   return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
249 }
250 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
251   return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
252 }
253 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
254   return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
255 }
256 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
257   return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
258 }
259
260 LLVMTypeRef LLVMFloatType(void) {
261   return LLVMFloatTypeInContext(LLVMGetGlobalContext());
262 }
263 LLVMTypeRef LLVMDoubleType(void) {
264   return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
265 }
266 LLVMTypeRef LLVMX86FP80Type(void) {
267   return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
268 }
269 LLVMTypeRef LLVMFP128Type(void) {
270   return LLVMFP128TypeInContext(LLVMGetGlobalContext());
271 }
272 LLVMTypeRef LLVMPPCFP128Type(void) {
273   return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
274 }
275 LLVMTypeRef LLVMX86MMXType(void) {
276   return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
277 }
278
279 /*--.. Operations on function types ........................................--*/
280
281 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
282                              LLVMTypeRef *ParamTypes, unsigned ParamCount,
283                              LLVMBool IsVarArg) {
284   std::vector<const Type*> Tys;
285   for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
286     Tys.push_back(unwrap(*I));
287   
288   return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
289 }
290
291 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
292   return unwrap<FunctionType>(FunctionTy)->isVarArg();
293 }
294
295 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
296   return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
297 }
298
299 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
300   return unwrap<FunctionType>(FunctionTy)->getNumParams();
301 }
302
303 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
304   FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
305   for (FunctionType::param_iterator I = Ty->param_begin(),
306                                     E = Ty->param_end(); I != E; ++I)
307     *Dest++ = wrap(*I);
308 }
309
310 /*--.. Operations on struct types ..........................................--*/
311
312 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
313                            unsigned ElementCount, LLVMBool Packed) {
314   std::vector<const Type*> Tys;
315   for (LLVMTypeRef *I = ElementTypes,
316                    *E = ElementTypes + ElementCount; I != E; ++I)
317     Tys.push_back(unwrap(*I));
318   
319   return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
320 }
321
322 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
323                            unsigned ElementCount, LLVMBool Packed) {
324   return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
325                                  ElementCount, Packed);
326 }
327
328
329 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
330   return unwrap<StructType>(StructTy)->getNumElements();
331 }
332
333 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
334   StructType *Ty = unwrap<StructType>(StructTy);
335   for (FunctionType::param_iterator I = Ty->element_begin(),
336                                     E = Ty->element_end(); I != E; ++I)
337     *Dest++ = wrap(*I);
338 }
339
340 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
341   return unwrap<StructType>(StructTy)->isPacked();
342 }
343
344 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
345
346 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
347   return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
348 }
349
350 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
351   return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
352 }
353
354 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
355   return wrap(VectorType::get(unwrap(ElementType), ElementCount));
356 }
357
358 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
359   return wrap(unwrap<SequentialType>(Ty)->getElementType());
360 }
361
362 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
363   return unwrap<ArrayType>(ArrayTy)->getNumElements();
364 }
365
366 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
367   return unwrap<PointerType>(PointerTy)->getAddressSpace();
368 }
369
370 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
371   return unwrap<VectorType>(VectorTy)->getNumElements();
372 }
373
374 /*--.. Operations on other types ...........................................--*/
375
376 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C)  {
377   return wrap(Type::getVoidTy(*unwrap(C)));
378 }
379 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
380   return wrap(Type::getLabelTy(*unwrap(C)));
381 }
382 LLVMTypeRef LLVMOpaqueTypeInContext(LLVMContextRef C) {
383   return wrap(OpaqueType::get(*unwrap(C)));
384 }
385
386 LLVMTypeRef LLVMVoidType(void)  {
387   return LLVMVoidTypeInContext(LLVMGetGlobalContext());
388 }
389 LLVMTypeRef LLVMLabelType(void) {
390   return LLVMLabelTypeInContext(LLVMGetGlobalContext());
391 }
392 LLVMTypeRef LLVMOpaqueType(void) {
393   return LLVMOpaqueTypeInContext(LLVMGetGlobalContext());
394 }
395
396 /*--.. Operations on type handles ..........................................--*/
397
398 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
399   return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
400 }
401
402 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
403   delete unwrap(TypeHandle);
404 }
405
406 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
407   return wrap(unwrap(TypeHandle)->get());
408 }
409
410 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
411   unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
412 }
413
414
415 /*===-- Operations on values ----------------------------------------------===*/
416
417 /*--.. Operations on all values ............................................--*/
418
419 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
420   return wrap(unwrap(Val)->getType());
421 }
422
423 const char *LLVMGetValueName(LLVMValueRef Val) {
424   return unwrap(Val)->getName().data();
425 }
426
427 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
428   unwrap(Val)->setName(Name);
429 }
430
431 void LLVMDumpValue(LLVMValueRef Val) {
432   unwrap(Val)->dump();
433 }
434
435 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
436   unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
437 }
438
439 int LLVMHasMetadata(LLVMValueRef Inst) {
440   return unwrap<Instruction>(Inst)->hasMetadata();
441 }
442
443 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
444   return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
445 }
446
447 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
448   unwrap<Instruction>(Inst)->setMetadata(KindID, MD? unwrap<MDNode>(MD) : NULL);
449 }
450
451 /*--.. Conversion functions ................................................--*/
452
453 #define LLVM_DEFINE_VALUE_CAST(name)                                       \
454   LLVMValueRef LLVMIsA##name(LLVMValueRef Val) {                           \
455     return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
456   }
457
458 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
459
460 /*--.. Operations on Uses ..................................................--*/
461 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
462   Value *V = unwrap(Val);
463   Value::use_iterator I = V->use_begin();
464   if (I == V->use_end())
465     return 0;
466   return wrap(&(I.getUse()));
467 }
468
469 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
470   Use *Next = unwrap(U)->getNext();
471   if (Next)
472     return wrap(Next);
473   return 0;
474 }
475
476 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
477   return wrap(unwrap(U)->getUser());
478 }
479
480 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
481   return wrap(unwrap(U)->get());
482 }
483
484 /*--.. Operations on Users .................................................--*/
485 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
486   return wrap(unwrap<User>(Val)->getOperand(Index));
487 }
488
489 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
490   unwrap<User>(Val)->setOperand(Index, unwrap(Op));
491 }
492
493 int LLVMGetNumOperands(LLVMValueRef Val) {
494   return unwrap<User>(Val)->getNumOperands();
495 }
496
497 /*--.. Operations on constants of any type .................................--*/
498
499 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
500   return wrap(Constant::getNullValue(unwrap(Ty)));
501 }
502
503 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
504   return wrap(Constant::getAllOnesValue(unwrap(Ty)));
505 }
506
507 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
508   return wrap(UndefValue::get(unwrap(Ty)));
509 }
510
511 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
512   return isa<Constant>(unwrap(Ty));
513 }
514
515 LLVMBool LLVMIsNull(LLVMValueRef Val) {
516   if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
517     return C->isNullValue();
518   return false;
519 }
520
521 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
522   return isa<UndefValue>(unwrap(Val));
523 }
524
525 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
526   return
527       wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
528 }
529
530 /*--.. Operations on metadata nodes ........................................--*/
531
532 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
533                                    unsigned SLen) {
534   return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
535 }
536
537 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
538   return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
539 }
540
541 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
542                                  unsigned Count) {
543   return wrap(MDNode::get(*unwrap(C), unwrap<Value>(Vals, Count), Count));
544 }
545
546 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
547   return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
548 }
549
550 /*--.. Operations on scalar constants ......................................--*/
551
552 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
553                           LLVMBool SignExtend) {
554   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
555 }
556
557 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
558                                               unsigned NumWords,
559                                               const uint64_t Words[]) {
560     IntegerType *Ty = unwrap<IntegerType>(IntTy);
561     return wrap(ConstantInt::get(Ty->getContext(),
562                                  APInt(Ty->getBitWidth(), NumWords, Words)));
563 }
564
565 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
566                                   uint8_t Radix) {
567   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
568                                Radix));
569 }
570
571 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
572                                          unsigned SLen, uint8_t Radix) {
573   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
574                                Radix));
575 }
576
577 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
578   return wrap(ConstantFP::get(unwrap(RealTy), N));
579 }
580
581 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
582   return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
583 }
584
585 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
586                                           unsigned SLen) {
587   return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
588 }
589
590 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
591   return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
592 }
593
594 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
595   return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
596 }
597
598 /*--.. Operations on composite constants ...................................--*/
599
600 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
601                                       unsigned Length,
602                                       LLVMBool DontNullTerminate) {
603   /* Inverted the sense of AddNull because ', 0)' is a
604      better mnemonic for null termination than ', 1)'. */
605   return wrap(ConstantArray::get(*unwrap(C), StringRef(Str, Length),
606                                  DontNullTerminate == 0));
607 }
608 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C, 
609                                       LLVMValueRef *ConstantVals,
610                                       unsigned Count, LLVMBool Packed) {
611   return wrap(ConstantStruct::get(*unwrap(C),
612                                   unwrap<Constant>(ConstantVals, Count),
613                                   Count, Packed != 0));
614 }
615
616 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
617                              LLVMBool DontNullTerminate) {
618   return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
619                                   DontNullTerminate);
620 }
621 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
622                             LLVMValueRef *ConstantVals, unsigned Length) {
623   return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
624                                  unwrap<Constant>(ConstantVals, Length),
625                                  Length));
626 }
627 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
628                              LLVMBool Packed) {
629   return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
630                                   Packed);
631 }
632 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
633   return wrap(ConstantVector::get(
634                             unwrap<Constant>(ScalarConstantVals, Size), Size));
635 }
636 /*--.. Constant expressions ................................................--*/
637
638 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
639   return (LLVMOpcode)unwrap<ConstantExpr>(ConstantVal)->getOpcode();
640 }
641
642 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
643   return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
644 }
645
646 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
647   return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
648 }
649
650 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
651   return wrap(ConstantExpr::getNeg(
652                                    unwrap<Constant>(ConstantVal)));
653 }
654
655 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
656   return wrap(ConstantExpr::getNSWNeg(
657                                       unwrap<Constant>(ConstantVal)));
658 }
659
660 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
661   return wrap(ConstantExpr::getNUWNeg(
662                                       unwrap<Constant>(ConstantVal)));
663 }
664
665
666 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
667   return wrap(ConstantExpr::getFNeg(
668                                     unwrap<Constant>(ConstantVal)));
669 }
670
671 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
672   return wrap(ConstantExpr::getNot(
673                                    unwrap<Constant>(ConstantVal)));
674 }
675
676 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
677   return wrap(ConstantExpr::getAdd(
678                                    unwrap<Constant>(LHSConstant),
679                                    unwrap<Constant>(RHSConstant)));
680 }
681
682 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
683                              LLVMValueRef RHSConstant) {
684   return wrap(ConstantExpr::getNSWAdd(
685                                       unwrap<Constant>(LHSConstant),
686                                       unwrap<Constant>(RHSConstant)));
687 }
688
689 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
690                              LLVMValueRef RHSConstant) {
691   return wrap(ConstantExpr::getNUWAdd(
692                                       unwrap<Constant>(LHSConstant),
693                                       unwrap<Constant>(RHSConstant)));
694 }
695
696 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
697   return wrap(ConstantExpr::getFAdd(
698                                     unwrap<Constant>(LHSConstant),
699                                     unwrap<Constant>(RHSConstant)));
700 }
701
702 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
703   return wrap(ConstantExpr::getSub(
704                                    unwrap<Constant>(LHSConstant),
705                                    unwrap<Constant>(RHSConstant)));
706 }
707
708 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
709                              LLVMValueRef RHSConstant) {
710   return wrap(ConstantExpr::getNSWSub(
711                                       unwrap<Constant>(LHSConstant),
712                                       unwrap<Constant>(RHSConstant)));
713 }
714
715 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
716                              LLVMValueRef RHSConstant) {
717   return wrap(ConstantExpr::getNUWSub(
718                                       unwrap<Constant>(LHSConstant),
719                                       unwrap<Constant>(RHSConstant)));
720 }
721
722 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
723   return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
724                                     unwrap<Constant>(RHSConstant)));
725 }
726
727 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
728   return wrap(ConstantExpr::getMul(
729                                    unwrap<Constant>(LHSConstant),
730                                    unwrap<Constant>(RHSConstant)));
731 }
732
733 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
734                              LLVMValueRef RHSConstant) {
735   return wrap(ConstantExpr::getNSWMul(
736                                       unwrap<Constant>(LHSConstant),
737                                       unwrap<Constant>(RHSConstant)));
738 }
739
740 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
741                              LLVMValueRef RHSConstant) {
742   return wrap(ConstantExpr::getNUWMul(
743                                       unwrap<Constant>(LHSConstant),
744                                       unwrap<Constant>(RHSConstant)));
745 }
746
747 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
748   return wrap(ConstantExpr::getFMul(
749                                     unwrap<Constant>(LHSConstant),
750                                     unwrap<Constant>(RHSConstant)));
751 }
752
753 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
754   return wrap(ConstantExpr::getUDiv(
755                                     unwrap<Constant>(LHSConstant),
756                                     unwrap<Constant>(RHSConstant)));
757 }
758
759 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
760   return wrap(ConstantExpr::getSDiv(
761                                     unwrap<Constant>(LHSConstant),
762                                     unwrap<Constant>(RHSConstant)));
763 }
764
765 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
766                                 LLVMValueRef RHSConstant) {
767   return wrap(ConstantExpr::getExactSDiv(
768                                          unwrap<Constant>(LHSConstant),
769                                          unwrap<Constant>(RHSConstant)));
770 }
771
772 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
773   return wrap(ConstantExpr::getFDiv(
774                                     unwrap<Constant>(LHSConstant),
775                                     unwrap<Constant>(RHSConstant)));
776 }
777
778 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
779   return wrap(ConstantExpr::getURem(
780                                     unwrap<Constant>(LHSConstant),
781                                     unwrap<Constant>(RHSConstant)));
782 }
783
784 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
785   return wrap(ConstantExpr::getSRem(
786                                     unwrap<Constant>(LHSConstant),
787                                     unwrap<Constant>(RHSConstant)));
788 }
789
790 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
791   return wrap(ConstantExpr::getFRem(
792                                     unwrap<Constant>(LHSConstant),
793                                     unwrap<Constant>(RHSConstant)));
794 }
795
796 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
797   return wrap(ConstantExpr::getAnd(
798                                    unwrap<Constant>(LHSConstant),
799                                    unwrap<Constant>(RHSConstant)));
800 }
801
802 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
803   return wrap(ConstantExpr::getOr(
804                                   unwrap<Constant>(LHSConstant),
805                                   unwrap<Constant>(RHSConstant)));
806 }
807
808 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
809   return wrap(ConstantExpr::getXor(
810                                    unwrap<Constant>(LHSConstant),
811                                    unwrap<Constant>(RHSConstant)));
812 }
813
814 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
815                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
816   return wrap(ConstantExpr::getICmp(Predicate,
817                                     unwrap<Constant>(LHSConstant),
818                                     unwrap<Constant>(RHSConstant)));
819 }
820
821 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
822                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
823   return wrap(ConstantExpr::getFCmp(Predicate,
824                                     unwrap<Constant>(LHSConstant),
825                                     unwrap<Constant>(RHSConstant)));
826 }
827
828 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
829   return wrap(ConstantExpr::getShl(
830                                   unwrap<Constant>(LHSConstant),
831                                   unwrap<Constant>(RHSConstant)));
832 }
833
834 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
835   return wrap(ConstantExpr::getLShr(
836                                     unwrap<Constant>(LHSConstant),
837                                     unwrap<Constant>(RHSConstant)));
838 }
839
840 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
841   return wrap(ConstantExpr::getAShr(
842                                     unwrap<Constant>(LHSConstant),
843                                     unwrap<Constant>(RHSConstant)));
844 }
845
846 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
847                           LLVMValueRef *ConstantIndices, unsigned NumIndices) {
848   return wrap(ConstantExpr::getGetElementPtr(
849                                              unwrap<Constant>(ConstantVal),
850                                              unwrap<Constant>(ConstantIndices, 
851                                                               NumIndices),
852                                              NumIndices));
853 }
854
855 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
856                                   LLVMValueRef *ConstantIndices,
857                                   unsigned NumIndices) {
858   Constant* Val = unwrap<Constant>(ConstantVal);
859   Constant** Idxs = unwrap<Constant>(ConstantIndices, NumIndices);
860   return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, Idxs, NumIndices));
861 }
862
863 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
864   return wrap(ConstantExpr::getTrunc(
865                                      unwrap<Constant>(ConstantVal),
866                                      unwrap(ToType)));
867 }
868
869 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
870   return wrap(ConstantExpr::getSExt(
871                                     unwrap<Constant>(ConstantVal),
872                                     unwrap(ToType)));
873 }
874
875 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
876   return wrap(ConstantExpr::getZExt(
877                                     unwrap<Constant>(ConstantVal),
878                                     unwrap(ToType)));
879 }
880
881 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
882   return wrap(ConstantExpr::getFPTrunc(
883                                        unwrap<Constant>(ConstantVal),
884                                        unwrap(ToType)));
885 }
886
887 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
888   return wrap(ConstantExpr::getFPExtend(
889                                         unwrap<Constant>(ConstantVal),
890                                         unwrap(ToType)));
891 }
892
893 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
894   return wrap(ConstantExpr::getUIToFP(
895                                       unwrap<Constant>(ConstantVal),
896                                       unwrap(ToType)));
897 }
898
899 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
900   return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
901                                       unwrap(ToType)));
902 }
903
904 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
905   return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
906                                       unwrap(ToType)));
907 }
908
909 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
910   return wrap(ConstantExpr::getFPToSI(
911                                       unwrap<Constant>(ConstantVal),
912                                       unwrap(ToType)));
913 }
914
915 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
916   return wrap(ConstantExpr::getPtrToInt(
917                                         unwrap<Constant>(ConstantVal),
918                                         unwrap(ToType)));
919 }
920
921 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
922   return wrap(ConstantExpr::getIntToPtr(
923                                         unwrap<Constant>(ConstantVal),
924                                         unwrap(ToType)));
925 }
926
927 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
928   return wrap(ConstantExpr::getBitCast(
929                                        unwrap<Constant>(ConstantVal),
930                                        unwrap(ToType)));
931 }
932
933 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
934                                     LLVMTypeRef ToType) {
935   return wrap(ConstantExpr::getZExtOrBitCast(
936                                              unwrap<Constant>(ConstantVal),
937                                              unwrap(ToType)));
938 }
939
940 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
941                                     LLVMTypeRef ToType) {
942   return wrap(ConstantExpr::getSExtOrBitCast(
943                                              unwrap<Constant>(ConstantVal),
944                                              unwrap(ToType)));
945 }
946
947 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
948                                      LLVMTypeRef ToType) {
949   return wrap(ConstantExpr::getTruncOrBitCast(
950                                               unwrap<Constant>(ConstantVal),
951                                               unwrap(ToType)));
952 }
953
954 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
955                                   LLVMTypeRef ToType) {
956   return wrap(ConstantExpr::getPointerCast(
957                                            unwrap<Constant>(ConstantVal),
958                                            unwrap(ToType)));
959 }
960
961 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
962                               LLVMBool isSigned) {
963   return wrap(ConstantExpr::getIntegerCast(
964                                            unwrap<Constant>(ConstantVal),
965                                            unwrap(ToType),
966                                            isSigned));
967 }
968
969 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
970   return wrap(ConstantExpr::getFPCast(
971                                       unwrap<Constant>(ConstantVal),
972                                       unwrap(ToType)));
973 }
974
975 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
976                              LLVMValueRef ConstantIfTrue,
977                              LLVMValueRef ConstantIfFalse) {
978   return wrap(ConstantExpr::getSelect(
979                                       unwrap<Constant>(ConstantCondition),
980                                       unwrap<Constant>(ConstantIfTrue),
981                                       unwrap<Constant>(ConstantIfFalse)));
982 }
983
984 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
985                                      LLVMValueRef IndexConstant) {
986   return wrap(ConstantExpr::getExtractElement(
987                                               unwrap<Constant>(VectorConstant),
988                                               unwrap<Constant>(IndexConstant)));
989 }
990
991 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
992                                     LLVMValueRef ElementValueConstant,
993                                     LLVMValueRef IndexConstant) {
994   return wrap(ConstantExpr::getInsertElement(
995                                          unwrap<Constant>(VectorConstant),
996                                          unwrap<Constant>(ElementValueConstant),
997                                              unwrap<Constant>(IndexConstant)));
998 }
999
1000 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1001                                     LLVMValueRef VectorBConstant,
1002                                     LLVMValueRef MaskConstant) {
1003   return wrap(ConstantExpr::getShuffleVector(
1004                                              unwrap<Constant>(VectorAConstant),
1005                                              unwrap<Constant>(VectorBConstant),
1006                                              unwrap<Constant>(MaskConstant)));
1007 }
1008
1009 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1010                                    unsigned NumIdx) {
1011   return wrap(ConstantExpr::getExtractValue(
1012                                             unwrap<Constant>(AggConstant),
1013                                             IdxList, NumIdx));
1014 }
1015
1016 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1017                                   LLVMValueRef ElementValueConstant,
1018                                   unsigned *IdxList, unsigned NumIdx) {
1019   return wrap(ConstantExpr::getInsertValue(
1020                                          unwrap<Constant>(AggConstant),
1021                                          unwrap<Constant>(ElementValueConstant),
1022                                            IdxList, NumIdx));
1023 }
1024
1025 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1026                                 const char *Constraints,
1027                                 LLVMBool HasSideEffects,
1028                                 LLVMBool IsAlignStack) {
1029   return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1030                              Constraints, HasSideEffects, IsAlignStack));
1031 }
1032
1033 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1034   return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1035 }
1036
1037 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1038
1039 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1040   return wrap(unwrap<GlobalValue>(Global)->getParent());
1041 }
1042
1043 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1044   return unwrap<GlobalValue>(Global)->isDeclaration();
1045 }
1046
1047 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1048   switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1049   default:
1050     assert(false && "Unhandled Linkage Type.");
1051   case GlobalValue::ExternalLinkage:
1052     return LLVMExternalLinkage;
1053   case GlobalValue::AvailableExternallyLinkage:
1054     return LLVMAvailableExternallyLinkage;
1055   case GlobalValue::LinkOnceAnyLinkage:
1056     return LLVMLinkOnceAnyLinkage;
1057   case GlobalValue::LinkOnceODRLinkage:
1058     return LLVMLinkOnceODRLinkage;
1059   case GlobalValue::WeakAnyLinkage:
1060     return LLVMWeakAnyLinkage;
1061   case GlobalValue::WeakODRLinkage:
1062     return LLVMWeakODRLinkage;
1063   case GlobalValue::AppendingLinkage:
1064     return LLVMAppendingLinkage;
1065   case GlobalValue::InternalLinkage:
1066     return LLVMInternalLinkage;
1067   case GlobalValue::PrivateLinkage:
1068     return LLVMPrivateLinkage;
1069   case GlobalValue::LinkerPrivateLinkage:
1070     return LLVMLinkerPrivateLinkage;
1071   case GlobalValue::LinkerPrivateWeakLinkage:
1072     return LLVMLinkerPrivateWeakLinkage;
1073   case GlobalValue::LinkerPrivateWeakDefAutoLinkage:
1074     return LLVMLinkerPrivateWeakDefAutoLinkage;
1075   case GlobalValue::DLLImportLinkage:
1076     return LLVMDLLImportLinkage;
1077   case GlobalValue::DLLExportLinkage:
1078     return LLVMDLLExportLinkage;
1079   case GlobalValue::ExternalWeakLinkage:
1080     return LLVMExternalWeakLinkage;
1081   case GlobalValue::CommonLinkage:
1082     return LLVMCommonLinkage;
1083   }
1084
1085   // Should never get here.
1086   return static_cast<LLVMLinkage>(0);
1087 }
1088
1089 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1090   GlobalValue *GV = unwrap<GlobalValue>(Global);
1091
1092   switch (Linkage) {
1093   default:
1094     assert(false && "Unhandled Linkage Type.");
1095   case LLVMExternalLinkage:
1096     GV->setLinkage(GlobalValue::ExternalLinkage);
1097     break;
1098   case LLVMAvailableExternallyLinkage:
1099     GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1100     break;
1101   case LLVMLinkOnceAnyLinkage:
1102     GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1103     break;
1104   case LLVMLinkOnceODRLinkage:
1105     GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1106     break;
1107   case LLVMWeakAnyLinkage:
1108     GV->setLinkage(GlobalValue::WeakAnyLinkage);
1109     break;
1110   case LLVMWeakODRLinkage:
1111     GV->setLinkage(GlobalValue::WeakODRLinkage);
1112     break;
1113   case LLVMAppendingLinkage:
1114     GV->setLinkage(GlobalValue::AppendingLinkage);
1115     break;
1116   case LLVMInternalLinkage:
1117     GV->setLinkage(GlobalValue::InternalLinkage);
1118     break;
1119   case LLVMPrivateLinkage:
1120     GV->setLinkage(GlobalValue::PrivateLinkage);
1121     break;
1122   case LLVMLinkerPrivateLinkage:
1123     GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
1124     break;
1125   case LLVMLinkerPrivateWeakLinkage:
1126     GV->setLinkage(GlobalValue::LinkerPrivateWeakLinkage);
1127     break;
1128   case LLVMLinkerPrivateWeakDefAutoLinkage:
1129     GV->setLinkage(GlobalValue::LinkerPrivateWeakDefAutoLinkage);
1130     break;
1131   case LLVMDLLImportLinkage:
1132     GV->setLinkage(GlobalValue::DLLImportLinkage);
1133     break;
1134   case LLVMDLLExportLinkage:
1135     GV->setLinkage(GlobalValue::DLLExportLinkage);
1136     break;
1137   case LLVMExternalWeakLinkage:
1138     GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1139     break;
1140   case LLVMGhostLinkage:
1141     DEBUG(errs()
1142           << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1143     break;
1144   case LLVMCommonLinkage:
1145     GV->setLinkage(GlobalValue::CommonLinkage);
1146     break;
1147   }
1148 }
1149
1150 const char *LLVMGetSection(LLVMValueRef Global) {
1151   return unwrap<GlobalValue>(Global)->getSection().c_str();
1152 }
1153
1154 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1155   unwrap<GlobalValue>(Global)->setSection(Section);
1156 }
1157
1158 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1159   return static_cast<LLVMVisibility>(
1160     unwrap<GlobalValue>(Global)->getVisibility());
1161 }
1162
1163 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1164   unwrap<GlobalValue>(Global)
1165     ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1166 }
1167
1168 unsigned LLVMGetAlignment(LLVMValueRef Global) {
1169   return unwrap<GlobalValue>(Global)->getAlignment();
1170 }
1171
1172 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
1173   unwrap<GlobalValue>(Global)->setAlignment(Bytes);
1174 }
1175
1176 /*--.. Operations on global variables ......................................--*/
1177
1178 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1179   return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1180                                  GlobalValue::ExternalLinkage, 0, Name));
1181 }
1182
1183 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1184                                          const char *Name,
1185                                          unsigned AddressSpace) {
1186   return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1187                                  GlobalValue::ExternalLinkage, 0, Name, 0,
1188                                  false, AddressSpace));
1189 }
1190
1191 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1192   return wrap(unwrap(M)->getNamedGlobal(Name));
1193 }
1194
1195 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1196   Module *Mod = unwrap(M);
1197   Module::global_iterator I = Mod->global_begin();
1198   if (I == Mod->global_end())
1199     return 0;
1200   return wrap(I);
1201 }
1202
1203 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1204   Module *Mod = unwrap(M);
1205   Module::global_iterator I = Mod->global_end();
1206   if (I == Mod->global_begin())
1207     return 0;
1208   return wrap(--I);
1209 }
1210
1211 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1212   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1213   Module::global_iterator I = GV;
1214   if (++I == GV->getParent()->global_end())
1215     return 0;
1216   return wrap(I);
1217 }
1218
1219 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1220   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1221   Module::global_iterator I = GV;
1222   if (I == GV->getParent()->global_begin())
1223     return 0;
1224   return wrap(--I);
1225 }
1226
1227 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1228   unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1229 }
1230
1231 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1232   GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1233   if ( !GV->hasInitializer() )
1234     return 0;
1235   return wrap(GV->getInitializer());
1236 }
1237
1238 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1239   unwrap<GlobalVariable>(GlobalVar)
1240     ->setInitializer(unwrap<Constant>(ConstantVal));
1241 }
1242
1243 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1244   return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1245 }
1246
1247 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1248   unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1249 }
1250
1251 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1252   return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1253 }
1254
1255 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1256   unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1257 }
1258
1259 /*--.. Operations on aliases ......................................--*/
1260
1261 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1262                           const char *Name) {
1263   return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1264                               unwrap<Constant>(Aliasee), unwrap (M)));
1265 }
1266
1267 /*--.. Operations on functions .............................................--*/
1268
1269 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1270                              LLVMTypeRef FunctionTy) {
1271   return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1272                                GlobalValue::ExternalLinkage, Name, unwrap(M)));
1273 }
1274
1275 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1276   return wrap(unwrap(M)->getFunction(Name));
1277 }
1278
1279 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1280   Module *Mod = unwrap(M);
1281   Module::iterator I = Mod->begin();
1282   if (I == Mod->end())
1283     return 0;
1284   return wrap(I);
1285 }
1286
1287 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1288   Module *Mod = unwrap(M);
1289   Module::iterator I = Mod->end();
1290   if (I == Mod->begin())
1291     return 0;
1292   return wrap(--I);
1293 }
1294
1295 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1296   Function *Func = unwrap<Function>(Fn);
1297   Module::iterator I = Func;
1298   if (++I == Func->getParent()->end())
1299     return 0;
1300   return wrap(I);
1301 }
1302
1303 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1304   Function *Func = unwrap<Function>(Fn);
1305   Module::iterator I = Func;
1306   if (I == Func->getParent()->begin())
1307     return 0;
1308   return wrap(--I);
1309 }
1310
1311 void LLVMDeleteFunction(LLVMValueRef Fn) {
1312   unwrap<Function>(Fn)->eraseFromParent();
1313 }
1314
1315 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1316   if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1317     return F->getIntrinsicID();
1318   return 0;
1319 }
1320
1321 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1322   return unwrap<Function>(Fn)->getCallingConv();
1323 }
1324
1325 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1326   return unwrap<Function>(Fn)->setCallingConv(
1327     static_cast<CallingConv::ID>(CC));
1328 }
1329
1330 const char *LLVMGetGC(LLVMValueRef Fn) {
1331   Function *F = unwrap<Function>(Fn);
1332   return F->hasGC()? F->getGC() : 0;
1333 }
1334
1335 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1336   Function *F = unwrap<Function>(Fn);
1337   if (GC)
1338     F->setGC(GC);
1339   else
1340     F->clearGC();
1341 }
1342
1343 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1344   Function *Func = unwrap<Function>(Fn);
1345   const AttrListPtr PAL = Func->getAttributes();
1346   const AttrListPtr PALnew = PAL.addAttr(~0U, PA);
1347   Func->setAttributes(PALnew);
1348 }
1349
1350 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1351   Function *Func = unwrap<Function>(Fn);
1352   const AttrListPtr PAL = Func->getAttributes();
1353   const AttrListPtr PALnew = PAL.removeAttr(~0U, PA);
1354   Func->setAttributes(PALnew);
1355 }
1356
1357 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1358   Function *Func = unwrap<Function>(Fn);
1359   const AttrListPtr PAL = Func->getAttributes();
1360   Attributes attr = PAL.getFnAttributes();
1361   return (LLVMAttribute)attr;
1362 }
1363
1364 /*--.. Operations on parameters ............................................--*/
1365
1366 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1367   // This function is strictly redundant to
1368   //   LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1369   return unwrap<Function>(FnRef)->arg_size();
1370 }
1371
1372 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1373   Function *Fn = unwrap<Function>(FnRef);
1374   for (Function::arg_iterator I = Fn->arg_begin(),
1375                               E = Fn->arg_end(); I != E; I++)
1376     *ParamRefs++ = wrap(I);
1377 }
1378
1379 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1380   Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1381   while (index --> 0)
1382     AI++;
1383   return wrap(AI);
1384 }
1385
1386 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1387   return wrap(unwrap<Argument>(V)->getParent());
1388 }
1389
1390 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1391   Function *Func = unwrap<Function>(Fn);
1392   Function::arg_iterator I = Func->arg_begin();
1393   if (I == Func->arg_end())
1394     return 0;
1395   return wrap(I);
1396 }
1397
1398 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1399   Function *Func = unwrap<Function>(Fn);
1400   Function::arg_iterator I = Func->arg_end();
1401   if (I == Func->arg_begin())
1402     return 0;
1403   return wrap(--I);
1404 }
1405
1406 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1407   Argument *A = unwrap<Argument>(Arg);
1408   Function::arg_iterator I = A;
1409   if (++I == A->getParent()->arg_end())
1410     return 0;
1411   return wrap(I);
1412 }
1413
1414 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1415   Argument *A = unwrap<Argument>(Arg);
1416   Function::arg_iterator I = A;
1417   if (I == A->getParent()->arg_begin())
1418     return 0;
1419   return wrap(--I);
1420 }
1421
1422 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1423   unwrap<Argument>(Arg)->addAttr(PA);
1424 }
1425
1426 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1427   unwrap<Argument>(Arg)->removeAttr(PA);
1428 }
1429
1430 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1431   Argument *A = unwrap<Argument>(Arg);
1432   Attributes attr = A->getParent()->getAttributes().getParamAttributes(
1433     A->getArgNo()+1);
1434   return (LLVMAttribute)attr;
1435 }
1436   
1437
1438 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1439   unwrap<Argument>(Arg)->addAttr(
1440           Attribute::constructAlignmentFromInt(align));
1441 }
1442
1443 /*--.. Operations on basic blocks ..........................................--*/
1444
1445 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1446   return wrap(static_cast<Value*>(unwrap(BB)));
1447 }
1448
1449 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1450   return isa<BasicBlock>(unwrap(Val));
1451 }
1452
1453 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1454   return wrap(unwrap<BasicBlock>(Val));
1455 }
1456
1457 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1458   return wrap(unwrap(BB)->getParent());
1459 }
1460
1461 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1462   return unwrap<Function>(FnRef)->size();
1463 }
1464
1465 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1466   Function *Fn = unwrap<Function>(FnRef);
1467   for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1468     *BasicBlocksRefs++ = wrap(I);
1469 }
1470
1471 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1472   return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1473 }
1474
1475 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1476   Function *Func = unwrap<Function>(Fn);
1477   Function::iterator I = Func->begin();
1478   if (I == Func->end())
1479     return 0;
1480   return wrap(I);
1481 }
1482
1483 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1484   Function *Func = unwrap<Function>(Fn);
1485   Function::iterator I = Func->end();
1486   if (I == Func->begin())
1487     return 0;
1488   return wrap(--I);
1489 }
1490
1491 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1492   BasicBlock *Block = unwrap(BB);
1493   Function::iterator I = Block;
1494   if (++I == Block->getParent()->end())
1495     return 0;
1496   return wrap(I);
1497 }
1498
1499 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1500   BasicBlock *Block = unwrap(BB);
1501   Function::iterator I = Block;
1502   if (I == Block->getParent()->begin())
1503     return 0;
1504   return wrap(--I);
1505 }
1506
1507 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1508                                                 LLVMValueRef FnRef,
1509                                                 const char *Name) {
1510   return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1511 }
1512
1513 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1514   return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1515 }
1516
1517 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1518                                                 LLVMBasicBlockRef BBRef,
1519                                                 const char *Name) {
1520   BasicBlock *BB = unwrap(BBRef);
1521   return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1522 }
1523
1524 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1525                                        const char *Name) {
1526   return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1527 }
1528
1529 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1530   unwrap(BBRef)->eraseFromParent();
1531 }
1532
1533 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1534   unwrap(BB)->moveBefore(unwrap(MovePos));
1535 }
1536
1537 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1538   unwrap(BB)->moveAfter(unwrap(MovePos));
1539 }
1540
1541 /*--.. Operations on instructions ..........................................--*/
1542
1543 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1544   return wrap(unwrap<Instruction>(Inst)->getParent());
1545 }
1546
1547 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1548   BasicBlock *Block = unwrap(BB);
1549   BasicBlock::iterator I = Block->begin();
1550   if (I == Block->end())
1551     return 0;
1552   return wrap(I);
1553 }
1554
1555 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1556   BasicBlock *Block = unwrap(BB);
1557   BasicBlock::iterator I = Block->end();
1558   if (I == Block->begin())
1559     return 0;
1560   return wrap(--I);
1561 }
1562
1563 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1564   Instruction *Instr = unwrap<Instruction>(Inst);
1565   BasicBlock::iterator I = Instr;
1566   if (++I == Instr->getParent()->end())
1567     return 0;
1568   return wrap(I);
1569 }
1570
1571 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1572   Instruction *Instr = unwrap<Instruction>(Inst);
1573   BasicBlock::iterator I = Instr;
1574   if (I == Instr->getParent()->begin())
1575     return 0;
1576   return wrap(--I);
1577 }
1578
1579 /*--.. Call and invoke instructions ........................................--*/
1580
1581 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1582   Value *V = unwrap(Instr);
1583   if (CallInst *CI = dyn_cast<CallInst>(V))
1584     return CI->getCallingConv();
1585   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1586     return II->getCallingConv();
1587   llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1588   return 0;
1589 }
1590
1591 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1592   Value *V = unwrap(Instr);
1593   if (CallInst *CI = dyn_cast<CallInst>(V))
1594     return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1595   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1596     return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1597   llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1598 }
1599
1600 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index, 
1601                            LLVMAttribute PA) {
1602   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1603   Call.setAttributes(
1604     Call.getAttributes().addAttr(index, PA));
1605 }
1606
1607 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index, 
1608                               LLVMAttribute PA) {
1609   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1610   Call.setAttributes(
1611     Call.getAttributes().removeAttr(index, PA));
1612 }
1613
1614 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index, 
1615                                 unsigned align) {
1616   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1617   Call.setAttributes(
1618     Call.getAttributes().addAttr(index, 
1619         Attribute::constructAlignmentFromInt(align)));
1620 }
1621
1622 /*--.. Operations on call instructions (only) ..............................--*/
1623
1624 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1625   return unwrap<CallInst>(Call)->isTailCall();
1626 }
1627
1628 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1629   unwrap<CallInst>(Call)->setTailCall(isTailCall);
1630 }
1631
1632 /*--.. Operations on phi nodes .............................................--*/
1633
1634 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1635                      LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1636   PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1637   for (unsigned I = 0; I != Count; ++I)
1638     PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1639 }
1640
1641 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1642   return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1643 }
1644
1645 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1646   return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1647 }
1648
1649 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1650   return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1651 }
1652
1653
1654 /*===-- Instruction builders ----------------------------------------------===*/
1655
1656 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1657   return wrap(new IRBuilder<>(*unwrap(C)));
1658 }
1659
1660 LLVMBuilderRef LLVMCreateBuilder(void) {
1661   return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1662 }
1663
1664 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1665                          LLVMValueRef Instr) {
1666   BasicBlock *BB = unwrap(Block);
1667   Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1668   unwrap(Builder)->SetInsertPoint(BB, I);
1669 }
1670
1671 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1672   Instruction *I = unwrap<Instruction>(Instr);
1673   unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1674 }
1675
1676 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1677   BasicBlock *BB = unwrap(Block);
1678   unwrap(Builder)->SetInsertPoint(BB);
1679 }
1680
1681 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1682    return wrap(unwrap(Builder)->GetInsertBlock());
1683 }
1684
1685 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1686   unwrap(Builder)->ClearInsertionPoint();
1687 }
1688
1689 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1690   unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1691 }
1692
1693 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1694                                    const char *Name) {
1695   unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1696 }
1697
1698 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1699   delete unwrap(Builder);
1700 }
1701
1702 /*--.. Metadata builders ...................................................--*/
1703
1704 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1705   MDNode *Loc = L ? unwrap<MDNode>(L) : NULL;
1706   unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1707 }
1708
1709 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1710   return wrap(unwrap(Builder)->getCurrentDebugLocation()
1711               .getAsMDNode(unwrap(Builder)->getContext()));
1712 }
1713
1714 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1715   unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
1716 }
1717
1718
1719 /*--.. Instruction builders ................................................--*/
1720
1721 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1722   return wrap(unwrap(B)->CreateRetVoid());
1723 }
1724
1725 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1726   return wrap(unwrap(B)->CreateRet(unwrap(V)));
1727 }
1728
1729 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1730                                    unsigned N) {
1731   return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1732 }
1733
1734 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1735   return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1736 }
1737
1738 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1739                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1740   return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1741 }
1742
1743 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1744                              LLVMBasicBlockRef Else, unsigned NumCases) {
1745   return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1746 }
1747
1748 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
1749                                  unsigned NumDests) {
1750   return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
1751 }
1752
1753 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1754                              LLVMValueRef *Args, unsigned NumArgs,
1755                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1756                              const char *Name) {
1757   return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1758                                       unwrap(Args), unwrap(Args) + NumArgs,
1759                                       Name));
1760 }
1761
1762 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1763   return wrap(unwrap(B)->CreateUnwind());
1764 }
1765
1766 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1767   return wrap(unwrap(B)->CreateUnreachable());
1768 }
1769
1770 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1771                  LLVMBasicBlockRef Dest) {
1772   unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1773 }
1774
1775 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
1776   unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
1777 }
1778
1779 /*--.. Arithmetic ..........................................................--*/
1780
1781 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1782                           const char *Name) {
1783   return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1784 }
1785
1786 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1787                           const char *Name) {
1788   return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1789 }
1790
1791 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1792                           const char *Name) {
1793   return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
1794 }
1795
1796 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1797                           const char *Name) {
1798   return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1799 }
1800
1801 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1802                           const char *Name) {
1803   return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1804 }
1805
1806 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1807                           const char *Name) {
1808   return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
1809 }
1810
1811 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1812                           const char *Name) {
1813   return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
1814 }
1815
1816 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1817                           const char *Name) {
1818   return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
1819 }
1820
1821 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1822                           const char *Name) {
1823   return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1824 }
1825
1826 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1827                           const char *Name) {
1828   return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
1829 }
1830
1831 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1832                           const char *Name) {
1833   return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
1834 }
1835
1836 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1837                           const char *Name) {
1838   return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
1839 }
1840
1841 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1842                            const char *Name) {
1843   return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1844 }
1845
1846 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1847                            const char *Name) {
1848   return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1849 }
1850
1851 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
1852                                 LLVMValueRef RHS, const char *Name) {
1853   return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
1854 }
1855
1856 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1857                            const char *Name) {
1858   return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1859 }
1860
1861 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1862                            const char *Name) {
1863   return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1864 }
1865
1866 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1867                            const char *Name) {
1868   return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1869 }
1870
1871 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1872                            const char *Name) {
1873   return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1874 }
1875
1876 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1877                           const char *Name) {
1878   return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1879 }
1880
1881 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1882                            const char *Name) {
1883   return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1884 }
1885
1886 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1887                            const char *Name) {
1888   return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1889 }
1890
1891 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1892                           const char *Name) {
1893   return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1894 }
1895
1896 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1897                          const char *Name) {
1898   return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1899 }
1900
1901 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1902                           const char *Name) {
1903   return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1904 }
1905
1906 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
1907                             LLVMValueRef LHS, LLVMValueRef RHS,
1908                             const char *Name) {
1909   return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(Op), unwrap(LHS),
1910                                      unwrap(RHS), Name));
1911 }
1912
1913 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1914   return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1915 }
1916
1917 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
1918                              const char *Name) {
1919   return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
1920 }
1921
1922 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
1923                              const char *Name) {
1924   return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
1925 }
1926
1927 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1928   return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
1929 }
1930
1931 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1932   return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1933 }
1934
1935 /*--.. Memory ..............................................................--*/
1936
1937 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1938                              const char *Name) {
1939   const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1940   Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1941   AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1942   Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), 
1943                                                ITy, unwrap(Ty), AllocSize, 
1944                                                0, 0, "");
1945   return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1946 }
1947
1948 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1949                                   LLVMValueRef Val, const char *Name) {
1950   const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1951   Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1952   AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1953   Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), 
1954                                                ITy, unwrap(Ty), AllocSize, 
1955                                                unwrap(Val), 0, "");
1956   return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1957 }
1958
1959 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1960                              const char *Name) {
1961   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1962 }
1963
1964 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1965                                   LLVMValueRef Val, const char *Name) {
1966   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1967 }
1968
1969 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1970   return wrap(unwrap(B)->Insert(
1971      CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
1972 }
1973
1974
1975 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1976                            const char *Name) {
1977   return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1978 }
1979
1980 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val, 
1981                             LLVMValueRef PointerVal) {
1982   return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1983 }
1984
1985 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1986                           LLVMValueRef *Indices, unsigned NumIndices,
1987                           const char *Name) {
1988   return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1989                                    unwrap(Indices) + NumIndices, Name));
1990 }
1991
1992 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1993                                   LLVMValueRef *Indices, unsigned NumIndices,
1994                                   const char *Name) {
1995   return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), unwrap(Indices),
1996                                            unwrap(Indices) + NumIndices, Name));
1997 }
1998
1999 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2000                                 unsigned Idx, const char *Name) {
2001   return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
2002 }
2003
2004 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2005                                    const char *Name) {
2006   return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2007 }
2008
2009 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2010                                       const char *Name) {
2011   return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2012 }
2013
2014 /*--.. Casts ...............................................................--*/
2015
2016 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2017                             LLVMTypeRef DestTy, const char *Name) {
2018   return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2019 }
2020
2021 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2022                            LLVMTypeRef DestTy, const char *Name) {
2023   return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2024 }
2025
2026 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2027                            LLVMTypeRef DestTy, const char *Name) {
2028   return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2029 }
2030
2031 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2032                              LLVMTypeRef DestTy, const char *Name) {
2033   return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2034 }
2035
2036 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2037                              LLVMTypeRef DestTy, const char *Name) {
2038   return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2039 }
2040
2041 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2042                              LLVMTypeRef DestTy, const char *Name) {
2043   return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2044 }
2045
2046 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2047                              LLVMTypeRef DestTy, const char *Name) {
2048   return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2049 }
2050
2051 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2052                               LLVMTypeRef DestTy, const char *Name) {
2053   return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2054 }
2055
2056 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2057                             LLVMTypeRef DestTy, const char *Name) {
2058   return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2059 }
2060
2061 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2062                                LLVMTypeRef DestTy, const char *Name) {
2063   return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2064 }
2065
2066 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2067                                LLVMTypeRef DestTy, const char *Name) {
2068   return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2069 }
2070
2071 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2072                               LLVMTypeRef DestTy, const char *Name) {
2073   return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2074 }
2075
2076 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2077                                     LLVMTypeRef DestTy, const char *Name) {
2078   return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2079                                              Name));
2080 }
2081
2082 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2083                                     LLVMTypeRef DestTy, const char *Name) {
2084   return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2085                                              Name));
2086 }
2087
2088 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2089                                      LLVMTypeRef DestTy, const char *Name) {
2090   return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2091                                               Name));
2092 }
2093
2094 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2095                            LLVMTypeRef DestTy, const char *Name) {
2096   return wrap(unwrap(B)->CreateCast(Instruction::CastOps(Op), unwrap(Val),
2097                                     unwrap(DestTy), Name));
2098 }
2099
2100 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2101                                   LLVMTypeRef DestTy, const char *Name) {
2102   return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2103 }
2104
2105 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2106                               LLVMTypeRef DestTy, const char *Name) {
2107   return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2108                                        /*isSigned*/true, Name));
2109 }
2110
2111 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2112                              LLVMTypeRef DestTy, const char *Name) {
2113   return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2114 }
2115
2116 /*--.. Comparisons .........................................................--*/
2117
2118 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2119                            LLVMValueRef LHS, LLVMValueRef RHS,
2120                            const char *Name) {
2121   return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2122                                     unwrap(LHS), unwrap(RHS), Name));
2123 }
2124
2125 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2126                            LLVMValueRef LHS, LLVMValueRef RHS,
2127                            const char *Name) {
2128   return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2129                                     unwrap(LHS), unwrap(RHS), Name));
2130 }
2131
2132 /*--.. Miscellaneous instructions ..........................................--*/
2133
2134 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2135   return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
2136 }
2137
2138 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2139                            LLVMValueRef *Args, unsigned NumArgs,
2140                            const char *Name) {
2141   return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
2142                                     unwrap(Args) + NumArgs, Name));
2143 }
2144
2145 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2146                              LLVMValueRef Then, LLVMValueRef Else,
2147                              const char *Name) {
2148   return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2149                                       Name));
2150 }
2151
2152 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2153                             LLVMTypeRef Ty, const char *Name) {
2154   return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2155 }
2156
2157 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2158                                       LLVMValueRef Index, const char *Name) {
2159   return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2160                                               Name));
2161 }
2162
2163 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2164                                     LLVMValueRef EltVal, LLVMValueRef Index,
2165                                     const char *Name) {
2166   return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2167                                              unwrap(Index), Name));
2168 }
2169
2170 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2171                                     LLVMValueRef V2, LLVMValueRef Mask,
2172                                     const char *Name) {
2173   return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2174                                              unwrap(Mask), Name));
2175 }
2176
2177 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2178                                    unsigned Index, const char *Name) {
2179   return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2180 }
2181
2182 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2183                                   LLVMValueRef EltVal, unsigned Index,
2184                                   const char *Name) {
2185   return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2186                                            Index, Name));
2187 }
2188
2189 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2190                              const char *Name) {
2191   return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2192 }
2193
2194 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2195                                 const char *Name) {
2196   return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2197 }
2198
2199 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2200                               LLVMValueRef RHS, const char *Name) {
2201   return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2202 }
2203
2204
2205 /*===-- Module providers --------------------------------------------------===*/
2206
2207 LLVMModuleProviderRef
2208 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2209   return reinterpret_cast<LLVMModuleProviderRef>(M);
2210 }
2211
2212 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2213   delete unwrap(MP);
2214 }
2215
2216
2217 /*===-- Memory buffers ----------------------------------------------------===*/
2218
2219 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2220     const char *Path,
2221     LLVMMemoryBufferRef *OutMemBuf,
2222     char **OutMessage) {
2223
2224   error_code ec;
2225   if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, ec)) {
2226     *OutMemBuf = wrap(MB);
2227     return 0;
2228   }
2229
2230   *OutMessage = strdup(ec.message().c_str());
2231   return 1;
2232 }
2233
2234 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2235                                          char **OutMessage) {
2236   error_code ec;
2237   if (MemoryBuffer *MB = MemoryBuffer::getSTDIN(ec)) {
2238     *OutMemBuf = wrap(MB);
2239     return 0;
2240   }
2241
2242   *OutMessage = strdup(ec.message().c_str());
2243   return 1;
2244 }
2245
2246 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2247   delete unwrap(MemBuf);
2248 }
2249
2250 /*===-- Pass Registry -----------------------------------------------------===*/
2251
2252 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2253   return wrap(PassRegistry::getPassRegistry());
2254 }
2255
2256 /*===-- Pass Manager ------------------------------------------------------===*/
2257
2258 LLVMPassManagerRef LLVMCreatePassManager() {
2259   return wrap(new PassManager());
2260 }
2261
2262 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2263   return wrap(new FunctionPassManager(unwrap(M)));
2264 }
2265
2266 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2267   return LLVMCreateFunctionPassManagerForModule(
2268                                             reinterpret_cast<LLVMModuleRef>(P));
2269 }
2270
2271 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2272   return unwrap<PassManager>(PM)->run(*unwrap(M));
2273 }
2274
2275 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2276   return unwrap<FunctionPassManager>(FPM)->doInitialization();
2277 }
2278
2279 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2280   return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2281 }
2282
2283 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2284   return unwrap<FunctionPassManager>(FPM)->doFinalization();
2285 }
2286
2287 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
2288   delete unwrap(PM);
2289 }