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