blob: 6ab227d95a8f406b648ea3254c20b86ea26e2eb7 [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"
Reid Spencer5f016e22007-07-11 17:01:13 +000023#include "llvm/Type.h"
Sanjiv Guptacc9b1632008-05-30 10:30:31 +000024#include "llvm/Support/Dwarf.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000025using 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();
36 case Decl::Struct: // struct X;
37 case Decl::Union: // union X;
38 case Decl::Class: // class X;
39 case Decl::Enum: // enum X;
Daniel Dunbar662174c82008-08-29 17:28:43 +000040 case Decl::EnumConstant: // enum ? { X = ? }
Argyrios Kyrtzidisa7a6dc02008-06-09 23:42:47 +000041 case Decl::CXXStruct: // struct X; [C++]
42 case Decl::CXXUnion: // union X; [C++]
43 case Decl::CXXClass: // class X; [C++]
Reid Spencer5f016e22007-07-11 17:01:13 +000044 // None of these decls require codegen support.
45 return;
46
Daniel Dunbar662174c82008-08-29 17:28:43 +000047 case Decl::Var: {
48 const VarDecl &VD = cast<VarDecl>(D);
49 assert(VD.isBlockVarDecl() &&
50 "Should not see file-scope variables inside a function!");
51 return EmitBlockVarDecl(VD);
Reid Spencer5f016e22007-07-11 17:01:13 +000052 }
Daniel Dunbar662174c82008-08-29 17:28:43 +000053 }
Reid Spencer5f016e22007-07-11 17:01:13 +000054}
55
56/// EmitBlockVarDecl - This method handles emission of any variable declaration
57/// inside a function, including static vars etc.
Steve Naroff248a7532008-04-15 22:42:06 +000058void CodeGenFunction::EmitBlockVarDecl(const VarDecl &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +000059 switch (D.getStorageClass()) {
60 case VarDecl::Static:
Anders Carlsson1a86b332007-10-17 00:52:43 +000061 return EmitStaticBlockVarDecl(D);
Reid Spencer5f016e22007-07-11 17:01:13 +000062 case VarDecl::Extern:
Lauro Ramos Venanciofea90b82008-02-16 22:30:38 +000063 // Don't emit it now, allow it to be emitted lazily on its first use.
64 return;
Reid Spencer5f016e22007-07-11 17:01:13 +000065 default:
66 assert((D.getStorageClass() == VarDecl::None ||
67 D.getStorageClass() == VarDecl::Auto ||
68 D.getStorageClass() == VarDecl::Register) &&
69 "Unknown storage class");
70 return EmitLocalBlockVarDecl(D);
71 }
72}
73
Chris Lattner2621fd12008-05-08 05:58:21 +000074llvm::GlobalValue *
75CodeGenFunction::GenerateStaticBlockVarDecl(const VarDecl &D,
76 bool NoInit,
77 const char *Separator) {
Chris Lattner8bcfc5b2008-04-06 23:10:54 +000078 QualType Ty = D.getType();
Eli Friedman3c2b3172008-02-15 12:20:59 +000079 assert(Ty->isConstantSizeType() && "VLAs can't be static");
Anders Carlsson1a86b332007-10-17 00:52:43 +000080
Chris Lattner19009e62008-01-09 18:47:25 +000081 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
Anders Carlsson1a86b332007-10-17 00:52:43 +000082 llvm::Constant *Init = 0;
Chris Lattner2621fd12008-05-08 05:58:21 +000083 if ((D.getInit() == 0) || NoInit) {
Anders Carlsson1a86b332007-10-17 00:52:43 +000084 Init = llvm::Constant::getNullValue(LTy);
Oliver Hunt28247232007-12-02 00:11:25 +000085 } else {
Anders Carlssone1b29ef2008-08-22 16:00:37 +000086 if (D.getInit()->isConstantExpr(getContext(), 0))
87 Init = CGM.EmitConstantExpr(D.getInit(), this);
88 else {
89 assert(getContext().getLangOptions().CPlusPlus &&
90 "only C++ supports non-constant static initializers!");
91 return GenerateStaticCXXBlockVarDecl(D);
92 }
Devang Patele40daa42007-10-26 17:50:58 +000093 }
94
Oliver Hunt28247232007-12-02 00:11:25 +000095 assert(Init && "Unable to create initialiser for static decl");
Nate Begeman8bd4afe2008-04-19 04:17:09 +000096
Chris Lattner352ffde22008-02-06 04:54:32 +000097 std::string ContextName;
Anders Carlsson19567ee2008-08-25 01:38:19 +000098 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurFuncDecl))
Chris Lattnerc8aa5f12008-04-04 04:07:35 +000099 ContextName = FD->getName();
Anders Carlsson19567ee2008-08-25 01:38:19 +0000100 else if (isa<ObjCMethodDecl>(CurFuncDecl))
101 ContextName = std::string(CurFn->getNameStart(),
102 CurFn->getNameStart() + CurFn->getNameLen());
Chris Lattner352ffde22008-02-06 04:54:32 +0000103 else
Anders Carlsson19567ee2008-08-25 01:38:19 +0000104 assert(0 && "Unknown context for block var decl");
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000105
Eli Friedman26590522008-06-08 01:23:18 +0000106 llvm::GlobalValue *GV =
107 new llvm::GlobalVariable(Init->getType(), false,
108 llvm::GlobalValue::InternalLinkage,
Chris Lattner2621fd12008-05-08 05:58:21 +0000109 Init, ContextName + Separator + D.getName(),
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000110 &CGM.getModule(), 0, Ty.getAddressSpace());
111
Chris Lattner2621fd12008-05-08 05:58:21 +0000112 return GV;
113}
114
115void CodeGenFunction::EmitStaticBlockVarDecl(const VarDecl &D) {
116
117 llvm::Value *&DMEntry = LocalDeclMap[&D];
118 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
119
120 llvm::GlobalValue *GV = GenerateStaticBlockVarDecl(D, false, ".");
121
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000122 if (const AnnotateAttr *AA = D.getAttr<AnnotateAttr>()) {
123 SourceManager &SM = CGM.getContext().getSourceManager();
124 llvm::Constant *Ann =
125 CGM.EmitAnnotateAttr(GV, AA, SM.getLogicalLineNumber(D.getLocation()));
126 CGM.AddAnnotation(Ann);
127 }
128
Eli Friedman26590522008-06-08 01:23:18 +0000129 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(D.getType());
130 const llvm::Type *LPtrTy =
131 llvm::PointerType::get(LTy, D.getType().getAddressSpace());
132 DMEntry = llvm::ConstantExpr::getBitCast(GV, LPtrTy);
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000133
134 // Emit global variable debug descriptor for static vars.
135 CGDebugInfo *DI = CGM.getDebugInfo();
136 if(DI) {
137 if(D.getLocation().isValid())
138 DI->setLocation(D.getLocation());
139 DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(GV), &D);
140 }
141
Anders Carlsson1a86b332007-10-17 00:52:43 +0000142}
143
Reid Spencer5f016e22007-07-11 17:01:13 +0000144/// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a
145/// variable declaration with auto, register, or no storage class specifier.
Chris Lattner2621fd12008-05-08 05:58:21 +0000146/// These turn into simple stack objects, or GlobalValues depending on target.
Steve Naroff248a7532008-04-15 22:42:06 +0000147void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) {
Chris Lattner8bcfc5b2008-04-06 23:10:54 +0000148 QualType Ty = D.getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000149
150 llvm::Value *DeclPtr;
Eli Friedman3c2b3172008-02-15 12:20:59 +0000151 if (Ty->isConstantSizeType()) {
Chris Lattner2621fd12008-05-08 05:58:21 +0000152 if (!Target.useGlobalsForAutomaticVariables()) {
153 // A normal fixed sized variable becomes an alloca in the entry block.
154 const llvm::Type *LTy = ConvertType(Ty);
Eli Friedman77eedd62008-05-31 21:20:41 +0000155 llvm::AllocaInst * Alloc = CreateTempAlloca(LTy, D.getName());
156 unsigned align = getContext().getTypeAlign(Ty);
157 if (const AlignedAttr* AA = D.getAttr<AlignedAttr>())
158 align = std::max(align, AA->getAlignment());
159 Alloc->setAlignment(align >> 3);
160 DeclPtr = Alloc;
Chris Lattner2621fd12008-05-08 05:58:21 +0000161 } else {
162 // Targets that don't support recursion emit locals as globals.
163 const char *Class =
164 D.getStorageClass() == VarDecl::Register ? ".reg." : ".auto.";
165 DeclPtr = GenerateStaticBlockVarDecl(D, true, Class);
166 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000167 } else {
Daniel Dunbar662174c82008-08-29 17:28:43 +0000168 CGM.ErrorUnsupported(&D, "variable-length array");
169
170 // FIXME: VLA: Add VLA support. For now just make up enough to let
171 // the compile go through.
172 const llvm::Type *LTy = ConvertType(Ty);
173 llvm::AllocaInst * Alloc = CreateTempAlloca(LTy, D.getName());
174 DeclPtr = Alloc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000175 }
176
177 llvm::Value *&DMEntry = LocalDeclMap[&D];
178 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
179 DMEntry = DeclPtr;
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000180
181 // Emit debug info for local var declaration.
182 CGDebugInfo *DI = CGM.getDebugInfo();
183 if(DI) {
184 if(D.getLocation().isValid())
185 DI->setLocation(D.getLocation());
186 DI->EmitDeclare(&D, llvm::dwarf::DW_TAG_auto_variable,
187 DeclPtr, Builder);
188 }
189
Chris Lattner19785962007-07-12 00:39:48 +0000190 // If this local has an initializer, emit it now.
Chris Lattner7f02f722007-08-24 05:35:26 +0000191 if (const Expr *Init = D.getInit()) {
Chris Lattnerd31beb12007-08-26 07:29:23 +0000192 if (!hasAggregateLLVMType(Init->getType())) {
193 llvm::Value *V = EmitScalarExpr(Init);
Chris Lattner8b2f3b72007-08-26 07:30:49 +0000194 Builder.CreateStore(V, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9b2dc282008-04-04 16:54:41 +0000195 } else if (Init->getType()->isAnyComplexType()) {
Chris Lattner190dbe22007-08-26 16:22:13 +0000196 EmitComplexExprIntoAddr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9a19edf2007-08-26 05:13:54 +0000197 } else {
Chris Lattner8b2f3b72007-08-26 07:30:49 +0000198 EmitAggExpr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9a19edf2007-08-26 05:13:54 +0000199 }
Chris Lattner7f02f722007-08-24 05:35:26 +0000200 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000201}
202
Chris Lattner2621fd12008-05-08 05:58:21 +0000203/// Emit an alloca (or GlobalValue depending on target)
204/// for the specified parameter and set up LocalDeclMap.
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000205void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg) {
206 // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl?
207 assert(isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D) &&
208 "Invalid argument to EmitParmDecl");
Chris Lattner8bcfc5b2008-04-06 23:10:54 +0000209 QualType Ty = D.getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000210
211 llvm::Value *DeclPtr;
Eli Friedman3c2b3172008-02-15 12:20:59 +0000212 if (!Ty->isConstantSizeType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 // Variable sized values always are passed by-reference.
214 DeclPtr = Arg;
Chris Lattner2621fd12008-05-08 05:58:21 +0000215 } else if (Target.useGlobalsForAutomaticVariables()) {
216 DeclPtr = GenerateStaticBlockVarDecl(D, true, ".arg.");
Reid Spencer5f016e22007-07-11 17:01:13 +0000217 } else {
Dan Gohmand79a7262008-05-22 22:12:56 +0000218 // A fixed sized single-value variable becomes an alloca in the entry block.
Reid Spencer5f016e22007-07-11 17:01:13 +0000219 const llvm::Type *LTy = ConvertType(Ty);
Dan Gohmand79a7262008-05-22 22:12:56 +0000220 if (LTy->isSingleValueType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000221 // TODO: Alignment
Daniel Dunbar56273772008-09-17 00:51:38 +0000222 std::string Name(D.getName());
223 Name += ".addr";
224 DeclPtr = CreateTempAlloca(LTy, Name.c_str());
Reid Spencer5f016e22007-07-11 17:01:13 +0000225
226 // Store the initial value into the alloca.
Eli Friedman1e692ac2008-06-13 23:01:12 +0000227 Builder.CreateStore(Arg, DeclPtr,Ty.isVolatileQualified());
Reid Spencer5f016e22007-07-11 17:01:13 +0000228 } else {
229 // Otherwise, if this is an aggregate, just use the input pointer.
230 DeclPtr = Arg;
231 }
232 Arg->setName(D.getName());
233 }
234
235 llvm::Value *&DMEntry = LocalDeclMap[&D];
236 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
237 DMEntry = DeclPtr;
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000238
239 // Emit debug info for param declaration.
240 CGDebugInfo *DI = CGM.getDebugInfo();
241 if(DI) {
242 if(D.getLocation().isValid())
243 DI->setLocation(D.getLocation());
244 DI->EmitDeclare(&D, llvm::dwarf::DW_TAG_arg_variable,
245 DeclPtr, Builder);
246 }
247
Reid Spencer5f016e22007-07-11 17:01:13 +0000248}
249