blob: 108538bb1fbaa4f3c2c87d57f7db4a528b8413e1 [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 Lattner84915fa2007-06-02 04:16:21 +000022
Chris Lattner1ad38f82007-06-09 01:20:56 +000023 switch (D.getKind()) {
Chris Lattner84915fa2007-06-02 04:16:21 +000024 default: assert(0 && "Unknown decl kind!");
25 case Decl::FileVariable:
26 assert(0 && "Should not see file-scope variables inside a function!");
27 case Decl::ParmVariable:
28 assert(0 && "Parmdecls should not be in declstmts!");
29 case Decl::Typedef: // typedef int X;
30 case Decl::Function: // void X();
31 case Decl::Struct: // struct X;
32 case Decl::Union: // union X;
33 case Decl::Class: // class X;
34 case Decl::Enum: // enum X;
35 // None of these decls require codegen support.
36 return;
37
38 case Decl::BlockVariable:
Chris Lattner1ad38f82007-06-09 01:20:56 +000039 return EmitBlockVarDecl(cast<BlockVarDecl>(D));
Chris Lattner84915fa2007-06-02 04:16:21 +000040 case Decl::EnumConstant:
Chris Lattner1ad38f82007-06-09 01:20:56 +000041 return EmitEnumConstantDecl(cast<EnumConstantDecl>(D));
Chris Lattner84915fa2007-06-02 04:16:21 +000042 }
43}
44
Chris Lattner84915fa2007-06-02 04:16:21 +000045void CodeGenFunction::EmitEnumConstantDecl(const EnumConstantDecl &D) {
46 assert(0 && "FIXME: Enum constant decls not implemented yet!");
47}
Chris Lattner03df1222007-06-02 04:53:11 +000048
49/// EmitBlockVarDecl - This method handles emission of any variable declaration
50/// inside a function, including static vars etc.
51void CodeGenFunction::EmitBlockVarDecl(const BlockVarDecl &D) {
52 switch (D.getStorageClass()) {
53 case VarDecl::Static:
54 assert(0 && "FIXME: local static vars not implemented yet");
55 case VarDecl::Extern:
56 assert(0 && "FIXME: should call up to codegenmodule");
57 default:
58 assert((D.getStorageClass() == VarDecl::None ||
59 D.getStorageClass() == VarDecl::Auto ||
60 D.getStorageClass() == VarDecl::Register) &&
61 "Unknown storage class");
62 return EmitLocalBlockVarDecl(D);
63 }
64}
65
66/// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a
67/// variable declaration with auto, register, or no storage class specifier.
68/// These turn into simple stack objects.
69void CodeGenFunction::EmitLocalBlockVarDecl(const BlockVarDecl &D) {
70 QualType Ty = D.getCanonicalType();
71
72 llvm::Value *DeclPtr;
73 if (Ty->isConstantSizeType()) {
74 // A normal fixed sized variable becomes an alloca in the entry block.
75 const llvm::Type *LTy = ConvertType(Ty, D.getLocation());
76 // TODO: Alignment
Chris Lattner23b7eb62007-06-15 23:05:46 +000077 DeclPtr = new llvm::AllocaInst(LTy, 0, D.getName(), AllocaInsertPt);
Chris Lattner03df1222007-06-02 04:53:11 +000078 } else {
79 // TODO: Create a dynamic alloca.
80 assert(0 && "FIXME: Local VLAs not implemented yet");
81 }
82
83 llvm::Value *&DMEntry = LocalDeclMap[&D];
84 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
85 DMEntry = DeclPtr;
86
87 // FIXME: Evaluate initializer.
88}
Chris Lattner53621a52007-06-13 20:44:40 +000089
90/// Emit an alloca for the specified parameter and set up LocalDeclMap.
91void CodeGenFunction::EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg) {
92 QualType Ty = D.getCanonicalType();
93
94 llvm::Value *DeclPtr;
95 if (!Ty->isConstantSizeType()) {
96 // Variable sized values always are passed by-reference.
97 DeclPtr = Arg;
98 Arg->setName(DeclPtr->getName());
99 } else {
100 // A fixed sized first class variable becomes an alloca in the entry block.
101 const llvm::Type *LTy = ConvertType(Ty, D.getLocation());
102 if (LTy->isFirstClassType()) {
103 // TODO: Alignment
Chris Lattner23b7eb62007-06-15 23:05:46 +0000104 DeclPtr = new llvm::AllocaInst(LTy, 0, std::string(D.getName())+".addr",
105 AllocaInsertPt);
Chris Lattner53621a52007-06-13 20:44:40 +0000106
107 // Store the initial value into the alloca.
108 Builder.CreateStore(Arg, DeclPtr);
109
110 Arg->setName(D.getName());
111 } else {
112 // Otherwise, if this is an aggregate, just use the input pointer.
113 DeclPtr = Arg;
114 Arg->setName(DeclPtr->getName());
115 }
116 }
117
118 llvm::Value *&DMEntry = LocalDeclMap[&D];
119 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
120 DMEntry = DeclPtr;
121}
122