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