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" |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 16 | #include "CGCUDARuntime.h" |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 17 | #include "CGCXXABI.h" |
Fariborz Jahanian | 60d215b | 2010-05-20 21:38:57 +0000 | [diff] [blame] | 18 | #include "CGObjCRuntime.h" |
Devang Patel | 91bbb55 | 2010-09-30 19:05:55 +0000 | [diff] [blame] | 19 | #include "CGDebugInfo.h" |
Chris Lattner | 26008e0 | 2010-07-20 20:19:24 +0000 | [diff] [blame] | 20 | #include "llvm/Intrinsics.h" |
Anders Carlsson | bbe277c | 2011-04-13 02:35:36 +0000 | [diff] [blame] | 21 | #include "llvm/Support/CallSite.h" |
| 22 | |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | using namespace CodeGen; |
| 25 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 26 | RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD, |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 27 | SourceLocation CallLoc, |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 28 | llvm::Value *Callee, |
| 29 | ReturnValueSlot ReturnValue, |
| 30 | llvm::Value *This, |
Anders Carlsson | e36a6b3 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 31 | llvm::Value *VTT, |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 32 | CallExpr::const_arg_iterator ArgBeg, |
| 33 | CallExpr::const_arg_iterator ArgEnd) { |
| 34 | assert(MD->isInstance() && |
| 35 | "Trying to emit a member call expr on a static method!"); |
| 36 | |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 37 | // C++11 [class.mfct.non-static]p2: |
| 38 | // If a non-static member function of a class X is called for an object that |
| 39 | // is not of type X, or of a type derived from X, the behavior is undefined. |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 40 | EmitTypeCheck(TCK_MemberCall, CallLoc, This, |
Richard Smith | 4d1458e | 2012-09-08 02:08:36 +0000 | [diff] [blame] | 41 | getContext().getRecordType(MD->getParent())); |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 42 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 43 | CallArgList Args; |
| 44 | |
| 45 | // Push the this ptr. |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 46 | Args.add(RValue::get(This), MD->getThisType(getContext())); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 47 | |
Anders Carlsson | e36a6b3 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 48 | // If there is a VTT parameter, emit it. |
| 49 | if (VTT) { |
| 50 | QualType T = getContext().getPointerType(getContext().VoidPtrTy); |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 51 | Args.add(RValue::get(VTT), T); |
Anders Carlsson | e36a6b3 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 52 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 53 | |
| 54 | const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); |
| 55 | RequiredArgs required = RequiredArgs::forPrototypePlus(FPT, Args.size()); |
Anders Carlsson | e36a6b3 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 56 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 57 | // And the rest of the call args. |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 58 | EmitCallArgs(Args, FPT, ArgBeg, ArgEnd); |
| 59 | |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 60 | return EmitCall(CGM.getTypes().arrangeCXXMethodCall(Args, FPT, required), |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 61 | Callee, ReturnValue, Args, MD); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Anders Carlsson | c53d9e8 | 2011-04-10 18:20:53 +0000 | [diff] [blame] | 64 | // FIXME: Ideally Expr::IgnoreParenNoopCasts should do this, but it doesn't do |
| 65 | // quite what we want. |
| 66 | static const Expr *skipNoOpCastsAndParens(const Expr *E) { |
| 67 | while (true) { |
| 68 | if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
| 69 | E = PE->getSubExpr(); |
| 70 | continue; |
| 71 | } |
| 72 | |
| 73 | if (const CastExpr *CE = dyn_cast<CastExpr>(E)) { |
| 74 | if (CE->getCastKind() == CK_NoOp) { |
| 75 | E = CE->getSubExpr(); |
| 76 | continue; |
| 77 | } |
| 78 | } |
| 79 | if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
| 80 | if (UO->getOpcode() == UO_Extension) { |
| 81 | E = UO->getSubExpr(); |
| 82 | continue; |
| 83 | } |
| 84 | } |
| 85 | return E; |
| 86 | } |
| 87 | } |
| 88 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 89 | /// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given |
| 90 | /// expr can be devirtualized. |
Fariborz Jahanian | 252a47f | 2011-01-21 01:04:41 +0000 | [diff] [blame] | 91 | static bool canDevirtualizeMemberFunctionCalls(ASTContext &Context, |
| 92 | const Expr *Base, |
Anders Carlsson | a7911fa | 2010-10-27 13:28:46 +0000 | [diff] [blame] | 93 | const CXXMethodDecl *MD) { |
| 94 | |
Anders Carlsson | 1ae64c5 | 2011-01-29 03:52:01 +0000 | [diff] [blame] | 95 | // When building with -fapple-kext, all calls must go through the vtable since |
| 96 | // the kernel linker can do runtime patching of vtables. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 97 | if (Context.getLangOpts().AppleKext) |
Fariborz Jahanian | 252a47f | 2011-01-21 01:04:41 +0000 | [diff] [blame] | 98 | return false; |
| 99 | |
Anders Carlsson | 1ae64c5 | 2011-01-29 03:52:01 +0000 | [diff] [blame] | 100 | // If the most derived class is marked final, we know that no subclass can |
| 101 | // override this member function and so we can devirtualize it. For example: |
| 102 | // |
| 103 | // struct A { virtual void f(); } |
| 104 | // struct B final : A { }; |
| 105 | // |
| 106 | // void f(B *b) { |
| 107 | // b->f(); |
| 108 | // } |
| 109 | // |
Rafael Espindola | b7f5a9c | 2012-06-27 18:18:05 +0000 | [diff] [blame] | 110 | const CXXRecordDecl *MostDerivedClassDecl = Base->getBestDynamicClassType(); |
Anders Carlsson | 1ae64c5 | 2011-01-29 03:52:01 +0000 | [diff] [blame] | 111 | if (MostDerivedClassDecl->hasAttr<FinalAttr>()) |
| 112 | return true; |
| 113 | |
Anders Carlsson | 19588aa | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 114 | // 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] | 115 | // overridden and can therefore devirtualize it. |
Anders Carlsson | 1eb9596 | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 116 | if (MD->hasAttr<FinalAttr>()) |
Anders Carlsson | a7911fa | 2010-10-27 13:28:46 +0000 | [diff] [blame] | 117 | return true; |
Anders Carlsson | b00c214 | 2010-10-27 13:34:43 +0000 | [diff] [blame] | 118 | |
Anders Carlsson | 19588aa | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 119 | // Similarly, if the class itself is marked 'final' it can't be overridden |
| 120 | // and we can therefore devirtualize the member function call. |
Anders Carlsson | 1eb9596 | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 121 | if (MD->getParent()->hasAttr<FinalAttr>()) |
Anders Carlsson | b00c214 | 2010-10-27 13:34:43 +0000 | [diff] [blame] | 122 | return true; |
| 123 | |
Anders Carlsson | c53d9e8 | 2011-04-10 18:20:53 +0000 | [diff] [blame] | 124 | Base = skipNoOpCastsAndParens(Base); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 125 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) { |
| 126 | if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) { |
| 127 | // This is a record decl. We know the type and can devirtualize it. |
| 128 | return VD->getType()->isRecordType(); |
| 129 | } |
| 130 | |
| 131 | return false; |
| 132 | } |
Richard Smith | 48c1531 | 2012-08-15 22:59:28 +0000 | [diff] [blame] | 133 | |
| 134 | // We can devirtualize calls on an object accessed by a class member access |
| 135 | // expression, since by C++11 [basic.life]p6 we know that it can't refer to |
| 136 | // a derived class object constructed in the same location. |
| 137 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Base)) |
| 138 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(ME->getMemberDecl())) |
| 139 | return VD->getType()->isRecordType(); |
| 140 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 141 | // We can always devirtualize calls on temporary object expressions. |
Eli Friedman | a682427 | 2010-01-31 20:58:15 +0000 | [diff] [blame] | 142 | if (isa<CXXConstructExpr>(Base)) |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 143 | return true; |
| 144 | |
| 145 | // And calls on bound temporaries. |
| 146 | if (isa<CXXBindTemporaryExpr>(Base)) |
| 147 | return true; |
| 148 | |
| 149 | // Check if this is a call expr that returns a record type. |
| 150 | if (const CallExpr *CE = dyn_cast<CallExpr>(Base)) |
| 151 | return CE->getCallReturnType()->isRecordType(); |
Anders Carlsson | a7911fa | 2010-10-27 13:28:46 +0000 | [diff] [blame] | 152 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 153 | // We can't devirtualize the call. |
| 154 | return false; |
| 155 | } |
| 156 | |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 157 | static CXXRecordDecl *getCXXRecord(const Expr *E) { |
| 158 | QualType T = E->getType(); |
| 159 | if (const PointerType *PTy = T->getAs<PointerType>()) |
| 160 | T = PTy->getPointeeType(); |
| 161 | const RecordType *Ty = T->castAs<RecordType>(); |
| 162 | return cast<CXXRecordDecl>(Ty->getDecl()); |
| 163 | } |
| 164 | |
Francois Pichet | 6422579 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 165 | // Note: This function also emit constructor calls to support a MSVC |
| 166 | // extensions allowing explicit constructor function call. |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 167 | RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE, |
| 168 | ReturnValueSlot ReturnValue) { |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 169 | const Expr *callee = CE->getCallee()->IgnoreParens(); |
| 170 | |
| 171 | if (isa<BinaryOperator>(callee)) |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 172 | return EmitCXXMemberPointerCallExpr(CE, ReturnValue); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 173 | |
| 174 | const MemberExpr *ME = cast<MemberExpr>(callee); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 175 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl()); |
| 176 | |
Devang Patel | 91bbb55 | 2010-09-30 19:05:55 +0000 | [diff] [blame] | 177 | CGDebugInfo *DI = getDebugInfo(); |
Douglas Gregor | b0eea8b | 2012-10-23 20:05:01 +0000 | [diff] [blame] | 178 | if (DI && |
| 179 | CGM.getCodeGenOpts().getDebugInfo() == CodeGenOptions::LimitedDebugInfo && |
| 180 | !isa<CallExpr>(ME->getBase())) { |
Devang Patel | 91bbb55 | 2010-09-30 19:05:55 +0000 | [diff] [blame] | 181 | QualType PQTy = ME->getBase()->IgnoreParenImpCasts()->getType(); |
| 182 | if (const PointerType * PTy = dyn_cast<PointerType>(PQTy)) { |
| 183 | DI->getOrCreateRecordType(PTy->getPointeeType(), |
| 184 | MD->getParent()->getLocation()); |
| 185 | } |
| 186 | } |
| 187 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 188 | if (MD->isStatic()) { |
| 189 | // The method is static, emit it as we would a regular call. |
| 190 | llvm::Value *Callee = CGM.GetAddrOfFunction(MD); |
| 191 | return EmitCall(getContext().getPointerType(MD->getType()), Callee, |
| 192 | ReturnValue, CE->arg_begin(), CE->arg_end()); |
| 193 | } |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 194 | |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 195 | // Compute the object pointer. |
Rafael Espindola | ecbe2e9 | 2012-06-28 01:56:38 +0000 | [diff] [blame] | 196 | const Expr *Base = ME->getBase(); |
| 197 | bool CanUseVirtualCall = MD->isVirtual() && !ME->hasQualifier(); |
Rafael Espindola | ecbe2e9 | 2012-06-28 01:56:38 +0000 | [diff] [blame] | 198 | |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 199 | const CXXMethodDecl *DevirtualizedMethod = NULL; |
| 200 | if (CanUseVirtualCall && |
| 201 | canDevirtualizeMemberFunctionCalls(getContext(), Base, MD)) { |
| 202 | const CXXRecordDecl *BestDynamicDecl = Base->getBestDynamicClassType(); |
| 203 | DevirtualizedMethod = MD->getCorrespondingMethodInClass(BestDynamicDecl); |
| 204 | assert(DevirtualizedMethod); |
| 205 | const CXXRecordDecl *DevirtualizedClass = DevirtualizedMethod->getParent(); |
| 206 | const Expr *Inner = Base->ignoreParenBaseCasts(); |
| 207 | if (getCXXRecord(Inner) == DevirtualizedClass) |
| 208 | // If the class of the Inner expression is where the dynamic method |
| 209 | // is defined, build the this pointer from it. |
| 210 | Base = Inner; |
| 211 | else if (getCXXRecord(Base) != DevirtualizedClass) { |
| 212 | // If the method is defined in a class that is not the best dynamic |
| 213 | // one or the one of the full expression, we would have to build |
| 214 | // a derived-to-base cast to compute the correct this pointer, but |
| 215 | // we don't have support for that yet, so do a virtual call. |
| 216 | DevirtualizedMethod = NULL; |
| 217 | } |
Rafael Espindola | b27564a | 2012-06-28 17:57:36 +0000 | [diff] [blame] | 218 | // If the return types are not the same, this might be a case where more |
| 219 | // code needs to run to compensate for it. For example, the derived |
| 220 | // method might return a type that inherits form from the return |
| 221 | // type of MD and has a prefix. |
| 222 | // For now we just avoid devirtualizing these covariant cases. |
| 223 | if (DevirtualizedMethod && |
| 224 | DevirtualizedMethod->getResultType().getCanonicalType() != |
| 225 | MD->getResultType().getCanonicalType()) |
Rafael Espindola | debc71c | 2012-06-28 15:11:39 +0000 | [diff] [blame] | 226 | DevirtualizedMethod = NULL; |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 227 | } |
Rafael Espindola | ecbe2e9 | 2012-06-28 01:56:38 +0000 | [diff] [blame] | 228 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 229 | llvm::Value *This; |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 230 | if (ME->isArrow()) |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 231 | This = EmitScalarExpr(Base); |
John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 232 | else |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 233 | This = EmitLValue(Base).getAddress(); |
Rafael Espindola | ecbe2e9 | 2012-06-28 01:56:38 +0000 | [diff] [blame] | 234 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 235 | |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 236 | if (MD->isTrivial()) { |
| 237 | if (isa<CXXDestructorDecl>(MD)) return RValue::get(0); |
Francois Pichet | 6422579 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 238 | if (isa<CXXConstructorDecl>(MD) && |
| 239 | cast<CXXConstructorDecl>(MD)->isDefaultConstructor()) |
| 240 | return RValue::get(0); |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 241 | |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 242 | if (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator()) { |
| 243 | // We don't like to generate the trivial copy/move assignment operator |
| 244 | // when it isn't necessary; just produce the proper effect here. |
Francois Pichet | 6422579 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 245 | llvm::Value *RHS = EmitLValue(*CE->arg_begin()).getAddress(); |
Benjamin Kramer | 1ca6691 | 2012-09-30 12:43:37 +0000 | [diff] [blame] | 246 | EmitAggregateAssign(This, RHS, CE->getType()); |
Francois Pichet | 6422579 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 247 | return RValue::get(This); |
| 248 | } |
| 249 | |
| 250 | if (isa<CXXConstructorDecl>(MD) && |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 251 | cast<CXXConstructorDecl>(MD)->isCopyOrMoveConstructor()) { |
| 252 | // Trivial move and copy ctor are the same. |
Francois Pichet | 6422579 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 253 | llvm::Value *RHS = EmitLValue(*CE->arg_begin()).getAddress(); |
| 254 | EmitSynthesizedCXXCopyCtorCall(cast<CXXConstructorDecl>(MD), This, RHS, |
| 255 | CE->arg_begin(), CE->arg_end()); |
| 256 | return RValue::get(This); |
| 257 | } |
| 258 | llvm_unreachable("unknown trivial member function"); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 259 | } |
| 260 | |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 261 | // Compute the function type we're calling. |
Eli Friedman | ade6097 | 2012-10-25 00:12:49 +0000 | [diff] [blame] | 262 | const CXXMethodDecl *CalleeDecl = DevirtualizedMethod ? DevirtualizedMethod : MD; |
Francois Pichet | 6422579 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 263 | const CGFunctionInfo *FInfo = 0; |
Eli Friedman | ade6097 | 2012-10-25 00:12:49 +0000 | [diff] [blame] | 264 | if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(CalleeDecl)) |
| 265 | FInfo = &CGM.getTypes().arrangeCXXDestructor(Dtor, |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 266 | Dtor_Complete); |
Eli Friedman | ade6097 | 2012-10-25 00:12:49 +0000 | [diff] [blame] | 267 | else if (const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(CalleeDecl)) |
| 268 | FInfo = &CGM.getTypes().arrangeCXXConstructorDeclaration(Ctor, |
| 269 | Ctor_Complete); |
Francois Pichet | 6422579 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 270 | else |
Eli Friedman | ade6097 | 2012-10-25 00:12:49 +0000 | [diff] [blame] | 271 | FInfo = &CGM.getTypes().arrangeCXXMethodDeclaration(CalleeDecl); |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 272 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 273 | llvm::Type *Ty = CGM.getTypes().GetFunctionType(*FInfo); |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 274 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 275 | // C++ [class.virtual]p12: |
| 276 | // Explicit qualification with the scope operator (5.1) suppresses the |
| 277 | // virtual call mechanism. |
| 278 | // |
| 279 | // We also don't emit a virtual call if the base expression has a record type |
| 280 | // because then we know what the type is. |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 281 | bool UseVirtualCall = CanUseVirtualCall && !DevirtualizedMethod; |
Rafael Espindola | 49e860b | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 282 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 283 | llvm::Value *Callee; |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 284 | if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD)) { |
| 285 | if (UseVirtualCall) { |
| 286 | Callee = BuildVirtualCall(Dtor, Dtor_Complete, This, Ty); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 287 | } else { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 288 | if (getContext().getLangOpts().AppleKext && |
Fariborz Jahanian | 265c325 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 289 | MD->isVirtual() && |
| 290 | ME->hasQualifier()) |
Fariborz Jahanian | 7f6f81b | 2011-02-03 19:27:17 +0000 | [diff] [blame] | 291 | Callee = BuildAppleKextVirtualCall(MD, ME->getQualifier(), Ty); |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 292 | else if (!DevirtualizedMethod) |
Rafael Espindola | 727a771 | 2012-06-26 19:18:25 +0000 | [diff] [blame] | 293 | Callee = CGM.GetAddrOfFunction(GlobalDecl(Dtor, Dtor_Complete), Ty); |
Rafael Espindola | 49e860b | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 294 | else { |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 295 | const CXXDestructorDecl *DDtor = |
| 296 | cast<CXXDestructorDecl>(DevirtualizedMethod); |
Rafael Espindola | 49e860b | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 297 | Callee = CGM.GetAddrOfFunction(GlobalDecl(DDtor, Dtor_Complete), Ty); |
| 298 | } |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 299 | } |
Francois Pichet | 6422579 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 300 | } else if (const CXXConstructorDecl *Ctor = |
| 301 | dyn_cast<CXXConstructorDecl>(MD)) { |
| 302 | Callee = CGM.GetAddrOfFunction(GlobalDecl(Ctor, Ctor_Complete), Ty); |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 303 | } else if (UseVirtualCall) { |
Fariborz Jahanian | 47609b0 | 2011-01-20 17:19:02 +0000 | [diff] [blame] | 304 | Callee = BuildVirtualCall(MD, This, Ty); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 305 | } else { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 306 | if (getContext().getLangOpts().AppleKext && |
Fariborz Jahanian | 9f9438b | 2011-01-28 23:42:29 +0000 | [diff] [blame] | 307 | MD->isVirtual() && |
Fariborz Jahanian | 252a47f | 2011-01-21 01:04:41 +0000 | [diff] [blame] | 308 | ME->hasQualifier()) |
Fariborz Jahanian | 7f6f81b | 2011-02-03 19:27:17 +0000 | [diff] [blame] | 309 | Callee = BuildAppleKextVirtualCall(MD, ME->getQualifier(), Ty); |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 310 | else if (!DevirtualizedMethod) |
Rafael Espindola | 727a771 | 2012-06-26 19:18:25 +0000 | [diff] [blame] | 311 | Callee = CGM.GetAddrOfFunction(MD, Ty); |
Rafael Espindola | 49e860b | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 312 | else { |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 313 | Callee = CGM.GetAddrOfFunction(DevirtualizedMethod, Ty); |
Rafael Espindola | 49e860b | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 314 | } |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 317 | return EmitCXXMemberCall(MD, CE->getExprLoc(), Callee, ReturnValue, This, |
| 318 | /*VTT=*/0, CE->arg_begin(), CE->arg_end()); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | RValue |
| 322 | CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E, |
| 323 | ReturnValueSlot ReturnValue) { |
| 324 | const BinaryOperator *BO = |
| 325 | cast<BinaryOperator>(E->getCallee()->IgnoreParens()); |
| 326 | const Expr *BaseExpr = BO->getLHS(); |
| 327 | const Expr *MemFnExpr = BO->getRHS(); |
| 328 | |
| 329 | const MemberPointerType *MPT = |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 330 | MemFnExpr->getType()->castAs<MemberPointerType>(); |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 331 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 332 | const FunctionProtoType *FPT = |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 333 | MPT->getPointeeType()->castAs<FunctionProtoType>(); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 334 | const CXXRecordDecl *RD = |
| 335 | cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl()); |
| 336 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 337 | // Get the member function pointer. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 338 | llvm::Value *MemFnPtr = EmitScalarExpr(MemFnExpr); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 339 | |
| 340 | // Emit the 'this' pointer. |
| 341 | llvm::Value *This; |
| 342 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 343 | if (BO->getOpcode() == BO_PtrMemI) |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 344 | This = EmitScalarExpr(BaseExpr); |
| 345 | else |
| 346 | This = EmitLValue(BaseExpr).getAddress(); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 347 | |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 348 | EmitTypeCheck(TCK_MemberCall, E->getExprLoc(), This, |
| 349 | QualType(MPT->getClass(), 0)); |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 350 | |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 351 | // Ask the ABI to load the callee. Note that This is modified. |
| 352 | llvm::Value *Callee = |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 353 | CGM.getCXXABI().EmitLoadOfMemberFunctionPointer(*this, This, MemFnPtr, MPT); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 354 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 355 | CallArgList Args; |
| 356 | |
| 357 | QualType ThisType = |
| 358 | getContext().getPointerType(getContext().getTagDeclType(RD)); |
| 359 | |
| 360 | // Push the this ptr. |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 361 | Args.add(RValue::get(This), ThisType); |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 362 | |
| 363 | RequiredArgs required = RequiredArgs::forPrototypePlus(FPT, 1); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 364 | |
| 365 | // And the rest of the call args |
| 366 | EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end()); |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 367 | return EmitCall(CGM.getTypes().arrangeCXXMethodCall(Args, FPT, required), Callee, |
Tilmann Scheller | 99cc30c | 2011-03-02 21:36:49 +0000 | [diff] [blame] | 368 | ReturnValue, Args); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | RValue |
| 372 | CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E, |
| 373 | const CXXMethodDecl *MD, |
| 374 | ReturnValueSlot ReturnValue) { |
| 375 | assert(MD->isInstance() && |
| 376 | "Trying to emit a member call expr on a static method!"); |
John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 377 | LValue LV = EmitLValue(E->getArg(0)); |
| 378 | llvm::Value *This = LV.getAddress(); |
| 379 | |
Douglas Gregor | 146b8e9 | 2011-09-06 16:26:56 +0000 | [diff] [blame] | 380 | if ((MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator()) && |
| 381 | MD->isTrivial()) { |
| 382 | llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress(); |
| 383 | QualType Ty = E->getType(); |
Benjamin Kramer | 1ca6691 | 2012-09-30 12:43:37 +0000 | [diff] [blame] | 384 | EmitAggregateAssign(This, Src, Ty); |
Douglas Gregor | 146b8e9 | 2011-09-06 16:26:56 +0000 | [diff] [blame] | 385 | return RValue::get(This); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Anders Carlsson | c36783e | 2011-05-08 20:32:23 +0000 | [diff] [blame] | 388 | llvm::Value *Callee = EmitCXXOperatorMemberCallee(E, MD, This); |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 389 | return EmitCXXMemberCall(MD, E->getExprLoc(), Callee, ReturnValue, This, |
| 390 | /*VTT=*/0, E->arg_begin() + 1, E->arg_end()); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 393 | RValue CodeGenFunction::EmitCUDAKernelCallExpr(const CUDAKernelCallExpr *E, |
| 394 | ReturnValueSlot ReturnValue) { |
| 395 | return CGM.getCUDARuntime().EmitCUDAKernelCallExpr(*this, E, ReturnValue); |
| 396 | } |
| 397 | |
Eli Friedman | fde961d | 2011-10-14 02:27:24 +0000 | [diff] [blame] | 398 | static void EmitNullBaseClassInitialization(CodeGenFunction &CGF, |
| 399 | llvm::Value *DestPtr, |
| 400 | const CXXRecordDecl *Base) { |
| 401 | if (Base->isEmpty()) |
| 402 | return; |
| 403 | |
| 404 | DestPtr = CGF.EmitCastToVoidPtr(DestPtr); |
| 405 | |
| 406 | const ASTRecordLayout &Layout = CGF.getContext().getASTRecordLayout(Base); |
| 407 | CharUnits Size = Layout.getNonVirtualSize(); |
| 408 | CharUnits Align = Layout.getNonVirtualAlign(); |
| 409 | |
| 410 | llvm::Value *SizeVal = CGF.CGM.getSize(Size); |
| 411 | |
| 412 | // If the type contains a pointer to data member we can't memset it to zero. |
| 413 | // Instead, create a null constant and copy it to the destination. |
| 414 | // TODO: there are other patterns besides zero that we can usefully memset, |
| 415 | // like -1, which happens to be the pattern used by member-pointers. |
| 416 | // TODO: isZeroInitializable can be over-conservative in the case where a |
| 417 | // virtual base contains a member pointer. |
| 418 | if (!CGF.CGM.getTypes().isZeroInitializable(Base)) { |
| 419 | llvm::Constant *NullConstant = CGF.CGM.EmitNullConstantForBase(Base); |
| 420 | |
| 421 | llvm::GlobalVariable *NullVariable = |
| 422 | new llvm::GlobalVariable(CGF.CGM.getModule(), NullConstant->getType(), |
| 423 | /*isConstant=*/true, |
| 424 | llvm::GlobalVariable::PrivateLinkage, |
| 425 | NullConstant, Twine()); |
| 426 | NullVariable->setAlignment(Align.getQuantity()); |
| 427 | llvm::Value *SrcPtr = CGF.EmitCastToVoidPtr(NullVariable); |
| 428 | |
| 429 | // Get and call the appropriate llvm.memcpy overload. |
| 430 | CGF.Builder.CreateMemCpy(DestPtr, SrcPtr, SizeVal, Align.getQuantity()); |
| 431 | return; |
| 432 | } |
| 433 | |
| 434 | // Otherwise, just memset the whole thing to zero. This is legal |
| 435 | // because in LLVM, all default initializers (other than the ones we just |
| 436 | // handled above) are guaranteed to have a bit pattern of all zeros. |
| 437 | CGF.Builder.CreateMemSet(DestPtr, CGF.Builder.getInt8(0), SizeVal, |
| 438 | Align.getQuantity()); |
| 439 | } |
| 440 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 441 | void |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 442 | CodeGenFunction::EmitCXXConstructExpr(const CXXConstructExpr *E, |
| 443 | AggValueSlot Dest) { |
| 444 | assert(!Dest.isIgnored() && "Must have a destination!"); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 445 | const CXXConstructorDecl *CD = E->getConstructor(); |
Douglas Gregor | 630c76e | 2010-08-22 16:15:35 +0000 | [diff] [blame] | 446 | |
| 447 | // If we require zero initialization before (or instead of) calling the |
| 448 | // constructor, as can be the case with a non-user-provided default |
Argyrios Kyrtzidis | 0353526 | 2011-04-28 22:57:55 +0000 | [diff] [blame] | 449 | // constructor, emit the zero initialization now, unless destination is |
| 450 | // already zeroed. |
Eli Friedman | fde961d | 2011-10-14 02:27:24 +0000 | [diff] [blame] | 451 | if (E->requiresZeroInitialization() && !Dest.isZeroed()) { |
| 452 | switch (E->getConstructionKind()) { |
| 453 | case CXXConstructExpr::CK_Delegating: |
Eli Friedman | fde961d | 2011-10-14 02:27:24 +0000 | [diff] [blame] | 454 | case CXXConstructExpr::CK_Complete: |
| 455 | EmitNullInitialization(Dest.getAddr(), E->getType()); |
| 456 | break; |
| 457 | case CXXConstructExpr::CK_VirtualBase: |
| 458 | case CXXConstructExpr::CK_NonVirtualBase: |
| 459 | EmitNullBaseClassInitialization(*this, Dest.getAddr(), CD->getParent()); |
| 460 | break; |
| 461 | } |
| 462 | } |
Douglas Gregor | 630c76e | 2010-08-22 16:15:35 +0000 | [diff] [blame] | 463 | |
| 464 | // If this is a call to a trivial default constructor, do nothing. |
| 465 | if (CD->isTrivial() && CD->isDefaultConstructor()) |
| 466 | return; |
| 467 | |
John McCall | 8ea46b6 | 2010-09-18 00:58:34 +0000 | [diff] [blame] | 468 | // Elide the constructor if we're constructing from a temporary. |
| 469 | // The temporary check is required because Sema sets this on NRVO |
| 470 | // returns. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 471 | if (getContext().getLangOpts().ElideConstructors && E->isElidable()) { |
John McCall | 8ea46b6 | 2010-09-18 00:58:34 +0000 | [diff] [blame] | 472 | assert(getContext().hasSameUnqualifiedType(E->getType(), |
| 473 | E->getArg(0)->getType())); |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 474 | if (E->getArg(0)->isTemporaryObject(getContext(), CD->getParent())) { |
| 475 | EmitAggExpr(E->getArg(0), Dest); |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 476 | return; |
| 477 | } |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 478 | } |
Douglas Gregor | 630c76e | 2010-08-22 16:15:35 +0000 | [diff] [blame] | 479 | |
John McCall | f677a8e | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 480 | if (const ConstantArrayType *arrayType |
| 481 | = getContext().getAsConstantArrayType(E->getType())) { |
| 482 | EmitCXXAggrConstructorCall(CD, arrayType, Dest.getAddr(), |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 483 | E->arg_begin(), E->arg_end()); |
John McCall | f677a8e | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 484 | } else { |
Cameron Esfahani | bceca20 | 2011-05-06 21:28:42 +0000 | [diff] [blame] | 485 | CXXCtorType Type = Ctor_Complete; |
Alexis Hunt | 271c368 | 2011-05-03 20:19:28 +0000 | [diff] [blame] | 486 | bool ForVirtualBase = false; |
| 487 | |
| 488 | switch (E->getConstructionKind()) { |
| 489 | case CXXConstructExpr::CK_Delegating: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 490 | // We should be emitting a constructor; GlobalDecl will assert this |
| 491 | Type = CurGD.getCtorType(); |
Alexis Hunt | 271c368 | 2011-05-03 20:19:28 +0000 | [diff] [blame] | 492 | break; |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 493 | |
Alexis Hunt | 271c368 | 2011-05-03 20:19:28 +0000 | [diff] [blame] | 494 | case CXXConstructExpr::CK_Complete: |
| 495 | Type = Ctor_Complete; |
| 496 | break; |
| 497 | |
| 498 | case CXXConstructExpr::CK_VirtualBase: |
| 499 | ForVirtualBase = true; |
| 500 | // fall-through |
| 501 | |
| 502 | case CXXConstructExpr::CK_NonVirtualBase: |
| 503 | Type = Ctor_Base; |
| 504 | } |
Anders Carlsson | e11f9ce | 2010-05-02 23:20:53 +0000 | [diff] [blame] | 505 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 506 | // Call the constructor. |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 507 | EmitCXXConstructorCall(CD, Type, ForVirtualBase, Dest.getAddr(), |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 508 | E->arg_begin(), E->arg_end()); |
Anders Carlsson | e11f9ce | 2010-05-02 23:20:53 +0000 | [diff] [blame] | 509 | } |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Fariborz Jahanian | e988bda | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 512 | void |
| 513 | CodeGenFunction::EmitSynthesizedCXXCopyCtor(llvm::Value *Dest, |
| 514 | llvm::Value *Src, |
Fariborz Jahanian | 5019809 | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 515 | const Expr *Exp) { |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 516 | if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(Exp)) |
Fariborz Jahanian | e988bda | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 517 | Exp = E->getSubExpr(); |
| 518 | assert(isa<CXXConstructExpr>(Exp) && |
| 519 | "EmitSynthesizedCXXCopyCtor - unknown copy ctor expr"); |
| 520 | const CXXConstructExpr* E = cast<CXXConstructExpr>(Exp); |
| 521 | const CXXConstructorDecl *CD = E->getConstructor(); |
| 522 | RunCleanupsScope Scope(*this); |
| 523 | |
| 524 | // If we require zero initialization before (or instead of) calling the |
| 525 | // constructor, as can be the case with a non-user-provided default |
| 526 | // constructor, emit the zero initialization now. |
| 527 | // FIXME. Do I still need this for a copy ctor synthesis? |
| 528 | if (E->requiresZeroInitialization()) |
| 529 | EmitNullInitialization(Dest, E->getType()); |
| 530 | |
Chandler Carruth | 99da11c | 2010-11-15 13:54:43 +0000 | [diff] [blame] | 531 | assert(!getContext().getAsConstantArrayType(E->getType()) |
| 532 | && "EmitSynthesizedCXXCopyCtor - Copied-in Array"); |
Fariborz Jahanian | e988bda | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 533 | EmitSynthesizedCXXCopyCtorCall(CD, Dest, Src, |
| 534 | E->arg_begin(), E->arg_end()); |
| 535 | } |
| 536 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 537 | static CharUnits CalculateCookiePadding(CodeGenFunction &CGF, |
| 538 | const CXXNewExpr *E) { |
Anders Carlsson | 21122cf | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 539 | if (!E->isArray()) |
Ken Dyck | 3eb55cf | 2010-01-26 19:44:24 +0000 | [diff] [blame] | 540 | return CharUnits::Zero(); |
Anders Carlsson | 21122cf | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 541 | |
John McCall | 7ec4b43 | 2011-05-16 01:05:12 +0000 | [diff] [blame] | 542 | // No cookie is required if the operator new[] being used is the |
| 543 | // reserved placement operator new[]. |
| 544 | if (E->getOperatorNew()->isReservedGlobalPlacementOperator()) |
John McCall | aa4149a | 2010-08-23 01:17:59 +0000 | [diff] [blame] | 545 | return CharUnits::Zero(); |
| 546 | |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 547 | return CGF.CGM.getCXXABI().GetArrayCookieSize(E); |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 548 | } |
| 549 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 550 | static llvm::Value *EmitCXXNewAllocSize(CodeGenFunction &CGF, |
| 551 | const CXXNewExpr *e, |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 552 | unsigned minElements, |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 553 | llvm::Value *&numElements, |
| 554 | llvm::Value *&sizeWithoutCookie) { |
| 555 | QualType type = e->getAllocatedType(); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 556 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 557 | if (!e->isArray()) { |
| 558 | CharUnits typeSize = CGF.getContext().getTypeSizeInChars(type); |
| 559 | sizeWithoutCookie |
| 560 | = llvm::ConstantInt::get(CGF.SizeTy, typeSize.getQuantity()); |
| 561 | return sizeWithoutCookie; |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 562 | } |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 563 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 564 | // The width of size_t. |
| 565 | unsigned sizeWidth = CGF.SizeTy->getBitWidth(); |
| 566 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 567 | // Figure out the cookie size. |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 568 | llvm::APInt cookieSize(sizeWidth, |
| 569 | CalculateCookiePadding(CGF, e).getQuantity()); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 570 | |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 571 | // Emit the array size expression. |
Argyrios Kyrtzidis | 7648fb4 | 2010-08-26 15:23:38 +0000 | [diff] [blame] | 572 | // We multiply the size of all dimensions for NumElements. |
| 573 | // e.g for 'int[2][3]', ElemType is 'int' and NumElements is 6. |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 574 | numElements = CGF.EmitScalarExpr(e->getArraySize()); |
| 575 | assert(isa<llvm::IntegerType>(numElements->getType())); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 576 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 577 | // The number of elements can be have an arbitrary integer type; |
| 578 | // essentially, we need to multiply it by a constant factor, add a |
| 579 | // cookie size, and verify that the result is representable as a |
| 580 | // size_t. That's just a gloss, though, and it's wrong in one |
| 581 | // important way: if the count is negative, it's an error even if |
| 582 | // the cookie size would bring the total size >= 0. |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 583 | bool isSigned |
| 584 | = e->getArraySize()->getType()->isSignedIntegerOrEnumerationType(); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 585 | llvm::IntegerType *numElementsType |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 586 | = cast<llvm::IntegerType>(numElements->getType()); |
| 587 | unsigned numElementsWidth = numElementsType->getBitWidth(); |
| 588 | |
| 589 | // Compute the constant factor. |
| 590 | llvm::APInt arraySizeMultiplier(sizeWidth, 1); |
Argyrios Kyrtzidis | 7648fb4 | 2010-08-26 15:23:38 +0000 | [diff] [blame] | 591 | while (const ConstantArrayType *CAT |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 592 | = CGF.getContext().getAsConstantArrayType(type)) { |
| 593 | type = CAT->getElementType(); |
| 594 | arraySizeMultiplier *= CAT->getSize(); |
Argyrios Kyrtzidis | 7648fb4 | 2010-08-26 15:23:38 +0000 | [diff] [blame] | 595 | } |
| 596 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 597 | CharUnits typeSize = CGF.getContext().getTypeSizeInChars(type); |
| 598 | llvm::APInt typeSizeMultiplier(sizeWidth, typeSize.getQuantity()); |
| 599 | typeSizeMultiplier *= arraySizeMultiplier; |
| 600 | |
| 601 | // This will be a size_t. |
| 602 | llvm::Value *size; |
Chris Lattner | f2f3870 | 2010-07-20 21:07:09 +0000 | [diff] [blame] | 603 | |
Chris Lattner | 32ac583 | 2010-07-20 21:55:52 +0000 | [diff] [blame] | 604 | // If someone is doing 'new int[42]' there is no need to do a dynamic check. |
| 605 | // Don't bloat the -O0 code. |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 606 | if (llvm::ConstantInt *numElementsC = |
| 607 | dyn_cast<llvm::ConstantInt>(numElements)) { |
| 608 | const llvm::APInt &count = numElementsC->getValue(); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 609 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 610 | bool hasAnyOverflow = false; |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 611 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 612 | // If 'count' was a negative number, it's an overflow. |
| 613 | if (isSigned && count.isNegative()) |
| 614 | hasAnyOverflow = true; |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 615 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 616 | // We want to do all this arithmetic in size_t. If numElements is |
| 617 | // wider than that, check whether it's already too big, and if so, |
| 618 | // overflow. |
| 619 | else if (numElementsWidth > sizeWidth && |
| 620 | numElementsWidth - sizeWidth > count.countLeadingZeros()) |
| 621 | hasAnyOverflow = true; |
| 622 | |
| 623 | // Okay, compute a count at the right width. |
| 624 | llvm::APInt adjustedCount = count.zextOrTrunc(sizeWidth); |
| 625 | |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 626 | // If there is a brace-initializer, we cannot allocate fewer elements than |
| 627 | // there are initializers. If we do, that's treated like an overflow. |
| 628 | if (adjustedCount.ult(minElements)) |
| 629 | hasAnyOverflow = true; |
| 630 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 631 | // Scale numElements by that. This might overflow, but we don't |
| 632 | // care because it only overflows if allocationSize does, too, and |
| 633 | // if that overflows then we shouldn't use this. |
| 634 | numElements = llvm::ConstantInt::get(CGF.SizeTy, |
| 635 | adjustedCount * arraySizeMultiplier); |
| 636 | |
| 637 | // Compute the size before cookie, and track whether it overflowed. |
| 638 | bool overflow; |
| 639 | llvm::APInt allocationSize |
| 640 | = adjustedCount.umul_ov(typeSizeMultiplier, overflow); |
| 641 | hasAnyOverflow |= overflow; |
| 642 | |
| 643 | // Add in the cookie, and check whether it's overflowed. |
| 644 | if (cookieSize != 0) { |
| 645 | // Save the current size without a cookie. This shouldn't be |
| 646 | // used if there was overflow. |
| 647 | sizeWithoutCookie = llvm::ConstantInt::get(CGF.SizeTy, allocationSize); |
| 648 | |
| 649 | allocationSize = allocationSize.uadd_ov(cookieSize, overflow); |
| 650 | hasAnyOverflow |= overflow; |
| 651 | } |
| 652 | |
| 653 | // On overflow, produce a -1 so operator new will fail. |
| 654 | if (hasAnyOverflow) { |
| 655 | size = llvm::Constant::getAllOnesValue(CGF.SizeTy); |
| 656 | } else { |
| 657 | size = llvm::ConstantInt::get(CGF.SizeTy, allocationSize); |
| 658 | } |
| 659 | |
| 660 | // Otherwise, we might need to use the overflow intrinsics. |
| 661 | } else { |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 662 | // There are up to five conditions we need to test for: |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 663 | // 1) if isSigned, we need to check whether numElements is negative; |
| 664 | // 2) if numElementsWidth > sizeWidth, we need to check whether |
| 665 | // numElements is larger than something representable in size_t; |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 666 | // 3) if minElements > 0, we need to check whether numElements is smaller |
| 667 | // than that. |
| 668 | // 4) we need to compute |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 669 | // sizeWithoutCookie := numElements * typeSizeMultiplier |
| 670 | // and check whether it overflows; and |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 671 | // 5) if we need a cookie, we need to compute |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 672 | // size := sizeWithoutCookie + cookieSize |
| 673 | // and check whether it overflows. |
| 674 | |
| 675 | llvm::Value *hasOverflow = 0; |
| 676 | |
| 677 | // If numElementsWidth > sizeWidth, then one way or another, we're |
| 678 | // going to have to do a comparison for (2), and this happens to |
| 679 | // take care of (1), too. |
| 680 | if (numElementsWidth > sizeWidth) { |
| 681 | llvm::APInt threshold(numElementsWidth, 1); |
| 682 | threshold <<= sizeWidth; |
| 683 | |
| 684 | llvm::Value *thresholdV |
| 685 | = llvm::ConstantInt::get(numElementsType, threshold); |
| 686 | |
| 687 | hasOverflow = CGF.Builder.CreateICmpUGE(numElements, thresholdV); |
| 688 | numElements = CGF.Builder.CreateTrunc(numElements, CGF.SizeTy); |
| 689 | |
| 690 | // Otherwise, if we're signed, we want to sext up to size_t. |
| 691 | } else if (isSigned) { |
| 692 | if (numElementsWidth < sizeWidth) |
| 693 | numElements = CGF.Builder.CreateSExt(numElements, CGF.SizeTy); |
| 694 | |
| 695 | // If there's a non-1 type size multiplier, then we can do the |
| 696 | // signedness check at the same time as we do the multiply |
| 697 | // because a negative number times anything will cause an |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 698 | // unsigned overflow. Otherwise, we have to do it here. But at least |
| 699 | // in this case, we can subsume the >= minElements check. |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 700 | if (typeSizeMultiplier == 1) |
| 701 | hasOverflow = CGF.Builder.CreateICmpSLT(numElements, |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 702 | llvm::ConstantInt::get(CGF.SizeTy, minElements)); |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 703 | |
| 704 | // Otherwise, zext up to size_t if necessary. |
| 705 | } else if (numElementsWidth < sizeWidth) { |
| 706 | numElements = CGF.Builder.CreateZExt(numElements, CGF.SizeTy); |
| 707 | } |
| 708 | |
| 709 | assert(numElements->getType() == CGF.SizeTy); |
| 710 | |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 711 | if (minElements) { |
| 712 | // Don't allow allocation of fewer elements than we have initializers. |
| 713 | if (!hasOverflow) { |
| 714 | hasOverflow = CGF.Builder.CreateICmpULT(numElements, |
| 715 | llvm::ConstantInt::get(CGF.SizeTy, minElements)); |
| 716 | } else if (numElementsWidth > sizeWidth) { |
| 717 | // The other existing overflow subsumes this check. |
| 718 | // We do an unsigned comparison, since any signed value < -1 is |
| 719 | // taken care of either above or below. |
| 720 | hasOverflow = CGF.Builder.CreateOr(hasOverflow, |
| 721 | CGF.Builder.CreateICmpULT(numElements, |
| 722 | llvm::ConstantInt::get(CGF.SizeTy, minElements))); |
| 723 | } |
| 724 | } |
| 725 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 726 | size = numElements; |
| 727 | |
| 728 | // Multiply by the type size if necessary. This multiplier |
| 729 | // includes all the factors for nested arrays. |
| 730 | // |
| 731 | // This step also causes numElements to be scaled up by the |
| 732 | // nested-array factor if necessary. Overflow on this computation |
| 733 | // can be ignored because the result shouldn't be used if |
| 734 | // allocation fails. |
| 735 | if (typeSizeMultiplier != 1) { |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 736 | llvm::Value *umul_with_overflow |
Benjamin Kramer | 8d375ce | 2011-07-14 17:45:50 +0000 | [diff] [blame] | 737 | = CGF.CGM.getIntrinsic(llvm::Intrinsic::umul_with_overflow, CGF.SizeTy); |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 738 | |
| 739 | llvm::Value *tsmV = |
| 740 | llvm::ConstantInt::get(CGF.SizeTy, typeSizeMultiplier); |
| 741 | llvm::Value *result = |
| 742 | CGF.Builder.CreateCall2(umul_with_overflow, size, tsmV); |
| 743 | |
| 744 | llvm::Value *overflowed = CGF.Builder.CreateExtractValue(result, 1); |
| 745 | if (hasOverflow) |
| 746 | hasOverflow = CGF.Builder.CreateOr(hasOverflow, overflowed); |
| 747 | else |
| 748 | hasOverflow = overflowed; |
| 749 | |
| 750 | size = CGF.Builder.CreateExtractValue(result, 0); |
| 751 | |
| 752 | // Also scale up numElements by the array size multiplier. |
| 753 | if (arraySizeMultiplier != 1) { |
| 754 | // If the base element type size is 1, then we can re-use the |
| 755 | // multiply we just did. |
| 756 | if (typeSize.isOne()) { |
| 757 | assert(arraySizeMultiplier == typeSizeMultiplier); |
| 758 | numElements = size; |
| 759 | |
| 760 | // Otherwise we need a separate multiply. |
| 761 | } else { |
| 762 | llvm::Value *asmV = |
| 763 | llvm::ConstantInt::get(CGF.SizeTy, arraySizeMultiplier); |
| 764 | numElements = CGF.Builder.CreateMul(numElements, asmV); |
| 765 | } |
| 766 | } |
| 767 | } else { |
| 768 | // numElements doesn't need to be scaled. |
| 769 | assert(arraySizeMultiplier == 1); |
Chris Lattner | 32ac583 | 2010-07-20 21:55:52 +0000 | [diff] [blame] | 770 | } |
| 771 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 772 | // Add in the cookie size if necessary. |
| 773 | if (cookieSize != 0) { |
| 774 | sizeWithoutCookie = size; |
| 775 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 776 | llvm::Value *uadd_with_overflow |
Benjamin Kramer | 8d375ce | 2011-07-14 17:45:50 +0000 | [diff] [blame] | 777 | = CGF.CGM.getIntrinsic(llvm::Intrinsic::uadd_with_overflow, CGF.SizeTy); |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 778 | |
| 779 | llvm::Value *cookieSizeV = llvm::ConstantInt::get(CGF.SizeTy, cookieSize); |
| 780 | llvm::Value *result = |
| 781 | CGF.Builder.CreateCall2(uadd_with_overflow, size, cookieSizeV); |
| 782 | |
| 783 | llvm::Value *overflowed = CGF.Builder.CreateExtractValue(result, 1); |
| 784 | if (hasOverflow) |
| 785 | hasOverflow = CGF.Builder.CreateOr(hasOverflow, overflowed); |
| 786 | else |
| 787 | hasOverflow = overflowed; |
| 788 | |
| 789 | size = CGF.Builder.CreateExtractValue(result, 0); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 790 | } |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 791 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 792 | // If we had any possibility of dynamic overflow, make a select to |
| 793 | // overwrite 'size' with an all-ones value, which should cause |
| 794 | // operator new to throw. |
| 795 | if (hasOverflow) |
| 796 | size = CGF.Builder.CreateSelect(hasOverflow, |
| 797 | llvm::Constant::getAllOnesValue(CGF.SizeTy), |
| 798 | size); |
Chris Lattner | 32ac583 | 2010-07-20 21:55:52 +0000 | [diff] [blame] | 799 | } |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 800 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 801 | if (cookieSize == 0) |
| 802 | sizeWithoutCookie = size; |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 803 | else |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 804 | assert(sizeWithoutCookie && "didn't set sizeWithoutCookie?"); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 805 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 806 | return size; |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 809 | static void StoreAnyExprIntoOneUnit(CodeGenFunction &CGF, const Expr *Init, |
| 810 | QualType AllocType, llvm::Value *NewPtr) { |
Daniel Dunbar | 0381634 | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 811 | |
Eli Friedman | 38cd36d | 2011-12-03 02:13:40 +0000 | [diff] [blame] | 812 | CharUnits Alignment = CGF.getContext().getTypeAlignInChars(AllocType); |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 813 | if (!CGF.hasAggregateLLVMType(AllocType)) |
Eli Friedman | 38cd36d | 2011-12-03 02:13:40 +0000 | [diff] [blame] | 814 | CGF.EmitScalarInit(Init, 0, CGF.MakeAddrLValue(NewPtr, AllocType, |
Eli Friedman | a0544d6 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 815 | Alignment), |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 816 | false); |
Fariborz Jahanian | d5202e0 | 2010-06-25 18:26:07 +0000 | [diff] [blame] | 817 | else if (AllocType->isAnyComplexType()) |
| 818 | CGF.EmitComplexExprIntoAddr(Init, NewPtr, |
| 819 | AllocType.isVolatileQualified()); |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 820 | else { |
| 821 | AggValueSlot Slot |
Eli Friedman | c1d85b9 | 2011-12-03 00:54:26 +0000 | [diff] [blame] | 822 | = AggValueSlot::forAddr(NewPtr, Alignment, AllocType.getQualifiers(), |
John McCall | 8d6fc95 | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 823 | AggValueSlot::IsDestructed, |
John McCall | 46759f4 | 2011-08-26 07:31:35 +0000 | [diff] [blame] | 824 | AggValueSlot::DoesNotNeedGCBarriers, |
Chad Rosier | 615ed1a | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 825 | AggValueSlot::IsNotAliased); |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 826 | CGF.EmitAggExpr(Init, Slot); |
Sebastian Redl | d026dc4 | 2012-02-19 16:03:09 +0000 | [diff] [blame] | 827 | |
| 828 | CGF.MaybeEmitStdInitializerListCleanup(NewPtr, Init); |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 829 | } |
Fariborz Jahanian | d5202e0 | 2010-06-25 18:26:07 +0000 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | void |
| 833 | CodeGenFunction::EmitNewArrayInitializer(const CXXNewExpr *E, |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 834 | QualType elementType, |
| 835 | llvm::Value *beginPtr, |
| 836 | llvm::Value *numElements) { |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 837 | if (!E->hasInitializer()) |
| 838 | return; // We have a POD type. |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 839 | |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 840 | llvm::Value *explicitPtr = beginPtr; |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 841 | // Find the end of the array, hoisted out of the loop. |
| 842 | llvm::Value *endPtr = |
| 843 | Builder.CreateInBoundsGEP(beginPtr, numElements, "array.end"); |
| 844 | |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 845 | unsigned initializerElements = 0; |
| 846 | |
| 847 | const Expr *Init = E->getInitializer(); |
Chad Rosier | f62290a | 2012-02-24 00:13:55 +0000 | [diff] [blame] | 848 | llvm::AllocaInst *endOfInit = 0; |
| 849 | QualType::DestructionKind dtorKind = elementType.isDestructedType(); |
| 850 | EHScopeStack::stable_iterator cleanup; |
| 851 | llvm::Instruction *cleanupDominator = 0; |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 852 | // If the initializer is an initializer list, first do the explicit elements. |
| 853 | if (const InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
| 854 | initializerElements = ILE->getNumInits(); |
Chad Rosier | f62290a | 2012-02-24 00:13:55 +0000 | [diff] [blame] | 855 | |
| 856 | // Enter a partial-destruction cleanup if necessary. |
| 857 | if (needsEHCleanup(dtorKind)) { |
| 858 | // In principle we could tell the cleanup where we are more |
| 859 | // directly, but the control flow can get so varied here that it |
| 860 | // would actually be quite complex. Therefore we go through an |
| 861 | // alloca. |
| 862 | endOfInit = CreateTempAlloca(beginPtr->getType(), "array.endOfInit"); |
| 863 | cleanupDominator = Builder.CreateStore(beginPtr, endOfInit); |
| 864 | pushIrregularPartialArrayCleanup(beginPtr, endOfInit, elementType, |
| 865 | getDestroyer(dtorKind)); |
| 866 | cleanup = EHStack.stable_begin(); |
| 867 | } |
| 868 | |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 869 | for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i) { |
Chad Rosier | f62290a | 2012-02-24 00:13:55 +0000 | [diff] [blame] | 870 | // Tell the cleanup that it needs to destroy up to this |
| 871 | // element. TODO: some of these stores can be trivially |
| 872 | // observed to be unnecessary. |
| 873 | if (endOfInit) Builder.CreateStore(explicitPtr, endOfInit); |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 874 | StoreAnyExprIntoOneUnit(*this, ILE->getInit(i), elementType, explicitPtr); |
| 875 | explicitPtr =Builder.CreateConstGEP1_32(explicitPtr, 1, "array.exp.next"); |
| 876 | } |
| 877 | |
| 878 | // The remaining elements are filled with the array filler expression. |
| 879 | Init = ILE->getArrayFiller(); |
| 880 | } |
| 881 | |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 882 | // Create the continuation block. |
| 883 | llvm::BasicBlock *contBB = createBasicBlock("new.loop.end"); |
| 884 | |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 885 | // If the number of elements isn't constant, we have to now check if there is |
| 886 | // anything left to initialize. |
| 887 | if (llvm::ConstantInt *constNum = dyn_cast<llvm::ConstantInt>(numElements)) { |
| 888 | // If all elements have already been initialized, skip the whole loop. |
Chad Rosier | f62290a | 2012-02-24 00:13:55 +0000 | [diff] [blame] | 889 | if (constNum->getZExtValue() <= initializerElements) { |
| 890 | // If there was a cleanup, deactivate it. |
| 891 | if (cleanupDominator) |
Dmitri Gribenko | 76bb5cabfa | 2012-09-10 21:20:09 +0000 | [diff] [blame] | 892 | DeactivateCleanupBlock(cleanup, cleanupDominator); |
Chad Rosier | f62290a | 2012-02-24 00:13:55 +0000 | [diff] [blame] | 893 | return; |
| 894 | } |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 895 | } else { |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 896 | llvm::BasicBlock *nonEmptyBB = createBasicBlock("new.loop.nonempty"); |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 897 | llvm::Value *isEmpty = Builder.CreateICmpEQ(explicitPtr, endPtr, |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 898 | "array.isempty"); |
| 899 | Builder.CreateCondBr(isEmpty, contBB, nonEmptyBB); |
| 900 | EmitBlock(nonEmptyBB); |
| 901 | } |
| 902 | |
| 903 | // Enter the loop. |
| 904 | llvm::BasicBlock *entryBB = Builder.GetInsertBlock(); |
| 905 | llvm::BasicBlock *loopBB = createBasicBlock("new.loop"); |
| 906 | |
| 907 | EmitBlock(loopBB); |
| 908 | |
| 909 | // Set up the current-element phi. |
| 910 | llvm::PHINode *curPtr = |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 911 | Builder.CreatePHI(explicitPtr->getType(), 2, "array.cur"); |
| 912 | curPtr->addIncoming(explicitPtr, entryBB); |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 913 | |
Chad Rosier | f62290a | 2012-02-24 00:13:55 +0000 | [diff] [blame] | 914 | // Store the new cleanup position for irregular cleanups. |
| 915 | if (endOfInit) Builder.CreateStore(curPtr, endOfInit); |
| 916 | |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 917 | // Enter a partial-destruction cleanup if necessary. |
Chad Rosier | f62290a | 2012-02-24 00:13:55 +0000 | [diff] [blame] | 918 | if (!cleanupDominator && needsEHCleanup(dtorKind)) { |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 919 | pushRegularPartialArrayCleanup(beginPtr, curPtr, elementType, |
| 920 | getDestroyer(dtorKind)); |
| 921 | cleanup = EHStack.stable_begin(); |
John McCall | f4beacd | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 922 | cleanupDominator = Builder.CreateUnreachable(); |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | // Emit the initializer into this element. |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 926 | StoreAnyExprIntoOneUnit(*this, Init, E->getAllocatedType(), curPtr); |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 927 | |
| 928 | // Leave the cleanup if we entered one. |
Eli Friedman | de6a86b | 2011-12-09 23:05:37 +0000 | [diff] [blame] | 929 | if (cleanupDominator) { |
John McCall | f4beacd | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 930 | DeactivateCleanupBlock(cleanup, cleanupDominator); |
| 931 | cleanupDominator->eraseFromParent(); |
| 932 | } |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 933 | |
| 934 | // Advance to the next element. |
| 935 | llvm::Value *nextPtr = Builder.CreateConstGEP1_32(curPtr, 1, "array.next"); |
| 936 | |
| 937 | // Check whether we've gotten to the end of the array and, if so, |
| 938 | // exit the loop. |
| 939 | llvm::Value *isEnd = Builder.CreateICmpEQ(nextPtr, endPtr, "array.atend"); |
| 940 | Builder.CreateCondBr(isEnd, contBB, loopBB); |
| 941 | curPtr->addIncoming(nextPtr, Builder.GetInsertBlock()); |
| 942 | |
| 943 | EmitBlock(contBB); |
Fariborz Jahanian | d5202e0 | 2010-06-25 18:26:07 +0000 | [diff] [blame] | 944 | } |
| 945 | |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 946 | static void EmitZeroMemSet(CodeGenFunction &CGF, QualType T, |
| 947 | llvm::Value *NewPtr, llvm::Value *Size) { |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 948 | CGF.EmitCastToVoidPtr(NewPtr); |
Ken Dyck | 705ba07 | 2011-01-19 01:58:38 +0000 | [diff] [blame] | 949 | CharUnits Alignment = CGF.getContext().getTypeAlignInChars(T); |
Benjamin Kramer | acc6b4e | 2010-12-30 00:13:21 +0000 | [diff] [blame] | 950 | CGF.Builder.CreateMemSet(NewPtr, CGF.Builder.getInt8(0), Size, |
Ken Dyck | 705ba07 | 2011-01-19 01:58:38 +0000 | [diff] [blame] | 951 | Alignment.getQuantity(), false); |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 952 | } |
| 953 | |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 954 | static void EmitNewInitializer(CodeGenFunction &CGF, const CXXNewExpr *E, |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 955 | QualType ElementType, |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 956 | llvm::Value *NewPtr, |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 957 | llvm::Value *NumElements, |
| 958 | llvm::Value *AllocSizeWithoutCookie) { |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 959 | const Expr *Init = E->getInitializer(); |
Anders Carlsson | 3a202f6 | 2009-11-24 18:43:52 +0000 | [diff] [blame] | 960 | if (E->isArray()) { |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 961 | if (const CXXConstructExpr *CCE = dyn_cast_or_null<CXXConstructExpr>(Init)){ |
| 962 | CXXConstructorDecl *Ctor = CCE->getConstructor(); |
Douglas Gregor | d153103 | 2012-02-23 17:07:43 +0000 | [diff] [blame] | 963 | if (Ctor->isTrivial()) { |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 964 | // If new expression did not specify value-initialization, then there |
| 965 | // is no initialization. |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 966 | if (!CCE->requiresZeroInitialization() || Ctor->getParent()->isEmpty()) |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 967 | return; |
| 968 | |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 969 | if (CGF.CGM.getTypes().isZeroInitializable(ElementType)) { |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 970 | // Optimization: since zero initialization will just set the memory |
| 971 | // to all zeroes, generate a single memset to do it in one shot. |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 972 | EmitZeroMemSet(CGF, ElementType, NewPtr, AllocSizeWithoutCookie); |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 973 | return; |
| 974 | } |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 975 | } |
John McCall | f677a8e | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 976 | |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 977 | CGF.EmitCXXAggrConstructorCall(Ctor, NumElements, NewPtr, |
| 978 | CCE->arg_begin(), CCE->arg_end(), |
Eli Friedman | 48ddcf2 | 2012-08-25 07:11:29 +0000 | [diff] [blame] | 979 | CCE->requiresZeroInitialization()); |
Anders Carlsson | d040e6b | 2010-05-03 15:09:17 +0000 | [diff] [blame] | 980 | return; |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 981 | } else if (Init && isa<ImplicitValueInitExpr>(Init) && |
Eli Friedman | de6a86b | 2011-12-09 23:05:37 +0000 | [diff] [blame] | 982 | CGF.CGM.getTypes().isZeroInitializable(ElementType)) { |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 983 | // Optimization: since zero initialization will just set the memory |
| 984 | // to all zeroes, generate a single memset to do it in one shot. |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 985 | EmitZeroMemSet(CGF, ElementType, NewPtr, AllocSizeWithoutCookie); |
| 986 | return; |
Fariborz Jahanian | d5202e0 | 2010-06-25 18:26:07 +0000 | [diff] [blame] | 987 | } |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 988 | CGF.EmitNewArrayInitializer(E, ElementType, NewPtr, NumElements); |
| 989 | return; |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 990 | } |
Anders Carlsson | 3a202f6 | 2009-11-24 18:43:52 +0000 | [diff] [blame] | 991 | |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 992 | if (!Init) |
Fariborz Jahanian | b66b08e | 2010-06-25 20:01:13 +0000 | [diff] [blame] | 993 | return; |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 994 | |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 995 | StoreAnyExprIntoOneUnit(CGF, Init, E->getAllocatedType(), NewPtr); |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 996 | } |
| 997 | |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 998 | namespace { |
| 999 | /// A cleanup to call the given 'operator delete' function upon |
| 1000 | /// abnormal exit from a new expression. |
| 1001 | class CallDeleteDuringNew : public EHScopeStack::Cleanup { |
| 1002 | size_t NumPlacementArgs; |
| 1003 | const FunctionDecl *OperatorDelete; |
| 1004 | llvm::Value *Ptr; |
| 1005 | llvm::Value *AllocSize; |
| 1006 | |
| 1007 | RValue *getPlacementArgs() { return reinterpret_cast<RValue*>(this+1); } |
| 1008 | |
| 1009 | public: |
| 1010 | static size_t getExtraSize(size_t NumPlacementArgs) { |
| 1011 | return NumPlacementArgs * sizeof(RValue); |
| 1012 | } |
| 1013 | |
| 1014 | CallDeleteDuringNew(size_t NumPlacementArgs, |
| 1015 | const FunctionDecl *OperatorDelete, |
| 1016 | llvm::Value *Ptr, |
| 1017 | llvm::Value *AllocSize) |
| 1018 | : NumPlacementArgs(NumPlacementArgs), OperatorDelete(OperatorDelete), |
| 1019 | Ptr(Ptr), AllocSize(AllocSize) {} |
| 1020 | |
| 1021 | void setPlacementArg(unsigned I, RValue Arg) { |
| 1022 | assert(I < NumPlacementArgs && "index out of range"); |
| 1023 | getPlacementArgs()[I] = Arg; |
| 1024 | } |
| 1025 | |
John McCall | 30317fd | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 1026 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1027 | const FunctionProtoType *FPT |
| 1028 | = OperatorDelete->getType()->getAs<FunctionProtoType>(); |
| 1029 | assert(FPT->getNumArgs() == NumPlacementArgs + 1 || |
John McCall | d441b1e | 2010-09-14 21:45:42 +0000 | [diff] [blame] | 1030 | (FPT->getNumArgs() == 2 && NumPlacementArgs == 0)); |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1031 | |
| 1032 | CallArgList DeleteArgs; |
| 1033 | |
| 1034 | // The first argument is always a void*. |
| 1035 | FunctionProtoType::arg_type_iterator AI = FPT->arg_type_begin(); |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1036 | DeleteArgs.add(RValue::get(Ptr), *AI++); |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1037 | |
| 1038 | // A member 'operator delete' can take an extra 'size_t' argument. |
| 1039 | if (FPT->getNumArgs() == NumPlacementArgs + 2) |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1040 | DeleteArgs.add(RValue::get(AllocSize), *AI++); |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1041 | |
| 1042 | // Pass the rest of the arguments, which must match exactly. |
| 1043 | for (unsigned I = 0; I != NumPlacementArgs; ++I) |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1044 | DeleteArgs.add(getPlacementArgs()[I], *AI++); |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1045 | |
| 1046 | // Call 'operator delete'. |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 1047 | CGF.EmitCall(CGF.CGM.getTypes().arrangeFreeFunctionCall(DeleteArgs, FPT), |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1048 | CGF.CGM.GetAddrOfFunction(OperatorDelete), |
| 1049 | ReturnValueSlot(), DeleteArgs, OperatorDelete); |
| 1050 | } |
| 1051 | }; |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1052 | |
| 1053 | /// A cleanup to call the given 'operator delete' function upon |
| 1054 | /// abnormal exit from a new expression when the new expression is |
| 1055 | /// conditional. |
| 1056 | class CallDeleteDuringConditionalNew : public EHScopeStack::Cleanup { |
| 1057 | size_t NumPlacementArgs; |
| 1058 | const FunctionDecl *OperatorDelete; |
John McCall | cb5f77f | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 1059 | DominatingValue<RValue>::saved_type Ptr; |
| 1060 | DominatingValue<RValue>::saved_type AllocSize; |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1061 | |
John McCall | cb5f77f | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 1062 | DominatingValue<RValue>::saved_type *getPlacementArgs() { |
| 1063 | return reinterpret_cast<DominatingValue<RValue>::saved_type*>(this+1); |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | public: |
| 1067 | static size_t getExtraSize(size_t NumPlacementArgs) { |
John McCall | cb5f77f | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 1068 | return NumPlacementArgs * sizeof(DominatingValue<RValue>::saved_type); |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | CallDeleteDuringConditionalNew(size_t NumPlacementArgs, |
| 1072 | const FunctionDecl *OperatorDelete, |
John McCall | cb5f77f | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 1073 | DominatingValue<RValue>::saved_type Ptr, |
| 1074 | DominatingValue<RValue>::saved_type AllocSize) |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1075 | : NumPlacementArgs(NumPlacementArgs), OperatorDelete(OperatorDelete), |
| 1076 | Ptr(Ptr), AllocSize(AllocSize) {} |
| 1077 | |
John McCall | cb5f77f | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 1078 | void setPlacementArg(unsigned I, DominatingValue<RValue>::saved_type Arg) { |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1079 | assert(I < NumPlacementArgs && "index out of range"); |
| 1080 | getPlacementArgs()[I] = Arg; |
| 1081 | } |
| 1082 | |
John McCall | 30317fd | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 1083 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1084 | const FunctionProtoType *FPT |
| 1085 | = OperatorDelete->getType()->getAs<FunctionProtoType>(); |
| 1086 | assert(FPT->getNumArgs() == NumPlacementArgs + 1 || |
| 1087 | (FPT->getNumArgs() == 2 && NumPlacementArgs == 0)); |
| 1088 | |
| 1089 | CallArgList DeleteArgs; |
| 1090 | |
| 1091 | // The first argument is always a void*. |
| 1092 | FunctionProtoType::arg_type_iterator AI = FPT->arg_type_begin(); |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1093 | DeleteArgs.add(Ptr.restore(CGF), *AI++); |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1094 | |
| 1095 | // A member 'operator delete' can take an extra 'size_t' argument. |
| 1096 | if (FPT->getNumArgs() == NumPlacementArgs + 2) { |
John McCall | cb5f77f | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 1097 | RValue RV = AllocSize.restore(CGF); |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1098 | DeleteArgs.add(RV, *AI++); |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | // Pass the rest of the arguments, which must match exactly. |
| 1102 | for (unsigned I = 0; I != NumPlacementArgs; ++I) { |
John McCall | cb5f77f | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 1103 | RValue RV = getPlacementArgs()[I].restore(CGF); |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1104 | DeleteArgs.add(RV, *AI++); |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | // Call 'operator delete'. |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 1108 | CGF.EmitCall(CGF.CGM.getTypes().arrangeFreeFunctionCall(DeleteArgs, FPT), |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1109 | CGF.CGM.GetAddrOfFunction(OperatorDelete), |
| 1110 | ReturnValueSlot(), DeleteArgs, OperatorDelete); |
| 1111 | } |
| 1112 | }; |
| 1113 | } |
| 1114 | |
| 1115 | /// Enter a cleanup to call 'operator delete' if the initializer in a |
| 1116 | /// new-expression throws. |
| 1117 | static void EnterNewDeleteCleanup(CodeGenFunction &CGF, |
| 1118 | const CXXNewExpr *E, |
| 1119 | llvm::Value *NewPtr, |
| 1120 | llvm::Value *AllocSize, |
| 1121 | const CallArgList &NewArgs) { |
| 1122 | // If we're not inside a conditional branch, then the cleanup will |
| 1123 | // dominate and we can do the easier (and more efficient) thing. |
| 1124 | if (!CGF.isInConditionalBranch()) { |
| 1125 | CallDeleteDuringNew *Cleanup = CGF.EHStack |
| 1126 | .pushCleanupWithExtra<CallDeleteDuringNew>(EHCleanup, |
| 1127 | E->getNumPlacementArgs(), |
| 1128 | E->getOperatorDelete(), |
| 1129 | NewPtr, AllocSize); |
| 1130 | for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I) |
Eli Friedman | f4258eb | 2011-05-02 18:05:27 +0000 | [diff] [blame] | 1131 | Cleanup->setPlacementArg(I, NewArgs[I+1].RV); |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1132 | |
| 1133 | return; |
| 1134 | } |
| 1135 | |
| 1136 | // Otherwise, we need to save all this stuff. |
John McCall | cb5f77f | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 1137 | DominatingValue<RValue>::saved_type SavedNewPtr = |
| 1138 | DominatingValue<RValue>::save(CGF, RValue::get(NewPtr)); |
| 1139 | DominatingValue<RValue>::saved_type SavedAllocSize = |
| 1140 | DominatingValue<RValue>::save(CGF, RValue::get(AllocSize)); |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1141 | |
| 1142 | CallDeleteDuringConditionalNew *Cleanup = CGF.EHStack |
John McCall | f4beacd | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 1143 | .pushCleanupWithExtra<CallDeleteDuringConditionalNew>(EHCleanup, |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1144 | E->getNumPlacementArgs(), |
| 1145 | E->getOperatorDelete(), |
| 1146 | SavedNewPtr, |
| 1147 | SavedAllocSize); |
| 1148 | for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I) |
John McCall | cb5f77f | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 1149 | Cleanup->setPlacementArg(I, |
Eli Friedman | f4258eb | 2011-05-02 18:05:27 +0000 | [diff] [blame] | 1150 | DominatingValue<RValue>::save(CGF, NewArgs[I+1].RV)); |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1151 | |
John McCall | f4beacd | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 1152 | CGF.initFullExprCleanup(); |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1155 | llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) { |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1156 | // The element type being allocated. |
| 1157 | QualType allocType = getContext().getBaseElementType(E->getAllocatedType()); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1158 | |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1159 | // 1. Build a call to the allocation function. |
| 1160 | FunctionDecl *allocator = E->getOperatorNew(); |
| 1161 | const FunctionProtoType *allocatorType = |
| 1162 | allocator->getType()->castAs<FunctionProtoType>(); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1163 | |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1164 | CallArgList allocatorArgs; |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1165 | |
| 1166 | // The allocation size is the first argument. |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1167 | QualType sizeType = getContext().getSizeType(); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1168 | |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 1169 | // If there is a brace-initializer, cannot allocate fewer elements than inits. |
| 1170 | unsigned minElements = 0; |
| 1171 | if (E->isArray() && E->hasInitializer()) { |
| 1172 | if (const InitListExpr *ILE = dyn_cast<InitListExpr>(E->getInitializer())) |
| 1173 | minElements = ILE->getNumInits(); |
| 1174 | } |
| 1175 | |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1176 | llvm::Value *numElements = 0; |
| 1177 | llvm::Value *allocSizeWithoutCookie = 0; |
| 1178 | llvm::Value *allocSize = |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 1179 | EmitCXXNewAllocSize(*this, E, minElements, numElements, |
| 1180 | allocSizeWithoutCookie); |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 1181 | |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1182 | allocatorArgs.add(RValue::get(allocSize), sizeType); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1183 | |
| 1184 | // Emit the rest of the arguments. |
| 1185 | // FIXME: Ideally, this should just use EmitCallArgs. |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1186 | CXXNewExpr::const_arg_iterator placementArg = E->placement_arg_begin(); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1187 | |
| 1188 | // First, use the types from the function type. |
| 1189 | // We start at 1 here because the first argument (the allocation size) |
| 1190 | // has already been emitted. |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1191 | for (unsigned i = 1, e = allocatorType->getNumArgs(); i != e; |
| 1192 | ++i, ++placementArg) { |
| 1193 | QualType argType = allocatorType->getArgType(i); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1194 | |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1195 | assert(getContext().hasSameUnqualifiedType(argType.getNonReferenceType(), |
| 1196 | placementArg->getType()) && |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1197 | "type mismatch in call argument!"); |
| 1198 | |
John McCall | 32ea969 | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 1199 | EmitCallArg(allocatorArgs, *placementArg, argType); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | // Either we've emitted all the call args, or we have a call to a |
| 1203 | // variadic function. |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1204 | assert((placementArg == E->placement_arg_end() || |
| 1205 | allocatorType->isVariadic()) && |
| 1206 | "Extra arguments to non-variadic function!"); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1207 | |
| 1208 | // 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] | 1209 | for (CXXNewExpr::const_arg_iterator placementArgsEnd = E->placement_arg_end(); |
| 1210 | placementArg != placementArgsEnd; ++placementArg) { |
John McCall | 32ea969 | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 1211 | EmitCallArg(allocatorArgs, *placementArg, placementArg->getType()); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
John McCall | 7ec4b43 | 2011-05-16 01:05:12 +0000 | [diff] [blame] | 1214 | // Emit the allocation call. If the allocator is a global placement |
| 1215 | // operator, just "inline" it directly. |
| 1216 | RValue RV; |
| 1217 | if (allocator->isReservedGlobalPlacementOperator()) { |
| 1218 | assert(allocatorArgs.size() == 2); |
| 1219 | RV = allocatorArgs[1].RV; |
| 1220 | // TODO: kill any unnecessary computations done for the size |
| 1221 | // argument. |
| 1222 | } else { |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 1223 | RV = EmitCall(CGM.getTypes().arrangeFreeFunctionCall(allocatorArgs, |
| 1224 | allocatorType), |
John McCall | 7ec4b43 | 2011-05-16 01:05:12 +0000 | [diff] [blame] | 1225 | CGM.GetAddrOfFunction(allocator), ReturnValueSlot(), |
| 1226 | allocatorArgs, allocator); |
| 1227 | } |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1228 | |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1229 | // Emit a null check on the allocation result if the allocation |
| 1230 | // function is allowed to return null (because it has a non-throwing |
| 1231 | // exception spec; for this part, we inline |
| 1232 | // CXXNewExpr::shouldNullCheckAllocation()) and we have an |
| 1233 | // interesting initializer. |
Sebastian Redl | 31ad754 | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 1234 | bool nullCheck = allocatorType->isNothrow(getContext()) && |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1235 | (!allocType.isPODType(getContext()) || E->hasInitializer()); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1236 | |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1237 | llvm::BasicBlock *nullCheckBB = 0; |
| 1238 | llvm::BasicBlock *contBB = 0; |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1239 | |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1240 | llvm::Value *allocation = RV.getScalarVal(); |
| 1241 | unsigned AS = |
| 1242 | cast<llvm::PointerType>(allocation->getType())->getAddressSpace(); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1243 | |
John McCall | f7dcf32 | 2011-03-07 01:52:56 +0000 | [diff] [blame] | 1244 | // The null-check means that the initializer is conditionally |
| 1245 | // evaluated. |
| 1246 | ConditionalEvaluation conditional(*this); |
| 1247 | |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1248 | if (nullCheck) { |
John McCall | f7dcf32 | 2011-03-07 01:52:56 +0000 | [diff] [blame] | 1249 | conditional.begin(*this); |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1250 | |
| 1251 | nullCheckBB = Builder.GetInsertBlock(); |
| 1252 | llvm::BasicBlock *notNullBB = createBasicBlock("new.notnull"); |
| 1253 | contBB = createBasicBlock("new.cont"); |
| 1254 | |
| 1255 | llvm::Value *isNull = Builder.CreateIsNull(allocation, "new.isnull"); |
| 1256 | Builder.CreateCondBr(isNull, contBB, notNullBB); |
| 1257 | EmitBlock(notNullBB); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1258 | } |
Anders Carlsson | f771681 | 2009-09-23 18:59:48 +0000 | [diff] [blame] | 1259 | |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1260 | // If there's an operator delete, enter a cleanup to call it if an |
| 1261 | // exception is thrown. |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1262 | EHScopeStack::stable_iterator operatorDeleteCleanup; |
John McCall | f4beacd | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 1263 | llvm::Instruction *cleanupDominator = 0; |
John McCall | 7ec4b43 | 2011-05-16 01:05:12 +0000 | [diff] [blame] | 1264 | if (E->getOperatorDelete() && |
| 1265 | !E->getOperatorDelete()->isReservedGlobalPlacementOperator()) { |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1266 | EnterNewDeleteCleanup(*this, E, allocation, allocSize, allocatorArgs); |
| 1267 | operatorDeleteCleanup = EHStack.stable_begin(); |
John McCall | f4beacd | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 1268 | cleanupDominator = Builder.CreateUnreachable(); |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
Eli Friedman | cf9b1f6 | 2011-09-06 18:53:03 +0000 | [diff] [blame] | 1271 | assert((allocSize == allocSizeWithoutCookie) == |
| 1272 | CalculateCookiePadding(*this, E).isZero()); |
| 1273 | if (allocSize != allocSizeWithoutCookie) { |
| 1274 | assert(E->isArray()); |
| 1275 | allocation = CGM.getCXXABI().InitializeArrayCookie(*this, allocation, |
| 1276 | numElements, |
| 1277 | E, allocType); |
| 1278 | } |
| 1279 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1280 | llvm::Type *elementPtrTy |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1281 | = ConvertTypeForMem(allocType)->getPointerTo(AS); |
| 1282 | llvm::Value *result = Builder.CreateBitCast(allocation, elementPtrTy); |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1283 | |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 1284 | EmitNewInitializer(*this, E, allocType, result, numElements, |
| 1285 | allocSizeWithoutCookie); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1286 | if (E->isArray()) { |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1287 | // NewPtr is a pointer to the base element type. If we're |
| 1288 | // allocating an array of arrays, we'll need to cast back to the |
| 1289 | // array pointer type. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1290 | llvm::Type *resultType = ConvertTypeForMem(E->getType()); |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1291 | if (result->getType() != resultType) |
| 1292 | result = Builder.CreateBitCast(result, resultType); |
Fariborz Jahanian | 47b4629 | 2010-03-24 16:57:01 +0000 | [diff] [blame] | 1293 | } |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1294 | |
| 1295 | // Deactivate the 'operator delete' cleanup if we finished |
| 1296 | // initialization. |
John McCall | f4beacd | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 1297 | if (operatorDeleteCleanup.isValid()) { |
| 1298 | DeactivateCleanupBlock(operatorDeleteCleanup, cleanupDominator); |
| 1299 | cleanupDominator->eraseFromParent(); |
| 1300 | } |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1301 | |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1302 | if (nullCheck) { |
John McCall | f7dcf32 | 2011-03-07 01:52:56 +0000 | [diff] [blame] | 1303 | conditional.end(*this); |
| 1304 | |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1305 | llvm::BasicBlock *notNullBB = Builder.GetInsertBlock(); |
| 1306 | EmitBlock(contBB); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1307 | |
Jay Foad | 20c0f02 | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 1308 | llvm::PHINode *PHI = Builder.CreatePHI(result->getType(), 2); |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1309 | PHI->addIncoming(result, notNullBB); |
| 1310 | PHI->addIncoming(llvm::Constant::getNullValue(result->getType()), |
| 1311 | nullCheckBB); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1312 | |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1313 | result = PHI; |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1314 | } |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1315 | |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1316 | return result; |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
Eli Friedman | fe81e3f | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1319 | void CodeGenFunction::EmitDeleteCall(const FunctionDecl *DeleteFD, |
| 1320 | llvm::Value *Ptr, |
| 1321 | QualType DeleteTy) { |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1322 | assert(DeleteFD->getOverloadedOperator() == OO_Delete); |
| 1323 | |
Eli Friedman | fe81e3f | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1324 | const FunctionProtoType *DeleteFTy = |
| 1325 | DeleteFD->getType()->getAs<FunctionProtoType>(); |
| 1326 | |
| 1327 | CallArgList DeleteArgs; |
| 1328 | |
Anders Carlsson | 21122cf | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 1329 | // Check if we need to pass the size to the delete operator. |
| 1330 | llvm::Value *Size = 0; |
| 1331 | QualType SizeTy; |
| 1332 | if (DeleteFTy->getNumArgs() == 2) { |
| 1333 | SizeTy = DeleteFTy->getArgType(1); |
Ken Dyck | 7df3cbe | 2010-01-26 19:59:28 +0000 | [diff] [blame] | 1334 | CharUnits DeleteTypeSize = getContext().getTypeSizeInChars(DeleteTy); |
| 1335 | Size = llvm::ConstantInt::get(ConvertType(SizeTy), |
| 1336 | DeleteTypeSize.getQuantity()); |
Anders Carlsson | 21122cf | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 1337 | } |
| 1338 | |
Eli Friedman | fe81e3f | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1339 | QualType ArgTy = DeleteFTy->getArgType(0); |
| 1340 | llvm::Value *DeletePtr = Builder.CreateBitCast(Ptr, ConvertType(ArgTy)); |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1341 | DeleteArgs.add(RValue::get(DeletePtr), ArgTy); |
Eli Friedman | fe81e3f | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1342 | |
Anders Carlsson | 21122cf | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 1343 | if (Size) |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1344 | DeleteArgs.add(RValue::get(Size), SizeTy); |
Eli Friedman | fe81e3f | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1345 | |
| 1346 | // Emit the call to delete. |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 1347 | EmitCall(CGM.getTypes().arrangeFreeFunctionCall(DeleteArgs, DeleteFTy), |
Anders Carlsson | 61a401c | 2009-12-24 19:25:24 +0000 | [diff] [blame] | 1348 | CGM.GetAddrOfFunction(DeleteFD), ReturnValueSlot(), |
Eli Friedman | fe81e3f | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1349 | DeleteArgs, DeleteFD); |
| 1350 | } |
| 1351 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1352 | namespace { |
| 1353 | /// Calls the given 'operator delete' on a single object. |
| 1354 | struct CallObjectDelete : EHScopeStack::Cleanup { |
| 1355 | llvm::Value *Ptr; |
| 1356 | const FunctionDecl *OperatorDelete; |
| 1357 | QualType ElementType; |
| 1358 | |
| 1359 | CallObjectDelete(llvm::Value *Ptr, |
| 1360 | const FunctionDecl *OperatorDelete, |
| 1361 | QualType ElementType) |
| 1362 | : Ptr(Ptr), OperatorDelete(OperatorDelete), ElementType(ElementType) {} |
| 1363 | |
John McCall | 30317fd | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 1364 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1365 | CGF.EmitDeleteCall(OperatorDelete, Ptr, ElementType); |
| 1366 | } |
| 1367 | }; |
| 1368 | } |
| 1369 | |
| 1370 | /// Emit the code for deleting a single object. |
| 1371 | static void EmitObjectDelete(CodeGenFunction &CGF, |
| 1372 | const FunctionDecl *OperatorDelete, |
| 1373 | llvm::Value *Ptr, |
Douglas Gregor | 1c2e20d | 2011-07-13 00:54:47 +0000 | [diff] [blame] | 1374 | QualType ElementType, |
| 1375 | bool UseGlobalDelete) { |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1376 | // Find the destructor for the type, if applicable. If the |
| 1377 | // destructor is virtual, we'll just emit the vcall and return. |
| 1378 | const CXXDestructorDecl *Dtor = 0; |
| 1379 | if (const RecordType *RT = ElementType->getAs<RecordType>()) { |
| 1380 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Eli Friedman | b23533d | 2011-08-02 18:05:30 +0000 | [diff] [blame] | 1381 | if (RD->hasDefinition() && !RD->hasTrivialDestructor()) { |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1382 | Dtor = RD->getDestructor(); |
| 1383 | |
| 1384 | if (Dtor->isVirtual()) { |
Douglas Gregor | 1c2e20d | 2011-07-13 00:54:47 +0000 | [diff] [blame] | 1385 | if (UseGlobalDelete) { |
| 1386 | // If we're supposed to call the global delete, make sure we do so |
| 1387 | // even if the destructor throws. |
John McCall | 82fb892 | 2012-09-25 10:10:39 +0000 | [diff] [blame] | 1388 | |
| 1389 | // Derive the complete-object pointer, which is what we need |
| 1390 | // to pass to the deallocation function. |
| 1391 | llvm::Value *completePtr = |
| 1392 | CGF.CGM.getCXXABI().adjustToCompleteObject(CGF, Ptr, ElementType); |
| 1393 | |
Douglas Gregor | 1c2e20d | 2011-07-13 00:54:47 +0000 | [diff] [blame] | 1394 | CGF.EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup, |
John McCall | 82fb892 | 2012-09-25 10:10:39 +0000 | [diff] [blame] | 1395 | completePtr, OperatorDelete, |
Douglas Gregor | 1c2e20d | 2011-07-13 00:54:47 +0000 | [diff] [blame] | 1396 | ElementType); |
| 1397 | } |
| 1398 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1399 | llvm::Type *Ty = |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1400 | CGF.getTypes().GetFunctionType( |
| 1401 | CGF.getTypes().arrangeCXXDestructor(Dtor, Dtor_Complete)); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1402 | |
| 1403 | llvm::Value *Callee |
Douglas Gregor | 1c2e20d | 2011-07-13 00:54:47 +0000 | [diff] [blame] | 1404 | = CGF.BuildVirtualCall(Dtor, |
| 1405 | UseGlobalDelete? Dtor_Complete : Dtor_Deleting, |
| 1406 | Ptr, Ty); |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 1407 | // FIXME: Provide a source location here. |
| 1408 | CGF.EmitCXXMemberCall(Dtor, SourceLocation(), Callee, ReturnValueSlot(), |
| 1409 | Ptr, /*VTT=*/0, 0, 0); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1410 | |
Douglas Gregor | 1c2e20d | 2011-07-13 00:54:47 +0000 | [diff] [blame] | 1411 | if (UseGlobalDelete) { |
| 1412 | CGF.PopCleanupBlock(); |
| 1413 | } |
| 1414 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1415 | return; |
| 1416 | } |
| 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | // Make sure that we call delete even if the dtor throws. |
John McCall | e4df6c8 | 2011-01-28 08:37:24 +0000 | [diff] [blame] | 1421 | // This doesn't have to a conditional cleanup because we're going |
| 1422 | // to pop it off in a second. |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1423 | CGF.EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup, |
| 1424 | Ptr, OperatorDelete, ElementType); |
| 1425 | |
| 1426 | if (Dtor) |
| 1427 | CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, |
| 1428 | /*ForVirtualBase=*/false, Ptr); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1429 | else if (CGF.getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1430 | ElementType->isObjCLifetimeType()) { |
| 1431 | switch (ElementType.getObjCLifetime()) { |
| 1432 | case Qualifiers::OCL_None: |
| 1433 | case Qualifiers::OCL_ExplicitNone: |
| 1434 | case Qualifiers::OCL_Autoreleasing: |
| 1435 | break; |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1436 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1437 | case Qualifiers::OCL_Strong: { |
| 1438 | // Load the pointer value. |
| 1439 | llvm::Value *PtrValue = CGF.Builder.CreateLoad(Ptr, |
| 1440 | ElementType.isVolatileQualified()); |
| 1441 | |
| 1442 | CGF.EmitARCRelease(PtrValue, /*precise*/ true); |
| 1443 | break; |
| 1444 | } |
| 1445 | |
| 1446 | case Qualifiers::OCL_Weak: |
| 1447 | CGF.EmitARCDestroyWeak(Ptr); |
| 1448 | break; |
| 1449 | } |
| 1450 | } |
| 1451 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1452 | CGF.PopCleanupBlock(); |
| 1453 | } |
| 1454 | |
| 1455 | namespace { |
| 1456 | /// Calls the given 'operator delete' on an array of objects. |
| 1457 | struct CallArrayDelete : EHScopeStack::Cleanup { |
| 1458 | llvm::Value *Ptr; |
| 1459 | const FunctionDecl *OperatorDelete; |
| 1460 | llvm::Value *NumElements; |
| 1461 | QualType ElementType; |
| 1462 | CharUnits CookieSize; |
| 1463 | |
| 1464 | CallArrayDelete(llvm::Value *Ptr, |
| 1465 | const FunctionDecl *OperatorDelete, |
| 1466 | llvm::Value *NumElements, |
| 1467 | QualType ElementType, |
| 1468 | CharUnits CookieSize) |
| 1469 | : Ptr(Ptr), OperatorDelete(OperatorDelete), NumElements(NumElements), |
| 1470 | ElementType(ElementType), CookieSize(CookieSize) {} |
| 1471 | |
John McCall | 30317fd | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 1472 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1473 | const FunctionProtoType *DeleteFTy = |
| 1474 | OperatorDelete->getType()->getAs<FunctionProtoType>(); |
| 1475 | assert(DeleteFTy->getNumArgs() == 1 || DeleteFTy->getNumArgs() == 2); |
| 1476 | |
| 1477 | CallArgList Args; |
| 1478 | |
| 1479 | // Pass the pointer as the first argument. |
| 1480 | QualType VoidPtrTy = DeleteFTy->getArgType(0); |
| 1481 | llvm::Value *DeletePtr |
| 1482 | = CGF.Builder.CreateBitCast(Ptr, CGF.ConvertType(VoidPtrTy)); |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1483 | Args.add(RValue::get(DeletePtr), VoidPtrTy); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1484 | |
| 1485 | // Pass the original requested size as the second argument. |
| 1486 | if (DeleteFTy->getNumArgs() == 2) { |
| 1487 | QualType size_t = DeleteFTy->getArgType(1); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1488 | llvm::IntegerType *SizeTy |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1489 | = cast<llvm::IntegerType>(CGF.ConvertType(size_t)); |
| 1490 | |
| 1491 | CharUnits ElementTypeSize = |
| 1492 | CGF.CGM.getContext().getTypeSizeInChars(ElementType); |
| 1493 | |
| 1494 | // The size of an element, multiplied by the number of elements. |
| 1495 | llvm::Value *Size |
| 1496 | = llvm::ConstantInt::get(SizeTy, ElementTypeSize.getQuantity()); |
| 1497 | Size = CGF.Builder.CreateMul(Size, NumElements); |
| 1498 | |
| 1499 | // Plus the size of the cookie if applicable. |
| 1500 | if (!CookieSize.isZero()) { |
| 1501 | llvm::Value *CookieSizeV |
| 1502 | = llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity()); |
| 1503 | Size = CGF.Builder.CreateAdd(Size, CookieSizeV); |
| 1504 | } |
| 1505 | |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1506 | Args.add(RValue::get(Size), size_t); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | // Emit the call to delete. |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 1510 | CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(Args, DeleteFTy), |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1511 | CGF.CGM.GetAddrOfFunction(OperatorDelete), |
| 1512 | ReturnValueSlot(), Args, OperatorDelete); |
| 1513 | } |
| 1514 | }; |
| 1515 | } |
| 1516 | |
| 1517 | /// Emit the code for deleting an array of objects. |
| 1518 | static void EmitArrayDelete(CodeGenFunction &CGF, |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1519 | const CXXDeleteExpr *E, |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1520 | llvm::Value *deletedPtr, |
| 1521 | QualType elementType) { |
| 1522 | llvm::Value *numElements = 0; |
| 1523 | llvm::Value *allocatedPtr = 0; |
| 1524 | CharUnits cookieSize; |
| 1525 | CGF.CGM.getCXXABI().ReadArrayCookie(CGF, deletedPtr, E, elementType, |
| 1526 | numElements, allocatedPtr, cookieSize); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1527 | |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1528 | assert(allocatedPtr && "ReadArrayCookie didn't set allocated pointer"); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1529 | |
| 1530 | // Make sure that we call delete even if one of the dtors throws. |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1531 | const FunctionDecl *operatorDelete = E->getOperatorDelete(); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1532 | CGF.EHStack.pushCleanup<CallArrayDelete>(NormalAndEHCleanup, |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1533 | allocatedPtr, operatorDelete, |
| 1534 | numElements, elementType, |
| 1535 | cookieSize); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1536 | |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1537 | // Destroy the elements. |
| 1538 | if (QualType::DestructionKind dtorKind = elementType.isDestructedType()) { |
| 1539 | assert(numElements && "no element count for a type with a destructor!"); |
| 1540 | |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1541 | llvm::Value *arrayEnd = |
| 1542 | CGF.Builder.CreateInBoundsGEP(deletedPtr, numElements, "delete.end"); |
John McCall | 97eab0a | 2011-07-13 08:09:46 +0000 | [diff] [blame] | 1543 | |
| 1544 | // Note that it is legal to allocate a zero-length array, and we |
| 1545 | // can never fold the check away because the length should always |
| 1546 | // come from a cookie. |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1547 | CGF.emitArrayDestroy(deletedPtr, arrayEnd, elementType, |
| 1548 | CGF.getDestroyer(dtorKind), |
John McCall | 97eab0a | 2011-07-13 08:09:46 +0000 | [diff] [blame] | 1549 | /*checkZeroLength*/ true, |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1550 | CGF.needsEHCleanup(dtorKind)); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1553 | // Pop the cleanup block. |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1554 | CGF.PopCleanupBlock(); |
| 1555 | } |
| 1556 | |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1557 | void CodeGenFunction::EmitCXXDeleteExpr(const CXXDeleteExpr *E) { |
Douglas Gregor | bb3e12f | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1558 | const Expr *Arg = E->getArgument(); |
Douglas Gregor | bb3e12f | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1559 | llvm::Value *Ptr = EmitScalarExpr(Arg); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1560 | |
| 1561 | // Null check the pointer. |
| 1562 | llvm::BasicBlock *DeleteNotNull = createBasicBlock("delete.notnull"); |
| 1563 | llvm::BasicBlock *DeleteEnd = createBasicBlock("delete.end"); |
| 1564 | |
Anders Carlsson | 98981b1 | 2011-04-11 00:30:07 +0000 | [diff] [blame] | 1565 | llvm::Value *IsNull = Builder.CreateIsNull(Ptr, "isnull"); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1566 | |
| 1567 | Builder.CreateCondBr(IsNull, DeleteEnd, DeleteNotNull); |
| 1568 | EmitBlock(DeleteNotNull); |
Anders Carlsson | e828c36 | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 1569 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1570 | // We might be deleting a pointer to array. If so, GEP down to the |
| 1571 | // first non-array element. |
| 1572 | // (this assumes that A(*)[3][7] is converted to [3 x [7 x %A]]*) |
| 1573 | QualType DeleteTy = Arg->getType()->getAs<PointerType>()->getPointeeType(); |
| 1574 | if (DeleteTy->isConstantArrayType()) { |
| 1575 | llvm::Value *Zero = Builder.getInt32(0); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1576 | SmallVector<llvm::Value*,8> GEP; |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1577 | |
| 1578 | GEP.push_back(Zero); // point at the outermost array |
| 1579 | |
| 1580 | // For each layer of array type we're pointing at: |
| 1581 | while (const ConstantArrayType *Arr |
| 1582 | = getContext().getAsConstantArrayType(DeleteTy)) { |
| 1583 | // 1. Unpeel the array type. |
| 1584 | DeleteTy = Arr->getElementType(); |
| 1585 | |
| 1586 | // 2. GEP to the first element of the array. |
| 1587 | GEP.push_back(Zero); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1588 | } |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1589 | |
Jay Foad | 040dd82 | 2011-07-22 08:16:57 +0000 | [diff] [blame] | 1590 | Ptr = Builder.CreateInBoundsGEP(Ptr, GEP, "del.first"); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1591 | } |
| 1592 | |
Douglas Gregor | 04f3621 | 2010-09-02 17:38:50 +0000 | [diff] [blame] | 1593 | assert(ConvertTypeForMem(DeleteTy) == |
| 1594 | cast<llvm::PointerType>(Ptr->getType())->getElementType()); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1595 | |
| 1596 | if (E->isArrayForm()) { |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1597 | EmitArrayDelete(*this, E, Ptr, DeleteTy); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1598 | } else { |
Douglas Gregor | 1c2e20d | 2011-07-13 00:54:47 +0000 | [diff] [blame] | 1599 | EmitObjectDelete(*this, E->getOperatorDelete(), Ptr, DeleteTy, |
| 1600 | E->isGlobalDelete()); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1601 | } |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1602 | |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1603 | EmitBlock(DeleteEnd); |
| 1604 | } |
Mike Stump | c9b231c | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 1605 | |
Anders Carlsson | 0c63350 | 2011-04-11 14:13:40 +0000 | [diff] [blame] | 1606 | static llvm::Constant *getBadTypeidFn(CodeGenFunction &CGF) { |
| 1607 | // void __cxa_bad_typeid(); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1608 | llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false); |
Anders Carlsson | 0c63350 | 2011-04-11 14:13:40 +0000 | [diff] [blame] | 1609 | |
| 1610 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_typeid"); |
| 1611 | } |
| 1612 | |
| 1613 | static void EmitBadTypeidCall(CodeGenFunction &CGF) { |
Anders Carlsson | bbe277c | 2011-04-13 02:35:36 +0000 | [diff] [blame] | 1614 | llvm::Value *Fn = getBadTypeidFn(CGF); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 1615 | CGF.EmitCallOrInvoke(Fn).setDoesNotReturn(); |
Anders Carlsson | 0c63350 | 2011-04-11 14:13:40 +0000 | [diff] [blame] | 1616 | CGF.Builder.CreateUnreachable(); |
| 1617 | } |
| 1618 | |
Anders Carlsson | 940f02d | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 1619 | static llvm::Value *EmitTypeidFromVTable(CodeGenFunction &CGF, |
| 1620 | const Expr *E, |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1621 | llvm::Type *StdTypeInfoPtrTy) { |
Anders Carlsson | 940f02d | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 1622 | // Get the vtable pointer. |
| 1623 | llvm::Value *ThisPtr = CGF.EmitLValue(E).getAddress(); |
| 1624 | |
| 1625 | // C++ [expr.typeid]p2: |
| 1626 | // If the glvalue expression is obtained by applying the unary * operator to |
| 1627 | // a pointer and the pointer is a null pointer value, the typeid expression |
| 1628 | // throws the std::bad_typeid exception. |
| 1629 | if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E->IgnoreParens())) { |
| 1630 | if (UO->getOpcode() == UO_Deref) { |
| 1631 | llvm::BasicBlock *BadTypeidBlock = |
| 1632 | CGF.createBasicBlock("typeid.bad_typeid"); |
| 1633 | llvm::BasicBlock *EndBlock = |
| 1634 | CGF.createBasicBlock("typeid.end"); |
| 1635 | |
| 1636 | llvm::Value *IsNull = CGF.Builder.CreateIsNull(ThisPtr); |
| 1637 | CGF.Builder.CreateCondBr(IsNull, BadTypeidBlock, EndBlock); |
| 1638 | |
| 1639 | CGF.EmitBlock(BadTypeidBlock); |
| 1640 | EmitBadTypeidCall(CGF); |
| 1641 | CGF.EmitBlock(EndBlock); |
| 1642 | } |
| 1643 | } |
| 1644 | |
| 1645 | llvm::Value *Value = CGF.GetVTablePtr(ThisPtr, |
| 1646 | StdTypeInfoPtrTy->getPointerTo()); |
| 1647 | |
| 1648 | // Load the type info. |
| 1649 | Value = CGF.Builder.CreateConstInBoundsGEP1_64(Value, -1ULL); |
| 1650 | return CGF.Builder.CreateLoad(Value); |
| 1651 | } |
| 1652 | |
John McCall | e4df6c8 | 2011-01-28 08:37:24 +0000 | [diff] [blame] | 1653 | llvm::Value *CodeGenFunction::EmitCXXTypeidExpr(const CXXTypeidExpr *E) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1654 | llvm::Type *StdTypeInfoPtrTy = |
Anders Carlsson | 940f02d | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 1655 | ConvertType(E->getType())->getPointerTo(); |
Anders Carlsson | fd7dfeb | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 1656 | |
Anders Carlsson | 3f4336c | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 1657 | if (E->isTypeOperand()) { |
| 1658 | llvm::Constant *TypeInfo = |
| 1659 | CGM.GetAddrOfRTTIDescriptor(E->getTypeOperand()); |
Anders Carlsson | 940f02d | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 1660 | return Builder.CreateBitCast(TypeInfo, StdTypeInfoPtrTy); |
Anders Carlsson | 3f4336c | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 1661 | } |
Anders Carlsson | 0c63350 | 2011-04-11 14:13:40 +0000 | [diff] [blame] | 1662 | |
Anders Carlsson | 940f02d | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 1663 | // C++ [expr.typeid]p2: |
| 1664 | // When typeid is applied to a glvalue expression whose type is a |
| 1665 | // polymorphic class type, the result refers to a std::type_info object |
| 1666 | // representing the type of the most derived object (that is, the dynamic |
| 1667 | // type) to which the glvalue refers. |
Richard Smith | ef8bf43 | 2012-08-13 20:08:14 +0000 | [diff] [blame] | 1668 | if (E->isPotentiallyEvaluated()) |
| 1669 | return EmitTypeidFromVTable(*this, E->getExprOperand(), |
| 1670 | StdTypeInfoPtrTy); |
Anders Carlsson | 940f02d | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 1671 | |
| 1672 | QualType OperandTy = E->getExprOperand()->getType(); |
| 1673 | return Builder.CreateBitCast(CGM.GetAddrOfRTTIDescriptor(OperandTy), |
| 1674 | StdTypeInfoPtrTy); |
Mike Stump | c9b231c | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 1675 | } |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1676 | |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1677 | static llvm::Constant *getDynamicCastFn(CodeGenFunction &CGF) { |
| 1678 | // void *__dynamic_cast(const void *sub, |
| 1679 | // const abi::__class_type_info *src, |
| 1680 | // const abi::__class_type_info *dst, |
| 1681 | // std::ptrdiff_t src2dst_offset); |
| 1682 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1683 | llvm::Type *Int8PtrTy = CGF.Int8PtrTy; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1684 | llvm::Type *PtrDiffTy = |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1685 | CGF.ConvertType(CGF.getContext().getPointerDiffType()); |
| 1686 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1687 | llvm::Type *Args[4] = { Int8PtrTy, Int8PtrTy, Int8PtrTy, PtrDiffTy }; |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1688 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1689 | llvm::FunctionType *FTy = |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1690 | llvm::FunctionType::get(Int8PtrTy, Args, false); |
| 1691 | |
| 1692 | return CGF.CGM.CreateRuntimeFunction(FTy, "__dynamic_cast"); |
| 1693 | } |
| 1694 | |
| 1695 | static llvm::Constant *getBadCastFn(CodeGenFunction &CGF) { |
| 1696 | // void __cxa_bad_cast(); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1697 | llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false); |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1698 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_cast"); |
| 1699 | } |
| 1700 | |
Anders Carlsson | c1c9971 | 2011-04-11 01:45:29 +0000 | [diff] [blame] | 1701 | static void EmitBadCastCall(CodeGenFunction &CGF) { |
Anders Carlsson | bbe277c | 2011-04-13 02:35:36 +0000 | [diff] [blame] | 1702 | llvm::Value *Fn = getBadCastFn(CGF); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 1703 | CGF.EmitCallOrInvoke(Fn).setDoesNotReturn(); |
Anders Carlsson | c1c9971 | 2011-04-11 01:45:29 +0000 | [diff] [blame] | 1704 | CGF.Builder.CreateUnreachable(); |
| 1705 | } |
| 1706 | |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1707 | static llvm::Value * |
| 1708 | EmitDynamicCastCall(CodeGenFunction &CGF, llvm::Value *Value, |
| 1709 | QualType SrcTy, QualType DestTy, |
| 1710 | llvm::BasicBlock *CastEnd) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1711 | llvm::Type *PtrDiffLTy = |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1712 | CGF.ConvertType(CGF.getContext().getPointerDiffType()); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1713 | llvm::Type *DestLTy = CGF.ConvertType(DestTy); |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1714 | |
| 1715 | if (const PointerType *PTy = DestTy->getAs<PointerType>()) { |
| 1716 | if (PTy->getPointeeType()->isVoidType()) { |
| 1717 | // C++ [expr.dynamic.cast]p7: |
| 1718 | // If T is "pointer to cv void," then the result is a pointer to the |
| 1719 | // most derived object pointed to by v. |
| 1720 | |
| 1721 | // Get the vtable pointer. |
| 1722 | llvm::Value *VTable = CGF.GetVTablePtr(Value, PtrDiffLTy->getPointerTo()); |
| 1723 | |
| 1724 | // Get the offset-to-top from the vtable. |
| 1725 | llvm::Value *OffsetToTop = |
| 1726 | CGF.Builder.CreateConstInBoundsGEP1_64(VTable, -2ULL); |
| 1727 | OffsetToTop = CGF.Builder.CreateLoad(OffsetToTop, "offset.to.top"); |
| 1728 | |
| 1729 | // Finally, add the offset to the pointer. |
| 1730 | Value = CGF.EmitCastToVoidPtr(Value); |
| 1731 | Value = CGF.Builder.CreateInBoundsGEP(Value, OffsetToTop); |
| 1732 | |
| 1733 | return CGF.Builder.CreateBitCast(Value, DestLTy); |
| 1734 | } |
| 1735 | } |
| 1736 | |
| 1737 | QualType SrcRecordTy; |
| 1738 | QualType DestRecordTy; |
| 1739 | |
| 1740 | if (const PointerType *DestPTy = DestTy->getAs<PointerType>()) { |
| 1741 | SrcRecordTy = SrcTy->castAs<PointerType>()->getPointeeType(); |
| 1742 | DestRecordTy = DestPTy->getPointeeType(); |
| 1743 | } else { |
| 1744 | SrcRecordTy = SrcTy; |
| 1745 | DestRecordTy = DestTy->castAs<ReferenceType>()->getPointeeType(); |
| 1746 | } |
| 1747 | |
| 1748 | assert(SrcRecordTy->isRecordType() && "source type must be a record type!"); |
| 1749 | assert(DestRecordTy->isRecordType() && "dest type must be a record type!"); |
| 1750 | |
| 1751 | llvm::Value *SrcRTTI = |
| 1752 | CGF.CGM.GetAddrOfRTTIDescriptor(SrcRecordTy.getUnqualifiedType()); |
| 1753 | llvm::Value *DestRTTI = |
| 1754 | CGF.CGM.GetAddrOfRTTIDescriptor(DestRecordTy.getUnqualifiedType()); |
| 1755 | |
| 1756 | // FIXME: Actually compute a hint here. |
| 1757 | llvm::Value *OffsetHint = llvm::ConstantInt::get(PtrDiffLTy, -1ULL); |
| 1758 | |
| 1759 | // Emit the call to __dynamic_cast. |
| 1760 | Value = CGF.EmitCastToVoidPtr(Value); |
| 1761 | Value = CGF.Builder.CreateCall4(getDynamicCastFn(CGF), Value, |
| 1762 | SrcRTTI, DestRTTI, OffsetHint); |
| 1763 | Value = CGF.Builder.CreateBitCast(Value, DestLTy); |
| 1764 | |
| 1765 | /// C++ [expr.dynamic.cast]p9: |
| 1766 | /// A failed cast to reference type throws std::bad_cast |
| 1767 | if (DestTy->isReferenceType()) { |
| 1768 | llvm::BasicBlock *BadCastBlock = |
| 1769 | CGF.createBasicBlock("dynamic_cast.bad_cast"); |
| 1770 | |
| 1771 | llvm::Value *IsNull = CGF.Builder.CreateIsNull(Value); |
| 1772 | CGF.Builder.CreateCondBr(IsNull, BadCastBlock, CastEnd); |
| 1773 | |
| 1774 | CGF.EmitBlock(BadCastBlock); |
Anders Carlsson | c1c9971 | 2011-04-11 01:45:29 +0000 | [diff] [blame] | 1775 | EmitBadCastCall(CGF); |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1776 | } |
| 1777 | |
| 1778 | return Value; |
| 1779 | } |
| 1780 | |
Anders Carlsson | c1c9971 | 2011-04-11 01:45:29 +0000 | [diff] [blame] | 1781 | static llvm::Value *EmitDynamicCastToNull(CodeGenFunction &CGF, |
| 1782 | QualType DestTy) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1783 | llvm::Type *DestLTy = CGF.ConvertType(DestTy); |
Anders Carlsson | c1c9971 | 2011-04-11 01:45:29 +0000 | [diff] [blame] | 1784 | if (DestTy->isPointerType()) |
| 1785 | return llvm::Constant::getNullValue(DestLTy); |
| 1786 | |
| 1787 | /// C++ [expr.dynamic.cast]p9: |
| 1788 | /// A failed cast to reference type throws std::bad_cast |
| 1789 | EmitBadCastCall(CGF); |
| 1790 | |
| 1791 | CGF.EmitBlock(CGF.createBasicBlock("dynamic_cast.end")); |
| 1792 | return llvm::UndefValue::get(DestLTy); |
| 1793 | } |
| 1794 | |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1795 | llvm::Value *CodeGenFunction::EmitDynamicCast(llvm::Value *Value, |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1796 | const CXXDynamicCastExpr *DCE) { |
Anders Carlsson | 3f4336c | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 1797 | QualType DestTy = DCE->getTypeAsWritten(); |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1798 | |
Anders Carlsson | c1c9971 | 2011-04-11 01:45:29 +0000 | [diff] [blame] | 1799 | if (DCE->isAlwaysNull()) |
| 1800 | return EmitDynamicCastToNull(*this, DestTy); |
| 1801 | |
| 1802 | QualType SrcTy = DCE->getSubExpr()->getType(); |
| 1803 | |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1804 | // C++ [expr.dynamic.cast]p4: |
| 1805 | // If the value of v is a null pointer value in the pointer case, the result |
| 1806 | // is the null pointer value of type T. |
| 1807 | bool ShouldNullCheckSrcValue = SrcTy->isPointerType(); |
Anders Carlsson | 3f4336c | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 1808 | |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1809 | llvm::BasicBlock *CastNull = 0; |
| 1810 | llvm::BasicBlock *CastNotNull = 0; |
| 1811 | llvm::BasicBlock *CastEnd = createBasicBlock("dynamic_cast.end"); |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1812 | |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1813 | if (ShouldNullCheckSrcValue) { |
| 1814 | CastNull = createBasicBlock("dynamic_cast.null"); |
| 1815 | CastNotNull = createBasicBlock("dynamic_cast.notnull"); |
| 1816 | |
| 1817 | llvm::Value *IsNull = Builder.CreateIsNull(Value); |
| 1818 | Builder.CreateCondBr(IsNull, CastNull, CastNotNull); |
| 1819 | EmitBlock(CastNotNull); |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1820 | } |
| 1821 | |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1822 | Value = EmitDynamicCastCall(*this, Value, SrcTy, DestTy, CastEnd); |
| 1823 | |
| 1824 | if (ShouldNullCheckSrcValue) { |
| 1825 | EmitBranch(CastEnd); |
| 1826 | |
| 1827 | EmitBlock(CastNull); |
| 1828 | EmitBranch(CastEnd); |
| 1829 | } |
| 1830 | |
| 1831 | EmitBlock(CastEnd); |
| 1832 | |
| 1833 | if (ShouldNullCheckSrcValue) { |
| 1834 | llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2); |
| 1835 | PHI->addIncoming(Value, CastNotNull); |
| 1836 | PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), CastNull); |
| 1837 | |
| 1838 | Value = PHI; |
| 1839 | } |
| 1840 | |
| 1841 | return Value; |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1842 | } |
Eli Friedman | c370a7e | 2012-02-09 03:32:31 +0000 | [diff] [blame] | 1843 | |
Eli Friedman | c370a7e | 2012-02-09 03:32:31 +0000 | [diff] [blame] | 1844 | void CodeGenFunction::EmitLambdaExpr(const LambdaExpr *E, AggValueSlot Slot) { |
Eli Friedman | 8631f3e8 | 2012-02-09 03:47:20 +0000 | [diff] [blame] | 1845 | RunCleanupsScope Scope(*this); |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 1846 | LValue SlotLV = MakeAddrLValue(Slot.getAddr(), E->getType(), |
| 1847 | Slot.getAlignment()); |
Eli Friedman | 8631f3e8 | 2012-02-09 03:47:20 +0000 | [diff] [blame] | 1848 | |
Eli Friedman | c370a7e | 2012-02-09 03:32:31 +0000 | [diff] [blame] | 1849 | CXXRecordDecl::field_iterator CurField = E->getLambdaClass()->field_begin(); |
| 1850 | for (LambdaExpr::capture_init_iterator i = E->capture_init_begin(), |
| 1851 | e = E->capture_init_end(); |
Eric Christopher | d47e086 | 2012-02-29 03:25:18 +0000 | [diff] [blame] | 1852 | i != e; ++i, ++CurField) { |
Eli Friedman | c370a7e | 2012-02-09 03:32:31 +0000 | [diff] [blame] | 1853 | // Emit initialization |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 1854 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1855 | LValue LV = EmitLValueForFieldInitialization(SlotLV, *CurField); |
Eli Friedman | 5f1a04f | 2012-02-14 02:31:03 +0000 | [diff] [blame] | 1856 | ArrayRef<VarDecl *> ArrayIndexes; |
| 1857 | if (CurField->getType()->isArrayType()) |
| 1858 | ArrayIndexes = E->getCaptureInitIndexVars(i); |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1859 | EmitInitializerForField(*CurField, LV, *i, ArrayIndexes); |
Eli Friedman | c370a7e | 2012-02-09 03:32:31 +0000 | [diff] [blame] | 1860 | } |
Eli Friedman | c370a7e | 2012-02-09 03:32:31 +0000 | [diff] [blame] | 1861 | } |