blob: 23c707d8c448cff8f5e40900c3196b2e184e6c3e [file] [log] [blame]
Chris Lattner84915fa2007-06-02 04:16:21 +00001//===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner84915fa2007-06-02 04:16:21 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Decl nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
Anders Carlssonf94cd1f2007-10-17 00:52:43 +000015#include "CodeGenModule.h"
Chris Lattner84915fa2007-06-02 04:16:21 +000016#include "clang/AST/AST.h"
Anders Carlssonf94cd1f2007-10-17 00:52:43 +000017#include "llvm/GlobalVariable.h"
Chris Lattner53621a52007-06-13 20:44:40 +000018#include "llvm/Type.h"
Chris Lattner84915fa2007-06-02 04:16:21 +000019using namespace clang;
20using namespace CodeGen;
21
22
Chris Lattner1ad38f82007-06-09 01:20:56 +000023void CodeGenFunction::EmitDecl(const Decl &D) {
Chris Lattner1ad38f82007-06-09 01:20:56 +000024 switch (D.getKind()) {
Chris Lattner84915fa2007-06-02 04:16:21 +000025 default: assert(0 && "Unknown decl kind!");
Chris Lattnerba9dddb2007-10-08 21:37:32 +000026 case Decl::FileVar:
Chris Lattner84915fa2007-06-02 04:16:21 +000027 assert(0 && "Should not see file-scope variables inside a function!");
Chris Lattnerba9dddb2007-10-08 21:37:32 +000028 case Decl::ParmVar:
Chris Lattner84915fa2007-06-02 04:16:21 +000029 assert(0 && "Parmdecls should not be in declstmts!");
30 case Decl::Typedef: // typedef int X;
31 case Decl::Function: // void X();
32 case Decl::Struct: // struct X;
33 case Decl::Union: // union X;
34 case Decl::Class: // class X;
35 case Decl::Enum: // enum X;
36 // None of these decls require codegen support.
37 return;
38
Chris Lattnerba9dddb2007-10-08 21:37:32 +000039 case Decl::BlockVar:
Chris Lattner1ad38f82007-06-09 01:20:56 +000040 return EmitBlockVarDecl(cast<BlockVarDecl>(D));
Chris Lattner84915fa2007-06-02 04:16:21 +000041 case Decl::EnumConstant:
Chris Lattner1ad38f82007-06-09 01:20:56 +000042 return EmitEnumConstantDecl(cast<EnumConstantDecl>(D));
Chris Lattner84915fa2007-06-02 04:16:21 +000043 }
44}
45
Chris Lattner84915fa2007-06-02 04:16:21 +000046void CodeGenFunction::EmitEnumConstantDecl(const EnumConstantDecl &D) {
47 assert(0 && "FIXME: Enum constant decls not implemented yet!");
48}
Chris Lattner03df1222007-06-02 04:53:11 +000049
50/// EmitBlockVarDecl - This method handles emission of any variable declaration
51/// inside a function, including static vars etc.
52void CodeGenFunction::EmitBlockVarDecl(const BlockVarDecl &D) {
53 switch (D.getStorageClass()) {
54 case VarDecl::Static:
Anders Carlssonf94cd1f2007-10-17 00:52:43 +000055 return EmitStaticBlockVarDecl(D);
Chris Lattner03df1222007-06-02 04:53:11 +000056 case VarDecl::Extern:
57 assert(0 && "FIXME: should call up to codegenmodule");
58 default:
59 assert((D.getStorageClass() == VarDecl::None ||
60 D.getStorageClass() == VarDecl::Auto ||
61 D.getStorageClass() == VarDecl::Register) &&
62 "Unknown storage class");
63 return EmitLocalBlockVarDecl(D);
64 }
65}
66
Anders Carlssonf94cd1f2007-10-17 00:52:43 +000067void CodeGenFunction::EmitStaticBlockVarDecl(const BlockVarDecl &D) {
68 QualType Ty = D.getCanonicalType();
69 assert(Ty->isConstantSizeType(getContext()) && "VLAs can't be static");
70
71 llvm::Value *&DMEntry = LocalDeclMap[&D];
72 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
73
Chris Lattner41a1ef02008-01-09 18:47:25 +000074 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
Anders Carlssonf94cd1f2007-10-17 00:52:43 +000075 llvm::Constant *Init = 0;
76 if (D.getInit() == 0) {
77 Init = llvm::Constant::getNullValue(LTy);
Oliver Huntaefc8fd2007-12-02 00:11:25 +000078 } else {
79 Init = CGM.EmitGlobalInit(D.getInit());
Devang Patelffdb07c2007-10-26 17:50:58 +000080 }
81
Oliver Huntaefc8fd2007-12-02 00:11:25 +000082 assert(Init && "Unable to create initialiser for static decl");
Anders Carlssonf94cd1f2007-10-17 00:52:43 +000083
84 DMEntry =
85 new llvm::GlobalVariable(LTy, false,
86 llvm::GlobalValue::InternalLinkage,
87 Init, D.getName(), &CGM.getModule());
88
89}
90
Chris Lattner03df1222007-06-02 04:53:11 +000091/// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a
92/// variable declaration with auto, register, or no storage class specifier.
93/// These turn into simple stack objects.
94void CodeGenFunction::EmitLocalBlockVarDecl(const BlockVarDecl &D) {
95 QualType Ty = D.getCanonicalType();
96
97 llvm::Value *DeclPtr;
Chris Lattner0e9d6222007-07-15 23:26:56 +000098 if (Ty->isConstantSizeType(getContext())) {
Chris Lattner03df1222007-06-02 04:53:11 +000099 // A normal fixed sized variable becomes an alloca in the entry block.
Chris Lattnerf033c142007-06-22 19:05:19 +0000100 const llvm::Type *LTy = ConvertType(Ty);
Chris Lattner03df1222007-06-02 04:53:11 +0000101 // TODO: Alignment
Chris Lattnere9a64532007-06-22 21:44:33 +0000102 DeclPtr = CreateTempAlloca(LTy, D.getName());
Chris Lattner03df1222007-06-02 04:53:11 +0000103 } else {
104 // TODO: Create a dynamic alloca.
105 assert(0 && "FIXME: Local VLAs not implemented yet");
106 }
107
108 llvm::Value *&DMEntry = LocalDeclMap[&D];
109 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
110 DMEntry = DeclPtr;
111
Chris Lattner07eb7332007-07-12 00:39:48 +0000112 // If this local has an initializer, emit it now.
Chris Lattner2da04b32007-08-24 05:35:26 +0000113 if (const Expr *Init = D.getInit()) {
Chris Lattnerb753f662007-08-26 07:29:23 +0000114 if (!hasAggregateLLVMType(Init->getType())) {
115 llvm::Value *V = EmitScalarExpr(Init);
Chris Lattnerf6dcc9d2007-08-26 07:30:49 +0000116 Builder.CreateStore(V, DeclPtr, D.getType().isVolatileQualified());
Chris Lattnerb753f662007-08-26 07:29:23 +0000117 } else if (Init->getType()->isComplexType()) {
Chris Lattnerb84bb952007-08-26 16:22:13 +0000118 EmitComplexExprIntoAddr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9de95272007-08-26 05:13:54 +0000119 } else {
Chris Lattnerf6dcc9d2007-08-26 07:30:49 +0000120 EmitAggExpr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9de95272007-08-26 05:13:54 +0000121 }
Chris Lattner2da04b32007-08-24 05:35:26 +0000122 }
Chris Lattner03df1222007-06-02 04:53:11 +0000123}
Chris Lattner53621a52007-06-13 20:44:40 +0000124
125/// Emit an alloca for the specified parameter and set up LocalDeclMap.
126void CodeGenFunction::EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg) {
127 QualType Ty = D.getCanonicalType();
128
129 llvm::Value *DeclPtr;
Chris Lattner0e9d6222007-07-15 23:26:56 +0000130 if (!Ty->isConstantSizeType(getContext())) {
Chris Lattner53621a52007-06-13 20:44:40 +0000131 // Variable sized values always are passed by-reference.
132 DeclPtr = Arg;
Chris Lattner53621a52007-06-13 20:44:40 +0000133 } else {
134 // A fixed sized first class variable becomes an alloca in the entry block.
Chris Lattnerf033c142007-06-22 19:05:19 +0000135 const llvm::Type *LTy = ConvertType(Ty);
Chris Lattner53621a52007-06-13 20:44:40 +0000136 if (LTy->isFirstClassType()) {
137 // TODO: Alignment
Chris Lattner23b7eb62007-06-15 23:05:46 +0000138 DeclPtr = new llvm::AllocaInst(LTy, 0, std::string(D.getName())+".addr",
139 AllocaInsertPt);
Chris Lattner53621a52007-06-13 20:44:40 +0000140
141 // Store the initial value into the alloca.
142 Builder.CreateStore(Arg, DeclPtr);
Chris Lattner53621a52007-06-13 20:44:40 +0000143 } else {
144 // Otherwise, if this is an aggregate, just use the input pointer.
Chris Lattner0fb84652007-06-22 18:57:44 +0000145 DeclPtr = Arg;
Chris Lattner53621a52007-06-13 20:44:40 +0000146 }
Chris Lattnerf39b03d2007-06-22 18:15:16 +0000147 Arg->setName(D.getName());
Chris Lattner53621a52007-06-13 20:44:40 +0000148 }
149
150 llvm::Value *&DMEntry = LocalDeclMap[&D];
151 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
152 DMEntry = DeclPtr;
153}
154