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