blob: a6c5da7c2622311f372e400fc92a26abe7c8a07f [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:
57 assert(0 && "FIXME: should call up to codegenmodule");
58 default:
59 assert((D.getStorageClass() == VarDecl::None ||
60 D.getStorageClass() == VarDecl::Auto ||
61 D.getStorageClass() == VarDecl::Register) &&
62 "Unknown storage class");
63 return EmitLocalBlockVarDecl(D);
64 }
65}
66
Anders Carlsson1a86b332007-10-17 00:52:43 +000067void CodeGenFunction::EmitStaticBlockVarDecl(const BlockVarDecl &D) {
68 QualType Ty = D.getCanonicalType();
69 assert(Ty->isConstantSizeType(getContext()) && "VLAs can't be static");
70
71 llvm::Value *&DMEntry = LocalDeclMap[&D];
72 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
73
Chris Lattner19009e62008-01-09 18:47:25 +000074 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
Anders Carlsson1a86b332007-10-17 00:52:43 +000075 llvm::Constant *Init = 0;
76 if (D.getInit() == 0) {
77 Init = llvm::Constant::getNullValue(LTy);
Oliver Hunt28247232007-12-02 00:11:25 +000078 } else {
79 Init = CGM.EmitGlobalInit(D.getInit());
Devang Patele40daa42007-10-26 17:50:58 +000080 }
81
Oliver Hunt28247232007-12-02 00:11:25 +000082 assert(Init && "Unable to create initialiser for static decl");
Anders Carlsson1a86b332007-10-17 00:52:43 +000083
84 DMEntry =
85 new llvm::GlobalVariable(LTy, false,
86 llvm::GlobalValue::InternalLinkage,
Christopher Lambebb97e92008-02-04 02:31:56 +000087 Init, D.getName(), &CGM.getModule(), 0,
88 Ty.getAddressSpace());
Anders Carlsson1a86b332007-10-17 00:52:43 +000089
90}
91
Reid Spencer5f016e22007-07-11 17:01:13 +000092/// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a
93/// variable declaration with auto, register, or no storage class specifier.
94/// These turn into simple stack objects.
95void CodeGenFunction::EmitLocalBlockVarDecl(const BlockVarDecl &D) {
96 QualType Ty = D.getCanonicalType();
97
98 llvm::Value *DeclPtr;
Chris Lattner590b6642007-07-15 23:26:56 +000099 if (Ty->isConstantSizeType(getContext())) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 // A normal fixed sized variable becomes an alloca in the entry block.
101 const llvm::Type *LTy = ConvertType(Ty);
102 // TODO: Alignment
103 DeclPtr = CreateTempAlloca(LTy, D.getName());
104 } else {
105 // TODO: Create a dynamic alloca.
106 assert(0 && "FIXME: Local VLAs not implemented yet");
107 }
108
109 llvm::Value *&DMEntry = LocalDeclMap[&D];
110 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
111 DMEntry = DeclPtr;
112
Chris Lattner19785962007-07-12 00:39:48 +0000113 // If this local has an initializer, emit it now.
Chris Lattner7f02f722007-08-24 05:35:26 +0000114 if (const Expr *Init = D.getInit()) {
Chris Lattnerd31beb12007-08-26 07:29:23 +0000115 if (!hasAggregateLLVMType(Init->getType())) {
116 llvm::Value *V = EmitScalarExpr(Init);
Chris Lattner8b2f3b72007-08-26 07:30:49 +0000117 Builder.CreateStore(V, DeclPtr, D.getType().isVolatileQualified());
Chris Lattnerd31beb12007-08-26 07:29:23 +0000118 } else if (Init->getType()->isComplexType()) {
Chris Lattner190dbe22007-08-26 16:22:13 +0000119 EmitComplexExprIntoAddr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9a19edf2007-08-26 05:13:54 +0000120 } else {
Chris Lattner8b2f3b72007-08-26 07:30:49 +0000121 EmitAggExpr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9a19edf2007-08-26 05:13:54 +0000122 }
Chris Lattner7f02f722007-08-24 05:35:26 +0000123 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000124}
125
126/// Emit an alloca for the specified parameter and set up LocalDeclMap.
127void CodeGenFunction::EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg) {
128 QualType Ty = D.getCanonicalType();
129
130 llvm::Value *DeclPtr;
Chris Lattner590b6642007-07-15 23:26:56 +0000131 if (!Ty->isConstantSizeType(getContext())) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 // Variable sized values always are passed by-reference.
133 DeclPtr = Arg;
134 } else {
135 // A fixed sized first class variable becomes an alloca in the entry block.
136 const llvm::Type *LTy = ConvertType(Ty);
137 if (LTy->isFirstClassType()) {
138 // TODO: Alignment
139 DeclPtr = new llvm::AllocaInst(LTy, 0, std::string(D.getName())+".addr",
140 AllocaInsertPt);
141
142 // Store the initial value into the alloca.
143 Builder.CreateStore(Arg, DeclPtr);
144 } else {
145 // Otherwise, if this is an aggregate, just use the input pointer.
146 DeclPtr = Arg;
147 }
148 Arg->setName(D.getName());
149 }
150
151 llvm::Value *&DMEntry = LocalDeclMap[&D];
152 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
153 DMEntry = DeclPtr;
154}
155