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