blob: 7bd430d15b88f7ff2608b66bc04d35414543511a [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"
16//#include "llvm/Constants.h"
17//#include "llvm/DerivedTypes.h"
18//#include "llvm/Function.h"
19using namespace llvm;
20using namespace clang;
21using namespace CodeGen;
22
23
Chris Lattner1ad38f82007-06-09 01:20:56 +000024void CodeGenFunction::EmitDecl(const Decl &D) {
Chris Lattner84915fa2007-06-02 04:16:21 +000025
Chris Lattner1ad38f82007-06-09 01:20:56 +000026 switch (D.getKind()) {
Chris Lattner84915fa2007-06-02 04:16:21 +000027 default: assert(0 && "Unknown decl kind!");
28 case Decl::FileVariable:
29 assert(0 && "Should not see file-scope variables inside a function!");
30 case Decl::ParmVariable:
31 assert(0 && "Parmdecls should not be in declstmts!");
32 case Decl::Typedef: // typedef int X;
33 case Decl::Function: // void X();
34 case Decl::Struct: // struct X;
35 case Decl::Union: // union X;
36 case Decl::Class: // class X;
37 case Decl::Enum: // enum X;
38 // None of these decls require codegen support.
39 return;
40
41 case Decl::BlockVariable:
Chris Lattner1ad38f82007-06-09 01:20:56 +000042 return EmitBlockVarDecl(cast<BlockVarDecl>(D));
Chris Lattner84915fa2007-06-02 04:16:21 +000043 case Decl::EnumConstant:
Chris Lattner1ad38f82007-06-09 01:20:56 +000044 return EmitEnumConstantDecl(cast<EnumConstantDecl>(D));
Chris Lattner84915fa2007-06-02 04:16:21 +000045 }
46}
47
Chris Lattner84915fa2007-06-02 04:16:21 +000048void CodeGenFunction::EmitEnumConstantDecl(const EnumConstantDecl &D) {
49 assert(0 && "FIXME: Enum constant decls not implemented yet!");
50}
Chris Lattner03df1222007-06-02 04:53:11 +000051
52/// EmitBlockVarDecl - This method handles emission of any variable declaration
53/// inside a function, including static vars etc.
54void CodeGenFunction::EmitBlockVarDecl(const BlockVarDecl &D) {
55 switch (D.getStorageClass()) {
56 case VarDecl::Static:
57 assert(0 && "FIXME: local static vars not implemented yet");
58 case VarDecl::Extern:
59 assert(0 && "FIXME: should call up to codegenmodule");
60 default:
61 assert((D.getStorageClass() == VarDecl::None ||
62 D.getStorageClass() == VarDecl::Auto ||
63 D.getStorageClass() == VarDecl::Register) &&
64 "Unknown storage class");
65 return EmitLocalBlockVarDecl(D);
66 }
67}
68
69/// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a
70/// variable declaration with auto, register, or no storage class specifier.
71/// These turn into simple stack objects.
72void CodeGenFunction::EmitLocalBlockVarDecl(const BlockVarDecl &D) {
73 QualType Ty = D.getCanonicalType();
74
75 llvm::Value *DeclPtr;
76 if (Ty->isConstantSizeType()) {
77 // A normal fixed sized variable becomes an alloca in the entry block.
78 const llvm::Type *LTy = ConvertType(Ty, D.getLocation());
79 // TODO: Alignment
80 DeclPtr = new AllocaInst(LTy, 0, D.getName(), AllocaInsertPt);
81 } else {
82 // TODO: Create a dynamic alloca.
83 assert(0 && "FIXME: Local VLAs not implemented yet");
84 }
85
86 llvm::Value *&DMEntry = LocalDeclMap[&D];
87 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
88 DMEntry = DeclPtr;
89
90 // FIXME: Evaluate initializer.
91}