blob: bbfad651cc72c194f939029b661edc6aed7b3ac3 [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//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Decl nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "clang/AST/AST.h"
Chris Lattner53621a52007-06-13 20:44:40 +000016#include "llvm/Type.h"
Chris Lattner84915fa2007-06-02 04:16:21 +000017using namespace clang;
18using namespace CodeGen;
19
20
Chris Lattner1ad38f82007-06-09 01:20:56 +000021void CodeGenFunction::EmitDecl(const Decl &D) {
Chris Lattner1ad38f82007-06-09 01:20:56 +000022 switch (D.getKind()) {
Chris Lattner84915fa2007-06-02 04:16:21 +000023 default: assert(0 && "Unknown decl kind!");
24 case Decl::FileVariable:
25 assert(0 && "Should not see file-scope variables inside a function!");
26 case Decl::ParmVariable:
27 assert(0 && "Parmdecls should not be in declstmts!");
28 case Decl::Typedef: // typedef int X;
29 case Decl::Function: // void X();
30 case Decl::Struct: // struct X;
31 case Decl::Union: // union X;
32 case Decl::Class: // class X;
33 case Decl::Enum: // enum X;
34 // None of these decls require codegen support.
35 return;
36
37 case Decl::BlockVariable:
Chris Lattner1ad38f82007-06-09 01:20:56 +000038 return EmitBlockVarDecl(cast<BlockVarDecl>(D));
Chris Lattner84915fa2007-06-02 04:16:21 +000039 case Decl::EnumConstant:
Chris Lattner1ad38f82007-06-09 01:20:56 +000040 return EmitEnumConstantDecl(cast<EnumConstantDecl>(D));
Chris Lattner84915fa2007-06-02 04:16:21 +000041 }
42}
43
Chris Lattner84915fa2007-06-02 04:16:21 +000044void CodeGenFunction::EmitEnumConstantDecl(const EnumConstantDecl &D) {
45 assert(0 && "FIXME: Enum constant decls not implemented yet!");
46}
Chris Lattner03df1222007-06-02 04:53:11 +000047
48/// EmitBlockVarDecl - This method handles emission of any variable declaration
49/// inside a function, including static vars etc.
50void CodeGenFunction::EmitBlockVarDecl(const BlockVarDecl &D) {
51 switch (D.getStorageClass()) {
52 case VarDecl::Static:
53 assert(0 && "FIXME: local static vars not implemented yet");
54 case VarDecl::Extern:
55 assert(0 && "FIXME: should call up to codegenmodule");
56 default:
57 assert((D.getStorageClass() == VarDecl::None ||
58 D.getStorageClass() == VarDecl::Auto ||
59 D.getStorageClass() == VarDecl::Register) &&
60 "Unknown storage class");
61 return EmitLocalBlockVarDecl(D);
62 }
63}
64
65/// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a
66/// variable declaration with auto, register, or no storage class specifier.
67/// These turn into simple stack objects.
68void CodeGenFunction::EmitLocalBlockVarDecl(const BlockVarDecl &D) {
69 QualType Ty = D.getCanonicalType();
70
71 llvm::Value *DeclPtr;
Chris Lattner0e9d6222007-07-15 23:26:56 +000072 if (Ty->isConstantSizeType(getContext())) {
Chris Lattner03df1222007-06-02 04:53:11 +000073 // A normal fixed sized variable becomes an alloca in the entry block.
Chris Lattnerf033c142007-06-22 19:05:19 +000074 const llvm::Type *LTy = ConvertType(Ty);
Chris Lattner03df1222007-06-02 04:53:11 +000075 // TODO: Alignment
Chris Lattnere9a64532007-06-22 21:44:33 +000076 DeclPtr = CreateTempAlloca(LTy, D.getName());
Chris Lattner03df1222007-06-02 04:53:11 +000077 } else {
78 // TODO: Create a dynamic alloca.
79 assert(0 && "FIXME: Local VLAs not implemented yet");
80 }
81
82 llvm::Value *&DMEntry = LocalDeclMap[&D];
83 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
84 DMEntry = DeclPtr;
85
Chris Lattner07eb7332007-07-12 00:39:48 +000086 // If this local has an initializer, emit it now.
Chris Lattner2da04b32007-08-24 05:35:26 +000087 if (const Expr *Init = D.getInit()) {
Chris Lattnerb753f662007-08-26 07:29:23 +000088 if (!hasAggregateLLVMType(Init->getType())) {
89 llvm::Value *V = EmitScalarExpr(Init);
90 // FIXME: Handle volatile.
91 Builder.CreateStore(V, DeclPtr);
92 } else if (Init->getType()->isComplexType()) {
Chris Lattner9de95272007-08-26 05:13:54 +000093 EmitComplexExprIntoAddr(Init, DeclPtr);
94 } else {
Chris Lattnerb753f662007-08-26 07:29:23 +000095 // FIXME: Handle volatile.
96 EmitAggExpr(Init, DeclPtr, false);
Chris Lattner9de95272007-08-26 05:13:54 +000097 }
Chris Lattner2da04b32007-08-24 05:35:26 +000098 }
Chris Lattner03df1222007-06-02 04:53:11 +000099}
Chris Lattner53621a52007-06-13 20:44:40 +0000100
101/// Emit an alloca for the specified parameter and set up LocalDeclMap.
102void CodeGenFunction::EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg) {
103 QualType Ty = D.getCanonicalType();
104
105 llvm::Value *DeclPtr;
Chris Lattner0e9d6222007-07-15 23:26:56 +0000106 if (!Ty->isConstantSizeType(getContext())) {
Chris Lattner53621a52007-06-13 20:44:40 +0000107 // Variable sized values always are passed by-reference.
108 DeclPtr = Arg;
Chris Lattner53621a52007-06-13 20:44:40 +0000109 } else {
110 // A fixed sized first class variable becomes an alloca in the entry block.
Chris Lattnerf033c142007-06-22 19:05:19 +0000111 const llvm::Type *LTy = ConvertType(Ty);
Chris Lattner53621a52007-06-13 20:44:40 +0000112 if (LTy->isFirstClassType()) {
113 // TODO: Alignment
Chris Lattner23b7eb62007-06-15 23:05:46 +0000114 DeclPtr = new llvm::AllocaInst(LTy, 0, std::string(D.getName())+".addr",
115 AllocaInsertPt);
Chris Lattner53621a52007-06-13 20:44:40 +0000116
117 // Store the initial value into the alloca.
118 Builder.CreateStore(Arg, DeclPtr);
Chris Lattner53621a52007-06-13 20:44:40 +0000119 } else {
120 // Otherwise, if this is an aggregate, just use the input pointer.
Chris Lattner0fb84652007-06-22 18:57:44 +0000121 DeclPtr = Arg;
Chris Lattner53621a52007-06-13 20:44:40 +0000122 }
Chris Lattnerf39b03d2007-06-22 18:15:16 +0000123 Arg->setName(D.getName());
Chris Lattner53621a52007-06-13 20:44:40 +0000124 }
125
126 llvm::Value *&DMEntry = LocalDeclMap[&D];
127 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
128 DMEntry = DeclPtr;
129}
130