blob: ff0cb124cc4185dd81de0869f75717ac05b0264b [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"
Nate Begemanfaae0812008-04-19 04:17:09 +000018#include "clang/Basic/SourceManager.h"
Chris Lattnerb781dc792008-05-08 05:58:21 +000019#include "clang/Basic/TargetInfo.h"
Anders Carlssonf94cd1f2007-10-17 00:52:43 +000020#include "llvm/GlobalVariable.h"
Chris Lattner53621a52007-06-13 20:44:40 +000021#include "llvm/Type.h"
Sanjiv Gupta18de6242008-05-30 10:30:31 +000022#include "llvm/Support/Dwarf.h"
Chris Lattner84915fa2007-06-02 04:16:21 +000023using namespace clang;
24using namespace CodeGen;
25
26
Chris Lattner1ad38f82007-06-09 01:20:56 +000027void CodeGenFunction::EmitDecl(const Decl &D) {
Chris Lattner1ad38f82007-06-09 01:20:56 +000028 switch (D.getKind()) {
Chris Lattner84915fa2007-06-02 04:16:21 +000029 default: assert(0 && "Unknown decl kind!");
Chris Lattnerba9dddb2007-10-08 21:37:32 +000030 case Decl::ParmVar:
Chris Lattner84915fa2007-06-02 04:16:21 +000031 assert(0 && "Parmdecls should not be in declstmts!");
32 case Decl::Typedef: // typedef int X;
33 case Decl::Function: // void X();
34 case Decl::Struct: // struct X;
35 case Decl::Union: // union X;
36 case Decl::Class: // class X;
37 case Decl::Enum: // enum X;
Argyrios Kyrtzidisd0c1df42008-06-09 23:42:47 +000038 case Decl::CXXStruct: // struct X; [C++]
39 case Decl::CXXUnion: // union X; [C++]
40 case Decl::CXXClass: // class X; [C++]
Chris Lattner84915fa2007-06-02 04:16:21 +000041 // None of these decls require codegen support.
42 return;
43
Steve Naroff08899ff2008-04-15 22:42:06 +000044 case Decl::Var:
45 if (cast<VarDecl>(D).isBlockVarDecl())
46 return EmitBlockVarDecl(cast<VarDecl>(D));
47 assert(0 && "Should not see file-scope variables inside a function!");
48
Chris Lattner84915fa2007-06-02 04:16:21 +000049 case Decl::EnumConstant:
Chris Lattner1ad38f82007-06-09 01:20:56 +000050 return EmitEnumConstantDecl(cast<EnumConstantDecl>(D));
Chris Lattner84915fa2007-06-02 04:16:21 +000051 }
52}
53
Chris Lattner84915fa2007-06-02 04:16:21 +000054void CodeGenFunction::EmitEnumConstantDecl(const EnumConstantDecl &D) {
55 assert(0 && "FIXME: Enum constant decls not implemented yet!");
56}
Chris Lattner03df1222007-06-02 04:53:11 +000057
58/// EmitBlockVarDecl - This method handles emission of any variable declaration
59/// inside a function, including static vars etc.
Steve Naroff08899ff2008-04-15 22:42:06 +000060void CodeGenFunction::EmitBlockVarDecl(const VarDecl &D) {
Chris Lattner03df1222007-06-02 04:53:11 +000061 switch (D.getStorageClass()) {
62 case VarDecl::Static:
Anders Carlssonf94cd1f2007-10-17 00:52:43 +000063 return EmitStaticBlockVarDecl(D);
Chris Lattner03df1222007-06-02 04:53:11 +000064 case VarDecl::Extern:
Lauro Ramos Venanciobada8d42008-02-16 22:30:38 +000065 // Don't emit it now, allow it to be emitted lazily on its first use.
66 return;
Chris Lattner03df1222007-06-02 04:53:11 +000067 default:
68 assert((D.getStorageClass() == VarDecl::None ||
69 D.getStorageClass() == VarDecl::Auto ||
70 D.getStorageClass() == VarDecl::Register) &&
71 "Unknown storage class");
72 return EmitLocalBlockVarDecl(D);
73 }
74}
75
Chris Lattnerb781dc792008-05-08 05:58:21 +000076llvm::GlobalValue *
77CodeGenFunction::GenerateStaticBlockVarDecl(const VarDecl &D,
78 bool NoInit,
79 const char *Separator) {
Chris Lattnerfc4379f2008-04-06 23:10:54 +000080 QualType Ty = D.getType();
Eli Friedmana682d392008-02-15 12:20:59 +000081 assert(Ty->isConstantSizeType() && "VLAs can't be static");
Anders Carlssonf94cd1f2007-10-17 00:52:43 +000082
Chris Lattner41a1ef02008-01-09 18:47:25 +000083 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
Anders Carlssonf94cd1f2007-10-17 00:52:43 +000084 llvm::Constant *Init = 0;
Chris Lattnerb781dc792008-05-08 05:58:21 +000085 if ((D.getInit() == 0) || NoInit) {
Anders Carlssonf94cd1f2007-10-17 00:52:43 +000086 Init = llvm::Constant::getNullValue(LTy);
Oliver Huntaefc8fd2007-12-02 00:11:25 +000087 } else {
Lauro Ramos Venancio01a72ff2008-02-26 21:41:45 +000088 Init = CGM.EmitConstantExpr(D.getInit(), this);
Devang Patelffdb07c2007-10-26 17:50:58 +000089 }
90
Oliver Huntaefc8fd2007-12-02 00:11:25 +000091 assert(Init && "Unable to create initialiser for static decl");
Nate Begemanfaae0812008-04-19 04:17:09 +000092
Chris Lattner8b945ee2008-02-06 04:54:32 +000093 std::string ContextName;
Chris Lattner5506f8c2008-04-04 04:07:35 +000094 if (const FunctionDecl * FD = dyn_cast<FunctionDecl>(CurFuncDecl))
95 ContextName = FD->getName();
Chris Lattner8b945ee2008-02-06 04:54:32 +000096 else
97 assert(0 && "Unknown context for block var decl"); // FIXME Handle objc.
Nate Begemanfaae0812008-04-19 04:17:09 +000098
Eli Friedmanc98a7ad2008-06-08 01:23:18 +000099 llvm::GlobalValue *GV =
100 new llvm::GlobalVariable(Init->getType(), false,
101 llvm::GlobalValue::InternalLinkage,
Chris Lattnerb781dc792008-05-08 05:58:21 +0000102 Init, ContextName + Separator + D.getName(),
Nate Begemanfaae0812008-04-19 04:17:09 +0000103 &CGM.getModule(), 0, Ty.getAddressSpace());
104
Chris Lattnerb781dc792008-05-08 05:58:21 +0000105 return GV;
106}
107
108void CodeGenFunction::EmitStaticBlockVarDecl(const VarDecl &D) {
109
110 llvm::Value *&DMEntry = LocalDeclMap[&D];
111 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
112
113 llvm::GlobalValue *GV = GenerateStaticBlockVarDecl(D, false, ".");
114
Nate Begemanfaae0812008-04-19 04:17:09 +0000115 if (const AnnotateAttr *AA = D.getAttr<AnnotateAttr>()) {
116 SourceManager &SM = CGM.getContext().getSourceManager();
117 llvm::Constant *Ann =
118 CGM.EmitAnnotateAttr(GV, AA, SM.getLogicalLineNumber(D.getLocation()));
119 CGM.AddAnnotation(Ann);
120 }
121
Eli Friedmanc98a7ad2008-06-08 01:23:18 +0000122 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(D.getType());
123 const llvm::Type *LPtrTy =
124 llvm::PointerType::get(LTy, D.getType().getAddressSpace());
125 DMEntry = llvm::ConstantExpr::getBitCast(GV, LPtrTy);
Sanjiv Gupta158143a2008-06-05 08:59:10 +0000126
127 // Emit global variable debug descriptor for static vars.
128 CGDebugInfo *DI = CGM.getDebugInfo();
129 if(DI) {
130 if(D.getLocation().isValid())
131 DI->setLocation(D.getLocation());
132 DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(GV), &D);
133 }
134
Anders Carlssonf94cd1f2007-10-17 00:52:43 +0000135}
136
Chris Lattner03df1222007-06-02 04:53:11 +0000137/// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a
138/// variable declaration with auto, register, or no storage class specifier.
Chris Lattnerb781dc792008-05-08 05:58:21 +0000139/// These turn into simple stack objects, or GlobalValues depending on target.
Steve Naroff08899ff2008-04-15 22:42:06 +0000140void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) {
Chris Lattnerfc4379f2008-04-06 23:10:54 +0000141 QualType Ty = D.getType();
Chris Lattner03df1222007-06-02 04:53:11 +0000142
143 llvm::Value *DeclPtr;
Eli Friedmana682d392008-02-15 12:20:59 +0000144 if (Ty->isConstantSizeType()) {
Chris Lattnerb781dc792008-05-08 05:58:21 +0000145 if (!Target.useGlobalsForAutomaticVariables()) {
146 // A normal fixed sized variable becomes an alloca in the entry block.
147 const llvm::Type *LTy = ConvertType(Ty);
Eli Friedman252e5f12008-05-31 21:20:41 +0000148 llvm::AllocaInst * Alloc = CreateTempAlloca(LTy, D.getName());
149 unsigned align = getContext().getTypeAlign(Ty);
150 if (const AlignedAttr* AA = D.getAttr<AlignedAttr>())
151 align = std::max(align, AA->getAlignment());
152 Alloc->setAlignment(align >> 3);
153 DeclPtr = Alloc;
Chris Lattnerb781dc792008-05-08 05:58:21 +0000154 } else {
155 // Targets that don't support recursion emit locals as globals.
156 const char *Class =
157 D.getStorageClass() == VarDecl::Register ? ".reg." : ".auto.";
158 DeclPtr = GenerateStaticBlockVarDecl(D, true, Class);
159 }
Chris Lattner03df1222007-06-02 04:53:11 +0000160 } else {
161 // TODO: Create a dynamic alloca.
162 assert(0 && "FIXME: Local VLAs not implemented yet");
163 }
164
165 llvm::Value *&DMEntry = LocalDeclMap[&D];
166 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
167 DMEntry = DeclPtr;
Sanjiv Gupta18de6242008-05-30 10:30:31 +0000168
169 // Emit debug info for local var declaration.
170 CGDebugInfo *DI = CGM.getDebugInfo();
171 if(DI) {
172 if(D.getLocation().isValid())
173 DI->setLocation(D.getLocation());
174 DI->EmitDeclare(&D, llvm::dwarf::DW_TAG_auto_variable,
175 DeclPtr, Builder);
176 }
177
Chris Lattner07eb7332007-07-12 00:39:48 +0000178 // If this local has an initializer, emit it now.
Chris Lattner2da04b32007-08-24 05:35:26 +0000179 if (const Expr *Init = D.getInit()) {
Chris Lattnerb753f662007-08-26 07:29:23 +0000180 if (!hasAggregateLLVMType(Init->getType())) {
181 llvm::Value *V = EmitScalarExpr(Init);
Chris Lattnerf6dcc9d2007-08-26 07:30:49 +0000182 Builder.CreateStore(V, DeclPtr, D.getType().isVolatileQualified());
Chris Lattnerf3bc75a2008-04-04 16:54:41 +0000183 } else if (Init->getType()->isAnyComplexType()) {
Chris Lattnerb84bb952007-08-26 16:22:13 +0000184 EmitComplexExprIntoAddr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9de95272007-08-26 05:13:54 +0000185 } else {
Chris Lattnerf6dcc9d2007-08-26 07:30:49 +0000186 EmitAggExpr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9de95272007-08-26 05:13:54 +0000187 }
Chris Lattner2da04b32007-08-24 05:35:26 +0000188 }
Chris Lattner03df1222007-06-02 04:53:11 +0000189}
Chris Lattner53621a52007-06-13 20:44:40 +0000190
Chris Lattnerb781dc792008-05-08 05:58:21 +0000191/// Emit an alloca (or GlobalValue depending on target)
192/// for the specified parameter and set up LocalDeclMap.
Chris Lattner53621a52007-06-13 20:44:40 +0000193void CodeGenFunction::EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg) {
Chris Lattnerfc4379f2008-04-06 23:10:54 +0000194 QualType Ty = D.getType();
Chris Lattner53621a52007-06-13 20:44:40 +0000195
196 llvm::Value *DeclPtr;
Eli Friedmana682d392008-02-15 12:20:59 +0000197 if (!Ty->isConstantSizeType()) {
Chris Lattner53621a52007-06-13 20:44:40 +0000198 // Variable sized values always are passed by-reference.
199 DeclPtr = Arg;
Chris Lattnerb781dc792008-05-08 05:58:21 +0000200 } else if (Target.useGlobalsForAutomaticVariables()) {
201 DeclPtr = GenerateStaticBlockVarDecl(D, true, ".arg.");
Chris Lattner53621a52007-06-13 20:44:40 +0000202 } else {
Dan Gohman5d309752008-05-22 22:12:56 +0000203 // A fixed sized single-value variable becomes an alloca in the entry block.
Chris Lattnerf033c142007-06-22 19:05:19 +0000204 const llvm::Type *LTy = ConvertType(Ty);
Dan Gohman5d309752008-05-22 22:12:56 +0000205 if (LTy->isSingleValueType()) {
Chris Lattner53621a52007-06-13 20:44:40 +0000206 // TODO: Alignment
Chris Lattner23b7eb62007-06-15 23:05:46 +0000207 DeclPtr = new llvm::AllocaInst(LTy, 0, std::string(D.getName())+".addr",
208 AllocaInsertPt);
Chris Lattner53621a52007-06-13 20:44:40 +0000209
210 // Store the initial value into the alloca.
Eli Friedman327944b2008-06-13 23:01:12 +0000211 Builder.CreateStore(Arg, DeclPtr,Ty.isVolatileQualified());
Chris Lattner53621a52007-06-13 20:44:40 +0000212 } else {
213 // Otherwise, if this is an aggregate, just use the input pointer.
Chris Lattner0fb84652007-06-22 18:57:44 +0000214 DeclPtr = Arg;
Chris Lattner53621a52007-06-13 20:44:40 +0000215 }
Chris Lattnerf39b03d2007-06-22 18:15:16 +0000216 Arg->setName(D.getName());
Chris Lattner53621a52007-06-13 20:44:40 +0000217 }
218
219 llvm::Value *&DMEntry = LocalDeclMap[&D];
220 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
221 DMEntry = DeclPtr;
Sanjiv Gupta18de6242008-05-30 10:30:31 +0000222
223 // Emit debug info for param declaration.
224 CGDebugInfo *DI = CGM.getDebugInfo();
225 if(DI) {
226 if(D.getLocation().isValid())
227 DI->setLocation(D.getLocation());
228 DI->EmitDeclare(&D, llvm::dwarf::DW_TAG_arg_variable,
229 DeclPtr, Builder);
230 }
231
Chris Lattner53621a52007-06-13 20:44:40 +0000232}
233