2007-06-02 12:16:21 +08:00
|
|
|
//===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
|
|
|
|
//
|
|
|
|
// 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 contains code to emit Decl nodes as LLVM code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenFunction.h"
|
|
|
|
#include "clang/AST/AST.h"
|
2007-06-14 04:44:40 +08:00
|
|
|
#include "llvm/Type.h"
|
2007-06-02 12:16:21 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
|
|
|
|
2007-06-09 09:20:56 +08:00
|
|
|
void CodeGenFunction::EmitDecl(const Decl &D) {
|
|
|
|
switch (D.getKind()) {
|
2007-06-02 12:16:21 +08:00
|
|
|
default: assert(0 && "Unknown decl kind!");
|
|
|
|
case Decl::FileVariable:
|
|
|
|
assert(0 && "Should not see file-scope variables inside a function!");
|
|
|
|
case Decl::ParmVariable:
|
|
|
|
assert(0 && "Parmdecls should not be in declstmts!");
|
|
|
|
case Decl::Typedef: // typedef int X;
|
|
|
|
case Decl::Function: // void X();
|
|
|
|
case Decl::Struct: // struct X;
|
|
|
|
case Decl::Union: // union X;
|
|
|
|
case Decl::Class: // class X;
|
|
|
|
case Decl::Enum: // enum X;
|
|
|
|
// None of these decls require codegen support.
|
|
|
|
return;
|
|
|
|
|
|
|
|
case Decl::BlockVariable:
|
2007-06-09 09:20:56 +08:00
|
|
|
return EmitBlockVarDecl(cast<BlockVarDecl>(D));
|
2007-06-02 12:16:21 +08:00
|
|
|
case Decl::EnumConstant:
|
2007-06-09 09:20:56 +08:00
|
|
|
return EmitEnumConstantDecl(cast<EnumConstantDecl>(D));
|
2007-06-02 12:16:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-02 12:53:11 +08:00
|
|
|
void CodeGenFunction::EmitEnumConstantDecl(const EnumConstantDecl &D) {
|
|
|
|
assert(0 && "FIXME: Enum constant decls not implemented yet!");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// EmitBlockVarDecl - This method handles emission of any variable declaration
|
|
|
|
/// inside a function, including static vars etc.
|
2007-06-02 12:16:21 +08:00
|
|
|
void CodeGenFunction::EmitBlockVarDecl(const BlockVarDecl &D) {
|
2007-06-02 12:53:11 +08:00
|
|
|
switch (D.getStorageClass()) {
|
|
|
|
case VarDecl::Static:
|
|
|
|
assert(0 && "FIXME: local static vars not implemented yet");
|
|
|
|
case VarDecl::Extern:
|
|
|
|
assert(0 && "FIXME: should call up to codegenmodule");
|
|
|
|
default:
|
|
|
|
assert((D.getStorageClass() == VarDecl::None ||
|
|
|
|
D.getStorageClass() == VarDecl::Auto ||
|
|
|
|
D.getStorageClass() == VarDecl::Register) &&
|
|
|
|
"Unknown storage class");
|
|
|
|
return EmitLocalBlockVarDecl(D);
|
|
|
|
}
|
2007-06-02 12:16:21 +08:00
|
|
|
}
|
|
|
|
|
2007-06-02 12:53:11 +08:00
|
|
|
/// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a
|
|
|
|
/// variable declaration with auto, register, or no storage class specifier.
|
|
|
|
/// These turn into simple stack objects.
|
|
|
|
void CodeGenFunction::EmitLocalBlockVarDecl(const BlockVarDecl &D) {
|
|
|
|
QualType Ty = D.getCanonicalType();
|
|
|
|
|
|
|
|
llvm::Value *DeclPtr;
|
2007-07-16 07:26:56 +08:00
|
|
|
if (Ty->isConstantSizeType(getContext())) {
|
2007-06-02 12:53:11 +08:00
|
|
|
// A normal fixed sized variable becomes an alloca in the entry block.
|
2007-06-23 03:05:19 +08:00
|
|
|
const llvm::Type *LTy = ConvertType(Ty);
|
2007-06-02 12:53:11 +08:00
|
|
|
// TODO: Alignment
|
2007-06-23 05:44:33 +08:00
|
|
|
DeclPtr = CreateTempAlloca(LTy, D.getName());
|
2007-06-02 12:53:11 +08:00
|
|
|
} else {
|
|
|
|
// TODO: Create a dynamic alloca.
|
|
|
|
assert(0 && "FIXME: Local VLAs not implemented yet");
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *&DMEntry = LocalDeclMap[&D];
|
|
|
|
assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
|
|
|
|
DMEntry = DeclPtr;
|
|
|
|
|
2007-07-12 08:39:48 +08:00
|
|
|
// If this local has an initializer, emit it now.
|
2007-08-24 13:35:26 +08:00
|
|
|
if (const Expr *Init = D.getInit()) {
|
2007-08-26 15:29:23 +08:00
|
|
|
if (!hasAggregateLLVMType(Init->getType())) {
|
|
|
|
llvm::Value *V = EmitScalarExpr(Init);
|
2007-08-26 15:30:49 +08:00
|
|
|
Builder.CreateStore(V, DeclPtr, D.getType().isVolatileQualified());
|
2007-08-26 15:29:23 +08:00
|
|
|
} else if (Init->getType()->isComplexType()) {
|
2007-08-27 00:22:13 +08:00
|
|
|
EmitComplexExprIntoAddr(Init, DeclPtr, D.getType().isVolatileQualified());
|
2007-08-26 13:13:54 +08:00
|
|
|
} else {
|
2007-08-26 15:30:49 +08:00
|
|
|
EmitAggExpr(Init, DeclPtr, D.getType().isVolatileQualified());
|
2007-08-26 13:13:54 +08:00
|
|
|
}
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
2007-06-02 12:16:21 +08:00
|
|
|
}
|
2007-06-14 04:44:40 +08:00
|
|
|
|
|
|
|
/// Emit an alloca for the specified parameter and set up LocalDeclMap.
|
|
|
|
void CodeGenFunction::EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg) {
|
|
|
|
QualType Ty = D.getCanonicalType();
|
|
|
|
|
|
|
|
llvm::Value *DeclPtr;
|
2007-07-16 07:26:56 +08:00
|
|
|
if (!Ty->isConstantSizeType(getContext())) {
|
2007-06-14 04:44:40 +08:00
|
|
|
// Variable sized values always are passed by-reference.
|
|
|
|
DeclPtr = Arg;
|
|
|
|
} else {
|
|
|
|
// A fixed sized first class variable becomes an alloca in the entry block.
|
2007-06-23 03:05:19 +08:00
|
|
|
const llvm::Type *LTy = ConvertType(Ty);
|
2007-06-14 04:44:40 +08:00
|
|
|
if (LTy->isFirstClassType()) {
|
|
|
|
// TODO: Alignment
|
2007-06-16 07:05:46 +08:00
|
|
|
DeclPtr = new llvm::AllocaInst(LTy, 0, std::string(D.getName())+".addr",
|
|
|
|
AllocaInsertPt);
|
2007-06-14 04:44:40 +08:00
|
|
|
|
|
|
|
// Store the initial value into the alloca.
|
|
|
|
Builder.CreateStore(Arg, DeclPtr);
|
|
|
|
} else {
|
|
|
|
// Otherwise, if this is an aggregate, just use the input pointer.
|
2007-06-23 02:57:44 +08:00
|
|
|
DeclPtr = Arg;
|
2007-06-14 04:44:40 +08:00
|
|
|
}
|
2007-06-23 02:15:16 +08:00
|
|
|
Arg->setName(D.getName());
|
2007-06-14 04:44:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *&DMEntry = LocalDeclMap[&D];
|
|
|
|
assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
|
|
|
|
DMEntry = DeclPtr;
|
|
|
|
}
|
|
|
|
|