blob: 1c72ba12280cb0a06c477301cfc6aec2c947bace [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!");
34 case Decl::Typedef: // typedef int X;
35 case Decl::Function: // void X();
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +000036 case Decl::Record: // struct/union/class X;
Reid Spencer5f016e22007-07-11 17:01:13 +000037 case Decl::Enum: // enum X;
Daniel Dunbar662174c82008-08-29 17:28:43 +000038 case Decl::EnumConstant: // enum ? { X = ? }
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +000039 case Decl::CXXRecord: // struct/union/class X; [C++]
Reid Spencer5f016e22007-07-11 17:01:13 +000040 // None of these decls require codegen support.
41 return;
42
Daniel Dunbar662174c82008-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);
Reid Spencer5f016e22007-07-11 17:01:13 +000048 }
Daniel Dunbar662174c82008-08-29 17:28:43 +000049 }
Reid Spencer5f016e22007-07-11 17:01:13 +000050}
51
52/// EmitBlockVarDecl - This method handles emission of any variable declaration
53/// inside a function, including static vars etc.
Steve Naroff248a7532008-04-15 22:42:06 +000054void CodeGenFunction::EmitBlockVarDecl(const VarDecl &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +000055 switch (D.getStorageClass()) {
56 case VarDecl::Static:
Anders Carlsson1a86b332007-10-17 00:52:43 +000057 return EmitStaticBlockVarDecl(D);
Reid Spencer5f016e22007-07-11 17:01:13 +000058 case VarDecl::Extern:
Lauro Ramos Venanciofea90b82008-02-16 22:30:38 +000059 // Don't emit it now, allow it to be emitted lazily on its first use.
60 return;
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner2621fd12008-05-08 05:58:21 +000070llvm::GlobalValue *
71CodeGenFunction::GenerateStaticBlockVarDecl(const VarDecl &D,
72 bool NoInit,
73 const char *Separator) {
Chris Lattner8bcfc5b2008-04-06 23:10:54 +000074 QualType Ty = D.getType();
Eli Friedman3c2b3172008-02-15 12:20:59 +000075 assert(Ty->isConstantSizeType() && "VLAs can't be static");
Anders Carlsson1a86b332007-10-17 00:52:43 +000076
Chris Lattner19009e62008-01-09 18:47:25 +000077 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
Anders Carlsson1a86b332007-10-17 00:52:43 +000078 llvm::Constant *Init = 0;
Chris Lattner2621fd12008-05-08 05:58:21 +000079 if ((D.getInit() == 0) || NoInit) {
Anders Carlsson1a86b332007-10-17 00:52:43 +000080 Init = llvm::Constant::getNullValue(LTy);
Oliver Hunt28247232007-12-02 00:11:25 +000081 } else {
Anders Carlssone1b29ef2008-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 Patele40daa42007-10-26 17:50:58 +000089 }
90
Oliver Hunt28247232007-12-02 00:11:25 +000091 assert(Init && "Unable to create initialiser for static decl");
Nate Begeman8bd4afe2008-04-19 04:17:09 +000092
Chris Lattner352ffde22008-02-06 04:54:32 +000093 std::string ContextName;
Anders Carlsson19567ee2008-08-25 01:38:19 +000094 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurFuncDecl))
Chris Lattner39f34e92008-11-24 04:00:27 +000095 ContextName = FD->getNameAsString();
Anders Carlsson19567ee2008-08-25 01:38:19 +000096 else if (isa<ObjCMethodDecl>(CurFuncDecl))
97 ContextName = std::string(CurFn->getNameStart(),
98 CurFn->getNameStart() + CurFn->getNameLen());
Chris Lattner352ffde22008-02-06 04:54:32 +000099 else
Anders Carlsson19567ee2008-08-25 01:38:19 +0000100 assert(0 && "Unknown context for block var decl");
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000101
Eli Friedman26590522008-06-08 01:23:18 +0000102 llvm::GlobalValue *GV =
103 new llvm::GlobalVariable(Init->getType(), false,
104 llvm::GlobalValue::InternalLinkage,
Chris Lattner39f34e92008-11-24 04:00:27 +0000105 Init, ContextName + Separator +D.getNameAsString(),
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000106 &CGM.getModule(), 0, Ty.getAddressSpace());
107
Chris Lattner2621fd12008-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 Begeman8bd4afe2008-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 Friedman26590522008-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 Gupta686226b2008-06-05 08:59:10 +0000129
130 // Emit global variable debug descriptor for static vars.
131 CGDebugInfo *DI = CGM.getDebugInfo();
132 if(DI) {
Daniel Dunbar66031a52008-10-17 16:15:48 +0000133 DI->setLocation(D.getLocation());
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000134 DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(GV), &D);
135 }
136
Anders Carlsson1a86b332007-10-17 00:52:43 +0000137}
138
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner2621fd12008-05-08 05:58:21 +0000141/// These turn into simple stack objects, or GlobalValues depending on target.
Steve Naroff248a7532008-04-15 22:42:06 +0000142void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) {
Chris Lattner8bcfc5b2008-04-06 23:10:54 +0000143 QualType Ty = D.getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000144
145 llvm::Value *DeclPtr;
Eli Friedman3c2b3172008-02-15 12:20:59 +0000146 if (Ty->isConstantSizeType()) {
Chris Lattner2621fd12008-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 Lattner8ec03f52008-11-24 03:54:41 +0000150 llvm::AllocaInst *Alloc =
151 CreateTempAlloca(LTy, D.getIdentifier()->getName());
Eli Friedman77eedd62008-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 Lattner2621fd12008-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 Lattner71e38c42008-12-13 18:58:59 +0000163 } else if (1) {
164 // FIXME: The code below is disabled because is causes a regression in the
165 // testsuite.
166 CGM.ErrorUnsupported(&D, "variable-length array");
167
168 const llvm::Type *LTy = ConvertType(Ty);
169 llvm::AllocaInst *Alloc =
170 CreateTempAlloca(LTy, D.getIdentifier()->getName());
171 DeclPtr = Alloc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 } else {
Anders Carlsson5d463152008-12-12 07:38:43 +0000173 const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty);
Daniel Dunbar662174c82008-08-29 17:28:43 +0000174
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
192 llvm::Value *VLASize = GetVLASize(VAT);
193
194 // Allocate memory for the array.
195 llvm::Value *VLA = Builder.CreateAlloca(llvm::Type::Int8Ty, VLASize, "vla");
196 VLA = Builder.CreateBitCast(VLA, LElemPtrTy, "tmp");
197
Chris Lattner8ec03f52008-11-24 03:54:41 +0000198 llvm::AllocaInst *Alloc =
Anders Carlsson5d463152008-12-12 07:38:43 +0000199 CreateTempAlloca(LElemPtrTy, D.getIdentifier()->getName());
200
201 // FIXME: Volatile?
202 Builder.CreateStore(VLA, Alloc);
203
Daniel Dunbar662174c82008-08-29 17:28:43 +0000204 DeclPtr = Alloc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000205 }
206
207 llvm::Value *&DMEntry = LocalDeclMap[&D];
208 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
209 DMEntry = DeclPtr;
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000210
211 // Emit debug info for local var declaration.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000212 if (CGDebugInfo *DI = CGM.getDebugInfo()) {
Daniel Dunbar66031a52008-10-17 16:15:48 +0000213 DI->setLocation(D.getLocation());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000214 DI->EmitDeclareOfAutoVariable(&D, DeclPtr, Builder);
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000215 }
216
Chris Lattner19785962007-07-12 00:39:48 +0000217 // If this local has an initializer, emit it now.
Chris Lattner7f02f722007-08-24 05:35:26 +0000218 if (const Expr *Init = D.getInit()) {
Chris Lattnerd31beb12007-08-26 07:29:23 +0000219 if (!hasAggregateLLVMType(Init->getType())) {
220 llvm::Value *V = EmitScalarExpr(Init);
Chris Lattner8b2f3b72007-08-26 07:30:49 +0000221 Builder.CreateStore(V, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9b2dc282008-04-04 16:54:41 +0000222 } else if (Init->getType()->isAnyComplexType()) {
Chris Lattner190dbe22007-08-26 16:22:13 +0000223 EmitComplexExprIntoAddr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9a19edf2007-08-26 05:13:54 +0000224 } else {
Chris Lattner8b2f3b72007-08-26 07:30:49 +0000225 EmitAggExpr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9a19edf2007-08-26 05:13:54 +0000226 }
Chris Lattner7f02f722007-08-24 05:35:26 +0000227 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000228}
229
Chris Lattner2621fd12008-05-08 05:58:21 +0000230/// Emit an alloca (or GlobalValue depending on target)
231/// for the specified parameter and set up LocalDeclMap.
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000232void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg) {
233 // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl?
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000234 assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) &&
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000235 "Invalid argument to EmitParmDecl");
Chris Lattner8bcfc5b2008-04-06 23:10:54 +0000236 QualType Ty = D.getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000237
238 llvm::Value *DeclPtr;
Eli Friedman3c2b3172008-02-15 12:20:59 +0000239 if (!Ty->isConstantSizeType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000240 // Variable sized values always are passed by-reference.
241 DeclPtr = Arg;
Chris Lattner2621fd12008-05-08 05:58:21 +0000242 } else if (Target.useGlobalsForAutomaticVariables()) {
243 DeclPtr = GenerateStaticBlockVarDecl(D, true, ".arg.");
Reid Spencer5f016e22007-07-11 17:01:13 +0000244 } else {
Dan Gohmand79a7262008-05-22 22:12:56 +0000245 // A fixed sized single-value variable becomes an alloca in the entry block.
Reid Spencer5f016e22007-07-11 17:01:13 +0000246 const llvm::Type *LTy = ConvertType(Ty);
Dan Gohmand79a7262008-05-22 22:12:56 +0000247 if (LTy->isSingleValueType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000248 // TODO: Alignment
Chris Lattner39f34e92008-11-24 04:00:27 +0000249 std::string Name = D.getNameAsString();
Daniel Dunbar56273772008-09-17 00:51:38 +0000250 Name += ".addr";
251 DeclPtr = CreateTempAlloca(LTy, Name.c_str());
Reid Spencer5f016e22007-07-11 17:01:13 +0000252
253 // Store the initial value into the alloca.
Eli Friedman1e692ac2008-06-13 23:01:12 +0000254 Builder.CreateStore(Arg, DeclPtr,Ty.isVolatileQualified());
Reid Spencer5f016e22007-07-11 17:01:13 +0000255 } else {
256 // Otherwise, if this is an aggregate, just use the input pointer.
257 DeclPtr = Arg;
258 }
Chris Lattner39f34e92008-11-24 04:00:27 +0000259 Arg->setName(D.getNameAsString());
Reid Spencer5f016e22007-07-11 17:01:13 +0000260 }
261
262 llvm::Value *&DMEntry = LocalDeclMap[&D];
263 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
264 DMEntry = DeclPtr;
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000265
266 // Emit debug info for param declaration.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000267 if (CGDebugInfo *DI = CGM.getDebugInfo()) {
Daniel Dunbar66031a52008-10-17 16:15:48 +0000268 DI->setLocation(D.getLocation());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000269 DI->EmitDeclareOfArgVariable(&D, DeclPtr, Builder);
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000270 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000271}
272