blob: 3b912ce3d65445d55c4222b388f950c8d5ab91ec [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Decl nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
Sanjiv Guptacc9b1632008-05-30 10:30:31 +000014#include "CGDebugInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000015#include "CodeGenFunction.h"
Anders Carlsson1a86b332007-10-17 00:52:43 +000016#include "CodeGenModule.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000017#include "clang/AST/ASTContext.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000018#include "clang/AST/Decl.h"
Anders Carlsson19567ee2008-08-25 01:38:19 +000019#include "clang/AST/DeclObjC.h"
Nate Begeman8bd4afe2008-04-19 04:17:09 +000020#include "clang/Basic/SourceManager.h"
Chris Lattner2621fd12008-05-08 05:58:21 +000021#include "clang/Basic/TargetInfo.h"
Anders Carlsson1a86b332007-10-17 00:52:43 +000022#include "llvm/GlobalVariable.h"
Anders Carlsson5d463152008-12-12 07:38:43 +000023#include "llvm/Intrinsics.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024#include "llvm/Type.h"
25using namespace clang;
26using namespace CodeGen;
27
28
29void CodeGenFunction::EmitDecl(const Decl &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +000030 switch (D.getKind()) {
31 default: assert(0 && "Unknown decl kind!");
Chris Lattneraa9fc462007-10-08 21:37:32 +000032 case Decl::ParmVar:
Reid Spencer5f016e22007-07-11 17:01:13 +000033 assert(0 && "Parmdecls should not be in declstmts!");
Reid Spencer5f016e22007-07-11 17:01:13 +000034 case Decl::Function: // void X();
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +000035 case Decl::Record: // struct/union/class X;
Reid Spencer5f016e22007-07-11 17:01:13 +000036 case Decl::Enum: // enum X;
Daniel Dunbar662174c82008-08-29 17:28:43 +000037 case Decl::EnumConstant: // enum ? { X = ? }
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +000038 case Decl::CXXRecord: // struct/union/class X; [C++]
Reid Spencer5f016e22007-07-11 17:01:13 +000039 // None of these decls require codegen support.
40 return;
41
Daniel Dunbar662174c82008-08-29 17:28:43 +000042 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 Spencer5f016e22007-07-11 17:01:13 +000047 }
Anders Carlssonfcdbb932008-12-20 21:51:53 +000048
49 case Decl::Typedef: { // typedef int X;
50 const TypedefDecl &TD = cast<TypedefDecl>(D);
51 QualType Ty = TD.getUnderlyingType();
52
53 if (Ty->isVariablyModifiedType())
54 EmitVLASize(Ty);
55 }
Daniel Dunbar662174c82008-08-29 17:28:43 +000056 }
Reid Spencer5f016e22007-07-11 17:01:13 +000057}
58
59/// EmitBlockVarDecl - This method handles emission of any variable declaration
60/// inside a function, including static vars etc.
Steve Naroff248a7532008-04-15 22:42:06 +000061void CodeGenFunction::EmitBlockVarDecl(const VarDecl &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +000062 switch (D.getStorageClass()) {
63 case VarDecl::Static:
Anders Carlsson1a86b332007-10-17 00:52:43 +000064 return EmitStaticBlockVarDecl(D);
Reid Spencer5f016e22007-07-11 17:01:13 +000065 case VarDecl::Extern:
Lauro Ramos Venanciofea90b82008-02-16 22:30:38 +000066 // Don't emit it now, allow it to be emitted lazily on its first use.
67 return;
Reid Spencer5f016e22007-07-11 17:01:13 +000068 default:
69 assert((D.getStorageClass() == VarDecl::None ||
70 D.getStorageClass() == VarDecl::Auto ||
71 D.getStorageClass() == VarDecl::Register) &&
72 "Unknown storage class");
73 return EmitLocalBlockVarDecl(D);
74 }
75}
76
Chris Lattner2621fd12008-05-08 05:58:21 +000077llvm::GlobalValue *
78CodeGenFunction::GenerateStaticBlockVarDecl(const VarDecl &D,
79 bool NoInit,
Sanjiv Gupta45206ec2009-02-03 18:07:49 +000080 const char *Separator,
81 llvm::GlobalValue
82 ::LinkageTypes Linkage) {
Chris Lattner8bcfc5b2008-04-06 23:10:54 +000083 QualType Ty = D.getType();
Eli Friedman3c2b3172008-02-15 12:20:59 +000084 assert(Ty->isConstantSizeType() && "VLAs can't be static");
Anders Carlsson1a86b332007-10-17 00:52:43 +000085
Chris Lattner19009e62008-01-09 18:47:25 +000086 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
Anders Carlsson1a86b332007-10-17 00:52:43 +000087 llvm::Constant *Init = 0;
Chris Lattner2621fd12008-05-08 05:58:21 +000088 if ((D.getInit() == 0) || NoInit) {
Anders Carlsson1a86b332007-10-17 00:52:43 +000089 Init = llvm::Constant::getNullValue(LTy);
Oliver Hunt28247232007-12-02 00:11:25 +000090 } else {
Eli Friedmanc9e8f602009-01-25 02:32:41 +000091 if (D.getInit()->isConstantInitializer(getContext()))
Anders Carlssone1b29ef2008-08-22 16:00:37 +000092 Init = CGM.EmitConstantExpr(D.getInit(), this);
93 else {
94 assert(getContext().getLangOptions().CPlusPlus &&
95 "only C++ supports non-constant static initializers!");
96 return GenerateStaticCXXBlockVarDecl(D);
97 }
Devang Patele40daa42007-10-26 17:50:58 +000098 }
99
Oliver Hunt28247232007-12-02 00:11:25 +0000100 assert(Init && "Unable to create initialiser for static decl");
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000101
Chris Lattner352ffde22008-02-06 04:54:32 +0000102 std::string ContextName;
Anders Carlsson19567ee2008-08-25 01:38:19 +0000103 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurFuncDecl))
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000104 ContextName = CGM.getMangledName(FD)->getName();
Anders Carlsson19567ee2008-08-25 01:38:19 +0000105 else if (isa<ObjCMethodDecl>(CurFuncDecl))
106 ContextName = std::string(CurFn->getNameStart(),
107 CurFn->getNameStart() + CurFn->getNameLen());
Chris Lattner352ffde22008-02-06 04:54:32 +0000108 else
Anders Carlsson19567ee2008-08-25 01:38:19 +0000109 assert(0 && "Unknown context for block var decl");
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000110
Eli Friedman26590522008-06-08 01:23:18 +0000111 llvm::GlobalValue *GV =
112 new llvm::GlobalVariable(Init->getType(), false,
Sanjiv Gupta45206ec2009-02-03 18:07:49 +0000113 Linkage,
Chris Lattner39f34e92008-11-24 04:00:27 +0000114 Init, ContextName + Separator +D.getNameAsString(),
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000115 &CGM.getModule(), 0, Ty.getAddressSpace());
116
Chris Lattner2621fd12008-05-08 05:58:21 +0000117 return GV;
118}
119
120void CodeGenFunction::EmitStaticBlockVarDecl(const VarDecl &D) {
121
122 llvm::Value *&DMEntry = LocalDeclMap[&D];
123 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
124
Sanjiv Gupta45206ec2009-02-03 18:07:49 +0000125 llvm::GlobalValue *GV;
126 GV = GenerateStaticBlockVarDecl(D, false, ".",
127 llvm::GlobalValue::InternalLinkage);
Chris Lattner2621fd12008-05-08 05:58:21 +0000128
Daniel Dunbar30510ab2009-02-12 23:32:54 +0000129 // FIXME: Merge attribute handling.
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000130 if (const AnnotateAttr *AA = D.getAttr<AnnotateAttr>()) {
131 SourceManager &SM = CGM.getContext().getSourceManager();
132 llvm::Constant *Ann =
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000133 CGM.EmitAnnotateAttr(GV, AA,
134 SM.getInstantiationLineNumber(D.getLocation()));
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000135 CGM.AddAnnotation(Ann);
136 }
137
Daniel Dunbar30510ab2009-02-12 23:32:54 +0000138 if (const SectionAttr *SA = D.getAttr<SectionAttr>())
139 GV->setSection(SA->getName());
140
Eli Friedman26590522008-06-08 01:23:18 +0000141 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(D.getType());
142 const llvm::Type *LPtrTy =
143 llvm::PointerType::get(LTy, D.getType().getAddressSpace());
144 DMEntry = llvm::ConstantExpr::getBitCast(GV, LPtrTy);
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000145
146 // Emit global variable debug descriptor for static vars.
147 CGDebugInfo *DI = CGM.getDebugInfo();
148 if(DI) {
Daniel Dunbar66031a52008-10-17 16:15:48 +0000149 DI->setLocation(D.getLocation());
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000150 DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(GV), &D);
151 }
152
Anders Carlsson1a86b332007-10-17 00:52:43 +0000153}
154
Reid Spencer5f016e22007-07-11 17:01:13 +0000155/// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a
156/// variable declaration with auto, register, or no storage class specifier.
Chris Lattner2621fd12008-05-08 05:58:21 +0000157/// These turn into simple stack objects, or GlobalValues depending on target.
Steve Naroff248a7532008-04-15 22:42:06 +0000158void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) {
Chris Lattner8bcfc5b2008-04-06 23:10:54 +0000159 QualType Ty = D.getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000160
161 llvm::Value *DeclPtr;
Eli Friedman3c2b3172008-02-15 12:20:59 +0000162 if (Ty->isConstantSizeType()) {
Chris Lattner2621fd12008-05-08 05:58:21 +0000163 if (!Target.useGlobalsForAutomaticVariables()) {
164 // A normal fixed sized variable becomes an alloca in the entry block.
165 const llvm::Type *LTy = ConvertType(Ty);
Chris Lattner8ec03f52008-11-24 03:54:41 +0000166 llvm::AllocaInst *Alloc =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000167 CreateTempAlloca(LTy, CGM.getMangledName(&D)->getName());
Eli Friedman77eedd62008-05-31 21:20:41 +0000168 unsigned align = getContext().getTypeAlign(Ty);
169 if (const AlignedAttr* AA = D.getAttr<AlignedAttr>())
170 align = std::max(align, AA->getAlignment());
171 Alloc->setAlignment(align >> 3);
172 DeclPtr = Alloc;
Chris Lattner2621fd12008-05-08 05:58:21 +0000173 } else {
174 // Targets that don't support recursion emit locals as globals.
175 const char *Class =
176 D.getStorageClass() == VarDecl::Register ? ".reg." : ".auto.";
Sanjiv Gupta45206ec2009-02-03 18:07:49 +0000177 DeclPtr = GenerateStaticBlockVarDecl(D, true, Class,
178 llvm::GlobalValue
179 ::InternalLinkage);
Chris Lattner2621fd12008-05-08 05:58:21 +0000180 }
Anders Carlsson60d35412008-12-20 20:46:34 +0000181
182 if (Ty->isVariablyModifiedType())
183 EmitVLASize(Ty);
Reid Spencer5f016e22007-07-11 17:01:13 +0000184 } else {
Anders Carlsson5ecb1b92009-02-09 20:41:50 +0000185 if (!DidCallStackSave) {
Anders Carlsson5d463152008-12-12 07:38:43 +0000186 // Save the stack.
187 const llvm::Type *LTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
188 llvm::Value *Stack = CreateTempAlloca(LTy, "saved_stack");
189
190 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave);
191 llvm::Value *V = Builder.CreateCall(F);
192
193 Builder.CreateStore(V, Stack);
Anders Carlsson5ecb1b92009-02-09 20:41:50 +0000194
195 DidCallStackSave = true;
Anders Carlsson5d463152008-12-12 07:38:43 +0000196
Anders Carlsson5ecb1b92009-02-09 20:41:50 +0000197 {
198 // Push a cleanup block and restore the stack there.
199 CleanupScope scope(*this);
200
201 V = Builder.CreateLoad(Stack, "tmp");
202 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stackrestore);
203 Builder.CreateCall(F, V);
204 }
Anders Carlsson5d463152008-12-12 07:38:43 +0000205 }
Anders Carlsson5ecb1b92009-02-09 20:41:50 +0000206
Anders Carlsson5d463152008-12-12 07:38:43 +0000207 // Get the element type.
208 const llvm::Type *LElemTy = ConvertType(Ty);
209 const llvm::Type *LElemPtrTy =
210 llvm::PointerType::get(LElemTy, D.getType().getAddressSpace());
211
Anders Carlsson60d35412008-12-20 20:46:34 +0000212 llvm::Value *VLASize = EmitVLASize(Ty);
Anders Carlsson5d463152008-12-12 07:38:43 +0000213
Anders Carlsson96f21472009-02-05 19:43:10 +0000214 // Downcast the VLA size expression
215 VLASize = Builder.CreateIntCast(VLASize, llvm::Type::Int32Ty, false, "tmp");
216
Anders Carlsson5d463152008-12-12 07:38:43 +0000217 // Allocate memory for the array.
218 llvm::Value *VLA = Builder.CreateAlloca(llvm::Type::Int8Ty, VLASize, "vla");
Eli Friedman8f39f5e2008-12-20 23:11:59 +0000219 DeclPtr = Builder.CreateBitCast(VLA, LElemPtrTy, "tmp");
Reid Spencer5f016e22007-07-11 17:01:13 +0000220 }
Eli Friedman8f39f5e2008-12-20 23:11:59 +0000221
Reid Spencer5f016e22007-07-11 17:01:13 +0000222 llvm::Value *&DMEntry = LocalDeclMap[&D];
223 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
224 DMEntry = DeclPtr;
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000225
226 // Emit debug info for local var declaration.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000227 if (CGDebugInfo *DI = CGM.getDebugInfo()) {
Daniel Dunbar66031a52008-10-17 16:15:48 +0000228 DI->setLocation(D.getLocation());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000229 DI->EmitDeclareOfAutoVariable(&D, DeclPtr, Builder);
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000230 }
231
Chris Lattner19785962007-07-12 00:39:48 +0000232 // If this local has an initializer, emit it now.
Chris Lattner7f02f722007-08-24 05:35:26 +0000233 if (const Expr *Init = D.getInit()) {
Chris Lattnerd31beb12007-08-26 07:29:23 +0000234 if (!hasAggregateLLVMType(Init->getType())) {
235 llvm::Value *V = EmitScalarExpr(Init);
Chris Lattner8b2f3b72007-08-26 07:30:49 +0000236 Builder.CreateStore(V, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9b2dc282008-04-04 16:54:41 +0000237 } else if (Init->getType()->isAnyComplexType()) {
Chris Lattner190dbe22007-08-26 16:22:13 +0000238 EmitComplexExprIntoAddr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9a19edf2007-08-26 05:13:54 +0000239 } else {
Chris Lattner8b2f3b72007-08-26 07:30:49 +0000240 EmitAggExpr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9a19edf2007-08-26 05:13:54 +0000241 }
Chris Lattner7f02f722007-08-24 05:35:26 +0000242 }
Anders Carlsson69c68b72009-02-07 23:51:38 +0000243
244 // Handle the cleanup attribute
245 if (const CleanupAttr *CA = D.getAttr<CleanupAttr>()) {
246 const FunctionDecl *FD = CA->getFunctionDecl();
247
248 llvm::Constant* F = CGM.GetAddrOfFunction(FD);
249 assert(F && "Could not find function!");
250
251 CleanupScope scope(*this);
252
253 CallArgList Args;
254 Args.push_back(std::make_pair(RValue::get(DeclPtr),
255 getContext().getPointerType(D.getType())));
256
257 EmitCall(CGM.getTypes().getFunctionInfo(FD), F, Args);
258 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000259}
260
Chris Lattner2621fd12008-05-08 05:58:21 +0000261/// Emit an alloca (or GlobalValue depending on target)
262/// for the specified parameter and set up LocalDeclMap.
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000263void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg) {
264 // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl?
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000265 assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) &&
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000266 "Invalid argument to EmitParmDecl");
Chris Lattner8bcfc5b2008-04-06 23:10:54 +0000267 QualType Ty = D.getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000268
269 llvm::Value *DeclPtr;
Eli Friedman3c2b3172008-02-15 12:20:59 +0000270 if (!Ty->isConstantSizeType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000271 // Variable sized values always are passed by-reference.
272 DeclPtr = Arg;
Chris Lattner2621fd12008-05-08 05:58:21 +0000273 } else if (Target.useGlobalsForAutomaticVariables()) {
Sanjiv Gupta45206ec2009-02-03 18:07:49 +0000274 // Targets that don't have stack use global address space for parameters.
275 // Specify external linkage for such globals so that llvm optimizer do
276 // not assume there values initialized as zero.
Sanjiv Gupta193cdbe2009-02-10 04:17:25 +0000277 DeclPtr = GenerateStaticBlockVarDecl(D, true, ".auto.",
Sanjiv Gupta45206ec2009-02-03 18:07:49 +0000278 llvm::GlobalValue::ExternalLinkage);
Reid Spencer5f016e22007-07-11 17:01:13 +0000279 } else {
Dan Gohmand79a7262008-05-22 22:12:56 +0000280 // A fixed sized single-value variable becomes an alloca in the entry block.
Reid Spencer5f016e22007-07-11 17:01:13 +0000281 const llvm::Type *LTy = ConvertType(Ty);
Dan Gohmand79a7262008-05-22 22:12:56 +0000282 if (LTy->isSingleValueType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000283 // TODO: Alignment
Chris Lattner39f34e92008-11-24 04:00:27 +0000284 std::string Name = D.getNameAsString();
Daniel Dunbar56273772008-09-17 00:51:38 +0000285 Name += ".addr";
286 DeclPtr = CreateTempAlloca(LTy, Name.c_str());
Reid Spencer5f016e22007-07-11 17:01:13 +0000287
288 // Store the initial value into the alloca.
Eli Friedman1e692ac2008-06-13 23:01:12 +0000289 Builder.CreateStore(Arg, DeclPtr,Ty.isVolatileQualified());
Reid Spencer5f016e22007-07-11 17:01:13 +0000290 } else {
291 // Otherwise, if this is an aggregate, just use the input pointer.
292 DeclPtr = Arg;
293 }
Chris Lattner39f34e92008-11-24 04:00:27 +0000294 Arg->setName(D.getNameAsString());
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 }
296
297 llvm::Value *&DMEntry = LocalDeclMap[&D];
298 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
299 DMEntry = DeclPtr;
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000300
301 // Emit debug info for param declaration.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000302 if (CGDebugInfo *DI = CGM.getDebugInfo()) {
Daniel Dunbar66031a52008-10-17 16:15:48 +0000303 DI->setLocation(D.getLocation());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000304 DI->EmitDeclareOfArgVariable(&D, DeclPtr, Builder);
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000305 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000306}
307