Anders Carlsson | 59486a2 | 2009-11-24 05:51:11 +0000 | [diff] [blame] | 1 | //===--- CGExprCXX.cpp - Emit LLVM Code for C++ expressions ---------------===// |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code dealing with code generation of C++ expressions |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenFunction.h" |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 15 | #include "CGCUDARuntime.h" |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 16 | #include "CGCXXABI.h" |
Devang Patel | 91bbb55 | 2010-09-30 19:05:55 +0000 | [diff] [blame] | 17 | #include "CGDebugInfo.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "CGObjCRuntime.h" |
Mark Lacey | a8e7df3 | 2013-10-30 21:53:58 +0000 | [diff] [blame] | 19 | #include "clang/CodeGen/CGFunctionInfo.h" |
Saleem Abdulrasool | 10a4972 | 2016-04-08 16:52:00 +0000 | [diff] [blame] | 20 | #include "clang/Frontend/CodeGenOptions.h" |
Chandler Carruth | c80ceea | 2014-03-04 11:02:08 +0000 | [diff] [blame] | 21 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Intrinsics.h" |
Anders Carlsson | bbe277c | 2011-04-13 02:35:36 +0000 | [diff] [blame] | 23 | |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | using namespace CodeGen; |
| 26 | |
Alexey Samsonov | efa956c | 2016-03-10 00:20:33 +0000 | [diff] [blame] | 27 | static RequiredArgs |
| 28 | commonEmitCXXMemberOrOperatorCall(CodeGenFunction &CGF, const CXXMethodDecl *MD, |
| 29 | llvm::Value *This, llvm::Value *ImplicitParam, |
| 30 | QualType ImplicitParamTy, const CallExpr *CE, |
Richard Smith | 762672a | 2016-09-28 19:09:10 +0000 | [diff] [blame] | 31 | CallArgList &Args, CallArgList *RtlArgs) { |
Alexey Samsonov | a5bf76b | 2014-08-25 20:17:35 +0000 | [diff] [blame] | 32 | assert(CE == nullptr || isa<CXXMemberCallExpr>(CE) || |
| 33 | isa<CXXOperatorCallExpr>(CE)); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 34 | assert(MD->isInstance() && |
Alexey Samsonov | a5bf76b | 2014-08-25 20:17:35 +0000 | [diff] [blame] | 35 | "Trying to emit a member or operator call expr on a static method!"); |
Reid Kleckner | 034e727 | 2016-09-07 15:15:51 +0000 | [diff] [blame] | 36 | ASTContext &C = CGF.getContext(); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 37 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 38 | // Push the this ptr. |
Reid Kleckner | 034e727 | 2016-09-07 15:15:51 +0000 | [diff] [blame] | 39 | const CXXRecordDecl *RD = |
| 40 | CGF.CGM.getCXXABI().getThisArgumentTypeForMethod(MD); |
| 41 | Args.add(RValue::get(This), |
| 42 | RD ? C.getPointerType(C.getTypeDeclType(RD)) : C.VoidPtrTy); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 43 | |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 44 | // If there is an implicit parameter (e.g. VTT), emit it. |
| 45 | if (ImplicitParam) { |
| 46 | Args.add(RValue::get(ImplicitParam), ImplicitParamTy); |
Anders Carlsson | e36a6b3 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 47 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 48 | |
| 49 | const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); |
George Burgess IV | 419996c | 2016-06-16 23:06:04 +0000 | [diff] [blame] | 50 | RequiredArgs required = RequiredArgs::forPrototypePlus(FPT, Args.size(), MD); |
Alexey Samsonov | 8e1162c | 2014-09-08 17:22:45 +0000 | [diff] [blame] | 51 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 52 | // And the rest of the call args. |
Richard Smith | 762672a | 2016-09-28 19:09:10 +0000 | [diff] [blame] | 53 | if (RtlArgs) { |
| 54 | // Special case: if the caller emitted the arguments right-to-left already |
| 55 | // (prior to emitting the *this argument), we're done. This happens for |
| 56 | // assignment operators. |
| 57 | Args.addFrom(*RtlArgs); |
| 58 | } else if (CE) { |
Alexey Samsonov | a5bf76b | 2014-08-25 20:17:35 +0000 | [diff] [blame] | 59 | // Special case: skip first argument of CXXOperatorCall (it is "this"). |
Alexey Samsonov | 8e1162c | 2014-09-08 17:22:45 +0000 | [diff] [blame] | 60 | unsigned ArgsToSkip = isa<CXXOperatorCallExpr>(CE) ? 1 : 0; |
David Blaikie | f05779e | 2015-07-21 18:37:18 +0000 | [diff] [blame] | 61 | CGF.EmitCallArgs(Args, FPT, drop_begin(CE->arguments(), ArgsToSkip), |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 62 | CE->getDirectCallee()); |
Alexey Samsonov | a5bf76b | 2014-08-25 20:17:35 +0000 | [diff] [blame] | 63 | } else { |
Alexey Samsonov | 8e1162c | 2014-09-08 17:22:45 +0000 | [diff] [blame] | 64 | assert( |
| 65 | FPT->getNumParams() == 0 && |
| 66 | "No CallExpr specified for function with non-zero number of arguments"); |
Alexey Samsonov | a5bf76b | 2014-08-25 20:17:35 +0000 | [diff] [blame] | 67 | } |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 68 | return required; |
| 69 | } |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 70 | |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 71 | RValue CodeGenFunction::EmitCXXMemberOrOperatorCall( |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 72 | const CXXMethodDecl *MD, const CGCallee &Callee, |
| 73 | ReturnValueSlot ReturnValue, |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 74 | llvm::Value *This, llvm::Value *ImplicitParam, QualType ImplicitParamTy, |
Richard Smith | 762672a | 2016-09-28 19:09:10 +0000 | [diff] [blame] | 75 | const CallExpr *CE, CallArgList *RtlArgs) { |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 76 | const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); |
| 77 | CallArgList Args; |
| 78 | RequiredArgs required = commonEmitCXXMemberOrOperatorCall( |
Richard Smith | 762672a | 2016-09-28 19:09:10 +0000 | [diff] [blame] | 79 | *this, MD, This, ImplicitParam, ImplicitParamTy, CE, Args, RtlArgs); |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 80 | auto &FnInfo = CGM.getTypes().arrangeCXXMethodCall(Args, FPT, required); |
| 81 | return EmitCall(FnInfo, Callee, ReturnValue, Args); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Alexey Samsonov | ae81bbb | 2016-03-10 00:20:37 +0000 | [diff] [blame] | 84 | RValue CodeGenFunction::EmitCXXDestructorCall( |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 85 | const CXXDestructorDecl *DD, const CGCallee &Callee, llvm::Value *This, |
Alexey Samsonov | ae81bbb | 2016-03-10 00:20:37 +0000 | [diff] [blame] | 86 | llvm::Value *ImplicitParam, QualType ImplicitParamTy, const CallExpr *CE, |
| 87 | StructorType Type) { |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 88 | CallArgList Args; |
Alexey Samsonov | ae81bbb | 2016-03-10 00:20:37 +0000 | [diff] [blame] | 89 | commonEmitCXXMemberOrOperatorCall(*this, DD, This, ImplicitParam, |
Richard Smith | 762672a | 2016-09-28 19:09:10 +0000 | [diff] [blame] | 90 | ImplicitParamTy, CE, Args, nullptr); |
Alexey Samsonov | ae81bbb | 2016-03-10 00:20:37 +0000 | [diff] [blame] | 91 | return EmitCall(CGM.getTypes().arrangeCXXStructorDeclaration(DD, Type), |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 92 | Callee, ReturnValueSlot(), Args); |
| 93 | } |
| 94 | |
| 95 | RValue CodeGenFunction::EmitCXXPseudoDestructorExpr( |
| 96 | const CXXPseudoDestructorExpr *E) { |
| 97 | QualType DestroyedType = E->getDestroyedType(); |
| 98 | if (DestroyedType.hasStrongOrWeakObjCLifetime()) { |
| 99 | // Automatic Reference Counting: |
| 100 | // If the pseudo-expression names a retainable object with weak or |
| 101 | // strong lifetime, the object shall be released. |
| 102 | Expr *BaseExpr = E->getBase(); |
| 103 | Address BaseValue = Address::invalid(); |
| 104 | Qualifiers BaseQuals; |
| 105 | |
| 106 | // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar. |
| 107 | if (E->isArrow()) { |
| 108 | BaseValue = EmitPointerWithAlignment(BaseExpr); |
| 109 | const PointerType *PTy = BaseExpr->getType()->getAs<PointerType>(); |
| 110 | BaseQuals = PTy->getPointeeType().getQualifiers(); |
| 111 | } else { |
| 112 | LValue BaseLV = EmitLValue(BaseExpr); |
| 113 | BaseValue = BaseLV.getAddress(); |
| 114 | QualType BaseTy = BaseExpr->getType(); |
| 115 | BaseQuals = BaseTy.getQualifiers(); |
| 116 | } |
| 117 | |
| 118 | switch (DestroyedType.getObjCLifetime()) { |
| 119 | case Qualifiers::OCL_None: |
| 120 | case Qualifiers::OCL_ExplicitNone: |
| 121 | case Qualifiers::OCL_Autoreleasing: |
| 122 | break; |
| 123 | |
| 124 | case Qualifiers::OCL_Strong: |
| 125 | EmitARCRelease(Builder.CreateLoad(BaseValue, |
| 126 | DestroyedType.isVolatileQualified()), |
| 127 | ARCPreciseLifetime); |
| 128 | break; |
| 129 | |
| 130 | case Qualifiers::OCL_Weak: |
| 131 | EmitARCDestroyWeak(BaseValue); |
| 132 | break; |
| 133 | } |
| 134 | } else { |
| 135 | // C++ [expr.pseudo]p1: |
| 136 | // The result shall only be used as the operand for the function call |
| 137 | // operator (), and the result of such a call has type void. The only |
| 138 | // effect is the evaluation of the postfix-expression before the dot or |
| 139 | // arrow. |
| 140 | EmitIgnoredExpr(E->getBase()); |
| 141 | } |
| 142 | |
| 143 | return RValue::get(nullptr); |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 146 | static CXXRecordDecl *getCXXRecord(const Expr *E) { |
| 147 | QualType T = E->getType(); |
| 148 | if (const PointerType *PTy = T->getAs<PointerType>()) |
| 149 | T = PTy->getPointeeType(); |
| 150 | const RecordType *Ty = T->castAs<RecordType>(); |
| 151 | return cast<CXXRecordDecl>(Ty->getDecl()); |
| 152 | } |
| 153 | |
Francois Pichet | 6422579 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 154 | // Note: This function also emit constructor calls to support a MSVC |
| 155 | // extensions allowing explicit constructor function call. |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 156 | RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE, |
| 157 | ReturnValueSlot ReturnValue) { |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 158 | const Expr *callee = CE->getCallee()->IgnoreParens(); |
| 159 | |
| 160 | if (isa<BinaryOperator>(callee)) |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 161 | return EmitCXXMemberPointerCallExpr(CE, ReturnValue); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 162 | |
| 163 | const MemberExpr *ME = cast<MemberExpr>(callee); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 164 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl()); |
| 165 | |
| 166 | if (MD->isStatic()) { |
| 167 | // The method is static, emit it as we would a regular call. |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 168 | CGCallee callee = CGCallee::forDirect(CGM.GetAddrOfFunction(MD), MD); |
| 169 | return EmitCall(getContext().getPointerType(MD->getType()), callee, CE, |
Alexey Samsonov | 70b9c01 | 2014-08-21 20:26:47 +0000 | [diff] [blame] | 170 | ReturnValue); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 171 | } |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 172 | |
Nico Weber | aad4af6 | 2014-12-03 01:21:41 +0000 | [diff] [blame] | 173 | bool HasQualifier = ME->hasQualifier(); |
| 174 | NestedNameSpecifier *Qualifier = HasQualifier ? ME->getQualifier() : nullptr; |
| 175 | bool IsArrow = ME->isArrow(); |
Rafael Espindola | ecbe2e9 | 2012-06-28 01:56:38 +0000 | [diff] [blame] | 176 | const Expr *Base = ME->getBase(); |
Nico Weber | aad4af6 | 2014-12-03 01:21:41 +0000 | [diff] [blame] | 177 | |
| 178 | return EmitCXXMemberOrOperatorMemberCallExpr( |
| 179 | CE, MD, ReturnValue, HasQualifier, Qualifier, IsArrow, Base); |
| 180 | } |
| 181 | |
| 182 | RValue CodeGenFunction::EmitCXXMemberOrOperatorMemberCallExpr( |
| 183 | const CallExpr *CE, const CXXMethodDecl *MD, ReturnValueSlot ReturnValue, |
| 184 | bool HasQualifier, NestedNameSpecifier *Qualifier, bool IsArrow, |
| 185 | const Expr *Base) { |
| 186 | assert(isa<CXXMemberCallExpr>(CE) || isa<CXXOperatorCallExpr>(CE)); |
| 187 | |
| 188 | // Compute the object pointer. |
| 189 | bool CanUseVirtualCall = MD->isVirtual() && !HasQualifier; |
Rafael Espindola | ecbe2e9 | 2012-06-28 01:56:38 +0000 | [diff] [blame] | 190 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 191 | const CXXMethodDecl *DevirtualizedMethod = nullptr; |
Benjamin Kramer | 7463ed7 | 2013-08-25 22:46:27 +0000 | [diff] [blame] | 192 | if (CanUseVirtualCall && CanDevirtualizeMemberFunctionCall(Base, MD)) { |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 193 | const CXXRecordDecl *BestDynamicDecl = Base->getBestDynamicClassType(); |
| 194 | DevirtualizedMethod = MD->getCorrespondingMethodInClass(BestDynamicDecl); |
| 195 | assert(DevirtualizedMethod); |
| 196 | const CXXRecordDecl *DevirtualizedClass = DevirtualizedMethod->getParent(); |
| 197 | const Expr *Inner = Base->ignoreParenBaseCasts(); |
Alexey Bataev | 5bd6879 | 2014-09-29 10:32:21 +0000 | [diff] [blame] | 198 | if (DevirtualizedMethod->getReturnType().getCanonicalType() != |
| 199 | MD->getReturnType().getCanonicalType()) |
| 200 | // If the return types are not the same, this might be a case where more |
| 201 | // code needs to run to compensate for it. For example, the derived |
| 202 | // method might return a type that inherits form from the return |
| 203 | // type of MD and has a prefix. |
| 204 | // For now we just avoid devirtualizing these covariant cases. |
| 205 | DevirtualizedMethod = nullptr; |
| 206 | else if (getCXXRecord(Inner) == DevirtualizedClass) |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 207 | // If the class of the Inner expression is where the dynamic method |
| 208 | // is defined, build the this pointer from it. |
| 209 | Base = Inner; |
| 210 | else if (getCXXRecord(Base) != DevirtualizedClass) { |
| 211 | // If the method is defined in a class that is not the best dynamic |
| 212 | // one or the one of the full expression, we would have to build |
| 213 | // a derived-to-base cast to compute the correct this pointer, but |
| 214 | // we don't have support for that yet, so do a virtual call. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 215 | DevirtualizedMethod = nullptr; |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 216 | } |
| 217 | } |
Rafael Espindola | ecbe2e9 | 2012-06-28 01:56:38 +0000 | [diff] [blame] | 218 | |
Richard Smith | 762672a | 2016-09-28 19:09:10 +0000 | [diff] [blame] | 219 | // C++17 demands that we evaluate the RHS of a (possibly-compound) assignment |
| 220 | // operator before the LHS. |
| 221 | CallArgList RtlArgStorage; |
| 222 | CallArgList *RtlArgs = nullptr; |
| 223 | if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(CE)) { |
| 224 | if (OCE->isAssignmentOp()) { |
| 225 | RtlArgs = &RtlArgStorage; |
| 226 | EmitCallArgs(*RtlArgs, MD->getType()->castAs<FunctionProtoType>(), |
| 227 | drop_begin(CE->arguments(), 1), CE->getDirectCallee(), |
Richard Smith | a560ccf | 2016-09-29 21:30:12 +0000 | [diff] [blame] | 228 | /*ParamsToSkip*/0, EvaluationOrder::ForceRightToLeft); |
Richard Smith | 762672a | 2016-09-28 19:09:10 +0000 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 232 | Address This = Address::invalid(); |
Nico Weber | aad4af6 | 2014-12-03 01:21:41 +0000 | [diff] [blame] | 233 | if (IsArrow) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 234 | This = EmitPointerWithAlignment(Base); |
John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 235 | else |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 236 | This = EmitLValue(Base).getAddress(); |
Rafael Espindola | ecbe2e9 | 2012-06-28 01:56:38 +0000 | [diff] [blame] | 237 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 238 | |
Richard Smith | 419bd09 | 2015-04-29 19:26:57 +0000 | [diff] [blame] | 239 | if (MD->isTrivial() || (MD->isDefaulted() && MD->getParent()->isUnion())) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 240 | if (isa<CXXDestructorDecl>(MD)) return RValue::get(nullptr); |
Francois Pichet | 6422579 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 241 | if (isa<CXXConstructorDecl>(MD) && |
| 242 | cast<CXXConstructorDecl>(MD)->isDefaultConstructor()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 243 | return RValue::get(nullptr); |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 244 | |
Nico Weber | aad4af6 | 2014-12-03 01:21:41 +0000 | [diff] [blame] | 245 | if (!MD->getParent()->mayInsertExtraPadding()) { |
| 246 | if (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator()) { |
| 247 | // We don't like to generate the trivial copy/move assignment operator |
| 248 | // when it isn't necessary; just produce the proper effect here. |
Richard Smith | 762672a | 2016-09-28 19:09:10 +0000 | [diff] [blame] | 249 | LValue RHS = isa<CXXOperatorCallExpr>(CE) |
| 250 | ? MakeNaturalAlignAddrLValue( |
| 251 | (*RtlArgs)[0].RV.getScalarVal(), |
| 252 | (*(CE->arg_begin() + 1))->getType()) |
| 253 | : EmitLValue(*CE->arg_begin()); |
| 254 | EmitAggregateAssign(This, RHS.getAddress(), CE->getType()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 255 | return RValue::get(This.getPointer()); |
Nico Weber | aad4af6 | 2014-12-03 01:21:41 +0000 | [diff] [blame] | 256 | } |
Alexey Samsonov | 525bf65 | 2014-08-25 21:58:56 +0000 | [diff] [blame] | 257 | |
Nico Weber | aad4af6 | 2014-12-03 01:21:41 +0000 | [diff] [blame] | 258 | if (isa<CXXConstructorDecl>(MD) && |
| 259 | cast<CXXConstructorDecl>(MD)->isCopyOrMoveConstructor()) { |
| 260 | // Trivial move and copy ctor are the same. |
| 261 | assert(CE->getNumArgs() == 1 && "unexpected argcount for trivial ctor"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 262 | Address RHS = EmitLValue(*CE->arg_begin()).getAddress(); |
Benjamin Kramer | f48ee44 | 2015-07-18 14:35:53 +0000 | [diff] [blame] | 263 | EmitAggregateCopy(This, RHS, (*CE->arg_begin())->getType()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 264 | return RValue::get(This.getPointer()); |
Nico Weber | aad4af6 | 2014-12-03 01:21:41 +0000 | [diff] [blame] | 265 | } |
| 266 | llvm_unreachable("unknown trivial member function"); |
Francois Pichet | 6422579 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 267 | } |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 268 | } |
| 269 | |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 270 | // Compute the function type we're calling. |
Nico Weber | 3abfe95 | 2014-12-02 20:41:18 +0000 | [diff] [blame] | 271 | const CXXMethodDecl *CalleeDecl = |
| 272 | DevirtualizedMethod ? DevirtualizedMethod : MD; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 273 | const CGFunctionInfo *FInfo = nullptr; |
Nico Weber | 3abfe95 | 2014-12-02 20:41:18 +0000 | [diff] [blame] | 274 | if (const auto *Dtor = dyn_cast<CXXDestructorDecl>(CalleeDecl)) |
Rafael Espindola | 8d2a19b | 2014-09-08 16:01:27 +0000 | [diff] [blame] | 275 | FInfo = &CGM.getTypes().arrangeCXXStructorDeclaration( |
| 276 | Dtor, StructorType::Complete); |
Nico Weber | 3abfe95 | 2014-12-02 20:41:18 +0000 | [diff] [blame] | 277 | else if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(CalleeDecl)) |
Rafael Espindola | 8d2a19b | 2014-09-08 16:01:27 +0000 | [diff] [blame] | 278 | FInfo = &CGM.getTypes().arrangeCXXStructorDeclaration( |
| 279 | Ctor, StructorType::Complete); |
Francois Pichet | 6422579 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 280 | else |
Eli Friedman | ade6097 | 2012-10-25 00:12:49 +0000 | [diff] [blame] | 281 | FInfo = &CGM.getTypes().arrangeCXXMethodDeclaration(CalleeDecl); |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 282 | |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 283 | llvm::FunctionType *Ty = CGM.getTypes().GetFunctionType(*FInfo); |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 284 | |
Ivan Krasin | d98f5d7 | 2016-11-17 00:39:48 +0000 | [diff] [blame] | 285 | // C++11 [class.mfct.non-static]p2: |
| 286 | // If a non-static member function of a class X is called for an object that |
| 287 | // is not of type X, or of a type derived from X, the behavior is undefined. |
| 288 | SourceLocation CallLoc; |
| 289 | ASTContext &C = getContext(); |
| 290 | if (CE) |
| 291 | CallLoc = CE->getExprLoc(); |
| 292 | |
| 293 | EmitTypeCheck(isa<CXXConstructorDecl>(CalleeDecl) |
| 294 | ? CodeGenFunction::TCK_ConstructorCall |
| 295 | : CodeGenFunction::TCK_MemberCall, |
| 296 | CallLoc, This.getPointer(), C.getRecordType(CalleeDecl->getParent())); |
| 297 | |
Vedant Kumar | 018f266 | 2016-10-19 20:21:16 +0000 | [diff] [blame] | 298 | // FIXME: Uses of 'MD' past this point need to be audited. We may need to use |
| 299 | // 'CalleeDecl' instead. |
| 300 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 301 | // C++ [class.virtual]p12: |
| 302 | // Explicit qualification with the scope operator (5.1) suppresses the |
| 303 | // virtual call mechanism. |
| 304 | // |
| 305 | // We also don't emit a virtual call if the base expression has a record type |
| 306 | // because then we know what the type is. |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 307 | bool UseVirtualCall = CanUseVirtualCall && !DevirtualizedMethod; |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 308 | |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 309 | if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD)) { |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 310 | assert(CE->arg_begin() == CE->arg_end() && |
| 311 | "Destructor shouldn't have explicit parameters"); |
| 312 | assert(ReturnValue.isNull() && "Destructor shouldn't have return value"); |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 313 | if (UseVirtualCall) { |
Nico Weber | aad4af6 | 2014-12-03 01:21:41 +0000 | [diff] [blame] | 314 | CGM.getCXXABI().EmitVirtualDestructorCall( |
| 315 | *this, Dtor, Dtor_Complete, This, cast<CXXMemberCallExpr>(CE)); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 316 | } else { |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 317 | CGCallee Callee; |
Nico Weber | aad4af6 | 2014-12-03 01:21:41 +0000 | [diff] [blame] | 318 | if (getLangOpts().AppleKext && MD->isVirtual() && HasQualifier) |
| 319 | Callee = BuildAppleKextVirtualCall(MD, Qualifier, Ty); |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 320 | else if (!DevirtualizedMethod) |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 321 | Callee = CGCallee::forDirect( |
| 322 | CGM.getAddrOfCXXStructor(Dtor, StructorType::Complete, FInfo, Ty), |
| 323 | Dtor); |
Rafael Espindola | 49e860b | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 324 | else { |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 325 | const CXXDestructorDecl *DDtor = |
| 326 | cast<CXXDestructorDecl>(DevirtualizedMethod); |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 327 | Callee = CGCallee::forDirect( |
| 328 | CGM.GetAddrOfFunction(GlobalDecl(DDtor, Dtor_Complete), Ty), |
| 329 | DDtor); |
Rafael Espindola | 49e860b | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 330 | } |
Vedant Kumar | 018f266 | 2016-10-19 20:21:16 +0000 | [diff] [blame] | 331 | EmitCXXMemberOrOperatorCall( |
| 332 | CalleeDecl, Callee, ReturnValue, This.getPointer(), |
| 333 | /*ImplicitParam=*/nullptr, QualType(), CE, nullptr); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 334 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 335 | return RValue::get(nullptr); |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 336 | } |
| 337 | |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 338 | CGCallee Callee; |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 339 | if (const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(MD)) { |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 340 | Callee = CGCallee::forDirect( |
| 341 | CGM.GetAddrOfFunction(GlobalDecl(Ctor, Ctor_Complete), Ty), |
| 342 | Ctor); |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 343 | } else if (UseVirtualCall) { |
Peter Collingbourne | 6708c4a | 2015-06-19 01:51:54 +0000 | [diff] [blame] | 344 | Callee = CGM.getCXXABI().getVirtualFunctionPointer(*this, MD, This, Ty, |
| 345 | CE->getLocStart()); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 346 | } else { |
Peter Collingbourne | 1a7488a | 2015-04-02 00:23:30 +0000 | [diff] [blame] | 347 | if (SanOpts.has(SanitizerKind::CFINVCall) && |
| 348 | MD->getParent()->isDynamicClass()) { |
Piotr Padlewski | 4b1ac72 | 2015-09-15 21:46:55 +0000 | [diff] [blame] | 349 | llvm::Value *VTable = GetVTablePtr(This, Int8PtrTy, MD->getParent()); |
Peter Collingbourne | fb532b9 | 2016-02-24 20:46:36 +0000 | [diff] [blame] | 350 | EmitVTablePtrCheckForCall(MD->getParent(), VTable, CFITCK_NVCall, |
| 351 | CE->getLocStart()); |
Peter Collingbourne | 1a7488a | 2015-04-02 00:23:30 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Nico Weber | aad4af6 | 2014-12-03 01:21:41 +0000 | [diff] [blame] | 354 | if (getLangOpts().AppleKext && MD->isVirtual() && HasQualifier) |
| 355 | Callee = BuildAppleKextVirtualCall(MD, Qualifier, Ty); |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 356 | else if (!DevirtualizedMethod) |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 357 | Callee = CGCallee::forDirect(CGM.GetAddrOfFunction(MD, Ty), MD); |
Rafael Espindola | 49e860b | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 358 | else { |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 359 | Callee = CGCallee::forDirect( |
| 360 | CGM.GetAddrOfFunction(DevirtualizedMethod, Ty), |
| 361 | DevirtualizedMethod); |
Rafael Espindola | 49e860b | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 362 | } |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Timur Iskhodzhanov | f174942 | 2014-03-14 17:43:37 +0000 | [diff] [blame] | 365 | if (MD->isVirtual()) { |
| 366 | This = CGM.getCXXABI().adjustThisArgumentForVirtualFunctionCall( |
Reid Kleckner | 4b60f30 | 2016-05-03 18:44:29 +0000 | [diff] [blame] | 367 | *this, CalleeDecl, This, UseVirtualCall); |
Timur Iskhodzhanov | f174942 | 2014-03-14 17:43:37 +0000 | [diff] [blame] | 368 | } |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 369 | |
Vedant Kumar | 018f266 | 2016-10-19 20:21:16 +0000 | [diff] [blame] | 370 | return EmitCXXMemberOrOperatorCall( |
| 371 | CalleeDecl, Callee, ReturnValue, This.getPointer(), |
| 372 | /*ImplicitParam=*/nullptr, QualType(), CE, RtlArgs); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | RValue |
| 376 | CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E, |
| 377 | ReturnValueSlot ReturnValue) { |
| 378 | const BinaryOperator *BO = |
| 379 | cast<BinaryOperator>(E->getCallee()->IgnoreParens()); |
| 380 | const Expr *BaseExpr = BO->getLHS(); |
| 381 | const Expr *MemFnExpr = BO->getRHS(); |
| 382 | |
| 383 | const MemberPointerType *MPT = |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 384 | MemFnExpr->getType()->castAs<MemberPointerType>(); |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 385 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 386 | const FunctionProtoType *FPT = |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 387 | MPT->getPointeeType()->castAs<FunctionProtoType>(); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 388 | const CXXRecordDecl *RD = |
| 389 | cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl()); |
| 390 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 391 | // Emit the 'this' pointer. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 392 | Address This = Address::invalid(); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 393 | if (BO->getOpcode() == BO_PtrMemI) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 394 | This = EmitPointerWithAlignment(BaseExpr); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 395 | else |
| 396 | This = EmitLValue(BaseExpr).getAddress(); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 397 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 398 | EmitTypeCheck(TCK_MemberCall, E->getExprLoc(), This.getPointer(), |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 399 | QualType(MPT->getClass(), 0)); |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 400 | |
Richard Smith | bde62d7 | 2016-09-26 23:56:57 +0000 | [diff] [blame] | 401 | // Get the member function pointer. |
| 402 | llvm::Value *MemFnPtr = EmitScalarExpr(MemFnExpr); |
| 403 | |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 404 | // Ask the ABI to load the callee. Note that This is modified. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 405 | llvm::Value *ThisPtrForCall = nullptr; |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 406 | CGCallee Callee = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 407 | CGM.getCXXABI().EmitLoadOfMemberFunctionPointer(*this, BO, This, |
| 408 | ThisPtrForCall, MemFnPtr, MPT); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 409 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 410 | CallArgList Args; |
| 411 | |
| 412 | QualType ThisType = |
| 413 | getContext().getPointerType(getContext().getTagDeclType(RD)); |
| 414 | |
| 415 | // Push the this ptr. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 416 | Args.add(RValue::get(ThisPtrForCall), ThisType); |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 417 | |
George Burgess IV | 419996c | 2016-06-16 23:06:04 +0000 | [diff] [blame] | 418 | RequiredArgs required = |
| 419 | RequiredArgs::forPrototypePlus(FPT, 1, /*FD=*/nullptr); |
| 420 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 421 | // And the rest of the call args |
George Burgess IV | 419996c | 2016-06-16 23:06:04 +0000 | [diff] [blame] | 422 | EmitCallArgs(Args, FPT, E->arguments()); |
Nick Lewycky | 5fa40c3 | 2013-10-01 21:51:38 +0000 | [diff] [blame] | 423 | return EmitCall(CGM.getTypes().arrangeCXXMethodCall(Args, FPT, required), |
| 424 | Callee, ReturnValue, Args); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | RValue |
| 428 | CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E, |
| 429 | const CXXMethodDecl *MD, |
| 430 | ReturnValueSlot ReturnValue) { |
| 431 | assert(MD->isInstance() && |
| 432 | "Trying to emit a member call expr on a static method!"); |
Nico Weber | aad4af6 | 2014-12-03 01:21:41 +0000 | [diff] [blame] | 433 | return EmitCXXMemberOrOperatorMemberCallExpr( |
| 434 | E, MD, ReturnValue, /*HasQualifier=*/false, /*Qualifier=*/nullptr, |
| 435 | /*IsArrow=*/false, E->getArg(0)); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 438 | RValue CodeGenFunction::EmitCUDAKernelCallExpr(const CUDAKernelCallExpr *E, |
| 439 | ReturnValueSlot ReturnValue) { |
| 440 | return CGM.getCUDARuntime().EmitCUDAKernelCallExpr(*this, E, ReturnValue); |
| 441 | } |
| 442 | |
Eli Friedman | fde961d | 2011-10-14 02:27:24 +0000 | [diff] [blame] | 443 | static void EmitNullBaseClassInitialization(CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 444 | Address DestPtr, |
Eli Friedman | fde961d | 2011-10-14 02:27:24 +0000 | [diff] [blame] | 445 | const CXXRecordDecl *Base) { |
| 446 | if (Base->isEmpty()) |
| 447 | return; |
| 448 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 449 | DestPtr = CGF.Builder.CreateElementBitCast(DestPtr, CGF.Int8Ty); |
Eli Friedman | fde961d | 2011-10-14 02:27:24 +0000 | [diff] [blame] | 450 | |
| 451 | const ASTRecordLayout &Layout = CGF.getContext().getASTRecordLayout(Base); |
David Majnemer | 8671c6e | 2015-11-02 09:01:44 +0000 | [diff] [blame] | 452 | CharUnits NVSize = Layout.getNonVirtualSize(); |
| 453 | |
| 454 | // We cannot simply zero-initialize the entire base sub-object if vbptrs are |
| 455 | // present, they are initialized by the most derived class before calling the |
| 456 | // constructor. |
| 457 | SmallVector<std::pair<CharUnits, CharUnits>, 1> Stores; |
| 458 | Stores.emplace_back(CharUnits::Zero(), NVSize); |
| 459 | |
| 460 | // Each store is split by the existence of a vbptr. |
| 461 | CharUnits VBPtrWidth = CGF.getPointerSize(); |
| 462 | std::vector<CharUnits> VBPtrOffsets = |
| 463 | CGF.CGM.getCXXABI().getVBPtrOffsets(Base); |
| 464 | for (CharUnits VBPtrOffset : VBPtrOffsets) { |
David Majnemer | 7f980d8 | 2016-05-12 03:51:52 +0000 | [diff] [blame] | 465 | // Stop before we hit any virtual base pointers located in virtual bases. |
| 466 | if (VBPtrOffset >= NVSize) |
| 467 | break; |
David Majnemer | 8671c6e | 2015-11-02 09:01:44 +0000 | [diff] [blame] | 468 | std::pair<CharUnits, CharUnits> LastStore = Stores.pop_back_val(); |
| 469 | CharUnits LastStoreOffset = LastStore.first; |
| 470 | CharUnits LastStoreSize = LastStore.second; |
| 471 | |
| 472 | CharUnits SplitBeforeOffset = LastStoreOffset; |
| 473 | CharUnits SplitBeforeSize = VBPtrOffset - SplitBeforeOffset; |
| 474 | assert(!SplitBeforeSize.isNegative() && "negative store size!"); |
| 475 | if (!SplitBeforeSize.isZero()) |
| 476 | Stores.emplace_back(SplitBeforeOffset, SplitBeforeSize); |
| 477 | |
| 478 | CharUnits SplitAfterOffset = VBPtrOffset + VBPtrWidth; |
| 479 | CharUnits SplitAfterSize = LastStoreSize - SplitAfterOffset; |
| 480 | assert(!SplitAfterSize.isNegative() && "negative store size!"); |
| 481 | if (!SplitAfterSize.isZero()) |
| 482 | Stores.emplace_back(SplitAfterOffset, SplitAfterSize); |
| 483 | } |
Eli Friedman | fde961d | 2011-10-14 02:27:24 +0000 | [diff] [blame] | 484 | |
| 485 | // If the type contains a pointer to data member we can't memset it to zero. |
| 486 | // Instead, create a null constant and copy it to the destination. |
| 487 | // TODO: there are other patterns besides zero that we can usefully memset, |
| 488 | // like -1, which happens to be the pattern used by member-pointers. |
| 489 | // TODO: isZeroInitializable can be over-conservative in the case where a |
| 490 | // virtual base contains a member pointer. |
David Majnemer | 8671c6e | 2015-11-02 09:01:44 +0000 | [diff] [blame] | 491 | llvm::Constant *NullConstantForBase = CGF.CGM.EmitNullConstantForBase(Base); |
| 492 | if (!NullConstantForBase->isNullValue()) { |
| 493 | llvm::GlobalVariable *NullVariable = new llvm::GlobalVariable( |
| 494 | CGF.CGM.getModule(), NullConstantForBase->getType(), |
| 495 | /*isConstant=*/true, llvm::GlobalVariable::PrivateLinkage, |
| 496 | NullConstantForBase, Twine()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 497 | |
| 498 | CharUnits Align = std::max(Layout.getNonVirtualAlignment(), |
| 499 | DestPtr.getAlignment()); |
Eli Friedman | fde961d | 2011-10-14 02:27:24 +0000 | [diff] [blame] | 500 | NullVariable->setAlignment(Align.getQuantity()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 501 | |
| 502 | Address SrcPtr = Address(CGF.EmitCastToVoidPtr(NullVariable), Align); |
Eli Friedman | fde961d | 2011-10-14 02:27:24 +0000 | [diff] [blame] | 503 | |
| 504 | // Get and call the appropriate llvm.memcpy overload. |
David Majnemer | 8671c6e | 2015-11-02 09:01:44 +0000 | [diff] [blame] | 505 | for (std::pair<CharUnits, CharUnits> Store : Stores) { |
| 506 | CharUnits StoreOffset = Store.first; |
| 507 | CharUnits StoreSize = Store.second; |
| 508 | llvm::Value *StoreSizeVal = CGF.CGM.getSize(StoreSize); |
| 509 | CGF.Builder.CreateMemCpy( |
| 510 | CGF.Builder.CreateConstInBoundsByteGEP(DestPtr, StoreOffset), |
| 511 | CGF.Builder.CreateConstInBoundsByteGEP(SrcPtr, StoreOffset), |
| 512 | StoreSizeVal); |
| 513 | } |
| 514 | |
Eli Friedman | fde961d | 2011-10-14 02:27:24 +0000 | [diff] [blame] | 515 | // Otherwise, just memset the whole thing to zero. This is legal |
| 516 | // because in LLVM, all default initializers (other than the ones we just |
| 517 | // handled above) are guaranteed to have a bit pattern of all zeros. |
David Majnemer | 8671c6e | 2015-11-02 09:01:44 +0000 | [diff] [blame] | 518 | } else { |
| 519 | for (std::pair<CharUnits, CharUnits> Store : Stores) { |
| 520 | CharUnits StoreOffset = Store.first; |
| 521 | CharUnits StoreSize = Store.second; |
| 522 | llvm::Value *StoreSizeVal = CGF.CGM.getSize(StoreSize); |
| 523 | CGF.Builder.CreateMemSet( |
| 524 | CGF.Builder.CreateConstInBoundsByteGEP(DestPtr, StoreOffset), |
| 525 | CGF.Builder.getInt8(0), StoreSizeVal); |
| 526 | } |
| 527 | } |
Eli Friedman | fde961d | 2011-10-14 02:27:24 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 530 | void |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 531 | CodeGenFunction::EmitCXXConstructExpr(const CXXConstructExpr *E, |
| 532 | AggValueSlot Dest) { |
| 533 | assert(!Dest.isIgnored() && "Must have a destination!"); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 534 | const CXXConstructorDecl *CD = E->getConstructor(); |
Douglas Gregor | 630c76e | 2010-08-22 16:15:35 +0000 | [diff] [blame] | 535 | |
| 536 | // If we require zero initialization before (or instead of) calling the |
| 537 | // constructor, as can be the case with a non-user-provided default |
Argyrios Kyrtzidis | 0353526 | 2011-04-28 22:57:55 +0000 | [diff] [blame] | 538 | // constructor, emit the zero initialization now, unless destination is |
| 539 | // already zeroed. |
Eli Friedman | fde961d | 2011-10-14 02:27:24 +0000 | [diff] [blame] | 540 | if (E->requiresZeroInitialization() && !Dest.isZeroed()) { |
| 541 | switch (E->getConstructionKind()) { |
| 542 | case CXXConstructExpr::CK_Delegating: |
Eli Friedman | fde961d | 2011-10-14 02:27:24 +0000 | [diff] [blame] | 543 | case CXXConstructExpr::CK_Complete: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 544 | EmitNullInitialization(Dest.getAddress(), E->getType()); |
Eli Friedman | fde961d | 2011-10-14 02:27:24 +0000 | [diff] [blame] | 545 | break; |
| 546 | case CXXConstructExpr::CK_VirtualBase: |
| 547 | case CXXConstructExpr::CK_NonVirtualBase: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 548 | EmitNullBaseClassInitialization(*this, Dest.getAddress(), |
| 549 | CD->getParent()); |
Eli Friedman | fde961d | 2011-10-14 02:27:24 +0000 | [diff] [blame] | 550 | break; |
| 551 | } |
| 552 | } |
Douglas Gregor | 630c76e | 2010-08-22 16:15:35 +0000 | [diff] [blame] | 553 | |
| 554 | // If this is a call to a trivial default constructor, do nothing. |
| 555 | if (CD->isTrivial() && CD->isDefaultConstructor()) |
| 556 | return; |
| 557 | |
John McCall | 8ea46b6 | 2010-09-18 00:58:34 +0000 | [diff] [blame] | 558 | // Elide the constructor if we're constructing from a temporary. |
| 559 | // The temporary check is required because Sema sets this on NRVO |
| 560 | // returns. |
Richard Smith | 9c6890a | 2012-11-01 22:30:59 +0000 | [diff] [blame] | 561 | if (getLangOpts().ElideConstructors && E->isElidable()) { |
John McCall | 8ea46b6 | 2010-09-18 00:58:34 +0000 | [diff] [blame] | 562 | assert(getContext().hasSameUnqualifiedType(E->getType(), |
| 563 | E->getArg(0)->getType())); |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 564 | if (E->getArg(0)->isTemporaryObject(getContext(), CD->getParent())) { |
| 565 | EmitAggExpr(E->getArg(0), Dest); |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 566 | return; |
| 567 | } |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 568 | } |
Douglas Gregor | 630c76e | 2010-08-22 16:15:35 +0000 | [diff] [blame] | 569 | |
Alexey Bataev | e7545b3 | 2016-04-29 09:39:50 +0000 | [diff] [blame] | 570 | if (const ArrayType *arrayType |
| 571 | = getContext().getAsArrayType(E->getType())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 572 | EmitCXXAggrConstructorCall(CD, arrayType, Dest.getAddress(), E); |
John McCall | f677a8e | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 573 | } else { |
Cameron Esfahani | bceca20 | 2011-05-06 21:28:42 +0000 | [diff] [blame] | 574 | CXXCtorType Type = Ctor_Complete; |
Alexis Hunt | 271c368 | 2011-05-03 20:19:28 +0000 | [diff] [blame] | 575 | bool ForVirtualBase = false; |
Douglas Gregor | 6153500 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 576 | bool Delegating = false; |
| 577 | |
Alexis Hunt | 271c368 | 2011-05-03 20:19:28 +0000 | [diff] [blame] | 578 | switch (E->getConstructionKind()) { |
| 579 | case CXXConstructExpr::CK_Delegating: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 580 | // We should be emitting a constructor; GlobalDecl will assert this |
| 581 | Type = CurGD.getCtorType(); |
Douglas Gregor | 6153500 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 582 | Delegating = true; |
Alexis Hunt | 271c368 | 2011-05-03 20:19:28 +0000 | [diff] [blame] | 583 | break; |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 584 | |
Alexis Hunt | 271c368 | 2011-05-03 20:19:28 +0000 | [diff] [blame] | 585 | case CXXConstructExpr::CK_Complete: |
| 586 | Type = Ctor_Complete; |
| 587 | break; |
| 588 | |
| 589 | case CXXConstructExpr::CK_VirtualBase: |
| 590 | ForVirtualBase = true; |
| 591 | // fall-through |
| 592 | |
| 593 | case CXXConstructExpr::CK_NonVirtualBase: |
| 594 | Type = Ctor_Base; |
| 595 | } |
Anders Carlsson | e11f9ce | 2010-05-02 23:20:53 +0000 | [diff] [blame] | 596 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 597 | // Call the constructor. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 598 | EmitCXXConstructorCall(CD, Type, ForVirtualBase, Delegating, |
| 599 | Dest.getAddress(), E); |
Anders Carlsson | e11f9ce | 2010-05-02 23:20:53 +0000 | [diff] [blame] | 600 | } |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 601 | } |
| 602 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 603 | void CodeGenFunction::EmitSynthesizedCXXCopyCtor(Address Dest, Address Src, |
| 604 | const Expr *Exp) { |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 605 | if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(Exp)) |
Fariborz Jahanian | e988bda | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 606 | Exp = E->getSubExpr(); |
| 607 | assert(isa<CXXConstructExpr>(Exp) && |
| 608 | "EmitSynthesizedCXXCopyCtor - unknown copy ctor expr"); |
| 609 | const CXXConstructExpr* E = cast<CXXConstructExpr>(Exp); |
| 610 | const CXXConstructorDecl *CD = E->getConstructor(); |
| 611 | RunCleanupsScope Scope(*this); |
| 612 | |
| 613 | // If we require zero initialization before (or instead of) calling the |
| 614 | // constructor, as can be the case with a non-user-provided default |
| 615 | // constructor, emit the zero initialization now. |
| 616 | // FIXME. Do I still need this for a copy ctor synthesis? |
| 617 | if (E->requiresZeroInitialization()) |
| 618 | EmitNullInitialization(Dest, E->getType()); |
| 619 | |
Chandler Carruth | 99da11c | 2010-11-15 13:54:43 +0000 | [diff] [blame] | 620 | assert(!getContext().getAsConstantArrayType(E->getType()) |
| 621 | && "EmitSynthesizedCXXCopyCtor - Copied-in Array"); |
Alexey Samsonov | 525bf65 | 2014-08-25 21:58:56 +0000 | [diff] [blame] | 622 | EmitSynthesizedCXXCopyCtorCall(CD, Dest, Src, E); |
Fariborz Jahanian | e988bda | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 623 | } |
| 624 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 625 | static CharUnits CalculateCookiePadding(CodeGenFunction &CGF, |
| 626 | const CXXNewExpr *E) { |
Anders Carlsson | 21122cf | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 627 | if (!E->isArray()) |
Ken Dyck | 3eb55cf | 2010-01-26 19:44:24 +0000 | [diff] [blame] | 628 | return CharUnits::Zero(); |
Anders Carlsson | 21122cf | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 629 | |
John McCall | 7ec4b43 | 2011-05-16 01:05:12 +0000 | [diff] [blame] | 630 | // No cookie is required if the operator new[] being used is the |
| 631 | // reserved placement operator new[]. |
| 632 | if (E->getOperatorNew()->isReservedGlobalPlacementOperator()) |
John McCall | aa4149a | 2010-08-23 01:17:59 +0000 | [diff] [blame] | 633 | return CharUnits::Zero(); |
| 634 | |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 635 | return CGF.CGM.getCXXABI().GetArrayCookieSize(E); |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 636 | } |
| 637 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 638 | static llvm::Value *EmitCXXNewAllocSize(CodeGenFunction &CGF, |
| 639 | const CXXNewExpr *e, |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 640 | unsigned minElements, |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 641 | llvm::Value *&numElements, |
| 642 | llvm::Value *&sizeWithoutCookie) { |
| 643 | QualType type = e->getAllocatedType(); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 644 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 645 | if (!e->isArray()) { |
| 646 | CharUnits typeSize = CGF.getContext().getTypeSizeInChars(type); |
| 647 | sizeWithoutCookie |
| 648 | = llvm::ConstantInt::get(CGF.SizeTy, typeSize.getQuantity()); |
| 649 | return sizeWithoutCookie; |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 650 | } |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 651 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 652 | // The width of size_t. |
| 653 | unsigned sizeWidth = CGF.SizeTy->getBitWidth(); |
| 654 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 655 | // Figure out the cookie size. |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 656 | llvm::APInt cookieSize(sizeWidth, |
| 657 | CalculateCookiePadding(CGF, e).getQuantity()); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 658 | |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 659 | // Emit the array size expression. |
Argyrios Kyrtzidis | 7648fb4 | 2010-08-26 15:23:38 +0000 | [diff] [blame] | 660 | // We multiply the size of all dimensions for NumElements. |
| 661 | // e.g for 'int[2][3]', ElemType is 'int' and NumElements is 6. |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 662 | numElements = CGF.EmitScalarExpr(e->getArraySize()); |
| 663 | assert(isa<llvm::IntegerType>(numElements->getType())); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 664 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 665 | // The number of elements can be have an arbitrary integer type; |
| 666 | // essentially, we need to multiply it by a constant factor, add a |
| 667 | // cookie size, and verify that the result is representable as a |
| 668 | // size_t. That's just a gloss, though, and it's wrong in one |
| 669 | // important way: if the count is negative, it's an error even if |
| 670 | // the cookie size would bring the total size >= 0. |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 671 | bool isSigned |
| 672 | = e->getArraySize()->getType()->isSignedIntegerOrEnumerationType(); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 673 | llvm::IntegerType *numElementsType |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 674 | = cast<llvm::IntegerType>(numElements->getType()); |
| 675 | unsigned numElementsWidth = numElementsType->getBitWidth(); |
| 676 | |
| 677 | // Compute the constant factor. |
| 678 | llvm::APInt arraySizeMultiplier(sizeWidth, 1); |
Argyrios Kyrtzidis | 7648fb4 | 2010-08-26 15:23:38 +0000 | [diff] [blame] | 679 | while (const ConstantArrayType *CAT |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 680 | = CGF.getContext().getAsConstantArrayType(type)) { |
| 681 | type = CAT->getElementType(); |
| 682 | arraySizeMultiplier *= CAT->getSize(); |
Argyrios Kyrtzidis | 7648fb4 | 2010-08-26 15:23:38 +0000 | [diff] [blame] | 683 | } |
| 684 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 685 | CharUnits typeSize = CGF.getContext().getTypeSizeInChars(type); |
| 686 | llvm::APInt typeSizeMultiplier(sizeWidth, typeSize.getQuantity()); |
| 687 | typeSizeMultiplier *= arraySizeMultiplier; |
| 688 | |
| 689 | // This will be a size_t. |
| 690 | llvm::Value *size; |
Chris Lattner | f2f3870 | 2010-07-20 21:07:09 +0000 | [diff] [blame] | 691 | |
Chris Lattner | 32ac583 | 2010-07-20 21:55:52 +0000 | [diff] [blame] | 692 | // If someone is doing 'new int[42]' there is no need to do a dynamic check. |
| 693 | // Don't bloat the -O0 code. |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 694 | if (llvm::ConstantInt *numElementsC = |
| 695 | dyn_cast<llvm::ConstantInt>(numElements)) { |
| 696 | const llvm::APInt &count = numElementsC->getValue(); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 697 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 698 | bool hasAnyOverflow = false; |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 699 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 700 | // If 'count' was a negative number, it's an overflow. |
| 701 | if (isSigned && count.isNegative()) |
| 702 | hasAnyOverflow = true; |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 703 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 704 | // We want to do all this arithmetic in size_t. If numElements is |
| 705 | // wider than that, check whether it's already too big, and if so, |
| 706 | // overflow. |
| 707 | else if (numElementsWidth > sizeWidth && |
| 708 | numElementsWidth - sizeWidth > count.countLeadingZeros()) |
| 709 | hasAnyOverflow = true; |
| 710 | |
| 711 | // Okay, compute a count at the right width. |
| 712 | llvm::APInt adjustedCount = count.zextOrTrunc(sizeWidth); |
| 713 | |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 714 | // If there is a brace-initializer, we cannot allocate fewer elements than |
| 715 | // there are initializers. If we do, that's treated like an overflow. |
| 716 | if (adjustedCount.ult(minElements)) |
| 717 | hasAnyOverflow = true; |
| 718 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 719 | // Scale numElements by that. This might overflow, but we don't |
| 720 | // care because it only overflows if allocationSize does, too, and |
| 721 | // if that overflows then we shouldn't use this. |
| 722 | numElements = llvm::ConstantInt::get(CGF.SizeTy, |
| 723 | adjustedCount * arraySizeMultiplier); |
| 724 | |
| 725 | // Compute the size before cookie, and track whether it overflowed. |
| 726 | bool overflow; |
| 727 | llvm::APInt allocationSize |
| 728 | = adjustedCount.umul_ov(typeSizeMultiplier, overflow); |
| 729 | hasAnyOverflow |= overflow; |
| 730 | |
| 731 | // Add in the cookie, and check whether it's overflowed. |
| 732 | if (cookieSize != 0) { |
| 733 | // Save the current size without a cookie. This shouldn't be |
| 734 | // used if there was overflow. |
| 735 | sizeWithoutCookie = llvm::ConstantInt::get(CGF.SizeTy, allocationSize); |
| 736 | |
| 737 | allocationSize = allocationSize.uadd_ov(cookieSize, overflow); |
| 738 | hasAnyOverflow |= overflow; |
| 739 | } |
| 740 | |
| 741 | // On overflow, produce a -1 so operator new will fail. |
Aaron Ballman | 455f42c | 2014-08-28 17:24:14 +0000 | [diff] [blame] | 742 | if (hasAnyOverflow) { |
| 743 | size = llvm::Constant::getAllOnesValue(CGF.SizeTy); |
| 744 | } else { |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 745 | size = llvm::ConstantInt::get(CGF.SizeTy, allocationSize); |
Aaron Ballman | 455f42c | 2014-08-28 17:24:14 +0000 | [diff] [blame] | 746 | } |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 747 | |
| 748 | // Otherwise, we might need to use the overflow intrinsics. |
| 749 | } else { |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 750 | // There are up to five conditions we need to test for: |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 751 | // 1) if isSigned, we need to check whether numElements is negative; |
| 752 | // 2) if numElementsWidth > sizeWidth, we need to check whether |
| 753 | // numElements is larger than something representable in size_t; |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 754 | // 3) if minElements > 0, we need to check whether numElements is smaller |
| 755 | // than that. |
| 756 | // 4) we need to compute |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 757 | // sizeWithoutCookie := numElements * typeSizeMultiplier |
| 758 | // and check whether it overflows; and |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 759 | // 5) if we need a cookie, we need to compute |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 760 | // size := sizeWithoutCookie + cookieSize |
| 761 | // and check whether it overflows. |
| 762 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 763 | llvm::Value *hasOverflow = nullptr; |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 764 | |
| 765 | // If numElementsWidth > sizeWidth, then one way or another, we're |
| 766 | // going to have to do a comparison for (2), and this happens to |
| 767 | // take care of (1), too. |
| 768 | if (numElementsWidth > sizeWidth) { |
| 769 | llvm::APInt threshold(numElementsWidth, 1); |
| 770 | threshold <<= sizeWidth; |
| 771 | |
| 772 | llvm::Value *thresholdV |
| 773 | = llvm::ConstantInt::get(numElementsType, threshold); |
| 774 | |
| 775 | hasOverflow = CGF.Builder.CreateICmpUGE(numElements, thresholdV); |
| 776 | numElements = CGF.Builder.CreateTrunc(numElements, CGF.SizeTy); |
| 777 | |
| 778 | // Otherwise, if we're signed, we want to sext up to size_t. |
| 779 | } else if (isSigned) { |
| 780 | if (numElementsWidth < sizeWidth) |
| 781 | numElements = CGF.Builder.CreateSExt(numElements, CGF.SizeTy); |
| 782 | |
| 783 | // If there's a non-1 type size multiplier, then we can do the |
| 784 | // signedness check at the same time as we do the multiply |
| 785 | // because a negative number times anything will cause an |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 786 | // unsigned overflow. Otherwise, we have to do it here. But at least |
| 787 | // in this case, we can subsume the >= minElements check. |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 788 | if (typeSizeMultiplier == 1) |
| 789 | hasOverflow = CGF.Builder.CreateICmpSLT(numElements, |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 790 | llvm::ConstantInt::get(CGF.SizeTy, minElements)); |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 791 | |
| 792 | // Otherwise, zext up to size_t if necessary. |
| 793 | } else if (numElementsWidth < sizeWidth) { |
| 794 | numElements = CGF.Builder.CreateZExt(numElements, CGF.SizeTy); |
| 795 | } |
| 796 | |
| 797 | assert(numElements->getType() == CGF.SizeTy); |
| 798 | |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 799 | if (minElements) { |
| 800 | // Don't allow allocation of fewer elements than we have initializers. |
| 801 | if (!hasOverflow) { |
| 802 | hasOverflow = CGF.Builder.CreateICmpULT(numElements, |
| 803 | llvm::ConstantInt::get(CGF.SizeTy, minElements)); |
| 804 | } else if (numElementsWidth > sizeWidth) { |
| 805 | // The other existing overflow subsumes this check. |
| 806 | // We do an unsigned comparison, since any signed value < -1 is |
| 807 | // taken care of either above or below. |
| 808 | hasOverflow = CGF.Builder.CreateOr(hasOverflow, |
| 809 | CGF.Builder.CreateICmpULT(numElements, |
| 810 | llvm::ConstantInt::get(CGF.SizeTy, minElements))); |
| 811 | } |
| 812 | } |
| 813 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 814 | size = numElements; |
| 815 | |
| 816 | // Multiply by the type size if necessary. This multiplier |
| 817 | // includes all the factors for nested arrays. |
| 818 | // |
| 819 | // This step also causes numElements to be scaled up by the |
| 820 | // nested-array factor if necessary. Overflow on this computation |
| 821 | // can be ignored because the result shouldn't be used if |
| 822 | // allocation fails. |
| 823 | if (typeSizeMultiplier != 1) { |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 824 | llvm::Value *umul_with_overflow |
Benjamin Kramer | 8d375ce | 2011-07-14 17:45:50 +0000 | [diff] [blame] | 825 | = CGF.CGM.getIntrinsic(llvm::Intrinsic::umul_with_overflow, CGF.SizeTy); |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 826 | |
| 827 | llvm::Value *tsmV = |
| 828 | llvm::ConstantInt::get(CGF.SizeTy, typeSizeMultiplier); |
| 829 | llvm::Value *result = |
David Blaikie | 43f9bb7 | 2015-05-18 22:14:03 +0000 | [diff] [blame] | 830 | CGF.Builder.CreateCall(umul_with_overflow, {size, tsmV}); |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 831 | |
| 832 | llvm::Value *overflowed = CGF.Builder.CreateExtractValue(result, 1); |
| 833 | if (hasOverflow) |
| 834 | hasOverflow = CGF.Builder.CreateOr(hasOverflow, overflowed); |
| 835 | else |
| 836 | hasOverflow = overflowed; |
| 837 | |
| 838 | size = CGF.Builder.CreateExtractValue(result, 0); |
| 839 | |
| 840 | // Also scale up numElements by the array size multiplier. |
| 841 | if (arraySizeMultiplier != 1) { |
| 842 | // If the base element type size is 1, then we can re-use the |
| 843 | // multiply we just did. |
| 844 | if (typeSize.isOne()) { |
| 845 | assert(arraySizeMultiplier == typeSizeMultiplier); |
| 846 | numElements = size; |
| 847 | |
| 848 | // Otherwise we need a separate multiply. |
| 849 | } else { |
| 850 | llvm::Value *asmV = |
| 851 | llvm::ConstantInt::get(CGF.SizeTy, arraySizeMultiplier); |
| 852 | numElements = CGF.Builder.CreateMul(numElements, asmV); |
| 853 | } |
| 854 | } |
| 855 | } else { |
| 856 | // numElements doesn't need to be scaled. |
| 857 | assert(arraySizeMultiplier == 1); |
Chris Lattner | 32ac583 | 2010-07-20 21:55:52 +0000 | [diff] [blame] | 858 | } |
| 859 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 860 | // Add in the cookie size if necessary. |
| 861 | if (cookieSize != 0) { |
| 862 | sizeWithoutCookie = size; |
| 863 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 864 | llvm::Value *uadd_with_overflow |
Benjamin Kramer | 8d375ce | 2011-07-14 17:45:50 +0000 | [diff] [blame] | 865 | = CGF.CGM.getIntrinsic(llvm::Intrinsic::uadd_with_overflow, CGF.SizeTy); |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 866 | |
| 867 | llvm::Value *cookieSizeV = llvm::ConstantInt::get(CGF.SizeTy, cookieSize); |
| 868 | llvm::Value *result = |
David Blaikie | 43f9bb7 | 2015-05-18 22:14:03 +0000 | [diff] [blame] | 869 | CGF.Builder.CreateCall(uadd_with_overflow, {size, cookieSizeV}); |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 870 | |
| 871 | llvm::Value *overflowed = CGF.Builder.CreateExtractValue(result, 1); |
| 872 | if (hasOverflow) |
| 873 | hasOverflow = CGF.Builder.CreateOr(hasOverflow, overflowed); |
| 874 | else |
| 875 | hasOverflow = overflowed; |
| 876 | |
| 877 | size = CGF.Builder.CreateExtractValue(result, 0); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 878 | } |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 879 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 880 | // If we had any possibility of dynamic overflow, make a select to |
| 881 | // overwrite 'size' with an all-ones value, which should cause |
| 882 | // operator new to throw. |
| 883 | if (hasOverflow) |
Aaron Ballman | 455f42c | 2014-08-28 17:24:14 +0000 | [diff] [blame] | 884 | size = CGF.Builder.CreateSelect(hasOverflow, |
| 885 | llvm::Constant::getAllOnesValue(CGF.SizeTy), |
| 886 | size); |
Chris Lattner | 32ac583 | 2010-07-20 21:55:52 +0000 | [diff] [blame] | 887 | } |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 888 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 889 | if (cookieSize == 0) |
| 890 | sizeWithoutCookie = size; |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 891 | else |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 892 | assert(sizeWithoutCookie && "didn't set sizeWithoutCookie?"); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 893 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 894 | return size; |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 895 | } |
| 896 | |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 897 | static void StoreAnyExprIntoOneUnit(CodeGenFunction &CGF, const Expr *Init, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 898 | QualType AllocType, Address NewPtr) { |
Richard Smith | 1c96bc5 | 2013-12-11 01:40:16 +0000 | [diff] [blame] | 899 | // FIXME: Refactor with EmitExprAsInit. |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 900 | switch (CGF.getEvaluationKind(AllocType)) { |
| 901 | case TEK_Scalar: |
David Blaikie | a2c1124 | 2014-12-10 19:04:09 +0000 | [diff] [blame] | 902 | CGF.EmitScalarInit(Init, nullptr, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 903 | CGF.MakeAddrLValue(NewPtr, AllocType), false); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 904 | return; |
| 905 | case TEK_Complex: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 906 | CGF.EmitComplexExprIntoLValue(Init, CGF.MakeAddrLValue(NewPtr, AllocType), |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 907 | /*isInit*/ true); |
| 908 | return; |
| 909 | case TEK_Aggregate: { |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 910 | AggValueSlot Slot |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 911 | = AggValueSlot::forAddr(NewPtr, AllocType.getQualifiers(), |
John McCall | 8d6fc95 | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 912 | AggValueSlot::IsDestructed, |
John McCall | 46759f4 | 2011-08-26 07:31:35 +0000 | [diff] [blame] | 913 | AggValueSlot::DoesNotNeedGCBarriers, |
Chad Rosier | 615ed1a | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 914 | AggValueSlot::IsNotAliased); |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 915 | CGF.EmitAggExpr(Init, Slot); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 916 | return; |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 917 | } |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 918 | } |
| 919 | llvm_unreachable("bad evaluation kind"); |
Fariborz Jahanian | d5202e0 | 2010-06-25 18:26:07 +0000 | [diff] [blame] | 920 | } |
| 921 | |
David Blaikie | fb901c7a | 2015-04-04 15:12:29 +0000 | [diff] [blame] | 922 | void CodeGenFunction::EmitNewArrayInitializer( |
| 923 | const CXXNewExpr *E, QualType ElementType, llvm::Type *ElementTy, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 924 | Address BeginPtr, llvm::Value *NumElements, |
David Blaikie | fb901c7a | 2015-04-04 15:12:29 +0000 | [diff] [blame] | 925 | llvm::Value *AllocSizeWithoutCookie) { |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 926 | // If we have a type with trivial initialization and no initializer, |
| 927 | // there's nothing to do. |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 928 | if (!E->hasInitializer()) |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 929 | return; |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 930 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 931 | Address CurPtr = BeginPtr; |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 932 | |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 933 | unsigned InitListElements = 0; |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 934 | |
| 935 | const Expr *Init = E->getInitializer(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 936 | Address EndOfInit = Address::invalid(); |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 937 | QualType::DestructionKind DtorKind = ElementType.isDestructedType(); |
| 938 | EHScopeStack::stable_iterator Cleanup; |
| 939 | llvm::Instruction *CleanupDominator = nullptr; |
Richard Smith | 1c96bc5 | 2013-12-11 01:40:16 +0000 | [diff] [blame] | 940 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 941 | CharUnits ElementSize = getContext().getTypeSizeInChars(ElementType); |
| 942 | CharUnits ElementAlign = |
| 943 | BeginPtr.getAlignment().alignmentOfArrayElement(ElementSize); |
| 944 | |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 945 | // Attempt to perform zero-initialization using memset. |
| 946 | auto TryMemsetInitialization = [&]() -> bool { |
| 947 | // FIXME: If the type is a pointer-to-data-member under the Itanium ABI, |
| 948 | // we can initialize with a memset to -1. |
| 949 | if (!CGM.getTypes().isZeroInitializable(ElementType)) |
| 950 | return false; |
| 951 | |
| 952 | // Optimization: since zero initialization will just set the memory |
| 953 | // to all zeroes, generate a single memset to do it in one shot. |
| 954 | |
| 955 | // Subtract out the size of any elements we've already initialized. |
| 956 | auto *RemainingSize = AllocSizeWithoutCookie; |
| 957 | if (InitListElements) { |
| 958 | // We know this can't overflow; we check this when doing the allocation. |
| 959 | auto *InitializedSize = llvm::ConstantInt::get( |
| 960 | RemainingSize->getType(), |
| 961 | getContext().getTypeSizeInChars(ElementType).getQuantity() * |
| 962 | InitListElements); |
| 963 | RemainingSize = Builder.CreateSub(RemainingSize, InitializedSize); |
| 964 | } |
| 965 | |
| 966 | // Create the memset. |
| 967 | Builder.CreateMemSet(CurPtr, Builder.getInt8(0), RemainingSize, false); |
| 968 | return true; |
| 969 | }; |
| 970 | |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 971 | // If the initializer is an initializer list, first do the explicit elements. |
| 972 | if (const InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 973 | // Initializing from a (braced) string literal is a special case; the init |
| 974 | // list element does not initialize a (single) array element. |
| 975 | if (ILE->isStringLiteralInit()) { |
| 976 | // Initialize the initial portion of length equal to that of the string |
| 977 | // literal. The allocation must be for at least this much; we emitted a |
| 978 | // check for that earlier. |
| 979 | AggValueSlot Slot = |
| 980 | AggValueSlot::forAddr(CurPtr, ElementType.getQualifiers(), |
| 981 | AggValueSlot::IsDestructed, |
| 982 | AggValueSlot::DoesNotNeedGCBarriers, |
| 983 | AggValueSlot::IsNotAliased); |
| 984 | EmitAggExpr(ILE->getInit(0), Slot); |
| 985 | |
| 986 | // Move past these elements. |
| 987 | InitListElements = |
| 988 | cast<ConstantArrayType>(ILE->getType()->getAsArrayTypeUnsafe()) |
| 989 | ->getSize().getZExtValue(); |
| 990 | CurPtr = |
| 991 | Address(Builder.CreateInBoundsGEP(CurPtr.getPointer(), |
| 992 | Builder.getSize(InitListElements), |
| 993 | "string.init.end"), |
| 994 | CurPtr.getAlignment().alignmentAtOffset(InitListElements * |
| 995 | ElementSize)); |
| 996 | |
| 997 | // Zero out the rest, if any remain. |
| 998 | llvm::ConstantInt *ConstNum = dyn_cast<llvm::ConstantInt>(NumElements); |
| 999 | if (!ConstNum || !ConstNum->equalsInt(InitListElements)) { |
| 1000 | bool OK = TryMemsetInitialization(); |
| 1001 | (void)OK; |
| 1002 | assert(OK && "couldn't memset character type?"); |
| 1003 | } |
| 1004 | return; |
| 1005 | } |
| 1006 | |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1007 | InitListElements = ILE->getNumInits(); |
Chad Rosier | f62290a | 2012-02-24 00:13:55 +0000 | [diff] [blame] | 1008 | |
Richard Smith | 1c96bc5 | 2013-12-11 01:40:16 +0000 | [diff] [blame] | 1009 | // If this is a multi-dimensional array new, we will initialize multiple |
| 1010 | // elements with each init list element. |
| 1011 | QualType AllocType = E->getAllocatedType(); |
| 1012 | if (const ConstantArrayType *CAT = dyn_cast_or_null<ConstantArrayType>( |
| 1013 | AllocType->getAsArrayTypeUnsafe())) { |
David Blaikie | fb901c7a | 2015-04-04 15:12:29 +0000 | [diff] [blame] | 1014 | ElementTy = ConvertTypeForMem(AllocType); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1015 | CurPtr = Builder.CreateElementBitCast(CurPtr, ElementTy); |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1016 | InitListElements *= getContext().getConstantArrayElementCount(CAT); |
Richard Smith | 1c96bc5 | 2013-12-11 01:40:16 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1019 | // Enter a partial-destruction Cleanup if necessary. |
| 1020 | if (needsEHCleanup(DtorKind)) { |
| 1021 | // In principle we could tell the Cleanup where we are more |
Chad Rosier | f62290a | 2012-02-24 00:13:55 +0000 | [diff] [blame] | 1022 | // directly, but the control flow can get so varied here that it |
| 1023 | // would actually be quite complex. Therefore we go through an |
| 1024 | // alloca. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1025 | EndOfInit = CreateTempAlloca(BeginPtr.getType(), getPointerAlign(), |
| 1026 | "array.init.end"); |
| 1027 | CleanupDominator = Builder.CreateStore(BeginPtr.getPointer(), EndOfInit); |
| 1028 | pushIrregularPartialArrayCleanup(BeginPtr.getPointer(), EndOfInit, |
| 1029 | ElementType, ElementAlign, |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1030 | getDestroyer(DtorKind)); |
| 1031 | Cleanup = EHStack.stable_begin(); |
Chad Rosier | f62290a | 2012-02-24 00:13:55 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1034 | CharUnits StartAlign = CurPtr.getAlignment(); |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 1035 | for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i) { |
Chad Rosier | f62290a | 2012-02-24 00:13:55 +0000 | [diff] [blame] | 1036 | // Tell the cleanup that it needs to destroy up to this |
| 1037 | // element. TODO: some of these stores can be trivially |
| 1038 | // observed to be unnecessary. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1039 | if (EndOfInit.isValid()) { |
| 1040 | auto FinishedPtr = |
| 1041 | Builder.CreateBitCast(CurPtr.getPointer(), BeginPtr.getType()); |
| 1042 | Builder.CreateStore(FinishedPtr, EndOfInit); |
| 1043 | } |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1044 | // FIXME: If the last initializer is an incomplete initializer list for |
| 1045 | // an array, and we have an array filler, we can fold together the two |
| 1046 | // initialization loops. |
Richard Smith | 1c96bc5 | 2013-12-11 01:40:16 +0000 | [diff] [blame] | 1047 | StoreAnyExprIntoOneUnit(*this, ILE->getInit(i), |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1048 | ILE->getInit(i)->getType(), CurPtr); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1049 | CurPtr = Address(Builder.CreateInBoundsGEP(CurPtr.getPointer(), |
| 1050 | Builder.getSize(1), |
| 1051 | "array.exp.next"), |
| 1052 | StartAlign.alignmentAtOffset((i + 1) * ElementSize)); |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | // The remaining elements are filled with the array filler expression. |
| 1056 | Init = ILE->getArrayFiller(); |
Richard Smith | 1c96bc5 | 2013-12-11 01:40:16 +0000 | [diff] [blame] | 1057 | |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1058 | // Extract the initializer for the individual array elements by pulling |
| 1059 | // out the array filler from all the nested initializer lists. This avoids |
| 1060 | // generating a nested loop for the initialization. |
| 1061 | while (Init && Init->getType()->isConstantArrayType()) { |
| 1062 | auto *SubILE = dyn_cast<InitListExpr>(Init); |
| 1063 | if (!SubILE) |
| 1064 | break; |
| 1065 | assert(SubILE->getNumInits() == 0 && "explicit inits in array filler?"); |
| 1066 | Init = SubILE->getArrayFiller(); |
| 1067 | } |
| 1068 | |
| 1069 | // Switch back to initializing one base element at a time. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1070 | CurPtr = Builder.CreateBitCast(CurPtr, BeginPtr.getType()); |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1073 | // If all elements have already been initialized, skip any further |
| 1074 | // initialization. |
| 1075 | llvm::ConstantInt *ConstNum = dyn_cast<llvm::ConstantInt>(NumElements); |
| 1076 | if (ConstNum && ConstNum->getZExtValue() <= InitListElements) { |
| 1077 | // If there was a Cleanup, deactivate it. |
| 1078 | if (CleanupDominator) |
| 1079 | DeactivateCleanupBlock(Cleanup, CleanupDominator); |
| 1080 | return; |
| 1081 | } |
| 1082 | |
| 1083 | assert(Init && "have trailing elements to initialize but no initializer"); |
| 1084 | |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1085 | // If this is a constructor call, try to optimize it out, and failing that |
| 1086 | // emit a single loop to initialize all remaining elements. |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1087 | if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) { |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1088 | CXXConstructorDecl *Ctor = CCE->getConstructor(); |
| 1089 | if (Ctor->isTrivial()) { |
| 1090 | // If new expression did not specify value-initialization, then there |
| 1091 | // is no initialization. |
| 1092 | if (!CCE->requiresZeroInitialization() || Ctor->getParent()->isEmpty()) |
| 1093 | return; |
| 1094 | |
| 1095 | if (TryMemsetInitialization()) |
| 1096 | return; |
| 1097 | } |
| 1098 | |
| 1099 | // Store the new Cleanup position for irregular Cleanups. |
| 1100 | // |
| 1101 | // FIXME: Share this cleanup with the constructor call emission rather than |
| 1102 | // having it create a cleanup of its own. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1103 | if (EndOfInit.isValid()) |
| 1104 | Builder.CreateStore(CurPtr.getPointer(), EndOfInit); |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1105 | |
| 1106 | // Emit a constructor call loop to initialize the remaining elements. |
| 1107 | if (InitListElements) |
| 1108 | NumElements = Builder.CreateSub( |
| 1109 | NumElements, |
| 1110 | llvm::ConstantInt::get(NumElements->getType(), InitListElements)); |
Alexey Samsonov | 70b9c01 | 2014-08-21 20:26:47 +0000 | [diff] [blame] | 1111 | EmitCXXAggrConstructorCall(Ctor, NumElements, CurPtr, CCE, |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1112 | CCE->requiresZeroInitialization()); |
Chandler Carruth | e6c980c | 2014-05-03 09:16:57 +0000 | [diff] [blame] | 1113 | return; |
| 1114 | } |
| 1115 | |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1116 | // If this is value-initialization, we can usually use memset. |
| 1117 | ImplicitValueInitExpr IVIE(ElementType); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1118 | if (isa<ImplicitValueInitExpr>(Init)) { |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1119 | if (TryMemsetInitialization()) |
| 1120 | return; |
| 1121 | |
| 1122 | // Switch to an ImplicitValueInitExpr for the element type. This handles |
| 1123 | // only one case: multidimensional array new of pointers to members. In |
| 1124 | // all other cases, we already have an initializer for the array element. |
| 1125 | Init = &IVIE; |
| 1126 | } |
| 1127 | |
| 1128 | // At this point we should have found an initializer for the individual |
| 1129 | // elements of the array. |
| 1130 | assert(getContext().hasSameUnqualifiedType(ElementType, Init->getType()) && |
| 1131 | "got wrong type of element to initialize"); |
| 1132 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1133 | // If we have an empty initializer list, we can usually use memset. |
| 1134 | if (auto *ILE = dyn_cast<InitListExpr>(Init)) |
| 1135 | if (ILE->getNumInits() == 0 && TryMemsetInitialization()) |
| 1136 | return; |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1137 | |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1138 | // If we have a struct whose every field is value-initialized, we can |
| 1139 | // usually use memset. |
| 1140 | if (auto *ILE = dyn_cast<InitListExpr>(Init)) { |
| 1141 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
| 1142 | if (RType->getDecl()->isStruct()) { |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 1143 | unsigned NumElements = 0; |
| 1144 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RType->getDecl())) |
| 1145 | NumElements = CXXRD->getNumBases(); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1146 | for (auto *Field : RType->getDecl()->fields()) |
| 1147 | if (!Field->isUnnamedBitfield()) |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 1148 | ++NumElements; |
| 1149 | // FIXME: Recurse into nested InitListExprs. |
| 1150 | if (ILE->getNumInits() == NumElements) |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1151 | for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i) |
| 1152 | if (!isa<ImplicitValueInitExpr>(ILE->getInit(i))) |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 1153 | --NumElements; |
| 1154 | if (ILE->getNumInits() == NumElements && TryMemsetInitialization()) |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1155 | return; |
| 1156 | } |
| 1157 | } |
| 1158 | } |
| 1159 | |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1160 | // Create the loop blocks. |
| 1161 | llvm::BasicBlock *EntryBB = Builder.GetInsertBlock(); |
| 1162 | llvm::BasicBlock *LoopBB = createBasicBlock("new.loop"); |
| 1163 | llvm::BasicBlock *ContBB = createBasicBlock("new.loop.end"); |
| 1164 | |
| 1165 | // Find the end of the array, hoisted out of the loop. |
| 1166 | llvm::Value *EndPtr = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1167 | Builder.CreateInBoundsGEP(BeginPtr.getPointer(), NumElements, "array.end"); |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 1168 | |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 1169 | // If the number of elements isn't constant, we have to now check if there is |
| 1170 | // anything left to initialize. |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1171 | if (!ConstNum) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1172 | llvm::Value *IsEmpty = |
| 1173 | Builder.CreateICmpEQ(CurPtr.getPointer(), EndPtr, "array.isempty"); |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1174 | Builder.CreateCondBr(IsEmpty, ContBB, LoopBB); |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | // Enter the loop. |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1178 | EmitBlock(LoopBB); |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 1179 | |
| 1180 | // Set up the current-element phi. |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1181 | llvm::PHINode *CurPtrPhi = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1182 | Builder.CreatePHI(CurPtr.getType(), 2, "array.cur"); |
| 1183 | CurPtrPhi->addIncoming(CurPtr.getPointer(), EntryBB); |
| 1184 | |
| 1185 | CurPtr = Address(CurPtrPhi, ElementAlign); |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 1186 | |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1187 | // Store the new Cleanup position for irregular Cleanups. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1188 | if (EndOfInit.isValid()) |
| 1189 | Builder.CreateStore(CurPtr.getPointer(), EndOfInit); |
Chad Rosier | f62290a | 2012-02-24 00:13:55 +0000 | [diff] [blame] | 1190 | |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1191 | // Enter a partial-destruction Cleanup if necessary. |
| 1192 | if (!CleanupDominator && needsEHCleanup(DtorKind)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1193 | pushRegularPartialArrayCleanup(BeginPtr.getPointer(), CurPtr.getPointer(), |
| 1194 | ElementType, ElementAlign, |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1195 | getDestroyer(DtorKind)); |
| 1196 | Cleanup = EHStack.stable_begin(); |
| 1197 | CleanupDominator = Builder.CreateUnreachable(); |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | // Emit the initializer into this element. |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1201 | StoreAnyExprIntoOneUnit(*this, Init, Init->getType(), CurPtr); |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 1202 | |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1203 | // Leave the Cleanup if we entered one. |
| 1204 | if (CleanupDominator) { |
| 1205 | DeactivateCleanupBlock(Cleanup, CleanupDominator); |
| 1206 | CleanupDominator->eraseFromParent(); |
John McCall | f4beacd | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 1207 | } |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 1208 | |
Faisal Vali | 57ae056 | 2013-12-14 00:40:05 +0000 | [diff] [blame] | 1209 | // Advance to the next element by adjusting the pointer type as necessary. |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1210 | llvm::Value *NextPtr = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1211 | Builder.CreateConstInBoundsGEP1_32(ElementTy, CurPtr.getPointer(), 1, |
| 1212 | "array.next"); |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1213 | |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 1214 | // Check whether we've gotten to the end of the array and, if so, |
| 1215 | // exit the loop. |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1216 | llvm::Value *IsEnd = Builder.CreateICmpEQ(NextPtr, EndPtr, "array.atend"); |
| 1217 | Builder.CreateCondBr(IsEnd, ContBB, LoopBB); |
| 1218 | CurPtrPhi->addIncoming(NextPtr, Builder.GetInsertBlock()); |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 1219 | |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1220 | EmitBlock(ContBB); |
Fariborz Jahanian | d5202e0 | 2010-06-25 18:26:07 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 1223 | static void EmitNewInitializer(CodeGenFunction &CGF, const CXXNewExpr *E, |
David Blaikie | fb901c7a | 2015-04-04 15:12:29 +0000 | [diff] [blame] | 1224 | QualType ElementType, llvm::Type *ElementTy, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1225 | Address NewPtr, llvm::Value *NumElements, |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 1226 | llvm::Value *AllocSizeWithoutCookie) { |
David Blaikie | 9b47966 | 2015-01-25 01:19:10 +0000 | [diff] [blame] | 1227 | ApplyDebugLocation DL(CGF, E); |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1228 | if (E->isArray()) |
David Blaikie | fb901c7a | 2015-04-04 15:12:29 +0000 | [diff] [blame] | 1229 | CGF.EmitNewArrayInitializer(E, ElementType, ElementTy, NewPtr, NumElements, |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1230 | AllocSizeWithoutCookie); |
| 1231 | else if (const Expr *Init = E->getInitializer()) |
David Blaikie | 66e4197 | 2015-01-14 07:38:27 +0000 | [diff] [blame] | 1232 | StoreAnyExprIntoOneUnit(CGF, Init, E->getAllocatedType(), NewPtr); |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 1235 | /// Emit a call to an operator new or operator delete function, as implicitly |
| 1236 | /// created by new-expressions and delete-expressions. |
| 1237 | static RValue EmitNewDeleteCall(CodeGenFunction &CGF, |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 1238 | const FunctionDecl *CalleeDecl, |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 1239 | const FunctionProtoType *CalleeType, |
| 1240 | const CallArgList &Args) { |
| 1241 | llvm::Instruction *CallOrInvoke; |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 1242 | llvm::Constant *CalleePtr = CGF.CGM.GetAddrOfFunction(CalleeDecl); |
| 1243 | CGCallee Callee = CGCallee::forDirect(CalleePtr, CalleeDecl); |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 1244 | RValue RV = |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 1245 | CGF.EmitCall(CGF.CGM.getTypes().arrangeFreeFunctionCall( |
| 1246 | Args, CalleeType, /*chainCall=*/false), |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 1247 | Callee, ReturnValueSlot(), Args, &CallOrInvoke); |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 1248 | |
| 1249 | /// C++1y [expr.new]p10: |
| 1250 | /// [In a new-expression,] an implementation is allowed to omit a call |
| 1251 | /// to a replaceable global allocation function. |
| 1252 | /// |
| 1253 | /// We model such elidable calls with the 'builtin' attribute. |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 1254 | llvm::Function *Fn = dyn_cast<llvm::Function>(CalleePtr); |
| 1255 | if (CalleeDecl->isReplaceableGlobalAllocationFunction() && |
Rafael Espindola | 6956d58 | 2013-10-22 14:23:09 +0000 | [diff] [blame] | 1256 | Fn && Fn->hasFnAttribute(llvm::Attribute::NoBuiltin)) { |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 1257 | // FIXME: Add addAttribute to CallSite. |
| 1258 | if (llvm::CallInst *CI = dyn_cast<llvm::CallInst>(CallOrInvoke)) |
| 1259 | CI->addAttribute(llvm::AttributeSet::FunctionIndex, |
| 1260 | llvm::Attribute::Builtin); |
| 1261 | else if (llvm::InvokeInst *II = dyn_cast<llvm::InvokeInst>(CallOrInvoke)) |
| 1262 | II->addAttribute(llvm::AttributeSet::FunctionIndex, |
| 1263 | llvm::Attribute::Builtin); |
| 1264 | else |
| 1265 | llvm_unreachable("unexpected kind of call instruction"); |
| 1266 | } |
| 1267 | |
| 1268 | return RV; |
| 1269 | } |
| 1270 | |
Richard Smith | 760520b | 2014-06-03 23:27:44 +0000 | [diff] [blame] | 1271 | RValue CodeGenFunction::EmitBuiltinNewDeleteCall(const FunctionProtoType *Type, |
| 1272 | const Expr *Arg, |
| 1273 | bool IsDelete) { |
| 1274 | CallArgList Args; |
| 1275 | const Stmt *ArgS = Arg; |
David Blaikie | f05779e | 2015-07-21 18:37:18 +0000 | [diff] [blame] | 1276 | EmitCallArgs(Args, *Type->param_type_begin(), llvm::makeArrayRef(ArgS)); |
Richard Smith | 760520b | 2014-06-03 23:27:44 +0000 | [diff] [blame] | 1277 | // Find the allocation or deallocation function that we're calling. |
| 1278 | ASTContext &Ctx = getContext(); |
| 1279 | DeclarationName Name = Ctx.DeclarationNames |
| 1280 | .getCXXOperatorName(IsDelete ? OO_Delete : OO_New); |
| 1281 | for (auto *Decl : Ctx.getTranslationUnitDecl()->lookup(Name)) |
Richard Smith | 599bed7 | 2014-06-05 00:43:02 +0000 | [diff] [blame] | 1282 | if (auto *FD = dyn_cast<FunctionDecl>(Decl)) |
| 1283 | if (Ctx.hasSameType(FD->getType(), QualType(Type, 0))) |
| 1284 | return EmitNewDeleteCall(*this, cast<FunctionDecl>(Decl), Type, Args); |
Richard Smith | 760520b | 2014-06-03 23:27:44 +0000 | [diff] [blame] | 1285 | llvm_unreachable("predeclared global operator new/delete is missing"); |
| 1286 | } |
| 1287 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1288 | static std::pair<bool, bool> |
| 1289 | shouldPassSizeAndAlignToUsualDelete(const FunctionProtoType *FPT) { |
| 1290 | auto AI = FPT->param_type_begin(), AE = FPT->param_type_end(); |
Richard Smith | 189e52f | 2016-10-10 06:42:31 +0000 | [diff] [blame] | 1291 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1292 | // The first argument is always a void*. |
| 1293 | ++AI; |
| 1294 | |
| 1295 | // Figure out what other parameters we should be implicitly passing. |
| 1296 | bool PassSize = false; |
| 1297 | bool PassAlignment = false; |
| 1298 | |
| 1299 | if (AI != AE && (*AI)->isIntegerType()) { |
| 1300 | PassSize = true; |
| 1301 | ++AI; |
| 1302 | } |
| 1303 | |
| 1304 | if (AI != AE && (*AI)->isAlignValT()) { |
| 1305 | PassAlignment = true; |
| 1306 | ++AI; |
| 1307 | } |
| 1308 | |
| 1309 | assert(AI == AE && "unexpected usual deallocation function parameter"); |
| 1310 | return {PassSize, PassAlignment}; |
| 1311 | } |
| 1312 | |
| 1313 | namespace { |
| 1314 | /// A cleanup to call the given 'operator delete' function upon abnormal |
| 1315 | /// exit from a new expression. Templated on a traits type that deals with |
| 1316 | /// ensuring that the arguments dominate the cleanup if necessary. |
| 1317 | template<typename Traits> |
| 1318 | class CallDeleteDuringNew final : public EHScopeStack::Cleanup { |
| 1319 | /// Type used to hold llvm::Value*s. |
| 1320 | typedef typename Traits::ValueTy ValueTy; |
| 1321 | /// Type used to hold RValues. |
| 1322 | typedef typename Traits::RValueTy RValueTy; |
| 1323 | struct PlacementArg { |
| 1324 | RValueTy ArgValue; |
| 1325 | QualType ArgType; |
| 1326 | }; |
| 1327 | |
| 1328 | unsigned NumPlacementArgs : 31; |
| 1329 | unsigned PassAlignmentToPlacementDelete : 1; |
| 1330 | const FunctionDecl *OperatorDelete; |
| 1331 | ValueTy Ptr; |
| 1332 | ValueTy AllocSize; |
| 1333 | CharUnits AllocAlign; |
| 1334 | |
| 1335 | PlacementArg *getPlacementArgs() { |
| 1336 | return reinterpret_cast<PlacementArg *>(this + 1); |
| 1337 | } |
Daniel Jasper | e9abe64 | 2016-10-10 14:13:55 +0000 | [diff] [blame] | 1338 | |
| 1339 | public: |
| 1340 | static size_t getExtraSize(size_t NumPlacementArgs) { |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1341 | return NumPlacementArgs * sizeof(PlacementArg); |
Daniel Jasper | e9abe64 | 2016-10-10 14:13:55 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | CallDeleteDuringNew(size_t NumPlacementArgs, |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1345 | const FunctionDecl *OperatorDelete, ValueTy Ptr, |
| 1346 | ValueTy AllocSize, bool PassAlignmentToPlacementDelete, |
| 1347 | CharUnits AllocAlign) |
| 1348 | : NumPlacementArgs(NumPlacementArgs), |
| 1349 | PassAlignmentToPlacementDelete(PassAlignmentToPlacementDelete), |
| 1350 | OperatorDelete(OperatorDelete), Ptr(Ptr), AllocSize(AllocSize), |
| 1351 | AllocAlign(AllocAlign) {} |
Daniel Jasper | e9abe64 | 2016-10-10 14:13:55 +0000 | [diff] [blame] | 1352 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1353 | void setPlacementArg(unsigned I, RValueTy Arg, QualType Type) { |
Daniel Jasper | e9abe64 | 2016-10-10 14:13:55 +0000 | [diff] [blame] | 1354 | assert(I < NumPlacementArgs && "index out of range"); |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1355 | getPlacementArgs()[I] = {Arg, Type}; |
Daniel Jasper | e9abe64 | 2016-10-10 14:13:55 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1359 | const FunctionProtoType *FPT = |
| 1360 | OperatorDelete->getType()->getAs<FunctionProtoType>(); |
Daniel Jasper | e9abe64 | 2016-10-10 14:13:55 +0000 | [diff] [blame] | 1361 | CallArgList DeleteArgs; |
| 1362 | |
| 1363 | // The first argument is always a void*. |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1364 | DeleteArgs.add(Traits::get(CGF, Ptr), FPT->getParamType(0)); |
Daniel Jasper | e9abe64 | 2016-10-10 14:13:55 +0000 | [diff] [blame] | 1365 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1366 | // Figure out what other parameters we should be implicitly passing. |
| 1367 | bool PassSize = false; |
| 1368 | bool PassAlignment = false; |
| 1369 | if (NumPlacementArgs) { |
| 1370 | // A placement deallocation function is implicitly passed an alignment |
| 1371 | // if the placement allocation function was, but is never passed a size. |
| 1372 | PassAlignment = PassAlignmentToPlacementDelete; |
| 1373 | } else { |
| 1374 | // For a non-placement new-expression, 'operator delete' can take a |
| 1375 | // size and/or an alignment if it has the right parameters. |
| 1376 | std::tie(PassSize, PassAlignment) = |
| 1377 | shouldPassSizeAndAlignToUsualDelete(FPT); |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1380 | // The second argument can be a std::size_t (for non-placement delete). |
| 1381 | if (PassSize) |
| 1382 | DeleteArgs.add(Traits::get(CGF, AllocSize), |
| 1383 | CGF.getContext().getSizeType()); |
| 1384 | |
| 1385 | // The next (second or third) argument can be a std::align_val_t, which |
| 1386 | // is an enum whose underlying type is std::size_t. |
| 1387 | // FIXME: Use the right type as the parameter type. Note that in a call |
| 1388 | // to operator delete(size_t, ...), we may not have it available. |
| 1389 | if (PassAlignment) |
| 1390 | DeleteArgs.add(RValue::get(llvm::ConstantInt::get( |
| 1391 | CGF.SizeTy, AllocAlign.getQuantity())), |
| 1392 | CGF.getContext().getSizeType()); |
| 1393 | |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1394 | // Pass the rest of the arguments, which must match exactly. |
| 1395 | for (unsigned I = 0; I != NumPlacementArgs; ++I) { |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1396 | auto Arg = getPlacementArgs()[I]; |
| 1397 | DeleteArgs.add(Traits::get(CGF, Arg.ArgValue), Arg.ArgType); |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | // Call 'operator delete'. |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 1401 | EmitNewDeleteCall(CGF, OperatorDelete, FPT, DeleteArgs); |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1402 | } |
| 1403 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1404 | } |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1405 | |
| 1406 | /// Enter a cleanup to call 'operator delete' if the initializer in a |
| 1407 | /// new-expression throws. |
| 1408 | static void EnterNewDeleteCleanup(CodeGenFunction &CGF, |
| 1409 | const CXXNewExpr *E, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1410 | Address NewPtr, |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1411 | llvm::Value *AllocSize, |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1412 | CharUnits AllocAlign, |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1413 | const CallArgList &NewArgs) { |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1414 | unsigned NumNonPlacementArgs = E->passAlignment() ? 2 : 1; |
| 1415 | |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1416 | // If we're not inside a conditional branch, then the cleanup will |
| 1417 | // dominate and we can do the easier (and more efficient) thing. |
| 1418 | if (!CGF.isInConditionalBranch()) { |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1419 | struct DirectCleanupTraits { |
| 1420 | typedef llvm::Value *ValueTy; |
| 1421 | typedef RValue RValueTy; |
| 1422 | static RValue get(CodeGenFunction &, ValueTy V) { return RValue::get(V); } |
| 1423 | static RValue get(CodeGenFunction &, RValueTy V) { return V; } |
| 1424 | }; |
| 1425 | |
| 1426 | typedef CallDeleteDuringNew<DirectCleanupTraits> DirectCleanup; |
| 1427 | |
| 1428 | DirectCleanup *Cleanup = CGF.EHStack |
| 1429 | .pushCleanupWithExtra<DirectCleanup>(EHCleanup, |
| 1430 | E->getNumPlacementArgs(), |
| 1431 | E->getOperatorDelete(), |
| 1432 | NewPtr.getPointer(), |
| 1433 | AllocSize, |
| 1434 | E->passAlignment(), |
| 1435 | AllocAlign); |
| 1436 | for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I) { |
| 1437 | auto &Arg = NewArgs[I + NumNonPlacementArgs]; |
| 1438 | Cleanup->setPlacementArg(I, Arg.RV, Arg.Ty); |
| 1439 | } |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1440 | |
| 1441 | return; |
| 1442 | } |
| 1443 | |
| 1444 | // Otherwise, we need to save all this stuff. |
John McCall | cb5f77f | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 1445 | DominatingValue<RValue>::saved_type SavedNewPtr = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1446 | DominatingValue<RValue>::save(CGF, RValue::get(NewPtr.getPointer())); |
John McCall | cb5f77f | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 1447 | DominatingValue<RValue>::saved_type SavedAllocSize = |
| 1448 | DominatingValue<RValue>::save(CGF, RValue::get(AllocSize)); |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1449 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1450 | struct ConditionalCleanupTraits { |
| 1451 | typedef DominatingValue<RValue>::saved_type ValueTy; |
| 1452 | typedef DominatingValue<RValue>::saved_type RValueTy; |
| 1453 | static RValue get(CodeGenFunction &CGF, ValueTy V) { |
| 1454 | return V.restore(CGF); |
| 1455 | } |
| 1456 | }; |
| 1457 | typedef CallDeleteDuringNew<ConditionalCleanupTraits> ConditionalCleanup; |
| 1458 | |
| 1459 | ConditionalCleanup *Cleanup = CGF.EHStack |
| 1460 | .pushCleanupWithExtra<ConditionalCleanup>(EHCleanup, |
| 1461 | E->getNumPlacementArgs(), |
| 1462 | E->getOperatorDelete(), |
| 1463 | SavedNewPtr, |
| 1464 | SavedAllocSize, |
| 1465 | E->passAlignment(), |
| 1466 | AllocAlign); |
| 1467 | for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I) { |
| 1468 | auto &Arg = NewArgs[I + NumNonPlacementArgs]; |
| 1469 | Cleanup->setPlacementArg(I, DominatingValue<RValue>::save(CGF, Arg.RV), |
| 1470 | Arg.Ty); |
| 1471 | } |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1472 | |
John McCall | f4beacd | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 1473 | CGF.initFullExprCleanup(); |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1476 | llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) { |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1477 | // The element type being allocated. |
| 1478 | QualType allocType = getContext().getBaseElementType(E->getAllocatedType()); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1479 | |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1480 | // 1. Build a call to the allocation function. |
| 1481 | FunctionDecl *allocator = E->getOperatorNew(); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1482 | |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 1483 | // If there is a brace-initializer, cannot allocate fewer elements than inits. |
| 1484 | unsigned minElements = 0; |
| 1485 | if (E->isArray() && E->hasInitializer()) { |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1486 | const InitListExpr *ILE = dyn_cast<InitListExpr>(E->getInitializer()); |
| 1487 | if (ILE && ILE->isStringLiteralInit()) |
| 1488 | minElements = |
| 1489 | cast<ConstantArrayType>(ILE->getType()->getAsArrayTypeUnsafe()) |
| 1490 | ->getSize().getZExtValue(); |
| 1491 | else if (ILE) |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 1492 | minElements = ILE->getNumInits(); |
| 1493 | } |
| 1494 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1495 | llvm::Value *numElements = nullptr; |
| 1496 | llvm::Value *allocSizeWithoutCookie = nullptr; |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1497 | llvm::Value *allocSize = |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 1498 | EmitCXXNewAllocSize(*this, E, minElements, numElements, |
| 1499 | allocSizeWithoutCookie); |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1500 | CharUnits allocAlign = getContext().getTypeAlignInChars(allocType); |
Alexey Samsonov | cbe875a | 2014-08-28 00:22:11 +0000 | [diff] [blame] | 1501 | |
John McCall | 7ec4b43 | 2011-05-16 01:05:12 +0000 | [diff] [blame] | 1502 | // Emit the allocation call. If the allocator is a global placement |
| 1503 | // operator, just "inline" it directly. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1504 | Address allocation = Address::invalid(); |
| 1505 | CallArgList allocatorArgs; |
John McCall | 7ec4b43 | 2011-05-16 01:05:12 +0000 | [diff] [blame] | 1506 | if (allocator->isReservedGlobalPlacementOperator()) { |
John McCall | 53dcf94 | 2015-09-29 23:55:17 +0000 | [diff] [blame] | 1507 | assert(E->getNumPlacementArgs() == 1); |
| 1508 | const Expr *arg = *E->placement_arguments().begin(); |
| 1509 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1510 | AlignmentSource alignSource; |
John McCall | 53dcf94 | 2015-09-29 23:55:17 +0000 | [diff] [blame] | 1511 | allocation = EmitPointerWithAlignment(arg, &alignSource); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1512 | |
| 1513 | // The pointer expression will, in many cases, be an opaque void*. |
| 1514 | // In these cases, discard the computed alignment and use the |
| 1515 | // formal alignment of the allocated type. |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1516 | if (alignSource != AlignmentSource::Decl) |
| 1517 | allocation = Address(allocation.getPointer(), allocAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1518 | |
John McCall | 53dcf94 | 2015-09-29 23:55:17 +0000 | [diff] [blame] | 1519 | // Set up allocatorArgs for the call to operator delete if it's not |
| 1520 | // the reserved global operator. |
| 1521 | if (E->getOperatorDelete() && |
| 1522 | !E->getOperatorDelete()->isReservedGlobalPlacementOperator()) { |
| 1523 | allocatorArgs.add(RValue::get(allocSize), getContext().getSizeType()); |
| 1524 | allocatorArgs.add(RValue::get(allocation.getPointer()), arg->getType()); |
| 1525 | } |
| 1526 | |
John McCall | 7ec4b43 | 2011-05-16 01:05:12 +0000 | [diff] [blame] | 1527 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1528 | const FunctionProtoType *allocatorType = |
| 1529 | allocator->getType()->castAs<FunctionProtoType>(); |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1530 | unsigned ParamsToSkip = 0; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1531 | |
| 1532 | // The allocation size is the first argument. |
| 1533 | QualType sizeType = getContext().getSizeType(); |
| 1534 | allocatorArgs.add(RValue::get(allocSize), sizeType); |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1535 | ++ParamsToSkip; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1536 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1537 | if (allocSize != allocSizeWithoutCookie) { |
| 1538 | CharUnits cookieAlign = getSizeAlign(); // FIXME: Ask the ABI. |
| 1539 | allocAlign = std::max(allocAlign, cookieAlign); |
| 1540 | } |
| 1541 | |
| 1542 | // The allocation alignment may be passed as the second argument. |
| 1543 | if (E->passAlignment()) { |
| 1544 | QualType AlignValT = sizeType; |
| 1545 | if (allocatorType->getNumParams() > 1) { |
| 1546 | AlignValT = allocatorType->getParamType(1); |
| 1547 | assert(getContext().hasSameUnqualifiedType( |
| 1548 | AlignValT->castAs<EnumType>()->getDecl()->getIntegerType(), |
| 1549 | sizeType) && |
| 1550 | "wrong type for alignment parameter"); |
| 1551 | ++ParamsToSkip; |
| 1552 | } else { |
| 1553 | // Corner case, passing alignment to 'operator new(size_t, ...)'. |
| 1554 | assert(allocator->isVariadic() && "can't pass alignment to allocator"); |
| 1555 | } |
| 1556 | allocatorArgs.add( |
| 1557 | RValue::get(llvm::ConstantInt::get(SizeTy, allocAlign.getQuantity())), |
| 1558 | AlignValT); |
| 1559 | } |
| 1560 | |
| 1561 | // FIXME: Why do we not pass a CalleeDecl here? |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1562 | EmitCallArgs(allocatorArgs, allocatorType, E->placement_arguments(), |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1563 | /*CalleeDecl*/nullptr, /*ParamsToSkip*/ParamsToSkip); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1564 | |
| 1565 | RValue RV = |
| 1566 | EmitNewDeleteCall(*this, allocator, allocatorType, allocatorArgs); |
| 1567 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1568 | // If this was a call to a global replaceable allocation function that does |
| 1569 | // not take an alignment argument, the allocator is known to produce |
| 1570 | // storage that's suitably aligned for any object that fits, up to a known |
| 1571 | // threshold. Otherwise assume it's suitably aligned for the allocated type. |
| 1572 | CharUnits allocationAlign = allocAlign; |
| 1573 | if (!E->passAlignment() && |
| 1574 | allocator->isReplaceableGlobalAllocationFunction()) { |
| 1575 | unsigned AllocatorAlign = llvm::PowerOf2Floor(std::min<uint64_t>( |
| 1576 | Target.getNewAlign(), getContext().getTypeSize(allocType))); |
| 1577 | allocationAlign = std::max( |
| 1578 | allocationAlign, getContext().toCharUnitsFromBits(AllocatorAlign)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
| 1581 | allocation = Address(RV.getScalarVal(), allocationAlign); |
John McCall | 7ec4b43 | 2011-05-16 01:05:12 +0000 | [diff] [blame] | 1582 | } |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1583 | |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1584 | // Emit a null check on the allocation result if the allocation |
| 1585 | // function is allowed to return null (because it has a non-throwing |
Richard Smith | 902a023 | 2015-02-14 01:52:20 +0000 | [diff] [blame] | 1586 | // exception spec or is the reserved placement new) and we have an |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1587 | // interesting initializer. |
Richard Smith | 902a023 | 2015-02-14 01:52:20 +0000 | [diff] [blame] | 1588 | bool nullCheck = E->shouldNullCheckAllocation(getContext()) && |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1589 | (!allocType.isPODType(getContext()) || E->hasInitializer()); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1590 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1591 | llvm::BasicBlock *nullCheckBB = nullptr; |
| 1592 | llvm::BasicBlock *contBB = nullptr; |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1593 | |
John McCall | f7dcf32 | 2011-03-07 01:52:56 +0000 | [diff] [blame] | 1594 | // The null-check means that the initializer is conditionally |
| 1595 | // evaluated. |
| 1596 | ConditionalEvaluation conditional(*this); |
| 1597 | |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1598 | if (nullCheck) { |
John McCall | f7dcf32 | 2011-03-07 01:52:56 +0000 | [diff] [blame] | 1599 | conditional.begin(*this); |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1600 | |
| 1601 | nullCheckBB = Builder.GetInsertBlock(); |
| 1602 | llvm::BasicBlock *notNullBB = createBasicBlock("new.notnull"); |
| 1603 | contBB = createBasicBlock("new.cont"); |
| 1604 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1605 | llvm::Value *isNull = |
| 1606 | Builder.CreateIsNull(allocation.getPointer(), "new.isnull"); |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1607 | Builder.CreateCondBr(isNull, contBB, notNullBB); |
| 1608 | EmitBlock(notNullBB); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1609 | } |
Anders Carlsson | f771681 | 2009-09-23 18:59:48 +0000 | [diff] [blame] | 1610 | |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1611 | // If there's an operator delete, enter a cleanup to call it if an |
| 1612 | // exception is thrown. |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1613 | EHScopeStack::stable_iterator operatorDeleteCleanup; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1614 | llvm::Instruction *cleanupDominator = nullptr; |
John McCall | 7ec4b43 | 2011-05-16 01:05:12 +0000 | [diff] [blame] | 1615 | if (E->getOperatorDelete() && |
| 1616 | !E->getOperatorDelete()->isReservedGlobalPlacementOperator()) { |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1617 | EnterNewDeleteCleanup(*this, E, allocation, allocSize, allocAlign, |
| 1618 | allocatorArgs); |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1619 | operatorDeleteCleanup = EHStack.stable_begin(); |
John McCall | f4beacd | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 1620 | cleanupDominator = Builder.CreateUnreachable(); |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1621 | } |
| 1622 | |
Eli Friedman | cf9b1f6 | 2011-09-06 18:53:03 +0000 | [diff] [blame] | 1623 | assert((allocSize == allocSizeWithoutCookie) == |
| 1624 | CalculateCookiePadding(*this, E).isZero()); |
| 1625 | if (allocSize != allocSizeWithoutCookie) { |
| 1626 | assert(E->isArray()); |
| 1627 | allocation = CGM.getCXXABI().InitializeArrayCookie(*this, allocation, |
| 1628 | numElements, |
| 1629 | E, allocType); |
| 1630 | } |
| 1631 | |
David Blaikie | fb901c7a | 2015-04-04 15:12:29 +0000 | [diff] [blame] | 1632 | llvm::Type *elementTy = ConvertTypeForMem(allocType); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1633 | Address result = Builder.CreateElementBitCast(allocation, elementTy); |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1634 | |
Piotr Padlewski | 338c9d0 | 2015-09-15 21:46:47 +0000 | [diff] [blame] | 1635 | // Passing pointer through invariant.group.barrier to avoid propagation of |
| 1636 | // vptrs information which may be included in previous type. |
| 1637 | if (CGM.getCodeGenOpts().StrictVTablePointers && |
| 1638 | CGM.getCodeGenOpts().OptimizationLevel > 0 && |
| 1639 | allocator->isReservedGlobalPlacementOperator()) |
| 1640 | result = Address(Builder.CreateInvariantGroupBarrier(result.getPointer()), |
| 1641 | result.getAlignment()); |
| 1642 | |
David Blaikie | fb901c7a | 2015-04-04 15:12:29 +0000 | [diff] [blame] | 1643 | EmitNewInitializer(*this, E, allocType, elementTy, result, numElements, |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 1644 | allocSizeWithoutCookie); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1645 | if (E->isArray()) { |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1646 | // NewPtr is a pointer to the base element type. If we're |
| 1647 | // allocating an array of arrays, we'll need to cast back to the |
| 1648 | // array pointer type. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1649 | llvm::Type *resultType = ConvertTypeForMem(E->getType()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1650 | if (result.getType() != resultType) |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1651 | result = Builder.CreateBitCast(result, resultType); |
Fariborz Jahanian | 47b4629 | 2010-03-24 16:57:01 +0000 | [diff] [blame] | 1652 | } |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1653 | |
| 1654 | // Deactivate the 'operator delete' cleanup if we finished |
| 1655 | // initialization. |
John McCall | f4beacd | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 1656 | if (operatorDeleteCleanup.isValid()) { |
| 1657 | DeactivateCleanupBlock(operatorDeleteCleanup, cleanupDominator); |
| 1658 | cleanupDominator->eraseFromParent(); |
| 1659 | } |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1660 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1661 | llvm::Value *resultPtr = result.getPointer(); |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1662 | if (nullCheck) { |
John McCall | f7dcf32 | 2011-03-07 01:52:56 +0000 | [diff] [blame] | 1663 | conditional.end(*this); |
| 1664 | |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1665 | llvm::BasicBlock *notNullBB = Builder.GetInsertBlock(); |
| 1666 | EmitBlock(contBB); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1667 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1668 | llvm::PHINode *PHI = Builder.CreatePHI(resultPtr->getType(), 2); |
| 1669 | PHI->addIncoming(resultPtr, notNullBB); |
| 1670 | PHI->addIncoming(llvm::Constant::getNullValue(resultPtr->getType()), |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1671 | nullCheckBB); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1672 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1673 | resultPtr = PHI; |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1674 | } |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1675 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1676 | return resultPtr; |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
Eli Friedman | fe81e3f | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1679 | void CodeGenFunction::EmitDeleteCall(const FunctionDecl *DeleteFD, |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1680 | llvm::Value *Ptr, QualType DeleteTy, |
| 1681 | llvm::Value *NumElements, |
| 1682 | CharUnits CookieSize) { |
| 1683 | assert((!NumElements && CookieSize.isZero()) || |
| 1684 | DeleteFD->getOverloadedOperator() == OO_Array_Delete); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1685 | |
Eli Friedman | fe81e3f | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1686 | const FunctionProtoType *DeleteFTy = |
| 1687 | DeleteFD->getType()->getAs<FunctionProtoType>(); |
| 1688 | |
| 1689 | CallArgList DeleteArgs; |
| 1690 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1691 | std::pair<bool, bool> PassSizeAndAlign = |
| 1692 | shouldPassSizeAndAlignToUsualDelete(DeleteFTy); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1693 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1694 | auto ParamTypeIt = DeleteFTy->param_type_begin(); |
| 1695 | |
| 1696 | // Pass the pointer itself. |
| 1697 | QualType ArgTy = *ParamTypeIt++; |
Eli Friedman | fe81e3f | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1698 | llvm::Value *DeletePtr = Builder.CreateBitCast(Ptr, ConvertType(ArgTy)); |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1699 | DeleteArgs.add(RValue::get(DeletePtr), ArgTy); |
Eli Friedman | fe81e3f | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1700 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1701 | // Pass the size if the delete function has a size_t parameter. |
| 1702 | if (PassSizeAndAlign.first) { |
| 1703 | QualType SizeType = *ParamTypeIt++; |
| 1704 | CharUnits DeleteTypeSize = getContext().getTypeSizeInChars(DeleteTy); |
| 1705 | llvm::Value *Size = llvm::ConstantInt::get(ConvertType(SizeType), |
| 1706 | DeleteTypeSize.getQuantity()); |
| 1707 | |
| 1708 | // For array new, multiply by the number of elements. |
| 1709 | if (NumElements) |
| 1710 | Size = Builder.CreateMul(Size, NumElements); |
| 1711 | |
| 1712 | // If there is a cookie, add the cookie size. |
| 1713 | if (!CookieSize.isZero()) |
| 1714 | Size = Builder.CreateAdd( |
| 1715 | Size, llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity())); |
| 1716 | |
| 1717 | DeleteArgs.add(RValue::get(Size), SizeType); |
| 1718 | } |
| 1719 | |
| 1720 | // Pass the alignment if the delete function has an align_val_t parameter. |
| 1721 | if (PassSizeAndAlign.second) { |
| 1722 | QualType AlignValType = *ParamTypeIt++; |
| 1723 | CharUnits DeleteTypeAlign = getContext().toCharUnitsFromBits( |
| 1724 | getContext().getTypeAlignIfKnown(DeleteTy)); |
| 1725 | llvm::Value *Align = llvm::ConstantInt::get(ConvertType(AlignValType), |
| 1726 | DeleteTypeAlign.getQuantity()); |
| 1727 | DeleteArgs.add(RValue::get(Align), AlignValType); |
| 1728 | } |
| 1729 | |
| 1730 | assert(ParamTypeIt == DeleteFTy->param_type_end() && |
| 1731 | "unknown parameter to usual delete function"); |
Eli Friedman | fe81e3f | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1732 | |
| 1733 | // Emit the call to delete. |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 1734 | EmitNewDeleteCall(*this, DeleteFD, DeleteFTy, DeleteArgs); |
Eli Friedman | fe81e3f | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1737 | namespace { |
| 1738 | /// Calls the given 'operator delete' on a single object. |
David Blaikie | 7e70d68 | 2015-08-18 22:40:54 +0000 | [diff] [blame] | 1739 | struct CallObjectDelete final : EHScopeStack::Cleanup { |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1740 | llvm::Value *Ptr; |
| 1741 | const FunctionDecl *OperatorDelete; |
| 1742 | QualType ElementType; |
| 1743 | |
| 1744 | CallObjectDelete(llvm::Value *Ptr, |
| 1745 | const FunctionDecl *OperatorDelete, |
| 1746 | QualType ElementType) |
| 1747 | : Ptr(Ptr), OperatorDelete(OperatorDelete), ElementType(ElementType) {} |
| 1748 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1749 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1750 | CGF.EmitDeleteCall(OperatorDelete, Ptr, ElementType); |
| 1751 | } |
| 1752 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1753 | } |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1754 | |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 1755 | void |
| 1756 | CodeGenFunction::pushCallObjectDeleteCleanup(const FunctionDecl *OperatorDelete, |
| 1757 | llvm::Value *CompletePtr, |
| 1758 | QualType ElementType) { |
| 1759 | EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup, CompletePtr, |
| 1760 | OperatorDelete, ElementType); |
| 1761 | } |
| 1762 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1763 | /// Emit the code for deleting a single object. |
| 1764 | static void EmitObjectDelete(CodeGenFunction &CGF, |
David Majnemer | 0868137 | 2014-11-01 07:37:17 +0000 | [diff] [blame] | 1765 | const CXXDeleteExpr *DE, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1766 | Address Ptr, |
David Majnemer | 0868137 | 2014-11-01 07:37:17 +0000 | [diff] [blame] | 1767 | QualType ElementType) { |
Ivan Krasin | d98f5d7 | 2016-11-17 00:39:48 +0000 | [diff] [blame] | 1768 | // C++11 [expr.delete]p3: |
| 1769 | // If the static type of the object to be deleted is different from its |
| 1770 | // dynamic type, the static type shall be a base class of the dynamic type |
| 1771 | // of the object to be deleted and the static type shall have a virtual |
| 1772 | // destructor or the behavior is undefined. |
| 1773 | CGF.EmitTypeCheck(CodeGenFunction::TCK_MemberCall, |
| 1774 | DE->getExprLoc(), Ptr.getPointer(), |
| 1775 | ElementType); |
| 1776 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1777 | // Find the destructor for the type, if applicable. If the |
| 1778 | // destructor is virtual, we'll just emit the vcall and return. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1779 | const CXXDestructorDecl *Dtor = nullptr; |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1780 | if (const RecordType *RT = ElementType->getAs<RecordType>()) { |
| 1781 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Eli Friedman | b23533d | 2011-08-02 18:05:30 +0000 | [diff] [blame] | 1782 | if (RD->hasDefinition() && !RD->hasTrivialDestructor()) { |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1783 | Dtor = RD->getDestructor(); |
| 1784 | |
| 1785 | if (Dtor->isVirtual()) { |
David Majnemer | 0868137 | 2014-11-01 07:37:17 +0000 | [diff] [blame] | 1786 | CGF.CGM.getCXXABI().emitVirtualObjectDelete(CGF, DE, Ptr, ElementType, |
| 1787 | Dtor); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1788 | return; |
| 1789 | } |
| 1790 | } |
| 1791 | } |
| 1792 | |
| 1793 | // Make sure that we call delete even if the dtor throws. |
John McCall | e4df6c8 | 2011-01-28 08:37:24 +0000 | [diff] [blame] | 1794 | // This doesn't have to a conditional cleanup because we're going |
| 1795 | // to pop it off in a second. |
David Majnemer | 0868137 | 2014-11-01 07:37:17 +0000 | [diff] [blame] | 1796 | const FunctionDecl *OperatorDelete = DE->getOperatorDelete(); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1797 | CGF.EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1798 | Ptr.getPointer(), |
| 1799 | OperatorDelete, ElementType); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1800 | |
| 1801 | if (Dtor) |
| 1802 | CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, |
Douglas Gregor | 6153500 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 1803 | /*ForVirtualBase=*/false, |
| 1804 | /*Delegating=*/false, |
| 1805 | Ptr); |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 1806 | else if (auto Lifetime = ElementType.getObjCLifetime()) { |
| 1807 | switch (Lifetime) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1808 | case Qualifiers::OCL_None: |
| 1809 | case Qualifiers::OCL_ExplicitNone: |
| 1810 | case Qualifiers::OCL_Autoreleasing: |
| 1811 | break; |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1812 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1813 | case Qualifiers::OCL_Strong: |
| 1814 | CGF.EmitARCDestroyStrong(Ptr, ARCPreciseLifetime); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1815 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1816 | |
| 1817 | case Qualifiers::OCL_Weak: |
| 1818 | CGF.EmitARCDestroyWeak(Ptr); |
| 1819 | break; |
| 1820 | } |
| 1821 | } |
| 1822 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1823 | CGF.PopCleanupBlock(); |
| 1824 | } |
| 1825 | |
| 1826 | namespace { |
| 1827 | /// Calls the given 'operator delete' on an array of objects. |
David Blaikie | 7e70d68 | 2015-08-18 22:40:54 +0000 | [diff] [blame] | 1828 | struct CallArrayDelete final : EHScopeStack::Cleanup { |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1829 | llvm::Value *Ptr; |
| 1830 | const FunctionDecl *OperatorDelete; |
| 1831 | llvm::Value *NumElements; |
| 1832 | QualType ElementType; |
| 1833 | CharUnits CookieSize; |
| 1834 | |
| 1835 | CallArrayDelete(llvm::Value *Ptr, |
| 1836 | const FunctionDecl *OperatorDelete, |
| 1837 | llvm::Value *NumElements, |
| 1838 | QualType ElementType, |
| 1839 | CharUnits CookieSize) |
| 1840 | : Ptr(Ptr), OperatorDelete(OperatorDelete), NumElements(NumElements), |
| 1841 | ElementType(ElementType), CookieSize(CookieSize) {} |
| 1842 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1843 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1844 | CGF.EmitDeleteCall(OperatorDelete, Ptr, ElementType, NumElements, |
| 1845 | CookieSize); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1846 | } |
| 1847 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1848 | } |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1849 | |
| 1850 | /// Emit the code for deleting an array of objects. |
| 1851 | static void EmitArrayDelete(CodeGenFunction &CGF, |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1852 | const CXXDeleteExpr *E, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1853 | Address deletedPtr, |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1854 | QualType elementType) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1855 | llvm::Value *numElements = nullptr; |
| 1856 | llvm::Value *allocatedPtr = nullptr; |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1857 | CharUnits cookieSize; |
| 1858 | CGF.CGM.getCXXABI().ReadArrayCookie(CGF, deletedPtr, E, elementType, |
| 1859 | numElements, allocatedPtr, cookieSize); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1860 | |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1861 | assert(allocatedPtr && "ReadArrayCookie didn't set allocated pointer"); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1862 | |
| 1863 | // Make sure that we call delete even if one of the dtors throws. |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1864 | const FunctionDecl *operatorDelete = E->getOperatorDelete(); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1865 | CGF.EHStack.pushCleanup<CallArrayDelete>(NormalAndEHCleanup, |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1866 | allocatedPtr, operatorDelete, |
| 1867 | numElements, elementType, |
| 1868 | cookieSize); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1869 | |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1870 | // Destroy the elements. |
| 1871 | if (QualType::DestructionKind dtorKind = elementType.isDestructedType()) { |
| 1872 | assert(numElements && "no element count for a type with a destructor!"); |
| 1873 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1874 | CharUnits elementSize = CGF.getContext().getTypeSizeInChars(elementType); |
| 1875 | CharUnits elementAlign = |
| 1876 | deletedPtr.getAlignment().alignmentOfArrayElement(elementSize); |
| 1877 | |
| 1878 | llvm::Value *arrayBegin = deletedPtr.getPointer(); |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1879 | llvm::Value *arrayEnd = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1880 | CGF.Builder.CreateInBoundsGEP(arrayBegin, numElements, "delete.end"); |
John McCall | 97eab0a | 2011-07-13 08:09:46 +0000 | [diff] [blame] | 1881 | |
| 1882 | // Note that it is legal to allocate a zero-length array, and we |
| 1883 | // can never fold the check away because the length should always |
| 1884 | // come from a cookie. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1885 | CGF.emitArrayDestroy(arrayBegin, arrayEnd, elementType, elementAlign, |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1886 | CGF.getDestroyer(dtorKind), |
John McCall | 97eab0a | 2011-07-13 08:09:46 +0000 | [diff] [blame] | 1887 | /*checkZeroLength*/ true, |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1888 | CGF.needsEHCleanup(dtorKind)); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1891 | // Pop the cleanup block. |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1892 | CGF.PopCleanupBlock(); |
| 1893 | } |
| 1894 | |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1895 | void CodeGenFunction::EmitCXXDeleteExpr(const CXXDeleteExpr *E) { |
Douglas Gregor | bb3e12f | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1896 | const Expr *Arg = E->getArgument(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1897 | Address Ptr = EmitPointerWithAlignment(Arg); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1898 | |
| 1899 | // Null check the pointer. |
| 1900 | llvm::BasicBlock *DeleteNotNull = createBasicBlock("delete.notnull"); |
| 1901 | llvm::BasicBlock *DeleteEnd = createBasicBlock("delete.end"); |
| 1902 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1903 | llvm::Value *IsNull = Builder.CreateIsNull(Ptr.getPointer(), "isnull"); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1904 | |
| 1905 | Builder.CreateCondBr(IsNull, DeleteEnd, DeleteNotNull); |
| 1906 | EmitBlock(DeleteNotNull); |
Anders Carlsson | e828c36 | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 1907 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1908 | // We might be deleting a pointer to array. If so, GEP down to the |
| 1909 | // first non-array element. |
| 1910 | // (this assumes that A(*)[3][7] is converted to [3 x [7 x %A]]*) |
| 1911 | QualType DeleteTy = Arg->getType()->getAs<PointerType>()->getPointeeType(); |
| 1912 | if (DeleteTy->isConstantArrayType()) { |
| 1913 | llvm::Value *Zero = Builder.getInt32(0); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1914 | SmallVector<llvm::Value*,8> GEP; |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1915 | |
| 1916 | GEP.push_back(Zero); // point at the outermost array |
| 1917 | |
| 1918 | // For each layer of array type we're pointing at: |
| 1919 | while (const ConstantArrayType *Arr |
| 1920 | = getContext().getAsConstantArrayType(DeleteTy)) { |
| 1921 | // 1. Unpeel the array type. |
| 1922 | DeleteTy = Arr->getElementType(); |
| 1923 | |
| 1924 | // 2. GEP to the first element of the array. |
| 1925 | GEP.push_back(Zero); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1926 | } |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1927 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1928 | Ptr = Address(Builder.CreateInBoundsGEP(Ptr.getPointer(), GEP, "del.first"), |
| 1929 | Ptr.getAlignment()); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1930 | } |
| 1931 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1932 | assert(ConvertTypeForMem(DeleteTy) == Ptr.getElementType()); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1933 | |
Reid Kleckner | 7270ef5 | 2015-03-19 17:03:58 +0000 | [diff] [blame] | 1934 | if (E->isArrayForm()) { |
| 1935 | EmitArrayDelete(*this, E, Ptr, DeleteTy); |
| 1936 | } else { |
| 1937 | EmitObjectDelete(*this, E, Ptr, DeleteTy); |
| 1938 | } |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1939 | |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1940 | EmitBlock(DeleteEnd); |
| 1941 | } |
Mike Stump | c9b231c | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 1942 | |
David Majnemer | 1c3d95e | 2014-07-19 00:17:06 +0000 | [diff] [blame] | 1943 | static bool isGLValueFromPointerDeref(const Expr *E) { |
| 1944 | E = E->IgnoreParens(); |
| 1945 | |
| 1946 | if (const auto *CE = dyn_cast<CastExpr>(E)) { |
| 1947 | if (!CE->getSubExpr()->isGLValue()) |
| 1948 | return false; |
| 1949 | return isGLValueFromPointerDeref(CE->getSubExpr()); |
| 1950 | } |
| 1951 | |
| 1952 | if (const auto *OVE = dyn_cast<OpaqueValueExpr>(E)) |
| 1953 | return isGLValueFromPointerDeref(OVE->getSourceExpr()); |
| 1954 | |
| 1955 | if (const auto *BO = dyn_cast<BinaryOperator>(E)) |
| 1956 | if (BO->getOpcode() == BO_Comma) |
| 1957 | return isGLValueFromPointerDeref(BO->getRHS()); |
| 1958 | |
| 1959 | if (const auto *ACO = dyn_cast<AbstractConditionalOperator>(E)) |
| 1960 | return isGLValueFromPointerDeref(ACO->getTrueExpr()) || |
| 1961 | isGLValueFromPointerDeref(ACO->getFalseExpr()); |
| 1962 | |
| 1963 | // C++11 [expr.sub]p1: |
| 1964 | // The expression E1[E2] is identical (by definition) to *((E1)+(E2)) |
| 1965 | if (isa<ArraySubscriptExpr>(E)) |
| 1966 | return true; |
| 1967 | |
| 1968 | if (const auto *UO = dyn_cast<UnaryOperator>(E)) |
| 1969 | if (UO->getOpcode() == UO_Deref) |
| 1970 | return true; |
| 1971 | |
| 1972 | return false; |
| 1973 | } |
| 1974 | |
Warren Hunt | 747e301 | 2014-06-18 21:15:55 +0000 | [diff] [blame] | 1975 | static llvm::Value *EmitTypeidFromVTable(CodeGenFunction &CGF, const Expr *E, |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1976 | llvm::Type *StdTypeInfoPtrTy) { |
Anders Carlsson | 940f02d | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 1977 | // Get the vtable pointer. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1978 | Address ThisPtr = CGF.EmitLValue(E).getAddress(); |
Anders Carlsson | 940f02d | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 1979 | |
| 1980 | // C++ [expr.typeid]p2: |
| 1981 | // If the glvalue expression is obtained by applying the unary * operator to |
| 1982 | // a pointer and the pointer is a null pointer value, the typeid expression |
| 1983 | // throws the std::bad_typeid exception. |
David Majnemer | 1c3d95e | 2014-07-19 00:17:06 +0000 | [diff] [blame] | 1984 | // |
| 1985 | // However, this paragraph's intent is not clear. We choose a very generous |
| 1986 | // interpretation which implores us to consider comma operators, conditional |
| 1987 | // operators, parentheses and other such constructs. |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 1988 | QualType SrcRecordTy = E->getType(); |
David Majnemer | 1c3d95e | 2014-07-19 00:17:06 +0000 | [diff] [blame] | 1989 | if (CGF.CGM.getCXXABI().shouldTypeidBeNullChecked( |
| 1990 | isGLValueFromPointerDeref(E), SrcRecordTy)) { |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 1991 | llvm::BasicBlock *BadTypeidBlock = |
Anders Carlsson | 940f02d | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 1992 | CGF.createBasicBlock("typeid.bad_typeid"); |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 1993 | llvm::BasicBlock *EndBlock = CGF.createBasicBlock("typeid.end"); |
Anders Carlsson | 940f02d | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 1994 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1995 | llvm::Value *IsNull = CGF.Builder.CreateIsNull(ThisPtr.getPointer()); |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 1996 | CGF.Builder.CreateCondBr(IsNull, BadTypeidBlock, EndBlock); |
Anders Carlsson | 940f02d | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 1997 | |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 1998 | CGF.EmitBlock(BadTypeidBlock); |
| 1999 | CGF.CGM.getCXXABI().EmitBadTypeidCall(CGF); |
| 2000 | CGF.EmitBlock(EndBlock); |
Anders Carlsson | 940f02d | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 2001 | } |
| 2002 | |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 2003 | return CGF.CGM.getCXXABI().EmitTypeid(CGF, SrcRecordTy, ThisPtr, |
| 2004 | StdTypeInfoPtrTy); |
Anders Carlsson | 940f02d | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 2005 | } |
| 2006 | |
John McCall | e4df6c8 | 2011-01-28 08:37:24 +0000 | [diff] [blame] | 2007 | llvm::Value *CodeGenFunction::EmitCXXTypeidExpr(const CXXTypeidExpr *E) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2008 | llvm::Type *StdTypeInfoPtrTy = |
Anders Carlsson | 940f02d | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 2009 | ConvertType(E->getType())->getPointerTo(); |
Anders Carlsson | fd7dfeb | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 2010 | |
Anders Carlsson | 3f4336c | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 2011 | if (E->isTypeOperand()) { |
David Majnemer | 143c55e | 2013-09-27 07:04:31 +0000 | [diff] [blame] | 2012 | llvm::Constant *TypeInfo = |
| 2013 | CGM.GetAddrOfRTTIDescriptor(E->getTypeOperand(getContext())); |
Anders Carlsson | 940f02d | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 2014 | return Builder.CreateBitCast(TypeInfo, StdTypeInfoPtrTy); |
Anders Carlsson | 3f4336c | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 2015 | } |
Anders Carlsson | 0c63350 | 2011-04-11 14:13:40 +0000 | [diff] [blame] | 2016 | |
Anders Carlsson | 940f02d | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 2017 | // C++ [expr.typeid]p2: |
| 2018 | // When typeid is applied to a glvalue expression whose type is a |
| 2019 | // polymorphic class type, the result refers to a std::type_info object |
| 2020 | // representing the type of the most derived object (that is, the dynamic |
| 2021 | // type) to which the glvalue refers. |
Richard Smith | ef8bf43 | 2012-08-13 20:08:14 +0000 | [diff] [blame] | 2022 | if (E->isPotentiallyEvaluated()) |
| 2023 | return EmitTypeidFromVTable(*this, E->getExprOperand(), |
| 2024 | StdTypeInfoPtrTy); |
Anders Carlsson | 940f02d | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 2025 | |
| 2026 | QualType OperandTy = E->getExprOperand()->getType(); |
| 2027 | return Builder.CreateBitCast(CGM.GetAddrOfRTTIDescriptor(OperandTy), |
| 2028 | StdTypeInfoPtrTy); |
Mike Stump | c9b231c | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 2029 | } |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 2030 | |
Anders Carlsson | c1c9971 | 2011-04-11 01:45:29 +0000 | [diff] [blame] | 2031 | static llvm::Value *EmitDynamicCastToNull(CodeGenFunction &CGF, |
| 2032 | QualType DestTy) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2033 | llvm::Type *DestLTy = CGF.ConvertType(DestTy); |
Anders Carlsson | c1c9971 | 2011-04-11 01:45:29 +0000 | [diff] [blame] | 2034 | if (DestTy->isPointerType()) |
| 2035 | return llvm::Constant::getNullValue(DestLTy); |
| 2036 | |
| 2037 | /// C++ [expr.dynamic.cast]p9: |
| 2038 | /// A failed cast to reference type throws std::bad_cast |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 2039 | if (!CGF.CGM.getCXXABI().EmitBadCastCall(CGF)) |
| 2040 | return nullptr; |
Anders Carlsson | c1c9971 | 2011-04-11 01:45:29 +0000 | [diff] [blame] | 2041 | |
| 2042 | CGF.EmitBlock(CGF.createBasicBlock("dynamic_cast.end")); |
| 2043 | return llvm::UndefValue::get(DestLTy); |
| 2044 | } |
| 2045 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2046 | llvm::Value *CodeGenFunction::EmitDynamicCast(Address ThisAddr, |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 2047 | const CXXDynamicCastExpr *DCE) { |
Alexey Bataev | 2bf9b4c | 2015-10-20 04:24:12 +0000 | [diff] [blame] | 2048 | CGM.EmitExplicitCastExprType(DCE, this); |
Anders Carlsson | 3f4336c | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 2049 | QualType DestTy = DCE->getTypeAsWritten(); |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 2050 | |
Anders Carlsson | c1c9971 | 2011-04-11 01:45:29 +0000 | [diff] [blame] | 2051 | if (DCE->isAlwaysNull()) |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 2052 | if (llvm::Value *T = EmitDynamicCastToNull(*this, DestTy)) |
| 2053 | return T; |
Anders Carlsson | c1c9971 | 2011-04-11 01:45:29 +0000 | [diff] [blame] | 2054 | |
| 2055 | QualType SrcTy = DCE->getSubExpr()->getType(); |
| 2056 | |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 2057 | // C++ [expr.dynamic.cast]p7: |
| 2058 | // If T is "pointer to cv void," then the result is a pointer to the most |
| 2059 | // derived object pointed to by v. |
| 2060 | const PointerType *DestPTy = DestTy->getAs<PointerType>(); |
| 2061 | |
| 2062 | bool isDynamicCastToVoid; |
| 2063 | QualType SrcRecordTy; |
| 2064 | QualType DestRecordTy; |
| 2065 | if (DestPTy) { |
| 2066 | isDynamicCastToVoid = DestPTy->getPointeeType()->isVoidType(); |
| 2067 | SrcRecordTy = SrcTy->castAs<PointerType>()->getPointeeType(); |
| 2068 | DestRecordTy = DestPTy->getPointeeType(); |
| 2069 | } else { |
| 2070 | isDynamicCastToVoid = false; |
| 2071 | SrcRecordTy = SrcTy; |
| 2072 | DestRecordTy = DestTy->castAs<ReferenceType>()->getPointeeType(); |
| 2073 | } |
| 2074 | |
| 2075 | assert(SrcRecordTy->isRecordType() && "source type must be a record type!"); |
| 2076 | |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 2077 | // C++ [expr.dynamic.cast]p4: |
| 2078 | // If the value of v is a null pointer value in the pointer case, the result |
| 2079 | // is the null pointer value of type T. |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 2080 | bool ShouldNullCheckSrcValue = |
| 2081 | CGM.getCXXABI().shouldDynamicCastCallBeNullChecked(SrcTy->isPointerType(), |
| 2082 | SrcRecordTy); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2083 | |
| 2084 | llvm::BasicBlock *CastNull = nullptr; |
| 2085 | llvm::BasicBlock *CastNotNull = nullptr; |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 2086 | llvm::BasicBlock *CastEnd = createBasicBlock("dynamic_cast.end"); |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 2087 | |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 2088 | if (ShouldNullCheckSrcValue) { |
| 2089 | CastNull = createBasicBlock("dynamic_cast.null"); |
| 2090 | CastNotNull = createBasicBlock("dynamic_cast.notnull"); |
| 2091 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2092 | llvm::Value *IsNull = Builder.CreateIsNull(ThisAddr.getPointer()); |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 2093 | Builder.CreateCondBr(IsNull, CastNull, CastNotNull); |
| 2094 | EmitBlock(CastNotNull); |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 2095 | } |
| 2096 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2097 | llvm::Value *Value; |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 2098 | if (isDynamicCastToVoid) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2099 | Value = CGM.getCXXABI().EmitDynamicCastToVoid(*this, ThisAddr, SrcRecordTy, |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 2100 | DestTy); |
| 2101 | } else { |
| 2102 | assert(DestRecordTy->isRecordType() && |
| 2103 | "destination type must be a record type!"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2104 | Value = CGM.getCXXABI().EmitDynamicCastCall(*this, ThisAddr, SrcRecordTy, |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 2105 | DestTy, DestRecordTy, CastEnd); |
David Majnemer | 67528ea | 2015-11-23 03:01:14 +0000 | [diff] [blame] | 2106 | CastNotNull = Builder.GetInsertBlock(); |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 2107 | } |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 2108 | |
| 2109 | if (ShouldNullCheckSrcValue) { |
| 2110 | EmitBranch(CastEnd); |
| 2111 | |
| 2112 | EmitBlock(CastNull); |
| 2113 | EmitBranch(CastEnd); |
| 2114 | } |
| 2115 | |
| 2116 | EmitBlock(CastEnd); |
| 2117 | |
| 2118 | if (ShouldNullCheckSrcValue) { |
| 2119 | llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2); |
| 2120 | PHI->addIncoming(Value, CastNotNull); |
| 2121 | PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), CastNull); |
| 2122 | |
| 2123 | Value = PHI; |
| 2124 | } |
| 2125 | |
| 2126 | return Value; |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 2127 | } |
Eli Friedman | c370a7e | 2012-02-09 03:32:31 +0000 | [diff] [blame] | 2128 | |
Eli Friedman | c370a7e | 2012-02-09 03:32:31 +0000 | [diff] [blame] | 2129 | void CodeGenFunction::EmitLambdaExpr(const LambdaExpr *E, AggValueSlot Slot) { |
Eli Friedman | 8631f3e8 | 2012-02-09 03:47:20 +0000 | [diff] [blame] | 2130 | RunCleanupsScope Scope(*this); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2131 | LValue SlotLV = MakeAddrLValue(Slot.getAddress(), E->getType()); |
Eli Friedman | 8631f3e8 | 2012-02-09 03:47:20 +0000 | [diff] [blame] | 2132 | |
Eli Friedman | c370a7e | 2012-02-09 03:32:31 +0000 | [diff] [blame] | 2133 | CXXRecordDecl::field_iterator CurField = E->getLambdaClass()->field_begin(); |
James Y Knight | 53c7616 | 2015-07-17 18:21:37 +0000 | [diff] [blame] | 2134 | for (LambdaExpr::const_capture_init_iterator i = E->capture_init_begin(), |
| 2135 | e = E->capture_init_end(); |
Eric Christopher | d47e086 | 2012-02-29 03:25:18 +0000 | [diff] [blame] | 2136 | i != e; ++i, ++CurField) { |
Eli Friedman | c370a7e | 2012-02-09 03:32:31 +0000 | [diff] [blame] | 2137 | // Emit initialization |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2138 | LValue LV = EmitLValueForFieldInitialization(SlotLV, *CurField); |
Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 2139 | if (CurField->hasCapturedVLAType()) { |
| 2140 | auto VAT = CurField->getCapturedVLAType(); |
| 2141 | EmitStoreThroughLValue(RValue::get(VLASizeMap[VAT->getSizeExpr()]), LV); |
| 2142 | } else { |
Richard Smith | 30e304e | 2016-12-14 00:03:17 +0000 | [diff] [blame^] | 2143 | EmitInitializerForField(*CurField, LV, *i); |
Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 2144 | } |
Eli Friedman | c370a7e | 2012-02-09 03:32:31 +0000 | [diff] [blame] | 2145 | } |
Eli Friedman | c370a7e | 2012-02-09 03:32:31 +0000 | [diff] [blame] | 2146 | } |