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