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