blob: df5882c2172b51a90325a438c18422e16e368d49 [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
14#include "CodeGenFunction.h"
Anders Carlssonf94cd1f2007-10-17 00:52:43 +000015#include "CodeGenModule.h"
Chris Lattner84915fa2007-06-02 04:16:21 +000016#include "clang/AST/AST.h"
Nate Begemanfaae0812008-04-19 04:17:09 +000017#include "clang/Basic/SourceManager.h"
Anders Carlssonf94cd1f2007-10-17 00:52:43 +000018#include "llvm/GlobalVariable.h"
Chris Lattner53621a52007-06-13 20:44:40 +000019#include "llvm/Type.h"
Chris Lattner84915fa2007-06-02 04:16:21 +000020using namespace clang;
21using namespace CodeGen;
22
23
Chris Lattner1ad38f82007-06-09 01:20:56 +000024void CodeGenFunction::EmitDecl(const Decl &D) {
Chris Lattner1ad38f82007-06-09 01:20:56 +000025 switch (D.getKind()) {
Chris Lattner84915fa2007-06-02 04:16:21 +000026 default: assert(0 && "Unknown decl kind!");
Chris Lattnerba9dddb2007-10-08 21:37:32 +000027 case Decl::ParmVar:
Chris Lattner84915fa2007-06-02 04:16:21 +000028 assert(0 && "Parmdecls should not be in declstmts!");
29 case Decl::Typedef: // typedef int X;
30 case Decl::Function: // void X();
31 case Decl::Struct: // struct X;
32 case Decl::Union: // union X;
33 case Decl::Class: // class X;
34 case Decl::Enum: // enum X;
35 // None of these decls require codegen support.
36 return;
37
Steve Naroff08899ff2008-04-15 22:42:06 +000038 case Decl::Var:
39 if (cast<VarDecl>(D).isBlockVarDecl())
40 return EmitBlockVarDecl(cast<VarDecl>(D));
41 assert(0 && "Should not see file-scope variables inside a function!");
42
Chris Lattner84915fa2007-06-02 04:16:21 +000043 case Decl::EnumConstant:
Chris Lattner1ad38f82007-06-09 01:20:56 +000044 return EmitEnumConstantDecl(cast<EnumConstantDecl>(D));
Chris Lattner84915fa2007-06-02 04:16:21 +000045 }
46}
47
Chris Lattner84915fa2007-06-02 04:16:21 +000048void CodeGenFunction::EmitEnumConstantDecl(const EnumConstantDecl &D) {
49 assert(0 && "FIXME: Enum constant decls not implemented yet!");
50}
Chris Lattner03df1222007-06-02 04:53:11 +000051
52/// EmitBlockVarDecl - This method handles emission of any variable declaration
53/// inside a function, including static vars etc.
Steve Naroff08899ff2008-04-15 22:42:06 +000054void CodeGenFunction::EmitBlockVarDecl(const VarDecl &D) {
Chris Lattner03df1222007-06-02 04:53:11 +000055 switch (D.getStorageClass()) {
56 case VarDecl::Static:
Anders Carlssonf94cd1f2007-10-17 00:52:43 +000057 return EmitStaticBlockVarDecl(D);
Chris Lattner03df1222007-06-02 04:53:11 +000058 case VarDecl::Extern:
Lauro Ramos Venanciobada8d42008-02-16 22:30:38 +000059 // Don't emit it now, allow it to be emitted lazily on its first use.
60 return;
Chris Lattner03df1222007-06-02 04:53:11 +000061 default:
62 assert((D.getStorageClass() == VarDecl::None ||
63 D.getStorageClass() == VarDecl::Auto ||
64 D.getStorageClass() == VarDecl::Register) &&
65 "Unknown storage class");
66 return EmitLocalBlockVarDecl(D);
67 }
68}
69
Steve Naroff08899ff2008-04-15 22:42:06 +000070void CodeGenFunction::EmitStaticBlockVarDecl(const VarDecl &D) {
Chris Lattnerfc4379f2008-04-06 23:10:54 +000071 QualType Ty = D.getType();
Eli Friedmana682d392008-02-15 12:20:59 +000072 assert(Ty->isConstantSizeType() && "VLAs can't be static");
Anders Carlssonf94cd1f2007-10-17 00:52:43 +000073
74 llvm::Value *&DMEntry = LocalDeclMap[&D];
75 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
76
Chris Lattner41a1ef02008-01-09 18:47:25 +000077 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
Anders Carlssonf94cd1f2007-10-17 00:52:43 +000078 llvm::Constant *Init = 0;
79 if (D.getInit() == 0) {
80 Init = llvm::Constant::getNullValue(LTy);
Oliver Huntaefc8fd2007-12-02 00:11:25 +000081 } else {
Lauro Ramos Venancio01a72ff2008-02-26 21:41:45 +000082 Init = CGM.EmitConstantExpr(D.getInit(), this);
Devang Patelffdb07c2007-10-26 17:50:58 +000083 }
84
Oliver Huntaefc8fd2007-12-02 00:11:25 +000085 assert(Init && "Unable to create initialiser for static decl");
Nate Begemanfaae0812008-04-19 04:17:09 +000086
Chris Lattner8b945ee2008-02-06 04:54:32 +000087 std::string ContextName;
Chris Lattner5506f8c2008-04-04 04:07:35 +000088 if (const FunctionDecl * FD = dyn_cast<FunctionDecl>(CurFuncDecl))
89 ContextName = FD->getName();
Chris Lattner8b945ee2008-02-06 04:54:32 +000090 else
91 assert(0 && "Unknown context for block var decl"); // FIXME Handle objc.
Nate Begemanfaae0812008-04-19 04:17:09 +000092
93 llvm::GlobalValue *GV =
94 new llvm::GlobalVariable(LTy, false, llvm::GlobalValue::InternalLinkage,
Chris Lattner8b945ee2008-02-06 04:54:32 +000095 Init, ContextName + "." + D.getName(),
Nate Begemanfaae0812008-04-19 04:17:09 +000096 &CGM.getModule(), 0, Ty.getAddressSpace());
97
98 if (const AnnotateAttr *AA = D.getAttr<AnnotateAttr>()) {
99 SourceManager &SM = CGM.getContext().getSourceManager();
100 llvm::Constant *Ann =
101 CGM.EmitAnnotateAttr(GV, AA, SM.getLogicalLineNumber(D.getLocation()));
102 CGM.AddAnnotation(Ann);
103 }
104
105 DMEntry = GV;
Anders Carlssonf94cd1f2007-10-17 00:52:43 +0000106}
107
Chris Lattner03df1222007-06-02 04:53:11 +0000108/// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a
109/// variable declaration with auto, register, or no storage class specifier.
110/// These turn into simple stack objects.
Steve Naroff08899ff2008-04-15 22:42:06 +0000111void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) {
Chris Lattnerfc4379f2008-04-06 23:10:54 +0000112 QualType Ty = D.getType();
Chris Lattner03df1222007-06-02 04:53:11 +0000113
114 llvm::Value *DeclPtr;
Eli Friedmana682d392008-02-15 12:20:59 +0000115 if (Ty->isConstantSizeType()) {
Chris Lattner03df1222007-06-02 04:53:11 +0000116 // A normal fixed sized variable becomes an alloca in the entry block.
Chris Lattnerf033c142007-06-22 19:05:19 +0000117 const llvm::Type *LTy = ConvertType(Ty);
Chris Lattner03df1222007-06-02 04:53:11 +0000118 // TODO: Alignment
Chris Lattnere9a64532007-06-22 21:44:33 +0000119 DeclPtr = CreateTempAlloca(LTy, D.getName());
Chris Lattner03df1222007-06-02 04:53:11 +0000120 } else {
121 // TODO: Create a dynamic alloca.
122 assert(0 && "FIXME: Local VLAs not implemented yet");
123 }
124
125 llvm::Value *&DMEntry = LocalDeclMap[&D];
126 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
127 DMEntry = DeclPtr;
128
Chris Lattner07eb7332007-07-12 00:39:48 +0000129 // If this local has an initializer, emit it now.
Chris Lattner2da04b32007-08-24 05:35:26 +0000130 if (const Expr *Init = D.getInit()) {
Chris Lattnerb753f662007-08-26 07:29:23 +0000131 if (!hasAggregateLLVMType(Init->getType())) {
132 llvm::Value *V = EmitScalarExpr(Init);
Chris Lattnerf6dcc9d2007-08-26 07:30:49 +0000133 Builder.CreateStore(V, DeclPtr, D.getType().isVolatileQualified());
Chris Lattnerf3bc75a2008-04-04 16:54:41 +0000134 } else if (Init->getType()->isAnyComplexType()) {
Chris Lattnerb84bb952007-08-26 16:22:13 +0000135 EmitComplexExprIntoAddr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9de95272007-08-26 05:13:54 +0000136 } else {
Chris Lattnerf6dcc9d2007-08-26 07:30:49 +0000137 EmitAggExpr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9de95272007-08-26 05:13:54 +0000138 }
Chris Lattner2da04b32007-08-24 05:35:26 +0000139 }
Chris Lattner03df1222007-06-02 04:53:11 +0000140}
Chris Lattner53621a52007-06-13 20:44:40 +0000141
142/// Emit an alloca for the specified parameter and set up LocalDeclMap.
143void CodeGenFunction::EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg) {
Chris Lattnerfc4379f2008-04-06 23:10:54 +0000144 QualType Ty = D.getType();
Chris Lattner53621a52007-06-13 20:44:40 +0000145
146 llvm::Value *DeclPtr;
Eli Friedmana682d392008-02-15 12:20:59 +0000147 if (!Ty->isConstantSizeType()) {
Chris Lattner53621a52007-06-13 20:44:40 +0000148 // Variable sized values always are passed by-reference.
149 DeclPtr = Arg;
Chris Lattner53621a52007-06-13 20:44:40 +0000150 } else {
151 // A fixed sized first class variable becomes an alloca in the entry block.
Chris Lattnerf033c142007-06-22 19:05:19 +0000152 const llvm::Type *LTy = ConvertType(Ty);
Chris Lattner53621a52007-06-13 20:44:40 +0000153 if (LTy->isFirstClassType()) {
154 // TODO: Alignment
Chris Lattner23b7eb62007-06-15 23:05:46 +0000155 DeclPtr = new llvm::AllocaInst(LTy, 0, std::string(D.getName())+".addr",
156 AllocaInsertPt);
Chris Lattner53621a52007-06-13 20:44:40 +0000157
158 // Store the initial value into the alloca.
159 Builder.CreateStore(Arg, DeclPtr);
Chris Lattner53621a52007-06-13 20:44:40 +0000160 } else {
161 // Otherwise, if this is an aggregate, just use the input pointer.
Chris Lattner0fb84652007-06-22 18:57:44 +0000162 DeclPtr = Arg;
Chris Lattner53621a52007-06-13 20:44:40 +0000163 }
Chris Lattnerf39b03d2007-06-22 18:15:16 +0000164 Arg->setName(D.getName());
Chris Lattner53621a52007-06-13 20:44:40 +0000165 }
166
167 llvm::Value *&DMEntry = LocalDeclMap[&D];
168 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
169 DMEntry = DeclPtr;
170}
171