Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 1 | //===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code dealing with C++ code generation. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 14 | // We might split this into multiple files if it gets too unwieldy |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 15 | |
| 16 | #include "CodeGenFunction.h" |
| 17 | #include "CodeGenModule.h" |
Anders Carlsson | 283a062 | 2009-04-13 18:03:33 +0000 | [diff] [blame] | 18 | #include "Mangle.h" |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
Fariborz Jahanian | 742cd1b | 2009-07-25 21:12:28 +0000 | [diff] [blame] | 20 | #include "clang/AST/RecordLayout.h" |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 21 | #include "clang/AST/Decl.h" |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclCXX.h" |
Anders Carlsson | 86e9644 | 2008-08-23 19:42:54 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 6815e94 | 2009-09-27 18:58:34 +0000 | [diff] [blame] | 24 | #include "clang/AST/StmtCXX.h" |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringExtras.h" |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | using namespace CodeGen; |
| 28 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 29 | void |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 30 | CodeGenFunction::EmitCXXGlobalDtorRegistration(const CXXDestructorDecl *Dtor, |
| 31 | llvm::Constant *DeclPtr) { |
Anders Carlsson | 6815e94 | 2009-09-27 18:58:34 +0000 | [diff] [blame] | 32 | const llvm::Type *Int8PtrTy = |
| 33 | llvm::Type::getInt8Ty(VMContext)->getPointerTo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 34 | |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 35 | std::vector<const llvm::Type *> Params; |
| 36 | Params.push_back(Int8PtrTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 37 | |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 38 | // Get the destructor function type |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 39 | const llvm::Type *DtorFnTy = |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 40 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Params, false); |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 41 | DtorFnTy = llvm::PointerType::getUnqual(DtorFnTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 42 | |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 43 | Params.clear(); |
| 44 | Params.push_back(DtorFnTy); |
| 45 | Params.push_back(Int8PtrTy); |
| 46 | Params.push_back(Int8PtrTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 47 | |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 48 | // Get the __cxa_atexit function type |
| 49 | // extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d ); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 50 | const llvm::FunctionType *AtExitFnTy = |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 51 | llvm::FunctionType::get(ConvertType(getContext().IntTy), Params, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 52 | |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 53 | llvm::Constant *AtExitFn = CGM.CreateRuntimeFunction(AtExitFnTy, |
| 54 | "__cxa_atexit"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 55 | |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 56 | llvm::Constant *Handle = CGM.CreateRuntimeVariable(Int8PtrTy, |
| 57 | "__dso_handle"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 58 | |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 59 | llvm::Constant *DtorFn = CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 60 | |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 61 | llvm::Value *Args[3] = { llvm::ConstantExpr::getBitCast(DtorFn, DtorFnTy), |
| 62 | llvm::ConstantExpr::getBitCast(DeclPtr, Int8PtrTy), |
| 63 | llvm::ConstantExpr::getBitCast(Handle, Int8PtrTy) }; |
| 64 | Builder.CreateCall(AtExitFn, &Args[0], llvm::array_endof(Args)); |
| 65 | } |
| 66 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 67 | void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D, |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 68 | llvm::Constant *DeclPtr) { |
| 69 | assert(D.hasGlobalStorage() && |
| 70 | "VarDecl must have global storage!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 71 | |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 72 | const Expr *Init = D.getInit(); |
| 73 | QualType T = D.getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 74 | |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 75 | if (T->isReferenceType()) { |
Anders Carlsson | 622f9dc | 2009-08-17 18:24:57 +0000 | [diff] [blame] | 76 | ErrorUnsupported(Init, "global variable that binds to a reference"); |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 77 | } else if (!hasAggregateLLVMType(T)) { |
| 78 | llvm::Value *V = EmitScalarExpr(Init); |
| 79 | EmitStoreOfScalar(V, DeclPtr, T.isVolatileQualified(), T); |
| 80 | } else if (T->isAnyComplexType()) { |
| 81 | EmitComplexExprIntoAddr(Init, DeclPtr, T.isVolatileQualified()); |
| 82 | } else { |
| 83 | EmitAggExpr(Init, DeclPtr, T.isVolatileQualified()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 84 | |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 85 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 86 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 87 | if (!RD->hasTrivialDestructor()) |
| 88 | EmitCXXGlobalDtorRegistration(RD->getDestructor(getContext()), DeclPtr); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
Anders Carlsson | 89ed31d | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 93 | void |
| 94 | CodeGenModule::EmitCXXGlobalInitFunc() { |
| 95 | if (CXXGlobalInits.empty()) |
| 96 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 98 | const llvm::FunctionType *FTy = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
Anders Carlsson | 89ed31d | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 99 | false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 100 | |
Anders Carlsson | 89ed31d | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 101 | // Create our global initialization function. |
| 102 | // FIXME: Should this be tweakable by targets? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 103 | llvm::Function *Fn = |
Anders Carlsson | 89ed31d | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 104 | llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage, |
| 105 | "__cxx_global_initialization", &TheModule); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | |
Anders Carlsson | 89ed31d | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 107 | CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, |
Benjamin Kramer | 10c40ee | 2009-08-08 23:43:26 +0000 | [diff] [blame] | 108 | &CXXGlobalInits[0], |
Anders Carlsson | 89ed31d | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 109 | CXXGlobalInits.size()); |
| 110 | AddGlobalCtor(Fn); |
| 111 | } |
| 112 | |
| 113 | void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn, |
| 114 | const VarDecl **Decls, |
| 115 | unsigned NumDecls) { |
Anders Carlsson | 0ff8baf | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 116 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(), |
Anders Carlsson | 89ed31d | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 117 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 118 | |
Anders Carlsson | 89ed31d | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 119 | for (unsigned i = 0; i != NumDecls; ++i) { |
| 120 | const VarDecl *D = Decls[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | |
Anders Carlsson | 89ed31d | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 122 | llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D); |
| 123 | EmitCXXGlobalVarDeclInit(*D, DeclPtr); |
| 124 | } |
| 125 | FinishFunction(); |
| 126 | } |
| 127 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 128 | void |
| 129 | CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D, |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 130 | llvm::GlobalVariable *GV) { |
Daniel Dunbar | 0096acf | 2009-02-25 19:24:29 +0000 | [diff] [blame] | 131 | // FIXME: This should use __cxa_guard_{acquire,release}? |
| 132 | |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 133 | assert(!getContext().getLangOptions().ThreadsafeStatics && |
| 134 | "thread safe statics are currently not supported!"); |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 135 | |
Anders Carlsson | 283a062 | 2009-04-13 18:03:33 +0000 | [diff] [blame] | 136 | llvm::SmallString<256> GuardVName; |
| 137 | llvm::raw_svector_ostream GuardVOut(GuardVName); |
Anders Carlsson | b540491 | 2009-10-07 01:06:45 +0000 | [diff] [blame] | 138 | mangleGuardVariable(CGM.getMangleContext(), &D, GuardVOut); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 139 | |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 140 | // Create the guard variable. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 141 | llvm::GlobalValue *GuardV = |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 142 | new llvm::GlobalVariable(CGM.getModule(), llvm::Type::getInt64Ty(VMContext), false, |
Daniel Dunbar | 0096acf | 2009-02-25 19:24:29 +0000 | [diff] [blame] | 143 | GV->getLinkage(), |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 144 | llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)), |
Daniel Dunbar | 7765934 | 2009-08-19 20:04:03 +0000 | [diff] [blame] | 145 | GuardVName.str()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 146 | |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 147 | // Load the first byte of the guard variable. |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 148 | const llvm::Type *PtrTy = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 149 | llvm::Value *V = Builder.CreateLoad(Builder.CreateBitCast(GuardV, PtrTy), |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 150 | "tmp"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 151 | |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 152 | // Compare it against 0. |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 153 | llvm::Value *nullValue = llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext)); |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 154 | llvm::Value *ICmp = Builder.CreateICmpEQ(V, nullValue , "tobool"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 156 | llvm::BasicBlock *InitBlock = createBasicBlock("init"); |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 157 | llvm::BasicBlock *EndBlock = createBasicBlock("init.end"); |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 158 | |
| 159 | // If the guard variable is 0, jump to the initializer code. |
| 160 | Builder.CreateCondBr(ICmp, InitBlock, EndBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 161 | |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 162 | EmitBlock(InitBlock); |
| 163 | |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 164 | EmitCXXGlobalVarDeclInit(D, GV); |
| 165 | |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 166 | Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 1), |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 167 | Builder.CreateBitCast(GuardV, PtrTy)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 168 | |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 169 | EmitBlock(EndBlock); |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 172 | RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD, |
| 173 | llvm::Value *Callee, |
| 174 | llvm::Value *This, |
| 175 | CallExpr::const_arg_iterator ArgBeg, |
| 176 | CallExpr::const_arg_iterator ArgEnd) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 177 | assert(MD->isInstance() && |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 178 | "Trying to emit a member call expr on a static method!"); |
| 179 | |
Douglas Gregor | 4fe95f9 | 2009-09-04 19:04:08 +0000 | [diff] [blame] | 180 | // A call to a trivial destructor requires no code generation. |
| 181 | if (const CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(MD)) |
| 182 | if (Destructor->isTrivial()) |
| 183 | return RValue::get(0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 184 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 185 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 186 | |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 187 | CallArgList Args; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 188 | |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 189 | // Push the this ptr. |
| 190 | Args.push_back(std::make_pair(RValue::get(This), |
| 191 | MD->getThisType(getContext()))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 192 | |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 193 | // And the rest of the call args |
| 194 | EmitCallArgs(Args, FPT, ArgBeg, ArgEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 196 | QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType(); |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 197 | return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args), |
| 198 | Callee, Args, MD); |
| 199 | } |
| 200 | |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 201 | RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE) { |
Anders Carlsson | 375c31c | 2009-10-03 19:43:08 +0000 | [diff] [blame] | 202 | if (isa<BinaryOperator>(CE->getCallee())) |
| 203 | return EmitCXXMemberPointerCallExpr(CE); |
| 204 | |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 205 | const MemberExpr *ME = cast<MemberExpr>(CE->getCallee()); |
| 206 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl()); |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 207 | |
Anders Carlsson | 2472bf0 | 2009-09-29 03:54:11 +0000 | [diff] [blame] | 208 | if (MD->isStatic()) { |
| 209 | // The method is static, emit it as we would a regular call. |
| 210 | llvm::Value *Callee = CGM.GetAddrOfFunction(MD); |
| 211 | return EmitCall(Callee, getContext().getPointerType(MD->getType()), |
| 212 | CE->arg_begin(), CE->arg_end(), 0); |
| 213 | |
| 214 | } |
| 215 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 216 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
Mike Stump | 7116da1 | 2009-07-30 21:47:44 +0000 | [diff] [blame] | 217 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 218 | const llvm::Type *Ty = |
| 219 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), |
Anders Carlsson | e9918d2 | 2009-04-08 20:31:57 +0000 | [diff] [blame] | 220 | FPT->isVariadic()); |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 221 | llvm::Value *This; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 222 | |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 223 | if (ME->isArrow()) |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 224 | This = EmitScalarExpr(ME->getBase()); |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 225 | else { |
| 226 | LValue BaseLV = EmitLValue(ME->getBase()); |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 227 | This = BaseLV.getAddress(); |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 228 | } |
Mike Stump | f0070db | 2009-08-26 20:46:33 +0000 | [diff] [blame] | 229 | |
Douglas Gregor | bd4c4ae | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 230 | // C++ [class.virtual]p12: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 231 | // Explicit qualification with the scope operator (5.1) suppresses the |
Douglas Gregor | bd4c4ae | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 232 | // virtual call mechanism. |
Mike Stump | f0070db | 2009-08-26 20:46:33 +0000 | [diff] [blame] | 233 | llvm::Value *Callee; |
Douglas Gregor | 0979c80 | 2009-08-31 21:41:48 +0000 | [diff] [blame] | 234 | if (MD->isVirtual() && !ME->hasQualifier()) |
Mike Stump | 740256b | 2009-09-29 00:50:50 +0000 | [diff] [blame] | 235 | // FIXME: push getCanonicalDecl as a conversion using the static type system (CanCXXMethodDecl). |
| 236 | Callee = BuildVirtualCall(MD->getCanonicalDecl(), This, Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 237 | else if (const CXXDestructorDecl *Destructor |
Douglas Gregor | 4fe95f9 | 2009-09-04 19:04:08 +0000 | [diff] [blame] | 238 | = dyn_cast<CXXDestructorDecl>(MD)) |
| 239 | Callee = CGM.GetAddrOfFunction(GlobalDecl(Destructor, Dtor_Complete), Ty); |
Douglas Gregor | 0979c80 | 2009-08-31 21:41:48 +0000 | [diff] [blame] | 240 | else |
Anders Carlsson | 555b4bb | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 241 | Callee = CGM.GetAddrOfFunction(MD, Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 242 | |
| 243 | return EmitCXXMemberCall(MD, Callee, This, |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 244 | CE->arg_begin(), CE->arg_end()); |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 245 | } |
Anders Carlsson | 5f4307b | 2009-04-14 16:58:56 +0000 | [diff] [blame] | 246 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 247 | RValue |
Anders Carlsson | 375c31c | 2009-10-03 19:43:08 +0000 | [diff] [blame] | 248 | CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E) { |
| 249 | const BinaryOperator *BO = cast<BinaryOperator>(E->getCallee()); |
| 250 | const DeclRefExpr *BaseExpr = cast<DeclRefExpr>(BO->getLHS()); |
| 251 | const DeclRefExpr *MemFn = cast<DeclRefExpr>(BO->getRHS()); |
| 252 | |
| 253 | const MemberPointerType *MPT = MemFn->getType()->getAs<MemberPointerType>(); |
| 254 | const FunctionProtoType *FPT = |
| 255 | MPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 256 | const CXXRecordDecl *RD = |
| 257 | cast<CXXRecordDecl>(cast<RecordType>(MPT->getClass())->getDecl()); |
| 258 | |
| 259 | const llvm::FunctionType *FTy = |
| 260 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT), |
| 261 | FPT->isVariadic()); |
| 262 | |
| 263 | const llvm::Type *Int8PtrTy = |
| 264 | llvm::Type::getInt8Ty(VMContext)->getPointerTo(); |
| 265 | |
| 266 | // Get the member function pointer. |
| 267 | llvm::Value *MemFnPtr = |
| 268 | CreateTempAlloca(ConvertType(MemFn->getType()), "mem.fn"); |
| 269 | EmitAggExpr(MemFn, MemFnPtr, /*VolatileDest=*/false); |
| 270 | |
| 271 | // Emit the 'this' pointer. |
| 272 | llvm::Value *This; |
| 273 | |
| 274 | if (BO->getOpcode() == BinaryOperator::PtrMemI) |
| 275 | This = EmitScalarExpr(BaseExpr); |
| 276 | else |
| 277 | This = EmitLValue(BaseExpr).getAddress(); |
| 278 | |
| 279 | // Adjust it. |
| 280 | llvm::Value *Adj = Builder.CreateStructGEP(MemFnPtr, 1); |
| 281 | Adj = Builder.CreateLoad(Adj, "mem.fn.adj"); |
| 282 | |
| 283 | llvm::Value *Ptr = Builder.CreateBitCast(This, Int8PtrTy, "ptr"); |
| 284 | Ptr = Builder.CreateGEP(Ptr, Adj, "adj"); |
| 285 | |
| 286 | This = Builder.CreateBitCast(Ptr, This->getType(), "this"); |
| 287 | |
| 288 | llvm::Value *FnPtr = Builder.CreateStructGEP(MemFnPtr, 0, "mem.fn.ptr"); |
| 289 | |
| 290 | const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType()); |
| 291 | |
| 292 | llvm::Value *FnAsInt = Builder.CreateLoad(FnPtr, "fn"); |
| 293 | |
| 294 | // If the LSB in the function pointer is 1, the function pointer points to |
| 295 | // a virtual function. |
| 296 | llvm::Value *IsVirtual |
| 297 | = Builder.CreateAnd(FnAsInt, llvm::ConstantInt::get(PtrDiffTy, 1), |
| 298 | "and"); |
| 299 | |
| 300 | IsVirtual = Builder.CreateTrunc(IsVirtual, |
| 301 | llvm::Type::getInt1Ty(VMContext)); |
| 302 | |
| 303 | llvm::BasicBlock *FnVirtual = createBasicBlock("fn.virtual"); |
| 304 | llvm::BasicBlock *FnNonVirtual = createBasicBlock("fn.nonvirtual"); |
| 305 | llvm::BasicBlock *FnEnd = createBasicBlock("fn.end"); |
| 306 | |
| 307 | Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual); |
| 308 | EmitBlock(FnVirtual); |
| 309 | |
| 310 | const llvm::Type *VTableTy = |
| 311 | FTy->getPointerTo()->getPointerTo()->getPointerTo(); |
| 312 | |
| 313 | llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy); |
| 314 | VTable = Builder.CreateLoad(VTable); |
| 315 | |
| 316 | VTable = Builder.CreateGEP(VTable, FnAsInt, "fn"); |
| 317 | |
| 318 | // Since the function pointer is 1 plus the virtual table offset, we |
| 319 | // subtract 1 by using a GEP. |
Mike Stump | 25bc275 | 2009-10-09 01:25:47 +0000 | [diff] [blame^] | 320 | VTable = Builder.CreateConstGEP1_64(VTable, (uint64_t)-1); |
Anders Carlsson | 375c31c | 2009-10-03 19:43:08 +0000 | [diff] [blame] | 321 | |
| 322 | llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "virtualfn"); |
| 323 | |
| 324 | EmitBranch(FnEnd); |
| 325 | EmitBlock(FnNonVirtual); |
| 326 | |
| 327 | // If the function is not virtual, just load the pointer. |
| 328 | llvm::Value *NonVirtualFn = Builder.CreateLoad(FnPtr, "fn"); |
| 329 | NonVirtualFn = Builder.CreateIntToPtr(NonVirtualFn, FTy->getPointerTo()); |
| 330 | |
| 331 | EmitBlock(FnEnd); |
| 332 | |
| 333 | llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo()); |
| 334 | Callee->reserveOperandSpace(2); |
| 335 | Callee->addIncoming(VirtualFn, FnVirtual); |
| 336 | Callee->addIncoming(NonVirtualFn, FnNonVirtual); |
| 337 | |
| 338 | CallArgList Args; |
| 339 | |
| 340 | QualType ThisType = |
| 341 | getContext().getPointerType(getContext().getTagDeclType(RD)); |
| 342 | |
| 343 | // Push the this ptr. |
| 344 | Args.push_back(std::make_pair(RValue::get(This), ThisType)); |
| 345 | |
| 346 | // And the rest of the call args |
| 347 | EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end()); |
| 348 | QualType ResultType = BO->getType()->getAs<FunctionType>()->getResultType(); |
| 349 | return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args), |
| 350 | Callee, Args, 0); |
| 351 | } |
| 352 | |
| 353 | RValue |
Anders Carlsson | 0f29463 | 2009-05-27 04:18:27 +0000 | [diff] [blame] | 354 | CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E, |
| 355 | const CXXMethodDecl *MD) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 356 | assert(MD->isInstance() && |
Anders Carlsson | 0f29463 | 2009-05-27 04:18:27 +0000 | [diff] [blame] | 357 | "Trying to emit a member call expr on a static method!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 358 | |
Fariborz Jahanian | ad25883 | 2009-08-13 21:09:41 +0000 | [diff] [blame] | 359 | if (MD->isCopyAssignment()) { |
| 360 | const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext()); |
| 361 | if (ClassDecl->hasTrivialCopyAssignment()) { |
| 362 | assert(!ClassDecl->hasUserDeclaredCopyAssignment() && |
| 363 | "EmitCXXOperatorMemberCallExpr - user declared copy assignment"); |
| 364 | llvm::Value *This = EmitLValue(E->getArg(0)).getAddress(); |
| 365 | llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress(); |
| 366 | QualType Ty = E->getType(); |
| 367 | EmitAggregateCopy(This, Src, Ty); |
| 368 | return RValue::get(This); |
| 369 | } |
| 370 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 371 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 372 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 373 | const llvm::Type *Ty = |
| 374 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), |
Mike Stump | ed032eb | 2009-09-04 18:27:16 +0000 | [diff] [blame] | 375 | FPT->isVariadic()); |
Anders Carlsson | 555b4bb | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 376 | llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 377 | |
Anders Carlsson | 0f29463 | 2009-05-27 04:18:27 +0000 | [diff] [blame] | 378 | llvm::Value *This = EmitLValue(E->getArg(0)).getAddress(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 379 | |
Anders Carlsson | 0f29463 | 2009-05-27 04:18:27 +0000 | [diff] [blame] | 380 | return EmitCXXMemberCall(MD, Callee, This, |
| 381 | E->arg_begin() + 1, E->arg_end()); |
| 382 | } |
| 383 | |
Anders Carlsson | 5f4307b | 2009-04-14 16:58:56 +0000 | [diff] [blame] | 384 | llvm::Value *CodeGenFunction::LoadCXXThis() { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 385 | assert(isa<CXXMethodDecl>(CurFuncDecl) && |
Anders Carlsson | 5f4307b | 2009-04-14 16:58:56 +0000 | [diff] [blame] | 386 | "Must be in a C++ member function decl to load 'this'"); |
| 387 | assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() && |
| 388 | "Must be in a C++ member function decl to load 'this'"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 389 | |
Anders Carlsson | 5f4307b | 2009-04-14 16:58:56 +0000 | [diff] [blame] | 390 | // FIXME: What if we're inside a block? |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 391 | // ans: See how CodeGenFunction::LoadObjCSelf() uses |
| 392 | // CodeGenFunction::BlockForwardSelf() for how to do this. |
Anders Carlsson | 5f4307b | 2009-04-14 16:58:56 +0000 | [diff] [blame] | 393 | return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this"); |
| 394 | } |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 395 | |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 396 | /// EmitCXXAggrConstructorCall - This routine essentially creates a (nested) |
| 397 | /// for-loop to call the default constructor on individual members of the |
Anders Carlsson | 569c1f4 | 2009-09-23 02:45:36 +0000 | [diff] [blame] | 398 | /// array. |
| 399 | /// 'D' is the default constructor for elements of the array, 'ArrayTy' is the |
| 400 | /// array type and 'ArrayPtr' points to the beginning fo the array. |
| 401 | /// 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] | 402 | void |
| 403 | CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, |
Anders Carlsson | 569c1f4 | 2009-09-23 02:45:36 +0000 | [diff] [blame] | 404 | const ConstantArrayType *ArrayTy, |
| 405 | llvm::Value *ArrayPtr) { |
| 406 | const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
| 407 | llvm::Value * NumElements = |
| 408 | llvm::ConstantInt::get(SizeTy, |
| 409 | getContext().getConstantArrayElementCount(ArrayTy)); |
| 410 | |
| 411 | EmitCXXAggrConstructorCall(D, NumElements, ArrayPtr); |
| 412 | } |
| 413 | |
| 414 | void |
| 415 | CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, |
| 416 | llvm::Value *NumElements, |
| 417 | llvm::Value *ArrayPtr) { |
| 418 | const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 419 | |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 420 | // Create a temporary for the loop index and initialize it with 0. |
Anders Carlsson | 569c1f4 | 2009-09-23 02:45:36 +0000 | [diff] [blame] | 421 | llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index"); |
| 422 | llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy); |
| 423 | Builder.CreateStore(Zero, IndexPtr, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 424 | |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 425 | // Start the loop with a block that tests the condition. |
| 426 | llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); |
| 427 | llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 428 | |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 429 | EmitBlock(CondBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 430 | |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 431 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 432 | |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 433 | // Generate: if (loop-index < number-of-elements fall to the loop body, |
| 434 | // otherwise, go to the block after the for-loop. |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 435 | llvm::Value *Counter = Builder.CreateLoad(IndexPtr); |
Anders Carlsson | 569c1f4 | 2009-09-23 02:45:36 +0000 | [diff] [blame] | 436 | llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless"); |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 437 | // If the condition is true, execute the body. |
| 438 | Builder.CreateCondBr(IsLess, ForBody, AfterFor); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 439 | |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 440 | EmitBlock(ForBody); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 441 | |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 442 | llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 443 | // Inside the loop body, emit the constructor call on the array element. |
Fariborz Jahanian | 995d281 | 2009-08-20 01:01:06 +0000 | [diff] [blame] | 444 | Counter = Builder.CreateLoad(IndexPtr); |
Anders Carlsson | 569c1f4 | 2009-09-23 02:45:36 +0000 | [diff] [blame] | 445 | llvm::Value *Address = Builder.CreateInBoundsGEP(ArrayPtr, Counter, |
| 446 | "arrayidx"); |
Fariborz Jahanian | 4f68d53 | 2009-08-26 00:23:27 +0000 | [diff] [blame] | 447 | EmitCXXConstructorCall(D, Ctor_Complete, Address, 0, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 448 | |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 449 | EmitBlock(ContinueBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 450 | |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 451 | // Emit the increment of the loop counter. |
Anders Carlsson | 569c1f4 | 2009-09-23 02:45:36 +0000 | [diff] [blame] | 452 | llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1); |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 453 | Counter = Builder.CreateLoad(IndexPtr); |
| 454 | NextVal = Builder.CreateAdd(Counter, NextVal, "inc"); |
| 455 | Builder.CreateStore(NextVal, IndexPtr, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 456 | |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 457 | // Finally, branch back up to the condition for the next iteration. |
| 458 | EmitBranch(CondBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 459 | |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 460 | // Emit the fall-through block. |
| 461 | EmitBlock(AfterFor, true); |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Fariborz Jahanian | 1c536bf | 2009-08-20 23:02:58 +0000 | [diff] [blame] | 464 | /// EmitCXXAggrDestructorCall - calls the default destructor on array |
| 465 | /// elements in reverse order of construction. |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 466 | void |
Fariborz Jahanian | f800f6c | 2009-08-20 20:54:15 +0000 | [diff] [blame] | 467 | CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D, |
| 468 | const ArrayType *Array, |
| 469 | llvm::Value *This) { |
Fariborz Jahanian | 1c536bf | 2009-08-20 23:02:58 +0000 | [diff] [blame] | 470 | const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array); |
| 471 | assert(CA && "Do we support VLA for destruction ?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 472 | llvm::Value *One = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), |
Fariborz Jahanian | 1c536bf | 2009-08-20 23:02:58 +0000 | [diff] [blame] | 473 | 1); |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 474 | uint64_t ElementCount = getContext().getConstantArrayElementCount(CA); |
Fariborz Jahanian | 1c536bf | 2009-08-20 23:02:58 +0000 | [diff] [blame] | 475 | // Create a temporary for the loop index and initialize it with count of |
| 476 | // array elements. |
| 477 | llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext), |
| 478 | "loop.index"); |
| 479 | // Index = ElementCount; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 480 | llvm::Value* UpperCount = |
Fariborz Jahanian | 1c536bf | 2009-08-20 23:02:58 +0000 | [diff] [blame] | 481 | llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), ElementCount); |
| 482 | Builder.CreateStore(UpperCount, IndexPtr, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 483 | |
Fariborz Jahanian | 1c536bf | 2009-08-20 23:02:58 +0000 | [diff] [blame] | 484 | // Start the loop with a block that tests the condition. |
| 485 | llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); |
| 486 | llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 487 | |
Fariborz Jahanian | 1c536bf | 2009-08-20 23:02:58 +0000 | [diff] [blame] | 488 | EmitBlock(CondBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 489 | |
Fariborz Jahanian | 1c536bf | 2009-08-20 23:02:58 +0000 | [diff] [blame] | 490 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 491 | |
Fariborz Jahanian | 1c536bf | 2009-08-20 23:02:58 +0000 | [diff] [blame] | 492 | // Generate: if (loop-index != 0 fall to the loop body, |
| 493 | // otherwise, go to the block after the for-loop. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 494 | llvm::Value* zeroConstant = |
Fariborz Jahanian | 1c536bf | 2009-08-20 23:02:58 +0000 | [diff] [blame] | 495 | llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)); |
| 496 | llvm::Value *Counter = Builder.CreateLoad(IndexPtr); |
| 497 | llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant, |
| 498 | "isne"); |
| 499 | // If the condition is true, execute the body. |
| 500 | Builder.CreateCondBr(IsNE, ForBody, AfterFor); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 501 | |
Fariborz Jahanian | 1c536bf | 2009-08-20 23:02:58 +0000 | [diff] [blame] | 502 | EmitBlock(ForBody); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 503 | |
Fariborz Jahanian | 1c536bf | 2009-08-20 23:02:58 +0000 | [diff] [blame] | 504 | llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); |
| 505 | // Inside the loop body, emit the constructor call on the array element. |
| 506 | Counter = Builder.CreateLoad(IndexPtr); |
| 507 | Counter = Builder.CreateSub(Counter, One); |
| 508 | llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx"); |
| 509 | EmitCXXDestructorCall(D, Dtor_Complete, Address); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 510 | |
Fariborz Jahanian | 1c536bf | 2009-08-20 23:02:58 +0000 | [diff] [blame] | 511 | EmitBlock(ContinueBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 512 | |
Fariborz Jahanian | 1c536bf | 2009-08-20 23:02:58 +0000 | [diff] [blame] | 513 | // Emit the decrement of the loop counter. |
| 514 | Counter = Builder.CreateLoad(IndexPtr); |
| 515 | Counter = Builder.CreateSub(Counter, One, "dec"); |
| 516 | Builder.CreateStore(Counter, IndexPtr, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 517 | |
Fariborz Jahanian | 1c536bf | 2009-08-20 23:02:58 +0000 | [diff] [blame] | 518 | // Finally, branch back up to the condition for the next iteration. |
| 519 | EmitBranch(CondBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 520 | |
Fariborz Jahanian | 1c536bf | 2009-08-20 23:02:58 +0000 | [diff] [blame] | 521 | // Emit the fall-through block. |
| 522 | EmitBlock(AfterFor, true); |
Fariborz Jahanian | f800f6c | 2009-08-20 20:54:15 +0000 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 526 | CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D, |
| 527 | CXXCtorType Type, |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 528 | llvm::Value *This, |
| 529 | CallExpr::const_arg_iterator ArgBeg, |
| 530 | CallExpr::const_arg_iterator ArgEnd) { |
Fariborz Jahanian | 343a3cf | 2009-08-14 20:11:43 +0000 | [diff] [blame] | 531 | if (D->isCopyConstructor(getContext())) { |
| 532 | const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D->getDeclContext()); |
| 533 | if (ClassDecl->hasTrivialCopyConstructor()) { |
| 534 | assert(!ClassDecl->hasUserDeclaredCopyConstructor() && |
| 535 | "EmitCXXConstructorCall - user declared copy constructor"); |
| 536 | const Expr *E = (*ArgBeg); |
| 537 | QualType Ty = E->getType(); |
| 538 | llvm::Value *Src = EmitLValue(E).getAddress(); |
| 539 | EmitAggregateCopy(This, Src, Ty); |
| 540 | return; |
| 541 | } |
| 542 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 543 | |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 544 | llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type); |
| 545 | |
| 546 | EmitCXXMemberCall(D, Callee, This, ArgBeg, ArgEnd); |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 549 | void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *D, |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 550 | CXXDtorType Type, |
| 551 | llvm::Value *This) { |
| 552 | llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(D, Type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 553 | |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 554 | EmitCXXMemberCall(D, Callee, This, 0, 0); |
| 555 | } |
| 556 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 557 | void |
| 558 | CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest, |
Anders Carlsson | 31ccf37 | 2009-05-03 17:47:16 +0000 | [diff] [blame] | 559 | const CXXConstructExpr *E) { |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 560 | assert(Dest && "Must have a destination!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 561 | |
| 562 | const CXXRecordDecl *RD = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 563 | cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl()); |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 564 | if (RD->hasTrivialConstructor()) |
| 565 | return; |
Fariborz Jahanian | 6904cbb | 2009-08-06 01:02:49 +0000 | [diff] [blame] | 566 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 567 | // Code gen optimization to eliminate copy constructor and return |
Fariborz Jahanian | 6904cbb | 2009-08-06 01:02:49 +0000 | [diff] [blame] | 568 | // its first argument instead. |
Anders Carlsson | 92f5822 | 2009-08-22 22:30:33 +0000 | [diff] [blame] | 569 | if (getContext().getLangOptions().ElideConstructors && E->isElidable()) { |
Fariborz Jahanian | 6904cbb | 2009-08-06 01:02:49 +0000 | [diff] [blame] | 570 | CXXConstructExpr::const_arg_iterator i = E->arg_begin(); |
Fariborz Jahanian | 1cf9ff8 | 2009-08-06 19:12:38 +0000 | [diff] [blame] | 571 | EmitAggExpr((*i), Dest, false); |
| 572 | return; |
Fariborz Jahanian | 6904cbb | 2009-08-06 01:02:49 +0000 | [diff] [blame] | 573 | } |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 574 | // Call the constructor. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 575 | EmitCXXConstructorCall(E->getConstructor(), Ctor_Complete, Dest, |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 576 | E->arg_begin(), E->arg_end()); |
| 577 | } |
| 578 | |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 579 | void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) { |
Anders Carlsson | 2a131fb | 2009-05-05 04:44:02 +0000 | [diff] [blame] | 580 | EmitGlobal(GlobalDecl(D, Ctor_Complete)); |
| 581 | EmitGlobal(GlobalDecl(D, Ctor_Base)); |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 582 | } |
Anders Carlsson | 363c184 | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 583 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 584 | void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D, |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 585 | CXXCtorType Type) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 586 | |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 587 | llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 588 | |
Anders Carlsson | 0ff8baf | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 589 | CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 590 | |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 591 | SetFunctionDefinitionAttributes(D, Fn); |
| 592 | SetLLVMFunctionAttributesForDefinition(D, Fn); |
| 593 | } |
| 594 | |
Anders Carlsson | 363c184 | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 595 | llvm::Function * |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 596 | CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D, |
Anders Carlsson | 363c184 | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 597 | CXXCtorType Type) { |
| 598 | const llvm::FunctionType *FTy = |
| 599 | getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 600 | |
Anders Carlsson | 363c184 | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 601 | const char *Name = getMangledCXXCtorName(D, Type); |
Chris Lattner | b4880ba | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 602 | return cast<llvm::Function>( |
| 603 | GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type))); |
Anders Carlsson | 363c184 | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 604 | } |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 605 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 606 | const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D, |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 607 | CXXCtorType Type) { |
| 608 | llvm::SmallString<256> Name; |
| 609 | llvm::raw_svector_ostream Out(Name); |
Anders Carlsson | b540491 | 2009-10-07 01:06:45 +0000 | [diff] [blame] | 610 | mangleCXXCtor(getMangleContext(), D, Type, Out); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 611 | |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 612 | Name += '\0'; |
| 613 | return UniqueMangledName(Name.begin(), Name.end()); |
| 614 | } |
| 615 | |
| 616 | void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) { |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 617 | EmitCXXDestructor(D, Dtor_Complete); |
| 618 | EmitCXXDestructor(D, Dtor_Base); |
| 619 | } |
| 620 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 621 | void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D, |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 622 | CXXDtorType Type) { |
| 623 | llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 624 | |
Anders Carlsson | 0ff8baf | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 625 | CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 626 | |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 627 | SetFunctionDefinitionAttributes(D, Fn); |
| 628 | SetLLVMFunctionAttributesForDefinition(D, Fn); |
| 629 | } |
| 630 | |
| 631 | llvm::Function * |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 632 | CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D, |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 633 | CXXDtorType Type) { |
| 634 | const llvm::FunctionType *FTy = |
| 635 | getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 636 | |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 637 | const char *Name = getMangledCXXDtorName(D, Type); |
Chris Lattner | b4880ba | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 638 | return cast<llvm::Function>( |
| 639 | GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type))); |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 642 | const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D, |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 643 | CXXDtorType Type) { |
| 644 | llvm::SmallString<256> Name; |
| 645 | llvm::raw_svector_ostream Out(Name); |
Anders Carlsson | b540491 | 2009-10-07 01:06:45 +0000 | [diff] [blame] | 646 | mangleCXXDtor(getMangleContext(), D, Type, Out); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 647 | |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 648 | Name += '\0'; |
| 649 | return UniqueMangledName(Name.begin(), Name.end()); |
| 650 | } |
Fariborz Jahanian | e7d346b | 2009-07-20 23:18:55 +0000 | [diff] [blame] | 651 | |
Mike Stump | 32f3701 | 2009-08-18 21:49:00 +0000 | [diff] [blame] | 652 | llvm::Constant *CodeGenModule::GenerateRtti(const CXXRecordDecl *RD) { |
Mike Stump | 738f8c2 | 2009-07-31 23:15:31 +0000 | [diff] [blame] | 653 | llvm::Type *Ptr8Ty; |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 654 | Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0); |
Mike Stump | cb1b5d3 | 2009-08-04 20:06:48 +0000 | [diff] [blame] | 655 | llvm::Constant *Rtti = llvm::Constant::getNullValue(Ptr8Ty); |
Mike Stump | 738f8c2 | 2009-07-31 23:15:31 +0000 | [diff] [blame] | 656 | |
| 657 | if (!getContext().getLangOptions().Rtti) |
Mike Stump | cb1b5d3 | 2009-08-04 20:06:48 +0000 | [diff] [blame] | 658 | return Rtti; |
Mike Stump | 738f8c2 | 2009-07-31 23:15:31 +0000 | [diff] [blame] | 659 | |
| 660 | llvm::SmallString<256> OutName; |
| 661 | llvm::raw_svector_ostream Out(OutName); |
| 662 | QualType ClassTy; |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 663 | ClassTy = getContext().getTagDeclType(RD); |
Anders Carlsson | b540491 | 2009-10-07 01:06:45 +0000 | [diff] [blame] | 664 | mangleCXXRtti(getMangleContext(), ClassTy, Out); |
Mike Stump | 738f8c2 | 2009-07-31 23:15:31 +0000 | [diff] [blame] | 665 | llvm::GlobalVariable::LinkageTypes linktype; |
| 666 | linktype = llvm::GlobalValue::WeakAnyLinkage; |
| 667 | std::vector<llvm::Constant *> info; |
Mike Stump | 4ef9809 | 2009-08-13 22:53:07 +0000 | [diff] [blame] | 668 | // assert(0 && "FIXME: implement rtti descriptor"); |
Mike Stump | 738f8c2 | 2009-07-31 23:15:31 +0000 | [diff] [blame] | 669 | // FIXME: descriptor |
| 670 | info.push_back(llvm::Constant::getNullValue(Ptr8Ty)); |
Mike Stump | 4ef9809 | 2009-08-13 22:53:07 +0000 | [diff] [blame] | 671 | // assert(0 && "FIXME: implement rtti ts"); |
Mike Stump | 738f8c2 | 2009-07-31 23:15:31 +0000 | [diff] [blame] | 672 | // FIXME: TS |
| 673 | info.push_back(llvm::Constant::getNullValue(Ptr8Ty)); |
| 674 | |
| 675 | llvm::Constant *C; |
| 676 | llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, info.size()); |
| 677 | C = llvm::ConstantArray::get(type, info); |
Mike Stump | 32f3701 | 2009-08-18 21:49:00 +0000 | [diff] [blame] | 678 | Rtti = new llvm::GlobalVariable(getModule(), type, true, linktype, C, |
Daniel Dunbar | 7765934 | 2009-08-19 20:04:03 +0000 | [diff] [blame] | 679 | Out.str()); |
Mike Stump | cb1b5d3 | 2009-08-04 20:06:48 +0000 | [diff] [blame] | 680 | Rtti = llvm::ConstantExpr::getBitCast(Rtti, Ptr8Ty); |
| 681 | return Rtti; |
Mike Stump | 738f8c2 | 2009-07-31 23:15:31 +0000 | [diff] [blame] | 682 | } |
| 683 | |
Mike Stump | eb7e9c3 | 2009-08-19 18:10:47 +0000 | [diff] [blame] | 684 | class VtableBuilder { |
Mike Stump | f0070db | 2009-08-26 20:46:33 +0000 | [diff] [blame] | 685 | public: |
| 686 | /// Index_t - Vtable index type. |
| 687 | typedef uint64_t Index_t; |
| 688 | private: |
Mike Stump | 7c435fa | 2009-08-18 20:50:28 +0000 | [diff] [blame] | 689 | std::vector<llvm::Constant *> &methods; |
Mike Stump | 15a24e0 | 2009-08-28 23:22:54 +0000 | [diff] [blame] | 690 | std::vector<llvm::Constant *> submethods; |
Mike Stump | 7c435fa | 2009-08-18 20:50:28 +0000 | [diff] [blame] | 691 | llvm::Type *Ptr8Ty; |
Mike Stump | b9871a2 | 2009-08-21 01:45:00 +0000 | [diff] [blame] | 692 | /// Class - The most derived class that this vtable is being built for. |
Mike Stump | 32f3701 | 2009-08-18 21:49:00 +0000 | [diff] [blame] | 693 | const CXXRecordDecl *Class; |
Mike Stump | b9871a2 | 2009-08-21 01:45:00 +0000 | [diff] [blame] | 694 | /// BLayout - Layout for the most derived class that this vtable is being |
| 695 | /// built for. |
Mike Stump | b46c92d | 2009-08-19 02:06:38 +0000 | [diff] [blame] | 696 | const ASTRecordLayout &BLayout; |
Mike Stump | ee560f3 | 2009-08-19 14:40:47 +0000 | [diff] [blame] | 697 | llvm::SmallSet<const CXXRecordDecl *, 32> IndirectPrimary; |
Mike Stump | 7fa0d93 | 2009-08-20 02:11:48 +0000 | [diff] [blame] | 698 | llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase; |
Mike Stump | 32f3701 | 2009-08-18 21:49:00 +0000 | [diff] [blame] | 699 | llvm::Constant *rtti; |
Mike Stump | 7c435fa | 2009-08-18 20:50:28 +0000 | [diff] [blame] | 700 | llvm::LLVMContext &VMContext; |
Mike Stump | 65defe3 | 2009-08-18 21:03:28 +0000 | [diff] [blame] | 701 | CodeGenModule &CGM; // Per-module state. |
Mike Stump | b9871a2 | 2009-08-21 01:45:00 +0000 | [diff] [blame] | 702 | /// Index - Maps a method decl into a vtable index. Useful for virtual |
| 703 | /// dispatch codegen. |
Mike Stump | f0070db | 2009-08-26 20:46:33 +0000 | [diff] [blame] | 704 | llvm::DenseMap<const CXXMethodDecl *, Index_t> Index; |
Mike Stump | 15a24e0 | 2009-08-28 23:22:54 +0000 | [diff] [blame] | 705 | llvm::DenseMap<const CXXMethodDecl *, Index_t> VCall; |
| 706 | llvm::DenseMap<const CXXMethodDecl *, Index_t> VCallOffset; |
Mike Stump | 97f4d46 | 2009-09-18 19:06:35 +0000 | [diff] [blame] | 707 | llvm::DenseMap<const CXXRecordDecl *, Index_t> VBIndex; |
Mike Stump | 6e319f6 | 2009-09-11 23:25:56 +0000 | [diff] [blame] | 708 | typedef std::pair<Index_t, Index_t> CallOffset; |
| 709 | typedef llvm::DenseMap<const CXXMethodDecl *, CallOffset> Thunks_t; |
Mike Stump | 77ca8f6 | 2009-09-05 07:20:32 +0000 | [diff] [blame] | 710 | Thunks_t Thunks; |
Mike Stump | 6e319f6 | 2009-09-11 23:25:56 +0000 | [diff] [blame] | 711 | typedef llvm::DenseMap<const CXXMethodDecl *, |
| 712 | std::pair<CallOffset, CallOffset> > CovariantThunks_t; |
| 713 | CovariantThunks_t CovariantThunks; |
Mike Stump | 15a24e0 | 2009-08-28 23:22:54 +0000 | [diff] [blame] | 714 | std::vector<Index_t> VCalls; |
Mike Stump | 552b275 | 2009-08-18 22:04:08 +0000 | [diff] [blame] | 715 | typedef CXXRecordDecl::method_iterator method_iter; |
Mike Stump | ed032eb | 2009-09-04 18:27:16 +0000 | [diff] [blame] | 716 | // FIXME: Linkage should follow vtable |
| 717 | const bool Extern; |
Mike Stump | 77ca8f6 | 2009-09-05 07:20:32 +0000 | [diff] [blame] | 718 | const uint32_t LLVMPointerWidth; |
| 719 | Index_t extra; |
Mike Stump | 7c435fa | 2009-08-18 20:50:28 +0000 | [diff] [blame] | 720 | public: |
Mike Stump | eb7e9c3 | 2009-08-19 18:10:47 +0000 | [diff] [blame] | 721 | VtableBuilder(std::vector<llvm::Constant *> &meth, |
| 722 | const CXXRecordDecl *c, |
| 723 | CodeGenModule &cgm) |
Mike Stump | b46c92d | 2009-08-19 02:06:38 +0000 | [diff] [blame] | 724 | : methods(meth), Class(c), BLayout(cgm.getContext().getASTRecordLayout(c)), |
| 725 | rtti(cgm.GenerateRtti(c)), VMContext(cgm.getModule().getContext()), |
Mike Stump | 77ca8f6 | 2009-09-05 07:20:32 +0000 | [diff] [blame] | 726 | CGM(cgm), Extern(true), |
| 727 | LLVMPointerWidth(cgm.getContext().Target.getPointerWidth(0)) { |
Mike Stump | 7c435fa | 2009-08-18 20:50:28 +0000 | [diff] [blame] | 728 | Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0); |
| 729 | } |
Mike Stump | 32f3701 | 2009-08-18 21:49:00 +0000 | [diff] [blame] | 730 | |
Mike Stump | f0070db | 2009-08-26 20:46:33 +0000 | [diff] [blame] | 731 | llvm::DenseMap<const CXXMethodDecl *, Index_t> &getIndex() { return Index; } |
Mike Stump | 97f4d46 | 2009-09-18 19:06:35 +0000 | [diff] [blame] | 732 | llvm::DenseMap<const CXXRecordDecl *, Index_t> &getVBIndex() |
| 733 | { return VBIndex; } |
Mike Stump | b46c92d | 2009-08-19 02:06:38 +0000 | [diff] [blame] | 734 | |
Mike Stump | 15a24e0 | 2009-08-28 23:22:54 +0000 | [diff] [blame] | 735 | llvm::Constant *wrap(Index_t i) { |
| 736 | llvm::Constant *m; |
| 737 | m = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), i); |
| 738 | return llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty); |
Mike Stump | b46c92d | 2009-08-19 02:06:38 +0000 | [diff] [blame] | 739 | } |
| 740 | |
Mike Stump | 15a24e0 | 2009-08-28 23:22:54 +0000 | [diff] [blame] | 741 | llvm::Constant *wrap(llvm::Constant *m) { |
| 742 | return llvm::ConstantExpr::getBitCast(m, Ptr8Ty); |
Mike Stump | 80a0e32 | 2009-08-12 23:25:18 +0000 | [diff] [blame] | 743 | } |
Mike Stump | 4c3aedd | 2009-08-12 23:14:12 +0000 | [diff] [blame] | 744 | |
Mike Stump | 7fa0d93 | 2009-08-20 02:11:48 +0000 | [diff] [blame] | 745 | void GenerateVBaseOffsets(std::vector<llvm::Constant *> &offsets, |
Mike Stump | b983744 | 2009-08-20 07:22:17 +0000 | [diff] [blame] | 746 | const CXXRecordDecl *RD, uint64_t Offset) { |
Mike Stump | 97f4d46 | 2009-09-18 19:06:35 +0000 | [diff] [blame] | 747 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
Mike Stump | 7fa0d93 | 2009-08-20 02:11:48 +0000 | [diff] [blame] | 748 | e = RD->bases_end(); i != e; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 749 | const CXXRecordDecl *Base = |
Mike Stump | 7fa0d93 | 2009-08-20 02:11:48 +0000 | [diff] [blame] | 750 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 751 | if (i->isVirtual() && !SeenVBase.count(Base)) { |
| 752 | SeenVBase.insert(Base); |
Mike Stump | b983744 | 2009-08-20 07:22:17 +0000 | [diff] [blame] | 753 | int64_t BaseOffset = -(Offset/8) + BLayout.getVBaseClassOffset(Base)/8; |
Mike Stump | 15a24e0 | 2009-08-28 23:22:54 +0000 | [diff] [blame] | 754 | llvm::Constant *m = wrap(BaseOffset); |
| 755 | m = wrap((0?700:0) + BaseOffset); |
Mike Stump | 97f4d46 | 2009-09-18 19:06:35 +0000 | [diff] [blame] | 756 | VBIndex[Base] = -(offsets.size()*LLVMPointerWidth/8) |
| 757 | - 3*LLVMPointerWidth/8; |
Mike Stump | 7fa0d93 | 2009-08-20 02:11:48 +0000 | [diff] [blame] | 758 | offsets.push_back(m); |
| 759 | } |
Mike Stump | b983744 | 2009-08-20 07:22:17 +0000 | [diff] [blame] | 760 | GenerateVBaseOffsets(offsets, Base, Offset); |
Mike Stump | 7fa0d93 | 2009-08-20 02:11:48 +0000 | [diff] [blame] | 761 | } |
| 762 | } |
| 763 | |
Mike Stump | b9871a2 | 2009-08-21 01:45:00 +0000 | [diff] [blame] | 764 | void StartNewTable() { |
| 765 | SeenVBase.clear(); |
| 766 | } |
Mike Stump | bc16aea | 2009-08-12 23:00:59 +0000 | [diff] [blame] | 767 | |
Mike Stump | 97f4d46 | 2009-09-18 19:06:35 +0000 | [diff] [blame] | 768 | Index_t VBlookup(CXXRecordDecl *D, CXXRecordDecl *B); |
| 769 | |
| 770 | /// getVbaseOffset - Returns the index into the vtable for the virtual base |
| 771 | /// offset for the given (B) virtual base of the derived class D. |
| 772 | Index_t getVbaseOffset(QualType qB, QualType qD) { |
| 773 | qD = qD->getAs<PointerType>()->getPointeeType(); |
| 774 | qB = qB->getAs<PointerType>()->getPointeeType(); |
| 775 | CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl()); |
| 776 | CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl()); |
| 777 | if (D != Class) |
| 778 | return VBlookup(D, B); |
| 779 | llvm::DenseMap<const CXXRecordDecl *, Index_t>::iterator i; |
| 780 | i = VBIndex.find(B); |
| 781 | if (i != VBIndex.end()) |
| 782 | return i->second; |
| 783 | // FIXME: temporal botch, is this data here, by the time we need it? |
| 784 | |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 785 | assert(false && "FIXME: Locate the containing virtual base first"); |
| 786 | return 0; |
Mike Stump | 97f4d46 | 2009-09-18 19:06:35 +0000 | [diff] [blame] | 787 | } |
| 788 | |
Mike Stump | 35191b6 | 2009-09-01 22:20:28 +0000 | [diff] [blame] | 789 | bool OverrideMethod(const CXXMethodDecl *MD, llvm::Constant *m, |
Mike Stump | dec025b | 2009-09-07 04:27:52 +0000 | [diff] [blame] | 790 | bool MorallyVirtual, Index_t Offset) { |
Mike Stump | b9871a2 | 2009-08-21 01:45:00 +0000 | [diff] [blame] | 791 | typedef CXXMethodDecl::method_iterator meth_iter; |
| 792 | |
Mike Stump | b9871a2 | 2009-08-21 01:45:00 +0000 | [diff] [blame] | 793 | // FIXME: Don't like the nested loops. For very large inheritance |
| 794 | // heirarchies we could have a table on the side with the final overridder |
| 795 | // and just replace each instance of an overridden method once. Would be |
| 796 | // nice to measure the cost/benefit on real code. |
| 797 | |
Mike Stump | b9871a2 | 2009-08-21 01:45:00 +0000 | [diff] [blame] | 798 | for (meth_iter mi = MD->begin_overridden_methods(), |
| 799 | e = MD->end_overridden_methods(); |
| 800 | mi != e; ++mi) { |
| 801 | const CXXMethodDecl *OMD = *mi; |
| 802 | llvm::Constant *om; |
Anders Carlsson | 555b4bb | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 803 | om = CGM.GetAddrOfFunction(OMD, Ptr8Ty); |
Mike Stump | b9871a2 | 2009-08-21 01:45:00 +0000 | [diff] [blame] | 804 | om = llvm::ConstantExpr::getBitCast(om, Ptr8Ty); |
| 805 | |
Mike Stump | dec025b | 2009-09-07 04:27:52 +0000 | [diff] [blame] | 806 | for (Index_t i = 0, e = submethods.size(); |
Mike Stump | f0070db | 2009-08-26 20:46:33 +0000 | [diff] [blame] | 807 | i != e; ++i) { |
Mike Stump | b9871a2 | 2009-08-21 01:45:00 +0000 | [diff] [blame] | 808 | // FIXME: begin_overridden_methods might be too lax, covariance */ |
Mike Stump | 77ca8f6 | 2009-09-05 07:20:32 +0000 | [diff] [blame] | 809 | if (submethods[i] != om) |
| 810 | continue; |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 811 | QualType nc_oret = OMD->getType()->getAs<FunctionType>()->getResultType(); |
Mike Stump | 6e319f6 | 2009-09-11 23:25:56 +0000 | [diff] [blame] | 812 | CanQualType oret = CGM.getContext().getCanonicalType(nc_oret); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 813 | QualType nc_ret = MD->getType()->getAs<FunctionType>()->getResultType(); |
Mike Stump | 6e319f6 | 2009-09-11 23:25:56 +0000 | [diff] [blame] | 814 | CanQualType ret = CGM.getContext().getCanonicalType(nc_ret); |
| 815 | CallOffset ReturnOffset = std::make_pair(0, 0); |
| 816 | if (oret != ret) { |
| 817 | // FIXME: calculate offsets for covariance |
Mike Stump | 97f4d46 | 2009-09-18 19:06:35 +0000 | [diff] [blame] | 818 | ReturnOffset = std::make_pair(42,getVbaseOffset(oret, ret)); |
Mike Stump | 6e319f6 | 2009-09-11 23:25:56 +0000 | [diff] [blame] | 819 | } |
Mike Stump | dec025b | 2009-09-07 04:27:52 +0000 | [diff] [blame] | 820 | Index[MD] = i; |
Mike Stump | 77ca8f6 | 2009-09-05 07:20:32 +0000 | [diff] [blame] | 821 | submethods[i] = m; |
Mike Stump | 77ca8f6 | 2009-09-05 07:20:32 +0000 | [diff] [blame] | 822 | |
| 823 | Thunks.erase(OMD); |
| 824 | if (MorallyVirtual) { |
Mike Stump | 77ca8f6 | 2009-09-05 07:20:32 +0000 | [diff] [blame] | 825 | Index_t &idx = VCall[OMD]; |
| 826 | if (idx == 0) { |
Mike Stump | dec025b | 2009-09-07 04:27:52 +0000 | [diff] [blame] | 827 | VCallOffset[MD] = Offset/8; |
Mike Stump | 77ca8f6 | 2009-09-05 07:20:32 +0000 | [diff] [blame] | 828 | idx = VCalls.size()+1; |
| 829 | VCalls.push_back(0); |
Mike Stump | dec025b | 2009-09-07 04:27:52 +0000 | [diff] [blame] | 830 | } else { |
| 831 | VCallOffset[MD] = VCallOffset[OMD]; |
| 832 | VCalls[idx-1] = -VCallOffset[OMD] + Offset/8; |
Mike Stump | 15a24e0 | 2009-08-28 23:22:54 +0000 | [diff] [blame] | 833 | } |
Mike Stump | 77ca8f6 | 2009-09-05 07:20:32 +0000 | [diff] [blame] | 834 | VCall[MD] = idx; |
Mike Stump | 6e319f6 | 2009-09-11 23:25:56 +0000 | [diff] [blame] | 835 | CallOffset ThisOffset; |
| 836 | // FIXME: calculate non-virtual offset |
| 837 | ThisOffset = std::make_pair(0, -((idx+extra+2)*LLVMPointerWidth/8)); |
| 838 | if (ReturnOffset.first || ReturnOffset.second) |
| 839 | CovariantThunks[MD] = std::make_pair(ThisOffset, ReturnOffset); |
| 840 | else |
| 841 | Thunks[MD] = ThisOffset; |
Mike Stump | 35191b6 | 2009-09-01 22:20:28 +0000 | [diff] [blame] | 842 | return true; |
Mike Stump | b9871a2 | 2009-08-21 01:45:00 +0000 | [diff] [blame] | 843 | } |
Mike Stump | 77ca8f6 | 2009-09-05 07:20:32 +0000 | [diff] [blame] | 844 | #if 0 |
| 845 | // FIXME: finish off |
| 846 | int64_t O = VCallOffset[OMD] - Offset/8; |
| 847 | if (O) { |
| 848 | Thunks[MD] = std::make_pair(O, 0); |
| 849 | } |
| 850 | #endif |
| 851 | return true; |
Mike Stump | 65defe3 | 2009-08-18 21:03:28 +0000 | [diff] [blame] | 852 | } |
Mike Stump | bc16aea | 2009-08-12 23:00:59 +0000 | [diff] [blame] | 853 | } |
Mike Stump | b9871a2 | 2009-08-21 01:45:00 +0000 | [diff] [blame] | 854 | |
Mike Stump | 35191b6 | 2009-09-01 22:20:28 +0000 | [diff] [blame] | 855 | return false; |
| 856 | } |
| 857 | |
Mike Stump | 98cc710 | 2009-09-05 11:28:33 +0000 | [diff] [blame] | 858 | void InstallThunks() { |
Mike Stump | 77ca8f6 | 2009-09-05 07:20:32 +0000 | [diff] [blame] | 859 | for (Thunks_t::iterator i = Thunks.begin(), e = Thunks.end(); |
| 860 | i != e; ++i) { |
| 861 | const CXXMethodDecl *MD = i->first; |
| 862 | Index_t idx = Index[MD]; |
| 863 | Index_t nv_O = i->second.first; |
| 864 | Index_t v_O = i->second.second; |
Mike Stump | 98cc710 | 2009-09-05 11:28:33 +0000 | [diff] [blame] | 865 | submethods[idx] = CGM.BuildThunk(MD, Extern, nv_O, v_O); |
Mike Stump | 77ca8f6 | 2009-09-05 07:20:32 +0000 | [diff] [blame] | 866 | } |
| 867 | Thunks.clear(); |
Mike Stump | 6e319f6 | 2009-09-11 23:25:56 +0000 | [diff] [blame] | 868 | for (CovariantThunks_t::iterator i = CovariantThunks.begin(), |
| 869 | e = CovariantThunks.end(); |
| 870 | i != e; ++i) { |
| 871 | const CXXMethodDecl *MD = i->first; |
| 872 | Index_t idx = Index[MD]; |
| 873 | Index_t nv_t = i->second.first.first; |
| 874 | Index_t v_t = i->second.first.second; |
| 875 | Index_t nv_r = i->second.second.first; |
| 876 | Index_t v_r = i->second.second.second; |
| 877 | submethods[idx] = CGM.BuildCovariantThunk(MD, Extern, nv_t, v_t, nv_r, |
| 878 | v_r); |
| 879 | } |
| 880 | CovariantThunks.clear(); |
Mike Stump | 77ca8f6 | 2009-09-05 07:20:32 +0000 | [diff] [blame] | 881 | } |
| 882 | |
Mike Stump | dec025b | 2009-09-07 04:27:52 +0000 | [diff] [blame] | 883 | void OverrideMethods(std::vector<std::pair<const CXXRecordDecl *, |
| 884 | int64_t> > *Path, bool MorallyVirtual) { |
| 885 | for (std::vector<std::pair<const CXXRecordDecl *, |
| 886 | int64_t> >::reverse_iterator i =Path->rbegin(), |
Mike Stump | 98cc710 | 2009-09-05 11:28:33 +0000 | [diff] [blame] | 887 | e = Path->rend(); i != e; ++i) { |
Mike Stump | dec025b | 2009-09-07 04:27:52 +0000 | [diff] [blame] | 888 | const CXXRecordDecl *RD = i->first; |
| 889 | int64_t Offset = i->second; |
Mike Stump | 98cc710 | 2009-09-05 11:28:33 +0000 | [diff] [blame] | 890 | for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me; |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 891 | ++mi) { |
| 892 | if (!mi->isVirtual()) |
| 893 | continue; |
| 894 | |
| 895 | const CXXMethodDecl *MD = *mi; |
| 896 | llvm::Constant *m = 0; |
Anders Carlsson | edee783 | 2009-10-06 23:02:00 +0000 | [diff] [blame] | 897 | if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD)) |
| 898 | m = wrap(CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete)); |
| 899 | else { |
Anders Carlsson | b299d35 | 2009-10-06 17:54:23 +0000 | [diff] [blame] | 900 | const FunctionProtoType *FPT = |
| 901 | MD->getType()->getAs<FunctionProtoType>(); |
| 902 | const llvm::Type *Ty = |
| 903 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), |
| 904 | FPT->isVariadic()); |
| 905 | |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 906 | m = wrap(CGM.GetAddrOfFunction(MD, Ty)); |
Anders Carlsson | edee783 | 2009-10-06 23:02:00 +0000 | [diff] [blame] | 907 | } |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 908 | |
| 909 | OverrideMethod(MD, m, MorallyVirtual, Offset); |
| 910 | } |
Mike Stump | 98cc710 | 2009-09-05 11:28:33 +0000 | [diff] [blame] | 911 | } |
Mike Stump | f9a883c | 2009-09-01 23:22:44 +0000 | [diff] [blame] | 912 | } |
| 913 | |
Mike Stump | 6d10eb8 | 2009-09-05 07:49:12 +0000 | [diff] [blame] | 914 | void AddMethod(const CXXMethodDecl *MD, bool MorallyVirtual, Index_t Offset) { |
Anders Carlsson | c7cba15 | 2009-09-12 00:00:29 +0000 | [diff] [blame] | 915 | llvm::Constant *m = 0; |
Anders Carlsson | 3fec4c6 | 2009-09-09 23:17:18 +0000 | [diff] [blame] | 916 | if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD)) |
Anders Carlsson | c7cba15 | 2009-09-12 00:00:29 +0000 | [diff] [blame] | 917 | m = wrap(CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete)); |
Anders Carlsson | b299d35 | 2009-10-06 17:54:23 +0000 | [diff] [blame] | 918 | else { |
| 919 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
| 920 | const llvm::Type *Ty = |
| 921 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), |
| 922 | FPT->isVariadic()); |
| 923 | |
| 924 | m = wrap(CGM.GetAddrOfFunction(MD, Ty)); |
| 925 | } |
| 926 | |
Mike Stump | 77ca8f6 | 2009-09-05 07:20:32 +0000 | [diff] [blame] | 927 | // If we can find a previously allocated slot for this, reuse it. |
Mike Stump | dec025b | 2009-09-07 04:27:52 +0000 | [diff] [blame] | 928 | if (OverrideMethod(MD, m, MorallyVirtual, Offset)) |
Mike Stump | 35191b6 | 2009-09-01 22:20:28 +0000 | [diff] [blame] | 929 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 930 | |
Mike Stump | b9871a2 | 2009-08-21 01:45:00 +0000 | [diff] [blame] | 931 | // else allocate a new slot. |
Mike Stump | 15a24e0 | 2009-08-28 23:22:54 +0000 | [diff] [blame] | 932 | Index[MD] = submethods.size(); |
Mike Stump | dec025b | 2009-09-07 04:27:52 +0000 | [diff] [blame] | 933 | submethods.push_back(m); |
Mike Stump | 15a24e0 | 2009-08-28 23:22:54 +0000 | [diff] [blame] | 934 | if (MorallyVirtual) { |
| 935 | VCallOffset[MD] = Offset/8; |
| 936 | Index_t &idx = VCall[MD]; |
| 937 | // Allocate the first one, after that, we reuse the previous one. |
| 938 | if (idx == 0) { |
| 939 | idx = VCalls.size()+1; |
Mike Stump | 15a24e0 | 2009-08-28 23:22:54 +0000 | [diff] [blame] | 940 | VCalls.push_back(0); |
| 941 | } |
| 942 | } |
Mike Stump | b9871a2 | 2009-08-21 01:45:00 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Mike Stump | 6d10eb8 | 2009-09-05 07:49:12 +0000 | [diff] [blame] | 945 | void AddMethods(const CXXRecordDecl *RD, bool MorallyVirtual, |
| 946 | Index_t Offset) { |
Mike Stump | b9871a2 | 2009-08-21 01:45:00 +0000 | [diff] [blame] | 947 | for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me; |
| 948 | ++mi) |
| 949 | if (mi->isVirtual()) |
Mike Stump | 6d10eb8 | 2009-09-05 07:49:12 +0000 | [diff] [blame] | 950 | AddMethod(*mi, MorallyVirtual, Offset); |
Mike Stump | bc16aea | 2009-08-12 23:00:59 +0000 | [diff] [blame] | 951 | } |
Mike Stump | 65defe3 | 2009-08-18 21:03:28 +0000 | [diff] [blame] | 952 | |
Mike Stump | 77ca8f6 | 2009-09-05 07:20:32 +0000 | [diff] [blame] | 953 | void NonVirtualBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout, |
| 954 | const CXXRecordDecl *PrimaryBase, |
| 955 | bool PrimaryBaseWasVirtual, bool MorallyVirtual, |
| 956 | int64_t Offset) { |
| 957 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 958 | e = RD->bases_end(); i != e; ++i) { |
| 959 | if (i->isVirtual()) |
| 960 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 961 | const CXXRecordDecl *Base = |
Mike Stump | 77ca8f6 | 2009-09-05 07:20:32 +0000 | [diff] [blame] | 962 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 963 | if (Base != PrimaryBase || PrimaryBaseWasVirtual) { |
| 964 | uint64_t o = Offset + Layout.getBaseClassOffset(Base); |
| 965 | StartNewTable(); |
Mike Stump | dec025b | 2009-09-07 04:27:52 +0000 | [diff] [blame] | 966 | std::vector<std::pair<const CXXRecordDecl *, |
| 967 | int64_t> > S; |
| 968 | S.push_back(std::make_pair(RD, Offset)); |
Mike Stump | 98cc710 | 2009-09-05 11:28:33 +0000 | [diff] [blame] | 969 | GenerateVtableForBase(Base, MorallyVirtual, o, false, &S); |
Mike Stump | 77ca8f6 | 2009-09-05 07:20:32 +0000 | [diff] [blame] | 970 | } |
| 971 | } |
| 972 | } |
| 973 | |
Mike Stump | 6d10eb8 | 2009-09-05 07:49:12 +0000 | [diff] [blame] | 974 | Index_t end(const CXXRecordDecl *RD, std::vector<llvm::Constant *> &offsets, |
| 975 | const ASTRecordLayout &Layout, |
| 976 | const CXXRecordDecl *PrimaryBase, |
| 977 | bool PrimaryBaseWasVirtual, bool MorallyVirtual, |
| 978 | int64_t Offset, bool ForVirtualBase) { |
| 979 | StartNewTable(); |
| 980 | extra = 0; |
| 981 | // FIXME: Cleanup. |
| 982 | if (!ForVirtualBase) { |
| 983 | // then virtual base offsets... |
| 984 | for (std::vector<llvm::Constant *>::reverse_iterator i = offsets.rbegin(), |
| 985 | e = offsets.rend(); i != e; ++i) |
| 986 | methods.push_back(*i); |
| 987 | } |
| 988 | |
| 989 | // The vcalls come first... |
Mike Stump | dec025b | 2009-09-07 04:27:52 +0000 | [diff] [blame] | 990 | for (std::vector<Index_t>::reverse_iterator i=VCalls.rbegin(), |
| 991 | e=VCalls.rend(); |
| 992 | i != e; ++i) |
Mike Stump | 6d10eb8 | 2009-09-05 07:49:12 +0000 | [diff] [blame] | 993 | methods.push_back(wrap((0?600:0) + *i)); |
| 994 | VCalls.clear(); |
| 995 | |
| 996 | if (ForVirtualBase) { |
| 997 | // then virtual base offsets... |
| 998 | for (std::vector<llvm::Constant *>::reverse_iterator i = offsets.rbegin(), |
| 999 | e = offsets.rend(); i != e; ++i) |
| 1000 | methods.push_back(*i); |
| 1001 | } |
| 1002 | |
| 1003 | methods.push_back(wrap(-(Offset/8))); |
| 1004 | methods.push_back(rtti); |
| 1005 | Index_t AddressPoint = methods.size(); |
| 1006 | |
Mike Stump | 98cc710 | 2009-09-05 11:28:33 +0000 | [diff] [blame] | 1007 | InstallThunks(); |
Mike Stump | 6d10eb8 | 2009-09-05 07:49:12 +0000 | [diff] [blame] | 1008 | methods.insert(methods.end(), submethods.begin(), submethods.end()); |
| 1009 | submethods.clear(); |
Mike Stump | 6d10eb8 | 2009-09-05 07:49:12 +0000 | [diff] [blame] | 1010 | |
| 1011 | // and then the non-virtual bases. |
| 1012 | NonVirtualBases(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual, |
| 1013 | MorallyVirtual, Offset); |
| 1014 | return AddressPoint; |
| 1015 | } |
| 1016 | |
Mike Stump | 078d778 | 2009-09-05 08:40:18 +0000 | [diff] [blame] | 1017 | void Primaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset) { |
Mike Stump | 9bbe962 | 2009-09-05 08:37:03 +0000 | [diff] [blame] | 1018 | if (!RD->isDynamicClass()) |
| 1019 | return; |
| 1020 | |
| 1021 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1022 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
Mike Stump | 9bbe962 | 2009-09-05 08:37:03 +0000 | [diff] [blame] | 1023 | const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual(); |
| 1024 | |
Mike Stump | 9bbe962 | 2009-09-05 08:37:03 +0000 | [diff] [blame] | 1025 | // vtables are composed from the chain of primaries. |
| 1026 | if (PrimaryBase) { |
| 1027 | if (PrimaryBaseWasVirtual) |
| 1028 | IndirectPrimary.insert(PrimaryBase); |
Mike Stump | 078d778 | 2009-09-05 08:40:18 +0000 | [diff] [blame] | 1029 | Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset); |
Mike Stump | 9bbe962 | 2009-09-05 08:37:03 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | // And add the virtuals for the class to the primary vtable. |
| 1033 | AddMethods(RD, MorallyVirtual, Offset); |
| 1034 | } |
| 1035 | |
Mike Stump | e45c90f | 2009-09-05 09:10:58 +0000 | [diff] [blame] | 1036 | int64_t GenerateVtableForBase(const CXXRecordDecl *RD, |
Mike Stump | a18df0e | 2009-09-05 09:24:43 +0000 | [diff] [blame] | 1037 | bool MorallyVirtual = false, int64_t Offset = 0, |
| 1038 | bool ForVirtualBase = false, |
Mike Stump | dec025b | 2009-09-07 04:27:52 +0000 | [diff] [blame] | 1039 | std::vector<std::pair<const CXXRecordDecl *, |
| 1040 | int64_t> > *Path = 0) { |
Mike Stump | bf595a3 | 2009-09-05 08:07:32 +0000 | [diff] [blame] | 1041 | if (!RD->isDynamicClass()) |
Mike Stump | 263b352 | 2009-08-21 23:09:30 +0000 | [diff] [blame] | 1042 | return 0; |
Mike Stump | 109b13d | 2009-08-18 21:30:21 +0000 | [diff] [blame] | 1043 | |
| 1044 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1045 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
Mike Stump | 109b13d | 2009-08-18 21:30:21 +0000 | [diff] [blame] | 1046 | const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual(); |
| 1047 | |
Mike Stump | 15a24e0 | 2009-08-28 23:22:54 +0000 | [diff] [blame] | 1048 | std::vector<llvm::Constant *> offsets; |
Mike Stump | b4d2861 | 2009-09-05 08:45:02 +0000 | [diff] [blame] | 1049 | extra = 0; |
| 1050 | GenerateVBaseOffsets(offsets, RD, Offset); |
| 1051 | if (ForVirtualBase) |
| 1052 | extra = offsets.size(); |
Mike Stump | 109b13d | 2009-08-18 21:30:21 +0000 | [diff] [blame] | 1053 | |
| 1054 | // vtables are composed from the chain of primaries. |
| 1055 | if (PrimaryBase) { |
| 1056 | if (PrimaryBaseWasVirtual) |
| 1057 | IndirectPrimary.insert(PrimaryBase); |
Mike Stump | 078d778 | 2009-09-05 08:40:18 +0000 | [diff] [blame] | 1058 | Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset); |
Mike Stump | 109b13d | 2009-08-18 21:30:21 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
Mike Stump | 15a24e0 | 2009-08-28 23:22:54 +0000 | [diff] [blame] | 1061 | // And add the virtuals for the class to the primary vtable. |
Mike Stump | 6d10eb8 | 2009-09-05 07:49:12 +0000 | [diff] [blame] | 1062 | AddMethods(RD, MorallyVirtual, Offset); |
Mike Stump | 15a24e0 | 2009-08-28 23:22:54 +0000 | [diff] [blame] | 1063 | |
Mike Stump | 98cc710 | 2009-09-05 11:28:33 +0000 | [diff] [blame] | 1064 | if (Path) |
Mike Stump | dec025b | 2009-09-07 04:27:52 +0000 | [diff] [blame] | 1065 | OverrideMethods(Path, MorallyVirtual); |
Mike Stump | 98cc710 | 2009-09-05 11:28:33 +0000 | [diff] [blame] | 1066 | |
Mike Stump | 6d10eb8 | 2009-09-05 07:49:12 +0000 | [diff] [blame] | 1067 | return end(RD, offsets, Layout, PrimaryBase, PrimaryBaseWasVirtual, |
| 1068 | MorallyVirtual, Offset, ForVirtualBase); |
Mike Stump | 109b13d | 2009-08-18 21:30:21 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
Mike Stump | 98cc710 | 2009-09-05 11:28:33 +0000 | [diff] [blame] | 1071 | void GenerateVtableForVBases(const CXXRecordDecl *RD, |
Mike Stump | dec025b | 2009-09-07 04:27:52 +0000 | [diff] [blame] | 1072 | int64_t Offset = 0, |
| 1073 | std::vector<std::pair<const CXXRecordDecl *, |
| 1074 | int64_t> > *Path = 0) { |
Mike Stump | 98cc710 | 2009-09-05 11:28:33 +0000 | [diff] [blame] | 1075 | bool alloc = false; |
| 1076 | if (Path == 0) { |
| 1077 | alloc = true; |
Mike Stump | dec025b | 2009-09-07 04:27:52 +0000 | [diff] [blame] | 1078 | Path = new std::vector<std::pair<const CXXRecordDecl *, |
| 1079 | int64_t> >; |
Mike Stump | 98cc710 | 2009-09-05 11:28:33 +0000 | [diff] [blame] | 1080 | } |
| 1081 | // FIXME: We also need to override using all paths to a virtual base, |
| 1082 | // right now, we just process the first path |
Mike Stump | dec025b | 2009-09-07 04:27:52 +0000 | [diff] [blame] | 1083 | Path->push_back(std::make_pair(RD, Offset)); |
Mike Stump | 109b13d | 2009-08-18 21:30:21 +0000 | [diff] [blame] | 1084 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 1085 | e = RD->bases_end(); i != e; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1086 | const CXXRecordDecl *Base = |
Mike Stump | 109b13d | 2009-08-18 21:30:21 +0000 | [diff] [blame] | 1087 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 1088 | if (i->isVirtual() && !IndirectPrimary.count(Base)) { |
| 1089 | // Mark it so we don't output it twice. |
| 1090 | IndirectPrimary.insert(Base); |
Mike Stump | b9871a2 | 2009-08-21 01:45:00 +0000 | [diff] [blame] | 1091 | StartNewTable(); |
Mike Stump | b983744 | 2009-08-20 07:22:17 +0000 | [diff] [blame] | 1092 | int64_t BaseOffset = BLayout.getVBaseClassOffset(Base); |
Mike Stump | 98cc710 | 2009-09-05 11:28:33 +0000 | [diff] [blame] | 1093 | GenerateVtableForBase(Base, true, BaseOffset, true, Path); |
Mike Stump | 109b13d | 2009-08-18 21:30:21 +0000 | [diff] [blame] | 1094 | } |
Mike Stump | dec025b | 2009-09-07 04:27:52 +0000 | [diff] [blame] | 1095 | int64_t BaseOffset = Offset; |
| 1096 | if (i->isVirtual()) |
| 1097 | BaseOffset = BLayout.getVBaseClassOffset(Base); |
Mike Stump | 109b13d | 2009-08-18 21:30:21 +0000 | [diff] [blame] | 1098 | if (Base->getNumVBases()) |
Mike Stump | dec025b | 2009-09-07 04:27:52 +0000 | [diff] [blame] | 1099 | GenerateVtableForVBases(Base, BaseOffset, Path); |
Mike Stump | 276b9f1 | 2009-08-16 01:46:26 +0000 | [diff] [blame] | 1100 | } |
Mike Stump | 98cc710 | 2009-09-05 11:28:33 +0000 | [diff] [blame] | 1101 | Path->pop_back(); |
| 1102 | if (alloc) |
| 1103 | delete Path; |
Mike Stump | 276b9f1 | 2009-08-16 01:46:26 +0000 | [diff] [blame] | 1104 | } |
Mike Stump | 109b13d | 2009-08-18 21:30:21 +0000 | [diff] [blame] | 1105 | }; |
Mike Stump | 8a12b56 | 2009-08-06 15:50:11 +0000 | [diff] [blame] | 1106 | |
Mike Stump | f0070db | 2009-08-26 20:46:33 +0000 | [diff] [blame] | 1107 | class VtableInfo { |
| 1108 | public: |
| 1109 | typedef VtableBuilder::Index_t Index_t; |
| 1110 | private: |
| 1111 | CodeGenModule &CGM; // Per-module state. |
| 1112 | /// Index_t - Vtable index type. |
| 1113 | typedef llvm::DenseMap<const CXXMethodDecl *, Index_t> ElTy; |
| 1114 | typedef llvm::DenseMap<const CXXRecordDecl *, ElTy *> MapTy; |
| 1115 | // FIXME: Move to Context. |
| 1116 | static MapTy IndexFor; |
Mike Stump | 97f4d46 | 2009-09-18 19:06:35 +0000 | [diff] [blame] | 1117 | |
| 1118 | typedef llvm::DenseMap<const CXXRecordDecl *, Index_t> VBElTy; |
| 1119 | typedef llvm::DenseMap<const CXXRecordDecl *, VBElTy *> VBMapTy; |
| 1120 | // FIXME: Move to Context. |
| 1121 | static VBMapTy VBIndexFor; |
Mike Stump | f0070db | 2009-08-26 20:46:33 +0000 | [diff] [blame] | 1122 | public: |
| 1123 | VtableInfo(CodeGenModule &cgm) : CGM(cgm) { } |
Mike Stump | 97f4d46 | 2009-09-18 19:06:35 +0000 | [diff] [blame] | 1124 | void RegisterIndex(const CXXRecordDecl *RD, const ElTy &e) { |
Mike Stump | f0070db | 2009-08-26 20:46:33 +0000 | [diff] [blame] | 1125 | assert(IndexFor.find(RD) == IndexFor.end() && "Don't compute vtbl twice"); |
| 1126 | // We own a copy of this, it will go away shortly. |
Mike Stump | f0070db | 2009-08-26 20:46:33 +0000 | [diff] [blame] | 1127 | IndexFor[RD] = new ElTy (e); |
| 1128 | } |
Mike Stump | 97f4d46 | 2009-09-18 19:06:35 +0000 | [diff] [blame] | 1129 | void RegisterVBIndex(const CXXRecordDecl *RD, const VBElTy &e) { |
| 1130 | assert(VBIndexFor.find(RD) == VBIndexFor.end() && "Don't compute vtbl twice"); |
| 1131 | // We own a copy of this, it will go away shortly. |
| 1132 | VBIndexFor[RD] = new VBElTy (e); |
| 1133 | } |
Mike Stump | f0070db | 2009-08-26 20:46:33 +0000 | [diff] [blame] | 1134 | Index_t lookup(const CXXMethodDecl *MD) { |
| 1135 | const CXXRecordDecl *RD = MD->getParent(); |
| 1136 | MapTy::iterator I = IndexFor.find(RD); |
| 1137 | if (I == IndexFor.end()) { |
| 1138 | std::vector<llvm::Constant *> methods; |
Mike Stump | 97f4d46 | 2009-09-18 19:06:35 +0000 | [diff] [blame] | 1139 | // FIXME: This seems expensive. Can we do a partial job to get |
| 1140 | // just this data. |
Mike Stump | f0070db | 2009-08-26 20:46:33 +0000 | [diff] [blame] | 1141 | VtableBuilder b(methods, RD, CGM); |
Mike Stump | a18df0e | 2009-09-05 09:24:43 +0000 | [diff] [blame] | 1142 | b.GenerateVtableForBase(RD); |
Mike Stump | bf595a3 | 2009-09-05 08:07:32 +0000 | [diff] [blame] | 1143 | b.GenerateVtableForVBases(RD); |
Mike Stump | 97f4d46 | 2009-09-18 19:06:35 +0000 | [diff] [blame] | 1144 | RegisterIndex(RD, b.getIndex()); |
Mike Stump | f0070db | 2009-08-26 20:46:33 +0000 | [diff] [blame] | 1145 | I = IndexFor.find(RD); |
| 1146 | } |
| 1147 | assert(I->second->find(MD)!=I->second->end() && "Can't find vtable index"); |
| 1148 | return (*I->second)[MD]; |
| 1149 | } |
Mike Stump | 97f4d46 | 2009-09-18 19:06:35 +0000 | [diff] [blame] | 1150 | Index_t VBlookup(const CXXRecordDecl *RD, const CXXRecordDecl *BD) { |
| 1151 | VBMapTy::iterator I = VBIndexFor.find(RD); |
| 1152 | if (I == VBIndexFor.end()) { |
| 1153 | std::vector<llvm::Constant *> methods; |
| 1154 | // FIXME: This seems expensive. Can we do a partial job to get |
| 1155 | // just this data. |
| 1156 | VtableBuilder b(methods, RD, CGM); |
| 1157 | b.GenerateVtableForBase(RD); |
| 1158 | b.GenerateVtableForVBases(RD); |
| 1159 | RegisterVBIndex(RD, b.getVBIndex()); |
| 1160 | I = VBIndexFor.find(RD); |
| 1161 | } |
| 1162 | assert(I->second->find(BD)!=I->second->end() && "Can't find vtable index"); |
| 1163 | return (*I->second)[BD]; |
| 1164 | } |
Mike Stump | f0070db | 2009-08-26 20:46:33 +0000 | [diff] [blame] | 1165 | }; |
| 1166 | |
Mike Stump | 97f4d46 | 2009-09-18 19:06:35 +0000 | [diff] [blame] | 1167 | // FIXME: move to Context |
| 1168 | static VtableInfo *vtableinfo; |
| 1169 | |
| 1170 | VtableBuilder::Index_t VtableBuilder::VBlookup(CXXRecordDecl *D, |
| 1171 | CXXRecordDecl *B) { |
| 1172 | if (vtableinfo == 0) |
| 1173 | vtableinfo = new VtableInfo(CGM); |
| 1174 | |
| 1175 | return vtableinfo->VBlookup(D, B); |
| 1176 | } |
| 1177 | |
| 1178 | |
Mike Stump | f0070db | 2009-08-26 20:46:33 +0000 | [diff] [blame] | 1179 | // FIXME: Move to Context. |
| 1180 | VtableInfo::MapTy VtableInfo::IndexFor; |
| 1181 | |
Mike Stump | 97f4d46 | 2009-09-18 19:06:35 +0000 | [diff] [blame] | 1182 | // FIXME: Move to Context. |
| 1183 | VtableInfo::VBMapTy VtableInfo::VBIndexFor; |
| 1184 | |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 1185 | llvm::Value *CodeGenFunction::GenerateVtable(const CXXRecordDecl *RD) { |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 1186 | llvm::SmallString<256> OutName; |
| 1187 | llvm::raw_svector_ostream Out(OutName); |
| 1188 | QualType ClassTy; |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 1189 | ClassTy = getContext().getTagDeclType(RD); |
Anders Carlsson | b540491 | 2009-10-07 01:06:45 +0000 | [diff] [blame] | 1190 | mangleCXXVtable(CGM.getMangleContext(), ClassTy, Out); |
Mike Stump | 82b5696 | 2009-07-31 21:43:43 +0000 | [diff] [blame] | 1191 | llvm::GlobalVariable::LinkageTypes linktype; |
| 1192 | linktype = llvm::GlobalValue::WeakAnyLinkage; |
| 1193 | std::vector<llvm::Constant *> methods; |
Mike Stump | 276b9f1 | 2009-08-16 01:46:26 +0000 | [diff] [blame] | 1194 | llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0); |
Mike Stump | 98cc710 | 2009-09-05 11:28:33 +0000 | [diff] [blame] | 1195 | int64_t AddressPoint; |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 1196 | |
Mike Stump | eb7e9c3 | 2009-08-19 18:10:47 +0000 | [diff] [blame] | 1197 | VtableBuilder b(methods, RD, CGM); |
Mike Stump | 109b13d | 2009-08-18 21:30:21 +0000 | [diff] [blame] | 1198 | |
Mike Stump | 276b9f1 | 2009-08-16 01:46:26 +0000 | [diff] [blame] | 1199 | // First comes the vtables for all the non-virtual bases... |
Mike Stump | 98cc710 | 2009-09-05 11:28:33 +0000 | [diff] [blame] | 1200 | AddressPoint = b.GenerateVtableForBase(RD); |
Mike Stump | 2153891 | 2009-08-14 01:44:03 +0000 | [diff] [blame] | 1201 | |
Mike Stump | 276b9f1 | 2009-08-16 01:46:26 +0000 | [diff] [blame] | 1202 | // then the vtables for all the virtual bases. |
Mike Stump | bf595a3 | 2009-09-05 08:07:32 +0000 | [diff] [blame] | 1203 | b.GenerateVtableForVBases(RD); |
Mike Stump | 104ffaa | 2009-08-04 21:58:42 +0000 | [diff] [blame] | 1204 | |
Mike Stump | 82b5696 | 2009-07-31 21:43:43 +0000 | [diff] [blame] | 1205 | llvm::Constant *C; |
| 1206 | llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, methods.size()); |
| 1207 | C = llvm::ConstantArray::get(type, methods); |
| 1208 | llvm::Value *vtable = new llvm::GlobalVariable(CGM.getModule(), type, true, |
Daniel Dunbar | 7765934 | 2009-08-19 20:04:03 +0000 | [diff] [blame] | 1209 | linktype, C, Out.str()); |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 1210 | vtable = Builder.CreateBitCast(vtable, Ptr8Ty); |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 1211 | vtable = Builder.CreateGEP(vtable, |
Mike Stump | 276b9f1 | 2009-08-16 01:46:26 +0000 | [diff] [blame] | 1212 | llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), |
Mike Stump | 98cc710 | 2009-09-05 11:28:33 +0000 | [diff] [blame] | 1213 | AddressPoint*LLVMPointerWidth/8)); |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 1214 | return vtable; |
| 1215 | } |
| 1216 | |
Mike Stump | ed032eb | 2009-09-04 18:27:16 +0000 | [diff] [blame] | 1217 | llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn, |
| 1218 | const CXXMethodDecl *MD, |
Mike Stump | 77ca8f6 | 2009-09-05 07:20:32 +0000 | [diff] [blame] | 1219 | bool Extern, int64_t nv, |
| 1220 | int64_t v) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1221 | QualType R = MD->getType()->getAs<FunctionType>()->getResultType(); |
Mike Stump | ed032eb | 2009-09-04 18:27:16 +0000 | [diff] [blame] | 1222 | |
| 1223 | FunctionArgList Args; |
| 1224 | ImplicitParamDecl *ThisDecl = |
| 1225 | ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, |
| 1226 | MD->getThisType(getContext())); |
| 1227 | Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType())); |
| 1228 | for (FunctionDecl::param_const_iterator i = MD->param_begin(), |
| 1229 | e = MD->param_end(); |
| 1230 | i != e; ++i) { |
| 1231 | ParmVarDecl *D = *i; |
| 1232 | Args.push_back(std::make_pair(D, D->getType())); |
| 1233 | } |
| 1234 | IdentifierInfo *II |
| 1235 | = &CGM.getContext().Idents.get("__thunk_named_foo_"); |
| 1236 | FunctionDecl *FD = FunctionDecl::Create(getContext(), |
| 1237 | getContext().getTranslationUnitDecl(), |
| 1238 | SourceLocation(), II, R, 0, |
| 1239 | Extern |
| 1240 | ? FunctionDecl::Extern |
| 1241 | : FunctionDecl::Static, |
| 1242 | false, true); |
| 1243 | StartFunction(FD, R, Fn, Args, SourceLocation()); |
| 1244 | // FIXME: generate body |
| 1245 | FinishFunction(); |
| 1246 | return Fn; |
| 1247 | } |
| 1248 | |
Mike Stump | 6e319f6 | 2009-09-11 23:25:56 +0000 | [diff] [blame] | 1249 | llvm::Constant *CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn, |
| 1250 | const CXXMethodDecl *MD, |
| 1251 | bool Extern, |
| 1252 | int64_t nv_t, |
| 1253 | int64_t v_t, |
| 1254 | int64_t nv_r, |
| 1255 | int64_t v_r) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1256 | QualType R = MD->getType()->getAs<FunctionType>()->getResultType(); |
Mike Stump | 6e319f6 | 2009-09-11 23:25:56 +0000 | [diff] [blame] | 1257 | |
| 1258 | FunctionArgList Args; |
| 1259 | ImplicitParamDecl *ThisDecl = |
| 1260 | ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, |
| 1261 | MD->getThisType(getContext())); |
| 1262 | Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType())); |
| 1263 | for (FunctionDecl::param_const_iterator i = MD->param_begin(), |
| 1264 | e = MD->param_end(); |
| 1265 | i != e; ++i) { |
| 1266 | ParmVarDecl *D = *i; |
| 1267 | Args.push_back(std::make_pair(D, D->getType())); |
| 1268 | } |
| 1269 | IdentifierInfo *II |
| 1270 | = &CGM.getContext().Idents.get("__thunk_named_foo_"); |
| 1271 | FunctionDecl *FD = FunctionDecl::Create(getContext(), |
| 1272 | getContext().getTranslationUnitDecl(), |
| 1273 | SourceLocation(), II, R, 0, |
| 1274 | Extern |
| 1275 | ? FunctionDecl::Extern |
| 1276 | : FunctionDecl::Static, |
| 1277 | false, true); |
| 1278 | StartFunction(FD, R, Fn, Args, SourceLocation()); |
| 1279 | // FIXME: generate body |
| 1280 | FinishFunction(); |
| 1281 | return Fn; |
| 1282 | } |
| 1283 | |
Mike Stump | 77ca8f6 | 2009-09-05 07:20:32 +0000 | [diff] [blame] | 1284 | llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern, |
| 1285 | int64_t nv, int64_t v) { |
Mike Stump | ed032eb | 2009-09-04 18:27:16 +0000 | [diff] [blame] | 1286 | llvm::SmallString<256> OutName; |
| 1287 | llvm::raw_svector_ostream Out(OutName); |
Anders Carlsson | b540491 | 2009-10-07 01:06:45 +0000 | [diff] [blame] | 1288 | mangleThunk(getMangleContext(), MD, nv, v, Out); |
Mike Stump | ed032eb | 2009-09-04 18:27:16 +0000 | [diff] [blame] | 1289 | llvm::GlobalVariable::LinkageTypes linktype; |
| 1290 | linktype = llvm::GlobalValue::WeakAnyLinkage; |
| 1291 | if (!Extern) |
| 1292 | linktype = llvm::GlobalValue::InternalLinkage; |
| 1293 | llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1294 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
Mike Stump | ed032eb | 2009-09-04 18:27:16 +0000 | [diff] [blame] | 1295 | const llvm::FunctionType *FTy = |
| 1296 | getTypes().GetFunctionType(getTypes().getFunctionInfo(MD), |
| 1297 | FPT->isVariadic()); |
| 1298 | |
| 1299 | llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(), |
| 1300 | &getModule()); |
Mike Stump | 77ca8f6 | 2009-09-05 07:20:32 +0000 | [diff] [blame] | 1301 | CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v); |
Mike Stump | ed032eb | 2009-09-04 18:27:16 +0000 | [diff] [blame] | 1302 | // Fn = Builder.CreateBitCast(Fn, Ptr8Ty); |
| 1303 | llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty); |
| 1304 | return m; |
| 1305 | } |
| 1306 | |
Mike Stump | 6e319f6 | 2009-09-11 23:25:56 +0000 | [diff] [blame] | 1307 | llvm::Constant *CodeGenModule::BuildCovariantThunk(const CXXMethodDecl *MD, |
| 1308 | bool Extern, int64_t nv_t, |
| 1309 | int64_t v_t, int64_t nv_r, |
| 1310 | int64_t v_r) { |
| 1311 | llvm::SmallString<256> OutName; |
| 1312 | llvm::raw_svector_ostream Out(OutName); |
Anders Carlsson | b540491 | 2009-10-07 01:06:45 +0000 | [diff] [blame] | 1313 | mangleCovariantThunk(getMangleContext(), MD, nv_t, v_t, nv_r, v_r, Out); |
Mike Stump | 6e319f6 | 2009-09-11 23:25:56 +0000 | [diff] [blame] | 1314 | llvm::GlobalVariable::LinkageTypes linktype; |
| 1315 | linktype = llvm::GlobalValue::WeakAnyLinkage; |
| 1316 | if (!Extern) |
| 1317 | linktype = llvm::GlobalValue::InternalLinkage; |
| 1318 | llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1319 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
Mike Stump | 6e319f6 | 2009-09-11 23:25:56 +0000 | [diff] [blame] | 1320 | const llvm::FunctionType *FTy = |
| 1321 | getTypes().GetFunctionType(getTypes().getFunctionInfo(MD), |
| 1322 | FPT->isVariadic()); |
| 1323 | |
| 1324 | llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(), |
| 1325 | &getModule()); |
| 1326 | CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, nv_t, v_t, nv_r, |
| 1327 | v_r); |
| 1328 | // Fn = Builder.CreateBitCast(Fn, Ptr8Ty); |
| 1329 | llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty); |
| 1330 | return m; |
| 1331 | } |
| 1332 | |
Mike Stump | f0070db | 2009-08-26 20:46:33 +0000 | [diff] [blame] | 1333 | llvm::Value * |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 1334 | CodeGenFunction::GetVirtualCXXBaseClassOffset(llvm::Value *This, |
| 1335 | const CXXRecordDecl *ClassDecl, |
| 1336 | const CXXRecordDecl *BaseClassDecl) { |
| 1337 | // FIXME: move to Context |
| 1338 | if (vtableinfo == 0) |
| 1339 | vtableinfo = new VtableInfo(CGM); |
| 1340 | |
| 1341 | const llvm::Type *Int8PtrTy = |
| 1342 | llvm::Type::getInt8Ty(VMContext)->getPointerTo(); |
| 1343 | |
| 1344 | llvm::Value *VTablePtr = Builder.CreateBitCast(This, |
| 1345 | Int8PtrTy->getPointerTo()); |
| 1346 | VTablePtr = Builder.CreateLoad(VTablePtr, "vtable"); |
| 1347 | |
| 1348 | llvm::Value *VBaseOffsetPtr = |
| 1349 | Builder.CreateConstGEP1_64(VTablePtr, |
| 1350 | vtableinfo->VBlookup(ClassDecl, BaseClassDecl), |
| 1351 | "vbase.offset.ptr"); |
| 1352 | const llvm::Type *PtrDiffTy = |
| 1353 | ConvertType(getContext().getPointerDiffType()); |
| 1354 | |
| 1355 | VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr, |
| 1356 | PtrDiffTy->getPointerTo()); |
| 1357 | |
| 1358 | llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset"); |
| 1359 | |
| 1360 | return VBaseOffset; |
| 1361 | } |
| 1362 | |
| 1363 | llvm::Value * |
Mike Stump | f0070db | 2009-08-26 20:46:33 +0000 | [diff] [blame] | 1364 | CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *&This, |
| 1365 | const llvm::Type *Ty) { |
| 1366 | // FIXME: If we know the dynamic type, we don't have to do a virtual dispatch. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1367 | |
Anders Carlsson | 2b35835 | 2009-10-03 14:56:57 +0000 | [diff] [blame] | 1368 | uint64_t Index = CGM.GetVtableIndex(MD); |
| 1369 | |
Mike Stump | f0070db | 2009-08-26 20:46:33 +0000 | [diff] [blame] | 1370 | Ty = llvm::PointerType::get(Ty, 0); |
| 1371 | Ty = llvm::PointerType::get(Ty, 0); |
| 1372 | Ty = llvm::PointerType::get(Ty, 0); |
| 1373 | llvm::Value *vtbl = Builder.CreateBitCast(This, Ty); |
| 1374 | vtbl = Builder.CreateLoad(vtbl); |
| 1375 | llvm::Value *vfn = Builder.CreateConstInBoundsGEP1_64(vtbl, |
Anders Carlsson | 2b35835 | 2009-10-03 14:56:57 +0000 | [diff] [blame] | 1376 | Index, "vfn"); |
Mike Stump | f0070db | 2009-08-26 20:46:33 +0000 | [diff] [blame] | 1377 | vfn = Builder.CreateLoad(vfn); |
| 1378 | return vfn; |
| 1379 | } |
| 1380 | |
Anders Carlsson | 2b35835 | 2009-10-03 14:56:57 +0000 | [diff] [blame] | 1381 | uint64_t CodeGenModule::GetVtableIndex(const CXXMethodDecl *MD) { |
| 1382 | // FIXME: move to CodeGenModule. |
| 1383 | if (vtableinfo == 0) |
| 1384 | vtableinfo = new VtableInfo(*this); |
| 1385 | |
| 1386 | return vtableinfo->lookup(MD); |
| 1387 | } |
| 1388 | |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 1389 | /// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class |
| 1390 | /// array of objects from SrcValue to DestValue. Copying can be either a bitwise |
| 1391 | /// copy or via a copy constructor call. |
Fariborz Jahanian | 4f68d53 | 2009-08-26 00:23:27 +0000 | [diff] [blame] | 1392 | // FIXME. Consolidate this with EmitCXXAggrConstructorCall. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1393 | void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest, |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 1394 | llvm::Value *Src, |
| 1395 | const ArrayType *Array, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1396 | const CXXRecordDecl *BaseClassDecl, |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 1397 | QualType Ty) { |
| 1398 | const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array); |
| 1399 | assert(CA && "VLA cannot be copied over"); |
| 1400 | bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1401 | |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 1402 | // Create a temporary for the loop index and initialize it with 0. |
| 1403 | llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext), |
| 1404 | "loop.index"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1405 | llvm::Value* zeroConstant = |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 1406 | llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)); |
Anders Carlsson | 2b35835 | 2009-10-03 14:56:57 +0000 | [diff] [blame] | 1407 | Builder.CreateStore(zeroConstant, IndexPtr, false); |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 1408 | // Start the loop with a block that tests the condition. |
| 1409 | llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); |
| 1410 | llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1411 | |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 1412 | EmitBlock(CondBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1413 | |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 1414 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
| 1415 | // Generate: if (loop-index < number-of-elements fall to the loop body, |
| 1416 | // otherwise, go to the block after the for-loop. |
| 1417 | uint64_t NumElements = getContext().getConstantArrayElementCount(CA); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1418 | llvm::Value * NumElementsPtr = |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 1419 | llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements); |
| 1420 | llvm::Value *Counter = Builder.CreateLoad(IndexPtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1421 | llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr, |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 1422 | "isless"); |
| 1423 | // If the condition is true, execute the body. |
| 1424 | Builder.CreateCondBr(IsLess, ForBody, AfterFor); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1425 | |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 1426 | EmitBlock(ForBody); |
| 1427 | llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); |
| 1428 | // Inside the loop body, emit the constructor call on the array element. |
| 1429 | Counter = Builder.CreateLoad(IndexPtr); |
| 1430 | Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress"); |
| 1431 | Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress"); |
| 1432 | if (BitwiseCopy) |
| 1433 | EmitAggregateCopy(Dest, Src, Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1434 | else if (CXXConstructorDecl *BaseCopyCtor = |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 1435 | BaseClassDecl->getCopyConstructor(getContext(), 0)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1436 | llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor, |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 1437 | Ctor_Complete); |
| 1438 | CallArgList CallArgs; |
| 1439 | // Push the this (Dest) ptr. |
| 1440 | CallArgs.push_back(std::make_pair(RValue::get(Dest), |
| 1441 | BaseCopyCtor->getThisType(getContext()))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1442 | |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 1443 | // Push the Src ptr. |
| 1444 | CallArgs.push_back(std::make_pair(RValue::get(Src), |
| 1445 | BaseCopyCtor->getParamDecl(0)->getType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1446 | QualType ResultType = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1447 | BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType(); |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 1448 | EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs), |
| 1449 | Callee, CallArgs, BaseCopyCtor); |
| 1450 | } |
| 1451 | EmitBlock(ContinueBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1452 | |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 1453 | // Emit the increment of the loop counter. |
| 1454 | llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1); |
| 1455 | Counter = Builder.CreateLoad(IndexPtr); |
| 1456 | NextVal = Builder.CreateAdd(Counter, NextVal, "inc"); |
| 1457 | Builder.CreateStore(NextVal, IndexPtr, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1458 | |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 1459 | // Finally, branch back up to the condition for the next iteration. |
| 1460 | EmitBranch(CondBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1461 | |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 1462 | // Emit the fall-through block. |
| 1463 | EmitBlock(AfterFor, true); |
| 1464 | } |
| 1465 | |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1466 | /// EmitClassAggrCopyAssignment - This routine generates code to assign a class |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1467 | /// array of objects from SrcValue to DestValue. Assignment can be either a |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1468 | /// bitwise assignment or via a copy assignment operator function call. |
| 1469 | /// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1470 | void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest, |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1471 | llvm::Value *Src, |
| 1472 | const ArrayType *Array, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1473 | const CXXRecordDecl *BaseClassDecl, |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1474 | QualType Ty) { |
| 1475 | const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array); |
| 1476 | assert(CA && "VLA cannot be asssigned"); |
| 1477 | bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1478 | |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1479 | // Create a temporary for the loop index and initialize it with 0. |
| 1480 | llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext), |
| 1481 | "loop.index"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1482 | llvm::Value* zeroConstant = |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1483 | llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)); |
| 1484 | Builder.CreateStore(zeroConstant, IndexPtr, false); |
| 1485 | // Start the loop with a block that tests the condition. |
| 1486 | llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); |
| 1487 | llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1488 | |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1489 | EmitBlock(CondBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1490 | |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1491 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
| 1492 | // Generate: if (loop-index < number-of-elements fall to the loop body, |
| 1493 | // otherwise, go to the block after the for-loop. |
| 1494 | uint64_t NumElements = getContext().getConstantArrayElementCount(CA); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1495 | llvm::Value * NumElementsPtr = |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1496 | llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements); |
| 1497 | llvm::Value *Counter = Builder.CreateLoad(IndexPtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1498 | llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr, |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1499 | "isless"); |
| 1500 | // If the condition is true, execute the body. |
| 1501 | Builder.CreateCondBr(IsLess, ForBody, AfterFor); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1502 | |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1503 | EmitBlock(ForBody); |
| 1504 | llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); |
| 1505 | // Inside the loop body, emit the assignment operator call on array element. |
| 1506 | Counter = Builder.CreateLoad(IndexPtr); |
| 1507 | Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress"); |
| 1508 | Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress"); |
| 1509 | const CXXMethodDecl *MD = 0; |
| 1510 | if (BitwiseAssign) |
| 1511 | EmitAggregateCopy(Dest, Src, Ty); |
| 1512 | else { |
| 1513 | bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(), |
| 1514 | MD); |
| 1515 | assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign"); |
| 1516 | (void)hasCopyAssign; |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1517 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1518 | const llvm::Type *LTy = |
| 1519 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), |
| 1520 | FPT->isVariadic()); |
Anders Carlsson | 555b4bb | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 1521 | llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1522 | |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1523 | CallArgList CallArgs; |
| 1524 | // Push the this (Dest) ptr. |
| 1525 | CallArgs.push_back(std::make_pair(RValue::get(Dest), |
| 1526 | MD->getThisType(getContext()))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1527 | |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1528 | // Push the Src ptr. |
| 1529 | CallArgs.push_back(std::make_pair(RValue::get(Src), |
| 1530 | MD->getParamDecl(0)->getType())); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1531 | QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType(); |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1532 | EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs), |
| 1533 | Callee, CallArgs, MD); |
| 1534 | } |
| 1535 | EmitBlock(ContinueBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1536 | |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1537 | // Emit the increment of the loop counter. |
| 1538 | llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1); |
| 1539 | Counter = Builder.CreateLoad(IndexPtr); |
| 1540 | NextVal = Builder.CreateAdd(Counter, NextVal, "inc"); |
| 1541 | Builder.CreateStore(NextVal, IndexPtr, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1542 | |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1543 | // Finally, branch back up to the condition for the next iteration. |
| 1544 | EmitBranch(CondBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1545 | |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1546 | // Emit the fall-through block. |
| 1547 | EmitBlock(AfterFor, true); |
| 1548 | } |
| 1549 | |
Fariborz Jahanian | ca28361 | 2009-08-07 23:51:33 +0000 | [diff] [blame] | 1550 | /// EmitClassMemberwiseCopy - This routine generates code to copy a class |
| 1551 | /// object from SrcValue to DestValue. Copying can be either a bitwise copy |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 1552 | /// or via a copy constructor call. |
Fariborz Jahanian | ca28361 | 2009-08-07 23:51:33 +0000 | [diff] [blame] | 1553 | void CodeGenFunction::EmitClassMemberwiseCopy( |
Fariborz Jahanian | 942f4f3 | 2009-08-08 23:32:22 +0000 | [diff] [blame] | 1554 | llvm::Value *Dest, llvm::Value *Src, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1555 | const CXXRecordDecl *ClassDecl, |
Fariborz Jahanian | 942f4f3 | 2009-08-08 23:32:22 +0000 | [diff] [blame] | 1556 | const CXXRecordDecl *BaseClassDecl, QualType Ty) { |
| 1557 | if (ClassDecl) { |
Anders Carlsson | 5a0f49e | 2009-09-12 04:26:35 +0000 | [diff] [blame] | 1558 | Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl, |
| 1559 | /*NullCheckValue=*/false); |
| 1560 | Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl, |
| 1561 | /*NullCheckValue=*/false); |
Fariborz Jahanian | 942f4f3 | 2009-08-08 23:32:22 +0000 | [diff] [blame] | 1562 | } |
| 1563 | if (BaseClassDecl->hasTrivialCopyConstructor()) { |
| 1564 | EmitAggregateCopy(Dest, Src, Ty); |
Fariborz Jahanian | ca28361 | 2009-08-07 23:51:33 +0000 | [diff] [blame] | 1565 | return; |
Fariborz Jahanian | 942f4f3 | 2009-08-08 23:32:22 +0000 | [diff] [blame] | 1566 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1567 | |
| 1568 | if (CXXConstructorDecl *BaseCopyCtor = |
Fariborz Jahanian | 80e4b9e | 2009-08-08 00:59:58 +0000 | [diff] [blame] | 1569 | BaseClassDecl->getCopyConstructor(getContext(), 0)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1570 | llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor, |
Fariborz Jahanian | ca28361 | 2009-08-07 23:51:33 +0000 | [diff] [blame] | 1571 | Ctor_Complete); |
Fariborz Jahanian | ca28361 | 2009-08-07 23:51:33 +0000 | [diff] [blame] | 1572 | CallArgList CallArgs; |
| 1573 | // Push the this (Dest) ptr. |
| 1574 | CallArgs.push_back(std::make_pair(RValue::get(Dest), |
| 1575 | BaseCopyCtor->getThisType(getContext()))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1576 | |
Fariborz Jahanian | ca28361 | 2009-08-07 23:51:33 +0000 | [diff] [blame] | 1577 | // Push the Src ptr. |
| 1578 | CallArgs.push_back(std::make_pair(RValue::get(Src), |
Fariborz Jahanian | 370c884 | 2009-08-10 17:20:45 +0000 | [diff] [blame] | 1579 | BaseCopyCtor->getParamDecl(0)->getType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1580 | QualType ResultType = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1581 | BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType(); |
Fariborz Jahanian | ca28361 | 2009-08-07 23:51:33 +0000 | [diff] [blame] | 1582 | EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs), |
| 1583 | Callee, CallArgs, BaseCopyCtor); |
| 1584 | } |
| 1585 | } |
Fariborz Jahanian | 06f598a | 2009-08-10 18:46:38 +0000 | [diff] [blame] | 1586 | |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1587 | /// EmitClassCopyAssignment - This routine generates code to copy assign a class |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1588 | /// object from SrcValue to DestValue. Assignment can be either a bitwise |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1589 | /// assignment of via an assignment operator call. |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1590 | // FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot. |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1591 | void CodeGenFunction::EmitClassCopyAssignment( |
| 1592 | llvm::Value *Dest, llvm::Value *Src, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1593 | const CXXRecordDecl *ClassDecl, |
| 1594 | const CXXRecordDecl *BaseClassDecl, |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1595 | QualType Ty) { |
| 1596 | if (ClassDecl) { |
Anders Carlsson | 5a0f49e | 2009-09-12 04:26:35 +0000 | [diff] [blame] | 1597 | Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl, |
| 1598 | /*NullCheckValue=*/false); |
| 1599 | Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl, |
| 1600 | /*NullCheckValue=*/false); |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1601 | } |
| 1602 | if (BaseClassDecl->hasTrivialCopyAssignment()) { |
| 1603 | EmitAggregateCopy(Dest, Src, Ty); |
| 1604 | return; |
| 1605 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1606 | |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1607 | const CXXMethodDecl *MD = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1608 | bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(), |
Fariborz Jahanian | e82c3e2 | 2009-08-13 00:53:36 +0000 | [diff] [blame] | 1609 | MD); |
| 1610 | assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign"); |
| 1611 | (void)ConstCopyAssignOp; |
| 1612 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1613 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1614 | const llvm::Type *LTy = |
| 1615 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), |
Fariborz Jahanian | e82c3e2 | 2009-08-13 00:53:36 +0000 | [diff] [blame] | 1616 | FPT->isVariadic()); |
Anders Carlsson | 555b4bb | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 1617 | llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1618 | |
Fariborz Jahanian | e82c3e2 | 2009-08-13 00:53:36 +0000 | [diff] [blame] | 1619 | CallArgList CallArgs; |
| 1620 | // Push the this (Dest) ptr. |
| 1621 | CallArgs.push_back(std::make_pair(RValue::get(Dest), |
| 1622 | MD->getThisType(getContext()))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1623 | |
Fariborz Jahanian | e82c3e2 | 2009-08-13 00:53:36 +0000 | [diff] [blame] | 1624 | // Push the Src ptr. |
| 1625 | CallArgs.push_back(std::make_pair(RValue::get(Src), |
| 1626 | MD->getParamDecl(0)->getType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1627 | QualType ResultType = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1628 | MD->getType()->getAs<FunctionType>()->getResultType(); |
Fariborz Jahanian | e82c3e2 | 2009-08-13 00:53:36 +0000 | [diff] [blame] | 1629 | EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs), |
| 1630 | Callee, CallArgs, MD); |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1631 | } |
| 1632 | |
Fariborz Jahanian | 06f598a | 2009-08-10 18:46:38 +0000 | [diff] [blame] | 1633 | /// SynthesizeDefaultConstructor - synthesize a default constructor |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1634 | void |
Anders Carlsson | de1d26b | 2009-09-14 05:32:02 +0000 | [diff] [blame] | 1635 | CodeGenFunction::SynthesizeDefaultConstructor(const CXXConstructorDecl *Ctor, |
| 1636 | CXXCtorType Type, |
Fariborz Jahanian | 06f598a | 2009-08-10 18:46:38 +0000 | [diff] [blame] | 1637 | llvm::Function *Fn, |
| 1638 | const FunctionArgList &Args) { |
Anders Carlsson | de1d26b | 2009-09-14 05:32:02 +0000 | [diff] [blame] | 1639 | StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args, |
| 1640 | SourceLocation()); |
| 1641 | EmitCtorPrologue(Ctor, Type); |
Fariborz Jahanian | 06f598a | 2009-08-10 18:46:38 +0000 | [diff] [blame] | 1642 | FinishFunction(); |
| 1643 | } |
| 1644 | |
Fariborz Jahanian | 8c241a2 | 2009-08-08 19:31:03 +0000 | [diff] [blame] | 1645 | /// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a copy |
Fariborz Jahanian | 97a9375 | 2009-08-07 20:22:40 +0000 | [diff] [blame] | 1646 | /// 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] | 1647 | /// The implicitly-defined copy constructor for class X performs a memberwise |
| 1648 | /// copy of its subobjects. The order of copying is the same as the order |
Fariborz Jahanian | 97a9375 | 2009-08-07 20:22:40 +0000 | [diff] [blame] | 1649 | /// of initialization of bases and members in a user-defined constructor |
| 1650 | /// Each subobject is copied in the manner appropriate to its type: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1651 | /// 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] | 1652 | /// used; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1653 | /// 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] | 1654 | /// appropriate to the element type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1655 | /// 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] | 1656 | /// used. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1657 | /// Virtual base class subobjects shall be copied only once by the |
| 1658 | /// implicitly-defined copy constructor |
Fariborz Jahanian | 97a9375 | 2009-08-07 20:22:40 +0000 | [diff] [blame] | 1659 | |
Anders Carlsson | de1d26b | 2009-09-14 05:32:02 +0000 | [diff] [blame] | 1660 | void |
| 1661 | CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *Ctor, |
| 1662 | CXXCtorType Type, |
| 1663 | llvm::Function *Fn, |
| 1664 | const FunctionArgList &Args) { |
Anders Carlsson | 0ff8baf | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 1665 | const CXXRecordDecl *ClassDecl = Ctor->getParent(); |
Fariborz Jahanian | 97a9375 | 2009-08-07 20:22:40 +0000 | [diff] [blame] | 1666 | assert(!ClassDecl->hasUserDeclaredCopyConstructor() && |
Fariborz Jahanian | 8c241a2 | 2009-08-08 19:31:03 +0000 | [diff] [blame] | 1667 | "SynthesizeCXXCopyConstructor - copy constructor has definition already"); |
Anders Carlsson | de1d26b | 2009-09-14 05:32:02 +0000 | [diff] [blame] | 1668 | StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args, |
| 1669 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1670 | |
Fariborz Jahanian | 1e4edd5 | 2009-08-08 00:15:41 +0000 | [diff] [blame] | 1671 | FunctionArgList::const_iterator i = Args.begin(); |
| 1672 | const VarDecl *ThisArg = i->first; |
| 1673 | llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg); |
| 1674 | llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this"); |
| 1675 | const VarDecl *SrcArg = (i+1)->first; |
| 1676 | llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg); |
| 1677 | llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1678 | |
Fariborz Jahanian | 97a9375 | 2009-08-07 20:22:40 +0000 | [diff] [blame] | 1679 | for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(); |
| 1680 | Base != ClassDecl->bases_end(); ++Base) { |
| 1681 | // FIXME. copy constrution of virtual base NYI |
| 1682 | if (Base->isVirtual()) |
| 1683 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1684 | |
Fariborz Jahanian | 97a9375 | 2009-08-07 20:22:40 +0000 | [diff] [blame] | 1685 | CXXRecordDecl *BaseClassDecl |
| 1686 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | 942f4f3 | 2009-08-08 23:32:22 +0000 | [diff] [blame] | 1687 | EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl, |
| 1688 | Base->getType()); |
Fariborz Jahanian | 97a9375 | 2009-08-07 20:22:40 +0000 | [diff] [blame] | 1689 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1690 | |
Fariborz Jahanian | 1e4edd5 | 2009-08-08 00:15:41 +0000 | [diff] [blame] | 1691 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 1692 | FieldEnd = ClassDecl->field_end(); |
| 1693 | Field != FieldEnd; ++Field) { |
| 1694 | QualType FieldType = getContext().getCanonicalType((*Field)->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1695 | const ConstantArrayType *Array = |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 1696 | getContext().getAsConstantArrayType(FieldType); |
| 1697 | if (Array) |
| 1698 | FieldType = getContext().getBaseElementType(FieldType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1699 | |
Fariborz Jahanian | 1e4edd5 | 2009-08-08 00:15:41 +0000 | [diff] [blame] | 1700 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
| 1701 | CXXRecordDecl *FieldClassDecl |
| 1702 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
| 1703 | LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0); |
| 1704 | LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0); |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 1705 | if (Array) { |
| 1706 | const llvm::Type *BasePtr = ConvertType(FieldType); |
| 1707 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1708 | llvm::Value *DestBaseAddrPtr = |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 1709 | Builder.CreateBitCast(LHS.getAddress(), BasePtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1710 | llvm::Value *SrcBaseAddrPtr = |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 1711 | Builder.CreateBitCast(RHS.getAddress(), BasePtr); |
| 1712 | EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array, |
| 1713 | FieldClassDecl, FieldType); |
| 1714 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1715 | else |
| 1716 | EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(), |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 1717 | 0 /*ClassDecl*/, FieldClassDecl, FieldType); |
Fariborz Jahanian | 1e4edd5 | 2009-08-08 00:15:41 +0000 | [diff] [blame] | 1718 | continue; |
| 1719 | } |
Fariborz Jahanian | f05fe65 | 2009-08-10 18:34:26 +0000 | [diff] [blame] | 1720 | // Do a built-in assignment of scalar data members. |
| 1721 | LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0); |
| 1722 | LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0); |
| 1723 | RValue RVRHS = EmitLoadOfLValue(RHS, FieldType); |
| 1724 | EmitStoreThroughLValue(RVRHS, LHS, FieldType); |
Fariborz Jahanian | 1e4edd5 | 2009-08-08 00:15:41 +0000 | [diff] [blame] | 1725 | } |
Fariborz Jahanian | 8c241a2 | 2009-08-08 19:31:03 +0000 | [diff] [blame] | 1726 | FinishFunction(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1727 | } |
Fariborz Jahanian | 97a9375 | 2009-08-07 20:22:40 +0000 | [diff] [blame] | 1728 | |
Fariborz Jahanian | 2198ba1 | 2009-08-12 21:14:35 +0000 | [diff] [blame] | 1729 | /// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1730 | /// Before the implicitly-declared copy assignment operator for a class is |
| 1731 | /// implicitly defined, all implicitly- declared copy assignment operators for |
| 1732 | /// its direct base classes and its nonstatic data members shall have been |
Fariborz Jahanian | 2198ba1 | 2009-08-12 21:14:35 +0000 | [diff] [blame] | 1733 | /// implicitly defined. [12.8-p12] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1734 | /// The implicitly-defined copy assignment operator for class X performs |
| 1735 | /// memberwise assignment of its subob- jects. The direct base classes of X are |
| 1736 | /// assigned first, in the order of their declaration in |
| 1737 | /// the base-specifier-list, and then the immediate nonstatic data members of X |
| 1738 | /// 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] | 1739 | /// definition.Each subobject is assigned in the manner appropriate to its type: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1740 | /// if the subobject is of class type, the copy assignment operator for the |
| 1741 | /// class is used (as if by explicit qualification; that is, ignoring any |
Fariborz Jahanian | 2198ba1 | 2009-08-12 21:14:35 +0000 | [diff] [blame] | 1742 | /// possible virtual overriding functions in more derived classes); |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1743 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1744 | /// 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] | 1745 | /// appropriate to the element type; |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1746 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1747 | /// 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] | 1748 | /// used. |
| 1749 | void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD, |
Fariborz Jahanian | 2198ba1 | 2009-08-12 21:14:35 +0000 | [diff] [blame] | 1750 | llvm::Function *Fn, |
| 1751 | const FunctionArgList &Args) { |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1752 | |
| 1753 | const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext()); |
| 1754 | assert(!ClassDecl->hasUserDeclaredCopyAssignment() && |
| 1755 | "SynthesizeCXXCopyAssignment - copy assignment has user declaration"); |
Anders Carlsson | de1d26b | 2009-09-14 05:32:02 +0000 | [diff] [blame] | 1756 | StartFunction(CD, CD->getResultType(), Fn, Args, SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1757 | |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1758 | FunctionArgList::const_iterator i = Args.begin(); |
| 1759 | const VarDecl *ThisArg = i->first; |
| 1760 | llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg); |
| 1761 | llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this"); |
| 1762 | const VarDecl *SrcArg = (i+1)->first; |
| 1763 | llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg); |
| 1764 | llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1765 | |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1766 | for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(); |
| 1767 | Base != ClassDecl->bases_end(); ++Base) { |
| 1768 | // FIXME. copy assignment of virtual base NYI |
| 1769 | if (Base->isVirtual()) |
| 1770 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1771 | |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1772 | CXXRecordDecl *BaseClassDecl |
| 1773 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 1774 | EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl, |
| 1775 | Base->getType()); |
| 1776 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1777 | |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1778 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 1779 | FieldEnd = ClassDecl->field_end(); |
| 1780 | Field != FieldEnd; ++Field) { |
| 1781 | QualType FieldType = getContext().getCanonicalType((*Field)->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1782 | const ConstantArrayType *Array = |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1783 | getContext().getAsConstantArrayType(FieldType); |
| 1784 | if (Array) |
| 1785 | FieldType = getContext().getBaseElementType(FieldType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1786 | |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1787 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
| 1788 | CXXRecordDecl *FieldClassDecl |
| 1789 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
| 1790 | LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0); |
| 1791 | LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0); |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1792 | if (Array) { |
| 1793 | const llvm::Type *BasePtr = ConvertType(FieldType); |
| 1794 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
| 1795 | llvm::Value *DestBaseAddrPtr = |
| 1796 | Builder.CreateBitCast(LHS.getAddress(), BasePtr); |
| 1797 | llvm::Value *SrcBaseAddrPtr = |
| 1798 | Builder.CreateBitCast(RHS.getAddress(), BasePtr); |
| 1799 | EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array, |
| 1800 | FieldClassDecl, FieldType); |
| 1801 | } |
| 1802 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1803 | EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(), |
Fariborz Jahanian | c28bbc2 | 2009-08-21 22:34:55 +0000 | [diff] [blame] | 1804 | 0 /*ClassDecl*/, FieldClassDecl, FieldType); |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 1805 | continue; |
| 1806 | } |
| 1807 | // Do a built-in assignment of scalar data members. |
| 1808 | LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0); |
| 1809 | LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0); |
| 1810 | RValue RVRHS = EmitLoadOfLValue(RHS, FieldType); |
| 1811 | EmitStoreThroughLValue(RVRHS, LHS, FieldType); |
Fariborz Jahanian | 183d718 | 2009-08-14 00:01:54 +0000 | [diff] [blame] | 1812 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1813 | |
Fariborz Jahanian | 183d718 | 2009-08-14 00:01:54 +0000 | [diff] [blame] | 1814 | // return *this; |
| 1815 | Builder.CreateStore(LoadOfThis, ReturnValue); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1816 | |
Fariborz Jahanian | 2198ba1 | 2009-08-12 21:14:35 +0000 | [diff] [blame] | 1817 | FinishFunction(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1818 | } |
Fariborz Jahanian | 97a9375 | 2009-08-07 20:22:40 +0000 | [diff] [blame] | 1819 | |
Fariborz Jahanian | e7d346b | 2009-07-20 23:18:55 +0000 | [diff] [blame] | 1820 | /// EmitCtorPrologue - This routine generates necessary code to initialize |
| 1821 | /// base classes and non-static data members belonging to this constructor. |
Anders Carlsson | 174754c | 2009-09-01 18:33:46 +0000 | [diff] [blame] | 1822 | /// FIXME: This needs to take a CXXCtorType. |
Anders Carlsson | de1d26b | 2009-09-14 05:32:02 +0000 | [diff] [blame] | 1823 | void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD, |
| 1824 | CXXCtorType CtorType) { |
Fariborz Jahanian | 742cd1b | 2009-07-25 21:12:28 +0000 | [diff] [blame] | 1825 | const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext()); |
Mike Stump | eb19fa9 | 2009-08-06 13:41:24 +0000 | [diff] [blame] | 1826 | // FIXME: Add vbase initialization |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 1827 | llvm::Value *LoadOfThis = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1828 | |
Fariborz Jahanian | 742cd1b | 2009-07-25 21:12:28 +0000 | [diff] [blame] | 1829 | for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(), |
Fariborz Jahanian | e7d346b | 2009-07-20 23:18:55 +0000 | [diff] [blame] | 1830 | E = CD->init_end(); |
| 1831 | B != E; ++B) { |
| 1832 | CXXBaseOrMemberInitializer *Member = (*B); |
| 1833 | if (Member->isBaseInitializer()) { |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 1834 | LoadOfThis = LoadCXXThis(); |
Fariborz Jahanian | 6d0bdaa | 2009-07-28 18:09:28 +0000 | [diff] [blame] | 1835 | Type *BaseType = Member->getBaseClass(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1836 | CXXRecordDecl *BaseClassDecl = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1837 | cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()); |
Anders Carlsson | 5a0f49e | 2009-09-12 04:26:35 +0000 | [diff] [blame] | 1838 | llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl, |
| 1839 | BaseClassDecl, |
| 1840 | /*NullCheckValue=*/false); |
Fariborz Jahanian | 742cd1b | 2009-07-25 21:12:28 +0000 | [diff] [blame] | 1841 | EmitCXXConstructorCall(Member->getConstructor(), |
Anders Carlsson | de1d26b | 2009-09-14 05:32:02 +0000 | [diff] [blame] | 1842 | CtorType, V, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1843 | Member->const_arg_begin(), |
Fariborz Jahanian | 742cd1b | 2009-07-25 21:12:28 +0000 | [diff] [blame] | 1844 | Member->const_arg_end()); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 1845 | } else { |
Fariborz Jahanian | e7d346b | 2009-07-20 23:18:55 +0000 | [diff] [blame] | 1846 | // non-static data member initilaizers. |
| 1847 | FieldDecl *Field = Member->getMember(); |
| 1848 | QualType FieldType = getContext().getCanonicalType((Field)->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1849 | const ConstantArrayType *Array = |
Fariborz Jahanian | eb0b6d5 | 2009-08-21 18:30:26 +0000 | [diff] [blame] | 1850 | getContext().getAsConstantArrayType(FieldType); |
Fariborz Jahanian | 64a54ad | 2009-08-21 17:09:38 +0000 | [diff] [blame] | 1851 | if (Array) |
| 1852 | FieldType = getContext().getBaseElementType(FieldType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1853 | |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 1854 | LoadOfThis = LoadCXXThis(); |
Eli Friedman | e3a97db | 2009-08-29 20:58:20 +0000 | [diff] [blame] | 1855 | LValue LHS; |
| 1856 | if (FieldType->isReferenceType()) { |
| 1857 | // FIXME: This is really ugly; should be refactored somehow |
| 1858 | unsigned idx = CGM.getTypes().getLLVMFieldNo(Field); |
| 1859 | llvm::Value *V = Builder.CreateStructGEP(LoadOfThis, idx, "tmp"); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1860 | assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs"); |
| 1861 | LHS = LValue::MakeAddr(V, MakeQualifiers(FieldType)); |
Eli Friedman | e3a97db | 2009-08-29 20:58:20 +0000 | [diff] [blame] | 1862 | } else { |
| 1863 | LHS = EmitLValueForField(LoadOfThis, Field, false, 0); |
| 1864 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1865 | if (FieldType->getAs<RecordType>()) { |
Fariborz Jahanian | e649412 | 2009-08-11 18:49:54 +0000 | [diff] [blame] | 1866 | if (!Field->isAnonymousStructOrUnion()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1867 | assert(Member->getConstructor() && |
Fariborz Jahanian | 50b8eea | 2009-07-24 17:57:02 +0000 | [diff] [blame] | 1868 | "EmitCtorPrologue - no constructor to initialize member"); |
Fariborz Jahanian | 64a54ad | 2009-08-21 17:09:38 +0000 | [diff] [blame] | 1869 | if (Array) { |
| 1870 | const llvm::Type *BasePtr = ConvertType(FieldType); |
| 1871 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1872 | llvm::Value *BaseAddrPtr = |
Fariborz Jahanian | 64a54ad | 2009-08-21 17:09:38 +0000 | [diff] [blame] | 1873 | Builder.CreateBitCast(LHS.getAddress(), BasePtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1874 | EmitCXXAggrConstructorCall(Member->getConstructor(), |
Fariborz Jahanian | 64a54ad | 2009-08-21 17:09:38 +0000 | [diff] [blame] | 1875 | Array, BaseAddrPtr); |
| 1876 | } |
| 1877 | else |
| 1878 | EmitCXXConstructorCall(Member->getConstructor(), |
| 1879 | Ctor_Complete, LHS.getAddress(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1880 | Member->const_arg_begin(), |
Fariborz Jahanian | 64a54ad | 2009-08-21 17:09:38 +0000 | [diff] [blame] | 1881 | Member->const_arg_end()); |
Fariborz Jahanian | e649412 | 2009-08-11 18:49:54 +0000 | [diff] [blame] | 1882 | continue; |
| 1883 | } |
| 1884 | else { |
| 1885 | // Initializing an anonymous union data member. |
| 1886 | FieldDecl *anonMember = Member->getAnonUnionMember(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1887 | LHS = EmitLValueForField(LHS.getAddress(), anonMember, |
Anders Carlsson | c186b8f | 2009-09-02 21:14:47 +0000 | [diff] [blame] | 1888 | /*IsUnion=*/true, 0); |
Fariborz Jahanian | e649412 | 2009-08-11 18:49:54 +0000 | [diff] [blame] | 1889 | FieldType = anonMember->getType(); |
| 1890 | } |
Fariborz Jahanian | 50b8eea | 2009-07-24 17:57:02 +0000 | [diff] [blame] | 1891 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1892 | |
Fariborz Jahanian | e7d346b | 2009-07-20 23:18:55 +0000 | [diff] [blame] | 1893 | assert(Member->getNumArgs() == 1 && "Initializer count must be 1 only"); |
Fariborz Jahanian | 50b8eea | 2009-07-24 17:57:02 +0000 | [diff] [blame] | 1894 | Expr *RhsExpr = *Member->arg_begin(); |
Eli Friedman | e3a97db | 2009-08-29 20:58:20 +0000 | [diff] [blame] | 1895 | RValue RHS; |
| 1896 | if (FieldType->isReferenceType()) |
| 1897 | RHS = EmitReferenceBindingToExpr(RhsExpr, FieldType, |
| 1898 | /*IsInitializer=*/true); |
| 1899 | else |
| 1900 | RHS = RValue::get(EmitScalarExpr(RhsExpr, true)); |
| 1901 | EmitStoreThroughLValue(RHS, LHS, FieldType); |
Fariborz Jahanian | e7d346b | 2009-07-20 23:18:55 +0000 | [diff] [blame] | 1902 | } |
| 1903 | } |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 1904 | |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 1905 | if (!CD->getNumBaseOrMemberInitializers() && !CD->isTrivial()) { |
Fariborz Jahanian | 1d9b5ef | 2009-08-15 18:55:17 +0000 | [diff] [blame] | 1906 | // Nontrivial default constructor with no initializer list. It may still |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1907 | // have bases classes and/or contain non-static data members which require |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 1908 | // construction. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1909 | for (CXXRecordDecl::base_class_const_iterator Base = |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 1910 | ClassDecl->bases_begin(); |
| 1911 | Base != ClassDecl->bases_end(); ++Base) { |
| 1912 | // FIXME. copy assignment of virtual base NYI |
| 1913 | if (Base->isVirtual()) |
| 1914 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1915 | |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 1916 | CXXRecordDecl *BaseClassDecl |
| 1917 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 1918 | if (BaseClassDecl->hasTrivialConstructor()) |
| 1919 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1920 | if (CXXConstructorDecl *BaseCX = |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 1921 | BaseClassDecl->getDefaultConstructor(getContext())) { |
| 1922 | LoadOfThis = LoadCXXThis(); |
Anders Carlsson | 5a0f49e | 2009-09-12 04:26:35 +0000 | [diff] [blame] | 1923 | llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl, |
| 1924 | BaseClassDecl, |
| 1925 | /*NullCheckValue=*/false); |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 1926 | EmitCXXConstructorCall(BaseCX, Ctor_Complete, V, 0, 0); |
| 1927 | } |
| 1928 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1929 | |
Fariborz Jahanian | 1d9b5ef | 2009-08-15 18:55:17 +0000 | [diff] [blame] | 1930 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 1931 | FieldEnd = ClassDecl->field_end(); |
| 1932 | Field != FieldEnd; ++Field) { |
| 1933 | QualType FieldType = getContext().getCanonicalType((*Field)->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1934 | const ConstantArrayType *Array = |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 1935 | getContext().getAsConstantArrayType(FieldType); |
Fariborz Jahanian | f800f6c | 2009-08-20 20:54:15 +0000 | [diff] [blame] | 1936 | if (Array) |
| 1937 | FieldType = getContext().getBaseElementType(FieldType); |
Fariborz Jahanian | 1d9b5ef | 2009-08-15 18:55:17 +0000 | [diff] [blame] | 1938 | if (!FieldType->getAs<RecordType>() || Field->isAnonymousStructOrUnion()) |
| 1939 | continue; |
| 1940 | const RecordType *ClassRec = FieldType->getAs<RecordType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1941 | CXXRecordDecl *MemberClassDecl = |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 1942 | dyn_cast<CXXRecordDecl>(ClassRec->getDecl()); |
| 1943 | if (!MemberClassDecl || MemberClassDecl->hasTrivialConstructor()) |
| 1944 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1945 | if (CXXConstructorDecl *MamberCX = |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 1946 | MemberClassDecl->getDefaultConstructor(getContext())) { |
| 1947 | LoadOfThis = LoadCXXThis(); |
| 1948 | LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0); |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 1949 | if (Array) { |
| 1950 | const llvm::Type *BasePtr = ConvertType(FieldType); |
| 1951 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1952 | llvm::Value *BaseAddrPtr = |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 1953 | Builder.CreateBitCast(LHS.getAddress(), BasePtr); |
| 1954 | EmitCXXAggrConstructorCall(MamberCX, Array, BaseAddrPtr); |
| 1955 | } |
| 1956 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1957 | EmitCXXConstructorCall(MamberCX, Ctor_Complete, LHS.getAddress(), |
Fariborz Jahanian | 288dcaf | 2009-08-19 20:55:16 +0000 | [diff] [blame] | 1958 | 0, 0); |
Fariborz Jahanian | 1d9b5ef | 2009-08-15 18:55:17 +0000 | [diff] [blame] | 1959 | } |
| 1960 | } |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 1961 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1962 | |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 1963 | // Initialize the vtable pointer |
Mike Stump | b502d83 | 2009-08-05 22:59:44 +0000 | [diff] [blame] | 1964 | if (ClassDecl->isDynamicClass()) { |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 1965 | if (!LoadOfThis) |
| 1966 | LoadOfThis = LoadCXXThis(); |
| 1967 | llvm::Value *VtableField; |
| 1968 | llvm::Type *Ptr8Ty, *PtrPtr8Ty; |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1969 | Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0); |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 1970 | PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0); |
| 1971 | VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty); |
| 1972 | llvm::Value *vtable = GenerateVtable(ClassDecl); |
| 1973 | Builder.CreateStore(vtable, VtableField); |
| 1974 | } |
Fariborz Jahanian | e7d346b | 2009-07-20 23:18:55 +0000 | [diff] [blame] | 1975 | } |
Fariborz Jahanian | 426cc38 | 2009-07-30 17:49:11 +0000 | [diff] [blame] | 1976 | |
| 1977 | /// EmitDtorEpilogue - Emit all code that comes at the end of class's |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1978 | /// destructor. This is to call destructors on members and base classes |
Fariborz Jahanian | 426cc38 | 2009-07-30 17:49:11 +0000 | [diff] [blame] | 1979 | /// in reverse order of their construction. |
Anders Carlsson | 174754c | 2009-09-01 18:33:46 +0000 | [diff] [blame] | 1980 | /// FIXME: This needs to take a CXXDtorType. |
Anders Carlsson | de1d26b | 2009-09-14 05:32:02 +0000 | [diff] [blame] | 1981 | void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD, |
| 1982 | CXXDtorType DtorType) { |
Fariborz Jahanian | 426cc38 | 2009-07-30 17:49:11 +0000 | [diff] [blame] | 1983 | const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext()); |
Anders Carlsson | de738fe | 2009-09-01 21:12:16 +0000 | [diff] [blame] | 1984 | assert(!ClassDecl->getNumVBases() && |
| 1985 | "FIXME: Destruction of virtual bases not supported"); |
Fariborz Jahanian | 426cc38 | 2009-07-30 17:49:11 +0000 | [diff] [blame] | 1986 | (void)ClassDecl; // prevent warning. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1987 | |
Fariborz Jahanian | 426cc38 | 2009-07-30 17:49:11 +0000 | [diff] [blame] | 1988 | for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(), |
| 1989 | *E = DD->destr_end(); B != E; ++B) { |
| 1990 | uintptr_t BaseOrMember = (*B); |
| 1991 | if (DD->isMemberToDestroy(BaseOrMember)) { |
| 1992 | FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember); |
| 1993 | QualType FieldType = getContext().getCanonicalType((FD)->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1994 | const ConstantArrayType *Array = |
Fariborz Jahanian | f800f6c | 2009-08-20 20:54:15 +0000 | [diff] [blame] | 1995 | getContext().getAsConstantArrayType(FieldType); |
| 1996 | if (Array) |
| 1997 | FieldType = getContext().getBaseElementType(FieldType); |
Fariborz Jahanian | 426cc38 | 2009-07-30 17:49:11 +0000 | [diff] [blame] | 1998 | const RecordType *RT = FieldType->getAs<RecordType>(); |
| 1999 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 2000 | if (FieldClassDecl->hasTrivialDestructor()) |
| 2001 | continue; |
| 2002 | llvm::Value *LoadOfThis = LoadCXXThis(); |
| 2003 | LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0); |
Fariborz Jahanian | f800f6c | 2009-08-20 20:54:15 +0000 | [diff] [blame] | 2004 | if (Array) { |
| 2005 | const llvm::Type *BasePtr = ConvertType(FieldType); |
| 2006 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2007 | llvm::Value *BaseAddrPtr = |
Fariborz Jahanian | f800f6c | 2009-08-20 20:54:15 +0000 | [diff] [blame] | 2008 | Builder.CreateBitCast(LHS.getAddress(), BasePtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2009 | EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()), |
Fariborz Jahanian | f800f6c | 2009-08-20 20:54:15 +0000 | [diff] [blame] | 2010 | Array, BaseAddrPtr); |
| 2011 | } |
| 2012 | else |
| 2013 | EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()), |
| 2014 | Dtor_Complete, LHS.getAddress()); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 2015 | } else { |
Fariborz Jahanian | 426cc38 | 2009-07-30 17:49:11 +0000 | [diff] [blame] | 2016 | const RecordType *RT = |
| 2017 | DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>(); |
| 2018 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 2019 | if (BaseClassDecl->hasTrivialDestructor()) |
| 2020 | continue; |
Anders Carlsson | 5a0f49e | 2009-09-12 04:26:35 +0000 | [diff] [blame] | 2021 | llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(), |
| 2022 | ClassDecl, BaseClassDecl, |
| 2023 | /*NullCheckValue=*/false); |
Fariborz Jahanian | 426cc38 | 2009-07-30 17:49:11 +0000 | [diff] [blame] | 2024 | EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()), |
Anders Carlsson | de1d26b | 2009-09-14 05:32:02 +0000 | [diff] [blame] | 2025 | DtorType, V); |
Fariborz Jahanian | 426cc38 | 2009-07-30 17:49:11 +0000 | [diff] [blame] | 2026 | } |
| 2027 | } |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 2028 | if (DD->getNumBaseOrMemberDestructions() || DD->isTrivial()) |
| 2029 | return; |
| 2030 | // Case of destructor synthesis with fields and base classes |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2031 | // which have non-trivial destructors. They must be destructed in |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 2032 | // reverse order of their construction. |
| 2033 | llvm::SmallVector<FieldDecl *, 16> DestructedFields; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2034 | |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 2035 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 2036 | FieldEnd = ClassDecl->field_end(); |
| 2037 | Field != FieldEnd; ++Field) { |
| 2038 | QualType FieldType = getContext().getCanonicalType((*Field)->getType()); |
Fariborz Jahanian | f800f6c | 2009-08-20 20:54:15 +0000 | [diff] [blame] | 2039 | if (getContext().getAsConstantArrayType(FieldType)) |
| 2040 | FieldType = getContext().getBaseElementType(FieldType); |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 2041 | if (const RecordType *RT = FieldType->getAs<RecordType>()) { |
| 2042 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 2043 | if (FieldClassDecl->hasTrivialDestructor()) |
| 2044 | continue; |
| 2045 | DestructedFields.push_back(*Field); |
| 2046 | } |
| 2047 | } |
| 2048 | if (!DestructedFields.empty()) |
| 2049 | for (int i = DestructedFields.size() -1; i >= 0; --i) { |
| 2050 | FieldDecl *Field = DestructedFields[i]; |
| 2051 | QualType FieldType = Field->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2052 | const ConstantArrayType *Array = |
Fariborz Jahanian | f800f6c | 2009-08-20 20:54:15 +0000 | [diff] [blame] | 2053 | getContext().getAsConstantArrayType(FieldType); |
| 2054 | if (Array) |
| 2055 | FieldType = getContext().getBaseElementType(FieldType); |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 2056 | const RecordType *RT = FieldType->getAs<RecordType>(); |
| 2057 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 2058 | llvm::Value *LoadOfThis = LoadCXXThis(); |
| 2059 | LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0); |
Fariborz Jahanian | f800f6c | 2009-08-20 20:54:15 +0000 | [diff] [blame] | 2060 | if (Array) { |
| 2061 | const llvm::Type *BasePtr = ConvertType(FieldType); |
| 2062 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2063 | llvm::Value *BaseAddrPtr = |
Fariborz Jahanian | f800f6c | 2009-08-20 20:54:15 +0000 | [diff] [blame] | 2064 | Builder.CreateBitCast(LHS.getAddress(), BasePtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2065 | EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()), |
Fariborz Jahanian | f800f6c | 2009-08-20 20:54:15 +0000 | [diff] [blame] | 2066 | Array, BaseAddrPtr); |
| 2067 | } |
| 2068 | else |
| 2069 | EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()), |
| 2070 | Dtor_Complete, LHS.getAddress()); |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 2071 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2072 | |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 2073 | llvm::SmallVector<CXXRecordDecl*, 4> DestructedBases; |
| 2074 | for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(); |
| 2075 | Base != ClassDecl->bases_end(); ++Base) { |
| 2076 | // FIXME. copy assignment of virtual base NYI |
| 2077 | if (Base->isVirtual()) |
| 2078 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2079 | |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 2080 | CXXRecordDecl *BaseClassDecl |
| 2081 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 2082 | if (BaseClassDecl->hasTrivialDestructor()) |
| 2083 | continue; |
| 2084 | DestructedBases.push_back(BaseClassDecl); |
| 2085 | } |
| 2086 | if (DestructedBases.empty()) |
| 2087 | return; |
| 2088 | for (int i = DestructedBases.size() -1; i >= 0; --i) { |
| 2089 | CXXRecordDecl *BaseClassDecl = DestructedBases[i]; |
Anders Carlsson | 5a0f49e | 2009-09-12 04:26:35 +0000 | [diff] [blame] | 2090 | llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(), |
| 2091 | ClassDecl,BaseClassDecl, |
| 2092 | /*NullCheckValue=*/false); |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 2093 | EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()), |
| 2094 | Dtor_Complete, V); |
| 2095 | } |
Fariborz Jahanian | 426cc38 | 2009-07-30 17:49:11 +0000 | [diff] [blame] | 2096 | } |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 2097 | |
Anders Carlsson | de1d26b | 2009-09-14 05:32:02 +0000 | [diff] [blame] | 2098 | void CodeGenFunction::SynthesizeDefaultDestructor(const CXXDestructorDecl *Dtor, |
| 2099 | CXXDtorType DtorType, |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 2100 | llvm::Function *Fn, |
| 2101 | const FunctionArgList &Args) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2102 | |
Anders Carlsson | 0ff8baf | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 2103 | const CXXRecordDecl *ClassDecl = Dtor->getParent(); |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 2104 | assert(!ClassDecl->hasUserDeclaredDestructor() && |
| 2105 | "SynthesizeDefaultDestructor - destructor has user declaration"); |
| 2106 | (void) ClassDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2107 | |
Anders Carlsson | de1d26b | 2009-09-14 05:32:02 +0000 | [diff] [blame] | 2108 | StartFunction(GlobalDecl(Dtor, DtorType), Dtor->getResultType(), Fn, Args, |
| 2109 | SourceLocation()); |
| 2110 | EmitDtorEpilogue(Dtor, DtorType); |
Fariborz Jahanian | 0880bac | 2009-08-17 19:04:50 +0000 | [diff] [blame] | 2111 | FinishFunction(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2112 | } |
Anders Carlsson | 6815e94 | 2009-09-27 18:58:34 +0000 | [diff] [blame] | 2113 | |
| 2114 | // FIXME: Move this to CGCXXStmt.cpp |
| 2115 | void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) { |
| 2116 | // FIXME: We need to do more here. |
| 2117 | EmitStmt(S.getTryBlock()); |
| 2118 | } |