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