blob: e54c4b386db98a81c43062d87580fed251756c7d [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"
24using namespace clang;
25using namespace CodeGen;
26
27
28void CodeGenFunction::EmitDecl(const Decl &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +000029 switch (D.getKind()) {
30 default: assert(0 && "Unknown decl kind!");
Chris Lattneraa9fc462007-10-08 21:37:32 +000031 case Decl::ParmVar:
Reid Spencer5f016e22007-07-11 17:01:13 +000032 assert(0 && "Parmdecls should not be in declstmts!");
33 case Decl::Typedef: // typedef int X;
34 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 }
Daniel Dunbar662174c82008-08-29 17:28:43 +000048 }
Reid Spencer5f016e22007-07-11 17:01:13 +000049}
50
51/// EmitBlockVarDecl - This method handles emission of any variable declaration
52/// inside a function, including static vars etc.
Steve Naroff248a7532008-04-15 22:42:06 +000053void CodeGenFunction::EmitBlockVarDecl(const VarDecl &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +000054 switch (D.getStorageClass()) {
55 case VarDecl::Static:
Anders Carlsson1a86b332007-10-17 00:52:43 +000056 return EmitStaticBlockVarDecl(D);
Reid Spencer5f016e22007-07-11 17:01:13 +000057 case VarDecl::Extern:
Lauro Ramos Venanciofea90b82008-02-16 22:30:38 +000058 // Don't emit it now, allow it to be emitted lazily on its first use.
59 return;
Reid Spencer5f016e22007-07-11 17:01:13 +000060 default:
61 assert((D.getStorageClass() == VarDecl::None ||
62 D.getStorageClass() == VarDecl::Auto ||
63 D.getStorageClass() == VarDecl::Register) &&
64 "Unknown storage class");
65 return EmitLocalBlockVarDecl(D);
66 }
67}
68
Chris Lattner2621fd12008-05-08 05:58:21 +000069llvm::GlobalValue *
70CodeGenFunction::GenerateStaticBlockVarDecl(const VarDecl &D,
71 bool NoInit,
72 const char *Separator) {
Chris Lattner8bcfc5b2008-04-06 23:10:54 +000073 QualType Ty = D.getType();
Eli Friedman3c2b3172008-02-15 12:20:59 +000074 assert(Ty->isConstantSizeType() && "VLAs can't be static");
Anders Carlsson1a86b332007-10-17 00:52:43 +000075
Chris Lattner19009e62008-01-09 18:47:25 +000076 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
Anders Carlsson1a86b332007-10-17 00:52:43 +000077 llvm::Constant *Init = 0;
Chris Lattner2621fd12008-05-08 05:58:21 +000078 if ((D.getInit() == 0) || NoInit) {
Anders Carlsson1a86b332007-10-17 00:52:43 +000079 Init = llvm::Constant::getNullValue(LTy);
Oliver Hunt28247232007-12-02 00:11:25 +000080 } else {
Anders Carlssone1b29ef2008-08-22 16:00:37 +000081 if (D.getInit()->isConstantExpr(getContext(), 0))
82 Init = CGM.EmitConstantExpr(D.getInit(), this);
83 else {
84 assert(getContext().getLangOptions().CPlusPlus &&
85 "only C++ supports non-constant static initializers!");
86 return GenerateStaticCXXBlockVarDecl(D);
87 }
Devang Patele40daa42007-10-26 17:50:58 +000088 }
89
Oliver Hunt28247232007-12-02 00:11:25 +000090 assert(Init && "Unable to create initialiser for static decl");
Nate Begeman8bd4afe2008-04-19 04:17:09 +000091
Chris Lattner352ffde22008-02-06 04:54:32 +000092 std::string ContextName;
Anders Carlsson19567ee2008-08-25 01:38:19 +000093 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurFuncDecl))
Chris Lattnerc8aa5f12008-04-04 04:07:35 +000094 ContextName = FD->getName();
Anders Carlsson19567ee2008-08-25 01:38:19 +000095 else if (isa<ObjCMethodDecl>(CurFuncDecl))
96 ContextName = std::string(CurFn->getNameStart(),
97 CurFn->getNameStart() + CurFn->getNameLen());
Chris Lattner352ffde22008-02-06 04:54:32 +000098 else
Anders Carlsson19567ee2008-08-25 01:38:19 +000099 assert(0 && "Unknown context for block var decl");
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000100
Eli Friedman26590522008-06-08 01:23:18 +0000101 llvm::GlobalValue *GV =
102 new llvm::GlobalVariable(Init->getType(), false,
103 llvm::GlobalValue::InternalLinkage,
Chris Lattner2621fd12008-05-08 05:58:21 +0000104 Init, ContextName + Separator + D.getName(),
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000105 &CGM.getModule(), 0, Ty.getAddressSpace());
106
Chris Lattner2621fd12008-05-08 05:58:21 +0000107 return GV;
108}
109
110void CodeGenFunction::EmitStaticBlockVarDecl(const VarDecl &D) {
111
112 llvm::Value *&DMEntry = LocalDeclMap[&D];
113 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
114
115 llvm::GlobalValue *GV = GenerateStaticBlockVarDecl(D, false, ".");
116
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000117 if (const AnnotateAttr *AA = D.getAttr<AnnotateAttr>()) {
118 SourceManager &SM = CGM.getContext().getSourceManager();
119 llvm::Constant *Ann =
120 CGM.EmitAnnotateAttr(GV, AA, SM.getLogicalLineNumber(D.getLocation()));
121 CGM.AddAnnotation(Ann);
122 }
123
Eli Friedman26590522008-06-08 01:23:18 +0000124 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(D.getType());
125 const llvm::Type *LPtrTy =
126 llvm::PointerType::get(LTy, D.getType().getAddressSpace());
127 DMEntry = llvm::ConstantExpr::getBitCast(GV, LPtrTy);
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000128
129 // Emit global variable debug descriptor for static vars.
130 CGDebugInfo *DI = CGM.getDebugInfo();
131 if(DI) {
Daniel Dunbar66031a52008-10-17 16:15:48 +0000132 DI->setLocation(D.getLocation());
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000133 DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(GV), &D);
134 }
135
Anders Carlsson1a86b332007-10-17 00:52:43 +0000136}
137
Reid Spencer5f016e22007-07-11 17:01:13 +0000138/// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a
139/// variable declaration with auto, register, or no storage class specifier.
Chris Lattner2621fd12008-05-08 05:58:21 +0000140/// These turn into simple stack objects, or GlobalValues depending on target.
Steve Naroff248a7532008-04-15 22:42:06 +0000141void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) {
Chris Lattner8bcfc5b2008-04-06 23:10:54 +0000142 QualType Ty = D.getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000143
144 llvm::Value *DeclPtr;
Eli Friedman3c2b3172008-02-15 12:20:59 +0000145 if (Ty->isConstantSizeType()) {
Chris Lattner2621fd12008-05-08 05:58:21 +0000146 if (!Target.useGlobalsForAutomaticVariables()) {
147 // A normal fixed sized variable becomes an alloca in the entry block.
148 const llvm::Type *LTy = ConvertType(Ty);
Eli Friedman77eedd62008-05-31 21:20:41 +0000149 llvm::AllocaInst * Alloc = CreateTempAlloca(LTy, D.getName());
150 unsigned align = getContext().getTypeAlign(Ty);
151 if (const AlignedAttr* AA = D.getAttr<AlignedAttr>())
152 align = std::max(align, AA->getAlignment());
153 Alloc->setAlignment(align >> 3);
154 DeclPtr = Alloc;
Chris Lattner2621fd12008-05-08 05:58:21 +0000155 } else {
156 // Targets that don't support recursion emit locals as globals.
157 const char *Class =
158 D.getStorageClass() == VarDecl::Register ? ".reg." : ".auto.";
159 DeclPtr = GenerateStaticBlockVarDecl(D, true, Class);
160 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 } else {
Daniel Dunbar662174c82008-08-29 17:28:43 +0000162 CGM.ErrorUnsupported(&D, "variable-length array");
163
164 // FIXME: VLA: Add VLA support. For now just make up enough to let
165 // the compile go through.
166 const llvm::Type *LTy = ConvertType(Ty);
167 llvm::AllocaInst * Alloc = CreateTempAlloca(LTy, D.getName());
168 DeclPtr = Alloc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000169 }
170
171 llvm::Value *&DMEntry = LocalDeclMap[&D];
172 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
173 DMEntry = DeclPtr;
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000174
175 // Emit debug info for local var declaration.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000176 if (CGDebugInfo *DI = CGM.getDebugInfo()) {
Daniel Dunbar66031a52008-10-17 16:15:48 +0000177 DI->setLocation(D.getLocation());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000178 DI->EmitDeclareOfAutoVariable(&D, DeclPtr, Builder);
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000179 }
180
Chris Lattner19785962007-07-12 00:39:48 +0000181 // If this local has an initializer, emit it now.
Chris Lattner7f02f722007-08-24 05:35:26 +0000182 if (const Expr *Init = D.getInit()) {
Chris Lattnerd31beb12007-08-26 07:29:23 +0000183 if (!hasAggregateLLVMType(Init->getType())) {
184 llvm::Value *V = EmitScalarExpr(Init);
Chris Lattner8b2f3b72007-08-26 07:30:49 +0000185 Builder.CreateStore(V, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9b2dc282008-04-04 16:54:41 +0000186 } else if (Init->getType()->isAnyComplexType()) {
Chris Lattner190dbe22007-08-26 16:22:13 +0000187 EmitComplexExprIntoAddr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9a19edf2007-08-26 05:13:54 +0000188 } else {
Chris Lattner8b2f3b72007-08-26 07:30:49 +0000189 EmitAggExpr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9a19edf2007-08-26 05:13:54 +0000190 }
Chris Lattner7f02f722007-08-24 05:35:26 +0000191 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000192}
193
Chris Lattner2621fd12008-05-08 05:58:21 +0000194/// Emit an alloca (or GlobalValue depending on target)
195/// for the specified parameter and set up LocalDeclMap.
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000196void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg) {
197 // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl?
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000198 assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) &&
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000199 "Invalid argument to EmitParmDecl");
Chris Lattner8bcfc5b2008-04-06 23:10:54 +0000200 QualType Ty = D.getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000201
202 llvm::Value *DeclPtr;
Eli Friedman3c2b3172008-02-15 12:20:59 +0000203 if (!Ty->isConstantSizeType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000204 // Variable sized values always are passed by-reference.
205 DeclPtr = Arg;
Chris Lattner2621fd12008-05-08 05:58:21 +0000206 } else if (Target.useGlobalsForAutomaticVariables()) {
207 DeclPtr = GenerateStaticBlockVarDecl(D, true, ".arg.");
Reid Spencer5f016e22007-07-11 17:01:13 +0000208 } else {
Dan Gohmand79a7262008-05-22 22:12:56 +0000209 // A fixed sized single-value variable becomes an alloca in the entry block.
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 const llvm::Type *LTy = ConvertType(Ty);
Dan Gohmand79a7262008-05-22 22:12:56 +0000211 if (LTy->isSingleValueType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000212 // TODO: Alignment
Daniel Dunbar56273772008-09-17 00:51:38 +0000213 std::string Name(D.getName());
214 Name += ".addr";
215 DeclPtr = CreateTempAlloca(LTy, Name.c_str());
Reid Spencer5f016e22007-07-11 17:01:13 +0000216
217 // Store the initial value into the alloca.
Eli Friedman1e692ac2008-06-13 23:01:12 +0000218 Builder.CreateStore(Arg, DeclPtr,Ty.isVolatileQualified());
Reid Spencer5f016e22007-07-11 17:01:13 +0000219 } else {
220 // Otherwise, if this is an aggregate, just use the input pointer.
221 DeclPtr = Arg;
222 }
223 Arg->setName(D.getName());
224 }
225
226 llvm::Value *&DMEntry = LocalDeclMap[&D];
227 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
228 DMEntry = DeclPtr;
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000229
230 // Emit debug info for param declaration.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000231 if (CGDebugInfo *DI = CGM.getDebugInfo()) {
Daniel Dunbar66031a52008-10-17 16:15:48 +0000232 DI->setLocation(D.getLocation());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000233 DI->EmitDeclareOfArgVariable(&D, DeclPtr, Builder);
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000234 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000235}
236