blob: e8084dcf179941beea9010afd1ac595a4d160e59 [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"
Anders Carlsson9be6aaf2008-12-12 07:38:43 +000023#include "llvm/Intrinsics.h"
Chris Lattner4b009652007-07-25 00:24:17 +000024#include "llvm/Type.h"
25using 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 Lattner6c5ec622008-11-24 04:00:27 +000095 ContextName = FD->getNameAsString();
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 Lattner6c5ec622008-11-24 04:00:27 +0000105 Init, ContextName + Separator +D.getNameAsString(),
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) {
Daniel Dunbar6fc1f972008-10-17 16:15:48 +0000133 DI->setLocation(D.getLocation());
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000134 DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(GV), &D);
135 }
136
Anders Carlsson855d78d2007-10-17 00:52:43 +0000137}
138
Chris Lattner4b009652007-07-25 00:24:17 +0000139/// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a
140/// variable declaration with auto, register, or no storage class specifier.
Chris Lattner85970f32008-05-08 05:58:21 +0000141/// These turn into simple stack objects, or GlobalValues depending on target.
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000142void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) {
Chris Lattner42a21742008-04-06 23:10:54 +0000143 QualType Ty = D.getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000144
145 llvm::Value *DeclPtr;
Eli Friedman62f67fd2008-02-15 12:20:59 +0000146 if (Ty->isConstantSizeType()) {
Chris Lattner85970f32008-05-08 05:58:21 +0000147 if (!Target.useGlobalsForAutomaticVariables()) {
148 // A normal fixed sized variable becomes an alloca in the entry block.
149 const llvm::Type *LTy = ConvertType(Ty);
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000150 llvm::AllocaInst *Alloc =
151 CreateTempAlloca(LTy, D.getIdentifier()->getName());
Eli Friedman696758f2008-05-31 21:20:41 +0000152 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 }
Anders Carlssond9767612008-12-20 20:46:34 +0000163
164 if (Ty->isVariablyModifiedType())
165 EmitVLASize(Ty);
Chris Lattner4b009652007-07-25 00:24:17 +0000166 } else {
Anders Carlsson9be6aaf2008-12-12 07:38:43 +0000167 if (!StackSaveValues.back()) {
168 // Save the stack.
169 const llvm::Type *LTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
170 llvm::Value *Stack = CreateTempAlloca(LTy, "saved_stack");
171
172 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave);
173 llvm::Value *V = Builder.CreateCall(F);
174
175 Builder.CreateStore(V, Stack);
176
177 StackSaveValues.back() = Stack;
178 }
179 // Get the element type.
180 const llvm::Type *LElemTy = ConvertType(Ty);
181 const llvm::Type *LElemPtrTy =
182 llvm::PointerType::get(LElemTy, D.getType().getAddressSpace());
183
Anders Carlssond9767612008-12-20 20:46:34 +0000184 llvm::Value *VLASize = EmitVLASize(Ty);
Anders Carlsson9be6aaf2008-12-12 07:38:43 +0000185
186 // Allocate memory for the array.
187 llvm::Value *VLA = Builder.CreateAlloca(llvm::Type::Int8Ty, VLASize, "vla");
188 VLA = Builder.CreateBitCast(VLA, LElemPtrTy, "tmp");
189
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000190 llvm::AllocaInst *Alloc =
Anders Carlsson9be6aaf2008-12-12 07:38:43 +0000191 CreateTempAlloca(LElemPtrTy, D.getIdentifier()->getName());
192
193 // FIXME: Volatile?
194 Builder.CreateStore(VLA, Alloc);
195
Daniel Dunbar952f4732008-08-29 17:28:43 +0000196 DeclPtr = Alloc;
Chris Lattner4b009652007-07-25 00:24:17 +0000197 }
198
199 llvm::Value *&DMEntry = LocalDeclMap[&D];
200 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
201 DMEntry = DeclPtr;
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000202
203 // Emit debug info for local var declaration.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000204 if (CGDebugInfo *DI = CGM.getDebugInfo()) {
Daniel Dunbar6fc1f972008-10-17 16:15:48 +0000205 DI->setLocation(D.getLocation());
Chris Lattner562ce0a2008-11-10 06:08:34 +0000206 DI->EmitDeclareOfAutoVariable(&D, DeclPtr, Builder);
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000207 }
208
Chris Lattner4b009652007-07-25 00:24:17 +0000209 // If this local has an initializer, emit it now.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000210 if (const Expr *Init = D.getInit()) {
Chris Lattner222d3222007-08-26 07:29:23 +0000211 if (!hasAggregateLLVMType(Init->getType())) {
212 llvm::Value *V = EmitScalarExpr(Init);
Chris Lattner9a918dd2007-08-26 07:30:49 +0000213 Builder.CreateStore(V, DeclPtr, D.getType().isVolatileQualified());
Chris Lattnerde0908b2008-04-04 16:54:41 +0000214 } else if (Init->getType()->isAnyComplexType()) {
Chris Lattner8e1f6e02007-08-26 16:22:13 +0000215 EmitComplexExprIntoAddr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner64a113b2007-08-26 05:13:54 +0000216 } else {
Chris Lattner9a918dd2007-08-26 07:30:49 +0000217 EmitAggExpr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner64a113b2007-08-26 05:13:54 +0000218 }
Chris Lattner9fba49a2007-08-24 05:35:26 +0000219 }
Chris Lattner4b009652007-07-25 00:24:17 +0000220}
221
Chris Lattner85970f32008-05-08 05:58:21 +0000222/// Emit an alloca (or GlobalValue depending on target)
223/// for the specified parameter and set up LocalDeclMap.
Daniel Dunbarace33292008-08-16 03:19:19 +0000224void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg) {
225 // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl?
Sanjiv Guptafa451432008-10-31 09:52:39 +0000226 assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) &&
Daniel Dunbarace33292008-08-16 03:19:19 +0000227 "Invalid argument to EmitParmDecl");
Chris Lattner42a21742008-04-06 23:10:54 +0000228 QualType Ty = D.getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000229
230 llvm::Value *DeclPtr;
Eli Friedman62f67fd2008-02-15 12:20:59 +0000231 if (!Ty->isConstantSizeType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000232 // Variable sized values always are passed by-reference.
233 DeclPtr = Arg;
Chris Lattner85970f32008-05-08 05:58:21 +0000234 } else if (Target.useGlobalsForAutomaticVariables()) {
235 DeclPtr = GenerateStaticBlockVarDecl(D, true, ".arg.");
Chris Lattner4b009652007-07-25 00:24:17 +0000236 } else {
Dan Gohman377ba9f2008-05-22 22:12:56 +0000237 // A fixed sized single-value variable becomes an alloca in the entry block.
Chris Lattner4b009652007-07-25 00:24:17 +0000238 const llvm::Type *LTy = ConvertType(Ty);
Dan Gohman377ba9f2008-05-22 22:12:56 +0000239 if (LTy->isSingleValueType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000240 // TODO: Alignment
Chris Lattner6c5ec622008-11-24 04:00:27 +0000241 std::string Name = D.getNameAsString();
Daniel Dunbar04d35782008-09-17 00:51:38 +0000242 Name += ".addr";
243 DeclPtr = CreateTempAlloca(LTy, Name.c_str());
Chris Lattner4b009652007-07-25 00:24:17 +0000244
245 // Store the initial value into the alloca.
Eli Friedman2e630542008-06-13 23:01:12 +0000246 Builder.CreateStore(Arg, DeclPtr,Ty.isVolatileQualified());
Chris Lattner4b009652007-07-25 00:24:17 +0000247 } else {
248 // Otherwise, if this is an aggregate, just use the input pointer.
249 DeclPtr = Arg;
250 }
Chris Lattner6c5ec622008-11-24 04:00:27 +0000251 Arg->setName(D.getNameAsString());
Chris Lattner4b009652007-07-25 00:24:17 +0000252 }
253
254 llvm::Value *&DMEntry = LocalDeclMap[&D];
255 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
256 DMEntry = DeclPtr;
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000257
258 // Emit debug info for param declaration.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000259 if (CGDebugInfo *DI = CGM.getDebugInfo()) {
Daniel Dunbar6fc1f972008-10-17 16:15:48 +0000260 DI->setLocation(D.getLocation());
Chris Lattner562ce0a2008-11-10 06:08:34 +0000261 DI->EmitDeclareOfArgVariable(&D, DeclPtr, Builder);
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000262 }
Chris Lattner4b009652007-07-25 00:24:17 +0000263}
264