blob: 6774f33337ae34d7e809edb9294efdd4f7db818f [file] [log] [blame]
Chris Lattner84915fa2007-06-02 04:16:21 +00001//===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattner84915fa2007-06-02 04:16:21 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Decl nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
Sanjiv Gupta18de6242008-05-30 10:30:31 +000014#include "CGDebugInfo.h"
Chris Lattner84915fa2007-06-02 04:16:21 +000015#include "CodeGenFunction.h"
Anders Carlssonf94cd1f2007-10-17 00:52:43 +000016#include "CodeGenModule.h"
Daniel Dunbarad319a72008-08-11 05:00:27 +000017#include "clang/AST/ASTContext.h"
Daniel Dunbar6e8aa532008-08-11 05:35:13 +000018#include "clang/AST/Decl.h"
Anders Carlsson4c03d982008-08-25 01:38:19 +000019#include "clang/AST/DeclObjC.h"
Nate Begemanfaae0812008-04-19 04:17:09 +000020#include "clang/Basic/SourceManager.h"
Chris Lattnerb781dc792008-05-08 05:58:21 +000021#include "clang/Basic/TargetInfo.h"
Anders Carlssonf94cd1f2007-10-17 00:52:43 +000022#include "llvm/GlobalVariable.h"
Anders Carlsson30032882008-12-12 07:38:43 +000023#include "llvm/Intrinsics.h"
Chris Lattner53621a52007-06-13 20:44:40 +000024#include "llvm/Type.h"
Chris Lattner84915fa2007-06-02 04:16:21 +000025using namespace clang;
26using namespace CodeGen;
27
28
Chris Lattner1ad38f82007-06-09 01:20:56 +000029void CodeGenFunction::EmitDecl(const Decl &D) {
Chris Lattner1ad38f82007-06-09 01:20:56 +000030 switch (D.getKind()) {
Chris Lattner84915fa2007-06-02 04:16:21 +000031 default: assert(0 && "Unknown decl kind!");
Chris Lattnerba9dddb2007-10-08 21:37:32 +000032 case Decl::ParmVar:
Chris Lattner84915fa2007-06-02 04:16:21 +000033 assert(0 && "Parmdecls should not be in declstmts!");
Chris Lattner84915fa2007-06-02 04:16:21 +000034 case Decl::Function: // void X();
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +000035 case Decl::Record: // struct/union/class X;
Chris Lattner84915fa2007-06-02 04:16:21 +000036 case Decl::Enum: // enum X;
Daniel Dunbara7998072008-08-29 17:28:43 +000037 case Decl::EnumConstant: // enum ? { X = ? }
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +000038 case Decl::CXXRecord: // struct/union/class X; [C++]
Chris Lattner84915fa2007-06-02 04:16:21 +000039 // None of these decls require codegen support.
40 return;
41
Daniel Dunbara7998072008-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);
Chris Lattner84915fa2007-06-02 04:16:21 +000047 }
Anders Carlsson5d985f52008-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 Dunbara7998072008-08-29 17:28:43 +000056 }
Chris Lattner84915fa2007-06-02 04:16:21 +000057}
Chris Lattner03df1222007-06-02 04:53:11 +000058
59/// EmitBlockVarDecl - This method handles emission of any variable declaration
60/// inside a function, including static vars etc.
Steve Naroff08899ff2008-04-15 22:42:06 +000061void CodeGenFunction::EmitBlockVarDecl(const VarDecl &D) {
Chris Lattner03df1222007-06-02 04:53:11 +000062 switch (D.getStorageClass()) {
63 case VarDecl::Static:
Anders Carlssonf94cd1f2007-10-17 00:52:43 +000064 return EmitStaticBlockVarDecl(D);
Chris Lattner03df1222007-06-02 04:53:11 +000065 case VarDecl::Extern:
Lauro Ramos Venanciobada8d42008-02-16 22:30:38 +000066 // Don't emit it now, allow it to be emitted lazily on its first use.
67 return;
Chris Lattner03df1222007-06-02 04:53:11 +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 Lattnerb781dc792008-05-08 05:58:21 +000077llvm::GlobalValue *
78CodeGenFunction::GenerateStaticBlockVarDecl(const VarDecl &D,
79 bool NoInit,
80 const char *Separator) {
Chris Lattnerfc4379f2008-04-06 23:10:54 +000081 QualType Ty = D.getType();
Eli Friedmana682d392008-02-15 12:20:59 +000082 assert(Ty->isConstantSizeType() && "VLAs can't be static");
Anders Carlssonf94cd1f2007-10-17 00:52:43 +000083
Chris Lattner41a1ef02008-01-09 18:47:25 +000084 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
Anders Carlssonf94cd1f2007-10-17 00:52:43 +000085 llvm::Constant *Init = 0;
Chris Lattnerb781dc792008-05-08 05:58:21 +000086 if ((D.getInit() == 0) || NoInit) {
Anders Carlssonf94cd1f2007-10-17 00:52:43 +000087 Init = llvm::Constant::getNullValue(LTy);
Oliver Huntaefc8fd2007-12-02 00:11:25 +000088 } else {
Eli Friedman7139af42009-01-25 02:32:41 +000089 if (D.getInit()->isConstantInitializer(getContext()))
Anders Carlsson87fc5a52008-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 Patelffdb07c2007-10-26 17:50:58 +000096 }
97
Oliver Huntaefc8fd2007-12-02 00:11:25 +000098 assert(Init && "Unable to create initialiser for static decl");
Nate Begemanfaae0812008-04-19 04:17:09 +000099
Chris Lattner8b945ee2008-02-06 04:54:32 +0000100 std::string ContextName;
Anders Carlsson4c03d982008-08-25 01:38:19 +0000101 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurFuncDecl))
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000102 ContextName = FD->getNameAsString();
Anders Carlsson4c03d982008-08-25 01:38:19 +0000103 else if (isa<ObjCMethodDecl>(CurFuncDecl))
104 ContextName = std::string(CurFn->getNameStart(),
105 CurFn->getNameStart() + CurFn->getNameLen());
Chris Lattner8b945ee2008-02-06 04:54:32 +0000106 else
Anders Carlsson4c03d982008-08-25 01:38:19 +0000107 assert(0 && "Unknown context for block var decl");
Nate Begemanfaae0812008-04-19 04:17:09 +0000108
Eli Friedmanc98a7ad2008-06-08 01:23:18 +0000109 llvm::GlobalValue *GV =
110 new llvm::GlobalVariable(Init->getType(), false,
111 llvm::GlobalValue::InternalLinkage,
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000112 Init, ContextName + Separator +D.getNameAsString(),
Nate Begemanfaae0812008-04-19 04:17:09 +0000113 &CGM.getModule(), 0, Ty.getAddressSpace());
114
Chris Lattnerb781dc792008-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 Begemanfaae0812008-04-19 04:17:09 +0000125 if (const AnnotateAttr *AA = D.getAttr<AnnotateAttr>()) {
126 SourceManager &SM = CGM.getContext().getSourceManager();
127 llvm::Constant *Ann =
Chris Lattner8a425862009-01-16 07:36:28 +0000128 CGM.EmitAnnotateAttr(GV, AA,
129 SM.getInstantiationLineNumber(D.getLocation()));
Nate Begemanfaae0812008-04-19 04:17:09 +0000130 CGM.AddAnnotation(Ann);
131 }
132
Eli Friedmanc98a7ad2008-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 Gupta158143a2008-06-05 08:59:10 +0000137
138 // Emit global variable debug descriptor for static vars.
139 CGDebugInfo *DI = CGM.getDebugInfo();
140 if(DI) {
Daniel Dunbarb9fd9022008-10-17 16:15:48 +0000141 DI->setLocation(D.getLocation());
Sanjiv Gupta158143a2008-06-05 08:59:10 +0000142 DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(GV), &D);
143 }
144
Anders Carlssonf94cd1f2007-10-17 00:52:43 +0000145}
146
Chris Lattner03df1222007-06-02 04:53:11 +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 Lattnerb781dc792008-05-08 05:58:21 +0000149/// These turn into simple stack objects, or GlobalValues depending on target.
Steve Naroff08899ff2008-04-15 22:42:06 +0000150void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) {
Chris Lattnerfc4379f2008-04-06 23:10:54 +0000151 QualType Ty = D.getType();
Chris Lattner03df1222007-06-02 04:53:11 +0000152
153 llvm::Value *DeclPtr;
Eli Friedmana682d392008-02-15 12:20:59 +0000154 if (Ty->isConstantSizeType()) {
Chris Lattnerb781dc792008-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 Lattner86d7d912008-11-24 03:54:41 +0000158 llvm::AllocaInst *Alloc =
159 CreateTempAlloca(LTy, D.getIdentifier()->getName());
Eli Friedman252e5f12008-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 Lattnerb781dc792008-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 Carlsson8a01b792008-12-20 20:46:34 +0000171
172 if (Ty->isVariablyModifiedType())
173 EmitVLASize(Ty);
Chris Lattner03df1222007-06-02 04:53:11 +0000174 } else {
Anders Carlsson30032882008-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 Carlsson8a01b792008-12-20 20:46:34 +0000192 llvm::Value *VLASize = EmitVLASize(Ty);
Anders Carlsson30032882008-12-12 07:38:43 +0000193
194 // Allocate memory for the array.
195 llvm::Value *VLA = Builder.CreateAlloca(llvm::Type::Int8Ty, VLASize, "vla");
Eli Friedmanf4084702008-12-20 23:11:59 +0000196 DeclPtr = Builder.CreateBitCast(VLA, LElemPtrTy, "tmp");
Chris Lattner03df1222007-06-02 04:53:11 +0000197 }
Eli Friedmanf4084702008-12-20 23:11:59 +0000198
Chris Lattner03df1222007-06-02 04:53:11 +0000199 llvm::Value *&DMEntry = LocalDeclMap[&D];
200 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
201 DMEntry = DeclPtr;
Sanjiv Gupta18de6242008-05-30 10:30:31 +0000202
203 // Emit debug info for local var declaration.
Chris Lattneraffb3732008-11-10 06:08:34 +0000204 if (CGDebugInfo *DI = CGM.getDebugInfo()) {
Daniel Dunbarb9fd9022008-10-17 16:15:48 +0000205 DI->setLocation(D.getLocation());
Chris Lattneraffb3732008-11-10 06:08:34 +0000206 DI->EmitDeclareOfAutoVariable(&D, DeclPtr, Builder);
Sanjiv Gupta18de6242008-05-30 10:30:31 +0000207 }
208
Chris Lattner07eb7332007-07-12 00:39:48 +0000209 // If this local has an initializer, emit it now.
Chris Lattner2da04b32007-08-24 05:35:26 +0000210 if (const Expr *Init = D.getInit()) {
Chris Lattnerb753f662007-08-26 07:29:23 +0000211 if (!hasAggregateLLVMType(Init->getType())) {
212 llvm::Value *V = EmitScalarExpr(Init);
Chris Lattnerf6dcc9d2007-08-26 07:30:49 +0000213 Builder.CreateStore(V, DeclPtr, D.getType().isVolatileQualified());
Chris Lattnerf3bc75a2008-04-04 16:54:41 +0000214 } else if (Init->getType()->isAnyComplexType()) {
Chris Lattnerb84bb952007-08-26 16:22:13 +0000215 EmitComplexExprIntoAddr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9de95272007-08-26 05:13:54 +0000216 } else {
Chris Lattnerf6dcc9d2007-08-26 07:30:49 +0000217 EmitAggExpr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9de95272007-08-26 05:13:54 +0000218 }
Chris Lattner2da04b32007-08-24 05:35:26 +0000219 }
Chris Lattner03df1222007-06-02 04:53:11 +0000220}
Chris Lattner53621a52007-06-13 20:44:40 +0000221
Chris Lattnerb781dc792008-05-08 05:58:21 +0000222/// Emit an alloca (or GlobalValue depending on target)
223/// for the specified parameter and set up LocalDeclMap.
Daniel Dunbara94ecd22008-08-16 03:19:19 +0000224void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg) {
225 // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl?
Sanjiv Guptad7959242008-10-31 09:52:39 +0000226 assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) &&
Daniel Dunbara94ecd22008-08-16 03:19:19 +0000227 "Invalid argument to EmitParmDecl");
Chris Lattnerfc4379f2008-04-06 23:10:54 +0000228 QualType Ty = D.getType();
Chris Lattner53621a52007-06-13 20:44:40 +0000229
230 llvm::Value *DeclPtr;
Eli Friedmana682d392008-02-15 12:20:59 +0000231 if (!Ty->isConstantSizeType()) {
Chris Lattner53621a52007-06-13 20:44:40 +0000232 // Variable sized values always are passed by-reference.
233 DeclPtr = Arg;
Chris Lattnerb781dc792008-05-08 05:58:21 +0000234 } else if (Target.useGlobalsForAutomaticVariables()) {
235 DeclPtr = GenerateStaticBlockVarDecl(D, true, ".arg.");
Chris Lattner53621a52007-06-13 20:44:40 +0000236 } else {
Dan Gohman5d309752008-05-22 22:12:56 +0000237 // A fixed sized single-value variable becomes an alloca in the entry block.
Chris Lattnerf033c142007-06-22 19:05:19 +0000238 const llvm::Type *LTy = ConvertType(Ty);
Dan Gohman5d309752008-05-22 22:12:56 +0000239 if (LTy->isSingleValueType()) {
Chris Lattner53621a52007-06-13 20:44:40 +0000240 // TODO: Alignment
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000241 std::string Name = D.getNameAsString();
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000242 Name += ".addr";
243 DeclPtr = CreateTempAlloca(LTy, Name.c_str());
Chris Lattner53621a52007-06-13 20:44:40 +0000244
245 // Store the initial value into the alloca.
Eli Friedman327944b2008-06-13 23:01:12 +0000246 Builder.CreateStore(Arg, DeclPtr,Ty.isVolatileQualified());
Chris Lattner53621a52007-06-13 20:44:40 +0000247 } else {
248 // Otherwise, if this is an aggregate, just use the input pointer.
Chris Lattner0fb84652007-06-22 18:57:44 +0000249 DeclPtr = Arg;
Chris Lattner53621a52007-06-13 20:44:40 +0000250 }
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000251 Arg->setName(D.getNameAsString());
Chris Lattner53621a52007-06-13 20:44:40 +0000252 }
253
254 llvm::Value *&DMEntry = LocalDeclMap[&D];
255 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
256 DMEntry = DeclPtr;
Sanjiv Gupta18de6242008-05-30 10:30:31 +0000257
258 // Emit debug info for param declaration.
Chris Lattneraffb3732008-11-10 06:08:34 +0000259 if (CGDebugInfo *DI = CGM.getDebugInfo()) {
Daniel Dunbarb9fd9022008-10-17 16:15:48 +0000260 DI->setLocation(D.getLocation());
Chris Lattneraffb3732008-11-10 06:08:34 +0000261 DI->EmitDeclareOfArgVariable(&D, DeclPtr, Builder);
Sanjiv Gupta18de6242008-05-30 10:30:31 +0000262 }
Chris Lattner53621a52007-06-13 20:44:40 +0000263}
264