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