Move types back to the 2.5 API.
[oota-llvm.git] / lib / VMCore / Core.cpp
1 //===-- Core.cpp ----------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the C bindings for libLLVMCore.a, which implements
11 // the LLVM intermediate representation.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm-c/Core.h"
16 #include "llvm/Bitcode/ReaderWriter.h"
17 #include "llvm/Constants.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/GlobalVariable.h"
20 #include "llvm/GlobalAlias.h"
21 #include "llvm/LLVMContext.h"
22 #include "llvm/TypeSymbolTable.h"
23 #include "llvm/ModuleProvider.h"
24 #include "llvm/InlineAsm.h"
25 #include "llvm/IntrinsicInst.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/CallSite.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include <cassert>
30 #include <cstdlib>
31 #include <cstring>
32
33 using namespace llvm;
34
35
36 /*===-- Error handling ----------------------------------------------------===*/
37
38 void LLVMDisposeMessage(char *Message) {
39   free(Message);
40 }
41
42
43 /*===-- Operations on contexts --------------------------------------------===*/
44
45 LLVMContextRef LLVMContextCreate() {
46   return wrap(new LLVMContext());
47 }
48
49 LLVMContextRef LLVMGetGlobalContext() {
50   return wrap(&getGlobalContext());
51 }
52
53 void LLVMContextDispose(LLVMContextRef C) {
54   delete unwrap(C);
55 }
56
57
58 /*===-- Operations on modules ---------------------------------------------===*/
59
60 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
61   return wrap(new Module(ModuleID, getGlobalContext()));
62 }
63
64 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID, 
65                                                 LLVMContextRef C) {
66   return wrap(new Module(ModuleID, *unwrap(C)));
67 }
68
69 void LLVMDisposeModule(LLVMModuleRef M) {
70   delete unwrap(M);
71 }
72
73 /*--.. Data layout .........................................................--*/
74 const char * LLVMGetDataLayout(LLVMModuleRef M) {
75   return unwrap(M)->getDataLayout().c_str();
76 }
77
78 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
79   unwrap(M)->setDataLayout(Triple);
80 }
81
82 /*--.. Target triple .......................................................--*/
83 const char * LLVMGetTarget(LLVMModuleRef M) {
84   return unwrap(M)->getTargetTriple().c_str();
85 }
86
87 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
88   unwrap(M)->setTargetTriple(Triple);
89 }
90
91 /*--.. Type names ..........................................................--*/
92 int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
93   return unwrap(M)->addTypeName(Name, unwrap(Ty));
94 }
95
96 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
97   std::string N(Name);
98   
99   TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
100   for (TypeSymbolTable::iterator I = TST.begin(), E = TST.end(); I != E; ++I)
101     if (I->first == N)
102       TST.remove(I);
103 }
104
105 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
106   std::string N(Name);
107   return wrap(unwrap(M)->getTypeByName(N));
108 }
109
110 void LLVMDumpModule(LLVMModuleRef M) {
111   unwrap(M)->dump();
112 }
113
114
115 /*===-- Operations on types -----------------------------------------------===*/
116
117 /*--.. Operations on all types (mostly) ....................................--*/
118
119 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
120   switch (unwrap(Ty)->getTypeID()) {
121   default:
122     assert(false && "Unhandled TypeID.");
123   case Type::VoidTyID:
124     return LLVMVoidTypeKind;
125   case Type::FloatTyID:
126     return LLVMFloatTypeKind;
127   case Type::DoubleTyID:
128     return LLVMDoubleTypeKind;
129   case Type::X86_FP80TyID:
130     return LLVMX86_FP80TypeKind;
131   case Type::FP128TyID:
132     return LLVMFP128TypeKind;
133   case Type::PPC_FP128TyID:
134     return LLVMPPC_FP128TypeKind;
135   case Type::LabelTyID:
136     return LLVMLabelTypeKind;
137   case Type::MetadataTyID:
138     return LLVMMetadataTypeKind;
139   case Type::IntegerTyID:
140     return LLVMIntegerTypeKind;
141   case Type::FunctionTyID:
142     return LLVMFunctionTypeKind;
143   case Type::StructTyID:
144     return LLVMStructTypeKind;
145   case Type::ArrayTyID:
146     return LLVMArrayTypeKind;
147   case Type::PointerTyID:
148     return LLVMPointerTypeKind;
149   case Type::OpaqueTyID:
150     return LLVMOpaqueTypeKind;
151   case Type::VectorTyID:
152     return LLVMVectorTypeKind;
153   }
154 }
155
156 /*--.. Operations on integer types .........................................--*/
157
158 LLVMTypeRef LLVMInt1Type(void)  { return (LLVMTypeRef) Type::Int1Ty;  }
159 LLVMTypeRef LLVMInt8Type(void)  { return (LLVMTypeRef) Type::Int8Ty;  }
160 LLVMTypeRef LLVMInt16Type(void) { return (LLVMTypeRef) Type::Int16Ty; }
161 LLVMTypeRef LLVMInt32Type(void) { return (LLVMTypeRef) Type::Int32Ty; }
162 LLVMTypeRef LLVMInt64Type(void) { return (LLVMTypeRef) Type::Int64Ty; }
163
164 LLVMTypeRef LLVMIntType(unsigned NumBits) {
165   return wrap(IntegerType::get(NumBits));
166 }
167
168 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
169   return unwrap<IntegerType>(IntegerTy)->getBitWidth();
170 }
171
172 /*--.. Operations on real types ............................................--*/
173
174 LLVMTypeRef LLVMFloatType(void)    { return (LLVMTypeRef) Type::FloatTy;     }
175 LLVMTypeRef LLVMDoubleType(void)   { return (LLVMTypeRef) Type::DoubleTy;    }
176 LLVMTypeRef LLVMX86FP80Type(void)  { return (LLVMTypeRef) Type::X86_FP80Ty;  }
177 LLVMTypeRef LLVMFP128Type(void)    { return (LLVMTypeRef) Type::FP128Ty;     }
178 LLVMTypeRef LLVMPPCFP128Type(void) { return (LLVMTypeRef) Type::PPC_FP128Ty; }
179
180 /*--.. Operations on function types ........................................--*/
181
182 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
183                              LLVMTypeRef *ParamTypes, unsigned ParamCount,
184                              int IsVarArg) {
185   std::vector<const Type*> Tys;
186   for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
187     Tys.push_back(unwrap(*I));
188   
189   return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
190 }
191
192 int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
193   return unwrap<FunctionType>(FunctionTy)->isVarArg();
194 }
195
196 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
197   return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
198 }
199
200 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
201   return unwrap<FunctionType>(FunctionTy)->getNumParams();
202 }
203
204 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
205   FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
206   for (FunctionType::param_iterator I = Ty->param_begin(),
207                                     E = Ty->param_end(); I != E; ++I)
208     *Dest++ = wrap(*I);
209 }
210
211 /*--.. Operations on struct types ..........................................--*/
212
213 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
214                            unsigned ElementCount, int Packed) {
215   std::vector<const Type*> Tys;
216   for (LLVMTypeRef *I = ElementTypes,
217                    *E = ElementTypes + ElementCount; I != E; ++I)
218     Tys.push_back(unwrap(*I));
219   
220   return wrap(StructType::get(Tys, Packed != 0));
221 }
222
223 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
224   return unwrap<StructType>(StructTy)->getNumElements();
225 }
226
227 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
228   StructType *Ty = unwrap<StructType>(StructTy);
229   for (FunctionType::param_iterator I = Ty->element_begin(),
230                                     E = Ty->element_end(); I != E; ++I)
231     *Dest++ = wrap(*I);
232 }
233
234 int LLVMIsPackedStruct(LLVMTypeRef StructTy) {
235   return unwrap<StructType>(StructTy)->isPacked();
236 }
237
238 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
239
240 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
241   return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
242 }
243
244 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
245   return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
246 }
247
248 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
249   return wrap(VectorType::get(unwrap(ElementType), ElementCount));
250 }
251
252 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
253   return wrap(unwrap<SequentialType>(Ty)->getElementType());
254 }
255
256 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
257   return unwrap<ArrayType>(ArrayTy)->getNumElements();
258 }
259
260 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
261   return unwrap<PointerType>(PointerTy)->getAddressSpace();
262 }
263
264 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
265   return unwrap<VectorType>(VectorTy)->getNumElements();
266 }
267
268 /*--.. Operations on other types ...........................................--*/
269
270 LLVMTypeRef LLVMVoidType(void)  { return (LLVMTypeRef) Type::VoidTy;  }
271 LLVMTypeRef LLVMLabelType(void) { return (LLVMTypeRef) Type::LabelTy; }
272
273 LLVMTypeRef LLVMOpaqueType(void) {
274   return wrap(OpaqueType::get());
275 }
276
277 /*--.. Operations on type handles ..........................................--*/
278
279 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
280   return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
281 }
282
283 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
284   delete unwrap(TypeHandle);
285 }
286
287 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
288   return wrap(unwrap(TypeHandle)->get());
289 }
290
291 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
292   unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
293 }
294
295
296 /*===-- Operations on values ----------------------------------------------===*/
297
298 /*--.. Operations on all values ............................................--*/
299
300 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
301   return wrap(unwrap(Val)->getType());
302 }
303
304 const char *LLVMGetValueName(LLVMValueRef Val) {
305   return unwrap(Val)->getName().data();
306 }
307
308 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
309   unwrap(Val)->setName(Name);
310 }
311
312 void LLVMDumpValue(LLVMValueRef Val) {
313   unwrap(Val)->dump();
314 }
315
316
317 /*--.. Conversion functions ................................................--*/
318
319 #define LLVM_DEFINE_VALUE_CAST(name)                                       \
320   LLVMValueRef LLVMIsA##name(LLVMValueRef Val) {                           \
321     return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
322   }
323
324 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
325
326
327 /*--.. Operations on constants of any type .................................--*/
328
329 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
330   return wrap(getGlobalContext().getNullValue(unwrap(Ty)));
331 }
332
333 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
334   return wrap(getGlobalContext().getAllOnesValue(unwrap(Ty)));
335 }
336
337 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
338   return wrap(getGlobalContext().getUndef(unwrap(Ty)));
339 }
340
341 int LLVMIsConstant(LLVMValueRef Ty) {
342   return isa<Constant>(unwrap(Ty));
343 }
344
345 int LLVMIsNull(LLVMValueRef Val) {
346   if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
347     return C->isNullValue();
348   return false;
349 }
350
351 int LLVMIsUndef(LLVMValueRef Val) {
352   return isa<UndefValue>(unwrap(Val));
353 }
354
355 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
356   return
357       wrap(getGlobalContext().getConstantPointerNull(unwrap<PointerType>(Ty)));
358 }
359
360 /*--.. Operations on scalar constants ......................................--*/
361
362 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
363                           int SignExtend) {
364   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
365 }
366
367 static const fltSemantics &SemanticsForType(Type *Ty) {
368   assert(Ty->isFloatingPoint() && "Type is not floating point!");
369   if (Ty == Type::FloatTy)
370     return APFloat::IEEEsingle;
371   if (Ty == Type::DoubleTy)
372     return APFloat::IEEEdouble;
373   if (Ty == Type::X86_FP80Ty)
374     return APFloat::x87DoubleExtended;
375   if (Ty == Type::FP128Ty)
376     return APFloat::IEEEquad;
377   if (Ty == Type::PPC_FP128Ty)
378     return APFloat::PPCDoubleDouble;
379   return APFloat::Bogus;
380 }
381
382 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
383   APFloat APN(N);
384   bool ignored;
385   APN.convert(SemanticsForType(unwrap(RealTy)), APFloat::rmNearestTiesToEven,
386               &ignored);
387   return wrap(ConstantFP::get(getGlobalContext(), APN));
388 }
389
390 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
391   return wrap(ConstantFP::get(getGlobalContext(),
392                               APFloat(SemanticsForType(unwrap(RealTy)), Text)));
393 }
394
395 /*--.. Operations on composite constants ...................................--*/
396
397 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
398                              int DontNullTerminate) {
399   /* Inverted the sense of AddNull because ', 0)' is a
400      better mnemonic for null termination than ', 1)'. */
401   return wrap(ConstantArray::get(std::string(Str, Length),
402                                  DontNullTerminate == 0));
403 }
404
405 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
406                             LLVMValueRef *ConstantVals, unsigned Length) {
407   return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
408                                  unwrap<Constant>(ConstantVals, Length),
409                                  Length));
410 }
411
412 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
413                              int Packed) {
414   return wrap(ConstantStruct::get(unwrap<Constant>(ConstantVals, Count),
415                                   Count, Packed != 0));
416 }
417
418 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
419   return wrap(ConstantVector::get(
420                             unwrap<Constant>(ScalarConstantVals, Size), Size));
421 }
422
423 /*--.. Constant expressions ................................................--*/
424
425 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
426   return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
427 }
428
429 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
430   return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
431 }
432
433 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
434   return wrap(ConstantExpr::getNeg(
435                                                 unwrap<Constant>(ConstantVal)));
436 }
437
438 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
439   return wrap(ConstantExpr::getNot(
440                                                 unwrap<Constant>(ConstantVal)));
441 }
442
443 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
444   return wrap(ConstantExpr::getAdd(
445                                    unwrap<Constant>(LHSConstant),
446                                    unwrap<Constant>(RHSConstant)));
447 }
448
449 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
450   return wrap(ConstantExpr::getSub(
451                                    unwrap<Constant>(LHSConstant),
452                                    unwrap<Constant>(RHSConstant)));
453 }
454
455 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
456   return wrap(ConstantExpr::getMul(
457                                    unwrap<Constant>(LHSConstant),
458                                    unwrap<Constant>(RHSConstant)));
459 }
460
461 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
462   return wrap(ConstantExpr::getUDiv(
463                                     unwrap<Constant>(LHSConstant),
464                                     unwrap<Constant>(RHSConstant)));
465 }
466
467 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
468   return wrap(ConstantExpr::getSDiv(
469                                     unwrap<Constant>(LHSConstant),
470                                     unwrap<Constant>(RHSConstant)));
471 }
472
473 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
474   return wrap(ConstantExpr::getFDiv(
475                                     unwrap<Constant>(LHSConstant),
476                                     unwrap<Constant>(RHSConstant)));
477 }
478
479 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
480   return wrap(ConstantExpr::getURem(
481                                     unwrap<Constant>(LHSConstant),
482                                     unwrap<Constant>(RHSConstant)));
483 }
484
485 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
486   return wrap(ConstantExpr::getSRem(
487                                     unwrap<Constant>(LHSConstant),
488                                     unwrap<Constant>(RHSConstant)));
489 }
490
491 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
492   return wrap(ConstantExpr::getFRem(
493                                     unwrap<Constant>(LHSConstant),
494                                     unwrap<Constant>(RHSConstant)));
495 }
496
497 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
498   return wrap(ConstantExpr::getAnd(
499                                    unwrap<Constant>(LHSConstant),
500                                    unwrap<Constant>(RHSConstant)));
501 }
502
503 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
504   return wrap(ConstantExpr::getOr(
505                                   unwrap<Constant>(LHSConstant),
506                                   unwrap<Constant>(RHSConstant)));
507 }
508
509 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
510   return wrap(ConstantExpr::getXor(
511                                    unwrap<Constant>(LHSConstant),
512                                    unwrap<Constant>(RHSConstant)));
513 }
514
515 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
516                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
517   return wrap(ConstantExpr::getICmp(Predicate,
518                                     unwrap<Constant>(LHSConstant),
519                                     unwrap<Constant>(RHSConstant)));
520 }
521
522 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
523                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
524   return wrap(ConstantExpr::getFCmp(Predicate,
525                                     unwrap<Constant>(LHSConstant),
526                                     unwrap<Constant>(RHSConstant)));
527 }
528
529 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
530   return wrap(ConstantExpr::getShl(
531                                   unwrap<Constant>(LHSConstant),
532                                   unwrap<Constant>(RHSConstant)));
533 }
534
535 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
536   return wrap(ConstantExpr::getLShr(
537                                     unwrap<Constant>(LHSConstant),
538                                     unwrap<Constant>(RHSConstant)));
539 }
540
541 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
542   return wrap(ConstantExpr::getAShr(
543                                     unwrap<Constant>(LHSConstant),
544                                     unwrap<Constant>(RHSConstant)));
545 }
546
547 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
548                           LLVMValueRef *ConstantIndices, unsigned NumIndices) {
549   return wrap(ConstantExpr::getGetElementPtr(
550                                              unwrap<Constant>(ConstantVal),
551                                              unwrap<Constant>(ConstantIndices, 
552                                                               NumIndices),
553                                              NumIndices));
554 }
555
556 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
557   return wrap(ConstantExpr::getTrunc(
558                                      unwrap<Constant>(ConstantVal),
559                                      unwrap(ToType)));
560 }
561
562 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
563   return wrap(ConstantExpr::getSExt(
564                                     unwrap<Constant>(ConstantVal),
565                                     unwrap(ToType)));
566 }
567
568 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
569   return wrap(ConstantExpr::getZExt(
570                                     unwrap<Constant>(ConstantVal),
571                                     unwrap(ToType)));
572 }
573
574 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
575   return wrap(ConstantExpr::getFPTrunc(
576                                        unwrap<Constant>(ConstantVal),
577                                        unwrap(ToType)));
578 }
579
580 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
581   return wrap(ConstantExpr::getFPExtend(
582                                         unwrap<Constant>(ConstantVal),
583                                         unwrap(ToType)));
584 }
585
586 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
587   return wrap(ConstantExpr::getUIToFP(
588                                       unwrap<Constant>(ConstantVal),
589                                       unwrap(ToType)));
590 }
591
592 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
593   return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
594                                       unwrap(ToType)));
595 }
596
597 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
598   return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
599                                       unwrap(ToType)));
600 }
601
602 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
603   return wrap(ConstantExpr::getFPToSI(
604                                       unwrap<Constant>(ConstantVal),
605                                       unwrap(ToType)));
606 }
607
608 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
609   return wrap(ConstantExpr::getPtrToInt(
610                                         unwrap<Constant>(ConstantVal),
611                                         unwrap(ToType)));
612 }
613
614 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
615   return wrap(ConstantExpr::getIntToPtr(
616                                         unwrap<Constant>(ConstantVal),
617                                         unwrap(ToType)));
618 }
619
620 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
621   return wrap(ConstantExpr::getBitCast(
622                                        unwrap<Constant>(ConstantVal),
623                                        unwrap(ToType)));
624 }
625
626 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
627                              LLVMValueRef ConstantIfTrue,
628                              LLVMValueRef ConstantIfFalse) {
629   return wrap(ConstantExpr::getSelect(
630                                       unwrap<Constant>(ConstantCondition),
631                                       unwrap<Constant>(ConstantIfTrue),
632                                       unwrap<Constant>(ConstantIfFalse)));
633 }
634
635 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
636                                      LLVMValueRef IndexConstant) {
637   return wrap(ConstantExpr::getExtractElement(
638                                               unwrap<Constant>(VectorConstant),
639                                               unwrap<Constant>(IndexConstant)));
640 }
641
642 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
643                                     LLVMValueRef ElementValueConstant,
644                                     LLVMValueRef IndexConstant) {
645   return wrap(ConstantExpr::getInsertElement(
646                                          unwrap<Constant>(VectorConstant),
647                                          unwrap<Constant>(ElementValueConstant),
648                                              unwrap<Constant>(IndexConstant)));
649 }
650
651 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
652                                     LLVMValueRef VectorBConstant,
653                                     LLVMValueRef MaskConstant) {
654   return wrap(ConstantExpr::getShuffleVector(
655                                              unwrap<Constant>(VectorAConstant),
656                                              unwrap<Constant>(VectorBConstant),
657                                              unwrap<Constant>(MaskConstant)));
658 }
659
660 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
661                                    unsigned NumIdx) {
662   return wrap(ConstantExpr::getExtractValue(
663                                             unwrap<Constant>(AggConstant),
664                                             IdxList, NumIdx));
665 }
666
667 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
668                                   LLVMValueRef ElementValueConstant,
669                                   unsigned *IdxList, unsigned NumIdx) {
670   return wrap(ConstantExpr::getInsertValue(
671                                          unwrap<Constant>(AggConstant),
672                                          unwrap<Constant>(ElementValueConstant),
673                                            IdxList, NumIdx));
674 }
675
676 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString, 
677                                 const char *Constraints, int HasSideEffects) {
678   return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString, 
679                              Constraints, HasSideEffects));
680 }
681
682 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
683
684 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
685   return wrap(unwrap<GlobalValue>(Global)->getParent());
686 }
687
688 int LLVMIsDeclaration(LLVMValueRef Global) {
689   return unwrap<GlobalValue>(Global)->isDeclaration();
690 }
691
692 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
693   switch (unwrap<GlobalValue>(Global)->getLinkage()) {
694   default:
695     assert(false && "Unhandled Linkage Type.");
696   case GlobalValue::ExternalLinkage:
697     return LLVMExternalLinkage;
698   case GlobalValue::AvailableExternallyLinkage:
699     return LLVMAvailableExternallyLinkage;
700   case GlobalValue::LinkOnceAnyLinkage:
701     return LLVMLinkOnceAnyLinkage;
702   case GlobalValue::LinkOnceODRLinkage:
703     return LLVMLinkOnceODRLinkage;
704   case GlobalValue::WeakAnyLinkage:
705     return LLVMWeakAnyLinkage;
706   case GlobalValue::WeakODRLinkage:
707     return LLVMWeakODRLinkage;
708   case GlobalValue::AppendingLinkage:
709     return LLVMAppendingLinkage;
710   case GlobalValue::InternalLinkage:
711     return LLVMInternalLinkage;
712   case GlobalValue::PrivateLinkage:
713     return LLVMPrivateLinkage;
714   case GlobalValue::LinkerPrivateLinkage:
715     return LLVMLinkerPrivateLinkage;
716   case GlobalValue::DLLImportLinkage:
717     return LLVMDLLImportLinkage;
718   case GlobalValue::DLLExportLinkage:
719     return LLVMDLLExportLinkage;
720   case GlobalValue::ExternalWeakLinkage:
721     return LLVMExternalWeakLinkage;
722   case GlobalValue::GhostLinkage:
723     return LLVMGhostLinkage;
724   case GlobalValue::CommonLinkage:
725     return LLVMCommonLinkage;
726   }
727
728   // Should never get here.
729   return static_cast<LLVMLinkage>(0);
730 }
731
732 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
733   GlobalValue *GV = unwrap<GlobalValue>(Global);
734
735   switch (Linkage) {
736   default:
737     assert(false && "Unhandled Linkage Type.");
738   case LLVMExternalLinkage:
739     GV->setLinkage(GlobalValue::ExternalLinkage);
740     break;
741   case LLVMAvailableExternallyLinkage:
742     GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
743     break;
744   case LLVMLinkOnceAnyLinkage:
745     GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
746     break;
747   case LLVMLinkOnceODRLinkage:
748     GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
749     break;
750   case LLVMWeakAnyLinkage:
751     GV->setLinkage(GlobalValue::WeakAnyLinkage);
752     break;
753   case LLVMWeakODRLinkage:
754     GV->setLinkage(GlobalValue::WeakODRLinkage);
755     break;
756   case LLVMAppendingLinkage:
757     GV->setLinkage(GlobalValue::AppendingLinkage);
758     break;
759   case LLVMInternalLinkage:
760     GV->setLinkage(GlobalValue::InternalLinkage);
761     break;
762   case LLVMPrivateLinkage:
763     GV->setLinkage(GlobalValue::PrivateLinkage);
764     break;
765   case LLVMLinkerPrivateLinkage:
766     GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
767     break;
768   case LLVMDLLImportLinkage:
769     GV->setLinkage(GlobalValue::DLLImportLinkage);
770     break;
771   case LLVMDLLExportLinkage:
772     GV->setLinkage(GlobalValue::DLLExportLinkage);
773     break;
774   case LLVMExternalWeakLinkage:
775     GV->setLinkage(GlobalValue::ExternalWeakLinkage);
776     break;
777   case LLVMGhostLinkage:
778     GV->setLinkage(GlobalValue::GhostLinkage);
779     break;
780   case LLVMCommonLinkage:
781     GV->setLinkage(GlobalValue::CommonLinkage);
782     break;
783   }
784 }
785
786 const char *LLVMGetSection(LLVMValueRef Global) {
787   return unwrap<GlobalValue>(Global)->getSection().c_str();
788 }
789
790 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
791   unwrap<GlobalValue>(Global)->setSection(Section);
792 }
793
794 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
795   return static_cast<LLVMVisibility>(
796     unwrap<GlobalValue>(Global)->getVisibility());
797 }
798
799 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
800   unwrap<GlobalValue>(Global)
801     ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
802 }
803
804 unsigned LLVMGetAlignment(LLVMValueRef Global) {
805   return unwrap<GlobalValue>(Global)->getAlignment();
806 }
807
808 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
809   unwrap<GlobalValue>(Global)->setAlignment(Bytes);
810 }
811
812 /*--.. Operations on global variables ......................................--*/
813
814 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
815   return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
816                                  GlobalValue::ExternalLinkage, 0, Name));
817 }
818
819 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
820   return wrap(unwrap(M)->getNamedGlobal(Name));
821 }
822
823 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
824   Module *Mod = unwrap(M);
825   Module::global_iterator I = Mod->global_begin();
826   if (I == Mod->global_end())
827     return 0;
828   return wrap(I);
829 }
830
831 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
832   Module *Mod = unwrap(M);
833   Module::global_iterator I = Mod->global_end();
834   if (I == Mod->global_begin())
835     return 0;
836   return wrap(--I);
837 }
838
839 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
840   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
841   Module::global_iterator I = GV;
842   if (++I == GV->getParent()->global_end())
843     return 0;
844   return wrap(I);
845 }
846
847 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
848   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
849   Module::global_iterator I = GV;
850   if (I == GV->getParent()->global_begin())
851     return 0;
852   return wrap(--I);
853 }
854
855 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
856   unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
857 }
858
859 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
860   return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
861 }
862
863 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
864   unwrap<GlobalVariable>(GlobalVar)
865     ->setInitializer(unwrap<Constant>(ConstantVal));
866 }
867
868 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
869   return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
870 }
871
872 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
873   unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
874 }
875
876 int LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
877   return unwrap<GlobalVariable>(GlobalVar)->isConstant();
878 }
879
880 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, int IsConstant) {
881   unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
882 }
883
884 /*--.. Operations on aliases ......................................--*/
885
886 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
887                           const char *Name) {
888   return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
889                               unwrap<Constant>(Aliasee), unwrap (M)));
890 }
891
892 /*--.. Operations on functions .............................................--*/
893
894 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
895                              LLVMTypeRef FunctionTy) {
896   return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
897                                GlobalValue::ExternalLinkage, Name, unwrap(M)));
898 }
899
900 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
901   return wrap(unwrap(M)->getFunction(Name));
902 }
903
904 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
905   Module *Mod = unwrap(M);
906   Module::iterator I = Mod->begin();
907   if (I == Mod->end())
908     return 0;
909   return wrap(I);
910 }
911
912 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
913   Module *Mod = unwrap(M);
914   Module::iterator I = Mod->end();
915   if (I == Mod->begin())
916     return 0;
917   return wrap(--I);
918 }
919
920 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
921   Function *Func = unwrap<Function>(Fn);
922   Module::iterator I = Func;
923   if (++I == Func->getParent()->end())
924     return 0;
925   return wrap(I);
926 }
927
928 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
929   Function *Func = unwrap<Function>(Fn);
930   Module::iterator I = Func;
931   if (I == Func->getParent()->begin())
932     return 0;
933   return wrap(--I);
934 }
935
936 void LLVMDeleteFunction(LLVMValueRef Fn) {
937   unwrap<Function>(Fn)->eraseFromParent();
938 }
939
940 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
941   if (Function *F = dyn_cast<Function>(unwrap(Fn)))
942     return F->getIntrinsicID();
943   return 0;
944 }
945
946 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
947   return unwrap<Function>(Fn)->getCallingConv();
948 }
949
950 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
951   return unwrap<Function>(Fn)->setCallingConv(CC);
952 }
953
954 const char *LLVMGetGC(LLVMValueRef Fn) {
955   Function *F = unwrap<Function>(Fn);
956   return F->hasGC()? F->getGC() : 0;
957 }
958
959 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
960   Function *F = unwrap<Function>(Fn);
961   if (GC)
962     F->setGC(GC);
963   else
964     F->clearGC();
965 }
966
967 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
968   Function *Func = unwrap<Function>(Fn);
969   const AttrListPtr PAL = Func->getAttributes();
970   const AttrListPtr PALnew = PAL.addAttr(0, PA);
971   Func->setAttributes(PALnew);
972 }
973
974 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
975   Function *Func = unwrap<Function>(Fn);
976   const AttrListPtr PAL = Func->getAttributes();
977   const AttrListPtr PALnew = PAL.removeAttr(0, PA);
978   Func->setAttributes(PALnew);
979 }
980
981 /*--.. Operations on parameters ............................................--*/
982
983 unsigned LLVMCountParams(LLVMValueRef FnRef) {
984   // This function is strictly redundant to
985   //   LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
986   return unwrap<Function>(FnRef)->arg_size();
987 }
988
989 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
990   Function *Fn = unwrap<Function>(FnRef);
991   for (Function::arg_iterator I = Fn->arg_begin(),
992                               E = Fn->arg_end(); I != E; I++)
993     *ParamRefs++ = wrap(I);
994 }
995
996 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
997   Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
998   while (index --> 0)
999     AI++;
1000   return wrap(AI);
1001 }
1002
1003 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1004   return wrap(unwrap<Argument>(V)->getParent());
1005 }
1006
1007 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1008   Function *Func = unwrap<Function>(Fn);
1009   Function::arg_iterator I = Func->arg_begin();
1010   if (I == Func->arg_end())
1011     return 0;
1012   return wrap(I);
1013 }
1014
1015 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1016   Function *Func = unwrap<Function>(Fn);
1017   Function::arg_iterator I = Func->arg_end();
1018   if (I == Func->arg_begin())
1019     return 0;
1020   return wrap(--I);
1021 }
1022
1023 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1024   Argument *A = unwrap<Argument>(Arg);
1025   Function::arg_iterator I = A;
1026   if (++I == A->getParent()->arg_end())
1027     return 0;
1028   return wrap(I);
1029 }
1030
1031 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1032   Argument *A = unwrap<Argument>(Arg);
1033   Function::arg_iterator I = A;
1034   if (I == A->getParent()->arg_begin())
1035     return 0;
1036   return wrap(--I);
1037 }
1038
1039 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1040   unwrap<Argument>(Arg)->addAttr(PA);
1041 }
1042
1043 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1044   unwrap<Argument>(Arg)->removeAttr(PA);
1045 }
1046
1047 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1048   unwrap<Argument>(Arg)->addAttr(
1049           Attribute::constructAlignmentFromInt(align));
1050 }
1051
1052 /*--.. Operations on basic blocks ..........................................--*/
1053
1054 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1055   return wrap(static_cast<Value*>(unwrap(BB)));
1056 }
1057
1058 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
1059   return isa<BasicBlock>(unwrap(Val));
1060 }
1061
1062 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1063   return wrap(unwrap<BasicBlock>(Val));
1064 }
1065
1066 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1067   return wrap(unwrap(BB)->getParent());
1068 }
1069
1070 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1071   return unwrap<Function>(FnRef)->size();
1072 }
1073
1074 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1075   Function *Fn = unwrap<Function>(FnRef);
1076   for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1077     *BasicBlocksRefs++ = wrap(I);
1078 }
1079
1080 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1081   return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1082 }
1083
1084 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1085   Function *Func = unwrap<Function>(Fn);
1086   Function::iterator I = Func->begin();
1087   if (I == Func->end())
1088     return 0;
1089   return wrap(I);
1090 }
1091
1092 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1093   Function *Func = unwrap<Function>(Fn);
1094   Function::iterator I = Func->end();
1095   if (I == Func->begin())
1096     return 0;
1097   return wrap(--I);
1098 }
1099
1100 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1101   BasicBlock *Block = unwrap(BB);
1102   Function::iterator I = Block;
1103   if (++I == Block->getParent()->end())
1104     return 0;
1105   return wrap(I);
1106 }
1107
1108 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1109   BasicBlock *Block = unwrap(BB);
1110   Function::iterator I = Block;
1111   if (I == Block->getParent()->begin())
1112     return 0;
1113   return wrap(--I);
1114 }
1115
1116 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1117   return wrap(BasicBlock::Create(Name, unwrap<Function>(FnRef)));
1118 }
1119
1120 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
1121                                        const char *Name) {
1122   BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
1123   return wrap(BasicBlock::Create(Name, InsertBeforeBB->getParent(),
1124                                  InsertBeforeBB));
1125 }
1126
1127 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1128   unwrap(BBRef)->eraseFromParent();
1129 }
1130
1131 /*--.. Operations on instructions ..........................................--*/
1132
1133 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1134   return wrap(unwrap<Instruction>(Inst)->getParent());
1135 }
1136
1137 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1138   BasicBlock *Block = unwrap(BB);
1139   BasicBlock::iterator I = Block->begin();
1140   if (I == Block->end())
1141     return 0;
1142   return wrap(I);
1143 }
1144
1145 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1146   BasicBlock *Block = unwrap(BB);
1147   BasicBlock::iterator I = Block->end();
1148   if (I == Block->begin())
1149     return 0;
1150   return wrap(--I);
1151 }
1152
1153 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1154   Instruction *Instr = unwrap<Instruction>(Inst);
1155   BasicBlock::iterator I = Instr;
1156   if (++I == Instr->getParent()->end())
1157     return 0;
1158   return wrap(I);
1159 }
1160
1161 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1162   Instruction *Instr = unwrap<Instruction>(Inst);
1163   BasicBlock::iterator I = Instr;
1164   if (I == Instr->getParent()->begin())
1165     return 0;
1166   return wrap(--I);
1167 }
1168
1169 /*--.. Call and invoke instructions ........................................--*/
1170
1171 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1172   Value *V = unwrap(Instr);
1173   if (CallInst *CI = dyn_cast<CallInst>(V))
1174     return CI->getCallingConv();
1175   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1176     return II->getCallingConv();
1177   llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1178   return 0;
1179 }
1180
1181 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1182   Value *V = unwrap(Instr);
1183   if (CallInst *CI = dyn_cast<CallInst>(V))
1184     return CI->setCallingConv(CC);
1185   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1186     return II->setCallingConv(CC);
1187   llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1188 }
1189
1190 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index, 
1191                            LLVMAttribute PA) {
1192   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1193   Call.setAttributes(
1194     Call.getAttributes().addAttr(index, PA));
1195 }
1196
1197 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index, 
1198                               LLVMAttribute PA) {
1199   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1200   Call.setAttributes(
1201     Call.getAttributes().removeAttr(index, PA));
1202 }
1203
1204 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index, 
1205                                 unsigned align) {
1206   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1207   Call.setAttributes(
1208     Call.getAttributes().addAttr(index, 
1209         Attribute::constructAlignmentFromInt(align)));
1210 }
1211
1212 /*--.. Operations on call instructions (only) ..............................--*/
1213
1214 int LLVMIsTailCall(LLVMValueRef Call) {
1215   return unwrap<CallInst>(Call)->isTailCall();
1216 }
1217
1218 void LLVMSetTailCall(LLVMValueRef Call, int isTailCall) {
1219   unwrap<CallInst>(Call)->setTailCall(isTailCall);
1220 }
1221
1222 /*--.. Operations on phi nodes .............................................--*/
1223
1224 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1225                      LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1226   PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1227   for (unsigned I = 0; I != Count; ++I)
1228     PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1229 }
1230
1231 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1232   return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1233 }
1234
1235 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1236   return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1237 }
1238
1239 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1240   return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1241 }
1242
1243
1244 /*===-- Instruction builders ----------------------------------------------===*/
1245
1246 LLVMBuilderRef LLVMCreateBuilder(void) {
1247   return wrap(new IRBuilder<>(getGlobalContext()));
1248 }
1249
1250 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1251                          LLVMValueRef Instr) {
1252   BasicBlock *BB = unwrap(Block);
1253   Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1254   unwrap(Builder)->SetInsertPoint(BB, I);
1255 }
1256
1257 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1258   Instruction *I = unwrap<Instruction>(Instr);
1259   unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1260 }
1261
1262 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1263   BasicBlock *BB = unwrap(Block);
1264   unwrap(Builder)->SetInsertPoint(BB);
1265 }
1266
1267 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1268    return wrap(unwrap(Builder)->GetInsertBlock());
1269 }
1270
1271 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1272   unwrap(Builder)->ClearInsertionPoint ();
1273 }
1274
1275 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1276   unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1277 }
1278
1279 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1280   delete unwrap(Builder);
1281 }
1282
1283 /*--.. Instruction builders ................................................--*/
1284
1285 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1286   return wrap(unwrap(B)->CreateRetVoid());
1287 }
1288
1289 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1290   return wrap(unwrap(B)->CreateRet(unwrap(V)));
1291 }
1292
1293 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1294   return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1295 }
1296
1297 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1298                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1299   return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1300 }
1301
1302 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1303                              LLVMBasicBlockRef Else, unsigned NumCases) {
1304   return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1305 }
1306
1307 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1308                              LLVMValueRef *Args, unsigned NumArgs,
1309                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1310                              const char *Name) {
1311   return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1312                                       unwrap(Args), unwrap(Args) + NumArgs,
1313                                       Name));
1314 }
1315
1316 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1317   return wrap(unwrap(B)->CreateUnwind());
1318 }
1319
1320 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1321   return wrap(unwrap(B)->CreateUnreachable());
1322 }
1323
1324 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1325                  LLVMBasicBlockRef Dest) {
1326   unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1327 }
1328
1329 /*--.. Arithmetic ..........................................................--*/
1330
1331 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1332                           const char *Name) {
1333   return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1334 }
1335
1336 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1337                           const char *Name) {
1338   return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1339 }
1340
1341 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1342                           const char *Name) {
1343   return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1344 }
1345
1346 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1347                            const char *Name) {
1348   return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1349 }
1350
1351 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1352                            const char *Name) {
1353   return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1354 }
1355
1356 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1357                            const char *Name) {
1358   return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1359 }
1360
1361 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1362                            const char *Name) {
1363   return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1364 }
1365
1366 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1367                            const char *Name) {
1368   return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1369 }
1370
1371 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1372                            const char *Name) {
1373   return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1374 }
1375
1376 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1377                           const char *Name) {
1378   return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1379 }
1380
1381 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1382                            const char *Name) {
1383   return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1384 }
1385
1386 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1387                            const char *Name) {
1388   return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1389 }
1390
1391 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1392                           const char *Name) {
1393   return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1394 }
1395
1396 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1397                          const char *Name) {
1398   return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1399 }
1400
1401 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1402                           const char *Name) {
1403   return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1404 }
1405
1406 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1407   return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1408 }
1409
1410 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1411   return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1412 }
1413
1414 /*--.. Memory ..............................................................--*/
1415
1416 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1417                              const char *Name) {
1418   return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
1419 }
1420
1421 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1422                                   LLVMValueRef Val, const char *Name) {
1423   return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
1424 }
1425
1426 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1427                              const char *Name) {
1428   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1429 }
1430
1431 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1432                                   LLVMValueRef Val, const char *Name) {
1433   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1434 }
1435
1436 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1437   return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
1438 }
1439
1440
1441 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1442                            const char *Name) {
1443   return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1444 }
1445
1446 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val, 
1447                             LLVMValueRef PointerVal) {
1448   return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1449 }
1450
1451 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1452                           LLVMValueRef *Indices, unsigned NumIndices,
1453                           const char *Name) {
1454   return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1455                                    unwrap(Indices) + NumIndices, Name));
1456 }
1457
1458 /*--.. Casts ...............................................................--*/
1459
1460 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1461                             LLVMTypeRef DestTy, const char *Name) {
1462   return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1463 }
1464
1465 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1466                            LLVMTypeRef DestTy, const char *Name) {
1467   return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1468 }
1469
1470 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1471                            LLVMTypeRef DestTy, const char *Name) {
1472   return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1473 }
1474
1475 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1476                              LLVMTypeRef DestTy, const char *Name) {
1477   return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1478 }
1479
1480 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
1481                              LLVMTypeRef DestTy, const char *Name) {
1482   return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
1483 }
1484
1485 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1486                              LLVMTypeRef DestTy, const char *Name) {
1487   return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1488 }
1489
1490 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1491                              LLVMTypeRef DestTy, const char *Name) {
1492   return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1493 }
1494
1495 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1496                               LLVMTypeRef DestTy, const char *Name) {
1497   return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
1498 }
1499
1500 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
1501                             LLVMTypeRef DestTy, const char *Name) {
1502   return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
1503 }
1504
1505 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
1506                                LLVMTypeRef DestTy, const char *Name) {
1507   return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1508 }
1509
1510 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1511                                LLVMTypeRef DestTy, const char *Name) {
1512   return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1513 }
1514
1515 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1516                               LLVMTypeRef DestTy, const char *Name) {
1517   return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1518 }
1519
1520 /*--.. Comparisons .........................................................--*/
1521
1522 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
1523                            LLVMValueRef LHS, LLVMValueRef RHS,
1524                            const char *Name) {
1525   return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
1526                                     unwrap(LHS), unwrap(RHS), Name));
1527 }
1528
1529 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
1530                            LLVMValueRef LHS, LLVMValueRef RHS,
1531                            const char *Name) {
1532   return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
1533                                     unwrap(LHS), unwrap(RHS), Name));
1534 }
1535
1536 /*--.. Miscellaneous instructions ..........................................--*/
1537
1538 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
1539   return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
1540 }
1541
1542 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1543                            LLVMValueRef *Args, unsigned NumArgs,
1544                            const char *Name) {
1545   return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1546                                     unwrap(Args) + NumArgs, Name));
1547 }
1548
1549 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1550                              LLVMValueRef Then, LLVMValueRef Else,
1551                              const char *Name) {
1552   return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1553                                       Name));
1554 }
1555
1556 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1557                             LLVMTypeRef Ty, const char *Name) {
1558   return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1559 }
1560
1561 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1562                                       LLVMValueRef Index, const char *Name) {
1563   return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1564                                               Name));
1565 }
1566
1567 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1568                                     LLVMValueRef EltVal, LLVMValueRef Index,
1569                                     const char *Name) {
1570   return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1571                                              unwrap(Index), Name));
1572 }
1573
1574 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1575                                     LLVMValueRef V2, LLVMValueRef Mask,
1576                                     const char *Name) {
1577   return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1578                                              unwrap(Mask), Name));
1579 }
1580
1581 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1582                                    unsigned Index, const char *Name) {
1583   return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
1584 }
1585
1586 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1587                                   LLVMValueRef EltVal, unsigned Index,
1588                                   const char *Name) {
1589   return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
1590                                            Index, Name));
1591 }
1592
1593
1594 /*===-- Module providers --------------------------------------------------===*/
1595
1596 LLVMModuleProviderRef
1597 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
1598   return wrap(new ExistingModuleProvider(unwrap(M)));
1599 }
1600
1601 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
1602   delete unwrap(MP);
1603 }
1604
1605
1606 /*===-- Memory buffers ----------------------------------------------------===*/
1607
1608 int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
1609                                              LLVMMemoryBufferRef *OutMemBuf,
1610                                              char **OutMessage) {
1611   std::string Error;
1612   if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
1613     *OutMemBuf = wrap(MB);
1614     return 0;
1615   }
1616   
1617   *OutMessage = strdup(Error.c_str());
1618   return 1;
1619 }
1620
1621 int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
1622                                     char **OutMessage) {
1623   if (MemoryBuffer *MB = MemoryBuffer::getSTDIN()) {
1624     *OutMemBuf = wrap(MB);
1625     return 0;
1626   }
1627   
1628   *OutMessage = strdup("stdin is empty.");
1629   return 1;
1630 }
1631
1632 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
1633   delete unwrap(MemBuf);
1634 }