Chris Lattner | 84915fa | 2007-06-02 04:16:21 +0000 | [diff] [blame] | 1 | //===--- 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" |
| 19 | using namespace llvm; |
| 20 | using namespace clang; |
| 21 | using namespace CodeGen; |
| 22 | |
| 23 | |
Chris Lattner | 1ad38f8 | 2007-06-09 01:20:56 +0000 | [diff] [blame^] | 24 | void CodeGenFunction::EmitDecl(const Decl &D) { |
Chris Lattner | 84915fa | 2007-06-02 04:16:21 +0000 | [diff] [blame] | 25 | |
Chris Lattner | 1ad38f8 | 2007-06-09 01:20:56 +0000 | [diff] [blame^] | 26 | switch (D.getKind()) { |
Chris Lattner | 84915fa | 2007-06-02 04:16:21 +0000 | [diff] [blame] | 27 | 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 Lattner | 1ad38f8 | 2007-06-09 01:20:56 +0000 | [diff] [blame^] | 42 | return EmitBlockVarDecl(cast<BlockVarDecl>(D)); |
Chris Lattner | 84915fa | 2007-06-02 04:16:21 +0000 | [diff] [blame] | 43 | case Decl::EnumConstant: |
Chris Lattner | 1ad38f8 | 2007-06-09 01:20:56 +0000 | [diff] [blame^] | 44 | return EmitEnumConstantDecl(cast<EnumConstantDecl>(D)); |
Chris Lattner | 84915fa | 2007-06-02 04:16:21 +0000 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
Chris Lattner | 84915fa | 2007-06-02 04:16:21 +0000 | [diff] [blame] | 48 | void CodeGenFunction::EmitEnumConstantDecl(const EnumConstantDecl &D) { |
| 49 | assert(0 && "FIXME: Enum constant decls not implemented yet!"); |
| 50 | } |
Chris Lattner | 03df122 | 2007-06-02 04:53:11 +0000 | [diff] [blame] | 51 | |
| 52 | /// EmitBlockVarDecl - This method handles emission of any variable declaration |
| 53 | /// inside a function, including static vars etc. |
| 54 | void 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. |
| 72 | void 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 | } |