blob: 75a32854ab5870c938939a0113606d903c68466f [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//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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
74 const llvm::Type *LTy = ConvertType(Ty);
75 llvm::Constant *Init = 0;
76 if (D.getInit() == 0) {
77 Init = llvm::Constant::getNullValue(LTy);
78 } else
79 assert(0 && "FIXME: Support initializers");
80
81
82 DMEntry =
83 new llvm::GlobalVariable(LTy, false,
84 llvm::GlobalValue::InternalLinkage,
85 Init, D.getName(), &CGM.getModule());
86
87}
88
Reid Spencer5f016e22007-07-11 17:01:13 +000089/// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a
90/// variable declaration with auto, register, or no storage class specifier.
91/// These turn into simple stack objects.
92void CodeGenFunction::EmitLocalBlockVarDecl(const BlockVarDecl &D) {
93 QualType Ty = D.getCanonicalType();
94
95 llvm::Value *DeclPtr;
Chris Lattner590b6642007-07-15 23:26:56 +000096 if (Ty->isConstantSizeType(getContext())) {
Reid Spencer5f016e22007-07-11 17:01:13 +000097 // A normal fixed sized variable becomes an alloca in the entry block.
98 const llvm::Type *LTy = ConvertType(Ty);
99 // TODO: Alignment
100 DeclPtr = CreateTempAlloca(LTy, D.getName());
101 } else {
102 // TODO: Create a dynamic alloca.
103 assert(0 && "FIXME: Local VLAs not implemented yet");
104 }
105
106 llvm::Value *&DMEntry = LocalDeclMap[&D];
107 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
108 DMEntry = DeclPtr;
109
Chris Lattner19785962007-07-12 00:39:48 +0000110 // If this local has an initializer, emit it now.
Chris Lattner7f02f722007-08-24 05:35:26 +0000111 if (const Expr *Init = D.getInit()) {
Chris Lattnerd31beb12007-08-26 07:29:23 +0000112 if (!hasAggregateLLVMType(Init->getType())) {
113 llvm::Value *V = EmitScalarExpr(Init);
Chris Lattner8b2f3b72007-08-26 07:30:49 +0000114 Builder.CreateStore(V, DeclPtr, D.getType().isVolatileQualified());
Chris Lattnerd31beb12007-08-26 07:29:23 +0000115 } else if (Init->getType()->isComplexType()) {
Chris Lattner190dbe22007-08-26 16:22:13 +0000116 EmitComplexExprIntoAddr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9a19edf2007-08-26 05:13:54 +0000117 } else {
Chris Lattner8b2f3b72007-08-26 07:30:49 +0000118 EmitAggExpr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9a19edf2007-08-26 05:13:54 +0000119 }
Chris Lattner7f02f722007-08-24 05:35:26 +0000120 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000121}
122
123/// Emit an alloca for the specified parameter and set up LocalDeclMap.
124void CodeGenFunction::EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg) {
125 QualType Ty = D.getCanonicalType();
126
127 llvm::Value *DeclPtr;
Chris Lattner590b6642007-07-15 23:26:56 +0000128 if (!Ty->isConstantSizeType(getContext())) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 // Variable sized values always are passed by-reference.
130 DeclPtr = Arg;
131 } else {
132 // A fixed sized first class variable becomes an alloca in the entry block.
133 const llvm::Type *LTy = ConvertType(Ty);
134 if (LTy->isFirstClassType()) {
135 // TODO: Alignment
136 DeclPtr = new llvm::AllocaInst(LTy, 0, std::string(D.getName())+".addr",
137 AllocaInsertPt);
138
139 // Store the initial value into the alloca.
140 Builder.CreateStore(Arg, DeclPtr);
141 } else {
142 // Otherwise, if this is an aggregate, just use the input pointer.
143 DeclPtr = Arg;
144 }
145 Arg->setName(D.getName());
146 }
147
148 llvm::Value *&DMEntry = LocalDeclMap[&D];
149 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
150 DMEntry = DeclPtr;
151}
152