blob: 63b9d6f881c84c572c8e0a868ec9ceeb657e4b80 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +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"
16#include "llvm/Type.h"
17using namespace clang;
18using namespace CodeGen;
19
20
21void CodeGenFunction::EmitDecl(const Decl &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +000022 switch (D.getKind()) {
23 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:
38 return EmitBlockVarDecl(cast<BlockVarDecl>(D));
39 case Decl::EnumConstant:
40 return EmitEnumConstantDecl(cast<EnumConstantDecl>(D));
41 }
42}
43
44void CodeGenFunction::EmitEnumConstantDecl(const EnumConstantDecl &D) {
45 assert(0 && "FIXME: Enum constant decls not implemented yet!");
46}
47
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 Lattner590b6642007-07-15 23:26:56 +000072 if (Ty->isConstantSizeType(getContext())) {
Reid Spencer5f016e22007-07-11 17:01:13 +000073 // A normal fixed sized variable becomes an alloca in the entry block.
74 const llvm::Type *LTy = ConvertType(Ty);
75 // TODO: Alignment
76 DeclPtr = CreateTempAlloca(LTy, D.getName());
77 } 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 Lattner19785962007-07-12 00:39:48 +000086 // If this local has an initializer, emit it now.
87 if (const Expr *Init = D.getInit())
88 EmitStoreThroughLValue(EmitExpr(Init), LValue::MakeAddr(DeclPtr), Ty);
Reid Spencer5f016e22007-07-11 17:01:13 +000089}
90
91/// Emit an alloca for the specified parameter and set up LocalDeclMap.
92void CodeGenFunction::EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg) {
93 QualType Ty = D.getCanonicalType();
94
95 llvm::Value *DeclPtr;
Chris Lattner590b6642007-07-15 23:26:56 +000096 if (!Ty->isConstantSizeType(getContext())) {
Reid Spencer5f016e22007-07-11 17:01:13 +000097 // Variable sized values always are passed by-reference.
98 DeclPtr = Arg;
99 } else {
100 // A fixed sized first class variable becomes an alloca in the entry block.
101 const llvm::Type *LTy = ConvertType(Ty);
102 if (LTy->isFirstClassType()) {
103 // TODO: Alignment
104 DeclPtr = new llvm::AllocaInst(LTy, 0, std::string(D.getName())+".addr",
105 AllocaInsertPt);
106
107 // Store the initial value into the alloca.
108 Builder.CreateStore(Arg, DeclPtr);
109 } else {
110 // Otherwise, if this is an aggregate, just use the input pointer.
111 DeclPtr = Arg;
112 }
113 Arg->setName(D.getName());
114 }
115
116 llvm::Value *&DMEntry = LocalDeclMap[&D];
117 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
118 DMEntry = DeclPtr;
119}
120