blob: a3155c4efa130f8ae97bf02b6293b07cd9201f23 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Decl nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
Anders Carlsson1a86b332007-10-17 00:52:43 +000015#include "CodeGenModule.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/AST/AST.h"
Anders Carlsson1a86b332007-10-17 00:52:43 +000017#include "llvm/GlobalVariable.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "llvm/Type.h"
19using namespace clang;
20using namespace CodeGen;
21
22
23void CodeGenFunction::EmitDecl(const Decl &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +000024 switch (D.getKind()) {
25 default: assert(0 && "Unknown decl kind!");
Chris Lattneraa9fc462007-10-08 21:37:32 +000026 case Decl::FileVar:
Reid Spencer5f016e22007-07-11 17:01:13 +000027 assert(0 && "Should not see file-scope variables inside a function!");
Chris Lattneraa9fc462007-10-08 21:37:32 +000028 case Decl::ParmVar:
Reid Spencer5f016e22007-07-11 17:01:13 +000029 assert(0 && "Parmdecls should not be in declstmts!");
30 case Decl::Typedef: // typedef int X;
31 case Decl::Function: // void X();
32 case Decl::Struct: // struct X;
33 case Decl::Union: // union X;
34 case Decl::Class: // class X;
35 case Decl::Enum: // enum X;
36 // None of these decls require codegen support.
37 return;
38
Chris Lattneraa9fc462007-10-08 21:37:32 +000039 case Decl::BlockVar:
Reid Spencer5f016e22007-07-11 17:01:13 +000040 return EmitBlockVarDecl(cast<BlockVarDecl>(D));
41 case Decl::EnumConstant:
42 return EmitEnumConstantDecl(cast<EnumConstantDecl>(D));
43 }
44}
45
46void CodeGenFunction::EmitEnumConstantDecl(const EnumConstantDecl &D) {
47 assert(0 && "FIXME: Enum constant decls not implemented yet!");
48}
49
50/// EmitBlockVarDecl - This method handles emission of any variable declaration
51/// inside a function, including static vars etc.
52void CodeGenFunction::EmitBlockVarDecl(const BlockVarDecl &D) {
53 switch (D.getStorageClass()) {
54 case VarDecl::Static:
Anders Carlsson1a86b332007-10-17 00:52:43 +000055 return EmitStaticBlockVarDecl(D);
Reid Spencer5f016e22007-07-11 17:01:13 +000056 case VarDecl::Extern:
Lauro Ramos Venanciofea90b82008-02-16 22:30:38 +000057 // Don't emit it now, allow it to be emitted lazily on its first use.
58 return;
Reid Spencer5f016e22007-07-11 17:01:13 +000059 default:
60 assert((D.getStorageClass() == VarDecl::None ||
61 D.getStorageClass() == VarDecl::Auto ||
62 D.getStorageClass() == VarDecl::Register) &&
63 "Unknown storage class");
64 return EmitLocalBlockVarDecl(D);
65 }
66}
67
Anders Carlsson1a86b332007-10-17 00:52:43 +000068void CodeGenFunction::EmitStaticBlockVarDecl(const BlockVarDecl &D) {
69 QualType Ty = D.getCanonicalType();
Eli Friedman3c2b3172008-02-15 12:20:59 +000070 assert(Ty->isConstantSizeType() && "VLAs can't be static");
Anders Carlsson1a86b332007-10-17 00:52:43 +000071
72 llvm::Value *&DMEntry = LocalDeclMap[&D];
73 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
74
Chris Lattner19009e62008-01-09 18:47:25 +000075 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
Anders Carlsson1a86b332007-10-17 00:52:43 +000076 llvm::Constant *Init = 0;
77 if (D.getInit() == 0) {
78 Init = llvm::Constant::getNullValue(LTy);
Oliver Hunt28247232007-12-02 00:11:25 +000079 } else {
Lauro Ramos Venancio81373352008-02-26 21:41:45 +000080 Init = CGM.EmitConstantExpr(D.getInit(), this);
Devang Patele40daa42007-10-26 17:50:58 +000081 }
82
Oliver Hunt28247232007-12-02 00:11:25 +000083 assert(Init && "Unable to create initialiser for static decl");
Anders Carlsson1a86b332007-10-17 00:52:43 +000084
Chris Lattner352ffde22008-02-06 04:54:32 +000085 std::string ContextName;
Chris Lattnerc8aa5f12008-04-04 04:07:35 +000086 if (const FunctionDecl * FD = dyn_cast<FunctionDecl>(CurFuncDecl))
87 ContextName = FD->getName();
Chris Lattner352ffde22008-02-06 04:54:32 +000088 else
89 assert(0 && "Unknown context for block var decl"); // FIXME Handle objc.
90
Anders Carlsson1a86b332007-10-17 00:52:43 +000091 DMEntry =
92 new llvm::GlobalVariable(LTy, false,
93 llvm::GlobalValue::InternalLinkage,
Chris Lattner352ffde22008-02-06 04:54:32 +000094 Init, ContextName + "." + D.getName(),
95 &CGM.getModule(), 0,
Christopher Lambebb97e92008-02-04 02:31:56 +000096 Ty.getAddressSpace());
Anders Carlsson1a86b332007-10-17 00:52:43 +000097
98}
99
Reid Spencer5f016e22007-07-11 17:01:13 +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.
103void CodeGenFunction::EmitLocalBlockVarDecl(const BlockVarDecl &D) {
104 QualType Ty = D.getCanonicalType();
105
106 llvm::Value *DeclPtr;
Eli Friedman3c2b3172008-02-15 12:20:59 +0000107 if (Ty->isConstantSizeType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +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
Chris Lattner19785962007-07-12 00:39:48 +0000121 // If this local has an initializer, emit it now.
Chris Lattner7f02f722007-08-24 05:35:26 +0000122 if (const Expr *Init = D.getInit()) {
Chris Lattnerd31beb12007-08-26 07:29:23 +0000123 if (!hasAggregateLLVMType(Init->getType())) {
124 llvm::Value *V = EmitScalarExpr(Init);
Chris Lattner8b2f3b72007-08-26 07:30:49 +0000125 Builder.CreateStore(V, DeclPtr, D.getType().isVolatileQualified());
Chris Lattnerd31beb12007-08-26 07:29:23 +0000126 } else if (Init->getType()->isComplexType()) {
Chris Lattner190dbe22007-08-26 16:22:13 +0000127 EmitComplexExprIntoAddr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9a19edf2007-08-26 05:13:54 +0000128 } else {
Chris Lattner8b2f3b72007-08-26 07:30:49 +0000129 EmitAggExpr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9a19edf2007-08-26 05:13:54 +0000130 }
Chris Lattner7f02f722007-08-24 05:35:26 +0000131 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000132}
133
134/// Emit an alloca for the specified parameter and set up LocalDeclMap.
135void CodeGenFunction::EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg) {
136 QualType Ty = D.getCanonicalType();
137
138 llvm::Value *DeclPtr;
Eli Friedman3c2b3172008-02-15 12:20:59 +0000139 if (!Ty->isConstantSizeType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +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