blob: df5882c2172b51a90325a438c18422e16e368d49 [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"
Nate Begeman8bd4afe2008-04-19 04:17:09 +000017#include "clang/Basic/SourceManager.h"
Anders Carlsson1a86b332007-10-17 00:52:43 +000018#include "llvm/GlobalVariable.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "llvm/Type.h"
20using namespace clang;
21using namespace CodeGen;
22
23
24void CodeGenFunction::EmitDecl(const Decl &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +000025 switch (D.getKind()) {
26 default: assert(0 && "Unknown decl kind!");
Chris Lattneraa9fc462007-10-08 21:37:32 +000027 case Decl::ParmVar:
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroff248a7532008-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
Reid Spencer5f016e22007-07-11 17:01:13 +000043 case Decl::EnumConstant:
44 return EmitEnumConstantDecl(cast<EnumConstantDecl>(D));
45 }
46}
47
48void CodeGenFunction::EmitEnumConstantDecl(const EnumConstantDecl &D) {
49 assert(0 && "FIXME: Enum constant decls not implemented yet!");
50}
51
52/// EmitBlockVarDecl - This method handles emission of any variable declaration
53/// inside a function, including static vars etc.
Steve Naroff248a7532008-04-15 22:42:06 +000054void CodeGenFunction::EmitBlockVarDecl(const VarDecl &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +000055 switch (D.getStorageClass()) {
56 case VarDecl::Static:
Anders Carlsson1a86b332007-10-17 00:52:43 +000057 return EmitStaticBlockVarDecl(D);
Reid Spencer5f016e22007-07-11 17:01:13 +000058 case VarDecl::Extern:
Lauro Ramos Venanciofea90b82008-02-16 22:30:38 +000059 // Don't emit it now, allow it to be emitted lazily on its first use.
60 return;
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroff248a7532008-04-15 22:42:06 +000070void CodeGenFunction::EmitStaticBlockVarDecl(const VarDecl &D) {
Chris Lattner8bcfc5b2008-04-06 23:10:54 +000071 QualType Ty = D.getType();
Eli Friedman3c2b3172008-02-15 12:20:59 +000072 assert(Ty->isConstantSizeType() && "VLAs can't be static");
Anders Carlsson1a86b332007-10-17 00:52:43 +000073
74 llvm::Value *&DMEntry = LocalDeclMap[&D];
75 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
76
Chris Lattner19009e62008-01-09 18:47:25 +000077 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
Anders Carlsson1a86b332007-10-17 00:52:43 +000078 llvm::Constant *Init = 0;
79 if (D.getInit() == 0) {
80 Init = llvm::Constant::getNullValue(LTy);
Oliver Hunt28247232007-12-02 00:11:25 +000081 } else {
Lauro Ramos Venancio81373352008-02-26 21:41:45 +000082 Init = CGM.EmitConstantExpr(D.getInit(), this);
Devang Patele40daa42007-10-26 17:50:58 +000083 }
84
Oliver Hunt28247232007-12-02 00:11:25 +000085 assert(Init && "Unable to create initialiser for static decl");
Nate Begeman8bd4afe2008-04-19 04:17:09 +000086
Chris Lattner352ffde22008-02-06 04:54:32 +000087 std::string ContextName;
Chris Lattnerc8aa5f12008-04-04 04:07:35 +000088 if (const FunctionDecl * FD = dyn_cast<FunctionDecl>(CurFuncDecl))
89 ContextName = FD->getName();
Chris Lattner352ffde22008-02-06 04:54:32 +000090 else
91 assert(0 && "Unknown context for block var decl"); // FIXME Handle objc.
Nate Begeman8bd4afe2008-04-19 04:17:09 +000092
93 llvm::GlobalValue *GV =
94 new llvm::GlobalVariable(LTy, false, llvm::GlobalValue::InternalLinkage,
Chris Lattner352ffde22008-02-06 04:54:32 +000095 Init, ContextName + "." + D.getName(),
Nate Begeman8bd4afe2008-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 Carlsson1a86b332007-10-17 00:52:43 +0000106}
107
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroff248a7532008-04-15 22:42:06 +0000111void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) {
Chris Lattner8bcfc5b2008-04-06 23:10:54 +0000112 QualType Ty = D.getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000113
114 llvm::Value *DeclPtr;
Eli Friedman3c2b3172008-02-15 12:20:59 +0000115 if (Ty->isConstantSizeType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 // A normal fixed sized variable becomes an alloca in the entry block.
117 const llvm::Type *LTy = ConvertType(Ty);
118 // TODO: Alignment
119 DeclPtr = CreateTempAlloca(LTy, D.getName());
120 } 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 Lattner19785962007-07-12 00:39:48 +0000129 // If this local has an initializer, emit it now.
Chris Lattner7f02f722007-08-24 05:35:26 +0000130 if (const Expr *Init = D.getInit()) {
Chris Lattnerd31beb12007-08-26 07:29:23 +0000131 if (!hasAggregateLLVMType(Init->getType())) {
132 llvm::Value *V = EmitScalarExpr(Init);
Chris Lattner8b2f3b72007-08-26 07:30:49 +0000133 Builder.CreateStore(V, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9b2dc282008-04-04 16:54:41 +0000134 } else if (Init->getType()->isAnyComplexType()) {
Chris Lattner190dbe22007-08-26 16:22:13 +0000135 EmitComplexExprIntoAddr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9a19edf2007-08-26 05:13:54 +0000136 } else {
Chris Lattner8b2f3b72007-08-26 07:30:49 +0000137 EmitAggExpr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9a19edf2007-08-26 05:13:54 +0000138 }
Chris Lattner7f02f722007-08-24 05:35:26 +0000139 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000140}
141
142/// Emit an alloca for the specified parameter and set up LocalDeclMap.
143void CodeGenFunction::EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg) {
Chris Lattner8bcfc5b2008-04-06 23:10:54 +0000144 QualType Ty = D.getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000145
146 llvm::Value *DeclPtr;
Eli Friedman3c2b3172008-02-15 12:20:59 +0000147 if (!Ty->isConstantSizeType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 // Variable sized values always are passed by-reference.
149 DeclPtr = Arg;
150 } else {
151 // A fixed sized first class variable becomes an alloca in the entry block.
152 const llvm::Type *LTy = ConvertType(Ty);
153 if (LTy->isFirstClassType()) {
154 // TODO: Alignment
155 DeclPtr = new llvm::AllocaInst(LTy, 0, std::string(D.getName())+".addr",
156 AllocaInsertPt);
157
158 // Store the initial value into the alloca.
159 Builder.CreateStore(Arg, DeclPtr);
160 } else {
161 // Otherwise, if this is an aggregate, just use the input pointer.
162 DeclPtr = Arg;
163 }
164 Arg->setName(D.getName());
165 }
166
167 llvm::Value *&DMEntry = LocalDeclMap[&D];
168 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
169 DMEntry = DeclPtr;
170}
171