blob: e186bb69ebf01234a015201016523869dd714caf [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
14#include "CodeGenFunction.h"
Anders Carlsson855d78d2007-10-17 00:52:43 +000015#include "CodeGenModule.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/AST/AST.h"
Anders Carlsson855d78d2007-10-17 00:52:43 +000017#include "llvm/GlobalVariable.h"
Chris Lattner4b009652007-07-25 00:24:17 +000018#include "llvm/Type.h"
19using namespace clang;
20using namespace CodeGen;
21
22
23void CodeGenFunction::EmitDecl(const Decl &D) {
24 switch (D.getKind()) {
25 default: assert(0 && "Unknown decl kind!");
Chris Lattner3289bca2007-10-08 21:37:32 +000026 case Decl::ParmVar:
Chris Lattner4b009652007-07-25 00:24:17 +000027 assert(0 && "Parmdecls should not be in declstmts!");
28 case Decl::Typedef: // typedef int X;
29 case Decl::Function: // void X();
30 case Decl::Struct: // struct X;
31 case Decl::Union: // union X;
32 case Decl::Class: // class X;
33 case Decl::Enum: // enum X;
34 // None of these decls require codegen support.
35 return;
36
Steve Naroff72a6ebc2008-04-15 22:42:06 +000037 case Decl::Var:
38 if (cast<VarDecl>(D).isBlockVarDecl())
39 return EmitBlockVarDecl(cast<VarDecl>(D));
40 assert(0 && "Should not see file-scope variables inside a function!");
41
Chris Lattner4b009652007-07-25 00:24:17 +000042 case Decl::EnumConstant:
43 return EmitEnumConstantDecl(cast<EnumConstantDecl>(D));
44 }
45}
46
47void CodeGenFunction::EmitEnumConstantDecl(const EnumConstantDecl &D) {
48 assert(0 && "FIXME: Enum constant decls not implemented yet!");
49}
50
51/// EmitBlockVarDecl - This method handles emission of any variable declaration
52/// inside a function, including static vars etc.
Steve Naroff72a6ebc2008-04-15 22:42:06 +000053void CodeGenFunction::EmitBlockVarDecl(const VarDecl &D) {
Chris Lattner4b009652007-07-25 00:24:17 +000054 switch (D.getStorageClass()) {
55 case VarDecl::Static:
Anders Carlsson855d78d2007-10-17 00:52:43 +000056 return EmitStaticBlockVarDecl(D);
Chris Lattner4b009652007-07-25 00:24:17 +000057 case VarDecl::Extern:
Lauro Ramos Venancio2348d972008-02-16 22:30:38 +000058 // Don't emit it now, allow it to be emitted lazily on its first use.
59 return;
Chris Lattner4b009652007-07-25 00:24:17 +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
Steve Naroff72a6ebc2008-04-15 22:42:06 +000069void CodeGenFunction::EmitStaticBlockVarDecl(const VarDecl &D) {
Chris Lattner42a21742008-04-06 23:10:54 +000070 QualType Ty = D.getType();
Eli Friedman62f67fd2008-02-15 12:20:59 +000071 assert(Ty->isConstantSizeType() && "VLAs can't be static");
Anders Carlsson855d78d2007-10-17 00:52:43 +000072
73 llvm::Value *&DMEntry = LocalDeclMap[&D];
74 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
75
Chris Lattner43cdbf52008-01-09 18:47:25 +000076 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
Anders Carlsson855d78d2007-10-17 00:52:43 +000077 llvm::Constant *Init = 0;
78 if (D.getInit() == 0) {
79 Init = llvm::Constant::getNullValue(LTy);
Oliver Hunt253e0a72007-12-02 00:11:25 +000080 } else {
Lauro Ramos Venancio934fb022008-02-26 21:41:45 +000081 Init = CGM.EmitConstantExpr(D.getInit(), this);
Devang Patel2ecd9012007-10-26 17:50:58 +000082 }
83
Oliver Hunt253e0a72007-12-02 00:11:25 +000084 assert(Init && "Unable to create initialiser for static decl");
Anders Carlsson855d78d2007-10-17 00:52:43 +000085
Chris Lattner9b026342008-02-06 04:54:32 +000086 std::string ContextName;
Chris Lattner6e6a5972008-04-04 04:07:35 +000087 if (const FunctionDecl * FD = dyn_cast<FunctionDecl>(CurFuncDecl))
88 ContextName = FD->getName();
Chris Lattner9b026342008-02-06 04:54:32 +000089 else
90 assert(0 && "Unknown context for block var decl"); // FIXME Handle objc.
91
Anders Carlsson855d78d2007-10-17 00:52:43 +000092 DMEntry =
93 new llvm::GlobalVariable(LTy, false,
94 llvm::GlobalValue::InternalLinkage,
Chris Lattner9b026342008-02-06 04:54:32 +000095 Init, ContextName + "." + D.getName(),
96 &CGM.getModule(), 0,
Christopher Lamb2a72bb32008-02-04 02:31:56 +000097 Ty.getAddressSpace());
Anders Carlsson855d78d2007-10-17 00:52:43 +000098}
99
Chris Lattner4b009652007-07-25 00:24:17 +0000100/// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a
101/// variable declaration with auto, register, or no storage class specifier.
102/// These turn into simple stack objects.
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000103void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) {
Chris Lattner42a21742008-04-06 23:10:54 +0000104 QualType Ty = D.getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000105
106 llvm::Value *DeclPtr;
Eli Friedman62f67fd2008-02-15 12:20:59 +0000107 if (Ty->isConstantSizeType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000108 // A normal fixed sized variable becomes an alloca in the entry block.
109 const llvm::Type *LTy = ConvertType(Ty);
110 // TODO: Alignment
111 DeclPtr = CreateTempAlloca(LTy, D.getName());
112 } else {
113 // TODO: Create a dynamic alloca.
114 assert(0 && "FIXME: Local VLAs not implemented yet");
115 }
116
117 llvm::Value *&DMEntry = LocalDeclMap[&D];
118 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
119 DMEntry = DeclPtr;
120
121 // If this local has an initializer, emit it now.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000122 if (const Expr *Init = D.getInit()) {
Chris Lattner222d3222007-08-26 07:29:23 +0000123 if (!hasAggregateLLVMType(Init->getType())) {
124 llvm::Value *V = EmitScalarExpr(Init);
Chris Lattner9a918dd2007-08-26 07:30:49 +0000125 Builder.CreateStore(V, DeclPtr, D.getType().isVolatileQualified());
Chris Lattnerde0908b2008-04-04 16:54:41 +0000126 } else if (Init->getType()->isAnyComplexType()) {
Chris Lattner8e1f6e02007-08-26 16:22:13 +0000127 EmitComplexExprIntoAddr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner64a113b2007-08-26 05:13:54 +0000128 } else {
Chris Lattner9a918dd2007-08-26 07:30:49 +0000129 EmitAggExpr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner64a113b2007-08-26 05:13:54 +0000130 }
Chris Lattner9fba49a2007-08-24 05:35:26 +0000131 }
Chris Lattner4b009652007-07-25 00:24:17 +0000132}
133
134/// Emit an alloca for the specified parameter and set up LocalDeclMap.
135void CodeGenFunction::EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg) {
Chris Lattner42a21742008-04-06 23:10:54 +0000136 QualType Ty = D.getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000137
138 llvm::Value *DeclPtr;
Eli Friedman62f67fd2008-02-15 12:20:59 +0000139 if (!Ty->isConstantSizeType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000140 // Variable sized values always are passed by-reference.
141 DeclPtr = Arg;
142 } else {
143 // A fixed sized first class variable becomes an alloca in the entry block.
144 const llvm::Type *LTy = ConvertType(Ty);
145 if (LTy->isFirstClassType()) {
146 // TODO: Alignment
147 DeclPtr = new llvm::AllocaInst(LTy, 0, std::string(D.getName())+".addr",
148 AllocaInsertPt);
149
150 // Store the initial value into the alloca.
151 Builder.CreateStore(Arg, DeclPtr);
152 } else {
153 // Otherwise, if this is an aggregate, just use the input pointer.
154 DeclPtr = Arg;
155 }
156 Arg->setName(D.getName());
157 }
158
159 llvm::Value *&DMEntry = LocalDeclMap[&D];
160 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
161 DMEntry = DeclPtr;
162}
163