2007-06-16 08:12:05 +08:00
|
|
|
//===--- CodeGenTypes.cpp - Type translation for LLVM CodeGen -------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Chris Lattner and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This is the code that handles AST -> LLVM type lowering.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenTypes.h"
|
|
|
|
#include "clang/Basic/TargetInfo.h"
|
|
|
|
#include "clang/AST/AST.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2007-07-14 09:29:45 +08:00
|
|
|
CodeGenTypes::CodeGenTypes(ASTContext &Ctx)
|
|
|
|
: Context(Ctx), Target(Ctx.Target) {
|
|
|
|
}
|
2007-06-16 08:12:05 +08:00
|
|
|
|
|
|
|
/// ConvertType - Convert the specified type to its LLVM form.
|
2007-06-23 03:05:19 +08:00
|
|
|
const llvm::Type *CodeGenTypes::ConvertType(QualType T) {
|
2007-06-16 08:12:05 +08:00
|
|
|
// FIXME: Cache these, move the CodeGenModule, expand, etc.
|
|
|
|
const clang::Type &Ty = *T.getCanonicalType();
|
|
|
|
|
|
|
|
switch (Ty.getTypeClass()) {
|
|
|
|
case Type::Builtin: {
|
|
|
|
switch (cast<BuiltinType>(Ty).getKind()) {
|
|
|
|
case BuiltinType::Void:
|
|
|
|
// LLVM void type can only be used as the result of a function call. Just
|
|
|
|
// map to the same as char.
|
2007-07-14 09:29:45 +08:00
|
|
|
return llvm::IntegerType::get(8);
|
2007-06-16 08:12:05 +08:00
|
|
|
|
|
|
|
case BuiltinType::Bool:
|
|
|
|
// FIXME: This is very strange. We want scalars to be i1, but in memory
|
|
|
|
// they can be i1 or i32. Should the codegen handle this issue?
|
|
|
|
return llvm::Type::Int1Ty;
|
|
|
|
|
2007-07-14 09:29:45 +08:00
|
|
|
case BuiltinType::Char_S:
|
|
|
|
case BuiltinType::Char_U:
|
|
|
|
case BuiltinType::SChar:
|
|
|
|
case BuiltinType::UChar:
|
2007-06-16 08:12:05 +08:00
|
|
|
case BuiltinType::Short:
|
|
|
|
case BuiltinType::UShort:
|
|
|
|
case BuiltinType::Int:
|
|
|
|
case BuiltinType::UInt:
|
|
|
|
case BuiltinType::Long:
|
|
|
|
case BuiltinType::ULong:
|
|
|
|
case BuiltinType::LongLong:
|
|
|
|
case BuiltinType::ULongLong:
|
2007-07-14 09:29:45 +08:00
|
|
|
return llvm::IntegerType::get(Context.getTypeSize(T, SourceLocation()));
|
2007-06-16 08:12:05 +08:00
|
|
|
|
|
|
|
case BuiltinType::Float: return llvm::Type::FloatTy;
|
|
|
|
case BuiltinType::Double: return llvm::Type::DoubleTy;
|
|
|
|
case BuiltinType::LongDouble:
|
2007-06-23 02:15:26 +08:00
|
|
|
// FIXME: mapping long double onto double.
|
|
|
|
return llvm::Type::DoubleTy;
|
2007-06-16 08:12:05 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2007-06-23 04:56:16 +08:00
|
|
|
case Type::Complex: {
|
|
|
|
std::vector<const llvm::Type*> Elts;
|
|
|
|
Elts.push_back(ConvertType(cast<ComplexType>(Ty).getElementType()));
|
|
|
|
Elts.push_back(Elts[0]);
|
|
|
|
return llvm::StructType::get(Elts);
|
|
|
|
}
|
2007-06-16 08:12:05 +08:00
|
|
|
case Type::Pointer: {
|
|
|
|
const PointerType &P = cast<PointerType>(Ty);
|
2007-06-23 03:05:19 +08:00
|
|
|
return llvm::PointerType::get(ConvertType(P.getPointeeType()));
|
2007-06-16 08:12:05 +08:00
|
|
|
}
|
|
|
|
case Type::Reference: {
|
|
|
|
const ReferenceType &R = cast<ReferenceType>(Ty);
|
2007-06-23 03:05:19 +08:00
|
|
|
return llvm::PointerType::get(ConvertType(R.getReferenceeType()));
|
2007-06-16 08:12:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case Type::Array: {
|
|
|
|
const ArrayType &A = cast<ArrayType>(Ty);
|
|
|
|
assert(A.getSizeModifier() == ArrayType::Normal &&
|
|
|
|
A.getIndexTypeQualifier() == 0 &&
|
|
|
|
"FIXME: We only handle trivial array types so far!");
|
|
|
|
|
|
|
|
llvm::APSInt Size(32);
|
2007-07-16 07:26:56 +08:00
|
|
|
if (A.getSizeExpr() &&
|
|
|
|
A.getSizeExpr()->isIntegerConstantExpr(Size, Context)) {
|
2007-06-23 03:05:19 +08:00
|
|
|
const llvm::Type *EltTy = ConvertType(A.getElementType());
|
2007-06-16 08:12:05 +08:00
|
|
|
return llvm::ArrayType::get(EltTy, Size.getZExtValue());
|
|
|
|
} else {
|
|
|
|
assert(0 && "FIXME: VLAs not implemented yet!");
|
|
|
|
}
|
|
|
|
}
|
2007-07-19 13:13:51 +08:00
|
|
|
case Type::OCUVector:
|
2007-07-10 08:23:39 +08:00
|
|
|
case Type::Vector: {
|
|
|
|
const VectorType &VT = cast<VectorType>(Ty);
|
|
|
|
return llvm::VectorType::get(ConvertType(VT.getElementType()),
|
|
|
|
VT.getNumElements());
|
|
|
|
}
|
2007-06-16 08:12:05 +08:00
|
|
|
case Type::FunctionNoProto:
|
|
|
|
case Type::FunctionProto: {
|
|
|
|
const FunctionType &FP = cast<FunctionType>(Ty);
|
|
|
|
const llvm::Type *ResultType;
|
|
|
|
|
|
|
|
if (FP.getResultType()->isVoidType())
|
|
|
|
ResultType = llvm::Type::VoidTy; // Result of function uses llvm void.
|
|
|
|
else
|
2007-06-23 03:05:19 +08:00
|
|
|
ResultType = ConvertType(FP.getResultType());
|
2007-06-16 08:12:05 +08:00
|
|
|
|
|
|
|
// FIXME: Convert argument types.
|
|
|
|
bool isVarArg;
|
|
|
|
std::vector<const llvm::Type*> ArgTys;
|
2007-06-23 06:02:34 +08:00
|
|
|
|
|
|
|
// Struct return passes the struct byref.
|
2007-06-28 02:08:49 +08:00
|
|
|
if (!ResultType->isFirstClassType() && ResultType != llvm::Type::VoidTy) {
|
2007-06-23 06:02:34 +08:00
|
|
|
ArgTys.push_back(llvm::PointerType::get(ResultType));
|
|
|
|
ResultType = llvm::Type::VoidTy;
|
|
|
|
}
|
|
|
|
|
2007-06-16 08:12:05 +08:00
|
|
|
if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(&FP)) {
|
2007-06-23 03:05:19 +08:00
|
|
|
DecodeArgumentTypes(*FTP, ArgTys);
|
2007-06-16 08:12:05 +08:00
|
|
|
isVarArg = FTP->isVariadic();
|
|
|
|
} else {
|
|
|
|
isVarArg = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return llvm::FunctionType::get(ResultType, ArgTys, isVarArg, 0);
|
|
|
|
}
|
|
|
|
case Type::TypeName:
|
|
|
|
case Type::Tagged:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: implement.
|
|
|
|
return llvm::OpaqueType::get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenTypes::DecodeArgumentTypes(const FunctionTypeProto &FTP,
|
2007-06-23 03:05:19 +08:00
|
|
|
std::vector<const llvm::Type*> &ArgTys) {
|
2007-06-16 08:12:05 +08:00
|
|
|
for (unsigned i = 0, e = FTP.getNumArgs(); i != e; ++i) {
|
2007-06-23 03:05:19 +08:00
|
|
|
const llvm::Type *Ty = ConvertType(FTP.getArgType(i));
|
2007-06-16 08:12:05 +08:00
|
|
|
if (Ty->isFirstClassType())
|
|
|
|
ArgTys.push_back(Ty);
|
|
|
|
else
|
|
|
|
ArgTys.push_back(llvm::PointerType::get(Ty));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|