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