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