blob: bd68288bba30f8337ea39bc70eb88433cbabff10 [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"
Chris Lattner2621fd12008-05-08 05:58:21 +000018#include "clang/Basic/TargetInfo.h"
Anders Carlsson1a86b332007-10-17 00:52:43 +000019#include "llvm/GlobalVariable.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "llvm/Type.h"
21using namespace clang;
22using namespace CodeGen;
23
24
25void CodeGenFunction::EmitDecl(const Decl &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +000026 switch (D.getKind()) {
27 default: assert(0 && "Unknown decl kind!");
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
Steve Naroff248a7532008-04-15 22:42:06 +000039 case Decl::Var:
40 if (cast<VarDecl>(D).isBlockVarDecl())
41 return EmitBlockVarDecl(cast<VarDecl>(D));
42 assert(0 && "Should not see file-scope variables inside a function!");
43
Reid Spencer5f016e22007-07-11 17:01:13 +000044 case Decl::EnumConstant:
45 return EmitEnumConstantDecl(cast<EnumConstantDecl>(D));
46 }
47}
48
49void CodeGenFunction::EmitEnumConstantDecl(const EnumConstantDecl &D) {
50 assert(0 && "FIXME: Enum constant decls not implemented yet!");
51}
52
53/// EmitBlockVarDecl - This method handles emission of any variable declaration
54/// inside a function, including static vars etc.
Steve Naroff248a7532008-04-15 22:42:06 +000055void CodeGenFunction::EmitBlockVarDecl(const VarDecl &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +000056 switch (D.getStorageClass()) {
57 case VarDecl::Static:
Anders Carlsson1a86b332007-10-17 00:52:43 +000058 return EmitStaticBlockVarDecl(D);
Reid Spencer5f016e22007-07-11 17:01:13 +000059 case VarDecl::Extern:
Lauro Ramos Venanciofea90b82008-02-16 22:30:38 +000060 // Don't emit it now, allow it to be emitted lazily on its first use.
61 return;
Reid Spencer5f016e22007-07-11 17:01:13 +000062 default:
63 assert((D.getStorageClass() == VarDecl::None ||
64 D.getStorageClass() == VarDecl::Auto ||
65 D.getStorageClass() == VarDecl::Register) &&
66 "Unknown storage class");
67 return EmitLocalBlockVarDecl(D);
68 }
69}
70
Chris Lattner2621fd12008-05-08 05:58:21 +000071llvm::GlobalValue *
72CodeGenFunction::GenerateStaticBlockVarDecl(const VarDecl &D,
73 bool NoInit,
74 const char *Separator) {
Chris Lattner8bcfc5b2008-04-06 23:10:54 +000075 QualType Ty = D.getType();
Eli Friedman3c2b3172008-02-15 12:20:59 +000076 assert(Ty->isConstantSizeType() && "VLAs can't be static");
Anders Carlsson1a86b332007-10-17 00:52:43 +000077
Chris Lattner19009e62008-01-09 18:47:25 +000078 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
Anders Carlsson1a86b332007-10-17 00:52:43 +000079 llvm::Constant *Init = 0;
Chris Lattner2621fd12008-05-08 05:58:21 +000080 if ((D.getInit() == 0) || NoInit) {
Anders Carlsson1a86b332007-10-17 00:52:43 +000081 Init = llvm::Constant::getNullValue(LTy);
Oliver Hunt28247232007-12-02 00:11:25 +000082 } else {
Lauro Ramos Venancio81373352008-02-26 21:41:45 +000083 Init = CGM.EmitConstantExpr(D.getInit(), this);
Devang Patele40daa42007-10-26 17:50:58 +000084 }
85
Oliver Hunt28247232007-12-02 00:11:25 +000086 assert(Init && "Unable to create initialiser for static decl");
Nate Begeman8bd4afe2008-04-19 04:17:09 +000087
Chris Lattner352ffde22008-02-06 04:54:32 +000088 std::string ContextName;
Chris Lattnerc8aa5f12008-04-04 04:07:35 +000089 if (const FunctionDecl * FD = dyn_cast<FunctionDecl>(CurFuncDecl))
90 ContextName = FD->getName();
Chris Lattner352ffde22008-02-06 04:54:32 +000091 else
92 assert(0 && "Unknown context for block var decl"); // FIXME Handle objc.
Nate Begeman8bd4afe2008-04-19 04:17:09 +000093
94 llvm::GlobalValue *GV =
95 new llvm::GlobalVariable(LTy, false, llvm::GlobalValue::InternalLinkage,
Chris Lattner2621fd12008-05-08 05:58:21 +000096 Init, ContextName + Separator + D.getName(),
Nate Begeman8bd4afe2008-04-19 04:17:09 +000097 &CGM.getModule(), 0, Ty.getAddressSpace());
98
Chris Lattner2621fd12008-05-08 05:58:21 +000099 return GV;
100}
101
102void CodeGenFunction::EmitStaticBlockVarDecl(const VarDecl &D) {
103
104 llvm::Value *&DMEntry = LocalDeclMap[&D];
105 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
106
107 llvm::GlobalValue *GV = GenerateStaticBlockVarDecl(D, false, ".");
108
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000109 if (const AnnotateAttr *AA = D.getAttr<AnnotateAttr>()) {
110 SourceManager &SM = CGM.getContext().getSourceManager();
111 llvm::Constant *Ann =
112 CGM.EmitAnnotateAttr(GV, AA, SM.getLogicalLineNumber(D.getLocation()));
113 CGM.AddAnnotation(Ann);
114 }
115
116 DMEntry = GV;
Anders Carlsson1a86b332007-10-17 00:52:43 +0000117}
118
Reid Spencer5f016e22007-07-11 17:01:13 +0000119/// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a
120/// variable declaration with auto, register, or no storage class specifier.
Chris Lattner2621fd12008-05-08 05:58:21 +0000121/// These turn into simple stack objects, or GlobalValues depending on target.
Steve Naroff248a7532008-04-15 22:42:06 +0000122void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) {
Chris Lattner8bcfc5b2008-04-06 23:10:54 +0000123 QualType Ty = D.getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000124
125 llvm::Value *DeclPtr;
Eli Friedman3c2b3172008-02-15 12:20:59 +0000126 if (Ty->isConstantSizeType()) {
Chris Lattner2621fd12008-05-08 05:58:21 +0000127 if (!Target.useGlobalsForAutomaticVariables()) {
128 // A normal fixed sized variable becomes an alloca in the entry block.
129 const llvm::Type *LTy = ConvertType(Ty);
130 // TODO: Alignment
131 DeclPtr = CreateTempAlloca(LTy, D.getName());
132 } else {
133 // Targets that don't support recursion emit locals as globals.
134 const char *Class =
135 D.getStorageClass() == VarDecl::Register ? ".reg." : ".auto.";
136 DeclPtr = GenerateStaticBlockVarDecl(D, true, Class);
137 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000138 } else {
139 // TODO: Create a dynamic alloca.
140 assert(0 && "FIXME: Local VLAs not implemented yet");
141 }
142
143 llvm::Value *&DMEntry = LocalDeclMap[&D];
144 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
145 DMEntry = DeclPtr;
146
Chris Lattner19785962007-07-12 00:39:48 +0000147 // If this local has an initializer, emit it now.
Chris Lattner7f02f722007-08-24 05:35:26 +0000148 if (const Expr *Init = D.getInit()) {
Chris Lattnerd31beb12007-08-26 07:29:23 +0000149 if (!hasAggregateLLVMType(Init->getType())) {
150 llvm::Value *V = EmitScalarExpr(Init);
Chris Lattner8b2f3b72007-08-26 07:30:49 +0000151 Builder.CreateStore(V, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9b2dc282008-04-04 16:54:41 +0000152 } else if (Init->getType()->isAnyComplexType()) {
Chris Lattner190dbe22007-08-26 16:22:13 +0000153 EmitComplexExprIntoAddr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9a19edf2007-08-26 05:13:54 +0000154 } else {
Chris Lattner8b2f3b72007-08-26 07:30:49 +0000155 EmitAggExpr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9a19edf2007-08-26 05:13:54 +0000156 }
Chris Lattner7f02f722007-08-24 05:35:26 +0000157 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000158}
159
Chris Lattner2621fd12008-05-08 05:58:21 +0000160/// Emit an alloca (or GlobalValue depending on target)
161/// for the specified parameter and set up LocalDeclMap.
Reid Spencer5f016e22007-07-11 17:01:13 +0000162void CodeGenFunction::EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg) {
Chris Lattner8bcfc5b2008-04-06 23:10:54 +0000163 QualType Ty = D.getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000164
165 llvm::Value *DeclPtr;
Eli Friedman3c2b3172008-02-15 12:20:59 +0000166 if (!Ty->isConstantSizeType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000167 // Variable sized values always are passed by-reference.
168 DeclPtr = Arg;
Chris Lattner2621fd12008-05-08 05:58:21 +0000169 } else if (Target.useGlobalsForAutomaticVariables()) {
170 DeclPtr = GenerateStaticBlockVarDecl(D, true, ".arg.");
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 } else {
172 // A fixed sized first class variable becomes an alloca in the entry block.
173 const llvm::Type *LTy = ConvertType(Ty);
174 if (LTy->isFirstClassType()) {
175 // TODO: Alignment
176 DeclPtr = new llvm::AllocaInst(LTy, 0, std::string(D.getName())+".addr",
177 AllocaInsertPt);
178
179 // Store the initial value into the alloca.
180 Builder.CreateStore(Arg, DeclPtr);
181 } else {
182 // Otherwise, if this is an aggregate, just use the input pointer.
183 DeclPtr = Arg;
184 }
185 Arg->setName(D.getName());
186 }
187
188 llvm::Value *&DMEntry = LocalDeclMap[&D];
189 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
190 DMEntry = DeclPtr;
191}
192