//===-- Core.cpp ----------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by Gordon Henriksen and is distributed under the
// University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the C bindings for libLLVMCore.a, which implements
// the LLVM intermediate representation.
//
//===----------------------------------------------------------------------===//
#include "llvm-c/Core.h"
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/GlobalVariable.h"
#include "llvm/TypeSymbolTable.h"
#include <cassert>
using namespace llvm;
/*===-- Operations on modules ---------------------------------------------===*/
LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
return wrap(new Module(ModuleID));
}
void LLVMDisposeModule(LLVMModuleRef M) {
delete unwrap(M);
}
int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
return unwrap(M)->addTypeName(Name, unwrap(Ty));
}
void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
std::string N(Name);
TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
for (TypeSymbolTable::iterator I = TST.begin(), E = TST.end(); I != E; ++I)
if (I->first == N)
TST.remove(I);
}
/*===-- Operations on types -----------------------------------------------===*/
/*--.. Operations on all types (mostly) ....................................--*/
LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
return static_cast<LLVMTypeKind>(unwrap(Ty)->getTypeID());
}
void LLVMRefineAbstractType(LLVMTypeRef AbstractType, LLVMTypeRef ConcreteType){
DerivedType *Ty = unwrap<DerivedType>(AbstractType);
Ty->refineAbstractTypeTo(unwrap(ConcreteType));
}
/*--.. Operations on integer types .........................................--*/
LLVMTypeRef LLVMInt1Type() { return (LLVMTypeRef) Type::Int1Ty; }
LLVMTypeRef LLVMInt8Type() { return (LLVMTypeRef) Type::Int8Ty; }
LLVMTypeRef LLVMInt16Type() { return (LLVMTypeRef) Type::Int16Ty; }
LLVMTypeRef LLVMInt32Type() { return (LLVMTypeRef) Type::Int32Ty; }
LLVMTypeRef LLVMInt64Type() { return (LLVMTypeRef) Type::Int64Ty; }
LLVMTypeRef LLVMCreateIntType(unsigned NumBits) {
return wrap(IntegerType::get(NumBits));
}
unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
return unwrap<IntegerType>(IntegerTy)->getBitWidth();
}
/*--.. Operations on real types ............................................--*/
LLVMTypeRef LLVMFloatType() { return (LLVMTypeRef) Type::FloatTy; }
LLVMTypeRef LLVMDoubleType() { return (LLVMTypeRef) Type::DoubleTy; }
LLVMTypeRef LLVMX86FP80Type() { return (LLVMTypeRef) Type::X86_FP80Ty; }
LLVMTypeRef LLVMFP128Type() { return (LLVMTypeRef) Type::FP128Ty; }
LLVMTypeRef LLVMPPCFP128Type() { return (LLVMTypeRef) Type::PPC_FP128Ty; }
/*--.. Operations on function types ........................................--*/
LLVMTypeRef LLVMCreateFunctionType(LLVMTypeRef ReturnType,
LLVMTypeRef *ParamTypes, unsigned ParamCount,
int IsVarArg) {
std::vector<const Type*> Tys;
for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
Tys.push_back(unwrap(*I));
return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
}
int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
return unwrap<FunctionType>(FunctionTy)->isVarArg();
}
LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
}
unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
return unwrap<FunctionType>(FunctionTy)->getNumParams();
}
void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
for (FunctionType::param_iterator I = Ty->param_begin(),
E = Ty->param_end(); I != E; ++I)
*Dest++ = wrap(*I);
}
/*--.. Operations on struct types ..........................................--*/
LLVMTypeRef LLVMCreateStructType(LLVMTypeRef *ElementTypes,
unsigned ElementCount,