blob: 5451d4afc02b689dbbdaf676e02a1d4a30fc42c7 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Decl nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
Sanjiv Guptab2a10452008-05-30 10:30:31 +000014#include "CGDebugInfo.h"
Chris Lattner4b009652007-07-25 00:24:17 +000015#include "CodeGenFunction.h"
Anders Carlsson855d78d2007-10-17 00:52:43 +000016#include "CodeGenModule.h"
Chris Lattner4b009652007-07-25 00:24:17 +000017#include "clang/AST/AST.h"
Nate Begeman8a704172008-04-19 04:17:09 +000018#include "clang/Basic/SourceManager.h"
Chris Lattner85970f32008-05-08 05:58:21 +000019#include "clang/Basic/TargetInfo.h"
Anders Carlsson855d78d2007-10-17 00:52:43 +000020#include "llvm/GlobalVariable.h"
Chris Lattner4b009652007-07-25 00:24:17 +000021#include "llvm/Type.h"
Sanjiv Guptab2a10452008-05-30 10:30:31 +000022#include "llvm/Support/Dwarf.h"
Chris Lattner4b009652007-07-25 00:24:17 +000023using namespace clang;
24using namespace CodeGen;
25
26
27void CodeGenFunction::EmitDecl(const Decl &D) {
28 switch (D.getKind()) {
29 default: assert(0 && "Unknown decl kind!");
Chris Lattner3289bca2007-10-08 21:37:32 +000030 case Decl::ParmVar:
Chris Lattner4b009652007-07-25 00:24:17 +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;
38 // None of these decls require codegen support.
39 return;
40
Steve Naroff72a6ebc2008-04-15 22:42:06 +000041 case Decl::Var:
42 if (cast<VarDecl>(D).isBlockVarDecl())
43 return EmitBlockVarDecl(cast<VarDecl>(D));
44 assert(0 && "Should not see file-scope variables inside a function!");
45
Chris Lattner4b009652007-07-25 00:24:17 +000046 case Decl::EnumConstant:
47 return EmitEnumConstantDecl(cast<EnumConstantDecl>(D));
48 }
49}
50
51void CodeGenFunction::EmitEnumConstantDecl(const EnumConstantDecl &D) {
52 assert(0 && "FIXME: Enum constant decls not implemented yet!");
53}
54
55/// EmitBlockVarDecl - This method handles emission of any variable declaration
56/// inside a function, including static vars etc.
Steve Naroff72a6ebc2008-04-15 22:42:06 +000057void CodeGenFunction::EmitBlockVarDecl(const VarDecl &D) {
Chris Lattner4b009652007-07-25 00:24:17 +000058 switch (D.getStorageClass()) {
59 case VarDecl::Static:
Anders Carlsson855d78d2007-10-17 00:52:43 +000060 return EmitStaticBlockVarDecl(D);
Chris Lattner4b009652007-07-25 00:24:17 +000061 case VarDecl::Extern:
Lauro Ramos Venancio2348d972008-02-16 22:30:38 +000062 // Don't emit it now, allow it to be emitted lazily on its first use.
63 return;
Chris Lattner4b009652007-07-25 00:24:17 +000064 default:
65 assert((D.getStorageClass() == VarDecl::None ||
66 D.getStorageClass() == VarDecl::Auto ||
67 D.getStorageClass() == VarDecl::Register) &&
68 "Unknown storage class");
69 return EmitLocalBlockVarDecl(D);
70 }
71}
72
Chris Lattner85970f32008-05-08 05:58:21 +000073llvm::GlobalValue *
74CodeGenFunction::GenerateStaticBlockVarDecl(const VarDecl &D,
75 bool NoInit,
76 const char *Separator) {
Chris Lattner42a21742008-04-06 23:10:54 +000077 QualType Ty = D.getType();
Eli Friedman62f67fd2008-02-15 12:20:59 +000078 assert(Ty->isConstantSizeType() && "VLAs can't be static");
Anders Carlsson855d78d2007-10-17 00:52:43 +000079
Chris Lattner43cdbf52008-01-09 18:47:25 +000080 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
Anders Carlsson855d78d2007-10-17 00:52:43 +000081 llvm::Constant *Init = 0;
Chris Lattner85970f32008-05-08 05:58:21 +000082 if ((D.getInit() == 0) || NoInit) {
Anders Carlsson855d78d2007-10-17 00:52:43 +000083 Init = llvm::Constant::getNullValue(LTy);
Oliver Hunt253e0a72007-12-02 00:11:25 +000084 } else {
Lauro Ramos Venancio934fb022008-02-26 21:41:45 +000085 Init = CGM.EmitConstantExpr(D.getInit(), this);
Devang Patel2ecd9012007-10-26 17:50:58 +000086 }
87
Oliver Hunt253e0a72007-12-02 00:11:25 +000088 assert(Init && "Unable to create initialiser for static decl");
Nate Begeman8a704172008-04-19 04:17:09 +000089
Chris Lattner9b026342008-02-06 04:54:32 +000090 std::string ContextName;
Chris Lattner6e6a5972008-04-04 04:07:35 +000091 if (const FunctionDecl * FD = dyn_cast<FunctionDecl>(CurFuncDecl))
92 ContextName = FD->getName();
Chris Lattner9b026342008-02-06 04:54:32 +000093 else
94 assert(0 && "Unknown context for block var decl"); // FIXME Handle objc.
Nate Begeman8a704172008-04-19 04:17:09 +000095
96 llvm::GlobalValue *GV =
97 new llvm::GlobalVariable(LTy, false, llvm::GlobalValue::InternalLinkage,
Chris Lattner85970f32008-05-08 05:58:21 +000098 Init, ContextName + Separator + D.getName(),
Nate Begeman8a704172008-04-19 04:17:09 +000099 &CGM.getModule(), 0, Ty.getAddressSpace());
100
Chris Lattner85970f32008-05-08 05:58:21 +0000101 return GV;
102}
103
104void CodeGenFunction::EmitStaticBlockVarDecl(const VarDecl &D) {
105
106 llvm::Value *&DMEntry = LocalDeclMap[&D];
107 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
108
109 llvm::GlobalValue *GV = GenerateStaticBlockVarDecl(D, false, ".");
110
Nate Begeman8a704172008-04-19 04:17:09 +0000111 if (const AnnotateAttr *AA = D.getAttr<AnnotateAttr>()) {
112 SourceManager &SM = CGM.getContext().getSourceManager();
113 llvm::Constant *Ann =
114 CGM.EmitAnnotateAttr(GV, AA, SM.getLogicalLineNumber(D.getLocation()));
115 CGM.AddAnnotation(Ann);
116 }
117
118 DMEntry = GV;
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000119
120 // Emit global variable debug descriptor for static vars.
121 CGDebugInfo *DI = CGM.getDebugInfo();
122 if(DI) {
123 if(D.getLocation().isValid())
124 DI->setLocation(D.getLocation());
125 DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(GV), &D);
126 }
127
Anders Carlsson855d78d2007-10-17 00:52:43 +0000128}
129
Chris Lattner4b009652007-07-25 00:24:17 +0000130/// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a
131/// variable declaration with auto, register, or no storage class specifier.
Chris Lattner85970f32008-05-08 05:58:21 +0000132/// These turn into simple stack objects, or GlobalValues depending on target.
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000133void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) {
Chris Lattner42a21742008-04-06 23:10:54 +0000134 QualType Ty = D.getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000135
136 llvm::Value *DeclPtr;
Eli Friedman62f67fd2008-02-15 12:20:59 +0000137 if (Ty->isConstantSizeType()) {
Chris Lattner85970f32008-05-08 05:58:21 +0000138 if (!Target.useGlobalsForAutomaticVariables()) {
139 // A normal fixed sized variable becomes an alloca in the entry block.
140 const llvm::Type *LTy = ConvertType(Ty);
Eli Friedman696758f2008-05-31 21:20:41 +0000141 llvm::AllocaInst * Alloc = CreateTempAlloca(LTy, D.getName());
142 unsigned align = getContext().getTypeAlign(Ty);
143 if (const AlignedAttr* AA = D.getAttr<AlignedAttr>())
144 align = std::max(align, AA->getAlignment());
145 Alloc->setAlignment(align >> 3);
146 DeclPtr = Alloc;
Chris Lattner85970f32008-05-08 05:58:21 +0000147 } else {
148 // Targets that don't support recursion emit locals as globals.
149 const char *Class =
150 D.getStorageClass() == VarDecl::Register ? ".reg." : ".auto.";
151 DeclPtr = GenerateStaticBlockVarDecl(D, true, Class);
152 }
Chris Lattner4b009652007-07-25 00:24:17 +0000153 } else {
154 // TODO: Create a dynamic alloca.
155 assert(0 && "FIXME: Local VLAs not implemented yet");
156 }
157
158 llvm::Value *&DMEntry = LocalDeclMap[&D];
159 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
160 DMEntry = DeclPtr;
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000161
162 // Emit debug info for local var declaration.
163 CGDebugInfo *DI = CGM.getDebugInfo();
164 if(DI) {
165 if(D.getLocation().isValid())
166 DI->setLocation(D.getLocation());
167 DI->EmitDeclare(&D, llvm::dwarf::DW_TAG_auto_variable,
168 DeclPtr, Builder);
169 }
170
Chris Lattner4b009652007-07-25 00:24:17 +0000171 // If this local has an initializer, emit it now.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000172 if (const Expr *Init = D.getInit()) {
Chris Lattner222d3222007-08-26 07:29:23 +0000173 if (!hasAggregateLLVMType(Init->getType())) {
174 llvm::Value *V = EmitScalarExpr(Init);
Chris Lattner9a918dd2007-08-26 07:30:49 +0000175 Builder.CreateStore(V, DeclPtr, D.getType().isVolatileQualified());
Chris Lattnerde0908b2008-04-04 16:54:41 +0000176 } else if (Init->getType()->isAnyComplexType()) {
Chris Lattner8e1f6e02007-08-26 16:22:13 +0000177 EmitComplexExprIntoAddr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner64a113b2007-08-26 05:13:54 +0000178 } else {
Chris Lattner9a918dd2007-08-26 07:30:49 +0000179 EmitAggExpr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner64a113b2007-08-26 05:13:54 +0000180 }
Chris Lattner9fba49a2007-08-24 05:35:26 +0000181 }
Chris Lattner4b009652007-07-25 00:24:17 +0000182}
183
Chris Lattner85970f32008-05-08 05:58:21 +0000184/// Emit an alloca (or GlobalValue depending on target)
185/// for the specified parameter and set up LocalDeclMap.
Chris Lattner4b009652007-07-25 00:24:17 +0000186void CodeGenFunction::EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg) {
Chris Lattner42a21742008-04-06 23:10:54 +0000187 QualType Ty = D.getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000188
189 llvm::Value *DeclPtr;
Eli Friedman62f67fd2008-02-15 12:20:59 +0000190 if (!Ty->isConstantSizeType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000191 // Variable sized values always are passed by-reference.
192 DeclPtr = Arg;
Chris Lattner85970f32008-05-08 05:58:21 +0000193 } else if (Target.useGlobalsForAutomaticVariables()) {
194 DeclPtr = GenerateStaticBlockVarDecl(D, true, ".arg.");
Chris Lattner4b009652007-07-25 00:24:17 +0000195 } else {
Dan Gohman377ba9f2008-05-22 22:12:56 +0000196 // A fixed sized single-value variable becomes an alloca in the entry block.
Chris Lattner4b009652007-07-25 00:24:17 +0000197 const llvm::Type *LTy = ConvertType(Ty);
Dan Gohman377ba9f2008-05-22 22:12:56 +0000198 if (LTy->isSingleValueType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000199 // TODO: Alignment
200 DeclPtr = new llvm::AllocaInst(LTy, 0, std::string(D.getName())+".addr",
201 AllocaInsertPt);
202
203 // Store the initial value into the alloca.
204 Builder.CreateStore(Arg, DeclPtr);
205 } else {
206 // Otherwise, if this is an aggregate, just use the input pointer.
207 DeclPtr = Arg;
208 }
209 Arg->setName(D.getName());
210 }
211
212 llvm::Value *&DMEntry = LocalDeclMap[&D];
213 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
214 DMEntry = DeclPtr;
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000215
216 // Emit debug info for param declaration.
217 CGDebugInfo *DI = CGM.getDebugInfo();
218 if(DI) {
219 if(D.getLocation().isValid())
220 DI->setLocation(D.getLocation());
221 DI->EmitDeclare(&D, llvm::dwarf::DW_TAG_arg_variable,
222 DeclPtr, Builder);
223 }
224
Chris Lattner4b009652007-07-25 00:24:17 +0000225}
226