LLVM API Documentation

Core.cpp
Go to the documentation of this file.
00001 //===-- Core.cpp ----------------------------------------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the common infrastructure (including the C bindings)
00011 // for libLLVMCore.a, which implements the LLVM intermediate representation.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "llvm-c/Core.h"
00016 #include "llvm/Bitcode/ReaderWriter.h"
00017 #include "llvm/IR/Attributes.h"
00018 #include "llvm/IR/CallSite.h"
00019 #include "llvm/IR/Constants.h"
00020 #include "llvm/IR/DerivedTypes.h"
00021 #include "llvm/IR/DiagnosticInfo.h"
00022 #include "llvm/IR/DiagnosticPrinter.h"
00023 #include "llvm/IR/GlobalAlias.h"
00024 #include "llvm/IR/GlobalVariable.h"
00025 #include "llvm/IR/IRBuilder.h"
00026 #include "llvm/IR/InlineAsm.h"
00027 #include "llvm/IR/IntrinsicInst.h"
00028 #include "llvm/IR/LLVMContext.h"
00029 #include "llvm/IR/Module.h"
00030 #include "llvm/PassManager.h"
00031 #include "llvm/Support/Debug.h"
00032 #include "llvm/Support/ErrorHandling.h"
00033 #include "llvm/Support/FileSystem.h"
00034 #include "llvm/Support/ManagedStatic.h"
00035 #include "llvm/Support/MemoryBuffer.h"
00036 #include "llvm/Support/Threading.h"
00037 #include "llvm/Support/raw_ostream.h"
00038 #include <cassert>
00039 #include <cstdlib>
00040 #include <cstring>
00041 #include <system_error>
00042 
00043 using namespace llvm;
00044 
00045 #define DEBUG_TYPE "ir"
00046 
00047 void llvm::initializeCore(PassRegistry &Registry) {
00048   initializeDominatorTreeWrapperPassPass(Registry);
00049   initializePrintModulePassWrapperPass(Registry);
00050   initializePrintFunctionPassWrapperPass(Registry);
00051   initializePrintBasicBlockPassPass(Registry);
00052   initializeVerifierLegacyPassPass(Registry);
00053 }
00054 
00055 void LLVMInitializeCore(LLVMPassRegistryRef R) {
00056   initializeCore(*unwrap(R));
00057 }
00058 
00059 void LLVMShutdown() {
00060   llvm_shutdown();
00061 }
00062 
00063 /*===-- Error handling ----------------------------------------------------===*/
00064 
00065 char *LLVMCreateMessage(const char *Message) {
00066   return strdup(Message);
00067 }
00068 
00069 void LLVMDisposeMessage(char *Message) {
00070   free(Message);
00071 }
00072 
00073 
00074 /*===-- Operations on contexts --------------------------------------------===*/
00075 
00076 LLVMContextRef LLVMContextCreate() {
00077   return wrap(new LLVMContext());
00078 }
00079 
00080 LLVMContextRef LLVMGetGlobalContext() {
00081   return wrap(&getGlobalContext());
00082 }
00083 
00084 void LLVMContextSetDiagnosticHandler(LLVMContextRef C,
00085                                      LLVMDiagnosticHandler Handler,
00086                                      void *DiagnosticContext) {
00087   unwrap(C)->setDiagnosticHandler(
00088       LLVM_EXTENSION reinterpret_cast<LLVMContext::DiagnosticHandlerTy>(Handler),
00089       DiagnosticContext);
00090 }
00091 
00092 void LLVMContextSetYieldCallback(LLVMContextRef C, LLVMYieldCallback Callback,
00093                                  void *OpaqueHandle) {
00094   auto YieldCallback =
00095     LLVM_EXTENSION reinterpret_cast<LLVMContext::YieldCallbackTy>(Callback);
00096   unwrap(C)->setYieldCallback(YieldCallback, OpaqueHandle);
00097 }
00098 
00099 void LLVMContextDispose(LLVMContextRef C) {
00100   delete unwrap(C);
00101 }
00102 
00103 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char* Name,
00104                                   unsigned SLen) {
00105   return unwrap(C)->getMDKindID(StringRef(Name, SLen));
00106 }
00107 
00108 unsigned LLVMGetMDKindID(const char* Name, unsigned SLen) {
00109   return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
00110 }
00111 
00112 char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI) {
00113   std::string MsgStorage;
00114   raw_string_ostream Stream(MsgStorage);
00115   DiagnosticPrinterRawOStream DP(Stream);
00116 
00117   unwrap(DI)->print(DP);
00118   Stream.flush();
00119 
00120   return LLVMCreateMessage(MsgStorage.c_str());
00121 }
00122 
00123 LLVMDiagnosticSeverity LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI){
00124     LLVMDiagnosticSeverity severity;
00125 
00126     switch(unwrap(DI)->getSeverity()) {
00127     default:
00128       severity = LLVMDSError;
00129       break;
00130     case DS_Warning:
00131       severity = LLVMDSWarning;
00132       break;
00133     case DS_Remark:
00134       severity = LLVMDSRemark;
00135       break;
00136     case DS_Note:
00137       severity = LLVMDSNote;
00138       break;
00139     }
00140 
00141     return severity;
00142 }
00143 
00144 
00145 
00146 
00147 /*===-- Operations on modules ---------------------------------------------===*/
00148 
00149 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
00150   return wrap(new Module(ModuleID, getGlobalContext()));
00151 }
00152 
00153 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
00154                                                 LLVMContextRef C) {
00155   return wrap(new Module(ModuleID, *unwrap(C)));
00156 }
00157 
00158 void LLVMDisposeModule(LLVMModuleRef M) {
00159   delete unwrap(M);
00160 }
00161 
00162 /*--.. Data layout .........................................................--*/
00163 const char * LLVMGetDataLayout(LLVMModuleRef M) {
00164   return unwrap(M)->getDataLayoutStr().c_str();
00165 }
00166 
00167 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
00168   unwrap(M)->setDataLayout(Triple);
00169 }
00170 
00171 /*--.. Target triple .......................................................--*/
00172 const char * LLVMGetTarget(LLVMModuleRef M) {
00173   return unwrap(M)->getTargetTriple().c_str();
00174 }
00175 
00176 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
00177   unwrap(M)->setTargetTriple(Triple);
00178 }
00179 
00180 void LLVMDumpModule(LLVMModuleRef M) {
00181   unwrap(M)->dump();
00182 }
00183 
00184 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
00185                                char **ErrorMessage) {
00186   std::error_code EC;
00187   raw_fd_ostream dest(Filename, EC, sys::fs::F_Text);
00188   if (EC) {
00189     *ErrorMessage = strdup(EC.message().c_str());
00190     return true;
00191   }
00192 
00193   unwrap(M)->print(dest, nullptr);
00194 
00195   dest.close();
00196 
00197   if (dest.has_error()) {
00198     *ErrorMessage = strdup("Error printing to file");
00199     return true;
00200   }
00201 
00202   return false;
00203 }
00204 
00205 char *LLVMPrintModuleToString(LLVMModuleRef M) {
00206   std::string buf;
00207   raw_string_ostream os(buf);
00208 
00209   unwrap(M)->print(os, nullptr);
00210   os.flush();
00211 
00212   return strdup(buf.c_str());
00213 }
00214 
00215 /*--.. Operations on inline assembler ......................................--*/
00216 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
00217   unwrap(M)->setModuleInlineAsm(StringRef(Asm));
00218 }
00219 
00220 
00221 /*--.. Operations on module contexts ......................................--*/
00222 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
00223   return wrap(&unwrap(M)->getContext());
00224 }
00225 
00226 
00227 /*===-- Operations on types -----------------------------------------------===*/
00228 
00229 /*--.. Operations on all types (mostly) ....................................--*/
00230 
00231 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
00232   switch (unwrap(Ty)->getTypeID()) {
00233   case Type::VoidTyID:
00234     return LLVMVoidTypeKind;
00235   case Type::HalfTyID:
00236     return LLVMHalfTypeKind;
00237   case Type::FloatTyID:
00238     return LLVMFloatTypeKind;
00239   case Type::DoubleTyID:
00240     return LLVMDoubleTypeKind;
00241   case Type::X86_FP80TyID:
00242     return LLVMX86_FP80TypeKind;
00243   case Type::FP128TyID:
00244     return LLVMFP128TypeKind;
00245   case Type::PPC_FP128TyID:
00246     return LLVMPPC_FP128TypeKind;
00247   case Type::LabelTyID:
00248     return LLVMLabelTypeKind;
00249   case Type::MetadataTyID:
00250     return LLVMMetadataTypeKind;
00251   case Type::IntegerTyID:
00252     return LLVMIntegerTypeKind;
00253   case Type::FunctionTyID:
00254     return LLVMFunctionTypeKind;
00255   case Type::StructTyID:
00256     return LLVMStructTypeKind;
00257   case Type::ArrayTyID:
00258     return LLVMArrayTypeKind;
00259   case Type::PointerTyID:
00260     return LLVMPointerTypeKind;
00261   case Type::VectorTyID:
00262     return LLVMVectorTypeKind;
00263   case Type::X86_MMXTyID:
00264     return LLVMX86_MMXTypeKind;
00265   }
00266   llvm_unreachable("Unhandled TypeID.");
00267 }
00268 
00269 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
00270 {
00271     return unwrap(Ty)->isSized();
00272 }
00273 
00274 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
00275   return wrap(&unwrap(Ty)->getContext());
00276 }
00277 
00278 void LLVMDumpType(LLVMTypeRef Ty) {
00279   return unwrap(Ty)->dump();
00280 }
00281 
00282 char *LLVMPrintTypeToString(LLVMTypeRef Ty) {
00283   std::string buf;
00284   raw_string_ostream os(buf);
00285 
00286   if (unwrap(Ty))
00287     unwrap(Ty)->print(os);
00288   else
00289     os << "Printing <null> Type";
00290 
00291   os.flush();
00292 
00293   return strdup(buf.c_str());
00294 }
00295 
00296 /*--.. Operations on integer types .........................................--*/
00297 
00298 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C)  {
00299   return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
00300 }
00301 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C)  {
00302   return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
00303 }
00304 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
00305   return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
00306 }
00307 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
00308   return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
00309 }
00310 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
00311   return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
00312 }
00313 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
00314   return wrap(IntegerType::get(*unwrap(C), NumBits));
00315 }
00316 
00317 LLVMTypeRef LLVMInt1Type(void)  {
00318   return LLVMInt1TypeInContext(LLVMGetGlobalContext());
00319 }
00320 LLVMTypeRef LLVMInt8Type(void)  {
00321   return LLVMInt8TypeInContext(LLVMGetGlobalContext());
00322 }
00323 LLVMTypeRef LLVMInt16Type(void) {
00324   return LLVMInt16TypeInContext(LLVMGetGlobalContext());
00325 }
00326 LLVMTypeRef LLVMInt32Type(void) {
00327   return LLVMInt32TypeInContext(LLVMGetGlobalContext());
00328 }
00329 LLVMTypeRef LLVMInt64Type(void) {
00330   return LLVMInt64TypeInContext(LLVMGetGlobalContext());
00331 }
00332 LLVMTypeRef LLVMIntType(unsigned NumBits) {
00333   return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
00334 }
00335 
00336 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
00337   return unwrap<IntegerType>(IntegerTy)->getBitWidth();
00338 }
00339 
00340 /*--.. Operations on real types ............................................--*/
00341 
00342 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
00343   return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
00344 }
00345 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
00346   return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
00347 }
00348 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
00349   return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
00350 }
00351 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
00352   return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
00353 }
00354 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
00355   return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
00356 }
00357 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
00358   return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
00359 }
00360 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
00361   return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
00362 }
00363 
00364 LLVMTypeRef LLVMHalfType(void) {
00365   return LLVMHalfTypeInContext(LLVMGetGlobalContext());
00366 }
00367 LLVMTypeRef LLVMFloatType(void) {
00368   return LLVMFloatTypeInContext(LLVMGetGlobalContext());
00369 }
00370 LLVMTypeRef LLVMDoubleType(void) {
00371   return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
00372 }
00373 LLVMTypeRef LLVMX86FP80Type(void) {
00374   return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
00375 }
00376 LLVMTypeRef LLVMFP128Type(void) {
00377   return LLVMFP128TypeInContext(LLVMGetGlobalContext());
00378 }
00379 LLVMTypeRef LLVMPPCFP128Type(void) {
00380   return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
00381 }
00382 LLVMTypeRef LLVMX86MMXType(void) {
00383   return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
00384 }
00385 
00386 /*--.. Operations on function types ........................................--*/
00387 
00388 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
00389                              LLVMTypeRef *ParamTypes, unsigned ParamCount,
00390                              LLVMBool IsVarArg) {
00391   ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
00392   return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
00393 }
00394 
00395 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
00396   return unwrap<FunctionType>(FunctionTy)->isVarArg();
00397 }
00398 
00399 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
00400   return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
00401 }
00402 
00403 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
00404   return unwrap<FunctionType>(FunctionTy)->getNumParams();
00405 }
00406 
00407 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
00408   FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
00409   for (FunctionType::param_iterator I = Ty->param_begin(),
00410                                     E = Ty->param_end(); I != E; ++I)
00411     *Dest++ = wrap(*I);
00412 }
00413 
00414 /*--.. Operations on struct types ..........................................--*/
00415 
00416 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
00417                            unsigned ElementCount, LLVMBool Packed) {
00418   ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
00419   return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
00420 }
00421 
00422 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
00423                            unsigned ElementCount, LLVMBool Packed) {
00424   return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
00425                                  ElementCount, Packed);
00426 }
00427 
00428 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
00429 {
00430   return wrap(StructType::create(*unwrap(C), Name));
00431 }
00432 
00433 const char *LLVMGetStructName(LLVMTypeRef Ty)
00434 {
00435   StructType *Type = unwrap<StructType>(Ty);
00436   if (!Type->hasName())
00437     return nullptr;
00438   return Type->getName().data();
00439 }
00440 
00441 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
00442                        unsigned ElementCount, LLVMBool Packed) {
00443   ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
00444   unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
00445 }
00446 
00447 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
00448   return unwrap<StructType>(StructTy)->getNumElements();
00449 }
00450 
00451 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
00452   StructType *Ty = unwrap<StructType>(StructTy);
00453   for (StructType::element_iterator I = Ty->element_begin(),
00454                                     E = Ty->element_end(); I != E; ++I)
00455     *Dest++ = wrap(*I);
00456 }
00457 
00458 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
00459   return unwrap<StructType>(StructTy)->isPacked();
00460 }
00461 
00462 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
00463   return unwrap<StructType>(StructTy)->isOpaque();
00464 }
00465 
00466 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
00467   return wrap(unwrap(M)->getTypeByName(Name));
00468 }
00469 
00470 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
00471 
00472 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
00473   return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
00474 }
00475 
00476 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
00477   return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
00478 }
00479 
00480 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
00481   return wrap(VectorType::get(unwrap(ElementType), ElementCount));
00482 }
00483 
00484 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
00485   return wrap(unwrap<SequentialType>(Ty)->getElementType());
00486 }
00487 
00488 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
00489   return unwrap<ArrayType>(ArrayTy)->getNumElements();
00490 }
00491 
00492 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
00493   return unwrap<PointerType>(PointerTy)->getAddressSpace();
00494 }
00495 
00496 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
00497   return unwrap<VectorType>(VectorTy)->getNumElements();
00498 }
00499 
00500 /*--.. Operations on other types ...........................................--*/
00501 
00502 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C)  {
00503   return wrap(Type::getVoidTy(*unwrap(C)));
00504 }
00505 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
00506   return wrap(Type::getLabelTy(*unwrap(C)));
00507 }
00508 
00509 LLVMTypeRef LLVMVoidType(void)  {
00510   return LLVMVoidTypeInContext(LLVMGetGlobalContext());
00511 }
00512 LLVMTypeRef LLVMLabelType(void) {
00513   return LLVMLabelTypeInContext(LLVMGetGlobalContext());
00514 }
00515 
00516 /*===-- Operations on values ----------------------------------------------===*/
00517 
00518 /*--.. Operations on all values ............................................--*/
00519 
00520 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
00521   return wrap(unwrap(Val)->getType());
00522 }
00523 
00524 const char *LLVMGetValueName(LLVMValueRef Val) {
00525   return unwrap(Val)->getName().data();
00526 }
00527 
00528 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
00529   unwrap(Val)->setName(Name);
00530 }
00531 
00532 void LLVMDumpValue(LLVMValueRef Val) {
00533   unwrap(Val)->dump();
00534 }
00535 
00536 char* LLVMPrintValueToString(LLVMValueRef Val) {
00537   std::string buf;
00538   raw_string_ostream os(buf);
00539 
00540   if (unwrap(Val))
00541     unwrap(Val)->print(os);
00542   else
00543     os << "Printing <null> Value";
00544 
00545   os.flush();
00546 
00547   return strdup(buf.c_str());
00548 }
00549 
00550 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
00551   unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
00552 }
00553 
00554 int LLVMHasMetadata(LLVMValueRef Inst) {
00555   return unwrap<Instruction>(Inst)->hasMetadata();
00556 }
00557 
00558 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
00559   return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
00560 }
00561 
00562 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
00563   unwrap<Instruction>(Inst)
00564       ->setMetadata(KindID, MD ? unwrap<MDNode>(MD) : nullptr);
00565 }
00566 
00567 /*--.. Conversion functions ................................................--*/
00568 
00569 #define LLVM_DEFINE_VALUE_CAST(name)                                       \
00570   LLVMValueRef LLVMIsA##name(LLVMValueRef Val) {                           \
00571     return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
00572   }
00573 
00574 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
00575 
00576 /*--.. Operations on Uses ..................................................--*/
00577 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
00578   Value *V = unwrap(Val);
00579   Value::use_iterator I = V->use_begin();
00580   if (I == V->use_end())
00581     return nullptr;
00582   return wrap(&*I);
00583 }
00584 
00585 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
00586   Use *Next = unwrap(U)->getNext();
00587   if (Next)
00588     return wrap(Next);
00589   return nullptr;
00590 }
00591 
00592 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
00593   return wrap(unwrap(U)->getUser());
00594 }
00595 
00596 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
00597   return wrap(unwrap(U)->get());
00598 }
00599 
00600 /*--.. Operations on Users .................................................--*/
00601 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
00602   Value *V = unwrap(Val);
00603   if (MDNode *MD = dyn_cast<MDNode>(V))
00604       return wrap(MD->getOperand(Index));
00605   return wrap(cast<User>(V)->getOperand(Index));
00606 }
00607 
00608 LLVMUseRef LLVMGetOperandUse(LLVMValueRef Val, unsigned Index) {
00609   Value *V = unwrap(Val);
00610   return wrap(&cast<User>(V)->getOperandUse(Index));
00611 }
00612 
00613 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
00614   unwrap<User>(Val)->setOperand(Index, unwrap(Op));
00615 }
00616 
00617 int LLVMGetNumOperands(LLVMValueRef Val) {
00618   Value *V = unwrap(Val);
00619   if (MDNode *MD = dyn_cast<MDNode>(V))
00620       return MD->getNumOperands();
00621   return cast<User>(V)->getNumOperands();
00622 }
00623 
00624 /*--.. Operations on constants of any type .................................--*/
00625 
00626 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
00627   return wrap(Constant::getNullValue(unwrap(Ty)));
00628 }
00629 
00630 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
00631   return wrap(Constant::getAllOnesValue(unwrap(Ty)));
00632 }
00633 
00634 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
00635   return wrap(UndefValue::get(unwrap(Ty)));
00636 }
00637 
00638 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
00639   return isa<Constant>(unwrap(Ty));
00640 }
00641 
00642 LLVMBool LLVMIsNull(LLVMValueRef Val) {
00643   if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
00644     return C->isNullValue();
00645   return false;
00646 }
00647 
00648 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
00649   return isa<UndefValue>(unwrap(Val));
00650 }
00651 
00652 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
00653   return
00654       wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
00655 }
00656 
00657 /*--.. Operations on metadata nodes ........................................--*/
00658 
00659 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
00660                                    unsigned SLen) {
00661   return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
00662 }
00663 
00664 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
00665   return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
00666 }
00667 
00668 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
00669                                  unsigned Count) {
00670   return wrap(MDNode::get(*unwrap(C),
00671                           makeArrayRef(unwrap<Value>(Vals, Count), Count)));
00672 }
00673 
00674 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
00675   return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
00676 }
00677 
00678 const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) {
00679   if (const MDString *S = dyn_cast<MDString>(unwrap(V))) {
00680     *Len = S->getString().size();
00681     return S->getString().data();
00682   }
00683   *Len = 0;
00684   return nullptr;
00685 }
00686 
00687 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V)
00688 {
00689   return cast<MDNode>(unwrap(V))->getNumOperands();
00690 }
00691 
00692 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest)
00693 {
00694   const MDNode *N = cast<MDNode>(unwrap(V));
00695   const unsigned numOperands = N->getNumOperands();
00696   for (unsigned i = 0; i < numOperands; i++)
00697     Dest[i] = wrap(N->getOperand(i));
00698 }
00699 
00700 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name)
00701 {
00702   if (NamedMDNode *N = unwrap(M)->getNamedMetadata(name)) {
00703     return N->getNumOperands();
00704   }
00705   return 0;
00706 }
00707 
00708 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRef *Dest)
00709 {
00710   NamedMDNode *N = unwrap(M)->getNamedMetadata(name);
00711   if (!N)
00712     return;
00713   for (unsigned i=0;i<N->getNumOperands();i++)
00714     Dest[i] = wrap(N->getOperand(i));
00715 }
00716 
00717 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name,
00718                                  LLVMValueRef Val)
00719 {
00720   NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
00721   if (!N)
00722     return;
00723   MDNode *Op = Val ? unwrap<MDNode>(Val) : nullptr;
00724   if (Op)
00725     N->addOperand(Op);
00726 }
00727 
00728 /*--.. Operations on scalar constants ......................................--*/
00729 
00730 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
00731                           LLVMBool SignExtend) {
00732   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
00733 }
00734 
00735 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
00736                                               unsigned NumWords,
00737                                               const uint64_t Words[]) {
00738     IntegerType *Ty = unwrap<IntegerType>(IntTy);
00739     return wrap(ConstantInt::get(Ty->getContext(),
00740                                  APInt(Ty->getBitWidth(),
00741                                        makeArrayRef(Words, NumWords))));
00742 }
00743 
00744 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
00745                                   uint8_t Radix) {
00746   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
00747                                Radix));
00748 }
00749 
00750 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
00751                                          unsigned SLen, uint8_t Radix) {
00752   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
00753                                Radix));
00754 }
00755 
00756 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
00757   return wrap(ConstantFP::get(unwrap(RealTy), N));
00758 }
00759 
00760 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
00761   return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
00762 }
00763 
00764 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
00765                                           unsigned SLen) {
00766   return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
00767 }
00768 
00769 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
00770   return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
00771 }
00772 
00773 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
00774   return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
00775 }
00776 
00777 double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
00778   ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
00779   Type *Ty = cFP->getType();
00780 
00781   if (Ty->isFloatTy()) {
00782     *LosesInfo = false;
00783     return cFP->getValueAPF().convertToFloat();
00784   }
00785 
00786   if (Ty->isDoubleTy()) {
00787     *LosesInfo = false;
00788     return cFP->getValueAPF().convertToDouble();
00789   }
00790 
00791   bool APFLosesInfo;
00792   APFloat APF = cFP->getValueAPF();
00793   APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &APFLosesInfo);
00794   *LosesInfo = APFLosesInfo;
00795   return APF.convertToDouble();
00796 }
00797 
00798 /*--.. Operations on composite constants ...................................--*/
00799 
00800 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
00801                                       unsigned Length,
00802                                       LLVMBool DontNullTerminate) {
00803   /* Inverted the sense of AddNull because ', 0)' is a
00804      better mnemonic for null termination than ', 1)'. */
00805   return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
00806                                            DontNullTerminate == 0));
00807 }
00808 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
00809                                       LLVMValueRef *ConstantVals,
00810                                       unsigned Count, LLVMBool Packed) {
00811   Constant **Elements = unwrap<Constant>(ConstantVals, Count);
00812   return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
00813                                       Packed != 0));
00814 }
00815 
00816 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
00817                              LLVMBool DontNullTerminate) {
00818   return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
00819                                   DontNullTerminate);
00820 }
00821 
00822 LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef c, unsigned idx) {
00823   return wrap(static_cast<ConstantDataSequential*>(unwrap(c))->getElementAsConstant(idx));
00824 }
00825 
00826 LLVMBool LLVMIsConstantString(LLVMValueRef c) {
00827   return static_cast<ConstantDataSequential*>(unwrap(c))->isString();
00828 }
00829 
00830 const char *LLVMGetAsString(LLVMValueRef c, size_t* Length) {
00831   StringRef str = static_cast<ConstantDataSequential*>(unwrap(c))->getAsString();
00832   *Length = str.size();
00833   return str.data();
00834 }
00835 
00836 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
00837                             LLVMValueRef *ConstantVals, unsigned Length) {
00838   ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
00839   return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
00840 }
00841 
00842 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
00843                              LLVMBool Packed) {
00844   return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
00845                                   Packed);
00846 }
00847 
00848 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
00849                                   LLVMValueRef *ConstantVals,
00850                                   unsigned Count) {
00851   Constant **Elements = unwrap<Constant>(ConstantVals, Count);
00852   StructType *Ty = cast<StructType>(unwrap(StructTy));
00853 
00854   return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
00855 }
00856 
00857 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
00858   return wrap(ConstantVector::get(makeArrayRef(
00859                             unwrap<Constant>(ScalarConstantVals, Size), Size)));
00860 }
00861 
00862 /*-- Opcode mapping */
00863 
00864 static LLVMOpcode map_to_llvmopcode(int opcode)
00865 {
00866     switch (opcode) {
00867       default: llvm_unreachable("Unhandled Opcode.");
00868 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
00869 #include "llvm/IR/Instruction.def"
00870 #undef HANDLE_INST
00871     }
00872 }
00873 
00874 static int map_from_llvmopcode(LLVMOpcode code)
00875 {
00876     switch (code) {
00877 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
00878 #include "llvm/IR/Instruction.def"
00879 #undef HANDLE_INST
00880     }
00881     llvm_unreachable("Unhandled Opcode.");
00882 }
00883 
00884 /*--.. Constant expressions ................................................--*/
00885 
00886 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
00887   return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
00888 }
00889 
00890 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
00891   return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
00892 }
00893 
00894 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
00895   return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
00896 }
00897 
00898 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
00899   return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
00900 }
00901 
00902 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
00903   return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
00904 }
00905 
00906 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
00907   return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
00908 }
00909 
00910 
00911 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
00912   return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
00913 }
00914 
00915 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
00916   return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
00917 }
00918 
00919 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00920   return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
00921                                    unwrap<Constant>(RHSConstant)));
00922 }
00923 
00924 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
00925                              LLVMValueRef RHSConstant) {
00926   return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
00927                                       unwrap<Constant>(RHSConstant)));
00928 }
00929 
00930 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
00931                              LLVMValueRef RHSConstant) {
00932   return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
00933                                       unwrap<Constant>(RHSConstant)));
00934 }
00935 
00936 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00937   return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
00938                                     unwrap<Constant>(RHSConstant)));
00939 }
00940 
00941 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00942   return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
00943                                    unwrap<Constant>(RHSConstant)));
00944 }
00945 
00946 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
00947                              LLVMValueRef RHSConstant) {
00948   return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
00949                                       unwrap<Constant>(RHSConstant)));
00950 }
00951 
00952 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
00953                              LLVMValueRef RHSConstant) {
00954   return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
00955                                       unwrap<Constant>(RHSConstant)));
00956 }
00957 
00958 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00959   return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
00960                                     unwrap<Constant>(RHSConstant)));
00961 }
00962 
00963 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00964   return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
00965                                    unwrap<Constant>(RHSConstant)));
00966 }
00967 
00968 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
00969                              LLVMValueRef RHSConstant) {
00970   return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
00971                                       unwrap<Constant>(RHSConstant)));
00972 }
00973 
00974 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
00975                              LLVMValueRef RHSConstant) {
00976   return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
00977                                       unwrap<Constant>(RHSConstant)));
00978 }
00979 
00980 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00981   return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
00982                                     unwrap<Constant>(RHSConstant)));
00983 }
00984 
00985 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00986   return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
00987                                     unwrap<Constant>(RHSConstant)));
00988 }
00989 
00990 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00991   return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
00992                                     unwrap<Constant>(RHSConstant)));
00993 }
00994 
00995 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
00996                                 LLVMValueRef RHSConstant) {
00997   return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
00998                                          unwrap<Constant>(RHSConstant)));
00999 }
01000 
01001 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
01002   return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
01003                                     unwrap<Constant>(RHSConstant)));
01004 }
01005 
01006 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
01007   return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
01008                                     unwrap<Constant>(RHSConstant)));
01009 }
01010 
01011 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
01012   return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
01013                                     unwrap<Constant>(RHSConstant)));
01014 }
01015 
01016 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
01017   return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
01018                                     unwrap<Constant>(RHSConstant)));
01019 }
01020 
01021 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
01022   return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
01023                                    unwrap<Constant>(RHSConstant)));
01024 }
01025 
01026 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
01027   return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
01028                                   unwrap<Constant>(RHSConstant)));
01029 }
01030 
01031 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
01032   return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
01033                                    unwrap<Constant>(RHSConstant)));
01034 }
01035 
01036 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
01037                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
01038   return wrap(ConstantExpr::getICmp(Predicate,
01039                                     unwrap<Constant>(LHSConstant),
01040                                     unwrap<Constant>(RHSConstant)));
01041 }
01042 
01043 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
01044                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
01045   return wrap(ConstantExpr::getFCmp(Predicate,
01046                                     unwrap<Constant>(LHSConstant),
01047                                     unwrap<Constant>(RHSConstant)));
01048 }
01049 
01050 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
01051   return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
01052                                    unwrap<Constant>(RHSConstant)));
01053 }
01054 
01055 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
01056   return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
01057                                     unwrap<Constant>(RHSConstant)));
01058 }
01059 
01060 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
01061   return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
01062                                     unwrap<Constant>(RHSConstant)));
01063 }
01064 
01065 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
01066                           LLVMValueRef *ConstantIndices, unsigned NumIndices) {
01067   ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
01068                                NumIndices);
01069   return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
01070                                              IdxList));
01071 }
01072 
01073 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
01074                                   LLVMValueRef *ConstantIndices,
01075                                   unsigned NumIndices) {
01076   Constant* Val = unwrap<Constant>(ConstantVal);
01077   ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
01078                                NumIndices);
01079   return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, IdxList));
01080 }
01081 
01082 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01083   return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
01084                                      unwrap(ToType)));
01085 }
01086 
01087 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01088   return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
01089                                     unwrap(ToType)));
01090 }
01091 
01092 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01093   return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
01094                                     unwrap(ToType)));
01095 }
01096 
01097 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01098   return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
01099                                        unwrap(ToType)));
01100 }
01101 
01102 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01103   return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
01104                                         unwrap(ToType)));
01105 }
01106 
01107 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01108   return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
01109                                       unwrap(ToType)));
01110 }
01111 
01112 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01113   return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
01114                                       unwrap(ToType)));
01115 }
01116 
01117 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01118   return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
01119                                       unwrap(ToType)));
01120 }
01121 
01122 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01123   return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
01124                                       unwrap(ToType)));
01125 }
01126 
01127 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01128   return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
01129                                         unwrap(ToType)));
01130 }
01131 
01132 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01133   return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
01134                                         unwrap(ToType)));
01135 }
01136 
01137 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01138   return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
01139                                        unwrap(ToType)));
01140 }
01141 
01142 LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
01143                                     LLVMTypeRef ToType) {
01144   return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
01145                                              unwrap(ToType)));
01146 }
01147 
01148 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
01149                                     LLVMTypeRef ToType) {
01150   return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
01151                                              unwrap(ToType)));
01152 }
01153 
01154 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
01155                                     LLVMTypeRef ToType) {
01156   return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
01157                                              unwrap(ToType)));
01158 }
01159 
01160 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
01161                                      LLVMTypeRef ToType) {
01162   return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
01163                                               unwrap(ToType)));
01164 }
01165 
01166 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
01167                                   LLVMTypeRef ToType) {
01168   return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
01169                                            unwrap(ToType)));
01170 }
01171 
01172 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
01173                               LLVMBool isSigned) {
01174   return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
01175                                            unwrap(ToType), isSigned));
01176 }
01177 
01178 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01179   return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
01180                                       unwrap(ToType)));
01181 }
01182 
01183 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
01184                              LLVMValueRef ConstantIfTrue,
01185                              LLVMValueRef ConstantIfFalse) {
01186   return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
01187                                       unwrap<Constant>(ConstantIfTrue),
01188                                       unwrap<Constant>(ConstantIfFalse)));
01189 }
01190 
01191 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
01192                                      LLVMValueRef IndexConstant) {
01193   return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
01194                                               unwrap<Constant>(IndexConstant)));
01195 }
01196 
01197 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
01198                                     LLVMValueRef ElementValueConstant,
01199                                     LLVMValueRef IndexConstant) {
01200   return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
01201                                          unwrap<Constant>(ElementValueConstant),
01202                                              unwrap<Constant>(IndexConstant)));
01203 }
01204 
01205 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
01206                                     LLVMValueRef VectorBConstant,
01207                                     LLVMValueRef MaskConstant) {
01208   return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
01209                                              unwrap<Constant>(VectorBConstant),
01210                                              unwrap<Constant>(MaskConstant)));
01211 }
01212 
01213 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
01214                                    unsigned NumIdx) {
01215   return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
01216                                             makeArrayRef(IdxList, NumIdx)));
01217 }
01218 
01219 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
01220                                   LLVMValueRef ElementValueConstant,
01221                                   unsigned *IdxList, unsigned NumIdx) {
01222   return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
01223                                          unwrap<Constant>(ElementValueConstant),
01224                                            makeArrayRef(IdxList, NumIdx)));
01225 }
01226 
01227 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
01228                                 const char *Constraints,
01229                                 LLVMBool HasSideEffects,
01230                                 LLVMBool IsAlignStack) {
01231   return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
01232                              Constraints, HasSideEffects, IsAlignStack));
01233 }
01234 
01235 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
01236   return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
01237 }
01238 
01239 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
01240 
01241 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
01242   return wrap(unwrap<GlobalValue>(Global)->getParent());
01243 }
01244 
01245 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
01246   return unwrap<GlobalValue>(Global)->isDeclaration();
01247 }
01248 
01249 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
01250   switch (unwrap<GlobalValue>(Global)->getLinkage()) {
01251   case GlobalValue::ExternalLinkage:
01252     return LLVMExternalLinkage;
01253   case GlobalValue::AvailableExternallyLinkage:
01254     return LLVMAvailableExternallyLinkage;
01255   case GlobalValue::LinkOnceAnyLinkage:
01256     return LLVMLinkOnceAnyLinkage;
01257   case GlobalValue::LinkOnceODRLinkage:
01258     return LLVMLinkOnceODRLinkage;
01259   case GlobalValue::WeakAnyLinkage:
01260     return LLVMWeakAnyLinkage;
01261   case GlobalValue::WeakODRLinkage:
01262     return LLVMWeakODRLinkage;
01263   case GlobalValue::AppendingLinkage:
01264     return LLVMAppendingLinkage;
01265   case GlobalValue::InternalLinkage:
01266     return LLVMInternalLinkage;
01267   case GlobalValue::PrivateLinkage:
01268     return LLVMPrivateLinkage;
01269   case GlobalValue::ExternalWeakLinkage:
01270     return LLVMExternalWeakLinkage;
01271   case GlobalValue::CommonLinkage:
01272     return LLVMCommonLinkage;
01273   }
01274 
01275   llvm_unreachable("Invalid GlobalValue linkage!");
01276 }
01277 
01278 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
01279   GlobalValue *GV = unwrap<GlobalValue>(Global);
01280 
01281   switch (Linkage) {
01282   case LLVMExternalLinkage:
01283     GV->setLinkage(GlobalValue::ExternalLinkage);
01284     break;
01285   case LLVMAvailableExternallyLinkage:
01286     GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
01287     break;
01288   case LLVMLinkOnceAnyLinkage:
01289     GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
01290     break;
01291   case LLVMLinkOnceODRLinkage:
01292     GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
01293     break;
01294   case LLVMLinkOnceODRAutoHideLinkage:
01295     DEBUG(errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
01296                     "longer supported.");
01297     break;
01298   case LLVMWeakAnyLinkage:
01299     GV->setLinkage(GlobalValue::WeakAnyLinkage);
01300     break;
01301   case LLVMWeakODRLinkage:
01302     GV->setLinkage(GlobalValue::WeakODRLinkage);
01303     break;
01304   case LLVMAppendingLinkage:
01305     GV->setLinkage(GlobalValue::AppendingLinkage);
01306     break;
01307   case LLVMInternalLinkage:
01308     GV->setLinkage(GlobalValue::InternalLinkage);
01309     break;
01310   case LLVMPrivateLinkage:
01311     GV->setLinkage(GlobalValue::PrivateLinkage);
01312     break;
01313   case LLVMLinkerPrivateLinkage:
01314     GV->setLinkage(GlobalValue::PrivateLinkage);
01315     break;
01316   case LLVMLinkerPrivateWeakLinkage:
01317     GV->setLinkage(GlobalValue::PrivateLinkage);
01318     break;
01319   case LLVMDLLImportLinkage:
01320     DEBUG(errs()
01321           << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
01322     break;
01323   case LLVMDLLExportLinkage:
01324     DEBUG(errs()
01325           << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
01326     break;
01327   case LLVMExternalWeakLinkage:
01328     GV->setLinkage(GlobalValue::ExternalWeakLinkage);
01329     break;
01330   case LLVMGhostLinkage:
01331     DEBUG(errs()
01332           << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
01333     break;
01334   case LLVMCommonLinkage:
01335     GV->setLinkage(GlobalValue::CommonLinkage);
01336     break;
01337   }
01338 }
01339 
01340 const char *LLVMGetSection(LLVMValueRef Global) {
01341   return unwrap<GlobalValue>(Global)->getSection();
01342 }
01343 
01344 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
01345   unwrap<GlobalObject>(Global)->setSection(Section);
01346 }
01347 
01348 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
01349   return static_cast<LLVMVisibility>(
01350     unwrap<GlobalValue>(Global)->getVisibility());
01351 }
01352 
01353 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
01354   unwrap<GlobalValue>(Global)
01355     ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
01356 }
01357 
01358 LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
01359   return static_cast<LLVMDLLStorageClass>(
01360       unwrap<GlobalValue>(Global)->getDLLStorageClass());
01361 }
01362 
01363 void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
01364   unwrap<GlobalValue>(Global)->setDLLStorageClass(
01365       static_cast<GlobalValue::DLLStorageClassTypes>(Class));
01366 }
01367 
01368 LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
01369   return unwrap<GlobalValue>(Global)->hasUnnamedAddr();
01370 }
01371 
01372 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
01373   unwrap<GlobalValue>(Global)->setUnnamedAddr(HasUnnamedAddr);
01374 }
01375 
01376 /*--.. Operations on global variables, load and store instructions .........--*/
01377 
01378 unsigned LLVMGetAlignment(LLVMValueRef V) {
01379   Value *P = unwrap<Value>(V);
01380   if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
01381     return GV->getAlignment();
01382   if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
01383     return AI->getAlignment();
01384   if (LoadInst *LI = dyn_cast<LoadInst>(P))
01385     return LI->getAlignment();
01386   if (StoreInst *SI = dyn_cast<StoreInst>(P))
01387     return SI->getAlignment();
01388 
01389   llvm_unreachable(
01390       "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
01391 }
01392 
01393 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
01394   Value *P = unwrap<Value>(V);
01395   if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
01396     GV->setAlignment(Bytes);
01397   else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
01398     AI->setAlignment(Bytes);
01399   else if (LoadInst *LI = dyn_cast<LoadInst>(P))
01400     LI->setAlignment(Bytes);
01401   else if (StoreInst *SI = dyn_cast<StoreInst>(P))
01402     SI->setAlignment(Bytes);
01403   else
01404     llvm_unreachable(
01405         "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
01406 }
01407 
01408 /*--.. Operations on global variables ......................................--*/
01409 
01410 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
01411   return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
01412                                  GlobalValue::ExternalLinkage, nullptr, Name));
01413 }
01414 
01415 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
01416                                          const char *Name,
01417                                          unsigned AddressSpace) {
01418   return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
01419                                  GlobalValue::ExternalLinkage, nullptr, Name,
01420                                  nullptr, GlobalVariable::NotThreadLocal,
01421                                  AddressSpace));
01422 }
01423 
01424 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
01425   return wrap(unwrap(M)->getNamedGlobal(Name));
01426 }
01427 
01428 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
01429   Module *Mod = unwrap(M);
01430   Module::global_iterator I = Mod->global_begin();
01431   if (I == Mod->global_end())
01432     return nullptr;
01433   return wrap(I);
01434 }
01435 
01436 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
01437   Module *Mod = unwrap(M);
01438   Module::global_iterator I = Mod->global_end();
01439   if (I == Mod->global_begin())
01440     return nullptr;
01441   return wrap(--I);
01442 }
01443 
01444 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
01445   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
01446   Module::global_iterator I = GV;
01447   if (++I == GV->getParent()->global_end())
01448     return nullptr;
01449   return wrap(I);
01450 }
01451 
01452 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
01453   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
01454   Module::global_iterator I = GV;
01455   if (I == GV->getParent()->global_begin())
01456     return nullptr;
01457   return wrap(--I);
01458 }
01459 
01460 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
01461   unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
01462 }
01463 
01464 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
01465   GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
01466   if ( !GV->hasInitializer() )
01467     return nullptr;
01468   return wrap(GV->getInitializer());
01469 }
01470 
01471 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
01472   unwrap<GlobalVariable>(GlobalVar)
01473     ->setInitializer(unwrap<Constant>(ConstantVal));
01474 }
01475 
01476 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
01477   return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
01478 }
01479 
01480 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
01481   unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
01482 }
01483 
01484 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
01485   return unwrap<GlobalVariable>(GlobalVar)->isConstant();
01486 }
01487 
01488 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
01489   unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
01490 }
01491 
01492 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
01493   switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
01494   case GlobalVariable::NotThreadLocal:
01495     return LLVMNotThreadLocal;
01496   case GlobalVariable::GeneralDynamicTLSModel:
01497     return LLVMGeneralDynamicTLSModel;
01498   case GlobalVariable::LocalDynamicTLSModel:
01499     return LLVMLocalDynamicTLSModel;
01500   case GlobalVariable::InitialExecTLSModel:
01501     return LLVMInitialExecTLSModel;
01502   case GlobalVariable::LocalExecTLSModel:
01503     return LLVMLocalExecTLSModel;
01504   }
01505 
01506   llvm_unreachable("Invalid GlobalVariable thread local mode");
01507 }
01508 
01509 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
01510   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
01511 
01512   switch (Mode) {
01513   case LLVMNotThreadLocal:
01514     GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
01515     break;
01516   case LLVMGeneralDynamicTLSModel:
01517     GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
01518     break;
01519   case LLVMLocalDynamicTLSModel:
01520     GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
01521     break;
01522   case LLVMInitialExecTLSModel:
01523     GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
01524     break;
01525   case LLVMLocalExecTLSModel:
01526     GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
01527     break;
01528   }
01529 }
01530 
01531 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
01532   return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
01533 }
01534 
01535 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
01536   unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
01537 }
01538 
01539 /*--.. Operations on aliases ......................................--*/
01540 
01541 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
01542                           const char *Name) {
01543   auto *PTy = cast<PointerType>(unwrap(Ty));
01544   return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
01545                                   GlobalValue::ExternalLinkage, Name,
01546                                   unwrap<GlobalObject>(Aliasee), unwrap(M)));
01547 }
01548 
01549 /*--.. Operations on functions .............................................--*/
01550 
01551 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
01552                              LLVMTypeRef FunctionTy) {
01553   return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
01554                                GlobalValue::ExternalLinkage, Name, unwrap(M)));
01555 }
01556 
01557 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
01558   return wrap(unwrap(M)->getFunction(Name));
01559 }
01560 
01561 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
01562   Module *Mod = unwrap(M);
01563   Module::iterator I = Mod->begin();
01564   if (I == Mod->end())
01565     return nullptr;
01566   return wrap(I);
01567 }
01568 
01569 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
01570   Module *Mod = unwrap(M);
01571   Module::iterator I = Mod->end();
01572   if (I == Mod->begin())
01573     return nullptr;
01574   return wrap(--I);
01575 }
01576 
01577 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
01578   Function *Func = unwrap<Function>(Fn);
01579   Module::iterator I = Func;
01580   if (++I == Func->getParent()->end())
01581     return nullptr;
01582   return wrap(I);
01583 }
01584 
01585 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
01586   Function *Func = unwrap<Function>(Fn);
01587   Module::iterator I = Func;
01588   if (I == Func->getParent()->begin())
01589     return nullptr;
01590   return wrap(--I);
01591 }
01592 
01593 void LLVMDeleteFunction(LLVMValueRef Fn) {
01594   unwrap<Function>(Fn)->eraseFromParent();
01595 }
01596 
01597 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
01598   if (Function *F = dyn_cast<Function>(unwrap(Fn)))
01599     return F->getIntrinsicID();
01600   return 0;
01601 }
01602 
01603 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
01604   return unwrap<Function>(Fn)->getCallingConv();
01605 }
01606 
01607 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
01608   return unwrap<Function>(Fn)->setCallingConv(
01609     static_cast<CallingConv::ID>(CC));
01610 }
01611 
01612 const char *LLVMGetGC(LLVMValueRef Fn) {
01613   Function *F = unwrap<Function>(Fn);
01614   return F->hasGC()? F->getGC() : nullptr;
01615 }
01616 
01617 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
01618   Function *F = unwrap<Function>(Fn);
01619   if (GC)
01620     F->setGC(GC);
01621   else
01622     F->clearGC();
01623 }
01624 
01625 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
01626   Function *Func = unwrap<Function>(Fn);
01627   const AttributeSet PAL = Func->getAttributes();
01628   AttrBuilder B(PA);
01629   const AttributeSet PALnew =
01630     PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
01631                       AttributeSet::get(Func->getContext(),
01632                                         AttributeSet::FunctionIndex, B));
01633   Func->setAttributes(PALnew);
01634 }
01635 
01636 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
01637                                         const char *V) {
01638   Function *Func = unwrap<Function>(Fn);
01639   AttributeSet::AttrIndex Idx =
01640     AttributeSet::AttrIndex(AttributeSet::FunctionIndex);
01641   AttrBuilder B;
01642 
01643   B.addAttribute(A, V);
01644   AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B);
01645   Func->addAttributes(Idx, Set);
01646 }
01647 
01648 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
01649   Function *Func = unwrap<Function>(Fn);
01650   const AttributeSet PAL = Func->getAttributes();
01651   AttrBuilder B(PA);
01652   const AttributeSet PALnew =
01653     PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
01654                          AttributeSet::get(Func->getContext(),
01655                                            AttributeSet::FunctionIndex, B));
01656   Func->setAttributes(PALnew);
01657 }
01658 
01659 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
01660   Function *Func = unwrap<Function>(Fn);
01661   const AttributeSet PAL = Func->getAttributes();
01662   return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex);
01663 }
01664 
01665 /*--.. Operations on parameters ............................................--*/
01666 
01667 unsigned LLVMCountParams(LLVMValueRef FnRef) {
01668   // This function is strictly redundant to
01669   //   LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
01670   return unwrap<Function>(FnRef)->arg_size();
01671 }
01672 
01673 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
01674   Function *Fn = unwrap<Function>(FnRef);
01675   for (Function::arg_iterator I = Fn->arg_begin(),
01676                               E = Fn->arg_end(); I != E; I++)
01677     *ParamRefs++ = wrap(I);
01678 }
01679 
01680 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
01681   Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
01682   while (index --> 0)
01683     AI++;
01684   return wrap(AI);
01685 }
01686 
01687 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
01688   return wrap(unwrap<Argument>(V)->getParent());
01689 }
01690 
01691 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
01692   Function *Func = unwrap<Function>(Fn);
01693   Function::arg_iterator I = Func->arg_begin();
01694   if (I == Func->arg_end())
01695     return nullptr;
01696   return wrap(I);
01697 }
01698 
01699 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
01700   Function *Func = unwrap<Function>(Fn);
01701   Function::arg_iterator I = Func->arg_end();
01702   if (I == Func->arg_begin())
01703     return nullptr;
01704   return wrap(--I);
01705 }
01706 
01707 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
01708   Argument *A = unwrap<Argument>(Arg);
01709   Function::arg_iterator I = A;
01710   if (++I == A->getParent()->arg_end())
01711     return nullptr;
01712   return wrap(I);
01713 }
01714 
01715 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
01716   Argument *A = unwrap<Argument>(Arg);
01717   Function::arg_iterator I = A;
01718   if (I == A->getParent()->arg_begin())
01719     return nullptr;
01720   return wrap(--I);
01721 }
01722 
01723 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
01724   Argument *A = unwrap<Argument>(Arg);
01725   AttrBuilder B(PA);
01726   A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1,  B));
01727 }
01728 
01729 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
01730   Argument *A = unwrap<Argument>(Arg);
01731   AttrBuilder B(PA);
01732   A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1,  B));
01733 }
01734 
01735 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
01736   Argument *A = unwrap<Argument>(Arg);
01737   return (LLVMAttribute)A->getParent()->getAttributes().
01738     Raw(A->getArgNo()+1);
01739 }
01740 
01741 
01742 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
01743   Argument *A = unwrap<Argument>(Arg);
01744   AttrBuilder B;
01745   B.addAlignmentAttr(align);
01746   A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B));
01747 }
01748 
01749 /*--.. Operations on basic blocks ..........................................--*/
01750 
01751 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
01752   return wrap(static_cast<Value*>(unwrap(BB)));
01753 }
01754 
01755 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
01756   return isa<BasicBlock>(unwrap(Val));
01757 }
01758 
01759 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
01760   return wrap(unwrap<BasicBlock>(Val));
01761 }
01762 
01763 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
01764   return wrap(unwrap(BB)->getParent());
01765 }
01766 
01767 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
01768   return wrap(unwrap(BB)->getTerminator());
01769 }
01770 
01771 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
01772   return unwrap<Function>(FnRef)->size();
01773 }
01774 
01775 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
01776   Function *Fn = unwrap<Function>(FnRef);
01777   for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
01778     *BasicBlocksRefs++ = wrap(I);
01779 }
01780 
01781 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
01782   return wrap(&unwrap<Function>(Fn)->getEntryBlock());
01783 }
01784 
01785 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
01786   Function *Func = unwrap<Function>(Fn);
01787   Function::iterator I = Func->begin();
01788   if (I == Func->end())
01789     return nullptr;
01790   return wrap(I);
01791 }
01792 
01793 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
01794   Function *Func = unwrap<Function>(Fn);
01795   Function::iterator I = Func->end();
01796   if (I == Func->begin())
01797     return nullptr;
01798   return wrap(--I);
01799 }
01800 
01801 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
01802   BasicBlock *Block = unwrap(BB);
01803   Function::iterator I = Block;
01804   if (++I == Block->getParent()->end())
01805     return nullptr;
01806   return wrap(I);
01807 }
01808 
01809 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
01810   BasicBlock *Block = unwrap(BB);
01811   Function::iterator I = Block;
01812   if (I == Block->getParent()->begin())
01813     return nullptr;
01814   return wrap(--I);
01815 }
01816 
01817 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
01818                                                 LLVMValueRef FnRef,
01819                                                 const char *Name) {
01820   return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
01821 }
01822 
01823 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
01824   return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
01825 }
01826 
01827 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
01828                                                 LLVMBasicBlockRef BBRef,
01829                                                 const char *Name) {
01830   BasicBlock *BB = unwrap(BBRef);
01831   return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
01832 }
01833 
01834 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
01835                                        const char *Name) {
01836   return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
01837 }
01838 
01839 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
01840   unwrap(BBRef)->eraseFromParent();
01841 }
01842 
01843 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
01844   unwrap(BBRef)->removeFromParent();
01845 }
01846 
01847 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
01848   unwrap(BB)->moveBefore(unwrap(MovePos));
01849 }
01850 
01851 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
01852   unwrap(BB)->moveAfter(unwrap(MovePos));
01853 }
01854 
01855 /*--.. Operations on instructions ..........................................--*/
01856 
01857 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
01858   return wrap(unwrap<Instruction>(Inst)->getParent());
01859 }
01860 
01861 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
01862   BasicBlock *Block = unwrap(BB);
01863   BasicBlock::iterator I = Block->begin();
01864   if (I == Block->end())
01865     return nullptr;
01866   return wrap(I);
01867 }
01868 
01869 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
01870   BasicBlock *Block = unwrap(BB);
01871   BasicBlock::iterator I = Block->end();
01872   if (I == Block->begin())
01873     return nullptr;
01874   return wrap(--I);
01875 }
01876 
01877 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
01878   Instruction *Instr = unwrap<Instruction>(Inst);
01879   BasicBlock::iterator I = Instr;
01880   if (++I == Instr->getParent()->end())
01881     return nullptr;
01882   return wrap(I);
01883 }
01884 
01885 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
01886   Instruction *Instr = unwrap<Instruction>(Inst);
01887   BasicBlock::iterator I = Instr;
01888   if (I == Instr->getParent()->begin())
01889     return nullptr;
01890   return wrap(--I);
01891 }
01892 
01893 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
01894   unwrap<Instruction>(Inst)->eraseFromParent();
01895 }
01896 
01897 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
01898   if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
01899     return (LLVMIntPredicate)I->getPredicate();
01900   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
01901     if (CE->getOpcode() == Instruction::ICmp)
01902       return (LLVMIntPredicate)CE->getPredicate();
01903   return (LLVMIntPredicate)0;
01904 }
01905 
01906 LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) {
01907   if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
01908     return (LLVMRealPredicate)I->getPredicate();
01909   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
01910     if (CE->getOpcode() == Instruction::FCmp)
01911       return (LLVMRealPredicate)CE->getPredicate();
01912   return (LLVMRealPredicate)0;
01913 }
01914 
01915 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
01916   if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
01917     return map_to_llvmopcode(C->getOpcode());
01918   return (LLVMOpcode)0;
01919 }
01920 
01921 LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) {
01922   if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
01923     return wrap(C->clone());
01924   return nullptr;
01925 }
01926 
01927 /*--.. Call and invoke instructions ........................................--*/
01928 
01929 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
01930   Value *V = unwrap(Instr);
01931   if (CallInst *CI = dyn_cast<CallInst>(V))
01932     return CI->getCallingConv();
01933   if (InvokeInst *II = dyn_cast<InvokeInst>(V))
01934     return II->getCallingConv();
01935   llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
01936 }
01937 
01938 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
01939   Value *V = unwrap(Instr);
01940   if (CallInst *CI = dyn_cast<CallInst>(V))
01941     return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
01942   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
01943     return II->setCallingConv(static_cast<CallingConv::ID>(CC));
01944   llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
01945 }
01946 
01947 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
01948                            LLVMAttribute PA) {
01949   CallSite Call = CallSite(unwrap<Instruction>(Instr));
01950   AttrBuilder B(PA);
01951   Call.setAttributes(
01952     Call.getAttributes().addAttributes(Call->getContext(), index,
01953                                        AttributeSet::get(Call->getContext(),
01954                                                          index, B)));
01955 }
01956 
01957 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
01958                               LLVMAttribute PA) {
01959   CallSite Call = CallSite(unwrap<Instruction>(Instr));
01960   AttrBuilder B(PA);
01961   Call.setAttributes(Call.getAttributes()
01962                        .removeAttributes(Call->getContext(), index,
01963                                          AttributeSet::get(Call->getContext(),
01964                                                            index, B)));
01965 }
01966 
01967 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
01968                                 unsigned align) {
01969   CallSite Call = CallSite(unwrap<Instruction>(Instr));
01970   AttrBuilder B;
01971   B.addAlignmentAttr(align);
01972   Call.setAttributes(Call.getAttributes()
01973                        .addAttributes(Call->getContext(), index,
01974                                       AttributeSet::get(Call->getContext(),
01975                                                         index, B)));
01976 }
01977 
01978 /*--.. Operations on call instructions (only) ..............................--*/
01979 
01980 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
01981   return unwrap<CallInst>(Call)->isTailCall();
01982 }
01983 
01984 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
01985   unwrap<CallInst>(Call)->setTailCall(isTailCall);
01986 }
01987 
01988 /*--.. Operations on terminators ...........................................--*/
01989 
01990 unsigned LLVMGetNumSuccessors(LLVMValueRef Term) {
01991   return unwrap<TerminatorInst>(Term)->getNumSuccessors();
01992 }
01993 
01994 LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) {
01995   return wrap(unwrap<TerminatorInst>(Term)->getSuccessor(i));
01996 }
01997 
01998 void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) {
01999   return unwrap<TerminatorInst>(Term)->setSuccessor(i,unwrap(block));
02000 }
02001 
02002 /*--.. Operations on branch instructions (only) ............................--*/
02003 
02004 LLVMBool LLVMIsConditional(LLVMValueRef Branch) {
02005   return unwrap<BranchInst>(Branch)->isConditional();
02006 }
02007 
02008 LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) {
02009   return wrap(unwrap<BranchInst>(Branch)->getCondition());
02010 }
02011 
02012 void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) {
02013   return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
02014 }
02015 
02016 /*--.. Operations on switch instructions (only) ............................--*/
02017 
02018 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
02019   return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
02020 }
02021 
02022 /*--.. Operations on phi nodes .............................................--*/
02023 
02024 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
02025                      LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
02026   PHINode *PhiVal = unwrap<PHINode>(PhiNode);
02027   for (unsigned I = 0; I != Count; ++I)
02028     PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
02029 }
02030 
02031 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
02032   return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
02033 }
02034 
02035 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
02036   return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
02037 }
02038 
02039 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
02040   return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
02041 }
02042 
02043 
02044 /*===-- Instruction builders ----------------------------------------------===*/
02045 
02046 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
02047   return wrap(new IRBuilder<>(*unwrap(C)));
02048 }
02049 
02050 LLVMBuilderRef LLVMCreateBuilder(void) {
02051   return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
02052 }
02053 
02054 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
02055                          LLVMValueRef Instr) {
02056   BasicBlock *BB = unwrap(Block);
02057   Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
02058   unwrap(Builder)->SetInsertPoint(BB, I);
02059 }
02060 
02061 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
02062   Instruction *I = unwrap<Instruction>(Instr);
02063   unwrap(Builder)->SetInsertPoint(I->getParent(), I);
02064 }
02065 
02066 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
02067   BasicBlock *BB = unwrap(Block);
02068   unwrap(Builder)->SetInsertPoint(BB);
02069 }
02070 
02071 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
02072    return wrap(unwrap(Builder)->GetInsertBlock());
02073 }
02074 
02075 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
02076   unwrap(Builder)->ClearInsertionPoint();
02077 }
02078 
02079 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
02080   unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
02081 }
02082 
02083 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
02084                                    const char *Name) {
02085   unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
02086 }
02087 
02088 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
02089   delete unwrap(Builder);
02090 }
02091 
02092 /*--.. Metadata builders ...................................................--*/
02093 
02094 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
02095   MDNode *Loc = L ? unwrap<MDNode>(L) : nullptr;
02096   unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
02097 }
02098 
02099 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
02100   return wrap(unwrap(Builder)->getCurrentDebugLocation()
02101               .getAsMDNode(unwrap(Builder)->getContext()));
02102 }
02103 
02104 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
02105   unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
02106 }
02107 
02108 
02109 /*--.. Instruction builders ................................................--*/
02110 
02111 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
02112   return wrap(unwrap(B)->CreateRetVoid());
02113 }
02114 
02115 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
02116   return wrap(unwrap(B)->CreateRet(unwrap(V)));
02117 }
02118 
02119 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
02120                                    unsigned N) {
02121   return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
02122 }
02123 
02124 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
02125   return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
02126 }
02127 
02128 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
02129                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
02130   return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
02131 }
02132 
02133 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
02134                              LLVMBasicBlockRef Else, unsigned NumCases) {
02135   return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
02136 }
02137 
02138 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
02139                                  unsigned NumDests) {
02140   return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
02141 }
02142 
02143 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
02144                              LLVMValueRef *Args, unsigned NumArgs,
02145                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
02146                              const char *Name) {
02147   return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
02148                                       makeArrayRef(unwrap(Args), NumArgs),
02149                                       Name));
02150 }
02151 
02152 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
02153                                  LLVMValueRef PersFn, unsigned NumClauses,
02154                                  const char *Name) {
02155   return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
02156                                           cast<Function>(unwrap(PersFn)),
02157                                           NumClauses, Name));
02158 }
02159 
02160 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
02161   return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
02162 }
02163 
02164 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
02165   return wrap(unwrap(B)->CreateUnreachable());
02166 }
02167 
02168 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
02169                  LLVMBasicBlockRef Dest) {
02170   unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
02171 }
02172 
02173 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
02174   unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
02175 }
02176 
02177 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
02178   unwrap<LandingPadInst>(LandingPad)->
02179     addClause(cast<Constant>(unwrap(ClauseVal)));
02180 }
02181 
02182 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
02183   unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
02184 }
02185 
02186 /*--.. Arithmetic ..........................................................--*/
02187 
02188 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02189                           const char *Name) {
02190   return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
02191 }
02192 
02193 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02194                           const char *Name) {
02195   return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
02196 }
02197 
02198 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02199                           const char *Name) {
02200   return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
02201 }
02202 
02203 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02204                           const char *Name) {
02205   return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
02206 }
02207 
02208 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02209                           const char *Name) {
02210   return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
02211 }
02212 
02213 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02214                           const char *Name) {
02215   return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
02216 }
02217 
02218 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02219                           const char *Name) {
02220   return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
02221 }
02222 
02223 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02224                           const char *Name) {
02225   return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
02226 }
02227 
02228 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02229                           const char *Name) {
02230   return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
02231 }
02232 
02233 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02234                           const char *Name) {
02235   return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
02236 }
02237 
02238 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02239                           const char *Name) {
02240   return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
02241 }
02242 
02243 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02244                           const char *Name) {
02245   return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
02246 }
02247 
02248 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02249                            const char *Name) {
02250   return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
02251 }
02252 
02253 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02254                            const char *Name) {
02255   return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
02256 }
02257 
02258 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
02259                                 LLVMValueRef RHS, const char *Name) {
02260   return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
02261 }
02262 
02263 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02264                            const char *Name) {
02265   return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
02266 }
02267 
02268 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02269                            const char *Name) {
02270   return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
02271 }
02272 
02273 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02274                            const char *Name) {
02275   return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
02276 }
02277 
02278 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02279                            const char *Name) {
02280   return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
02281 }
02282 
02283 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02284                           const char *Name) {
02285   return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
02286 }
02287 
02288 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02289                            const char *Name) {
02290   return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
02291 }
02292 
02293 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02294                            const char *Name) {
02295   return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
02296 }
02297 
02298 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02299                           const char *Name) {
02300   return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
02301 }
02302 
02303 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02304                          const char *Name) {
02305   return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
02306 }
02307 
02308 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02309                           const char *Name) {
02310   return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
02311 }
02312 
02313 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
02314                             LLVMValueRef LHS, LLVMValueRef RHS,
02315                             const char *Name) {
02316   return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
02317                                      unwrap(RHS), Name));
02318 }
02319 
02320 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
02321   return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
02322 }
02323 
02324 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
02325                              const char *Name) {
02326   return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
02327 }
02328 
02329 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
02330                              const char *Name) {
02331   return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
02332 }
02333 
02334 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
02335   return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
02336 }
02337 
02338 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
02339   return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
02340 }
02341 
02342 /*--.. Memory ..............................................................--*/
02343 
02344 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
02345                              const char *Name) {
02346   Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
02347   Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
02348   AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
02349   Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
02350                                                ITy, unwrap(Ty), AllocSize,
02351                                                nullptr, nullptr, "");
02352   return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
02353 }
02354 
02355 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
02356                                   LLVMValueRef Val, const char *Name) {
02357   Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
02358   Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
02359   AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
02360   Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
02361                                                ITy, unwrap(Ty), AllocSize,
02362                                                unwrap(Val), nullptr, "");
02363   return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
02364 }
02365 
02366 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
02367                              const char *Name) {
02368   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
02369 }
02370 
02371 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
02372                                   LLVMValueRef Val, const char *Name) {
02373   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
02374 }
02375 
02376 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
02377   return wrap(unwrap(B)->Insert(
02378      CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
02379 }
02380 
02381 
02382 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
02383                            const char *Name) {
02384   return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
02385 }
02386 
02387 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
02388                             LLVMValueRef PointerVal) {
02389   return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
02390 }
02391 
02392 static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
02393   switch (Ordering) {
02394     case LLVMAtomicOrderingNotAtomic: return NotAtomic;
02395     case LLVMAtomicOrderingUnordered: return Unordered;
02396     case LLVMAtomicOrderingMonotonic: return Monotonic;
02397     case LLVMAtomicOrderingAcquire: return Acquire;
02398     case LLVMAtomicOrderingRelease: return Release;
02399     case LLVMAtomicOrderingAcquireRelease: return AcquireRelease;
02400     case LLVMAtomicOrderingSequentiallyConsistent:
02401       return SequentiallyConsistent;
02402   }
02403 
02404   llvm_unreachable("Invalid LLVMAtomicOrdering value!");
02405 }
02406 
02407 LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
02408                             LLVMBool isSingleThread, const char *Name) {
02409   return wrap(
02410     unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
02411                            isSingleThread ? SingleThread : CrossThread,
02412                            Name));
02413 }
02414 
02415 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
02416                           LLVMValueRef *Indices, unsigned NumIndices,
02417                           const char *Name) {
02418   ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
02419   return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name));
02420 }
02421 
02422 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
02423                                   LLVMValueRef *Indices, unsigned NumIndices,
02424                                   const char *Name) {
02425   ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
02426   return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name));
02427 }
02428 
02429 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
02430                                 unsigned Idx, const char *Name) {
02431   return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
02432 }
02433 
02434 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
02435                                    const char *Name) {
02436   return wrap(unwrap(B)->CreateGlobalString(Str, Name));
02437 }
02438 
02439 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
02440                                       const char *Name) {
02441   return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
02442 }
02443 
02444 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
02445   Value *P = unwrap<Value>(MemAccessInst);
02446   if (LoadInst *LI = dyn_cast<LoadInst>(P))
02447     return LI->isVolatile();
02448   return cast<StoreInst>(P)->isVolatile();
02449 }
02450 
02451 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
02452   Value *P = unwrap<Value>(MemAccessInst);
02453   if (LoadInst *LI = dyn_cast<LoadInst>(P))
02454     return LI->setVolatile(isVolatile);
02455   return cast<StoreInst>(P)->setVolatile(isVolatile);
02456 }
02457 
02458 /*--.. Casts ...............................................................--*/
02459 
02460 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
02461                             LLVMTypeRef DestTy, const char *Name) {
02462   return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
02463 }
02464 
02465 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
02466                            LLVMTypeRef DestTy, const char *Name) {
02467   return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
02468 }
02469 
02470 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
02471                            LLVMTypeRef DestTy, const char *Name) {
02472   return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
02473 }
02474 
02475 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
02476                              LLVMTypeRef DestTy, const char *Name) {
02477   return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
02478 }
02479 
02480 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
02481                              LLVMTypeRef DestTy, const char *Name) {
02482   return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
02483 }
02484 
02485 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
02486                              LLVMTypeRef DestTy, const char *Name) {
02487   return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
02488 }
02489 
02490 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
02491                              LLVMTypeRef DestTy, const char *Name) {
02492   return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
02493 }
02494 
02495 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
02496                               LLVMTypeRef DestTy, const char *Name) {
02497   return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
02498 }
02499 
02500 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
02501                             LLVMTypeRef DestTy, const char *Name) {
02502   return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
02503 }
02504 
02505 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
02506                                LLVMTypeRef DestTy, const char *Name) {
02507   return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
02508 }
02509 
02510 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
02511                                LLVMTypeRef DestTy, const char *Name) {
02512   return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
02513 }
02514 
02515 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
02516                               LLVMTypeRef DestTy, const char *Name) {
02517   return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
02518 }
02519 
02520 LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
02521                                     LLVMTypeRef DestTy, const char *Name) {
02522   return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
02523 }
02524 
02525 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
02526                                     LLVMTypeRef DestTy, const char *Name) {
02527   return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
02528                                              Name));
02529 }
02530 
02531 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
02532                                     LLVMTypeRef DestTy, const char *Name) {
02533   return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
02534                                              Name));
02535 }
02536 
02537 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
02538                                      LLVMTypeRef DestTy, const char *Name) {
02539   return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
02540                                               Name));
02541 }
02542 
02543 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
02544                            LLVMTypeRef DestTy, const char *Name) {
02545   return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
02546                                     unwrap(DestTy), Name));
02547 }
02548 
02549 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
02550                                   LLVMTypeRef DestTy, const char *Name) {
02551   return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
02552 }
02553 
02554 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
02555                               LLVMTypeRef DestTy, const char *Name) {
02556   return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
02557                                        /*isSigned*/true, Name));
02558 }
02559 
02560 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
02561                              LLVMTypeRef DestTy, const char *Name) {
02562   return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
02563 }
02564 
02565 /*--.. Comparisons .........................................................--*/
02566 
02567 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
02568                            LLVMValueRef LHS, LLVMValueRef RHS,
02569                            const char *Name) {
02570   return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
02571                                     unwrap(LHS), unwrap(RHS), Name));
02572 }
02573 
02574 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
02575                            LLVMValueRef LHS, LLVMValueRef RHS,
02576                            const char *Name) {
02577   return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
02578                                     unwrap(LHS), unwrap(RHS), Name));
02579 }
02580 
02581 /*--.. Miscellaneous instructions ..........................................--*/
02582 
02583 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
02584   return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
02585 }
02586 
02587 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
02588                            LLVMValueRef *Args, unsigned NumArgs,
02589                            const char *Name) {
02590   return wrap(unwrap(B)->CreateCall(unwrap(Fn),
02591                                     makeArrayRef(unwrap(Args), NumArgs),
02592                                     Name));
02593 }
02594 
02595 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
02596                              LLVMValueRef Then, LLVMValueRef Else,
02597                              const char *Name) {
02598   return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
02599                                       Name));
02600 }
02601 
02602 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
02603                             LLVMTypeRef Ty, const char *Name) {
02604   return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
02605 }
02606 
02607 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
02608                                       LLVMValueRef Index, const char *Name) {
02609   return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
02610                                               Name));
02611 }
02612 
02613 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
02614                                     LLVMValueRef EltVal, LLVMValueRef Index,
02615                                     const char *Name) {
02616   return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
02617                                              unwrap(Index), Name));
02618 }
02619 
02620 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
02621                                     LLVMValueRef V2, LLVMValueRef Mask,
02622                                     const char *Name) {
02623   return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
02624                                              unwrap(Mask), Name));
02625 }
02626 
02627 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
02628                                    unsigned Index, const char *Name) {
02629   return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
02630 }
02631 
02632 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
02633                                   LLVMValueRef EltVal, unsigned Index,
02634                                   const char *Name) {
02635   return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
02636                                            Index, Name));
02637 }
02638 
02639 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
02640                              const char *Name) {
02641   return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
02642 }
02643 
02644 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
02645                                 const char *Name) {
02646   return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
02647 }
02648 
02649 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
02650                               LLVMValueRef RHS, const char *Name) {
02651   return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
02652 }
02653 
02654 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
02655                                LLVMValueRef PTR, LLVMValueRef Val,
02656                                LLVMAtomicOrdering ordering,
02657                                LLVMBool singleThread) {
02658   AtomicRMWInst::BinOp intop;
02659   switch (op) {
02660     case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
02661     case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
02662     case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
02663     case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
02664     case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
02665     case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
02666     case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
02667     case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
02668     case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
02669     case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
02670     case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
02671   }
02672   return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
02673     mapFromLLVMOrdering(ordering), singleThread ? SingleThread : CrossThread));
02674 }
02675 
02676 
02677 /*===-- Module providers --------------------------------------------------===*/
02678 
02679 LLVMModuleProviderRef
02680 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
02681   return reinterpret_cast<LLVMModuleProviderRef>(M);
02682 }
02683 
02684 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
02685   delete unwrap(MP);
02686 }
02687 
02688 
02689 /*===-- Memory buffers ----------------------------------------------------===*/
02690 
02691 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
02692     const char *Path,
02693     LLVMMemoryBufferRef *OutMemBuf,
02694     char **OutMessage) {
02695 
02696   ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
02697   if (std::error_code EC = MBOrErr.getError()) {
02698     *OutMessage = strdup(EC.message().c_str());
02699     return 1;
02700   }
02701   *OutMemBuf = wrap(MBOrErr.get().release());
02702   return 0;
02703 }
02704 
02705 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
02706                                          char **OutMessage) {
02707   ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN();
02708   if (std::error_code EC = MBOrErr.getError()) {
02709     *OutMessage = strdup(EC.message().c_str());
02710     return 1;
02711   }
02712   *OutMemBuf = wrap(MBOrErr.get().release());
02713   return 0;
02714 }
02715 
02716 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
02717     const char *InputData,
02718     size_t InputDataLength,
02719     const char *BufferName,
02720     LLVMBool RequiresNullTerminator) {
02721 
02722   return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength),
02723                                          StringRef(BufferName),
02724                                          RequiresNullTerminator).release());
02725 }
02726 
02727 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
02728     const char *InputData,
02729     size_t InputDataLength,
02730     const char *BufferName) {
02731 
02732   return wrap(
02733       MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength),
02734                                      StringRef(BufferName)).release());
02735 }
02736 
02737 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
02738   return unwrap(MemBuf)->getBufferStart();
02739 }
02740 
02741 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
02742   return unwrap(MemBuf)->getBufferSize();
02743 }
02744 
02745 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
02746   delete unwrap(MemBuf);
02747 }
02748 
02749 /*===-- Pass Registry -----------------------------------------------------===*/
02750 
02751 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
02752   return wrap(PassRegistry::getPassRegistry());
02753 }
02754 
02755 /*===-- Pass Manager ------------------------------------------------------===*/
02756 
02757 LLVMPassManagerRef LLVMCreatePassManager() {
02758   return wrap(new PassManager());
02759 }
02760 
02761 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
02762   return wrap(new FunctionPassManager(unwrap(M)));
02763 }
02764 
02765 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
02766   return LLVMCreateFunctionPassManagerForModule(
02767                                             reinterpret_cast<LLVMModuleRef>(P));
02768 }
02769 
02770 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
02771   return unwrap<PassManager>(PM)->run(*unwrap(M));
02772 }
02773 
02774 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
02775   return unwrap<FunctionPassManager>(FPM)->doInitialization();
02776 }
02777 
02778 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
02779   return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
02780 }
02781 
02782 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
02783   return unwrap<FunctionPassManager>(FPM)->doFinalization();
02784 }
02785 
02786 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
02787   delete unwrap(PM);
02788 }
02789 
02790 /*===-- Threading ------------------------------------------------------===*/
02791 
02792 LLVMBool LLVMStartMultithreaded() {
02793   return LLVMIsMultithreaded();
02794 }
02795 
02796 void LLVMStopMultithreaded() {
02797 }
02798 
02799 LLVMBool LLVMIsMultithreaded() {
02800   return llvm_is_multithreaded();
02801 }