blob: 57e9c39832f52ae63c5fc70ff73454fcda0a2e0b [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Decl nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
Sanjiv Guptab2a10452008-05-30 10:30:31 +000014#include "CGDebugInfo.h"
Chris Lattner4b009652007-07-25 00:24:17 +000015#include "CodeGenFunction.h"
Anders Carlsson855d78d2007-10-17 00:52:43 +000016#include "CodeGenModule.h"
Daniel Dunbareee5cd12008-08-11 05:00:27 +000017#include "clang/AST/ASTContext.h"
Daniel Dunbar64789f82008-08-11 05:35:13 +000018#include "clang/AST/Decl.h"
Anders Carlssonb10bac72008-08-25 01:38:19 +000019#include "clang/AST/DeclObjC.h"
Nate Begeman8a704172008-04-19 04:17:09 +000020#include "clang/Basic/SourceManager.h"
Chris Lattner85970f32008-05-08 05:58:21 +000021#include "clang/Basic/TargetInfo.h"
Anders Carlsson855d78d2007-10-17 00:52:43 +000022#include "llvm/GlobalVariable.h"
Chris Lattner4b009652007-07-25 00:24:17 +000023#include "llvm/Type.h"
Sanjiv Guptab2a10452008-05-30 10:30:31 +000024#include "llvm/Support/Dwarf.h"
Chris Lattner4b009652007-07-25 00:24:17 +000025using namespace clang;
26using namespace CodeGen;
27
28
29void CodeGenFunction::EmitDecl(const Decl &D) {
30 switch (D.getKind()) {
31 default: assert(0 && "Unknown decl kind!");
Chris Lattner3289bca2007-10-08 21:37:32 +000032 case Decl::ParmVar:
Chris Lattner4b009652007-07-25 00:24:17 +000033 assert(0 && "Parmdecls should not be in declstmts!");
34 case Decl::Typedef: // typedef int X;
35 case Decl::Function: // void X();
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +000036 case Decl::Record: // struct/union/class X;
Chris Lattner4b009652007-07-25 00:24:17 +000037 case Decl::Enum: // enum X;
Daniel Dunbar952f4732008-08-29 17:28:43 +000038 case Decl::EnumConstant: // enum ? { X = ? }
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +000039 case Decl::CXXRecord: // struct/union/class X; [C++]
Chris Lattner4b009652007-07-25 00:24:17 +000040 // None of these decls require codegen support.
41 return;
42
Daniel Dunbar952f4732008-08-29 17:28:43 +000043 case Decl::Var: {
44 const VarDecl &VD = cast<VarDecl>(D);
45 assert(VD.isBlockVarDecl() &&
46 "Should not see file-scope variables inside a function!");
47 return EmitBlockVarDecl(VD);
Chris Lattner4b009652007-07-25 00:24:17 +000048 }
Daniel Dunbar952f4732008-08-29 17:28:43 +000049 }
Chris Lattner4b009652007-07-25 00:24:17 +000050}
51
52/// EmitBlockVarDecl - This method handles emission of any variable declaration
53/// inside a function, including static vars etc.
Steve Naroff72a6ebc2008-04-15 22:42:06 +000054void CodeGenFunction::EmitBlockVarDecl(const VarDecl &D) {
Chris Lattner4b009652007-07-25 00:24:17 +000055 switch (D.getStorageClass()) {
56 case VarDecl::Static:
Anders Carlsson855d78d2007-10-17 00:52:43 +000057 return EmitStaticBlockVarDecl(D);
Chris Lattner4b009652007-07-25 00:24:17 +000058 case VarDecl::Extern:
Lauro Ramos Venancio2348d972008-02-16 22:30:38 +000059 // Don't emit it now, allow it to be emitted lazily on its first use.
60 return;
Chris Lattner4b009652007-07-25 00:24:17 +000061 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
Chris Lattner85970f32008-05-08 05:58:21 +000070llvm::GlobalValue *
71CodeGenFunction::GenerateStaticBlockVarDecl(const VarDecl &D,
72 bool NoInit,
73 const char *Separator) {
Chris Lattner42a21742008-04-06 23:10:54 +000074 QualType Ty = D.getType();
Eli Friedman62f67fd2008-02-15 12:20:59 +000075 assert(Ty->isConstantSizeType() && "VLAs can't be static");
Anders Carlsson855d78d2007-10-17 00:52:43 +000076
Chris Lattner43cdbf52008-01-09 18:47:25 +000077 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
Anders Carlsson855d78d2007-10-17 00:52:43 +000078 llvm::Constant *Init = 0;
Chris Lattner85970f32008-05-08 05:58:21 +000079 if ((D.getInit() == 0) || NoInit) {
Anders Carlsson855d78d2007-10-17 00:52:43 +000080 Init = llvm::Constant::getNullValue(LTy);
Oliver Hunt253e0a72007-12-02 00:11:25 +000081 } else {
Anders Carlssonc9f8ccd2008-08-22 16:00:37 +000082 if (D.getInit()->isConstantExpr(getContext(), 0))
83 Init = CGM.EmitConstantExpr(D.getInit(), this);
84 else {
85 assert(getContext().getLangOptions().CPlusPlus &&
86 "only C++ supports non-constant static initializers!");
87 return GenerateStaticCXXBlockVarDecl(D);
88 }
Devang Patel2ecd9012007-10-26 17:50:58 +000089 }
90
Oliver Hunt253e0a72007-12-02 00:11:25 +000091 assert(Init && "Unable to create initialiser for static decl");
Nate Begeman8a704172008-04-19 04:17:09 +000092
Chris Lattner9b026342008-02-06 04:54:32 +000093 std::string ContextName;
Anders Carlssonb10bac72008-08-25 01:38:19 +000094 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurFuncDecl))
Chris Lattner6e6a5972008-04-04 04:07:35 +000095 ContextName = FD->getName();
Anders Carlssonb10bac72008-08-25 01:38:19 +000096 else if (isa<ObjCMethodDecl>(CurFuncDecl))
97 ContextName = std::string(CurFn->getNameStart(),
98 CurFn->getNameStart() + CurFn->getNameLen());
Chris Lattner9b026342008-02-06 04:54:32 +000099 else
Anders Carlssonb10bac72008-08-25 01:38:19 +0000100 assert(0 && "Unknown context for block var decl");
Nate Begeman8a704172008-04-19 04:17:09 +0000101
Eli Friedman168cf2a2008-06-08 01:23:18 +0000102 llvm::GlobalValue *GV =
103 new llvm::GlobalVariable(Init->getType(), false,
104 llvm::GlobalValue::InternalLinkage,
Chris Lattner85970f32008-05-08 05:58:21 +0000105 Init, ContextName + Separator + D.getName(),
Nate Begeman8a704172008-04-19 04:17:09 +0000106 &CGM.getModule(), 0, Ty.getAddressSpace());
107
Chris Lattner85970f32008-05-08 05:58:21 +0000108 return GV;
109}
110
111void CodeGenFunction::EmitStaticBlockVarDecl(const VarDecl &D) {
112
113 llvm::Value *&DMEntry = LocalDeclMap[&D];
114 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
115
116 llvm::GlobalValue *GV = GenerateStaticBlockVarDecl(D, false, ".");
117
Nate Begeman8a704172008-04-19 04:17:09 +0000118 if (const AnnotateAttr *AA = D.getAttr<AnnotateAttr>()) {
119 SourceManager &SM = CGM.getContext().getSourceManager();
120 llvm::Constant *Ann =
121 CGM.EmitAnnotateAttr(GV, AA, SM.getLogicalLineNumber(D.getLocation()));
122 CGM.AddAnnotation(Ann);
123 }
124
Eli Friedman168cf2a2008-06-08 01:23:18 +0000125 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(D.getType());
126 const llvm::Type *LPtrTy =
127 llvm::PointerType::get(LTy, D.getType().getAddressSpace());
128 DMEntry = llvm::ConstantExpr::getBitCast(GV, LPtrTy);
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000129
130 // Emit global variable debug descriptor for static vars.
131 CGDebugInfo *DI = CGM.getDebugInfo();
132 if(DI) {
133 if(D.getLocation().isValid())
134 DI->setLocation(D.getLocation());
135 DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(GV), &D);
136 }
137
Anders Carlsson855d78d2007-10-17 00:52:43 +0000138}
139
Chris Lattner4b009652007-07-25 00:24:17 +0000140/// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a
141/// variable declaration with auto, register, or no storage class specifier.
Chris Lattner85970f32008-05-08 05:58:21 +0000142/// These turn into simple stack objects, or GlobalValues depending on target.
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000143void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) {
Chris Lattner42a21742008-04-06 23:10:54 +0000144 QualType Ty = D.getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000145
146 llvm::Value *DeclPtr;
Eli Friedman62f67fd2008-02-15 12:20:59 +0000147 if (Ty->isConstantSizeType()) {
Chris Lattner85970f32008-05-08 05:58:21 +0000148 if (!Target.useGlobalsForAutomaticVariables()) {
149 // A normal fixed sized variable becomes an alloca in the entry block.
150 const llvm::Type *LTy = ConvertType(Ty);
Eli Friedman696758f2008-05-31 21:20:41 +0000151 llvm::AllocaInst * Alloc = CreateTempAlloca(LTy, D.getName());
152 unsigned align = getContext().getTypeAlign(Ty);
153 if (const AlignedAttr* AA = D.getAttr<AlignedAttr>())
154 align = std::max(align, AA->getAlignment());
155 Alloc->setAlignment(align >> 3);
156 DeclPtr = Alloc;
Chris Lattner85970f32008-05-08 05:58:21 +0000157 } else {
158 // Targets that don't support recursion emit locals as globals.
159 const char *Class =
160 D.getStorageClass() == VarDecl::Register ? ".reg." : ".auto.";
161 DeclPtr = GenerateStaticBlockVarDecl(D, true, Class);
162 }
Chris Lattner4b009652007-07-25 00:24:17 +0000163 } else {
Daniel Dunbar952f4732008-08-29 17:28:43 +0000164 CGM.ErrorUnsupported(&D, "variable-length array");
165
166 // FIXME: VLA: Add VLA support. For now just make up enough to let
167 // the compile go through.
168 const llvm::Type *LTy = ConvertType(Ty);
169 llvm::AllocaInst * Alloc = CreateTempAlloca(LTy, D.getName());
170 DeclPtr = Alloc;
Chris Lattner4b009652007-07-25 00:24:17 +0000171 }
172
173 llvm::Value *&DMEntry = LocalDeclMap[&D];
174 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
175 DMEntry = DeclPtr;
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000176
177 // Emit debug info for local var declaration.
178 CGDebugInfo *DI = CGM.getDebugInfo();
179 if(DI) {
180 if(D.getLocation().isValid())
181 DI->setLocation(D.getLocation());
182 DI->EmitDeclare(&D, llvm::dwarf::DW_TAG_auto_variable,
183 DeclPtr, Builder);
184 }
185
Chris Lattner4b009652007-07-25 00:24:17 +0000186 // If this local has an initializer, emit it now.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000187 if (const Expr *Init = D.getInit()) {
Chris Lattner222d3222007-08-26 07:29:23 +0000188 if (!hasAggregateLLVMType(Init->getType())) {
189 llvm::Value *V = EmitScalarExpr(Init);
Chris Lattner9a918dd2007-08-26 07:30:49 +0000190 Builder.CreateStore(V, DeclPtr, D.getType().isVolatileQualified());
Chris Lattnerde0908b2008-04-04 16:54:41 +0000191 } else if (Init->getType()->isAnyComplexType()) {
Chris Lattner8e1f6e02007-08-26 16:22:13 +0000192 EmitComplexExprIntoAddr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner64a113b2007-08-26 05:13:54 +0000193 } else {
Chris Lattner9a918dd2007-08-26 07:30:49 +0000194 EmitAggExpr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner64a113b2007-08-26 05:13:54 +0000195 }
Chris Lattner9fba49a2007-08-24 05:35:26 +0000196 }
Chris Lattner4b009652007-07-25 00:24:17 +0000197}
198
Chris Lattner85970f32008-05-08 05:58:21 +0000199/// Emit an alloca (or GlobalValue depending on target)
200/// for the specified parameter and set up LocalDeclMap.
Daniel Dunbarace33292008-08-16 03:19:19 +0000201void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg) {
202 // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl?
203 assert(isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D) &&
204 "Invalid argument to EmitParmDecl");
Chris Lattner42a21742008-04-06 23:10:54 +0000205 QualType Ty = D.getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000206
207 llvm::Value *DeclPtr;
Eli Friedman62f67fd2008-02-15 12:20:59 +0000208 if (!Ty->isConstantSizeType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000209 // Variable sized values always are passed by-reference.
210 DeclPtr = Arg;
Chris Lattner85970f32008-05-08 05:58:21 +0000211 } else if (Target.useGlobalsForAutomaticVariables()) {
212 DeclPtr = GenerateStaticBlockVarDecl(D, true, ".arg.");
Chris Lattner4b009652007-07-25 00:24:17 +0000213 } else {
Dan Gohman377ba9f2008-05-22 22:12:56 +0000214 // A fixed sized single-value variable becomes an alloca in the entry block.
Chris Lattner4b009652007-07-25 00:24:17 +0000215 const llvm::Type *LTy = ConvertType(Ty);
Dan Gohman377ba9f2008-05-22 22:12:56 +0000216 if (LTy->isSingleValueType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000217 // TODO: Alignment
Daniel Dunbar04d35782008-09-17 00:51:38 +0000218 std::string Name(D.getName());
219 Name += ".addr";
220 DeclPtr = CreateTempAlloca(LTy, Name.c_str());
Chris Lattner4b009652007-07-25 00:24:17 +0000221
222 // Store the initial value into the alloca.
Eli Friedman2e630542008-06-13 23:01:12 +0000223 Builder.CreateStore(Arg, DeclPtr,Ty.isVolatileQualified());
Chris Lattner4b009652007-07-25 00:24:17 +0000224 } else {
225 // Otherwise, if this is an aggregate, just use the input pointer.
226 DeclPtr = Arg;
227 }
228 Arg->setName(D.getName());
229 }
230
231 llvm::Value *&DMEntry = LocalDeclMap[&D];
232 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
233 DMEntry = DeclPtr;
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000234
235 // Emit debug info for param declaration.
236 CGDebugInfo *DI = CGM.getDebugInfo();
237 if(DI) {
238 if(D.getLocation().isValid())
239 DI->setLocation(D.getLocation());
240 DI->EmitDeclare(&D, llvm::dwarf::DW_TAG_arg_variable,
241 DeclPtr, Builder);
242 }
243
Chris Lattner4b009652007-07-25 00:24:17 +0000244}
245