Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 1 | //===--- 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 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 14 | // We might split this into multiple files if it gets too unwieldy |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 15 | |
| 16 | #include "CodeGenFunction.h" |
| 17 | #include "CodeGenModule.h" |
Anders Carlsson | 283a062 | 2009-04-13 18:03:33 +0000 | [diff] [blame] | 18 | #include "Mangle.h" |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
Fariborz Jahanian | 742cd1b | 2009-07-25 21:12:28 +0000 | [diff] [blame] | 20 | #include "clang/AST/RecordLayout.h" |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 21 | #include "clang/AST/Decl.h" |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclCXX.h" |
Anders Carlsson | 86e9644 | 2008-08-23 19:42:54 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 6815e94 | 2009-09-27 18:58:34 +0000 | [diff] [blame] | 24 | #include "clang/AST/StmtCXX.h" |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringExtras.h" |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | using namespace CodeGen; |
| 28 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 29 | void |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 30 | CodeGenFunction::EmitCXXGlobalDtorRegistration(const CXXDestructorDecl *Dtor, |
| 31 | llvm::Constant *DeclPtr) { |
Anders Carlsson | 6815e94 | 2009-09-27 18:58:34 +0000 | [diff] [blame] | 32 | const llvm::Type *Int8PtrTy = |
| 33 | llvm::Type::getInt8Ty(VMContext)->getPointerTo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 34 | |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 35 | std::vector<const llvm::Type *> Params; |
| 36 | Params.push_back(Int8PtrTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 37 | |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 38 | // Get the destructor function type |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 39 | const llvm::Type *DtorFnTy = |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 40 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Params, false); |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 41 | DtorFnTy = llvm::PointerType::getUnqual(DtorFnTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 42 | |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 43 | Params.clear(); |
| 44 | Params.push_back(DtorFnTy); |
| 45 | Params.push_back(Int8PtrTy); |
| 46 | Params.push_back(Int8PtrTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 47 | |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 48 | // Get the __cxa_atexit function type |
| 49 | // extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d ); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 50 | const llvm::FunctionType *AtExitFnTy = |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 51 | llvm::FunctionType::get(ConvertType(getContext().IntTy), Params, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 52 | |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 53 | llvm::Constant *AtExitFn = CGM.CreateRuntimeFunction(AtExitFnTy, |
| 54 | "__cxa_atexit"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 55 | |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 56 | llvm::Constant *Handle = CGM.CreateRuntimeVariable(Int8PtrTy, |
| 57 | "__dso_handle"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 58 | |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 59 | llvm::Constant *DtorFn = CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 60 | |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 61 | llvm::Value *Args[3] = { llvm::ConstantExpr::getBitCast(DtorFn, DtorFnTy), |
| 62 | llvm::ConstantExpr::getBitCast(DeclPtr, Int8PtrTy), |
| 63 | llvm::ConstantExpr::getBitCast(Handle, Int8PtrTy) }; |
| 64 | Builder.CreateCall(AtExitFn, &Args[0], llvm::array_endof(Args)); |
| 65 | } |
| 66 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 67 | void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D, |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 68 | llvm::Constant *DeclPtr) { |
| 69 | assert(D.hasGlobalStorage() && |
| 70 | "VarDecl must have global storage!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 71 | |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 72 | const Expr *Init = D.getInit(); |
| 73 | QualType T = D.getType(); |
Mike Stump | df317bf | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 74 | bool isVolatile = getContext().getCanonicalType(T).isVolatileQualified(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 75 | |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 76 | if (T->isReferenceType()) { |
Anders Carlsson | 622f9dc | 2009-08-17 18:24:57 +0000 | [diff] [blame] | 77 | ErrorUnsupported(Init, "global variable that binds to a reference"); |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 78 | } else if (!hasAggregateLLVMType(T)) { |
| 79 | llvm::Value *V = EmitScalarExpr(Init); |
Mike Stump | df317bf | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 80 | EmitStoreOfScalar(V, DeclPtr, isVolatile, T); |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 81 | } else if (T->isAnyComplexType()) { |
Mike Stump | df317bf | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 82 | EmitComplexExprIntoAddr(Init, DeclPtr, isVolatile); |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 83 | } else { |
Mike Stump | df317bf | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 84 | EmitAggExpr(Init, DeclPtr, isVolatile); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 85 | |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 86 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 87 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 88 | if (!RD->hasTrivialDestructor()) |
| 89 | EmitCXXGlobalDtorRegistration(RD->getDestructor(getContext()), DeclPtr); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
Anders Carlsson | 89ed31d | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 94 | void |
| 95 | CodeGenModule::EmitCXXGlobalInitFunc() { |
| 96 | if (CXXGlobalInits.empty()) |
| 97 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 98 | |
Mike Stump | 79d5768 | 2009-11-04 01:11:15 +0000 | [diff] [blame] | 99 | const llvm::FunctionType *FTy |
| 100 | = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
| 101 | false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | |
Anders Carlsson | 89ed31d | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 103 | // Create our global initialization function. |
| 104 | // FIXME: Should this be tweakable by targets? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 105 | llvm::Function *Fn = |
Anders Carlsson | 89ed31d | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 106 | llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage, |
| 107 | "__cxx_global_initialization", &TheModule); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 108 | |
Anders Carlsson | 89ed31d | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 109 | CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, |
Benjamin Kramer | 10c40ee | 2009-08-08 23:43:26 +0000 | [diff] [blame] | 110 | &CXXGlobalInits[0], |
Anders Carlsson | 89ed31d | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 111 | CXXGlobalInits.size()); |
| 112 | AddGlobalCtor(Fn); |
| 113 | } |
| 114 | |
| 115 | void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn, |
| 116 | const VarDecl **Decls, |
| 117 | unsigned NumDecls) { |
Anders Carlsson | 0ff8baf | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 118 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(), |
Anders Carlsson | 89ed31d | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 119 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 120 | |
Anders Carlsson | 89ed31d | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 121 | for (unsigned i = 0; i != NumDecls; ++i) { |
| 122 | const VarDecl *D = Decls[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 123 | |
Anders Carlsson | 89ed31d | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 124 | llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D); |
| 125 | EmitCXXGlobalVarDeclInit(*D, DeclPtr); |
| 126 | } |
| 127 | FinishFunction(); |
| 128 | } |
| 129 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 130 | void |
| 131 | CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D, |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 132 | llvm::GlobalVariable *GV) { |
Daniel Dunbar | 0096acf | 2009-02-25 19:24:29 +0000 | [diff] [blame] | 133 | // FIXME: This should use __cxa_guard_{acquire,release}? |
| 134 | |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 135 | assert(!getContext().getLangOptions().ThreadsafeStatics && |
| 136 | "thread safe statics are currently not supported!"); |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 137 | |
Anders Carlsson | 283a062 | 2009-04-13 18:03:33 +0000 | [diff] [blame] | 138 | llvm::SmallString<256> GuardVName; |
| 139 | llvm::raw_svector_ostream GuardVOut(GuardVName); |
Anders Carlsson | b540491 | 2009-10-07 01:06:45 +0000 | [diff] [blame] | 140 | mangleGuardVariable(CGM.getMangleContext(), &D, GuardVOut); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 141 | |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 142 | // Create the guard variable. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | llvm::GlobalValue *GuardV = |
Mike Stump | 79d5768 | 2009-11-04 01:11:15 +0000 | [diff] [blame] | 144 | new llvm::GlobalVariable(CGM.getModule(), llvm::Type::getInt64Ty(VMContext), |
| 145 | false, GV->getLinkage(), |
| 146 | llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)), |
Daniel Dunbar | 7765934 | 2009-08-19 20:04:03 +0000 | [diff] [blame] | 147 | GuardVName.str()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 148 | |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 149 | // Load the first byte of the guard variable. |
Mike Stump | 79d5768 | 2009-11-04 01:11:15 +0000 | [diff] [blame] | 150 | const llvm::Type *PtrTy |
| 151 | = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | llvm::Value *V = Builder.CreateLoad(Builder.CreateBitCast(GuardV, PtrTy), |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 153 | "tmp"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 154 | |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 155 | // Compare it against 0. |
Mike Stump | 79d5768 | 2009-11-04 01:11:15 +0000 | [diff] [blame] | 156 | llvm::Value *nullValue |
| 157 | = llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext)); |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 158 | llvm::Value *ICmp = Builder.CreateICmpEQ(V, nullValue , "tobool"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 159 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 160 | llvm::BasicBlock *InitBlock = createBasicBlock("init"); |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 161 | llvm::BasicBlock *EndBlock = createBasicBlock("init.end"); |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 162 | |
| 163 | // If the guard variable is 0, jump to the initializer code. |
| 164 | Builder.CreateCondBr(ICmp, InitBlock, EndBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 165 | |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 166 | EmitBlock(InitBlock); |
| 167 | |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 168 | EmitCXXGlobalVarDeclInit(D, GV); |
| 169 | |
Mike Stump | 79d5768 | 2009-11-04 01:11:15 +0000 | [diff] [blame] | 170 | Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), |
| 171 | 1), |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 172 | Builder.CreateBitCast(GuardV, PtrTy)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 173 | |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 174 | EmitBlock(EndBlock); |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 177 | RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD, |
| 178 | llvm::Value *Callee, |
| 179 | llvm::Value *This, |
| 180 | CallExpr::const_arg_iterator ArgBeg, |
| 181 | CallExpr::const_arg_iterator ArgEnd) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 182 | assert(MD->isInstance() && |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 183 | "Trying to emit a member call expr on a static method!"); |
| 184 | |
Douglas Gregor | 4fe95f9 | 2009-09-04 19:04:08 +0000 | [diff] [blame] | 185 | // A call to a trivial destructor requires no code generation. |
| 186 | if (const CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(MD)) |
| 187 | if (Destructor->isTrivial()) |
| 188 | return RValue::get(0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 189 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 190 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 191 | |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 192 | CallArgList Args; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 193 | |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 194 | // Push the this ptr. |
| 195 | Args.push_back(std::make_pair(RValue::get(This), |
| 196 | MD->getThisType(getContext()))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 197 | |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 198 | // And the rest of the call args |
| 199 | EmitCallArgs(Args, FPT, ArgBeg, ArgEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 200 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 201 | QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType(); |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 202 | return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args), |
| 203 | Callee, Args, MD); |
| 204 | } |
| 205 | |
Anders Carlsson | 8e7670d | 2009-10-12 19:41:04 +0000 | [diff] [blame] | 206 | /// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given |
| 207 | /// expr can be devirtualized. |
| 208 | static bool canDevirtualizeMemberFunctionCalls(const Expr *Base) { |
| 209 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) { |
| 210 | if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) { |
| 211 | // This is a record decl. We know the type and can devirtualize it. |
| 212 | return VD->getType()->isRecordType(); |
| 213 | } |
Anders Carlsson | 7636648 | 2009-10-12 19:45:47 +0000 | [diff] [blame] | 214 | |
| 215 | return false; |
Anders Carlsson | 8e7670d | 2009-10-12 19:41:04 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Anders Carlsson | 4a0d832 | 2009-10-12 19:59:15 +0000 | [diff] [blame] | 218 | // We can always devirtualize calls on temporary object expressions. |
Anders Carlsson | 7636648 | 2009-10-12 19:45:47 +0000 | [diff] [blame] | 219 | if (isa<CXXTemporaryObjectExpr>(Base)) |
| 220 | return true; |
| 221 | |
Anders Carlsson | 4a0d832 | 2009-10-12 19:59:15 +0000 | [diff] [blame] | 222 | // And calls on bound temporaries. |
| 223 | if (isa<CXXBindTemporaryExpr>(Base)) |
| 224 | return true; |
| 225 | |
Anders Carlsson | cf5deec | 2009-10-12 19:51:33 +0000 | [diff] [blame] | 226 | // Check if this is a call expr that returns a record type. |
| 227 | if (const CallExpr *CE = dyn_cast<CallExpr>(Base)) |
| 228 | return CE->getCallReturnType()->isRecordType(); |
| 229 | |
Anders Carlsson | 8e7670d | 2009-10-12 19:41:04 +0000 | [diff] [blame] | 230 | // We can't devirtualize the call. |
| 231 | return false; |
| 232 | } |
| 233 | |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 234 | RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE) { |
Anders Carlsson | 375c31c | 2009-10-03 19:43:08 +0000 | [diff] [blame] | 235 | if (isa<BinaryOperator>(CE->getCallee())) |
| 236 | return EmitCXXMemberPointerCallExpr(CE); |
| 237 | |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 238 | const MemberExpr *ME = cast<MemberExpr>(CE->getCallee()); |
| 239 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl()); |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 240 | |
Anders Carlsson | 2472bf0 | 2009-09-29 03:54:11 +0000 | [diff] [blame] | 241 | if (MD->isStatic()) { |
| 242 | // The method is static, emit it as we would a regular call. |
| 243 | llvm::Value *Callee = CGM.GetAddrOfFunction(MD); |
| 244 | return EmitCall(Callee, getContext().getPointerType(MD->getType()), |
| 245 | CE->arg_begin(), CE->arg_end(), 0); |
| 246 | |
| 247 | } |
| 248 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 249 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
Mike Stump | 7116da1 | 2009-07-30 21:47:44 +0000 | [diff] [blame] | 250 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 251 | const llvm::Type *Ty = |
| 252 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), |
Anders Carlsson | e9918d2 | 2009-04-08 20:31:57 +0000 | [diff] [blame] | 253 | FPT->isVariadic()); |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 254 | llvm::Value *This; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 255 | |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 256 | if (ME->isArrow()) |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 257 | This = EmitScalarExpr(ME->getBase()); |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 258 | else { |
| 259 | LValue BaseLV = EmitLValue(ME->getBase()); |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 260 | This = BaseLV.getAddress(); |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 261 | } |
Mike Stump | f0070db | 2009-08-26 20:46:33 +0000 | [diff] [blame] | 262 | |
Douglas Gregor | bd4c4ae | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 263 | // C++ [class.virtual]p12: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 264 | // Explicit qualification with the scope operator (5.1) suppresses the |
Douglas Gregor | bd4c4ae | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 265 | // virtual call mechanism. |
Anders Carlsson | 3b89f3f | 2009-10-11 23:55:52 +0000 | [diff] [blame] | 266 | // |
| 267 | // We also don't emit a virtual call if the base expression has a record type |
| 268 | // because then we know what the type is. |
Mike Stump | f0070db | 2009-08-26 20:46:33 +0000 | [diff] [blame] | 269 | llvm::Value *Callee; |
Anders Carlsson | 3b89f3f | 2009-10-11 23:55:52 +0000 | [diff] [blame] | 270 | if (MD->isVirtual() && !ME->hasQualifier() && |
Anders Carlsson | 8e7670d | 2009-10-12 19:41:04 +0000 | [diff] [blame] | 271 | !canDevirtualizeMemberFunctionCalls(ME->getBase())) |
Anders Carlsson | 3b89f3f | 2009-10-11 23:55:52 +0000 | [diff] [blame] | 272 | Callee = BuildVirtualCall(MD, This, Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 273 | else if (const CXXDestructorDecl *Destructor |
Douglas Gregor | 4fe95f9 | 2009-09-04 19:04:08 +0000 | [diff] [blame] | 274 | = dyn_cast<CXXDestructorDecl>(MD)) |
| 275 | Callee = CGM.GetAddrOfFunction(GlobalDecl(Destructor, Dtor_Complete), Ty); |
Douglas Gregor | 0979c80 | 2009-08-31 21:41:48 +0000 | [diff] [blame] | 276 | else |
Anders Carlsson | 555b4bb | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 277 | Callee = CGM.GetAddrOfFunction(MD, Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 278 | |
| 279 | return EmitCXXMemberCall(MD, Callee, This, |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 280 | CE->arg_begin(), CE->arg_end()); |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 281 | } |
Anders Carlsson | 5f4307b | 2009-04-14 16:58:56 +0000 | [diff] [blame] | 282 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 283 | RValue |
Anders Carlsson | 375c31c | 2009-10-03 19:43:08 +0000 | [diff] [blame] | 284 | CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E) { |
| 285 | const BinaryOperator *BO = cast<BinaryOperator>(E->getCallee()); |
Anders Carlsson | 3eea635 | 2009-10-13 17:41:28 +0000 | [diff] [blame] | 286 | const Expr *BaseExpr = BO->getLHS(); |
| 287 | const Expr *MemFnExpr = BO->getRHS(); |
Anders Carlsson | 375c31c | 2009-10-03 19:43:08 +0000 | [diff] [blame] | 288 | |
Anders Carlsson | 3eea635 | 2009-10-13 17:41:28 +0000 | [diff] [blame] | 289 | const MemberPointerType *MPT = |
| 290 | MemFnExpr->getType()->getAs<MemberPointerType>(); |
Anders Carlsson | 375c31c | 2009-10-03 19:43:08 +0000 | [diff] [blame] | 291 | const FunctionProtoType *FPT = |
| 292 | MPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 293 | const CXXRecordDecl *RD = |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 294 | cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl()); |
Anders Carlsson | 375c31c | 2009-10-03 19:43:08 +0000 | [diff] [blame] | 295 | |
| 296 | const llvm::FunctionType *FTy = |
| 297 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT), |
| 298 | FPT->isVariadic()); |
| 299 | |
| 300 | const llvm::Type *Int8PtrTy = |
| 301 | llvm::Type::getInt8Ty(VMContext)->getPointerTo(); |
| 302 | |
| 303 | // Get the member function pointer. |
| 304 | llvm::Value *MemFnPtr = |
Anders Carlsson | 3eea635 | 2009-10-13 17:41:28 +0000 | [diff] [blame] | 305 | CreateTempAlloca(ConvertType(MemFnExpr->getType()), "mem.fn"); |
| 306 | EmitAggExpr(MemFnExpr, MemFnPtr, /*VolatileDest=*/false); |
Anders Carlsson | 375c31c | 2009-10-03 19:43:08 +0000 | [diff] [blame] | 307 | |
| 308 | // Emit the 'this' pointer. |
| 309 | llvm::Value *This; |
| 310 | |
| 311 | if (BO->getOpcode() == BinaryOperator::PtrMemI) |
| 312 | This = EmitScalarExpr(BaseExpr); |
| 313 | else |
| 314 | This = EmitLValue(BaseExpr).getAddress(); |
| 315 | |
| 316 | // Adjust it. |
| 317 | llvm::Value *Adj = Builder.CreateStructGEP(MemFnPtr, 1); |
| 318 | Adj = Builder.CreateLoad(Adj, "mem.fn.adj"); |
| 319 | |
| 320 | llvm::Value *Ptr = Builder.CreateBitCast(This, Int8PtrTy, "ptr"); |
| 321 | Ptr = Builder.CreateGEP(Ptr, Adj, "adj"); |
| 322 | |
| 323 | This = Builder.CreateBitCast(Ptr, This->getType(), "this"); |
| 324 | |
| 325 | llvm::Value *FnPtr = Builder.CreateStructGEP(MemFnPtr, 0, "mem.fn.ptr"); |
| 326 | |
| 327 | const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType()); |
| 328 | |
| 329 | llvm::Value *FnAsInt = Builder.CreateLoad(FnPtr, "fn"); |
| 330 | |
| 331 | // If the LSB in the function pointer is 1, the function pointer points to |
| 332 | // a virtual function. |
| 333 | llvm::Value *IsVirtual |
| 334 | = Builder.CreateAnd(FnAsInt, llvm::ConstantInt::get(PtrDiffTy, 1), |
| 335 | "and"); |
| 336 | |
| 337 | IsVirtual = Builder.CreateTrunc(IsVirtual, |
| 338 | llvm::Type::getInt1Ty(VMContext)); |
| 339 | |
| 340 | llvm::BasicBlock *FnVirtual = createBasicBlock("fn.virtual"); |
| 341 | llvm::BasicBlock *FnNonVirtual = createBasicBlock("fn.nonvirtual"); |
| 342 | llvm::BasicBlock *FnEnd = createBasicBlock("fn.end"); |
| 343 | |
| 344 | Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual); |
| 345 | EmitBlock(FnVirtual); |
| 346 | |
| 347 | const llvm::Type *VTableTy = |
| 348 | FTy->getPointerTo()->getPointerTo()->getPointerTo(); |
| 349 | |
| 350 | llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy); |
| 351 | VTable = Builder.CreateLoad(VTable); |
| 352 | |
| 353 | VTable = Builder.CreateGEP(VTable, FnAsInt, "fn"); |
| 354 | |
| 355 | // Since the function pointer is 1 plus the virtual table offset, we |
| 356 | // subtract 1 by using a GEP. |
Mike Stump | 25bc275 | 2009-10-09 01:25:47 +0000 | [diff] [blame] | 357 | VTable = Builder.CreateConstGEP1_64(VTable, (uint64_t)-1); |
Anders Carlsson | 375c31c | 2009-10-03 19:43:08 +0000 | [diff] [blame] | 358 | |
| 359 | llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "virtualfn"); |
| 360 | |
| 361 | EmitBranch(FnEnd); |
| 362 | EmitBlock(FnNonVirtual); |
| 363 | |
| 364 | // If the function is not virtual, just load the pointer. |
| 365 | llvm::Value *NonVirtualFn = Builder.CreateLoad(FnPtr, "fn"); |
| 366 | NonVirtualFn = Builder.CreateIntToPtr(NonVirtualFn, FTy->getPointerTo()); |
| 367 | |
| 368 | EmitBlock(FnEnd); |
| 369 | |
| 370 | llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo()); |
| 371 | Callee->reserveOperandSpace(2); |
| 372 | Callee->addIncoming(VirtualFn, FnVirtual); |
| 373 | Callee->addIncoming(NonVirtualFn, FnNonVirtual); |
| 374 | |
| 375 | CallArgList Args; |
| 376 | |
| 377 | QualType ThisType = |
| 378 | getContext().getPointerType(getContext().getTagDeclType(RD)); |
| 379 | |
| 380 | // Push the this ptr. |
| 381 | Args.push_back(std::make_pair(RValue::get(This), ThisType)); |
| 382 | |
| 383 | // And the rest of the call args |
| 384 | EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end()); |
| 385 | QualType ResultType = BO->getType()->getAs<FunctionType>()->getResultType(); |
| 386 | return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args), |
| 387 | Callee, Args, 0); |
| 388 | } |
| 389 | |
| 390 | RValue |
Anders Carlsson | 0f29463 | 2009-05-27 04:18:27 +0000 | [diff] [blame] | 391 | CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E, |
| 392 | const CXXMethodDecl *MD) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 393 | assert(MD->isInstance() && |
Anders Carlsson | 0f29463 | 2009-05-27 04:18:27 +0000 | [diff] [blame] | 394 | "Trying to emit a member call expr on a static method!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 395 | |
Fariborz Jahanian | ad25883 | 2009-08-13 21:09:41 +0000 | [diff] [blame] | 396 | if (MD->isCopyAssignment()) { |
| 397 | const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext()); |
| 398 | if (ClassDecl->hasTrivialCopyAssignment()) { |
| 399 | assert(!ClassDecl->hasUserDeclaredCopyAssignment() && |
| 400 | "EmitCXXOperatorMemberCallExpr - user declared copy assignment"); |
| 401 | llvm::Value *This = EmitLValue(E->getArg(0)).getAddress(); |
| 402 | llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress(); |
| 403 | QualType Ty = E->getType(); |
| 404 | EmitAggregateCopy(This, Src, Ty); |
| 405 | return RValue::get(This); |
| 406 | } |
| 407 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 408 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 409 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 410 | const llvm::Type *Ty = |
| 411 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), |
Mike Stump | ed032eb | 2009-09-04 18:27:16 +0000 | [diff] [blame] | 412 | FPT->isVariadic()); |
Anders Carlsson | 555b4bb | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 413 | llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 414 | |
Anders Carlsson | 0f29463 | 2009-05-27 04:18:27 +0000 | [diff] [blame] | 415 | llvm::Value *This = EmitLValue(E->getArg(0)).getAddress(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 416 | |
Anders Carlsson | 0f29463 | 2009-05-27 04:18:27 +0000 | [diff] [blame] | 417 | return EmitCXXMemberCall(MD, Callee, This, |
| 418 | E->arg_begin() + 1, E->arg_end()); |
| 419 | } |
| 420 | |
Anders Carlsson | 5f4307b | 2009-04-14 16:58:56 +0000 | [diff] [blame] | 421 | llvm::Value *CodeGenFunction::LoadCXXThis() { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 422 | assert(isa<CXXMethodDecl>(CurFuncDecl) && |
Anders Carlsson | 5f4307b | 2009-04-14 16:58:56 +0000 | [diff] [blame] | 423 | "Must be in a C++ member function decl to load 'this'"); |
| 424 | assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() && |
| 425 | "Must be in a C++ member function decl to load 'this'"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 426 | |
Anders Carlsson | 5f4307b | 2009-04-14 16:58:56 +0000 | [diff] [blame] | 427 | // FIXME: What if we're inside a block? |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 428 | // ans: See how CodeGenFunction::LoadObjCSelf() uses |
| 429 | // CodeGenFunction::BlockForwardSelf() for how to do this. |
Anders Carlsson | 5f4307b | 2009-04-14 16:58:56 +0000 | [diff] [blame] | 430 | return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this"); |
| 431 | } |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 432 | |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 433 | /// EmitCXXAggrConstructorCall - This routine essentially creates a (nested) |
| 434 | /// for-loop to call the default constructor on individual members of the |
Anders Carlsson | 569c1f4 | 2009-09-23 02:45:36 +0000 | [diff] [blame] | 435 | /// array. |
| 436 | /// 'D' is the default constructor for elements of the array, 'ArrayTy' is the |
| 437 | /// array type and 'ArrayPtr' points to the beginning fo the array. |
| 438 | /// It is assumed that all relevant checks have been made by the caller. |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 439 | void |
| 440 | CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, |
Anders Carlsson | 569c1f4 | 2009-09-23 02:45:36 +0000 | [diff] [blame] | 441 | const ConstantArrayType *ArrayTy, |
| 442 | llvm::Value *ArrayPtr) { |
| 443 | const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
| 444 | llvm::Value * NumElements = |
| 445 | llvm::ConstantInt::get(SizeTy, |
| 446 | getContext().getConstantArrayElementCount(ArrayTy)); |
| 447 | |
| 448 | EmitCXXAggrConstructorCall(D, NumElements, ArrayPtr); |
| 449 | } |
| 450 | |
| 451 | void |
| 452 | CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, |
| 453 | llvm::Value *NumElements, |
| 454 | llvm::Value *ArrayPtr) { |
| 455 | const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 456 | |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 457 | // Create a temporary for the loop index and initialize it with 0. |
Anders Carlsson | 569c1f4 | 2009-09-23 02:45:36 +0000 | [diff] [blame] | 458 | llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index"); |
| 459 | llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy); |
| 460 | Builder.CreateStore(Zero, IndexPtr, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 461 | |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 462 | // Start the loop with a block that tests the condition. |
| 463 | llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); |
| 464 | llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 465 | |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 466 | EmitBlock(CondBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 467 | |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 468 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 469 | |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 470 | // Generate: if (loop-index < number-of-elements fall to the loop body, |
| 471 | // otherwise, go to the block after the for-loop. |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 472 | llvm::Value *Counter = Builder.CreateLoad(IndexPtr); |
Anders Carlsson | 569c1f4 | 2009-09-23 02:45:36 +0000 | [diff] [blame] | 473 | llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless"); |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 474 | // If the condition is true, execute the body. |
| 475 | Builder.CreateCondBr(IsLess, ForBody, AfterFor); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 476 | |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 477 | EmitBlock(ForBody); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 478 | |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 479 | llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 480 | // Inside the loop body, emit the constructor call on the array element. |
Fariborz Jahanian | 995d281 | 2009-08-20 01:01:06 +0000 | [diff] [blame] | 481 | Counter = Builder.CreateLoad(IndexPtr); |
Anders Carlsson | 569c1f4 | 2009-09-23 02:45:36 +0000 | [diff] [blame] | 482 | llvm::Value *Address = Builder.CreateInBoundsGEP(ArrayPtr, Counter, |
| 483 | "arrayidx"); |
Fariborz Jahanian | 4f68d53 | 2009-08-26 00:23:27 +0000 | [diff] [blame] | 484 | EmitCXXConstructorCall(D, Ctor_Complete, Address, 0, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 485 | |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 486 | EmitBlock(ContinueBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 487 | |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 488 | // Emit the increment of the loop counter. |
Anders Carlsson | 569c1f4 | 2009-09-23 02:45:36 +0000 | [diff] [blame] | 489 | llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1); |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 490 | Counter = Builder.CreateLoad(IndexPtr); |
| 491 | NextVal = Builder.CreateAdd(Counter, NextVal, "inc"); |
| 492 | Builder.CreateStore(NextVal, IndexPtr, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 493 | |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 494 | // Finally, branch back up to the condition for the next iteration. |
| 495 | EmitBranch(CondBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 496 | |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 497 | // Emit the fall-through block. |
| 498 | EmitBlock(AfterFor, true); |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Fariborz Jahanian | 1c536bf | 2009-08-20 23:02:58 +0000 | [diff] [blame] | 501 | /// EmitCXXAggrDestructorCall - calls the default destructor on array |
| 502 | /// elements in reverse order of construction. |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 503 | void |
Fariborz Jahanian | f800f6c | 2009-08-20 20:54:15 +0000 | [diff] [blame] | 504 | CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D, |
| 505 | const ArrayType *Array, |
| 506 | llvm::Value *This) { |
Fariborz Jahanian | 1c536bf | 2009-08-20 23:02:58 +0000 | [diff] [blame] | 507 | const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array); |
| 508 | assert(CA && "Do we support VLA for destruction ?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 509 | llvm::Value *One = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), |
Fariborz Jahanian | 1c536bf | 2009-08-20 23:02:58 +0000 | [diff] [blame] | 510 | 1); |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 511 | uint64_t ElementCount = getContext().getConstantArrayElementCount(CA); |
Fariborz Jahanian | 1c536bf | 2009-08-20 23:02:58 +0000 | [diff] [blame] | 512 | // Create a temporary for the loop index and initialize it with count of |
| 513 | // array elements. |
| 514 | llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext), |
| 515 | "loop.index"); |
| 516 | // Index = ElementCount; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 517 | llvm::Value* UpperCount = |
Fariborz Jahanian | 1c536bf | 2009-08-20 23:02:58 +0000 | [diff] [blame] | 518 | llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), ElementCount); |
| 519 | Builder.CreateStore(UpperCount, IndexPtr, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 520 | |
Fariborz Jahanian | 1c536bf | 2009-08-20 23:02:58 +0000 | [diff] [blame] | 521 | // Start the loop with a block that tests the condition. |
| 522 | llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); |
| 523 | llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 524 | |
Fariborz Jahanian | 1c536bf | 2009-08-20 23:02:58 +0000 | [diff] [blame] | 525 | EmitBlock(CondBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 526 | |
Fariborz Jahanian | 1c536bf | 2009-08-20 23:02:58 +0000 | [diff] [blame] | 527 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 528 | |
Fariborz Jahanian | 1c536bf | 2009-08-20 23:02:58 +0000 | [diff] [blame] | 529 | // Generate: if (loop-index != 0 fall to the loop body, |
| 530 | // otherwise, go to the block after the for-loop. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 531 | llvm::Value* zeroConstant = |
Fariborz Jahanian | 1c536bf | 2009-08-20 23:02:58 +0000 | [diff] [blame] | 532 | llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)); |
| 533 | llvm::Value *Counter = Builder.CreateLoad(IndexPtr); |
| 534 | llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant, |
| 535 | "isne"); |
| 536 | // If the condition is true, execute the body. |
| 537 | Builder.CreateCondBr(IsNE, ForBody, AfterFor); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 538 | |
Fariborz Jahanian | 1c536bf | 2009-08-20 23:02:58 +0000 | [diff] [blame] | 539 | EmitBlock(ForBody); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 540 | |
Fariborz Jahanian | 1c536bf | 2009-08-20 23:02:58 +0000 | [diff] [blame] | 541 | llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); |
| 542 | // Inside the loop body, emit the constructor call on the array element. |
| 543 | Counter = Builder.CreateLoad(IndexPtr); |
| 544 | Counter = Builder.CreateSub(Counter, One); |
| 545 | llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx"); |
| 546 | EmitCXXDestructorCall(D, Dtor_Complete, Address); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 547 | |
Fariborz Jahanian | 1c536bf | 2009-08-20 23:02:58 +0000 | [diff] [blame] | 548 | EmitBlock(ContinueBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 549 | |
Fariborz Jahanian | 1c536bf | 2009-08-20 23:02:58 +0000 | [diff] [blame] | 550 | // Emit the decrement of the loop counter. |
| 551 | Counter = Builder.CreateLoad(IndexPtr); |
| 552 | Counter = Builder.CreateSub(Counter, One, "dec"); |
| 553 | Builder.CreateStore(Counter, IndexPtr, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 554 | |
Fariborz Jahanian | 1c536bf | 2009-08-20 23:02:58 +0000 | [diff] [blame] | 555 | // Finally, branch back up to the condition for the next iteration. |
| 556 | EmitBranch(CondBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 557 | |
Fariborz Jahanian | 1c536bf | 2009-08-20 23:02:58 +0000 | [diff] [blame] | 558 | // Emit the fall-through block. |
| 559 | EmitBlock(AfterFor, true); |
Fariborz Jahanian | f800f6c | 2009-08-20 20:54:15 +0000 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 563 | CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D, |
| 564 | CXXCtorType Type, |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 565 | llvm::Value *This, |
| 566 | CallExpr::const_arg_iterator ArgBeg, |
| 567 | CallExpr::const_arg_iterator ArgEnd) { |
Fariborz Jahanian | 343a3cf | 2009-08-14 20:11:43 +0000 | [diff] [blame] | 568 | if (D->isCopyConstructor(getContext())) { |
| 569 | const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D->getDeclContext()); |
| 570 | if (ClassDecl->hasTrivialCopyConstructor()) { |
| 571 | assert(!ClassDecl->hasUserDeclaredCopyConstructor() && |
| 572 | "EmitCXXConstructorCall - user declared copy constructor"); |
| 573 | const Expr *E = (*ArgBeg); |
| 574 | QualType Ty = E->getType(); |
| 575 | llvm::Value *Src = EmitLValue(E).getAddress(); |
| 576 | EmitAggregateCopy(This, Src, Ty); |
| 577 | return; |
| 578 | } |
| 579 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 580 | |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 581 | llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type); |
| 582 | |
| 583 | EmitCXXMemberCall(D, Callee, This, ArgBeg, ArgEnd); |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 586 | void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *D, |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 587 | CXXDtorType Type, |
| 588 | llvm::Value *This) { |
| 589 | llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(D, Type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 590 | |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 591 | EmitCXXMemberCall(D, Callee, This, 0, 0); |
| 592 | } |
| 593 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 594 | void |
| 595 | CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest, |
Anders Carlsson | 31ccf37 | 2009-05-03 17:47:16 +0000 | [diff] [blame] | 596 | const CXXConstructExpr *E) { |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 597 | assert(Dest && "Must have a destination!"); |
Fariborz Jahanian | 93034ca | 2009-10-16 19:20:59 +0000 | [diff] [blame] | 598 | const CXXConstructorDecl *CD = E->getConstructor(); |
Fariborz Jahanian | d7a4a43 | 2009-10-28 21:07:28 +0000 | [diff] [blame] | 599 | const ConstantArrayType *Array = |
| 600 | getContext().getAsConstantArrayType(E->getType()); |
Fariborz Jahanian | 93034ca | 2009-10-16 19:20:59 +0000 | [diff] [blame] | 601 | // For a copy constructor, even if it is trivial, must fall thru so |
| 602 | // its argument is code-gen'ed. |
| 603 | if (!CD->isCopyConstructor(getContext())) { |
Fariborz Jahanian | ae013b9 | 2009-10-28 20:55:41 +0000 | [diff] [blame] | 604 | QualType InitType = E->getType(); |
Fariborz Jahanian | d7a4a43 | 2009-10-28 21:07:28 +0000 | [diff] [blame] | 605 | if (Array) |
Fariborz Jahanian | ae013b9 | 2009-10-28 20:55:41 +0000 | [diff] [blame] | 606 | InitType = getContext().getBaseElementType(Array); |
Fariborz Jahanian | 93034ca | 2009-10-16 19:20:59 +0000 | [diff] [blame] | 607 | const CXXRecordDecl *RD = |
Fariborz Jahanian | ae013b9 | 2009-10-28 20:55:41 +0000 | [diff] [blame] | 608 | cast<CXXRecordDecl>(InitType->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | 93034ca | 2009-10-16 19:20:59 +0000 | [diff] [blame] | 609 | if (RD->hasTrivialConstructor()) |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 610 | return; |
Fariborz Jahanian | 93034ca | 2009-10-16 19:20:59 +0000 | [diff] [blame] | 611 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 612 | // Code gen optimization to eliminate copy constructor and return |
Fariborz Jahanian | 6904cbb | 2009-08-06 01:02:49 +0000 | [diff] [blame] | 613 | // its first argument instead. |
Anders Carlsson | 92f5822 | 2009-08-22 22:30:33 +0000 | [diff] [blame] | 614 | if (getContext().getLangOptions().ElideConstructors && E->isElidable()) { |
Fariborz Jahanian | 6904cbb | 2009-08-06 01:02:49 +0000 | [diff] [blame] | 615 | CXXConstructExpr::const_arg_iterator i = E->arg_begin(); |
Fariborz Jahanian | 1cf9ff8 | 2009-08-06 19:12:38 +0000 | [diff] [blame] | 616 | EmitAggExpr((*i), Dest, false); |
| 617 | return; |
Fariborz Jahanian | 6904cbb | 2009-08-06 01:02:49 +0000 | [diff] [blame] | 618 | } |
Fariborz Jahanian | d7a4a43 | 2009-10-28 21:07:28 +0000 | [diff] [blame] | 619 | if (Array) { |
Fariborz Jahanian | ae013b9 | 2009-10-28 20:55:41 +0000 | [diff] [blame] | 620 | QualType BaseElementTy = getContext().getBaseElementType(Array); |
| 621 | const llvm::Type *BasePtr = ConvertType(BaseElementTy); |
| 622 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
| 623 | llvm::Value *BaseAddrPtr = |
| 624 | Builder.CreateBitCast(Dest, BasePtr); |
| 625 | EmitCXXAggrConstructorCall(CD, Array, BaseAddrPtr); |
| 626 | } |
| 627 | else |
| 628 | // Call the constructor. |
| 629 | EmitCXXConstructorCall(CD, Ctor_Complete, Dest, |
| 630 | E->arg_begin(), E->arg_end()); |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 631 | } |
| 632 | |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 633 | void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) { |
Anders Carlsson | 2a131fb | 2009-05-05 04:44:02 +0000 | [diff] [blame] | 634 | EmitGlobal(GlobalDecl(D, Ctor_Complete)); |
| 635 | EmitGlobal(GlobalDecl(D, Ctor_Base)); |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 636 | } |
Anders Carlsson | 363c184 | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 637 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 638 | void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D, |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 639 | CXXCtorType Type) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 640 | |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 641 | llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 642 | |
Anders Carlsson | 0ff8baf | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 643 | CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 644 | |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 645 | SetFunctionDefinitionAttributes(D, Fn); |
| 646 | SetLLVMFunctionAttributesForDefinition(D, Fn); |
| 647 | } |
| 648 | |
Anders Carlsson | 363c184 | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 649 | llvm::Function * |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 650 | CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D, |
Anders Carlsson | 363c184 | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 651 | CXXCtorType Type) { |
Fariborz Jahanian | 30509a3 | 2009-11-06 18:47:57 +0000 | [diff] [blame^] | 652 | const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>(); |
Anders Carlsson | 363c184 | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 653 | const llvm::FunctionType *FTy = |
Fariborz Jahanian | 30509a3 | 2009-11-06 18:47:57 +0000 | [diff] [blame^] | 654 | getTypes().GetFunctionType(getTypes().getFunctionInfo(D), |
| 655 | FPT->isVariadic()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 656 | |
Anders Carlsson | 363c184 | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 657 | const char *Name = getMangledCXXCtorName(D, Type); |
Chris Lattner | b4880ba | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 658 | return cast<llvm::Function>( |
| 659 | GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type))); |
Anders Carlsson | 363c184 | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 660 | } |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 661 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 662 | const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D, |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 663 | CXXCtorType Type) { |
| 664 | llvm::SmallString<256> Name; |
| 665 | llvm::raw_svector_ostream Out(Name); |
Anders Carlsson | b540491 | 2009-10-07 01:06:45 +0000 | [diff] [blame] | 666 | mangleCXXCtor(getMangleContext(), D, Type, Out); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 667 | |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 668 | Name += '\0'; |
| 669 | return UniqueMangledName(Name.begin(), Name.end()); |
| 670 | } |
| 671 | |
| 672 | void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) { |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 673 | EmitCXXDestructor(D, Dtor_Complete); |
| 674 | EmitCXXDestructor(D, Dtor_Base); |
| 675 | } |
| 676 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 677 | void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D, |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 678 | CXXDtorType Type) { |
| 679 | llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 680 | |
Anders Carlsson | 0ff8baf | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 681 | CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 682 | |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 683 | SetFunctionDefinitionAttributes(D, Fn); |
| 684 | SetLLVMFunctionAttributesForDefinition(D, Fn); |
| 685 | } |
| 686 | |
| 687 | llvm::Function * |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 688 | CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D, |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 689 | CXXDtorType Type) { |
| 690 | const llvm::FunctionType *FTy = |
| 691 | getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 692 | |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 693 | const char *Name = getMangledCXXDtorName(D, Type); |
Chris Lattner | b4880ba | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 694 | return cast<llvm::Function>( |
| 695 | GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type))); |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 698 | const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D, |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 699 | CXXDtorType Type) { |
| 700 | llvm::SmallString<256> Name; |
| 701 | llvm::raw_svector_ostream Out(Name); |
Anders Carlsson | b540491 | 2009-10-07 01:06:45 +0000 | [diff] [blame] | 702 | mangleCXXDtor(getMangleContext(), D, Type, Out); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 703 | |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 704 | Name += '\0'; |
| 705 | return UniqueMangledName(Name.begin(), Name.end()); |
| 706 | } |
Fariborz Jahanian | e7d346b | 2009-07-20 23:18:55 +0000 | [diff] [blame] | 707 | |
Mike Stump | ed032eb | 2009-09-04 18:27:16 +0000 | [diff] [blame] | 708 | llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn, |
| 709 | const CXXMethodDecl *MD, |
Mike Stump | 77ca8f6 | 2009-09-05 07:20:32 +0000 | [diff] [blame] | 710 | bool Extern, int64_t nv, |
| 711 | int64_t v) { |
Mike Stump | c5dac4e | 2009-11-02 23:22:01 +0000 | [diff] [blame] | 712 | return GenerateCovariantThunk(Fn, MD, Extern, nv, v, 0, 0); |
Mike Stump | ed032eb | 2009-09-04 18:27:16 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Mike Stump | c902d22 | 2009-11-03 16:59:27 +0000 | [diff] [blame] | 715 | llvm::Value *CodeGenFunction::DynamicTypeAdjust(llvm::Value *V, int64_t nv, |
| 716 | int64_t v) { |
| 717 | llvm::Type *Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), |
| 718 | 0); |
| 719 | const llvm::Type *OrigTy = V->getType(); |
| 720 | if (nv) { |
| 721 | // Do the non-virtual adjustment |
| 722 | V = Builder.CreateBitCast(V, Ptr8Ty); |
| 723 | V = Builder.CreateConstInBoundsGEP1_64(V, nv); |
| 724 | V = Builder.CreateBitCast(V, OrigTy); |
| 725 | } |
| 726 | if (v) { |
| 727 | // Do the virtual this adjustment |
| 728 | const llvm::Type *PtrDiffTy = |
| 729 | ConvertType(getContext().getPointerDiffType()); |
| 730 | llvm::Type *PtrPtr8Ty, *PtrPtrDiffTy; |
| 731 | PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0); |
| 732 | PtrPtrDiffTy = llvm::PointerType::get(PtrDiffTy, 0); |
| 733 | llvm::Value *ThisVal = Builder.CreateBitCast(V, Ptr8Ty); |
| 734 | V = Builder.CreateBitCast(V, PtrPtrDiffTy->getPointerTo()); |
| 735 | V = Builder.CreateLoad(V, "vtable"); |
| 736 | llvm::Value *VTablePtr = V; |
| 737 | assert(v % (LLVMPointerWidth/8) == 0 && "vtable entry unaligned"); |
| 738 | v /= LLVMPointerWidth/8; |
| 739 | V = Builder.CreateConstInBoundsGEP1_64(VTablePtr, v); |
| 740 | V = Builder.CreateLoad(V); |
| 741 | V = Builder.CreateGEP(ThisVal, V); |
| 742 | V = Builder.CreateBitCast(V, OrigTy); |
| 743 | } |
| 744 | return V; |
| 745 | } |
| 746 | |
Mike Stump | 6e319f6 | 2009-09-11 23:25:56 +0000 | [diff] [blame] | 747 | llvm::Constant *CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn, |
| 748 | const CXXMethodDecl *MD, |
| 749 | bool Extern, |
| 750 | int64_t nv_t, |
| 751 | int64_t v_t, |
| 752 | int64_t nv_r, |
| 753 | int64_t v_r) { |
Mike Stump | c5dac4e | 2009-11-02 23:22:01 +0000 | [diff] [blame] | 754 | QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType(); |
Mike Stump | 6e319f6 | 2009-09-11 23:25:56 +0000 | [diff] [blame] | 755 | |
| 756 | FunctionArgList Args; |
| 757 | ImplicitParamDecl *ThisDecl = |
| 758 | ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, |
| 759 | MD->getThisType(getContext())); |
| 760 | Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType())); |
| 761 | for (FunctionDecl::param_const_iterator i = MD->param_begin(), |
| 762 | e = MD->param_end(); |
| 763 | i != e; ++i) { |
| 764 | ParmVarDecl *D = *i; |
| 765 | Args.push_back(std::make_pair(D, D->getType())); |
| 766 | } |
| 767 | IdentifierInfo *II |
| 768 | = &CGM.getContext().Idents.get("__thunk_named_foo_"); |
| 769 | FunctionDecl *FD = FunctionDecl::Create(getContext(), |
| 770 | getContext().getTranslationUnitDecl(), |
Mike Stump | c5dac4e | 2009-11-02 23:22:01 +0000 | [diff] [blame] | 771 | SourceLocation(), II, ResultType, 0, |
Mike Stump | 6e319f6 | 2009-09-11 23:25:56 +0000 | [diff] [blame] | 772 | Extern |
| 773 | ? FunctionDecl::Extern |
| 774 | : FunctionDecl::Static, |
| 775 | false, true); |
Mike Stump | c5dac4e | 2009-11-02 23:22:01 +0000 | [diff] [blame] | 776 | StartFunction(FD, ResultType, Fn, Args, SourceLocation()); |
| 777 | |
| 778 | // generate body |
Mike Stump | 736529e | 2009-11-03 02:12:59 +0000 | [diff] [blame] | 779 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
| 780 | const llvm::Type *Ty = |
| 781 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), |
| 782 | FPT->isVariadic()); |
| 783 | llvm::Value *Callee = CGM.GetAddrOfFunction(MD, Ty); |
Mike Stump | c5dac4e | 2009-11-02 23:22:01 +0000 | [diff] [blame] | 784 | CallArgList CallArgs; |
| 785 | |
Mike Stump | 736529e | 2009-11-03 02:12:59 +0000 | [diff] [blame] | 786 | QualType ArgType = MD->getThisType(getContext()); |
| 787 | llvm::Value *Arg = Builder.CreateLoad(LocalDeclMap[ThisDecl], "this"); |
Mike Stump | d0fe536 | 2009-11-04 00:53:51 +0000 | [diff] [blame] | 788 | if (nv_t || v_t) { |
Mike Stump | c902d22 | 2009-11-03 16:59:27 +0000 | [diff] [blame] | 789 | // Do the this adjustment. |
Mike Stump | d0fe536 | 2009-11-04 00:53:51 +0000 | [diff] [blame] | 790 | const llvm::Type *OrigTy = Callee->getType(); |
Mike Stump | c902d22 | 2009-11-03 16:59:27 +0000 | [diff] [blame] | 791 | Arg = DynamicTypeAdjust(Arg, nv_t, v_t); |
Mike Stump | d0fe536 | 2009-11-04 00:53:51 +0000 | [diff] [blame] | 792 | if (nv_r || v_r) { |
| 793 | Callee = CGM.BuildCovariantThunk(MD, Extern, 0, 0, nv_r, v_r); |
| 794 | Callee = Builder.CreateBitCast(Callee, OrigTy); |
| 795 | nv_r = v_r = 0; |
| 796 | } |
| 797 | } |
| 798 | |
Mike Stump | 736529e | 2009-11-03 02:12:59 +0000 | [diff] [blame] | 799 | CallArgs.push_back(std::make_pair(RValue::get(Arg), ArgType)); |
| 800 | |
Mike Stump | c5dac4e | 2009-11-02 23:22:01 +0000 | [diff] [blame] | 801 | for (FunctionDecl::param_const_iterator i = MD->param_begin(), |
| 802 | e = MD->param_end(); |
| 803 | i != e; ++i) { |
| 804 | ParmVarDecl *D = *i; |
| 805 | QualType ArgType = D->getType(); |
| 806 | |
| 807 | // llvm::Value *Arg = CGF.GetAddrOfLocalVar(Dst); |
| 808 | Expr *Arg = new (getContext()) DeclRefExpr(D, ArgType, SourceLocation()); |
Mike Stump | c5dac4e | 2009-11-02 23:22:01 +0000 | [diff] [blame] | 809 | CallArgs.push_back(std::make_pair(EmitCallArg(Arg, ArgType), ArgType)); |
| 810 | } |
| 811 | |
Mike Stump | f49ed94 | 2009-11-02 23:47:45 +0000 | [diff] [blame] | 812 | RValue RV = EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs), |
| 813 | Callee, CallArgs, MD); |
Mike Stump | c5dac4e | 2009-11-02 23:22:01 +0000 | [diff] [blame] | 814 | if (nv_r || v_r) { |
Mike Stump | 03e777e | 2009-11-05 06:32:02 +0000 | [diff] [blame] | 815 | bool CanBeZero = !(ResultType->isReferenceType() |
| 816 | // FIXME: attr nonnull can't be zero either |
| 817 | /* || ResultType->hasAttr<NonNullAttr>() */ ); |
Mike Stump | c902d22 | 2009-11-03 16:59:27 +0000 | [diff] [blame] | 818 | // Do the return result adjustment. |
Mike Stump | 03e777e | 2009-11-05 06:32:02 +0000 | [diff] [blame] | 819 | if (CanBeZero) { |
| 820 | llvm::BasicBlock *NonZeroBlock = createBasicBlock(); |
| 821 | llvm::BasicBlock *ZeroBlock = createBasicBlock(); |
| 822 | llvm::BasicBlock *ContBlock = createBasicBlock(); |
Mike Stump | 7c276b8 | 2009-11-05 06:12:26 +0000 | [diff] [blame] | 823 | |
Mike Stump | 03e777e | 2009-11-05 06:32:02 +0000 | [diff] [blame] | 824 | const llvm::Type *Ty = RV.getScalarVal()->getType(); |
| 825 | llvm::Value *Zero = llvm::Constant::getNullValue(Ty); |
| 826 | Builder.CreateCondBr(Builder.CreateICmpNE(RV.getScalarVal(), Zero), |
| 827 | NonZeroBlock, ZeroBlock); |
| 828 | EmitBlock(NonZeroBlock); |
| 829 | llvm::Value *NZ = DynamicTypeAdjust(RV.getScalarVal(), nv_r, v_r); |
| 830 | EmitBranch(ContBlock); |
| 831 | EmitBlock(ZeroBlock); |
| 832 | llvm::Value *Z = RV.getScalarVal(); |
| 833 | EmitBlock(ContBlock); |
| 834 | llvm::PHINode *RVOrZero = Builder.CreatePHI(Ty); |
| 835 | RVOrZero->reserveOperandSpace(2); |
| 836 | RVOrZero->addIncoming(NZ, NonZeroBlock); |
| 837 | RVOrZero->addIncoming(Z, ZeroBlock); |
| 838 | RV = RValue::get(RVOrZero); |
| 839 | } else |
| 840 | RV = RValue::get(DynamicTypeAdjust(RV.getScalarVal(), nv_r, v_r)); |
Mike Stump | c5dac4e | 2009-11-02 23:22:01 +0000 | [diff] [blame] | 841 | } |
| 842 | |
Mike Stump | f49ed94 | 2009-11-02 23:47:45 +0000 | [diff] [blame] | 843 | if (!ResultType->isVoidType()) |
| 844 | EmitReturnOfRValue(RV, ResultType); |
| 845 | |
Mike Stump | 6e319f6 | 2009-09-11 23:25:56 +0000 | [diff] [blame] | 846 | FinishFunction(); |
| 847 | return Fn; |
| 848 | } |
| 849 | |
Mike Stump | 77ca8f6 | 2009-09-05 07:20:32 +0000 | [diff] [blame] | 850 | llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern, |
| 851 | int64_t nv, int64_t v) { |
Mike Stump | ed032eb | 2009-09-04 18:27:16 +0000 | [diff] [blame] | 852 | llvm::SmallString<256> OutName; |
| 853 | llvm::raw_svector_ostream Out(OutName); |
Anders Carlsson | b540491 | 2009-10-07 01:06:45 +0000 | [diff] [blame] | 854 | mangleThunk(getMangleContext(), MD, nv, v, Out); |
Mike Stump | ed032eb | 2009-09-04 18:27:16 +0000 | [diff] [blame] | 855 | llvm::GlobalVariable::LinkageTypes linktype; |
| 856 | linktype = llvm::GlobalValue::WeakAnyLinkage; |
| 857 | if (!Extern) |
| 858 | linktype = llvm::GlobalValue::InternalLinkage; |
| 859 | llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 860 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
Mike Stump | ed032eb | 2009-09-04 18:27:16 +0000 | [diff] [blame] | 861 | const llvm::FunctionType *FTy = |
| 862 | getTypes().GetFunctionType(getTypes().getFunctionInfo(MD), |
| 863 | FPT->isVariadic()); |
| 864 | |
| 865 | llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(), |
| 866 | &getModule()); |
Mike Stump | 77ca8f6 | 2009-09-05 07:20:32 +0000 | [diff] [blame] | 867 | CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v); |
Mike Stump | ed032eb | 2009-09-04 18:27:16 +0000 | [diff] [blame] | 868 | llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty); |
| 869 | return m; |
| 870 | } |
| 871 | |
Mike Stump | 6e319f6 | 2009-09-11 23:25:56 +0000 | [diff] [blame] | 872 | llvm::Constant *CodeGenModule::BuildCovariantThunk(const CXXMethodDecl *MD, |
| 873 | bool Extern, int64_t nv_t, |
| 874 | int64_t v_t, int64_t nv_r, |
| 875 | int64_t v_r) { |
| 876 | llvm::SmallString<256> OutName; |
| 877 | llvm::raw_svector_ostream Out(OutName); |
Anders Carlsson | b540491 | 2009-10-07 01:06:45 +0000 | [diff] [blame] | 878 | mangleCovariantThunk(getMangleContext(), MD, nv_t, v_t, nv_r, v_r, Out); |
Mike Stump | 6e319f6 | 2009-09-11 23:25:56 +0000 | [diff] [blame] | 879 | llvm::GlobalVariable::LinkageTypes linktype; |
| 880 | linktype = llvm::GlobalValue::WeakAnyLinkage; |
| 881 | if (!Extern) |
| 882 | linktype = llvm::GlobalValue::InternalLinkage; |
| 883 | llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 884 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
Mike Stump | 6e319f6 | 2009-09-11 23:25:56 +0000 | [diff] [blame] | 885 | const llvm::FunctionType *FTy = |
| 886 | getTypes().GetFunctionType(getTypes().getFunctionInfo(MD), |
| 887 | FPT->isVariadic()); |
| 888 | |
| 889 | llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(), |
| 890 | &getModule()); |
| 891 | CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, nv_t, v_t, nv_r, |
| 892 | v_r); |
Mike Stump | 6e319f6 | 2009-09-11 23:25:56 +0000 | [diff] [blame] | 893 | llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty); |
| 894 | return m; |
| 895 | } |
| 896 | |
Mike Stump | f0070db | 2009-08-26 20:46:33 +0000 | [diff] [blame] | 897 | llvm::Value * |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 898 | CodeGenFunction::GetVirtualCXXBaseClassOffset(llvm::Value *This, |
| 899 | const CXXRecordDecl *ClassDecl, |
| 900 | const CXXRecordDecl *BaseClassDecl) { |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 901 | const llvm::Type *Int8PtrTy = |
| 902 | llvm::Type::getInt8Ty(VMContext)->getPointerTo(); |
| 903 | |
| 904 | llvm::Value *VTablePtr = Builder.CreateBitCast(This, |
| 905 | Int8PtrTy->getPointerTo()); |
| 906 | VTablePtr = Builder.CreateLoad(VTablePtr, "vtable"); |
| 907 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 908 | int64_t VBaseOffsetIndex = |
| 909 | CGM.getVtableInfo().getVirtualBaseOffsetIndex(ClassDecl, BaseClassDecl); |
| 910 | |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 911 | llvm::Value *VBaseOffsetPtr = |
Mike Stump | 79d5768 | 2009-11-04 01:11:15 +0000 | [diff] [blame] | 912 | Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetIndex, "vbase.offset.ptr"); |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 913 | const llvm::Type *PtrDiffTy = |
| 914 | ConvertType(getContext().getPointerDiffType()); |
| 915 | |
| 916 | VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr, |
| 917 | PtrDiffTy->getPointerTo()); |
| 918 | |
| 919 | llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset"); |
| 920 | |
| 921 | return VBaseOffset; |
| 922 | } |
| 923 | |
| 924 | llvm::Value * |
Mike Stump | f0070db | 2009-08-26 20:46:33 +0000 | [diff] [blame] | 925 | CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *&This, |
| 926 | const llvm::Type *Ty) { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 927 | int64_t Index = CGM.getVtableInfo().getMethodVtableIndex(MD); |
Anders Carlsson | 2b35835 | 2009-10-03 14:56:57 +0000 | [diff] [blame] | 928 | |
Mike Stump | f0070db | 2009-08-26 20:46:33 +0000 | [diff] [blame] | 929 | Ty = llvm::PointerType::get(Ty, 0); |
| 930 | Ty = llvm::PointerType::get(Ty, 0); |
| 931 | Ty = llvm::PointerType::get(Ty, 0); |
| 932 | llvm::Value *vtbl = Builder.CreateBitCast(This, Ty); |
| 933 | vtbl = Builder.CreateLoad(vtbl); |
| 934 | llvm::Value *vfn = Builder.CreateConstInBoundsGEP1_64(vtbl, |
Anders Carlsson | 2b35835 | 2009-10-03 14:56:57 +0000 | [diff] [blame] | 935 | Index, "vfn"); |
Mike Stump | f0070db | 2009-08-26 20:46:33 +0000 | [diff] [blame] | 936 | vfn = Builder.CreateLoad(vfn); |
| 937 | return vfn; |
| 938 | } |
| 939 | |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 940 | /// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class |
| 941 | /// array of objects from SrcValue to DestValue. Copying can be either a bitwise |
| 942 | /// copy or via a copy constructor call. |
Fariborz Jahanian | 4f68d53 | 2009-08-26 00:23:27 +0000 | [diff] [blame] | 943 | // FIXME. Consolidate this with EmitCXXAggrConstructorCall. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 944 | void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest, |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 945 | llvm::Value *Src, |
| 946 | const ArrayType *Array, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 947 | const CXXRecordDecl *BaseClassDecl, |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 948 | QualType Ty) { |
| 949 | const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array); |
| 950 | assert(CA && "VLA cannot be copied over"); |
| 951 | bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 952 | |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 953 | // Create a temporary for the loop index and initialize it with 0. |
| 954 | llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext), |
| 955 | "loop.index"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 956 | llvm::Value* zeroConstant = |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 957 | llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)); |
Anders Carlsson | 2b35835 | 2009-10-03 14:56:57 +0000 | [diff] [blame] | 958 | Builder.CreateStore(zeroConstant, IndexPtr, false); |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 959 | // Start the loop with a block that tests the condition. |
| 960 | llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); |
| 961 | llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 962 | |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 963 | EmitBlock(CondBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 964 | |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 965 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
| 966 | // Generate: if (loop-index < number-of-elements fall to the loop body, |
| 967 | // otherwise, go to the block after the for-loop. |
| 968 | uint64_t NumElements = getContext().getConstantArrayElementCount(CA); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 969 | llvm::Value * NumElementsPtr = |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 970 | llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements); |
| 971 | llvm::Value *Counter = Builder.CreateLoad(IndexPtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 972 | llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr, |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 973 | "isless"); |
| 974 | // If the condition is true, execute the body. |
| 975 | Builder.CreateCondBr(IsLess, ForBody, AfterFor); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 976 | |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 977 | EmitBlock(ForBody); |
| 978 | llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); |
| 979 | // Inside the loop body, emit the constructor call on the array element. |
| 980 | Counter = Builder.CreateLoad(IndexPtr); |
| 981 | Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress"); |
| 982 | Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress"); |
| 983 | if (BitwiseCopy) |
| 984 | EmitAggregateCopy(Dest, Src, Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 985 | else if (CXXConstructorDecl *BaseCopyCtor = |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 986 | BaseClassDecl->getCopyConstructor(getContext(), 0)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 987 | llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor, |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 988 | Ctor_Complete); |
| 989 | CallArgList CallArgs; |
| 990 | // Push the this (Dest) ptr. |
| 991 | CallArgs.push_back(std::make_pair(RValue::get(Dest), |
| 992 | BaseCopyCtor->getThisType(getContext()))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 993 | |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 994 | // Push the Src ptr. |
| 995 | CallArgs.push_back(std::make_pair(RValue::get(Src), |
Mike Stump | 79d5768 | 2009-11-04 01:11:15 +0000 | [diff] [blame] | 996 | BaseCopyCtor->getParamDecl(0)->getType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 997 | QualType ResultType = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 998 | BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType(); |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 999 | EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs), |
| 1000 | Callee, CallArgs, BaseCopyCtor); |
| 1001 | } |
| 1002 | EmitBlock(ContinueBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1003 | |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 1004 | // Emit the increment of the loop counter. |
| 1005 | llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1); |
| 1006 | Counter = Builder.CreateLoad(IndexPtr); |
| 1007 | NextVal = Builder.CreateAdd(Counter, NextVal, "inc"); |
| 1008 | Builder.CreateStore(NextVal, IndexPtr, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1009 | |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 1010 | // Finally, branch back up to the condition for the next iteration. |
| 1011 | EmitBranch(CondBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1012 | |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 1013 | // Emit the fall-through block. |
| 1014 | EmitBlock(AfterFor, true); |
| 1015 | } |
| 1016 | |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1017 | /// EmitClassAggrCopyAssignment - This routine generates code to assign a class |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1018 | /// array of objects from SrcValue to DestValue. Assignment can be either a |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1019 | /// bitwise assignment or via a copy assignment operator function call. |
| 1020 | /// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1021 | void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest, |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1022 | llvm::Value *Src, |
| 1023 | const ArrayType *Array, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1024 | const CXXRecordDecl *BaseClassDecl, |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1025 | QualType Ty) { |
| 1026 | const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array); |
| 1027 | assert(CA && "VLA cannot be asssigned"); |
| 1028 | bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1029 | |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1030 | // Create a temporary for the loop index and initialize it with 0. |
| 1031 | llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext), |
| 1032 | "loop.index"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1033 | llvm::Value* zeroConstant = |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1034 | llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)); |
| 1035 | Builder.CreateStore(zeroConstant, IndexPtr, false); |
| 1036 | // Start the loop with a block that tests the condition. |
| 1037 | llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); |
| 1038 | llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1039 | |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1040 | EmitBlock(CondBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1041 | |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1042 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
| 1043 | // Generate: if (loop-index < number-of-elements fall to the loop body, |
| 1044 | // otherwise, go to the block after the for-loop. |
| 1045 | uint64_t NumElements = getContext().getConstantArrayElementCount(CA); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1046 | llvm::Value * NumElementsPtr = |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1047 | llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements); |
| 1048 | llvm::Value *Counter = Builder.CreateLoad(IndexPtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1049 | llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr, |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1050 | "isless"); |
| 1051 | // If the condition is true, execute the body. |
| 1052 | Builder.CreateCondBr(IsLess, ForBody, AfterFor); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1053 | |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1054 | EmitBlock(ForBody); |
| 1055 | llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); |
| 1056 | // Inside the loop body, emit the assignment operator call on array element. |
| 1057 | Counter = Builder.CreateLoad(IndexPtr); |
| 1058 | Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress"); |
| 1059 | Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress"); |
| 1060 | const CXXMethodDecl *MD = 0; |
| 1061 | if (BitwiseAssign) |
| 1062 | EmitAggregateCopy(Dest, Src, Ty); |
| 1063 | else { |
| 1064 | bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(), |
| 1065 | MD); |
| 1066 | assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign"); |
| 1067 | (void)hasCopyAssign; |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1068 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1069 | const llvm::Type *LTy = |
| 1070 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), |
| 1071 | FPT->isVariadic()); |
Anders Carlsson | 555b4bb | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 1072 | llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1073 | |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1074 | CallArgList CallArgs; |
| 1075 | // Push the this (Dest) ptr. |
| 1076 | CallArgs.push_back(std::make_pair(RValue::get(Dest), |
| 1077 | MD->getThisType(getContext()))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1078 | |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1079 | // Push the Src ptr. |
| 1080 | CallArgs.push_back(std::make_pair(RValue::get(Src), |
| 1081 | MD->getParamDecl(0)->getType())); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1082 | QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType(); |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1083 | EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs), |
| 1084 | Callee, CallArgs, MD); |
| 1085 | } |
| 1086 | EmitBlock(ContinueBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1087 | |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1088 | // Emit the increment of the loop counter. |
| 1089 | llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1); |
| 1090 | Counter = Builder.CreateLoad(IndexPtr); |
| 1091 | NextVal = Builder.CreateAdd(Counter, NextVal, "inc"); |
| 1092 | Builder.CreateStore(NextVal, IndexPtr, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1093 | |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1094 | // Finally, branch back up to the condition for the next iteration. |
| 1095 | EmitBranch(CondBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1096 | |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1097 | // Emit the fall-through block. |
| 1098 | EmitBlock(AfterFor, true); |
| 1099 | } |
| 1100 | |
Fariborz Jahanian | ca28361 | 2009-08-07 23:51:33 +0000 | [diff] [blame] | 1101 | /// EmitClassMemberwiseCopy - This routine generates code to copy a class |
| 1102 | /// object from SrcValue to DestValue. Copying can be either a bitwise copy |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 1103 | /// or via a copy constructor call. |
Fariborz Jahanian | ca28361 | 2009-08-07 23:51:33 +0000 | [diff] [blame] | 1104 | void CodeGenFunction::EmitClassMemberwiseCopy( |
Fariborz Jahanian | 942f4f3 | 2009-08-08 23:32:22 +0000 | [diff] [blame] | 1105 | llvm::Value *Dest, llvm::Value *Src, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1106 | const CXXRecordDecl *ClassDecl, |
Fariborz Jahanian | 942f4f3 | 2009-08-08 23:32:22 +0000 | [diff] [blame] | 1107 | const CXXRecordDecl *BaseClassDecl, QualType Ty) { |
| 1108 | if (ClassDecl) { |
Anders Carlsson | 5a0f49e | 2009-09-12 04:26:35 +0000 | [diff] [blame] | 1109 | Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl, |
| 1110 | /*NullCheckValue=*/false); |
| 1111 | Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl, |
| 1112 | /*NullCheckValue=*/false); |
Fariborz Jahanian | 942f4f3 | 2009-08-08 23:32:22 +0000 | [diff] [blame] | 1113 | } |
| 1114 | if (BaseClassDecl->hasTrivialCopyConstructor()) { |
| 1115 | EmitAggregateCopy(Dest, Src, Ty); |
Fariborz Jahanian | ca28361 | 2009-08-07 23:51:33 +0000 | [diff] [blame] | 1116 | return; |
Fariborz Jahanian | 942f4f3 | 2009-08-08 23:32:22 +0000 | [diff] [blame] | 1117 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1118 | |
| 1119 | if (CXXConstructorDecl *BaseCopyCtor = |
Fariborz Jahanian | 80e4b9e | 2009-08-08 00:59:58 +0000 | [diff] [blame] | 1120 | BaseClassDecl->getCopyConstructor(getContext(), 0)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1121 | llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor, |
Fariborz Jahanian | ca28361 | 2009-08-07 23:51:33 +0000 | [diff] [blame] | 1122 | Ctor_Complete); |
Fariborz Jahanian | ca28361 | 2009-08-07 23:51:33 +0000 | [diff] [blame] | 1123 | CallArgList CallArgs; |
| 1124 | // Push the this (Dest) ptr. |
| 1125 | CallArgs.push_back(std::make_pair(RValue::get(Dest), |
| 1126 | BaseCopyCtor->getThisType(getContext()))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1127 | |
Fariborz Jahanian | ca28361 | 2009-08-07 23:51:33 +0000 | [diff] [blame] | 1128 | // Push the Src ptr. |
| 1129 | CallArgs.push_back(std::make_pair(RValue::get(Src), |
Fariborz Jahanian | 370c884 | 2009-08-10 17:20:45 +0000 | [diff] [blame] | 1130 | BaseCopyCtor->getParamDecl(0)->getType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1131 | QualType ResultType = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1132 | BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType(); |
Fariborz Jahanian | ca28361 | 2009-08-07 23:51:33 +0000 | [diff] [blame] | 1133 | EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs), |
| 1134 | Callee, CallArgs, BaseCopyCtor); |
| 1135 | } |
| 1136 | } |
Fariborz Jahanian | 06f598a | 2009-08-10 18:46:38 +0000 | [diff] [blame] | 1137 | |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1138 | /// EmitClassCopyAssignment - This routine generates code to copy assign a class |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1139 | /// object from SrcValue to DestValue. Assignment can be either a bitwise |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1140 | /// assignment of via an assignment operator call. |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1141 | // FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot. |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1142 | void CodeGenFunction::EmitClassCopyAssignment( |
| 1143 | llvm::Value *Dest, llvm::Value *Src, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1144 | const CXXRecordDecl *ClassDecl, |
| 1145 | const CXXRecordDecl *BaseClassDecl, |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1146 | QualType Ty) { |
| 1147 | if (ClassDecl) { |
Anders Carlsson | 5a0f49e | 2009-09-12 04:26:35 +0000 | [diff] [blame] | 1148 | Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl, |
| 1149 | /*NullCheckValue=*/false); |
| 1150 | Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl, |
| 1151 | /*NullCheckValue=*/false); |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1152 | } |
| 1153 | if (BaseClassDecl->hasTrivialCopyAssignment()) { |
| 1154 | EmitAggregateCopy(Dest, Src, Ty); |
| 1155 | return; |
| 1156 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1157 | |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1158 | const CXXMethodDecl *MD = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1159 | bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(), |
Fariborz Jahanian | e82c3e2 | 2009-08-13 00:53:36 +0000 | [diff] [blame] | 1160 | MD); |
| 1161 | assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign"); |
| 1162 | (void)ConstCopyAssignOp; |
| 1163 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1164 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1165 | const llvm::Type *LTy = |
| 1166 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), |
Fariborz Jahanian | e82c3e2 | 2009-08-13 00:53:36 +0000 | [diff] [blame] | 1167 | FPT->isVariadic()); |
Anders Carlsson | 555b4bb | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 1168 | llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1169 | |
Fariborz Jahanian | e82c3e2 | 2009-08-13 00:53:36 +0000 | [diff] [blame] | 1170 | CallArgList CallArgs; |
| 1171 | // Push the this (Dest) ptr. |
| 1172 | CallArgs.push_back(std::make_pair(RValue::get(Dest), |
| 1173 | MD->getThisType(getContext()))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1174 | |
Fariborz Jahanian | e82c3e2 | 2009-08-13 00:53:36 +0000 | [diff] [blame] | 1175 | // Push the Src ptr. |
| 1176 | CallArgs.push_back(std::make_pair(RValue::get(Src), |
| 1177 | MD->getParamDecl(0)->getType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1178 | QualType ResultType = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1179 | MD->getType()->getAs<FunctionType>()->getResultType(); |
Fariborz Jahanian | e82c3e2 | 2009-08-13 00:53:36 +0000 | [diff] [blame] | 1180 | EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs), |
| 1181 | Callee, CallArgs, MD); |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
Fariborz Jahanian | 06f598a | 2009-08-10 18:46:38 +0000 | [diff] [blame] | 1184 | /// SynthesizeDefaultConstructor - synthesize a default constructor |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1185 | void |
Anders Carlsson | de1d26b | 2009-09-14 05:32:02 +0000 | [diff] [blame] | 1186 | CodeGenFunction::SynthesizeDefaultConstructor(const CXXConstructorDecl *Ctor, |
| 1187 | CXXCtorType Type, |
Fariborz Jahanian | 06f598a | 2009-08-10 18:46:38 +0000 | [diff] [blame] | 1188 | llvm::Function *Fn, |
| 1189 | const FunctionArgList &Args) { |
Anders Carlsson | de1d26b | 2009-09-14 05:32:02 +0000 | [diff] [blame] | 1190 | StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args, |
| 1191 | SourceLocation()); |
| 1192 | EmitCtorPrologue(Ctor, Type); |
Fariborz Jahanian | 06f598a | 2009-08-10 18:46:38 +0000 | [diff] [blame] | 1193 | FinishFunction(); |
| 1194 | } |
| 1195 | |
Mike Stump | 79d5768 | 2009-11-04 01:11:15 +0000 | [diff] [blame] | 1196 | /// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a |
| 1197 | /// copy constructor, in accordance with section 12.8 (p7 and p8) of C++03 |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1198 | /// The implicitly-defined copy constructor for class X performs a memberwise |
Mike Stump | 79d5768 | 2009-11-04 01:11:15 +0000 | [diff] [blame] | 1199 | /// copy of its subobjects. The order of copying is the same as the order of |
| 1200 | /// initialization of bases and members in a user-defined constructor |
Fariborz Jahanian | 97a9375 | 2009-08-07 20:22:40 +0000 | [diff] [blame] | 1201 | /// Each subobject is copied in the manner appropriate to its type: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1202 | /// if the subobject is of class type, the copy constructor for the class is |
Fariborz Jahanian | 97a9375 | 2009-08-07 20:22:40 +0000 | [diff] [blame] | 1203 | /// used; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1204 | /// if the subobject is an array, each element is copied, in the manner |
Fariborz Jahanian | 97a9375 | 2009-08-07 20:22:40 +0000 | [diff] [blame] | 1205 | /// appropriate to the element type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1206 | /// if the subobject is of scalar type, the built-in assignment operator is |
Fariborz Jahanian | 97a9375 | 2009-08-07 20:22:40 +0000 | [diff] [blame] | 1207 | /// used. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1208 | /// Virtual base class subobjects shall be copied only once by the |
| 1209 | /// implicitly-defined copy constructor |
Fariborz Jahanian | 97a9375 | 2009-08-07 20:22:40 +0000 | [diff] [blame] | 1210 | |
Anders Carlsson | de1d26b | 2009-09-14 05:32:02 +0000 | [diff] [blame] | 1211 | void |
| 1212 | CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *Ctor, |
| 1213 | CXXCtorType Type, |
| 1214 | llvm::Function *Fn, |
| 1215 | const FunctionArgList &Args) { |
Anders Carlsson | 0ff8baf | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 1216 | const CXXRecordDecl *ClassDecl = Ctor->getParent(); |
Fariborz Jahanian | 97a9375 | 2009-08-07 20:22:40 +0000 | [diff] [blame] | 1217 | assert(!ClassDecl->hasUserDeclaredCopyConstructor() && |
Mike Stump | 79d5768 | 2009-11-04 01:11:15 +0000 | [diff] [blame] | 1218 | "SynthesizeCXXCopyConstructor - copy constructor has definition already"); |
Anders Carlsson | de1d26b | 2009-09-14 05:32:02 +0000 | [diff] [blame] | 1219 | StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args, |
| 1220 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1221 | |
Fariborz Jahanian | 1e4edd5 | 2009-08-08 00:15:41 +0000 | [diff] [blame] | 1222 | FunctionArgList::const_iterator i = Args.begin(); |
| 1223 | const VarDecl *ThisArg = i->first; |
| 1224 | llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg); |
| 1225 | llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this"); |
| 1226 | const VarDecl *SrcArg = (i+1)->first; |
| 1227 | llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg); |
| 1228 | llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1229 | |
Fariborz Jahanian | 97a9375 | 2009-08-07 20:22:40 +0000 | [diff] [blame] | 1230 | for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(); |
| 1231 | Base != ClassDecl->bases_end(); ++Base) { |
| 1232 | // FIXME. copy constrution of virtual base NYI |
| 1233 | if (Base->isVirtual()) |
| 1234 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1235 | |
Fariborz Jahanian | 97a9375 | 2009-08-07 20:22:40 +0000 | [diff] [blame] | 1236 | CXXRecordDecl *BaseClassDecl |
| 1237 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | 942f4f3 | 2009-08-08 23:32:22 +0000 | [diff] [blame] | 1238 | EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl, |
| 1239 | Base->getType()); |
Fariborz Jahanian | 97a9375 | 2009-08-07 20:22:40 +0000 | [diff] [blame] | 1240 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1241 | |
Fariborz Jahanian | 1e4edd5 | 2009-08-08 00:15:41 +0000 | [diff] [blame] | 1242 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 1243 | FieldEnd = ClassDecl->field_end(); |
| 1244 | Field != FieldEnd; ++Field) { |
| 1245 | QualType FieldType = getContext().getCanonicalType((*Field)->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1246 | const ConstantArrayType *Array = |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 1247 | getContext().getAsConstantArrayType(FieldType); |
| 1248 | if (Array) |
| 1249 | FieldType = getContext().getBaseElementType(FieldType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1250 | |
Fariborz Jahanian | 1e4edd5 | 2009-08-08 00:15:41 +0000 | [diff] [blame] | 1251 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
| 1252 | CXXRecordDecl *FieldClassDecl |
| 1253 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
| 1254 | LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0); |
| 1255 | LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0); |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 1256 | if (Array) { |
| 1257 | const llvm::Type *BasePtr = ConvertType(FieldType); |
| 1258 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1259 | llvm::Value *DestBaseAddrPtr = |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 1260 | Builder.CreateBitCast(LHS.getAddress(), BasePtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1261 | llvm::Value *SrcBaseAddrPtr = |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 1262 | Builder.CreateBitCast(RHS.getAddress(), BasePtr); |
| 1263 | EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array, |
| 1264 | FieldClassDecl, FieldType); |
| 1265 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1266 | else |
| 1267 | EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(), |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 1268 | 0 /*ClassDecl*/, FieldClassDecl, FieldType); |
Fariborz Jahanian | 1e4edd5 | 2009-08-08 00:15:41 +0000 | [diff] [blame] | 1269 | continue; |
| 1270 | } |
Fariborz Jahanian | f05fe65 | 2009-08-10 18:34:26 +0000 | [diff] [blame] | 1271 | // Do a built-in assignment of scalar data members. |
| 1272 | LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0); |
| 1273 | LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0); |
| 1274 | RValue RVRHS = EmitLoadOfLValue(RHS, FieldType); |
| 1275 | EmitStoreThroughLValue(RVRHS, LHS, FieldType); |
Fariborz Jahanian | 1e4edd5 | 2009-08-08 00:15:41 +0000 | [diff] [blame] | 1276 | } |
Fariborz Jahanian | 8c241a2 | 2009-08-08 19:31:03 +0000 | [diff] [blame] | 1277 | FinishFunction(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1278 | } |
Fariborz Jahanian | 97a9375 | 2009-08-07 20:22:40 +0000 | [diff] [blame] | 1279 | |
Fariborz Jahanian | 2198ba1 | 2009-08-12 21:14:35 +0000 | [diff] [blame] | 1280 | /// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1281 | /// Before the implicitly-declared copy assignment operator for a class is |
| 1282 | /// implicitly defined, all implicitly- declared copy assignment operators for |
| 1283 | /// its direct base classes and its nonstatic data members shall have been |
Fariborz Jahanian | 2198ba1 | 2009-08-12 21:14:35 +0000 | [diff] [blame] | 1284 | /// implicitly defined. [12.8-p12] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1285 | /// The implicitly-defined copy assignment operator for class X performs |
| 1286 | /// memberwise assignment of its subob- jects. The direct base classes of X are |
| 1287 | /// assigned first, in the order of their declaration in |
| 1288 | /// the base-specifier-list, and then the immediate nonstatic data members of X |
| 1289 | /// are assigned, in the order in which they were declared in the class |
Fariborz Jahanian | 2198ba1 | 2009-08-12 21:14:35 +0000 | [diff] [blame] | 1290 | /// definition.Each subobject is assigned in the manner appropriate to its type: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1291 | /// if the subobject is of class type, the copy assignment operator for the |
| 1292 | /// class is used (as if by explicit qualification; that is, ignoring any |
Fariborz Jahanian | 2198ba1 | 2009-08-12 21:14:35 +0000 | [diff] [blame] | 1293 | /// possible virtual overriding functions in more derived classes); |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1294 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1295 | /// if the subobject is an array, each element is assigned, in the manner |
Fariborz Jahanian | 2198ba1 | 2009-08-12 21:14:35 +0000 | [diff] [blame] | 1296 | /// appropriate to the element type; |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1297 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1298 | /// if the subobject is of scalar type, the built-in assignment operator is |
Fariborz Jahanian | 2198ba1 | 2009-08-12 21:14:35 +0000 | [diff] [blame] | 1299 | /// used. |
| 1300 | void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD, |
Fariborz Jahanian | 2198ba1 | 2009-08-12 21:14:35 +0000 | [diff] [blame] | 1301 | llvm::Function *Fn, |
| 1302 | const FunctionArgList &Args) { |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1303 | |
| 1304 | const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext()); |
| 1305 | assert(!ClassDecl->hasUserDeclaredCopyAssignment() && |
| 1306 | "SynthesizeCXXCopyAssignment - copy assignment has user declaration"); |
Anders Carlsson | de1d26b | 2009-09-14 05:32:02 +0000 | [diff] [blame] | 1307 | StartFunction(CD, CD->getResultType(), Fn, Args, SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1308 | |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1309 | FunctionArgList::const_iterator i = Args.begin(); |
| 1310 | const VarDecl *ThisArg = i->first; |
| 1311 | llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg); |
| 1312 | llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this"); |
| 1313 | const VarDecl *SrcArg = (i+1)->first; |
| 1314 | llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg); |
| 1315 | llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1316 | |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1317 | for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(); |
| 1318 | Base != ClassDecl->bases_end(); ++Base) { |
| 1319 | // FIXME. copy assignment of virtual base NYI |
| 1320 | if (Base->isVirtual()) |
| 1321 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1322 | |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1323 | CXXRecordDecl *BaseClassDecl |
| 1324 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 1325 | EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl, |
| 1326 | Base->getType()); |
| 1327 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1328 | |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1329 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 1330 | FieldEnd = ClassDecl->field_end(); |
| 1331 | Field != FieldEnd; ++Field) { |
| 1332 | QualType FieldType = getContext().getCanonicalType((*Field)->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1333 | const ConstantArrayType *Array = |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1334 | getContext().getAsConstantArrayType(FieldType); |
| 1335 | if (Array) |
| 1336 | FieldType = getContext().getBaseElementType(FieldType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1337 | |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1338 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
| 1339 | CXXRecordDecl *FieldClassDecl |
| 1340 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
| 1341 | LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0); |
| 1342 | LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0); |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1343 | if (Array) { |
| 1344 | const llvm::Type *BasePtr = ConvertType(FieldType); |
| 1345 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
| 1346 | llvm::Value *DestBaseAddrPtr = |
| 1347 | Builder.CreateBitCast(LHS.getAddress(), BasePtr); |
| 1348 | llvm::Value *SrcBaseAddrPtr = |
| 1349 | Builder.CreateBitCast(RHS.getAddress(), BasePtr); |
| 1350 | EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array, |
| 1351 | FieldClassDecl, FieldType); |
| 1352 | } |
| 1353 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1354 | EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(), |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1355 | 0 /*ClassDecl*/, FieldClassDecl, FieldType); |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1356 | continue; |
| 1357 | } |
| 1358 | // Do a built-in assignment of scalar data members. |
| 1359 | LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0); |
| 1360 | LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0); |
| 1361 | RValue RVRHS = EmitLoadOfLValue(RHS, FieldType); |
| 1362 | EmitStoreThroughLValue(RVRHS, LHS, FieldType); |
Fariborz Jahanian | 183d718 | 2009-08-14 00:01:54 +0000 | [diff] [blame] | 1363 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1364 | |
Fariborz Jahanian | 183d718 | 2009-08-14 00:01:54 +0000 | [diff] [blame] | 1365 | // return *this; |
| 1366 | Builder.CreateStore(LoadOfThis, ReturnValue); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1367 | |
Fariborz Jahanian | 2198ba1 | 2009-08-12 21:14:35 +0000 | [diff] [blame] | 1368 | FinishFunction(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1369 | } |
Fariborz Jahanian | 97a9375 | 2009-08-07 20:22:40 +0000 | [diff] [blame] | 1370 | |
Anders Carlsson | b1156b9 | 2009-11-06 03:23:06 +0000 | [diff] [blame] | 1371 | static void EmitBaseInitializer(CodeGenFunction &CGF, |
| 1372 | const CXXRecordDecl *ClassDecl, |
| 1373 | CXXBaseOrMemberInitializer *BaseInit, |
| 1374 | CXXCtorType CtorType) { |
| 1375 | assert(BaseInit->isBaseInitializer() && |
| 1376 | "Must have base initializer!"); |
| 1377 | |
| 1378 | llvm::Value *ThisPtr = CGF.LoadCXXThis(); |
| 1379 | |
| 1380 | const Type *BaseType = BaseInit->getBaseClass(); |
| 1381 | CXXRecordDecl *BaseClassDecl = |
| 1382 | cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()); |
| 1383 | llvm::Value *V = CGF.GetAddressCXXOfBaseClass(ThisPtr, ClassDecl, |
| 1384 | BaseClassDecl, |
| 1385 | /*NullCheckValue=*/false); |
| 1386 | CGF.EmitCXXConstructorCall(BaseInit->getConstructor(), |
| 1387 | CtorType, V, |
| 1388 | BaseInit->const_arg_begin(), |
| 1389 | BaseInit->const_arg_end()); |
| 1390 | } |
| 1391 | |
| 1392 | static void EmitMemberInitializer(CodeGenFunction &CGF, |
| 1393 | const CXXRecordDecl *ClassDecl, |
| 1394 | CXXBaseOrMemberInitializer *MemberInit) { |
| 1395 | assert(MemberInit->isMemberInitializer() && |
| 1396 | "Must have member initializer!"); |
| 1397 | |
| 1398 | // non-static data member initializers. |
| 1399 | FieldDecl *Field = MemberInit->getMember(); |
| 1400 | QualType FieldType = CGF.getContext().getCanonicalType((Field)->getType()); |
| 1401 | const ConstantArrayType *Array = |
| 1402 | CGF.getContext().getAsConstantArrayType(FieldType); |
| 1403 | if (Array) |
| 1404 | FieldType = CGF.getContext().getBaseElementType(FieldType); |
| 1405 | |
| 1406 | llvm::Value *ThisPtr = CGF.LoadCXXThis(); |
| 1407 | LValue LHS; |
| 1408 | if (FieldType->isReferenceType()) { |
| 1409 | // FIXME: This is really ugly; should be refactored somehow |
| 1410 | unsigned idx = CGF.CGM.getTypes().getLLVMFieldNo(Field); |
| 1411 | llvm::Value *V = CGF.Builder.CreateStructGEP(ThisPtr, idx, "tmp"); |
| 1412 | assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs"); |
| 1413 | LHS = LValue::MakeAddr(V, CGF.MakeQualifiers(FieldType)); |
| 1414 | } else { |
| 1415 | LHS = CGF.EmitLValueForField(ThisPtr, Field, false, 0); |
| 1416 | } |
| 1417 | if (FieldType->getAs<RecordType>()) { |
| 1418 | if (!Field->isAnonymousStructOrUnion()) { |
| 1419 | assert(MemberInit->getConstructor() && |
| 1420 | "EmitCtorPrologue - no constructor to initialize member"); |
| 1421 | if (Array) { |
| 1422 | const llvm::Type *BasePtr = CGF.ConvertType(FieldType); |
| 1423 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
| 1424 | llvm::Value *BaseAddrPtr = |
| 1425 | CGF.Builder.CreateBitCast(LHS.getAddress(), BasePtr); |
| 1426 | CGF.EmitCXXAggrConstructorCall(MemberInit->getConstructor(), |
| 1427 | Array, BaseAddrPtr); |
| 1428 | } |
| 1429 | else |
| 1430 | CGF.EmitCXXConstructorCall(MemberInit->getConstructor(), |
| 1431 | Ctor_Complete, LHS.getAddress(), |
| 1432 | MemberInit->const_arg_begin(), |
| 1433 | MemberInit->const_arg_end()); |
| 1434 | return; |
| 1435 | } |
| 1436 | else { |
| 1437 | // Initializing an anonymous union data member. |
| 1438 | FieldDecl *anonMember = MemberInit->getAnonUnionMember(); |
| 1439 | LHS = CGF.EmitLValueForField(LHS.getAddress(), anonMember, |
| 1440 | /*IsUnion=*/true, 0); |
| 1441 | FieldType = anonMember->getType(); |
| 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | assert(MemberInit->getNumArgs() == 1 && "Initializer count must be 1 only"); |
| 1446 | Expr *RhsExpr = *MemberInit->arg_begin(); |
| 1447 | RValue RHS; |
| 1448 | if (FieldType->isReferenceType()) |
| 1449 | RHS = CGF.EmitReferenceBindingToExpr(RhsExpr, FieldType, |
| 1450 | /*IsInitializer=*/true); |
| 1451 | else if (FieldType->isMemberFunctionPointerType()) |
| 1452 | RHS = RValue::get(CGF.CGM.EmitConstantExpr(RhsExpr, FieldType, &CGF)); |
| 1453 | else |
| 1454 | RHS = RValue::get(CGF.EmitScalarExpr(RhsExpr, true)); |
| 1455 | CGF.EmitStoreThroughLValue(RHS, LHS, FieldType); |
| 1456 | } |
| 1457 | |
Fariborz Jahanian | e7d346b | 2009-07-20 23:18:55 +0000 | [diff] [blame] | 1458 | /// EmitCtorPrologue - This routine generates necessary code to initialize |
| 1459 | /// base classes and non-static data members belonging to this constructor. |
Anders Carlsson | 174754c | 2009-09-01 18:33:46 +0000 | [diff] [blame] | 1460 | /// FIXME: This needs to take a CXXCtorType. |
Anders Carlsson | de1d26b | 2009-09-14 05:32:02 +0000 | [diff] [blame] | 1461 | void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD, |
| 1462 | CXXCtorType CtorType) { |
Anders Carlsson | b1156b9 | 2009-11-06 03:23:06 +0000 | [diff] [blame] | 1463 | const CXXRecordDecl *ClassDecl = CD->getParent(); |
| 1464 | |
Mike Stump | eb19fa9 | 2009-08-06 13:41:24 +0000 | [diff] [blame] | 1465 | // FIXME: Add vbase initialization |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 1466 | llvm::Value *LoadOfThis = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1467 | |
Fariborz Jahanian | 742cd1b | 2009-07-25 21:12:28 +0000 | [diff] [blame] | 1468 | for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(), |
Fariborz Jahanian | e7d346b | 2009-07-20 23:18:55 +0000 | [diff] [blame] | 1469 | E = CD->init_end(); |
| 1470 | B != E; ++B) { |
| 1471 | CXXBaseOrMemberInitializer *Member = (*B); |
Anders Carlsson | b1156b9 | 2009-11-06 03:23:06 +0000 | [diff] [blame] | 1472 | |
Anders Carlsson | 1faf674 | 2009-11-06 04:11:09 +0000 | [diff] [blame] | 1473 | assert(LiveTemporaries.empty() && |
| 1474 | "Should not have any live temporaries at initializer start!"); |
| 1475 | |
Anders Carlsson | b1156b9 | 2009-11-06 03:23:06 +0000 | [diff] [blame] | 1476 | if (Member->isBaseInitializer()) |
| 1477 | EmitBaseInitializer(*this, ClassDecl, Member, CtorType); |
| 1478 | else |
| 1479 | EmitMemberInitializer(*this, ClassDecl, Member); |
Anders Carlsson | 1faf674 | 2009-11-06 04:11:09 +0000 | [diff] [blame] | 1480 | |
| 1481 | // Pop any live temporaries that the initializers might have pushed. |
| 1482 | while (!LiveTemporaries.empty()) |
| 1483 | PopCXXTemporary(); |
Fariborz Jahanian | e7d346b | 2009-07-20 23:18:55 +0000 | [diff] [blame] | 1484 | } |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 1485 | |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 1486 | if (!CD->getNumBaseOrMemberInitializers() && !CD->isTrivial()) { |
Fariborz Jahanian | 1d9b5ef | 2009-08-15 18:55:17 +0000 | [diff] [blame] | 1487 | // Nontrivial default constructor with no initializer list. It may still |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1488 | // have bases classes and/or contain non-static data members which require |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 1489 | // construction. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1490 | for (CXXRecordDecl::base_class_const_iterator Base = |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 1491 | ClassDecl->bases_begin(); |
| 1492 | Base != ClassDecl->bases_end(); ++Base) { |
| 1493 | // FIXME. copy assignment of virtual base NYI |
| 1494 | if (Base->isVirtual()) |
| 1495 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1496 | |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 1497 | CXXRecordDecl *BaseClassDecl |
| 1498 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 1499 | if (BaseClassDecl->hasTrivialConstructor()) |
| 1500 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1501 | if (CXXConstructorDecl *BaseCX = |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 1502 | BaseClassDecl->getDefaultConstructor(getContext())) { |
| 1503 | LoadOfThis = LoadCXXThis(); |
Anders Carlsson | 5a0f49e | 2009-09-12 04:26:35 +0000 | [diff] [blame] | 1504 | llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl, |
| 1505 | BaseClassDecl, |
| 1506 | /*NullCheckValue=*/false); |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 1507 | EmitCXXConstructorCall(BaseCX, Ctor_Complete, V, 0, 0); |
| 1508 | } |
| 1509 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1510 | |
Fariborz Jahanian | 1d9b5ef | 2009-08-15 18:55:17 +0000 | [diff] [blame] | 1511 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 1512 | FieldEnd = ClassDecl->field_end(); |
| 1513 | Field != FieldEnd; ++Field) { |
| 1514 | QualType FieldType = getContext().getCanonicalType((*Field)->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1515 | const ConstantArrayType *Array = |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 1516 | getContext().getAsConstantArrayType(FieldType); |
Fariborz Jahanian | f800f6c | 2009-08-20 20:54:15 +0000 | [diff] [blame] | 1517 | if (Array) |
| 1518 | FieldType = getContext().getBaseElementType(FieldType); |
Fariborz Jahanian | 1d9b5ef | 2009-08-15 18:55:17 +0000 | [diff] [blame] | 1519 | if (!FieldType->getAs<RecordType>() || Field->isAnonymousStructOrUnion()) |
| 1520 | continue; |
| 1521 | const RecordType *ClassRec = FieldType->getAs<RecordType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1522 | CXXRecordDecl *MemberClassDecl = |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 1523 | dyn_cast<CXXRecordDecl>(ClassRec->getDecl()); |
| 1524 | if (!MemberClassDecl || MemberClassDecl->hasTrivialConstructor()) |
| 1525 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1526 | if (CXXConstructorDecl *MamberCX = |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 1527 | MemberClassDecl->getDefaultConstructor(getContext())) { |
| 1528 | LoadOfThis = LoadCXXThis(); |
| 1529 | LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0); |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 1530 | if (Array) { |
| 1531 | const llvm::Type *BasePtr = ConvertType(FieldType); |
| 1532 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1533 | llvm::Value *BaseAddrPtr = |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 1534 | Builder.CreateBitCast(LHS.getAddress(), BasePtr); |
| 1535 | EmitCXXAggrConstructorCall(MamberCX, Array, BaseAddrPtr); |
| 1536 | } |
| 1537 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1538 | EmitCXXConstructorCall(MamberCX, Ctor_Complete, LHS.getAddress(), |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 1539 | 0, 0); |
Fariborz Jahanian | 1d9b5ef | 2009-08-15 18:55:17 +0000 | [diff] [blame] | 1540 | } |
| 1541 | } |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 1542 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1543 | |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 1544 | // Initialize the vtable pointer |
Mike Stump | b502d83 | 2009-08-05 22:59:44 +0000 | [diff] [blame] | 1545 | if (ClassDecl->isDynamicClass()) { |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 1546 | if (!LoadOfThis) |
| 1547 | LoadOfThis = LoadCXXThis(); |
| 1548 | llvm::Value *VtableField; |
| 1549 | llvm::Type *Ptr8Ty, *PtrPtr8Ty; |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1550 | Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0); |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 1551 | PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0); |
| 1552 | VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty); |
| 1553 | llvm::Value *vtable = GenerateVtable(ClassDecl); |
| 1554 | Builder.CreateStore(vtable, VtableField); |
| 1555 | } |
Fariborz Jahanian | e7d346b | 2009-07-20 23:18:55 +0000 | [diff] [blame] | 1556 | } |
Fariborz Jahanian | 426cc38 | 2009-07-30 17:49:11 +0000 | [diff] [blame] | 1557 | |
| 1558 | /// EmitDtorEpilogue - Emit all code that comes at the end of class's |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1559 | /// destructor. This is to call destructors on members and base classes |
Fariborz Jahanian | 426cc38 | 2009-07-30 17:49:11 +0000 | [diff] [blame] | 1560 | /// in reverse order of their construction. |
Anders Carlsson | 174754c | 2009-09-01 18:33:46 +0000 | [diff] [blame] | 1561 | /// FIXME: This needs to take a CXXDtorType. |
Anders Carlsson | de1d26b | 2009-09-14 05:32:02 +0000 | [diff] [blame] | 1562 | void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD, |
| 1563 | CXXDtorType DtorType) { |
Fariborz Jahanian | 426cc38 | 2009-07-30 17:49:11 +0000 | [diff] [blame] | 1564 | const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext()); |
Anders Carlsson | de738fe | 2009-09-01 21:12:16 +0000 | [diff] [blame] | 1565 | assert(!ClassDecl->getNumVBases() && |
| 1566 | "FIXME: Destruction of virtual bases not supported"); |
Fariborz Jahanian | 426cc38 | 2009-07-30 17:49:11 +0000 | [diff] [blame] | 1567 | (void)ClassDecl; // prevent warning. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1568 | |
Fariborz Jahanian | 426cc38 | 2009-07-30 17:49:11 +0000 | [diff] [blame] | 1569 | for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(), |
| 1570 | *E = DD->destr_end(); B != E; ++B) { |
| 1571 | uintptr_t BaseOrMember = (*B); |
| 1572 | if (DD->isMemberToDestroy(BaseOrMember)) { |
| 1573 | FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember); |
| 1574 | QualType FieldType = getContext().getCanonicalType((FD)->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1575 | const ConstantArrayType *Array = |
Fariborz Jahanian | f800f6c | 2009-08-20 20:54:15 +0000 | [diff] [blame] | 1576 | getContext().getAsConstantArrayType(FieldType); |
| 1577 | if (Array) |
| 1578 | FieldType = getContext().getBaseElementType(FieldType); |
Fariborz Jahanian | 426cc38 | 2009-07-30 17:49:11 +0000 | [diff] [blame] | 1579 | const RecordType *RT = FieldType->getAs<RecordType>(); |
| 1580 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 1581 | if (FieldClassDecl->hasTrivialDestructor()) |
| 1582 | continue; |
| 1583 | llvm::Value *LoadOfThis = LoadCXXThis(); |
| 1584 | LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0); |
Fariborz Jahanian | f800f6c | 2009-08-20 20:54:15 +0000 | [diff] [blame] | 1585 | if (Array) { |
| 1586 | const llvm::Type *BasePtr = ConvertType(FieldType); |
| 1587 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1588 | llvm::Value *BaseAddrPtr = |
Fariborz Jahanian | f800f6c | 2009-08-20 20:54:15 +0000 | [diff] [blame] | 1589 | Builder.CreateBitCast(LHS.getAddress(), BasePtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1590 | EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()), |
Fariborz Jahanian | f800f6c | 2009-08-20 20:54:15 +0000 | [diff] [blame] | 1591 | Array, BaseAddrPtr); |
| 1592 | } |
| 1593 | else |
| 1594 | EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()), |
| 1595 | Dtor_Complete, LHS.getAddress()); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 1596 | } else { |
Fariborz Jahanian | 426cc38 | 2009-07-30 17:49:11 +0000 | [diff] [blame] | 1597 | const RecordType *RT = |
| 1598 | DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>(); |
| 1599 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 1600 | if (BaseClassDecl->hasTrivialDestructor()) |
| 1601 | continue; |
Anders Carlsson | 5a0f49e | 2009-09-12 04:26:35 +0000 | [diff] [blame] | 1602 | llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(), |
| 1603 | ClassDecl, BaseClassDecl, |
| 1604 | /*NullCheckValue=*/false); |
Fariborz Jahanian | 426cc38 | 2009-07-30 17:49:11 +0000 | [diff] [blame] | 1605 | EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()), |
Anders Carlsson | de1d26b | 2009-09-14 05:32:02 +0000 | [diff] [blame] | 1606 | DtorType, V); |
Fariborz Jahanian | 426cc38 | 2009-07-30 17:49:11 +0000 | [diff] [blame] | 1607 | } |
| 1608 | } |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 1609 | if (DD->getNumBaseOrMemberDestructions() || DD->isTrivial()) |
| 1610 | return; |
| 1611 | // Case of destructor synthesis with fields and base classes |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1612 | // which have non-trivial destructors. They must be destructed in |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 1613 | // reverse order of their construction. |
| 1614 | llvm::SmallVector<FieldDecl *, 16> DestructedFields; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1615 | |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 1616 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 1617 | FieldEnd = ClassDecl->field_end(); |
| 1618 | Field != FieldEnd; ++Field) { |
| 1619 | QualType FieldType = getContext().getCanonicalType((*Field)->getType()); |
Fariborz Jahanian | f800f6c | 2009-08-20 20:54:15 +0000 | [diff] [blame] | 1620 | if (getContext().getAsConstantArrayType(FieldType)) |
| 1621 | FieldType = getContext().getBaseElementType(FieldType); |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 1622 | if (const RecordType *RT = FieldType->getAs<RecordType>()) { |
| 1623 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 1624 | if (FieldClassDecl->hasTrivialDestructor()) |
| 1625 | continue; |
| 1626 | DestructedFields.push_back(*Field); |
| 1627 | } |
| 1628 | } |
| 1629 | if (!DestructedFields.empty()) |
| 1630 | for (int i = DestructedFields.size() -1; i >= 0; --i) { |
| 1631 | FieldDecl *Field = DestructedFields[i]; |
| 1632 | QualType FieldType = Field->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1633 | const ConstantArrayType *Array = |
Fariborz Jahanian | f800f6c | 2009-08-20 20:54:15 +0000 | [diff] [blame] | 1634 | getContext().getAsConstantArrayType(FieldType); |
| 1635 | if (Array) |
| 1636 | FieldType = getContext().getBaseElementType(FieldType); |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 1637 | const RecordType *RT = FieldType->getAs<RecordType>(); |
| 1638 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 1639 | llvm::Value *LoadOfThis = LoadCXXThis(); |
| 1640 | LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0); |
Fariborz Jahanian | f800f6c | 2009-08-20 20:54:15 +0000 | [diff] [blame] | 1641 | if (Array) { |
| 1642 | const llvm::Type *BasePtr = ConvertType(FieldType); |
| 1643 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1644 | llvm::Value *BaseAddrPtr = |
Fariborz Jahanian | f800f6c | 2009-08-20 20:54:15 +0000 | [diff] [blame] | 1645 | Builder.CreateBitCast(LHS.getAddress(), BasePtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1646 | EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()), |
Fariborz Jahanian | f800f6c | 2009-08-20 20:54:15 +0000 | [diff] [blame] | 1647 | Array, BaseAddrPtr); |
| 1648 | } |
| 1649 | else |
| 1650 | EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()), |
| 1651 | Dtor_Complete, LHS.getAddress()); |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 1652 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1653 | |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 1654 | llvm::SmallVector<CXXRecordDecl*, 4> DestructedBases; |
| 1655 | for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(); |
| 1656 | Base != ClassDecl->bases_end(); ++Base) { |
| 1657 | // FIXME. copy assignment of virtual base NYI |
| 1658 | if (Base->isVirtual()) |
| 1659 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1660 | |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 1661 | CXXRecordDecl *BaseClassDecl |
| 1662 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 1663 | if (BaseClassDecl->hasTrivialDestructor()) |
| 1664 | continue; |
| 1665 | DestructedBases.push_back(BaseClassDecl); |
| 1666 | } |
| 1667 | if (DestructedBases.empty()) |
| 1668 | return; |
| 1669 | for (int i = DestructedBases.size() -1; i >= 0; --i) { |
| 1670 | CXXRecordDecl *BaseClassDecl = DestructedBases[i]; |
Anders Carlsson | 5a0f49e | 2009-09-12 04:26:35 +0000 | [diff] [blame] | 1671 | llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(), |
| 1672 | ClassDecl,BaseClassDecl, |
| 1673 | /*NullCheckValue=*/false); |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 1674 | EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()), |
| 1675 | Dtor_Complete, V); |
| 1676 | } |
Fariborz Jahanian | 426cc38 | 2009-07-30 17:49:11 +0000 | [diff] [blame] | 1677 | } |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 1678 | |
Anders Carlsson | de1d26b | 2009-09-14 05:32:02 +0000 | [diff] [blame] | 1679 | void CodeGenFunction::SynthesizeDefaultDestructor(const CXXDestructorDecl *Dtor, |
| 1680 | CXXDtorType DtorType, |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 1681 | llvm::Function *Fn, |
| 1682 | const FunctionArgList &Args) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1683 | |
Anders Carlsson | 0ff8baf | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 1684 | const CXXRecordDecl *ClassDecl = Dtor->getParent(); |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 1685 | assert(!ClassDecl->hasUserDeclaredDestructor() && |
| 1686 | "SynthesizeDefaultDestructor - destructor has user declaration"); |
| 1687 | (void) ClassDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1688 | |
Anders Carlsson | de1d26b | 2009-09-14 05:32:02 +0000 | [diff] [blame] | 1689 | StartFunction(GlobalDecl(Dtor, DtorType), Dtor->getResultType(), Fn, Args, |
| 1690 | SourceLocation()); |
| 1691 | EmitDtorEpilogue(Dtor, DtorType); |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 1692 | FinishFunction(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1693 | } |
Anders Carlsson | 6815e94 | 2009-09-27 18:58:34 +0000 | [diff] [blame] | 1694 | |
| 1695 | // FIXME: Move this to CGCXXStmt.cpp |
| 1696 | void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) { |
| 1697 | // FIXME: We need to do more here. |
| 1698 | EmitStmt(S.getTryBlock()); |
| 1699 | } |