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