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