Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code to emit Decl nodes as LLVM code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenFunction.h" |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 15 | #include "CodeGenModule.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | #include "clang/AST/AST.h" |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 17 | #include "llvm/GlobalVariable.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "llvm/Type.h" |
| 19 | using namespace clang; |
| 20 | using namespace CodeGen; |
| 21 | |
| 22 | |
| 23 | void CodeGenFunction::EmitDecl(const Decl &D) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | switch (D.getKind()) { |
| 25 | default: assert(0 && "Unknown decl kind!"); |
Chris Lattner | aa9fc46 | 2007-10-08 21:37:32 +0000 | [diff] [blame] | 26 | case Decl::FileVar: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 27 | assert(0 && "Should not see file-scope variables inside a function!"); |
Chris Lattner | aa9fc46 | 2007-10-08 21:37:32 +0000 | [diff] [blame] | 28 | case Decl::ParmVar: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 29 | assert(0 && "Parmdecls should not be in declstmts!"); |
| 30 | case Decl::Typedef: // typedef int X; |
| 31 | case Decl::Function: // void X(); |
| 32 | case Decl::Struct: // struct X; |
| 33 | case Decl::Union: // union X; |
| 34 | case Decl::Class: // class X; |
| 35 | case Decl::Enum: // enum X; |
| 36 | // None of these decls require codegen support. |
| 37 | return; |
| 38 | |
Chris Lattner | aa9fc46 | 2007-10-08 21:37:32 +0000 | [diff] [blame] | 39 | case Decl::BlockVar: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 40 | return EmitBlockVarDecl(cast<BlockVarDecl>(D)); |
| 41 | case Decl::EnumConstant: |
| 42 | return EmitEnumConstantDecl(cast<EnumConstantDecl>(D)); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | void CodeGenFunction::EmitEnumConstantDecl(const EnumConstantDecl &D) { |
| 47 | assert(0 && "FIXME: Enum constant decls not implemented yet!"); |
| 48 | } |
| 49 | |
| 50 | /// EmitBlockVarDecl - This method handles emission of any variable declaration |
| 51 | /// inside a function, including static vars etc. |
| 52 | void CodeGenFunction::EmitBlockVarDecl(const BlockVarDecl &D) { |
| 53 | switch (D.getStorageClass()) { |
| 54 | case VarDecl::Static: |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 55 | return EmitStaticBlockVarDecl(D); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 56 | case VarDecl::Extern: |
Lauro Ramos Venancio | fea90b8 | 2008-02-16 22:30:38 +0000 | [diff] [blame] | 57 | // Don't emit it now, allow it to be emitted lazily on its first use. |
| 58 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 59 | default: |
| 60 | assert((D.getStorageClass() == VarDecl::None || |
| 61 | D.getStorageClass() == VarDecl::Auto || |
| 62 | D.getStorageClass() == VarDecl::Register) && |
| 63 | "Unknown storage class"); |
| 64 | return EmitLocalBlockVarDecl(D); |
| 65 | } |
| 66 | } |
| 67 | |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 68 | void CodeGenFunction::EmitStaticBlockVarDecl(const BlockVarDecl &D) { |
| 69 | QualType Ty = D.getCanonicalType(); |
Eli Friedman | 3c2b317 | 2008-02-15 12:20:59 +0000 | [diff] [blame] | 70 | assert(Ty->isConstantSizeType() && "VLAs can't be static"); |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 71 | |
| 72 | llvm::Value *&DMEntry = LocalDeclMap[&D]; |
| 73 | assert(DMEntry == 0 && "Decl already exists in localdeclmap!"); |
| 74 | |
Chris Lattner | 19009e6 | 2008-01-09 18:47:25 +0000 | [diff] [blame] | 75 | const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty); |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 76 | llvm::Constant *Init = 0; |
| 77 | if (D.getInit() == 0) { |
| 78 | Init = llvm::Constant::getNullValue(LTy); |
Oliver Hunt | 2824723 | 2007-12-02 00:11:25 +0000 | [diff] [blame] | 79 | } else { |
| 80 | Init = CGM.EmitGlobalInit(D.getInit()); |
Devang Patel | e40daa4 | 2007-10-26 17:50:58 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Oliver Hunt | 2824723 | 2007-12-02 00:11:25 +0000 | [diff] [blame] | 83 | assert(Init && "Unable to create initialiser for static decl"); |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 84 | |
Chris Lattner | 352ffde2 | 2008-02-06 04:54:32 +0000 | [diff] [blame] | 85 | std::string ContextName; |
| 86 | if (CurFuncDecl) |
| 87 | ContextName = CurFuncDecl->getName(); |
| 88 | else |
| 89 | assert(0 && "Unknown context for block var decl"); // FIXME Handle objc. |
| 90 | |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 91 | DMEntry = |
| 92 | new llvm::GlobalVariable(LTy, false, |
| 93 | llvm::GlobalValue::InternalLinkage, |
Chris Lattner | 352ffde2 | 2008-02-06 04:54:32 +0000 | [diff] [blame] | 94 | Init, ContextName + "." + D.getName(), |
| 95 | &CGM.getModule(), 0, |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 96 | Ty.getAddressSpace()); |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 97 | |
| 98 | } |
| 99 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 100 | /// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a |
| 101 | /// variable declaration with auto, register, or no storage class specifier. |
| 102 | /// These turn into simple stack objects. |
| 103 | void CodeGenFunction::EmitLocalBlockVarDecl(const BlockVarDecl &D) { |
| 104 | QualType Ty = D.getCanonicalType(); |
| 105 | |
| 106 | llvm::Value *DeclPtr; |
Eli Friedman | 3c2b317 | 2008-02-15 12:20:59 +0000 | [diff] [blame] | 107 | if (Ty->isConstantSizeType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 108 | // A normal fixed sized variable becomes an alloca in the entry block. |
| 109 | const llvm::Type *LTy = ConvertType(Ty); |
| 110 | // TODO: Alignment |
| 111 | DeclPtr = CreateTempAlloca(LTy, D.getName()); |
| 112 | } else { |
| 113 | // TODO: Create a dynamic alloca. |
| 114 | assert(0 && "FIXME: Local VLAs not implemented yet"); |
| 115 | } |
| 116 | |
| 117 | llvm::Value *&DMEntry = LocalDeclMap[&D]; |
| 118 | assert(DMEntry == 0 && "Decl already exists in localdeclmap!"); |
| 119 | DMEntry = DeclPtr; |
| 120 | |
Chris Lattner | 1978596 | 2007-07-12 00:39:48 +0000 | [diff] [blame] | 121 | // If this local has an initializer, emit it now. |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 122 | if (const Expr *Init = D.getInit()) { |
Chris Lattner | d31beb1 | 2007-08-26 07:29:23 +0000 | [diff] [blame] | 123 | if (!hasAggregateLLVMType(Init->getType())) { |
| 124 | llvm::Value *V = EmitScalarExpr(Init); |
Chris Lattner | 8b2f3b7 | 2007-08-26 07:30:49 +0000 | [diff] [blame] | 125 | Builder.CreateStore(V, DeclPtr, D.getType().isVolatileQualified()); |
Chris Lattner | d31beb1 | 2007-08-26 07:29:23 +0000 | [diff] [blame] | 126 | } else if (Init->getType()->isComplexType()) { |
Chris Lattner | 190dbe2 | 2007-08-26 16:22:13 +0000 | [diff] [blame] | 127 | EmitComplexExprIntoAddr(Init, DeclPtr, D.getType().isVolatileQualified()); |
Chris Lattner | 9a19edf | 2007-08-26 05:13:54 +0000 | [diff] [blame] | 128 | } else { |
Chris Lattner | 8b2f3b7 | 2007-08-26 07:30:49 +0000 | [diff] [blame] | 129 | EmitAggExpr(Init, DeclPtr, D.getType().isVolatileQualified()); |
Chris Lattner | 9a19edf | 2007-08-26 05:13:54 +0000 | [diff] [blame] | 130 | } |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 131 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /// Emit an alloca for the specified parameter and set up LocalDeclMap. |
| 135 | void CodeGenFunction::EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg) { |
| 136 | QualType Ty = D.getCanonicalType(); |
| 137 | |
| 138 | llvm::Value *DeclPtr; |
Eli Friedman | 3c2b317 | 2008-02-15 12:20:59 +0000 | [diff] [blame] | 139 | if (!Ty->isConstantSizeType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 140 | // Variable sized values always are passed by-reference. |
| 141 | DeclPtr = Arg; |
| 142 | } else { |
| 143 | // A fixed sized first class variable becomes an alloca in the entry block. |
| 144 | const llvm::Type *LTy = ConvertType(Ty); |
| 145 | if (LTy->isFirstClassType()) { |
| 146 | // TODO: Alignment |
| 147 | DeclPtr = new llvm::AllocaInst(LTy, 0, std::string(D.getName())+".addr", |
| 148 | AllocaInsertPt); |
| 149 | |
| 150 | // Store the initial value into the alloca. |
| 151 | Builder.CreateStore(Arg, DeclPtr); |
| 152 | } else { |
| 153 | // Otherwise, if this is an aggregate, just use the input pointer. |
| 154 | DeclPtr = Arg; |
| 155 | } |
| 156 | Arg->setName(D.getName()); |
| 157 | } |
| 158 | |
| 159 | llvm::Value *&DMEntry = LocalDeclMap[&D]; |
| 160 | assert(DMEntry == 0 && "Decl already exists in localdeclmap!"); |
| 161 | DMEntry = DeclPtr; |
| 162 | } |
| 163 | |