Anders Carlsson | 5b95592 | 2009-11-24 05:51:11 +0000 | [diff] [blame] | 1 | //===--- CGExprCXX.cpp - Emit LLVM Code for C++ expressions ---------------===// |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 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 code generation of C++ expressions |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Devang Patel | c69e1cf | 2010-09-30 19:05:55 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/CodeGenOptions.h" |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 15 | #include "CodeGenFunction.h" |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 16 | #include "CGCXXABI.h" |
Fariborz Jahanian | 842ddd0 | 2010-05-20 21:38:57 +0000 | [diff] [blame] | 17 | #include "CGObjCRuntime.h" |
Devang Patel | c69e1cf | 2010-09-30 19:05:55 +0000 | [diff] [blame] | 18 | #include "CGDebugInfo.h" |
Chris Lattner | 6c552c1 | 2010-07-20 20:19:24 +0000 | [diff] [blame] | 19 | #include "llvm/Intrinsics.h" |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | using namespace CodeGen; |
| 22 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 23 | RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD, |
| 24 | llvm::Value *Callee, |
| 25 | ReturnValueSlot ReturnValue, |
| 26 | llvm::Value *This, |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 27 | llvm::Value *VTT, |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 28 | CallExpr::const_arg_iterator ArgBeg, |
| 29 | CallExpr::const_arg_iterator ArgEnd) { |
| 30 | assert(MD->isInstance() && |
| 31 | "Trying to emit a member call expr on a static method!"); |
| 32 | |
| 33 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
| 34 | |
| 35 | CallArgList Args; |
| 36 | |
| 37 | // Push the this ptr. |
| 38 | Args.push_back(std::make_pair(RValue::get(This), |
| 39 | MD->getThisType(getContext()))); |
| 40 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 41 | // If there is a VTT parameter, emit it. |
| 42 | if (VTT) { |
| 43 | QualType T = getContext().getPointerType(getContext().VoidPtrTy); |
| 44 | Args.push_back(std::make_pair(RValue::get(VTT), T)); |
| 45 | } |
| 46 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 47 | // And the rest of the call args |
| 48 | EmitCallArgs(Args, FPT, ArgBeg, ArgEnd); |
| 49 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 50 | QualType ResultType = FPT->getResultType(); |
Tilmann Scheller | 9c6082f | 2011-03-02 21:36:49 +0000 | [diff] [blame] | 51 | return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args, |
| 52 | FPT->getExtInfo()), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 53 | Callee, ReturnValue, Args, MD); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Anders Carlsson | 1679f5a | 2011-01-29 03:52:01 +0000 | [diff] [blame] | 56 | static const CXXRecordDecl *getMostDerivedClassDecl(const Expr *Base) { |
Anders Carlsson | 268ab8c | 2011-01-29 05:04:11 +0000 | [diff] [blame] | 57 | const Expr *E = Base; |
| 58 | |
| 59 | while (true) { |
| 60 | E = E->IgnoreParens(); |
| 61 | if (const CastExpr *CE = dyn_cast<CastExpr>(E)) { |
| 62 | if (CE->getCastKind() == CK_DerivedToBase || |
| 63 | CE->getCastKind() == CK_UncheckedDerivedToBase || |
| 64 | CE->getCastKind() == CK_NoOp) { |
| 65 | E = CE->getSubExpr(); |
| 66 | continue; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | break; |
| 71 | } |
| 72 | |
| 73 | QualType DerivedType = E->getType(); |
Anders Carlsson | 1679f5a | 2011-01-29 03:52:01 +0000 | [diff] [blame] | 74 | if (const PointerType *PTy = DerivedType->getAs<PointerType>()) |
| 75 | DerivedType = PTy->getPointeeType(); |
| 76 | |
| 77 | return cast<CXXRecordDecl>(DerivedType->castAs<RecordType>()->getDecl()); |
| 78 | } |
| 79 | |
Anders Carlsson | cd0b32e | 2011-04-10 18:20:53 +0000 | [diff] [blame] | 80 | // FIXME: Ideally Expr::IgnoreParenNoopCasts should do this, but it doesn't do |
| 81 | // quite what we want. |
| 82 | static const Expr *skipNoOpCastsAndParens(const Expr *E) { |
| 83 | while (true) { |
| 84 | if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
| 85 | E = PE->getSubExpr(); |
| 86 | continue; |
| 87 | } |
| 88 | |
| 89 | if (const CastExpr *CE = dyn_cast<CastExpr>(E)) { |
| 90 | if (CE->getCastKind() == CK_NoOp) { |
| 91 | E = CE->getSubExpr(); |
| 92 | continue; |
| 93 | } |
| 94 | } |
| 95 | if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
| 96 | if (UO->getOpcode() == UO_Extension) { |
| 97 | E = UO->getSubExpr(); |
| 98 | continue; |
| 99 | } |
| 100 | } |
| 101 | return E; |
| 102 | } |
| 103 | } |
| 104 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 105 | /// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given |
| 106 | /// expr can be devirtualized. |
Fariborz Jahanian | 7ac0ff2 | 2011-01-21 01:04:41 +0000 | [diff] [blame] | 107 | static bool canDevirtualizeMemberFunctionCalls(ASTContext &Context, |
| 108 | const Expr *Base, |
Anders Carlsson | bd2bfae | 2010-10-27 13:28:46 +0000 | [diff] [blame] | 109 | const CXXMethodDecl *MD) { |
| 110 | |
Anders Carlsson | 1679f5a | 2011-01-29 03:52:01 +0000 | [diff] [blame] | 111 | // When building with -fapple-kext, all calls must go through the vtable since |
| 112 | // the kernel linker can do runtime patching of vtables. |
Fariborz Jahanian | 7ac0ff2 | 2011-01-21 01:04:41 +0000 | [diff] [blame] | 113 | if (Context.getLangOptions().AppleKext) |
| 114 | return false; |
| 115 | |
Anders Carlsson | 1679f5a | 2011-01-29 03:52:01 +0000 | [diff] [blame] | 116 | // If the most derived class is marked final, we know that no subclass can |
| 117 | // override this member function and so we can devirtualize it. For example: |
| 118 | // |
| 119 | // struct A { virtual void f(); } |
| 120 | // struct B final : A { }; |
| 121 | // |
| 122 | // void f(B *b) { |
| 123 | // b->f(); |
| 124 | // } |
| 125 | // |
| 126 | const CXXRecordDecl *MostDerivedClassDecl = getMostDerivedClassDecl(Base); |
| 127 | if (MostDerivedClassDecl->hasAttr<FinalAttr>()) |
| 128 | return true; |
| 129 | |
Anders Carlsson | f89e042 | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 130 | // If the member function is marked 'final', we know that it can't be |
Anders Carlsson | d66f428 | 2010-10-27 13:34:43 +0000 | [diff] [blame] | 131 | // overridden and can therefore devirtualize it. |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 132 | if (MD->hasAttr<FinalAttr>()) |
Anders Carlsson | bd2bfae | 2010-10-27 13:28:46 +0000 | [diff] [blame] | 133 | return true; |
Anders Carlsson | d66f428 | 2010-10-27 13:34:43 +0000 | [diff] [blame] | 134 | |
Anders Carlsson | f89e042 | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 135 | // Similarly, if the class itself is marked 'final' it can't be overridden |
| 136 | // and we can therefore devirtualize the member function call. |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 137 | if (MD->getParent()->hasAttr<FinalAttr>()) |
Anders Carlsson | d66f428 | 2010-10-27 13:34:43 +0000 | [diff] [blame] | 138 | return true; |
| 139 | |
Anders Carlsson | cd0b32e | 2011-04-10 18:20:53 +0000 | [diff] [blame] | 140 | Base = skipNoOpCastsAndParens(Base); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 141 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) { |
| 142 | if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) { |
| 143 | // This is a record decl. We know the type and can devirtualize it. |
| 144 | return VD->getType()->isRecordType(); |
| 145 | } |
| 146 | |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | // We can always devirtualize calls on temporary object expressions. |
Eli Friedman | 6997aae | 2010-01-31 20:58:15 +0000 | [diff] [blame] | 151 | if (isa<CXXConstructExpr>(Base)) |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 152 | return true; |
| 153 | |
| 154 | // And calls on bound temporaries. |
| 155 | if (isa<CXXBindTemporaryExpr>(Base)) |
| 156 | return true; |
| 157 | |
| 158 | // Check if this is a call expr that returns a record type. |
| 159 | if (const CallExpr *CE = dyn_cast<CallExpr>(Base)) |
| 160 | return CE->getCallReturnType()->isRecordType(); |
Anders Carlsson | bd2bfae | 2010-10-27 13:28:46 +0000 | [diff] [blame] | 161 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 162 | // We can't devirtualize the call. |
| 163 | return false; |
| 164 | } |
| 165 | |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 166 | // Note: This function also emit constructor calls to support a MSVC |
| 167 | // extensions allowing explicit constructor function call. |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 168 | RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE, |
| 169 | ReturnValueSlot ReturnValue) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 170 | const Expr *callee = CE->getCallee()->IgnoreParens(); |
| 171 | |
| 172 | if (isa<BinaryOperator>(callee)) |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 173 | return EmitCXXMemberPointerCallExpr(CE, ReturnValue); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 174 | |
| 175 | const MemberExpr *ME = cast<MemberExpr>(callee); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 176 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl()); |
| 177 | |
Devang Patel | c69e1cf | 2010-09-30 19:05:55 +0000 | [diff] [blame] | 178 | CGDebugInfo *DI = getDebugInfo(); |
Devang Patel | 6802027 | 2010-10-22 18:56:27 +0000 | [diff] [blame] | 179 | if (DI && CGM.getCodeGenOpts().LimitDebugInfo |
| 180 | && !isa<CallExpr>(ME->getBase())) { |
Devang Patel | c69e1cf | 2010-09-30 19:05:55 +0000 | [diff] [blame] | 181 | QualType PQTy = ME->getBase()->IgnoreParenImpCasts()->getType(); |
| 182 | if (const PointerType * PTy = dyn_cast<PointerType>(PQTy)) { |
| 183 | DI->getOrCreateRecordType(PTy->getPointeeType(), |
| 184 | MD->getParent()->getLocation()); |
| 185 | } |
| 186 | } |
| 187 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 188 | if (MD->isStatic()) { |
| 189 | // The method is static, emit it as we would a regular call. |
| 190 | llvm::Value *Callee = CGM.GetAddrOfFunction(MD); |
| 191 | return EmitCall(getContext().getPointerType(MD->getType()), Callee, |
| 192 | ReturnValue, CE->arg_begin(), CE->arg_end()); |
| 193 | } |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 194 | |
John McCall | fc40028 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 195 | // Compute the object pointer. |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 196 | llvm::Value *This; |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 197 | if (ME->isArrow()) |
| 198 | This = EmitScalarExpr(ME->getBase()); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 199 | else |
| 200 | This = EmitLValue(ME->getBase()).getAddress(); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 201 | |
John McCall | fc40028 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 202 | if (MD->isTrivial()) { |
| 203 | if (isa<CXXDestructorDecl>(MD)) return RValue::get(0); |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 204 | if (isa<CXXConstructorDecl>(MD) && |
| 205 | cast<CXXConstructorDecl>(MD)->isDefaultConstructor()) |
| 206 | return RValue::get(0); |
John McCall | fc40028 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 207 | |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 208 | if (MD->isCopyAssignmentOperator()) { |
| 209 | // We don't like to generate the trivial copy assignment operator when |
| 210 | // it isn't necessary; just produce the proper effect here. |
| 211 | llvm::Value *RHS = EmitLValue(*CE->arg_begin()).getAddress(); |
| 212 | EmitAggregateCopy(This, RHS, CE->getType()); |
| 213 | return RValue::get(This); |
| 214 | } |
| 215 | |
| 216 | if (isa<CXXConstructorDecl>(MD) && |
| 217 | cast<CXXConstructorDecl>(MD)->isCopyConstructor()) { |
| 218 | llvm::Value *RHS = EmitLValue(*CE->arg_begin()).getAddress(); |
| 219 | EmitSynthesizedCXXCopyCtorCall(cast<CXXConstructorDecl>(MD), This, RHS, |
| 220 | CE->arg_begin(), CE->arg_end()); |
| 221 | return RValue::get(This); |
| 222 | } |
| 223 | llvm_unreachable("unknown trivial member function"); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 224 | } |
| 225 | |
John McCall | fc40028 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 226 | // Compute the function type we're calling. |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 227 | const CGFunctionInfo *FInfo = 0; |
| 228 | if (isa<CXXDestructorDecl>(MD)) |
| 229 | FInfo = &CGM.getTypes().getFunctionInfo(cast<CXXDestructorDecl>(MD), |
| 230 | Dtor_Complete); |
| 231 | else if (isa<CXXConstructorDecl>(MD)) |
| 232 | FInfo = &CGM.getTypes().getFunctionInfo(cast<CXXConstructorDecl>(MD), |
| 233 | Ctor_Complete); |
| 234 | else |
| 235 | FInfo = &CGM.getTypes().getFunctionInfo(MD); |
John McCall | fc40028 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 236 | |
| 237 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
| 238 | const llvm::Type *Ty |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 239 | = CGM.getTypes().GetFunctionType(*FInfo, FPT->isVariadic()); |
John McCall | fc40028 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 240 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 241 | // C++ [class.virtual]p12: |
| 242 | // Explicit qualification with the scope operator (5.1) suppresses the |
| 243 | // virtual call mechanism. |
| 244 | // |
| 245 | // We also don't emit a virtual call if the base expression has a record type |
| 246 | // because then we know what the type is. |
Fariborz Jahanian | 2726267 | 2011-01-20 17:19:02 +0000 | [diff] [blame] | 247 | bool UseVirtualCall; |
Fariborz Jahanian | 7ac0ff2 | 2011-01-21 01:04:41 +0000 | [diff] [blame] | 248 | UseVirtualCall = MD->isVirtual() && !ME->hasQualifier() |
| 249 | && !canDevirtualizeMemberFunctionCalls(getContext(), |
| 250 | ME->getBase(), MD); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 251 | llvm::Value *Callee; |
John McCall | fc40028 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 252 | if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD)) { |
| 253 | if (UseVirtualCall) { |
| 254 | Callee = BuildVirtualCall(Dtor, Dtor_Complete, This, Ty); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 255 | } else { |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 256 | if (getContext().getLangOptions().AppleKext && |
| 257 | MD->isVirtual() && |
| 258 | ME->hasQualifier()) |
Fariborz Jahanian | 771c678 | 2011-02-03 19:27:17 +0000 | [diff] [blame] | 259 | Callee = BuildAppleKextVirtualCall(MD, ME->getQualifier(), Ty); |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 260 | else |
| 261 | Callee = CGM.GetAddrOfFunction(GlobalDecl(Dtor, Dtor_Complete), Ty); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 262 | } |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 263 | } else if (const CXXConstructorDecl *Ctor = |
| 264 | dyn_cast<CXXConstructorDecl>(MD)) { |
| 265 | Callee = CGM.GetAddrOfFunction(GlobalDecl(Ctor, Ctor_Complete), Ty); |
John McCall | fc40028 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 266 | } else if (UseVirtualCall) { |
Fariborz Jahanian | 2726267 | 2011-01-20 17:19:02 +0000 | [diff] [blame] | 267 | Callee = BuildVirtualCall(MD, This, Ty); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 268 | } else { |
Fariborz Jahanian | 7ac0ff2 | 2011-01-21 01:04:41 +0000 | [diff] [blame] | 269 | if (getContext().getLangOptions().AppleKext && |
Fariborz Jahanian | a50e33e | 2011-01-28 23:42:29 +0000 | [diff] [blame] | 270 | MD->isVirtual() && |
Fariborz Jahanian | 7ac0ff2 | 2011-01-21 01:04:41 +0000 | [diff] [blame] | 271 | ME->hasQualifier()) |
Fariborz Jahanian | 771c678 | 2011-02-03 19:27:17 +0000 | [diff] [blame] | 272 | Callee = BuildAppleKextVirtualCall(MD, ME->getQualifier(), Ty); |
Fariborz Jahanian | 7ac0ff2 | 2011-01-21 01:04:41 +0000 | [diff] [blame] | 273 | else |
| 274 | Callee = CGM.GetAddrOfFunction(MD, Ty); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 277 | return EmitCXXMemberCall(MD, Callee, ReturnValue, This, /*VTT=*/0, |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 278 | CE->arg_begin(), CE->arg_end()); |
| 279 | } |
| 280 | |
| 281 | RValue |
| 282 | CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E, |
| 283 | ReturnValueSlot ReturnValue) { |
| 284 | const BinaryOperator *BO = |
| 285 | cast<BinaryOperator>(E->getCallee()->IgnoreParens()); |
| 286 | const Expr *BaseExpr = BO->getLHS(); |
| 287 | const Expr *MemFnExpr = BO->getRHS(); |
| 288 | |
| 289 | const MemberPointerType *MPT = |
| 290 | MemFnExpr->getType()->getAs<MemberPointerType>(); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 291 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 292 | const FunctionProtoType *FPT = |
| 293 | MPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 294 | const CXXRecordDecl *RD = |
| 295 | cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl()); |
| 296 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 297 | // Get the member function pointer. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 298 | llvm::Value *MemFnPtr = EmitScalarExpr(MemFnExpr); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 299 | |
| 300 | // Emit the 'this' pointer. |
| 301 | llvm::Value *This; |
| 302 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 303 | if (BO->getOpcode() == BO_PtrMemI) |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 304 | This = EmitScalarExpr(BaseExpr); |
| 305 | else |
| 306 | This = EmitLValue(BaseExpr).getAddress(); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 307 | |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 308 | // Ask the ABI to load the callee. Note that This is modified. |
| 309 | llvm::Value *Callee = |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 310 | CGM.getCXXABI().EmitLoadOfMemberFunctionPointer(*this, This, MemFnPtr, MPT); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 311 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 312 | CallArgList Args; |
| 313 | |
| 314 | QualType ThisType = |
| 315 | getContext().getPointerType(getContext().getTagDeclType(RD)); |
| 316 | |
| 317 | // Push the this ptr. |
| 318 | Args.push_back(std::make_pair(RValue::get(This), ThisType)); |
| 319 | |
| 320 | // And the rest of the call args |
| 321 | EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end()); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 322 | const FunctionType *BO_FPT = BO->getType()->getAs<FunctionProtoType>(); |
Tilmann Scheller | 9c6082f | 2011-03-02 21:36:49 +0000 | [diff] [blame] | 323 | return EmitCall(CGM.getTypes().getFunctionInfo(Args, BO_FPT), Callee, |
| 324 | ReturnValue, Args); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | RValue |
| 328 | CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E, |
| 329 | const CXXMethodDecl *MD, |
| 330 | ReturnValueSlot ReturnValue) { |
| 331 | assert(MD->isInstance() && |
| 332 | "Trying to emit a member call expr on a static method!"); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 333 | LValue LV = EmitLValue(E->getArg(0)); |
| 334 | llvm::Value *This = LV.getAddress(); |
| 335 | |
Douglas Gregor | 3e9438b | 2010-09-27 22:37:28 +0000 | [diff] [blame] | 336 | if (MD->isCopyAssignmentOperator()) { |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 337 | const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext()); |
| 338 | if (ClassDecl->hasTrivialCopyAssignment()) { |
| 339 | assert(!ClassDecl->hasUserDeclaredCopyAssignment() && |
| 340 | "EmitCXXOperatorMemberCallExpr - user declared copy assignment"); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 341 | llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress(); |
| 342 | QualType Ty = E->getType(); |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 343 | EmitAggregateCopy(This, Src, Ty); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 344 | return RValue::get(This); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
| 349 | const llvm::Type *Ty = |
| 350 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), |
| 351 | FPT->isVariadic()); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 352 | llvm::Value *Callee; |
Fariborz Jahanian | 2726267 | 2011-01-20 17:19:02 +0000 | [diff] [blame] | 353 | if (MD->isVirtual() && |
Fariborz Jahanian | 7ac0ff2 | 2011-01-21 01:04:41 +0000 | [diff] [blame] | 354 | !canDevirtualizeMemberFunctionCalls(getContext(), |
| 355 | E->getArg(0), MD)) |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 356 | Callee = BuildVirtualCall(MD, This, Ty); |
| 357 | else |
| 358 | Callee = CGM.GetAddrOfFunction(MD, Ty); |
| 359 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 360 | return EmitCXXMemberCall(MD, Callee, ReturnValue, This, /*VTT=*/0, |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 361 | E->arg_begin() + 1, E->arg_end()); |
| 362 | } |
| 363 | |
| 364 | void |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 365 | CodeGenFunction::EmitCXXConstructExpr(const CXXConstructExpr *E, |
| 366 | AggValueSlot Dest) { |
| 367 | assert(!Dest.isIgnored() && "Must have a destination!"); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 368 | const CXXConstructorDecl *CD = E->getConstructor(); |
Douglas Gregor | 759e41b | 2010-08-22 16:15:35 +0000 | [diff] [blame] | 369 | |
| 370 | // If we require zero initialization before (or instead of) calling the |
| 371 | // constructor, as can be the case with a non-user-provided default |
| 372 | // constructor, emit the zero initialization now. |
| 373 | if (E->requiresZeroInitialization()) |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 374 | EmitNullInitialization(Dest.getAddr(), E->getType()); |
Douglas Gregor | 759e41b | 2010-08-22 16:15:35 +0000 | [diff] [blame] | 375 | |
| 376 | // If this is a call to a trivial default constructor, do nothing. |
| 377 | if (CD->isTrivial() && CD->isDefaultConstructor()) |
| 378 | return; |
| 379 | |
John McCall | fc1e6c7 | 2010-09-18 00:58:34 +0000 | [diff] [blame] | 380 | // Elide the constructor if we're constructing from a temporary. |
| 381 | // The temporary check is required because Sema sets this on NRVO |
| 382 | // returns. |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 383 | if (getContext().getLangOptions().ElideConstructors && E->isElidable()) { |
John McCall | fc1e6c7 | 2010-09-18 00:58:34 +0000 | [diff] [blame] | 384 | assert(getContext().hasSameUnqualifiedType(E->getType(), |
| 385 | E->getArg(0)->getType())); |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 386 | if (E->getArg(0)->isTemporaryObject(getContext(), CD->getParent())) { |
| 387 | EmitAggExpr(E->getArg(0), Dest); |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 388 | return; |
| 389 | } |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 390 | } |
Douglas Gregor | 759e41b | 2010-08-22 16:15:35 +0000 | [diff] [blame] | 391 | |
| 392 | const ConstantArrayType *Array |
| 393 | = getContext().getAsConstantArrayType(E->getType()); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 394 | if (Array) { |
| 395 | QualType BaseElementTy = getContext().getBaseElementType(Array); |
| 396 | const llvm::Type *BasePtr = ConvertType(BaseElementTy); |
| 397 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
| 398 | llvm::Value *BaseAddrPtr = |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 399 | Builder.CreateBitCast(Dest.getAddr(), BasePtr); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 400 | |
| 401 | EmitCXXAggrConstructorCall(CD, Array, BaseAddrPtr, |
| 402 | E->arg_begin(), E->arg_end()); |
| 403 | } |
Anders Carlsson | 155ed4a | 2010-05-02 23:20:53 +0000 | [diff] [blame] | 404 | else { |
| 405 | CXXCtorType Type = |
| 406 | (E->getConstructionKind() == CXXConstructExpr::CK_Complete) |
| 407 | ? Ctor_Complete : Ctor_Base; |
| 408 | bool ForVirtualBase = |
| 409 | E->getConstructionKind() == CXXConstructExpr::CK_VirtualBase; |
| 410 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 411 | // Call the constructor. |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 412 | EmitCXXConstructorCall(CD, Type, ForVirtualBase, Dest.getAddr(), |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 413 | E->arg_begin(), E->arg_end()); |
Anders Carlsson | 155ed4a | 2010-05-02 23:20:53 +0000 | [diff] [blame] | 414 | } |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 417 | void |
| 418 | CodeGenFunction::EmitSynthesizedCXXCopyCtor(llvm::Value *Dest, |
| 419 | llvm::Value *Src, |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 420 | const Expr *Exp) { |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 421 | if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(Exp)) |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 422 | Exp = E->getSubExpr(); |
| 423 | assert(isa<CXXConstructExpr>(Exp) && |
| 424 | "EmitSynthesizedCXXCopyCtor - unknown copy ctor expr"); |
| 425 | const CXXConstructExpr* E = cast<CXXConstructExpr>(Exp); |
| 426 | const CXXConstructorDecl *CD = E->getConstructor(); |
| 427 | RunCleanupsScope Scope(*this); |
| 428 | |
| 429 | // If we require zero initialization before (or instead of) calling the |
| 430 | // constructor, as can be the case with a non-user-provided default |
| 431 | // constructor, emit the zero initialization now. |
| 432 | // FIXME. Do I still need this for a copy ctor synthesis? |
| 433 | if (E->requiresZeroInitialization()) |
| 434 | EmitNullInitialization(Dest, E->getType()); |
| 435 | |
Chandler Carruth | 858a546 | 2010-11-15 13:54:43 +0000 | [diff] [blame] | 436 | assert(!getContext().getAsConstantArrayType(E->getType()) |
| 437 | && "EmitSynthesizedCXXCopyCtor - Copied-in Array"); |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 438 | EmitSynthesizedCXXCopyCtorCall(CD, Dest, Src, |
| 439 | E->arg_begin(), E->arg_end()); |
| 440 | } |
| 441 | |
John McCall | 5172ed9 | 2010-08-23 01:17:59 +0000 | [diff] [blame] | 442 | /// Check whether the given operator new[] is the global placement |
| 443 | /// operator new[]. |
| 444 | static bool IsPlacementOperatorNewArray(ASTContext &Ctx, |
| 445 | const FunctionDecl *Fn) { |
| 446 | // Must be in global scope. Note that allocation functions can't be |
| 447 | // declared in namespaces. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 448 | if (!Fn->getDeclContext()->getRedeclContext()->isFileContext()) |
John McCall | 5172ed9 | 2010-08-23 01:17:59 +0000 | [diff] [blame] | 449 | return false; |
| 450 | |
| 451 | // Signature must be void *operator new[](size_t, void*). |
| 452 | // The size_t is common to all operator new[]s. |
| 453 | if (Fn->getNumParams() != 2) |
| 454 | return false; |
| 455 | |
| 456 | CanQualType ParamType = Ctx.getCanonicalType(Fn->getParamDecl(1)->getType()); |
| 457 | return (ParamType == Ctx.VoidPtrTy); |
| 458 | } |
| 459 | |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 460 | static CharUnits CalculateCookiePadding(CodeGenFunction &CGF, |
| 461 | const CXXNewExpr *E) { |
Anders Carlsson | 871d078 | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 462 | if (!E->isArray()) |
Ken Dyck | caf647c | 2010-01-26 19:44:24 +0000 | [diff] [blame] | 463 | return CharUnits::Zero(); |
Anders Carlsson | 871d078 | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 464 | |
Anders Carlsson | dd93755 | 2009-12-13 20:34:34 +0000 | [diff] [blame] | 465 | // No cookie is required if the new operator being used is |
| 466 | // ::operator new[](size_t, void*). |
| 467 | const FunctionDecl *OperatorNew = E->getOperatorNew(); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 468 | if (IsPlacementOperatorNewArray(CGF.getContext(), OperatorNew)) |
John McCall | 5172ed9 | 2010-08-23 01:17:59 +0000 | [diff] [blame] | 469 | return CharUnits::Zero(); |
| 470 | |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 471 | return CGF.CGM.getCXXABI().GetArrayCookieSize(E); |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Fariborz Jahanian | ceb43b6 | 2010-03-24 16:57:01 +0000 | [diff] [blame] | 474 | static llvm::Value *EmitCXXNewAllocSize(ASTContext &Context, |
Chris Lattner | defe8b2 | 2010-07-20 18:45:57 +0000 | [diff] [blame] | 475 | CodeGenFunction &CGF, |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 476 | const CXXNewExpr *E, |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 477 | llvm::Value *&NumElements, |
| 478 | llvm::Value *&SizeWithoutCookie) { |
Argyrios Kyrtzidis | e7ab92e | 2010-08-26 15:23:38 +0000 | [diff] [blame] | 479 | QualType ElemType = E->getAllocatedType(); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 480 | |
| 481 | const llvm::IntegerType *SizeTy = |
| 482 | cast<llvm::IntegerType>(CGF.ConvertType(CGF.getContext().getSizeType())); |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 483 | |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 484 | CharUnits TypeSize = CGF.getContext().getTypeSizeInChars(ElemType); |
| 485 | |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 486 | if (!E->isArray()) { |
| 487 | SizeWithoutCookie = llvm::ConstantInt::get(SizeTy, TypeSize.getQuantity()); |
| 488 | return SizeWithoutCookie; |
| 489 | } |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 490 | |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 491 | // Figure out the cookie size. |
| 492 | CharUnits CookieSize = CalculateCookiePadding(CGF, E); |
| 493 | |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 494 | // Emit the array size expression. |
Argyrios Kyrtzidis | e7ab92e | 2010-08-26 15:23:38 +0000 | [diff] [blame] | 495 | // We multiply the size of all dimensions for NumElements. |
| 496 | // e.g for 'int[2][3]', ElemType is 'int' and NumElements is 6. |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 497 | NumElements = CGF.EmitScalarExpr(E->getArraySize()); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 498 | assert(NumElements->getType() == SizeTy && "element count not a size_t"); |
| 499 | |
| 500 | uint64_t ArraySizeMultiplier = 1; |
Argyrios Kyrtzidis | e7ab92e | 2010-08-26 15:23:38 +0000 | [diff] [blame] | 501 | while (const ConstantArrayType *CAT |
| 502 | = CGF.getContext().getAsConstantArrayType(ElemType)) { |
| 503 | ElemType = CAT->getElementType(); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 504 | ArraySizeMultiplier *= CAT->getSize().getZExtValue(); |
Argyrios Kyrtzidis | e7ab92e | 2010-08-26 15:23:38 +0000 | [diff] [blame] | 505 | } |
| 506 | |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 507 | llvm::Value *Size; |
Chris Lattner | 83252dc | 2010-07-20 21:07:09 +0000 | [diff] [blame] | 508 | |
Chris Lattner | 806941e | 2010-07-20 21:55:52 +0000 | [diff] [blame] | 509 | // If someone is doing 'new int[42]' there is no need to do a dynamic check. |
| 510 | // Don't bloat the -O0 code. |
| 511 | if (llvm::ConstantInt *NumElementsC = |
| 512 | dyn_cast<llvm::ConstantInt>(NumElements)) { |
Chris Lattner | 806941e | 2010-07-20 21:55:52 +0000 | [diff] [blame] | 513 | llvm::APInt NEC = NumElementsC->getValue(); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 514 | unsigned SizeWidth = NEC.getBitWidth(); |
| 515 | |
| 516 | // Determine if there is an overflow here by doing an extended multiply. |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 517 | NEC = NEC.zext(SizeWidth*2); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 518 | llvm::APInt SC(SizeWidth*2, TypeSize.getQuantity()); |
Chris Lattner | 806941e | 2010-07-20 21:55:52 +0000 | [diff] [blame] | 519 | SC *= NEC; |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 520 | |
| 521 | if (!CookieSize.isZero()) { |
| 522 | // Save the current size without a cookie. We don't care if an |
| 523 | // overflow's already happened because SizeWithoutCookie isn't |
| 524 | // used if the allocator returns null or throws, as it should |
| 525 | // always do on an overflow. |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 526 | llvm::APInt SWC = SC.trunc(SizeWidth); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 527 | SizeWithoutCookie = llvm::ConstantInt::get(SizeTy, SWC); |
| 528 | |
| 529 | // Add the cookie size. |
| 530 | SC += llvm::APInt(SizeWidth*2, CookieSize.getQuantity()); |
Chris Lattner | 806941e | 2010-07-20 21:55:52 +0000 | [diff] [blame] | 531 | } |
| 532 | |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 533 | if (SC.countLeadingZeros() >= SizeWidth) { |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 534 | SC = SC.trunc(SizeWidth); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 535 | Size = llvm::ConstantInt::get(SizeTy, SC); |
| 536 | } else { |
| 537 | // On overflow, produce a -1 so operator new throws. |
| 538 | Size = llvm::Constant::getAllOnesValue(SizeTy); |
| 539 | } |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 540 | |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 541 | // Scale NumElements while we're at it. |
| 542 | uint64_t N = NEC.getZExtValue() * ArraySizeMultiplier; |
| 543 | NumElements = llvm::ConstantInt::get(SizeTy, N); |
| 544 | |
| 545 | // Otherwise, we don't need to do an overflow-checked multiplication if |
| 546 | // we're multiplying by one. |
| 547 | } else if (TypeSize.isOne()) { |
| 548 | assert(ArraySizeMultiplier == 1); |
| 549 | |
| 550 | Size = NumElements; |
| 551 | |
| 552 | // If we need a cookie, add its size in with an overflow check. |
| 553 | // This is maybe a little paranoid. |
| 554 | if (!CookieSize.isZero()) { |
| 555 | SizeWithoutCookie = Size; |
| 556 | |
| 557 | llvm::Value *CookieSizeV |
| 558 | = llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity()); |
| 559 | |
| 560 | const llvm::Type *Types[] = { SizeTy }; |
| 561 | llvm::Value *UAddF |
| 562 | = CGF.CGM.getIntrinsic(llvm::Intrinsic::uadd_with_overflow, Types, 1); |
| 563 | llvm::Value *AddRes |
| 564 | = CGF.Builder.CreateCall2(UAddF, Size, CookieSizeV); |
| 565 | |
| 566 | Size = CGF.Builder.CreateExtractValue(AddRes, 0); |
| 567 | llvm::Value *DidOverflow = CGF.Builder.CreateExtractValue(AddRes, 1); |
| 568 | Size = CGF.Builder.CreateSelect(DidOverflow, |
| 569 | llvm::ConstantInt::get(SizeTy, -1), |
| 570 | Size); |
| 571 | } |
| 572 | |
| 573 | // Otherwise use the int.umul.with.overflow intrinsic. |
| 574 | } else { |
| 575 | llvm::Value *OutermostElementSize |
| 576 | = llvm::ConstantInt::get(SizeTy, TypeSize.getQuantity()); |
| 577 | |
| 578 | llvm::Value *NumOutermostElements = NumElements; |
| 579 | |
| 580 | // Scale NumElements by the array size multiplier. This might |
| 581 | // overflow, but only if the multiplication below also overflows, |
| 582 | // in which case this multiplication isn't used. |
| 583 | if (ArraySizeMultiplier != 1) |
| 584 | NumElements = CGF.Builder.CreateMul(NumElements, |
| 585 | llvm::ConstantInt::get(SizeTy, ArraySizeMultiplier)); |
| 586 | |
| 587 | // The requested size of the outermost array is non-constant. |
| 588 | // Multiply that by the static size of the elements of that array; |
| 589 | // on unsigned overflow, set the size to -1 to trigger an |
| 590 | // exception from the allocation routine. This is sufficient to |
| 591 | // prevent buffer overruns from the allocator returning a |
| 592 | // seemingly valid pointer to insufficient space. This idea comes |
| 593 | // originally from MSVC, and GCC has an open bug requesting |
| 594 | // similar behavior: |
| 595 | // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19351 |
| 596 | // |
| 597 | // This will not be sufficient for C++0x, which requires a |
| 598 | // specific exception class (std::bad_array_new_length). |
| 599 | // That will require ABI support that has not yet been specified. |
| 600 | const llvm::Type *Types[] = { SizeTy }; |
| 601 | llvm::Value *UMulF |
| 602 | = CGF.CGM.getIntrinsic(llvm::Intrinsic::umul_with_overflow, Types, 1); |
| 603 | llvm::Value *MulRes = CGF.Builder.CreateCall2(UMulF, NumOutermostElements, |
| 604 | OutermostElementSize); |
| 605 | |
| 606 | // The overflow bit. |
| 607 | llvm::Value *DidOverflow = CGF.Builder.CreateExtractValue(MulRes, 1); |
| 608 | |
| 609 | // The result of the multiplication. |
| 610 | Size = CGF.Builder.CreateExtractValue(MulRes, 0); |
| 611 | |
| 612 | // If we have a cookie, we need to add that size in, too. |
| 613 | if (!CookieSize.isZero()) { |
| 614 | SizeWithoutCookie = Size; |
| 615 | |
| 616 | llvm::Value *CookieSizeV |
| 617 | = llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity()); |
| 618 | llvm::Value *UAddF |
| 619 | = CGF.CGM.getIntrinsic(llvm::Intrinsic::uadd_with_overflow, Types, 1); |
| 620 | llvm::Value *AddRes |
| 621 | = CGF.Builder.CreateCall2(UAddF, SizeWithoutCookie, CookieSizeV); |
| 622 | |
| 623 | Size = CGF.Builder.CreateExtractValue(AddRes, 0); |
| 624 | |
| 625 | llvm::Value *AddDidOverflow = CGF.Builder.CreateExtractValue(AddRes, 1); |
Eli Friedman | 5536daa | 2011-04-09 19:54:33 +0000 | [diff] [blame] | 626 | DidOverflow = CGF.Builder.CreateOr(DidOverflow, AddDidOverflow); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | Size = CGF.Builder.CreateSelect(DidOverflow, |
| 630 | llvm::ConstantInt::get(SizeTy, -1), |
| 631 | Size); |
Chris Lattner | 806941e | 2010-07-20 21:55:52 +0000 | [diff] [blame] | 632 | } |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 633 | |
| 634 | if (CookieSize.isZero()) |
| 635 | SizeWithoutCookie = Size; |
| 636 | else |
| 637 | assert(SizeWithoutCookie && "didn't set SizeWithoutCookie?"); |
| 638 | |
Chris Lattner | 806941e | 2010-07-20 21:55:52 +0000 | [diff] [blame] | 639 | return Size; |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Fariborz Jahanian | ef66872 | 2010-06-25 18:26:07 +0000 | [diff] [blame] | 642 | static void StoreAnyExprIntoOneUnit(CodeGenFunction &CGF, const CXXNewExpr *E, |
| 643 | llvm::Value *NewPtr) { |
Fariborz Jahanian | ef66872 | 2010-06-25 18:26:07 +0000 | [diff] [blame] | 644 | |
| 645 | assert(E->getNumConstructorArgs() == 1 && |
| 646 | "Can only have one argument to initializer of POD type."); |
| 647 | |
| 648 | const Expr *Init = E->getConstructorArg(0); |
| 649 | QualType AllocType = E->getAllocatedType(); |
Daniel Dunbar | 91a16fa | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 650 | |
| 651 | unsigned Alignment = |
| 652 | CGF.getContext().getTypeAlignInChars(AllocType).getQuantity(); |
Fariborz Jahanian | ef66872 | 2010-06-25 18:26:07 +0000 | [diff] [blame] | 653 | if (!CGF.hasAggregateLLVMType(AllocType)) |
| 654 | CGF.EmitStoreOfScalar(CGF.EmitScalarExpr(Init), NewPtr, |
Daniel Dunbar | 91a16fa | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 655 | AllocType.isVolatileQualified(), Alignment, |
| 656 | AllocType); |
Fariborz Jahanian | ef66872 | 2010-06-25 18:26:07 +0000 | [diff] [blame] | 657 | else if (AllocType->isAnyComplexType()) |
| 658 | CGF.EmitComplexExprIntoAddr(Init, NewPtr, |
| 659 | AllocType.isVolatileQualified()); |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 660 | else { |
| 661 | AggValueSlot Slot |
| 662 | = AggValueSlot::forAddr(NewPtr, AllocType.isVolatileQualified(), true); |
| 663 | CGF.EmitAggExpr(Init, Slot); |
| 664 | } |
Fariborz Jahanian | ef66872 | 2010-06-25 18:26:07 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | void |
| 668 | CodeGenFunction::EmitNewArrayInitializer(const CXXNewExpr *E, |
| 669 | llvm::Value *NewPtr, |
| 670 | llvm::Value *NumElements) { |
Fariborz Jahanian | 5304c95 | 2010-06-25 20:01:13 +0000 | [diff] [blame] | 671 | // We have a POD type. |
| 672 | if (E->getNumConstructorArgs() == 0) |
| 673 | return; |
| 674 | |
Fariborz Jahanian | ef66872 | 2010-06-25 18:26:07 +0000 | [diff] [blame] | 675 | const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
| 676 | |
| 677 | // Create a temporary for the loop index and initialize it with 0. |
| 678 | llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index"); |
| 679 | llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy); |
| 680 | Builder.CreateStore(Zero, IndexPtr); |
| 681 | |
| 682 | // Start the loop with a block that tests the condition. |
| 683 | llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); |
| 684 | llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); |
| 685 | |
| 686 | EmitBlock(CondBlock); |
| 687 | |
| 688 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
| 689 | |
| 690 | // Generate: if (loop-index < number-of-elements fall to the loop body, |
| 691 | // otherwise, go to the block after the for-loop. |
| 692 | llvm::Value *Counter = Builder.CreateLoad(IndexPtr); |
| 693 | llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless"); |
| 694 | // If the condition is true, execute the body. |
| 695 | Builder.CreateCondBr(IsLess, ForBody, AfterFor); |
| 696 | |
| 697 | EmitBlock(ForBody); |
| 698 | |
| 699 | llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); |
| 700 | // Inside the loop body, emit the constructor call on the array element. |
| 701 | Counter = Builder.CreateLoad(IndexPtr); |
| 702 | llvm::Value *Address = Builder.CreateInBoundsGEP(NewPtr, Counter, |
| 703 | "arrayidx"); |
| 704 | StoreAnyExprIntoOneUnit(*this, E, Address); |
| 705 | |
| 706 | EmitBlock(ContinueBlock); |
| 707 | |
| 708 | // Emit the increment of the loop counter. |
| 709 | llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1); |
| 710 | Counter = Builder.CreateLoad(IndexPtr); |
| 711 | NextVal = Builder.CreateAdd(Counter, NextVal, "inc"); |
| 712 | Builder.CreateStore(NextVal, IndexPtr); |
| 713 | |
| 714 | // Finally, branch back up to the condition for the next iteration. |
| 715 | EmitBranch(CondBlock); |
| 716 | |
| 717 | // Emit the fall-through block. |
| 718 | EmitBlock(AfterFor, true); |
| 719 | } |
| 720 | |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 721 | static void EmitZeroMemSet(CodeGenFunction &CGF, QualType T, |
| 722 | llvm::Value *NewPtr, llvm::Value *Size) { |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 723 | CGF.EmitCastToVoidPtr(NewPtr); |
Ken Dyck | fe71008 | 2011-01-19 01:58:38 +0000 | [diff] [blame] | 724 | CharUnits Alignment = CGF.getContext().getTypeAlignInChars(T); |
Benjamin Kramer | 9f0c7cc | 2010-12-30 00:13:21 +0000 | [diff] [blame] | 725 | CGF.Builder.CreateMemSet(NewPtr, CGF.Builder.getInt8(0), Size, |
Ken Dyck | fe71008 | 2011-01-19 01:58:38 +0000 | [diff] [blame] | 726 | Alignment.getQuantity(), false); |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 729 | static void EmitNewInitializer(CodeGenFunction &CGF, const CXXNewExpr *E, |
| 730 | llvm::Value *NewPtr, |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 731 | llvm::Value *NumElements, |
| 732 | llvm::Value *AllocSizeWithoutCookie) { |
Anders Carlsson | 5d4d946 | 2009-11-24 18:43:52 +0000 | [diff] [blame] | 733 | if (E->isArray()) { |
Anders Carlsson | e99bdb6 | 2010-05-03 15:09:17 +0000 | [diff] [blame] | 734 | if (CXXConstructorDecl *Ctor = E->getConstructor()) { |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 735 | bool RequiresZeroInitialization = false; |
| 736 | if (Ctor->getParent()->hasTrivialConstructor()) { |
| 737 | // If new expression did not specify value-initialization, then there |
| 738 | // is no initialization. |
| 739 | if (!E->hasInitializer() || Ctor->getParent()->isEmpty()) |
| 740 | return; |
| 741 | |
John McCall | f16aa10 | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 742 | if (CGF.CGM.getTypes().isZeroInitializable(E->getAllocatedType())) { |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 743 | // Optimization: since zero initialization will just set the memory |
| 744 | // to all zeroes, generate a single memset to do it in one shot. |
| 745 | EmitZeroMemSet(CGF, E->getAllocatedType(), NewPtr, |
| 746 | AllocSizeWithoutCookie); |
| 747 | return; |
| 748 | } |
| 749 | |
| 750 | RequiresZeroInitialization = true; |
| 751 | } |
| 752 | |
| 753 | CGF.EmitCXXAggrConstructorCall(Ctor, NumElements, NewPtr, |
| 754 | E->constructor_arg_begin(), |
| 755 | E->constructor_arg_end(), |
| 756 | RequiresZeroInitialization); |
Anders Carlsson | e99bdb6 | 2010-05-03 15:09:17 +0000 | [diff] [blame] | 757 | return; |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 758 | } else if (E->getNumConstructorArgs() == 1 && |
| 759 | isa<ImplicitValueInitExpr>(E->getConstructorArg(0))) { |
| 760 | // Optimization: since zero initialization will just set the memory |
| 761 | // to all zeroes, generate a single memset to do it in one shot. |
| 762 | EmitZeroMemSet(CGF, E->getAllocatedType(), NewPtr, |
| 763 | AllocSizeWithoutCookie); |
| 764 | return; |
| 765 | } else { |
Fariborz Jahanian | ef66872 | 2010-06-25 18:26:07 +0000 | [diff] [blame] | 766 | CGF.EmitNewArrayInitializer(E, NewPtr, NumElements); |
| 767 | return; |
| 768 | } |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 769 | } |
Anders Carlsson | 5d4d946 | 2009-11-24 18:43:52 +0000 | [diff] [blame] | 770 | |
| 771 | if (CXXConstructorDecl *Ctor = E->getConstructor()) { |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 772 | // Per C++ [expr.new]p15, if we have an initializer, then we're performing |
| 773 | // direct initialization. C++ [dcl.init]p5 requires that we |
| 774 | // zero-initialize storage if there are no user-declared constructors. |
| 775 | if (E->hasInitializer() && |
| 776 | !Ctor->getParent()->hasUserDeclaredConstructor() && |
| 777 | !Ctor->getParent()->isEmpty()) |
| 778 | CGF.EmitNullInitialization(NewPtr, E->getAllocatedType()); |
| 779 | |
Douglas Gregor | 8474567 | 2010-07-07 23:37:33 +0000 | [diff] [blame] | 780 | CGF.EmitCXXConstructorCall(Ctor, Ctor_Complete, /*ForVirtualBase=*/false, |
| 781 | NewPtr, E->constructor_arg_begin(), |
| 782 | E->constructor_arg_end()); |
Anders Carlsson | 5d4d946 | 2009-11-24 18:43:52 +0000 | [diff] [blame] | 783 | |
| 784 | return; |
| 785 | } |
Fariborz Jahanian | 5304c95 | 2010-06-25 20:01:13 +0000 | [diff] [blame] | 786 | // We have a POD type. |
| 787 | if (E->getNumConstructorArgs() == 0) |
| 788 | return; |
| 789 | |
Fariborz Jahanian | ef66872 | 2010-06-25 18:26:07 +0000 | [diff] [blame] | 790 | StoreAnyExprIntoOneUnit(CGF, E, NewPtr); |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 791 | } |
| 792 | |
John McCall | 7d8647f | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 793 | namespace { |
| 794 | /// A cleanup to call the given 'operator delete' function upon |
| 795 | /// abnormal exit from a new expression. |
| 796 | class CallDeleteDuringNew : public EHScopeStack::Cleanup { |
| 797 | size_t NumPlacementArgs; |
| 798 | const FunctionDecl *OperatorDelete; |
| 799 | llvm::Value *Ptr; |
| 800 | llvm::Value *AllocSize; |
| 801 | |
| 802 | RValue *getPlacementArgs() { return reinterpret_cast<RValue*>(this+1); } |
| 803 | |
| 804 | public: |
| 805 | static size_t getExtraSize(size_t NumPlacementArgs) { |
| 806 | return NumPlacementArgs * sizeof(RValue); |
| 807 | } |
| 808 | |
| 809 | CallDeleteDuringNew(size_t NumPlacementArgs, |
| 810 | const FunctionDecl *OperatorDelete, |
| 811 | llvm::Value *Ptr, |
| 812 | llvm::Value *AllocSize) |
| 813 | : NumPlacementArgs(NumPlacementArgs), OperatorDelete(OperatorDelete), |
| 814 | Ptr(Ptr), AllocSize(AllocSize) {} |
| 815 | |
| 816 | void setPlacementArg(unsigned I, RValue Arg) { |
| 817 | assert(I < NumPlacementArgs && "index out of range"); |
| 818 | getPlacementArgs()[I] = Arg; |
| 819 | } |
| 820 | |
| 821 | void Emit(CodeGenFunction &CGF, bool IsForEH) { |
| 822 | const FunctionProtoType *FPT |
| 823 | = OperatorDelete->getType()->getAs<FunctionProtoType>(); |
| 824 | assert(FPT->getNumArgs() == NumPlacementArgs + 1 || |
John McCall | c384636 | 2010-09-14 21:45:42 +0000 | [diff] [blame] | 825 | (FPT->getNumArgs() == 2 && NumPlacementArgs == 0)); |
John McCall | 7d8647f | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 826 | |
| 827 | CallArgList DeleteArgs; |
| 828 | |
| 829 | // The first argument is always a void*. |
| 830 | FunctionProtoType::arg_type_iterator AI = FPT->arg_type_begin(); |
| 831 | DeleteArgs.push_back(std::make_pair(RValue::get(Ptr), *AI++)); |
| 832 | |
| 833 | // A member 'operator delete' can take an extra 'size_t' argument. |
| 834 | if (FPT->getNumArgs() == NumPlacementArgs + 2) |
| 835 | DeleteArgs.push_back(std::make_pair(RValue::get(AllocSize), *AI++)); |
| 836 | |
| 837 | // Pass the rest of the arguments, which must match exactly. |
| 838 | for (unsigned I = 0; I != NumPlacementArgs; ++I) |
| 839 | DeleteArgs.push_back(std::make_pair(getPlacementArgs()[I], *AI++)); |
| 840 | |
| 841 | // Call 'operator delete'. |
Tilmann Scheller | 9c6082f | 2011-03-02 21:36:49 +0000 | [diff] [blame] | 842 | CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(DeleteArgs, FPT), |
John McCall | 7d8647f | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 843 | CGF.CGM.GetAddrOfFunction(OperatorDelete), |
| 844 | ReturnValueSlot(), DeleteArgs, OperatorDelete); |
| 845 | } |
| 846 | }; |
John McCall | 3019c44 | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 847 | |
| 848 | /// A cleanup to call the given 'operator delete' function upon |
| 849 | /// abnormal exit from a new expression when the new expression is |
| 850 | /// conditional. |
| 851 | class CallDeleteDuringConditionalNew : public EHScopeStack::Cleanup { |
| 852 | size_t NumPlacementArgs; |
| 853 | const FunctionDecl *OperatorDelete; |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 854 | DominatingValue<RValue>::saved_type Ptr; |
| 855 | DominatingValue<RValue>::saved_type AllocSize; |
John McCall | 3019c44 | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 856 | |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 857 | DominatingValue<RValue>::saved_type *getPlacementArgs() { |
| 858 | return reinterpret_cast<DominatingValue<RValue>::saved_type*>(this+1); |
John McCall | 3019c44 | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | public: |
| 862 | static size_t getExtraSize(size_t NumPlacementArgs) { |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 863 | return NumPlacementArgs * sizeof(DominatingValue<RValue>::saved_type); |
John McCall | 3019c44 | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | CallDeleteDuringConditionalNew(size_t NumPlacementArgs, |
| 867 | const FunctionDecl *OperatorDelete, |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 868 | DominatingValue<RValue>::saved_type Ptr, |
| 869 | DominatingValue<RValue>::saved_type AllocSize) |
John McCall | 3019c44 | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 870 | : NumPlacementArgs(NumPlacementArgs), OperatorDelete(OperatorDelete), |
| 871 | Ptr(Ptr), AllocSize(AllocSize) {} |
| 872 | |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 873 | void setPlacementArg(unsigned I, DominatingValue<RValue>::saved_type Arg) { |
John McCall | 3019c44 | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 874 | assert(I < NumPlacementArgs && "index out of range"); |
| 875 | getPlacementArgs()[I] = Arg; |
| 876 | } |
| 877 | |
| 878 | void Emit(CodeGenFunction &CGF, bool IsForEH) { |
| 879 | const FunctionProtoType *FPT |
| 880 | = OperatorDelete->getType()->getAs<FunctionProtoType>(); |
| 881 | assert(FPT->getNumArgs() == NumPlacementArgs + 1 || |
| 882 | (FPT->getNumArgs() == 2 && NumPlacementArgs == 0)); |
| 883 | |
| 884 | CallArgList DeleteArgs; |
| 885 | |
| 886 | // The first argument is always a void*. |
| 887 | FunctionProtoType::arg_type_iterator AI = FPT->arg_type_begin(); |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 888 | DeleteArgs.push_back(std::make_pair(Ptr.restore(CGF), *AI++)); |
John McCall | 3019c44 | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 889 | |
| 890 | // A member 'operator delete' can take an extra 'size_t' argument. |
| 891 | if (FPT->getNumArgs() == NumPlacementArgs + 2) { |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 892 | RValue RV = AllocSize.restore(CGF); |
John McCall | 3019c44 | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 893 | DeleteArgs.push_back(std::make_pair(RV, *AI++)); |
| 894 | } |
| 895 | |
| 896 | // Pass the rest of the arguments, which must match exactly. |
| 897 | for (unsigned I = 0; I != NumPlacementArgs; ++I) { |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 898 | RValue RV = getPlacementArgs()[I].restore(CGF); |
John McCall | 3019c44 | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 899 | DeleteArgs.push_back(std::make_pair(RV, *AI++)); |
| 900 | } |
| 901 | |
| 902 | // Call 'operator delete'. |
Tilmann Scheller | 9c6082f | 2011-03-02 21:36:49 +0000 | [diff] [blame] | 903 | CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(DeleteArgs, FPT), |
John McCall | 3019c44 | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 904 | CGF.CGM.GetAddrOfFunction(OperatorDelete), |
| 905 | ReturnValueSlot(), DeleteArgs, OperatorDelete); |
| 906 | } |
| 907 | }; |
| 908 | } |
| 909 | |
| 910 | /// Enter a cleanup to call 'operator delete' if the initializer in a |
| 911 | /// new-expression throws. |
| 912 | static void EnterNewDeleteCleanup(CodeGenFunction &CGF, |
| 913 | const CXXNewExpr *E, |
| 914 | llvm::Value *NewPtr, |
| 915 | llvm::Value *AllocSize, |
| 916 | const CallArgList &NewArgs) { |
| 917 | // If we're not inside a conditional branch, then the cleanup will |
| 918 | // dominate and we can do the easier (and more efficient) thing. |
| 919 | if (!CGF.isInConditionalBranch()) { |
| 920 | CallDeleteDuringNew *Cleanup = CGF.EHStack |
| 921 | .pushCleanupWithExtra<CallDeleteDuringNew>(EHCleanup, |
| 922 | E->getNumPlacementArgs(), |
| 923 | E->getOperatorDelete(), |
| 924 | NewPtr, AllocSize); |
| 925 | for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I) |
| 926 | Cleanup->setPlacementArg(I, NewArgs[I+1].first); |
| 927 | |
| 928 | return; |
| 929 | } |
| 930 | |
| 931 | // Otherwise, we need to save all this stuff. |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 932 | DominatingValue<RValue>::saved_type SavedNewPtr = |
| 933 | DominatingValue<RValue>::save(CGF, RValue::get(NewPtr)); |
| 934 | DominatingValue<RValue>::saved_type SavedAllocSize = |
| 935 | DominatingValue<RValue>::save(CGF, RValue::get(AllocSize)); |
John McCall | 3019c44 | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 936 | |
| 937 | CallDeleteDuringConditionalNew *Cleanup = CGF.EHStack |
| 938 | .pushCleanupWithExtra<CallDeleteDuringConditionalNew>(InactiveEHCleanup, |
| 939 | E->getNumPlacementArgs(), |
| 940 | E->getOperatorDelete(), |
| 941 | SavedNewPtr, |
| 942 | SavedAllocSize); |
| 943 | for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I) |
John McCall | 804b807 | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 944 | Cleanup->setPlacementArg(I, |
| 945 | DominatingValue<RValue>::save(CGF, NewArgs[I+1].first)); |
John McCall | 3019c44 | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 946 | |
| 947 | CGF.ActivateCleanupBlock(CGF.EHStack.stable_begin()); |
John McCall | 7d8647f | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 950 | llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) { |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 951 | // The element type being allocated. |
| 952 | QualType allocType = getContext().getBaseElementType(E->getAllocatedType()); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 953 | |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 954 | // 1. Build a call to the allocation function. |
| 955 | FunctionDecl *allocator = E->getOperatorNew(); |
| 956 | const FunctionProtoType *allocatorType = |
| 957 | allocator->getType()->castAs<FunctionProtoType>(); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 958 | |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 959 | CallArgList allocatorArgs; |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 960 | |
| 961 | // The allocation size is the first argument. |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 962 | QualType sizeType = getContext().getSizeType(); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 963 | |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 964 | llvm::Value *numElements = 0; |
| 965 | llvm::Value *allocSizeWithoutCookie = 0; |
| 966 | llvm::Value *allocSize = |
| 967 | EmitCXXNewAllocSize(getContext(), *this, E, numElements, |
| 968 | allocSizeWithoutCookie); |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 969 | |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 970 | allocatorArgs.push_back(std::make_pair(RValue::get(allocSize), sizeType)); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 971 | |
| 972 | // Emit the rest of the arguments. |
| 973 | // FIXME: Ideally, this should just use EmitCallArgs. |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 974 | CXXNewExpr::const_arg_iterator placementArg = E->placement_arg_begin(); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 975 | |
| 976 | // First, use the types from the function type. |
| 977 | // We start at 1 here because the first argument (the allocation size) |
| 978 | // has already been emitted. |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 979 | for (unsigned i = 1, e = allocatorType->getNumArgs(); i != e; |
| 980 | ++i, ++placementArg) { |
| 981 | QualType argType = allocatorType->getArgType(i); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 982 | |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 983 | assert(getContext().hasSameUnqualifiedType(argType.getNonReferenceType(), |
| 984 | placementArg->getType()) && |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 985 | "type mismatch in call argument!"); |
| 986 | |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 987 | EmitCallArg(allocatorArgs, *placementArg, argType); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | // Either we've emitted all the call args, or we have a call to a |
| 991 | // variadic function. |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 992 | assert((placementArg == E->placement_arg_end() || |
| 993 | allocatorType->isVariadic()) && |
| 994 | "Extra arguments to non-variadic function!"); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 995 | |
| 996 | // If we still have any arguments, emit them using the type of the argument. |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 997 | for (CXXNewExpr::const_arg_iterator placementArgsEnd = E->placement_arg_end(); |
| 998 | placementArg != placementArgsEnd; ++placementArg) { |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 999 | EmitCallArg(allocatorArgs, *placementArg, placementArg->getType()); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1002 | // Emit the allocation call. |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1003 | RValue RV = |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1004 | EmitCall(CGM.getTypes().getFunctionInfo(allocatorArgs, allocatorType), |
| 1005 | CGM.GetAddrOfFunction(allocator), ReturnValueSlot(), |
| 1006 | allocatorArgs, allocator); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1007 | |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1008 | // Emit a null check on the allocation result if the allocation |
| 1009 | // function is allowed to return null (because it has a non-throwing |
| 1010 | // exception spec; for this part, we inline |
| 1011 | // CXXNewExpr::shouldNullCheckAllocation()) and we have an |
| 1012 | // interesting initializer. |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 1013 | bool nullCheck = allocatorType->isNothrow(getContext()) && |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1014 | !(allocType->isPODType() && !E->hasInitializer()); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1015 | |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1016 | llvm::BasicBlock *nullCheckBB = 0; |
| 1017 | llvm::BasicBlock *contBB = 0; |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1018 | |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1019 | llvm::Value *allocation = RV.getScalarVal(); |
| 1020 | unsigned AS = |
| 1021 | cast<llvm::PointerType>(allocation->getType())->getAddressSpace(); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1022 | |
John McCall | a7f633f | 2011-03-07 01:52:56 +0000 | [diff] [blame] | 1023 | // The null-check means that the initializer is conditionally |
| 1024 | // evaluated. |
| 1025 | ConditionalEvaluation conditional(*this); |
| 1026 | |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1027 | if (nullCheck) { |
John McCall | a7f633f | 2011-03-07 01:52:56 +0000 | [diff] [blame] | 1028 | conditional.begin(*this); |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1029 | |
| 1030 | nullCheckBB = Builder.GetInsertBlock(); |
| 1031 | llvm::BasicBlock *notNullBB = createBasicBlock("new.notnull"); |
| 1032 | contBB = createBasicBlock("new.cont"); |
| 1033 | |
| 1034 | llvm::Value *isNull = Builder.CreateIsNull(allocation, "new.isnull"); |
| 1035 | Builder.CreateCondBr(isNull, contBB, notNullBB); |
| 1036 | EmitBlock(notNullBB); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1037 | } |
Ken Dyck | caf647c | 2010-01-26 19:44:24 +0000 | [diff] [blame] | 1038 | |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1039 | assert((allocSize == allocSizeWithoutCookie) == |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1040 | CalculateCookiePadding(*this, E).isZero()); |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1041 | if (allocSize != allocSizeWithoutCookie) { |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1042 | assert(E->isArray()); |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1043 | allocation = CGM.getCXXABI().InitializeArrayCookie(*this, allocation, |
| 1044 | numElements, |
| 1045 | E, allocType); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1046 | } |
Anders Carlsson | 6ac5fc4 | 2009-09-23 18:59:48 +0000 | [diff] [blame] | 1047 | |
John McCall | 7d8647f | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1048 | // If there's an operator delete, enter a cleanup to call it if an |
| 1049 | // exception is thrown. |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1050 | EHScopeStack::stable_iterator operatorDeleteCleanup; |
John McCall | 7d8647f | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1051 | if (E->getOperatorDelete()) { |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1052 | EnterNewDeleteCleanup(*this, E, allocation, allocSize, allocatorArgs); |
| 1053 | operatorDeleteCleanup = EHStack.stable_begin(); |
John McCall | 7d8647f | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1056 | const llvm::Type *elementPtrTy |
| 1057 | = ConvertTypeForMem(allocType)->getPointerTo(AS); |
| 1058 | llvm::Value *result = Builder.CreateBitCast(allocation, elementPtrTy); |
John McCall | 7d8647f | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1059 | |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1060 | if (E->isArray()) { |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1061 | EmitNewInitializer(*this, E, result, numElements, allocSizeWithoutCookie); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1062 | |
| 1063 | // NewPtr is a pointer to the base element type. If we're |
| 1064 | // allocating an array of arrays, we'll need to cast back to the |
| 1065 | // array pointer type. |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1066 | const llvm::Type *resultType = ConvertTypeForMem(E->getType()); |
| 1067 | if (result->getType() != resultType) |
| 1068 | result = Builder.CreateBitCast(result, resultType); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1069 | } else { |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1070 | EmitNewInitializer(*this, E, result, numElements, allocSizeWithoutCookie); |
Fariborz Jahanian | ceb43b6 | 2010-03-24 16:57:01 +0000 | [diff] [blame] | 1071 | } |
John McCall | 7d8647f | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1072 | |
| 1073 | // Deactivate the 'operator delete' cleanup if we finished |
| 1074 | // initialization. |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1075 | if (operatorDeleteCleanup.isValid()) |
| 1076 | DeactivateCleanupBlock(operatorDeleteCleanup); |
Fariborz Jahanian | ceb43b6 | 2010-03-24 16:57:01 +0000 | [diff] [blame] | 1077 | |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1078 | if (nullCheck) { |
John McCall | a7f633f | 2011-03-07 01:52:56 +0000 | [diff] [blame] | 1079 | conditional.end(*this); |
| 1080 | |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1081 | llvm::BasicBlock *notNullBB = Builder.GetInsertBlock(); |
| 1082 | EmitBlock(contBB); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1083 | |
Jay Foad | bbf3bac | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 1084 | llvm::PHINode *PHI = Builder.CreatePHI(result->getType(), 2); |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1085 | PHI->addIncoming(result, notNullBB); |
| 1086 | PHI->addIncoming(llvm::Constant::getNullValue(result->getType()), |
| 1087 | nullCheckBB); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1088 | |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1089 | result = PHI; |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1090 | } |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1091 | |
John McCall | c2f3e7f | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1092 | return result; |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
Eli Friedman | 5fe0598 | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1095 | void CodeGenFunction::EmitDeleteCall(const FunctionDecl *DeleteFD, |
| 1096 | llvm::Value *Ptr, |
| 1097 | QualType DeleteTy) { |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1098 | assert(DeleteFD->getOverloadedOperator() == OO_Delete); |
| 1099 | |
Eli Friedman | 5fe0598 | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1100 | const FunctionProtoType *DeleteFTy = |
| 1101 | DeleteFD->getType()->getAs<FunctionProtoType>(); |
| 1102 | |
| 1103 | CallArgList DeleteArgs; |
| 1104 | |
Anders Carlsson | 871d078 | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 1105 | // Check if we need to pass the size to the delete operator. |
| 1106 | llvm::Value *Size = 0; |
| 1107 | QualType SizeTy; |
| 1108 | if (DeleteFTy->getNumArgs() == 2) { |
| 1109 | SizeTy = DeleteFTy->getArgType(1); |
Ken Dyck | 4f122ef | 2010-01-26 19:59:28 +0000 | [diff] [blame] | 1110 | CharUnits DeleteTypeSize = getContext().getTypeSizeInChars(DeleteTy); |
| 1111 | Size = llvm::ConstantInt::get(ConvertType(SizeTy), |
| 1112 | DeleteTypeSize.getQuantity()); |
Anders Carlsson | 871d078 | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
Eli Friedman | 5fe0598 | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1115 | QualType ArgTy = DeleteFTy->getArgType(0); |
| 1116 | llvm::Value *DeletePtr = Builder.CreateBitCast(Ptr, ConvertType(ArgTy)); |
| 1117 | DeleteArgs.push_back(std::make_pair(RValue::get(DeletePtr), ArgTy)); |
| 1118 | |
Anders Carlsson | 871d078 | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 1119 | if (Size) |
Eli Friedman | 5fe0598 | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1120 | DeleteArgs.push_back(std::make_pair(RValue::get(Size), SizeTy)); |
Eli Friedman | 5fe0598 | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1121 | |
| 1122 | // Emit the call to delete. |
Tilmann Scheller | 9c6082f | 2011-03-02 21:36:49 +0000 | [diff] [blame] | 1123 | EmitCall(CGM.getTypes().getFunctionInfo(DeleteArgs, DeleteFTy), |
Anders Carlsson | f3c47c9 | 2009-12-24 19:25:24 +0000 | [diff] [blame] | 1124 | CGM.GetAddrOfFunction(DeleteFD), ReturnValueSlot(), |
Eli Friedman | 5fe0598 | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1125 | DeleteArgs, DeleteFD); |
| 1126 | } |
| 1127 | |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1128 | namespace { |
| 1129 | /// Calls the given 'operator delete' on a single object. |
| 1130 | struct CallObjectDelete : EHScopeStack::Cleanup { |
| 1131 | llvm::Value *Ptr; |
| 1132 | const FunctionDecl *OperatorDelete; |
| 1133 | QualType ElementType; |
| 1134 | |
| 1135 | CallObjectDelete(llvm::Value *Ptr, |
| 1136 | const FunctionDecl *OperatorDelete, |
| 1137 | QualType ElementType) |
| 1138 | : Ptr(Ptr), OperatorDelete(OperatorDelete), ElementType(ElementType) {} |
| 1139 | |
| 1140 | void Emit(CodeGenFunction &CGF, bool IsForEH) { |
| 1141 | CGF.EmitDeleteCall(OperatorDelete, Ptr, ElementType); |
| 1142 | } |
| 1143 | }; |
| 1144 | } |
| 1145 | |
| 1146 | /// Emit the code for deleting a single object. |
| 1147 | static void EmitObjectDelete(CodeGenFunction &CGF, |
| 1148 | const FunctionDecl *OperatorDelete, |
| 1149 | llvm::Value *Ptr, |
| 1150 | QualType ElementType) { |
| 1151 | // Find the destructor for the type, if applicable. If the |
| 1152 | // destructor is virtual, we'll just emit the vcall and return. |
| 1153 | const CXXDestructorDecl *Dtor = 0; |
| 1154 | if (const RecordType *RT = ElementType->getAs<RecordType>()) { |
| 1155 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 1156 | if (!RD->hasTrivialDestructor()) { |
| 1157 | Dtor = RD->getDestructor(); |
| 1158 | |
| 1159 | if (Dtor->isVirtual()) { |
| 1160 | const llvm::Type *Ty = |
John McCall | fc40028 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 1161 | CGF.getTypes().GetFunctionType(CGF.getTypes().getFunctionInfo(Dtor, |
| 1162 | Dtor_Complete), |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1163 | /*isVariadic=*/false); |
| 1164 | |
| 1165 | llvm::Value *Callee |
| 1166 | = CGF.BuildVirtualCall(Dtor, Dtor_Deleting, Ptr, Ty); |
| 1167 | CGF.EmitCXXMemberCall(Dtor, Callee, ReturnValueSlot(), Ptr, /*VTT=*/0, |
| 1168 | 0, 0); |
| 1169 | |
| 1170 | // The dtor took care of deleting the object. |
| 1171 | return; |
| 1172 | } |
| 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | // Make sure that we call delete even if the dtor throws. |
John McCall | 3ad32c8 | 2011-01-28 08:37:24 +0000 | [diff] [blame] | 1177 | // This doesn't have to a conditional cleanup because we're going |
| 1178 | // to pop it off in a second. |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1179 | CGF.EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup, |
| 1180 | Ptr, OperatorDelete, ElementType); |
| 1181 | |
| 1182 | if (Dtor) |
| 1183 | CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, |
| 1184 | /*ForVirtualBase=*/false, Ptr); |
| 1185 | |
| 1186 | CGF.PopCleanupBlock(); |
| 1187 | } |
| 1188 | |
| 1189 | namespace { |
| 1190 | /// Calls the given 'operator delete' on an array of objects. |
| 1191 | struct CallArrayDelete : EHScopeStack::Cleanup { |
| 1192 | llvm::Value *Ptr; |
| 1193 | const FunctionDecl *OperatorDelete; |
| 1194 | llvm::Value *NumElements; |
| 1195 | QualType ElementType; |
| 1196 | CharUnits CookieSize; |
| 1197 | |
| 1198 | CallArrayDelete(llvm::Value *Ptr, |
| 1199 | const FunctionDecl *OperatorDelete, |
| 1200 | llvm::Value *NumElements, |
| 1201 | QualType ElementType, |
| 1202 | CharUnits CookieSize) |
| 1203 | : Ptr(Ptr), OperatorDelete(OperatorDelete), NumElements(NumElements), |
| 1204 | ElementType(ElementType), CookieSize(CookieSize) {} |
| 1205 | |
| 1206 | void Emit(CodeGenFunction &CGF, bool IsForEH) { |
| 1207 | const FunctionProtoType *DeleteFTy = |
| 1208 | OperatorDelete->getType()->getAs<FunctionProtoType>(); |
| 1209 | assert(DeleteFTy->getNumArgs() == 1 || DeleteFTy->getNumArgs() == 2); |
| 1210 | |
| 1211 | CallArgList Args; |
| 1212 | |
| 1213 | // Pass the pointer as the first argument. |
| 1214 | QualType VoidPtrTy = DeleteFTy->getArgType(0); |
| 1215 | llvm::Value *DeletePtr |
| 1216 | = CGF.Builder.CreateBitCast(Ptr, CGF.ConvertType(VoidPtrTy)); |
| 1217 | Args.push_back(std::make_pair(RValue::get(DeletePtr), VoidPtrTy)); |
| 1218 | |
| 1219 | // Pass the original requested size as the second argument. |
| 1220 | if (DeleteFTy->getNumArgs() == 2) { |
| 1221 | QualType size_t = DeleteFTy->getArgType(1); |
| 1222 | const llvm::IntegerType *SizeTy |
| 1223 | = cast<llvm::IntegerType>(CGF.ConvertType(size_t)); |
| 1224 | |
| 1225 | CharUnits ElementTypeSize = |
| 1226 | CGF.CGM.getContext().getTypeSizeInChars(ElementType); |
| 1227 | |
| 1228 | // The size of an element, multiplied by the number of elements. |
| 1229 | llvm::Value *Size |
| 1230 | = llvm::ConstantInt::get(SizeTy, ElementTypeSize.getQuantity()); |
| 1231 | Size = CGF.Builder.CreateMul(Size, NumElements); |
| 1232 | |
| 1233 | // Plus the size of the cookie if applicable. |
| 1234 | if (!CookieSize.isZero()) { |
| 1235 | llvm::Value *CookieSizeV |
| 1236 | = llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity()); |
| 1237 | Size = CGF.Builder.CreateAdd(Size, CookieSizeV); |
| 1238 | } |
| 1239 | |
| 1240 | Args.push_back(std::make_pair(RValue::get(Size), size_t)); |
| 1241 | } |
| 1242 | |
| 1243 | // Emit the call to delete. |
Tilmann Scheller | 9c6082f | 2011-03-02 21:36:49 +0000 | [diff] [blame] | 1244 | CGF.EmitCall(CGF.getTypes().getFunctionInfo(Args, DeleteFTy), |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1245 | CGF.CGM.GetAddrOfFunction(OperatorDelete), |
| 1246 | ReturnValueSlot(), Args, OperatorDelete); |
| 1247 | } |
| 1248 | }; |
| 1249 | } |
| 1250 | |
| 1251 | /// Emit the code for deleting an array of objects. |
| 1252 | static void EmitArrayDelete(CodeGenFunction &CGF, |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1253 | const CXXDeleteExpr *E, |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1254 | llvm::Value *Ptr, |
| 1255 | QualType ElementType) { |
| 1256 | llvm::Value *NumElements = 0; |
| 1257 | llvm::Value *AllocatedPtr = 0; |
| 1258 | CharUnits CookieSize; |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1259 | CGF.CGM.getCXXABI().ReadArrayCookie(CGF, Ptr, E, ElementType, |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1260 | NumElements, AllocatedPtr, CookieSize); |
| 1261 | |
| 1262 | assert(AllocatedPtr && "ReadArrayCookie didn't set AllocatedPtr"); |
| 1263 | |
| 1264 | // Make sure that we call delete even if one of the dtors throws. |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1265 | const FunctionDecl *OperatorDelete = E->getOperatorDelete(); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1266 | CGF.EHStack.pushCleanup<CallArrayDelete>(NormalAndEHCleanup, |
| 1267 | AllocatedPtr, OperatorDelete, |
| 1268 | NumElements, ElementType, |
| 1269 | CookieSize); |
| 1270 | |
| 1271 | if (const CXXRecordDecl *RD = ElementType->getAsCXXRecordDecl()) { |
| 1272 | if (!RD->hasTrivialDestructor()) { |
| 1273 | assert(NumElements && "ReadArrayCookie didn't find element count" |
| 1274 | " for a class with destructor"); |
| 1275 | CGF.EmitCXXAggrDestructorCall(RD->getDestructor(), NumElements, Ptr); |
| 1276 | } |
| 1277 | } |
| 1278 | |
| 1279 | CGF.PopCleanupBlock(); |
| 1280 | } |
| 1281 | |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1282 | void CodeGenFunction::EmitCXXDeleteExpr(const CXXDeleteExpr *E) { |
Fariborz Jahanian | 72c2153 | 2009-11-13 19:27:47 +0000 | [diff] [blame] | 1283 | |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1284 | // Get at the argument before we performed the implicit conversion |
| 1285 | // to void*. |
| 1286 | const Expr *Arg = E->getArgument(); |
| 1287 | while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1288 | if (ICE->getCastKind() != CK_UserDefinedConversion && |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1289 | ICE->getType()->isVoidPointerType()) |
| 1290 | Arg = ICE->getSubExpr(); |
Douglas Gregor | d69dd78 | 2009-10-01 05:49:51 +0000 | [diff] [blame] | 1291 | else |
| 1292 | break; |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1293 | } |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1294 | |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1295 | llvm::Value *Ptr = EmitScalarExpr(Arg); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1296 | |
| 1297 | // Null check the pointer. |
| 1298 | llvm::BasicBlock *DeleteNotNull = createBasicBlock("delete.notnull"); |
| 1299 | llvm::BasicBlock *DeleteEnd = createBasicBlock("delete.end"); |
| 1300 | |
Anders Carlsson | b924124 | 2011-04-11 00:30:07 +0000 | [diff] [blame] | 1301 | llvm::Value *IsNull = Builder.CreateIsNull(Ptr, "isnull"); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1302 | |
| 1303 | Builder.CreateCondBr(IsNull, DeleteEnd, DeleteNotNull); |
| 1304 | EmitBlock(DeleteNotNull); |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 1305 | |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1306 | // We might be deleting a pointer to array. If so, GEP down to the |
| 1307 | // first non-array element. |
| 1308 | // (this assumes that A(*)[3][7] is converted to [3 x [7 x %A]]*) |
| 1309 | QualType DeleteTy = Arg->getType()->getAs<PointerType>()->getPointeeType(); |
| 1310 | if (DeleteTy->isConstantArrayType()) { |
| 1311 | llvm::Value *Zero = Builder.getInt32(0); |
| 1312 | llvm::SmallVector<llvm::Value*,8> GEP; |
| 1313 | |
| 1314 | GEP.push_back(Zero); // point at the outermost array |
| 1315 | |
| 1316 | // For each layer of array type we're pointing at: |
| 1317 | while (const ConstantArrayType *Arr |
| 1318 | = getContext().getAsConstantArrayType(DeleteTy)) { |
| 1319 | // 1. Unpeel the array type. |
| 1320 | DeleteTy = Arr->getElementType(); |
| 1321 | |
| 1322 | // 2. GEP to the first element of the array. |
| 1323 | GEP.push_back(Zero); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1324 | } |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1325 | |
| 1326 | Ptr = Builder.CreateInBoundsGEP(Ptr, GEP.begin(), GEP.end(), "del.first"); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
Douglas Gregor | eede61a | 2010-09-02 17:38:50 +0000 | [diff] [blame] | 1329 | assert(ConvertTypeForMem(DeleteTy) == |
| 1330 | cast<llvm::PointerType>(Ptr->getType())->getElementType()); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1331 | |
| 1332 | if (E->isArrayForm()) { |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1333 | EmitArrayDelete(*this, E, Ptr, DeleteTy); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1334 | } else { |
| 1335 | EmitObjectDelete(*this, E->getOperatorDelete(), Ptr, DeleteTy); |
| 1336 | } |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1337 | |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1338 | EmitBlock(DeleteEnd); |
| 1339 | } |
Mike Stump | c2e84ae | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 1340 | |
Anders Carlsson | 4bdbc0c | 2011-04-11 14:13:40 +0000 | [diff] [blame] | 1341 | static llvm::Constant *getBadTypeidFn(CodeGenFunction &CGF) { |
| 1342 | // void __cxa_bad_typeid(); |
| 1343 | |
| 1344 | const llvm::Type *VoidTy = llvm::Type::getVoidTy(CGF.getLLVMContext()); |
| 1345 | const llvm::FunctionType *FTy = |
| 1346 | llvm::FunctionType::get(VoidTy, false); |
| 1347 | |
| 1348 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_typeid"); |
| 1349 | } |
| 1350 | |
| 1351 | static void EmitBadTypeidCall(CodeGenFunction &CGF) { |
| 1352 | llvm::Value *F = getBadTypeidFn(CGF); |
| 1353 | if (llvm::BasicBlock *InvokeDest = CGF.getInvokeDest()) { |
| 1354 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
| 1355 | CGF.Builder.CreateInvoke(F, Cont, InvokeDest)->setDoesNotReturn(); |
| 1356 | CGF.EmitBlock(Cont); |
| 1357 | } else |
| 1358 | CGF.Builder.CreateCall(F)->setDoesNotReturn(); |
| 1359 | |
| 1360 | CGF.Builder.CreateUnreachable(); |
| 1361 | } |
| 1362 | |
John McCall | 3ad32c8 | 2011-01-28 08:37:24 +0000 | [diff] [blame] | 1363 | llvm::Value *CodeGenFunction::EmitCXXTypeidExpr(const CXXTypeidExpr *E) { |
Mike Stump | c2e84ae | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 1364 | QualType Ty = E->getType(); |
| 1365 | const llvm::Type *LTy = ConvertType(Ty)->getPointerTo(); |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 1366 | |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 1367 | if (E->isTypeOperand()) { |
| 1368 | llvm::Constant *TypeInfo = |
| 1369 | CGM.GetAddrOfRTTIDescriptor(E->getTypeOperand()); |
| 1370 | return Builder.CreateBitCast(TypeInfo, LTy); |
| 1371 | } |
| 1372 | |
Mike Stump | c2e84ae | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 1373 | Expr *subE = E->getExprOperand(); |
Mike Stump | 5fae856 | 2009-11-17 22:33:00 +0000 | [diff] [blame] | 1374 | Ty = subE->getType(); |
| 1375 | CanQualType CanTy = CGM.getContext().getCanonicalType(Ty); |
| 1376 | Ty = CanTy.getUnqualifiedType().getNonReferenceType(); |
Mike Stump | c2e84ae | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 1377 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 1378 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 1379 | if (RD->isPolymorphic()) { |
| 1380 | // FIXME: if subE is an lvalue do |
| 1381 | LValue Obj = EmitLValue(subE); |
| 1382 | llvm::Value *This = Obj.getAddress(); |
Mike Stump | f549e89 | 2009-11-15 16:52:53 +0000 | [diff] [blame] | 1383 | // We need to do a zero check for *p, unless it has NonNullAttr. |
| 1384 | // FIXME: PointerType->hasAttr<NonNullAttr>() |
| 1385 | bool CanBeZero = false; |
Mike Stump | db519a4 | 2009-11-17 00:45:21 +0000 | [diff] [blame] | 1386 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(subE->IgnoreParens())) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1387 | if (UO->getOpcode() == UO_Deref) |
Mike Stump | f549e89 | 2009-11-15 16:52:53 +0000 | [diff] [blame] | 1388 | CanBeZero = true; |
| 1389 | if (CanBeZero) { |
| 1390 | llvm::BasicBlock *NonZeroBlock = createBasicBlock(); |
| 1391 | llvm::BasicBlock *ZeroBlock = createBasicBlock(); |
| 1392 | |
Dan Gohman | 043fb9a | 2010-10-26 18:44:08 +0000 | [diff] [blame] | 1393 | llvm::Value *Zero = llvm::Constant::getNullValue(This->getType()); |
| 1394 | Builder.CreateCondBr(Builder.CreateICmpNE(This, Zero), |
Mike Stump | f549e89 | 2009-11-15 16:52:53 +0000 | [diff] [blame] | 1395 | NonZeroBlock, ZeroBlock); |
| 1396 | EmitBlock(ZeroBlock); |
Anders Carlsson | 4bdbc0c | 2011-04-11 14:13:40 +0000 | [diff] [blame] | 1397 | |
| 1398 | EmitBadTypeidCall(*this); |
| 1399 | |
Mike Stump | f549e89 | 2009-11-15 16:52:53 +0000 | [diff] [blame] | 1400 | EmitBlock(NonZeroBlock); |
| 1401 | } |
Dan Gohman | 043fb9a | 2010-10-26 18:44:08 +0000 | [diff] [blame] | 1402 | llvm::Value *V = GetVTablePtr(This, LTy->getPointerTo()); |
Mike Stump | c2e84ae | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 1403 | V = Builder.CreateConstInBoundsGEP1_64(V, -1ULL); |
| 1404 | V = Builder.CreateLoad(V); |
| 1405 | return V; |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 1406 | } |
Mike Stump | c2e84ae | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 1407 | } |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 1408 | return Builder.CreateBitCast(CGM.GetAddrOfRTTIDescriptor(Ty), LTy); |
Mike Stump | c2e84ae | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 1409 | } |
Mike Stump | c849c05 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1410 | |
Anders Carlsson | f0cb4a6 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1411 | static llvm::Constant *getDynamicCastFn(CodeGenFunction &CGF) { |
| 1412 | // void *__dynamic_cast(const void *sub, |
| 1413 | // const abi::__class_type_info *src, |
| 1414 | // const abi::__class_type_info *dst, |
| 1415 | // std::ptrdiff_t src2dst_offset); |
| 1416 | |
| 1417 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 1418 | const llvm::Type *PtrDiffTy = |
| 1419 | CGF.ConvertType(CGF.getContext().getPointerDiffType()); |
| 1420 | |
| 1421 | const llvm::Type *Args[4] = { Int8PtrTy, Int8PtrTy, Int8PtrTy, PtrDiffTy }; |
| 1422 | |
| 1423 | const llvm::FunctionType *FTy = |
| 1424 | llvm::FunctionType::get(Int8PtrTy, Args, false); |
| 1425 | |
| 1426 | return CGF.CGM.CreateRuntimeFunction(FTy, "__dynamic_cast"); |
| 1427 | } |
| 1428 | |
| 1429 | static llvm::Constant *getBadCastFn(CodeGenFunction &CGF) { |
| 1430 | // void __cxa_bad_cast(); |
| 1431 | |
| 1432 | const llvm::Type *VoidTy = llvm::Type::getVoidTy(CGF.getLLVMContext()); |
| 1433 | const llvm::FunctionType *FTy = |
| 1434 | llvm::FunctionType::get(VoidTy, false); |
| 1435 | |
| 1436 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_cast"); |
| 1437 | } |
| 1438 | |
Anders Carlsson | 3ddcdd5 | 2011-04-11 01:45:29 +0000 | [diff] [blame] | 1439 | static void EmitBadCastCall(CodeGenFunction &CGF) { |
| 1440 | llvm::Value *F = getBadCastFn(CGF); |
| 1441 | if (llvm::BasicBlock *InvokeDest = CGF.getInvokeDest()) { |
| 1442 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
| 1443 | CGF.Builder.CreateInvoke(F, Cont, InvokeDest)->setDoesNotReturn(); |
| 1444 | CGF.EmitBlock(Cont); |
| 1445 | } else |
| 1446 | CGF.Builder.CreateCall(F)->setDoesNotReturn(); |
| 1447 | |
| 1448 | CGF.Builder.CreateUnreachable(); |
| 1449 | } |
| 1450 | |
Anders Carlsson | f0cb4a6 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1451 | static llvm::Value * |
| 1452 | EmitDynamicCastCall(CodeGenFunction &CGF, llvm::Value *Value, |
| 1453 | QualType SrcTy, QualType DestTy, |
| 1454 | llvm::BasicBlock *CastEnd) { |
| 1455 | const llvm::Type *PtrDiffLTy = |
| 1456 | CGF.ConvertType(CGF.getContext().getPointerDiffType()); |
| 1457 | const llvm::Type *DestLTy = CGF.ConvertType(DestTy); |
| 1458 | |
| 1459 | if (const PointerType *PTy = DestTy->getAs<PointerType>()) { |
| 1460 | if (PTy->getPointeeType()->isVoidType()) { |
| 1461 | // C++ [expr.dynamic.cast]p7: |
| 1462 | // If T is "pointer to cv void," then the result is a pointer to the |
| 1463 | // most derived object pointed to by v. |
| 1464 | |
| 1465 | // Get the vtable pointer. |
| 1466 | llvm::Value *VTable = CGF.GetVTablePtr(Value, PtrDiffLTy->getPointerTo()); |
| 1467 | |
| 1468 | // Get the offset-to-top from the vtable. |
| 1469 | llvm::Value *OffsetToTop = |
| 1470 | CGF.Builder.CreateConstInBoundsGEP1_64(VTable, -2ULL); |
| 1471 | OffsetToTop = CGF.Builder.CreateLoad(OffsetToTop, "offset.to.top"); |
| 1472 | |
| 1473 | // Finally, add the offset to the pointer. |
| 1474 | Value = CGF.EmitCastToVoidPtr(Value); |
| 1475 | Value = CGF.Builder.CreateInBoundsGEP(Value, OffsetToTop); |
| 1476 | |
| 1477 | return CGF.Builder.CreateBitCast(Value, DestLTy); |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | QualType SrcRecordTy; |
| 1482 | QualType DestRecordTy; |
| 1483 | |
| 1484 | if (const PointerType *DestPTy = DestTy->getAs<PointerType>()) { |
| 1485 | SrcRecordTy = SrcTy->castAs<PointerType>()->getPointeeType(); |
| 1486 | DestRecordTy = DestPTy->getPointeeType(); |
| 1487 | } else { |
| 1488 | SrcRecordTy = SrcTy; |
| 1489 | DestRecordTy = DestTy->castAs<ReferenceType>()->getPointeeType(); |
| 1490 | } |
| 1491 | |
| 1492 | assert(SrcRecordTy->isRecordType() && "source type must be a record type!"); |
| 1493 | assert(DestRecordTy->isRecordType() && "dest type must be a record type!"); |
| 1494 | |
| 1495 | llvm::Value *SrcRTTI = |
| 1496 | CGF.CGM.GetAddrOfRTTIDescriptor(SrcRecordTy.getUnqualifiedType()); |
| 1497 | llvm::Value *DestRTTI = |
| 1498 | CGF.CGM.GetAddrOfRTTIDescriptor(DestRecordTy.getUnqualifiedType()); |
| 1499 | |
| 1500 | // FIXME: Actually compute a hint here. |
| 1501 | llvm::Value *OffsetHint = llvm::ConstantInt::get(PtrDiffLTy, -1ULL); |
| 1502 | |
| 1503 | // Emit the call to __dynamic_cast. |
| 1504 | Value = CGF.EmitCastToVoidPtr(Value); |
| 1505 | Value = CGF.Builder.CreateCall4(getDynamicCastFn(CGF), Value, |
| 1506 | SrcRTTI, DestRTTI, OffsetHint); |
| 1507 | Value = CGF.Builder.CreateBitCast(Value, DestLTy); |
| 1508 | |
| 1509 | /// C++ [expr.dynamic.cast]p9: |
| 1510 | /// A failed cast to reference type throws std::bad_cast |
| 1511 | if (DestTy->isReferenceType()) { |
| 1512 | llvm::BasicBlock *BadCastBlock = |
| 1513 | CGF.createBasicBlock("dynamic_cast.bad_cast"); |
| 1514 | |
| 1515 | llvm::Value *IsNull = CGF.Builder.CreateIsNull(Value); |
| 1516 | CGF.Builder.CreateCondBr(IsNull, BadCastBlock, CastEnd); |
| 1517 | |
| 1518 | CGF.EmitBlock(BadCastBlock); |
Anders Carlsson | 3ddcdd5 | 2011-04-11 01:45:29 +0000 | [diff] [blame] | 1519 | EmitBadCastCall(CGF); |
Anders Carlsson | f0cb4a6 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | return Value; |
| 1523 | } |
| 1524 | |
Anders Carlsson | 3ddcdd5 | 2011-04-11 01:45:29 +0000 | [diff] [blame] | 1525 | static llvm::Value *EmitDynamicCastToNull(CodeGenFunction &CGF, |
| 1526 | QualType DestTy) { |
| 1527 | const llvm::Type *DestLTy = CGF.ConvertType(DestTy); |
| 1528 | if (DestTy->isPointerType()) |
| 1529 | return llvm::Constant::getNullValue(DestLTy); |
| 1530 | |
| 1531 | /// C++ [expr.dynamic.cast]p9: |
| 1532 | /// A failed cast to reference type throws std::bad_cast |
| 1533 | EmitBadCastCall(CGF); |
| 1534 | |
| 1535 | CGF.EmitBlock(CGF.createBasicBlock("dynamic_cast.end")); |
| 1536 | return llvm::UndefValue::get(DestLTy); |
| 1537 | } |
| 1538 | |
Anders Carlsson | f0cb4a6 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1539 | llvm::Value *CodeGenFunction::EmitDynamicCast(llvm::Value *Value, |
Mike Stump | c849c05 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1540 | const CXXDynamicCastExpr *DCE) { |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 1541 | QualType DestTy = DCE->getTypeAsWritten(); |
Anders Carlsson | f0cb4a6 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1542 | |
Anders Carlsson | 3ddcdd5 | 2011-04-11 01:45:29 +0000 | [diff] [blame] | 1543 | if (DCE->isAlwaysNull()) |
| 1544 | return EmitDynamicCastToNull(*this, DestTy); |
| 1545 | |
| 1546 | QualType SrcTy = DCE->getSubExpr()->getType(); |
| 1547 | |
Anders Carlsson | f0cb4a6 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1548 | // C++ [expr.dynamic.cast]p4: |
| 1549 | // If the value of v is a null pointer value in the pointer case, the result |
| 1550 | // is the null pointer value of type T. |
| 1551 | bool ShouldNullCheckSrcValue = SrcTy->isPointerType(); |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 1552 | |
Anders Carlsson | f0cb4a6 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1553 | llvm::BasicBlock *CastNull = 0; |
| 1554 | llvm::BasicBlock *CastNotNull = 0; |
| 1555 | llvm::BasicBlock *CastEnd = createBasicBlock("dynamic_cast.end"); |
Mike Stump | c849c05 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1556 | |
Anders Carlsson | f0cb4a6 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1557 | if (ShouldNullCheckSrcValue) { |
| 1558 | CastNull = createBasicBlock("dynamic_cast.null"); |
| 1559 | CastNotNull = createBasicBlock("dynamic_cast.notnull"); |
| 1560 | |
| 1561 | llvm::Value *IsNull = Builder.CreateIsNull(Value); |
| 1562 | Builder.CreateCondBr(IsNull, CastNull, CastNotNull); |
| 1563 | EmitBlock(CastNotNull); |
Mike Stump | c849c05 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1564 | } |
| 1565 | |
Anders Carlsson | f0cb4a6 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1566 | Value = EmitDynamicCastCall(*this, Value, SrcTy, DestTy, CastEnd); |
| 1567 | |
| 1568 | if (ShouldNullCheckSrcValue) { |
| 1569 | EmitBranch(CastEnd); |
| 1570 | |
| 1571 | EmitBlock(CastNull); |
| 1572 | EmitBranch(CastEnd); |
| 1573 | } |
| 1574 | |
| 1575 | EmitBlock(CastEnd); |
| 1576 | |
| 1577 | if (ShouldNullCheckSrcValue) { |
| 1578 | llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2); |
| 1579 | PHI->addIncoming(Value, CastNotNull); |
| 1580 | PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), CastNull); |
| 1581 | |
| 1582 | Value = PHI; |
| 1583 | } |
| 1584 | |
| 1585 | return Value; |
Mike Stump | c849c05 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1586 | } |