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 | |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 14 | #include "CGDebugInfo.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 15 | #include "CodeGenFunction.h" |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 16 | #include "CodeGenModule.h" |
Daniel Dunbar | de7fb84 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 18 | #include "clang/AST/Decl.h" |
Anders Carlsson | 19567ee | 2008-08-25 01:38:19 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 20 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 21 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 22 | #include "llvm/GlobalVariable.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | #include "llvm/Type.h" |
| 24 | using namespace clang; |
| 25 | using namespace CodeGen; |
| 26 | |
| 27 | |
| 28 | void CodeGenFunction::EmitDecl(const Decl &D) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 29 | switch (D.getKind()) { |
| 30 | default: assert(0 && "Unknown decl kind!"); |
Chris Lattner | aa9fc46 | 2007-10-08 21:37:32 +0000 | [diff] [blame] | 31 | case Decl::ParmVar: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 32 | assert(0 && "Parmdecls should not be in declstmts!"); |
| 33 | case Decl::Typedef: // typedef int X; |
| 34 | case Decl::Function: // void X(); |
Argyrios Kyrtzidis | 35bc082 | 2008-10-15 00:42:39 +0000 | [diff] [blame] | 35 | case Decl::Record: // struct/union/class X; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 36 | case Decl::Enum: // enum X; |
Daniel Dunbar | 662174c8 | 2008-08-29 17:28:43 +0000 | [diff] [blame] | 37 | case Decl::EnumConstant: // enum ? { X = ? } |
Argyrios Kyrtzidis | 35bc082 | 2008-10-15 00:42:39 +0000 | [diff] [blame] | 38 | case Decl::CXXRecord: // struct/union/class X; [C++] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 39 | // None of these decls require codegen support. |
| 40 | return; |
| 41 | |
Daniel Dunbar | 662174c8 | 2008-08-29 17:28:43 +0000 | [diff] [blame] | 42 | case Decl::Var: { |
| 43 | const VarDecl &VD = cast<VarDecl>(D); |
| 44 | assert(VD.isBlockVarDecl() && |
| 45 | "Should not see file-scope variables inside a function!"); |
| 46 | return EmitBlockVarDecl(VD); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 47 | } |
Daniel Dunbar | 662174c8 | 2008-08-29 17:28:43 +0000 | [diff] [blame] | 48 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /// EmitBlockVarDecl - This method handles emission of any variable declaration |
| 52 | /// inside a function, including static vars etc. |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 53 | void CodeGenFunction::EmitBlockVarDecl(const VarDecl &D) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 54 | switch (D.getStorageClass()) { |
| 55 | case VarDecl::Static: |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 56 | return EmitStaticBlockVarDecl(D); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 57 | case VarDecl::Extern: |
Lauro Ramos Venancio | fea90b8 | 2008-02-16 22:30:38 +0000 | [diff] [blame] | 58 | // Don't emit it now, allow it to be emitted lazily on its first use. |
| 59 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 69 | llvm::GlobalValue * |
| 70 | CodeGenFunction::GenerateStaticBlockVarDecl(const VarDecl &D, |
| 71 | bool NoInit, |
| 72 | const char *Separator) { |
Chris Lattner | 8bcfc5b | 2008-04-06 23:10:54 +0000 | [diff] [blame] | 73 | QualType Ty = D.getType(); |
Eli Friedman | 3c2b317 | 2008-02-15 12:20:59 +0000 | [diff] [blame] | 74 | assert(Ty->isConstantSizeType() && "VLAs can't be static"); |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 75 | |
Chris Lattner | 19009e6 | 2008-01-09 18:47:25 +0000 | [diff] [blame] | 76 | const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty); |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 77 | llvm::Constant *Init = 0; |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 78 | if ((D.getInit() == 0) || NoInit) { |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 79 | Init = llvm::Constant::getNullValue(LTy); |
Oliver Hunt | 2824723 | 2007-12-02 00:11:25 +0000 | [diff] [blame] | 80 | } else { |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 81 | if (D.getInit()->isConstantExpr(getContext(), 0)) |
| 82 | Init = CGM.EmitConstantExpr(D.getInit(), this); |
| 83 | else { |
| 84 | assert(getContext().getLangOptions().CPlusPlus && |
| 85 | "only C++ supports non-constant static initializers!"); |
| 86 | return GenerateStaticCXXBlockVarDecl(D); |
| 87 | } |
Devang Patel | e40daa4 | 2007-10-26 17:50:58 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Oliver Hunt | 2824723 | 2007-12-02 00:11:25 +0000 | [diff] [blame] | 90 | assert(Init && "Unable to create initialiser for static decl"); |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 91 | |
Chris Lattner | 352ffde2 | 2008-02-06 04:54:32 +0000 | [diff] [blame] | 92 | std::string ContextName; |
Anders Carlsson | 19567ee | 2008-08-25 01:38:19 +0000 | [diff] [blame] | 93 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurFuncDecl)) |
Chris Lattner | c8aa5f1 | 2008-04-04 04:07:35 +0000 | [diff] [blame] | 94 | ContextName = FD->getName(); |
Anders Carlsson | 19567ee | 2008-08-25 01:38:19 +0000 | [diff] [blame] | 95 | else if (isa<ObjCMethodDecl>(CurFuncDecl)) |
| 96 | ContextName = std::string(CurFn->getNameStart(), |
| 97 | CurFn->getNameStart() + CurFn->getNameLen()); |
Chris Lattner | 352ffde2 | 2008-02-06 04:54:32 +0000 | [diff] [blame] | 98 | else |
Anders Carlsson | 19567ee | 2008-08-25 01:38:19 +0000 | [diff] [blame] | 99 | assert(0 && "Unknown context for block var decl"); |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 100 | |
Eli Friedman | 2659052 | 2008-06-08 01:23:18 +0000 | [diff] [blame] | 101 | llvm::GlobalValue *GV = |
| 102 | new llvm::GlobalVariable(Init->getType(), false, |
| 103 | llvm::GlobalValue::InternalLinkage, |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 104 | Init, ContextName + Separator + D.getName(), |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 105 | &CGM.getModule(), 0, Ty.getAddressSpace()); |
| 106 | |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 107 | return GV; |
| 108 | } |
| 109 | |
| 110 | void CodeGenFunction::EmitStaticBlockVarDecl(const VarDecl &D) { |
| 111 | |
| 112 | llvm::Value *&DMEntry = LocalDeclMap[&D]; |
| 113 | assert(DMEntry == 0 && "Decl already exists in localdeclmap!"); |
| 114 | |
| 115 | llvm::GlobalValue *GV = GenerateStaticBlockVarDecl(D, false, "."); |
| 116 | |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 117 | if (const AnnotateAttr *AA = D.getAttr<AnnotateAttr>()) { |
| 118 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 119 | llvm::Constant *Ann = |
| 120 | CGM.EmitAnnotateAttr(GV, AA, SM.getLogicalLineNumber(D.getLocation())); |
| 121 | CGM.AddAnnotation(Ann); |
| 122 | } |
| 123 | |
Eli Friedman | 2659052 | 2008-06-08 01:23:18 +0000 | [diff] [blame] | 124 | const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(D.getType()); |
| 125 | const llvm::Type *LPtrTy = |
| 126 | llvm::PointerType::get(LTy, D.getType().getAddressSpace()); |
| 127 | DMEntry = llvm::ConstantExpr::getBitCast(GV, LPtrTy); |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 128 | |
| 129 | // Emit global variable debug descriptor for static vars. |
| 130 | CGDebugInfo *DI = CGM.getDebugInfo(); |
| 131 | if(DI) { |
Daniel Dunbar | 66031a5 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 132 | DI->setLocation(D.getLocation()); |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 133 | DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(GV), &D); |
| 134 | } |
| 135 | |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 138 | /// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a |
| 139 | /// variable declaration with auto, register, or no storage class specifier. |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 140 | /// These turn into simple stack objects, or GlobalValues depending on target. |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 141 | void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) { |
Chris Lattner | 8bcfc5b | 2008-04-06 23:10:54 +0000 | [diff] [blame] | 142 | QualType Ty = D.getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 143 | |
| 144 | llvm::Value *DeclPtr; |
Eli Friedman | 3c2b317 | 2008-02-15 12:20:59 +0000 | [diff] [blame] | 145 | if (Ty->isConstantSizeType()) { |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 146 | if (!Target.useGlobalsForAutomaticVariables()) { |
| 147 | // A normal fixed sized variable becomes an alloca in the entry block. |
| 148 | const llvm::Type *LTy = ConvertType(Ty); |
Eli Friedman | 77eedd6 | 2008-05-31 21:20:41 +0000 | [diff] [blame] | 149 | llvm::AllocaInst * Alloc = CreateTempAlloca(LTy, D.getName()); |
| 150 | unsigned align = getContext().getTypeAlign(Ty); |
| 151 | if (const AlignedAttr* AA = D.getAttr<AlignedAttr>()) |
| 152 | align = std::max(align, AA->getAlignment()); |
| 153 | Alloc->setAlignment(align >> 3); |
| 154 | DeclPtr = Alloc; |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 155 | } else { |
| 156 | // Targets that don't support recursion emit locals as globals. |
| 157 | const char *Class = |
| 158 | D.getStorageClass() == VarDecl::Register ? ".reg." : ".auto."; |
| 159 | DeclPtr = GenerateStaticBlockVarDecl(D, true, Class); |
| 160 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 161 | } else { |
Daniel Dunbar | 662174c8 | 2008-08-29 17:28:43 +0000 | [diff] [blame] | 162 | CGM.ErrorUnsupported(&D, "variable-length array"); |
| 163 | |
| 164 | // FIXME: VLA: Add VLA support. For now just make up enough to let |
| 165 | // the compile go through. |
| 166 | const llvm::Type *LTy = ConvertType(Ty); |
| 167 | llvm::AllocaInst * Alloc = CreateTempAlloca(LTy, D.getName()); |
| 168 | DeclPtr = Alloc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | llvm::Value *&DMEntry = LocalDeclMap[&D]; |
| 172 | assert(DMEntry == 0 && "Decl already exists in localdeclmap!"); |
| 173 | DMEntry = DeclPtr; |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 174 | |
| 175 | // Emit debug info for local var declaration. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame^] | 176 | if (CGDebugInfo *DI = CGM.getDebugInfo()) { |
Daniel Dunbar | 66031a5 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 177 | DI->setLocation(D.getLocation()); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame^] | 178 | DI->EmitDeclareOfAutoVariable(&D, DeclPtr, Builder); |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Chris Lattner | 1978596 | 2007-07-12 00:39:48 +0000 | [diff] [blame] | 181 | // If this local has an initializer, emit it now. |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 182 | if (const Expr *Init = D.getInit()) { |
Chris Lattner | d31beb1 | 2007-08-26 07:29:23 +0000 | [diff] [blame] | 183 | if (!hasAggregateLLVMType(Init->getType())) { |
| 184 | llvm::Value *V = EmitScalarExpr(Init); |
Chris Lattner | 8b2f3b7 | 2007-08-26 07:30:49 +0000 | [diff] [blame] | 185 | Builder.CreateStore(V, DeclPtr, D.getType().isVolatileQualified()); |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 186 | } else if (Init->getType()->isAnyComplexType()) { |
Chris Lattner | 190dbe2 | 2007-08-26 16:22:13 +0000 | [diff] [blame] | 187 | EmitComplexExprIntoAddr(Init, DeclPtr, D.getType().isVolatileQualified()); |
Chris Lattner | 9a19edf | 2007-08-26 05:13:54 +0000 | [diff] [blame] | 188 | } else { |
Chris Lattner | 8b2f3b7 | 2007-08-26 07:30:49 +0000 | [diff] [blame] | 189 | EmitAggExpr(Init, DeclPtr, D.getType().isVolatileQualified()); |
Chris Lattner | 9a19edf | 2007-08-26 05:13:54 +0000 | [diff] [blame] | 190 | } |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 191 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 194 | /// Emit an alloca (or GlobalValue depending on target) |
| 195 | /// for the specified parameter and set up LocalDeclMap. |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 196 | void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg) { |
| 197 | // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl? |
Sanjiv Gupta | 31fc07d | 2008-10-31 09:52:39 +0000 | [diff] [blame] | 198 | assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) && |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 199 | "Invalid argument to EmitParmDecl"); |
Chris Lattner | 8bcfc5b | 2008-04-06 23:10:54 +0000 | [diff] [blame] | 200 | QualType Ty = D.getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 201 | |
| 202 | llvm::Value *DeclPtr; |
Eli Friedman | 3c2b317 | 2008-02-15 12:20:59 +0000 | [diff] [blame] | 203 | if (!Ty->isConstantSizeType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 204 | // Variable sized values always are passed by-reference. |
| 205 | DeclPtr = Arg; |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 206 | } else if (Target.useGlobalsForAutomaticVariables()) { |
| 207 | DeclPtr = GenerateStaticBlockVarDecl(D, true, ".arg."); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 208 | } else { |
Dan Gohman | d79a726 | 2008-05-22 22:12:56 +0000 | [diff] [blame] | 209 | // A fixed sized single-value variable becomes an alloca in the entry block. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 210 | const llvm::Type *LTy = ConvertType(Ty); |
Dan Gohman | d79a726 | 2008-05-22 22:12:56 +0000 | [diff] [blame] | 211 | if (LTy->isSingleValueType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 212 | // TODO: Alignment |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 213 | std::string Name(D.getName()); |
| 214 | Name += ".addr"; |
| 215 | DeclPtr = CreateTempAlloca(LTy, Name.c_str()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 216 | |
| 217 | // Store the initial value into the alloca. |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 218 | Builder.CreateStore(Arg, DeclPtr,Ty.isVolatileQualified()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 219 | } else { |
| 220 | // Otherwise, if this is an aggregate, just use the input pointer. |
| 221 | DeclPtr = Arg; |
| 222 | } |
| 223 | Arg->setName(D.getName()); |
| 224 | } |
| 225 | |
| 226 | llvm::Value *&DMEntry = LocalDeclMap[&D]; |
| 227 | assert(DMEntry == 0 && "Decl already exists in localdeclmap!"); |
| 228 | DMEntry = DeclPtr; |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 229 | |
| 230 | // Emit debug info for param declaration. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame^] | 231 | if (CGDebugInfo *DI = CGM.getDebugInfo()) { |
Daniel Dunbar | 66031a5 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 232 | DI->setLocation(D.getLocation()); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame^] | 233 | DI->EmitDeclareOfArgVariable(&D, DeclPtr, Builder); |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 234 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 235 | } |
| 236 | |