blob: 6774f33337ae34d7e809edb9294efdd4f7db818f [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,
80 const char *Separator) {
Chris Lattner8bcfc5b2008-04-06 23:10:54 +000081 QualType Ty = D.getType();
Eli Friedman3c2b3172008-02-15 12:20:59 +000082 assert(Ty->isConstantSizeType() && "VLAs can't be static");
Anders Carlsson1a86b332007-10-17 00:52:43 +000083
Chris Lattner19009e62008-01-09 18:47:25 +000084 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
Anders Carlsson1a86b332007-10-17 00:52:43 +000085 llvm::Constant *Init = 0;
Chris Lattner2621fd12008-05-08 05:58:21 +000086 if ((D.getInit() == 0) || NoInit) {
Anders Carlsson1a86b332007-10-17 00:52:43 +000087 Init = llvm::Constant::getNullValue(LTy);
Oliver Hunt28247232007-12-02 00:11:25 +000088 } else {
Eli Friedmanc9e8f602009-01-25 02:32:41 +000089 if (D.getInit()->isConstantInitializer(getContext()))
Anders Carlssone1b29ef2008-08-22 16:00:37 +000090 Init = CGM.EmitConstantExpr(D.getInit(), this);
91 else {
92 assert(getContext().getLangOptions().CPlusPlus &&
93 "only C++ supports non-constant static initializers!");
94 return GenerateStaticCXXBlockVarDecl(D);
95 }
Devang Patele40daa42007-10-26 17:50:58 +000096 }
97
Oliver Hunt28247232007-12-02 00:11:25 +000098 assert(Init && "Unable to create initialiser for static decl");
Nate Begeman8bd4afe2008-04-19 04:17:09 +000099
Chris Lattner352ffde22008-02-06 04:54:32 +0000100 std::string ContextName;
Anders Carlsson19567ee2008-08-25 01:38:19 +0000101 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurFuncDecl))
Chris Lattner39f34e92008-11-24 04:00:27 +0000102 ContextName = FD->getNameAsString();
Anders Carlsson19567ee2008-08-25 01:38:19 +0000103 else if (isa<ObjCMethodDecl>(CurFuncDecl))
104 ContextName = std::string(CurFn->getNameStart(),
105 CurFn->getNameStart() + CurFn->getNameLen());
Chris Lattner352ffde22008-02-06 04:54:32 +0000106 else
Anders Carlsson19567ee2008-08-25 01:38:19 +0000107 assert(0 && "Unknown context for block var decl");
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000108
Eli Friedman26590522008-06-08 01:23:18 +0000109 llvm::GlobalValue *GV =
110 new llvm::GlobalVariable(Init->getType(), false,
111 llvm::GlobalValue::InternalLinkage,
Chris Lattner39f34e92008-11-24 04:00:27 +0000112 Init, ContextName + Separator +D.getNameAsString(),
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000113 &CGM.getModule(), 0, Ty.getAddressSpace());
114
Chris Lattner2621fd12008-05-08 05:58:21 +0000115 return GV;
116}
117
118void CodeGenFunction::EmitStaticBlockVarDecl(const VarDecl &D) {
119
120 llvm::Value *&DMEntry = LocalDeclMap[&D];
121 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
122
123 llvm::GlobalValue *GV = GenerateStaticBlockVarDecl(D, false, ".");
124
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000125 if (const AnnotateAttr *AA = D.getAttr<AnnotateAttr>()) {
126 SourceManager &SM = CGM.getContext().getSourceManager();
127 llvm::Constant *Ann =
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000128 CGM.EmitAnnotateAttr(GV, AA,
129 SM.getInstantiationLineNumber(D.getLocation()));
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000130 CGM.AddAnnotation(Ann);
131 }
132
Eli Friedman26590522008-06-08 01:23:18 +0000133 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(D.getType());
134 const llvm::Type *LPtrTy =
135 llvm::PointerType::get(LTy, D.getType().getAddressSpace());
136 DMEntry = llvm::ConstantExpr::getBitCast(GV, LPtrTy);
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000137
138 // Emit global variable debug descriptor for static vars.
139 CGDebugInfo *DI = CGM.getDebugInfo();
140 if(DI) {
Daniel Dunbar66031a52008-10-17 16:15:48 +0000141 DI->setLocation(D.getLocation());
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000142 DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(GV), &D);
143 }
144
Anders Carlsson1a86b332007-10-17 00:52:43 +0000145}
146
Reid Spencer5f016e22007-07-11 17:01:13 +0000147/// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a
148/// variable declaration with auto, register, or no storage class specifier.
Chris Lattner2621fd12008-05-08 05:58:21 +0000149/// These turn into simple stack objects, or GlobalValues depending on target.
Steve Naroff248a7532008-04-15 22:42:06 +0000150void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) {
Chris Lattner8bcfc5b2008-04-06 23:10:54 +0000151 QualType Ty = D.getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000152
153 llvm::Value *DeclPtr;
Eli Friedman3c2b3172008-02-15 12:20:59 +0000154 if (Ty->isConstantSizeType()) {
Chris Lattner2621fd12008-05-08 05:58:21 +0000155 if (!Target.useGlobalsForAutomaticVariables()) {
156 // A normal fixed sized variable becomes an alloca in the entry block.
157 const llvm::Type *LTy = ConvertType(Ty);
Chris Lattner8ec03f52008-11-24 03:54:41 +0000158 llvm::AllocaInst *Alloc =
159 CreateTempAlloca(LTy, D.getIdentifier()->getName());
Eli Friedman77eedd62008-05-31 21:20:41 +0000160 unsigned align = getContext().getTypeAlign(Ty);
161 if (const AlignedAttr* AA = D.getAttr<AlignedAttr>())
162 align = std::max(align, AA->getAlignment());
163 Alloc->setAlignment(align >> 3);
164 DeclPtr = Alloc;
Chris Lattner2621fd12008-05-08 05:58:21 +0000165 } else {
166 // Targets that don't support recursion emit locals as globals.
167 const char *Class =
168 D.getStorageClass() == VarDecl::Register ? ".reg." : ".auto.";
169 DeclPtr = GenerateStaticBlockVarDecl(D, true, Class);
170 }
Anders Carlsson60d35412008-12-20 20:46:34 +0000171
172 if (Ty->isVariablyModifiedType())
173 EmitVLASize(Ty);
Reid Spencer5f016e22007-07-11 17:01:13 +0000174 } else {
Anders Carlsson5d463152008-12-12 07:38:43 +0000175 if (!StackSaveValues.back()) {
176 // Save the stack.
177 const llvm::Type *LTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
178 llvm::Value *Stack = CreateTempAlloca(LTy, "saved_stack");
179
180 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave);
181 llvm::Value *V = Builder.CreateCall(F);
182
183 Builder.CreateStore(V, Stack);
184
185 StackSaveValues.back() = Stack;
186 }
187 // Get the element type.
188 const llvm::Type *LElemTy = ConvertType(Ty);
189 const llvm::Type *LElemPtrTy =
190 llvm::PointerType::get(LElemTy, D.getType().getAddressSpace());
191
Anders Carlsson60d35412008-12-20 20:46:34 +0000192 llvm::Value *VLASize = EmitVLASize(Ty);
Anders Carlsson5d463152008-12-12 07:38:43 +0000193
194 // Allocate memory for the array.
195 llvm::Value *VLA = Builder.CreateAlloca(llvm::Type::Int8Ty, VLASize, "vla");
Eli Friedman8f39f5e2008-12-20 23:11:59 +0000196 DeclPtr = Builder.CreateBitCast(VLA, LElemPtrTy, "tmp");
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 }
Eli Friedman8f39f5e2008-12-20 23:11:59 +0000198
Reid Spencer5f016e22007-07-11 17:01:13 +0000199 llvm::Value *&DMEntry = LocalDeclMap[&D];
200 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
201 DMEntry = DeclPtr;
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000202
203 // Emit debug info for local var declaration.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000204 if (CGDebugInfo *DI = CGM.getDebugInfo()) {
Daniel Dunbar66031a52008-10-17 16:15:48 +0000205 DI->setLocation(D.getLocation());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000206 DI->EmitDeclareOfAutoVariable(&D, DeclPtr, Builder);
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000207 }
208
Chris Lattner19785962007-07-12 00:39:48 +0000209 // If this local has an initializer, emit it now.
Chris Lattner7f02f722007-08-24 05:35:26 +0000210 if (const Expr *Init = D.getInit()) {
Chris Lattnerd31beb12007-08-26 07:29:23 +0000211 if (!hasAggregateLLVMType(Init->getType())) {
212 llvm::Value *V = EmitScalarExpr(Init);
Chris Lattner8b2f3b72007-08-26 07:30:49 +0000213 Builder.CreateStore(V, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9b2dc282008-04-04 16:54:41 +0000214 } else if (Init->getType()->isAnyComplexType()) {
Chris Lattner190dbe22007-08-26 16:22:13 +0000215 EmitComplexExprIntoAddr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9a19edf2007-08-26 05:13:54 +0000216 } else {
Chris Lattner8b2f3b72007-08-26 07:30:49 +0000217 EmitAggExpr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9a19edf2007-08-26 05:13:54 +0000218 }
Chris Lattner7f02f722007-08-24 05:35:26 +0000219 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000220}
221
Chris Lattner2621fd12008-05-08 05:58:21 +0000222/// Emit an alloca (or GlobalValue depending on target)
223/// for the specified parameter and set up LocalDeclMap.
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000224void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg) {
225 // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl?
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000226 assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) &&
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000227 "Invalid argument to EmitParmDecl");
Chris Lattner8bcfc5b2008-04-06 23:10:54 +0000228 QualType Ty = D.getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000229
230 llvm::Value *DeclPtr;
Eli Friedman3c2b3172008-02-15 12:20:59 +0000231 if (!Ty->isConstantSizeType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000232 // Variable sized values always are passed by-reference.
233 DeclPtr = Arg;
Chris Lattner2621fd12008-05-08 05:58:21 +0000234 } else if (Target.useGlobalsForAutomaticVariables()) {
235 DeclPtr = GenerateStaticBlockVarDecl(D, true, ".arg.");
Reid Spencer5f016e22007-07-11 17:01:13 +0000236 } else {
Dan Gohmand79a7262008-05-22 22:12:56 +0000237 // A fixed sized single-value variable becomes an alloca in the entry block.
Reid Spencer5f016e22007-07-11 17:01:13 +0000238 const llvm::Type *LTy = ConvertType(Ty);
Dan Gohmand79a7262008-05-22 22:12:56 +0000239 if (LTy->isSingleValueType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000240 // TODO: Alignment
Chris Lattner39f34e92008-11-24 04:00:27 +0000241 std::string Name = D.getNameAsString();
Daniel Dunbar56273772008-09-17 00:51:38 +0000242 Name += ".addr";
243 DeclPtr = CreateTempAlloca(LTy, Name.c_str());
Reid Spencer5f016e22007-07-11 17:01:13 +0000244
245 // Store the initial value into the alloca.
Eli Friedman1e692ac2008-06-13 23:01:12 +0000246 Builder.CreateStore(Arg, DeclPtr,Ty.isVolatileQualified());
Reid Spencer5f016e22007-07-11 17:01:13 +0000247 } else {
248 // Otherwise, if this is an aggregate, just use the input pointer.
249 DeclPtr = Arg;
250 }
Chris Lattner39f34e92008-11-24 04:00:27 +0000251 Arg->setName(D.getNameAsString());
Reid Spencer5f016e22007-07-11 17:01:13 +0000252 }
253
254 llvm::Value *&DMEntry = LocalDeclMap[&D];
255 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
256 DMEntry = DeclPtr;
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000257
258 // Emit debug info for param declaration.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000259 if (CGDebugInfo *DI = CGM.getDebugInfo()) {
Daniel Dunbar66031a52008-10-17 16:15:48 +0000260 DI->setLocation(D.getLocation());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000261 DI->EmitDeclareOfArgVariable(&D, DeclPtr, Builder);
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000262 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000263}
264