Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1 | //===--- SemaInit.cpp - Semantic Analysis for Initializers ----------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Sebastian Redl | 26bcc94 | 2011-09-24 17:47:39 +0000 | [diff] [blame] | 9 | // This file implements semantic analysis for initializers. |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 10 | // |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 13 | #include "clang/AST/ASTContext.h" |
John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 14 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 98cee2f | 2009-05-27 16:10:08 +0000 | [diff] [blame] | 15 | #include "clang/AST/ExprCXX.h" |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 16 | #include "clang/AST/ExprObjC.h" |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprOpenMP.h" |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 18 | #include "clang/AST/TypeLoc.h" |
James Molloy | 9eef265 | 2014-06-20 14:35:13 +0000 | [diff] [blame] | 19 | #include "clang/Basic/TargetInfo.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/Sema/Designator.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 21 | #include "clang/Sema/Initialization.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "clang/Sema/Lookup.h" |
| 23 | #include "clang/Sema/SemaInternal.h" |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/APInt.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 28 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 29 | using namespace clang; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 30 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | // Sema Initialization Checking |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 35 | /// Check whether T is compatible with a wide character type (wchar_t, |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 36 | /// char16_t or char32_t). |
| 37 | static bool IsWideCharCompatible(QualType T, ASTContext &Context) { |
| 38 | if (Context.typesAreCompatible(Context.getWideCharType(), T)) |
| 39 | return true; |
| 40 | if (Context.getLangOpts().CPlusPlus || Context.getLangOpts().C11) { |
| 41 | return Context.typesAreCompatible(Context.Char16Ty, T) || |
| 42 | Context.typesAreCompatible(Context.Char32Ty, T); |
| 43 | } |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | enum StringInitFailureKind { |
| 48 | SIF_None, |
| 49 | SIF_NarrowStringIntoWideChar, |
| 50 | SIF_WideStringIntoChar, |
| 51 | SIF_IncompatWideStringIntoWideChar, |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 52 | SIF_UTF8StringIntoPlainChar, |
| 53 | SIF_PlainStringIntoUTF8Char, |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 54 | SIF_Other |
| 55 | }; |
| 56 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 57 | /// Check whether the array of type AT can be initialized by the Init |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 58 | /// expression by means of string initialization. Returns SIF_None if so, |
| 59 | /// otherwise returns a StringInitFailureKind that describes why the |
| 60 | /// initialization would not work. |
| 61 | static StringInitFailureKind IsStringInit(Expr *Init, const ArrayType *AT, |
| 62 | ASTContext &Context) { |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 63 | if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT)) |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 64 | return SIF_Other; |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 65 | |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 66 | // See if this is a string literal or @encode. |
| 67 | Init = Init->IgnoreParens(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 68 | |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 69 | // Handle @encode, which is a narrow string. |
| 70 | if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType()) |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 71 | return SIF_None; |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 72 | |
| 73 | // Otherwise we can only handle string literals. |
| 74 | StringLiteral *SL = dyn_cast<StringLiteral>(Init); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 75 | if (!SL) |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 76 | return SIF_Other; |
Eli Friedman | 42a8465 | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 77 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 78 | const QualType ElemTy = |
| 79 | Context.getCanonicalType(AT->getElementType()).getUnqualifiedType(); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 80 | |
| 81 | switch (SL->getKind()) { |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 82 | case StringLiteral::UTF8: |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 83 | // char8_t array can be initialized with a UTF-8 string. |
| 84 | if (ElemTy->isChar8Type()) |
| 85 | return SIF_None; |
| 86 | LLVM_FALLTHROUGH; |
| 87 | case StringLiteral::Ascii: |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 88 | // char array can be initialized with a narrow string. |
| 89 | // Only allow char x[] = "foo"; not char x[] = L"foo"; |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 90 | if (ElemTy->isCharType()) |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 91 | return (SL->getKind() == StringLiteral::UTF8 && |
| 92 | Context.getLangOpts().Char8) |
| 93 | ? SIF_UTF8StringIntoPlainChar |
| 94 | : SIF_None; |
| 95 | if (ElemTy->isChar8Type()) |
| 96 | return SIF_PlainStringIntoUTF8Char; |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 97 | if (IsWideCharCompatible(ElemTy, Context)) |
| 98 | return SIF_NarrowStringIntoWideChar; |
| 99 | return SIF_Other; |
| 100 | // C99 6.7.8p15 (with correction from DR343), or C11 6.7.9p15: |
| 101 | // "An array with element type compatible with a qualified or unqualified |
| 102 | // version of wchar_t, char16_t, or char32_t may be initialized by a wide |
| 103 | // string literal with the corresponding encoding prefix (L, u, or U, |
| 104 | // respectively), optionally enclosed in braces. |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 105 | case StringLiteral::UTF16: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 106 | if (Context.typesAreCompatible(Context.Char16Ty, ElemTy)) |
| 107 | return SIF_None; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 108 | if (ElemTy->isCharType() || ElemTy->isChar8Type()) |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 109 | return SIF_WideStringIntoChar; |
| 110 | if (IsWideCharCompatible(ElemTy, Context)) |
| 111 | return SIF_IncompatWideStringIntoWideChar; |
| 112 | return SIF_Other; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 113 | case StringLiteral::UTF32: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 114 | if (Context.typesAreCompatible(Context.Char32Ty, ElemTy)) |
| 115 | return SIF_None; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 116 | if (ElemTy->isCharType() || ElemTy->isChar8Type()) |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 117 | return SIF_WideStringIntoChar; |
| 118 | if (IsWideCharCompatible(ElemTy, Context)) |
| 119 | return SIF_IncompatWideStringIntoWideChar; |
| 120 | return SIF_Other; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 121 | case StringLiteral::Wide: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 122 | if (Context.typesAreCompatible(Context.getWideCharType(), ElemTy)) |
| 123 | return SIF_None; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 124 | if (ElemTy->isCharType() || ElemTy->isChar8Type()) |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 125 | return SIF_WideStringIntoChar; |
| 126 | if (IsWideCharCompatible(ElemTy, Context)) |
| 127 | return SIF_IncompatWideStringIntoWideChar; |
| 128 | return SIF_Other; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 129 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 130 | |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 131 | llvm_unreachable("missed a StringLiteral kind?"); |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Hans Wennborg | 950f318 | 2013-05-16 09:22:40 +0000 | [diff] [blame] | 134 | static StringInitFailureKind IsStringInit(Expr *init, QualType declType, |
| 135 | ASTContext &Context) { |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 136 | const ArrayType *arrayType = Context.getAsArrayType(declType); |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 137 | if (!arrayType) |
Hans Wennborg | 950f318 | 2013-05-16 09:22:40 +0000 | [diff] [blame] | 138 | return SIF_Other; |
| 139 | return IsStringInit(init, arrayType, Context); |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 142 | /// Update the type of a string literal, including any surrounding parentheses, |
| 143 | /// to match the type of the object which it is initializing. |
| 144 | static void updateStringLiteralType(Expr *E, QualType Ty) { |
Richard Smith | d74b1606 | 2013-05-06 00:35:47 +0000 | [diff] [blame] | 145 | while (true) { |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 146 | E->setType(Ty); |
Richard Smith | d74b1606 | 2013-05-06 00:35:47 +0000 | [diff] [blame] | 147 | if (isa<StringLiteral>(E) || isa<ObjCEncodeExpr>(E)) |
| 148 | break; |
| 149 | else if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) |
| 150 | E = PE->getSubExpr(); |
| 151 | else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) |
| 152 | E = UO->getSubExpr(); |
| 153 | else if (GenericSelectionExpr *GSE = dyn_cast<GenericSelectionExpr>(E)) |
| 154 | E = GSE->getResultExpr(); |
| 155 | else |
| 156 | llvm_unreachable("unexpected expr in string literal init"); |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 157 | } |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 158 | } |
| 159 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 160 | static void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT, |
| 161 | Sema &S) { |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 162 | // Get the length of the string as parsed. |
Ben Langmuir | 577b393 | 2015-01-26 19:04:10 +0000 | [diff] [blame] | 163 | auto *ConstantArrayTy = |
Ben Langmuir | 7b30f53 | 2015-01-26 20:01:17 +0000 | [diff] [blame] | 164 | cast<ConstantArrayType>(Str->getType()->getAsArrayTypeUnsafe()); |
Ben Langmuir | 577b393 | 2015-01-26 19:04:10 +0000 | [diff] [blame] | 165 | uint64_t StrLength = ConstantArrayTy->getSize().getZExtValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 166 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 167 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 168 | // C99 6.7.8p14. We have an array of character type with unknown size |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 169 | // being initialized to a string literal. |
Benjamin Kramer | e073177 | 2012-08-04 17:00:46 +0000 | [diff] [blame] | 170 | llvm::APInt ConstVal(32, StrLength); |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 171 | // Return a new array type (C99 6.7.8p22). |
John McCall | c5b8225 | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 172 | DeclT = S.Context.getConstantArrayType(IAT->getElementType(), |
| 173 | ConstVal, |
| 174 | ArrayType::Normal, 0); |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 175 | updateStringLiteralType(Str, DeclT); |
Chris Lattner | 94e6c4b | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 176 | return; |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 177 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 178 | |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 179 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 180 | |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 181 | // We have an array of character type with known size. However, |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 182 | // the size may be smaller or larger than the string we are initializing. |
| 183 | // FIXME: Avoid truncation for 64-bit length strings. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 184 | if (S.getLangOpts().CPlusPlus) { |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 185 | if (StringLiteral *SL = dyn_cast<StringLiteral>(Str->IgnoreParens())) { |
Anders Carlsson | d162fb8 | 2011-04-14 00:41:11 +0000 | [diff] [blame] | 186 | // For Pascal strings it's OK to strip off the terminating null character, |
| 187 | // so the example below is valid: |
| 188 | // |
| 189 | // unsigned char a[2] = "\pa"; |
| 190 | if (SL->isPascal()) |
| 191 | StrLength--; |
| 192 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 193 | |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 194 | // [dcl.init.string]p2 |
| 195 | if (StrLength > CAT->getSize().getZExtValue()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 196 | S.Diag(Str->getBeginLoc(), |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 197 | diag::err_initializer_string_for_char_array_too_long) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 198 | << Str->getSourceRange(); |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 199 | } else { |
| 200 | // C99 6.7.8p14. |
| 201 | if (StrLength-1 > CAT->getSize().getZExtValue()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 202 | S.Diag(Str->getBeginLoc(), |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 203 | diag::ext_initializer_string_for_char_array_too_long) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 204 | << Str->getSourceRange(); |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 205 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 206 | |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 207 | // Set the type to the actual size that we are initializing. If we have |
| 208 | // something like: |
| 209 | // char x[1] = "foo"; |
| 210 | // then this will set the string literal's type to char[1]. |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 211 | updateStringLiteralType(Str, DeclT); |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 214 | //===----------------------------------------------------------------------===// |
| 215 | // Semantic checking for initializer lists. |
| 216 | //===----------------------------------------------------------------------===// |
| 217 | |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 218 | namespace { |
| 219 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 220 | /// Semantic checking for initializer lists. |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 221 | /// |
| 222 | /// The InitListChecker class contains a set of routines that each |
| 223 | /// handle the initialization of a certain kind of entity, e.g., |
| 224 | /// arrays, vectors, struct/union types, scalars, etc. The |
| 225 | /// InitListChecker itself performs a recursive walk of the subobject |
| 226 | /// structure of the type to be initialized, while stepping through |
| 227 | /// the initializer list one element at a time. The IList and Index |
| 228 | /// parameters to each of the Check* routines contain the active |
| 229 | /// (syntactic) initializer list and the index into that initializer |
| 230 | /// list that represents the current initializer. Each routine is |
| 231 | /// responsible for moving that Index forward as it consumes elements. |
| 232 | /// |
| 233 | /// Each Check* routine also has a StructuredList/StructuredIndex |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 234 | /// arguments, which contains the current "structured" (semantic) |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 235 | /// initializer list and the index into that initializer list where we |
| 236 | /// are copying initializers as we map them over to the semantic |
| 237 | /// list. Once we have completed our recursive walk of the subobject |
| 238 | /// structure, we will have constructed a full semantic initializer |
| 239 | /// list. |
| 240 | /// |
| 241 | /// C99 designators cause changes in the initializer list traversal, |
| 242 | /// because they make the initialization "jump" into a specific |
| 243 | /// subobject and then continue the initialization from that |
| 244 | /// point. CheckDesignatedInitializer() recursively steps into the |
| 245 | /// designated subobject and manages backing out the recursion to |
| 246 | /// initialize the subobjects after the one designated. |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 247 | class InitListChecker { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 248 | Sema &SemaRef; |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 249 | bool hadError; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 250 | bool VerifyOnly; // no diagnostics, no structure building |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 251 | bool TreatUnavailableAsInvalid; // Used only in VerifyOnly mode. |
Benjamin Kramer | 6b441d6 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 252 | llvm::DenseMap<InitListExpr *, InitListExpr *> SyntacticToSemantic; |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 253 | InitListExpr *FullyStructuredList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 254 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 255 | void CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 256 | InitListExpr *ParentIList, QualType T, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 257 | unsigned &Index, InitListExpr *StructuredList, |
Eli Friedman | c616c5f | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 258 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 259 | void CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 260 | InitListExpr *IList, QualType &T, |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 261 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 262 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 263 | void CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 264 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 265 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 266 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 267 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 268 | unsigned &StructuredIndex, |
| 269 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 270 | void CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 271 | InitListExpr *IList, QualType ElemType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 272 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 273 | InitListExpr *StructuredList, |
| 274 | unsigned &StructuredIndex); |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 275 | void CheckComplexType(const InitializedEntity &Entity, |
| 276 | InitListExpr *IList, QualType DeclType, |
| 277 | unsigned &Index, |
| 278 | InitListExpr *StructuredList, |
| 279 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 280 | void CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 281 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 282 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 283 | InitListExpr *StructuredList, |
| 284 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 285 | void CheckReferenceType(const InitializedEntity &Entity, |
| 286 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 287 | unsigned &Index, |
| 288 | InitListExpr *StructuredList, |
| 289 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 290 | void CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 291 | InitListExpr *IList, QualType DeclType, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 292 | InitListExpr *StructuredList, |
| 293 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 294 | void CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 73eb7cd | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 295 | InitListExpr *IList, QualType DeclType, |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 296 | CXXRecordDecl::base_class_range Bases, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 297 | RecordDecl::field_iterator Field, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 298 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 299 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 300 | unsigned &StructuredIndex, |
| 301 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 302 | void CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 303 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 304 | llvm::APSInt elementIndex, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 305 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 306 | InitListExpr *StructuredList, |
| 307 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 308 | bool CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 309 | InitListExpr *IList, DesignatedInitExpr *DIE, |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 310 | unsigned DesigIdx, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 311 | QualType &CurrentObjectType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 312 | RecordDecl::field_iterator *NextField, |
| 313 | llvm::APSInt *NextElementIndex, |
| 314 | unsigned &Index, |
| 315 | InitListExpr *StructuredList, |
| 316 | unsigned &StructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 317 | bool FinishSubobjectInit, |
| 318 | bool TopLevelObject); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 319 | InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 320 | QualType CurrentObjectType, |
| 321 | InitListExpr *StructuredList, |
| 322 | unsigned StructuredIndex, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 323 | SourceRange InitRange, |
| 324 | bool IsFullyOverwritten = false); |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 325 | void UpdateStructuredListElement(InitListExpr *StructuredList, |
| 326 | unsigned &StructuredIndex, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 327 | Expr *expr); |
| 328 | int numArrayElements(QualType DeclType); |
| 329 | int numStructUnionElements(QualType DeclType); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 330 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 331 | static ExprResult PerformEmptyInit(Sema &SemaRef, |
| 332 | SourceLocation Loc, |
| 333 | const InitializedEntity &Entity, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 334 | bool VerifyOnly, |
| 335 | bool TreatUnavailableAsInvalid); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 336 | |
| 337 | // Explanation on the "FillWithNoInit" mode: |
| 338 | // |
| 339 | // Assume we have the following definitions (Case#1): |
| 340 | // struct P { char x[6][6]; } xp = { .x[1] = "bar" }; |
| 341 | // struct PP { struct P lp; } l = { .lp = xp, .lp.x[1][2] = 'f' }; |
| 342 | // |
| 343 | // l.lp.x[1][0..1] should not be filled with implicit initializers because the |
| 344 | // "base" initializer "xp" will provide values for them; l.lp.x[1] will be "baf". |
| 345 | // |
| 346 | // But if we have (Case#2): |
| 347 | // struct PP l = { .lp = xp, .lp.x[1] = { [2] = 'f' } }; |
| 348 | // |
| 349 | // l.lp.x[1][0..1] are implicitly initialized and do not use values from the |
| 350 | // "base" initializer; l.lp.x[1] will be "\0\0f\0\0\0". |
| 351 | // |
| 352 | // To distinguish Case#1 from Case#2, and also to avoid leaving many "holes" |
| 353 | // in the InitListExpr, the "holes" in Case#1 are filled not with empty |
| 354 | // initializers but with special "NoInitExpr" place holders, which tells the |
| 355 | // CodeGen not to generate any initializers for these parts. |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 356 | void FillInEmptyInitForBase(unsigned Init, const CXXBaseSpecifier &Base, |
| 357 | const InitializedEntity &ParentEntity, |
| 358 | InitListExpr *ILE, bool &RequiresSecondPass, |
| 359 | bool FillWithNoInit); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 360 | void FillInEmptyInitForField(unsigned Init, FieldDecl *Field, |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 361 | const InitializedEntity &ParentEntity, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 362 | InitListExpr *ILE, bool &RequiresSecondPass, |
| 363 | bool FillWithNoInit = false); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 364 | void FillInEmptyInitializations(const InitializedEntity &Entity, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 365 | InitListExpr *ILE, bool &RequiresSecondPass, |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 366 | InitListExpr *OuterILE, unsigned OuterIndex, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 367 | bool FillWithNoInit = false); |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 368 | bool CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 369 | Expr *InitExpr, FieldDecl *Field, |
| 370 | bool TopLevelObject); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 371 | void CheckEmptyInitializable(const InitializedEntity &Entity, |
| 372 | SourceLocation Loc); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 373 | |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 374 | public: |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 375 | InitListChecker(Sema &S, const InitializedEntity &Entity, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 376 | InitListExpr *IL, QualType &T, bool VerifyOnly, |
| 377 | bool TreatUnavailableAsInvalid); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 378 | bool HadError() { return hadError; } |
| 379 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 380 | // Retrieves the fully-structured initializer list used for |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 381 | // semantic analysis and code generation. |
| 382 | InitListExpr *getFullyStructuredList() const { return FullyStructuredList; } |
| 383 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 384 | |
Chris Lattner | 9ececce | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 385 | } // end anonymous namespace |
Chris Lattner | d9ae05b | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 386 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 387 | ExprResult InitListChecker::PerformEmptyInit(Sema &SemaRef, |
| 388 | SourceLocation Loc, |
| 389 | const InitializedEntity &Entity, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 390 | bool VerifyOnly, |
| 391 | bool TreatUnavailableAsInvalid) { |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 392 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 393 | true); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 394 | MultiExprArg SubInit; |
| 395 | Expr *InitExpr; |
| 396 | InitListExpr DummyInitList(SemaRef.Context, Loc, None, Loc); |
| 397 | |
| 398 | // C++ [dcl.init.aggr]p7: |
| 399 | // If there are fewer initializer-clauses in the list than there are |
| 400 | // members in the aggregate, then each member not explicitly initialized |
| 401 | // ... |
Nico Weber | bcb70ee | 2014-07-02 23:51:09 +0000 | [diff] [blame] | 402 | bool EmptyInitList = SemaRef.getLangOpts().CPlusPlus11 && |
| 403 | Entity.getType()->getBaseElementTypeUnsafe()->isRecordType(); |
| 404 | if (EmptyInitList) { |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 405 | // C++1y / DR1070: |
| 406 | // shall be initialized [...] from an empty initializer list. |
| 407 | // |
| 408 | // We apply the resolution of this DR to C++11 but not C++98, since C++98 |
| 409 | // does not have useful semantics for initialization from an init list. |
| 410 | // We treat this as copy-initialization, because aggregate initialization |
| 411 | // always performs copy-initialization on its elements. |
| 412 | // |
| 413 | // Only do this if we're initializing a class type, to avoid filling in |
| 414 | // the initializer list where possible. |
| 415 | InitExpr = VerifyOnly ? &DummyInitList : new (SemaRef.Context) |
| 416 | InitListExpr(SemaRef.Context, Loc, None, Loc); |
| 417 | InitExpr->setType(SemaRef.Context.VoidTy); |
| 418 | SubInit = InitExpr; |
| 419 | Kind = InitializationKind::CreateCopy(Loc, Loc); |
| 420 | } else { |
| 421 | // C++03: |
| 422 | // shall be value-initialized. |
| 423 | } |
| 424 | |
| 425 | InitializationSequence InitSeq(SemaRef, Entity, Kind, SubInit); |
Nico Weber | bcb70ee | 2014-07-02 23:51:09 +0000 | [diff] [blame] | 426 | // libstdc++4.6 marks the vector default constructor as explicit in |
| 427 | // _GLIBCXX_DEBUG mode, so recover using the C++03 logic in that case. |
| 428 | // stlport does so too. Look for std::__debug for libstdc++, and for |
| 429 | // std:: for stlport. This is effectively a compiler-side implementation of |
| 430 | // LWG2193. |
| 431 | if (!InitSeq && EmptyInitList && InitSeq.getFailureKind() == |
| 432 | InitializationSequence::FK_ExplicitConstructor) { |
| 433 | OverloadCandidateSet::iterator Best; |
| 434 | OverloadingResult O = |
| 435 | InitSeq.getFailedCandidateSet() |
| 436 | .BestViableFunction(SemaRef, Kind.getLocation(), Best); |
| 437 | (void)O; |
| 438 | assert(O == OR_Success && "Inconsistent overload resolution"); |
| 439 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 440 | CXXRecordDecl *R = CtorDecl->getParent(); |
| 441 | |
| 442 | if (CtorDecl->getMinRequiredArguments() == 0 && |
| 443 | CtorDecl->isExplicit() && R->getDeclName() && |
| 444 | SemaRef.SourceMgr.isInSystemHeader(CtorDecl->getLocation())) { |
Nico Weber | bcb70ee | 2014-07-02 23:51:09 +0000 | [diff] [blame] | 445 | bool IsInStd = false; |
| 446 | for (NamespaceDecl *ND = dyn_cast<NamespaceDecl>(R->getDeclContext()); |
Nico Weber | 5752ad0 | 2014-07-03 00:38:25 +0000 | [diff] [blame] | 447 | ND && !IsInStd; ND = dyn_cast<NamespaceDecl>(ND->getParent())) { |
Nico Weber | bcb70ee | 2014-07-02 23:51:09 +0000 | [diff] [blame] | 448 | if (SemaRef.getStdNamespace()->InEnclosingNamespaceSetOf(ND)) |
| 449 | IsInStd = true; |
| 450 | } |
| 451 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 452 | if (IsInStd && llvm::StringSwitch<bool>(R->getName()) |
Nico Weber | bcb70ee | 2014-07-02 23:51:09 +0000 | [diff] [blame] | 453 | .Cases("basic_string", "deque", "forward_list", true) |
| 454 | .Cases("list", "map", "multimap", "multiset", true) |
| 455 | .Cases("priority_queue", "queue", "set", "stack", true) |
| 456 | .Cases("unordered_map", "unordered_set", "vector", true) |
| 457 | .Default(false)) { |
| 458 | InitSeq.InitializeFrom( |
| 459 | SemaRef, Entity, |
| 460 | InitializationKind::CreateValue(Loc, Loc, Loc, true), |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 461 | MultiExprArg(), /*TopLevelOfInitList=*/false, |
| 462 | TreatUnavailableAsInvalid); |
Nico Weber | bcb70ee | 2014-07-02 23:51:09 +0000 | [diff] [blame] | 463 | // Emit a warning for this. System header warnings aren't shown |
| 464 | // by default, but people working on system headers should see it. |
| 465 | if (!VerifyOnly) { |
| 466 | SemaRef.Diag(CtorDecl->getLocation(), |
| 467 | diag::warn_invalid_initializer_from_system_header); |
David Majnemer | 9588a95 | 2015-08-21 06:44:10 +0000 | [diff] [blame] | 468 | if (Entity.getKind() == InitializedEntity::EK_Member) |
| 469 | SemaRef.Diag(Entity.getDecl()->getLocation(), |
| 470 | diag::note_used_in_initialization_here); |
| 471 | else if (Entity.getKind() == InitializedEntity::EK_ArrayElement) |
| 472 | SemaRef.Diag(Loc, diag::note_used_in_initialization_here); |
Nico Weber | bcb70ee | 2014-07-02 23:51:09 +0000 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | } |
| 476 | } |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 477 | if (!InitSeq) { |
| 478 | if (!VerifyOnly) { |
| 479 | InitSeq.Diagnose(SemaRef, Entity, Kind, SubInit); |
| 480 | if (Entity.getKind() == InitializedEntity::EK_Member) |
| 481 | SemaRef.Diag(Entity.getDecl()->getLocation(), |
| 482 | diag::note_in_omitted_aggregate_initializer) |
| 483 | << /*field*/1 << Entity.getDecl(); |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 484 | else if (Entity.getKind() == InitializedEntity::EK_ArrayElement) { |
| 485 | bool IsTrailingArrayNewMember = |
| 486 | Entity.getParent() && |
| 487 | Entity.getParent()->isVariableLengthArrayNew(); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 488 | SemaRef.Diag(Loc, diag::note_in_omitted_aggregate_initializer) |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 489 | << (IsTrailingArrayNewMember ? 2 : /*array element*/0) |
| 490 | << Entity.getElementIndex(); |
| 491 | } |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 492 | } |
| 493 | return ExprError(); |
| 494 | } |
| 495 | |
| 496 | return VerifyOnly ? ExprResult(static_cast<Expr *>(nullptr)) |
| 497 | : InitSeq.Perform(SemaRef, Entity, Kind, SubInit); |
| 498 | } |
| 499 | |
| 500 | void InitListChecker::CheckEmptyInitializable(const InitializedEntity &Entity, |
| 501 | SourceLocation Loc) { |
| 502 | assert(VerifyOnly && |
| 503 | "CheckEmptyInitializable is only inteded for verification mode."); |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 504 | if (PerformEmptyInit(SemaRef, Loc, Entity, /*VerifyOnly*/true, |
| 505 | TreatUnavailableAsInvalid).isInvalid()) |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 506 | hadError = true; |
| 507 | } |
| 508 | |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 509 | void InitListChecker::FillInEmptyInitForBase( |
| 510 | unsigned Init, const CXXBaseSpecifier &Base, |
| 511 | const InitializedEntity &ParentEntity, InitListExpr *ILE, |
| 512 | bool &RequiresSecondPass, bool FillWithNoInit) { |
| 513 | assert(Init < ILE->getNumInits() && "should have been expanded"); |
| 514 | |
| 515 | InitializedEntity BaseEntity = InitializedEntity::InitializeBase( |
| 516 | SemaRef.Context, &Base, false, &ParentEntity); |
| 517 | |
| 518 | if (!ILE->getInit(Init)) { |
| 519 | ExprResult BaseInit = |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 520 | FillWithNoInit |
| 521 | ? new (SemaRef.Context) NoInitExpr(Base.getType()) |
| 522 | : PerformEmptyInit(SemaRef, ILE->getEndLoc(), BaseEntity, |
| 523 | /*VerifyOnly*/ false, TreatUnavailableAsInvalid); |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 524 | if (BaseInit.isInvalid()) { |
| 525 | hadError = true; |
| 526 | return; |
| 527 | } |
| 528 | |
| 529 | ILE->setInit(Init, BaseInit.getAs<Expr>()); |
| 530 | } else if (InitListExpr *InnerILE = |
| 531 | dyn_cast<InitListExpr>(ILE->getInit(Init))) { |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 532 | FillInEmptyInitializations(BaseEntity, InnerILE, RequiresSecondPass, |
| 533 | ILE, Init, FillWithNoInit); |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 534 | } else if (DesignatedInitUpdateExpr *InnerDIUE = |
| 535 | dyn_cast<DesignatedInitUpdateExpr>(ILE->getInit(Init))) { |
| 536 | FillInEmptyInitializations(BaseEntity, InnerDIUE->getUpdater(), |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 537 | RequiresSecondPass, ILE, Init, |
| 538 | /*FillWithNoInit =*/true); |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 542 | void InitListChecker::FillInEmptyInitForField(unsigned Init, FieldDecl *Field, |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 543 | const InitializedEntity &ParentEntity, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 544 | InitListExpr *ILE, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 545 | bool &RequiresSecondPass, |
| 546 | bool FillWithNoInit) { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 547 | SourceLocation Loc = ILE->getEndLoc(); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 548 | unsigned NumInits = ILE->getNumInits(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 549 | InitializedEntity MemberEntity |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 550 | = InitializedEntity::InitializeMember(Field, &ParentEntity); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 551 | |
| 552 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) |
| 553 | if (!RType->getDecl()->isUnion()) |
| 554 | assert(Init < NumInits && "This ILE should have been expanded"); |
| 555 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 556 | if (Init >= NumInits || !ILE->getInit(Init)) { |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 557 | if (FillWithNoInit) { |
| 558 | Expr *Filler = new (SemaRef.Context) NoInitExpr(Field->getType()); |
| 559 | if (Init < NumInits) |
| 560 | ILE->setInit(Init, Filler); |
| 561 | else |
| 562 | ILE->updateInit(SemaRef.Context, Init, Filler); |
| 563 | return; |
| 564 | } |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 565 | // C++1y [dcl.init.aggr]p7: |
| 566 | // If there are fewer initializer-clauses in the list than there are |
| 567 | // members in the aggregate, then each member not explicitly initialized |
| 568 | // shall be initialized from its brace-or-equal-initializer [...] |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 569 | if (Field->hasInClassInitializer()) { |
Reid Kleckner | d60b82f | 2014-11-17 23:36:45 +0000 | [diff] [blame] | 570 | ExprResult DIE = SemaRef.BuildCXXDefaultInitExpr(Loc, Field); |
| 571 | if (DIE.isInvalid()) { |
| 572 | hadError = true; |
| 573 | return; |
| 574 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 575 | SemaRef.checkInitializerLifetime(MemberEntity, DIE.get()); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 576 | if (Init < NumInits) |
Reid Kleckner | d60b82f | 2014-11-17 23:36:45 +0000 | [diff] [blame] | 577 | ILE->setInit(Init, DIE.get()); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 578 | else { |
Reid Kleckner | d60b82f | 2014-11-17 23:36:45 +0000 | [diff] [blame] | 579 | ILE->updateInit(SemaRef.Context, Init, DIE.get()); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 580 | RequiresSecondPass = true; |
| 581 | } |
| 582 | return; |
| 583 | } |
| 584 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 585 | if (Field->getType()->isReferenceType()) { |
| 586 | // C++ [dcl.init.aggr]p9: |
| 587 | // If an incomplete or empty initializer-list leaves a |
| 588 | // member of reference type uninitialized, the program is |
| 589 | // ill-formed. |
| 590 | SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized) |
| 591 | << Field->getType() |
| 592 | << ILE->getSyntacticForm()->getSourceRange(); |
| 593 | SemaRef.Diag(Field->getLocation(), |
| 594 | diag::note_uninit_reference_member); |
| 595 | hadError = true; |
| 596 | return; |
| 597 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 598 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 599 | ExprResult MemberInit = PerformEmptyInit(SemaRef, Loc, MemberEntity, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 600 | /*VerifyOnly*/false, |
| 601 | TreatUnavailableAsInvalid); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 602 | if (MemberInit.isInvalid()) { |
| 603 | hadError = true; |
| 604 | return; |
| 605 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 606 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 607 | if (hadError) { |
| 608 | // Do nothing |
| 609 | } else if (Init < NumInits) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 610 | ILE->setInit(Init, MemberInit.getAs<Expr>()); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 611 | } else if (!isa<ImplicitValueInitExpr>(MemberInit.get())) { |
| 612 | // Empty initialization requires a constructor call, so |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 613 | // extend the initializer list to include the constructor |
| 614 | // call and make a note that we'll need to take another pass |
| 615 | // through the initializer list. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 616 | ILE->updateInit(SemaRef.Context, Init, MemberInit.getAs<Expr>()); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 617 | RequiresSecondPass = true; |
| 618 | } |
| 619 | } else if (InitListExpr *InnerILE |
| 620 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 621 | FillInEmptyInitializations(MemberEntity, InnerILE, |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 622 | RequiresSecondPass, ILE, Init, FillWithNoInit); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 623 | else if (DesignatedInitUpdateExpr *InnerDIUE |
| 624 | = dyn_cast<DesignatedInitUpdateExpr>(ILE->getInit(Init))) |
| 625 | FillInEmptyInitializations(MemberEntity, InnerDIUE->getUpdater(), |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 626 | RequiresSecondPass, ILE, Init, |
| 627 | /*FillWithNoInit =*/true); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 630 | /// Recursively replaces NULL values within the given initializer list |
| 631 | /// with expressions that perform value-initialization of the |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 632 | /// appropriate type, and finish off the InitListExpr formation. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 633 | void |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 634 | InitListChecker::FillInEmptyInitializations(const InitializedEntity &Entity, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 635 | InitListExpr *ILE, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 636 | bool &RequiresSecondPass, |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 637 | InitListExpr *OuterILE, |
| 638 | unsigned OuterIndex, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 639 | bool FillWithNoInit) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 640 | assert((ILE->getType() != SemaRef.Context.VoidTy) && |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 641 | "Should not have void type"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 642 | |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 643 | // If this is a nested initializer list, we might have changed its contents |
| 644 | // (and therefore some of its properties, such as instantiation-dependence) |
| 645 | // while filling it in. Inform the outer initializer list so that its state |
| 646 | // can be updated to match. |
| 647 | // FIXME: We should fully build the inner initializers before constructing |
| 648 | // the outer InitListExpr instead of mutating AST nodes after they have |
| 649 | // been used as subexpressions of other nodes. |
| 650 | struct UpdateOuterILEWithUpdatedInit { |
| 651 | InitListExpr *Outer; |
| 652 | unsigned OuterIndex; |
| 653 | ~UpdateOuterILEWithUpdatedInit() { |
| 654 | if (Outer) |
| 655 | Outer->setInit(OuterIndex, Outer->getInit(OuterIndex)); |
| 656 | } |
| 657 | } UpdateOuterRAII = {OuterILE, OuterIndex}; |
| 658 | |
Richard Smith | 382bc51 | 2017-02-23 22:41:47 +0000 | [diff] [blame] | 659 | // A transparent ILE is not performing aggregate initialization and should |
| 660 | // not be filled in. |
| 661 | if (ILE->isTransparent()) |
| 662 | return; |
| 663 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 664 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 665 | const RecordDecl *RDecl = RType->getDecl(); |
| 666 | if (RDecl->isUnion() && ILE->getInitializedFieldInUnion()) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 667 | FillInEmptyInitForField(0, ILE->getInitializedFieldInUnion(), |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 668 | Entity, ILE, RequiresSecondPass, FillWithNoInit); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 669 | else if (RDecl->isUnion() && isa<CXXRecordDecl>(RDecl) && |
| 670 | cast<CXXRecordDecl>(RDecl)->hasInClassInitializer()) { |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 671 | for (auto *Field : RDecl->fields()) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 672 | if (Field->hasInClassInitializer()) { |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 673 | FillInEmptyInitForField(0, Field, Entity, ILE, RequiresSecondPass, |
| 674 | FillWithNoInit); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 675 | break; |
| 676 | } |
| 677 | } |
| 678 | } else { |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 679 | // The fields beyond ILE->getNumInits() are default initialized, so in |
| 680 | // order to leave them uninitialized, the ILE is expanded and the extra |
| 681 | // fields are then filled with NoInitExpr. |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 682 | unsigned NumElems = numStructUnionElements(ILE->getType()); |
| 683 | if (RDecl->hasFlexibleArrayMember()) |
| 684 | ++NumElems; |
| 685 | if (ILE->getNumInits() < NumElems) |
| 686 | ILE->resizeInits(SemaRef.Context, NumElems); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 687 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 688 | unsigned Init = 0; |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 689 | |
| 690 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RDecl)) { |
| 691 | for (auto &Base : CXXRD->bases()) { |
| 692 | if (hadError) |
| 693 | return; |
| 694 | |
| 695 | FillInEmptyInitForBase(Init, Base, Entity, ILE, RequiresSecondPass, |
| 696 | FillWithNoInit); |
| 697 | ++Init; |
| 698 | } |
| 699 | } |
| 700 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 701 | for (auto *Field : RDecl->fields()) { |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 702 | if (Field->isUnnamedBitfield()) |
| 703 | continue; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 704 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 705 | if (hadError) |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 706 | return; |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 707 | |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 708 | FillInEmptyInitForField(Init, Field, Entity, ILE, RequiresSecondPass, |
| 709 | FillWithNoInit); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 710 | if (hadError) |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 711 | return; |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 712 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 713 | ++Init; |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 714 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 715 | // Only look at the first initialization of a union. |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 716 | if (RDecl->isUnion()) |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 717 | break; |
| 718 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 722 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 723 | |
| 724 | QualType ElementType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 725 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 726 | InitializedEntity ElementEntity = Entity; |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 727 | unsigned NumInits = ILE->getNumInits(); |
| 728 | unsigned NumElements = NumInits; |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 729 | if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 730 | ElementType = AType->getElementType(); |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 731 | if (const auto *CAType = dyn_cast<ConstantArrayType>(AType)) |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 732 | NumElements = CAType->getSize().getZExtValue(); |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 733 | // For an array new with an unknown bound, ask for one additional element |
| 734 | // in order to populate the array filler. |
| 735 | if (Entity.isVariableLengthArrayNew()) |
| 736 | ++NumElements; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 737 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 738 | 0, Entity); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 739 | } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 740 | ElementType = VType->getElementType(); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 741 | NumElements = VType->getNumElements(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 742 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 743 | 0, Entity); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 744 | } else |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 745 | ElementType = ILE->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 746 | |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 747 | for (unsigned Init = 0; Init != NumElements; ++Init) { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 748 | if (hadError) |
| 749 | return; |
| 750 | |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 751 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement || |
| 752 | ElementEntity.getKind() == InitializedEntity::EK_VectorElement) |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 753 | ElementEntity.setElementIndex(Init); |
| 754 | |
Richard Smith | 3e26863 | 2018-05-23 23:41:38 +0000 | [diff] [blame] | 755 | if (Init >= NumInits && ILE->hasArrayFiller()) |
| 756 | return; |
| 757 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 758 | Expr *InitExpr = (Init < NumInits ? ILE->getInit(Init) : nullptr); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 759 | if (!InitExpr && Init < NumInits && ILE->hasArrayFiller()) |
| 760 | ILE->setInit(Init, ILE->getArrayFiller()); |
| 761 | else if (!InitExpr && !ILE->hasArrayFiller()) { |
| 762 | Expr *Filler = nullptr; |
| 763 | |
| 764 | if (FillWithNoInit) |
| 765 | Filler = new (SemaRef.Context) NoInitExpr(ElementType); |
| 766 | else { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 767 | ExprResult ElementInit = |
| 768 | PerformEmptyInit(SemaRef, ILE->getEndLoc(), ElementEntity, |
| 769 | /*VerifyOnly*/ false, TreatUnavailableAsInvalid); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 770 | if (ElementInit.isInvalid()) { |
| 771 | hadError = true; |
| 772 | return; |
| 773 | } |
| 774 | |
| 775 | Filler = ElementInit.getAs<Expr>(); |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | if (hadError) { |
| 779 | // Do nothing |
| 780 | } else if (Init < NumInits) { |
Argyrios Kyrtzidis | 446bcf2 | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 781 | // For arrays, just set the expression used for value-initialization |
| 782 | // of the "holes" in the array. |
| 783 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 784 | ILE->setArrayFiller(Filler); |
Argyrios Kyrtzidis | 446bcf2 | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 785 | else |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 786 | ILE->setInit(Init, Filler); |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 787 | } else { |
| 788 | // For arrays, just set the expression used for value-initialization |
| 789 | // of the rest of elements and exit. |
| 790 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) { |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 791 | ILE->setArrayFiller(Filler); |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 792 | return; |
| 793 | } |
| 794 | |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 795 | if (!isa<ImplicitValueInitExpr>(Filler) && !isa<NoInitExpr>(Filler)) { |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 796 | // Empty initialization requires a constructor call, so |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 797 | // extend the initializer list to include the constructor |
| 798 | // call and make a note that we'll need to take another pass |
| 799 | // through the initializer list. |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 800 | ILE->updateInit(SemaRef.Context, Init, Filler); |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 801 | RequiresSecondPass = true; |
| 802 | } |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 803 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 804 | } else if (InitListExpr *InnerILE |
Argyrios Kyrtzidis | d4590a5d | 2011-10-21 23:02:22 +0000 | [diff] [blame] | 805 | = dyn_cast_or_null<InitListExpr>(InitExpr)) |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 806 | FillInEmptyInitializations(ElementEntity, InnerILE, RequiresSecondPass, |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 807 | ILE, Init, FillWithNoInit); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 808 | else if (DesignatedInitUpdateExpr *InnerDIUE |
| 809 | = dyn_cast_or_null<DesignatedInitUpdateExpr>(InitExpr)) |
| 810 | FillInEmptyInitializations(ElementEntity, InnerDIUE->getUpdater(), |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 811 | RequiresSecondPass, ILE, Init, |
| 812 | /*FillWithNoInit =*/true); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 813 | } |
| 814 | } |
| 815 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 816 | InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 817 | InitListExpr *IL, QualType &T, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 818 | bool VerifyOnly, |
| 819 | bool TreatUnavailableAsInvalid) |
| 820 | : SemaRef(S), VerifyOnly(VerifyOnly), |
| 821 | TreatUnavailableAsInvalid(TreatUnavailableAsInvalid) { |
Richard Smith | 520449d | 2015-02-05 06:15:50 +0000 | [diff] [blame] | 822 | // FIXME: Check that IL isn't already the semantic form of some other |
| 823 | // InitListExpr. If it is, we'd create a broken AST. |
| 824 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 825 | hadError = false; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 826 | |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 827 | FullyStructuredList = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 828 | getStructuredSubobjectInit(IL, 0, T, nullptr, 0, IL->getSourceRange()); |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 829 | CheckExplicitInitList(Entity, IL, T, FullyStructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 830 | /*TopLevelObject=*/true); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 831 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 832 | if (!hadError && !VerifyOnly) { |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 833 | bool RequiresSecondPass = false; |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 834 | FillInEmptyInitializations(Entity, FullyStructuredList, RequiresSecondPass, |
| 835 | /*OuterILE=*/nullptr, /*OuterIndex=*/0); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 836 | if (RequiresSecondPass && !hadError) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 837 | FillInEmptyInitializations(Entity, FullyStructuredList, |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 838 | RequiresSecondPass, nullptr, 0); |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 839 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 843 | // FIXME: use a proper constant |
| 844 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 845 | if (const ConstantArrayType *CAT = |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 846 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 847 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 848 | } |
| 849 | return maxElements; |
| 850 | } |
| 851 | |
| 852 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 853 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 854 | int InitializableMembers = 0; |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 855 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(structDecl)) |
| 856 | InitializableMembers += CXXRD->getNumBases(); |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 857 | for (const auto *Field : structDecl->fields()) |
Douglas Gregor | 556e586 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 858 | if (!Field->isUnnamedBitfield()) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 859 | ++InitializableMembers; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 860 | |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 861 | if (structDecl->isUnion()) |
Eli Friedman | 0e56c82 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 862 | return std::min(InitializableMembers, 1); |
| 863 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 864 | } |
| 865 | |
Richard Smith | 283e207 | 2017-10-03 20:36:00 +0000 | [diff] [blame] | 866 | /// Determine whether Entity is an entity for which it is idiomatic to elide |
| 867 | /// the braces in aggregate initialization. |
| 868 | static bool isIdiomaticBraceElisionEntity(const InitializedEntity &Entity) { |
| 869 | // Recursive initialization of the one and only field within an aggregate |
| 870 | // class is considered idiomatic. This case arises in particular for |
| 871 | // initialization of std::array, where the C++ standard suggests the idiom of |
| 872 | // |
| 873 | // std::array<T, N> arr = {1, 2, 3}; |
| 874 | // |
| 875 | // (where std::array is an aggregate struct containing a single array field. |
| 876 | |
| 877 | // FIXME: Should aggregate initialization of a struct with a single |
| 878 | // base class and no members also suppress the warning? |
| 879 | if (Entity.getKind() != InitializedEntity::EK_Member || !Entity.getParent()) |
| 880 | return false; |
| 881 | |
| 882 | auto *ParentRD = |
| 883 | Entity.getParent()->getType()->castAs<RecordType>()->getDecl(); |
| 884 | if (CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(ParentRD)) |
| 885 | if (CXXRD->getNumBases()) |
| 886 | return false; |
| 887 | |
| 888 | auto FieldIt = ParentRD->field_begin(); |
| 889 | assert(FieldIt != ParentRD->field_end() && |
| 890 | "no fields but have initializer for member?"); |
| 891 | return ++FieldIt == ParentRD->field_end(); |
| 892 | } |
| 893 | |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 894 | /// Check whether the range of the initializer \p ParentIList from element |
| 895 | /// \p Index onwards can be used to initialize an object of type \p T. Update |
| 896 | /// \p Index to indicate how many elements of the list were consumed. |
| 897 | /// |
| 898 | /// This also fills in \p StructuredList, from element \p StructuredIndex |
| 899 | /// onwards, with the fully-braced, desugared form of the initialization. |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 900 | void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 901 | InitListExpr *ParentIList, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 902 | QualType T, unsigned &Index, |
| 903 | InitListExpr *StructuredList, |
Eli Friedman | c616c5f | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 904 | unsigned &StructuredIndex) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 905 | int maxElements = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 906 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 907 | if (T->isArrayType()) |
| 908 | maxElements = numArrayElements(T); |
Douglas Gregor | 8385a06 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 909 | else if (T->isRecordType()) |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 910 | maxElements = numStructUnionElements(T); |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 911 | else if (T->isVectorType()) |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 912 | maxElements = T->getAs<VectorType>()->getNumElements(); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 913 | else |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 914 | llvm_unreachable("CheckImplicitInitList(): Illegal type"); |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 915 | |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 916 | if (maxElements == 0) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 917 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 918 | SemaRef.Diag(ParentIList->getInit(Index)->getBeginLoc(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 919 | diag::err_implicit_empty_initializer); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 920 | ++Index; |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 921 | hadError = true; |
| 922 | return; |
| 923 | } |
| 924 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 925 | // Build a structured initializer list corresponding to this subobject. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 926 | InitListExpr *StructuredSubobjectInitList = getStructuredSubobjectInit( |
| 927 | ParentIList, Index, T, StructuredList, StructuredIndex, |
| 928 | SourceRange(ParentIList->getInit(Index)->getBeginLoc(), |
| 929 | ParentIList->getSourceRange().getEnd())); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 930 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 931 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 932 | // Check the element types and build the structural subobject. |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 933 | unsigned StartIndex = Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 934 | CheckListElementTypes(Entity, ParentIList, T, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 935 | /*SubobjectIsDesignatorContext=*/false, Index, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 936 | StructuredSubobjectInitList, |
Eli Friedman | c616c5f | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 937 | StructuredSubobjectInitIndex); |
Sebastian Redl | 8b6412a | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 938 | |
Richard Smith | de22923 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 939 | if (!VerifyOnly) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 940 | StructuredSubobjectInitList->setType(T); |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 941 | |
Sebastian Redl | 8b6412a | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 942 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 943 | // Update the structured sub-object initializer so that it's ending |
| 944 | // range corresponds with the end of the last initializer it used. |
Reid Kleckner | 4a09e88 | 2015-12-09 23:18:38 +0000 | [diff] [blame] | 945 | if (EndIndex < ParentIList->getNumInits() && |
| 946 | ParentIList->getInit(EndIndex)) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 947 | SourceLocation EndLoc |
| 948 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 949 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 950 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 951 | |
Sebastian Redl | 8b6412a | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 952 | // Complain about missing braces. |
Daniel Marjamaki | 817a3bf | 2017-09-29 09:44:41 +0000 | [diff] [blame] | 953 | if ((T->isArrayType() || T->isRecordType()) && |
Richard Smith | 283e207 | 2017-10-03 20:36:00 +0000 | [diff] [blame] | 954 | !ParentIList->isIdiomaticZeroInitializer(SemaRef.getLangOpts()) && |
| 955 | !isIdiomaticBraceElisionEntity(Entity)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 956 | SemaRef.Diag(StructuredSubobjectInitList->getBeginLoc(), |
Richard Smith | de22923 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 957 | diag::warn_missing_braces) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 958 | << StructuredSubobjectInitList->getSourceRange() |
| 959 | << FixItHint::CreateInsertion( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 960 | StructuredSubobjectInitList->getBeginLoc(), "{") |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 961 | << FixItHint::CreateInsertion( |
| 962 | SemaRef.getLocForEndOfToken( |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 963 | StructuredSubobjectInitList->getEndLoc()), |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 964 | "}"); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 965 | } |
Richard Smith | 79c88c3 | 2018-09-26 19:00:16 +0000 | [diff] [blame] | 966 | |
| 967 | // Warn if this type won't be an aggregate in future versions of C++. |
| 968 | auto *CXXRD = T->getAsCXXRecordDecl(); |
| 969 | if (CXXRD && CXXRD->hasUserDeclaredConstructor()) { |
| 970 | SemaRef.Diag(StructuredSubobjectInitList->getBeginLoc(), |
| 971 | diag::warn_cxx2a_compat_aggregate_init_with_ctors) |
| 972 | << StructuredSubobjectInitList->getSourceRange() << T; |
| 973 | } |
Tanya Lattner | 5029d56 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 974 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Richard Smith | 420fa12 | 2015-02-12 01:50:05 +0000 | [diff] [blame] | 977 | /// Warn that \p Entity was of scalar type and was initialized by a |
| 978 | /// single-element braced initializer list. |
| 979 | static void warnBracedScalarInit(Sema &S, const InitializedEntity &Entity, |
| 980 | SourceRange Braces) { |
| 981 | // Don't warn during template instantiation. If the initialization was |
| 982 | // non-dependent, we warned during the initial parse; otherwise, the |
| 983 | // type might not be scalar in some uses of the template. |
Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 984 | if (S.inTemplateInstantiation()) |
Richard Smith | 420fa12 | 2015-02-12 01:50:05 +0000 | [diff] [blame] | 985 | return; |
| 986 | |
| 987 | unsigned DiagID = 0; |
| 988 | |
| 989 | switch (Entity.getKind()) { |
| 990 | case InitializedEntity::EK_VectorElement: |
| 991 | case InitializedEntity::EK_ComplexElement: |
| 992 | case InitializedEntity::EK_ArrayElement: |
| 993 | case InitializedEntity::EK_Parameter: |
| 994 | case InitializedEntity::EK_Parameter_CF_Audited: |
| 995 | case InitializedEntity::EK_Result: |
| 996 | // Extra braces here are suspicious. |
| 997 | DiagID = diag::warn_braces_around_scalar_init; |
| 998 | break; |
| 999 | |
| 1000 | case InitializedEntity::EK_Member: |
| 1001 | // Warn on aggregate initialization but not on ctor init list or |
| 1002 | // default member initializer. |
| 1003 | if (Entity.getParent()) |
| 1004 | DiagID = diag::warn_braces_around_scalar_init; |
| 1005 | break; |
| 1006 | |
| 1007 | case InitializedEntity::EK_Variable: |
| 1008 | case InitializedEntity::EK_LambdaCapture: |
| 1009 | // No warning, might be direct-list-initialization. |
| 1010 | // FIXME: Should we warn for copy-list-initialization in these cases? |
| 1011 | break; |
| 1012 | |
| 1013 | case InitializedEntity::EK_New: |
| 1014 | case InitializedEntity::EK_Temporary: |
| 1015 | case InitializedEntity::EK_CompoundLiteralInit: |
| 1016 | // No warning, braces are part of the syntax of the underlying construct. |
| 1017 | break; |
| 1018 | |
| 1019 | case InitializedEntity::EK_RelatedResult: |
| 1020 | // No warning, we already warned when initializing the result. |
| 1021 | break; |
| 1022 | |
| 1023 | case InitializedEntity::EK_Exception: |
| 1024 | case InitializedEntity::EK_Base: |
| 1025 | case InitializedEntity::EK_Delegating: |
| 1026 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 1027 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 1028 | case InitializedEntity::EK_Binding: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 1029 | case InitializedEntity::EK_StmtExprResult: |
Richard Smith | 420fa12 | 2015-02-12 01:50:05 +0000 | [diff] [blame] | 1030 | llvm_unreachable("unexpected braced scalar init"); |
| 1031 | } |
| 1032 | |
| 1033 | if (DiagID) { |
| 1034 | S.Diag(Braces.getBegin(), DiagID) |
| 1035 | << Braces |
| 1036 | << FixItHint::CreateRemoval(Braces.getBegin()) |
| 1037 | << FixItHint::CreateRemoval(Braces.getEnd()); |
| 1038 | } |
| 1039 | } |
| 1040 | |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 1041 | /// Check whether the initializer \p IList (that was written with explicit |
| 1042 | /// braces) can be used to initialize an object of type \p T. |
| 1043 | /// |
| 1044 | /// This also fills in \p StructuredList with the fully-braced, desugared |
| 1045 | /// form of the initialization. |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1046 | void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1047 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1048 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1049 | bool TopLevelObject) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1050 | if (!VerifyOnly) { |
| 1051 | SyntacticToSemantic[IList] = StructuredList; |
| 1052 | StructuredList->setSyntacticForm(IList); |
| 1053 | } |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 1054 | |
| 1055 | unsigned Index = 0, StructuredIndex = 0; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1056 | CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1057 | Index, StructuredList, StructuredIndex, TopLevelObject); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1058 | if (!VerifyOnly) { |
Eli Friedman | 91f5ae5 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 1059 | QualType ExprTy = T; |
| 1060 | if (!ExprTy->isArrayType()) |
| 1061 | ExprTy = ExprTy.getNonLValueExprType(SemaRef.Context); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1062 | IList->setType(ExprTy); |
| 1063 | StructuredList->setType(ExprTy); |
| 1064 | } |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1065 | if (hadError) |
| 1066 | return; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 1067 | |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1068 | if (Index < IList->getNumInits()) { |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 1069 | // We have leftover initializers |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1070 | if (VerifyOnly) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1071 | if (SemaRef.getLangOpts().CPlusPlus || |
| 1072 | (SemaRef.getLangOpts().OpenCL && |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1073 | IList->getType()->isVectorType())) { |
| 1074 | hadError = true; |
| 1075 | } |
| 1076 | return; |
| 1077 | } |
| 1078 | |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 1079 | if (StructuredIndex == 1 && |
Hans Wennborg | 950f318 | 2013-05-16 09:22:40 +0000 | [diff] [blame] | 1080 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context) == |
| 1081 | SIF_None) { |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 1082 | unsigned DK = diag::ext_excess_initializers_in_char_array_initializer; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1083 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 1084 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 1085 | hadError = true; |
| 1086 | } |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 1087 | // Special-case |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1088 | SemaRef.Diag(IList->getInit(Index)->getBeginLoc(), DK) |
| 1089 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d0e48ea | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 1090 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | d42a0fb | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 1091 | // Don't complain for incomplete types, since we'll get an error |
| 1092 | // elsewhere |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1093 | QualType CurrentObjectType = StructuredList->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1094 | int initKind = |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1095 | CurrentObjectType->isArrayType()? 0 : |
| 1096 | CurrentObjectType->isVectorType()? 1 : |
| 1097 | CurrentObjectType->isScalarType()? 2 : |
| 1098 | CurrentObjectType->isUnionType()? 3 : |
| 1099 | 4; |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 1100 | |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 1101 | unsigned DK = diag::ext_excess_initializers; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1102 | if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 1103 | DK = diag::err_excess_initializers; |
| 1104 | hadError = true; |
| 1105 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1106 | if (SemaRef.getLangOpts().OpenCL && initKind == 1) { |
Nate Begeman | 425038c | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 1107 | DK = diag::err_excess_initializers; |
| 1108 | hadError = true; |
| 1109 | } |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 1110 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1111 | SemaRef.Diag(IList->getInit(Index)->getBeginLoc(), DK) |
| 1112 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 1113 | } |
| 1114 | } |
Eli Friedman | 6fcdec2 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 1115 | |
Richard Smith | 79c88c3 | 2018-09-26 19:00:16 +0000 | [diff] [blame] | 1116 | if (!VerifyOnly) { |
| 1117 | if (T->isScalarType() && IList->getNumInits() == 1 && |
| 1118 | !isa<InitListExpr>(IList->getInit(0))) |
| 1119 | warnBracedScalarInit(SemaRef, Entity, IList->getSourceRange()); |
| 1120 | |
| 1121 | // Warn if this is a class type that won't be an aggregate in future |
| 1122 | // versions of C++. |
| 1123 | auto *CXXRD = T->getAsCXXRecordDecl(); |
| 1124 | if (CXXRD && CXXRD->hasUserDeclaredConstructor()) { |
| 1125 | // Don't warn if there's an equivalent default constructor that would be |
| 1126 | // used instead. |
| 1127 | bool HasEquivCtor = false; |
| 1128 | if (IList->getNumInits() == 0) { |
| 1129 | auto *CD = SemaRef.LookupDefaultConstructor(CXXRD); |
| 1130 | HasEquivCtor = CD && !CD->isDeleted(); |
| 1131 | } |
| 1132 | |
| 1133 | if (!HasEquivCtor) { |
| 1134 | SemaRef.Diag(IList->getBeginLoc(), |
| 1135 | diag::warn_cxx2a_compat_aggregate_init_with_ctors) |
| 1136 | << IList->getSourceRange() << T; |
| 1137 | } |
| 1138 | } |
| 1139 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1142 | void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1143 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1144 | QualType &DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1145 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1146 | unsigned &Index, |
| 1147 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1148 | unsigned &StructuredIndex, |
| 1149 | bool TopLevelObject) { |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 1150 | if (DeclType->isAnyComplexType() && SubobjectIsDesignatorContext) { |
| 1151 | // Explicitly braced initializer for complex type can be real+imaginary |
| 1152 | // parts. |
| 1153 | CheckComplexType(Entity, IList, DeclType, Index, |
| 1154 | StructuredList, StructuredIndex); |
| 1155 | } else if (DeclType->isScalarType()) { |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1156 | CheckScalarType(Entity, IList, DeclType, Index, |
| 1157 | StructuredList, StructuredIndex); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 1158 | } else if (DeclType->isVectorType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1159 | CheckVectorType(Entity, IList, DeclType, Index, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1160 | StructuredList, StructuredIndex); |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 1161 | } else if (DeclType->isRecordType()) { |
| 1162 | assert(DeclType->isAggregateType() && |
| 1163 | "non-aggregate records should be handed in CheckSubElementType"); |
| 1164 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 1165 | auto Bases = |
| 1166 | CXXRecordDecl::base_class_range(CXXRecordDecl::base_class_iterator(), |
| 1167 | CXXRecordDecl::base_class_iterator()); |
| 1168 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
| 1169 | Bases = CXXRD->bases(); |
| 1170 | CheckStructUnionTypes(Entity, IList, DeclType, Bases, RD->field_begin(), |
| 1171 | SubobjectIsDesignatorContext, Index, StructuredList, |
| 1172 | StructuredIndex, TopLevelObject); |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 1173 | } else if (DeclType->isArrayType()) { |
| 1174 | llvm::APSInt Zero( |
| 1175 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
| 1176 | false); |
| 1177 | CheckArrayType(Entity, IList, DeclType, Zero, |
| 1178 | SubobjectIsDesignatorContext, Index, |
| 1179 | StructuredList, StructuredIndex); |
Steve Naroff | eaf5853 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 1180 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 1181 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1182 | ++Index; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1183 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1184 | SemaRef.Diag(IList->getBeginLoc(), diag::err_illegal_initializer_type) |
| 1185 | << DeclType; |
Eli Friedman | d0e48ea | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 1186 | hadError = true; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1187 | } else if (DeclType->isReferenceType()) { |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1188 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 1189 | StructuredList, StructuredIndex); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1190 | } else if (DeclType->isObjCObjectType()) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1191 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1192 | SemaRef.Diag(IList->getBeginLoc(), diag::err_init_objc_class) << DeclType; |
Douglas Gregor | 50ec46d | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 1193 | hadError = true; |
Andrew Savonichev | 3fee351 | 2018-11-08 11:25:41 +0000 | [diff] [blame] | 1194 | } else if (DeclType->isOCLIntelSubgroupAVCType()) { |
| 1195 | // Checks for scalar type are sufficient for these types too. |
| 1196 | CheckScalarType(Entity, IList, DeclType, Index, StructuredList, |
| 1197 | StructuredIndex); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1198 | } else { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1199 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1200 | SemaRef.Diag(IList->getBeginLoc(), diag::err_illegal_initializer_type) |
| 1201 | << DeclType; |
Douglas Gregor | 50ec46d | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 1202 | hadError = true; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1203 | } |
| 1204 | } |
| 1205 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1206 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1207 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1208 | QualType ElemType, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1209 | unsigned &Index, |
| 1210 | InitListExpr *StructuredList, |
| 1211 | unsigned &StructuredIndex) { |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1212 | Expr *expr = IList->getInit(Index); |
Richard Smith | 72752e8 | 2013-05-31 02:56:17 +0000 | [diff] [blame] | 1213 | |
| 1214 | if (ElemType->isReferenceType()) |
| 1215 | return CheckReferenceType(Entity, IList, ElemType, Index, |
| 1216 | StructuredList, StructuredIndex); |
| 1217 | |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 1218 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1219 | if (SubInitList->getNumInits() == 1 && |
| 1220 | IsStringInit(SubInitList->getInit(0), ElemType, SemaRef.Context) == |
| 1221 | SIF_None) { |
| 1222 | expr = SubInitList->getInit(0); |
| 1223 | } else if (!SemaRef.getLangOpts().CPlusPlus) { |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 1224 | InitListExpr *InnerStructuredList |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 1225 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 1226 | StructuredList, StructuredIndex, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1227 | SubInitList->getSourceRange(), true); |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 1228 | CheckExplicitInitList(Entity, SubInitList, ElemType, |
| 1229 | InnerStructuredList); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1230 | |
| 1231 | if (!hadError && !VerifyOnly) { |
| 1232 | bool RequiresSecondPass = false; |
| 1233 | FillInEmptyInitializations(Entity, InnerStructuredList, |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 1234 | RequiresSecondPass, StructuredList, |
| 1235 | StructuredIndex); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1236 | if (RequiresSecondPass && !hadError) |
| 1237 | FillInEmptyInitializations(Entity, InnerStructuredList, |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 1238 | RequiresSecondPass, StructuredList, |
| 1239 | StructuredIndex); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1240 | } |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 1241 | ++StructuredIndex; |
| 1242 | ++Index; |
| 1243 | return; |
| 1244 | } |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 1245 | // C++ initialization is handled later. |
Richard Smith | c4158e86 | 2014-07-18 04:47:25 +0000 | [diff] [blame] | 1246 | } else if (isa<ImplicitValueInitExpr>(expr)) { |
Richard Smith | 8aa561b | 2014-07-17 23:12:06 +0000 | [diff] [blame] | 1247 | // This happens during template instantiation when we see an InitListExpr |
| 1248 | // that we've already checked once. |
Richard Smith | c4158e86 | 2014-07-18 04:47:25 +0000 | [diff] [blame] | 1249 | assert(SemaRef.Context.hasSameType(expr->getType(), ElemType) && |
Richard Smith | 8aa561b | 2014-07-17 23:12:06 +0000 | [diff] [blame] | 1250 | "found implicit initialization for the wrong type"); |
| 1251 | if (!VerifyOnly) |
| 1252 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 1253 | ++Index; |
| 1254 | return; |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1257 | if (SemaRef.getLangOpts().CPlusPlus) { |
| 1258 | // C++ [dcl.init.aggr]p2: |
| 1259 | // Each member is copy-initialized from the corresponding |
| 1260 | // initializer-clause. |
| 1261 | |
| 1262 | // FIXME: Better EqualLoc? |
| 1263 | InitializationKind Kind = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1264 | InitializationKind::CreateCopy(expr->getBeginLoc(), SourceLocation()); |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1265 | InitializationSequence Seq(SemaRef, Entity, Kind, expr, |
| 1266 | /*TopLevelOfInitList*/ true); |
| 1267 | |
| 1268 | // C++14 [dcl.init.aggr]p13: |
| 1269 | // If the assignment-expression can initialize a member, the member is |
| 1270 | // initialized. Otherwise [...] brace elision is assumed |
| 1271 | // |
| 1272 | // Brace elision is never performed if the element is not an |
| 1273 | // assignment-expression. |
| 1274 | if (Seq || isa<InitListExpr>(expr)) { |
| 1275 | if (!VerifyOnly) { |
| 1276 | ExprResult Result = |
| 1277 | Seq.Perform(SemaRef, Entity, Kind, expr); |
| 1278 | if (Result.isInvalid()) |
| 1279 | hadError = true; |
| 1280 | |
| 1281 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1282 | Result.getAs<Expr>()); |
Richard Smith | 40574cc | 2015-02-16 04:42:59 +0000 | [diff] [blame] | 1283 | } else if (!Seq) |
| 1284 | hadError = true; |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1285 | ++Index; |
| 1286 | return; |
| 1287 | } |
| 1288 | |
| 1289 | // Fall through for subaggregate initialization |
| 1290 | } else if (ElemType->isScalarType() || ElemType->isAtomicType()) { |
| 1291 | // FIXME: Need to handle atomic aggregate types with implicit init lists. |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1292 | return CheckScalarType(Entity, IList, ElemType, Index, |
| 1293 | StructuredList, StructuredIndex); |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1294 | } else if (const ArrayType *arrayType = |
| 1295 | SemaRef.Context.getAsArrayType(ElemType)) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1296 | // arrayType can be incomplete if we're initializing a flexible |
| 1297 | // array member. There's nothing we can do with the completed |
| 1298 | // type here, though. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1299 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1300 | if (IsStringInit(expr, arrayType, SemaRef.Context) == SIF_None) { |
Eli Friedman | d8d7a37 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 1301 | if (!VerifyOnly) { |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1302 | CheckStringInit(expr, ElemType, arrayType, SemaRef); |
| 1303 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
Eli Friedman | d8d7a37 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 1304 | } |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1305 | ++Index; |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1306 | return; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1307 | } |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1308 | |
| 1309 | // Fall through for subaggregate initialization. |
| 1310 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1311 | } else { |
Anastasia Stulova | db7a31c | 2016-07-05 11:31:24 +0000 | [diff] [blame] | 1312 | assert((ElemType->isRecordType() || ElemType->isVectorType() || |
Egor Churaev | 45fe70f | 2017-05-10 10:28:34 +0000 | [diff] [blame] | 1313 | ElemType->isOpenCLSpecificType()) && "Unexpected type"); |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1314 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1315 | // C99 6.7.8p13: |
| 1316 | // |
| 1317 | // The initializer for a structure or union object that has |
| 1318 | // automatic storage duration shall be either an initializer |
| 1319 | // list as described below, or a single expression that has |
| 1320 | // compatible structure or union type. In the latter case, the |
| 1321 | // initial value of the object, including unnamed members, is |
| 1322 | // that of the expression. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1323 | ExprResult ExprRes = expr; |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1324 | if (SemaRef.CheckSingleAssignmentConstraints( |
| 1325 | ElemType, ExprRes, !VerifyOnly) != Sema::Incompatible) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1326 | if (ExprRes.isInvalid()) |
| 1327 | hadError = true; |
| 1328 | else { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1329 | ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.get()); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 1330 | if (ExprRes.isInvalid()) |
| 1331 | hadError = true; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1332 | } |
| 1333 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1334 | ExprRes.getAs<Expr>()); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1335 | ++Index; |
| 1336 | return; |
| 1337 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1338 | ExprRes.get(); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1339 | // Fall through for subaggregate initialization |
| 1340 | } |
| 1341 | |
| 1342 | // C++ [dcl.init.aggr]p12: |
| 1343 | // |
| 1344 | // [...] Otherwise, if the member is itself a non-empty |
| 1345 | // subaggregate, brace elision is assumed and the initializer is |
| 1346 | // considered for the initialization of the first member of |
| 1347 | // the subaggregate. |
Yaxun Liu | a91da4b | 2016-10-11 15:53:28 +0000 | [diff] [blame] | 1348 | // OpenCL vector initializer is handled elsewhere. |
| 1349 | if ((!SemaRef.getLangOpts().OpenCL && ElemType->isVectorType()) || |
| 1350 | ElemType->isAggregateType()) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1351 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
| 1352 | StructuredIndex); |
| 1353 | ++StructuredIndex; |
| 1354 | } else { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1355 | if (!VerifyOnly) { |
| 1356 | // We cannot initialize this element, so let |
| 1357 | // PerformCopyInitialization produce the appropriate diagnostic. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1358 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), expr, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1359 | /*TopLevelOfInitList=*/true); |
| 1360 | } |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1361 | hadError = true; |
| 1362 | ++Index; |
| 1363 | ++StructuredIndex; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1364 | } |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 1367 | void InitListChecker::CheckComplexType(const InitializedEntity &Entity, |
| 1368 | InitListExpr *IList, QualType DeclType, |
| 1369 | unsigned &Index, |
| 1370 | InitListExpr *StructuredList, |
| 1371 | unsigned &StructuredIndex) { |
| 1372 | assert(Index == 0 && "Index in explicit init list must be zero"); |
| 1373 | |
| 1374 | // As an extension, clang supports complex initializers, which initialize |
| 1375 | // a complex number component-wise. When an explicit initializer list for |
| 1376 | // a complex number contains two two initializers, this extension kicks in: |
| 1377 | // it exepcts the initializer list to contain two elements convertible to |
| 1378 | // the element type of the complex type. The first element initializes |
| 1379 | // the real part, and the second element intitializes the imaginary part. |
| 1380 | |
| 1381 | if (IList->getNumInits() != 2) |
| 1382 | return CheckScalarType(Entity, IList, DeclType, Index, StructuredList, |
| 1383 | StructuredIndex); |
| 1384 | |
| 1385 | // This is an extension in C. (The builtin _Complex type does not exist |
| 1386 | // in the C++ standard.) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1387 | if (!SemaRef.getLangOpts().CPlusPlus && !VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1388 | SemaRef.Diag(IList->getBeginLoc(), diag::ext_complex_component_init) |
| 1389 | << IList->getSourceRange(); |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 1390 | |
| 1391 | // Initialize the complex number. |
| 1392 | QualType elementType = DeclType->getAs<ComplexType>()->getElementType(); |
| 1393 | InitializedEntity ElementEntity = |
| 1394 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
| 1395 | |
| 1396 | for (unsigned i = 0; i < 2; ++i) { |
| 1397 | ElementEntity.setElementIndex(Index); |
| 1398 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1399 | StructuredList, StructuredIndex); |
| 1400 | } |
| 1401 | } |
| 1402 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1403 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1404 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1405 | unsigned &Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1406 | InitListExpr *StructuredList, |
| 1407 | unsigned &StructuredIndex) { |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1408 | if (Index >= IList->getNumInits()) { |
Richard Smith | c823973 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 1409 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1410 | SemaRef.Diag(IList->getBeginLoc(), |
| 1411 | SemaRef.getLangOpts().CPlusPlus11 |
| 1412 | ? diag::warn_cxx98_compat_empty_scalar_initializer |
| 1413 | : diag::err_empty_scalar_initializer) |
| 1414 | << IList->getSourceRange(); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1415 | hadError = !SemaRef.getLangOpts().CPlusPlus11; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1416 | ++Index; |
| 1417 | ++StructuredIndex; |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 1418 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1419 | } |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1420 | |
| 1421 | Expr *expr = IList->getInit(Index); |
| 1422 | if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) { |
Richard Smith | fe9d2c0 | 2013-11-19 03:41:32 +0000 | [diff] [blame] | 1423 | // FIXME: This is invalid, and accepting it causes overload resolution |
| 1424 | // to pick the wrong overload in some corner cases. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1425 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1426 | SemaRef.Diag(SubIList->getBeginLoc(), |
Richard Smith | fe9d2c0 | 2013-11-19 03:41:32 +0000 | [diff] [blame] | 1427 | diag::ext_many_braces_around_scalar_init) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1428 | << SubIList->getSourceRange(); |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1429 | |
| 1430 | CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList, |
| 1431 | StructuredIndex); |
| 1432 | return; |
| 1433 | } else if (isa<DesignatedInitExpr>(expr)) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1434 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1435 | SemaRef.Diag(expr->getBeginLoc(), diag::err_designator_for_scalar_init) |
| 1436 | << DeclType << expr->getSourceRange(); |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1437 | hadError = true; |
| 1438 | ++Index; |
| 1439 | ++StructuredIndex; |
| 1440 | return; |
| 1441 | } |
| 1442 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1443 | if (VerifyOnly) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1444 | if (!SemaRef.CanPerformCopyInitialization(Entity,expr)) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1445 | hadError = true; |
| 1446 | ++Index; |
| 1447 | return; |
| 1448 | } |
| 1449 | |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1450 | ExprResult Result = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1451 | SemaRef.PerformCopyInitialization(Entity, expr->getBeginLoc(), expr, |
| 1452 | /*TopLevelOfInitList=*/true); |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1453 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1454 | Expr *ResultExpr = nullptr; |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1455 | |
| 1456 | if (Result.isInvalid()) |
| 1457 | hadError = true; // types weren't compatible. |
| 1458 | else { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1459 | ResultExpr = Result.getAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1460 | |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1461 | if (ResultExpr != expr) { |
| 1462 | // The type was promoted, update initializer list. |
| 1463 | IList->setInit(Index, ResultExpr); |
| 1464 | } |
| 1465 | } |
| 1466 | if (hadError) |
| 1467 | ++StructuredIndex; |
| 1468 | else |
| 1469 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 1470 | ++Index; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1471 | } |
| 1472 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1473 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 1474 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1475 | unsigned &Index, |
| 1476 | InitListExpr *StructuredList, |
| 1477 | unsigned &StructuredIndex) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1478 | if (Index >= IList->getNumInits()) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1479 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 1480 | // general, it would be useful to pass location information down the stack, |
| 1481 | // so that we know the location (or decl) of the "current object" being |
| 1482 | // initialized. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1483 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1484 | SemaRef.Diag(IList->getBeginLoc(), |
| 1485 | diag::err_init_reference_member_uninitialized) |
| 1486 | << DeclType << IList->getSourceRange(); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1487 | hadError = true; |
| 1488 | ++Index; |
| 1489 | ++StructuredIndex; |
| 1490 | return; |
| 1491 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1492 | |
| 1493 | Expr *expr = IList->getInit(Index); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1494 | if (isa<InitListExpr>(expr) && !SemaRef.getLangOpts().CPlusPlus11) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1495 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1496 | SemaRef.Diag(IList->getBeginLoc(), diag::err_init_non_aggr_init_list) |
| 1497 | << DeclType << IList->getSourceRange(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1498 | hadError = true; |
| 1499 | ++Index; |
| 1500 | ++StructuredIndex; |
| 1501 | return; |
| 1502 | } |
| 1503 | |
| 1504 | if (VerifyOnly) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1505 | if (!SemaRef.CanPerformCopyInitialization(Entity,expr)) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1506 | hadError = true; |
| 1507 | ++Index; |
| 1508 | return; |
| 1509 | } |
| 1510 | |
| 1511 | ExprResult Result = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1512 | SemaRef.PerformCopyInitialization(Entity, expr->getBeginLoc(), expr, |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1513 | /*TopLevelOfInitList=*/true); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1514 | |
| 1515 | if (Result.isInvalid()) |
| 1516 | hadError = true; |
| 1517 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1518 | expr = Result.getAs<Expr>(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1519 | IList->setInit(Index, expr); |
| 1520 | |
| 1521 | if (hadError) |
| 1522 | ++StructuredIndex; |
| 1523 | else |
| 1524 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 1525 | ++Index; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1528 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1529 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1530 | unsigned &Index, |
| 1531 | InitListExpr *StructuredList, |
| 1532 | unsigned &StructuredIndex) { |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1533 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 1534 | unsigned maxElements = VT->getNumElements(); |
| 1535 | unsigned numEltsInit = 0; |
| 1536 | QualType elementType = VT->getElementType(); |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1537 | |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1538 | if (Index >= IList->getNumInits()) { |
| 1539 | // Make sure the element type can be value-initialized. |
| 1540 | if (VerifyOnly) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1541 | CheckEmptyInitializable( |
| 1542 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity), |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1543 | IList->getEndLoc()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1544 | return; |
| 1545 | } |
| 1546 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1547 | if (!SemaRef.getLangOpts().OpenCL) { |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1548 | // If the initializing element is a vector, try to copy-initialize |
| 1549 | // instead of breaking it apart (which is doomed to failure anyway). |
| 1550 | Expr *Init = IList->getInit(Index); |
| 1551 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1552 | if (VerifyOnly) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1553 | if (!SemaRef.CanPerformCopyInitialization(Entity, Init)) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1554 | hadError = true; |
| 1555 | ++Index; |
| 1556 | return; |
| 1557 | } |
| 1558 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1559 | ExprResult Result = |
| 1560 | SemaRef.PerformCopyInitialization(Entity, Init->getBeginLoc(), Init, |
| 1561 | /*TopLevelOfInitList=*/true); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1562 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1563 | Expr *ResultExpr = nullptr; |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1564 | if (Result.isInvalid()) |
| 1565 | hadError = true; // types weren't compatible. |
| 1566 | else { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1567 | ResultExpr = Result.getAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1568 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1569 | if (ResultExpr != Init) { |
| 1570 | // The type was promoted, update initializer list. |
| 1571 | IList->setInit(Index, ResultExpr); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1572 | } |
| 1573 | } |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1574 | if (hadError) |
| 1575 | ++StructuredIndex; |
| 1576 | else |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1577 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1578 | ResultExpr); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1579 | ++Index; |
| 1580 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1581 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1582 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1583 | InitializedEntity ElementEntity = |
| 1584 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1585 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1586 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 1587 | // Don't attempt to go past the end of the init list |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1588 | if (Index >= IList->getNumInits()) { |
| 1589 | if (VerifyOnly) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1590 | CheckEmptyInitializable(ElementEntity, IList->getEndLoc()); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1591 | break; |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1592 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1593 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1594 | ElementEntity.setElementIndex(Index); |
| 1595 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1596 | StructuredList, StructuredIndex); |
| 1597 | } |
James Molloy | 9eef265 | 2014-06-20 14:35:13 +0000 | [diff] [blame] | 1598 | |
| 1599 | if (VerifyOnly) |
| 1600 | return; |
| 1601 | |
| 1602 | bool isBigEndian = SemaRef.Context.getTargetInfo().isBigEndian(); |
| 1603 | const VectorType *T = Entity.getType()->getAs<VectorType>(); |
| 1604 | if (isBigEndian && (T->getVectorKind() == VectorType::NeonVector || |
| 1605 | T->getVectorKind() == VectorType::NeonPolyVector)) { |
| 1606 | // The ability to use vector initializer lists is a GNU vector extension |
| 1607 | // and is unrelated to the NEON intrinsics in arm_neon.h. On little |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1608 | // endian machines it works fine, however on big endian machines it |
James Molloy | 9eef265 | 2014-06-20 14:35:13 +0000 | [diff] [blame] | 1609 | // exhibits surprising behaviour: |
| 1610 | // |
| 1611 | // uint32x2_t x = {42, 64}; |
| 1612 | // return vget_lane_u32(x, 0); // Will return 64. |
| 1613 | // |
| 1614 | // Because of this, explicitly call out that it is non-portable. |
| 1615 | // |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1616 | SemaRef.Diag(IList->getBeginLoc(), |
James Molloy | 9eef265 | 2014-06-20 14:35:13 +0000 | [diff] [blame] | 1617 | diag::warn_neon_vector_initializer_non_portable); |
| 1618 | |
| 1619 | const char *typeCode; |
| 1620 | unsigned typeSize = SemaRef.Context.getTypeSize(elementType); |
| 1621 | |
| 1622 | if (elementType->isFloatingType()) |
| 1623 | typeCode = "f"; |
| 1624 | else if (elementType->isSignedIntegerType()) |
| 1625 | typeCode = "s"; |
| 1626 | else if (elementType->isUnsignedIntegerType()) |
| 1627 | typeCode = "u"; |
| 1628 | else |
| 1629 | llvm_unreachable("Invalid element type!"); |
| 1630 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1631 | SemaRef.Diag(IList->getBeginLoc(), |
| 1632 | SemaRef.Context.getTypeSize(VT) > 64 |
| 1633 | ? diag::note_neon_vector_initializer_non_portable_q |
| 1634 | : diag::note_neon_vector_initializer_non_portable) |
| 1635 | << typeCode << typeSize; |
James Molloy | 9eef265 | 2014-06-20 14:35:13 +0000 | [diff] [blame] | 1636 | } |
| 1637 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1638 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1639 | } |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1640 | |
| 1641 | InitializedEntity ElementEntity = |
| 1642 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1643 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1644 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 1645 | for (unsigned i = 0; i < maxElements; ++i) { |
| 1646 | // Don't attempt to go past the end of the init list |
| 1647 | if (Index >= IList->getNumInits()) |
| 1648 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1649 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1650 | ElementEntity.setElementIndex(Index); |
| 1651 | |
| 1652 | QualType IType = IList->getInit(Index)->getType(); |
| 1653 | if (!IType->isVectorType()) { |
| 1654 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1655 | StructuredList, StructuredIndex); |
| 1656 | ++numEltsInit; |
| 1657 | } else { |
| 1658 | QualType VecType; |
| 1659 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 1660 | unsigned numIElts = IVT->getNumElements(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1661 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1662 | if (IType->isExtVectorType()) |
| 1663 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 1664 | else |
| 1665 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1666 | IVT->getVectorKind()); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1667 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 1668 | StructuredList, StructuredIndex); |
| 1669 | numEltsInit += numIElts; |
| 1670 | } |
| 1671 | } |
| 1672 | |
| 1673 | // OpenCL requires all elements to be initialized. |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1674 | if (numEltsInit != maxElements) { |
| 1675 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1676 | SemaRef.Diag(IList->getBeginLoc(), |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1677 | diag::err_vector_incorrect_num_initializers) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1678 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1679 | hadError = true; |
| 1680 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1683 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1684 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1685 | llvm::APSInt elementIndex, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1686 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1687 | unsigned &Index, |
| 1688 | InitListExpr *StructuredList, |
| 1689 | unsigned &StructuredIndex) { |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1690 | const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType); |
| 1691 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1692 | // Check for the special-case of initializing an array with a string. |
| 1693 | if (Index < IList->getNumInits()) { |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1694 | if (IsStringInit(IList->getInit(Index), arrayType, SemaRef.Context) == |
| 1695 | SIF_None) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1696 | // We place the string literal directly into the resulting |
| 1697 | // initializer list. This is the only place where the structure |
| 1698 | // of the structured initializer list doesn't match exactly, |
| 1699 | // because doing so would involve allocating one character |
| 1700 | // constant for each string. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1701 | if (!VerifyOnly) { |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1702 | CheckStringInit(IList->getInit(Index), DeclType, arrayType, SemaRef); |
| 1703 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1704 | IList->getInit(Index)); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1705 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
| 1706 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1707 | ++Index; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1708 | return; |
| 1709 | } |
| 1710 | } |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1711 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1712 | // Check for VLAs; in standard C it would be possible to check this |
| 1713 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 1714 | // them in all sorts of strange places). |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1715 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1716 | SemaRef.Diag(VAT->getSizeExpr()->getBeginLoc(), |
| 1717 | diag::err_variable_object_no_init) |
| 1718 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1719 | hadError = true; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1720 | ++Index; |
| 1721 | ++StructuredIndex; |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1722 | return; |
| 1723 | } |
| 1724 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1725 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1726 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 1727 | elementIndex.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1728 | bool maxElementsKnown = false; |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1729 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1730 | maxElements = CAT->getSize(); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1731 | elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1732 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1733 | maxElementsKnown = true; |
| 1734 | } |
| 1735 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1736 | QualType elementType = arrayType->getElementType(); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1737 | while (Index < IList->getNumInits()) { |
| 1738 | Expr *Init = IList->getInit(Index); |
| 1739 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1740 | // If we're not the subobject that matches up with the '{' for |
| 1741 | // the designator, we shouldn't be handling the |
| 1742 | // designator. Return immediately. |
| 1743 | if (!SubobjectIsDesignatorContext) |
| 1744 | return; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1745 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1746 | // Handle this designated initializer. elementIndex will be |
| 1747 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1748 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1749 | DeclType, nullptr, &elementIndex, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1750 | StructuredList, StructuredIndex, true, |
| 1751 | false)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1752 | hadError = true; |
| 1753 | continue; |
| 1754 | } |
| 1755 | |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1756 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1757 | maxElements = maxElements.extend(elementIndex.getBitWidth()); |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1758 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1759 | elementIndex = elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1760 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1761 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1762 | // If the array is of incomplete type, keep track of the number of |
| 1763 | // elements in the initializer. |
| 1764 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1765 | maxElements = elementIndex; |
| 1766 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1767 | continue; |
| 1768 | } |
| 1769 | |
| 1770 | // If we know the maximum number of elements, and we've already |
| 1771 | // hit it, stop consuming elements in the initializer list. |
| 1772 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1773 | break; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1774 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1775 | InitializedEntity ElementEntity = |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1776 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1777 | Entity); |
| 1778 | // Check this element. |
| 1779 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1780 | StructuredList, StructuredIndex); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1781 | ++elementIndex; |
| 1782 | |
| 1783 | // If the array is of incomplete type, keep track of the number of |
| 1784 | // elements in the initializer. |
| 1785 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1786 | maxElements = elementIndex; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1787 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1788 | if (!hadError && DeclType->isIncompleteArrayType() && !VerifyOnly) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1789 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1790 | // be calculated here. |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1791 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Richard Smith | 73edb6d | 2017-01-24 23:18:28 +0000 | [diff] [blame] | 1792 | if (maxElements == Zero && !Entity.isVariableLengthArrayNew()) { |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1793 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1794 | // but is supported by GNU. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1795 | SemaRef.Diag(IList->getBeginLoc(), diag::ext_typecheck_zero_array_size); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1796 | } |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1797 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1798 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1799 | ArrayType::Normal, 0); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1800 | } |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1801 | if (!hadError && VerifyOnly) { |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1802 | // If there are any members of the array that get value-initialized, check |
| 1803 | // that is possible. That happens if we know the bound and don't have |
| 1804 | // enough elements, or if we're performing an array new with an unknown |
| 1805 | // bound. |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1806 | // FIXME: This needs to detect holes left by designated initializers too. |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1807 | if ((maxElementsKnown && elementIndex < maxElements) || |
| 1808 | Entity.isVariableLengthArrayNew()) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1809 | CheckEmptyInitializable( |
| 1810 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity), |
| 1811 | IList->getEndLoc()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1812 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1813 | } |
| 1814 | |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1815 | bool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 1816 | Expr *InitExpr, |
| 1817 | FieldDecl *Field, |
| 1818 | bool TopLevelObject) { |
| 1819 | // Handle GNU flexible array initializers. |
| 1820 | unsigned FlexArrayDiag; |
| 1821 | if (isa<InitListExpr>(InitExpr) && |
| 1822 | cast<InitListExpr>(InitExpr)->getNumInits() == 0) { |
| 1823 | // Empty flexible array init always allowed as an extension |
| 1824 | FlexArrayDiag = diag::ext_flexible_array_init; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1825 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1826 | // Disallow flexible array init in C++; it is not required for gcc |
| 1827 | // compatibility, and it needs work to IRGen correctly in general. |
| 1828 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1829 | } else if (!TopLevelObject) { |
| 1830 | // Disallow flexible array init on non-top-level object |
| 1831 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1832 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
| 1833 | // Disallow flexible array init on anything which is not a variable. |
| 1834 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1835 | } else if (cast<VarDecl>(Entity.getDecl())->hasLocalStorage()) { |
| 1836 | // Disallow flexible array init on local variables. |
| 1837 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1838 | } else { |
| 1839 | // Allow other cases. |
| 1840 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1841 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1842 | |
| 1843 | if (!VerifyOnly) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1844 | SemaRef.Diag(InitExpr->getBeginLoc(), FlexArrayDiag) |
| 1845 | << InitExpr->getBeginLoc(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1846 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1847 | << Field; |
| 1848 | } |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1849 | |
| 1850 | return FlexArrayDiag != diag::ext_flexible_array_init; |
| 1851 | } |
| 1852 | |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 1853 | /// Check if the type of a class element has an accessible destructor. |
| 1854 | /// |
| 1855 | /// Aggregate initialization requires a class element's destructor be |
| 1856 | /// accessible per 11.6.1 [dcl.init.aggr]: |
| 1857 | /// |
| 1858 | /// The destructor for each element of class type is potentially invoked |
| 1859 | /// (15.4 [class.dtor]) from the context where the aggregate initialization |
| 1860 | /// occurs. |
| 1861 | static bool hasAccessibleDestructor(QualType ElementType, SourceLocation Loc, |
| 1862 | Sema &SemaRef) { |
| 1863 | auto *CXXRD = ElementType->getAsCXXRecordDecl(); |
| 1864 | if (!CXXRD) |
| 1865 | return false; |
| 1866 | |
| 1867 | CXXDestructorDecl *Destructor = SemaRef.LookupDestructor(CXXRD); |
| 1868 | SemaRef.CheckDestructorAccess(Loc, Destructor, |
| 1869 | SemaRef.PDiag(diag::err_access_dtor_temp) |
| 1870 | << ElementType); |
| 1871 | SemaRef.MarkFunctionReferenced(Loc, Destructor); |
| 1872 | if (SemaRef.DiagnoseUseOfDecl(Destructor, Loc)) |
| 1873 | return true; |
| 1874 | return false; |
| 1875 | } |
| 1876 | |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 1877 | void InitListChecker::CheckStructUnionTypes( |
| 1878 | const InitializedEntity &Entity, InitListExpr *IList, QualType DeclType, |
| 1879 | CXXRecordDecl::base_class_range Bases, RecordDecl::field_iterator Field, |
| 1880 | bool SubobjectIsDesignatorContext, unsigned &Index, |
| 1881 | InitListExpr *StructuredList, unsigned &StructuredIndex, |
| 1882 | bool TopLevelObject) { |
| 1883 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1884 | |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1885 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1886 | // confusion, we forgo checking the intializer for the entire record. |
| 1887 | if (structDecl->isInvalidDecl()) { |
Richard Smith | 845aa66 | 2012-09-28 21:23:50 +0000 | [diff] [blame] | 1888 | // Assume it was supposed to consume a single initializer. |
| 1889 | ++Index; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1890 | hadError = true; |
| 1891 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1892 | } |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1893 | |
| 1894 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1895 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1896 | |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 1897 | if (!VerifyOnly) |
| 1898 | for (FieldDecl *FD : RD->fields()) { |
| 1899 | QualType ET = SemaRef.Context.getBaseElementType(FD->getType()); |
| 1900 | if (hasAccessibleDestructor(ET, IList->getEndLoc(), SemaRef)) { |
| 1901 | hadError = true; |
| 1902 | return; |
| 1903 | } |
| 1904 | } |
| 1905 | |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1906 | // If there's a default initializer, use it. |
| 1907 | if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->hasInClassInitializer()) { |
| 1908 | if (VerifyOnly) |
| 1909 | return; |
| 1910 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1911 | Field != FieldEnd; ++Field) { |
| 1912 | if (Field->hasInClassInitializer()) { |
| 1913 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1914 | // FIXME: Actually build a CXXDefaultInitExpr? |
| 1915 | return; |
| 1916 | } |
| 1917 | } |
| 1918 | } |
| 1919 | |
Reid Kleckner | 6d829bd | 2014-11-12 21:30:23 +0000 | [diff] [blame] | 1920 | // Value-initialize the first member of the union that isn't an unnamed |
| 1921 | // bitfield. |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1922 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1923 | Field != FieldEnd; ++Field) { |
Reid Kleckner | 6d829bd | 2014-11-12 21:30:23 +0000 | [diff] [blame] | 1924 | if (!Field->isUnnamedBitfield()) { |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1925 | if (VerifyOnly) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1926 | CheckEmptyInitializable( |
| 1927 | InitializedEntity::InitializeMember(*Field, &Entity), |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1928 | IList->getEndLoc()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1929 | else |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1930 | StructuredList->setInitializedFieldInUnion(*Field); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1931 | break; |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1932 | } |
| 1933 | } |
| 1934 | return; |
| 1935 | } |
| 1936 | |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 1937 | bool InitializedSomething = false; |
| 1938 | |
| 1939 | // If we have any base classes, they are initialized prior to the fields. |
| 1940 | for (auto &Base : Bases) { |
| 1941 | Expr *Init = Index < IList->getNumInits() ? IList->getInit(Index) : nullptr; |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 1942 | |
| 1943 | // Designated inits always initialize fields, so if we see one, all |
| 1944 | // remaining base classes have no explicit initializer. |
| 1945 | if (Init && isa<DesignatedInitExpr>(Init)) |
| 1946 | Init = nullptr; |
| 1947 | |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 1948 | SourceLocation InitLoc = Init ? Init->getBeginLoc() : IList->getEndLoc(); |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 1949 | InitializedEntity BaseEntity = InitializedEntity::InitializeBase( |
| 1950 | SemaRef.Context, &Base, false, &Entity); |
| 1951 | if (Init) { |
| 1952 | CheckSubElementType(BaseEntity, IList, Base.getType(), Index, |
| 1953 | StructuredList, StructuredIndex); |
| 1954 | InitializedSomething = true; |
| 1955 | } else if (VerifyOnly) { |
| 1956 | CheckEmptyInitializable(BaseEntity, InitLoc); |
| 1957 | } |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 1958 | |
| 1959 | if (!VerifyOnly) |
| 1960 | if (hasAccessibleDestructor(Base.getType(), InitLoc, SemaRef)) { |
| 1961 | hadError = true; |
| 1962 | return; |
| 1963 | } |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 1964 | } |
| 1965 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1966 | // If structDecl is a forward declaration, this loop won't do |
| 1967 | // anything except look at designated initializers; That's okay, |
| 1968 | // because an error should get printed out elsewhere. It might be |
| 1969 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1970 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1971 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Daniel Marjamaki | 817a3bf | 2017-09-29 09:44:41 +0000 | [diff] [blame] | 1972 | bool CheckForMissingFields = |
| 1973 | !IList->isIdiomaticZeroInitializer(SemaRef.getLangOpts()); |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 1974 | bool HasDesignatedInit = false; |
Daniel Marjamaki | 817a3bf | 2017-09-29 09:44:41 +0000 | [diff] [blame] | 1975 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1976 | while (Index < IList->getNumInits()) { |
| 1977 | Expr *Init = IList->getInit(Index); |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 1978 | SourceLocation InitLoc = Init->getBeginLoc(); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1979 | |
| 1980 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1981 | // If we're not the subobject that matches up with the '{' for |
| 1982 | // the designator, we shouldn't be handling the |
| 1983 | // designator. Return immediately. |
| 1984 | if (!SubobjectIsDesignatorContext) |
| 1985 | return; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1986 | |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 1987 | HasDesignatedInit = true; |
| 1988 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1989 | // Handle this designated initializer. Field will be updated to |
| 1990 | // the next field that we'll be initializing. |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1991 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1992 | DeclType, &Field, nullptr, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1993 | StructuredList, StructuredIndex, |
| 1994 | true, TopLevelObject)) |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1995 | hadError = true; |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 1996 | else if (!VerifyOnly) { |
| 1997 | // Find the field named by the designated initializer. |
| 1998 | RecordDecl::field_iterator F = RD->field_begin(); |
| 1999 | while (std::next(F) != Field) |
| 2000 | ++F; |
| 2001 | QualType ET = SemaRef.Context.getBaseElementType(F->getType()); |
| 2002 | if (hasAccessibleDestructor(ET, InitLoc, SemaRef)) { |
| 2003 | hadError = true; |
| 2004 | return; |
| 2005 | } |
| 2006 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2007 | |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 2008 | InitializedSomething = true; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 2009 | |
| 2010 | // Disable check for missing fields when designators are used. |
| 2011 | // This matches gcc behaviour. |
| 2012 | CheckForMissingFields = false; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2013 | continue; |
| 2014 | } |
| 2015 | |
| 2016 | if (Field == FieldEnd) { |
| 2017 | // We've run out of fields. We're done. |
| 2018 | break; |
| 2019 | } |
| 2020 | |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 2021 | // We've already initialized a member of a union. We're done. |
| 2022 | if (InitializedSomething && DeclType->isUnionType()) |
| 2023 | break; |
| 2024 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2025 | // If we've hit the flexible array member at the end, we're done. |
| 2026 | if (Field->getType()->isIncompleteArrayType()) |
| 2027 | break; |
| 2028 | |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 2029 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2030 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2031 | ++Field; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 2032 | continue; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 2033 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2034 | |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2035 | // Make sure we can use this declaration. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2036 | bool InvalidUse; |
| 2037 | if (VerifyOnly) |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 2038 | InvalidUse = !SemaRef.CanUseDecl(*Field, TreatUnavailableAsInvalid); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2039 | else |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2040 | InvalidUse = SemaRef.DiagnoseUseOfDecl( |
| 2041 | *Field, IList->getInit(Index)->getBeginLoc()); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2042 | if (InvalidUse) { |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2043 | ++Index; |
| 2044 | ++Field; |
| 2045 | hadError = true; |
| 2046 | continue; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2047 | } |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2048 | |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 2049 | if (!VerifyOnly) { |
| 2050 | QualType ET = SemaRef.Context.getBaseElementType(Field->getType()); |
| 2051 | if (hasAccessibleDestructor(ET, InitLoc, SemaRef)) { |
| 2052 | hadError = true; |
| 2053 | return; |
| 2054 | } |
| 2055 | } |
| 2056 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2057 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2058 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2059 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 2060 | StructuredList, StructuredIndex); |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 2061 | InitializedSomething = true; |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 2062 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2063 | if (DeclType->isUnionType() && !VerifyOnly) { |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 2064 | // Initialize the first field within the union. |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2065 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 2066 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2067 | |
| 2068 | ++Field; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 2069 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2070 | |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 2071 | // Emit warnings for missing struct field initializers. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2072 | if (!VerifyOnly && InitializedSomething && CheckForMissingFields && |
| 2073 | Field != FieldEnd && !Field->getType()->isIncompleteArrayType() && |
| 2074 | !DeclType->isUnionType()) { |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 2075 | // It is possible we have one or more unnamed bitfields remaining. |
| 2076 | // Find first (if any) named field and emit warning. |
| 2077 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 2078 | it != end; ++it) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 2079 | if (!it->isUnnamedBitfield() && !it->hasInClassInitializer()) { |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 2080 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
Aaron Ballman | b9bb201 | 2014-01-03 14:54:10 +0000 | [diff] [blame] | 2081 | diag::warn_missing_field_initializers) << *it; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 2082 | break; |
| 2083 | } |
| 2084 | } |
| 2085 | } |
| 2086 | |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 2087 | // Check that any remaining fields can be value-initialized. |
| 2088 | if (VerifyOnly && Field != FieldEnd && !DeclType->isUnionType() && |
| 2089 | !Field->getType()->isIncompleteArrayType()) { |
| 2090 | // FIXME: Should check for holes left by designated initializers too. |
| 2091 | for (; Field != FieldEnd && !hadError; ++Field) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 2092 | if (!Field->isUnnamedBitfield() && !Field->hasInClassInitializer()) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 2093 | CheckEmptyInitializable( |
| 2094 | InitializedEntity::InitializeMember(*Field, &Entity), |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2095 | IList->getEndLoc()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 2096 | } |
| 2097 | } |
| 2098 | |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 2099 | // Check that the types of the remaining fields have accessible destructors. |
| 2100 | if (!VerifyOnly) { |
| 2101 | // If the initializer expression has a designated initializer, check the |
| 2102 | // elements for which a designated initializer is not provided too. |
| 2103 | RecordDecl::field_iterator I = HasDesignatedInit ? RD->field_begin() |
| 2104 | : Field; |
| 2105 | for (RecordDecl::field_iterator E = RD->field_end(); I != E; ++I) { |
| 2106 | QualType ET = SemaRef.Context.getBaseElementType(I->getType()); |
| 2107 | if (hasAccessibleDestructor(ET, IList->getEndLoc(), SemaRef)) { |
| 2108 | hadError = true; |
| 2109 | return; |
| 2110 | } |
| 2111 | } |
| 2112 | } |
| 2113 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2114 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 2115 | Index >= IList->getNumInits()) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2116 | return; |
| 2117 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2118 | if (CheckFlexibleArrayInit(Entity, IList->getInit(Index), *Field, |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 2119 | TopLevelObject)) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2120 | hadError = true; |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 2121 | ++Index; |
| 2122 | return; |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2123 | } |
| 2124 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2125 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2126 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2127 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2128 | if (isa<InitListExpr>(IList->getInit(Index))) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2129 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2130 | StructuredList, StructuredIndex); |
| 2131 | else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2132 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 2133 | StructuredList, StructuredIndex); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 2134 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 2135 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2136 | /// Expand a field designator that refers to a member of an |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2137 | /// anonymous struct or union into a series of field designators that |
| 2138 | /// refers to the field within the appropriate subobject. |
| 2139 | /// |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2140 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2141 | DesignatedInitExpr *DIE, |
| 2142 | unsigned DesigIdx, |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2143 | IndirectFieldDecl *IndirectField) { |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2144 | typedef DesignatedInitExpr::Designator Designator; |
| 2145 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2146 | // Build the replacement designators. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2147 | SmallVector<Designator, 4> Replacements; |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2148 | for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), |
| 2149 | PE = IndirectField->chain_end(); PI != PE; ++PI) { |
| 2150 | if (PI + 1 == PE) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2151 | Replacements.push_back(Designator((IdentifierInfo *)nullptr, |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2152 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 2153 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 2154 | else |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2155 | Replacements.push_back(Designator((IdentifierInfo *)nullptr, |
| 2156 | SourceLocation(), SourceLocation())); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2157 | assert(isa<FieldDecl>(*PI)); |
| 2158 | Replacements.back().setField(cast<FieldDecl>(*PI)); |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2159 | } |
| 2160 | |
| 2161 | // Expand the current designator into the set of replacement |
| 2162 | // designators, so we have a full subobject path down to where the |
| 2163 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 03e8bdc | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 2164 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2165 | &Replacements[0] + Replacements.size()); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2166 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2167 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2168 | static DesignatedInitExpr *CloneDesignatedInitExpr(Sema &SemaRef, |
| 2169 | DesignatedInitExpr *DIE) { |
| 2170 | unsigned NumIndexExprs = DIE->getNumSubExprs() - 1; |
| 2171 | SmallVector<Expr*, 4> IndexExprs(NumIndexExprs); |
| 2172 | for (unsigned I = 0; I < NumIndexExprs; ++I) |
| 2173 | IndexExprs[I] = DIE->getSubExpr(I + 1); |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 2174 | return DesignatedInitExpr::Create(SemaRef.Context, DIE->designators(), |
| 2175 | IndexExprs, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2176 | DIE->getEqualOrColonLoc(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2177 | DIE->usesGNUSyntax(), DIE->getInit()); |
| 2178 | } |
| 2179 | |
Kaelyn Uhrain | b02c5e9 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 2180 | namespace { |
| 2181 | |
| 2182 | // Callback to only accept typo corrections that are for field members of |
| 2183 | // the given struct or union. |
| 2184 | class FieldInitializerValidatorCCC : public CorrectionCandidateCallback { |
| 2185 | public: |
| 2186 | explicit FieldInitializerValidatorCCC(RecordDecl *RD) |
| 2187 | : Record(RD) {} |
| 2188 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 2189 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
Kaelyn Uhrain | b02c5e9 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 2190 | FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>(); |
| 2191 | return FD && FD->getDeclContext()->getRedeclContext()->Equals(Record); |
| 2192 | } |
| 2193 | |
| 2194 | private: |
| 2195 | RecordDecl *Record; |
| 2196 | }; |
| 2197 | |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 2198 | } // end anonymous namespace |
Kaelyn Uhrain | b02c5e9 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 2199 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2200 | /// Check the well-formedness of a C99 designated initializer. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2201 | /// |
| 2202 | /// Determines whether the designated initializer @p DIE, which |
| 2203 | /// resides at the given @p Index within the initializer list @p |
| 2204 | /// IList, is well-formed for a current object of type @p DeclType |
| 2205 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2206 | /// within the current subobject is returned in either |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2207 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2208 | /// |
| 2209 | /// @param IList The initializer list in which this designated |
| 2210 | /// initializer occurs. |
| 2211 | /// |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 2212 | /// @param DIE The designated initializer expression. |
| 2213 | /// |
| 2214 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2215 | /// |
Dmitri Gribenko | adba9be | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 2216 | /// @param CurrentObjectType The type of the "current object" (C99 6.7.8p17), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2217 | /// into which the designation in @p DIE should refer. |
| 2218 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2219 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 2220 | /// a field, this will be set to the field declaration corresponding |
| 2221 | /// to the field named by the designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2222 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2223 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 2224 | /// DIE is an array designator or GNU array-range designator, this |
| 2225 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2226 | /// |
| 2227 | /// @param Index Index into @p IList where the designated initializer |
| 2228 | /// @p DIE occurs. |
| 2229 | /// |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2230 | /// @param StructuredList The initializer list expression that |
| 2231 | /// describes all of the subobject initializers in the order they'll |
| 2232 | /// actually be initialized. |
| 2233 | /// |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2234 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2235 | bool |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2236 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2237 | InitListExpr *IList, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2238 | DesignatedInitExpr *DIE, |
| 2239 | unsigned DesigIdx, |
| 2240 | QualType &CurrentObjectType, |
| 2241 | RecordDecl::field_iterator *NextField, |
| 2242 | llvm::APSInt *NextElementIndex, |
| 2243 | unsigned &Index, |
| 2244 | InitListExpr *StructuredList, |
| 2245 | unsigned &StructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2246 | bool FinishSubobjectInit, |
| 2247 | bool TopLevelObject) { |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 2248 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2249 | // Check the actual initialization for the designated object type. |
| 2250 | bool prevHadError = hadError; |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 2251 | |
| 2252 | // Temporarily remove the designator expression from the |
| 2253 | // initializer list that the child calls see, so that we don't try |
| 2254 | // to re-process the designator. |
| 2255 | unsigned OldIndex = Index; |
| 2256 | IList->setInit(OldIndex, DIE->getInit()); |
| 2257 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2258 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2259 | StructuredList, StructuredIndex); |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 2260 | |
| 2261 | // Restore the designated initializer expression in the syntactic |
| 2262 | // form of the initializer list. |
| 2263 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 2264 | DIE->setInit(IList->getInit(OldIndex)); |
| 2265 | IList->setInit(OldIndex, DIE); |
| 2266 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2267 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2268 | } |
| 2269 | |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 2270 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2271 | bool IsFirstDesignator = (DesigIdx == 0); |
| 2272 | if (!VerifyOnly) { |
| 2273 | assert((IsFirstDesignator || StructuredList) && |
| 2274 | "Need a non-designated initializer list to start from"); |
| 2275 | |
| 2276 | // Determine the structural initializer list that corresponds to the |
| 2277 | // current subobject. |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2278 | if (IsFirstDesignator) |
| 2279 | StructuredList = SyntacticToSemantic.lookup(IList); |
| 2280 | else { |
| 2281 | Expr *ExistingInit = StructuredIndex < StructuredList->getNumInits() ? |
| 2282 | StructuredList->getInit(StructuredIndex) : nullptr; |
| 2283 | if (!ExistingInit && StructuredList->hasArrayFiller()) |
| 2284 | ExistingInit = StructuredList->getArrayFiller(); |
| 2285 | |
| 2286 | if (!ExistingInit) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2287 | StructuredList = getStructuredSubobjectInit( |
| 2288 | IList, Index, CurrentObjectType, StructuredList, StructuredIndex, |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2289 | SourceRange(D->getBeginLoc(), DIE->getEndLoc())); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2290 | else if (InitListExpr *Result = dyn_cast<InitListExpr>(ExistingInit)) |
| 2291 | StructuredList = Result; |
| 2292 | else { |
| 2293 | if (DesignatedInitUpdateExpr *E = |
| 2294 | dyn_cast<DesignatedInitUpdateExpr>(ExistingInit)) |
| 2295 | StructuredList = E->getUpdater(); |
| 2296 | else { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2297 | DesignatedInitUpdateExpr *DIUE = new (SemaRef.Context) |
| 2298 | DesignatedInitUpdateExpr(SemaRef.Context, D->getBeginLoc(), |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2299 | ExistingInit, DIE->getEndLoc()); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2300 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, DIUE); |
| 2301 | StructuredList = DIUE->getUpdater(); |
| 2302 | } |
| 2303 | |
| 2304 | // We need to check on source range validity because the previous |
| 2305 | // initializer does not have to be an explicit initializer. e.g., |
| 2306 | // |
| 2307 | // struct P { int a, b; }; |
| 2308 | // struct PP { struct P p } l = { { .a = 2 }, .p.b = 3 }; |
| 2309 | // |
| 2310 | // There is an overwrite taking place because the first braced initializer |
| 2311 | // list "{ .a = 2 }" already provides value for .p.b (which is zero). |
| 2312 | if (ExistingInit->getSourceRange().isValid()) { |
| 2313 | // We are creating an initializer list that initializes the |
| 2314 | // subobjects of the current object, but there was already an |
| 2315 | // initialization that completely initialized the current |
| 2316 | // subobject, e.g., by a compound literal: |
| 2317 | // |
| 2318 | // struct X { int a, b; }; |
| 2319 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
| 2320 | // |
| 2321 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 2322 | // designated initializer re-initializes the whole |
| 2323 | // subobject [0], overwriting previous initializers. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2324 | SemaRef.Diag(D->getBeginLoc(), |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2325 | diag::warn_subobject_initializer_overrides) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2326 | << SourceRange(D->getBeginLoc(), DIE->getEndLoc()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2327 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2328 | SemaRef.Diag(ExistingInit->getBeginLoc(), |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2329 | diag::note_previous_initializer) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2330 | << /*FIXME:has side effects=*/0 << ExistingInit->getSourceRange(); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2331 | } |
| 2332 | } |
| 2333 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2334 | assert(StructuredList && "Expected a structured initializer list"); |
| 2335 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2336 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2337 | if (D->isFieldDesignator()) { |
| 2338 | // C99 6.7.8p7: |
| 2339 | // |
| 2340 | // If a designator has the form |
| 2341 | // |
| 2342 | // . identifier |
| 2343 | // |
| 2344 | // then the current object (defined below) shall have |
| 2345 | // structure or union type and the identifier shall be the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2346 | // name of a member of that type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2347 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2348 | if (!RT) { |
| 2349 | SourceLocation Loc = D->getDotLoc(); |
| 2350 | if (Loc.isInvalid()) |
| 2351 | Loc = D->getFieldLoc(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2352 | if (!VerifyOnly) |
| 2353 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2354 | << SemaRef.getLangOpts().CPlusPlus << CurrentObjectType; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2355 | ++Index; |
| 2356 | return true; |
| 2357 | } |
| 2358 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2359 | FieldDecl *KnownField = D->getField(); |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2360 | if (!KnownField) { |
| 2361 | IdentifierInfo *FieldName = D->getFieldName(); |
| 2362 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
| 2363 | for (NamedDecl *ND : Lookup) { |
| 2364 | if (auto *FD = dyn_cast<FieldDecl>(ND)) { |
| 2365 | KnownField = FD; |
| 2366 | break; |
| 2367 | } |
| 2368 | if (auto *IFD = dyn_cast<IndirectFieldDecl>(ND)) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2369 | // In verify mode, don't modify the original. |
| 2370 | if (VerifyOnly) |
| 2371 | DIE = CloneDesignatedInitExpr(SemaRef, DIE); |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2372 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IFD); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2373 | D = DIE->getDesignator(DesigIdx); |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2374 | KnownField = cast<FieldDecl>(*IFD->chain_begin()); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2375 | break; |
| 2376 | } |
| 2377 | } |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2378 | if (!KnownField) { |
| 2379 | if (VerifyOnly) { |
| 2380 | ++Index; |
| 2381 | return true; // No typo correction when just trying this out. |
| 2382 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2383 | |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2384 | // Name lookup found something, but it wasn't a field. |
| 2385 | if (!Lookup.empty()) { |
| 2386 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
| 2387 | << FieldName; |
| 2388 | SemaRef.Diag(Lookup.front()->getLocation(), |
| 2389 | diag::note_field_designator_found); |
| 2390 | ++Index; |
| 2391 | return true; |
| 2392 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2393 | |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2394 | // Name lookup didn't find anything. |
| 2395 | // Determine whether this was a typo for another field name. |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 2396 | if (TypoCorrection Corrected = SemaRef.CorrectTypo( |
| 2397 | DeclarationNameInfo(FieldName, D->getFieldLoc()), |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2398 | Sema::LookupMemberName, /*Scope=*/nullptr, /*SS=*/nullptr, |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 2399 | llvm::make_unique<FieldInitializerValidatorCCC>(RT->getDecl()), |
| 2400 | Sema::CTK_ErrorRecovery, RT->getDecl())) { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 2401 | SemaRef.diagnoseTypo( |
| 2402 | Corrected, |
| 2403 | SemaRef.PDiag(diag::err_field_designator_unknown_suggest) |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2404 | << FieldName << CurrentObjectType); |
| 2405 | KnownField = Corrected.getCorrectionDeclAs<FieldDecl>(); |
Benjamin Kramer | afe718f | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 2406 | hadError = true; |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 2407 | } else { |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2408 | // Typo correction didn't find anything. |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 2409 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 2410 | << FieldName << CurrentObjectType; |
| 2411 | ++Index; |
| 2412 | return true; |
| 2413 | } |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 2414 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2415 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2416 | |
David Majnemer | 58e4ea9 | 2014-08-23 01:48:50 +0000 | [diff] [blame] | 2417 | unsigned FieldIndex = 0; |
Akira Hatanaka | 8eccb9b | 2017-01-17 19:35:54 +0000 | [diff] [blame] | 2418 | |
| 2419 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) |
| 2420 | FieldIndex = CXXRD->getNumBases(); |
| 2421 | |
David Majnemer | 58e4ea9 | 2014-08-23 01:48:50 +0000 | [diff] [blame] | 2422 | for (auto *FI : RT->getDecl()->fields()) { |
| 2423 | if (FI->isUnnamedBitfield()) |
| 2424 | continue; |
Richard Smith | fe1bc70 | 2016-04-08 19:57:40 +0000 | [diff] [blame] | 2425 | if (declaresSameEntity(KnownField, FI)) { |
| 2426 | KnownField = FI; |
David Majnemer | 58e4ea9 | 2014-08-23 01:48:50 +0000 | [diff] [blame] | 2427 | break; |
Richard Smith | fe1bc70 | 2016-04-08 19:57:40 +0000 | [diff] [blame] | 2428 | } |
David Majnemer | 58e4ea9 | 2014-08-23 01:48:50 +0000 | [diff] [blame] | 2429 | ++FieldIndex; |
| 2430 | } |
| 2431 | |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2432 | RecordDecl::field_iterator Field = |
| 2433 | RecordDecl::field_iterator(DeclContext::decl_iterator(KnownField)); |
| 2434 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2435 | // All of the fields of a union are located at the same place in |
| 2436 | // the initializer list. |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 2437 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2438 | FieldIndex = 0; |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2439 | if (!VerifyOnly) { |
| 2440 | FieldDecl *CurrentField = StructuredList->getInitializedFieldInUnion(); |
Richard Smith | fe1bc70 | 2016-04-08 19:57:40 +0000 | [diff] [blame] | 2441 | if (CurrentField && !declaresSameEntity(CurrentField, *Field)) { |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2442 | assert(StructuredList->getNumInits() == 1 |
| 2443 | && "A union should never have more than one initializer!"); |
| 2444 | |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2445 | Expr *ExistingInit = StructuredList->getInit(0); |
Vassil Vassilev | 1a1678e | 2017-04-14 08:48:08 +0000 | [diff] [blame] | 2446 | if (ExistingInit) { |
| 2447 | // We're about to throw away an initializer, emit warning. |
| 2448 | SemaRef.Diag(D->getFieldLoc(), |
| 2449 | diag::warn_initializer_overrides) |
| 2450 | << D->getSourceRange(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2451 | SemaRef.Diag(ExistingInit->getBeginLoc(), |
Vassil Vassilev | 1a1678e | 2017-04-14 08:48:08 +0000 | [diff] [blame] | 2452 | diag::note_previous_initializer) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2453 | << /*FIXME:has side effects=*/0 |
| 2454 | << ExistingInit->getSourceRange(); |
Vassil Vassilev | 1a1678e | 2017-04-14 08:48:08 +0000 | [diff] [blame] | 2455 | } |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2456 | |
| 2457 | // remove existing initializer |
| 2458 | StructuredList->resizeInits(SemaRef.Context, 0); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2459 | StructuredList->setInitializedFieldInUnion(nullptr); |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2460 | } |
| 2461 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2462 | StructuredList->setInitializedFieldInUnion(*Field); |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2463 | } |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 2464 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2465 | |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2466 | // Make sure we can use this declaration. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2467 | bool InvalidUse; |
| 2468 | if (VerifyOnly) |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 2469 | InvalidUse = !SemaRef.CanUseDecl(*Field, TreatUnavailableAsInvalid); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2470 | else |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2471 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, D->getFieldLoc()); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2472 | if (InvalidUse) { |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2473 | ++Index; |
| 2474 | return true; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2475 | } |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2476 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2477 | if (!VerifyOnly) { |
| 2478 | // Update the designator with the field declaration. |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2479 | D->setField(*Field); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2480 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2481 | // Make sure that our non-designated initializer list has space |
| 2482 | // for a subobject corresponding to this field. |
| 2483 | if (FieldIndex >= StructuredList->getNumInits()) |
| 2484 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
| 2485 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2486 | |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2487 | // This designator names a flexible array member. |
| 2488 | if (Field->getType()->isIncompleteArrayType()) { |
| 2489 | bool Invalid = false; |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 2490 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2491 | // We can't designate an object within the flexible array |
| 2492 | // member (because GCC doesn't allow it). |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2493 | if (!VerifyOnly) { |
| 2494 | DesignatedInitExpr::Designator *NextD |
| 2495 | = DIE->getDesignator(DesigIdx + 1); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2496 | SemaRef.Diag(NextD->getBeginLoc(), |
| 2497 | diag::err_designator_into_flexible_array_member) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2498 | << SourceRange(NextD->getBeginLoc(), DIE->getEndLoc()); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2499 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2500 | << *Field; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2501 | } |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2502 | Invalid = true; |
| 2503 | } |
| 2504 | |
Chris Lattner | 001b29c | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 2505 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 2506 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2507 | // The initializer is not an initializer list. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2508 | if (!VerifyOnly) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2509 | SemaRef.Diag(DIE->getInit()->getBeginLoc(), |
| 2510 | diag::err_flexible_array_init_needs_braces) |
| 2511 | << DIE->getInit()->getSourceRange(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2512 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2513 | << *Field; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2514 | } |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2515 | Invalid = true; |
| 2516 | } |
| 2517 | |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 2518 | // Check GNU flexible array initializer. |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2519 | if (!Invalid && CheckFlexibleArrayInit(Entity, DIE->getInit(), *Field, |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 2520 | TopLevelObject)) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2521 | Invalid = true; |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2522 | |
| 2523 | if (Invalid) { |
| 2524 | ++Index; |
| 2525 | return true; |
| 2526 | } |
| 2527 | |
| 2528 | // Initialize the array. |
| 2529 | bool prevHadError = hadError; |
| 2530 | unsigned newStructuredIndex = FieldIndex; |
| 2531 | unsigned OldIndex = Index; |
| 2532 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2533 | |
| 2534 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2535 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2536 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2537 | StructuredList, newStructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2538 | |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2539 | IList->setInit(OldIndex, DIE); |
| 2540 | if (hadError && !prevHadError) { |
| 2541 | ++Field; |
| 2542 | ++FieldIndex; |
| 2543 | if (NextField) |
| 2544 | *NextField = Field; |
| 2545 | StructuredIndex = FieldIndex; |
| 2546 | return true; |
| 2547 | } |
| 2548 | } else { |
| 2549 | // Recurse to check later designated subobjects. |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 2550 | QualType FieldType = Field->getType(); |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2551 | unsigned newStructuredIndex = FieldIndex; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2552 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2553 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2554 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2555 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2556 | FieldType, nullptr, nullptr, Index, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2557 | StructuredList, newStructuredIndex, |
Alexey Bataev | 86a489e | 2016-01-25 05:14:03 +0000 | [diff] [blame] | 2558 | FinishSubobjectInit, false)) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2559 | return true; |
| 2560 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2561 | |
| 2562 | // Find the position of the next field to be initialized in this |
| 2563 | // subobject. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2564 | ++Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2565 | ++FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2566 | |
| 2567 | // If this the first designator, our caller will continue checking |
| 2568 | // the rest of this struct/class/union subobject. |
| 2569 | if (IsFirstDesignator) { |
| 2570 | if (NextField) |
| 2571 | *NextField = Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2572 | StructuredIndex = FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2573 | return false; |
| 2574 | } |
| 2575 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2576 | if (!FinishSubobjectInit) |
| 2577 | return false; |
| 2578 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2579 | // We've already initialized something in the union; we're done. |
| 2580 | if (RT->getDecl()->isUnion()) |
| 2581 | return hadError; |
| 2582 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2583 | // Check the remaining fields within this class/struct/union subobject. |
| 2584 | bool prevHadError = hadError; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2585 | |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 2586 | auto NoBases = |
| 2587 | CXXRecordDecl::base_class_range(CXXRecordDecl::base_class_iterator(), |
| 2588 | CXXRecordDecl::base_class_iterator()); |
| 2589 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, NoBases, Field, |
| 2590 | false, Index, StructuredList, FieldIndex); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2591 | return hadError && !prevHadError; |
| 2592 | } |
| 2593 | |
| 2594 | // C99 6.7.8p6: |
| 2595 | // |
| 2596 | // If a designator has the form |
| 2597 | // |
| 2598 | // [ constant-expression ] |
| 2599 | // |
| 2600 | // then the current object (defined below) shall have array |
| 2601 | // type and the expression shall be an integer constant |
| 2602 | // expression. If the array is of unknown size, any |
| 2603 | // nonnegative value is valid. |
| 2604 | // |
| 2605 | // Additionally, cope with the GNU extension that permits |
| 2606 | // designators of the form |
| 2607 | // |
| 2608 | // [ constant-expression ... constant-expression ] |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 2609 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2610 | if (!AT) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2611 | if (!VerifyOnly) |
| 2612 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
| 2613 | << CurrentObjectType; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2614 | ++Index; |
| 2615 | return true; |
| 2616 | } |
| 2617 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2618 | Expr *IndexExpr = nullptr; |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2619 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 2620 | if (D->isArrayDesignator()) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2621 | IndexExpr = DIE->getArrayIndex(*D); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2622 | DesignatedStartIndex = IndexExpr->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2623 | DesignatedEndIndex = DesignatedStartIndex; |
| 2624 | } else { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2625 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2626 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2627 | DesignatedStartIndex = |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2628 | DIE->getArrayRangeStart(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2629 | DesignatedEndIndex = |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2630 | DIE->getArrayRangeEnd(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2631 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2632 | |
Chris Lattner | b0ed51d | 2011-02-19 22:28:58 +0000 | [diff] [blame] | 2633 | // Codegen can't handle evaluating array range designators that have side |
| 2634 | // effects, because we replicate the AST value for each initialized element. |
| 2635 | // As such, set the sawArrayRangeDesignator() bit if we initialize multiple |
| 2636 | // elements with something that has a side effect, so codegen can emit an |
| 2637 | // "error unsupported" error instead of miscompiling the app. |
| 2638 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2639 | DIE->getInit()->HasSideEffects(SemaRef.Context) && !VerifyOnly) |
Douglas Gregor | bf7207a | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 2640 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2641 | } |
| 2642 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2643 | if (isa<ConstantArrayType>(AT)) { |
| 2644 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2645 | DesignatedStartIndex |
| 2646 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2647 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2648 | DesignatedEndIndex |
| 2649 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2650 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 2651 | if (DesignatedEndIndex >= MaxElements) { |
Eli Friedman | ed0f916 | 2011-09-26 18:53:43 +0000 | [diff] [blame] | 2652 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2653 | SemaRef.Diag(IndexExpr->getBeginLoc(), |
| 2654 | diag::err_array_designator_too_large) |
| 2655 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
| 2656 | << IndexExpr->getSourceRange(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2657 | ++Index; |
| 2658 | return true; |
| 2659 | } |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2660 | } else { |
Argyrios Kyrtzidis | 4746c2f | 2015-07-27 23:16:53 +0000 | [diff] [blame] | 2661 | unsigned DesignatedIndexBitWidth = |
| 2662 | ConstantArrayType::getMaxSizeBits(SemaRef.Context); |
| 2663 | DesignatedStartIndex = |
| 2664 | DesignatedStartIndex.extOrTrunc(DesignatedIndexBitWidth); |
| 2665 | DesignatedEndIndex = |
| 2666 | DesignatedEndIndex.extOrTrunc(DesignatedIndexBitWidth); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2667 | DesignatedStartIndex.setIsUnsigned(true); |
| 2668 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2669 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2670 | |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2671 | if (!VerifyOnly && StructuredList->isStringLiteralInit()) { |
| 2672 | // We're modifying a string literal init; we have to decompose the string |
| 2673 | // so we can modify the individual characters. |
| 2674 | ASTContext &Context = SemaRef.Context; |
| 2675 | Expr *SubExpr = StructuredList->getInit(0)->IgnoreParens(); |
| 2676 | |
| 2677 | // Compute the character type |
| 2678 | QualType CharTy = AT->getElementType(); |
| 2679 | |
| 2680 | // Compute the type of the integer literals. |
| 2681 | QualType PromotedCharTy = CharTy; |
| 2682 | if (CharTy->isPromotableIntegerType()) |
| 2683 | PromotedCharTy = Context.getPromotedIntegerType(CharTy); |
| 2684 | unsigned PromotedCharTyWidth = Context.getTypeSize(PromotedCharTy); |
| 2685 | |
| 2686 | if (StringLiteral *SL = dyn_cast<StringLiteral>(SubExpr)) { |
| 2687 | // Get the length of the string. |
| 2688 | uint64_t StrLen = SL->getLength(); |
| 2689 | if (cast<ConstantArrayType>(AT)->getSize().ult(StrLen)) |
| 2690 | StrLen = cast<ConstantArrayType>(AT)->getSize().getZExtValue(); |
| 2691 | StructuredList->resizeInits(Context, StrLen); |
| 2692 | |
| 2693 | // Build a literal for each character in the string, and put them into |
| 2694 | // the init list. |
| 2695 | for (unsigned i = 0, e = StrLen; i != e; ++i) { |
| 2696 | llvm::APInt CodeUnit(PromotedCharTyWidth, SL->getCodeUnit(i)); |
| 2697 | Expr *Init = new (Context) IntegerLiteral( |
Eli Friedman | 6cc05f7 | 2013-06-11 22:26:34 +0000 | [diff] [blame] | 2698 | Context, CodeUnit, PromotedCharTy, SubExpr->getExprLoc()); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2699 | if (CharTy != PromotedCharTy) |
| 2700 | Init = ImplicitCastExpr::Create(Context, CharTy, CK_IntegralCast, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2701 | Init, nullptr, VK_RValue); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2702 | StructuredList->updateInit(Context, i, Init); |
| 2703 | } |
| 2704 | } else { |
| 2705 | ObjCEncodeExpr *E = cast<ObjCEncodeExpr>(SubExpr); |
| 2706 | std::string Str; |
| 2707 | Context.getObjCEncodingForType(E->getEncodedType(), Str); |
| 2708 | |
| 2709 | // Get the length of the string. |
| 2710 | uint64_t StrLen = Str.size(); |
| 2711 | if (cast<ConstantArrayType>(AT)->getSize().ult(StrLen)) |
| 2712 | StrLen = cast<ConstantArrayType>(AT)->getSize().getZExtValue(); |
| 2713 | StructuredList->resizeInits(Context, StrLen); |
| 2714 | |
| 2715 | // Build a literal for each character in the string, and put them into |
| 2716 | // the init list. |
| 2717 | for (unsigned i = 0, e = StrLen; i != e; ++i) { |
| 2718 | llvm::APInt CodeUnit(PromotedCharTyWidth, Str[i]); |
| 2719 | Expr *Init = new (Context) IntegerLiteral( |
Eli Friedman | 6cc05f7 | 2013-06-11 22:26:34 +0000 | [diff] [blame] | 2720 | Context, CodeUnit, PromotedCharTy, SubExpr->getExprLoc()); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2721 | if (CharTy != PromotedCharTy) |
| 2722 | Init = ImplicitCastExpr::Create(Context, CharTy, CK_IntegralCast, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2723 | Init, nullptr, VK_RValue); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2724 | StructuredList->updateInit(Context, i, Init); |
| 2725 | } |
| 2726 | } |
| 2727 | } |
| 2728 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2729 | // Make sure that our non-designated initializer list has space |
| 2730 | // for a subobject corresponding to this array element. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2731 | if (!VerifyOnly && |
| 2732 | DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2733 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2734 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2735 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2736 | // Repeatedly perform subobject initializations in the range |
| 2737 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2738 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2739 | // Move to the next designator |
| 2740 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2741 | unsigned OldIndex = Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2742 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2743 | InitializedEntity ElementEntity = |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2744 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2745 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2746 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 2747 | // Recurse to check later designated subobjects. |
| 2748 | QualType ElementType = AT->getElementType(); |
| 2749 | Index = OldIndex; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2750 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2751 | ElementEntity.setElementIndex(ElementIndex); |
Alexey Bataev | 86a489e | 2016-01-25 05:14:03 +0000 | [diff] [blame] | 2752 | if (CheckDesignatedInitializer( |
| 2753 | ElementEntity, IList, DIE, DesigIdx + 1, ElementType, nullptr, |
| 2754 | nullptr, Index, StructuredList, ElementIndex, |
| 2755 | FinishSubobjectInit && (DesignatedStartIndex == DesignatedEndIndex), |
| 2756 | false)) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2757 | return true; |
| 2758 | |
| 2759 | // Move to the next index in the array that we'll be initializing. |
| 2760 | ++DesignatedStartIndex; |
| 2761 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2762 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2763 | |
| 2764 | // If this the first designator, our caller will continue checking |
| 2765 | // the rest of this array subobject. |
| 2766 | if (IsFirstDesignator) { |
| 2767 | if (NextElementIndex) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2768 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2769 | StructuredIndex = ElementIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2770 | return false; |
| 2771 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2772 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2773 | if (!FinishSubobjectInit) |
| 2774 | return false; |
| 2775 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2776 | // Check the remaining elements within this array subobject. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2777 | bool prevHadError = hadError; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2778 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 2779 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2780 | StructuredList, ElementIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2781 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2782 | } |
| 2783 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2784 | // Get the structured initializer list for a subobject of type |
| 2785 | // @p CurrentObjectType. |
| 2786 | InitListExpr * |
| 2787 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 2788 | QualType CurrentObjectType, |
| 2789 | InitListExpr *StructuredList, |
| 2790 | unsigned StructuredIndex, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2791 | SourceRange InitRange, |
| 2792 | bool IsFullyOverwritten) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2793 | if (VerifyOnly) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2794 | return nullptr; // No structured list in verification-only mode. |
| 2795 | Expr *ExistingInit = nullptr; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2796 | if (!StructuredList) |
Benjamin Kramer | 6b441d6 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 2797 | ExistingInit = SyntacticToSemantic.lookup(IList); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2798 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 2799 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2800 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2801 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2802 | // There might have already been initializers for subobjects of the current |
| 2803 | // object, but a subsequent initializer list will overwrite the entirety |
| 2804 | // of the current object. (See DR 253 and C99 6.7.8p21). e.g., |
| 2805 | // |
| 2806 | // struct P { char x[6]; }; |
| 2807 | // struct P l = { .x[2] = 'x', .x = { [0] = 'f' } }; |
| 2808 | // |
| 2809 | // The first designated initializer is ignored, and l.x is just "f". |
| 2810 | if (!IsFullyOverwritten) |
| 2811 | return Result; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2812 | |
| 2813 | if (ExistingInit) { |
| 2814 | // We are creating an initializer list that initializes the |
| 2815 | // subobjects of the current object, but there was already an |
| 2816 | // initialization that completely initialized the current |
| 2817 | // subobject, e.g., by a compound literal: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2818 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2819 | // struct X { int a, b; }; |
| 2820 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2821 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2822 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 2823 | // designated initializer re-initializes the whole |
| 2824 | // subobject [0], overwriting previous initializers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2825 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2826 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2827 | << InitRange; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2828 | SemaRef.Diag(ExistingInit->getBeginLoc(), diag::note_previous_initializer) |
| 2829 | << /*FIXME:has side effects=*/0 << ExistingInit->getSourceRange(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2830 | } |
| 2831 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2832 | InitListExpr *Result |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2833 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
Dmitri Gribenko | 78852e9 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 2834 | InitRange.getBegin(), None, |
Ted Kremenek | 013041e | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 2835 | InitRange.getEnd()); |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2836 | |
Eli Friedman | 91f5ae5 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 2837 | QualType ResultType = CurrentObjectType; |
| 2838 | if (!ResultType->isArrayType()) |
| 2839 | ResultType = ResultType.getNonLValueExprType(SemaRef.Context); |
| 2840 | Result->setType(ResultType); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2841 | |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2842 | // Pre-allocate storage for the structured initializer list. |
| 2843 | unsigned NumElements = 0; |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2844 | unsigned NumInits = 0; |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2845 | bool GotNumInits = false; |
| 2846 | if (!StructuredList) { |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2847 | NumInits = IList->getNumInits(); |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2848 | GotNumInits = true; |
| 2849 | } else if (Index < IList->getNumInits()) { |
| 2850 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) { |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2851 | NumInits = SubList->getNumInits(); |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2852 | GotNumInits = true; |
| 2853 | } |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2854 | } |
| 2855 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2856 | if (const ArrayType *AType |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2857 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 2858 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 2859 | NumElements = CAType->getSize().getZExtValue(); |
| 2860 | // Simple heuristic so that we don't allocate a very large |
| 2861 | // initializer with many empty entries at the end. |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2862 | if (GotNumInits && NumElements > NumInits) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2863 | NumElements = 0; |
| 2864 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2865 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2866 | NumElements = VType->getNumElements(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2867 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2868 | RecordDecl *RDecl = RType->getDecl(); |
| 2869 | if (RDecl->isUnion()) |
| 2870 | NumElements = 1; |
| 2871 | else |
Aaron Ballman | 62e47c4 | 2014-03-10 13:43:55 +0000 | [diff] [blame] | 2872 | NumElements = std::distance(RDecl->field_begin(), RDecl->field_end()); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2873 | } |
| 2874 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2875 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2876 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2877 | // Link this new initializer list into the structured initializer |
| 2878 | // lists. |
| 2879 | if (StructuredList) |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2880 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2881 | else { |
| 2882 | Result->setSyntacticForm(IList); |
| 2883 | SyntacticToSemantic[IList] = Result; |
| 2884 | } |
| 2885 | |
| 2886 | return Result; |
| 2887 | } |
| 2888 | |
| 2889 | /// Update the initializer at index @p StructuredIndex within the |
| 2890 | /// structured initializer list to the value @p expr. |
| 2891 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 2892 | unsigned &StructuredIndex, |
| 2893 | Expr *expr) { |
| 2894 | // No structured initializer list to update |
| 2895 | if (!StructuredList) |
| 2896 | return; |
| 2897 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2898 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 2899 | StructuredIndex, expr)) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2900 | // This initializer overwrites a previous initializer. Warn. |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2901 | // We need to check on source range validity because the previous |
| 2902 | // initializer does not have to be an explicit initializer. |
| 2903 | // struct P { int a, b; }; |
| 2904 | // struct PP { struct P p } l = { { .a = 2 }, .p.b = 3 }; |
| 2905 | // There is an overwrite taking place because the first braced initializer |
| 2906 | // list "{ .a = 2 }' already provides value for .p.b (which is zero). |
| 2907 | if (PrevInit->getSourceRange().isValid()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2908 | SemaRef.Diag(expr->getBeginLoc(), diag::warn_initializer_overrides) |
| 2909 | << expr->getSourceRange(); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2910 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2911 | SemaRef.Diag(PrevInit->getBeginLoc(), diag::note_previous_initializer) |
| 2912 | << /*FIXME:has side effects=*/0 << PrevInit->getSourceRange(); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2913 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2914 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2915 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2916 | ++StructuredIndex; |
| 2917 | } |
| 2918 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2919 | /// Check that the given Index expression is a valid array designator |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2920 | /// value. This is essentially just a wrapper around |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2921 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2922 | /// and produces a reasonable diagnostic if there is a |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2923 | /// failure. Returns the index expression, possibly with an implicit cast |
| 2924 | /// added, on success. If everything went okay, Value will receive the |
| 2925 | /// value of the constant expression. |
| 2926 | static ExprResult |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2927 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2928 | SourceLocation Loc = Index->getBeginLoc(); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2929 | |
| 2930 | // Make sure this is an integer constant expression. |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2931 | ExprResult Result = S.VerifyIntegerConstantExpression(Index, &Value); |
| 2932 | if (Result.isInvalid()) |
| 2933 | return Result; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2934 | |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2935 | if (Value.isSigned() && Value.isNegative()) |
| 2936 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2937 | << Value.toString(10) << Index->getSourceRange(); |
| 2938 | |
Douglas Gregor | 51650d3 | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 2939 | Value.setIsUnsigned(true); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2940 | return Result; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2941 | } |
| 2942 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2943 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 2944 | SourceLocation Loc, |
| 2945 | bool GNUSyntax, |
| 2946 | ExprResult Init) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2947 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 2948 | |
| 2949 | bool Invalid = false; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2950 | SmallVector<ASTDesignator, 32> Designators; |
| 2951 | SmallVector<Expr *, 32> InitExpressions; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2952 | |
| 2953 | // Build designators and check array designator expressions. |
| 2954 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 2955 | const Designator &D = Desig.getDesignator(Idx); |
| 2956 | switch (D.getKind()) { |
| 2957 | case Designator::FieldDesignator: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2958 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2959 | D.getFieldLoc())); |
| 2960 | break; |
| 2961 | |
| 2962 | case Designator::ArrayDesignator: { |
| 2963 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 2964 | llvm::APSInt IndexValue; |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2965 | if (!Index->isTypeDependent() && !Index->isValueDependent()) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2966 | Index = CheckArrayDesignatorExpr(*this, Index, IndexValue).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2967 | if (!Index) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2968 | Invalid = true; |
| 2969 | else { |
| 2970 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2971 | D.getLBracketLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2972 | D.getRBracketLoc())); |
| 2973 | InitExpressions.push_back(Index); |
| 2974 | } |
| 2975 | break; |
| 2976 | } |
| 2977 | |
| 2978 | case Designator::ArrayRangeDesignator: { |
| 2979 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 2980 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 2981 | llvm::APSInt StartValue; |
| 2982 | llvm::APSInt EndValue; |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2983 | bool StartDependent = StartIndex->isTypeDependent() || |
| 2984 | StartIndex->isValueDependent(); |
| 2985 | bool EndDependent = EndIndex->isTypeDependent() || |
| 2986 | EndIndex->isValueDependent(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2987 | if (!StartDependent) |
| 2988 | StartIndex = |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2989 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2990 | if (!EndDependent) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2991 | EndIndex = CheckArrayDesignatorExpr(*this, EndIndex, EndValue).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2992 | |
| 2993 | if (!StartIndex || !EndIndex) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2994 | Invalid = true; |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2995 | else { |
| 2996 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2997 | if (StartDependent || EndDependent) { |
| 2998 | // Nothing to compute. |
| 2999 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3000 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 3001 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3002 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 3003 | |
Douglas Gregor | 0f9d400 | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 3004 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 3005 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3006 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 3007 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 3008 | Invalid = true; |
| 3009 | } else { |
| 3010 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3011 | D.getLBracketLoc(), |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 3012 | D.getEllipsisLoc(), |
| 3013 | D.getRBracketLoc())); |
| 3014 | InitExpressions.push_back(StartIndex); |
| 3015 | InitExpressions.push_back(EndIndex); |
| 3016 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3017 | } |
| 3018 | break; |
| 3019 | } |
| 3020 | } |
| 3021 | } |
| 3022 | |
| 3023 | if (Invalid || Init.isInvalid()) |
| 3024 | return ExprError(); |
| 3025 | |
| 3026 | // Clear out the expressions within the designation. |
| 3027 | Desig.ClearExprs(*this); |
| 3028 | |
| 3029 | DesignatedInitExpr *DIE |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 3030 | = DesignatedInitExpr::Create(Context, |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 3031 | Designators, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3032 | InitExpressions, Loc, GNUSyntax, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3033 | Init.getAs<Expr>()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3034 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3035 | if (!getLangOpts().C99) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3036 | Diag(DIE->getBeginLoc(), diag::ext_designated_init) |
| 3037 | << DIE->getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3038 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3039 | return DIE; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3040 | } |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 3041 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3042 | //===----------------------------------------------------------------------===// |
| 3043 | // Initialization entity |
| 3044 | //===----------------------------------------------------------------------===// |
| 3045 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3046 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 3047 | const InitializedEntity &Parent) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3048 | : Parent(&Parent), Index(Index) |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 3049 | { |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3050 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 3051 | Kind = EK_ArrayElement; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3052 | Type = AT->getElementType(); |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3053 | } else if (const VectorType *VT = Parent.getType()->getAs<VectorType>()) { |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3054 | Kind = EK_VectorElement; |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3055 | Type = VT->getElementType(); |
| 3056 | } else { |
| 3057 | const ComplexType *CT = Parent.getType()->getAs<ComplexType>(); |
| 3058 | assert(CT && "Unexpected type"); |
| 3059 | Kind = EK_ComplexElement; |
| 3060 | Type = CT->getElementType(); |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3061 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3062 | } |
| 3063 | |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 3064 | InitializedEntity |
| 3065 | InitializedEntity::InitializeBase(ASTContext &Context, |
| 3066 | const CXXBaseSpecifier *Base, |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 3067 | bool IsInheritedVirtualBase, |
| 3068 | const InitializedEntity *Parent) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3069 | InitializedEntity Result; |
| 3070 | Result.Kind = EK_Base; |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 3071 | Result.Parent = Parent; |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 3072 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 3073 | if (IsInheritedVirtualBase) |
| 3074 | Result.Base |= 0x01; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3075 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3076 | Result.Type = Base->getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3077 | return Result; |
| 3078 | } |
| 3079 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3080 | DeclarationName InitializedEntity::getName() const { |
| 3081 | switch (getKind()) { |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3082 | case EK_Parameter: |
| 3083 | case EK_Parameter_CF_Audited: { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3084 | ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 3085 | return (D ? D->getDeclName() : DeclarationName()); |
| 3086 | } |
Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3087 | |
| 3088 | case EK_Variable: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3089 | case EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3090 | case EK_Binding: |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3091 | return Variable.VariableOrMember->getDeclName(); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3092 | |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 3093 | case EK_LambdaCapture: |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 3094 | return DeclarationName(Capture.VarID); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3095 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3096 | case EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 3097 | case EK_StmtExprResult: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3098 | case EK_Exception: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3099 | case EK_New: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3100 | case EK_Temporary: |
| 3101 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3102 | case EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3103 | case EK_ArrayElement: |
| 3104 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3105 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3106 | case EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3107 | case EK_LambdaToBlockConversionBlockElement: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 3108 | case EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 3109 | case EK_RelatedResult: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3110 | return DeclarationName(); |
| 3111 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3112 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3113 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3114 | } |
| 3115 | |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3116 | ValueDecl *InitializedEntity::getDecl() const { |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3117 | switch (getKind()) { |
| 3118 | case EK_Variable: |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3119 | case EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3120 | case EK_Binding: |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3121 | return Variable.VariableOrMember; |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3122 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3123 | case EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3124 | case EK_Parameter_CF_Audited: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3125 | return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 3126 | |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3127 | case EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 3128 | case EK_StmtExprResult: |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3129 | case EK_Exception: |
| 3130 | case EK_New: |
| 3131 | case EK_Temporary: |
| 3132 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3133 | case EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3134 | case EK_ArrayElement: |
| 3135 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3136 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3137 | case EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3138 | case EK_LambdaToBlockConversionBlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 3139 | case EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 3140 | case EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 3141 | case EK_RelatedResult: |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3142 | return nullptr; |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3143 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3144 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3145 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3146 | } |
| 3147 | |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3148 | bool InitializedEntity::allowsNRVO() const { |
| 3149 | switch (getKind()) { |
| 3150 | case EK_Result: |
| 3151 | case EK_Exception: |
| 3152 | return LocAndNRVO.NRVO; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3153 | |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 3154 | case EK_StmtExprResult: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3155 | case EK_Variable: |
| 3156 | case EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3157 | case EK_Parameter_CF_Audited: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3158 | case EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3159 | case EK_Binding: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3160 | case EK_New: |
| 3161 | case EK_Temporary: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 3162 | case EK_CompoundLiteralInit: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3163 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3164 | case EK_Delegating: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3165 | case EK_ArrayElement: |
| 3166 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3167 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3168 | case EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3169 | case EK_LambdaToBlockConversionBlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 3170 | case EK_LambdaCapture: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 3171 | case EK_RelatedResult: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3172 | break; |
| 3173 | } |
| 3174 | |
| 3175 | return false; |
| 3176 | } |
| 3177 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3178 | unsigned InitializedEntity::dumpImpl(raw_ostream &OS) const { |
Richard Smith | e3b28bc | 2013-06-12 21:51:50 +0000 | [diff] [blame] | 3179 | assert(getParent() != this); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3180 | unsigned Depth = getParent() ? getParent()->dumpImpl(OS) : 0; |
| 3181 | for (unsigned I = 0; I != Depth; ++I) |
| 3182 | OS << "`-"; |
| 3183 | |
| 3184 | switch (getKind()) { |
| 3185 | case EK_Variable: OS << "Variable"; break; |
| 3186 | case EK_Parameter: OS << "Parameter"; break; |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3187 | case EK_Parameter_CF_Audited: OS << "CF audited function Parameter"; |
| 3188 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3189 | case EK_Result: OS << "Result"; break; |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 3190 | case EK_StmtExprResult: OS << "StmtExprResult"; break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3191 | case EK_Exception: OS << "Exception"; break; |
| 3192 | case EK_Member: OS << "Member"; break; |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3193 | case EK_Binding: OS << "Binding"; break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3194 | case EK_New: OS << "New"; break; |
| 3195 | case EK_Temporary: OS << "Temporary"; break; |
| 3196 | case EK_CompoundLiteralInit: OS << "CompoundLiteral";break; |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 3197 | case EK_RelatedResult: OS << "RelatedResult"; break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3198 | case EK_Base: OS << "Base"; break; |
| 3199 | case EK_Delegating: OS << "Delegating"; break; |
| 3200 | case EK_ArrayElement: OS << "ArrayElement " << Index; break; |
| 3201 | case EK_VectorElement: OS << "VectorElement " << Index; break; |
| 3202 | case EK_ComplexElement: OS << "ComplexElement " << Index; break; |
| 3203 | case EK_BlockElement: OS << "Block"; break; |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3204 | case EK_LambdaToBlockConversionBlockElement: |
| 3205 | OS << "Block (lambda)"; |
| 3206 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3207 | case EK_LambdaCapture: |
| 3208 | OS << "LambdaCapture "; |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 3209 | OS << DeclarationName(Capture.VarID); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3210 | break; |
| 3211 | } |
| 3212 | |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3213 | if (auto *D = getDecl()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3214 | OS << " "; |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3215 | D->printQualifiedName(OS); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3216 | } |
| 3217 | |
| 3218 | OS << " '" << getType().getAsString() << "'\n"; |
| 3219 | |
| 3220 | return Depth + 1; |
| 3221 | } |
| 3222 | |
Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 3223 | LLVM_DUMP_METHOD void InitializedEntity::dump() const { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3224 | dumpImpl(llvm::errs()); |
| 3225 | } |
| 3226 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3227 | //===----------------------------------------------------------------------===// |
| 3228 | // Initialization sequence |
| 3229 | //===----------------------------------------------------------------------===// |
| 3230 | |
| 3231 | void InitializationSequence::Step::Destroy() { |
| 3232 | switch (Kind) { |
| 3233 | case SK_ResolveAddressOfOverloadedFunction: |
| 3234 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3235 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3236 | case SK_CastDerivedToBaseLValue: |
| 3237 | case SK_BindReference: |
| 3238 | case SK_BindReferenceToTemporary: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 3239 | case SK_FinalCopy: |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3240 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3241 | case SK_UserConversion: |
| 3242 | case SK_QualificationConversionRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3243 | case SK_QualificationConversionXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3244 | case SK_QualificationConversionLValue: |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 3245 | case SK_AtomicConversion: |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3246 | case SK_LValueToRValue: |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3247 | case SK_ListInitialization: |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3248 | case SK_UnwrapInitList: |
| 3249 | case SK_RewrapInitList: |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3250 | case SK_ConstructorInitialization: |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 3251 | case SK_ConstructorInitializationFromList: |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3252 | case SK_ZeroInitialization: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3253 | case SK_CAssignment: |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3254 | case SK_StringInit: |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3255 | case SK_ObjCObjectConversion: |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3256 | case SK_ArrayLoopIndex: |
| 3257 | case SK_ArrayLoopInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3258 | case SK_ArrayInit: |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 3259 | case SK_GNUArrayInit: |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 3260 | case SK_ParenthesizedArrayInit: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3261 | case SK_PassByIndirectCopyRestore: |
| 3262 | case SK_PassByIndirectRestore: |
| 3263 | case SK_ProduceObjCObject: |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 3264 | case SK_StdInitializerList: |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3265 | case SK_StdInitializerListConstructorCall: |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 3266 | case SK_OCLSamplerInit: |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 3267 | case SK_OCLZeroOpaqueType: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3268 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3269 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3270 | case SK_ConversionSequence: |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 3271 | case SK_ConversionSequenceNoNarrowing: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3272 | delete ICS; |
| 3273 | } |
| 3274 | } |
| 3275 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3276 | bool InitializationSequence::isDirectReferenceBinding() const { |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 3277 | // There can be some lvalue adjustments after the SK_BindReference step. |
| 3278 | for (auto I = Steps.rbegin(); I != Steps.rend(); ++I) { |
| 3279 | if (I->Kind == SK_BindReference) |
| 3280 | return true; |
| 3281 | if (I->Kind == SK_BindReferenceToTemporary) |
| 3282 | return false; |
| 3283 | } |
| 3284 | return false; |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3285 | } |
| 3286 | |
| 3287 | bool InitializationSequence::isAmbiguous() const { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 3288 | if (!Failed()) |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3289 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3290 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3291 | switch (getFailureKind()) { |
| 3292 | case FK_TooManyInitsForReference: |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 3293 | case FK_ParenthesizedListInitForReference: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3294 | case FK_ArrayNeedsInitList: |
| 3295 | case FK_ArrayNeedsInitListOrStringLiteral: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 3296 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 3297 | case FK_NarrowStringIntoWideCharArray: |
| 3298 | case FK_WideStringIntoCharArray: |
| 3299 | case FK_IncompatWideStringIntoWideChar: |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 3300 | case FK_PlainStringIntoUTF8Char: |
| 3301 | case FK_UTF8StringIntoPlainChar: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3302 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 3303 | case FK_NonConstLValueReferenceBindingToTemporary: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 3304 | case FK_NonConstLValueReferenceBindingToBitfield: |
| 3305 | case FK_NonConstLValueReferenceBindingToVectorElement: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3306 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 3307 | case FK_RValueReferenceBindingToLValue: |
| 3308 | case FK_ReferenceInitDropsQualifiers: |
| 3309 | case FK_ReferenceInitFailed: |
| 3310 | case FK_ConversionFailed: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3311 | case FK_ConversionFromPropertyFailed: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3312 | case FK_TooManyInitsForScalar: |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 3313 | case FK_ParenthesizedListInitForScalar: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3314 | case FK_ReferenceBindingToInitList: |
| 3315 | case FK_InitListBadDestinationType: |
| 3316 | case FK_DefaultInitOfConst: |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 3317 | case FK_Incomplete: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3318 | case FK_ArrayTypeMismatch: |
| 3319 | case FK_NonConstantArrayInit: |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 3320 | case FK_ListInitializationFailed: |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 3321 | case FK_VariableLengthArrayHasInitializer: |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3322 | case FK_PlaceholderType: |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3323 | case FK_ExplicitConstructor: |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 3324 | case FK_AddressOfUnaddressableFunction: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3325 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3326 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3327 | case FK_ReferenceInitOverloadFailed: |
| 3328 | case FK_UserConversionOverloadFailed: |
| 3329 | case FK_ConstructorOverloadFailed: |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3330 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3331 | return FailedOverloadResult == OR_Ambiguous; |
| 3332 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3333 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3334 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3335 | } |
| 3336 | |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 3337 | bool InitializationSequence::isConstructorInitialization() const { |
| 3338 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 3339 | } |
| 3340 | |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3341 | void |
| 3342 | InitializationSequence |
| 3343 | ::AddAddressOverloadResolutionStep(FunctionDecl *Function, |
| 3344 | DeclAccessPair Found, |
| 3345 | bool HadMultipleCandidates) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3346 | Step S; |
| 3347 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 3348 | S.Type = Function->getType(); |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3349 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3350 | S.Function.Function = Function; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3351 | S.Function.FoundDecl = Found; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3352 | Steps.push_back(S); |
| 3353 | } |
| 3354 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3355 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3356 | ExprValueKind VK) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3357 | Step S; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3358 | switch (VK) { |
| 3359 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 3360 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 3361 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3362 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3363 | S.Type = BaseType; |
| 3364 | Steps.push_back(S); |
| 3365 | } |
| 3366 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3367 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3368 | bool BindingTemporary) { |
| 3369 | Step S; |
| 3370 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 3371 | S.Type = T; |
| 3372 | Steps.push_back(S); |
| 3373 | } |
| 3374 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 3375 | void InitializationSequence::AddFinalCopy(QualType T) { |
| 3376 | Step S; |
| 3377 | S.Kind = SK_FinalCopy; |
| 3378 | S.Type = T; |
| 3379 | Steps.push_back(S); |
| 3380 | } |
| 3381 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3382 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 3383 | Step S; |
| 3384 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 3385 | S.Type = T; |
| 3386 | Steps.push_back(S); |
| 3387 | } |
| 3388 | |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3389 | void |
| 3390 | InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
| 3391 | DeclAccessPair FoundDecl, |
| 3392 | QualType T, |
| 3393 | bool HadMultipleCandidates) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3394 | Step S; |
| 3395 | S.Kind = SK_UserConversion; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3396 | S.Type = T; |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3397 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3398 | S.Function.Function = Function; |
| 3399 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3400 | Steps.push_back(S); |
| 3401 | } |
| 3402 | |
| 3403 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3404 | ExprValueKind VK) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3405 | Step S; |
John McCall | 7a1da89 | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 3406 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3407 | switch (VK) { |
| 3408 | case VK_RValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3409 | S.Kind = SK_QualificationConversionRValue; |
| 3410 | break; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3411 | case VK_XValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3412 | S.Kind = SK_QualificationConversionXValue; |
| 3413 | break; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3414 | case VK_LValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3415 | S.Kind = SK_QualificationConversionLValue; |
| 3416 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3417 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3418 | S.Type = Ty; |
| 3419 | Steps.push_back(S); |
| 3420 | } |
| 3421 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 3422 | void InitializationSequence::AddAtomicConversionStep(QualType Ty) { |
| 3423 | Step S; |
| 3424 | S.Kind = SK_AtomicConversion; |
| 3425 | S.Type = Ty; |
| 3426 | Steps.push_back(S); |
| 3427 | } |
| 3428 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3429 | void InitializationSequence::AddLValueToRValueStep(QualType Ty) { |
| 3430 | assert(!Ty.hasQualifiers() && "rvalues may not have qualifiers"); |
| 3431 | |
| 3432 | Step S; |
| 3433 | S.Kind = SK_LValueToRValue; |
| 3434 | S.Type = Ty; |
| 3435 | Steps.push_back(S); |
| 3436 | } |
| 3437 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3438 | void InitializationSequence::AddConversionSequenceStep( |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 3439 | const ImplicitConversionSequence &ICS, QualType T, |
| 3440 | bool TopLevelOfInitList) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3441 | Step S; |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 3442 | S.Kind = TopLevelOfInitList ? SK_ConversionSequenceNoNarrowing |
| 3443 | : SK_ConversionSequence; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3444 | S.Type = T; |
| 3445 | S.ICS = new ImplicitConversionSequence(ICS); |
| 3446 | Steps.push_back(S); |
| 3447 | } |
| 3448 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3449 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 3450 | Step S; |
| 3451 | S.Kind = SK_ListInitialization; |
| 3452 | S.Type = T; |
| 3453 | Steps.push_back(S); |
| 3454 | } |
| 3455 | |
Richard Smith | 55c2888 | 2016-05-12 23:45:49 +0000 | [diff] [blame] | 3456 | void InitializationSequence::AddConstructorInitializationStep( |
| 3457 | DeclAccessPair FoundDecl, CXXConstructorDecl *Constructor, QualType T, |
| 3458 | bool HadMultipleCandidates, bool FromInitList, bool AsInitList) { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3459 | Step S; |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3460 | S.Kind = FromInitList ? AsInitList ? SK_StdInitializerListConstructorCall |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 3461 | : SK_ConstructorInitializationFromList |
| 3462 | : SK_ConstructorInitialization; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3463 | S.Type = T; |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3464 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3465 | S.Function.Function = Constructor; |
Richard Smith | 55c2888 | 2016-05-12 23:45:49 +0000 | [diff] [blame] | 3466 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3467 | Steps.push_back(S); |
| 3468 | } |
| 3469 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3470 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 3471 | Step S; |
| 3472 | S.Kind = SK_ZeroInitialization; |
| 3473 | S.Type = T; |
| 3474 | Steps.push_back(S); |
| 3475 | } |
| 3476 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3477 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 3478 | Step S; |
| 3479 | S.Kind = SK_CAssignment; |
| 3480 | S.Type = T; |
| 3481 | Steps.push_back(S); |
| 3482 | } |
| 3483 | |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3484 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 3485 | Step S; |
| 3486 | S.Kind = SK_StringInit; |
| 3487 | S.Type = T; |
| 3488 | Steps.push_back(S); |
| 3489 | } |
| 3490 | |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3491 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 3492 | Step S; |
| 3493 | S.Kind = SK_ObjCObjectConversion; |
| 3494 | S.Type = T; |
| 3495 | Steps.push_back(S); |
| 3496 | } |
| 3497 | |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 3498 | void InitializationSequence::AddArrayInitStep(QualType T, bool IsGNUExtension) { |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3499 | Step S; |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 3500 | S.Kind = IsGNUExtension ? SK_GNUArrayInit : SK_ArrayInit; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3501 | S.Type = T; |
| 3502 | Steps.push_back(S); |
| 3503 | } |
| 3504 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3505 | void InitializationSequence::AddArrayInitLoopStep(QualType T, QualType EltT) { |
| 3506 | Step S; |
| 3507 | S.Kind = SK_ArrayLoopIndex; |
| 3508 | S.Type = EltT; |
| 3509 | Steps.insert(Steps.begin(), S); |
| 3510 | |
| 3511 | S.Kind = SK_ArrayLoopInit; |
| 3512 | S.Type = T; |
| 3513 | Steps.push_back(S); |
| 3514 | } |
| 3515 | |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 3516 | void InitializationSequence::AddParenthesizedArrayInitStep(QualType T) { |
| 3517 | Step S; |
| 3518 | S.Kind = SK_ParenthesizedArrayInit; |
| 3519 | S.Type = T; |
| 3520 | Steps.push_back(S); |
| 3521 | } |
| 3522 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3523 | void InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type, |
| 3524 | bool shouldCopy) { |
| 3525 | Step s; |
| 3526 | s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore |
| 3527 | : SK_PassByIndirectRestore); |
| 3528 | s.Type = type; |
| 3529 | Steps.push_back(s); |
| 3530 | } |
| 3531 | |
| 3532 | void InitializationSequence::AddProduceObjCObjectStep(QualType T) { |
| 3533 | Step S; |
| 3534 | S.Kind = SK_ProduceObjCObject; |
| 3535 | S.Type = T; |
| 3536 | Steps.push_back(S); |
| 3537 | } |
| 3538 | |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 3539 | void InitializationSequence::AddStdInitializerListConstructionStep(QualType T) { |
| 3540 | Step S; |
| 3541 | S.Kind = SK_StdInitializerList; |
| 3542 | S.Type = T; |
| 3543 | Steps.push_back(S); |
| 3544 | } |
| 3545 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 3546 | void InitializationSequence::AddOCLSamplerInitStep(QualType T) { |
| 3547 | Step S; |
| 3548 | S.Kind = SK_OCLSamplerInit; |
| 3549 | S.Type = T; |
| 3550 | Steps.push_back(S); |
| 3551 | } |
| 3552 | |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 3553 | void InitializationSequence::AddOCLZeroOpaqueTypeStep(QualType T) { |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 3554 | Step S; |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 3555 | S.Kind = SK_OCLZeroOpaqueType; |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 3556 | S.Type = T; |
| 3557 | Steps.push_back(S); |
| 3558 | } |
| 3559 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3560 | void InitializationSequence::RewrapReferenceInitList(QualType T, |
| 3561 | InitListExpr *Syntactic) { |
| 3562 | assert(Syntactic->getNumInits() == 1 && |
| 3563 | "Can only rewrap trivial init lists."); |
| 3564 | Step S; |
| 3565 | S.Kind = SK_UnwrapInitList; |
| 3566 | S.Type = Syntactic->getInit(0)->getType(); |
| 3567 | Steps.insert(Steps.begin(), S); |
| 3568 | |
| 3569 | S.Kind = SK_RewrapInitList; |
| 3570 | S.Type = T; |
| 3571 | S.WrappingSyntacticList = Syntactic; |
| 3572 | Steps.push_back(S); |
| 3573 | } |
| 3574 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3575 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3576 | OverloadingResult Result) { |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 3577 | setSequenceKind(FailedSequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3578 | this->Failure = Failure; |
| 3579 | this->FailedOverloadResult = Result; |
| 3580 | } |
| 3581 | |
| 3582 | //===----------------------------------------------------------------------===// |
| 3583 | // Attempt initialization |
| 3584 | //===----------------------------------------------------------------------===// |
| 3585 | |
Nico Weber | 337d5aa | 2015-04-17 08:32:38 +0000 | [diff] [blame] | 3586 | /// Tries to add a zero initializer. Returns true if that worked. |
| 3587 | static bool |
| 3588 | maybeRecoverWithZeroInitialization(Sema &S, InitializationSequence &Sequence, |
| 3589 | const InitializedEntity &Entity) { |
| 3590 | if (Entity.getKind() != InitializedEntity::EK_Variable) |
| 3591 | return false; |
| 3592 | |
| 3593 | VarDecl *VD = cast<VarDecl>(Entity.getDecl()); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 3594 | if (VD->getInit() || VD->getEndLoc().isMacroID()) |
Nico Weber | 337d5aa | 2015-04-17 08:32:38 +0000 | [diff] [blame] | 3595 | return false; |
| 3596 | |
| 3597 | QualType VariableTy = VD->getType().getCanonicalType(); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 3598 | SourceLocation Loc = S.getLocForEndOfToken(VD->getEndLoc()); |
Nico Weber | 337d5aa | 2015-04-17 08:32:38 +0000 | [diff] [blame] | 3599 | std::string Init = S.getFixItZeroInitializerForType(VariableTy, Loc); |
| 3600 | if (!Init.empty()) { |
| 3601 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3602 | Sequence.SetZeroInitializationFixit(Init, Loc); |
| 3603 | return true; |
| 3604 | } |
| 3605 | return false; |
| 3606 | } |
| 3607 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3608 | static void MaybeProduceObjCObject(Sema &S, |
| 3609 | InitializationSequence &Sequence, |
| 3610 | const InitializedEntity &Entity) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3611 | if (!S.getLangOpts().ObjCAutoRefCount) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3612 | |
| 3613 | /// When initializing a parameter, produce the value if it's marked |
| 3614 | /// __attribute__((ns_consumed)). |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3615 | if (Entity.isParameterKind()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3616 | if (!Entity.isParameterConsumed()) |
| 3617 | return; |
| 3618 | |
| 3619 | assert(Entity.getType()->isObjCRetainableType() && |
| 3620 | "consuming an object of unretainable type?"); |
| 3621 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 3622 | |
| 3623 | /// When initializing a return value, if the return type is a |
| 3624 | /// retainable type, then returns need to immediately retain the |
| 3625 | /// object. If an autorelease is required, it will be done at the |
| 3626 | /// last instant. |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 3627 | } else if (Entity.getKind() == InitializedEntity::EK_Result || |
| 3628 | Entity.getKind() == InitializedEntity::EK_StmtExprResult) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3629 | if (!Entity.getType()->isObjCRetainableType()) |
| 3630 | return; |
| 3631 | |
| 3632 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 3633 | } |
| 3634 | } |
| 3635 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3636 | static void TryListInitialization(Sema &S, |
| 3637 | const InitializedEntity &Entity, |
| 3638 | const InitializationKind &Kind, |
| 3639 | InitListExpr *InitList, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 3640 | InitializationSequence &Sequence, |
| 3641 | bool TreatUnavailableAsInvalid); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3642 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3643 | /// When initializing from init list via constructor, handle |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3644 | /// initialization of an object of type std::initializer_list<T>. |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3645 | /// |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3646 | /// \return true if we have handled initialization of an object of type |
| 3647 | /// std::initializer_list<T>, false otherwise. |
| 3648 | static bool TryInitializerListConstruction(Sema &S, |
| 3649 | InitListExpr *List, |
| 3650 | QualType DestType, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 3651 | InitializationSequence &Sequence, |
| 3652 | bool TreatUnavailableAsInvalid) { |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3653 | QualType E; |
| 3654 | if (!S.isStdInitializerList(DestType, &E)) |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3655 | return false; |
| 3656 | |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3657 | if (!S.isCompleteType(List->getExprLoc(), E)) { |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3658 | Sequence.setIncompleteTypeFailure(E); |
| 3659 | return true; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3660 | } |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3661 | |
| 3662 | // Try initializing a temporary array from the init list. |
| 3663 | QualType ArrayType = S.Context.getConstantArrayType( |
| 3664 | E.withConst(), llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 3665 | List->getNumInits()), |
| 3666 | clang::ArrayType::Normal, 0); |
| 3667 | InitializedEntity HiddenArray = |
| 3668 | InitializedEntity::InitializeTemporary(ArrayType); |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 3669 | InitializationKind Kind = InitializationKind::CreateDirectList( |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 3670 | List->getExprLoc(), List->getBeginLoc(), List->getEndLoc()); |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 3671 | TryListInitialization(S, HiddenArray, Kind, List, Sequence, |
| 3672 | TreatUnavailableAsInvalid); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3673 | if (Sequence) |
| 3674 | Sequence.AddStdInitializerListConstructionStep(DestType); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3675 | return true; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3676 | } |
| 3677 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3678 | /// Determine if the constructor has the signature of a copy or move |
| 3679 | /// constructor for the type T of the class in which it was found. That is, |
| 3680 | /// determine if its first parameter is of type T or reference to (possibly |
| 3681 | /// cv-qualified) T. |
| 3682 | static bool hasCopyOrMoveCtorParam(ASTContext &Ctx, |
| 3683 | const ConstructorInfo &Info) { |
| 3684 | if (Info.Constructor->getNumParams() == 0) |
| 3685 | return false; |
| 3686 | |
| 3687 | QualType ParmT = |
| 3688 | Info.Constructor->getParamDecl(0)->getType().getNonReferenceType(); |
| 3689 | QualType ClassT = |
| 3690 | Ctx.getRecordType(cast<CXXRecordDecl>(Info.FoundDecl->getDeclContext())); |
| 3691 | |
| 3692 | return Ctx.hasSameUnqualifiedType(ParmT, ClassT); |
| 3693 | } |
| 3694 | |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3695 | static OverloadingResult |
| 3696 | ResolveConstructorOverload(Sema &S, SourceLocation DeclLoc, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3697 | MultiExprArg Args, |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3698 | OverloadCandidateSet &CandidateSet, |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3699 | QualType DestType, |
Richard Smith | 40c7806 | 2015-02-21 02:31:57 +0000 | [diff] [blame] | 3700 | DeclContext::lookup_result Ctors, |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3701 | OverloadCandidateSet::iterator &Best, |
| 3702 | bool CopyInitializing, bool AllowExplicit, |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3703 | bool OnlyListConstructors, bool IsListInit, |
| 3704 | bool SecondStepOfCopyInit = false) { |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3705 | CandidateSet.clear(OverloadCandidateSet::CSK_InitByConstructor); |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3706 | |
Richard Smith | 40c7806 | 2015-02-21 02:31:57 +0000 | [diff] [blame] | 3707 | for (NamedDecl *D : Ctors) { |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3708 | auto Info = getConstructorInfo(D); |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3709 | if (!Info.Constructor || Info.Constructor->isInvalidDecl()) |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3710 | continue; |
| 3711 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3712 | if (!AllowExplicit && Info.Constructor->isExplicit()) |
| 3713 | continue; |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3714 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3715 | if (OnlyListConstructors && !S.isInitListConstructor(Info.Constructor)) |
| 3716 | continue; |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3717 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3718 | // C++11 [over.best.ics]p4: |
| 3719 | // ... and the constructor or user-defined conversion function is a |
| 3720 | // candidate by |
| 3721 | // - 13.3.1.3, when the argument is the temporary in the second step |
| 3722 | // of a class copy-initialization, or |
| 3723 | // - 13.3.1.4, 13.3.1.5, or 13.3.1.6 (in all cases), [not handled here] |
| 3724 | // - the second phase of 13.3.1.7 when the initializer list has exactly |
| 3725 | // one element that is itself an initializer list, and the target is |
| 3726 | // the first parameter of a constructor of class X, and the conversion |
| 3727 | // is to X or reference to (possibly cv-qualified X), |
| 3728 | // user-defined conversion sequences are not considered. |
| 3729 | bool SuppressUserConversions = |
| 3730 | SecondStepOfCopyInit || |
| 3731 | (IsListInit && Args.size() == 1 && isa<InitListExpr>(Args[0]) && |
| 3732 | hasCopyOrMoveCtorParam(S.Context, Info)); |
| 3733 | |
| 3734 | if (Info.ConstructorTmpl) |
| 3735 | S.AddTemplateOverloadCandidate(Info.ConstructorTmpl, Info.FoundDecl, |
| 3736 | /*ExplicitArgs*/ nullptr, Args, |
| 3737 | CandidateSet, SuppressUserConversions); |
| 3738 | else { |
| 3739 | // C++ [over.match.copy]p1: |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3740 | // - When initializing a temporary to be bound to the first parameter |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3741 | // of a constructor [for type T] that takes a reference to possibly |
| 3742 | // cv-qualified T as its first argument, called with a single |
| 3743 | // argument in the context of direct-initialization, explicit |
| 3744 | // conversion functions are also considered. |
| 3745 | // FIXME: What if a constructor template instantiates to such a signature? |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3746 | bool AllowExplicitConv = AllowExplicit && !CopyInitializing && |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3747 | Args.size() == 1 && |
| 3748 | hasCopyOrMoveCtorParam(S.Context, Info); |
| 3749 | S.AddOverloadCandidate(Info.Constructor, Info.FoundDecl, Args, |
| 3750 | CandidateSet, SuppressUserConversions, |
| 3751 | /*PartialOverloading=*/false, |
| 3752 | /*AllowExplicit=*/AllowExplicitConv); |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3753 | } |
| 3754 | } |
| 3755 | |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3756 | // FIXME: Work around a bug in C++17 guaranteed copy elision. |
| 3757 | // |
| 3758 | // When initializing an object of class type T by constructor |
| 3759 | // ([over.match.ctor]) or by list-initialization ([over.match.list]) |
| 3760 | // from a single expression of class type U, conversion functions of |
| 3761 | // U that convert to the non-reference type cv T are candidates. |
| 3762 | // Explicit conversion functions are only candidates during |
| 3763 | // direct-initialization. |
| 3764 | // |
| 3765 | // Note: SecondStepOfCopyInit is only ever true in this case when |
| 3766 | // evaluating whether to produce a C++98 compatibility warning. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 3767 | if (S.getLangOpts().CPlusPlus17 && Args.size() == 1 && |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3768 | !SecondStepOfCopyInit) { |
| 3769 | Expr *Initializer = Args[0]; |
| 3770 | auto *SourceRD = Initializer->getType()->getAsCXXRecordDecl(); |
| 3771 | if (SourceRD && S.isCompleteType(DeclLoc, Initializer->getType())) { |
| 3772 | const auto &Conversions = SourceRD->getVisibleConversionFunctions(); |
| 3773 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
| 3774 | NamedDecl *D = *I; |
| 3775 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3776 | D = D->getUnderlyingDecl(); |
| 3777 | |
| 3778 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3779 | CXXConversionDecl *Conv; |
| 3780 | if (ConvTemplate) |
| 3781 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3782 | else |
| 3783 | Conv = cast<CXXConversionDecl>(D); |
| 3784 | |
| 3785 | if ((AllowExplicit && !CopyInitializing) || !Conv->isExplicit()) { |
| 3786 | if (ConvTemplate) |
| 3787 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
| 3788 | ActingDC, Initializer, DestType, |
| 3789 | CandidateSet, AllowExplicit, |
| 3790 | /*AllowResultConversion*/false); |
| 3791 | else |
| 3792 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Initializer, |
| 3793 | DestType, CandidateSet, AllowExplicit, |
| 3794 | /*AllowResultConversion*/false); |
| 3795 | } |
| 3796 | } |
| 3797 | } |
| 3798 | } |
| 3799 | |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3800 | // Perform overload resolution and return the result. |
| 3801 | return CandidateSet.BestViableFunction(S, DeclLoc, Best); |
| 3802 | } |
| 3803 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3804 | /// Attempt initialization by constructor (C++ [dcl.init]), which |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3805 | /// enumerates the constructors of the initialized entity and performs overload |
| 3806 | /// resolution to select the best. |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3807 | /// \param DestType The destination class type. |
| 3808 | /// \param DestArrayType The destination type, which is either DestType or |
| 3809 | /// a (possibly multidimensional) array of DestType. |
NAKAMURA Takumi | ffcc98a | 2015-02-05 23:12:13 +0000 | [diff] [blame] | 3810 | /// \param IsListInit Is this list-initialization? |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3811 | /// \param IsInitListCopy Is this non-list-initialization resulting from a |
| 3812 | /// list-initialization from {x} where x is the same |
| 3813 | /// type as the entity? |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3814 | static void TryConstructorInitialization(Sema &S, |
| 3815 | const InitializedEntity &Entity, |
| 3816 | const InitializationKind &Kind, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3817 | MultiExprArg Args, QualType DestType, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3818 | QualType DestArrayType, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3819 | InitializationSequence &Sequence, |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3820 | bool IsListInit = false, |
| 3821 | bool IsInitListCopy = false) { |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3822 | assert(((!IsListInit && !IsInitListCopy) || |
| 3823 | (Args.size() == 1 && isa<InitListExpr>(Args[0]))) && |
| 3824 | "IsListInit/IsInitListCopy must come with a single initializer list " |
| 3825 | "argument."); |
| 3826 | InitListExpr *ILE = |
| 3827 | (IsListInit || IsInitListCopy) ? cast<InitListExpr>(Args[0]) : nullptr; |
| 3828 | MultiExprArg UnwrappedArgs = |
| 3829 | ILE ? MultiExprArg(ILE->getInits(), ILE->getNumInits()) : Args; |
Sebastian Redl | 88e4d49 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3830 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3831 | // The type we're constructing needs to be complete. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3832 | if (!S.isCompleteType(Kind.getLocation(), DestType)) { |
Douglas Gregor | 85f3423 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 3833 | Sequence.setIncompleteTypeFailure(DestType); |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3834 | return; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3835 | } |
| 3836 | |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 3837 | // C++17 [dcl.init]p17: |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3838 | // - If the initializer expression is a prvalue and the cv-unqualified |
| 3839 | // version of the source type is the same class as the class of the |
| 3840 | // destination, the initializer expression is used to initialize the |
| 3841 | // destination object. |
| 3842 | // Per DR (no number yet), this does not apply when initializing a base |
| 3843 | // class or delegating to another constructor from a mem-initializer. |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3844 | // ObjC++: Lambda captured by the block in the lambda to block conversion |
| 3845 | // should avoid copy elision. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 3846 | if (S.getLangOpts().CPlusPlus17 && |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3847 | Entity.getKind() != InitializedEntity::EK_Base && |
| 3848 | Entity.getKind() != InitializedEntity::EK_Delegating && |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3849 | Entity.getKind() != |
| 3850 | InitializedEntity::EK_LambdaToBlockConversionBlockElement && |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3851 | UnwrappedArgs.size() == 1 && UnwrappedArgs[0]->isRValue() && |
| 3852 | S.Context.hasSameUnqualifiedType(UnwrappedArgs[0]->getType(), DestType)) { |
| 3853 | // Convert qualifications if necessary. |
Richard Smith | 16d3150 | 2016-12-21 01:31:56 +0000 | [diff] [blame] | 3854 | Sequence.AddQualificationConversionStep(DestType, VK_RValue); |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3855 | if (ILE) |
| 3856 | Sequence.RewrapReferenceInitList(DestType, ILE); |
| 3857 | return; |
| 3858 | } |
| 3859 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3860 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
| 3861 | assert(DestRecordType && "Constructor initialization requires record type"); |
| 3862 | CXXRecordDecl *DestRecordDecl |
| 3863 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
| 3864 | |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3865 | // Build the candidate set directly in the initialization sequence |
| 3866 | // structure, so that it will persist if we fail. |
| 3867 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3868 | |
| 3869 | // Determine whether we are allowed to call explicit constructors or |
| 3870 | // explicit conversion operators. |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3871 | bool AllowExplicit = Kind.AllowExplicit() || IsListInit; |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3872 | bool CopyInitialization = Kind.getKind() == InitializationKind::IK_Copy; |
Sebastian Redl | 88e4d49 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3873 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3874 | // - Otherwise, if T is a class type, constructors are considered. The |
| 3875 | // applicable constructors are enumerated, and the best one is chosen |
| 3876 | // through overload resolution. |
Richard Smith | 40c7806 | 2015-02-21 02:31:57 +0000 | [diff] [blame] | 3877 | DeclContext::lookup_result Ctors = S.LookupConstructors(DestRecordDecl); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3878 | |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3879 | OverloadingResult Result = OR_No_Viable_Function; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3880 | OverloadCandidateSet::iterator Best; |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3881 | bool AsInitializerList = false; |
| 3882 | |
Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 3883 | // C++11 [over.match.list]p1, per DR1467: |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 3884 | // When objects of non-aggregate type T are list-initialized, such that |
| 3885 | // 8.5.4 [dcl.init.list] specifies that overload resolution is performed |
| 3886 | // according to the rules in this section, overload resolution selects |
| 3887 | // the constructor in two phases: |
| 3888 | // |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3889 | // - Initially, the candidate functions are the initializer-list |
| 3890 | // constructors of the class T and the argument list consists of the |
| 3891 | // initializer list as a single argument. |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3892 | if (IsListInit) { |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3893 | AsInitializerList = true; |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3894 | |
| 3895 | // If the initializer list has no elements and T has a default constructor, |
| 3896 | // the first phase is omitted. |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3897 | if (!(UnwrappedArgs.empty() && DestRecordDecl->hasDefaultConstructor())) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3898 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3899 | CandidateSet, DestType, Ctors, Best, |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3900 | CopyInitialization, AllowExplicit, |
Larisse Voufo | bcf327a | 2015-02-10 02:20:14 +0000 | [diff] [blame] | 3901 | /*OnlyListConstructor=*/true, |
| 3902 | IsListInit); |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3903 | } |
| 3904 | |
| 3905 | // C++11 [over.match.list]p1: |
| 3906 | // - If no viable initializer-list constructor is found, overload resolution |
| 3907 | // is performed again, where the candidate functions are all the |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3908 | // constructors of the class T and the argument list consists of the |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3909 | // elements of the initializer list. |
| 3910 | if (Result == OR_No_Viable_Function) { |
| 3911 | AsInitializerList = false; |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3912 | Result = ResolveConstructorOverload(S, Kind.getLocation(), UnwrappedArgs, |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3913 | CandidateSet, DestType, Ctors, Best, |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3914 | CopyInitialization, AllowExplicit, |
Larisse Voufo | bcf327a | 2015-02-10 02:20:14 +0000 | [diff] [blame] | 3915 | /*OnlyListConstructors=*/false, |
| 3916 | IsListInit); |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3917 | } |
| 3918 | if (Result) { |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3919 | Sequence.SetOverloadFailure(IsListInit ? |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3920 | InitializationSequence::FK_ListConstructorOverloadFailed : |
| 3921 | InitializationSequence::FK_ConstructorOverloadFailed, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3922 | Result); |
| 3923 | return; |
| 3924 | } |
| 3925 | |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3926 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 3927 | |
| 3928 | // In C++17, ResolveConstructorOverload can select a conversion function |
| 3929 | // instead of a constructor. |
| 3930 | if (auto *CD = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 3931 | // Add the user-defined conversion step that calls the conversion function. |
| 3932 | QualType ConvType = CD->getConversionType(); |
| 3933 | assert(S.Context.hasSameUnqualifiedType(ConvType, DestType) && |
| 3934 | "should not have selected this conversion function"); |
| 3935 | Sequence.AddUserConversionStep(CD, Best->FoundDecl, ConvType, |
| 3936 | HadMultipleCandidates); |
| 3937 | if (!S.Context.hasSameType(ConvType, DestType)) |
| 3938 | Sequence.AddQualificationConversionStep(DestType, VK_RValue); |
| 3939 | if (IsListInit) |
| 3940 | Sequence.RewrapReferenceInitList(Entity.getType(), ILE); |
| 3941 | return; |
| 3942 | } |
| 3943 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3944 | // C++11 [dcl.init]p6: |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3945 | // If a program calls for the default initialization of an object |
| 3946 | // of a const-qualified type T, T shall be a class type with a |
| 3947 | // user-provided default constructor. |
Nico Weber | 6a6376b | 2016-02-19 01:52:46 +0000 | [diff] [blame] | 3948 | // C++ core issue 253 proposal: |
| 3949 | // If the implicit default constructor initializes all subobjects, no |
| 3950 | // initializer should be required. |
| 3951 | // The 253 proposal is for example needed to process libstdc++ headers in 5.x. |
| 3952 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3953 | if (Kind.getKind() == InitializationKind::IK_Default && |
Nico Weber | 6a6376b | 2016-02-19 01:52:46 +0000 | [diff] [blame] | 3954 | Entity.getType().isConstQualified()) { |
| 3955 | if (!CtorDecl->getParent()->allowConstDefaultInit()) { |
| 3956 | if (!maybeRecoverWithZeroInitialization(S, Sequence, Entity)) |
| 3957 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 3958 | return; |
| 3959 | } |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3960 | } |
| 3961 | |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3962 | // C++11 [over.match.list]p1: |
| 3963 | // In copy-list-initialization, if an explicit constructor is chosen, the |
| 3964 | // initializer is ill-formed. |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3965 | if (IsListInit && !Kind.AllowExplicit() && CtorDecl->isExplicit()) { |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3966 | Sequence.SetFailed(InitializationSequence::FK_ExplicitConstructor); |
| 3967 | return; |
| 3968 | } |
| 3969 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3970 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 3971 | // subsumed by the initialization. |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3972 | Sequence.AddConstructorInitializationStep( |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3973 | Best->FoundDecl, CtorDecl, DestArrayType, HadMultipleCandidates, |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3974 | IsListInit | IsInitListCopy, AsInitializerList); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3975 | } |
| 3976 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3977 | static bool |
| 3978 | ResolveOverloadedFunctionForReferenceBinding(Sema &S, |
| 3979 | Expr *Initializer, |
| 3980 | QualType &SourceType, |
| 3981 | QualType &UnqualifiedSourceType, |
| 3982 | QualType UnqualifiedTargetType, |
| 3983 | InitializationSequence &Sequence) { |
| 3984 | if (S.Context.getCanonicalType(UnqualifiedSourceType) == |
| 3985 | S.Context.OverloadTy) { |
| 3986 | DeclAccessPair Found; |
| 3987 | bool HadMultipleCandidates = false; |
| 3988 | if (FunctionDecl *Fn |
| 3989 | = S.ResolveAddressOfOverloadedFunction(Initializer, |
| 3990 | UnqualifiedTargetType, |
| 3991 | false, Found, |
| 3992 | &HadMultipleCandidates)) { |
| 3993 | Sequence.AddAddressOverloadResolutionStep(Fn, Found, |
| 3994 | HadMultipleCandidates); |
| 3995 | SourceType = Fn->getType(); |
| 3996 | UnqualifiedSourceType = SourceType.getUnqualifiedType(); |
| 3997 | } else if (!UnqualifiedTargetType->isRecordType()) { |
| 3998 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3999 | return true; |
| 4000 | } |
| 4001 | } |
| 4002 | return false; |
| 4003 | } |
| 4004 | |
| 4005 | static void TryReferenceInitializationCore(Sema &S, |
| 4006 | const InitializedEntity &Entity, |
| 4007 | const InitializationKind &Kind, |
| 4008 | Expr *Initializer, |
| 4009 | QualType cv1T1, QualType T1, |
| 4010 | Qualifiers T1Quals, |
| 4011 | QualType cv2T2, QualType T2, |
| 4012 | Qualifiers T2Quals, |
| 4013 | InitializationSequence &Sequence); |
| 4014 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4015 | static void TryValueInitialization(Sema &S, |
| 4016 | const InitializedEntity &Entity, |
| 4017 | const InitializationKind &Kind, |
| 4018 | InitializationSequence &Sequence, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4019 | InitListExpr *InitList = nullptr); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4020 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4021 | /// Attempt list initialization of a reference. |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4022 | static void TryReferenceListInitialization(Sema &S, |
| 4023 | const InitializedEntity &Entity, |
| 4024 | const InitializationKind &Kind, |
| 4025 | InitListExpr *InitList, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4026 | InitializationSequence &Sequence, |
| 4027 | bool TreatUnavailableAsInvalid) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4028 | // First, catch C++03 where this isn't possible. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4029 | if (!S.getLangOpts().CPlusPlus11) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4030 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 4031 | return; |
| 4032 | } |
David Majnemer | 9370dc2 | 2015-04-26 07:35:03 +0000 | [diff] [blame] | 4033 | // Can't reference initialize a compound literal. |
| 4034 | if (Entity.getKind() == InitializedEntity::EK_CompoundLiteralInit) { |
| 4035 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 4036 | return; |
| 4037 | } |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4038 | |
| 4039 | QualType DestType = Entity.getType(); |
| 4040 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 4041 | Qualifiers T1Quals; |
| 4042 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
| 4043 | |
| 4044 | // Reference initialization via an initializer list works thus: |
| 4045 | // If the initializer list consists of a single element that is |
| 4046 | // reference-related to the referenced type, bind directly to that element |
| 4047 | // (possibly creating temporaries). |
| 4048 | // Otherwise, initialize a temporary with the initializer list and |
| 4049 | // bind to that. |
| 4050 | if (InitList->getNumInits() == 1) { |
| 4051 | Expr *Initializer = InitList->getInit(0); |
| 4052 | QualType cv2T2 = Initializer->getType(); |
| 4053 | Qualifiers T2Quals; |
| 4054 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
| 4055 | |
| 4056 | // If this fails, creating a temporary wouldn't work either. |
| 4057 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 4058 | T1, Sequence)) |
| 4059 | return; |
| 4060 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4061 | SourceLocation DeclLoc = Initializer->getBeginLoc(); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4062 | bool dummy1, dummy2, dummy3; |
| 4063 | Sema::ReferenceCompareResult RefRelationship |
| 4064 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, dummy1, |
| 4065 | dummy2, dummy3); |
| 4066 | if (RefRelationship >= Sema::Ref_Related) { |
| 4067 | // Try to bind the reference here. |
| 4068 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 4069 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 4070 | if (Sequence) |
| 4071 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
| 4072 | return; |
| 4073 | } |
Richard Smith | 03d9393 | 2013-01-15 07:58:29 +0000 | [diff] [blame] | 4074 | |
| 4075 | // Update the initializer if we've resolved an overloaded function. |
| 4076 | if (Sequence.step_begin() != Sequence.step_end()) |
| 4077 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4078 | } |
| 4079 | |
| 4080 | // Not reference-related. Create a temporary and bind to that. |
| 4081 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 4082 | |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4083 | TryListInitialization(S, TempEntity, Kind, InitList, Sequence, |
| 4084 | TreatUnavailableAsInvalid); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4085 | if (Sequence) { |
| 4086 | if (DestType->isRValueReferenceType() || |
| 4087 | (T1Quals.hasConst() && !T1Quals.hasVolatile())) |
| 4088 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 4089 | else |
| 4090 | Sequence.SetFailed( |
| 4091 | InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
| 4092 | } |
| 4093 | } |
| 4094 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4095 | /// Attempt list initialization (C++0x [dcl.init.list]) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4096 | static void TryListInitialization(Sema &S, |
| 4097 | const InitializedEntity &Entity, |
| 4098 | const InitializationKind &Kind, |
| 4099 | InitListExpr *InitList, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4100 | InitializationSequence &Sequence, |
| 4101 | bool TreatUnavailableAsInvalid) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4102 | QualType DestType = Entity.getType(); |
| 4103 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4104 | // C++ doesn't allow scalar initialization with more than one argument. |
| 4105 | // But C99 complex numbers are scalars and it makes sense there. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4106 | if (S.getLangOpts().CPlusPlus && DestType->isScalarType() && |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4107 | !DestType->isAnyComplexType() && InitList->getNumInits() > 1) { |
| 4108 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 4109 | return; |
| 4110 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4111 | if (DestType->isReferenceType()) { |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4112 | TryReferenceListInitialization(S, Entity, Kind, InitList, Sequence, |
| 4113 | TreatUnavailableAsInvalid); |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4114 | return; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4115 | } |
Sebastian Redl | 4f28b58 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 4116 | |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4117 | if (DestType->isRecordType() && |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4118 | !S.isCompleteType(InitList->getBeginLoc(), DestType)) { |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4119 | Sequence.setIncompleteTypeFailure(DestType); |
| 4120 | return; |
| 4121 | } |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4122 | |
Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 4123 | // C++11 [dcl.init.list]p3, per DR1467: |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4124 | // - If T is a class type and the initializer list has a single element of |
| 4125 | // type cv U, where U is T or a class derived from T, the object is |
| 4126 | // initialized from that element (by copy-initialization for |
| 4127 | // copy-list-initialization, or by direct-initialization for |
| 4128 | // direct-list-initialization). |
| 4129 | // - Otherwise, if T is a character array and the initializer list has a |
| 4130 | // single element that is an appropriately-typed string literal |
| 4131 | // (8.5.2 [dcl.init.string]), initialization is performed as described |
| 4132 | // in that section. |
Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 4133 | // - Otherwise, if T is an aggregate, [...] (continue below). |
| 4134 | if (S.getLangOpts().CPlusPlus11 && InitList->getNumInits() == 1) { |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4135 | if (DestType->isRecordType()) { |
| 4136 | QualType InitType = InitList->getInit(0)->getType(); |
| 4137 | if (S.Context.hasSameUnqualifiedType(InitType, DestType) || |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4138 | S.IsDerivedFrom(InitList->getBeginLoc(), InitType, DestType)) { |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 4139 | Expr *InitListAsExpr = InitList; |
| 4140 | TryConstructorInitialization(S, Entity, Kind, InitListAsExpr, DestType, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 4141 | DestType, Sequence, |
| 4142 | /*InitListSyntax*/false, |
| 4143 | /*IsInitListCopy*/true); |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4144 | return; |
| 4145 | } |
| 4146 | } |
| 4147 | if (const ArrayType *DestAT = S.Context.getAsArrayType(DestType)) { |
| 4148 | Expr *SubInit[1] = {InitList->getInit(0)}; |
| 4149 | if (!isa<VariableArrayType>(DestAT) && |
| 4150 | IsStringInit(SubInit[0], DestAT, S.Context) == SIF_None) { |
| 4151 | InitializationKind SubKind = |
| 4152 | Kind.getKind() == InitializationKind::IK_DirectList |
| 4153 | ? InitializationKind::CreateDirect(Kind.getLocation(), |
| 4154 | InitList->getLBraceLoc(), |
| 4155 | InitList->getRBraceLoc()) |
| 4156 | : Kind; |
| 4157 | Sequence.InitializeFrom(S, Entity, SubKind, SubInit, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4158 | /*TopLevelOfInitList*/ true, |
| 4159 | TreatUnavailableAsInvalid); |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4160 | |
| 4161 | // TryStringLiteralInitialization() (in InitializeFrom()) will fail if |
| 4162 | // the element is not an appropriately-typed string literal, in which |
| 4163 | // case we should proceed as in C++11 (below). |
| 4164 | if (Sequence) { |
| 4165 | Sequence.RewrapReferenceInitList(Entity.getType(), InitList); |
| 4166 | return; |
| 4167 | } |
| 4168 | } |
Sebastian Redl | 4f28b58 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 4169 | } |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4170 | } |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4171 | |
| 4172 | // C++11 [dcl.init.list]p3: |
| 4173 | // - If T is an aggregate, aggregate initialization is performed. |
Faisal Vali | 30622bb | 2015-12-07 02:37:44 +0000 | [diff] [blame] | 4174 | if ((DestType->isRecordType() && !DestType->isAggregateType()) || |
| 4175 | (S.getLangOpts().CPlusPlus11 && |
| 4176 | S.isStdInitializerList(DestType, nullptr))) { |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4177 | if (S.getLangOpts().CPlusPlus11) { |
| 4178 | // - Otherwise, if the initializer list has no elements and T is a |
| 4179 | // class type with a default constructor, the object is |
| 4180 | // value-initialized. |
| 4181 | if (InitList->getNumInits() == 0) { |
| 4182 | CXXRecordDecl *RD = DestType->getAsCXXRecordDecl(); |
| 4183 | if (RD->hasDefaultConstructor()) { |
| 4184 | TryValueInitialization(S, Entity, Kind, Sequence, InitList); |
| 4185 | return; |
| 4186 | } |
| 4187 | } |
| 4188 | |
| 4189 | // - Otherwise, if T is a specialization of std::initializer_list<E>, |
| 4190 | // an initializer_list object constructed [...] |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4191 | if (TryInitializerListConstruction(S, InitList, DestType, Sequence, |
| 4192 | TreatUnavailableAsInvalid)) |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4193 | return; |
| 4194 | |
| 4195 | // - Otherwise, if T is a class type, constructors are considered. |
| 4196 | Expr *InitListAsExpr = InitList; |
| 4197 | TryConstructorInitialization(S, Entity, Kind, InitListAsExpr, DestType, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 4198 | DestType, Sequence, /*InitListSyntax*/true); |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4199 | } else |
| 4200 | Sequence.SetFailed(InitializationSequence::FK_InitListBadDestinationType); |
| 4201 | return; |
| 4202 | } |
| 4203 | |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 4204 | if (S.getLangOpts().CPlusPlus && !DestType->isAggregateType() && |
Richard Smith | ed63886 | 2016-03-28 06:08:37 +0000 | [diff] [blame] | 4205 | InitList->getNumInits() == 1) { |
| 4206 | Expr *E = InitList->getInit(0); |
| 4207 | |
| 4208 | // - Otherwise, if T is an enumeration with a fixed underlying type, |
| 4209 | // the initializer-list has a single element v, and the initialization |
| 4210 | // is direct-list-initialization, the object is initialized with the |
| 4211 | // value T(v); if a narrowing conversion is required to convert v to |
| 4212 | // the underlying type of T, the program is ill-formed. |
| 4213 | auto *ET = DestType->getAs<EnumType>(); |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 4214 | if (S.getLangOpts().CPlusPlus17 && |
Richard Smith | ed63886 | 2016-03-28 06:08:37 +0000 | [diff] [blame] | 4215 | Kind.getKind() == InitializationKind::IK_DirectList && |
| 4216 | ET && ET->getDecl()->isFixed() && |
| 4217 | !S.Context.hasSameUnqualifiedType(E->getType(), DestType) && |
| 4218 | (E->getType()->isIntegralOrEnumerationType() || |
| 4219 | E->getType()->isFloatingType())) { |
| 4220 | // There are two ways that T(v) can work when T is an enumeration type. |
| 4221 | // If there is either an implicit conversion sequence from v to T or |
| 4222 | // a conversion function that can convert from v to T, then we use that. |
| 4223 | // Otherwise, if v is of integral, enumeration, or floating-point type, |
| 4224 | // it is converted to the enumeration type via its underlying type. |
| 4225 | // There is no overlap possible between these two cases (except when the |
| 4226 | // source value is already of the destination type), and the first |
| 4227 | // case is handled by the general case for single-element lists below. |
| 4228 | ImplicitConversionSequence ICS; |
| 4229 | ICS.setStandard(); |
| 4230 | ICS.Standard.setAsIdentityConversion(); |
Vedant Kumar | f4217f8 | 2017-02-16 01:20:00 +0000 | [diff] [blame] | 4231 | if (!E->isRValue()) |
| 4232 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
Richard Smith | ed63886 | 2016-03-28 06:08:37 +0000 | [diff] [blame] | 4233 | // If E is of a floating-point type, then the conversion is ill-formed |
| 4234 | // due to narrowing, but go through the motions in order to produce the |
| 4235 | // right diagnostic. |
| 4236 | ICS.Standard.Second = E->getType()->isFloatingType() |
| 4237 | ? ICK_Floating_Integral |
| 4238 | : ICK_Integral_Conversion; |
| 4239 | ICS.Standard.setFromType(E->getType()); |
| 4240 | ICS.Standard.setToType(0, E->getType()); |
| 4241 | ICS.Standard.setToType(1, DestType); |
| 4242 | ICS.Standard.setToType(2, DestType); |
| 4243 | Sequence.AddConversionSequenceStep(ICS, ICS.Standard.getToType(2), |
| 4244 | /*TopLevelOfInitList*/true); |
| 4245 | Sequence.RewrapReferenceInitList(Entity.getType(), InitList); |
| 4246 | return; |
| 4247 | } |
| 4248 | |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 4249 | // - Otherwise, if the initializer list has a single element of type E |
| 4250 | // [...references are handled above...], the object or reference is |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4251 | // initialized from that element (by copy-initialization for |
| 4252 | // copy-list-initialization, or by direct-initialization for |
| 4253 | // direct-list-initialization); if a narrowing conversion is required |
| 4254 | // to convert the element to T, the program is ill-formed. |
| 4255 | // |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 4256 | // Per core-24034, this is direct-initialization if we were performing |
| 4257 | // direct-list-initialization and copy-initialization otherwise. |
| 4258 | // We can't use InitListChecker for this, because it always performs |
| 4259 | // copy-initialization. This only matters if we might use an 'explicit' |
| 4260 | // conversion operator, so we only need to handle the cases where the source |
| 4261 | // is of record type. |
Richard Smith | ed63886 | 2016-03-28 06:08:37 +0000 | [diff] [blame] | 4262 | if (InitList->getInit(0)->getType()->isRecordType()) { |
| 4263 | InitializationKind SubKind = |
| 4264 | Kind.getKind() == InitializationKind::IK_DirectList |
| 4265 | ? InitializationKind::CreateDirect(Kind.getLocation(), |
| 4266 | InitList->getLBraceLoc(), |
| 4267 | InitList->getRBraceLoc()) |
| 4268 | : Kind; |
| 4269 | Expr *SubInit[1] = { InitList->getInit(0) }; |
| 4270 | Sequence.InitializeFrom(S, Entity, SubKind, SubInit, |
| 4271 | /*TopLevelOfInitList*/true, |
| 4272 | TreatUnavailableAsInvalid); |
| 4273 | if (Sequence) |
| 4274 | Sequence.RewrapReferenceInitList(Entity.getType(), InitList); |
| 4275 | return; |
| 4276 | } |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 4277 | } |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4278 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4279 | InitListChecker CheckInitList(S, Entity, InitList, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4280 | DestType, /*VerifyOnly=*/true, TreatUnavailableAsInvalid); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4281 | if (CheckInitList.HadError()) { |
| 4282 | Sequence.SetFailed(InitializationSequence::FK_ListInitializationFailed); |
| 4283 | return; |
| 4284 | } |
| 4285 | |
| 4286 | // Add the list initialization step with the built init list. |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4287 | Sequence.AddListInitializationStep(DestType); |
| 4288 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4289 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4290 | /// Try a reference initialization that involves calling a conversion |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4291 | /// function. |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4292 | static OverloadingResult TryRefInitWithConversionFunction( |
| 4293 | Sema &S, const InitializedEntity &Entity, const InitializationKind &Kind, |
| 4294 | Expr *Initializer, bool AllowRValues, bool IsLValueRef, |
| 4295 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4296 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4297 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 4298 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 4299 | QualType cv2T2 = Initializer->getType(); |
| 4300 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 4301 | |
| 4302 | bool DerivedToBase; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4303 | bool ObjCConversion; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4304 | bool ObjCLifetimeConversion; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4305 | assert(!S.CompareReferenceRelationship(Initializer->getBeginLoc(), T1, T2, |
| 4306 | DerivedToBase, ObjCConversion, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4307 | ObjCLifetimeConversion) && |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4308 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 8abbc65 | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 4309 | (void)DerivedToBase; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4310 | (void)ObjCConversion; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4311 | (void)ObjCLifetimeConversion; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4312 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4313 | // Build the candidate set directly in the initialization sequence |
| 4314 | // structure, so that it will persist if we fail. |
| 4315 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 4316 | CandidateSet.clear(OverloadCandidateSet::CSK_InitByUserDefinedConversion); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4317 | |
Richard Smith | b368ea8 | 2018-07-02 23:25:22 +0000 | [diff] [blame] | 4318 | // Determine whether we are allowed to call explicit conversion operators. |
| 4319 | // Note that none of [over.match.copy], [over.match.conv], nor |
| 4320 | // [over.match.ref] permit an explicit constructor to be chosen when |
| 4321 | // initializing a reference, not even for direct-initialization. |
| 4322 | bool AllowExplicitCtors = false; |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4323 | bool AllowExplicitConvs = Kind.allowExplicitConversionFunctionsInRefBinding(); |
| 4324 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4325 | const RecordType *T1RecordType = nullptr; |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 4326 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4327 | S.isCompleteType(Kind.getLocation(), T1)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4328 | // The type we're converting to is a class type. Enumerate its constructors |
| 4329 | // to see if there is a suitable conversion. |
| 4330 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 4331 | |
Richard Smith | 40c7806 | 2015-02-21 02:31:57 +0000 | [diff] [blame] | 4332 | for (NamedDecl *D : S.LookupConstructors(T1RecordDecl)) { |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4333 | auto Info = getConstructorInfo(D); |
| 4334 | if (!Info.Constructor) |
| 4335 | continue; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4336 | |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4337 | if (!Info.Constructor->isInvalidDecl() && |
Richard Smith | b368ea8 | 2018-07-02 23:25:22 +0000 | [diff] [blame] | 4338 | Info.Constructor->isConvertingConstructor(AllowExplicitCtors)) { |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4339 | if (Info.ConstructorTmpl) |
| 4340 | S.AddTemplateOverloadCandidate(Info.ConstructorTmpl, Info.FoundDecl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4341 | /*ExplicitArgs*/ nullptr, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4342 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | dfbdfbb | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 4343 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4344 | else |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4345 | S.AddOverloadCandidate(Info.Constructor, Info.FoundDecl, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4346 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | dfbdfbb | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 4347 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4348 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4349 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4350 | } |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 4351 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 4352 | return OR_No_Viable_Function; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4353 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4354 | const RecordType *T2RecordType = nullptr; |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 4355 | if ((T2RecordType = T2->getAs<RecordType>()) && |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4356 | S.isCompleteType(Kind.getLocation(), T2)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4357 | // The type we're converting from is a class type, enumerate its conversion |
| 4358 | // functions. |
| 4359 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 4360 | |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 4361 | const auto &Conversions = T2RecordDecl->getVisibleConversionFunctions(); |
| 4362 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4363 | NamedDecl *D = *I; |
| 4364 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 4365 | if (isa<UsingShadowDecl>(D)) |
| 4366 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4367 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4368 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 4369 | CXXConversionDecl *Conv; |
| 4370 | if (ConvTemplate) |
| 4371 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 4372 | else |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4373 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4374 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4375 | // If the conversion function doesn't return a reference type, |
| 4376 | // it can't be considered for this conversion unless we're allowed to |
| 4377 | // consider rvalues. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4378 | // FIXME: Do we need to make sure that we only consider conversion |
| 4379 | // candidates with reference-compatible results? That might be needed to |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4380 | // break recursion. |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4381 | if ((AllowExplicitConvs || !Conv->isExplicit()) && |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4382 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 4383 | if (ConvTemplate) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4384 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4385 | ActingDC, Initializer, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 4386 | DestType, CandidateSet, |
| 4387 | /*AllowObjCConversionOnExplicit=*/ |
| 4388 | false); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4389 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4390 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 4391 | Initializer, DestType, CandidateSet, |
| 4392 | /*AllowObjCConversionOnExplicit=*/false); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4393 | } |
| 4394 | } |
| 4395 | } |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 4396 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 4397 | return OR_No_Viable_Function; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4398 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4399 | SourceLocation DeclLoc = Initializer->getBeginLoc(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4400 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4401 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4402 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4403 | if (OverloadingResult Result |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 4404 | = CandidateSet.BestViableFunction(S, DeclLoc, Best)) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4405 | return Result; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 4406 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4407 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 4408 | // This is the overload that will be used for this initialization step if we |
| 4409 | // use this initialization. Mark it as referenced. |
| 4410 | Function->setReferenced(); |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4411 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4412 | // Compute the returned type and value kind of the conversion. |
| 4413 | QualType cv3T3; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4414 | if (isa<CXXConversionDecl>(Function)) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4415 | cv3T3 = Function->getReturnType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4416 | else |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4417 | cv3T3 = T1; |
| 4418 | |
| 4419 | ExprValueKind VK = VK_RValue; |
| 4420 | if (cv3T3->isLValueReferenceType()) |
| 4421 | VK = VK_LValue; |
| 4422 | else if (const auto *RRef = cv3T3->getAs<RValueReferenceType>()) |
| 4423 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
| 4424 | cv3T3 = cv3T3.getNonLValueExprType(S.Context); |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 4425 | |
| 4426 | // Add the user-defined conversion step. |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4427 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4428 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, cv3T3, |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4429 | HadMultipleCandidates); |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 4430 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4431 | // Determine whether we'll need to perform derived-to-base adjustments or |
| 4432 | // other conversions. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4433 | bool NewDerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4434 | bool NewObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4435 | bool NewObjCLifetimeConversion = false; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4436 | Sema::ReferenceCompareResult NewRefRelationship |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4437 | = S.CompareReferenceRelationship(DeclLoc, T1, cv3T3, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4438 | NewDerivedToBase, NewObjCConversion, |
| 4439 | NewObjCLifetimeConversion); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4440 | |
| 4441 | // Add the final conversion sequence, if necessary. |
Douglas Gregor | 1ce52ca | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 4442 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4443 | assert(!isa<CXXConstructorDecl>(Function) && |
| 4444 | "should not have conversion after constructor"); |
| 4445 | |
Douglas Gregor | 1ce52ca | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 4446 | ImplicitConversionSequence ICS; |
| 4447 | ICS.setStandard(); |
| 4448 | ICS.Standard = Best->FinalConversion; |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4449 | Sequence.AddConversionSequenceStep(ICS, ICS.Standard.getToType(2)); |
| 4450 | |
| 4451 | // Every implicit conversion results in a prvalue, except for a glvalue |
| 4452 | // derived-to-base conversion, which we handle below. |
| 4453 | cv3T3 = ICS.Standard.getToType(2); |
| 4454 | VK = VK_RValue; |
| 4455 | } |
| 4456 | |
| 4457 | // If the converted initializer is a prvalue, its type T4 is adjusted to |
| 4458 | // type "cv1 T4" and the temporary materialization conversion is applied. |
| 4459 | // |
| 4460 | // We adjust the cv-qualifications to match the reference regardless of |
| 4461 | // whether we have a prvalue so that the AST records the change. In this |
| 4462 | // case, T4 is "cv3 T3". |
| 4463 | QualType cv1T4 = S.Context.getQualifiedType(cv3T3, cv1T1.getQualifiers()); |
| 4464 | if (cv1T4.getQualifiers() != cv3T3.getQualifiers()) |
| 4465 | Sequence.AddQualificationConversionStep(cv1T4, VK); |
| 4466 | Sequence.AddReferenceBindingStep(cv1T4, VK == VK_RValue); |
| 4467 | VK = IsLValueRef ? VK_LValue : VK_XValue; |
| 4468 | |
| 4469 | if (NewDerivedToBase) |
| 4470 | Sequence.AddDerivedToBaseCastStep(cv1T1, VK); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4471 | else if (NewObjCConversion) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4472 | Sequence.AddObjCObjectConversionStep(cv1T1); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4473 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4474 | return OR_Success; |
| 4475 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4476 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4477 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 4478 | const InitializedEntity &Entity, |
| 4479 | Expr *CurInitExpr); |
| 4480 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4481 | /// Attempt reference initialization (C++0x [dcl.init.ref]) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4482 | static void TryReferenceInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4483 | const InitializedEntity &Entity, |
| 4484 | const InitializationKind &Kind, |
| 4485 | Expr *Initializer, |
| 4486 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4487 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4488 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 4489 | Qualifiers T1Quals; |
| 4490 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4491 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 4492 | Qualifiers T2Quals; |
| 4493 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4494 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4495 | // If the initializer is the address of an overloaded function, try |
| 4496 | // to resolve the overloaded function. If all goes well, T2 is the |
| 4497 | // type of the resulting function. |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4498 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 4499 | T1, Sequence)) |
| 4500 | return; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4501 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4502 | // Delegate everything else to a subfunction. |
| 4503 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 4504 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 4505 | } |
| 4506 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4507 | /// Determine whether an expression is a non-referenceable glvalue (one to |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 4508 | /// which a reference can never bind). Attempting to bind a reference to |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4509 | /// such a glvalue will always create a temporary. |
| 4510 | static bool isNonReferenceableGLValue(Expr *E) { |
| 4511 | return E->refersToBitField() || E->refersToVectorElement(); |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 4512 | } |
| 4513 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4514 | /// Reference initialization without resolving overloaded functions. |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4515 | static void TryReferenceInitializationCore(Sema &S, |
| 4516 | const InitializedEntity &Entity, |
| 4517 | const InitializationKind &Kind, |
| 4518 | Expr *Initializer, |
| 4519 | QualType cv1T1, QualType T1, |
| 4520 | Qualifiers T1Quals, |
| 4521 | QualType cv2T2, QualType T2, |
| 4522 | Qualifiers T2Quals, |
| 4523 | InitializationSequence &Sequence) { |
| 4524 | QualType DestType = Entity.getType(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4525 | SourceLocation DeclLoc = Initializer->getBeginLoc(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4526 | // Compute some basic properties of the types and the initializer. |
| 4527 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 4528 | bool isRValueRef = !isLValueRef; |
| 4529 | bool DerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4530 | bool ObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4531 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4532 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4533 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4534 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4535 | ObjCConversion, ObjCLifetimeConversion); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4536 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4537 | // C++0x [dcl.init.ref]p5: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4538 | // A reference to type "cv1 T1" is initialized by an expression of type |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4539 | // "cv2 T2" as follows: |
| 4540 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4541 | // - If the reference is an lvalue reference and the initializer |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4542 | // expression |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4543 | // Note the analogous bullet points for rvalue refs to functions. Because |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4544 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 4545 | // like lvalue refs. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4546 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4547 | bool T1Function = T1->isFunctionType(); |
| 4548 | if (isLValueRef || T1Function) { |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4549 | if (InitCategory.isLValue() && !isNonReferenceableGLValue(Initializer) && |
Richard Smith | ce76629 | 2016-10-21 23:01:55 +0000 | [diff] [blame] | 4550 | (RefRelationship == Sema::Ref_Compatible || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4551 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4552 | RefRelationship == Sema::Ref_Related))) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4553 | // - is an lvalue (but is not a bit-field), and "cv1 T1" is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4554 | // reference-compatible with "cv2 T2," or |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4555 | if (T1Quals != T2Quals) |
| 4556 | // Convert to cv1 T2. This should only add qualifiers unless this is a |
| 4557 | // c-style cast. The removal of qualifiers in that case notionally |
| 4558 | // happens after the reference binding, but that doesn't matter. |
| 4559 | Sequence.AddQualificationConversionStep( |
| 4560 | S.Context.getQualifiedType(T2, T1Quals), |
| 4561 | Initializer->getValueKind()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4562 | if (DerivedToBase) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4563 | Sequence.AddDerivedToBaseCastStep(cv1T1, VK_LValue); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4564 | else if (ObjCConversion) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4565 | Sequence.AddObjCObjectConversionStep(cv1T1); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4566 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4567 | // We only create a temporary here when binding a reference to a |
| 4568 | // bit-field or vector element. Those cases are't supposed to be |
| 4569 | // handled by this bullet, but the outcome is the same either way. |
| 4570 | Sequence.AddReferenceBindingStep(cv1T1, false); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4571 | return; |
| 4572 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4573 | |
| 4574 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 4575 | // reference-related to T2, and can be implicitly converted to an |
| 4576 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 4577 | // with "cv3 T3" (this conversion is selected by enumerating the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4578 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 4579 | // one through overload resolution (13.3)), |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4580 | // If we have an rvalue ref to function type here, the rhs must be |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4581 | // an rvalue. DR1287 removed the "implicitly" here. |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4582 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 4583 | (isLValueRef || InitCategory.isRValue())) { |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4584 | ConvOvlResult = TryRefInitWithConversionFunction( |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4585 | S, Entity, Kind, Initializer, /*AllowRValues*/ isRValueRef, |
| 4586 | /*IsLValueRef*/ isLValueRef, Sequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4587 | if (ConvOvlResult == OR_Success) |
| 4588 | return; |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4589 | if (ConvOvlResult != OR_No_Viable_Function) |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4590 | Sequence.SetOverloadFailure( |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4591 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4592 | ConvOvlResult); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4593 | } |
| 4594 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4595 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4596 | // - Otherwise, the reference shall be an lvalue reference to a |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4597 | // non-volatile const type (i.e., cv1 shall be const), or the reference |
Douglas Gregor | 7a2a116 | 2011-01-20 16:08:06 +0000 | [diff] [blame] | 4598 | // shall be an rvalue reference. |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 4599 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) { |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 4600 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 4601 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 4602 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4603 | Sequence.SetOverloadFailure( |
| 4604 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4605 | ConvOvlResult); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4606 | else if (!InitCategory.isLValue()) |
| 4607 | Sequence.SetFailed( |
| 4608 | InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
| 4609 | else { |
| 4610 | InitializationSequence::FailureKind FK; |
| 4611 | switch (RefRelationship) { |
| 4612 | case Sema::Ref_Compatible: |
| 4613 | if (Initializer->refersToBitField()) |
| 4614 | FK = InitializationSequence:: |
| 4615 | FK_NonConstLValueReferenceBindingToBitfield; |
| 4616 | else if (Initializer->refersToVectorElement()) |
| 4617 | FK = InitializationSequence:: |
| 4618 | FK_NonConstLValueReferenceBindingToVectorElement; |
| 4619 | else |
| 4620 | llvm_unreachable("unexpected kind of compatible initializer"); |
| 4621 | break; |
| 4622 | case Sema::Ref_Related: |
| 4623 | FK = InitializationSequence::FK_ReferenceInitDropsQualifiers; |
| 4624 | break; |
| 4625 | case Sema::Ref_Incompatible: |
| 4626 | FK = InitializationSequence:: |
| 4627 | FK_NonConstLValueReferenceBindingToUnrelated; |
| 4628 | break; |
| 4629 | } |
| 4630 | Sequence.SetFailed(FK); |
| 4631 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4632 | return; |
| 4633 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4634 | |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4635 | // - If the initializer expression |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4636 | // - is an |
| 4637 | // [<=14] xvalue (but not a bit-field), class prvalue, array prvalue, or |
| 4638 | // [1z] rvalue (but not a bit-field) or |
| 4639 | // function lvalue and "cv1 T1" is reference-compatible with "cv2 T2" |
| 4640 | // |
| 4641 | // Note: functions are handled above and below rather than here... |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4642 | if (!T1Function && |
Richard Smith | ce76629 | 2016-10-21 23:01:55 +0000 | [diff] [blame] | 4643 | (RefRelationship == Sema::Ref_Compatible || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4644 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4645 | RefRelationship == Sema::Ref_Related)) && |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4646 | ((InitCategory.isXValue() && !isNonReferenceableGLValue(Initializer)) || |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 4647 | (InitCategory.isPRValue() && |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 4648 | (S.getLangOpts().CPlusPlus17 || T2->isRecordType() || |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 4649 | T2->isArrayType())))) { |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4650 | ExprValueKind ValueKind = InitCategory.isXValue() ? VK_XValue : VK_RValue; |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4651 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4652 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 4653 | // compiler the freedom to perform a copy here or bind to the |
| 4654 | // object, while C++0x requires that we bind directly to the |
| 4655 | // object. Hence, we always bind to the object without making an |
| 4656 | // extra copy. However, in C++03 requires that we check for the |
| 4657 | // presence of a suitable copy constructor: |
| 4658 | // |
| 4659 | // The constructor that would be used to make the copy shall |
| 4660 | // be callable whether or not the copy is actually done. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4661 | if (!S.getLangOpts().CPlusPlus11 && !S.getLangOpts().MicrosoftExt) |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4662 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4663 | else if (S.getLangOpts().CPlusPlus11) |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4664 | CheckCXX98CompatAccessibleCopy(S, Entity, Initializer); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4665 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4666 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4667 | // C++1z [dcl.init.ref]/5.2.1.2: |
| 4668 | // If the converted initializer is a prvalue, its type T4 is adjusted |
| 4669 | // to type "cv1 T4" and the temporary materialization conversion is |
| 4670 | // applied. |
Anastasia Stulova | d1986d1 | 2019-01-14 11:44:22 +0000 | [diff] [blame] | 4671 | // Postpone address space conversions to after the temporary materialization |
| 4672 | // conversion to allow creating temporaries in the alloca address space. |
| 4673 | auto AS1 = T1Quals.getAddressSpace(); |
| 4674 | auto AS2 = T2Quals.getAddressSpace(); |
| 4675 | T1Quals.removeAddressSpace(); |
| 4676 | T2Quals.removeAddressSpace(); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4677 | QualType cv1T4 = S.Context.getQualifiedType(cv2T2, T1Quals); |
| 4678 | if (T1Quals != T2Quals) |
| 4679 | Sequence.AddQualificationConversionStep(cv1T4, ValueKind); |
| 4680 | Sequence.AddReferenceBindingStep(cv1T4, ValueKind == VK_RValue); |
| 4681 | ValueKind = isLValueRef ? VK_LValue : VK_XValue; |
Anastasia Stulova | d1986d1 | 2019-01-14 11:44:22 +0000 | [diff] [blame] | 4682 | if (AS1 != AS2) { |
| 4683 | T1Quals.addAddressSpace(AS1); |
| 4684 | QualType cv1AST4 = S.Context.getQualifiedType(cv2T2, T1Quals); |
| 4685 | Sequence.AddQualificationConversionStep(cv1AST4, ValueKind); |
| 4686 | } |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4687 | |
| 4688 | // In any case, the reference is bound to the resulting glvalue (or to |
| 4689 | // an appropriate base class subobject). |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4690 | if (DerivedToBase) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4691 | Sequence.AddDerivedToBaseCastStep(cv1T1, ValueKind); |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4692 | else if (ObjCConversion) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4693 | Sequence.AddObjCObjectConversionStep(cv1T1); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4694 | return; |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4695 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4696 | |
| 4697 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 4698 | // reference-related to T2, and can be implicitly converted to an |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4699 | // xvalue, class prvalue, or function lvalue of type "cv3 T3", |
| 4700 | // where "cv1 T1" is reference-compatible with "cv3 T3", |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4701 | // |
| 4702 | // DR1287 removes the "implicitly" here. |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4703 | if (T2->isRecordType()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4704 | if (RefRelationship == Sema::Ref_Incompatible) { |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4705 | ConvOvlResult = TryRefInitWithConversionFunction( |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4706 | S, Entity, Kind, Initializer, /*AllowRValues*/ true, |
| 4707 | /*IsLValueRef*/ isLValueRef, Sequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4708 | if (ConvOvlResult) |
| 4709 | Sequence.SetOverloadFailure( |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4710 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4711 | ConvOvlResult); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4712 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4713 | return; |
| 4714 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4715 | |
Richard Smith | ce76629 | 2016-10-21 23:01:55 +0000 | [diff] [blame] | 4716 | if (RefRelationship == Sema::Ref_Compatible && |
Douglas Gregor | 6fa6ab0 | 2013-03-26 23:59:23 +0000 | [diff] [blame] | 4717 | isRValueRef && InitCategory.isLValue()) { |
| 4718 | Sequence.SetFailed( |
| 4719 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 4720 | return; |
| 4721 | } |
| 4722 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4723 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 4724 | return; |
| 4725 | } |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4726 | |
| 4727 | // - Otherwise, a temporary of type "cv1 T1" is created and initialized |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4728 | // from the initializer expression using the rules for a non-reference |
Richard Smith | 2eabf78 | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 4729 | // copy-initialization (8.5). The reference is then bound to the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4730 | // temporary. [...] |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 4731 | |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 4732 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 4733 | |
Richard Smith | 2eabf78 | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 4734 | // FIXME: Why do we use an implicit conversion here rather than trying |
| 4735 | // copy-initialization? |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4736 | ImplicitConversionSequence ICS |
| 4737 | = S.TryImplicitConversion(Initializer, TempEntity.getType(), |
Richard Smith | 2eabf78 | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 4738 | /*SuppressUserConversions=*/false, |
| 4739 | /*AllowExplicit=*/false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4740 | /*FIXME:InOverloadResolution=*/false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4741 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 4742 | /*AllowObjCWritebackConversion=*/false); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4743 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4744 | if (ICS.isBad()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4745 | // FIXME: Use the conversion function set stored in ICS to turn |
| 4746 | // this into an overloading ambiguity diagnostic. However, we need |
| 4747 | // to keep that set as an OverloadCandidateSet rather than as some |
| 4748 | // other kind of set. |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4749 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 4750 | Sequence.SetOverloadFailure( |
| 4751 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4752 | ConvOvlResult); |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 4753 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 4754 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4755 | else |
| 4756 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4757 | return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4758 | } else { |
| 4759 | Sequence.AddConversionSequenceStep(ICS, TempEntity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4760 | } |
| 4761 | |
| 4762 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 4763 | // same cv-qualification as, or greater cv-qualification |
| 4764 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 4765 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 4766 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4767 | if (RefRelationship == Sema::Ref_Related && |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 4768 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4769 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 4770 | return; |
| 4771 | } |
| 4772 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4773 | // [...] If T1 is reference-related to T2 and the reference is an rvalue |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 4774 | // reference, the initializer expression shall not be an lvalue. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4775 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 4776 | InitCategory.isLValue()) { |
| 4777 | Sequence.SetFailed( |
| 4778 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 4779 | return; |
| 4780 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4781 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4782 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4783 | } |
| 4784 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4785 | /// Attempt character array initialization from a string literal |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4786 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 4787 | static void TryStringLiteralInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4788 | const InitializedEntity &Entity, |
| 4789 | const InitializationKind &Kind, |
| 4790 | Expr *Initializer, |
| 4791 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4792 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4793 | } |
| 4794 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4795 | /// Attempt value initialization (C++ [dcl.init]p7). |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4796 | static void TryValueInitialization(Sema &S, |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4797 | const InitializedEntity &Entity, |
| 4798 | const InitializationKind &Kind, |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4799 | InitializationSequence &Sequence, |
| 4800 | InitListExpr *InitList) { |
| 4801 | assert((!InitList || InitList->getNumInits() == 0) && |
| 4802 | "Shouldn't use value-init for non-empty init lists"); |
| 4803 | |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4804 | // C++98 [dcl.init]p5, C++11 [dcl.init]p7: |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4805 | // |
| 4806 | // To value-initialize an object of type T means: |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4807 | QualType T = Entity.getType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4808 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4809 | // -- if T is an array type, then each element is value-initialized; |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4810 | T = S.Context.getBaseElementType(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4811 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4812 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 4813 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4814 | bool NeedZeroInitialization = true; |
Richard Smith | 505ef81 | 2016-12-21 01:57:02 +0000 | [diff] [blame] | 4815 | // C++98: |
| 4816 | // -- if T is a class type (clause 9) with a user-declared constructor |
| 4817 | // (12.1), then the default constructor for T is called (and the |
| 4818 | // initialization is ill-formed if T has no accessible default |
| 4819 | // constructor); |
| 4820 | // C++11: |
| 4821 | // -- if T is a class type (clause 9) with either no default constructor |
| 4822 | // (12.1 [class.ctor]) or a default constructor that is user-provided |
| 4823 | // or deleted, then the object is default-initialized; |
| 4824 | // |
| 4825 | // Note that the C++11 rule is the same as the C++98 rule if there are no |
| 4826 | // defaulted or deleted constructors, so we just use it unconditionally. |
| 4827 | CXXConstructorDecl *CD = S.LookupDefaultConstructor(ClassDecl); |
| 4828 | if (!CD || !CD->getCanonicalDecl()->isDefaulted() || CD->isDeleted()) |
| 4829 | NeedZeroInitialization = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4830 | |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4831 | // -- if T is a (possibly cv-qualified) non-union class type without a |
| 4832 | // user-provided or deleted default constructor, then the object is |
| 4833 | // zero-initialized and, if T has a non-trivial default constructor, |
| 4834 | // default-initialized; |
Richard Smith | fb26652 | 2012-10-18 00:44:17 +0000 | [diff] [blame] | 4835 | // The 'non-union' here was removed by DR1502. The 'non-trivial default |
| 4836 | // constructor' part was removed by DR1507. |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4837 | if (NeedZeroInitialization) |
| 4838 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 4839 | |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 4840 | // C++03: |
| 4841 | // -- if T is a non-union class type without a user-declared constructor, |
| 4842 | // then every non-static data member and base class component of T is |
| 4843 | // value-initialized; |
| 4844 | // [...] A program that calls for [...] value-initialization of an |
| 4845 | // entity of reference type is ill-formed. |
| 4846 | // |
| 4847 | // C++11 doesn't need this handling, because value-initialization does not |
| 4848 | // occur recursively there, and the implicit default constructor is |
| 4849 | // defined as deleted in the problematic cases. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4850 | if (!S.getLangOpts().CPlusPlus11 && |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 4851 | ClassDecl->hasUninitializedReferenceMember()) { |
| 4852 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForReference); |
| 4853 | return; |
| 4854 | } |
| 4855 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4856 | // If this is list-value-initialization, pass the empty init list on when |
| 4857 | // building the constructor call. This affects the semantics of a few |
| 4858 | // things (such as whether an explicit default constructor can be called). |
| 4859 | Expr *InitListAsExpr = InitList; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4860 | MultiExprArg Args(&InitListAsExpr, InitList ? 1 : 0); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4861 | bool InitListSyntax = InitList; |
| 4862 | |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 4863 | // FIXME: Instead of creating a CXXConstructExpr of array type here, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 4864 | // wrap a class-typed CXXConstructExpr in an ArrayInitLoopExpr. |
| 4865 | return TryConstructorInitialization( |
| 4866 | S, Entity, Kind, Args, T, Entity.getType(), Sequence, InitListSyntax); |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4867 | } |
| 4868 | } |
| 4869 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4870 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4871 | } |
| 4872 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4873 | /// Attempt default initialization (C++ [dcl.init]p6). |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4874 | static void TryDefaultInitialization(Sema &S, |
| 4875 | const InitializedEntity &Entity, |
| 4876 | const InitializationKind &Kind, |
| 4877 | InitializationSequence &Sequence) { |
| 4878 | assert(Kind.getKind() == InitializationKind::IK_Default); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4879 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4880 | // C++ [dcl.init]p6: |
| 4881 | // To default-initialize an object of type T means: |
| 4882 | // - if T is an array type, each element is default-initialized; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4883 | QualType DestType = S.Context.getBaseElementType(Entity.getType()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4884 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4885 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 4886 | // constructor for T is called (and the initialization is ill-formed if |
| 4887 | // T has no accessible default constructor); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4888 | if (DestType->isRecordType() && S.getLangOpts().CPlusPlus) { |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 4889 | TryConstructorInitialization(S, Entity, Kind, None, DestType, |
| 4890 | Entity.getType(), Sequence); |
Chandler Carruth | c926240 | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 4891 | return; |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4892 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4893 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4894 | // - otherwise, no initialization is performed. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4895 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4896 | // If a program calls for the default initialization of an object of |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4897 | // a const-qualified type T, T shall be a class type with a user-provided |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4898 | // default constructor. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4899 | if (DestType.isConstQualified() && S.getLangOpts().CPlusPlus) { |
Nico Weber | 337d5aa | 2015-04-17 08:32:38 +0000 | [diff] [blame] | 4900 | if (!maybeRecoverWithZeroInitialization(S, Sequence, Entity)) |
| 4901 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4902 | return; |
| 4903 | } |
| 4904 | |
| 4905 | // If the destination type has a lifetime property, zero-initialize it. |
| 4906 | if (DestType.getQualifiers().hasObjCLifetime()) { |
| 4907 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 4908 | return; |
| 4909 | } |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4910 | } |
| 4911 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4912 | /// Attempt a user-defined conversion between two types (C++ [dcl.init]), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4913 | /// which enumerates all conversion functions and performs overload resolution |
| 4914 | /// to select the best. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4915 | static void TryUserDefinedConversion(Sema &S, |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 4916 | QualType DestType, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4917 | const InitializationKind &Kind, |
| 4918 | Expr *Initializer, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4919 | InitializationSequence &Sequence, |
| 4920 | bool TopLevelOfInitList) { |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4921 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 4922 | QualType SourceType = Initializer->getType(); |
| 4923 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 4924 | "Must have a class type to perform a user-defined conversion"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4925 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4926 | // Build the candidate set directly in the initialization sequence |
| 4927 | // structure, so that it will persist if we fail. |
| 4928 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 4929 | CandidateSet.clear(OverloadCandidateSet::CSK_InitByUserDefinedConversion); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4930 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4931 | // Determine whether we are allowed to call explicit constructors or |
| 4932 | // explicit conversion operators. |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 4933 | bool AllowExplicit = Kind.AllowExplicit(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4934 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4935 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 4936 | // The type we're converting to is a class type. Enumerate its constructors |
| 4937 | // to see if there is a suitable conversion. |
| 4938 | CXXRecordDecl *DestRecordDecl |
| 4939 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4940 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4941 | // Try to complete the type we're converting to. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4942 | if (S.isCompleteType(Kind.getLocation(), DestType)) { |
Richard Smith | 776e9c3 | 2017-02-01 03:28:59 +0000 | [diff] [blame] | 4943 | for (NamedDecl *D : S.LookupConstructors(DestRecordDecl)) { |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4944 | auto Info = getConstructorInfo(D); |
| 4945 | if (!Info.Constructor) |
| 4946 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4947 | |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4948 | if (!Info.Constructor->isInvalidDecl() && |
| 4949 | Info.Constructor->isConvertingConstructor(AllowExplicit)) { |
| 4950 | if (Info.ConstructorTmpl) |
| 4951 | S.AddTemplateOverloadCandidate(Info.ConstructorTmpl, Info.FoundDecl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4952 | /*ExplicitArgs*/ nullptr, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4953 | Initializer, CandidateSet, |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 4954 | /*SuppressUserConversions=*/true); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4955 | else |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4956 | S.AddOverloadCandidate(Info.Constructor, Info.FoundDecl, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4957 | Initializer, CandidateSet, |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 4958 | /*SuppressUserConversions=*/true); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4959 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4960 | } |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4961 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4962 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4963 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4964 | SourceLocation DeclLoc = Initializer->getBeginLoc(); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4965 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4966 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 4967 | // The type we're converting from is a class type, enumerate its conversion |
| 4968 | // functions. |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4969 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4970 | // We can only enumerate the conversion functions for a complete type; if |
| 4971 | // the type isn't complete, simply skip this step. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4972 | if (S.isCompleteType(DeclLoc, SourceType)) { |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4973 | CXXRecordDecl *SourceRecordDecl |
| 4974 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4975 | |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 4976 | const auto &Conversions = |
| 4977 | SourceRecordDecl->getVisibleConversionFunctions(); |
| 4978 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4979 | NamedDecl *D = *I; |
| 4980 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 4981 | if (isa<UsingShadowDecl>(D)) |
| 4982 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4983 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4984 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 4985 | CXXConversionDecl *Conv; |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4986 | if (ConvTemplate) |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4987 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4988 | else |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4989 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4990 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4991 | if (AllowExplicit || !Conv->isExplicit()) { |
| 4992 | if (ConvTemplate) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4993 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4994 | ActingDC, Initializer, DestType, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 4995 | CandidateSet, AllowExplicit); |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4996 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4997 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 4998 | Initializer, DestType, CandidateSet, |
| 4999 | AllowExplicit); |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 5000 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5001 | } |
| 5002 | } |
| 5003 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5004 | |
| 5005 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5006 | OverloadCandidateSet::iterator Best; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5007 | if (OverloadingResult Result |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 5008 | = CandidateSet.BestViableFunction(S, DeclLoc, Best)) { |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5009 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5010 | InitializationSequence::FK_UserConversionOverloadFailed, |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5011 | Result); |
| 5012 | return; |
| 5013 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5014 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5015 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 5016 | Function->setReferenced(); |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 5017 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5018 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5019 | if (isa<CXXConstructorDecl>(Function)) { |
| 5020 | // Add the user-defined conversion step. Any cv-qualification conversion is |
Richard Smith | b24f067 | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 5021 | // subsumed by the initialization. Per DR5, the created temporary is of the |
| 5022 | // cv-unqualified type of the destination. |
| 5023 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
| 5024 | DestType.getUnqualifiedType(), |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 5025 | HadMultipleCandidates); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5026 | |
| 5027 | // C++14 and before: |
| 5028 | // - if the function is a constructor, the call initializes a temporary |
| 5029 | // of the cv-unqualified version of the destination type. The [...] |
| 5030 | // temporary [...] is then used to direct-initialize, according to the |
| 5031 | // rules above, the object that is the destination of the |
| 5032 | // copy-initialization. |
| 5033 | // Note that this just performs a simple object copy from the temporary. |
| 5034 | // |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 5035 | // C++17: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5036 | // - if the function is a constructor, the call is a prvalue of the |
| 5037 | // cv-unqualified version of the destination type whose return object |
| 5038 | // is initialized by the constructor. The call is used to |
| 5039 | // direct-initialize, according to the rules above, the object that |
| 5040 | // is the destination of the copy-initialization. |
| 5041 | // Therefore we need to do nothing further. |
| 5042 | // |
| 5043 | // FIXME: Mark this copy as extraneous. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 5044 | if (!S.getLangOpts().CPlusPlus17) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5045 | Sequence.AddFinalCopy(DestType); |
Richard Smith | 16d3150 | 2016-12-21 01:31:56 +0000 | [diff] [blame] | 5046 | else if (DestType.hasQualifiers()) |
| 5047 | Sequence.AddQualificationConversionStep(DestType, VK_RValue); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5048 | return; |
| 5049 | } |
| 5050 | |
| 5051 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 5052 | QualType ConvType = Function->getCallResultType(); |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 5053 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType, |
| 5054 | HadMultipleCandidates); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5055 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5056 | if (ConvType->getAs<RecordType>()) { |
| 5057 | // The call is used to direct-initialize [...] the object that is the |
| 5058 | // destination of the copy-initialization. |
| 5059 | // |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 5060 | // In C++17, this does not call a constructor if we enter /17.6.1: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5061 | // - If the initializer expression is a prvalue and the cv-unqualified |
| 5062 | // version of the source type is the same as the class of the |
| 5063 | // destination [... do not make an extra copy] |
| 5064 | // |
| 5065 | // FIXME: Mark this copy as extraneous. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 5066 | if (!S.getLangOpts().CPlusPlus17 || |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5067 | Function->getReturnType()->isReferenceType() || |
| 5068 | !S.Context.hasSameUnqualifiedType(ConvType, DestType)) |
| 5069 | Sequence.AddFinalCopy(DestType); |
Richard Smith | 16d3150 | 2016-12-21 01:31:56 +0000 | [diff] [blame] | 5070 | else if (!S.Context.hasSameType(ConvType, DestType)) |
| 5071 | Sequence.AddQualificationConversionStep(DestType, VK_RValue); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5072 | return; |
| 5073 | } |
| 5074 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5075 | // If the conversion following the call to the conversion function |
| 5076 | // is interesting, add it as a separate step. |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5077 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 5078 | Best->FinalConversion.Third) { |
| 5079 | ImplicitConversionSequence ICS; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5080 | ICS.setStandard(); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5081 | ICS.Standard = Best->FinalConversion; |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5082 | Sequence.AddConversionSequenceStep(ICS, DestType, TopLevelOfInitList); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5083 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5084 | } |
| 5085 | |
Richard Smith | f032001b | 2013-06-20 02:18:31 +0000 | [diff] [blame] | 5086 | /// An egregious hack for compatibility with libstdc++-4.2: in <tr1/hashtable>, |
| 5087 | /// a function with a pointer return type contains a 'return false;' statement. |
| 5088 | /// In C++11, 'false' is not a null pointer, so this breaks the build of any |
| 5089 | /// code using that header. |
| 5090 | /// |
| 5091 | /// Work around this by treating 'return false;' as zero-initializing the result |
| 5092 | /// if it's used in a pointer-returning function in a system header. |
| 5093 | static bool isLibstdcxxPointerReturnFalseHack(Sema &S, |
| 5094 | const InitializedEntity &Entity, |
| 5095 | const Expr *Init) { |
| 5096 | return S.getLangOpts().CPlusPlus11 && |
| 5097 | Entity.getKind() == InitializedEntity::EK_Result && |
| 5098 | Entity.getType()->isPointerType() && |
| 5099 | isa<CXXBoolLiteralExpr>(Init) && |
| 5100 | !cast<CXXBoolLiteralExpr>(Init)->getValue() && |
| 5101 | S.getSourceManager().isInSystemHeader(Init->getExprLoc()); |
| 5102 | } |
| 5103 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5104 | /// The non-zero enum values here are indexes into diagnostic alternatives. |
| 5105 | enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar }; |
| 5106 | |
| 5107 | /// Determines whether this expression is an acceptable ICR source. |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 5108 | static InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e, |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5109 | bool isAddressOf, bool &isWeakAccess) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5110 | // Skip parens. |
| 5111 | e = e->IgnoreParens(); |
| 5112 | |
| 5113 | // Skip address-of nodes. |
| 5114 | if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 5115 | if (op->getOpcode() == UO_AddrOf) |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5116 | return isInvalidICRSource(C, op->getSubExpr(), /*addressof*/ true, |
| 5117 | isWeakAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5118 | |
| 5119 | // Skip certain casts. |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 5120 | } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 5121 | switch (ce->getCastKind()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5122 | case CK_Dependent: |
| 5123 | case CK_BitCast: |
| 5124 | case CK_LValueBitCast: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5125 | case CK_NoOp: |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5126 | return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf, isWeakAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5127 | |
| 5128 | case CK_ArrayToPointerDecay: |
| 5129 | return IIK_nonscalar; |
| 5130 | |
| 5131 | case CK_NullToPointer: |
| 5132 | return IIK_okay; |
| 5133 | |
| 5134 | default: |
| 5135 | break; |
| 5136 | } |
| 5137 | |
| 5138 | // If we have a declaration reference, it had better be a local variable. |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5139 | } else if (isa<DeclRefExpr>(e)) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5140 | // set isWeakAccess to true, to mean that there will be an implicit |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5141 | // load which requires a cleanup. |
| 5142 | if (e->getType().getObjCLifetime() == Qualifiers::OCL_Weak) |
| 5143 | isWeakAccess = true; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5144 | |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 5145 | if (!isAddressOf) return IIK_nonlocal; |
| 5146 | |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5147 | VarDecl *var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl()); |
| 5148 | if (!var) return IIK_nonlocal; |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 5149 | |
| 5150 | return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5151 | |
| 5152 | // If we have a conditional operator, check both sides. |
| 5153 | } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) { |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5154 | if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf, |
| 5155 | isWeakAccess)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5156 | return iik; |
| 5157 | |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5158 | return isInvalidICRSource(C, cond->getRHS(), isAddressOf, isWeakAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5159 | |
| 5160 | // These are never scalar. |
| 5161 | } else if (isa<ArraySubscriptExpr>(e)) { |
| 5162 | return IIK_nonscalar; |
| 5163 | |
| 5164 | // Otherwise, it needs to be a null pointer constant. |
| 5165 | } else { |
| 5166 | return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull) |
| 5167 | ? IIK_okay : IIK_nonlocal); |
| 5168 | } |
| 5169 | |
| 5170 | return IIK_nonlocal; |
| 5171 | } |
| 5172 | |
| 5173 | /// Check whether the given expression is a valid operand for an |
| 5174 | /// indirect copy/restore. |
| 5175 | static void checkIndirectCopyRestoreSource(Sema &S, Expr *src) { |
| 5176 | assert(src->isRValue()); |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5177 | bool isWeakAccess = false; |
| 5178 | InvalidICRKind iik = isInvalidICRSource(S.Context, src, false, isWeakAccess); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5179 | // If isWeakAccess to true, there will be an implicit |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5180 | // load which requires a cleanup. |
| 5181 | if (S.getLangOpts().ObjCAutoRefCount && isWeakAccess) |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 5182 | S.Cleanup.setExprNeedsCleanups(true); |
| 5183 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5184 | if (iik == IIK_okay) return; |
| 5185 | |
| 5186 | S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback) |
| 5187 | << ((unsigned) iik - 1) // shift index into diagnostic explanations |
| 5188 | << src->getSourceRange(); |
| 5189 | } |
| 5190 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5191 | /// Determine whether we have compatible array types for the |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5192 | /// purposes of GNU by-copy array initialization. |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 5193 | static bool hasCompatibleArrayTypes(ASTContext &Context, const ArrayType *Dest, |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5194 | const ArrayType *Source) { |
| 5195 | // If the source and destination array types are equivalent, we're |
| 5196 | // done. |
| 5197 | if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0))) |
| 5198 | return true; |
| 5199 | |
| 5200 | // Make sure that the element types are the same. |
| 5201 | if (!Context.hasSameType(Dest->getElementType(), Source->getElementType())) |
| 5202 | return false; |
| 5203 | |
| 5204 | // The only mismatch we allow is when the destination is an |
| 5205 | // incomplete array type and the source is a constant array type. |
| 5206 | return Source->isConstantArrayType() && Dest->isIncompleteArrayType(); |
| 5207 | } |
| 5208 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5209 | static bool tryObjCWritebackConversion(Sema &S, |
| 5210 | InitializationSequence &Sequence, |
| 5211 | const InitializedEntity &Entity, |
| 5212 | Expr *Initializer) { |
| 5213 | bool ArrayDecay = false; |
| 5214 | QualType ArgType = Initializer->getType(); |
| 5215 | QualType ArgPointee; |
| 5216 | if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) { |
| 5217 | ArrayDecay = true; |
| 5218 | ArgPointee = ArgArrayType->getElementType(); |
| 5219 | ArgType = S.Context.getPointerType(ArgPointee); |
| 5220 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5221 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5222 | // Handle write-back conversion. |
| 5223 | QualType ConvertedArgType; |
| 5224 | if (!S.isObjCWritebackConversion(ArgType, Entity.getType(), |
| 5225 | ConvertedArgType)) |
| 5226 | return false; |
| 5227 | |
| 5228 | // We should copy unless we're passing to an argument explicitly |
| 5229 | // marked 'out'. |
| 5230 | bool ShouldCopy = true; |
| 5231 | if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 5232 | ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 5233 | |
| 5234 | // Do we need an lvalue conversion? |
| 5235 | if (ArrayDecay || Initializer->isGLValue()) { |
| 5236 | ImplicitConversionSequence ICS; |
| 5237 | ICS.setStandard(); |
| 5238 | ICS.Standard.setAsIdentityConversion(); |
| 5239 | |
| 5240 | QualType ResultType; |
| 5241 | if (ArrayDecay) { |
| 5242 | ICS.Standard.First = ICK_Array_To_Pointer; |
| 5243 | ResultType = S.Context.getPointerType(ArgPointee); |
| 5244 | } else { |
| 5245 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
| 5246 | ResultType = Initializer->getType().getNonLValueExprType(S.Context); |
| 5247 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5248 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5249 | Sequence.AddConversionSequenceStep(ICS, ResultType); |
| 5250 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5251 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5252 | Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
| 5253 | return true; |
| 5254 | } |
| 5255 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5256 | static bool TryOCLSamplerInitialization(Sema &S, |
| 5257 | InitializationSequence &Sequence, |
| 5258 | QualType DestType, |
| 5259 | Expr *Initializer) { |
| 5260 | if (!S.getLangOpts().OpenCL || !DestType->isSamplerT() || |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 5261 | (!Initializer->isIntegerConstantExpr(S.Context) && |
| 5262 | !Initializer->getType()->isSamplerT())) |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5263 | return false; |
| 5264 | |
| 5265 | Sequence.AddOCLSamplerInitStep(DestType); |
| 5266 | return true; |
| 5267 | } |
| 5268 | |
Andrew Savonichev | 3fee351 | 2018-11-08 11:25:41 +0000 | [diff] [blame] | 5269 | static bool IsZeroInitializer(Expr *Initializer, Sema &S) { |
| 5270 | return Initializer->isIntegerConstantExpr(S.getASTContext()) && |
| 5271 | (Initializer->EvaluateKnownConstInt(S.getASTContext()) == 0); |
| 5272 | } |
| 5273 | |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 5274 | static bool TryOCLZeroOpaqueTypeInitialization(Sema &S, |
| 5275 | InitializationSequence &Sequence, |
| 5276 | QualType DestType, |
| 5277 | Expr *Initializer) { |
| 5278 | if (!S.getLangOpts().OpenCL) |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5279 | return false; |
| 5280 | |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 5281 | // |
| 5282 | // OpenCL 1.2 spec, s6.12.10 |
| 5283 | // |
| 5284 | // The event argument can also be used to associate the |
| 5285 | // async_work_group_copy with a previous async copy allowing |
| 5286 | // an event to be shared by multiple async copies; otherwise |
| 5287 | // event should be zero. |
| 5288 | // |
| 5289 | if (DestType->isEventT() || DestType->isQueueT()) { |
Andrew Savonichev | 3fee351 | 2018-11-08 11:25:41 +0000 | [diff] [blame] | 5290 | if (!IsZeroInitializer(Initializer, S)) |
| 5291 | return false; |
| 5292 | |
| 5293 | Sequence.AddOCLZeroOpaqueTypeStep(DestType); |
| 5294 | return true; |
| 5295 | } |
| 5296 | |
| 5297 | // We should allow zero initialization for all types defined in the |
| 5298 | // cl_intel_device_side_avc_motion_estimation extension, except |
| 5299 | // intel_sub_group_avc_mce_payload_t and intel_sub_group_avc_mce_result_t. |
| 5300 | if (S.getOpenCLOptions().isEnabled( |
| 5301 | "cl_intel_device_side_avc_motion_estimation") && |
| 5302 | DestType->isOCLIntelSubgroupAVCType()) { |
| 5303 | if (DestType->isOCLIntelSubgroupAVCMcePayloadType() || |
| 5304 | DestType->isOCLIntelSubgroupAVCMceResultType()) |
| 5305 | return false; |
| 5306 | if (!IsZeroInitializer(Initializer, S)) |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 5307 | return false; |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5308 | |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 5309 | Sequence.AddOCLZeroOpaqueTypeStep(DestType); |
| 5310 | return true; |
| 5311 | } |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 5312 | |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 5313 | return false; |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 5314 | } |
| 5315 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5316 | InitializationSequence::InitializationSequence(Sema &S, |
| 5317 | const InitializedEntity &Entity, |
| 5318 | const InitializationKind &Kind, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5319 | MultiExprArg Args, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 5320 | bool TopLevelOfInitList, |
| 5321 | bool TreatUnavailableAsInvalid) |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 5322 | : FailedCandidateSet(Kind.getLocation(), OverloadCandidateSet::CSK_Normal) { |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 5323 | InitializeFrom(S, Entity, Kind, Args, TopLevelOfInitList, |
| 5324 | TreatUnavailableAsInvalid); |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 5325 | } |
| 5326 | |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 5327 | /// Tries to get a FunctionDecl out of `E`. If it succeeds and we can take the |
| 5328 | /// address of that function, this returns true. Otherwise, it returns false. |
| 5329 | static bool isExprAnUnaddressableFunction(Sema &S, const Expr *E) { |
| 5330 | auto *DRE = dyn_cast<DeclRefExpr>(E); |
| 5331 | if (!DRE || !isa<FunctionDecl>(DRE->getDecl())) |
| 5332 | return false; |
| 5333 | |
| 5334 | return !S.checkAddressOfFunctionIsAvailable( |
| 5335 | cast<FunctionDecl>(DRE->getDecl())); |
| 5336 | } |
| 5337 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5338 | /// Determine whether we can perform an elementwise array copy for this kind |
| 5339 | /// of entity. |
| 5340 | static bool canPerformArrayCopy(const InitializedEntity &Entity) { |
| 5341 | switch (Entity.getKind()) { |
| 5342 | case InitializedEntity::EK_LambdaCapture: |
| 5343 | // C++ [expr.prim.lambda]p24: |
| 5344 | // For array members, the array elements are direct-initialized in |
| 5345 | // increasing subscript order. |
| 5346 | return true; |
| 5347 | |
| 5348 | case InitializedEntity::EK_Variable: |
| 5349 | // C++ [dcl.decomp]p1: |
| 5350 | // [...] each element is copy-initialized or direct-initialized from the |
| 5351 | // corresponding element of the assignment-expression [...] |
| 5352 | return isa<DecompositionDecl>(Entity.getDecl()); |
| 5353 | |
| 5354 | case InitializedEntity::EK_Member: |
| 5355 | // C++ [class.copy.ctor]p14: |
| 5356 | // - if the member is an array, each element is direct-initialized with |
| 5357 | // the corresponding subobject of x |
| 5358 | return Entity.isImplicitMemberInitializer(); |
| 5359 | |
| 5360 | case InitializedEntity::EK_ArrayElement: |
| 5361 | // All the above cases are intended to apply recursively, even though none |
| 5362 | // of them actually say that. |
| 5363 | if (auto *E = Entity.getParent()) |
| 5364 | return canPerformArrayCopy(*E); |
| 5365 | break; |
| 5366 | |
| 5367 | default: |
| 5368 | break; |
| 5369 | } |
| 5370 | |
| 5371 | return false; |
| 5372 | } |
| 5373 | |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 5374 | void InitializationSequence::InitializeFrom(Sema &S, |
| 5375 | const InitializedEntity &Entity, |
| 5376 | const InitializationKind &Kind, |
| 5377 | MultiExprArg Args, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 5378 | bool TopLevelOfInitList, |
| 5379 | bool TreatUnavailableAsInvalid) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5380 | ASTContext &Context = S.Context; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5381 | |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 5382 | // Eliminate non-overload placeholder types in the arguments. We |
| 5383 | // need to do this before checking whether types are dependent |
| 5384 | // because lowering a pseudo-object expression might well give us |
| 5385 | // something of dependent type. |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5386 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 5387 | if (Args[I]->getType()->isNonOverloadPlaceholderType()) { |
| 5388 | // FIXME: should we be doing this here? |
| 5389 | ExprResult result = S.CheckPlaceholderExpr(Args[I]); |
| 5390 | if (result.isInvalid()) { |
| 5391 | SetFailed(FK_PlaceholderType); |
| 5392 | return; |
| 5393 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5394 | Args[I] = result.get(); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 5395 | } |
| 5396 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5397 | // C++0x [dcl.init]p16: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5398 | // The semantics of initializers are as follows. The destination type is |
| 5399 | // the type of the object or reference being initialized and the source |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5400 | // type is the type of the initializer expression. The source type is not |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5401 | // defined when the initializer is a braced-init-list or when it is a |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5402 | // parenthesized list of expressions. |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5403 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5404 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5405 | if (DestType->isDependentType() || |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5406 | Expr::hasAnyTypeDependentArguments(Args)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5407 | SequenceKind = DependentSequence; |
| 5408 | return; |
| 5409 | } |
| 5410 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5411 | // Almost everything is a normal sequence. |
| 5412 | setSequenceKind(NormalSequence); |
| 5413 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5414 | QualType SourceType; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5415 | Expr *Initializer = nullptr; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5416 | if (Args.size() == 1) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5417 | Initializer = Args[0]; |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 5418 | if (S.getLangOpts().ObjC) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5419 | if (S.CheckObjCBridgeRelatedConversions(Initializer->getBeginLoc(), |
Fariborz Jahanian | 283bf89 | 2013-12-18 21:04:43 +0000 | [diff] [blame] | 5420 | DestType, Initializer->getType(), |
| 5421 | Initializer) || |
| 5422 | S.ConversionToObjCStringLiteralCheck(DestType, Initializer)) |
| 5423 | Args[0] = Initializer; |
Fariborz Jahanian | 283bf89 | 2013-12-18 21:04:43 +0000 | [diff] [blame] | 5424 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5425 | if (!isa<InitListExpr>(Initializer)) |
| 5426 | SourceType = Initializer->getType(); |
| 5427 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5428 | |
Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 5429 | // - If the initializer is a (non-parenthesized) braced-init-list, the |
| 5430 | // object is list-initialized (8.5.4). |
| 5431 | if (Kind.getKind() != InitializationKind::IK_Direct) { |
| 5432 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 5433 | TryListInitialization(S, Entity, Kind, InitList, *this, |
| 5434 | TreatUnavailableAsInvalid); |
Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 5435 | return; |
| 5436 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5437 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5438 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5439 | // - If the destination type is a reference type, see 8.5.3. |
| 5440 | if (DestType->isReferenceType()) { |
| 5441 | // C++0x [dcl.init.ref]p1: |
| 5442 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 5443 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 5444 | // by an object that can be converted into a T. |
| 5445 | // (Therefore, multiple arguments are not permitted.) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5446 | if (Args.size() != 1) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5447 | SetFailed(FK_TooManyInitsForReference); |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 5448 | // C++17 [dcl.init.ref]p5: |
| 5449 | // A reference [...] is initialized by an expression [...] as follows: |
| 5450 | // If the initializer is not an expression, presumably we should reject, |
| 5451 | // but the standard fails to actually say so. |
| 5452 | else if (isa<InitListExpr>(Args[0])) |
| 5453 | SetFailed(FK_ParenthesizedListInitForReference); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5454 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5455 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5456 | return; |
| 5457 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5458 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5459 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5460 | if (Kind.getKind() == InitializationKind::IK_Value || |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5461 | (Kind.getKind() == InitializationKind::IK_Direct && Args.empty())) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5462 | TryValueInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5463 | return; |
| 5464 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5465 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5466 | // Handle default initialization. |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 5467 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5468 | TryDefaultInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5469 | return; |
| 5470 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5471 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 5472 | // - If the destination type is an array of characters, an array of |
| 5473 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 5474 | // initializer is a string literal, see 8.5.2. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5475 | // - Otherwise, if the destination type is an array, the program is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5476 | // ill-formed. |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5477 | if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) { |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 5478 | if (Initializer && isa<VariableArrayType>(DestAT)) { |
| 5479 | SetFailed(FK_VariableLengthArrayHasInitializer); |
| 5480 | return; |
| 5481 | } |
| 5482 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 5483 | if (Initializer) { |
| 5484 | switch (IsStringInit(Initializer, DestAT, Context)) { |
| 5485 | case SIF_None: |
| 5486 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
| 5487 | return; |
| 5488 | case SIF_NarrowStringIntoWideChar: |
| 5489 | SetFailed(FK_NarrowStringIntoWideCharArray); |
| 5490 | return; |
| 5491 | case SIF_WideStringIntoChar: |
| 5492 | SetFailed(FK_WideStringIntoCharArray); |
| 5493 | return; |
| 5494 | case SIF_IncompatWideStringIntoWideChar: |
| 5495 | SetFailed(FK_IncompatWideStringIntoWideChar); |
| 5496 | return; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 5497 | case SIF_PlainStringIntoUTF8Char: |
| 5498 | SetFailed(FK_PlainStringIntoUTF8Char); |
| 5499 | return; |
| 5500 | case SIF_UTF8StringIntoPlainChar: |
| 5501 | SetFailed(FK_UTF8StringIntoPlainChar); |
| 5502 | return; |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 5503 | case SIF_Other: |
| 5504 | break; |
| 5505 | } |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 5506 | } |
| 5507 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5508 | // Some kinds of initialization permit an array to be initialized from |
| 5509 | // another array of the same type, and perform elementwise initialization. |
| 5510 | if (Initializer && isa<ConstantArrayType>(DestAT) && |
| 5511 | S.Context.hasSameUnqualifiedType(Initializer->getType(), |
| 5512 | Entity.getType()) && |
| 5513 | canPerformArrayCopy(Entity)) { |
| 5514 | // If source is a prvalue, use it directly. |
| 5515 | if (Initializer->getValueKind() == VK_RValue) { |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 5516 | AddArrayInitStep(DestType, /*IsGNUExtension*/false); |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5517 | return; |
| 5518 | } |
| 5519 | |
| 5520 | // Emit element-at-a-time copy loop. |
| 5521 | InitializedEntity Element = |
| 5522 | InitializedEntity::InitializeElement(S.Context, 0, Entity); |
| 5523 | QualType InitEltT = |
| 5524 | Context.getAsArrayType(Initializer->getType())->getElementType(); |
Richard Smith | 30e304e | 2016-12-14 00:03:17 +0000 | [diff] [blame] | 5525 | OpaqueValueExpr OVE(Initializer->getExprLoc(), InitEltT, |
| 5526 | Initializer->getValueKind(), |
| 5527 | Initializer->getObjectKind()); |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5528 | Expr *OVEAsExpr = &OVE; |
| 5529 | InitializeFrom(S, Element, Kind, OVEAsExpr, TopLevelOfInitList, |
| 5530 | TreatUnavailableAsInvalid); |
| 5531 | if (!Failed()) |
| 5532 | AddArrayInitLoopStep(Entity.getType(), InitEltT); |
| 5533 | return; |
| 5534 | } |
| 5535 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5536 | // Note: as an GNU C extension, we allow initialization of an |
| 5537 | // array from a compound literal that creates an array of the same |
| 5538 | // type, so long as the initializer has no side effects. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5539 | if (!S.getLangOpts().CPlusPlus && Initializer && |
Bill Wendling | 8003edc | 2018-11-09 00:41:36 +0000 | [diff] [blame] | 5540 | (isa<ConstantExpr>(Initializer->IgnoreParens()) || |
| 5541 | isa<CompoundLiteralExpr>(Initializer->IgnoreParens())) && |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5542 | Initializer->getType()->isArrayType()) { |
| 5543 | const ArrayType *SourceAT |
| 5544 | = Context.getAsArrayType(Initializer->getType()); |
| 5545 | if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5546 | SetFailed(FK_ArrayTypeMismatch); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5547 | else if (Initializer->HasSideEffects(S.Context)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5548 | SetFailed(FK_NonConstantArrayInit); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5549 | else { |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 5550 | AddArrayInitStep(DestType, /*IsGNUExtension*/true); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5551 | } |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5552 | } |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5553 | // Note: as a GNU C++ extension, we allow list-initialization of a |
| 5554 | // class member of array type from a parenthesized initializer list. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5555 | else if (S.getLangOpts().CPlusPlus && |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5556 | Entity.getKind() == InitializedEntity::EK_Member && |
| 5557 | Initializer && isa<InitListExpr>(Initializer)) { |
| 5558 | TryListInitialization(S, Entity, Kind, cast<InitListExpr>(Initializer), |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 5559 | *this, TreatUnavailableAsInvalid); |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5560 | AddParenthesizedArrayInitStep(DestType); |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 5561 | } else if (DestAT->getElementType()->isCharType()) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5562 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 5563 | else if (IsWideCharCompatible(DestAT->getElementType(), Context)) |
| 5564 | SetFailed(FK_ArrayNeedsInitListOrWideStringLiteral); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5565 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5566 | SetFailed(FK_ArrayNeedsInitList); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5567 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5568 | return; |
| 5569 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5570 | |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 5571 | // Determine whether we should consider writeback conversions for |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5572 | // Objective-C ARC. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5573 | bool allowObjCWritebackConversion = S.getLangOpts().ObjCAutoRefCount && |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5574 | Entity.isParameterKind(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5575 | |
| 5576 | // We're at the end of the line for C: it's either a write-back conversion |
| 5577 | // or it's a C assignment. There's no need to check anything else. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5578 | if (!S.getLangOpts().CPlusPlus) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5579 | // If allowed, check whether this is an Objective-C writeback conversion. |
| 5580 | if (allowObjCWritebackConversion && |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5581 | tryObjCWritebackConversion(S, *this, Entity, Initializer)) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5582 | return; |
| 5583 | } |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5584 | |
| 5585 | if (TryOCLSamplerInitialization(S, *this, DestType, Initializer)) |
| 5586 | return; |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5587 | |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 5588 | if (TryOCLZeroOpaqueTypeInitialization(S, *this, DestType, Initializer)) |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5589 | return; |
| 5590 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5591 | // Handle initialization in C |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5592 | AddCAssignmentStep(DestType); |
| 5593 | MaybeProduceObjCObject(S, *this, Entity); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5594 | return; |
| 5595 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5596 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5597 | assert(S.getLangOpts().CPlusPlus); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 5598 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5599 | // - If the destination type is a (possibly cv-qualified) class type: |
| 5600 | if (DestType->isRecordType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5601 | // - If the initialization is direct-initialization, or if it is |
| 5602 | // copy-initialization where the cv-unqualified version of the |
| 5603 | // source type is the same class as, or a derived class of, the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5604 | // class of the destination, constructors are considered. [...] |
| 5605 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 5606 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 5607 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5608 | S.IsDerivedFrom(Initializer->getBeginLoc(), SourceType, DestType)))) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5609 | TryConstructorInitialization(S, Entity, Kind, Args, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5610 | DestType, DestType, *this); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5611 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5612 | // user-defined conversion sequences that can convert from the source |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5613 | // type to the destination type or (when a conversion function is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5614 | // used) to a derived class thereof are enumerated as described in |
| 5615 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 5616 | // (13.3). |
| 5617 | else |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5618 | TryUserDefinedConversion(S, DestType, Kind, Initializer, *this, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5619 | TopLevelOfInitList); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5620 | return; |
| 5621 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5622 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 5623 | assert(Args.size() >= 1 && "Zero-argument case handled above"); |
| 5624 | |
| 5625 | // The remaining cases all need a source type. |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5626 | if (Args.size() > 1) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5627 | SetFailed(FK_TooManyInitsForScalar); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5628 | return; |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 5629 | } else if (isa<InitListExpr>(Args[0])) { |
| 5630 | SetFailed(FK_ParenthesizedListInitForScalar); |
| 5631 | return; |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5632 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5633 | |
| 5634 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5635 | // type, conversion functions are considered. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5636 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5637 | // For a conversion to _Atomic(T) from either T or a class type derived |
| 5638 | // from T, initialize the T object then convert to _Atomic type. |
| 5639 | bool NeedAtomicConversion = false; |
| 5640 | if (const AtomicType *Atomic = DestType->getAs<AtomicType>()) { |
| 5641 | if (Context.hasSameUnqualifiedType(SourceType, Atomic->getValueType()) || |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5642 | S.IsDerivedFrom(Initializer->getBeginLoc(), SourceType, |
Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 5643 | Atomic->getValueType())) { |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5644 | DestType = Atomic->getValueType(); |
| 5645 | NeedAtomicConversion = true; |
| 5646 | } |
| 5647 | } |
| 5648 | |
| 5649 | TryUserDefinedConversion(S, DestType, Kind, Initializer, *this, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5650 | TopLevelOfInitList); |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5651 | MaybeProduceObjCObject(S, *this, Entity); |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5652 | if (!Failed() && NeedAtomicConversion) |
| 5653 | AddAtomicConversionStep(Entity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5654 | return; |
| 5655 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5656 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5657 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5658 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5659 | // conversions (Clause 4) will be used, if necessary, to convert the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5660 | // initializer expression to the cv-unqualified version of the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5661 | // destination type; no user-defined conversions are considered. |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5662 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5663 | ImplicitConversionSequence ICS |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5664 | = S.TryImplicitConversion(Initializer, DestType, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5665 | /*SuppressUserConversions*/true, |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 5666 | /*AllowExplicitConversions*/ false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 5667 | /*InOverloadResolution*/ false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5668 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 5669 | allowObjCWritebackConversion); |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5670 | |
| 5671 | if (ICS.isStandard() && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5672 | ICS.Standard.Second == ICK_Writeback_Conversion) { |
| 5673 | // Objective-C ARC writeback conversion. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5674 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5675 | // We should copy unless we're passing to an argument explicitly |
| 5676 | // marked 'out'. |
| 5677 | bool ShouldCopy = true; |
| 5678 | if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 5679 | ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5680 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5681 | // If there was an lvalue adjustment, add it as a separate conversion. |
| 5682 | if (ICS.Standard.First == ICK_Array_To_Pointer || |
| 5683 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 5684 | ImplicitConversionSequence LvalueICS; |
| 5685 | LvalueICS.setStandard(); |
| 5686 | LvalueICS.Standard.setAsIdentityConversion(); |
| 5687 | LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0)); |
| 5688 | LvalueICS.Standard.First = ICS.Standard.First; |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5689 | AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5690 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5691 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5692 | AddPassByIndirectCopyRestoreStep(DestType, ShouldCopy); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5693 | } else if (ICS.isBad()) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 5694 | DeclAccessPair dap; |
Richard Smith | f032001b | 2013-06-20 02:18:31 +0000 | [diff] [blame] | 5695 | if (isLibstdcxxPointerReturnFalseHack(S, Entity, Initializer)) { |
| 5696 | AddZeroInitializationStep(Entity.getType()); |
| 5697 | } else if (Initializer->getType() == Context.OverloadTy && |
| 5698 | !S.ResolveAddressOfOverloadedFunction(Initializer, DestType, |
| 5699 | false, dap)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5700 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 5701 | else if (Initializer->getType()->isFunctionType() && |
| 5702 | isExprAnUnaddressableFunction(S, Initializer)) |
| 5703 | SetFailed(InitializationSequence::FK_AddressOfUnaddressableFunction); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 5704 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5705 | SetFailed(InitializationSequence::FK_ConversionFailed); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5706 | } else { |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5707 | AddConversionSequenceStep(ICS, DestType, TopLevelOfInitList); |
John McCall | fa27234 | 2011-06-16 23:24:51 +0000 | [diff] [blame] | 5708 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5709 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 5710 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5711 | } |
| 5712 | |
| 5713 | InitializationSequence::~InitializationSequence() { |
Davide Italiano | 67bb9f7 | 2015-07-01 21:51:58 +0000 | [diff] [blame] | 5714 | for (auto &S : Steps) |
| 5715 | S.Destroy(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5716 | } |
| 5717 | |
| 5718 | //===----------------------------------------------------------------------===// |
| 5719 | // Perform initialization |
| 5720 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5721 | static Sema::AssignmentAction |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 5722 | getAssignmentAction(const InitializedEntity &Entity, bool Diagnose = false) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5723 | switch(Entity.getKind()) { |
| 5724 | case InitializedEntity::EK_Variable: |
| 5725 | case InitializedEntity::EK_New: |
Douglas Gregor | 6dd3a6a | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 5726 | case InitializedEntity::EK_Exception: |
| 5727 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 5728 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5729 | return Sema::AA_Initializing; |
| 5730 | |
| 5731 | case InitializedEntity::EK_Parameter: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5732 | if (Entity.getDecl() && |
Douglas Gregor | 6b7f12c | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 5733 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 5734 | return Sema::AA_Sending; |
| 5735 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5736 | return Sema::AA_Passing; |
| 5737 | |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 5738 | case InitializedEntity::EK_Parameter_CF_Audited: |
| 5739 | if (Entity.getDecl() && |
| 5740 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 5741 | return Sema::AA_Sending; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5742 | |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 5743 | return !Diagnose ? Sema::AA_Passing : Sema::AA_Passing_CFAudited; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5744 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5745 | case InitializedEntity::EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 5746 | case InitializedEntity::EK_StmtExprResult: // FIXME: Not quite right. |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5747 | return Sema::AA_Returning; |
| 5748 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5749 | case InitializedEntity::EK_Temporary: |
Fariborz Jahanian | 14e9541 | 2013-07-11 19:13:34 +0000 | [diff] [blame] | 5750 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5751 | // FIXME: Can we tell apart casting vs. converting? |
| 5752 | return Sema::AA_Casting; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5753 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5754 | case InitializedEntity::EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 5755 | case InitializedEntity::EK_Binding: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 5756 | case InitializedEntity::EK_ArrayElement: |
| 5757 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 5758 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 5759 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 5760 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 5761 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5762 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5763 | return Sema::AA_Initializing; |
| 5764 | } |
| 5765 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 5766 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5767 | } |
| 5768 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5769 | /// Whether we should bind a created object as a temporary when |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5770 | /// initializing the given entity. |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5771 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5772 | switch (Entity.getKind()) { |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 5773 | case InitializedEntity::EK_ArrayElement: |
| 5774 | case InitializedEntity::EK_Member: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5775 | case InitializedEntity::EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 5776 | case InitializedEntity::EK_StmtExprResult: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5777 | case InitializedEntity::EK_New: |
| 5778 | case InitializedEntity::EK_Variable: |
| 5779 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 5780 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 5781 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 5782 | case InitializedEntity::EK_ComplexElement: |
Anders Carlsson | fcd764a | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 5783 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 5784 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 5785 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 5786 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5787 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5788 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5789 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5790 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5791 | case InitializedEntity::EK_Parameter_CF_Audited: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5792 | case InitializedEntity::EK_Temporary: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5793 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 5794 | case InitializedEntity::EK_Binding: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5795 | return true; |
| 5796 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5797 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5798 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 5799 | } |
| 5800 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5801 | /// Whether the given entity, when initialized with an object |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5802 | /// created for that initialization, requires destruction. |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5803 | static bool shouldDestroyEntity(const InitializedEntity &Entity) { |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5804 | switch (Entity.getKind()) { |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5805 | case InitializedEntity::EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 5806 | case InitializedEntity::EK_StmtExprResult: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5807 | case InitializedEntity::EK_New: |
| 5808 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 5809 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5810 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 5811 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 5812 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 5813 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 5814 | case InitializedEntity::EK_LambdaCapture: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5815 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5816 | |
Richard Smith | 27874d6 | 2013-01-08 00:08:23 +0000 | [diff] [blame] | 5817 | case InitializedEntity::EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 5818 | case InitializedEntity::EK_Binding: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5819 | case InitializedEntity::EK_Variable: |
| 5820 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5821 | case InitializedEntity::EK_Parameter_CF_Audited: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5822 | case InitializedEntity::EK_Temporary: |
| 5823 | case InitializedEntity::EK_ArrayElement: |
| 5824 | case InitializedEntity::EK_Exception: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5825 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5826 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5827 | return true; |
| 5828 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5829 | |
| 5830 | llvm_unreachable("missed an InitializedEntity kind?"); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5831 | } |
| 5832 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5833 | /// Get the location at which initialization diagnostics should appear. |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5834 | static SourceLocation getInitializationLoc(const InitializedEntity &Entity, |
| 5835 | Expr *Initializer) { |
| 5836 | switch (Entity.getKind()) { |
| 5837 | case InitializedEntity::EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 5838 | case InitializedEntity::EK_StmtExprResult: |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5839 | return Entity.getReturnLoc(); |
| 5840 | |
| 5841 | case InitializedEntity::EK_Exception: |
| 5842 | return Entity.getThrowLoc(); |
| 5843 | |
| 5844 | case InitializedEntity::EK_Variable: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 5845 | case InitializedEntity::EK_Binding: |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5846 | return Entity.getDecl()->getLocation(); |
| 5847 | |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 5848 | case InitializedEntity::EK_LambdaCapture: |
| 5849 | return Entity.getCaptureLoc(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5850 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5851 | case InitializedEntity::EK_ArrayElement: |
| 5852 | case InitializedEntity::EK_Member: |
| 5853 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5854 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5855 | case InitializedEntity::EK_Temporary: |
| 5856 | case InitializedEntity::EK_New: |
| 5857 | case InitializedEntity::EK_Base: |
| 5858 | case InitializedEntity::EK_Delegating: |
| 5859 | case InitializedEntity::EK_VectorElement: |
| 5860 | case InitializedEntity::EK_ComplexElement: |
| 5861 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 5862 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5863 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5864 | case InitializedEntity::EK_RelatedResult: |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5865 | return Initializer->getBeginLoc(); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5866 | } |
| 5867 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 5868 | } |
| 5869 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5870 | /// Make a (potentially elidable) temporary copy of the object |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5871 | /// provided by the given initializer by calling the appropriate copy |
| 5872 | /// constructor. |
| 5873 | /// |
| 5874 | /// \param S The Sema object used for type-checking. |
| 5875 | /// |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 5876 | /// \param T The type of the temporary object, which must either be |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5877 | /// the type of the initializer expression or a superclass thereof. |
| 5878 | /// |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5879 | /// \param Entity The entity being initialized. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5880 | /// |
| 5881 | /// \param CurInit The initializer expression. |
| 5882 | /// |
| 5883 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 5884 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 5885 | /// an rvalue. |
| 5886 | /// |
| 5887 | /// \returns An expression that copies the initializer expression into |
| 5888 | /// a temporary object, or an error expression if a copy could not be |
| 5889 | /// created. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5890 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 5891 | QualType T, |
| 5892 | const InitializedEntity &Entity, |
| 5893 | ExprResult CurInit, |
| 5894 | bool IsExtraneousCopy) { |
Fariborz Jahanian | 36f7f13 | 2015-01-28 22:08:10 +0000 | [diff] [blame] | 5895 | if (CurInit.isInvalid()) |
| 5896 | return CurInit; |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5897 | // Determine which class type we're copying to. |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 5898 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5899 | CXXRecordDecl *Class = nullptr; |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5900 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5901 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 5902 | if (!Class) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5903 | return CurInit; |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5904 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5905 | SourceLocation Loc = getInitializationLoc(Entity, CurInit.get()); |
Douglas Gregor | d5c231e | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 5906 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5907 | // Make sure that the type we are copying is complete. |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5908 | if (S.RequireCompleteType(Loc, T, diag::err_temp_copy_incomplete)) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5909 | return CurInit; |
Douglas Gregor | d5c231e | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 5910 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5911 | // Perform overload resolution using the class's constructors. Per |
| 5912 | // C++11 [dcl.init]p16, second bullet for class types, this initialization |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5913 | // is direct-initialization. |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 5914 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5915 | DeclContext::lookup_result Ctors = S.LookupConstructors(Class); |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5916 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5917 | OverloadCandidateSet::iterator Best; |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5918 | switch (ResolveConstructorOverload( |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 5919 | S, Loc, CurInitExpr, CandidateSet, T, Ctors, Best, |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5920 | /*CopyInitializing=*/false, /*AllowExplicit=*/true, |
| 5921 | /*OnlyListConstructors=*/false, /*IsListInit=*/false, |
| 5922 | /*SecondStepOfCopyInit=*/true)) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5923 | case OR_Success: |
| 5924 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5925 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5926 | case OR_No_Viable_Function: |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 5927 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 5928 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 5929 | : diag::err_temp_copy_no_viable) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 5930 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5931 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5932 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 5933 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5934 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5935 | return CurInit; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5936 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5937 | case OR_Ambiguous: |
| 5938 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 5939 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5940 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5941 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5942 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5943 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5944 | case OR_Deleted: |
| 5945 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 5946 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5947 | << CurInitExpr->getSourceRange(); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5948 | S.NoteDeletedFunction(Best->Function); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5949 | return ExprError(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5950 | } |
| 5951 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5952 | bool HadMultipleCandidates = CandidateSet.size() > 1; |
| 5953 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5954 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 5955 | SmallVector<Expr*, 8> ConstructorArgs; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5956 | CurInit.get(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5957 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 5958 | S.CheckConstructorAccess(Loc, Constructor, Best->FoundDecl, Entity, |
| 5959 | IsExtraneousCopy); |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5960 | |
| 5961 | if (IsExtraneousCopy) { |
| 5962 | // If this is a totally extraneous copy for C++03 reference |
| 5963 | // binding purposes, just return the original initialization |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 5964 | // expression. We don't generate an (elided) copy operation here |
| 5965 | // because doing so would require us to pass down a flag to avoid |
| 5966 | // infinite recursion, where each step adds another extraneous, |
| 5967 | // elidable copy. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5968 | |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 5969 | // Instantiate the default arguments of any extra parameters in |
| 5970 | // the selected copy constructor, as if we were going to create a |
| 5971 | // proper call to the copy constructor. |
| 5972 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 5973 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 5974 | if (S.RequireCompleteType(Loc, Parm->getType(), |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5975 | diag::err_call_incomplete_argument)) |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 5976 | break; |
| 5977 | |
| 5978 | // Build the default argument expression; we don't actually care |
| 5979 | // if this succeeds or not, because this routine will complain |
| 5980 | // if there was a problem. |
| 5981 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 5982 | } |
| 5983 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5984 | return CurInitExpr; |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5985 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5986 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5987 | // Determine the arguments required to actually perform the |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5988 | // constructor call (we might have derived-to-base conversions, or |
| 5989 | // the copy constructor may have default arguments). |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5990 | if (S.CompleteConstructorCall(Constructor, CurInitExpr, Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5991 | return ExprError(); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5992 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5993 | // C++0x [class.copy]p32: |
| 5994 | // When certain criteria are met, an implementation is allowed to |
| 5995 | // omit the copy/move construction of a class object, even if the |
| 5996 | // copy/move constructor and/or destructor for the object have |
| 5997 | // side effects. [...] |
| 5998 | // - when a temporary class object that has not been bound to a |
| 5999 | // reference (12.2) would be copied/moved to a class object |
| 6000 | // with the same cv-unqualified type, the copy/move operation |
| 6001 | // can be omitted by constructing the temporary object |
| 6002 | // directly into the target of the omitted copy/move |
| 6003 | // |
| 6004 | // Note that the other three bullets are handled elsewhere. Copy |
| 6005 | // elision for return statements and throw expressions are handled as part |
| 6006 | // of constructor initialization, while copy elision for exception handlers |
| 6007 | // is handled by the run-time. |
| 6008 | // |
| 6009 | // FIXME: If the function parameter is not the same type as the temporary, we |
| 6010 | // should still be able to elide the copy, but we don't have a way to |
| 6011 | // represent in the AST how much should be elided in this case. |
| 6012 | bool Elidable = |
| 6013 | CurInitExpr->isTemporaryObject(S.Context, Class) && |
| 6014 | S.Context.hasSameUnqualifiedType( |
| 6015 | Best->Function->getParamDecl(0)->getType().getNonReferenceType(), |
| 6016 | CurInitExpr->getType()); |
| 6017 | |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 6018 | // Actually perform the constructor call. |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 6019 | CurInit = S.BuildCXXConstructExpr(Loc, T, Best->FoundDecl, Constructor, |
| 6020 | Elidable, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6021 | ConstructorArgs, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 6022 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6023 | /*ListInit*/ false, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6024 | /*StdInitListInit*/ false, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 6025 | /*ZeroInit*/ false, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 6026 | CXXConstructExpr::CK_Complete, |
| 6027 | SourceRange()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6028 | |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 6029 | // If we're supposed to bind temporaries, do so. |
| 6030 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6031 | CurInit = S.MaybeBindToTemporary(CurInit.getAs<Expr>()); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6032 | return CurInit; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6033 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6034 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6035 | /// Check whether elidable copy construction for binding a reference to |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6036 | /// a temporary would have succeeded if we were building in C++98 mode, for |
| 6037 | /// -Wc++98-compat. |
| 6038 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 6039 | const InitializedEntity &Entity, |
| 6040 | Expr *CurInitExpr) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6041 | assert(S.getLangOpts().CPlusPlus11); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6042 | |
| 6043 | const RecordType *Record = CurInitExpr->getType()->getAs<RecordType>(); |
| 6044 | if (!Record) |
| 6045 | return; |
| 6046 | |
| 6047 | SourceLocation Loc = getInitializationLoc(Entity, CurInitExpr); |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 6048 | if (S.Diags.isIgnored(diag::warn_cxx98_compat_temp_copy, Loc)) |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6049 | return; |
| 6050 | |
| 6051 | // Find constructors which would have been considered. |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 6052 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 6053 | DeclContext::lookup_result Ctors = |
| 6054 | S.LookupConstructors(cast<CXXRecordDecl>(Record->getDecl())); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6055 | |
| 6056 | // Perform overload resolution. |
| 6057 | OverloadCandidateSet::iterator Best; |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 6058 | OverloadingResult OR = ResolveConstructorOverload( |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 6059 | S, Loc, CurInitExpr, CandidateSet, CurInitExpr->getType(), Ctors, Best, |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 6060 | /*CopyInitializing=*/false, /*AllowExplicit=*/true, |
| 6061 | /*OnlyListConstructors=*/false, /*IsListInit=*/false, |
| 6062 | /*SecondStepOfCopyInit=*/true); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6063 | |
| 6064 | PartialDiagnostic Diag = S.PDiag(diag::warn_cxx98_compat_temp_copy) |
| 6065 | << OR << (int)Entity.getKind() << CurInitExpr->getType() |
| 6066 | << CurInitExpr->getSourceRange(); |
| 6067 | |
| 6068 | switch (OR) { |
| 6069 | case OR_Success: |
| 6070 | S.CheckConstructorAccess(Loc, cast<CXXConstructorDecl>(Best->Function), |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6071 | Best->FoundDecl, Entity, Diag); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6072 | // FIXME: Check default arguments as far as that's possible. |
| 6073 | break; |
| 6074 | |
| 6075 | case OR_No_Viable_Function: |
| 6076 | S.Diag(Loc, Diag); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6077 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6078 | break; |
| 6079 | |
| 6080 | case OR_Ambiguous: |
| 6081 | S.Diag(Loc, Diag); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6082 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6083 | break; |
| 6084 | |
| 6085 | case OR_Deleted: |
| 6086 | S.Diag(Loc, Diag); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6087 | S.NoteDeletedFunction(Best->Function); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6088 | break; |
| 6089 | } |
| 6090 | } |
| 6091 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6092 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 6093 | const InitializedEntity &Entity) { |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6094 | if (Entity.isParameterKind() && Entity.getDecl()) { |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6095 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 6096 | return; |
| 6097 | |
| 6098 | if (Entity.getDecl()->getDeclName()) |
| 6099 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 6100 | << Entity.getDecl()->getDeclName(); |
| 6101 | else |
| 6102 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 6103 | } |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 6104 | else if (Entity.getKind() == InitializedEntity::EK_RelatedResult && |
| 6105 | Entity.getMethodDecl()) |
| 6106 | S.Diag(Entity.getMethodDecl()->getLocation(), |
| 6107 | diag::note_method_return_type_change) |
| 6108 | << Entity.getMethodDecl()->getDeclName(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6109 | } |
| 6110 | |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 6111 | /// Returns true if the parameters describe a constructor initialization of |
| 6112 | /// an explicit temporary object, e.g. "Point(x, y)". |
| 6113 | static bool isExplicitTemporary(const InitializedEntity &Entity, |
| 6114 | const InitializationKind &Kind, |
| 6115 | unsigned NumArgs) { |
| 6116 | switch (Entity.getKind()) { |
| 6117 | case InitializedEntity::EK_Temporary: |
| 6118 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 6119 | case InitializedEntity::EK_RelatedResult: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 6120 | break; |
| 6121 | default: |
| 6122 | return false; |
| 6123 | } |
| 6124 | |
| 6125 | switch (Kind.getKind()) { |
| 6126 | case InitializationKind::IK_DirectList: |
| 6127 | return true; |
| 6128 | // FIXME: Hack to work around cast weirdness. |
| 6129 | case InitializationKind::IK_Direct: |
| 6130 | case InitializationKind::IK_Value: |
| 6131 | return NumArgs != 1; |
| 6132 | default: |
| 6133 | return false; |
| 6134 | } |
| 6135 | } |
| 6136 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6137 | static ExprResult |
| 6138 | PerformConstructorInitialization(Sema &S, |
| 6139 | const InitializedEntity &Entity, |
| 6140 | const InitializationKind &Kind, |
| 6141 | MultiExprArg Args, |
| 6142 | const InitializationSequence::Step& Step, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6143 | bool &ConstructorInitRequiresZeroInit, |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 6144 | bool IsListInitialization, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6145 | bool IsStdInitListInitialization, |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 6146 | SourceLocation LBraceLoc, |
| 6147 | SourceLocation RBraceLoc) { |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6148 | unsigned NumArgs = Args.size(); |
| 6149 | CXXConstructorDecl *Constructor |
| 6150 | = cast<CXXConstructorDecl>(Step.Function.Function); |
| 6151 | bool HadMultipleCandidates = Step.Function.HadMultipleCandidates; |
| 6152 | |
| 6153 | // Build a call to the selected constructor. |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 6154 | SmallVector<Expr*, 8> ConstructorArgs; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6155 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 6156 | ? Kind.getEqualLoc() |
| 6157 | : Kind.getLocation(); |
| 6158 | |
| 6159 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 6160 | // Force even a trivial, implicit default constructor to be |
| 6161 | // semantically checked. We do this explicitly because we don't build |
| 6162 | // the definition for completely trivial constructors. |
Matt Beaumont-Gay | 47ff122 | 2012-02-24 08:37:56 +0000 | [diff] [blame] | 6163 | assert(Constructor->getParent() && "No parent class for constructor."); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6164 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Douglas Gregor | f704ade | 2012-02-24 07:48:37 +0000 | [diff] [blame] | 6165 | Constructor->isTrivial() && !Constructor->isUsed(false)) |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6166 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 6167 | } |
| 6168 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6169 | ExprResult CurInit((Expr *)nullptr); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6170 | |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6171 | // C++ [over.match.copy]p1: |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6172 | // - When initializing a temporary to be bound to the first parameter |
| 6173 | // of a constructor that takes a reference to possibly cv-qualified |
| 6174 | // T as its first argument, called with a single argument in the |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6175 | // context of direct-initialization, explicit conversion functions |
| 6176 | // are also considered. |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 6177 | bool AllowExplicitConv = |
| 6178 | Kind.AllowExplicit() && !Kind.isCopyInit() && Args.size() == 1 && |
| 6179 | hasCopyOrMoveCtorParam(S.Context, |
| 6180 | getConstructorInfo(Step.Function.FoundDecl)); |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6181 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6182 | // Determine the arguments required to actually perform the constructor |
| 6183 | // call. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6184 | if (S.CompleteConstructorCall(Constructor, Args, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6185 | Loc, ConstructorArgs, |
Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 6186 | AllowExplicitConv, |
| 6187 | IsListInitialization)) |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6188 | return ExprError(); |
| 6189 | |
| 6190 | |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 6191 | if (isExplicitTemporary(Entity, Kind, NumArgs)) { |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6192 | // An explicitly-constructed temporary, e.g., X(1, 2). |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 6193 | if (S.DiagnoseUseOfDecl(Constructor, Loc)) |
| 6194 | return ExprError(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6195 | |
| 6196 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 6197 | if (!TSInfo) |
| 6198 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
Vedant Kumar | c9a9531 | 2018-11-19 20:10:21 +0000 | [diff] [blame] | 6199 | SourceRange ParenOrBraceRange = |
| 6200 | (Kind.getKind() == InitializationKind::IK_DirectList) |
| 6201 | ? SourceRange(LBraceLoc, RBraceLoc) |
| 6202 | : Kind.getParenOrBraceRange(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6203 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6204 | if (auto *Shadow = dyn_cast<ConstructorUsingShadowDecl>( |
Richard Smith | 80a4702 | 2016-06-29 01:10:27 +0000 | [diff] [blame] | 6205 | Step.Function.FoundDecl.getDecl())) { |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6206 | Constructor = S.findInheritingConstructor(Loc, Constructor, Shadow); |
Richard Smith | 80a4702 | 2016-06-29 01:10:27 +0000 | [diff] [blame] | 6207 | if (S.DiagnoseUseOfDecl(Constructor, Loc)) |
| 6208 | return ExprError(); |
| 6209 | } |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6210 | S.MarkFunctionReferenced(Loc, Constructor); |
| 6211 | |
Bruno Ricci | ddb8f6b | 2018-12-22 14:39:30 +0000 | [diff] [blame] | 6212 | CurInit = CXXTemporaryObjectExpr::Create( |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 6213 | S.Context, Constructor, |
| 6214 | Entity.getType().getNonLValueExprType(S.Context), TSInfo, |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 6215 | ConstructorArgs, ParenOrBraceRange, HadMultipleCandidates, |
| 6216 | IsListInitialization, IsStdInitListInitialization, |
| 6217 | ConstructorInitRequiresZeroInit); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6218 | } else { |
| 6219 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 6220 | CXXConstructExpr::CK_Complete; |
| 6221 | |
| 6222 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 6223 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
| 6224 | CXXConstructExpr::CK_VirtualBase : |
| 6225 | CXXConstructExpr::CK_NonVirtualBase; |
| 6226 | } else if (Entity.getKind() == InitializedEntity::EK_Delegating) { |
| 6227 | ConstructKind = CXXConstructExpr::CK_Delegating; |
| 6228 | } |
| 6229 | |
Peter Collingbourne | b8d17e7 | 2014-02-22 02:59:41 +0000 | [diff] [blame] | 6230 | // Only get the parenthesis or brace range if it is a list initialization or |
| 6231 | // direct construction. |
| 6232 | SourceRange ParenOrBraceRange; |
| 6233 | if (IsListInitialization) |
| 6234 | ParenOrBraceRange = SourceRange(LBraceLoc, RBraceLoc); |
| 6235 | else if (Kind.getKind() == InitializationKind::IK_Direct) |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 6236 | ParenOrBraceRange = Kind.getParenOrBraceRange(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6237 | |
| 6238 | // If the entity allows NRVO, mark the construction as elidable |
| 6239 | // unconditionally. |
| 6240 | if (Entity.allowsNRVO()) |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 6241 | CurInit = S.BuildCXXConstructExpr(Loc, Step.Type, |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 6242 | Step.Function.FoundDecl, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6243 | Constructor, /*Elidable=*/true, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6244 | ConstructorArgs, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6245 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6246 | IsListInitialization, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6247 | IsStdInitListInitialization, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6248 | ConstructorInitRequiresZeroInit, |
| 6249 | ConstructKind, |
Peter Collingbourne | b8d17e7 | 2014-02-22 02:59:41 +0000 | [diff] [blame] | 6250 | ParenOrBraceRange); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6251 | else |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 6252 | CurInit = S.BuildCXXConstructExpr(Loc, Step.Type, |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 6253 | Step.Function.FoundDecl, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6254 | Constructor, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6255 | ConstructorArgs, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6256 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6257 | IsListInitialization, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6258 | IsStdInitListInitialization, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6259 | ConstructorInitRequiresZeroInit, |
| 6260 | ConstructKind, |
Peter Collingbourne | b8d17e7 | 2014-02-22 02:59:41 +0000 | [diff] [blame] | 6261 | ParenOrBraceRange); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6262 | } |
| 6263 | if (CurInit.isInvalid()) |
| 6264 | return ExprError(); |
| 6265 | |
| 6266 | // Only check access if all of that succeeded. |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6267 | S.CheckConstructorAccess(Loc, Constructor, Step.Function.FoundDecl, Entity); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 6268 | if (S.DiagnoseUseOfDecl(Step.Function.FoundDecl, Loc)) |
| 6269 | return ExprError(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6270 | |
| 6271 | if (shouldBindAsTemporary(Entity)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6272 | CurInit = S.MaybeBindToTemporary(CurInit.get()); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6273 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6274 | return CurInit; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6275 | } |
| 6276 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6277 | namespace { |
| 6278 | enum LifetimeKind { |
| 6279 | /// The lifetime of a temporary bound to this entity ends at the end of the |
| 6280 | /// full-expression, and that's (probably) fine. |
| 6281 | LK_FullExpression, |
| 6282 | |
| 6283 | /// The lifetime of a temporary bound to this entity is extended to the |
| 6284 | /// lifeitme of the entity itself. |
| 6285 | LK_Extended, |
| 6286 | |
| 6287 | /// The lifetime of a temporary bound to this entity probably ends too soon, |
| 6288 | /// because the entity is allocated in a new-expression. |
| 6289 | LK_New, |
| 6290 | |
| 6291 | /// The lifetime of a temporary bound to this entity ends too soon, because |
| 6292 | /// the entity is a return object. |
| 6293 | LK_Return, |
| 6294 | |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 6295 | /// The lifetime of a temporary bound to this entity ends too soon, because |
| 6296 | /// the entity is the result of a statement expression. |
| 6297 | LK_StmtExprResult, |
| 6298 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6299 | /// This is a mem-initializer: if it would extend a temporary (other than via |
| 6300 | /// a default member initializer), the program is ill-formed. |
| 6301 | LK_MemInitializer, |
| 6302 | }; |
| 6303 | using LifetimeResult = |
| 6304 | llvm::PointerIntPair<const InitializedEntity *, 3, LifetimeKind>; |
| 6305 | } |
| 6306 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6307 | /// Determine the declaration which an initialized entity ultimately refers to, |
| 6308 | /// for the purpose of lifetime-extending a temporary bound to a reference in |
| 6309 | /// the initialization of \p Entity. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6310 | static LifetimeResult getEntityLifetime( |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 6311 | const InitializedEntity *Entity, |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6312 | const InitializedEntity *InitField = nullptr) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6313 | // C++11 [class.temporary]p5: |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 6314 | switch (Entity->getKind()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6315 | case InitializedEntity::EK_Variable: |
| 6316 | // The temporary [...] persists for the lifetime of the reference |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6317 | return {Entity, LK_Extended}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6318 | |
| 6319 | case InitializedEntity::EK_Member: |
| 6320 | // For subobjects, we look at the complete object. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 6321 | if (Entity->getParent()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6322 | return getEntityLifetime(Entity->getParent(), Entity); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6323 | |
| 6324 | // except: |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6325 | // C++17 [class.base.init]p8: |
| 6326 | // A temporary expression bound to a reference member in a |
| 6327 | // mem-initializer is ill-formed. |
| 6328 | // C++17 [class.base.init]p11: |
| 6329 | // A temporary expression bound to a reference member from a |
| 6330 | // default member initializer is ill-formed. |
| 6331 | // |
| 6332 | // The context of p11 and its example suggest that it's only the use of a |
| 6333 | // default member initializer from a constructor that makes the program |
| 6334 | // ill-formed, not its mere existence, and that it can even be used by |
| 6335 | // aggregate initialization. |
| 6336 | return {Entity, Entity->isDefaultMemberInitializer() ? LK_Extended |
| 6337 | : LK_MemInitializer}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6338 | |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 6339 | case InitializedEntity::EK_Binding: |
Richard Smith | 3997b1b | 2016-08-12 01:55:21 +0000 | [diff] [blame] | 6340 | // Per [dcl.decomp]p3, the binding is treated as a variable of reference |
| 6341 | // type. |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6342 | return {Entity, LK_Extended}; |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 6343 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6344 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6345 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6346 | // -- A temporary bound to a reference parameter in a function call |
| 6347 | // persists until the completion of the full-expression containing |
| 6348 | // the call. |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6349 | return {nullptr, LK_FullExpression}; |
| 6350 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6351 | case InitializedEntity::EK_Result: |
| 6352 | // -- The lifetime of a temporary bound to the returned value in a |
| 6353 | // function return statement is not extended; the temporary is |
| 6354 | // destroyed at the end of the full-expression in the return statement. |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6355 | return {nullptr, LK_Return}; |
| 6356 | |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 6357 | case InitializedEntity::EK_StmtExprResult: |
| 6358 | // FIXME: Should we lifetime-extend through the result of a statement |
| 6359 | // expression? |
| 6360 | return {nullptr, LK_StmtExprResult}; |
| 6361 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6362 | case InitializedEntity::EK_New: |
| 6363 | // -- A temporary bound to a reference in a new-initializer persists |
| 6364 | // until the completion of the full-expression containing the |
| 6365 | // new-initializer. |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6366 | return {nullptr, LK_New}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6367 | |
| 6368 | case InitializedEntity::EK_Temporary: |
| 6369 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 6370 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6371 | // We don't yet know the storage duration of the surrounding temporary. |
| 6372 | // Assume it's got full-expression duration for now, it will patch up our |
| 6373 | // storage duration if that's not correct. |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6374 | return {nullptr, LK_FullExpression}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6375 | |
| 6376 | case InitializedEntity::EK_ArrayElement: |
| 6377 | // For subobjects, we look at the complete object. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6378 | return getEntityLifetime(Entity->getParent(), InitField); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6379 | |
| 6380 | case InitializedEntity::EK_Base: |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 6381 | // For subobjects, we look at the complete object. |
| 6382 | if (Entity->getParent()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6383 | return getEntityLifetime(Entity->getParent(), InitField); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6384 | return {InitField, LK_MemInitializer}; |
| 6385 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6386 | case InitializedEntity::EK_Delegating: |
| 6387 | // We can reach this case for aggregate initialization in a constructor: |
| 6388 | // struct A { int &&r; }; |
| 6389 | // struct B : A { B() : A{0} {} }; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6390 | // In this case, use the outermost field decl as the context. |
| 6391 | return {InitField, LK_MemInitializer}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6392 | |
| 6393 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 6394 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6395 | case InitializedEntity::EK_LambdaCapture: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6396 | case InitializedEntity::EK_VectorElement: |
| 6397 | case InitializedEntity::EK_ComplexElement: |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6398 | return {nullptr, LK_FullExpression}; |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6399 | |
| 6400 | case InitializedEntity::EK_Exception: |
| 6401 | // FIXME: Can we diagnose lifetime problems with exceptions? |
| 6402 | return {nullptr, LK_FullExpression}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6403 | } |
Benjamin Kramer | cabc882 | 2013-06-05 15:37:50 +0000 | [diff] [blame] | 6404 | llvm_unreachable("unknown entity kind"); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6405 | } |
| 6406 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6407 | namespace { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6408 | enum ReferenceKind { |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6409 | /// Lifetime would be extended by a reference binding to a temporary. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6410 | RK_ReferenceBinding, |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6411 | /// Lifetime would be extended by a std::initializer_list object binding to |
| 6412 | /// its backing array. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6413 | RK_StdInitializerList, |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6414 | }; |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6415 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6416 | /// A temporary or local variable. This will be one of: |
| 6417 | /// * A MaterializeTemporaryExpr. |
| 6418 | /// * A DeclRefExpr whose declaration is a local. |
| 6419 | /// * An AddrLabelExpr. |
| 6420 | /// * A BlockExpr for a block with captures. |
| 6421 | using Local = Expr*; |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6422 | |
| 6423 | /// Expressions we stepped over when looking for the local state. Any steps |
| 6424 | /// that would inhibit lifetime extension or take us out of subexpressions of |
| 6425 | /// the initializer are included. |
| 6426 | struct IndirectLocalPathEntry { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6427 | enum EntryKind { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6428 | DefaultInit, |
| 6429 | AddressOf, |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6430 | VarInit, |
| 6431 | LValToRVal, |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6432 | LifetimeBoundCall, |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6433 | } Kind; |
| 6434 | Expr *E; |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6435 | const Decl *D = nullptr; |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6436 | IndirectLocalPathEntry() {} |
| 6437 | IndirectLocalPathEntry(EntryKind K, Expr *E) : Kind(K), E(E) {} |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6438 | IndirectLocalPathEntry(EntryKind K, Expr *E, const Decl *D) |
| 6439 | : Kind(K), E(E), D(D) {} |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6440 | }; |
| 6441 | |
| 6442 | using IndirectLocalPath = llvm::SmallVectorImpl<IndirectLocalPathEntry>; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6443 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6444 | struct RevertToOldSizeRAII { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6445 | IndirectLocalPath &Path; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6446 | unsigned OldSize = Path.size(); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6447 | RevertToOldSizeRAII(IndirectLocalPath &Path) : Path(Path) {} |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6448 | ~RevertToOldSizeRAII() { Path.resize(OldSize); } |
| 6449 | }; |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6450 | |
| 6451 | using LocalVisitor = llvm::function_ref<bool(IndirectLocalPath &Path, Local L, |
| 6452 | ReferenceKind RK)>; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6453 | } |
| 6454 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6455 | static bool isVarOnPath(IndirectLocalPath &Path, VarDecl *VD) { |
| 6456 | for (auto E : Path) |
| 6457 | if (E.Kind == IndirectLocalPathEntry::VarInit && E.D == VD) |
| 6458 | return true; |
| 6459 | return false; |
| 6460 | } |
| 6461 | |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6462 | static bool pathContainsInit(IndirectLocalPath &Path) { |
Fangrui Song | 3117b17 | 2018-10-20 17:53:42 +0000 | [diff] [blame] | 6463 | return llvm::any_of(Path, [=](IndirectLocalPathEntry E) { |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6464 | return E.Kind == IndirectLocalPathEntry::DefaultInit || |
| 6465 | E.Kind == IndirectLocalPathEntry::VarInit; |
| 6466 | }); |
| 6467 | } |
| 6468 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6469 | static void visitLocalsRetainedByInitializer(IndirectLocalPath &Path, |
| 6470 | Expr *Init, LocalVisitor Visit, |
| 6471 | bool RevisitSubinits); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6472 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6473 | static void visitLocalsRetainedByReferenceBinding(IndirectLocalPath &Path, |
| 6474 | Expr *Init, ReferenceKind RK, |
| 6475 | LocalVisitor Visit); |
| 6476 | |
| 6477 | static bool implicitObjectParamIsLifetimeBound(const FunctionDecl *FD) { |
| 6478 | const TypeSourceInfo *TSI = FD->getTypeSourceInfo(); |
| 6479 | if (!TSI) |
| 6480 | return false; |
Martin Storsjo | d03fa99 | 2018-08-02 18:12:08 +0000 | [diff] [blame] | 6481 | // Don't declare this variable in the second operand of the for-statement; |
| 6482 | // GCC miscompiles that by ending its lifetime before evaluating the |
| 6483 | // third operand. See gcc.gnu.org/PR86769. |
| 6484 | AttributedTypeLoc ATL; |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6485 | for (TypeLoc TL = TSI->getTypeLoc(); |
Martin Storsjo | d03fa99 | 2018-08-02 18:12:08 +0000 | [diff] [blame] | 6486 | (ATL = TL.getAsAdjusted<AttributedTypeLoc>()); |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6487 | TL = ATL.getModifiedLoc()) { |
Richard Smith | e43e2b3 | 2018-08-20 21:47:29 +0000 | [diff] [blame] | 6488 | if (ATL.getAttrAs<LifetimeBoundAttr>()) |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6489 | return true; |
| 6490 | } |
| 6491 | return false; |
| 6492 | } |
| 6493 | |
| 6494 | static void visitLifetimeBoundArguments(IndirectLocalPath &Path, Expr *Call, |
| 6495 | LocalVisitor Visit) { |
| 6496 | const FunctionDecl *Callee; |
| 6497 | ArrayRef<Expr*> Args; |
| 6498 | |
| 6499 | if (auto *CE = dyn_cast<CallExpr>(Call)) { |
| 6500 | Callee = CE->getDirectCallee(); |
| 6501 | Args = llvm::makeArrayRef(CE->getArgs(), CE->getNumArgs()); |
| 6502 | } else { |
| 6503 | auto *CCE = cast<CXXConstructExpr>(Call); |
| 6504 | Callee = CCE->getConstructor(); |
| 6505 | Args = llvm::makeArrayRef(CCE->getArgs(), CCE->getNumArgs()); |
| 6506 | } |
| 6507 | if (!Callee) |
| 6508 | return; |
| 6509 | |
| 6510 | Expr *ObjectArg = nullptr; |
| 6511 | if (isa<CXXOperatorCallExpr>(Call) && Callee->isCXXInstanceMember()) { |
| 6512 | ObjectArg = Args[0]; |
| 6513 | Args = Args.slice(1); |
| 6514 | } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(Call)) { |
| 6515 | ObjectArg = MCE->getImplicitObjectArgument(); |
| 6516 | } |
| 6517 | |
| 6518 | auto VisitLifetimeBoundArg = [&](const Decl *D, Expr *Arg) { |
| 6519 | Path.push_back({IndirectLocalPathEntry::LifetimeBoundCall, Arg, D}); |
| 6520 | if (Arg->isGLValue()) |
| 6521 | visitLocalsRetainedByReferenceBinding(Path, Arg, RK_ReferenceBinding, |
| 6522 | Visit); |
| 6523 | else |
| 6524 | visitLocalsRetainedByInitializer(Path, Arg, Visit, true); |
| 6525 | Path.pop_back(); |
| 6526 | }; |
| 6527 | |
| 6528 | if (ObjectArg && implicitObjectParamIsLifetimeBound(Callee)) |
| 6529 | VisitLifetimeBoundArg(Callee, ObjectArg); |
| 6530 | |
| 6531 | for (unsigned I = 0, |
| 6532 | N = std::min<unsigned>(Callee->getNumParams(), Args.size()); |
| 6533 | I != N; ++I) { |
| 6534 | if (Callee->getParamDecl(I)->hasAttr<LifetimeBoundAttr>()) |
| 6535 | VisitLifetimeBoundArg(Callee->getParamDecl(I), Args[I]); |
| 6536 | } |
| 6537 | } |
| 6538 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6539 | /// Visit the locals that would be reachable through a reference bound to the |
| 6540 | /// glvalue expression \c Init. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6541 | static void visitLocalsRetainedByReferenceBinding(IndirectLocalPath &Path, |
| 6542 | Expr *Init, ReferenceKind RK, |
| 6543 | LocalVisitor Visit) { |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6544 | RevertToOldSizeRAII RAII(Path); |
| 6545 | |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 6546 | // Walk past any constructs which we can lifetime-extend across. |
| 6547 | Expr *Old; |
| 6548 | do { |
| 6549 | Old = Init; |
| 6550 | |
Bill Wendling | 7c44da2 | 2018-10-31 03:48:47 +0000 | [diff] [blame] | 6551 | if (auto *FE = dyn_cast<FullExpr>(Init)) |
| 6552 | Init = FE->getSubExpr(); |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6553 | |
Richard Smith | dbc8249 | 2015-01-10 01:28:13 +0000 | [diff] [blame] | 6554 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6555 | // If this is just redundant braces around an initializer, step over it. |
| 6556 | if (ILE->isTransparent()) |
Richard Smith | dbc8249 | 2015-01-10 01:28:13 +0000 | [diff] [blame] | 6557 | Init = ILE->getInit(0); |
Richard Smith | dbc8249 | 2015-01-10 01:28:13 +0000 | [diff] [blame] | 6558 | } |
| 6559 | |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 6560 | // Step over any subobject adjustments; we may have a materialized |
| 6561 | // temporary inside them. |
Richard Smith | 4baaa5a | 2016-12-03 01:14:32 +0000 | [diff] [blame] | 6562 | Init = const_cast<Expr *>(Init->skipRValueSubobjectAdjustments()); |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 6563 | |
| 6564 | // Per current approach for DR1376, look through casts to reference type |
| 6565 | // when performing lifetime extension. |
| 6566 | if (CastExpr *CE = dyn_cast<CastExpr>(Init)) |
| 6567 | if (CE->getSubExpr()->isGLValue()) |
| 6568 | Init = CE->getSubExpr(); |
| 6569 | |
Richard Smith | b3189a1 | 2016-12-05 07:49:14 +0000 | [diff] [blame] | 6570 | // Per the current approach for DR1299, look through array element access |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6571 | // on array glvalues when performing lifetime extension. |
| 6572 | if (auto *ASE = dyn_cast<ArraySubscriptExpr>(Init)) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6573 | Init = ASE->getBase(); |
| 6574 | auto *ICE = dyn_cast<ImplicitCastExpr>(Init); |
| 6575 | if (ICE && ICE->getCastKind() == CK_ArrayToPointerDecay) |
| 6576 | Init = ICE->getSubExpr(); |
| 6577 | else |
| 6578 | // We can't lifetime extend through this but we might still find some |
| 6579 | // retained temporaries. |
| 6580 | return visitLocalsRetainedByInitializer(Path, Init, Visit, true); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6581 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6582 | |
| 6583 | // Step into CXXDefaultInitExprs so we can diagnose cases where a |
| 6584 | // constructor inherits one as an implicit mem-initializer. |
| 6585 | if (auto *DIE = dyn_cast<CXXDefaultInitExpr>(Init)) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6586 | Path.push_back( |
| 6587 | {IndirectLocalPathEntry::DefaultInit, DIE, DIE->getField()}); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6588 | Init = DIE->getExpr(); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6589 | } |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 6590 | } while (Init != Old); |
| 6591 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6592 | if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(Init)) { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6593 | if (Visit(Path, Local(MTE), RK)) |
| 6594 | visitLocalsRetainedByInitializer(Path, MTE->GetTemporaryExpr(), Visit, |
| 6595 | true); |
| 6596 | } |
| 6597 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6598 | if (isa<CallExpr>(Init)) |
| 6599 | return visitLifetimeBoundArguments(Path, Init, Visit); |
| 6600 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6601 | switch (Init->getStmtClass()) { |
| 6602 | case Stmt::DeclRefExprClass: { |
| 6603 | // If we find the name of a local non-reference parameter, we could have a |
| 6604 | // lifetime problem. |
| 6605 | auto *DRE = cast<DeclRefExpr>(Init); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6606 | auto *VD = dyn_cast<VarDecl>(DRE->getDecl()); |
| 6607 | if (VD && VD->hasLocalStorage() && |
| 6608 | !DRE->refersToEnclosingVariableOrCapture()) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6609 | if (!VD->getType()->isReferenceType()) { |
| 6610 | Visit(Path, Local(DRE), RK); |
| 6611 | } else if (isa<ParmVarDecl>(DRE->getDecl())) { |
| 6612 | // The lifetime of a reference parameter is unknown; assume it's OK |
| 6613 | // for now. |
| 6614 | break; |
| 6615 | } else if (VD->getInit() && !isVarOnPath(Path, VD)) { |
| 6616 | Path.push_back({IndirectLocalPathEntry::VarInit, DRE, VD}); |
| 6617 | visitLocalsRetainedByReferenceBinding(Path, VD->getInit(), |
| 6618 | RK_ReferenceBinding, Visit); |
| 6619 | } |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6620 | } |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6621 | break; |
| 6622 | } |
| 6623 | |
| 6624 | case Stmt::UnaryOperatorClass: { |
| 6625 | // The only unary operator that make sense to handle here |
| 6626 | // is Deref. All others don't resolve to a "name." This includes |
| 6627 | // handling all sorts of rvalues passed to a unary operator. |
| 6628 | const UnaryOperator *U = cast<UnaryOperator>(Init); |
| 6629 | if (U->getOpcode() == UO_Deref) |
| 6630 | visitLocalsRetainedByInitializer(Path, U->getSubExpr(), Visit, true); |
| 6631 | break; |
| 6632 | } |
| 6633 | |
| 6634 | case Stmt::OMPArraySectionExprClass: { |
| 6635 | visitLocalsRetainedByInitializer( |
| 6636 | Path, cast<OMPArraySectionExpr>(Init)->getBase(), Visit, true); |
| 6637 | break; |
| 6638 | } |
| 6639 | |
| 6640 | case Stmt::ConditionalOperatorClass: |
| 6641 | case Stmt::BinaryConditionalOperatorClass: { |
| 6642 | auto *C = cast<AbstractConditionalOperator>(Init); |
| 6643 | if (!C->getTrueExpr()->getType()->isVoidType()) |
| 6644 | visitLocalsRetainedByReferenceBinding(Path, C->getTrueExpr(), RK, Visit); |
| 6645 | if (!C->getFalseExpr()->getType()->isVoidType()) |
| 6646 | visitLocalsRetainedByReferenceBinding(Path, C->getFalseExpr(), RK, Visit); |
| 6647 | break; |
| 6648 | } |
| 6649 | |
| 6650 | // FIXME: Visit the left-hand side of an -> or ->*. |
| 6651 | |
| 6652 | default: |
| 6653 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6654 | } |
| 6655 | } |
| 6656 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6657 | /// Visit the locals that would be reachable through an object initialized by |
| 6658 | /// the prvalue expression \c Init. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6659 | static void visitLocalsRetainedByInitializer(IndirectLocalPath &Path, |
| 6660 | Expr *Init, LocalVisitor Visit, |
| 6661 | bool RevisitSubinits) { |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6662 | RevertToOldSizeRAII RAII(Path); |
| 6663 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6664 | Expr *Old; |
| 6665 | do { |
| 6666 | Old = Init; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6667 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6668 | // Step into CXXDefaultInitExprs so we can diagnose cases where a |
| 6669 | // constructor inherits one as an implicit mem-initializer. |
| 6670 | if (auto *DIE = dyn_cast<CXXDefaultInitExpr>(Init)) { |
| 6671 | Path.push_back({IndirectLocalPathEntry::DefaultInit, DIE, DIE->getField()}); |
| 6672 | Init = DIE->getExpr(); |
| 6673 | } |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6674 | |
Bill Wendling | 7c44da2 | 2018-10-31 03:48:47 +0000 | [diff] [blame] | 6675 | if (auto *FE = dyn_cast<FullExpr>(Init)) |
| 6676 | Init = FE->getSubExpr(); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6677 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6678 | // Dig out the expression which constructs the extended temporary. |
| 6679 | Init = const_cast<Expr *>(Init->skipRValueSubobjectAdjustments()); |
| 6680 | |
| 6681 | if (CXXBindTemporaryExpr *BTE = dyn_cast<CXXBindTemporaryExpr>(Init)) |
| 6682 | Init = BTE->getSubExpr(); |
| 6683 | |
| 6684 | Init = Init->IgnoreParens(); |
| 6685 | |
| 6686 | // Step over value-preserving rvalue casts. |
| 6687 | if (auto *CE = dyn_cast<CastExpr>(Init)) { |
| 6688 | switch (CE->getCastKind()) { |
| 6689 | case CK_LValueToRValue: |
| 6690 | // If we can match the lvalue to a const object, we can look at its |
| 6691 | // initializer. |
| 6692 | Path.push_back({IndirectLocalPathEntry::LValToRVal, CE}); |
| 6693 | return visitLocalsRetainedByReferenceBinding( |
| 6694 | Path, Init, RK_ReferenceBinding, |
| 6695 | [&](IndirectLocalPath &Path, Local L, ReferenceKind RK) -> bool { |
| 6696 | if (auto *DRE = dyn_cast<DeclRefExpr>(L)) { |
| 6697 | auto *VD = dyn_cast<VarDecl>(DRE->getDecl()); |
| 6698 | if (VD && VD->getType().isConstQualified() && VD->getInit() && |
| 6699 | !isVarOnPath(Path, VD)) { |
| 6700 | Path.push_back({IndirectLocalPathEntry::VarInit, DRE, VD}); |
| 6701 | visitLocalsRetainedByInitializer(Path, VD->getInit(), Visit, true); |
| 6702 | } |
| 6703 | } else if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(L)) { |
| 6704 | if (MTE->getType().isConstQualified()) |
| 6705 | visitLocalsRetainedByInitializer(Path, MTE->GetTemporaryExpr(), |
| 6706 | Visit, true); |
| 6707 | } |
| 6708 | return false; |
| 6709 | }); |
| 6710 | |
| 6711 | // We assume that objects can be retained by pointers cast to integers, |
| 6712 | // but not if the integer is cast to floating-point type or to _Complex. |
| 6713 | // We assume that casts to 'bool' do not preserve enough information to |
| 6714 | // retain a local object. |
| 6715 | case CK_NoOp: |
| 6716 | case CK_BitCast: |
| 6717 | case CK_BaseToDerived: |
| 6718 | case CK_DerivedToBase: |
| 6719 | case CK_UncheckedDerivedToBase: |
| 6720 | case CK_Dynamic: |
| 6721 | case CK_ToUnion: |
| 6722 | case CK_UserDefinedConversion: |
| 6723 | case CK_ConstructorConversion: |
| 6724 | case CK_IntegralToPointer: |
| 6725 | case CK_PointerToIntegral: |
| 6726 | case CK_VectorSplat: |
| 6727 | case CK_IntegralCast: |
| 6728 | case CK_CPointerToObjCPointerCast: |
| 6729 | case CK_BlockPointerToObjCPointerCast: |
| 6730 | case CK_AnyPointerToBlockPointerCast: |
| 6731 | case CK_AddressSpaceConversion: |
| 6732 | break; |
| 6733 | |
| 6734 | case CK_ArrayToPointerDecay: |
| 6735 | // Model array-to-pointer decay as taking the address of the array |
| 6736 | // lvalue. |
| 6737 | Path.push_back({IndirectLocalPathEntry::AddressOf, CE}); |
| 6738 | return visitLocalsRetainedByReferenceBinding(Path, CE->getSubExpr(), |
| 6739 | RK_ReferenceBinding, Visit); |
| 6740 | |
| 6741 | default: |
| 6742 | return; |
| 6743 | } |
| 6744 | |
| 6745 | Init = CE->getSubExpr(); |
| 6746 | } |
| 6747 | } while (Old != Init); |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 6748 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6749 | // C++17 [dcl.init.list]p6: |
| 6750 | // initializing an initializer_list object from the array extends the |
| 6751 | // lifetime of the array exactly like binding a reference to a temporary. |
| 6752 | if (auto *ILE = dyn_cast<CXXStdInitializerListExpr>(Init)) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6753 | return visitLocalsRetainedByReferenceBinding(Path, ILE->getSubExpr(), |
| 6754 | RK_StdInitializerList, Visit); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6755 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6756 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6757 | // We already visited the elements of this initializer list while |
| 6758 | // performing the initialization. Don't visit them again unless we've |
| 6759 | // changed the lifetime of the initialized entity. |
| 6760 | if (!RevisitSubinits) |
| 6761 | return; |
| 6762 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6763 | if (ILE->isTransparent()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6764 | return visitLocalsRetainedByInitializer(Path, ILE->getInit(0), Visit, |
| 6765 | RevisitSubinits); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6766 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6767 | if (ILE->getType()->isArrayType()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6768 | for (unsigned I = 0, N = ILE->getNumInits(); I != N; ++I) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6769 | visitLocalsRetainedByInitializer(Path, ILE->getInit(I), Visit, |
| 6770 | RevisitSubinits); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6771 | return; |
| 6772 | } |
| 6773 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6774 | if (CXXRecordDecl *RD = ILE->getType()->getAsCXXRecordDecl()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6775 | assert(RD->isAggregate() && "aggregate init on non-aggregate"); |
| 6776 | |
| 6777 | // If we lifetime-extend a braced initializer which is initializing an |
| 6778 | // aggregate, and that aggregate contains reference members which are |
| 6779 | // bound to temporaries, those temporaries are also lifetime-extended. |
| 6780 | if (RD->isUnion() && ILE->getInitializedFieldInUnion() && |
| 6781 | ILE->getInitializedFieldInUnion()->getType()->isReferenceType()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6782 | visitLocalsRetainedByReferenceBinding(Path, ILE->getInit(0), |
| 6783 | RK_ReferenceBinding, Visit); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6784 | else { |
| 6785 | unsigned Index = 0; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6786 | for (const auto *I : RD->fields()) { |
Richard Smith | 0bca59d | 2013-07-01 06:08:20 +0000 | [diff] [blame] | 6787 | if (Index >= ILE->getNumInits()) |
| 6788 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6789 | if (I->isUnnamedBitfield()) |
| 6790 | continue; |
Richard Smith | 8d7f11d | 2013-06-27 22:54:33 +0000 | [diff] [blame] | 6791 | Expr *SubInit = ILE->getInit(Index); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6792 | if (I->getType()->isReferenceType()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6793 | visitLocalsRetainedByReferenceBinding(Path, SubInit, |
| 6794 | RK_ReferenceBinding, Visit); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6795 | else |
| 6796 | // This might be either aggregate-initialization of a member or |
| 6797 | // initialization of a std::initializer_list object. Regardless, |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6798 | // we should recursively lifetime-extend that initializer. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6799 | visitLocalsRetainedByInitializer(Path, SubInit, Visit, |
| 6800 | RevisitSubinits); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6801 | ++Index; |
| 6802 | } |
| 6803 | } |
| 6804 | } |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6805 | return; |
| 6806 | } |
| 6807 | |
Richard Smith | b3d203f | 2018-10-19 19:01:34 +0000 | [diff] [blame] | 6808 | // The lifetime of an init-capture is that of the closure object constructed |
| 6809 | // by a lambda-expression. |
| 6810 | if (auto *LE = dyn_cast<LambdaExpr>(Init)) { |
| 6811 | for (Expr *E : LE->capture_inits()) { |
| 6812 | if (!E) |
| 6813 | continue; |
| 6814 | if (E->isGLValue()) |
| 6815 | visitLocalsRetainedByReferenceBinding(Path, E, RK_ReferenceBinding, |
| 6816 | Visit); |
| 6817 | else |
| 6818 | visitLocalsRetainedByInitializer(Path, E, Visit, true); |
| 6819 | } |
| 6820 | } |
| 6821 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6822 | if (isa<CallExpr>(Init) || isa<CXXConstructExpr>(Init)) |
| 6823 | return visitLifetimeBoundArguments(Path, Init, Visit); |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6824 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6825 | switch (Init->getStmtClass()) { |
| 6826 | case Stmt::UnaryOperatorClass: { |
| 6827 | auto *UO = cast<UnaryOperator>(Init); |
| 6828 | // If the initializer is the address of a local, we could have a lifetime |
| 6829 | // problem. |
| 6830 | if (UO->getOpcode() == UO_AddrOf) { |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6831 | // If this is &rvalue, then it's ill-formed and we have already diagnosed |
| 6832 | // it. Don't produce a redundant warning about the lifetime of the |
| 6833 | // temporary. |
| 6834 | if (isa<MaterializeTemporaryExpr>(UO->getSubExpr())) |
| 6835 | return; |
| 6836 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6837 | Path.push_back({IndirectLocalPathEntry::AddressOf, UO}); |
| 6838 | visitLocalsRetainedByReferenceBinding(Path, UO->getSubExpr(), |
| 6839 | RK_ReferenceBinding, Visit); |
| 6840 | } |
| 6841 | break; |
| 6842 | } |
| 6843 | |
| 6844 | case Stmt::BinaryOperatorClass: { |
| 6845 | // Handle pointer arithmetic. |
| 6846 | auto *BO = cast<BinaryOperator>(Init); |
| 6847 | BinaryOperatorKind BOK = BO->getOpcode(); |
| 6848 | if (!BO->getType()->isPointerType() || (BOK != BO_Add && BOK != BO_Sub)) |
| 6849 | break; |
| 6850 | |
| 6851 | if (BO->getLHS()->getType()->isPointerType()) |
| 6852 | visitLocalsRetainedByInitializer(Path, BO->getLHS(), Visit, true); |
| 6853 | else if (BO->getRHS()->getType()->isPointerType()) |
| 6854 | visitLocalsRetainedByInitializer(Path, BO->getRHS(), Visit, true); |
| 6855 | break; |
| 6856 | } |
| 6857 | |
| 6858 | case Stmt::ConditionalOperatorClass: |
| 6859 | case Stmt::BinaryConditionalOperatorClass: { |
| 6860 | auto *C = cast<AbstractConditionalOperator>(Init); |
| 6861 | // In C++, we can have a throw-expression operand, which has 'void' type |
| 6862 | // and isn't interesting from a lifetime perspective. |
| 6863 | if (!C->getTrueExpr()->getType()->isVoidType()) |
| 6864 | visitLocalsRetainedByInitializer(Path, C->getTrueExpr(), Visit, true); |
| 6865 | if (!C->getFalseExpr()->getType()->isVoidType()) |
| 6866 | visitLocalsRetainedByInitializer(Path, C->getFalseExpr(), Visit, true); |
| 6867 | break; |
| 6868 | } |
| 6869 | |
| 6870 | case Stmt::BlockExprClass: |
| 6871 | if (cast<BlockExpr>(Init)->getBlockDecl()->hasCaptures()) { |
| 6872 | // This is a local block, whose lifetime is that of the function. |
| 6873 | Visit(Path, Local(cast<BlockExpr>(Init)), RK_ReferenceBinding); |
| 6874 | } |
| 6875 | break; |
| 6876 | |
| 6877 | case Stmt::AddrLabelExprClass: |
| 6878 | // We want to warn if the address of a label would escape the function. |
| 6879 | Visit(Path, Local(cast<AddrLabelExpr>(Init)), RK_ReferenceBinding); |
| 6880 | break; |
| 6881 | |
| 6882 | default: |
| 6883 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6884 | } |
| 6885 | } |
| 6886 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6887 | /// Determine whether this is an indirect path to a temporary that we are |
| 6888 | /// supposed to lifetime-extend along (but don't). |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6889 | static bool shouldLifetimeExtendThroughPath(const IndirectLocalPath &Path) { |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6890 | for (auto Elem : Path) { |
Richard Smith | f66e4f7 | 2018-07-23 22:56:45 +0000 | [diff] [blame] | 6891 | if (Elem.Kind != IndirectLocalPathEntry::DefaultInit) |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6892 | return false; |
| 6893 | } |
| 6894 | return true; |
| 6895 | } |
| 6896 | |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 6897 | /// Find the range for the first interesting entry in the path at or after I. |
| 6898 | static SourceRange nextPathEntryRange(const IndirectLocalPath &Path, unsigned I, |
| 6899 | Expr *E) { |
| 6900 | for (unsigned N = Path.size(); I != N; ++I) { |
| 6901 | switch (Path[I].Kind) { |
| 6902 | case IndirectLocalPathEntry::AddressOf: |
| 6903 | case IndirectLocalPathEntry::LValToRVal: |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6904 | case IndirectLocalPathEntry::LifetimeBoundCall: |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 6905 | // These exist primarily to mark the path as not permitting or |
| 6906 | // supporting lifetime extension. |
| 6907 | break; |
| 6908 | |
| 6909 | case IndirectLocalPathEntry::DefaultInit: |
| 6910 | case IndirectLocalPathEntry::VarInit: |
| 6911 | return Path[I].E->getSourceRange(); |
| 6912 | } |
| 6913 | } |
| 6914 | return E->getSourceRange(); |
| 6915 | } |
| 6916 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6917 | void Sema::checkInitializerLifetime(const InitializedEntity &Entity, |
| 6918 | Expr *Init) { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6919 | LifetimeResult LR = getEntityLifetime(&Entity); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6920 | LifetimeKind LK = LR.getInt(); |
| 6921 | const InitializedEntity *ExtendingEntity = LR.getPointer(); |
| 6922 | |
| 6923 | // If this entity doesn't have an interesting lifetime, don't bother looking |
| 6924 | // for temporaries within its initializer. |
| 6925 | if (LK == LK_FullExpression) |
| 6926 | return; |
| 6927 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6928 | auto TemporaryVisitor = [&](IndirectLocalPath &Path, Local L, |
| 6929 | ReferenceKind RK) -> bool { |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 6930 | SourceRange DiagRange = nextPathEntryRange(Path, 0, L); |
| 6931 | SourceLocation DiagLoc = DiagRange.getBegin(); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6932 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6933 | switch (LK) { |
| 6934 | case LK_FullExpression: |
| 6935 | llvm_unreachable("already handled this"); |
| 6936 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6937 | case LK_Extended: { |
| 6938 | auto *MTE = dyn_cast<MaterializeTemporaryExpr>(L); |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6939 | if (!MTE) { |
| 6940 | // The initialized entity has lifetime beyond the full-expression, |
| 6941 | // and the local entity does too, so don't warn. |
| 6942 | // |
| 6943 | // FIXME: We should consider warning if a static / thread storage |
| 6944 | // duration variable retains an automatic storage duration local. |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6945 | return false; |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6946 | } |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6947 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6948 | // Lifetime-extend the temporary. |
| 6949 | if (Path.empty()) { |
| 6950 | // Update the storage duration of the materialized temporary. |
| 6951 | // FIXME: Rebuild the expression instead of mutating it. |
| 6952 | MTE->setExtendingDecl(ExtendingEntity->getDecl(), |
| 6953 | ExtendingEntity->allocateManglingNumber()); |
| 6954 | // Also visit the temporaries lifetime-extended by this initializer. |
| 6955 | return true; |
| 6956 | } |
| 6957 | |
| 6958 | if (shouldLifetimeExtendThroughPath(Path)) { |
| 6959 | // We're supposed to lifetime-extend the temporary along this path (per |
| 6960 | // the resolution of DR1815), but we don't support that yet. |
| 6961 | // |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6962 | // FIXME: Properly handle this situation. Perhaps the easiest approach |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6963 | // would be to clone the initializer expression on each use that would |
| 6964 | // lifetime extend its temporaries. |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6965 | Diag(DiagLoc, diag::warn_unsupported_lifetime_extension) |
| 6966 | << RK << DiagRange; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6967 | } else { |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6968 | // If the path goes through the initialization of a variable or field, |
| 6969 | // it can't possibly reach a temporary created in this full-expression. |
| 6970 | // We will have already diagnosed any problems with the initializer. |
| 6971 | if (pathContainsInit(Path)) |
| 6972 | return false; |
| 6973 | |
| 6974 | Diag(DiagLoc, diag::warn_dangling_variable) |
Richard Smith | ad5bbcc | 2018-08-01 01:03:33 +0000 | [diff] [blame] | 6975 | << RK << !Entity.getParent() |
| 6976 | << ExtendingEntity->getDecl()->isImplicit() |
| 6977 | << ExtendingEntity->getDecl() << Init->isGLValue() << DiagRange; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6978 | } |
| 6979 | break; |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6980 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6981 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6982 | case LK_MemInitializer: { |
George Burgess IV | 06df229 | 2018-07-24 02:10:53 +0000 | [diff] [blame] | 6983 | if (isa<MaterializeTemporaryExpr>(L)) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6984 | // Under C++ DR1696, if a mem-initializer (or a default member |
| 6985 | // initializer used by the absence of one) would lifetime-extend a |
| 6986 | // temporary, the program is ill-formed. |
| 6987 | if (auto *ExtendingDecl = |
| 6988 | ExtendingEntity ? ExtendingEntity->getDecl() : nullptr) { |
| 6989 | bool IsSubobjectMember = ExtendingEntity != &Entity; |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6990 | Diag(DiagLoc, shouldLifetimeExtendThroughPath(Path) |
| 6991 | ? diag::err_dangling_member |
| 6992 | : diag::warn_dangling_member) |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6993 | << ExtendingDecl << IsSubobjectMember << RK << DiagRange; |
| 6994 | // Don't bother adding a note pointing to the field if we're inside |
| 6995 | // its default member initializer; our primary diagnostic points to |
| 6996 | // the same place in that case. |
| 6997 | if (Path.empty() || |
| 6998 | Path.back().Kind != IndirectLocalPathEntry::DefaultInit) { |
| 6999 | Diag(ExtendingDecl->getLocation(), |
| 7000 | diag::note_lifetime_extending_member_declared_here) |
| 7001 | << RK << IsSubobjectMember; |
| 7002 | } |
| 7003 | } else { |
| 7004 | // We have a mem-initializer but no particular field within it; this |
| 7005 | // is either a base class or a delegating initializer directly |
| 7006 | // initializing the base-class from something that doesn't live long |
| 7007 | // enough. |
| 7008 | // |
| 7009 | // FIXME: Warn on this. |
| 7010 | return false; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7011 | } |
| 7012 | } else { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7013 | // Paths via a default initializer can only occur during error recovery |
| 7014 | // (there's no other way that a default initializer can refer to a |
| 7015 | // local). Don't produce a bogus warning on those cases. |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 7016 | if (pathContainsInit(Path)) |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7017 | return false; |
| 7018 | |
| 7019 | auto *DRE = dyn_cast<DeclRefExpr>(L); |
| 7020 | auto *VD = DRE ? dyn_cast<VarDecl>(DRE->getDecl()) : nullptr; |
| 7021 | if (!VD) { |
| 7022 | // A member was initialized to a local block. |
| 7023 | // FIXME: Warn on this. |
| 7024 | return false; |
| 7025 | } |
| 7026 | |
| 7027 | if (auto *Member = |
| 7028 | ExtendingEntity ? ExtendingEntity->getDecl() : nullptr) { |
| 7029 | bool IsPointer = Member->getType()->isAnyPointerType(); |
| 7030 | Diag(DiagLoc, IsPointer ? diag::warn_init_ptr_member_to_parameter_addr |
| 7031 | : diag::warn_bind_ref_member_to_parameter) |
| 7032 | << Member << VD << isa<ParmVarDecl>(VD) << DiagRange; |
| 7033 | Diag(Member->getLocation(), |
| 7034 | diag::note_ref_or_ptr_member_declared_here) |
| 7035 | << (unsigned)IsPointer; |
| 7036 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7037 | } |
| 7038 | break; |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7039 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7040 | |
| 7041 | case LK_New: |
George Burgess IV | 06df229 | 2018-07-24 02:10:53 +0000 | [diff] [blame] | 7042 | if (isa<MaterializeTemporaryExpr>(L)) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7043 | Diag(DiagLoc, RK == RK_ReferenceBinding |
| 7044 | ? diag::warn_new_dangling_reference |
| 7045 | : diag::warn_new_dangling_initializer_list) |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 7046 | << !Entity.getParent() << DiagRange; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7047 | } else { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7048 | // We can't determine if the allocation outlives the local declaration. |
| 7049 | return false; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7050 | } |
| 7051 | break; |
| 7052 | |
| 7053 | case LK_Return: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 7054 | case LK_StmtExprResult: |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7055 | if (auto *DRE = dyn_cast<DeclRefExpr>(L)) { |
| 7056 | // We can't determine if the local variable outlives the statement |
| 7057 | // expression. |
| 7058 | if (LK == LK_StmtExprResult) |
| 7059 | return false; |
| 7060 | Diag(DiagLoc, diag::warn_ret_stack_addr_ref) |
| 7061 | << Entity.getType()->isReferenceType() << DRE->getDecl() |
| 7062 | << isa<ParmVarDecl>(DRE->getDecl()) << DiagRange; |
| 7063 | } else if (isa<BlockExpr>(L)) { |
| 7064 | Diag(DiagLoc, diag::err_ret_local_block) << DiagRange; |
| 7065 | } else if (isa<AddrLabelExpr>(L)) { |
Reid Kleckner | 4c33d19 | 2018-08-17 22:11:31 +0000 | [diff] [blame] | 7066 | // Don't warn when returning a label from a statement expression. |
| 7067 | // Leaving the scope doesn't end its lifetime. |
| 7068 | if (LK == LK_StmtExprResult) |
| 7069 | return false; |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7070 | Diag(DiagLoc, diag::warn_ret_addr_label) << DiagRange; |
| 7071 | } else { |
| 7072 | Diag(DiagLoc, diag::warn_ret_local_temp_addr_ref) |
| 7073 | << Entity.getType()->isReferenceType() << DiagRange; |
| 7074 | } |
| 7075 | break; |
Florian Hahn | 0aa117d | 2018-07-17 09:23:31 +0000 | [diff] [blame] | 7076 | } |
| 7077 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7078 | for (unsigned I = 0; I != Path.size(); ++I) { |
| 7079 | auto Elem = Path[I]; |
| 7080 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7081 | switch (Elem.Kind) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7082 | case IndirectLocalPathEntry::AddressOf: |
| 7083 | case IndirectLocalPathEntry::LValToRVal: |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 7084 | // These exist primarily to mark the path as not permitting or |
| 7085 | // supporting lifetime extension. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7086 | break; |
| 7087 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 7088 | case IndirectLocalPathEntry::LifetimeBoundCall: |
| 7089 | // FIXME: Consider adding a note for this. |
| 7090 | break; |
| 7091 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7092 | case IndirectLocalPathEntry::DefaultInit: { |
| 7093 | auto *FD = cast<FieldDecl>(Elem.D); |
| 7094 | Diag(FD->getLocation(), diag::note_init_with_default_member_initalizer) |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 7095 | << FD << nextPathEntryRange(Path, I + 1, L); |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7096 | break; |
| 7097 | } |
| 7098 | |
| 7099 | case IndirectLocalPathEntry::VarInit: |
| 7100 | const VarDecl *VD = cast<VarDecl>(Elem.D); |
| 7101 | Diag(VD->getLocation(), diag::note_local_var_initializer) |
Richard Smith | ad5bbcc | 2018-08-01 01:03:33 +0000 | [diff] [blame] | 7102 | << VD->getType()->isReferenceType() |
| 7103 | << VD->isImplicit() << VD->getDeclName() |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 7104 | << nextPathEntryRange(Path, I + 1, L); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7105 | break; |
Florian Hahn | 0aa117d | 2018-07-17 09:23:31 +0000 | [diff] [blame] | 7106 | } |
| 7107 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7108 | |
| 7109 | // We didn't lifetime-extend, so don't go any further; we don't need more |
| 7110 | // warnings or errors on inner temporaries within this one's initializer. |
| 7111 | return false; |
| 7112 | }; |
| 7113 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7114 | llvm::SmallVector<IndirectLocalPathEntry, 8> Path; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7115 | if (Init->isGLValue()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7116 | visitLocalsRetainedByReferenceBinding(Path, Init, RK_ReferenceBinding, |
| 7117 | TemporaryVisitor); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7118 | else |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7119 | visitLocalsRetainedByInitializer(Path, Init, TemporaryVisitor, false); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 7120 | } |
| 7121 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7122 | static void DiagnoseNarrowingInInitList(Sema &S, |
| 7123 | const ImplicitConversionSequence &ICS, |
| 7124 | QualType PreNarrowingType, |
| 7125 | QualType EntityType, |
| 7126 | const Expr *PostInit); |
| 7127 | |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7128 | /// Provide warnings when std::move is used on construction. |
| 7129 | static void CheckMoveOnConstruction(Sema &S, const Expr *InitExpr, |
| 7130 | bool IsReturnStmt) { |
| 7131 | if (!InitExpr) |
| 7132 | return; |
| 7133 | |
Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 7134 | if (S.inTemplateInstantiation()) |
Richard Trieu | 6093d14 | 2015-07-29 17:03:34 +0000 | [diff] [blame] | 7135 | return; |
| 7136 | |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7137 | QualType DestType = InitExpr->getType(); |
| 7138 | if (!DestType->isRecordType()) |
| 7139 | return; |
| 7140 | |
| 7141 | unsigned DiagID = 0; |
| 7142 | if (IsReturnStmt) { |
| 7143 | const CXXConstructExpr *CCE = |
| 7144 | dyn_cast<CXXConstructExpr>(InitExpr->IgnoreParens()); |
| 7145 | if (!CCE || CCE->getNumArgs() != 1) |
| 7146 | return; |
| 7147 | |
| 7148 | if (!CCE->getConstructor()->isCopyOrMoveConstructor()) |
| 7149 | return; |
| 7150 | |
| 7151 | InitExpr = CCE->getArg(0)->IgnoreImpCasts(); |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7152 | } |
| 7153 | |
| 7154 | // Find the std::move call and get the argument. |
| 7155 | const CallExpr *CE = dyn_cast<CallExpr>(InitExpr->IgnoreParens()); |
Nico Weber | 192184c | 2018-06-20 15:57:38 +0000 | [diff] [blame] | 7156 | if (!CE || !CE->isCallToStdMove()) |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7157 | return; |
| 7158 | |
| 7159 | const Expr *Arg = CE->getArg(0)->IgnoreImplicit(); |
| 7160 | |
| 7161 | if (IsReturnStmt) { |
| 7162 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg->IgnoreParenImpCasts()); |
| 7163 | if (!DRE || DRE->refersToEnclosingVariableOrCapture()) |
| 7164 | return; |
| 7165 | |
| 7166 | const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()); |
| 7167 | if (!VD || !VD->hasLocalStorage()) |
| 7168 | return; |
| 7169 | |
Alex Lorenz | bbe51d8 | 2017-11-07 21:40:11 +0000 | [diff] [blame] | 7170 | // __block variables are not moved implicitly. |
| 7171 | if (VD->hasAttr<BlocksAttr>()) |
| 7172 | return; |
| 7173 | |
Richard Trieu | 8d4006a | 2015-07-28 19:06:16 +0000 | [diff] [blame] | 7174 | QualType SourceType = VD->getType(); |
| 7175 | if (!SourceType->isRecordType()) |
Richard Trieu | 1d4911bc | 2015-05-18 19:54:08 +0000 | [diff] [blame] | 7176 | return; |
| 7177 | |
Richard Trieu | 8d4006a | 2015-07-28 19:06:16 +0000 | [diff] [blame] | 7178 | if (!S.Context.hasSameUnqualifiedType(DestType, SourceType)) { |
Richard Trieu | 1993dc8 | 2015-07-29 23:47:19 +0000 | [diff] [blame] | 7179 | return; |
Richard Trieu | 8d4006a | 2015-07-28 19:06:16 +0000 | [diff] [blame] | 7180 | } |
| 7181 | |
Davide Italiano | 7842c3f | 2015-07-18 01:15:19 +0000 | [diff] [blame] | 7182 | // If we're returning a function parameter, copy elision |
| 7183 | // is not possible. |
| 7184 | if (isa<ParmVarDecl>(VD)) |
| 7185 | DiagID = diag::warn_redundant_move_on_return; |
Richard Trieu | 1993dc8 | 2015-07-29 23:47:19 +0000 | [diff] [blame] | 7186 | else |
| 7187 | DiagID = diag::warn_pessimizing_move_on_return; |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7188 | } else { |
| 7189 | DiagID = diag::warn_pessimizing_move_on_initialization; |
| 7190 | const Expr *ArgStripped = Arg->IgnoreImplicit()->IgnoreParens(); |
| 7191 | if (!ArgStripped->isRValue() || !ArgStripped->getType()->isRecordType()) |
| 7192 | return; |
| 7193 | } |
| 7194 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7195 | S.Diag(CE->getBeginLoc(), DiagID); |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7196 | |
| 7197 | // Get all the locations for a fix-it. Don't emit the fix-it if any location |
| 7198 | // is within a macro. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7199 | SourceLocation CallBegin = CE->getCallee()->getBeginLoc(); |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7200 | if (CallBegin.isMacroID()) |
| 7201 | return; |
| 7202 | SourceLocation RParen = CE->getRParenLoc(); |
| 7203 | if (RParen.isMacroID()) |
| 7204 | return; |
| 7205 | SourceLocation LParen; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7206 | SourceLocation ArgLoc = Arg->getBeginLoc(); |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7207 | |
| 7208 | // Special testing for the argument location. Since the fix-it needs the |
| 7209 | // location right before the argument, the argument location can be in a |
| 7210 | // macro only if it is at the beginning of the macro. |
| 7211 | while (ArgLoc.isMacroID() && |
| 7212 | S.getSourceManager().isAtStartOfImmediateMacroExpansion(ArgLoc)) { |
Richard Smith | b5f8171 | 2018-04-30 05:25:48 +0000 | [diff] [blame] | 7213 | ArgLoc = S.getSourceManager().getImmediateExpansionRange(ArgLoc).getBegin(); |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7214 | } |
| 7215 | |
| 7216 | if (LParen.isMacroID()) |
| 7217 | return; |
| 7218 | |
| 7219 | LParen = ArgLoc.getLocWithOffset(-1); |
| 7220 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7221 | S.Diag(CE->getBeginLoc(), diag::note_remove_move) |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7222 | << FixItHint::CreateRemoval(SourceRange(CallBegin, LParen)) |
| 7223 | << FixItHint::CreateRemoval(SourceRange(RParen, RParen)); |
| 7224 | } |
| 7225 | |
Nick Lewycky | 2eeddfb | 2016-05-14 17:44:14 +0000 | [diff] [blame] | 7226 | static void CheckForNullPointerDereference(Sema &S, const Expr *E) { |
| 7227 | // Check to see if we are dereferencing a null pointer. If so, this is |
| 7228 | // undefined behavior, so warn about it. This only handles the pattern |
| 7229 | // "*null", which is a very syntactic check. |
| 7230 | if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E->IgnoreParenCasts())) |
| 7231 | if (UO->getOpcode() == UO_Deref && |
| 7232 | UO->getSubExpr()->IgnoreParenCasts()-> |
| 7233 | isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull)) { |
| 7234 | S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
| 7235 | S.PDiag(diag::warn_binding_null_to_reference) |
| 7236 | << UO->getSubExpr()->getSourceRange()); |
| 7237 | } |
| 7238 | } |
| 7239 | |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 7240 | MaterializeTemporaryExpr * |
| 7241 | Sema::CreateMaterializeTemporaryExpr(QualType T, Expr *Temporary, |
| 7242 | bool BoundToLvalueReference) { |
| 7243 | auto MTE = new (Context) |
| 7244 | MaterializeTemporaryExpr(T, Temporary, BoundToLvalueReference); |
| 7245 | |
| 7246 | // Order an ExprWithCleanups for lifetime marks. |
| 7247 | // |
| 7248 | // TODO: It'll be good to have a single place to check the access of the |
| 7249 | // destructor and generate ExprWithCleanups for various uses. Currently these |
| 7250 | // are done in both CreateMaterializeTemporaryExpr and MaybeBindToTemporary, |
| 7251 | // but there may be a chance to merge them. |
| 7252 | Cleanup.setExprNeedsCleanups(false); |
| 7253 | return MTE; |
| 7254 | } |
| 7255 | |
Richard Smith | 4baaa5a | 2016-12-03 01:14:32 +0000 | [diff] [blame] | 7256 | ExprResult Sema::TemporaryMaterializationConversion(Expr *E) { |
| 7257 | // In C++98, we don't want to implicitly create an xvalue. |
| 7258 | // FIXME: This means that AST consumers need to deal with "prvalues" that |
| 7259 | // denote materialized temporaries. Maybe we should add another ValueKind |
| 7260 | // for "xvalue pretending to be a prvalue" for C++98 support. |
| 7261 | if (!E->isRValue() || !getLangOpts().CPlusPlus11) |
| 7262 | return E; |
| 7263 | |
| 7264 | // C++1z [conv.rval]/1: T shall be a complete type. |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7265 | // FIXME: Does this ever matter (can we form a prvalue of incomplete type)? |
| 7266 | // If so, we should check for a non-abstract class type here too. |
Richard Smith | 4baaa5a | 2016-12-03 01:14:32 +0000 | [diff] [blame] | 7267 | QualType T = E->getType(); |
| 7268 | if (RequireCompleteType(E->getExprLoc(), T, diag::err_incomplete_type)) |
| 7269 | return ExprError(); |
| 7270 | |
| 7271 | return CreateMaterializeTemporaryExpr(E->getType(), E, false); |
| 7272 | } |
| 7273 | |
Anastasia Stulova | 0430794 | 2018-11-16 16:22:56 +0000 | [diff] [blame] | 7274 | ExprResult Sema::PerformQualificationConversion(Expr *E, QualType Ty, |
| 7275 | ExprValueKind VK, |
| 7276 | CheckedConversionKind CCK) { |
| 7277 | CastKind CK = (Ty.getAddressSpace() != E->getType().getAddressSpace()) |
| 7278 | ? CK_AddressSpaceConversion |
| 7279 | : CK_NoOp; |
| 7280 | return ImpCastExprToType(E, Ty, CK, VK, /*BasePath=*/nullptr, CCK); |
| 7281 | } |
| 7282 | |
| 7283 | ExprResult InitializationSequence::Perform(Sema &S, |
| 7284 | const InitializedEntity &Entity, |
| 7285 | const InitializationKind &Kind, |
| 7286 | MultiExprArg Args, |
| 7287 | QualType *ResultType) { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 7288 | if (Failed()) { |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 7289 | Diagnose(S, Entity, Kind, Args); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7290 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7291 | } |
Nico Weber | 337d5aa | 2015-04-17 08:32:38 +0000 | [diff] [blame] | 7292 | if (!ZeroInitializationFixit.empty()) { |
| 7293 | unsigned DiagID = diag::err_default_init_const; |
| 7294 | if (Decl *D = Entity.getDecl()) |
| 7295 | if (S.getLangOpts().MSVCCompat && D->hasAttr<SelectAnyAttr>()) |
| 7296 | DiagID = diag::ext_default_init_const; |
| 7297 | |
| 7298 | // The initialization would have succeeded with this fixit. Since the fixit |
| 7299 | // is on the error, we need to build a valid AST in this case, so this isn't |
| 7300 | // handled in the Failed() branch above. |
| 7301 | QualType DestType = Entity.getType(); |
| 7302 | S.Diag(Kind.getLocation(), DiagID) |
| 7303 | << DestType << (bool)DestType->getAs<RecordType>() |
| 7304 | << FixItHint::CreateInsertion(ZeroInitializationFixitLoc, |
| 7305 | ZeroInitializationFixit); |
| 7306 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7307 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 7308 | if (getKind() == DependentSequence) { |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7309 | // If the declaration is a non-dependent, incomplete array type |
| 7310 | // that has an initializer, then its type will be completed once |
| 7311 | // the initializer is instantiated. |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7312 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7313 | Args.size() == 1) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7314 | QualType DeclType = Entity.getType(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7315 | if (const IncompleteArrayType *ArrayT |
| 7316 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 7317 | // FIXME: We don't currently have the ability to accurately |
| 7318 | // compute the length of an initializer list without |
| 7319 | // performing full type-checking of the initializer list |
| 7320 | // (since we have to determine where braces are implicitly |
| 7321 | // introduced and such). So, we fall back to making the array |
| 7322 | // type a dependently-sized array type with no specified |
| 7323 | // bound. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7324 | if (isa<InitListExpr>((Expr *)Args[0])) { |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7325 | SourceRange Brackets; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7326 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7327 | // Scavange the location of the brackets from the entity, if we can. |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 7328 | if (auto *DD = dyn_cast_or_null<DeclaratorDecl>(Entity.getDecl())) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7329 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 7330 | TypeLoc TL = TInfo->getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7331 | if (IncompleteArrayTypeLoc ArrayLoc = |
| 7332 | TL.getAs<IncompleteArrayTypeLoc>()) |
| 7333 | Brackets = ArrayLoc.getBracketsRange(); |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7334 | } |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7335 | } |
| 7336 | |
| 7337 | *ResultType |
| 7338 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7339 | /*NumElts=*/nullptr, |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7340 | ArrayT->getSizeModifier(), |
| 7341 | ArrayT->getIndexTypeCVRQualifiers(), |
| 7342 | Brackets); |
| 7343 | } |
| 7344 | |
| 7345 | } |
| 7346 | } |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 7347 | if (Kind.getKind() == InitializationKind::IK_Direct && |
| 7348 | !Kind.isExplicitCast()) { |
| 7349 | // Rebuild the ParenListExpr. |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 7350 | SourceRange ParenRange = Kind.getParenOrBraceRange(); |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 7351 | return S.ActOnParenListExpr(ParenRange.getBegin(), ParenRange.getEnd(), |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7352 | Args); |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 7353 | } |
Manuel Klimek | f2b4b69 | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 7354 | assert(Kind.getKind() == InitializationKind::IK_Copy || |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7355 | Kind.isExplicitCast() || |
Douglas Gregor | bf13895 | 2012-04-04 04:06:51 +0000 | [diff] [blame] | 7356 | Kind.getKind() == InitializationKind::IK_DirectList); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7357 | return ExprResult(Args[0]); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7358 | } |
| 7359 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 7360 | // No steps means no initialization. |
| 7361 | if (Steps.empty()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7362 | return ExprResult((Expr *)nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7363 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7364 | if (S.getLangOpts().CPlusPlus11 && Entity.getType()->isReferenceType() && |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7365 | Args.size() == 1 && isa<InitListExpr>(Args[0]) && |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 7366 | !Entity.isParameterKind()) { |
Richard Smith | 2b349ae | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 7367 | // Produce a C++98 compatibility warning if we are initializing a reference |
| 7368 | // from an initializer list. For parameters, we produce a better warning |
| 7369 | // elsewhere. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7370 | Expr *Init = Args[0]; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7371 | S.Diag(Init->getBeginLoc(), diag::warn_cxx98_compat_reference_list_init) |
| 7372 | << Init->getSourceRange(); |
Richard Smith | 2b349ae | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 7373 | } |
| 7374 | |
Egor Churaev | 3bccec5 | 2017-04-05 12:47:10 +0000 | [diff] [blame] | 7375 | // OpenCL v2.0 s6.13.11.1. atomic variables can be initialized in global scope |
| 7376 | QualType ETy = Entity.getType(); |
| 7377 | Qualifiers TyQualifiers = ETy.getQualifiers(); |
| 7378 | bool HasGlobalAS = TyQualifiers.hasAddressSpace() && |
| 7379 | TyQualifiers.getAddressSpace() == LangAS::opencl_global; |
| 7380 | |
| 7381 | if (S.getLangOpts().OpenCLVersion >= 200 && |
| 7382 | ETy->isAtomicType() && !HasGlobalAS && |
| 7383 | Entity.getKind() == InitializedEntity::EK_Variable && Args.size() > 0) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7384 | S.Diag(Args[0]->getBeginLoc(), diag::err_opencl_atomic_init) |
| 7385 | << 1 |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 7386 | << SourceRange(Entity.getDecl()->getBeginLoc(), Args[0]->getEndLoc()); |
Egor Churaev | 3bccec5 | 2017-04-05 12:47:10 +0000 | [diff] [blame] | 7387 | return ExprError(); |
| 7388 | } |
| 7389 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7390 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 7391 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 7392 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 7393 | // and we want latter when it makes sense. |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7394 | if (ResultType) |
Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 7395 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7396 | Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7397 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7398 | ExprResult CurInit((Expr *)nullptr); |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7399 | SmallVector<Expr*, 4> ArrayLoopCommonExprs; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7400 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7401 | // For initialization steps that start with a single initializer, |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 7402 | // grab the only argument out the Args and place it into the "current" |
| 7403 | // initializer. |
| 7404 | switch (Steps.front().Kind) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7405 | case SK_ResolveAddressOfOverloadedFunction: |
| 7406 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7407 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7408 | case SK_CastDerivedToBaseLValue: |
| 7409 | case SK_BindReference: |
| 7410 | case SK_BindReferenceToTemporary: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7411 | case SK_FinalCopy: |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 7412 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7413 | case SK_UserConversion: |
| 7414 | case SK_QualificationConversionLValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7415 | case SK_QualificationConversionXValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7416 | case SK_QualificationConversionRValue: |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 7417 | case SK_AtomicConversion: |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 7418 | case SK_LValueToRValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7419 | case SK_ConversionSequence: |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7420 | case SK_ConversionSequenceNoNarrowing: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7421 | case SK_ListInitialization: |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7422 | case SK_UnwrapInitList: |
| 7423 | case SK_RewrapInitList: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7424 | case SK_CAssignment: |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 7425 | case SK_StringInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7426 | case SK_ObjCObjectConversion: |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7427 | case SK_ArrayLoopIndex: |
| 7428 | case SK_ArrayLoopInit: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7429 | case SK_ArrayInit: |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 7430 | case SK_GNUArrayInit: |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 7431 | case SK_ParenthesizedArrayInit: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7432 | case SK_PassByIndirectCopyRestore: |
| 7433 | case SK_PassByIndirectRestore: |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 7434 | case SK_ProduceObjCObject: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7435 | case SK_StdInitializerList: |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 7436 | case SK_OCLSamplerInit: |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 7437 | case SK_OCLZeroOpaqueType: { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7438 | assert(Args.size() == 1); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7439 | CurInit = Args[0]; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7440 | if (!CurInit.get()) return ExprError(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7441 | break; |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7442 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7443 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7444 | case SK_ConstructorInitialization: |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7445 | case SK_ConstructorInitializationFromList: |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 7446 | case SK_StdInitializerListConstructorCall: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7447 | case SK_ZeroInitialization: |
| 7448 | break; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7449 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7450 | |
Richard Smith | d6a1508 | 2017-01-07 00:48:55 +0000 | [diff] [blame] | 7451 | // Promote from an unevaluated context to an unevaluated list context in |
| 7452 | // C++11 list-initialization; we need to instantiate entities usable in |
| 7453 | // constant expressions here in order to perform narrowing checks =( |
| 7454 | EnterExpressionEvaluationContext Evaluated( |
| 7455 | S, EnterExpressionEvaluationContext::InitList, |
| 7456 | CurInit.get() && isa<InitListExpr>(CurInit.get())); |
| 7457 | |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7458 | // C++ [class.abstract]p2: |
| 7459 | // no objects of an abstract class can be created except as subobjects |
| 7460 | // of a class derived from it |
| 7461 | auto checkAbstractType = [&](QualType T) -> bool { |
| 7462 | if (Entity.getKind() == InitializedEntity::EK_Base || |
| 7463 | Entity.getKind() == InitializedEntity::EK_Delegating) |
| 7464 | return false; |
| 7465 | return S.RequireNonAbstractType(Kind.getLocation(), T, |
| 7466 | diag::err_allocation_of_abstract_type); |
| 7467 | }; |
| 7468 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7469 | // Walk through the computed steps for the initialization sequence, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7470 | // performing the specified conversions along the way. |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7471 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7472 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 7473 | Step != StepEnd; ++Step) { |
| 7474 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7475 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7476 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7477 | QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7478 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7479 | switch (Step->Kind) { |
| 7480 | case SK_ResolveAddressOfOverloadedFunction: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7481 | // Overload resolution determined which function invoke; update the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7482 | // initializer to reflect that choice. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7483 | S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 7484 | if (S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation())) |
| 7485 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7486 | CurInit = S.FixOverloadedFunctionReference(CurInit, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7487 | Step->Function.FoundDecl, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7488 | Step->Function.Function); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7489 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7490 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7491 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7492 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7493 | case SK_CastDerivedToBaseLValue: { |
| 7494 | // We have a derived-to-base cast that produces either an rvalue or an |
| 7495 | // lvalue. Perform that cast. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7496 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 7497 | CXXCastPath BasePath; |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 7498 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7499 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 7500 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7501 | if (S.CheckDerivedToBaseConversion( |
| 7502 | SourceType, Step->Type, CurInit.get()->getBeginLoc(), |
| 7503 | CurInit.get()->getSourceRange(), &BasePath, IgnoreBaseAccess)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7504 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7505 | |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7506 | ExprValueKind VK = |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7507 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7508 | VK_LValue : |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7509 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7510 | VK_XValue : |
| 7511 | VK_RValue); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7512 | CurInit = |
| 7513 | ImplicitCastExpr::Create(S.Context, Step->Type, CK_DerivedToBase, |
| 7514 | CurInit.get(), &BasePath, VK); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7515 | break; |
| 7516 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7517 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7518 | case SK_BindReference: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7519 | // Reference binding does not have any corresponding ASTs. |
| 7520 | |
| 7521 | // Check exception specifications |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7522 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7523 | return ExprError(); |
Anders Carlsson | ab0ddb5 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 7524 | |
George Burgess IV | cfd48d9 | 2017-04-13 23:47:08 +0000 | [diff] [blame] | 7525 | // We don't check for e.g. function pointers here, since address |
| 7526 | // availability checks should only occur when the function first decays |
| 7527 | // into a pointer or reference. |
| 7528 | if (CurInit.get()->getType()->isFunctionProtoType()) { |
| 7529 | if (auto *DRE = dyn_cast<DeclRefExpr>(CurInit.get()->IgnoreParens())) { |
| 7530 | if (auto *FD = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
| 7531 | if (!S.checkAddressOfFunctionIsAvailable(FD, /*Complain=*/true, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7532 | DRE->getBeginLoc())) |
George Burgess IV | cfd48d9 | 2017-04-13 23:47:08 +0000 | [diff] [blame] | 7533 | return ExprError(); |
| 7534 | } |
| 7535 | } |
| 7536 | } |
| 7537 | |
Nick Lewycky | 2eeddfb | 2016-05-14 17:44:14 +0000 | [diff] [blame] | 7538 | CheckForNullPointerDereference(S, CurInit.get()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7539 | break; |
Anders Carlsson | ab0ddb5 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 7540 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 7541 | case SK_BindReferenceToTemporary: { |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 7542 | // Make sure the "temporary" is actually an rvalue. |
| 7543 | assert(CurInit.get()->isRValue() && "not a temporary"); |
| 7544 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7545 | // Check exception specifications |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7546 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7547 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7548 | |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 7549 | // Materialize the temporary into memory. |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 7550 | MaterializeTemporaryExpr *MTE = S.CreateMaterializeTemporaryExpr( |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7551 | Step->Type, CurInit.get(), Entity.getType()->isLValueReferenceType()); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7552 | CurInit = MTE; |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 7553 | |
Brian Kelley | 762f928 | 2017-03-29 18:16:38 +0000 | [diff] [blame] | 7554 | // If we're extending this temporary to automatic storage duration -- we |
| 7555 | // need to register its cleanup during the full-expression's cleanups. |
| 7556 | if (MTE->getStorageDuration() == SD_Automatic && |
| 7557 | MTE->getType().isDestructedType()) |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 7558 | S.Cleanup.setExprNeedsCleanups(true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7559 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 7560 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7561 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7562 | case SK_FinalCopy: |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7563 | if (checkAbstractType(Step->Type)) |
| 7564 | return ExprError(); |
| 7565 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7566 | // If the overall initialization is initializing a temporary, we already |
| 7567 | // bound our argument if it was necessary to do so. If not (if we're |
| 7568 | // ultimately initializing a non-temporary), our argument needs to be |
| 7569 | // bound since it's initializing a function parameter. |
| 7570 | // FIXME: This is a mess. Rationalize temporary destruction. |
| 7571 | if (!shouldBindAsTemporary(Entity)) |
| 7572 | CurInit = S.MaybeBindToTemporary(CurInit.get()); |
| 7573 | CurInit = CopyObject(S, Step->Type, Entity, CurInit, |
| 7574 | /*IsExtraneousCopy=*/false); |
| 7575 | break; |
| 7576 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 7577 | case SK_ExtraneousCopyToTemporary: |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7578 | CurInit = CopyObject(S, Step->Type, Entity, CurInit, |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 7579 | /*IsExtraneousCopy=*/true); |
| 7580 | break; |
| 7581 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7582 | case SK_UserConversion: { |
| 7583 | // We have a user-defined conversion that invokes either a constructor |
| 7584 | // or a conversion function. |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 7585 | CastKind CastKind; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7586 | FunctionDecl *Fn = Step->Function.Function; |
| 7587 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 7588 | bool HadMultipleCandidates = Step->Function.HadMultipleCandidates; |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 7589 | bool CreatedObject = false; |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 7590 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7591 | // Build a call to the selected constructor. |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 7592 | SmallVector<Expr*, 8> ConstructorArgs; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7593 | SourceLocation Loc = CurInit.get()->getBeginLoc(); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 7594 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7595 | // Determine the arguments required to actually perform the constructor |
| 7596 | // call. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7597 | Expr *Arg = CurInit.get(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7598 | if (S.CompleteConstructorCall(Constructor, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7599 | MultiExprArg(&Arg, 1), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7600 | Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7601 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7602 | |
Richard Smith | b24f067 | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 7603 | // Build an expression that constructs a temporary. |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 7604 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, |
| 7605 | FoundFn, Constructor, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7606 | ConstructorArgs, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 7607 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 7608 | /*ListInit*/ false, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 7609 | /*StdInitListInit*/ false, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 7610 | /*ZeroInit*/ false, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 7611 | CXXConstructExpr::CK_Complete, |
| 7612 | SourceRange()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7613 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7614 | return ExprError(); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 7615 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 7616 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, FoundFn, |
| 7617 | Entity); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 7618 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 7619 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7620 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7621 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 7622 | CreatedObject = true; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7623 | } else { |
| 7624 | // Build a call to the conversion function. |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 7625 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7626 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), nullptr, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7627 | FoundFn); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 7628 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 7629 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7630 | |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 7631 | CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion, |
| 7632 | HadMultipleCandidates); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7633 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7634 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7635 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7636 | CastKind = CK_UserDefinedConversion; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7637 | CreatedObject = Conversion->getReturnType()->isRecordType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7638 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7639 | |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7640 | if (CreatedObject && checkAbstractType(CurInit.get()->getType())) |
| 7641 | return ExprError(); |
| 7642 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7643 | CurInit = ImplicitCastExpr::Create(S.Context, CurInit.get()->getType(), |
| 7644 | CastKind, CurInit.get(), nullptr, |
| 7645 | CurInit.get()->getValueKind()); |
Abramo Bagnara | b0cf297 | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 7646 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7647 | if (shouldBindAsTemporary(Entity)) |
| 7648 | // The overall entity is temporary, so this expression should be |
| 7649 | // destroyed at the end of its full-expression. |
| 7650 | CurInit = S.MaybeBindToTemporary(CurInit.getAs<Expr>()); |
| 7651 | else if (CreatedObject && shouldDestroyEntity(Entity)) { |
| 7652 | // The object outlasts the full-expression, but we need to prepare for |
| 7653 | // a destructor being run on it. |
| 7654 | // FIXME: It makes no sense to do this here. This should happen |
| 7655 | // regardless of how we initialized the entity. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7656 | QualType T = CurInit.get()->getType(); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 7657 | if (const RecordType *Record = T->getAs<RecordType>()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7658 | CXXDestructorDecl *Destructor |
Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 7659 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7660 | S.CheckDestructorAccess(CurInit.get()->getBeginLoc(), Destructor, |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 7661 | S.PDiag(diag::err_access_dtor_temp) << T); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7662 | S.MarkFunctionReferenced(CurInit.get()->getBeginLoc(), Destructor); |
| 7663 | if (S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getBeginLoc())) |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 7664 | return ExprError(); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 7665 | } |
| 7666 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7667 | break; |
| 7668 | } |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7669 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7670 | case SK_QualificationConversionLValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7671 | case SK_QualificationConversionXValue: |
| 7672 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7673 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7674 | ExprValueKind VK = |
Anastasia Stulova | 0430794 | 2018-11-16 16:22:56 +0000 | [diff] [blame] | 7675 | Step->Kind == SK_QualificationConversionLValue |
| 7676 | ? VK_LValue |
| 7677 | : (Step->Kind == SK_QualificationConversionXValue ? VK_XValue |
| 7678 | : VK_RValue); |
| 7679 | CurInit = S.PerformQualificationConversion(CurInit.get(), Step->Type, VK); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7680 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7681 | } |
| 7682 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 7683 | case SK_AtomicConversion: { |
| 7684 | assert(CurInit.get()->isRValue() && "cannot convert glvalue to atomic"); |
| 7685 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
| 7686 | CK_NonAtomicToAtomic, VK_RValue); |
| 7687 | break; |
| 7688 | } |
| 7689 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 7690 | case SK_LValueToRValue: { |
| 7691 | assert(CurInit.get()->isGLValue() && "cannot load from a prvalue"); |
Richard Smith | 3501895 | 2018-11-03 02:23:33 +0000 | [diff] [blame] | 7692 | CurInit = ImplicitCastExpr::Create(S.Context, Step->Type, |
| 7693 | CK_LValueToRValue, CurInit.get(), |
| 7694 | /*BasePath=*/nullptr, VK_RValue); |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 7695 | break; |
| 7696 | } |
| 7697 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7698 | case SK_ConversionSequence: |
| 7699 | case SK_ConversionSequenceNoNarrowing: { |
Leonard Chan | ad7ac96 | 2018-12-06 01:05:54 +0000 | [diff] [blame] | 7700 | if (const auto *FromPtrType = |
| 7701 | CurInit.get()->getType()->getAs<PointerType>()) { |
| 7702 | if (const auto *ToPtrType = Step->Type->getAs<PointerType>()) { |
| 7703 | if (FromPtrType->getPointeeType()->hasAttr(attr::NoDeref) && |
| 7704 | !ToPtrType->getPointeeType()->hasAttr(attr::NoDeref)) { |
| 7705 | S.Diag(CurInit.get()->getExprLoc(), |
| 7706 | diag::warn_noderef_to_dereferenceable_pointer) |
| 7707 | << CurInit.get()->getSourceRange(); |
| 7708 | } |
| 7709 | } |
| 7710 | } |
| 7711 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7712 | Sema::CheckedConversionKind CCK |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7713 | = Kind.isCStyleCast()? Sema::CCK_CStyleCast |
| 7714 | : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 7715 | : Kind.isExplicitCast()? Sema::CCK_OtherCast |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7716 | : Sema::CCK_ImplicitConversion; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7717 | ExprResult CurInitExprRes = |
| 7718 | S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7719 | getAssignmentAction(Entity), CCK); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7720 | if (CurInitExprRes.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7721 | return ExprError(); |
Roger Ferrer Ibanez | 722a4db | 2016-08-12 08:04:13 +0000 | [diff] [blame] | 7722 | |
| 7723 | S.DiscardMisalignedMemberAddress(Step->Type.getTypePtr(), CurInit.get()); |
| 7724 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7725 | CurInit = CurInitExprRes; |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7726 | |
| 7727 | if (Step->Kind == SK_ConversionSequenceNoNarrowing && |
Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 7728 | S.getLangOpts().CPlusPlus) |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7729 | DiagnoseNarrowingInInitList(S, *Step->ICS, SourceType, Entity.getType(), |
| 7730 | CurInit.get()); |
Roger Ferrer Ibanez | 722a4db | 2016-08-12 08:04:13 +0000 | [diff] [blame] | 7731 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7732 | break; |
Douglas Gregor | 5c8ffab | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 7733 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7734 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7735 | case SK_ListInitialization: { |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7736 | if (checkAbstractType(Step->Type)) |
| 7737 | return ExprError(); |
| 7738 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7739 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 7740 | // If we're not initializing the top-level entity, we need to create an |
| 7741 | // InitializeTemporary entity for our target type. |
| 7742 | QualType Ty = Step->Type; |
| 7743 | bool IsTemporary = !S.Context.hasSameType(Entity.getType(), Ty); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7744 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(Ty); |
Richard Smith | d712d0d | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 7745 | InitializedEntity InitEntity = IsTemporary ? TempEntity : Entity; |
| 7746 | InitListChecker PerformInitList(S, InitEntity, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 7747 | InitList, Ty, /*VerifyOnly=*/false, |
| 7748 | /*TreatUnavailableAsInvalid=*/false); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 7749 | if (PerformInitList.HadError()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7750 | return ExprError(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7751 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 7752 | // Hack: We must update *ResultType if available in order to set the |
| 7753 | // bounds of arrays, e.g. in 'int ar[] = {1, 2, 3};'. |
| 7754 | // Worst case: 'const int (&arref)[] = {1, 2, 3};'. |
| 7755 | if (ResultType && |
| 7756 | ResultType->getNonReferenceType()->isIncompleteArrayType()) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7757 | if ((*ResultType)->isRValueReferenceType()) |
| 7758 | Ty = S.Context.getRValueReferenceType(Ty); |
| 7759 | else if ((*ResultType)->isLValueReferenceType()) |
| 7760 | Ty = S.Context.getLValueReferenceType(Ty, |
| 7761 | (*ResultType)->getAs<LValueReferenceType>()->isSpelledAsLValue()); |
| 7762 | *ResultType = Ty; |
| 7763 | } |
| 7764 | |
| 7765 | InitListExpr *StructuredInitList = |
| 7766 | PerformInitList.getFullyStructuredList(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7767 | CurInit.get(); |
Richard Smith | d712d0d | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 7768 | CurInit = shouldBindAsTemporary(InitEntity) |
| 7769 | ? S.MaybeBindToTemporary(StructuredInitList) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7770 | : StructuredInitList; |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7771 | break; |
| 7772 | } |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 7773 | |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7774 | case SK_ConstructorInitializationFromList: { |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7775 | if (checkAbstractType(Step->Type)) |
| 7776 | return ExprError(); |
| 7777 | |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 7778 | // When an initializer list is passed for a parameter of type "reference |
| 7779 | // to object", we don't get an EK_Temporary entity, but instead an |
| 7780 | // EK_Parameter entity with reference type. |
Sebastian Redl | 99f6616 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 7781 | // FIXME: This is a hack. What we really should do is create a user |
| 7782 | // conversion step for this case, but this makes it considerably more |
| 7783 | // complicated. For now, this will do. |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 7784 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 7785 | Entity.getType().getNonReferenceType()); |
| 7786 | bool UseTemporary = Entity.getType()->isReferenceType(); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 7787 | assert(Args.size() == 1 && "expected a single argument for list init"); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7788 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Richard Smith | 2b349ae | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 7789 | S.Diag(InitList->getExprLoc(), diag::warn_cxx98_compat_ctor_list_init) |
| 7790 | << InitList->getSourceRange(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 7791 | MultiExprArg Arg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 7792 | CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity : |
| 7793 | Entity, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7794 | Kind, Arg, *Step, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 7795 | ConstructorInitRequiresZeroInit, |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7796 | /*IsListInitialization*/true, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 7797 | /*IsStdInitListInit*/false, |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 7798 | InitList->getLBraceLoc(), |
| 7799 | InitList->getRBraceLoc()); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 7800 | break; |
| 7801 | } |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 7802 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7803 | case SK_UnwrapInitList: |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7804 | CurInit = cast<InitListExpr>(CurInit.get())->getInit(0); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7805 | break; |
| 7806 | |
| 7807 | case SK_RewrapInitList: { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7808 | Expr *E = CurInit.get(); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7809 | InitListExpr *Syntactic = Step->WrappingSyntacticList; |
| 7810 | InitListExpr *ILE = new (S.Context) InitListExpr(S.Context, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 7811 | Syntactic->getLBraceLoc(), E, Syntactic->getRBraceLoc()); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7812 | ILE->setSyntacticForm(Syntactic); |
| 7813 | ILE->setType(E->getType()); |
| 7814 | ILE->setValueKind(E->getValueKind()); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7815 | CurInit = ILE; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7816 | break; |
| 7817 | } |
| 7818 | |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7819 | case SK_ConstructorInitialization: |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 7820 | case SK_StdInitializerListConstructorCall: { |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7821 | if (checkAbstractType(Step->Type)) |
| 7822 | return ExprError(); |
| 7823 | |
Sebastian Redl | 99f6616 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 7824 | // When an initializer list is passed for a parameter of type "reference |
| 7825 | // to object", we don't get an EK_Temporary entity, but instead an |
| 7826 | // EK_Parameter entity with reference type. |
| 7827 | // FIXME: This is a hack. What we really should do is create a user |
| 7828 | // conversion step for this case, but this makes it considerably more |
| 7829 | // complicated. For now, this will do. |
| 7830 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 7831 | Entity.getType().getNonReferenceType()); |
| 7832 | bool UseTemporary = Entity.getType()->isReferenceType(); |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 7833 | bool IsStdInitListInit = |
| 7834 | Step->Kind == SK_StdInitializerListConstructorCall; |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7835 | Expr *Source = CurInit.get(); |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 7836 | SourceRange Range = Kind.hasParenOrBraceRange() |
| 7837 | ? Kind.getParenOrBraceRange() |
| 7838 | : SourceRange(); |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7839 | CurInit = PerformConstructorInitialization( |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7840 | S, UseTemporary ? TempEntity : Entity, Kind, |
| 7841 | Source ? MultiExprArg(Source) : Args, *Step, |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7842 | ConstructorInitRequiresZeroInit, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7843 | /*IsListInitialization*/ IsStdInitListInit, |
| 7844 | /*IsStdInitListInitialization*/ IsStdInitListInit, |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 7845 | /*LBraceLoc*/ Range.getBegin(), |
| 7846 | /*RBraceLoc*/ Range.getEnd()); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 7847 | break; |
Sebastian Redl | 99f6616 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 7848 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7849 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 7850 | case SK_ZeroInitialization: { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7851 | step_iterator NextStep = Step; |
| 7852 | ++NextStep; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7853 | if (NextStep != StepEnd && |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 7854 | (NextStep->Kind == SK_ConstructorInitialization || |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7855 | NextStep->Kind == SK_ConstructorInitializationFromList)) { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7856 | // The need for zero-initialization is recorded directly into |
| 7857 | // the call to the object's constructor within the next step. |
| 7858 | ConstructorInitRequiresZeroInit = true; |
| 7859 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7860 | S.getLangOpts().CPlusPlus && |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7861 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 7862 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 7863 | if (!TSInfo) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7864 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 7865 | Kind.getRange().getBegin()); |
| 7866 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7867 | CurInit = new (S.Context) CXXScalarValueInitExpr( |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 7868 | Entity.getType().getNonLValueExprType(S.Context), TSInfo, |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7869 | Kind.getRange().getEnd()); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7870 | } else { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7871 | CurInit = new (S.Context) ImplicitValueInitExpr(Step->Type); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7872 | } |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 7873 | break; |
| 7874 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7875 | |
| 7876 | case SK_CAssignment: { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7877 | QualType SourceType = CurInit.get()->getType(); |
Leonard Chan | ad7ac96 | 2018-12-06 01:05:54 +0000 | [diff] [blame] | 7878 | |
George Burgess IV | 5f21c71 | 2015-10-12 19:57:04 +0000 | [diff] [blame] | 7879 | // Save off the initial CurInit in case we need to emit a diagnostic |
| 7880 | ExprResult InitialCurInit = CurInit; |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7881 | ExprResult Result = CurInit; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7882 | Sema::AssignConvertType ConvTy = |
Fariborz Jahanian | 25eef19 | 2013-07-31 21:40:51 +0000 | [diff] [blame] | 7883 | S.CheckSingleAssignmentConstraints(Step->Type, Result, true, |
| 7884 | Entity.getKind() == InitializedEntity::EK_Parameter_CF_Audited); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7885 | if (Result.isInvalid()) |
| 7886 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7887 | CurInit = Result; |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 7888 | |
| 7889 | // If this is a call, allow conversion to a transparent union. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7890 | ExprResult CurInitExprRes = CurInit; |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 7891 | if (ConvTy != Sema::Compatible && |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 7892 | Entity.isParameterKind() && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7893 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes) |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 7894 | == Sema::Compatible) |
| 7895 | ConvTy = Sema::Compatible; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7896 | if (CurInitExprRes.isInvalid()) |
| 7897 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7898 | CurInit = CurInitExprRes; |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 7899 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 7900 | bool Complained; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7901 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 7902 | Step->Type, SourceType, |
George Burgess IV | 5f21c71 | 2015-10-12 19:57:04 +0000 | [diff] [blame] | 7903 | InitialCurInit.get(), |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 7904 | getAssignmentAction(Entity, true), |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 7905 | &Complained)) { |
| 7906 | PrintInitLocationNote(S, Entity); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7907 | return ExprError(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 7908 | } else if (Complained) |
| 7909 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7910 | break; |
| 7911 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 7912 | |
| 7913 | case SK_StringInit: { |
| 7914 | QualType Ty = Step->Type; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7915 | CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty, |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 7916 | S.Context.getAsArrayType(Ty), S); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 7917 | break; |
| 7918 | } |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 7919 | |
| 7920 | case SK_ObjCObjectConversion: |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7921 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7922 | CK_ObjCObjectLValueCast, |
Eli Friedman | be4b363 | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 7923 | CurInit.get()->getValueKind()); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 7924 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7925 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7926 | case SK_ArrayLoopIndex: { |
| 7927 | Expr *Cur = CurInit.get(); |
| 7928 | Expr *BaseExpr = new (S.Context) |
| 7929 | OpaqueValueExpr(Cur->getExprLoc(), Cur->getType(), |
| 7930 | Cur->getValueKind(), Cur->getObjectKind(), Cur); |
| 7931 | Expr *IndexExpr = |
| 7932 | new (S.Context) ArrayInitIndexExpr(S.Context.getSizeType()); |
| 7933 | CurInit = S.CreateBuiltinArraySubscriptExpr( |
| 7934 | BaseExpr, Kind.getLocation(), IndexExpr, Kind.getLocation()); |
| 7935 | ArrayLoopCommonExprs.push_back(BaseExpr); |
| 7936 | break; |
| 7937 | } |
| 7938 | |
| 7939 | case SK_ArrayLoopInit: { |
| 7940 | assert(!ArrayLoopCommonExprs.empty() && |
| 7941 | "mismatched SK_ArrayLoopIndex and SK_ArrayLoopInit"); |
| 7942 | Expr *Common = ArrayLoopCommonExprs.pop_back_val(); |
| 7943 | CurInit = new (S.Context) ArrayInitLoopExpr(Step->Type, Common, |
| 7944 | CurInit.get()); |
| 7945 | break; |
| 7946 | } |
| 7947 | |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 7948 | case SK_GNUArrayInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7949 | // Okay: we checked everything before creating this step. Note that |
| 7950 | // this is a GNU extension. |
| 7951 | S.Diag(Kind.getLocation(), diag::ext_array_init_copy) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7952 | << Step->Type << CurInit.get()->getType() |
| 7953 | << CurInit.get()->getSourceRange(); |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 7954 | LLVM_FALLTHROUGH; |
| 7955 | case SK_ArrayInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7956 | // If the destination type is an incomplete array type, update the |
| 7957 | // type accordingly. |
| 7958 | if (ResultType) { |
| 7959 | if (const IncompleteArrayType *IncompleteDest |
| 7960 | = S.Context.getAsIncompleteArrayType(Step->Type)) { |
| 7961 | if (const ConstantArrayType *ConstantSource |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7962 | = S.Context.getAsConstantArrayType(CurInit.get()->getType())) { |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7963 | *ResultType = S.Context.getConstantArrayType( |
| 7964 | IncompleteDest->getElementType(), |
| 7965 | ConstantSource->getSize(), |
| 7966 | ArrayType::Normal, 0); |
| 7967 | } |
| 7968 | } |
| 7969 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7970 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7971 | |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 7972 | case SK_ParenthesizedArrayInit: |
| 7973 | // Okay: we checked everything before creating this step. Note that |
| 7974 | // this is a GNU extension. |
| 7975 | S.Diag(Kind.getLocation(), diag::ext_array_init_parens) |
| 7976 | << CurInit.get()->getSourceRange(); |
| 7977 | break; |
| 7978 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7979 | case SK_PassByIndirectCopyRestore: |
| 7980 | case SK_PassByIndirectRestore: |
| 7981 | checkIndirectCopyRestoreSource(S, CurInit.get()); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7982 | CurInit = new (S.Context) ObjCIndirectCopyRestoreExpr( |
| 7983 | CurInit.get(), Step->Type, |
| 7984 | Step->Kind == SK_PassByIndirectCopyRestore); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7985 | break; |
| 7986 | |
| 7987 | case SK_ProduceObjCObject: |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7988 | CurInit = |
| 7989 | ImplicitCastExpr::Create(S.Context, Step->Type, CK_ARCProduceObject, |
| 7990 | CurInit.get(), nullptr, VK_RValue); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7991 | break; |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 7992 | |
| 7993 | case SK_StdInitializerList: { |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 7994 | S.Diag(CurInit.get()->getExprLoc(), |
| 7995 | diag::warn_cxx98_compat_initializer_list_init) |
| 7996 | << CurInit.get()->getSourceRange(); |
Sebastian Redl | 249dee5 | 2012-03-05 19:35:43 +0000 | [diff] [blame] | 7997 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 7998 | // Materialize the temporary into memory. |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 7999 | MaterializeTemporaryExpr *MTE = S.CreateMaterializeTemporaryExpr( |
| 8000 | CurInit.get()->getType(), CurInit.get(), |
| 8001 | /*BoundToLvalueReference=*/false); |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 8002 | |
Florian Hahn | 0aa117d | 2018-07-17 09:23:31 +0000 | [diff] [blame] | 8003 | // Wrap it in a construction of a std::initializer_list<T>. |
| 8004 | CurInit = new (S.Context) CXXStdInitializerListExpr(Step->Type, MTE); |
Richard Smith | 0a9969b | 2018-07-17 00:11:41 +0000 | [diff] [blame] | 8005 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 8006 | // Bind the result, in case the library has given initializer_list a |
| 8007 | // non-trivial destructor. |
| 8008 | if (shouldBindAsTemporary(Entity)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 8009 | CurInit = S.MaybeBindToTemporary(CurInit.get()); |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 8010 | break; |
| 8011 | } |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 8012 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 8013 | case SK_OCLSamplerInit: { |
Raphael Isemann | b23ccec | 2018-12-10 12:37:46 +0000 | [diff] [blame] | 8014 | // Sampler initialization have 5 cases: |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 8015 | // 1. function argument passing |
| 8016 | // 1a. argument is a file-scope variable |
| 8017 | // 1b. argument is a function-scope variable |
| 8018 | // 1c. argument is one of caller function's parameters |
| 8019 | // 2. variable initialization |
| 8020 | // 2a. initializing a file-scope variable |
| 8021 | // 2b. initializing a function-scope variable |
| 8022 | // |
| 8023 | // For file-scope variables, since they cannot be initialized by function |
| 8024 | // call of __translate_sampler_initializer in LLVM IR, their references |
| 8025 | // need to be replaced by a cast from their literal initializers to |
| 8026 | // sampler type. Since sampler variables can only be used in function |
| 8027 | // calls as arguments, we only need to replace them when handling the |
| 8028 | // argument passing. |
| 8029 | assert(Step->Type->isSamplerT() && |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 8030 | "Sampler initialization on non-sampler type."); |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 8031 | Expr *Init = CurInit.get(); |
| 8032 | QualType SourceType = Init->getType(); |
| 8033 | // Case 1 |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 8034 | if (Entity.isParameterKind()) { |
Egor Churaev | a8d2451 | 2017-04-05 09:02:56 +0000 | [diff] [blame] | 8035 | if (!SourceType->isSamplerT() && !SourceType->isIntegerType()) { |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 8036 | S.Diag(Kind.getLocation(), diag::err_sampler_argument_required) |
| 8037 | << SourceType; |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 8038 | break; |
| 8039 | } else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Init)) { |
| 8040 | auto Var = cast<VarDecl>(DRE->getDecl()); |
| 8041 | // Case 1b and 1c |
| 8042 | // No cast from integer to sampler is needed. |
| 8043 | if (!Var->hasGlobalStorage()) { |
| 8044 | CurInit = ImplicitCastExpr::Create(S.Context, Step->Type, |
| 8045 | CK_LValueToRValue, Init, |
| 8046 | /*BasePath=*/nullptr, VK_RValue); |
| 8047 | break; |
| 8048 | } |
| 8049 | // Case 1a |
| 8050 | // For function call with a file-scope sampler variable as argument, |
| 8051 | // get the integer literal. |
| 8052 | // Do not diagnose if the file-scope variable does not have initializer |
| 8053 | // since this has already been diagnosed when parsing the variable |
| 8054 | // declaration. |
| 8055 | if (!Var->getInit() || !isa<ImplicitCastExpr>(Var->getInit())) |
| 8056 | break; |
| 8057 | Init = cast<ImplicitCastExpr>(const_cast<Expr*>( |
| 8058 | Var->getInit()))->getSubExpr(); |
| 8059 | SourceType = Init->getType(); |
| 8060 | } |
| 8061 | } else { |
| 8062 | // Case 2 |
| 8063 | // Check initializer is 32 bit integer constant. |
| 8064 | // If the initializer is taken from global variable, do not diagnose since |
| 8065 | // this has already been done when parsing the variable declaration. |
| 8066 | if (!Init->isConstantInitializer(S.Context, false)) |
| 8067 | break; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8068 | |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 8069 | if (!SourceType->isIntegerType() || |
| 8070 | 32 != S.Context.getIntWidth(SourceType)) { |
| 8071 | S.Diag(Kind.getLocation(), diag::err_sampler_initializer_not_integer) |
| 8072 | << SourceType; |
| 8073 | break; |
| 8074 | } |
| 8075 | |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 8076 | Expr::EvalResult EVResult; |
| 8077 | Init->EvaluateAsInt(EVResult, S.Context); |
| 8078 | llvm::APSInt Result = EVResult.Val.getInt(); |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 8079 | const uint64_t SamplerValue = Result.getLimitedValue(); |
| 8080 | // 32-bit value of sampler's initializer is interpreted as |
| 8081 | // bit-field with the following structure: |
| 8082 | // |unspecified|Filter|Addressing Mode| Normalized Coords| |
| 8083 | // |31 6|5 4|3 1| 0| |
| 8084 | // This structure corresponds to enum values of sampler properties |
| 8085 | // defined in SPIR spec v1.2 and also opencl-c.h |
| 8086 | unsigned AddressingMode = (0x0E & SamplerValue) >> 1; |
| 8087 | unsigned FilterMode = (0x30 & SamplerValue) >> 4; |
Andrew Savonichev | 3fee351 | 2018-11-08 11:25:41 +0000 | [diff] [blame] | 8088 | if (FilterMode != 1 && FilterMode != 2 && |
| 8089 | !S.getOpenCLOptions().isEnabled( |
| 8090 | "cl_intel_device_side_avc_motion_estimation")) |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 8091 | S.Diag(Kind.getLocation(), |
| 8092 | diag::warn_sampler_initializer_invalid_bits) |
| 8093 | << "Filter Mode"; |
| 8094 | if (AddressingMode > 4) |
| 8095 | S.Diag(Kind.getLocation(), |
| 8096 | diag::warn_sampler_initializer_invalid_bits) |
| 8097 | << "Addressing Mode"; |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 8098 | } |
| 8099 | |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 8100 | // Cases 1a, 2a and 2b |
| 8101 | // Insert cast from integer to sampler. |
| 8102 | CurInit = S.ImpCastExprToType(Init, S.Context.OCLSamplerTy, |
| 8103 | CK_IntToOCLSampler); |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 8104 | break; |
| 8105 | } |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 8106 | case SK_OCLZeroOpaqueType: { |
Andrew Savonichev | 3fee351 | 2018-11-08 11:25:41 +0000 | [diff] [blame] | 8107 | assert((Step->Type->isEventT() || Step->Type->isQueueT() || |
| 8108 | Step->Type->isOCLIntelSubgroupAVCType()) && |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 8109 | "Wrong type for initialization of OpenCL opaque type."); |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 8110 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 8111 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 8112 | CK_ZeroToOCLOpaqueType, |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 8113 | CurInit.get()->getValueKind()); |
| 8114 | break; |
| 8115 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8116 | } |
| 8117 | } |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 8118 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 8119 | // Check whether the initializer has a shorter lifetime than the initialized |
| 8120 | // entity, and if not, either lifetime-extend or warn as appropriate. |
| 8121 | if (auto *Init = CurInit.get()) |
| 8122 | S.checkInitializerLifetime(Entity, Init); |
| 8123 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 8124 | // Diagnose non-fatal problems with the completed initialization. |
| 8125 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 8126 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 8127 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 8128 | cast<FieldDecl>(Entity.getDecl()), |
| 8129 | CurInit.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8130 | |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 8131 | // Check for std::move on construction. |
| 8132 | if (const Expr *E = CurInit.get()) { |
| 8133 | CheckMoveOnConstruction(S, E, |
| 8134 | Entity.getKind() == InitializedEntity::EK_Result); |
| 8135 | } |
| 8136 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 8137 | return CurInit; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8138 | } |
| 8139 | |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 8140 | /// Somewhere within T there is an uninitialized reference subobject. |
| 8141 | /// Dig it out and diagnose it. |
Benjamin Kramer | 3e35026 | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 8142 | static bool DiagnoseUninitializedReference(Sema &S, SourceLocation Loc, |
| 8143 | QualType T) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 8144 | if (T->isReferenceType()) { |
| 8145 | S.Diag(Loc, diag::err_reference_without_init) |
| 8146 | << T.getNonReferenceType(); |
| 8147 | return true; |
| 8148 | } |
| 8149 | |
| 8150 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 8151 | if (!RD || !RD->hasUninitializedReferenceMember()) |
| 8152 | return false; |
| 8153 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 8154 | for (const auto *FI : RD->fields()) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 8155 | if (FI->isUnnamedBitfield()) |
| 8156 | continue; |
| 8157 | |
| 8158 | if (DiagnoseUninitializedReference(S, FI->getLocation(), FI->getType())) { |
| 8159 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 8160 | return true; |
| 8161 | } |
| 8162 | } |
| 8163 | |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 8164 | for (const auto &BI : RD->bases()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8165 | if (DiagnoseUninitializedReference(S, BI.getBeginLoc(), BI.getType())) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 8166 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 8167 | return true; |
| 8168 | } |
| 8169 | } |
| 8170 | |
| 8171 | return false; |
| 8172 | } |
| 8173 | |
| 8174 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8175 | //===----------------------------------------------------------------------===// |
| 8176 | // Diagnose initialization failures |
| 8177 | //===----------------------------------------------------------------------===// |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 8178 | |
| 8179 | /// Emit notes associated with an initialization that failed due to a |
| 8180 | /// "simple" conversion failure. |
| 8181 | static void emitBadConversionNotes(Sema &S, const InitializedEntity &entity, |
| 8182 | Expr *op) { |
| 8183 | QualType destType = entity.getType(); |
| 8184 | if (destType.getNonReferenceType()->isObjCObjectPointerType() && |
| 8185 | op->getType()->isObjCObjectPointerType()) { |
| 8186 | |
| 8187 | // Emit a possible note about the conversion failing because the |
| 8188 | // operand is a message send with a related result type. |
| 8189 | S.EmitRelatedResultTypeNote(op); |
| 8190 | |
| 8191 | // Emit a possible note about a return failing because we're |
| 8192 | // expecting a related result type. |
| 8193 | if (entity.getKind() == InitializedEntity::EK_Result) |
| 8194 | S.EmitRelatedResultTypeNoteForReturn(destType); |
| 8195 | } |
| 8196 | } |
| 8197 | |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 8198 | static void diagnoseListInit(Sema &S, const InitializedEntity &Entity, |
| 8199 | InitListExpr *InitList) { |
| 8200 | QualType DestType = Entity.getType(); |
| 8201 | |
| 8202 | QualType E; |
| 8203 | if (S.getLangOpts().CPlusPlus11 && S.isStdInitializerList(DestType, &E)) { |
| 8204 | QualType ArrayType = S.Context.getConstantArrayType( |
| 8205 | E.withConst(), |
| 8206 | llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 8207 | InitList->getNumInits()), |
| 8208 | clang::ArrayType::Normal, 0); |
| 8209 | InitializedEntity HiddenArray = |
| 8210 | InitializedEntity::InitializeTemporary(ArrayType); |
| 8211 | return diagnoseListInit(S, HiddenArray, InitList); |
| 8212 | } |
| 8213 | |
Richard Smith | 8d082d1 | 2014-09-04 22:13:39 +0000 | [diff] [blame] | 8214 | if (DestType->isReferenceType()) { |
| 8215 | // A list-initialization failure for a reference means that we tried to |
| 8216 | // create a temporary of the inner type (per [dcl.init.list]p3.6) and the |
| 8217 | // inner initialization failed. |
| 8218 | QualType T = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 8219 | diagnoseListInit(S, InitializedEntity::InitializeTemporary(T), InitList); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8220 | SourceLocation Loc = InitList->getBeginLoc(); |
Richard Smith | 8d082d1 | 2014-09-04 22:13:39 +0000 | [diff] [blame] | 8221 | if (auto *D = Entity.getDecl()) |
| 8222 | Loc = D->getLocation(); |
| 8223 | S.Diag(Loc, diag::note_in_reference_temporary_list_initializer) << T; |
| 8224 | return; |
| 8225 | } |
| 8226 | |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 8227 | InitListChecker DiagnoseInitList(S, Entity, InitList, DestType, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 8228 | /*VerifyOnly=*/false, |
| 8229 | /*TreatUnavailableAsInvalid=*/false); |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 8230 | assert(DiagnoseInitList.HadError() && |
| 8231 | "Inconsistent init list check result."); |
| 8232 | } |
| 8233 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8234 | bool InitializationSequence::Diagnose(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8235 | const InitializedEntity &Entity, |
| 8236 | const InitializationKind &Kind, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8237 | ArrayRef<Expr *> Args) { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 8238 | if (!Failed()) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8239 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8240 | |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8241 | // When we want to diagnose only one element of a braced-init-list, |
| 8242 | // we need to factor it out. |
| 8243 | Expr *OnlyArg; |
| 8244 | if (Args.size() == 1) { |
| 8245 | auto *List = dyn_cast<InitListExpr>(Args[0]); |
| 8246 | if (List && List->getNumInits() == 1) |
| 8247 | OnlyArg = List->getInit(0); |
| 8248 | else |
| 8249 | OnlyArg = Args[0]; |
| 8250 | } |
| 8251 | else |
| 8252 | OnlyArg = nullptr; |
| 8253 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 8254 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8255 | switch (Failure) { |
| 8256 | case FK_TooManyInitsForReference: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8257 | // FIXME: Customize for the initialized entity? |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8258 | if (Args.empty()) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 8259 | // Dig out the reference subobject which is uninitialized and diagnose it. |
| 8260 | // If this is value-initialization, this could be nested some way within |
| 8261 | // the target type. |
| 8262 | assert(Kind.getKind() == InitializationKind::IK_Value || |
| 8263 | DestType->isReferenceType()); |
| 8264 | bool Diagnosed = |
| 8265 | DiagnoseUninitializedReference(S, Kind.getLocation(), DestType); |
| 8266 | assert(Diagnosed && "couldn't find uninitialized reference to diagnose"); |
| 8267 | (void)Diagnosed; |
| 8268 | } else // FIXME: diagnostic below could be better! |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8269 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 8270 | << SourceRange(Args.front()->getBeginLoc(), Args.back()->getEndLoc()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8271 | break; |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 8272 | case FK_ParenthesizedListInitForReference: |
| 8273 | S.Diag(Kind.getLocation(), diag::err_list_init_in_parens) |
| 8274 | << 1 << Entity.getType() << Args[0]->getSourceRange(); |
| 8275 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8276 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8277 | case FK_ArrayNeedsInitList: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 8278 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 0; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8279 | break; |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 8280 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 8281 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 1; |
| 8282 | break; |
| 8283 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 8284 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 2; |
| 8285 | break; |
| 8286 | case FK_NarrowStringIntoWideCharArray: |
| 8287 | S.Diag(Kind.getLocation(), diag::err_array_init_narrow_string_into_wchar); |
| 8288 | break; |
| 8289 | case FK_WideStringIntoCharArray: |
| 8290 | S.Diag(Kind.getLocation(), diag::err_array_init_wide_string_into_char); |
| 8291 | break; |
| 8292 | case FK_IncompatWideStringIntoWideChar: |
| 8293 | S.Diag(Kind.getLocation(), |
| 8294 | diag::err_array_init_incompat_wide_string_into_wchar); |
| 8295 | break; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 8296 | case FK_PlainStringIntoUTF8Char: |
| 8297 | S.Diag(Kind.getLocation(), |
| 8298 | diag::err_array_init_plain_string_into_char8_t); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8299 | S.Diag(Args.front()->getBeginLoc(), |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 8300 | diag::note_array_init_plain_string_into_char8_t) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8301 | << FixItHint::CreateInsertion(Args.front()->getBeginLoc(), "u8"); |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 8302 | break; |
| 8303 | case FK_UTF8StringIntoPlainChar: |
| 8304 | S.Diag(Kind.getLocation(), |
Richard Smith | 28ddb91 | 2018-11-14 21:04:34 +0000 | [diff] [blame] | 8305 | diag::err_array_init_utf8_string_into_char) |
| 8306 | << S.getLangOpts().CPlusPlus2a; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 8307 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8308 | case FK_ArrayTypeMismatch: |
| 8309 | case FK_NonConstantArrayInit: |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 8310 | S.Diag(Kind.getLocation(), |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8311 | (Failure == FK_ArrayTypeMismatch |
| 8312 | ? diag::err_array_init_different_type |
| 8313 | : diag::err_array_init_non_constant_array)) |
| 8314 | << DestType.getNonReferenceType() |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8315 | << OnlyArg->getType() |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8316 | << Args[0]->getSourceRange(); |
| 8317 | break; |
| 8318 | |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 8319 | case FK_VariableLengthArrayHasInitializer: |
| 8320 | S.Diag(Kind.getLocation(), diag::err_variable_object_no_init) |
| 8321 | << Args[0]->getSourceRange(); |
| 8322 | break; |
| 8323 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8324 | case FK_AddressOfOverloadFailed: { |
| 8325 | DeclAccessPair Found; |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8326 | S.ResolveAddressOfOverloadedFunction(OnlyArg, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8327 | DestType.getNonReferenceType(), |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8328 | true, |
| 8329 | Found); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8330 | break; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8331 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8332 | |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 8333 | case FK_AddressOfUnaddressableFunction: { |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8334 | auto *FD = cast<FunctionDecl>(cast<DeclRefExpr>(OnlyArg)->getDecl()); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 8335 | S.checkAddressOfFunctionIsAvailable(FD, /*Complain=*/true, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8336 | OnlyArg->getBeginLoc()); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 8337 | break; |
| 8338 | } |
| 8339 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8340 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 8341 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8342 | switch (FailedOverloadResult) { |
| 8343 | case OR_Ambiguous: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8344 | if (Failure == FK_UserConversionOverloadFailed) |
| 8345 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8346 | << OnlyArg->getType() << DestType |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8347 | << Args[0]->getSourceRange(); |
| 8348 | else |
| 8349 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8350 | << DestType << OnlyArg->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8351 | << Args[0]->getSourceRange(); |
| 8352 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8353 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8354 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8355 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8356 | case OR_No_Viable_Function: |
Larisse Voufo | 70bb43a | 2013-06-27 03:36:30 +0000 | [diff] [blame] | 8357 | if (!S.RequireCompleteType(Kind.getLocation(), |
Larisse Voufo | 64cf3ef | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 8358 | DestType.getNonReferenceType(), |
| 8359 | diag::err_typecheck_nonviable_condition_incomplete, |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8360 | OnlyArg->getType(), Args[0]->getSourceRange())) |
Larisse Voufo | 64cf3ef | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 8361 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
Nick Lewycky | 08426e2 | 2015-08-25 22:18:46 +0000 | [diff] [blame] | 8362 | << (Entity.getKind() == InitializedEntity::EK_Result) |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8363 | << OnlyArg->getType() << Args[0]->getSourceRange() |
Larisse Voufo | 64cf3ef | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 8364 | << DestType.getNonReferenceType(); |
| 8365 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8366 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8367 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8368 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8369 | case OR_Deleted: { |
| 8370 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8371 | << OnlyArg->getType() << DestType.getNonReferenceType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8372 | << Args[0]->getSourceRange(); |
| 8373 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8374 | OverloadingResult Ovl |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 8375 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8376 | if (Ovl == OR_Deleted) { |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 8377 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8378 | } else { |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 8379 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8380 | } |
| 8381 | break; |
| 8382 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8383 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8384 | case OR_Success: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 8385 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8386 | } |
| 8387 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8388 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8389 | case FK_NonConstLValueReferenceBindingToTemporary: |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8390 | if (isa<InitListExpr>(Args[0])) { |
| 8391 | S.Diag(Kind.getLocation(), |
| 8392 | diag::err_lvalue_reference_bind_to_initlist) |
| 8393 | << DestType.getNonReferenceType().isVolatileQualified() |
| 8394 | << DestType.getNonReferenceType() |
| 8395 | << Args[0]->getSourceRange(); |
| 8396 | break; |
| 8397 | } |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 8398 | LLVM_FALLTHROUGH; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8399 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8400 | case FK_NonConstLValueReferenceBindingToUnrelated: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8401 | S.Diag(Kind.getLocation(), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8402 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 8403 | ? diag::err_lvalue_reference_bind_to_temporary |
| 8404 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | d1e0864 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 8405 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8406 | << DestType.getNonReferenceType() |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8407 | << OnlyArg->getType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8408 | << Args[0]->getSourceRange(); |
| 8409 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8410 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8411 | case FK_NonConstLValueReferenceBindingToBitfield: { |
| 8412 | // We don't necessarily have an unambiguous source bit-field. |
| 8413 | FieldDecl *BitField = Args[0]->getSourceBitField(); |
| 8414 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
| 8415 | << DestType.isVolatileQualified() |
| 8416 | << (BitField ? BitField->getDeclName() : DeclarationName()) |
| 8417 | << (BitField != nullptr) |
| 8418 | << Args[0]->getSourceRange(); |
| 8419 | if (BitField) |
| 8420 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
| 8421 | break; |
| 8422 | } |
| 8423 | |
| 8424 | case FK_NonConstLValueReferenceBindingToVectorElement: |
| 8425 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 8426 | << DestType.isVolatileQualified() |
| 8427 | << Args[0]->getSourceRange(); |
| 8428 | break; |
| 8429 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8430 | case FK_RValueReferenceBindingToLValue: |
| 8431 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8432 | << DestType.getNonReferenceType() << OnlyArg->getType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8433 | << Args[0]->getSourceRange(); |
| 8434 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8435 | |
Richard Trieu | f956a49 | 2015-05-16 01:27:03 +0000 | [diff] [blame] | 8436 | case FK_ReferenceInitDropsQualifiers: { |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8437 | QualType SourceType = OnlyArg->getType(); |
Richard Trieu | f956a49 | 2015-05-16 01:27:03 +0000 | [diff] [blame] | 8438 | QualType NonRefType = DestType.getNonReferenceType(); |
| 8439 | Qualifiers DroppedQualifiers = |
| 8440 | SourceType.getQualifiers() - NonRefType.getQualifiers(); |
| 8441 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8442 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
Richard Trieu | f956a49 | 2015-05-16 01:27:03 +0000 | [diff] [blame] | 8443 | << SourceType |
| 8444 | << NonRefType |
| 8445 | << DroppedQualifiers.getCVRQualifiers() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8446 | << Args[0]->getSourceRange(); |
| 8447 | break; |
Richard Trieu | f956a49 | 2015-05-16 01:27:03 +0000 | [diff] [blame] | 8448 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8449 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8450 | case FK_ReferenceInitFailed: |
| 8451 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 8452 | << DestType.getNonReferenceType() |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8453 | << OnlyArg->isLValue() |
| 8454 | << OnlyArg->getType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8455 | << Args[0]->getSourceRange(); |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 8456 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8457 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8458 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8459 | case FK_ConversionFailed: { |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8460 | QualType FromType = OnlyArg->getType(); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8461 | PartialDiagnostic PDiag = S.PDiag(diag::err_init_conversion_failed) |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8462 | << (int)Entity.getKind() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8463 | << DestType |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8464 | << OnlyArg->isLValue() |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8465 | << FromType |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8466 | << Args[0]->getSourceRange(); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8467 | S.HandleFunctionTypeMismatch(PDiag, FromType, DestType); |
| 8468 | S.Diag(Kind.getLocation(), PDiag); |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 8469 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8470 | break; |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8471 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8472 | |
| 8473 | case FK_ConversionFromPropertyFailed: |
| 8474 | // No-op. This error has already been reported. |
| 8475 | break; |
| 8476 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8477 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 8478 | SourceRange R; |
| 8479 | |
David Majnemer | bd38544 | 2015-04-10 04:52:06 +0000 | [diff] [blame] | 8480 | auto *InitList = dyn_cast<InitListExpr>(Args[0]); |
Benjamin Kramer | c4284e3 | 2015-09-23 16:03:53 +0000 | [diff] [blame] | 8481 | if (InitList && InitList->getNumInits() >= 1) { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 8482 | R = SourceRange(InitList->getInit(0)->getEndLoc(), InitList->getEndLoc()); |
Benjamin Kramer | c4284e3 | 2015-09-23 16:03:53 +0000 | [diff] [blame] | 8483 | } else { |
| 8484 | assert(Args.size() > 1 && "Expected multiple initializers!"); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 8485 | R = SourceRange(Args.front()->getEndLoc(), Args.back()->getEndLoc()); |
Benjamin Kramer | c4284e3 | 2015-09-23 16:03:53 +0000 | [diff] [blame] | 8486 | } |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8487 | |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 8488 | R.setBegin(S.getLocForEndOfToken(R.getBegin())); |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 8489 | if (Kind.isCStyleOrFunctionalCast()) |
| 8490 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 8491 | << R; |
| 8492 | else |
| 8493 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 8494 | << /*scalar=*/2 << R; |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8495 | break; |
| 8496 | } |
| 8497 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 8498 | case FK_ParenthesizedListInitForScalar: |
| 8499 | S.Diag(Kind.getLocation(), diag::err_list_init_in_parens) |
| 8500 | << 0 << Entity.getType() << Args[0]->getSourceRange(); |
| 8501 | break; |
| 8502 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8503 | case FK_ReferenceBindingToInitList: |
| 8504 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 8505 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 8506 | break; |
| 8507 | |
| 8508 | case FK_InitListBadDestinationType: |
| 8509 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 8510 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 8511 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8512 | |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 8513 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8514 | case FK_ConstructorOverloadFailed: { |
| 8515 | SourceRange ArgsRange; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8516 | if (Args.size()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8517 | ArgsRange = |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 8518 | SourceRange(Args.front()->getBeginLoc(), Args.back()->getEndLoc()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8519 | |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 8520 | if (Failure == FK_ListConstructorOverloadFailed) { |
Nico Weber | 9709ebf | 2014-07-08 23:54:25 +0000 | [diff] [blame] | 8521 | assert(Args.size() == 1 && |
| 8522 | "List construction from other than 1 argument."); |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 8523 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8524 | Args = MultiExprArg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 8525 | } |
| 8526 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8527 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 8528 | // bad. |
| 8529 | switch (FailedOverloadResult) { |
| 8530 | case OR_Ambiguous: |
| 8531 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 8532 | << DestType << ArgsRange; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8533 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8534 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8535 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8536 | case OR_No_Viable_Function: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8537 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 8538 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 8539 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 8540 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 8541 | // This is implicit default initialization of a member or |
| 8542 | // base within a constructor. If no viable function was |
Nico Weber | a691689 | 2016-06-10 18:53:04 +0000 | [diff] [blame] | 8543 | // found, notify the user that they need to explicitly |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8544 | // initialize this base/member. |
| 8545 | CXXConstructorDecl *Constructor |
| 8546 | = cast<CXXConstructorDecl>(S.CurContext); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 8547 | const CXXRecordDecl *InheritedFrom = nullptr; |
| 8548 | if (auto Inherited = Constructor->getInheritedConstructor()) |
| 8549 | InheritedFrom = Inherited.getShadowDecl()->getNominatedBaseClass(); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8550 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 8551 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 8552 | << (InheritedFrom ? 2 : Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8553 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 8554 | << /*base=*/0 |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 8555 | << Entity.getType() |
| 8556 | << InheritedFrom; |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8557 | |
| 8558 | RecordDecl *BaseDecl |
| 8559 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 8560 | ->getDecl(); |
| 8561 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 8562 | << S.Context.getTagDeclType(BaseDecl); |
| 8563 | } else { |
| 8564 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 8565 | << (InheritedFrom ? 2 : Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8566 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 8567 | << /*member=*/1 |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 8568 | << Entity.getName() |
| 8569 | << InheritedFrom; |
Alp Toker | 2afa878 | 2014-05-28 12:20:14 +0000 | [diff] [blame] | 8570 | S.Diag(Entity.getDecl()->getLocation(), |
| 8571 | diag::note_member_declared_at); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8572 | |
| 8573 | if (const RecordType *Record |
| 8574 | = Entity.getType()->getAs<RecordType>()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8575 | S.Diag(Record->getDecl()->getLocation(), |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8576 | diag::note_previous_decl) |
| 8577 | << S.Context.getTagDeclType(Record->getDecl()); |
| 8578 | } |
| 8579 | break; |
| 8580 | } |
| 8581 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8582 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 8583 | << DestType << ArgsRange; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8584 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8585 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8586 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8587 | case OR_Deleted: { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8588 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8589 | OverloadingResult Ovl |
| 8590 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 8591 | if (Ovl != OR_Deleted) { |
| 8592 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 8593 | << true << DestType << ArgsRange; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8594 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 8595 | break; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8596 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8597 | |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 8598 | // If this is a defaulted or implicitly-declared function, then |
| 8599 | // it was implicitly deleted. Make it clear that the deletion was |
| 8600 | // implicit. |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 8601 | if (S.isImplicitlyDeleted(Best->Function)) |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 8602 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_special_init) |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 8603 | << S.getSpecialMember(cast<CXXMethodDecl>(Best->Function)) |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 8604 | << DestType << ArgsRange; |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 8605 | else |
| 8606 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 8607 | << true << DestType << ArgsRange; |
| 8608 | |
| 8609 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8610 | break; |
| 8611 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8612 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8613 | case OR_Success: |
| 8614 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8615 | } |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8616 | } |
David Blaikie | 60deeee | 2012-01-17 08:24:58 +0000 | [diff] [blame] | 8617 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8618 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 8619 | case FK_DefaultInitOfConst: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8620 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 8621 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 8622 | // This is implicit default-initialization of a const member in |
| 8623 | // a constructor. Complain that it needs to be explicitly |
| 8624 | // initialized. |
| 8625 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 8626 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 8627 | << (Constructor->getInheritedConstructor() ? 2 : |
| 8628 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8629 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 8630 | << /*const=*/1 |
| 8631 | << Entity.getName(); |
| 8632 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 8633 | << Entity.getName(); |
| 8634 | } else { |
| 8635 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
Nico Weber | 9386c82 | 2014-07-23 05:16:10 +0000 | [diff] [blame] | 8636 | << DestType << (bool)DestType->getAs<RecordType>(); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8637 | } |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 8638 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8639 | |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 8640 | case FK_Incomplete: |
Douglas Gregor | 85f3423 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 8641 | S.RequireCompleteType(Kind.getLocation(), FailedIncompleteType, |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 8642 | diag::err_init_incomplete_type); |
| 8643 | break; |
| 8644 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 8645 | case FK_ListInitializationFailed: { |
| 8646 | // Run the init list checker again to emit diagnostics. |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 8647 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
| 8648 | diagnoseListInit(S, Entity, InitList); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 8649 | break; |
| 8650 | } |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 8651 | |
| 8652 | case FK_PlaceholderType: { |
| 8653 | // FIXME: Already diagnosed! |
| 8654 | break; |
| 8655 | } |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 8656 | |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 8657 | case FK_ExplicitConstructor: { |
| 8658 | S.Diag(Kind.getLocation(), diag::err_selected_explicit_constructor) |
| 8659 | << Args[0]->getSourceRange(); |
| 8660 | OverloadCandidateSet::iterator Best; |
| 8661 | OverloadingResult Ovl |
| 8662 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Matt Beaumont-Gay | 5dcce09 | 2012-04-02 19:05:35 +0000 | [diff] [blame] | 8663 | (void)Ovl; |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 8664 | assert(Ovl == OR_Success && "Inconsistent overload resolution"); |
| 8665 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 8666 | S.Diag(CtorDecl->getLocation(), |
| 8667 | diag::note_explicit_ctor_deduction_guide_here) << false; |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 8668 | break; |
| 8669 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8670 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8671 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 8672 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8673 | return true; |
| 8674 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8675 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8676 | void InitializationSequence::dump(raw_ostream &OS) const { |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8677 | switch (SequenceKind) { |
| 8678 | case FailedSequence: { |
| 8679 | OS << "Failed sequence: "; |
| 8680 | switch (Failure) { |
| 8681 | case FK_TooManyInitsForReference: |
| 8682 | OS << "too many initializers for reference"; |
| 8683 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8684 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 8685 | case FK_ParenthesizedListInitForReference: |
| 8686 | OS << "parenthesized list init for reference"; |
| 8687 | break; |
| 8688 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8689 | case FK_ArrayNeedsInitList: |
| 8690 | OS << "array requires initializer list"; |
| 8691 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8692 | |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 8693 | case FK_AddressOfUnaddressableFunction: |
| 8694 | OS << "address of unaddressable function was taken"; |
| 8695 | break; |
| 8696 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8697 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 8698 | OS << "array requires initializer list or string literal"; |
| 8699 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8700 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 8701 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 8702 | OS << "array requires initializer list or wide string literal"; |
| 8703 | break; |
| 8704 | |
| 8705 | case FK_NarrowStringIntoWideCharArray: |
| 8706 | OS << "narrow string into wide char array"; |
| 8707 | break; |
| 8708 | |
| 8709 | case FK_WideStringIntoCharArray: |
| 8710 | OS << "wide string into char array"; |
| 8711 | break; |
| 8712 | |
| 8713 | case FK_IncompatWideStringIntoWideChar: |
| 8714 | OS << "incompatible wide string into wide char array"; |
| 8715 | break; |
| 8716 | |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 8717 | case FK_PlainStringIntoUTF8Char: |
| 8718 | OS << "plain string literal into char8_t array"; |
| 8719 | break; |
| 8720 | |
| 8721 | case FK_UTF8StringIntoPlainChar: |
| 8722 | OS << "u8 string literal into char array"; |
| 8723 | break; |
| 8724 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8725 | case FK_ArrayTypeMismatch: |
| 8726 | OS << "array type mismatch"; |
| 8727 | break; |
| 8728 | |
| 8729 | case FK_NonConstantArrayInit: |
| 8730 | OS << "non-constant array initializer"; |
| 8731 | break; |
| 8732 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8733 | case FK_AddressOfOverloadFailed: |
| 8734 | OS << "address of overloaded function failed"; |
| 8735 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8736 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8737 | case FK_ReferenceInitOverloadFailed: |
| 8738 | OS << "overload resolution for reference initialization failed"; |
| 8739 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8740 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8741 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 8742 | OS << "non-const lvalue reference bound to temporary"; |
| 8743 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8744 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8745 | case FK_NonConstLValueReferenceBindingToBitfield: |
| 8746 | OS << "non-const lvalue reference bound to bit-field"; |
| 8747 | break; |
| 8748 | |
| 8749 | case FK_NonConstLValueReferenceBindingToVectorElement: |
| 8750 | OS << "non-const lvalue reference bound to vector element"; |
| 8751 | break; |
| 8752 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8753 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 8754 | OS << "non-const lvalue reference bound to unrelated type"; |
| 8755 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8756 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8757 | case FK_RValueReferenceBindingToLValue: |
| 8758 | OS << "rvalue reference bound to an lvalue"; |
| 8759 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8760 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8761 | case FK_ReferenceInitDropsQualifiers: |
| 8762 | OS << "reference initialization drops qualifiers"; |
| 8763 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8764 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8765 | case FK_ReferenceInitFailed: |
| 8766 | OS << "reference initialization failed"; |
| 8767 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8768 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8769 | case FK_ConversionFailed: |
| 8770 | OS << "conversion failed"; |
| 8771 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8772 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8773 | case FK_ConversionFromPropertyFailed: |
| 8774 | OS << "conversion from property failed"; |
| 8775 | break; |
| 8776 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8777 | case FK_TooManyInitsForScalar: |
| 8778 | OS << "too many initializers for scalar"; |
| 8779 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8780 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 8781 | case FK_ParenthesizedListInitForScalar: |
| 8782 | OS << "parenthesized list init for reference"; |
| 8783 | break; |
| 8784 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8785 | case FK_ReferenceBindingToInitList: |
| 8786 | OS << "referencing binding to initializer list"; |
| 8787 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8788 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8789 | case FK_InitListBadDestinationType: |
| 8790 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 8791 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8792 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8793 | case FK_UserConversionOverloadFailed: |
| 8794 | OS << "overloading failed for user-defined conversion"; |
| 8795 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8796 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8797 | case FK_ConstructorOverloadFailed: |
| 8798 | OS << "constructor overloading failed"; |
| 8799 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8800 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8801 | case FK_DefaultInitOfConst: |
| 8802 | OS << "default initialization of a const variable"; |
| 8803 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8804 | |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 8805 | case FK_Incomplete: |
| 8806 | OS << "initialization of incomplete type"; |
| 8807 | break; |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 8808 | |
| 8809 | case FK_ListInitializationFailed: |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 8810 | OS << "list initialization checker failure"; |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 8811 | break; |
| 8812 | |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 8813 | case FK_VariableLengthArrayHasInitializer: |
| 8814 | OS << "variable length array has an initializer"; |
| 8815 | break; |
| 8816 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 8817 | case FK_PlaceholderType: |
| 8818 | OS << "initializer expression isn't contextually valid"; |
| 8819 | break; |
Nick Lewycky | 097f47c | 2011-12-22 20:21:32 +0000 | [diff] [blame] | 8820 | |
| 8821 | case FK_ListConstructorOverloadFailed: |
| 8822 | OS << "list constructor overloading failed"; |
| 8823 | break; |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 8824 | |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 8825 | case FK_ExplicitConstructor: |
| 8826 | OS << "list copy initialization chose explicit constructor"; |
| 8827 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8828 | } |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8829 | OS << '\n'; |
| 8830 | return; |
| 8831 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8832 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8833 | case DependentSequence: |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 8834 | OS << "Dependent sequence\n"; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8835 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8836 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 8837 | case NormalSequence: |
| 8838 | OS << "Normal sequence: "; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8839 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8840 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8841 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8842 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 8843 | if (S != step_begin()) { |
| 8844 | OS << " -> "; |
| 8845 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8846 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8847 | switch (S->Kind) { |
| 8848 | case SK_ResolveAddressOfOverloadedFunction: |
| 8849 | OS << "resolve address of overloaded function"; |
| 8850 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8851 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8852 | case SK_CastDerivedToBaseRValue: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8853 | OS << "derived-to-base (rvalue)"; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8854 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8855 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 8856 | case SK_CastDerivedToBaseXValue: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8857 | OS << "derived-to-base (xvalue)"; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 8858 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8859 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8860 | case SK_CastDerivedToBaseLValue: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8861 | OS << "derived-to-base (lvalue)"; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8862 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8863 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8864 | case SK_BindReference: |
| 8865 | OS << "bind reference to lvalue"; |
| 8866 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8867 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8868 | case SK_BindReferenceToTemporary: |
| 8869 | OS << "bind reference to a temporary"; |
| 8870 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8871 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8872 | case SK_FinalCopy: |
| 8873 | OS << "final copy in class direct-initialization"; |
| 8874 | break; |
| 8875 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 8876 | case SK_ExtraneousCopyToTemporary: |
| 8877 | OS << "extraneous C++03 copy to temporary"; |
| 8878 | break; |
| 8879 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8880 | case SK_UserConversion: |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 8881 | OS << "user-defined conversion via " << *S->Function.Function; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8882 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 8883 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8884 | case SK_QualificationConversionRValue: |
| 8885 | OS << "qualification conversion (rvalue)"; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8886 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8887 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 8888 | case SK_QualificationConversionXValue: |
| 8889 | OS << "qualification conversion (xvalue)"; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8890 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 8891 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8892 | case SK_QualificationConversionLValue: |
| 8893 | OS << "qualification conversion (lvalue)"; |
| 8894 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8895 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 8896 | case SK_AtomicConversion: |
| 8897 | OS << "non-atomic-to-atomic conversion"; |
| 8898 | break; |
| 8899 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 8900 | case SK_LValueToRValue: |
| 8901 | OS << "load (lvalue to rvalue)"; |
| 8902 | break; |
| 8903 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8904 | case SK_ConversionSequence: |
| 8905 | OS << "implicit conversion sequence ("; |
Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 8906 | S->ICS->dump(); // FIXME: use OS |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8907 | OS << ")"; |
| 8908 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8909 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 8910 | case SK_ConversionSequenceNoNarrowing: |
| 8911 | OS << "implicit conversion sequence with narrowing prohibited ("; |
Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 8912 | S->ICS->dump(); // FIXME: use OS |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 8913 | OS << ")"; |
| 8914 | break; |
| 8915 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8916 | case SK_ListInitialization: |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 8917 | OS << "list aggregate initialization"; |
| 8918 | break; |
| 8919 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8920 | case SK_UnwrapInitList: |
| 8921 | OS << "unwrap reference initializer list"; |
| 8922 | break; |
| 8923 | |
| 8924 | case SK_RewrapInitList: |
| 8925 | OS << "rewrap reference initializer list"; |
| 8926 | break; |
| 8927 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8928 | case SK_ConstructorInitialization: |
| 8929 | OS << "constructor initialization"; |
| 8930 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8931 | |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 8932 | case SK_ConstructorInitializationFromList: |
| 8933 | OS << "list initialization via constructor"; |
| 8934 | break; |
| 8935 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8936 | case SK_ZeroInitialization: |
| 8937 | OS << "zero initialization"; |
| 8938 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8939 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8940 | case SK_CAssignment: |
| 8941 | OS << "C assignment"; |
| 8942 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8943 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8944 | case SK_StringInit: |
| 8945 | OS << "string initialization"; |
| 8946 | break; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 8947 | |
| 8948 | case SK_ObjCObjectConversion: |
| 8949 | OS << "Objective-C object conversion"; |
| 8950 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8951 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 8952 | case SK_ArrayLoopIndex: |
| 8953 | OS << "indexing for array initialization loop"; |
| 8954 | break; |
| 8955 | |
| 8956 | case SK_ArrayLoopInit: |
| 8957 | OS << "array initialization loop"; |
| 8958 | break; |
| 8959 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8960 | case SK_ArrayInit: |
| 8961 | OS << "array initialization"; |
| 8962 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8963 | |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 8964 | case SK_GNUArrayInit: |
| 8965 | OS << "array initialization (GNU extension)"; |
| 8966 | break; |
| 8967 | |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 8968 | case SK_ParenthesizedArrayInit: |
| 8969 | OS << "parenthesized array initialization"; |
| 8970 | break; |
| 8971 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8972 | case SK_PassByIndirectCopyRestore: |
| 8973 | OS << "pass by indirect copy and restore"; |
| 8974 | break; |
| 8975 | |
| 8976 | case SK_PassByIndirectRestore: |
| 8977 | OS << "pass by indirect restore"; |
| 8978 | break; |
| 8979 | |
| 8980 | case SK_ProduceObjCObject: |
| 8981 | OS << "Objective-C object retension"; |
| 8982 | break; |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 8983 | |
| 8984 | case SK_StdInitializerList: |
| 8985 | OS << "std::initializer_list from initializer list"; |
| 8986 | break; |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 8987 | |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 8988 | case SK_StdInitializerListConstructorCall: |
| 8989 | OS << "list initialization from std::initializer_list"; |
| 8990 | break; |
| 8991 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 8992 | case SK_OCLSamplerInit: |
| 8993 | OS << "OpenCL sampler_t from integer constant"; |
| 8994 | break; |
| 8995 | |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 8996 | case SK_OCLZeroOpaqueType: |
| 8997 | OS << "OpenCL opaque type from zero"; |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 8998 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8999 | } |
Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 9000 | |
| 9001 | OS << " [" << S->Type.getAsString() << ']'; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9002 | } |
Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 9003 | |
| 9004 | OS << '\n'; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9005 | } |
| 9006 | |
| 9007 | void InitializationSequence::dump() const { |
| 9008 | dump(llvm::errs()); |
| 9009 | } |
| 9010 | |
Nico Weber | 3d7f00d | 2018-06-19 23:19:34 +0000 | [diff] [blame] | 9011 | static bool NarrowingErrs(const LangOptions &L) { |
| 9012 | return L.CPlusPlus11 && |
| 9013 | (!L.MicrosoftExt || L.isCompatibleWithMSVC(LangOptions::MSVC2015)); |
| 9014 | } |
| 9015 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 9016 | static void DiagnoseNarrowingInInitList(Sema &S, |
| 9017 | const ImplicitConversionSequence &ICS, |
| 9018 | QualType PreNarrowingType, |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9019 | QualType EntityType, |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9020 | const Expr *PostInit) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9021 | const StandardConversionSequence *SCS = nullptr; |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9022 | switch (ICS.getKind()) { |
| 9023 | case ImplicitConversionSequence::StandardConversion: |
| 9024 | SCS = &ICS.Standard; |
| 9025 | break; |
| 9026 | case ImplicitConversionSequence::UserDefinedConversion: |
| 9027 | SCS = &ICS.UserDefined.After; |
| 9028 | break; |
| 9029 | case ImplicitConversionSequence::AmbiguousConversion: |
| 9030 | case ImplicitConversionSequence::EllipsisConversion: |
| 9031 | case ImplicitConversionSequence::BadConversion: |
| 9032 | return; |
| 9033 | } |
| 9034 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9035 | // C++11 [dcl.init.list]p7: Check whether this is a narrowing conversion. |
| 9036 | APValue ConstantValue; |
Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 9037 | QualType ConstantType; |
| 9038 | switch (SCS->getNarrowingKind(S.Context, PostInit, ConstantValue, |
| 9039 | ConstantType)) { |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9040 | case NK_Not_Narrowing: |
Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 9041 | case NK_Dependent_Narrowing: |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9042 | // No narrowing occurred. |
| 9043 | return; |
| 9044 | |
| 9045 | case NK_Type_Narrowing: |
| 9046 | // This was a floating-to-integer conversion, which is always considered a |
| 9047 | // narrowing conversion even if the value is a constant and can be |
| 9048 | // represented exactly as an integer. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9049 | S.Diag(PostInit->getBeginLoc(), NarrowingErrs(S.getLangOpts()) |
Nico Weber | 3d7f00d | 2018-06-19 23:19:34 +0000 | [diff] [blame] | 9050 | ? diag::ext_init_list_type_narrowing |
| 9051 | : diag::warn_init_list_type_narrowing) |
| 9052 | << PostInit->getSourceRange() |
| 9053 | << PreNarrowingType.getLocalUnqualifiedType() |
| 9054 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9055 | break; |
| 9056 | |
| 9057 | case NK_Constant_Narrowing: |
| 9058 | // A constant value was narrowed. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9059 | S.Diag(PostInit->getBeginLoc(), |
Nico Weber | 3d7f00d | 2018-06-19 23:19:34 +0000 | [diff] [blame] | 9060 | NarrowingErrs(S.getLangOpts()) |
| 9061 | ? diag::ext_init_list_constant_narrowing |
| 9062 | : diag::warn_init_list_constant_narrowing) |
| 9063 | << PostInit->getSourceRange() |
| 9064 | << ConstantValue.getAsString(S.getASTContext(), ConstantType) |
| 9065 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9066 | break; |
| 9067 | |
| 9068 | case NK_Variable_Narrowing: |
| 9069 | // A variable's value may have been narrowed. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9070 | S.Diag(PostInit->getBeginLoc(), |
Nico Weber | 3d7f00d | 2018-06-19 23:19:34 +0000 | [diff] [blame] | 9071 | NarrowingErrs(S.getLangOpts()) |
| 9072 | ? diag::ext_init_list_variable_narrowing |
| 9073 | : diag::warn_init_list_variable_narrowing) |
| 9074 | << PostInit->getSourceRange() |
| 9075 | << PreNarrowingType.getLocalUnqualifiedType() |
| 9076 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9077 | break; |
| 9078 | } |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 9079 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 9080 | SmallString<128> StaticCast; |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 9081 | llvm::raw_svector_ostream OS(StaticCast); |
| 9082 | OS << "static_cast<"; |
| 9083 | if (const TypedefType *TT = EntityType->getAs<TypedefType>()) { |
| 9084 | // It's important to use the typedef's name if there is one so that the |
| 9085 | // fixit doesn't break code using types like int64_t. |
| 9086 | // |
| 9087 | // FIXME: This will break if the typedef requires qualification. But |
| 9088 | // getQualifiedNameAsString() includes non-machine-parsable components. |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 9089 | OS << *TT->getDecl(); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 9090 | } else if (const BuiltinType *BT = EntityType->getAs<BuiltinType>()) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9091 | OS << BT->getName(S.getLangOpts()); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 9092 | else { |
| 9093 | // Oops, we didn't find the actual type of the variable. Don't emit a fixit |
| 9094 | // with a broken cast. |
| 9095 | return; |
| 9096 | } |
| 9097 | OS << ">("; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9098 | S.Diag(PostInit->getBeginLoc(), diag::note_init_list_narrowing_silence) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 9099 | << PostInit->getSourceRange() |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9100 | << FixItHint::CreateInsertion(PostInit->getBeginLoc(), OS.str()) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 9101 | << FixItHint::CreateInsertion( |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 9102 | S.getLocForEndOfToken(PostInit->getEndLoc()), ")"); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 9103 | } |
| 9104 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 9105 | //===----------------------------------------------------------------------===// |
| 9106 | // Initialization helper functions |
| 9107 | //===----------------------------------------------------------------------===// |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 9108 | bool |
| 9109 | Sema::CanPerformCopyInitialization(const InitializedEntity &Entity, |
| 9110 | ExprResult Init) { |
| 9111 | if (Init.isInvalid()) |
| 9112 | return false; |
| 9113 | |
| 9114 | Expr *InitE = Init.get(); |
| 9115 | assert(InitE && "No initialization expression"); |
| 9116 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9117 | InitializationKind Kind = |
| 9118 | InitializationKind::CreateCopy(InitE->getBeginLoc(), SourceLocation()); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 9119 | InitializationSequence Seq(*this, Entity, Kind, InitE); |
Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 9120 | return !Seq.Failed(); |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 9121 | } |
| 9122 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9123 | ExprResult |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 9124 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 9125 | SourceLocation EqualLoc, |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 9126 | ExprResult Init, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 9127 | bool TopLevelOfInitList, |
| 9128 | bool AllowExplicit) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 9129 | if (Init.isInvalid()) |
| 9130 | return ExprError(); |
| 9131 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 9132 | Expr *InitE = Init.get(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 9133 | assert(InitE && "No initialization expression?"); |
| 9134 | |
| 9135 | if (EqualLoc.isInvalid()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9136 | EqualLoc = InitE->getBeginLoc(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 9137 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9138 | InitializationKind Kind = InitializationKind::CreateCopy( |
| 9139 | InitE->getBeginLoc(), EqualLoc, AllowExplicit); |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 9140 | InitializationSequence Seq(*this, Entity, Kind, InitE, TopLevelOfInitList); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 9141 | |
Alex Lorenz | de69ff9 | 2017-05-16 10:23:58 +0000 | [diff] [blame] | 9142 | // Prevent infinite recursion when performing parameter copy-initialization. |
| 9143 | const bool ShouldTrackCopy = |
| 9144 | Entity.isParameterKind() && Seq.isConstructorInitialization(); |
| 9145 | if (ShouldTrackCopy) { |
| 9146 | if (llvm::find(CurrentParameterCopyTypes, Entity.getType()) != |
| 9147 | CurrentParameterCopyTypes.end()) { |
| 9148 | Seq.SetOverloadFailure( |
| 9149 | InitializationSequence::FK_ConstructorOverloadFailed, |
| 9150 | OR_No_Viable_Function); |
| 9151 | |
| 9152 | // Try to give a meaningful diagnostic note for the problematic |
| 9153 | // constructor. |
| 9154 | const auto LastStep = Seq.step_end() - 1; |
| 9155 | assert(LastStep->Kind == |
| 9156 | InitializationSequence::SK_ConstructorInitialization); |
| 9157 | const FunctionDecl *Function = LastStep->Function.Function; |
| 9158 | auto Candidate = |
| 9159 | llvm::find_if(Seq.getFailedCandidateSet(), |
| 9160 | [Function](const OverloadCandidate &Candidate) -> bool { |
| 9161 | return Candidate.Viable && |
| 9162 | Candidate.Function == Function && |
| 9163 | Candidate.Conversions.size() > 0; |
| 9164 | }); |
| 9165 | if (Candidate != Seq.getFailedCandidateSet().end() && |
| 9166 | Function->getNumParams() > 0) { |
| 9167 | Candidate->Viable = false; |
| 9168 | Candidate->FailureKind = ovl_fail_bad_conversion; |
| 9169 | Candidate->Conversions[0].setBad(BadConversionSequence::no_conversion, |
| 9170 | InitE, |
| 9171 | Function->getParamDecl(0)->getType()); |
| 9172 | } |
| 9173 | } |
| 9174 | CurrentParameterCopyTypes.push_back(Entity.getType()); |
| 9175 | } |
| 9176 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 9177 | ExprResult Result = Seq.Perform(*this, Entity, Kind, InitE); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9178 | |
Alex Lorenz | de69ff9 | 2017-05-16 10:23:58 +0000 | [diff] [blame] | 9179 | if (ShouldTrackCopy) |
| 9180 | CurrentParameterCopyTypes.pop_back(); |
| 9181 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9182 | return Result; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 9183 | } |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9184 | |
Richard Smith | 1363e8f | 2017-09-07 07:22:36 +0000 | [diff] [blame] | 9185 | /// Determine whether RD is, or is derived from, a specialization of CTD. |
| 9186 | static bool isOrIsDerivedFromSpecializationOf(CXXRecordDecl *RD, |
| 9187 | ClassTemplateDecl *CTD) { |
| 9188 | auto NotSpecialization = [&] (const CXXRecordDecl *Candidate) { |
| 9189 | auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Candidate); |
| 9190 | return !CTSD || !declaresSameEntity(CTSD->getSpecializedTemplate(), CTD); |
| 9191 | }; |
| 9192 | return !(NotSpecialization(RD) && RD->forallBases(NotSpecialization)); |
| 9193 | } |
| 9194 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9195 | QualType Sema::DeduceTemplateSpecializationFromInitializer( |
| 9196 | TypeSourceInfo *TSInfo, const InitializedEntity &Entity, |
| 9197 | const InitializationKind &Kind, MultiExprArg Inits) { |
| 9198 | auto *DeducedTST = dyn_cast<DeducedTemplateSpecializationType>( |
| 9199 | TSInfo->getType()->getContainedDeducedType()); |
| 9200 | assert(DeducedTST && "not a deduced template specialization type"); |
| 9201 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9202 | auto TemplateName = DeducedTST->getTemplateName(); |
Richard Smith | cff4201 | 2018-09-28 03:18:53 +0000 | [diff] [blame] | 9203 | if (TemplateName.isDependent()) |
| 9204 | return Context.DependentTy; |
| 9205 | |
| 9206 | // We can only perform deduction for class templates. |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9207 | auto *Template = |
| 9208 | dyn_cast_or_null<ClassTemplateDecl>(TemplateName.getAsTemplateDecl()); |
| 9209 | if (!Template) { |
| 9210 | Diag(Kind.getLocation(), |
| 9211 | diag::err_deduced_non_class_template_specialization_type) |
| 9212 | << (int)getTemplateNameKindForDiagnostics(TemplateName) << TemplateName; |
| 9213 | if (auto *TD = TemplateName.getAsTemplateDecl()) |
| 9214 | Diag(TD->getLocation(), diag::note_template_decl_here); |
| 9215 | return QualType(); |
| 9216 | } |
| 9217 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9218 | // Can't deduce from dependent arguments. |
Richard Smith | 8eeb16f | 2018-09-10 20:31:03 +0000 | [diff] [blame] | 9219 | if (Expr::hasAnyTypeDependentArguments(Inits)) { |
| 9220 | Diag(TSInfo->getTypeLoc().getBeginLoc(), |
| 9221 | diag::warn_cxx14_compat_class_template_argument_deduction) |
| 9222 | << TSInfo->getTypeLoc().getSourceRange() << 0; |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9223 | return Context.DependentTy; |
Richard Smith | 8eeb16f | 2018-09-10 20:31:03 +0000 | [diff] [blame] | 9224 | } |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9225 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9226 | // FIXME: Perform "exact type" matching first, per CWG discussion? |
| 9227 | // Or implement this via an implied 'T(T) -> T' deduction guide? |
| 9228 | |
| 9229 | // FIXME: Do we need/want a std::initializer_list<T> special case? |
| 9230 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9231 | // Look up deduction guides, including those synthesized from constructors. |
| 9232 | // |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9233 | // C++1z [over.match.class.deduct]p1: |
| 9234 | // A set of functions and function templates is formed comprising: |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9235 | // - For each constructor of the class template designated by the |
| 9236 | // template-name, a function template [...] |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9237 | // - For each deduction-guide, a function or function template [...] |
| 9238 | DeclarationNameInfo NameInfo( |
| 9239 | Context.DeclarationNames.getCXXDeductionGuideName(Template), |
| 9240 | TSInfo->getTypeLoc().getEndLoc()); |
| 9241 | LookupResult Guides(*this, NameInfo, LookupOrdinaryName); |
| 9242 | LookupQualifiedName(Guides, Template->getDeclContext()); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9243 | |
| 9244 | // FIXME: Do not diagnose inaccessible deduction guides. The standard isn't |
| 9245 | // clear on this, but they're not found by name so access does not apply. |
| 9246 | Guides.suppressDiagnostics(); |
| 9247 | |
| 9248 | // Figure out if this is list-initialization. |
| 9249 | InitListExpr *ListInit = |
| 9250 | (Inits.size() == 1 && Kind.getKind() != InitializationKind::IK_Direct) |
| 9251 | ? dyn_cast<InitListExpr>(Inits[0]) |
| 9252 | : nullptr; |
| 9253 | |
| 9254 | // C++1z [over.match.class.deduct]p1: |
| 9255 | // Initialization and overload resolution are performed as described in |
| 9256 | // [dcl.init] and [over.match.ctor], [over.match.copy], or [over.match.list] |
| 9257 | // (as appropriate for the type of initialization performed) for an object |
| 9258 | // of a hypothetical class type, where the selected functions and function |
| 9259 | // templates are considered to be the constructors of that class type |
| 9260 | // |
| 9261 | // Since we know we're initializing a class type of a type unrelated to that |
| 9262 | // of the initializer, this reduces to something fairly reasonable. |
| 9263 | OverloadCandidateSet Candidates(Kind.getLocation(), |
| 9264 | OverloadCandidateSet::CSK_Normal); |
| 9265 | OverloadCandidateSet::iterator Best; |
Eric Fiselier | 73b51ae | 2019-01-17 21:44:24 +0000 | [diff] [blame] | 9266 | |
| 9267 | bool HasAnyDeductionGuide = false; |
| 9268 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9269 | auto tryToResolveOverload = |
| 9270 | [&](bool OnlyListConstructors) -> OverloadingResult { |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 9271 | Candidates.clear(OverloadCandidateSet::CSK_Normal); |
Eric Fiselier | 73b51ae | 2019-01-17 21:44:24 +0000 | [diff] [blame] | 9272 | HasAnyDeductionGuide = false; |
| 9273 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9274 | for (auto I = Guides.begin(), E = Guides.end(); I != E; ++I) { |
| 9275 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9276 | if (D->isInvalidDecl()) |
| 9277 | continue; |
| 9278 | |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9279 | auto *TD = dyn_cast<FunctionTemplateDecl>(D); |
| 9280 | auto *GD = dyn_cast_or_null<CXXDeductionGuideDecl>( |
| 9281 | TD ? TD->getTemplatedDecl() : dyn_cast<FunctionDecl>(D)); |
| 9282 | if (!GD) |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9283 | continue; |
| 9284 | |
Eric Fiselier | 73b51ae | 2019-01-17 21:44:24 +0000 | [diff] [blame] | 9285 | if (!GD->isImplicit()) |
| 9286 | HasAnyDeductionGuide = true; |
| 9287 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9288 | // C++ [over.match.ctor]p1: (non-list copy-initialization from non-class) |
| 9289 | // For copy-initialization, the candidate functions are all the |
| 9290 | // converting constructors (12.3.1) of that class. |
| 9291 | // C++ [over.match.copy]p1: (non-list copy-initialization from class) |
| 9292 | // The converting constructors of T are candidate functions. |
| 9293 | if (Kind.isCopyInit() && !ListInit) { |
Richard Smith | afe4aa8 | 2017-02-10 02:19:05 +0000 | [diff] [blame] | 9294 | // Only consider converting constructors. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9295 | if (GD->isExplicit()) |
Richard Smith | afe4aa8 | 2017-02-10 02:19:05 +0000 | [diff] [blame] | 9296 | continue; |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9297 | |
| 9298 | // When looking for a converting constructor, deduction guides that |
Richard Smith | afe4aa8 | 2017-02-10 02:19:05 +0000 | [diff] [blame] | 9299 | // could never be called with one argument are not interesting to |
| 9300 | // check or note. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9301 | if (GD->getMinRequiredArguments() > 1 || |
| 9302 | (GD->getNumParams() == 0 && !GD->isVariadic())) |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9303 | continue; |
| 9304 | } |
| 9305 | |
| 9306 | // C++ [over.match.list]p1.1: (first phase list initialization) |
| 9307 | // Initially, the candidate functions are the initializer-list |
| 9308 | // constructors of the class T |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9309 | if (OnlyListConstructors && !isInitListConstructor(GD)) |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9310 | continue; |
| 9311 | |
| 9312 | // C++ [over.match.list]p1.2: (second phase list initialization) |
| 9313 | // the candidate functions are all the constructors of the class T |
| 9314 | // C++ [over.match.ctor]p1: (all other cases) |
| 9315 | // the candidate functions are all the constructors of the class of |
| 9316 | // the object being initialized |
| 9317 | |
| 9318 | // C++ [over.best.ics]p4: |
| 9319 | // When [...] the constructor [...] is a candidate by |
| 9320 | // - [over.match.copy] (in all cases) |
| 9321 | // FIXME: The "second phase of [over.match.list] case can also |
| 9322 | // theoretically happen here, but it's not clear whether we can |
| 9323 | // ever have a parameter of the right type. |
| 9324 | bool SuppressUserConversions = Kind.isCopyInit(); |
| 9325 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9326 | if (TD) |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9327 | AddTemplateOverloadCandidate(TD, I.getPair(), /*ExplicitArgs*/ nullptr, |
| 9328 | Inits, Candidates, |
| 9329 | SuppressUserConversions); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9330 | else |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9331 | AddOverloadCandidate(GD, I.getPair(), Inits, Candidates, |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9332 | SuppressUserConversions); |
| 9333 | } |
| 9334 | return Candidates.BestViableFunction(*this, Kind.getLocation(), Best); |
| 9335 | }; |
| 9336 | |
| 9337 | OverloadingResult Result = OR_No_Viable_Function; |
| 9338 | |
| 9339 | // C++11 [over.match.list]p1, per DR1467: for list-initialization, first |
| 9340 | // try initializer-list constructors. |
| 9341 | if (ListInit) { |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9342 | bool TryListConstructors = true; |
| 9343 | |
| 9344 | // Try list constructors unless the list is empty and the class has one or |
| 9345 | // more default constructors, in which case those constructors win. |
| 9346 | if (!ListInit->getNumInits()) { |
| 9347 | for (NamedDecl *D : Guides) { |
| 9348 | auto *FD = dyn_cast<FunctionDecl>(D->getUnderlyingDecl()); |
| 9349 | if (FD && FD->getMinRequiredArguments() == 0) { |
| 9350 | TryListConstructors = false; |
| 9351 | break; |
| 9352 | } |
| 9353 | } |
Richard Smith | 1363e8f | 2017-09-07 07:22:36 +0000 | [diff] [blame] | 9354 | } else if (ListInit->getNumInits() == 1) { |
| 9355 | // C++ [over.match.class.deduct]: |
| 9356 | // As an exception, the first phase in [over.match.list] (considering |
| 9357 | // initializer-list constructors) is omitted if the initializer list |
| 9358 | // consists of a single expression of type cv U, where U is a |
| 9359 | // specialization of C or a class derived from a specialization of C. |
| 9360 | Expr *E = ListInit->getInit(0); |
| 9361 | auto *RD = E->getType()->getAsCXXRecordDecl(); |
| 9362 | if (!isa<InitListExpr>(E) && RD && |
Erik Pilkington | dd0b344 | 2018-07-26 23:40:42 +0000 | [diff] [blame] | 9363 | isCompleteType(Kind.getLocation(), E->getType()) && |
Richard Smith | 1363e8f | 2017-09-07 07:22:36 +0000 | [diff] [blame] | 9364 | isOrIsDerivedFromSpecializationOf(RD, Template)) |
| 9365 | TryListConstructors = false; |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9366 | } |
| 9367 | |
| 9368 | if (TryListConstructors) |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9369 | Result = tryToResolveOverload(/*OnlyListConstructor*/true); |
| 9370 | // Then unwrap the initializer list and try again considering all |
| 9371 | // constructors. |
| 9372 | Inits = MultiExprArg(ListInit->getInits(), ListInit->getNumInits()); |
| 9373 | } |
| 9374 | |
| 9375 | // If list-initialization fails, or if we're doing any other kind of |
| 9376 | // initialization, we (eventually) consider constructors. |
| 9377 | if (Result == OR_No_Viable_Function) |
| 9378 | Result = tryToResolveOverload(/*OnlyListConstructor*/false); |
| 9379 | |
| 9380 | switch (Result) { |
| 9381 | case OR_Ambiguous: |
| 9382 | Diag(Kind.getLocation(), diag::err_deduced_class_template_ctor_ambiguous) |
| 9383 | << TemplateName; |
| 9384 | // FIXME: For list-initialization candidates, it'd usually be better to |
| 9385 | // list why they were not viable when given the initializer list itself as |
| 9386 | // an argument. |
| 9387 | Candidates.NoteCandidates(*this, OCD_ViableCandidates, Inits); |
| 9388 | return QualType(); |
| 9389 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9390 | case OR_No_Viable_Function: { |
| 9391 | CXXRecordDecl *Primary = |
| 9392 | cast<ClassTemplateDecl>(Template)->getTemplatedDecl(); |
| 9393 | bool Complete = |
| 9394 | isCompleteType(Kind.getLocation(), Context.getTypeDeclType(Primary)); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9395 | Diag(Kind.getLocation(), |
| 9396 | Complete ? diag::err_deduced_class_template_ctor_no_viable |
| 9397 | : diag::err_deduced_class_template_incomplete) |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9398 | << TemplateName << !Guides.empty(); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9399 | Candidates.NoteCandidates(*this, OCD_AllCandidates, Inits); |
| 9400 | return QualType(); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9401 | } |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9402 | |
| 9403 | case OR_Deleted: { |
| 9404 | Diag(Kind.getLocation(), diag::err_deduced_class_template_deleted) |
| 9405 | << TemplateName; |
| 9406 | NoteDeletedFunction(Best->Function); |
| 9407 | return QualType(); |
| 9408 | } |
| 9409 | |
| 9410 | case OR_Success: |
| 9411 | // C++ [over.match.list]p1: |
| 9412 | // In copy-list-initialization, if an explicit constructor is chosen, the |
| 9413 | // initialization is ill-formed. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9414 | if (Kind.isCopyInit() && ListInit && |
| 9415 | cast<CXXDeductionGuideDecl>(Best->Function)->isExplicit()) { |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9416 | bool IsDeductionGuide = !Best->Function->isImplicit(); |
| 9417 | Diag(Kind.getLocation(), diag::err_deduced_class_template_explicit) |
| 9418 | << TemplateName << IsDeductionGuide; |
| 9419 | Diag(Best->Function->getLocation(), |
| 9420 | diag::note_explicit_ctor_deduction_guide_here) |
| 9421 | << IsDeductionGuide; |
| 9422 | return QualType(); |
| 9423 | } |
| 9424 | |
| 9425 | // Make sure we didn't select an unusable deduction guide, and mark it |
| 9426 | // as referenced. |
| 9427 | DiagnoseUseOfDecl(Best->Function, Kind.getLocation()); |
| 9428 | MarkFunctionReferenced(Kind.getLocation(), Best->Function); |
| 9429 | break; |
| 9430 | } |
| 9431 | |
| 9432 | // C++ [dcl.type.class.deduct]p1: |
| 9433 | // The placeholder is replaced by the return type of the function selected |
| 9434 | // by overload resolution for class template deduction. |
Richard Smith | 8eeb16f | 2018-09-10 20:31:03 +0000 | [diff] [blame] | 9435 | QualType DeducedType = |
| 9436 | SubstAutoType(TSInfo->getType(), Best->Function->getReturnType()); |
| 9437 | Diag(TSInfo->getTypeLoc().getBeginLoc(), |
| 9438 | diag::warn_cxx14_compat_class_template_argument_deduction) |
| 9439 | << TSInfo->getTypeLoc().getSourceRange() << 1 << DeducedType; |
Eric Fiselier | 73b51ae | 2019-01-17 21:44:24 +0000 | [diff] [blame] | 9440 | |
| 9441 | // Warn if CTAD was used on a type that does not have any user-defined |
| 9442 | // deduction guides. |
| 9443 | if (!HasAnyDeductionGuide) { |
| 9444 | Diag(TSInfo->getTypeLoc().getBeginLoc(), |
| 9445 | diag::warn_ctad_maybe_unsupported) |
| 9446 | << TemplateName; |
| 9447 | Diag(Template->getLocation(), diag::note_suppress_ctad_maybe_unsupported); |
| 9448 | } |
| 9449 | |
Richard Smith | 8eeb16f | 2018-09-10 20:31:03 +0000 | [diff] [blame] | 9450 | return DeducedType; |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9451 | } |