blob: e0ea2b98eb07e3c42c09fa4cc1022a154ccb6208 [file] [log] [blame]
Anders Carlssone1b29ef2008-08-22 16:00:37 +00001//===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code dealing with C++ code generation.
11//
12//===----------------------------------------------------------------------===//
13
14// We might split this into multiple files if it gets too unwieldy
15
16#include "CodeGenFunction.h"
17#include "CodeGenModule.h"
Anders Carlsson283a0622009-04-13 18:03:33 +000018#include "Mangle.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
Anders Carlsson774e7c62009-04-03 22:50:24 +000021#include "clang/AST/DeclCXX.h"
Anders Carlsson86e96442008-08-23 19:42:54 +000022#include "clang/AST/DeclObjC.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000023#include "llvm/ADT/StringExtras.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000024using namespace clang;
25using namespace CodeGen;
26
Daniel Dunbar0096acf2009-02-25 19:24:29 +000027void
28CodeGenFunction::GenerateStaticCXXBlockVarDeclInit(const VarDecl &D,
29 llvm::GlobalVariable *GV) {
30 // FIXME: This should use __cxa_guard_{acquire,release}?
31
Anders Carlssone1b29ef2008-08-22 16:00:37 +000032 assert(!getContext().getLangOptions().ThreadsafeStatics &&
33 "thread safe statics are currently not supported!");
Anders Carlssone1b29ef2008-08-22 16:00:37 +000034
Anders Carlsson283a0622009-04-13 18:03:33 +000035 llvm::SmallString<256> GuardVName;
36 llvm::raw_svector_ostream GuardVOut(GuardVName);
37 mangleGuardVariable(&D, getContext(), GuardVOut);
38
Anders Carlssone1b29ef2008-08-22 16:00:37 +000039 // Create the guard variable.
40 llvm::GlobalValue *GuardV =
41 new llvm::GlobalVariable(llvm::Type::Int64Ty, false,
Daniel Dunbar0096acf2009-02-25 19:24:29 +000042 GV->getLinkage(),
Anders Carlssone1b29ef2008-08-22 16:00:37 +000043 llvm::Constant::getNullValue(llvm::Type::Int64Ty),
Anders Carlsson283a0622009-04-13 18:03:33 +000044 GuardVName.c_str(),
Anders Carlssone1b29ef2008-08-22 16:00:37 +000045 &CGM.getModule());
46
Anders Carlssone1b29ef2008-08-22 16:00:37 +000047 // Load the first byte of the guard variable.
Daniel Dunbar0096acf2009-02-25 19:24:29 +000048 const llvm::Type *PtrTy = llvm::PointerType::get(llvm::Type::Int8Ty, 0);
Anders Carlssone1b29ef2008-08-22 16:00:37 +000049 llvm::Value *V = Builder.CreateLoad(Builder.CreateBitCast(GuardV, PtrTy),
50 "tmp");
51
52 // Compare it against 0.
53 llvm::Value *nullValue = llvm::Constant::getNullValue(llvm::Type::Int8Ty);
54 llvm::Value *ICmp = Builder.CreateICmpEQ(V, nullValue , "tobool");
55
Daniel Dunbar55e87422008-11-11 02:29:29 +000056 llvm::BasicBlock *InitBlock = createBasicBlock("init");
Daniel Dunbar9615ecb2008-11-13 01:38:36 +000057 llvm::BasicBlock *EndBlock = createBasicBlock("init.end");
Anders Carlssone1b29ef2008-08-22 16:00:37 +000058
59 // If the guard variable is 0, jump to the initializer code.
60 Builder.CreateCondBr(ICmp, InitBlock, EndBlock);
61
62 EmitBlock(InitBlock);
63
Anders Carlssone1b29ef2008-08-22 16:00:37 +000064 const Expr *Init = D.getInit();
65 if (!hasAggregateLLVMType(Init->getType())) {
66 llvm::Value *V = EmitScalarExpr(Init);
67 Builder.CreateStore(V, GV, D.getType().isVolatileQualified());
68 } else if (Init->getType()->isAnyComplexType()) {
69 EmitComplexExprIntoAddr(Init, GV, D.getType().isVolatileQualified());
70 } else {
71 EmitAggExpr(Init, GV, D.getType().isVolatileQualified());
72 }
73
74 Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::Int8Ty, 1),
75 Builder.CreateBitCast(GuardV, PtrTy));
76
77 EmitBlock(EndBlock);
Anders Carlssone1b29ef2008-08-22 16:00:37 +000078}
79
Anders Carlssonb9de2c52009-05-11 23:37:08 +000080RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD,
81 llvm::Value *Callee,
82 llvm::Value *This,
83 CallExpr::const_arg_iterator ArgBeg,
84 CallExpr::const_arg_iterator ArgEnd) {
85 assert(MD->isInstance() &&
86 "Trying to emit a member call expr on a static method!");
87
88 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
89
90 CallArgList Args;
91
92 // Push the this ptr.
93 Args.push_back(std::make_pair(RValue::get(This),
94 MD->getThisType(getContext())));
95
96 // And the rest of the call args
97 EmitCallArgs(Args, FPT, ArgBeg, ArgEnd);
98
99 QualType ResultType = MD->getType()->getAsFunctionType()->getResultType();
100 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
101 Callee, Args, MD);
102}
103
Anders Carlsson774e7c62009-04-03 22:50:24 +0000104RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE) {
105 const MemberExpr *ME = cast<MemberExpr>(CE->getCallee());
106 const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000107
Anders Carlssone9918d22009-04-08 20:31:57 +0000108 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
Anders Carlsson774e7c62009-04-03 22:50:24 +0000109 const llvm::Type *Ty =
Anders Carlssone9918d22009-04-08 20:31:57 +0000110 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
111 FPT->isVariadic());
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000112 llvm::Constant *Callee = CGM.GetAddrOfFunction(GlobalDecl(MD), Ty);
Anders Carlsson774e7c62009-04-03 22:50:24 +0000113
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000114 llvm::Value *This;
Anders Carlsson774e7c62009-04-03 22:50:24 +0000115
Anders Carlsson774e7c62009-04-03 22:50:24 +0000116 if (ME->isArrow())
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000117 This = EmitScalarExpr(ME->getBase());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000118 else {
119 LValue BaseLV = EmitLValue(ME->getBase());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000120 This = BaseLV.getAddress();
Anders Carlsson774e7c62009-04-03 22:50:24 +0000121 }
122
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000123 return EmitCXXMemberCall(MD, Callee, This,
124 CE->arg_begin(), CE->arg_end());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000125}
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000126
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000127llvm::Value *CodeGenFunction::LoadCXXThis() {
128 assert(isa<CXXMethodDecl>(CurFuncDecl) &&
129 "Must be in a C++ member function decl to load 'this'");
130 assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() &&
131 "Must be in a C++ member function decl to load 'this'");
132
133 // FIXME: What if we're inside a block?
Mike Stumpf5408fe2009-05-16 07:57:57 +0000134 // ans: See how CodeGenFunction::LoadObjCSelf() uses
135 // CodeGenFunction::BlockForwardSelf() for how to do this.
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000136 return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this");
137}
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000138
Anders Carlssonb14095a2009-04-17 00:06:03 +0000139void
140CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
141 CXXCtorType Type,
142 llvm::Value *This,
143 CallExpr::const_arg_iterator ArgBeg,
144 CallExpr::const_arg_iterator ArgEnd) {
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000145 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type);
146
147 EmitCXXMemberCall(D, Callee, This, ArgBeg, ArgEnd);
Anders Carlssonb14095a2009-04-17 00:06:03 +0000148}
149
Anders Carlssonb14095a2009-04-17 00:06:03 +0000150void
Anders Carlsson31ccf372009-05-03 17:47:16 +0000151CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest,
152 const CXXConstructExpr *E) {
Anders Carlssonb14095a2009-04-17 00:06:03 +0000153 assert(Dest && "Must have a destination!");
154
155 const CXXRecordDecl *RD =
156 cast<CXXRecordDecl>(E->getType()->getAsRecordType()->getDecl());
157 if (RD->hasTrivialConstructor())
158 return;
159
160 // Call the constructor.
161 EmitCXXConstructorCall(E->getConstructor(), Ctor_Complete, Dest,
162 E->arg_begin(), E->arg_end());
163}
164
Anders Carlsson27ae5362009-04-17 01:58:57 +0000165static bool canGenerateCXXstructor(const CXXRecordDecl *RD,
166 ASTContext &Context) {
Anders Carlsson59d8e0f2009-04-15 21:02:13 +0000167 // The class has base classes - we don't support that right now.
168 if (RD->getNumBases() > 0)
169 return false;
170
171 for (CXXRecordDecl::field_iterator I = RD->field_begin(Context),
172 E = RD->field_end(Context); I != E; ++I) {
173 // We don't support ctors for fields that aren't POD.
174 if (!I->getType()->isPODType())
175 return false;
176 }
177
178 return true;
179}
180
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000181void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
Anders Carlsson27ae5362009-04-17 01:58:57 +0000182 if (!canGenerateCXXstructor(D->getParent(), getContext())) {
Anders Carlsson59d8e0f2009-04-15 21:02:13 +0000183 ErrorUnsupported(D, "C++ constructor", true);
184 return;
185 }
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000186
Anders Carlsson2a131fb2009-05-05 04:44:02 +0000187 EmitGlobal(GlobalDecl(D, Ctor_Complete));
188 EmitGlobal(GlobalDecl(D, Ctor_Base));
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000189}
Anders Carlsson363c1842009-04-16 23:57:24 +0000190
Anders Carlsson27ae5362009-04-17 01:58:57 +0000191void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
192 CXXCtorType Type) {
193
194 llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
195
196 CodeGenFunction(*this).GenerateCode(D, Fn);
197
198 SetFunctionDefinitionAttributes(D, Fn);
199 SetLLVMFunctionAttributesForDefinition(D, Fn);
200}
201
Anders Carlsson363c1842009-04-16 23:57:24 +0000202llvm::Function *
203CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
204 CXXCtorType Type) {
205 const llvm::FunctionType *FTy =
206 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
207
208 const char *Name = getMangledCXXCtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000209 return cast<llvm::Function>(
210 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson363c1842009-04-16 23:57:24 +0000211}
Anders Carlsson27ae5362009-04-17 01:58:57 +0000212
213const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
214 CXXCtorType Type) {
215 llvm::SmallString<256> Name;
216 llvm::raw_svector_ostream Out(Name);
217 mangleCXXCtor(D, Type, Context, Out);
218
219 Name += '\0';
220 return UniqueMangledName(Name.begin(), Name.end());
221}
222
223void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
224 if (!canGenerateCXXstructor(D->getParent(), getContext())) {
225 ErrorUnsupported(D, "C++ destructor", true);
226 return;
227 }
228
229 EmitCXXDestructor(D, Dtor_Complete);
230 EmitCXXDestructor(D, Dtor_Base);
231}
232
233void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
234 CXXDtorType Type) {
235 llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
236
237 CodeGenFunction(*this).GenerateCode(D, Fn);
238
239 SetFunctionDefinitionAttributes(D, Fn);
240 SetLLVMFunctionAttributesForDefinition(D, Fn);
241}
242
243llvm::Function *
244CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
245 CXXDtorType Type) {
246 const llvm::FunctionType *FTy =
247 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
248
249 const char *Name = getMangledCXXDtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000250 return cast<llvm::Function>(
251 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000252}
253
254const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
255 CXXDtorType Type) {
256 llvm::SmallString<256> Name;
257 llvm::raw_svector_ostream Out(Name);
258 mangleCXXDtor(D, Type, Context, Out);
259
260 Name += '\0';
261 return UniqueMangledName(Name.begin(), Name.end());
262}
263