blob: 4541fc4caa349f354636aebf021e9a19b338884f [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"
Chris Lattner84915fa2007-06-02 04:16:21 +000017#include "clang/AST/AST.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;
38 // None of these decls require codegen support.
39 return;
40
Steve Naroff08899ff2008-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 Lattner84915fa2007-06-02 04:16:21 +000046 case Decl::EnumConstant:
Chris Lattner1ad38f82007-06-09 01:20:56 +000047 return EmitEnumConstantDecl(cast<EnumConstantDecl>(D));
Chris Lattner84915fa2007-06-02 04:16:21 +000048 }
49}
50
Chris Lattner84915fa2007-06-02 04:16:21 +000051void CodeGenFunction::EmitEnumConstantDecl(const EnumConstantDecl &D) {
52 assert(0 && "FIXME: Enum constant decls not implemented yet!");
53}
Chris Lattner03df1222007-06-02 04:53:11 +000054
55/// EmitBlockVarDecl - This method handles emission of any variable declaration
56/// inside a function, including static vars etc.
Steve Naroff08899ff2008-04-15 22:42:06 +000057void CodeGenFunction::EmitBlockVarDecl(const VarDecl &D) {
Chris Lattner03df1222007-06-02 04:53:11 +000058 switch (D.getStorageClass()) {
59 case VarDecl::Static:
Anders Carlssonf94cd1f2007-10-17 00:52:43 +000060 return EmitStaticBlockVarDecl(D);
Chris Lattner03df1222007-06-02 04:53:11 +000061 case VarDecl::Extern:
Lauro Ramos Venanciobada8d42008-02-16 22:30:38 +000062 // Don't emit it now, allow it to be emitted lazily on its first use.
63 return;
Chris Lattner03df1222007-06-02 04:53:11 +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 Lattnerb781dc792008-05-08 05:58:21 +000073llvm::GlobalValue *
74CodeGenFunction::GenerateStaticBlockVarDecl(const VarDecl &D,
75 bool NoInit,
76 const char *Separator) {
Chris Lattnerfc4379f2008-04-06 23:10:54 +000077 QualType Ty = D.getType();
Eli Friedmana682d392008-02-15 12:20:59 +000078 assert(Ty->isConstantSizeType() && "VLAs can't be static");
Anders Carlssonf94cd1f2007-10-17 00:52:43 +000079
Chris Lattner41a1ef02008-01-09 18:47:25 +000080 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
Anders Carlssonf94cd1f2007-10-17 00:52:43 +000081 llvm::Constant *Init = 0;
Chris Lattnerb781dc792008-05-08 05:58:21 +000082 if ((D.getInit() == 0) || NoInit) {
Anders Carlssonf94cd1f2007-10-17 00:52:43 +000083 Init = llvm::Constant::getNullValue(LTy);
Oliver Huntaefc8fd2007-12-02 00:11:25 +000084 } else {
Lauro Ramos Venancio01a72ff2008-02-26 21:41:45 +000085 Init = CGM.EmitConstantExpr(D.getInit(), this);
Devang Patelffdb07c2007-10-26 17:50:58 +000086 }
87
Oliver Huntaefc8fd2007-12-02 00:11:25 +000088 assert(Init && "Unable to create initialiser for static decl");
Nate Begemanfaae0812008-04-19 04:17:09 +000089
Chris Lattner8b945ee2008-02-06 04:54:32 +000090 std::string ContextName;
Chris Lattner5506f8c2008-04-04 04:07:35 +000091 if (const FunctionDecl * FD = dyn_cast<FunctionDecl>(CurFuncDecl))
92 ContextName = FD->getName();
Chris Lattner8b945ee2008-02-06 04:54:32 +000093 else
94 assert(0 && "Unknown context for block var decl"); // FIXME Handle objc.
Nate Begemanfaae0812008-04-19 04:17:09 +000095
96 llvm::GlobalValue *GV =
97 new llvm::GlobalVariable(LTy, false, llvm::GlobalValue::InternalLinkage,
Chris Lattnerb781dc792008-05-08 05:58:21 +000098 Init, ContextName + Separator + D.getName(),
Nate Begemanfaae0812008-04-19 04:17:09 +000099 &CGM.getModule(), 0, Ty.getAddressSpace());
100
Chris Lattnerb781dc792008-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 Begemanfaae0812008-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;
Anders Carlssonf94cd1f2007-10-17 00:52:43 +0000119}
120
Chris Lattner03df1222007-06-02 04:53:11 +0000121/// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a
122/// variable declaration with auto, register, or no storage class specifier.
Chris Lattnerb781dc792008-05-08 05:58:21 +0000123/// These turn into simple stack objects, or GlobalValues depending on target.
Steve Naroff08899ff2008-04-15 22:42:06 +0000124void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) {
Chris Lattnerfc4379f2008-04-06 23:10:54 +0000125 QualType Ty = D.getType();
Chris Lattner03df1222007-06-02 04:53:11 +0000126
127 llvm::Value *DeclPtr;
Eli Friedmana682d392008-02-15 12:20:59 +0000128 if (Ty->isConstantSizeType()) {
Chris Lattnerb781dc792008-05-08 05:58:21 +0000129 if (!Target.useGlobalsForAutomaticVariables()) {
130 // A normal fixed sized variable becomes an alloca in the entry block.
131 const llvm::Type *LTy = ConvertType(Ty);
132 // TODO: Alignment
133 DeclPtr = CreateTempAlloca(LTy, D.getName());
134 } else {
135 // Targets that don't support recursion emit locals as globals.
136 const char *Class =
137 D.getStorageClass() == VarDecl::Register ? ".reg." : ".auto.";
138 DeclPtr = GenerateStaticBlockVarDecl(D, true, Class);
139 }
Chris Lattner03df1222007-06-02 04:53:11 +0000140 } else {
141 // TODO: Create a dynamic alloca.
142 assert(0 && "FIXME: Local VLAs not implemented yet");
143 }
144
145 llvm::Value *&DMEntry = LocalDeclMap[&D];
146 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
147 DMEntry = DeclPtr;
Sanjiv Gupta18de6242008-05-30 10:30:31 +0000148
149 // Emit debug info for local var declaration.
150 CGDebugInfo *DI = CGM.getDebugInfo();
151 if(DI) {
152 if(D.getLocation().isValid())
153 DI->setLocation(D.getLocation());
154 DI->EmitDeclare(&D, llvm::dwarf::DW_TAG_auto_variable,
155 DeclPtr, Builder);
156 }
157
Chris Lattner07eb7332007-07-12 00:39:48 +0000158 // If this local has an initializer, emit it now.
Chris Lattner2da04b32007-08-24 05:35:26 +0000159 if (const Expr *Init = D.getInit()) {
Chris Lattnerb753f662007-08-26 07:29:23 +0000160 if (!hasAggregateLLVMType(Init->getType())) {
161 llvm::Value *V = EmitScalarExpr(Init);
Chris Lattnerf6dcc9d2007-08-26 07:30:49 +0000162 Builder.CreateStore(V, DeclPtr, D.getType().isVolatileQualified());
Chris Lattnerf3bc75a2008-04-04 16:54:41 +0000163 } else if (Init->getType()->isAnyComplexType()) {
Chris Lattnerb84bb952007-08-26 16:22:13 +0000164 EmitComplexExprIntoAddr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9de95272007-08-26 05:13:54 +0000165 } else {
Chris Lattnerf6dcc9d2007-08-26 07:30:49 +0000166 EmitAggExpr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9de95272007-08-26 05:13:54 +0000167 }
Chris Lattner2da04b32007-08-24 05:35:26 +0000168 }
Chris Lattner03df1222007-06-02 04:53:11 +0000169}
Chris Lattner53621a52007-06-13 20:44:40 +0000170
Chris Lattnerb781dc792008-05-08 05:58:21 +0000171/// Emit an alloca (or GlobalValue depending on target)
172/// for the specified parameter and set up LocalDeclMap.
Chris Lattner53621a52007-06-13 20:44:40 +0000173void CodeGenFunction::EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg) {
Chris Lattnerfc4379f2008-04-06 23:10:54 +0000174 QualType Ty = D.getType();
Chris Lattner53621a52007-06-13 20:44:40 +0000175
176 llvm::Value *DeclPtr;
Eli Friedmana682d392008-02-15 12:20:59 +0000177 if (!Ty->isConstantSizeType()) {
Chris Lattner53621a52007-06-13 20:44:40 +0000178 // Variable sized values always are passed by-reference.
179 DeclPtr = Arg;
Chris Lattnerb781dc792008-05-08 05:58:21 +0000180 } else if (Target.useGlobalsForAutomaticVariables()) {
181 DeclPtr = GenerateStaticBlockVarDecl(D, true, ".arg.");
Chris Lattner53621a52007-06-13 20:44:40 +0000182 } else {
Dan Gohman5d309752008-05-22 22:12:56 +0000183 // A fixed sized single-value variable becomes an alloca in the entry block.
Chris Lattnerf033c142007-06-22 19:05:19 +0000184 const llvm::Type *LTy = ConvertType(Ty);
Dan Gohman5d309752008-05-22 22:12:56 +0000185 if (LTy->isSingleValueType()) {
Chris Lattner53621a52007-06-13 20:44:40 +0000186 // TODO: Alignment
Chris Lattner23b7eb62007-06-15 23:05:46 +0000187 DeclPtr = new llvm::AllocaInst(LTy, 0, std::string(D.getName())+".addr",
188 AllocaInsertPt);
Chris Lattner53621a52007-06-13 20:44:40 +0000189
190 // Store the initial value into the alloca.
191 Builder.CreateStore(Arg, DeclPtr);
Chris Lattner53621a52007-06-13 20:44:40 +0000192 } else {
193 // Otherwise, if this is an aggregate, just use the input pointer.
Chris Lattner0fb84652007-06-22 18:57:44 +0000194 DeclPtr = Arg;
Chris Lattner53621a52007-06-13 20:44:40 +0000195 }
Chris Lattnerf39b03d2007-06-22 18:15:16 +0000196 Arg->setName(D.getName());
Chris Lattner53621a52007-06-13 20:44:40 +0000197 }
198
199 llvm::Value *&DMEntry = LocalDeclMap[&D];
200 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
201 DMEntry = DeclPtr;
Sanjiv Gupta18de6242008-05-30 10:30:31 +0000202
203 // Emit debug info for param declaration.
204 CGDebugInfo *DI = CGM.getDebugInfo();
205 if(DI) {
206 if(D.getLocation().isValid())
207 DI->setLocation(D.getLocation());
208 DI->EmitDeclare(&D, llvm::dwarf::DW_TAG_arg_variable,
209 DeclPtr, Builder);
210 }
211
Chris Lattner53621a52007-06-13 20:44:40 +0000212}
213