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