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