Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1 | //===--- SemaInit.cpp - Semantic Analysis for Initializers ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Sebastian Redl | 26bcc94 | 2011-09-24 17:47:39 +0000 | [diff] [blame] | 10 | // This file implements semantic analysis for initializers. |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 11 | // |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTContext.h" |
John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 98cee2f | 2009-05-27 16:10:08 +0000 | [diff] [blame] | 16 | #include "clang/AST/ExprCXX.h" |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprObjC.h" |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 18 | #include "clang/AST/ExprOpenMP.h" |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeLoc.h" |
James Molloy | 9eef265 | 2014-06-20 14:35:13 +0000 | [diff] [blame] | 20 | #include "clang/Basic/TargetInfo.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "clang/Sema/Designator.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 22 | #include "clang/Sema/Initialization.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "clang/Sema/Lookup.h" |
| 24 | #include "clang/Sema/SemaInternal.h" |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/APInt.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ErrorHandling.h" |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 29 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 30 | using namespace clang; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 32 | //===----------------------------------------------------------------------===// |
| 33 | // Sema Initialization Checking |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 36 | /// Check whether T is compatible with a wide character type (wchar_t, |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 37 | /// char16_t or char32_t). |
| 38 | static bool IsWideCharCompatible(QualType T, ASTContext &Context) { |
| 39 | if (Context.typesAreCompatible(Context.getWideCharType(), T)) |
| 40 | return true; |
| 41 | if (Context.getLangOpts().CPlusPlus || Context.getLangOpts().C11) { |
| 42 | return Context.typesAreCompatible(Context.Char16Ty, T) || |
| 43 | Context.typesAreCompatible(Context.Char32Ty, T); |
| 44 | } |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | enum StringInitFailureKind { |
| 49 | SIF_None, |
| 50 | SIF_NarrowStringIntoWideChar, |
| 51 | SIF_WideStringIntoChar, |
| 52 | SIF_IncompatWideStringIntoWideChar, |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 53 | SIF_UTF8StringIntoPlainChar, |
| 54 | SIF_PlainStringIntoUTF8Char, |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 55 | SIF_Other |
| 56 | }; |
| 57 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 58 | /// 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] | 59 | /// expression by means of string initialization. Returns SIF_None if so, |
| 60 | /// otherwise returns a StringInitFailureKind that describes why the |
| 61 | /// initialization would not work. |
| 62 | static StringInitFailureKind IsStringInit(Expr *Init, const ArrayType *AT, |
| 63 | ASTContext &Context) { |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 64 | if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT)) |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 65 | return SIF_Other; |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 66 | |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 67 | // See if this is a string literal or @encode. |
| 68 | Init = Init->IgnoreParens(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 69 | |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 70 | // Handle @encode, which is a narrow string. |
| 71 | if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType()) |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 72 | return SIF_None; |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 73 | |
| 74 | // Otherwise we can only handle string literals. |
| 75 | StringLiteral *SL = dyn_cast<StringLiteral>(Init); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 76 | if (!SL) |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 77 | return SIF_Other; |
Eli Friedman | 42a8465 | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 78 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 79 | const QualType ElemTy = |
| 80 | Context.getCanonicalType(AT->getElementType()).getUnqualifiedType(); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 81 | |
| 82 | switch (SL->getKind()) { |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 83 | case StringLiteral::UTF8: |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 84 | // char8_t array can be initialized with a UTF-8 string. |
| 85 | if (ElemTy->isChar8Type()) |
| 86 | return SIF_None; |
| 87 | LLVM_FALLTHROUGH; |
| 88 | case StringLiteral::Ascii: |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 89 | // char array can be initialized with a narrow string. |
| 90 | // Only allow char x[] = "foo"; not char x[] = L"foo"; |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 91 | if (ElemTy->isCharType()) |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 92 | return (SL->getKind() == StringLiteral::UTF8 && |
| 93 | Context.getLangOpts().Char8) |
| 94 | ? SIF_UTF8StringIntoPlainChar |
| 95 | : SIF_None; |
| 96 | if (ElemTy->isChar8Type()) |
| 97 | return SIF_PlainStringIntoUTF8Char; |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 98 | if (IsWideCharCompatible(ElemTy, Context)) |
| 99 | return SIF_NarrowStringIntoWideChar; |
| 100 | return SIF_Other; |
| 101 | // C99 6.7.8p15 (with correction from DR343), or C11 6.7.9p15: |
| 102 | // "An array with element type compatible with a qualified or unqualified |
| 103 | // version of wchar_t, char16_t, or char32_t may be initialized by a wide |
| 104 | // string literal with the corresponding encoding prefix (L, u, or U, |
| 105 | // respectively), optionally enclosed in braces. |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 106 | case StringLiteral::UTF16: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 107 | if (Context.typesAreCompatible(Context.Char16Ty, ElemTy)) |
| 108 | return SIF_None; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 109 | if (ElemTy->isCharType() || ElemTy->isChar8Type()) |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 110 | return SIF_WideStringIntoChar; |
| 111 | if (IsWideCharCompatible(ElemTy, Context)) |
| 112 | return SIF_IncompatWideStringIntoWideChar; |
| 113 | return SIF_Other; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 114 | case StringLiteral::UTF32: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 115 | if (Context.typesAreCompatible(Context.Char32Ty, ElemTy)) |
| 116 | return SIF_None; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 117 | if (ElemTy->isCharType() || ElemTy->isChar8Type()) |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 118 | return SIF_WideStringIntoChar; |
| 119 | if (IsWideCharCompatible(ElemTy, Context)) |
| 120 | return SIF_IncompatWideStringIntoWideChar; |
| 121 | return SIF_Other; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 122 | case StringLiteral::Wide: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 123 | if (Context.typesAreCompatible(Context.getWideCharType(), ElemTy)) |
| 124 | return SIF_None; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 125 | if (ElemTy->isCharType() || ElemTy->isChar8Type()) |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 126 | return SIF_WideStringIntoChar; |
| 127 | if (IsWideCharCompatible(ElemTy, Context)) |
| 128 | return SIF_IncompatWideStringIntoWideChar; |
| 129 | return SIF_Other; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 130 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 132 | llvm_unreachable("missed a StringLiteral kind?"); |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Hans Wennborg | 950f318 | 2013-05-16 09:22:40 +0000 | [diff] [blame] | 135 | static StringInitFailureKind IsStringInit(Expr *init, QualType declType, |
| 136 | ASTContext &Context) { |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 137 | const ArrayType *arrayType = Context.getAsArrayType(declType); |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 138 | if (!arrayType) |
Hans Wennborg | 950f318 | 2013-05-16 09:22:40 +0000 | [diff] [blame] | 139 | return SIF_Other; |
| 140 | return IsStringInit(init, arrayType, Context); |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 143 | /// Update the type of a string literal, including any surrounding parentheses, |
| 144 | /// to match the type of the object which it is initializing. |
| 145 | static void updateStringLiteralType(Expr *E, QualType Ty) { |
Richard Smith | d74b1606 | 2013-05-06 00:35:47 +0000 | [diff] [blame] | 146 | while (true) { |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 147 | E->setType(Ty); |
Richard Smith | d74b1606 | 2013-05-06 00:35:47 +0000 | [diff] [blame] | 148 | if (isa<StringLiteral>(E) || isa<ObjCEncodeExpr>(E)) |
| 149 | break; |
| 150 | else if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) |
| 151 | E = PE->getSubExpr(); |
| 152 | else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) |
| 153 | E = UO->getSubExpr(); |
| 154 | else if (GenericSelectionExpr *GSE = dyn_cast<GenericSelectionExpr>(E)) |
| 155 | E = GSE->getResultExpr(); |
| 156 | else |
| 157 | llvm_unreachable("unexpected expr in string literal init"); |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 158 | } |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 159 | } |
| 160 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 161 | static void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT, |
| 162 | Sema &S) { |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 163 | // Get the length of the string as parsed. |
Ben Langmuir | 577b393 | 2015-01-26 19:04:10 +0000 | [diff] [blame] | 164 | auto *ConstantArrayTy = |
Ben Langmuir | 7b30f53 | 2015-01-26 20:01:17 +0000 | [diff] [blame] | 165 | cast<ConstantArrayType>(Str->getType()->getAsArrayTypeUnsafe()); |
Ben Langmuir | 577b393 | 2015-01-26 19:04:10 +0000 | [diff] [blame] | 166 | uint64_t StrLength = ConstantArrayTy->getSize().getZExtValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 168 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 169 | // 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] | 170 | // being initialized to a string literal. |
Benjamin Kramer | e073177 | 2012-08-04 17:00:46 +0000 | [diff] [blame] | 171 | llvm::APInt ConstVal(32, StrLength); |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 172 | // Return a new array type (C99 6.7.8p22). |
John McCall | c5b8225 | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 173 | DeclT = S.Context.getConstantArrayType(IAT->getElementType(), |
| 174 | ConstVal, |
| 175 | ArrayType::Normal, 0); |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 176 | updateStringLiteralType(Str, DeclT); |
Chris Lattner | 94e6c4b | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 177 | return; |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 178 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 179 | |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 180 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 181 | |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 182 | // We have an array of character type with known size. However, |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 183 | // the size may be smaller or larger than the string we are initializing. |
| 184 | // FIXME: Avoid truncation for 64-bit length strings. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 185 | if (S.getLangOpts().CPlusPlus) { |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 186 | if (StringLiteral *SL = dyn_cast<StringLiteral>(Str->IgnoreParens())) { |
Anders Carlsson | d162fb8 | 2011-04-14 00:41:11 +0000 | [diff] [blame] | 187 | // For Pascal strings it's OK to strip off the terminating null character, |
| 188 | // so the example below is valid: |
| 189 | // |
| 190 | // unsigned char a[2] = "\pa"; |
| 191 | if (SL->isPascal()) |
| 192 | StrLength--; |
| 193 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 194 | |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 195 | // [dcl.init.string]p2 |
| 196 | if (StrLength > CAT->getSize().getZExtValue()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 197 | S.Diag(Str->getBeginLoc(), |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 198 | diag::err_initializer_string_for_char_array_too_long) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 199 | << Str->getSourceRange(); |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 200 | } else { |
| 201 | // C99 6.7.8p14. |
| 202 | if (StrLength-1 > CAT->getSize().getZExtValue()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 203 | S.Diag(Str->getBeginLoc(), |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 204 | diag::ext_initializer_string_for_char_array_too_long) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 205 | << Str->getSourceRange(); |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 206 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 207 | |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 208 | // Set the type to the actual size that we are initializing. If we have |
| 209 | // something like: |
| 210 | // char x[1] = "foo"; |
| 211 | // then this will set the string literal's type to char[1]. |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 212 | updateStringLiteralType(Str, DeclT); |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 215 | //===----------------------------------------------------------------------===// |
| 216 | // Semantic checking for initializer lists. |
| 217 | //===----------------------------------------------------------------------===// |
| 218 | |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 219 | namespace { |
| 220 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 221 | /// Semantic checking for initializer lists. |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 222 | /// |
| 223 | /// The InitListChecker class contains a set of routines that each |
| 224 | /// handle the initialization of a certain kind of entity, e.g., |
| 225 | /// arrays, vectors, struct/union types, scalars, etc. The |
| 226 | /// InitListChecker itself performs a recursive walk of the subobject |
| 227 | /// structure of the type to be initialized, while stepping through |
| 228 | /// the initializer list one element at a time. The IList and Index |
| 229 | /// parameters to each of the Check* routines contain the active |
| 230 | /// (syntactic) initializer list and the index into that initializer |
| 231 | /// list that represents the current initializer. Each routine is |
| 232 | /// responsible for moving that Index forward as it consumes elements. |
| 233 | /// |
| 234 | /// Each Check* routine also has a StructuredList/StructuredIndex |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 235 | /// arguments, which contains the current "structured" (semantic) |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 236 | /// initializer list and the index into that initializer list where we |
| 237 | /// are copying initializers as we map them over to the semantic |
| 238 | /// list. Once we have completed our recursive walk of the subobject |
| 239 | /// structure, we will have constructed a full semantic initializer |
| 240 | /// list. |
| 241 | /// |
| 242 | /// C99 designators cause changes in the initializer list traversal, |
| 243 | /// because they make the initialization "jump" into a specific |
| 244 | /// subobject and then continue the initialization from that |
| 245 | /// point. CheckDesignatedInitializer() recursively steps into the |
| 246 | /// designated subobject and manages backing out the recursion to |
| 247 | /// initialize the subobjects after the one designated. |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 248 | class InitListChecker { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 249 | Sema &SemaRef; |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 250 | bool hadError; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 251 | bool VerifyOnly; // no diagnostics, no structure building |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 252 | bool TreatUnavailableAsInvalid; // Used only in VerifyOnly mode. |
Benjamin Kramer | 6b441d6 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 253 | llvm::DenseMap<InitListExpr *, InitListExpr *> SyntacticToSemantic; |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 254 | InitListExpr *FullyStructuredList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 255 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 256 | void CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 257 | InitListExpr *ParentIList, QualType T, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 258 | unsigned &Index, InitListExpr *StructuredList, |
Eli Friedman | c616c5f | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 259 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 260 | void CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 261 | InitListExpr *IList, QualType &T, |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 262 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 263 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 264 | void CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 265 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 266 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 267 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 268 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 269 | unsigned &StructuredIndex, |
| 270 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 271 | void CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 272 | InitListExpr *IList, QualType ElemType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 273 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 274 | InitListExpr *StructuredList, |
| 275 | unsigned &StructuredIndex); |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 276 | void CheckComplexType(const InitializedEntity &Entity, |
| 277 | InitListExpr *IList, QualType DeclType, |
| 278 | unsigned &Index, |
| 279 | InitListExpr *StructuredList, |
| 280 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 281 | void CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 282 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 283 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 284 | InitListExpr *StructuredList, |
| 285 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 286 | void CheckReferenceType(const InitializedEntity &Entity, |
| 287 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 288 | unsigned &Index, |
| 289 | InitListExpr *StructuredList, |
| 290 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 291 | void CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 292 | InitListExpr *IList, QualType DeclType, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 293 | InitListExpr *StructuredList, |
| 294 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 295 | void CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 73eb7cd | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 296 | InitListExpr *IList, QualType DeclType, |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 297 | CXXRecordDecl::base_class_range Bases, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 298 | RecordDecl::field_iterator Field, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 299 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 300 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 301 | unsigned &StructuredIndex, |
| 302 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 303 | void CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 304 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 305 | llvm::APSInt elementIndex, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 306 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 307 | InitListExpr *StructuredList, |
| 308 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 309 | bool CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 310 | InitListExpr *IList, DesignatedInitExpr *DIE, |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 311 | unsigned DesigIdx, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 312 | QualType &CurrentObjectType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 313 | RecordDecl::field_iterator *NextField, |
| 314 | llvm::APSInt *NextElementIndex, |
| 315 | unsigned &Index, |
| 316 | InitListExpr *StructuredList, |
| 317 | unsigned &StructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 318 | bool FinishSubobjectInit, |
| 319 | bool TopLevelObject); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 320 | InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 321 | QualType CurrentObjectType, |
| 322 | InitListExpr *StructuredList, |
| 323 | unsigned StructuredIndex, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 324 | SourceRange InitRange, |
| 325 | bool IsFullyOverwritten = false); |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 326 | void UpdateStructuredListElement(InitListExpr *StructuredList, |
| 327 | unsigned &StructuredIndex, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 328 | Expr *expr); |
| 329 | int numArrayElements(QualType DeclType); |
| 330 | int numStructUnionElements(QualType DeclType); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 331 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 332 | static ExprResult PerformEmptyInit(Sema &SemaRef, |
| 333 | SourceLocation Loc, |
| 334 | const InitializedEntity &Entity, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 335 | bool VerifyOnly, |
| 336 | bool TreatUnavailableAsInvalid); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 337 | |
| 338 | // Explanation on the "FillWithNoInit" mode: |
| 339 | // |
| 340 | // Assume we have the following definitions (Case#1): |
| 341 | // struct P { char x[6][6]; } xp = { .x[1] = "bar" }; |
| 342 | // struct PP { struct P lp; } l = { .lp = xp, .lp.x[1][2] = 'f' }; |
| 343 | // |
| 344 | // l.lp.x[1][0..1] should not be filled with implicit initializers because the |
| 345 | // "base" initializer "xp" will provide values for them; l.lp.x[1] will be "baf". |
| 346 | // |
| 347 | // But if we have (Case#2): |
| 348 | // struct PP l = { .lp = xp, .lp.x[1] = { [2] = 'f' } }; |
| 349 | // |
| 350 | // l.lp.x[1][0..1] are implicitly initialized and do not use values from the |
| 351 | // "base" initializer; l.lp.x[1] will be "\0\0f\0\0\0". |
| 352 | // |
| 353 | // To distinguish Case#1 from Case#2, and also to avoid leaving many "holes" |
| 354 | // in the InitListExpr, the "holes" in Case#1 are filled not with empty |
| 355 | // initializers but with special "NoInitExpr" place holders, which tells the |
| 356 | // CodeGen not to generate any initializers for these parts. |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 357 | void FillInEmptyInitForBase(unsigned Init, const CXXBaseSpecifier &Base, |
| 358 | const InitializedEntity &ParentEntity, |
| 359 | InitListExpr *ILE, bool &RequiresSecondPass, |
| 360 | bool FillWithNoInit); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 361 | void FillInEmptyInitForField(unsigned Init, FieldDecl *Field, |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 362 | const InitializedEntity &ParentEntity, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 363 | InitListExpr *ILE, bool &RequiresSecondPass, |
| 364 | bool FillWithNoInit = false); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 365 | void FillInEmptyInitializations(const InitializedEntity &Entity, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 366 | InitListExpr *ILE, bool &RequiresSecondPass, |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 367 | InitListExpr *OuterILE, unsigned OuterIndex, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 368 | bool FillWithNoInit = false); |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 369 | bool CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 370 | Expr *InitExpr, FieldDecl *Field, |
| 371 | bool TopLevelObject); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 372 | void CheckEmptyInitializable(const InitializedEntity &Entity, |
| 373 | SourceLocation Loc); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 374 | |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 375 | public: |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 376 | InitListChecker(Sema &S, const InitializedEntity &Entity, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 377 | InitListExpr *IL, QualType &T, bool VerifyOnly, |
| 378 | bool TreatUnavailableAsInvalid); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 379 | bool HadError() { return hadError; } |
| 380 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 381 | // Retrieves the fully-structured initializer list used for |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 382 | // semantic analysis and code generation. |
| 383 | InitListExpr *getFullyStructuredList() const { return FullyStructuredList; } |
| 384 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 385 | |
Chris Lattner | 9ececce | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 386 | } // end anonymous namespace |
Chris Lattner | d9ae05b | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 387 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 388 | ExprResult InitListChecker::PerformEmptyInit(Sema &SemaRef, |
| 389 | SourceLocation Loc, |
| 390 | const InitializedEntity &Entity, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 391 | bool VerifyOnly, |
| 392 | bool TreatUnavailableAsInvalid) { |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 393 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 394 | true); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 395 | MultiExprArg SubInit; |
| 396 | Expr *InitExpr; |
| 397 | InitListExpr DummyInitList(SemaRef.Context, Loc, None, Loc); |
| 398 | |
| 399 | // C++ [dcl.init.aggr]p7: |
| 400 | // If there are fewer initializer-clauses in the list than there are |
| 401 | // members in the aggregate, then each member not explicitly initialized |
| 402 | // ... |
Nico Weber | bcb70ee | 2014-07-02 23:51:09 +0000 | [diff] [blame] | 403 | bool EmptyInitList = SemaRef.getLangOpts().CPlusPlus11 && |
| 404 | Entity.getType()->getBaseElementTypeUnsafe()->isRecordType(); |
| 405 | if (EmptyInitList) { |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 406 | // C++1y / DR1070: |
| 407 | // shall be initialized [...] from an empty initializer list. |
| 408 | // |
| 409 | // We apply the resolution of this DR to C++11 but not C++98, since C++98 |
| 410 | // does not have useful semantics for initialization from an init list. |
| 411 | // We treat this as copy-initialization, because aggregate initialization |
| 412 | // always performs copy-initialization on its elements. |
| 413 | // |
| 414 | // Only do this if we're initializing a class type, to avoid filling in |
| 415 | // the initializer list where possible. |
| 416 | InitExpr = VerifyOnly ? &DummyInitList : new (SemaRef.Context) |
| 417 | InitListExpr(SemaRef.Context, Loc, None, Loc); |
| 418 | InitExpr->setType(SemaRef.Context.VoidTy); |
| 419 | SubInit = InitExpr; |
| 420 | Kind = InitializationKind::CreateCopy(Loc, Loc); |
| 421 | } else { |
| 422 | // C++03: |
| 423 | // shall be value-initialized. |
| 424 | } |
| 425 | |
| 426 | InitializationSequence InitSeq(SemaRef, Entity, Kind, SubInit); |
Nico Weber | bcb70ee | 2014-07-02 23:51:09 +0000 | [diff] [blame] | 427 | // libstdc++4.6 marks the vector default constructor as explicit in |
| 428 | // _GLIBCXX_DEBUG mode, so recover using the C++03 logic in that case. |
| 429 | // stlport does so too. Look for std::__debug for libstdc++, and for |
| 430 | // std:: for stlport. This is effectively a compiler-side implementation of |
| 431 | // LWG2193. |
| 432 | if (!InitSeq && EmptyInitList && InitSeq.getFailureKind() == |
| 433 | InitializationSequence::FK_ExplicitConstructor) { |
| 434 | OverloadCandidateSet::iterator Best; |
| 435 | OverloadingResult O = |
| 436 | InitSeq.getFailedCandidateSet() |
| 437 | .BestViableFunction(SemaRef, Kind.getLocation(), Best); |
| 438 | (void)O; |
| 439 | assert(O == OR_Success && "Inconsistent overload resolution"); |
| 440 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 441 | CXXRecordDecl *R = CtorDecl->getParent(); |
| 442 | |
| 443 | if (CtorDecl->getMinRequiredArguments() == 0 && |
| 444 | CtorDecl->isExplicit() && R->getDeclName() && |
| 445 | SemaRef.SourceMgr.isInSystemHeader(CtorDecl->getLocation())) { |
Nico Weber | bcb70ee | 2014-07-02 23:51:09 +0000 | [diff] [blame] | 446 | bool IsInStd = false; |
| 447 | for (NamespaceDecl *ND = dyn_cast<NamespaceDecl>(R->getDeclContext()); |
Nico Weber | 5752ad0 | 2014-07-03 00:38:25 +0000 | [diff] [blame] | 448 | ND && !IsInStd; ND = dyn_cast<NamespaceDecl>(ND->getParent())) { |
Nico Weber | bcb70ee | 2014-07-02 23:51:09 +0000 | [diff] [blame] | 449 | if (SemaRef.getStdNamespace()->InEnclosingNamespaceSetOf(ND)) |
| 450 | IsInStd = true; |
| 451 | } |
| 452 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 453 | if (IsInStd && llvm::StringSwitch<bool>(R->getName()) |
Nico Weber | bcb70ee | 2014-07-02 23:51:09 +0000 | [diff] [blame] | 454 | .Cases("basic_string", "deque", "forward_list", true) |
| 455 | .Cases("list", "map", "multimap", "multiset", true) |
| 456 | .Cases("priority_queue", "queue", "set", "stack", true) |
| 457 | .Cases("unordered_map", "unordered_set", "vector", true) |
| 458 | .Default(false)) { |
| 459 | InitSeq.InitializeFrom( |
| 460 | SemaRef, Entity, |
| 461 | InitializationKind::CreateValue(Loc, Loc, Loc, true), |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 462 | MultiExprArg(), /*TopLevelOfInitList=*/false, |
| 463 | TreatUnavailableAsInvalid); |
Nico Weber | bcb70ee | 2014-07-02 23:51:09 +0000 | [diff] [blame] | 464 | // Emit a warning for this. System header warnings aren't shown |
| 465 | // by default, but people working on system headers should see it. |
| 466 | if (!VerifyOnly) { |
| 467 | SemaRef.Diag(CtorDecl->getLocation(), |
| 468 | diag::warn_invalid_initializer_from_system_header); |
David Majnemer | 9588a95 | 2015-08-21 06:44:10 +0000 | [diff] [blame] | 469 | if (Entity.getKind() == InitializedEntity::EK_Member) |
| 470 | SemaRef.Diag(Entity.getDecl()->getLocation(), |
| 471 | diag::note_used_in_initialization_here); |
| 472 | else if (Entity.getKind() == InitializedEntity::EK_ArrayElement) |
| 473 | SemaRef.Diag(Loc, diag::note_used_in_initialization_here); |
Nico Weber | bcb70ee | 2014-07-02 23:51:09 +0000 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | } |
| 477 | } |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 478 | if (!InitSeq) { |
| 479 | if (!VerifyOnly) { |
| 480 | InitSeq.Diagnose(SemaRef, Entity, Kind, SubInit); |
| 481 | if (Entity.getKind() == InitializedEntity::EK_Member) |
| 482 | SemaRef.Diag(Entity.getDecl()->getLocation(), |
| 483 | diag::note_in_omitted_aggregate_initializer) |
| 484 | << /*field*/1 << Entity.getDecl(); |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 485 | else if (Entity.getKind() == InitializedEntity::EK_ArrayElement) { |
| 486 | bool IsTrailingArrayNewMember = |
| 487 | Entity.getParent() && |
| 488 | Entity.getParent()->isVariableLengthArrayNew(); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 489 | SemaRef.Diag(Loc, diag::note_in_omitted_aggregate_initializer) |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 490 | << (IsTrailingArrayNewMember ? 2 : /*array element*/0) |
| 491 | << Entity.getElementIndex(); |
| 492 | } |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 493 | } |
| 494 | return ExprError(); |
| 495 | } |
| 496 | |
| 497 | return VerifyOnly ? ExprResult(static_cast<Expr *>(nullptr)) |
| 498 | : InitSeq.Perform(SemaRef, Entity, Kind, SubInit); |
| 499 | } |
| 500 | |
| 501 | void InitListChecker::CheckEmptyInitializable(const InitializedEntity &Entity, |
| 502 | SourceLocation Loc) { |
| 503 | assert(VerifyOnly && |
| 504 | "CheckEmptyInitializable is only inteded for verification mode."); |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 505 | if (PerformEmptyInit(SemaRef, Loc, Entity, /*VerifyOnly*/true, |
| 506 | TreatUnavailableAsInvalid).isInvalid()) |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 507 | hadError = true; |
| 508 | } |
| 509 | |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 510 | void InitListChecker::FillInEmptyInitForBase( |
| 511 | unsigned Init, const CXXBaseSpecifier &Base, |
| 512 | const InitializedEntity &ParentEntity, InitListExpr *ILE, |
| 513 | bool &RequiresSecondPass, bool FillWithNoInit) { |
| 514 | assert(Init < ILE->getNumInits() && "should have been expanded"); |
| 515 | |
| 516 | InitializedEntity BaseEntity = InitializedEntity::InitializeBase( |
| 517 | SemaRef.Context, &Base, false, &ParentEntity); |
| 518 | |
| 519 | if (!ILE->getInit(Init)) { |
| 520 | ExprResult BaseInit = |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 521 | FillWithNoInit |
| 522 | ? new (SemaRef.Context) NoInitExpr(Base.getType()) |
| 523 | : PerformEmptyInit(SemaRef, ILE->getEndLoc(), BaseEntity, |
| 524 | /*VerifyOnly*/ false, TreatUnavailableAsInvalid); |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 525 | if (BaseInit.isInvalid()) { |
| 526 | hadError = true; |
| 527 | return; |
| 528 | } |
| 529 | |
| 530 | ILE->setInit(Init, BaseInit.getAs<Expr>()); |
| 531 | } else if (InitListExpr *InnerILE = |
| 532 | dyn_cast<InitListExpr>(ILE->getInit(Init))) { |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 533 | FillInEmptyInitializations(BaseEntity, InnerILE, RequiresSecondPass, |
| 534 | ILE, Init, FillWithNoInit); |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 535 | } else if (DesignatedInitUpdateExpr *InnerDIUE = |
| 536 | dyn_cast<DesignatedInitUpdateExpr>(ILE->getInit(Init))) { |
| 537 | FillInEmptyInitializations(BaseEntity, InnerDIUE->getUpdater(), |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 538 | RequiresSecondPass, ILE, Init, |
| 539 | /*FillWithNoInit =*/true); |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 543 | void InitListChecker::FillInEmptyInitForField(unsigned Init, FieldDecl *Field, |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 544 | const InitializedEntity &ParentEntity, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 545 | InitListExpr *ILE, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 546 | bool &RequiresSecondPass, |
| 547 | bool FillWithNoInit) { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 548 | SourceLocation Loc = ILE->getEndLoc(); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 549 | unsigned NumInits = ILE->getNumInits(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 550 | InitializedEntity MemberEntity |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 551 | = InitializedEntity::InitializeMember(Field, &ParentEntity); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 552 | |
| 553 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) |
| 554 | if (!RType->getDecl()->isUnion()) |
| 555 | assert(Init < NumInits && "This ILE should have been expanded"); |
| 556 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 557 | if (Init >= NumInits || !ILE->getInit(Init)) { |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 558 | if (FillWithNoInit) { |
| 559 | Expr *Filler = new (SemaRef.Context) NoInitExpr(Field->getType()); |
| 560 | if (Init < NumInits) |
| 561 | ILE->setInit(Init, Filler); |
| 562 | else |
| 563 | ILE->updateInit(SemaRef.Context, Init, Filler); |
| 564 | return; |
| 565 | } |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 566 | // C++1y [dcl.init.aggr]p7: |
| 567 | // If there are fewer initializer-clauses in the list than there are |
| 568 | // members in the aggregate, then each member not explicitly initialized |
| 569 | // shall be initialized from its brace-or-equal-initializer [...] |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 570 | if (Field->hasInClassInitializer()) { |
Reid Kleckner | d60b82f | 2014-11-17 23:36:45 +0000 | [diff] [blame] | 571 | ExprResult DIE = SemaRef.BuildCXXDefaultInitExpr(Loc, Field); |
| 572 | if (DIE.isInvalid()) { |
| 573 | hadError = true; |
| 574 | return; |
| 575 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 576 | SemaRef.checkInitializerLifetime(MemberEntity, DIE.get()); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 577 | if (Init < NumInits) |
Reid Kleckner | d60b82f | 2014-11-17 23:36:45 +0000 | [diff] [blame] | 578 | ILE->setInit(Init, DIE.get()); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 579 | else { |
Reid Kleckner | d60b82f | 2014-11-17 23:36:45 +0000 | [diff] [blame] | 580 | ILE->updateInit(SemaRef.Context, Init, DIE.get()); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 581 | RequiresSecondPass = true; |
| 582 | } |
| 583 | return; |
| 584 | } |
| 585 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 586 | if (Field->getType()->isReferenceType()) { |
| 587 | // C++ [dcl.init.aggr]p9: |
| 588 | // If an incomplete or empty initializer-list leaves a |
| 589 | // member of reference type uninitialized, the program is |
| 590 | // ill-formed. |
| 591 | SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized) |
| 592 | << Field->getType() |
| 593 | << ILE->getSyntacticForm()->getSourceRange(); |
| 594 | SemaRef.Diag(Field->getLocation(), |
| 595 | diag::note_uninit_reference_member); |
| 596 | hadError = true; |
| 597 | return; |
| 598 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 599 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 600 | ExprResult MemberInit = PerformEmptyInit(SemaRef, Loc, MemberEntity, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 601 | /*VerifyOnly*/false, |
| 602 | TreatUnavailableAsInvalid); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 603 | if (MemberInit.isInvalid()) { |
| 604 | hadError = true; |
| 605 | return; |
| 606 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 607 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 608 | if (hadError) { |
| 609 | // Do nothing |
| 610 | } else if (Init < NumInits) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 611 | ILE->setInit(Init, MemberInit.getAs<Expr>()); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 612 | } else if (!isa<ImplicitValueInitExpr>(MemberInit.get())) { |
| 613 | // Empty initialization requires a constructor call, so |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 614 | // extend the initializer list to include the constructor |
| 615 | // call and make a note that we'll need to take another pass |
| 616 | // through the initializer list. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 617 | ILE->updateInit(SemaRef.Context, Init, MemberInit.getAs<Expr>()); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 618 | RequiresSecondPass = true; |
| 619 | } |
| 620 | } else if (InitListExpr *InnerILE |
| 621 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 622 | FillInEmptyInitializations(MemberEntity, InnerILE, |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 623 | RequiresSecondPass, ILE, Init, FillWithNoInit); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 624 | else if (DesignatedInitUpdateExpr *InnerDIUE |
| 625 | = dyn_cast<DesignatedInitUpdateExpr>(ILE->getInit(Init))) |
| 626 | FillInEmptyInitializations(MemberEntity, InnerDIUE->getUpdater(), |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 627 | RequiresSecondPass, ILE, Init, |
| 628 | /*FillWithNoInit =*/true); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 631 | /// Recursively replaces NULL values within the given initializer list |
| 632 | /// with expressions that perform value-initialization of the |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 633 | /// appropriate type, and finish off the InitListExpr formation. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 634 | void |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 635 | InitListChecker::FillInEmptyInitializations(const InitializedEntity &Entity, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 636 | InitListExpr *ILE, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 637 | bool &RequiresSecondPass, |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 638 | InitListExpr *OuterILE, |
| 639 | unsigned OuterIndex, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 640 | bool FillWithNoInit) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 641 | assert((ILE->getType() != SemaRef.Context.VoidTy) && |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 642 | "Should not have void type"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 643 | |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 644 | // If this is a nested initializer list, we might have changed its contents |
| 645 | // (and therefore some of its properties, such as instantiation-dependence) |
| 646 | // while filling it in. Inform the outer initializer list so that its state |
| 647 | // can be updated to match. |
| 648 | // FIXME: We should fully build the inner initializers before constructing |
| 649 | // the outer InitListExpr instead of mutating AST nodes after they have |
| 650 | // been used as subexpressions of other nodes. |
| 651 | struct UpdateOuterILEWithUpdatedInit { |
| 652 | InitListExpr *Outer; |
| 653 | unsigned OuterIndex; |
| 654 | ~UpdateOuterILEWithUpdatedInit() { |
| 655 | if (Outer) |
| 656 | Outer->setInit(OuterIndex, Outer->getInit(OuterIndex)); |
| 657 | } |
| 658 | } UpdateOuterRAII = {OuterILE, OuterIndex}; |
| 659 | |
Richard Smith | 382bc51 | 2017-02-23 22:41:47 +0000 | [diff] [blame] | 660 | // A transparent ILE is not performing aggregate initialization and should |
| 661 | // not be filled in. |
| 662 | if (ILE->isTransparent()) |
| 663 | return; |
| 664 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 665 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 666 | const RecordDecl *RDecl = RType->getDecl(); |
| 667 | if (RDecl->isUnion() && ILE->getInitializedFieldInUnion()) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 668 | FillInEmptyInitForField(0, ILE->getInitializedFieldInUnion(), |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 669 | Entity, ILE, RequiresSecondPass, FillWithNoInit); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 670 | else if (RDecl->isUnion() && isa<CXXRecordDecl>(RDecl) && |
| 671 | cast<CXXRecordDecl>(RDecl)->hasInClassInitializer()) { |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 672 | for (auto *Field : RDecl->fields()) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 673 | if (Field->hasInClassInitializer()) { |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 674 | FillInEmptyInitForField(0, Field, Entity, ILE, RequiresSecondPass, |
| 675 | FillWithNoInit); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 676 | break; |
| 677 | } |
| 678 | } |
| 679 | } else { |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 680 | // The fields beyond ILE->getNumInits() are default initialized, so in |
| 681 | // order to leave them uninitialized, the ILE is expanded and the extra |
| 682 | // fields are then filled with NoInitExpr. |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 683 | unsigned NumElems = numStructUnionElements(ILE->getType()); |
| 684 | if (RDecl->hasFlexibleArrayMember()) |
| 685 | ++NumElems; |
| 686 | if (ILE->getNumInits() < NumElems) |
| 687 | ILE->resizeInits(SemaRef.Context, NumElems); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 688 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 689 | unsigned Init = 0; |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 690 | |
| 691 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RDecl)) { |
| 692 | for (auto &Base : CXXRD->bases()) { |
| 693 | if (hadError) |
| 694 | return; |
| 695 | |
| 696 | FillInEmptyInitForBase(Init, Base, Entity, ILE, RequiresSecondPass, |
| 697 | FillWithNoInit); |
| 698 | ++Init; |
| 699 | } |
| 700 | } |
| 701 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 702 | for (auto *Field : RDecl->fields()) { |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 703 | if (Field->isUnnamedBitfield()) |
| 704 | continue; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 705 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 706 | if (hadError) |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 707 | return; |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 708 | |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 709 | FillInEmptyInitForField(Init, Field, Entity, ILE, RequiresSecondPass, |
| 710 | FillWithNoInit); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 711 | if (hadError) |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 712 | return; |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 713 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 714 | ++Init; |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 715 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 716 | // Only look at the first initialization of a union. |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 717 | if (RDecl->isUnion()) |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 718 | break; |
| 719 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 723 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 724 | |
| 725 | QualType ElementType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 726 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 727 | InitializedEntity ElementEntity = Entity; |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 728 | unsigned NumInits = ILE->getNumInits(); |
| 729 | unsigned NumElements = NumInits; |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 730 | if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 731 | ElementType = AType->getElementType(); |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 732 | if (const auto *CAType = dyn_cast<ConstantArrayType>(AType)) |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 733 | NumElements = CAType->getSize().getZExtValue(); |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 734 | // For an array new with an unknown bound, ask for one additional element |
| 735 | // in order to populate the array filler. |
| 736 | if (Entity.isVariableLengthArrayNew()) |
| 737 | ++NumElements; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 738 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 739 | 0, Entity); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 740 | } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 741 | ElementType = VType->getElementType(); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 742 | NumElements = VType->getNumElements(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 743 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 744 | 0, Entity); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 745 | } else |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 746 | ElementType = ILE->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 747 | |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 748 | for (unsigned Init = 0; Init != NumElements; ++Init) { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 749 | if (hadError) |
| 750 | return; |
| 751 | |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 752 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement || |
| 753 | ElementEntity.getKind() == InitializedEntity::EK_VectorElement) |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 754 | ElementEntity.setElementIndex(Init); |
| 755 | |
Richard Smith | 3e26863 | 2018-05-23 23:41:38 +0000 | [diff] [blame] | 756 | if (Init >= NumInits && ILE->hasArrayFiller()) |
| 757 | return; |
| 758 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 759 | Expr *InitExpr = (Init < NumInits ? ILE->getInit(Init) : nullptr); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 760 | if (!InitExpr && Init < NumInits && ILE->hasArrayFiller()) |
| 761 | ILE->setInit(Init, ILE->getArrayFiller()); |
| 762 | else if (!InitExpr && !ILE->hasArrayFiller()) { |
| 763 | Expr *Filler = nullptr; |
| 764 | |
| 765 | if (FillWithNoInit) |
| 766 | Filler = new (SemaRef.Context) NoInitExpr(ElementType); |
| 767 | else { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 768 | ExprResult ElementInit = |
| 769 | PerformEmptyInit(SemaRef, ILE->getEndLoc(), ElementEntity, |
| 770 | /*VerifyOnly*/ false, TreatUnavailableAsInvalid); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 771 | if (ElementInit.isInvalid()) { |
| 772 | hadError = true; |
| 773 | return; |
| 774 | } |
| 775 | |
| 776 | Filler = ElementInit.getAs<Expr>(); |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | if (hadError) { |
| 780 | // Do nothing |
| 781 | } else if (Init < NumInits) { |
Argyrios Kyrtzidis | 446bcf2 | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 782 | // For arrays, just set the expression used for value-initialization |
| 783 | // of the "holes" in the array. |
| 784 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 785 | ILE->setArrayFiller(Filler); |
Argyrios Kyrtzidis | 446bcf2 | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 786 | else |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 787 | ILE->setInit(Init, Filler); |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 788 | } else { |
| 789 | // For arrays, just set the expression used for value-initialization |
| 790 | // of the rest of elements and exit. |
| 791 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) { |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 792 | ILE->setArrayFiller(Filler); |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 793 | return; |
| 794 | } |
| 795 | |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 796 | if (!isa<ImplicitValueInitExpr>(Filler) && !isa<NoInitExpr>(Filler)) { |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 797 | // Empty initialization requires a constructor call, so |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 798 | // extend the initializer list to include the constructor |
| 799 | // call and make a note that we'll need to take another pass |
| 800 | // through the initializer list. |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 801 | ILE->updateInit(SemaRef.Context, Init, Filler); |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 802 | RequiresSecondPass = true; |
| 803 | } |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 804 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 805 | } else if (InitListExpr *InnerILE |
Argyrios Kyrtzidis | d4590a5d | 2011-10-21 23:02:22 +0000 | [diff] [blame] | 806 | = dyn_cast_or_null<InitListExpr>(InitExpr)) |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 807 | FillInEmptyInitializations(ElementEntity, InnerILE, RequiresSecondPass, |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 808 | ILE, Init, FillWithNoInit); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 809 | else if (DesignatedInitUpdateExpr *InnerDIUE |
| 810 | = dyn_cast_or_null<DesignatedInitUpdateExpr>(InitExpr)) |
| 811 | FillInEmptyInitializations(ElementEntity, InnerDIUE->getUpdater(), |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 812 | RequiresSecondPass, ILE, Init, |
| 813 | /*FillWithNoInit =*/true); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 814 | } |
| 815 | } |
| 816 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 817 | InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 818 | InitListExpr *IL, QualType &T, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 819 | bool VerifyOnly, |
| 820 | bool TreatUnavailableAsInvalid) |
| 821 | : SemaRef(S), VerifyOnly(VerifyOnly), |
| 822 | TreatUnavailableAsInvalid(TreatUnavailableAsInvalid) { |
Richard Smith | 520449d | 2015-02-05 06:15:50 +0000 | [diff] [blame] | 823 | // FIXME: Check that IL isn't already the semantic form of some other |
| 824 | // InitListExpr. If it is, we'd create a broken AST. |
| 825 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 826 | hadError = false; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 827 | |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 828 | FullyStructuredList = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 829 | getStructuredSubobjectInit(IL, 0, T, nullptr, 0, IL->getSourceRange()); |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 830 | CheckExplicitInitList(Entity, IL, T, FullyStructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 831 | /*TopLevelObject=*/true); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 832 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 833 | if (!hadError && !VerifyOnly) { |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 834 | bool RequiresSecondPass = false; |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 835 | FillInEmptyInitializations(Entity, FullyStructuredList, RequiresSecondPass, |
| 836 | /*OuterILE=*/nullptr, /*OuterIndex=*/0); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 837 | if (RequiresSecondPass && !hadError) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 838 | FillInEmptyInitializations(Entity, FullyStructuredList, |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 839 | RequiresSecondPass, nullptr, 0); |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 840 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 844 | // FIXME: use a proper constant |
| 845 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 846 | if (const ConstantArrayType *CAT = |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 847 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 848 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 849 | } |
| 850 | return maxElements; |
| 851 | } |
| 852 | |
| 853 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 854 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 855 | int InitializableMembers = 0; |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 856 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(structDecl)) |
| 857 | InitializableMembers += CXXRD->getNumBases(); |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 858 | for (const auto *Field : structDecl->fields()) |
Douglas Gregor | 556e586 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 859 | if (!Field->isUnnamedBitfield()) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 860 | ++InitializableMembers; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 861 | |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 862 | if (structDecl->isUnion()) |
Eli Friedman | 0e56c82 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 863 | return std::min(InitializableMembers, 1); |
| 864 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 865 | } |
| 866 | |
Richard Smith | 283e207 | 2017-10-03 20:36:00 +0000 | [diff] [blame] | 867 | /// Determine whether Entity is an entity for which it is idiomatic to elide |
| 868 | /// the braces in aggregate initialization. |
| 869 | static bool isIdiomaticBraceElisionEntity(const InitializedEntity &Entity) { |
| 870 | // Recursive initialization of the one and only field within an aggregate |
| 871 | // class is considered idiomatic. This case arises in particular for |
| 872 | // initialization of std::array, where the C++ standard suggests the idiom of |
| 873 | // |
| 874 | // std::array<T, N> arr = {1, 2, 3}; |
| 875 | // |
| 876 | // (where std::array is an aggregate struct containing a single array field. |
| 877 | |
| 878 | // FIXME: Should aggregate initialization of a struct with a single |
| 879 | // base class and no members also suppress the warning? |
| 880 | if (Entity.getKind() != InitializedEntity::EK_Member || !Entity.getParent()) |
| 881 | return false; |
| 882 | |
| 883 | auto *ParentRD = |
| 884 | Entity.getParent()->getType()->castAs<RecordType>()->getDecl(); |
| 885 | if (CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(ParentRD)) |
| 886 | if (CXXRD->getNumBases()) |
| 887 | return false; |
| 888 | |
| 889 | auto FieldIt = ParentRD->field_begin(); |
| 890 | assert(FieldIt != ParentRD->field_end() && |
| 891 | "no fields but have initializer for member?"); |
| 892 | return ++FieldIt == ParentRD->field_end(); |
| 893 | } |
| 894 | |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 895 | /// Check whether the range of the initializer \p ParentIList from element |
| 896 | /// \p Index onwards can be used to initialize an object of type \p T. Update |
| 897 | /// \p Index to indicate how many elements of the list were consumed. |
| 898 | /// |
| 899 | /// This also fills in \p StructuredList, from element \p StructuredIndex |
| 900 | /// onwards, with the fully-braced, desugared form of the initialization. |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 901 | void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 902 | InitListExpr *ParentIList, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 903 | QualType T, unsigned &Index, |
| 904 | InitListExpr *StructuredList, |
Eli Friedman | c616c5f | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 905 | unsigned &StructuredIndex) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 906 | int maxElements = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 907 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 908 | if (T->isArrayType()) |
| 909 | maxElements = numArrayElements(T); |
Douglas Gregor | 8385a06 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 910 | else if (T->isRecordType()) |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 911 | maxElements = numStructUnionElements(T); |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 912 | else if (T->isVectorType()) |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 913 | maxElements = T->getAs<VectorType>()->getNumElements(); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 914 | else |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 915 | llvm_unreachable("CheckImplicitInitList(): Illegal type"); |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 916 | |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 917 | if (maxElements == 0) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 918 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 919 | SemaRef.Diag(ParentIList->getInit(Index)->getBeginLoc(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 920 | diag::err_implicit_empty_initializer); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 921 | ++Index; |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 922 | hadError = true; |
| 923 | return; |
| 924 | } |
| 925 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 926 | // Build a structured initializer list corresponding to this subobject. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 927 | InitListExpr *StructuredSubobjectInitList = getStructuredSubobjectInit( |
| 928 | ParentIList, Index, T, StructuredList, StructuredIndex, |
| 929 | SourceRange(ParentIList->getInit(Index)->getBeginLoc(), |
| 930 | ParentIList->getSourceRange().getEnd())); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 931 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 932 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 933 | // Check the element types and build the structural subobject. |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 934 | unsigned StartIndex = Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 935 | CheckListElementTypes(Entity, ParentIList, T, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 936 | /*SubobjectIsDesignatorContext=*/false, Index, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 937 | StructuredSubobjectInitList, |
Eli Friedman | c616c5f | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 938 | StructuredSubobjectInitIndex); |
Sebastian Redl | 8b6412a | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 939 | |
Richard Smith | de22923 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 940 | if (!VerifyOnly) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 941 | StructuredSubobjectInitList->setType(T); |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 942 | |
Sebastian Redl | 8b6412a | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 943 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 944 | // Update the structured sub-object initializer so that it's ending |
| 945 | // range corresponds with the end of the last initializer it used. |
Reid Kleckner | 4a09e88 | 2015-12-09 23:18:38 +0000 | [diff] [blame] | 946 | if (EndIndex < ParentIList->getNumInits() && |
| 947 | ParentIList->getInit(EndIndex)) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 948 | SourceLocation EndLoc |
| 949 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 950 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 951 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 952 | |
Sebastian Redl | 8b6412a | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 953 | // Complain about missing braces. |
Daniel Marjamaki | 817a3bf | 2017-09-29 09:44:41 +0000 | [diff] [blame] | 954 | if ((T->isArrayType() || T->isRecordType()) && |
Richard Smith | 283e207 | 2017-10-03 20:36:00 +0000 | [diff] [blame] | 955 | !ParentIList->isIdiomaticZeroInitializer(SemaRef.getLangOpts()) && |
| 956 | !isIdiomaticBraceElisionEntity(Entity)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 957 | SemaRef.Diag(StructuredSubobjectInitList->getBeginLoc(), |
Richard Smith | de22923 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 958 | diag::warn_missing_braces) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 959 | << StructuredSubobjectInitList->getSourceRange() |
| 960 | << FixItHint::CreateInsertion( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 961 | StructuredSubobjectInitList->getBeginLoc(), "{") |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 962 | << FixItHint::CreateInsertion( |
| 963 | SemaRef.getLocForEndOfToken( |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 964 | StructuredSubobjectInitList->getEndLoc()), |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 965 | "}"); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 966 | } |
Tanya Lattner | 5029d56 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 967 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Richard Smith | 420fa12 | 2015-02-12 01:50:05 +0000 | [diff] [blame] | 970 | /// Warn that \p Entity was of scalar type and was initialized by a |
| 971 | /// single-element braced initializer list. |
| 972 | static void warnBracedScalarInit(Sema &S, const InitializedEntity &Entity, |
| 973 | SourceRange Braces) { |
| 974 | // Don't warn during template instantiation. If the initialization was |
| 975 | // non-dependent, we warned during the initial parse; otherwise, the |
| 976 | // type might not be scalar in some uses of the template. |
Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 977 | if (S.inTemplateInstantiation()) |
Richard Smith | 420fa12 | 2015-02-12 01:50:05 +0000 | [diff] [blame] | 978 | return; |
| 979 | |
| 980 | unsigned DiagID = 0; |
| 981 | |
| 982 | switch (Entity.getKind()) { |
| 983 | case InitializedEntity::EK_VectorElement: |
| 984 | case InitializedEntity::EK_ComplexElement: |
| 985 | case InitializedEntity::EK_ArrayElement: |
| 986 | case InitializedEntity::EK_Parameter: |
| 987 | case InitializedEntity::EK_Parameter_CF_Audited: |
| 988 | case InitializedEntity::EK_Result: |
| 989 | // Extra braces here are suspicious. |
| 990 | DiagID = diag::warn_braces_around_scalar_init; |
| 991 | break; |
| 992 | |
| 993 | case InitializedEntity::EK_Member: |
| 994 | // Warn on aggregate initialization but not on ctor init list or |
| 995 | // default member initializer. |
| 996 | if (Entity.getParent()) |
| 997 | DiagID = diag::warn_braces_around_scalar_init; |
| 998 | break; |
| 999 | |
| 1000 | case InitializedEntity::EK_Variable: |
| 1001 | case InitializedEntity::EK_LambdaCapture: |
| 1002 | // No warning, might be direct-list-initialization. |
| 1003 | // FIXME: Should we warn for copy-list-initialization in these cases? |
| 1004 | break; |
| 1005 | |
| 1006 | case InitializedEntity::EK_New: |
| 1007 | case InitializedEntity::EK_Temporary: |
| 1008 | case InitializedEntity::EK_CompoundLiteralInit: |
| 1009 | // No warning, braces are part of the syntax of the underlying construct. |
| 1010 | break; |
| 1011 | |
| 1012 | case InitializedEntity::EK_RelatedResult: |
| 1013 | // No warning, we already warned when initializing the result. |
| 1014 | break; |
| 1015 | |
| 1016 | case InitializedEntity::EK_Exception: |
| 1017 | case InitializedEntity::EK_Base: |
| 1018 | case InitializedEntity::EK_Delegating: |
| 1019 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 1020 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 1021 | case InitializedEntity::EK_Binding: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 1022 | case InitializedEntity::EK_StmtExprResult: |
Richard Smith | 420fa12 | 2015-02-12 01:50:05 +0000 | [diff] [blame] | 1023 | llvm_unreachable("unexpected braced scalar init"); |
| 1024 | } |
| 1025 | |
| 1026 | if (DiagID) { |
| 1027 | S.Diag(Braces.getBegin(), DiagID) |
| 1028 | << Braces |
| 1029 | << FixItHint::CreateRemoval(Braces.getBegin()) |
| 1030 | << FixItHint::CreateRemoval(Braces.getEnd()); |
| 1031 | } |
| 1032 | } |
| 1033 | |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 1034 | /// Check whether the initializer \p IList (that was written with explicit |
| 1035 | /// braces) can be used to initialize an object of type \p T. |
| 1036 | /// |
| 1037 | /// This also fills in \p StructuredList with the fully-braced, desugared |
| 1038 | /// form of the initialization. |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1039 | void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1040 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1041 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1042 | bool TopLevelObject) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1043 | if (!VerifyOnly) { |
| 1044 | SyntacticToSemantic[IList] = StructuredList; |
| 1045 | StructuredList->setSyntacticForm(IList); |
| 1046 | } |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 1047 | |
| 1048 | unsigned Index = 0, StructuredIndex = 0; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1049 | CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1050 | Index, StructuredList, StructuredIndex, TopLevelObject); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1051 | if (!VerifyOnly) { |
Eli Friedman | 91f5ae5 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 1052 | QualType ExprTy = T; |
| 1053 | if (!ExprTy->isArrayType()) |
| 1054 | ExprTy = ExprTy.getNonLValueExprType(SemaRef.Context); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1055 | IList->setType(ExprTy); |
| 1056 | StructuredList->setType(ExprTy); |
| 1057 | } |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1058 | if (hadError) |
| 1059 | return; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 1060 | |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1061 | if (Index < IList->getNumInits()) { |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 1062 | // We have leftover initializers |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1063 | if (VerifyOnly) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1064 | if (SemaRef.getLangOpts().CPlusPlus || |
| 1065 | (SemaRef.getLangOpts().OpenCL && |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1066 | IList->getType()->isVectorType())) { |
| 1067 | hadError = true; |
| 1068 | } |
| 1069 | return; |
| 1070 | } |
| 1071 | |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 1072 | if (StructuredIndex == 1 && |
Hans Wennborg | 950f318 | 2013-05-16 09:22:40 +0000 | [diff] [blame] | 1073 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context) == |
| 1074 | SIF_None) { |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 1075 | unsigned DK = diag::ext_excess_initializers_in_char_array_initializer; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1076 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 1077 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 1078 | hadError = true; |
| 1079 | } |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 1080 | // Special-case |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1081 | SemaRef.Diag(IList->getInit(Index)->getBeginLoc(), DK) |
| 1082 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d0e48ea | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 1083 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | d42a0fb | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 1084 | // Don't complain for incomplete types, since we'll get an error |
| 1085 | // elsewhere |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1086 | QualType CurrentObjectType = StructuredList->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1087 | int initKind = |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1088 | CurrentObjectType->isArrayType()? 0 : |
| 1089 | CurrentObjectType->isVectorType()? 1 : |
| 1090 | CurrentObjectType->isScalarType()? 2 : |
| 1091 | CurrentObjectType->isUnionType()? 3 : |
| 1092 | 4; |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 1093 | |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 1094 | unsigned DK = diag::ext_excess_initializers; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1095 | if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 1096 | DK = diag::err_excess_initializers; |
| 1097 | hadError = true; |
| 1098 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1099 | if (SemaRef.getLangOpts().OpenCL && initKind == 1) { |
Nate Begeman | 425038c | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 1100 | DK = diag::err_excess_initializers; |
| 1101 | hadError = true; |
| 1102 | } |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 1103 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1104 | SemaRef.Diag(IList->getInit(Index)->getBeginLoc(), DK) |
| 1105 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 1106 | } |
| 1107 | } |
Eli Friedman | 6fcdec2 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 1108 | |
Richard Smith | 420fa12 | 2015-02-12 01:50:05 +0000 | [diff] [blame] | 1109 | if (!VerifyOnly && T->isScalarType() && |
| 1110 | IList->getNumInits() == 1 && !isa<InitListExpr>(IList->getInit(0))) |
| 1111 | warnBracedScalarInit(SemaRef, Entity, IList->getSourceRange()); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1114 | void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1115 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1116 | QualType &DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1117 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1118 | unsigned &Index, |
| 1119 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1120 | unsigned &StructuredIndex, |
| 1121 | bool TopLevelObject) { |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 1122 | if (DeclType->isAnyComplexType() && SubobjectIsDesignatorContext) { |
| 1123 | // Explicitly braced initializer for complex type can be real+imaginary |
| 1124 | // parts. |
| 1125 | CheckComplexType(Entity, IList, DeclType, Index, |
| 1126 | StructuredList, StructuredIndex); |
| 1127 | } else if (DeclType->isScalarType()) { |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1128 | CheckScalarType(Entity, IList, DeclType, Index, |
| 1129 | StructuredList, StructuredIndex); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 1130 | } else if (DeclType->isVectorType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1131 | CheckVectorType(Entity, IList, DeclType, Index, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1132 | StructuredList, StructuredIndex); |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 1133 | } else if (DeclType->isRecordType()) { |
| 1134 | assert(DeclType->isAggregateType() && |
| 1135 | "non-aggregate records should be handed in CheckSubElementType"); |
| 1136 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 1137 | auto Bases = |
| 1138 | CXXRecordDecl::base_class_range(CXXRecordDecl::base_class_iterator(), |
| 1139 | CXXRecordDecl::base_class_iterator()); |
| 1140 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
| 1141 | Bases = CXXRD->bases(); |
| 1142 | CheckStructUnionTypes(Entity, IList, DeclType, Bases, RD->field_begin(), |
| 1143 | SubobjectIsDesignatorContext, Index, StructuredList, |
| 1144 | StructuredIndex, TopLevelObject); |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 1145 | } else if (DeclType->isArrayType()) { |
| 1146 | llvm::APSInt Zero( |
| 1147 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
| 1148 | false); |
| 1149 | CheckArrayType(Entity, IList, DeclType, Zero, |
| 1150 | SubobjectIsDesignatorContext, Index, |
| 1151 | StructuredList, StructuredIndex); |
Steve Naroff | eaf5853 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 1152 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 1153 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1154 | ++Index; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1155 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1156 | SemaRef.Diag(IList->getBeginLoc(), diag::err_illegal_initializer_type) |
| 1157 | << DeclType; |
Eli Friedman | d0e48ea | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 1158 | hadError = true; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1159 | } else if (DeclType->isReferenceType()) { |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1160 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 1161 | StructuredList, StructuredIndex); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1162 | } else if (DeclType->isObjCObjectType()) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1163 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1164 | SemaRef.Diag(IList->getBeginLoc(), diag::err_init_objc_class) << DeclType; |
Douglas Gregor | 50ec46d | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 1165 | hadError = true; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1166 | } else { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1167 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1168 | SemaRef.Diag(IList->getBeginLoc(), diag::err_illegal_initializer_type) |
| 1169 | << DeclType; |
Douglas Gregor | 50ec46d | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 1170 | hadError = true; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1171 | } |
| 1172 | } |
| 1173 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1174 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1175 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1176 | QualType ElemType, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1177 | unsigned &Index, |
| 1178 | InitListExpr *StructuredList, |
| 1179 | unsigned &StructuredIndex) { |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1180 | Expr *expr = IList->getInit(Index); |
Richard Smith | 72752e8 | 2013-05-31 02:56:17 +0000 | [diff] [blame] | 1181 | |
| 1182 | if (ElemType->isReferenceType()) |
| 1183 | return CheckReferenceType(Entity, IList, ElemType, Index, |
| 1184 | StructuredList, StructuredIndex); |
| 1185 | |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 1186 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1187 | if (SubInitList->getNumInits() == 1 && |
| 1188 | IsStringInit(SubInitList->getInit(0), ElemType, SemaRef.Context) == |
| 1189 | SIF_None) { |
| 1190 | expr = SubInitList->getInit(0); |
| 1191 | } else if (!SemaRef.getLangOpts().CPlusPlus) { |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 1192 | InitListExpr *InnerStructuredList |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 1193 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 1194 | StructuredList, StructuredIndex, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1195 | SubInitList->getSourceRange(), true); |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 1196 | CheckExplicitInitList(Entity, SubInitList, ElemType, |
| 1197 | InnerStructuredList); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1198 | |
| 1199 | if (!hadError && !VerifyOnly) { |
| 1200 | bool RequiresSecondPass = false; |
| 1201 | FillInEmptyInitializations(Entity, InnerStructuredList, |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 1202 | RequiresSecondPass, StructuredList, |
| 1203 | StructuredIndex); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1204 | if (RequiresSecondPass && !hadError) |
| 1205 | FillInEmptyInitializations(Entity, InnerStructuredList, |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 1206 | RequiresSecondPass, StructuredList, |
| 1207 | StructuredIndex); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1208 | } |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 1209 | ++StructuredIndex; |
| 1210 | ++Index; |
| 1211 | return; |
| 1212 | } |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 1213 | // C++ initialization is handled later. |
Richard Smith | c4158e86 | 2014-07-18 04:47:25 +0000 | [diff] [blame] | 1214 | } else if (isa<ImplicitValueInitExpr>(expr)) { |
Richard Smith | 8aa561b | 2014-07-17 23:12:06 +0000 | [diff] [blame] | 1215 | // This happens during template instantiation when we see an InitListExpr |
| 1216 | // that we've already checked once. |
Richard Smith | c4158e86 | 2014-07-18 04:47:25 +0000 | [diff] [blame] | 1217 | assert(SemaRef.Context.hasSameType(expr->getType(), ElemType) && |
Richard Smith | 8aa561b | 2014-07-17 23:12:06 +0000 | [diff] [blame] | 1218 | "found implicit initialization for the wrong type"); |
| 1219 | if (!VerifyOnly) |
| 1220 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 1221 | ++Index; |
| 1222 | return; |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1225 | if (SemaRef.getLangOpts().CPlusPlus) { |
| 1226 | // C++ [dcl.init.aggr]p2: |
| 1227 | // Each member is copy-initialized from the corresponding |
| 1228 | // initializer-clause. |
| 1229 | |
| 1230 | // FIXME: Better EqualLoc? |
| 1231 | InitializationKind Kind = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1232 | InitializationKind::CreateCopy(expr->getBeginLoc(), SourceLocation()); |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1233 | InitializationSequence Seq(SemaRef, Entity, Kind, expr, |
| 1234 | /*TopLevelOfInitList*/ true); |
| 1235 | |
| 1236 | // C++14 [dcl.init.aggr]p13: |
| 1237 | // If the assignment-expression can initialize a member, the member is |
| 1238 | // initialized. Otherwise [...] brace elision is assumed |
| 1239 | // |
| 1240 | // Brace elision is never performed if the element is not an |
| 1241 | // assignment-expression. |
| 1242 | if (Seq || isa<InitListExpr>(expr)) { |
| 1243 | if (!VerifyOnly) { |
| 1244 | ExprResult Result = |
| 1245 | Seq.Perform(SemaRef, Entity, Kind, expr); |
| 1246 | if (Result.isInvalid()) |
| 1247 | hadError = true; |
| 1248 | |
| 1249 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1250 | Result.getAs<Expr>()); |
Richard Smith | 40574cc | 2015-02-16 04:42:59 +0000 | [diff] [blame] | 1251 | } else if (!Seq) |
| 1252 | hadError = true; |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1253 | ++Index; |
| 1254 | return; |
| 1255 | } |
| 1256 | |
| 1257 | // Fall through for subaggregate initialization |
| 1258 | } else if (ElemType->isScalarType() || ElemType->isAtomicType()) { |
| 1259 | // FIXME: Need to handle atomic aggregate types with implicit init lists. |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1260 | return CheckScalarType(Entity, IList, ElemType, Index, |
| 1261 | StructuredList, StructuredIndex); |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1262 | } else if (const ArrayType *arrayType = |
| 1263 | SemaRef.Context.getAsArrayType(ElemType)) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1264 | // arrayType can be incomplete if we're initializing a flexible |
| 1265 | // array member. There's nothing we can do with the completed |
| 1266 | // type here, though. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1267 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1268 | if (IsStringInit(expr, arrayType, SemaRef.Context) == SIF_None) { |
Eli Friedman | d8d7a37 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 1269 | if (!VerifyOnly) { |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1270 | CheckStringInit(expr, ElemType, arrayType, SemaRef); |
| 1271 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
Eli Friedman | d8d7a37 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 1272 | } |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1273 | ++Index; |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1274 | return; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1275 | } |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1276 | |
| 1277 | // Fall through for subaggregate initialization. |
| 1278 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1279 | } else { |
Anastasia Stulova | db7a31c | 2016-07-05 11:31:24 +0000 | [diff] [blame] | 1280 | assert((ElemType->isRecordType() || ElemType->isVectorType() || |
Egor Churaev | 45fe70f | 2017-05-10 10:28:34 +0000 | [diff] [blame] | 1281 | ElemType->isOpenCLSpecificType()) && "Unexpected type"); |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1282 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1283 | // C99 6.7.8p13: |
| 1284 | // |
| 1285 | // The initializer for a structure or union object that has |
| 1286 | // automatic storage duration shall be either an initializer |
| 1287 | // list as described below, or a single expression that has |
| 1288 | // compatible structure or union type. In the latter case, the |
| 1289 | // initial value of the object, including unnamed members, is |
| 1290 | // that of the expression. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1291 | ExprResult ExprRes = expr; |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1292 | if (SemaRef.CheckSingleAssignmentConstraints( |
| 1293 | ElemType, ExprRes, !VerifyOnly) != Sema::Incompatible) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1294 | if (ExprRes.isInvalid()) |
| 1295 | hadError = true; |
| 1296 | else { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1297 | ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.get()); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 1298 | if (ExprRes.isInvalid()) |
| 1299 | hadError = true; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1300 | } |
| 1301 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1302 | ExprRes.getAs<Expr>()); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1303 | ++Index; |
| 1304 | return; |
| 1305 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1306 | ExprRes.get(); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1307 | // Fall through for subaggregate initialization |
| 1308 | } |
| 1309 | |
| 1310 | // C++ [dcl.init.aggr]p12: |
| 1311 | // |
| 1312 | // [...] Otherwise, if the member is itself a non-empty |
| 1313 | // subaggregate, brace elision is assumed and the initializer is |
| 1314 | // considered for the initialization of the first member of |
| 1315 | // the subaggregate. |
Yaxun Liu | a91da4b | 2016-10-11 15:53:28 +0000 | [diff] [blame] | 1316 | // OpenCL vector initializer is handled elsewhere. |
| 1317 | if ((!SemaRef.getLangOpts().OpenCL && ElemType->isVectorType()) || |
| 1318 | ElemType->isAggregateType()) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1319 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
| 1320 | StructuredIndex); |
| 1321 | ++StructuredIndex; |
| 1322 | } else { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1323 | if (!VerifyOnly) { |
| 1324 | // We cannot initialize this element, so let |
| 1325 | // PerformCopyInitialization produce the appropriate diagnostic. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1326 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), expr, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1327 | /*TopLevelOfInitList=*/true); |
| 1328 | } |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1329 | hadError = true; |
| 1330 | ++Index; |
| 1331 | ++StructuredIndex; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1332 | } |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 1335 | void InitListChecker::CheckComplexType(const InitializedEntity &Entity, |
| 1336 | InitListExpr *IList, QualType DeclType, |
| 1337 | unsigned &Index, |
| 1338 | InitListExpr *StructuredList, |
| 1339 | unsigned &StructuredIndex) { |
| 1340 | assert(Index == 0 && "Index in explicit init list must be zero"); |
| 1341 | |
| 1342 | // As an extension, clang supports complex initializers, which initialize |
| 1343 | // a complex number component-wise. When an explicit initializer list for |
| 1344 | // a complex number contains two two initializers, this extension kicks in: |
| 1345 | // it exepcts the initializer list to contain two elements convertible to |
| 1346 | // the element type of the complex type. The first element initializes |
| 1347 | // the real part, and the second element intitializes the imaginary part. |
| 1348 | |
| 1349 | if (IList->getNumInits() != 2) |
| 1350 | return CheckScalarType(Entity, IList, DeclType, Index, StructuredList, |
| 1351 | StructuredIndex); |
| 1352 | |
| 1353 | // This is an extension in C. (The builtin _Complex type does not exist |
| 1354 | // in the C++ standard.) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1355 | if (!SemaRef.getLangOpts().CPlusPlus && !VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1356 | SemaRef.Diag(IList->getBeginLoc(), diag::ext_complex_component_init) |
| 1357 | << IList->getSourceRange(); |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 1358 | |
| 1359 | // Initialize the complex number. |
| 1360 | QualType elementType = DeclType->getAs<ComplexType>()->getElementType(); |
| 1361 | InitializedEntity ElementEntity = |
| 1362 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
| 1363 | |
| 1364 | for (unsigned i = 0; i < 2; ++i) { |
| 1365 | ElementEntity.setElementIndex(Index); |
| 1366 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1367 | StructuredList, StructuredIndex); |
| 1368 | } |
| 1369 | } |
| 1370 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1371 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1372 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1373 | unsigned &Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1374 | InitListExpr *StructuredList, |
| 1375 | unsigned &StructuredIndex) { |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1376 | if (Index >= IList->getNumInits()) { |
Richard Smith | c823973 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 1377 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1378 | SemaRef.Diag(IList->getBeginLoc(), |
| 1379 | SemaRef.getLangOpts().CPlusPlus11 |
| 1380 | ? diag::warn_cxx98_compat_empty_scalar_initializer |
| 1381 | : diag::err_empty_scalar_initializer) |
| 1382 | << IList->getSourceRange(); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1383 | hadError = !SemaRef.getLangOpts().CPlusPlus11; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1384 | ++Index; |
| 1385 | ++StructuredIndex; |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 1386 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1387 | } |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1388 | |
| 1389 | Expr *expr = IList->getInit(Index); |
| 1390 | if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) { |
Richard Smith | fe9d2c0 | 2013-11-19 03:41:32 +0000 | [diff] [blame] | 1391 | // FIXME: This is invalid, and accepting it causes overload resolution |
| 1392 | // to pick the wrong overload in some corner cases. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1393 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1394 | SemaRef.Diag(SubIList->getBeginLoc(), |
Richard Smith | fe9d2c0 | 2013-11-19 03:41:32 +0000 | [diff] [blame] | 1395 | diag::ext_many_braces_around_scalar_init) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1396 | << SubIList->getSourceRange(); |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1397 | |
| 1398 | CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList, |
| 1399 | StructuredIndex); |
| 1400 | return; |
| 1401 | } else if (isa<DesignatedInitExpr>(expr)) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1402 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1403 | SemaRef.Diag(expr->getBeginLoc(), diag::err_designator_for_scalar_init) |
| 1404 | << DeclType << expr->getSourceRange(); |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1405 | hadError = true; |
| 1406 | ++Index; |
| 1407 | ++StructuredIndex; |
| 1408 | return; |
| 1409 | } |
| 1410 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1411 | if (VerifyOnly) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1412 | if (!SemaRef.CanPerformCopyInitialization(Entity,expr)) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1413 | hadError = true; |
| 1414 | ++Index; |
| 1415 | return; |
| 1416 | } |
| 1417 | |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1418 | ExprResult Result = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1419 | SemaRef.PerformCopyInitialization(Entity, expr->getBeginLoc(), expr, |
| 1420 | /*TopLevelOfInitList=*/true); |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1421 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1422 | Expr *ResultExpr = nullptr; |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1423 | |
| 1424 | if (Result.isInvalid()) |
| 1425 | hadError = true; // types weren't compatible. |
| 1426 | else { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1427 | ResultExpr = Result.getAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1428 | |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1429 | if (ResultExpr != expr) { |
| 1430 | // The type was promoted, update initializer list. |
| 1431 | IList->setInit(Index, ResultExpr); |
| 1432 | } |
| 1433 | } |
| 1434 | if (hadError) |
| 1435 | ++StructuredIndex; |
| 1436 | else |
| 1437 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 1438 | ++Index; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1441 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 1442 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1443 | unsigned &Index, |
| 1444 | InitListExpr *StructuredList, |
| 1445 | unsigned &StructuredIndex) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1446 | if (Index >= IList->getNumInits()) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1447 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 1448 | // general, it would be useful to pass location information down the stack, |
| 1449 | // so that we know the location (or decl) of the "current object" being |
| 1450 | // initialized. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1451 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1452 | SemaRef.Diag(IList->getBeginLoc(), |
| 1453 | diag::err_init_reference_member_uninitialized) |
| 1454 | << DeclType << IList->getSourceRange(); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1455 | hadError = true; |
| 1456 | ++Index; |
| 1457 | ++StructuredIndex; |
| 1458 | return; |
| 1459 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1460 | |
| 1461 | Expr *expr = IList->getInit(Index); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1462 | if (isa<InitListExpr>(expr) && !SemaRef.getLangOpts().CPlusPlus11) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1463 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1464 | SemaRef.Diag(IList->getBeginLoc(), diag::err_init_non_aggr_init_list) |
| 1465 | << DeclType << IList->getSourceRange(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1466 | hadError = true; |
| 1467 | ++Index; |
| 1468 | ++StructuredIndex; |
| 1469 | return; |
| 1470 | } |
| 1471 | |
| 1472 | if (VerifyOnly) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1473 | if (!SemaRef.CanPerformCopyInitialization(Entity,expr)) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1474 | hadError = true; |
| 1475 | ++Index; |
| 1476 | return; |
| 1477 | } |
| 1478 | |
| 1479 | ExprResult Result = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1480 | SemaRef.PerformCopyInitialization(Entity, expr->getBeginLoc(), expr, |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1481 | /*TopLevelOfInitList=*/true); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1482 | |
| 1483 | if (Result.isInvalid()) |
| 1484 | hadError = true; |
| 1485 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1486 | expr = Result.getAs<Expr>(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1487 | IList->setInit(Index, expr); |
| 1488 | |
| 1489 | if (hadError) |
| 1490 | ++StructuredIndex; |
| 1491 | else |
| 1492 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 1493 | ++Index; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1496 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1497 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1498 | unsigned &Index, |
| 1499 | InitListExpr *StructuredList, |
| 1500 | unsigned &StructuredIndex) { |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1501 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 1502 | unsigned maxElements = VT->getNumElements(); |
| 1503 | unsigned numEltsInit = 0; |
| 1504 | QualType elementType = VT->getElementType(); |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1505 | |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1506 | if (Index >= IList->getNumInits()) { |
| 1507 | // Make sure the element type can be value-initialized. |
| 1508 | if (VerifyOnly) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1509 | CheckEmptyInitializable( |
| 1510 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity), |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1511 | IList->getEndLoc()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1512 | return; |
| 1513 | } |
| 1514 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1515 | if (!SemaRef.getLangOpts().OpenCL) { |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1516 | // If the initializing element is a vector, try to copy-initialize |
| 1517 | // instead of breaking it apart (which is doomed to failure anyway). |
| 1518 | Expr *Init = IList->getInit(Index); |
| 1519 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1520 | if (VerifyOnly) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1521 | if (!SemaRef.CanPerformCopyInitialization(Entity, Init)) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1522 | hadError = true; |
| 1523 | ++Index; |
| 1524 | return; |
| 1525 | } |
| 1526 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1527 | ExprResult Result = |
| 1528 | SemaRef.PerformCopyInitialization(Entity, Init->getBeginLoc(), Init, |
| 1529 | /*TopLevelOfInitList=*/true); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1530 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1531 | Expr *ResultExpr = nullptr; |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1532 | if (Result.isInvalid()) |
| 1533 | hadError = true; // types weren't compatible. |
| 1534 | else { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1535 | ResultExpr = Result.getAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1536 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1537 | if (ResultExpr != Init) { |
| 1538 | // The type was promoted, update initializer list. |
| 1539 | IList->setInit(Index, ResultExpr); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1540 | } |
| 1541 | } |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1542 | if (hadError) |
| 1543 | ++StructuredIndex; |
| 1544 | else |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1545 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1546 | ResultExpr); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1547 | ++Index; |
| 1548 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1549 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1550 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1551 | InitializedEntity ElementEntity = |
| 1552 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1553 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1554 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 1555 | // Don't attempt to go past the end of the init list |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1556 | if (Index >= IList->getNumInits()) { |
| 1557 | if (VerifyOnly) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1558 | CheckEmptyInitializable(ElementEntity, IList->getEndLoc()); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1559 | break; |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1560 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1561 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1562 | ElementEntity.setElementIndex(Index); |
| 1563 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1564 | StructuredList, StructuredIndex); |
| 1565 | } |
James Molloy | 9eef265 | 2014-06-20 14:35:13 +0000 | [diff] [blame] | 1566 | |
| 1567 | if (VerifyOnly) |
| 1568 | return; |
| 1569 | |
| 1570 | bool isBigEndian = SemaRef.Context.getTargetInfo().isBigEndian(); |
| 1571 | const VectorType *T = Entity.getType()->getAs<VectorType>(); |
| 1572 | if (isBigEndian && (T->getVectorKind() == VectorType::NeonVector || |
| 1573 | T->getVectorKind() == VectorType::NeonPolyVector)) { |
| 1574 | // The ability to use vector initializer lists is a GNU vector extension |
| 1575 | // 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] | 1576 | // endian machines it works fine, however on big endian machines it |
James Molloy | 9eef265 | 2014-06-20 14:35:13 +0000 | [diff] [blame] | 1577 | // exhibits surprising behaviour: |
| 1578 | // |
| 1579 | // uint32x2_t x = {42, 64}; |
| 1580 | // return vget_lane_u32(x, 0); // Will return 64. |
| 1581 | // |
| 1582 | // Because of this, explicitly call out that it is non-portable. |
| 1583 | // |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1584 | SemaRef.Diag(IList->getBeginLoc(), |
James Molloy | 9eef265 | 2014-06-20 14:35:13 +0000 | [diff] [blame] | 1585 | diag::warn_neon_vector_initializer_non_portable); |
| 1586 | |
| 1587 | const char *typeCode; |
| 1588 | unsigned typeSize = SemaRef.Context.getTypeSize(elementType); |
| 1589 | |
| 1590 | if (elementType->isFloatingType()) |
| 1591 | typeCode = "f"; |
| 1592 | else if (elementType->isSignedIntegerType()) |
| 1593 | typeCode = "s"; |
| 1594 | else if (elementType->isUnsignedIntegerType()) |
| 1595 | typeCode = "u"; |
| 1596 | else |
| 1597 | llvm_unreachable("Invalid element type!"); |
| 1598 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1599 | SemaRef.Diag(IList->getBeginLoc(), |
| 1600 | SemaRef.Context.getTypeSize(VT) > 64 |
| 1601 | ? diag::note_neon_vector_initializer_non_portable_q |
| 1602 | : diag::note_neon_vector_initializer_non_portable) |
| 1603 | << typeCode << typeSize; |
James Molloy | 9eef265 | 2014-06-20 14:35:13 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1606 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1607 | } |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1608 | |
| 1609 | InitializedEntity ElementEntity = |
| 1610 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1611 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1612 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 1613 | for (unsigned i = 0; i < maxElements; ++i) { |
| 1614 | // Don't attempt to go past the end of the init list |
| 1615 | if (Index >= IList->getNumInits()) |
| 1616 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1617 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1618 | ElementEntity.setElementIndex(Index); |
| 1619 | |
| 1620 | QualType IType = IList->getInit(Index)->getType(); |
| 1621 | if (!IType->isVectorType()) { |
| 1622 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1623 | StructuredList, StructuredIndex); |
| 1624 | ++numEltsInit; |
| 1625 | } else { |
| 1626 | QualType VecType; |
| 1627 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 1628 | unsigned numIElts = IVT->getNumElements(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1629 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1630 | if (IType->isExtVectorType()) |
| 1631 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 1632 | else |
| 1633 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1634 | IVT->getVectorKind()); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1635 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 1636 | StructuredList, StructuredIndex); |
| 1637 | numEltsInit += numIElts; |
| 1638 | } |
| 1639 | } |
| 1640 | |
| 1641 | // OpenCL requires all elements to be initialized. |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1642 | if (numEltsInit != maxElements) { |
| 1643 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1644 | SemaRef.Diag(IList->getBeginLoc(), |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1645 | diag::err_vector_incorrect_num_initializers) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1646 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1647 | hadError = true; |
| 1648 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1651 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1652 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1653 | llvm::APSInt elementIndex, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1654 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1655 | unsigned &Index, |
| 1656 | InitListExpr *StructuredList, |
| 1657 | unsigned &StructuredIndex) { |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1658 | const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType); |
| 1659 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1660 | // Check for the special-case of initializing an array with a string. |
| 1661 | if (Index < IList->getNumInits()) { |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1662 | if (IsStringInit(IList->getInit(Index), arrayType, SemaRef.Context) == |
| 1663 | SIF_None) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1664 | // We place the string literal directly into the resulting |
| 1665 | // initializer list. This is the only place where the structure |
| 1666 | // of the structured initializer list doesn't match exactly, |
| 1667 | // because doing so would involve allocating one character |
| 1668 | // constant for each string. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1669 | if (!VerifyOnly) { |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1670 | CheckStringInit(IList->getInit(Index), DeclType, arrayType, SemaRef); |
| 1671 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1672 | IList->getInit(Index)); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1673 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
| 1674 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1675 | ++Index; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1676 | return; |
| 1677 | } |
| 1678 | } |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1679 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1680 | // Check for VLAs; in standard C it would be possible to check this |
| 1681 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 1682 | // them in all sorts of strange places). |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1683 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1684 | SemaRef.Diag(VAT->getSizeExpr()->getBeginLoc(), |
| 1685 | diag::err_variable_object_no_init) |
| 1686 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1687 | hadError = true; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1688 | ++Index; |
| 1689 | ++StructuredIndex; |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1690 | return; |
| 1691 | } |
| 1692 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1693 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1694 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 1695 | elementIndex.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1696 | bool maxElementsKnown = false; |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1697 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1698 | maxElements = CAT->getSize(); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1699 | elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1700 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1701 | maxElementsKnown = true; |
| 1702 | } |
| 1703 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1704 | QualType elementType = arrayType->getElementType(); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1705 | while (Index < IList->getNumInits()) { |
| 1706 | Expr *Init = IList->getInit(Index); |
| 1707 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1708 | // If we're not the subobject that matches up with the '{' for |
| 1709 | // the designator, we shouldn't be handling the |
| 1710 | // designator. Return immediately. |
| 1711 | if (!SubobjectIsDesignatorContext) |
| 1712 | return; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1713 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1714 | // Handle this designated initializer. elementIndex will be |
| 1715 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1716 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1717 | DeclType, nullptr, &elementIndex, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1718 | StructuredList, StructuredIndex, true, |
| 1719 | false)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1720 | hadError = true; |
| 1721 | continue; |
| 1722 | } |
| 1723 | |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1724 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1725 | maxElements = maxElements.extend(elementIndex.getBitWidth()); |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1726 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1727 | elementIndex = elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1728 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1729 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1730 | // If the array is of incomplete type, keep track of the number of |
| 1731 | // elements in the initializer. |
| 1732 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1733 | maxElements = elementIndex; |
| 1734 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1735 | continue; |
| 1736 | } |
| 1737 | |
| 1738 | // If we know the maximum number of elements, and we've already |
| 1739 | // hit it, stop consuming elements in the initializer list. |
| 1740 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1741 | break; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1742 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1743 | InitializedEntity ElementEntity = |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1744 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1745 | Entity); |
| 1746 | // Check this element. |
| 1747 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1748 | StructuredList, StructuredIndex); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1749 | ++elementIndex; |
| 1750 | |
| 1751 | // If the array is of incomplete type, keep track of the number of |
| 1752 | // elements in the initializer. |
| 1753 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1754 | maxElements = elementIndex; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1755 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1756 | if (!hadError && DeclType->isIncompleteArrayType() && !VerifyOnly) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1757 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1758 | // be calculated here. |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1759 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Richard Smith | 73edb6d | 2017-01-24 23:18:28 +0000 | [diff] [blame] | 1760 | if (maxElements == Zero && !Entity.isVariableLengthArrayNew()) { |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1761 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1762 | // but is supported by GNU. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1763 | SemaRef.Diag(IList->getBeginLoc(), diag::ext_typecheck_zero_array_size); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1764 | } |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1765 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1766 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1767 | ArrayType::Normal, 0); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1768 | } |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1769 | if (!hadError && VerifyOnly) { |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1770 | // If there are any members of the array that get value-initialized, check |
| 1771 | // that is possible. That happens if we know the bound and don't have |
| 1772 | // enough elements, or if we're performing an array new with an unknown |
| 1773 | // bound. |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1774 | // FIXME: This needs to detect holes left by designated initializers too. |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1775 | if ((maxElementsKnown && elementIndex < maxElements) || |
| 1776 | Entity.isVariableLengthArrayNew()) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1777 | CheckEmptyInitializable( |
| 1778 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity), |
| 1779 | IList->getEndLoc()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1780 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1781 | } |
| 1782 | |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1783 | bool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 1784 | Expr *InitExpr, |
| 1785 | FieldDecl *Field, |
| 1786 | bool TopLevelObject) { |
| 1787 | // Handle GNU flexible array initializers. |
| 1788 | unsigned FlexArrayDiag; |
| 1789 | if (isa<InitListExpr>(InitExpr) && |
| 1790 | cast<InitListExpr>(InitExpr)->getNumInits() == 0) { |
| 1791 | // Empty flexible array init always allowed as an extension |
| 1792 | FlexArrayDiag = diag::ext_flexible_array_init; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1793 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1794 | // Disallow flexible array init in C++; it is not required for gcc |
| 1795 | // compatibility, and it needs work to IRGen correctly in general. |
| 1796 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1797 | } else if (!TopLevelObject) { |
| 1798 | // Disallow flexible array init on non-top-level object |
| 1799 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1800 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
| 1801 | // Disallow flexible array init on anything which is not a variable. |
| 1802 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1803 | } else if (cast<VarDecl>(Entity.getDecl())->hasLocalStorage()) { |
| 1804 | // Disallow flexible array init on local variables. |
| 1805 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1806 | } else { |
| 1807 | // Allow other cases. |
| 1808 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1809 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1810 | |
| 1811 | if (!VerifyOnly) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1812 | SemaRef.Diag(InitExpr->getBeginLoc(), FlexArrayDiag) |
| 1813 | << InitExpr->getBeginLoc(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1814 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1815 | << Field; |
| 1816 | } |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1817 | |
| 1818 | return FlexArrayDiag != diag::ext_flexible_array_init; |
| 1819 | } |
| 1820 | |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 1821 | void InitListChecker::CheckStructUnionTypes( |
| 1822 | const InitializedEntity &Entity, InitListExpr *IList, QualType DeclType, |
| 1823 | CXXRecordDecl::base_class_range Bases, RecordDecl::field_iterator Field, |
| 1824 | bool SubobjectIsDesignatorContext, unsigned &Index, |
| 1825 | InitListExpr *StructuredList, unsigned &StructuredIndex, |
| 1826 | bool TopLevelObject) { |
| 1827 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1828 | |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1829 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1830 | // confusion, we forgo checking the intializer for the entire record. |
| 1831 | if (structDecl->isInvalidDecl()) { |
Richard Smith | 845aa66 | 2012-09-28 21:23:50 +0000 | [diff] [blame] | 1832 | // Assume it was supposed to consume a single initializer. |
| 1833 | ++Index; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1834 | hadError = true; |
| 1835 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1836 | } |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1837 | |
| 1838 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1839 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1840 | |
| 1841 | // If there's a default initializer, use it. |
| 1842 | if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->hasInClassInitializer()) { |
| 1843 | if (VerifyOnly) |
| 1844 | return; |
| 1845 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1846 | Field != FieldEnd; ++Field) { |
| 1847 | if (Field->hasInClassInitializer()) { |
| 1848 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1849 | // FIXME: Actually build a CXXDefaultInitExpr? |
| 1850 | return; |
| 1851 | } |
| 1852 | } |
| 1853 | } |
| 1854 | |
Reid Kleckner | 6d829bd | 2014-11-12 21:30:23 +0000 | [diff] [blame] | 1855 | // Value-initialize the first member of the union that isn't an unnamed |
| 1856 | // bitfield. |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1857 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1858 | Field != FieldEnd; ++Field) { |
Reid Kleckner | 6d829bd | 2014-11-12 21:30:23 +0000 | [diff] [blame] | 1859 | if (!Field->isUnnamedBitfield()) { |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1860 | if (VerifyOnly) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1861 | CheckEmptyInitializable( |
| 1862 | InitializedEntity::InitializeMember(*Field, &Entity), |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1863 | IList->getEndLoc()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1864 | else |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1865 | StructuredList->setInitializedFieldInUnion(*Field); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1866 | break; |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1867 | } |
| 1868 | } |
| 1869 | return; |
| 1870 | } |
| 1871 | |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 1872 | bool InitializedSomething = false; |
| 1873 | |
| 1874 | // If we have any base classes, they are initialized prior to the fields. |
| 1875 | for (auto &Base : Bases) { |
| 1876 | Expr *Init = Index < IList->getNumInits() ? IList->getInit(Index) : nullptr; |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1877 | SourceLocation InitLoc = Init ? Init->getBeginLoc() : IList->getEndLoc(); |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 1878 | |
| 1879 | // Designated inits always initialize fields, so if we see one, all |
| 1880 | // remaining base classes have no explicit initializer. |
| 1881 | if (Init && isa<DesignatedInitExpr>(Init)) |
| 1882 | Init = nullptr; |
| 1883 | |
| 1884 | InitializedEntity BaseEntity = InitializedEntity::InitializeBase( |
| 1885 | SemaRef.Context, &Base, false, &Entity); |
| 1886 | if (Init) { |
| 1887 | CheckSubElementType(BaseEntity, IList, Base.getType(), Index, |
| 1888 | StructuredList, StructuredIndex); |
| 1889 | InitializedSomething = true; |
| 1890 | } else if (VerifyOnly) { |
| 1891 | CheckEmptyInitializable(BaseEntity, InitLoc); |
| 1892 | } |
| 1893 | } |
| 1894 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1895 | // If structDecl is a forward declaration, this loop won't do |
| 1896 | // anything except look at designated initializers; That's okay, |
| 1897 | // because an error should get printed out elsewhere. It might be |
| 1898 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1899 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1900 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Daniel Marjamaki | 817a3bf | 2017-09-29 09:44:41 +0000 | [diff] [blame] | 1901 | bool CheckForMissingFields = |
| 1902 | !IList->isIdiomaticZeroInitializer(SemaRef.getLangOpts()); |
| 1903 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1904 | while (Index < IList->getNumInits()) { |
| 1905 | Expr *Init = IList->getInit(Index); |
| 1906 | |
| 1907 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1908 | // If we're not the subobject that matches up with the '{' for |
| 1909 | // the designator, we shouldn't be handling the |
| 1910 | // designator. Return immediately. |
| 1911 | if (!SubobjectIsDesignatorContext) |
| 1912 | return; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1913 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1914 | // Handle this designated initializer. Field will be updated to |
| 1915 | // the next field that we'll be initializing. |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1916 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1917 | DeclType, &Field, nullptr, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1918 | StructuredList, StructuredIndex, |
| 1919 | true, TopLevelObject)) |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1920 | hadError = true; |
| 1921 | |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1922 | InitializedSomething = true; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1923 | |
| 1924 | // Disable check for missing fields when designators are used. |
| 1925 | // This matches gcc behaviour. |
| 1926 | CheckForMissingFields = false; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1927 | continue; |
| 1928 | } |
| 1929 | |
| 1930 | if (Field == FieldEnd) { |
| 1931 | // We've run out of fields. We're done. |
| 1932 | break; |
| 1933 | } |
| 1934 | |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1935 | // We've already initialized a member of a union. We're done. |
| 1936 | if (InitializedSomething && DeclType->isUnionType()) |
| 1937 | break; |
| 1938 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1939 | // If we've hit the flexible array member at the end, we're done. |
| 1940 | if (Field->getType()->isIncompleteArrayType()) |
| 1941 | break; |
| 1942 | |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1943 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1944 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1945 | ++Field; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1946 | continue; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1947 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1948 | |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1949 | // Make sure we can use this declaration. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1950 | bool InvalidUse; |
| 1951 | if (VerifyOnly) |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 1952 | InvalidUse = !SemaRef.CanUseDecl(*Field, TreatUnavailableAsInvalid); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1953 | else |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1954 | InvalidUse = SemaRef.DiagnoseUseOfDecl( |
| 1955 | *Field, IList->getInit(Index)->getBeginLoc()); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1956 | if (InvalidUse) { |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1957 | ++Index; |
| 1958 | ++Field; |
| 1959 | hadError = true; |
| 1960 | continue; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1961 | } |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1962 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1963 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1964 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1965 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 1966 | StructuredList, StructuredIndex); |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1967 | InitializedSomething = true; |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1968 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1969 | if (DeclType->isUnionType() && !VerifyOnly) { |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1970 | // Initialize the first field within the union. |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1971 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1972 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1973 | |
| 1974 | ++Field; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1975 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1976 | |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1977 | // Emit warnings for missing struct field initializers. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1978 | if (!VerifyOnly && InitializedSomething && CheckForMissingFields && |
| 1979 | Field != FieldEnd && !Field->getType()->isIncompleteArrayType() && |
| 1980 | !DeclType->isUnionType()) { |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1981 | // It is possible we have one or more unnamed bitfields remaining. |
| 1982 | // Find first (if any) named field and emit warning. |
| 1983 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 1984 | it != end; ++it) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1985 | if (!it->isUnnamedBitfield() && !it->hasInClassInitializer()) { |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1986 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
Aaron Ballman | b9bb201 | 2014-01-03 14:54:10 +0000 | [diff] [blame] | 1987 | diag::warn_missing_field_initializers) << *it; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1988 | break; |
| 1989 | } |
| 1990 | } |
| 1991 | } |
| 1992 | |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1993 | // Check that any remaining fields can be value-initialized. |
| 1994 | if (VerifyOnly && Field != FieldEnd && !DeclType->isUnionType() && |
| 1995 | !Field->getType()->isIncompleteArrayType()) { |
| 1996 | // FIXME: Should check for holes left by designated initializers too. |
| 1997 | for (; Field != FieldEnd && !hadError; ++Field) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1998 | if (!Field->isUnnamedBitfield() && !Field->hasInClassInitializer()) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1999 | CheckEmptyInitializable( |
| 2000 | InitializedEntity::InitializeMember(*Field, &Entity), |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2001 | IList->getEndLoc()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 2002 | } |
| 2003 | } |
| 2004 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2005 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 2006 | Index >= IList->getNumInits()) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2007 | return; |
| 2008 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2009 | if (CheckFlexibleArrayInit(Entity, IList->getInit(Index), *Field, |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 2010 | TopLevelObject)) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2011 | hadError = true; |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 2012 | ++Index; |
| 2013 | return; |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2014 | } |
| 2015 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2016 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2017 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2018 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2019 | if (isa<InitListExpr>(IList->getInit(Index))) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2020 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2021 | StructuredList, StructuredIndex); |
| 2022 | else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2023 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 2024 | StructuredList, StructuredIndex); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 2025 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 2026 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2027 | /// Expand a field designator that refers to a member of an |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2028 | /// anonymous struct or union into a series of field designators that |
| 2029 | /// refers to the field within the appropriate subobject. |
| 2030 | /// |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2031 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2032 | DesignatedInitExpr *DIE, |
| 2033 | unsigned DesigIdx, |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2034 | IndirectFieldDecl *IndirectField) { |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2035 | typedef DesignatedInitExpr::Designator Designator; |
| 2036 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2037 | // Build the replacement designators. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2038 | SmallVector<Designator, 4> Replacements; |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2039 | for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), |
| 2040 | PE = IndirectField->chain_end(); PI != PE; ++PI) { |
| 2041 | if (PI + 1 == PE) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2042 | Replacements.push_back(Designator((IdentifierInfo *)nullptr, |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2043 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 2044 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 2045 | else |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2046 | Replacements.push_back(Designator((IdentifierInfo *)nullptr, |
| 2047 | SourceLocation(), SourceLocation())); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2048 | assert(isa<FieldDecl>(*PI)); |
| 2049 | Replacements.back().setField(cast<FieldDecl>(*PI)); |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | // Expand the current designator into the set of replacement |
| 2053 | // designators, so we have a full subobject path down to where the |
| 2054 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 03e8bdc | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 2055 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2056 | &Replacements[0] + Replacements.size()); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2057 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2058 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2059 | static DesignatedInitExpr *CloneDesignatedInitExpr(Sema &SemaRef, |
| 2060 | DesignatedInitExpr *DIE) { |
| 2061 | unsigned NumIndexExprs = DIE->getNumSubExprs() - 1; |
| 2062 | SmallVector<Expr*, 4> IndexExprs(NumIndexExprs); |
| 2063 | for (unsigned I = 0; I < NumIndexExprs; ++I) |
| 2064 | IndexExprs[I] = DIE->getSubExpr(I + 1); |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 2065 | return DesignatedInitExpr::Create(SemaRef.Context, DIE->designators(), |
| 2066 | IndexExprs, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2067 | DIE->getEqualOrColonLoc(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2068 | DIE->usesGNUSyntax(), DIE->getInit()); |
| 2069 | } |
| 2070 | |
Kaelyn Uhrain | b02c5e9 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 2071 | namespace { |
| 2072 | |
| 2073 | // Callback to only accept typo corrections that are for field members of |
| 2074 | // the given struct or union. |
| 2075 | class FieldInitializerValidatorCCC : public CorrectionCandidateCallback { |
| 2076 | public: |
| 2077 | explicit FieldInitializerValidatorCCC(RecordDecl *RD) |
| 2078 | : Record(RD) {} |
| 2079 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 2080 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
Kaelyn Uhrain | b02c5e9 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 2081 | FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>(); |
| 2082 | return FD && FD->getDeclContext()->getRedeclContext()->Equals(Record); |
| 2083 | } |
| 2084 | |
| 2085 | private: |
| 2086 | RecordDecl *Record; |
| 2087 | }; |
| 2088 | |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 2089 | } // end anonymous namespace |
Kaelyn Uhrain | b02c5e9 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 2090 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2091 | /// Check the well-formedness of a C99 designated initializer. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2092 | /// |
| 2093 | /// Determines whether the designated initializer @p DIE, which |
| 2094 | /// resides at the given @p Index within the initializer list @p |
| 2095 | /// IList, is well-formed for a current object of type @p DeclType |
| 2096 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2097 | /// within the current subobject is returned in either |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2098 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2099 | /// |
| 2100 | /// @param IList The initializer list in which this designated |
| 2101 | /// initializer occurs. |
| 2102 | /// |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 2103 | /// @param DIE The designated initializer expression. |
| 2104 | /// |
| 2105 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2106 | /// |
Dmitri Gribenko | adba9be | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 2107 | /// @param CurrentObjectType The type of the "current object" (C99 6.7.8p17), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2108 | /// into which the designation in @p DIE should refer. |
| 2109 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2110 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 2111 | /// a field, this will be set to the field declaration corresponding |
| 2112 | /// to the field named by the designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2113 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2114 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 2115 | /// DIE is an array designator or GNU array-range designator, this |
| 2116 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2117 | /// |
| 2118 | /// @param Index Index into @p IList where the designated initializer |
| 2119 | /// @p DIE occurs. |
| 2120 | /// |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2121 | /// @param StructuredList The initializer list expression that |
| 2122 | /// describes all of the subobject initializers in the order they'll |
| 2123 | /// actually be initialized. |
| 2124 | /// |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2125 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2126 | bool |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2127 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2128 | InitListExpr *IList, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2129 | DesignatedInitExpr *DIE, |
| 2130 | unsigned DesigIdx, |
| 2131 | QualType &CurrentObjectType, |
| 2132 | RecordDecl::field_iterator *NextField, |
| 2133 | llvm::APSInt *NextElementIndex, |
| 2134 | unsigned &Index, |
| 2135 | InitListExpr *StructuredList, |
| 2136 | unsigned &StructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2137 | bool FinishSubobjectInit, |
| 2138 | bool TopLevelObject) { |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 2139 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2140 | // Check the actual initialization for the designated object type. |
| 2141 | bool prevHadError = hadError; |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 2142 | |
| 2143 | // Temporarily remove the designator expression from the |
| 2144 | // initializer list that the child calls see, so that we don't try |
| 2145 | // to re-process the designator. |
| 2146 | unsigned OldIndex = Index; |
| 2147 | IList->setInit(OldIndex, DIE->getInit()); |
| 2148 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2149 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2150 | StructuredList, StructuredIndex); |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 2151 | |
| 2152 | // Restore the designated initializer expression in the syntactic |
| 2153 | // form of the initializer list. |
| 2154 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 2155 | DIE->setInit(IList->getInit(OldIndex)); |
| 2156 | IList->setInit(OldIndex, DIE); |
| 2157 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2158 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2159 | } |
| 2160 | |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 2161 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2162 | bool IsFirstDesignator = (DesigIdx == 0); |
| 2163 | if (!VerifyOnly) { |
| 2164 | assert((IsFirstDesignator || StructuredList) && |
| 2165 | "Need a non-designated initializer list to start from"); |
| 2166 | |
| 2167 | // Determine the structural initializer list that corresponds to the |
| 2168 | // current subobject. |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2169 | if (IsFirstDesignator) |
| 2170 | StructuredList = SyntacticToSemantic.lookup(IList); |
| 2171 | else { |
| 2172 | Expr *ExistingInit = StructuredIndex < StructuredList->getNumInits() ? |
| 2173 | StructuredList->getInit(StructuredIndex) : nullptr; |
| 2174 | if (!ExistingInit && StructuredList->hasArrayFiller()) |
| 2175 | ExistingInit = StructuredList->getArrayFiller(); |
| 2176 | |
| 2177 | if (!ExistingInit) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2178 | StructuredList = getStructuredSubobjectInit( |
| 2179 | IList, Index, CurrentObjectType, StructuredList, StructuredIndex, |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2180 | SourceRange(D->getBeginLoc(), DIE->getEndLoc())); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2181 | else if (InitListExpr *Result = dyn_cast<InitListExpr>(ExistingInit)) |
| 2182 | StructuredList = Result; |
| 2183 | else { |
| 2184 | if (DesignatedInitUpdateExpr *E = |
| 2185 | dyn_cast<DesignatedInitUpdateExpr>(ExistingInit)) |
| 2186 | StructuredList = E->getUpdater(); |
| 2187 | else { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2188 | DesignatedInitUpdateExpr *DIUE = new (SemaRef.Context) |
| 2189 | DesignatedInitUpdateExpr(SemaRef.Context, D->getBeginLoc(), |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2190 | ExistingInit, DIE->getEndLoc()); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2191 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, DIUE); |
| 2192 | StructuredList = DIUE->getUpdater(); |
| 2193 | } |
| 2194 | |
| 2195 | // We need to check on source range validity because the previous |
| 2196 | // initializer does not have to be an explicit initializer. e.g., |
| 2197 | // |
| 2198 | // struct P { int a, b; }; |
| 2199 | // struct PP { struct P p } l = { { .a = 2 }, .p.b = 3 }; |
| 2200 | // |
| 2201 | // There is an overwrite taking place because the first braced initializer |
| 2202 | // list "{ .a = 2 }" already provides value for .p.b (which is zero). |
| 2203 | if (ExistingInit->getSourceRange().isValid()) { |
| 2204 | // We are creating an initializer list that initializes the |
| 2205 | // subobjects of the current object, but there was already an |
| 2206 | // initialization that completely initialized the current |
| 2207 | // subobject, e.g., by a compound literal: |
| 2208 | // |
| 2209 | // struct X { int a, b; }; |
| 2210 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
| 2211 | // |
| 2212 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 2213 | // designated initializer re-initializes the whole |
| 2214 | // subobject [0], overwriting previous initializers. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2215 | SemaRef.Diag(D->getBeginLoc(), |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2216 | diag::warn_subobject_initializer_overrides) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2217 | << SourceRange(D->getBeginLoc(), DIE->getEndLoc()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2218 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2219 | SemaRef.Diag(ExistingInit->getBeginLoc(), |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2220 | diag::note_previous_initializer) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2221 | << /*FIXME:has side effects=*/0 << ExistingInit->getSourceRange(); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2222 | } |
| 2223 | } |
| 2224 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2225 | assert(StructuredList && "Expected a structured initializer list"); |
| 2226 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2227 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2228 | if (D->isFieldDesignator()) { |
| 2229 | // C99 6.7.8p7: |
| 2230 | // |
| 2231 | // If a designator has the form |
| 2232 | // |
| 2233 | // . identifier |
| 2234 | // |
| 2235 | // then the current object (defined below) shall have |
| 2236 | // structure or union type and the identifier shall be the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2237 | // name of a member of that type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2238 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2239 | if (!RT) { |
| 2240 | SourceLocation Loc = D->getDotLoc(); |
| 2241 | if (Loc.isInvalid()) |
| 2242 | Loc = D->getFieldLoc(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2243 | if (!VerifyOnly) |
| 2244 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2245 | << SemaRef.getLangOpts().CPlusPlus << CurrentObjectType; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2246 | ++Index; |
| 2247 | return true; |
| 2248 | } |
| 2249 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2250 | FieldDecl *KnownField = D->getField(); |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2251 | if (!KnownField) { |
| 2252 | IdentifierInfo *FieldName = D->getFieldName(); |
| 2253 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
| 2254 | for (NamedDecl *ND : Lookup) { |
| 2255 | if (auto *FD = dyn_cast<FieldDecl>(ND)) { |
| 2256 | KnownField = FD; |
| 2257 | break; |
| 2258 | } |
| 2259 | if (auto *IFD = dyn_cast<IndirectFieldDecl>(ND)) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2260 | // In verify mode, don't modify the original. |
| 2261 | if (VerifyOnly) |
| 2262 | DIE = CloneDesignatedInitExpr(SemaRef, DIE); |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2263 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IFD); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2264 | D = DIE->getDesignator(DesigIdx); |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2265 | KnownField = cast<FieldDecl>(*IFD->chain_begin()); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2266 | break; |
| 2267 | } |
| 2268 | } |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2269 | if (!KnownField) { |
| 2270 | if (VerifyOnly) { |
| 2271 | ++Index; |
| 2272 | return true; // No typo correction when just trying this out. |
| 2273 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2274 | |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2275 | // Name lookup found something, but it wasn't a field. |
| 2276 | if (!Lookup.empty()) { |
| 2277 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
| 2278 | << FieldName; |
| 2279 | SemaRef.Diag(Lookup.front()->getLocation(), |
| 2280 | diag::note_field_designator_found); |
| 2281 | ++Index; |
| 2282 | return true; |
| 2283 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2284 | |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2285 | // Name lookup didn't find anything. |
| 2286 | // Determine whether this was a typo for another field name. |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 2287 | if (TypoCorrection Corrected = SemaRef.CorrectTypo( |
| 2288 | DeclarationNameInfo(FieldName, D->getFieldLoc()), |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2289 | Sema::LookupMemberName, /*Scope=*/nullptr, /*SS=*/nullptr, |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 2290 | llvm::make_unique<FieldInitializerValidatorCCC>(RT->getDecl()), |
| 2291 | Sema::CTK_ErrorRecovery, RT->getDecl())) { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 2292 | SemaRef.diagnoseTypo( |
| 2293 | Corrected, |
| 2294 | SemaRef.PDiag(diag::err_field_designator_unknown_suggest) |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2295 | << FieldName << CurrentObjectType); |
| 2296 | KnownField = Corrected.getCorrectionDeclAs<FieldDecl>(); |
Benjamin Kramer | afe718f | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 2297 | hadError = true; |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 2298 | } else { |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2299 | // Typo correction didn't find anything. |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 2300 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 2301 | << FieldName << CurrentObjectType; |
| 2302 | ++Index; |
| 2303 | return true; |
| 2304 | } |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 2305 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2306 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2307 | |
David Majnemer | 58e4ea9 | 2014-08-23 01:48:50 +0000 | [diff] [blame] | 2308 | unsigned FieldIndex = 0; |
Akira Hatanaka | 8eccb9b | 2017-01-17 19:35:54 +0000 | [diff] [blame] | 2309 | |
| 2310 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) |
| 2311 | FieldIndex = CXXRD->getNumBases(); |
| 2312 | |
David Majnemer | 58e4ea9 | 2014-08-23 01:48:50 +0000 | [diff] [blame] | 2313 | for (auto *FI : RT->getDecl()->fields()) { |
| 2314 | if (FI->isUnnamedBitfield()) |
| 2315 | continue; |
Richard Smith | fe1bc70 | 2016-04-08 19:57:40 +0000 | [diff] [blame] | 2316 | if (declaresSameEntity(KnownField, FI)) { |
| 2317 | KnownField = FI; |
David Majnemer | 58e4ea9 | 2014-08-23 01:48:50 +0000 | [diff] [blame] | 2318 | break; |
Richard Smith | fe1bc70 | 2016-04-08 19:57:40 +0000 | [diff] [blame] | 2319 | } |
David Majnemer | 58e4ea9 | 2014-08-23 01:48:50 +0000 | [diff] [blame] | 2320 | ++FieldIndex; |
| 2321 | } |
| 2322 | |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2323 | RecordDecl::field_iterator Field = |
| 2324 | RecordDecl::field_iterator(DeclContext::decl_iterator(KnownField)); |
| 2325 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2326 | // All of the fields of a union are located at the same place in |
| 2327 | // the initializer list. |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 2328 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2329 | FieldIndex = 0; |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2330 | if (!VerifyOnly) { |
| 2331 | FieldDecl *CurrentField = StructuredList->getInitializedFieldInUnion(); |
Richard Smith | fe1bc70 | 2016-04-08 19:57:40 +0000 | [diff] [blame] | 2332 | if (CurrentField && !declaresSameEntity(CurrentField, *Field)) { |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2333 | assert(StructuredList->getNumInits() == 1 |
| 2334 | && "A union should never have more than one initializer!"); |
| 2335 | |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2336 | Expr *ExistingInit = StructuredList->getInit(0); |
Vassil Vassilev | 1a1678e | 2017-04-14 08:48:08 +0000 | [diff] [blame] | 2337 | if (ExistingInit) { |
| 2338 | // We're about to throw away an initializer, emit warning. |
| 2339 | SemaRef.Diag(D->getFieldLoc(), |
| 2340 | diag::warn_initializer_overrides) |
| 2341 | << D->getSourceRange(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2342 | SemaRef.Diag(ExistingInit->getBeginLoc(), |
Vassil Vassilev | 1a1678e | 2017-04-14 08:48:08 +0000 | [diff] [blame] | 2343 | diag::note_previous_initializer) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2344 | << /*FIXME:has side effects=*/0 |
| 2345 | << ExistingInit->getSourceRange(); |
Vassil Vassilev | 1a1678e | 2017-04-14 08:48:08 +0000 | [diff] [blame] | 2346 | } |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2347 | |
| 2348 | // remove existing initializer |
| 2349 | StructuredList->resizeInits(SemaRef.Context, 0); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2350 | StructuredList->setInitializedFieldInUnion(nullptr); |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2351 | } |
| 2352 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2353 | StructuredList->setInitializedFieldInUnion(*Field); |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2354 | } |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 2355 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2356 | |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2357 | // Make sure we can use this declaration. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2358 | bool InvalidUse; |
| 2359 | if (VerifyOnly) |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 2360 | InvalidUse = !SemaRef.CanUseDecl(*Field, TreatUnavailableAsInvalid); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2361 | else |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2362 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, D->getFieldLoc()); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2363 | if (InvalidUse) { |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2364 | ++Index; |
| 2365 | return true; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2366 | } |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2367 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2368 | if (!VerifyOnly) { |
| 2369 | // Update the designator with the field declaration. |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2370 | D->setField(*Field); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2371 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2372 | // Make sure that our non-designated initializer list has space |
| 2373 | // for a subobject corresponding to this field. |
| 2374 | if (FieldIndex >= StructuredList->getNumInits()) |
| 2375 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
| 2376 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2377 | |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2378 | // This designator names a flexible array member. |
| 2379 | if (Field->getType()->isIncompleteArrayType()) { |
| 2380 | bool Invalid = false; |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 2381 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2382 | // We can't designate an object within the flexible array |
| 2383 | // member (because GCC doesn't allow it). |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2384 | if (!VerifyOnly) { |
| 2385 | DesignatedInitExpr::Designator *NextD |
| 2386 | = DIE->getDesignator(DesigIdx + 1); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2387 | SemaRef.Diag(NextD->getBeginLoc(), |
| 2388 | diag::err_designator_into_flexible_array_member) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2389 | << SourceRange(NextD->getBeginLoc(), DIE->getEndLoc()); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2390 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2391 | << *Field; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2392 | } |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2393 | Invalid = true; |
| 2394 | } |
| 2395 | |
Chris Lattner | 001b29c | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 2396 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 2397 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2398 | // The initializer is not an initializer list. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2399 | if (!VerifyOnly) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2400 | SemaRef.Diag(DIE->getInit()->getBeginLoc(), |
| 2401 | diag::err_flexible_array_init_needs_braces) |
| 2402 | << DIE->getInit()->getSourceRange(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2403 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2404 | << *Field; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2405 | } |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2406 | Invalid = true; |
| 2407 | } |
| 2408 | |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 2409 | // Check GNU flexible array initializer. |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2410 | if (!Invalid && CheckFlexibleArrayInit(Entity, DIE->getInit(), *Field, |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 2411 | TopLevelObject)) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2412 | Invalid = true; |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2413 | |
| 2414 | if (Invalid) { |
| 2415 | ++Index; |
| 2416 | return true; |
| 2417 | } |
| 2418 | |
| 2419 | // Initialize the array. |
| 2420 | bool prevHadError = hadError; |
| 2421 | unsigned newStructuredIndex = FieldIndex; |
| 2422 | unsigned OldIndex = Index; |
| 2423 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2424 | |
| 2425 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2426 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2427 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2428 | StructuredList, newStructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2429 | |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2430 | IList->setInit(OldIndex, DIE); |
| 2431 | if (hadError && !prevHadError) { |
| 2432 | ++Field; |
| 2433 | ++FieldIndex; |
| 2434 | if (NextField) |
| 2435 | *NextField = Field; |
| 2436 | StructuredIndex = FieldIndex; |
| 2437 | return true; |
| 2438 | } |
| 2439 | } else { |
| 2440 | // Recurse to check later designated subobjects. |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 2441 | QualType FieldType = Field->getType(); |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2442 | unsigned newStructuredIndex = FieldIndex; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2443 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2444 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2445 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2446 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2447 | FieldType, nullptr, nullptr, Index, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2448 | StructuredList, newStructuredIndex, |
Alexey Bataev | 86a489e | 2016-01-25 05:14:03 +0000 | [diff] [blame] | 2449 | FinishSubobjectInit, false)) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2450 | return true; |
| 2451 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2452 | |
| 2453 | // Find the position of the next field to be initialized in this |
| 2454 | // subobject. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2455 | ++Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2456 | ++FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2457 | |
| 2458 | // If this the first designator, our caller will continue checking |
| 2459 | // the rest of this struct/class/union subobject. |
| 2460 | if (IsFirstDesignator) { |
| 2461 | if (NextField) |
| 2462 | *NextField = Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2463 | StructuredIndex = FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2464 | return false; |
| 2465 | } |
| 2466 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2467 | if (!FinishSubobjectInit) |
| 2468 | return false; |
| 2469 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2470 | // We've already initialized something in the union; we're done. |
| 2471 | if (RT->getDecl()->isUnion()) |
| 2472 | return hadError; |
| 2473 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2474 | // Check the remaining fields within this class/struct/union subobject. |
| 2475 | bool prevHadError = hadError; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2476 | |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 2477 | auto NoBases = |
| 2478 | CXXRecordDecl::base_class_range(CXXRecordDecl::base_class_iterator(), |
| 2479 | CXXRecordDecl::base_class_iterator()); |
| 2480 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, NoBases, Field, |
| 2481 | false, Index, StructuredList, FieldIndex); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2482 | return hadError && !prevHadError; |
| 2483 | } |
| 2484 | |
| 2485 | // C99 6.7.8p6: |
| 2486 | // |
| 2487 | // If a designator has the form |
| 2488 | // |
| 2489 | // [ constant-expression ] |
| 2490 | // |
| 2491 | // then the current object (defined below) shall have array |
| 2492 | // type and the expression shall be an integer constant |
| 2493 | // expression. If the array is of unknown size, any |
| 2494 | // nonnegative value is valid. |
| 2495 | // |
| 2496 | // Additionally, cope with the GNU extension that permits |
| 2497 | // designators of the form |
| 2498 | // |
| 2499 | // [ constant-expression ... constant-expression ] |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 2500 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2501 | if (!AT) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2502 | if (!VerifyOnly) |
| 2503 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
| 2504 | << CurrentObjectType; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2505 | ++Index; |
| 2506 | return true; |
| 2507 | } |
| 2508 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2509 | Expr *IndexExpr = nullptr; |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2510 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 2511 | if (D->isArrayDesignator()) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2512 | IndexExpr = DIE->getArrayIndex(*D); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2513 | DesignatedStartIndex = IndexExpr->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2514 | DesignatedEndIndex = DesignatedStartIndex; |
| 2515 | } else { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2516 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2517 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2518 | DesignatedStartIndex = |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2519 | DIE->getArrayRangeStart(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2520 | DesignatedEndIndex = |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2521 | DIE->getArrayRangeEnd(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2522 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2523 | |
Chris Lattner | b0ed51d | 2011-02-19 22:28:58 +0000 | [diff] [blame] | 2524 | // Codegen can't handle evaluating array range designators that have side |
| 2525 | // effects, because we replicate the AST value for each initialized element. |
| 2526 | // As such, set the sawArrayRangeDesignator() bit if we initialize multiple |
| 2527 | // elements with something that has a side effect, so codegen can emit an |
| 2528 | // "error unsupported" error instead of miscompiling the app. |
| 2529 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2530 | DIE->getInit()->HasSideEffects(SemaRef.Context) && !VerifyOnly) |
Douglas Gregor | bf7207a | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 2531 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2534 | if (isa<ConstantArrayType>(AT)) { |
| 2535 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2536 | DesignatedStartIndex |
| 2537 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2538 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2539 | DesignatedEndIndex |
| 2540 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2541 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 2542 | if (DesignatedEndIndex >= MaxElements) { |
Eli Friedman | ed0f916 | 2011-09-26 18:53:43 +0000 | [diff] [blame] | 2543 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2544 | SemaRef.Diag(IndexExpr->getBeginLoc(), |
| 2545 | diag::err_array_designator_too_large) |
| 2546 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
| 2547 | << IndexExpr->getSourceRange(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2548 | ++Index; |
| 2549 | return true; |
| 2550 | } |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2551 | } else { |
Argyrios Kyrtzidis | 4746c2f | 2015-07-27 23:16:53 +0000 | [diff] [blame] | 2552 | unsigned DesignatedIndexBitWidth = |
| 2553 | ConstantArrayType::getMaxSizeBits(SemaRef.Context); |
| 2554 | DesignatedStartIndex = |
| 2555 | DesignatedStartIndex.extOrTrunc(DesignatedIndexBitWidth); |
| 2556 | DesignatedEndIndex = |
| 2557 | DesignatedEndIndex.extOrTrunc(DesignatedIndexBitWidth); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2558 | DesignatedStartIndex.setIsUnsigned(true); |
| 2559 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2560 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2561 | |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2562 | if (!VerifyOnly && StructuredList->isStringLiteralInit()) { |
| 2563 | // We're modifying a string literal init; we have to decompose the string |
| 2564 | // so we can modify the individual characters. |
| 2565 | ASTContext &Context = SemaRef.Context; |
| 2566 | Expr *SubExpr = StructuredList->getInit(0)->IgnoreParens(); |
| 2567 | |
| 2568 | // Compute the character type |
| 2569 | QualType CharTy = AT->getElementType(); |
| 2570 | |
| 2571 | // Compute the type of the integer literals. |
| 2572 | QualType PromotedCharTy = CharTy; |
| 2573 | if (CharTy->isPromotableIntegerType()) |
| 2574 | PromotedCharTy = Context.getPromotedIntegerType(CharTy); |
| 2575 | unsigned PromotedCharTyWidth = Context.getTypeSize(PromotedCharTy); |
| 2576 | |
| 2577 | if (StringLiteral *SL = dyn_cast<StringLiteral>(SubExpr)) { |
| 2578 | // Get the length of the string. |
| 2579 | uint64_t StrLen = SL->getLength(); |
| 2580 | if (cast<ConstantArrayType>(AT)->getSize().ult(StrLen)) |
| 2581 | StrLen = cast<ConstantArrayType>(AT)->getSize().getZExtValue(); |
| 2582 | StructuredList->resizeInits(Context, StrLen); |
| 2583 | |
| 2584 | // Build a literal for each character in the string, and put them into |
| 2585 | // the init list. |
| 2586 | for (unsigned i = 0, e = StrLen; i != e; ++i) { |
| 2587 | llvm::APInt CodeUnit(PromotedCharTyWidth, SL->getCodeUnit(i)); |
| 2588 | Expr *Init = new (Context) IntegerLiteral( |
Eli Friedman | 6cc05f7 | 2013-06-11 22:26:34 +0000 | [diff] [blame] | 2589 | Context, CodeUnit, PromotedCharTy, SubExpr->getExprLoc()); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2590 | if (CharTy != PromotedCharTy) |
| 2591 | Init = ImplicitCastExpr::Create(Context, CharTy, CK_IntegralCast, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2592 | Init, nullptr, VK_RValue); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2593 | StructuredList->updateInit(Context, i, Init); |
| 2594 | } |
| 2595 | } else { |
| 2596 | ObjCEncodeExpr *E = cast<ObjCEncodeExpr>(SubExpr); |
| 2597 | std::string Str; |
| 2598 | Context.getObjCEncodingForType(E->getEncodedType(), Str); |
| 2599 | |
| 2600 | // Get the length of the string. |
| 2601 | uint64_t StrLen = Str.size(); |
| 2602 | if (cast<ConstantArrayType>(AT)->getSize().ult(StrLen)) |
| 2603 | StrLen = cast<ConstantArrayType>(AT)->getSize().getZExtValue(); |
| 2604 | StructuredList->resizeInits(Context, StrLen); |
| 2605 | |
| 2606 | // Build a literal for each character in the string, and put them into |
| 2607 | // the init list. |
| 2608 | for (unsigned i = 0, e = StrLen; i != e; ++i) { |
| 2609 | llvm::APInt CodeUnit(PromotedCharTyWidth, Str[i]); |
| 2610 | Expr *Init = new (Context) IntegerLiteral( |
Eli Friedman | 6cc05f7 | 2013-06-11 22:26:34 +0000 | [diff] [blame] | 2611 | Context, CodeUnit, PromotedCharTy, SubExpr->getExprLoc()); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2612 | if (CharTy != PromotedCharTy) |
| 2613 | Init = ImplicitCastExpr::Create(Context, CharTy, CK_IntegralCast, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2614 | Init, nullptr, VK_RValue); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2615 | StructuredList->updateInit(Context, i, Init); |
| 2616 | } |
| 2617 | } |
| 2618 | } |
| 2619 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2620 | // Make sure that our non-designated initializer list has space |
| 2621 | // for a subobject corresponding to this array element. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2622 | if (!VerifyOnly && |
| 2623 | DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2624 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2625 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2626 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2627 | // Repeatedly perform subobject initializations in the range |
| 2628 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2629 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2630 | // Move to the next designator |
| 2631 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2632 | unsigned OldIndex = Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2633 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2634 | InitializedEntity ElementEntity = |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2635 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2636 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2637 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 2638 | // Recurse to check later designated subobjects. |
| 2639 | QualType ElementType = AT->getElementType(); |
| 2640 | Index = OldIndex; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2641 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2642 | ElementEntity.setElementIndex(ElementIndex); |
Alexey Bataev | 86a489e | 2016-01-25 05:14:03 +0000 | [diff] [blame] | 2643 | if (CheckDesignatedInitializer( |
| 2644 | ElementEntity, IList, DIE, DesigIdx + 1, ElementType, nullptr, |
| 2645 | nullptr, Index, StructuredList, ElementIndex, |
| 2646 | FinishSubobjectInit && (DesignatedStartIndex == DesignatedEndIndex), |
| 2647 | false)) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2648 | return true; |
| 2649 | |
| 2650 | // Move to the next index in the array that we'll be initializing. |
| 2651 | ++DesignatedStartIndex; |
| 2652 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2653 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2654 | |
| 2655 | // If this the first designator, our caller will continue checking |
| 2656 | // the rest of this array subobject. |
| 2657 | if (IsFirstDesignator) { |
| 2658 | if (NextElementIndex) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2659 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2660 | StructuredIndex = ElementIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2661 | return false; |
| 2662 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2663 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2664 | if (!FinishSubobjectInit) |
| 2665 | return false; |
| 2666 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2667 | // Check the remaining elements within this array subobject. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2668 | bool prevHadError = hadError; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2669 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 2670 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2671 | StructuredList, ElementIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2672 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2673 | } |
| 2674 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2675 | // Get the structured initializer list for a subobject of type |
| 2676 | // @p CurrentObjectType. |
| 2677 | InitListExpr * |
| 2678 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 2679 | QualType CurrentObjectType, |
| 2680 | InitListExpr *StructuredList, |
| 2681 | unsigned StructuredIndex, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2682 | SourceRange InitRange, |
| 2683 | bool IsFullyOverwritten) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2684 | if (VerifyOnly) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2685 | return nullptr; // No structured list in verification-only mode. |
| 2686 | Expr *ExistingInit = nullptr; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2687 | if (!StructuredList) |
Benjamin Kramer | 6b441d6 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 2688 | ExistingInit = SyntacticToSemantic.lookup(IList); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2689 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 2690 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2691 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2692 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2693 | // There might have already been initializers for subobjects of the current |
| 2694 | // object, but a subsequent initializer list will overwrite the entirety |
| 2695 | // of the current object. (See DR 253 and C99 6.7.8p21). e.g., |
| 2696 | // |
| 2697 | // struct P { char x[6]; }; |
| 2698 | // struct P l = { .x[2] = 'x', .x = { [0] = 'f' } }; |
| 2699 | // |
| 2700 | // The first designated initializer is ignored, and l.x is just "f". |
| 2701 | if (!IsFullyOverwritten) |
| 2702 | return Result; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2703 | |
| 2704 | if (ExistingInit) { |
| 2705 | // We are creating an initializer list that initializes the |
| 2706 | // subobjects of the current object, but there was already an |
| 2707 | // initialization that completely initialized the current |
| 2708 | // subobject, e.g., by a compound literal: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2709 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2710 | // struct X { int a, b; }; |
| 2711 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2712 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2713 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 2714 | // designated initializer re-initializes the whole |
| 2715 | // subobject [0], overwriting previous initializers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2716 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2717 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2718 | << InitRange; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2719 | SemaRef.Diag(ExistingInit->getBeginLoc(), diag::note_previous_initializer) |
| 2720 | << /*FIXME:has side effects=*/0 << ExistingInit->getSourceRange(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2721 | } |
| 2722 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2723 | InitListExpr *Result |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2724 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
Dmitri Gribenko | 78852e9 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 2725 | InitRange.getBegin(), None, |
Ted Kremenek | 013041e | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 2726 | InitRange.getEnd()); |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2727 | |
Eli Friedman | 91f5ae5 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 2728 | QualType ResultType = CurrentObjectType; |
| 2729 | if (!ResultType->isArrayType()) |
| 2730 | ResultType = ResultType.getNonLValueExprType(SemaRef.Context); |
| 2731 | Result->setType(ResultType); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2732 | |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2733 | // Pre-allocate storage for the structured initializer list. |
| 2734 | unsigned NumElements = 0; |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2735 | unsigned NumInits = 0; |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2736 | bool GotNumInits = false; |
| 2737 | if (!StructuredList) { |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2738 | NumInits = IList->getNumInits(); |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2739 | GotNumInits = true; |
| 2740 | } else if (Index < IList->getNumInits()) { |
| 2741 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) { |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2742 | NumInits = SubList->getNumInits(); |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2743 | GotNumInits = true; |
| 2744 | } |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2745 | } |
| 2746 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2747 | if (const ArrayType *AType |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2748 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 2749 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 2750 | NumElements = CAType->getSize().getZExtValue(); |
| 2751 | // Simple heuristic so that we don't allocate a very large |
| 2752 | // initializer with many empty entries at the end. |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2753 | if (GotNumInits && NumElements > NumInits) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2754 | NumElements = 0; |
| 2755 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2756 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2757 | NumElements = VType->getNumElements(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2758 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2759 | RecordDecl *RDecl = RType->getDecl(); |
| 2760 | if (RDecl->isUnion()) |
| 2761 | NumElements = 1; |
| 2762 | else |
Aaron Ballman | 62e47c4 | 2014-03-10 13:43:55 +0000 | [diff] [blame] | 2763 | NumElements = std::distance(RDecl->field_begin(), RDecl->field_end()); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2764 | } |
| 2765 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2766 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2767 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2768 | // Link this new initializer list into the structured initializer |
| 2769 | // lists. |
| 2770 | if (StructuredList) |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2771 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2772 | else { |
| 2773 | Result->setSyntacticForm(IList); |
| 2774 | SyntacticToSemantic[IList] = Result; |
| 2775 | } |
| 2776 | |
| 2777 | return Result; |
| 2778 | } |
| 2779 | |
| 2780 | /// Update the initializer at index @p StructuredIndex within the |
| 2781 | /// structured initializer list to the value @p expr. |
| 2782 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 2783 | unsigned &StructuredIndex, |
| 2784 | Expr *expr) { |
| 2785 | // No structured initializer list to update |
| 2786 | if (!StructuredList) |
| 2787 | return; |
| 2788 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2789 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 2790 | StructuredIndex, expr)) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2791 | // This initializer overwrites a previous initializer. Warn. |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2792 | // We need to check on source range validity because the previous |
| 2793 | // initializer does not have to be an explicit initializer. |
| 2794 | // struct P { int a, b; }; |
| 2795 | // struct PP { struct P p } l = { { .a = 2 }, .p.b = 3 }; |
| 2796 | // There is an overwrite taking place because the first braced initializer |
| 2797 | // list "{ .a = 2 }' already provides value for .p.b (which is zero). |
| 2798 | if (PrevInit->getSourceRange().isValid()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2799 | SemaRef.Diag(expr->getBeginLoc(), diag::warn_initializer_overrides) |
| 2800 | << expr->getSourceRange(); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2801 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2802 | SemaRef.Diag(PrevInit->getBeginLoc(), diag::note_previous_initializer) |
| 2803 | << /*FIXME:has side effects=*/0 << PrevInit->getSourceRange(); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2804 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2805 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2806 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2807 | ++StructuredIndex; |
| 2808 | } |
| 2809 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2810 | /// Check that the given Index expression is a valid array designator |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2811 | /// value. This is essentially just a wrapper around |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2812 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2813 | /// and produces a reasonable diagnostic if there is a |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2814 | /// failure. Returns the index expression, possibly with an implicit cast |
| 2815 | /// added, on success. If everything went okay, Value will receive the |
| 2816 | /// value of the constant expression. |
| 2817 | static ExprResult |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2818 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2819 | SourceLocation Loc = Index->getBeginLoc(); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2820 | |
| 2821 | // Make sure this is an integer constant expression. |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2822 | ExprResult Result = S.VerifyIntegerConstantExpression(Index, &Value); |
| 2823 | if (Result.isInvalid()) |
| 2824 | return Result; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2825 | |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2826 | if (Value.isSigned() && Value.isNegative()) |
| 2827 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2828 | << Value.toString(10) << Index->getSourceRange(); |
| 2829 | |
Douglas Gregor | 51650d3 | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 2830 | Value.setIsUnsigned(true); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2831 | return Result; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2832 | } |
| 2833 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2834 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 2835 | SourceLocation Loc, |
| 2836 | bool GNUSyntax, |
| 2837 | ExprResult Init) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2838 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 2839 | |
| 2840 | bool Invalid = false; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2841 | SmallVector<ASTDesignator, 32> Designators; |
| 2842 | SmallVector<Expr *, 32> InitExpressions; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2843 | |
| 2844 | // Build designators and check array designator expressions. |
| 2845 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 2846 | const Designator &D = Desig.getDesignator(Idx); |
| 2847 | switch (D.getKind()) { |
| 2848 | case Designator::FieldDesignator: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2849 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2850 | D.getFieldLoc())); |
| 2851 | break; |
| 2852 | |
| 2853 | case Designator::ArrayDesignator: { |
| 2854 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 2855 | llvm::APSInt IndexValue; |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2856 | if (!Index->isTypeDependent() && !Index->isValueDependent()) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2857 | Index = CheckArrayDesignatorExpr(*this, Index, IndexValue).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2858 | if (!Index) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2859 | Invalid = true; |
| 2860 | else { |
| 2861 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2862 | D.getLBracketLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2863 | D.getRBracketLoc())); |
| 2864 | InitExpressions.push_back(Index); |
| 2865 | } |
| 2866 | break; |
| 2867 | } |
| 2868 | |
| 2869 | case Designator::ArrayRangeDesignator: { |
| 2870 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 2871 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 2872 | llvm::APSInt StartValue; |
| 2873 | llvm::APSInt EndValue; |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2874 | bool StartDependent = StartIndex->isTypeDependent() || |
| 2875 | StartIndex->isValueDependent(); |
| 2876 | bool EndDependent = EndIndex->isTypeDependent() || |
| 2877 | EndIndex->isValueDependent(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2878 | if (!StartDependent) |
| 2879 | StartIndex = |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2880 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2881 | if (!EndDependent) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2882 | EndIndex = CheckArrayDesignatorExpr(*this, EndIndex, EndValue).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2883 | |
| 2884 | if (!StartIndex || !EndIndex) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2885 | Invalid = true; |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2886 | else { |
| 2887 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2888 | if (StartDependent || EndDependent) { |
| 2889 | // Nothing to compute. |
| 2890 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2891 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2892 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2893 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2894 | |
Douglas Gregor | 0f9d400 | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 2895 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2896 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2897 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2898 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 2899 | Invalid = true; |
| 2900 | } else { |
| 2901 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2902 | D.getLBracketLoc(), |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2903 | D.getEllipsisLoc(), |
| 2904 | D.getRBracketLoc())); |
| 2905 | InitExpressions.push_back(StartIndex); |
| 2906 | InitExpressions.push_back(EndIndex); |
| 2907 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2908 | } |
| 2909 | break; |
| 2910 | } |
| 2911 | } |
| 2912 | } |
| 2913 | |
| 2914 | if (Invalid || Init.isInvalid()) |
| 2915 | return ExprError(); |
| 2916 | |
| 2917 | // Clear out the expressions within the designation. |
| 2918 | Desig.ClearExprs(*this); |
| 2919 | |
| 2920 | DesignatedInitExpr *DIE |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2921 | = DesignatedInitExpr::Create(Context, |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 2922 | Designators, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2923 | InitExpressions, Loc, GNUSyntax, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2924 | Init.getAs<Expr>()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2925 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2926 | if (!getLangOpts().C99) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2927 | Diag(DIE->getBeginLoc(), diag::ext_designated_init) |
| 2928 | << DIE->getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2929 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2930 | return DIE; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2931 | } |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 2932 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2933 | //===----------------------------------------------------------------------===// |
| 2934 | // Initialization entity |
| 2935 | //===----------------------------------------------------------------------===// |
| 2936 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2937 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2938 | const InitializedEntity &Parent) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2939 | : Parent(&Parent), Index(Index) |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2940 | { |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2941 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 2942 | Kind = EK_ArrayElement; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2943 | Type = AT->getElementType(); |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2944 | } else if (const VectorType *VT = Parent.getType()->getAs<VectorType>()) { |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2945 | Kind = EK_VectorElement; |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2946 | Type = VT->getElementType(); |
| 2947 | } else { |
| 2948 | const ComplexType *CT = Parent.getType()->getAs<ComplexType>(); |
| 2949 | assert(CT && "Unexpected type"); |
| 2950 | Kind = EK_ComplexElement; |
| 2951 | Type = CT->getElementType(); |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2952 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2953 | } |
| 2954 | |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2955 | InitializedEntity |
| 2956 | InitializedEntity::InitializeBase(ASTContext &Context, |
| 2957 | const CXXBaseSpecifier *Base, |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 2958 | bool IsInheritedVirtualBase, |
| 2959 | const InitializedEntity *Parent) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2960 | InitializedEntity Result; |
| 2961 | Result.Kind = EK_Base; |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 2962 | Result.Parent = Parent; |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2963 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 2964 | if (IsInheritedVirtualBase) |
| 2965 | Result.Base |= 0x01; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2966 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2967 | Result.Type = Base->getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2968 | return Result; |
| 2969 | } |
| 2970 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2971 | DeclarationName InitializedEntity::getName() const { |
| 2972 | switch (getKind()) { |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2973 | case EK_Parameter: |
| 2974 | case EK_Parameter_CF_Audited: { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2975 | ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2976 | return (D ? D->getDeclName() : DeclarationName()); |
| 2977 | } |
Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 2978 | |
| 2979 | case EK_Variable: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2980 | case EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2981 | case EK_Binding: |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 2982 | return Variable.VariableOrMember->getDeclName(); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2983 | |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2984 | case EK_LambdaCapture: |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 2985 | return DeclarationName(Capture.VarID); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2986 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2987 | case EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 2988 | case EK_StmtExprResult: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2989 | case EK_Exception: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2990 | case EK_New: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2991 | case EK_Temporary: |
| 2992 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2993 | case EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2994 | case EK_ArrayElement: |
| 2995 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2996 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2997 | case EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 2998 | case EK_LambdaToBlockConversionBlockElement: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 2999 | case EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 3000 | case EK_RelatedResult: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3001 | return DeclarationName(); |
| 3002 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3003 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3004 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3005 | } |
| 3006 | |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3007 | ValueDecl *InitializedEntity::getDecl() const { |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3008 | switch (getKind()) { |
| 3009 | case EK_Variable: |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3010 | case EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3011 | case EK_Binding: |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3012 | return Variable.VariableOrMember; |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3013 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3014 | case EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3015 | case EK_Parameter_CF_Audited: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3016 | return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 3017 | |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3018 | case EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 3019 | case EK_StmtExprResult: |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3020 | case EK_Exception: |
| 3021 | case EK_New: |
| 3022 | case EK_Temporary: |
| 3023 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3024 | case EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3025 | case EK_ArrayElement: |
| 3026 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3027 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3028 | case EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3029 | case EK_LambdaToBlockConversionBlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 3030 | case EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 3031 | case EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 3032 | case EK_RelatedResult: |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3033 | return nullptr; |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3034 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3035 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3036 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3037 | } |
| 3038 | |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3039 | bool InitializedEntity::allowsNRVO() const { |
| 3040 | switch (getKind()) { |
| 3041 | case EK_Result: |
| 3042 | case EK_Exception: |
| 3043 | return LocAndNRVO.NRVO; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3044 | |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 3045 | case EK_StmtExprResult: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3046 | case EK_Variable: |
| 3047 | case EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3048 | case EK_Parameter_CF_Audited: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3049 | case EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3050 | case EK_Binding: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3051 | case EK_New: |
| 3052 | case EK_Temporary: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 3053 | case EK_CompoundLiteralInit: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3054 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3055 | case EK_Delegating: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3056 | case EK_ArrayElement: |
| 3057 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3058 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3059 | case EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3060 | case EK_LambdaToBlockConversionBlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 3061 | case EK_LambdaCapture: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 3062 | case EK_RelatedResult: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3063 | break; |
| 3064 | } |
| 3065 | |
| 3066 | return false; |
| 3067 | } |
| 3068 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3069 | unsigned InitializedEntity::dumpImpl(raw_ostream &OS) const { |
Richard Smith | e3b28bc | 2013-06-12 21:51:50 +0000 | [diff] [blame] | 3070 | assert(getParent() != this); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3071 | unsigned Depth = getParent() ? getParent()->dumpImpl(OS) : 0; |
| 3072 | for (unsigned I = 0; I != Depth; ++I) |
| 3073 | OS << "`-"; |
| 3074 | |
| 3075 | switch (getKind()) { |
| 3076 | case EK_Variable: OS << "Variable"; break; |
| 3077 | case EK_Parameter: OS << "Parameter"; break; |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3078 | case EK_Parameter_CF_Audited: OS << "CF audited function Parameter"; |
| 3079 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3080 | case EK_Result: OS << "Result"; break; |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 3081 | case EK_StmtExprResult: OS << "StmtExprResult"; break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3082 | case EK_Exception: OS << "Exception"; break; |
| 3083 | case EK_Member: OS << "Member"; break; |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3084 | case EK_Binding: OS << "Binding"; break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3085 | case EK_New: OS << "New"; break; |
| 3086 | case EK_Temporary: OS << "Temporary"; break; |
| 3087 | case EK_CompoundLiteralInit: OS << "CompoundLiteral";break; |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 3088 | case EK_RelatedResult: OS << "RelatedResult"; break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3089 | case EK_Base: OS << "Base"; break; |
| 3090 | case EK_Delegating: OS << "Delegating"; break; |
| 3091 | case EK_ArrayElement: OS << "ArrayElement " << Index; break; |
| 3092 | case EK_VectorElement: OS << "VectorElement " << Index; break; |
| 3093 | case EK_ComplexElement: OS << "ComplexElement " << Index; break; |
| 3094 | case EK_BlockElement: OS << "Block"; break; |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3095 | case EK_LambdaToBlockConversionBlockElement: |
| 3096 | OS << "Block (lambda)"; |
| 3097 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3098 | case EK_LambdaCapture: |
| 3099 | OS << "LambdaCapture "; |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 3100 | OS << DeclarationName(Capture.VarID); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3101 | break; |
| 3102 | } |
| 3103 | |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3104 | if (auto *D = getDecl()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3105 | OS << " "; |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3106 | D->printQualifiedName(OS); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3107 | } |
| 3108 | |
| 3109 | OS << " '" << getType().getAsString() << "'\n"; |
| 3110 | |
| 3111 | return Depth + 1; |
| 3112 | } |
| 3113 | |
Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 3114 | LLVM_DUMP_METHOD void InitializedEntity::dump() const { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3115 | dumpImpl(llvm::errs()); |
| 3116 | } |
| 3117 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3118 | //===----------------------------------------------------------------------===// |
| 3119 | // Initialization sequence |
| 3120 | //===----------------------------------------------------------------------===// |
| 3121 | |
| 3122 | void InitializationSequence::Step::Destroy() { |
| 3123 | switch (Kind) { |
| 3124 | case SK_ResolveAddressOfOverloadedFunction: |
| 3125 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3126 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3127 | case SK_CastDerivedToBaseLValue: |
| 3128 | case SK_BindReference: |
| 3129 | case SK_BindReferenceToTemporary: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 3130 | case SK_FinalCopy: |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3131 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3132 | case SK_UserConversion: |
| 3133 | case SK_QualificationConversionRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3134 | case SK_QualificationConversionXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3135 | case SK_QualificationConversionLValue: |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 3136 | case SK_AtomicConversion: |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3137 | case SK_LValueToRValue: |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3138 | case SK_ListInitialization: |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3139 | case SK_UnwrapInitList: |
| 3140 | case SK_RewrapInitList: |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3141 | case SK_ConstructorInitialization: |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 3142 | case SK_ConstructorInitializationFromList: |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3143 | case SK_ZeroInitialization: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3144 | case SK_CAssignment: |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3145 | case SK_StringInit: |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3146 | case SK_ObjCObjectConversion: |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3147 | case SK_ArrayLoopIndex: |
| 3148 | case SK_ArrayLoopInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3149 | case SK_ArrayInit: |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 3150 | case SK_GNUArrayInit: |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 3151 | case SK_ParenthesizedArrayInit: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3152 | case SK_PassByIndirectCopyRestore: |
| 3153 | case SK_PassByIndirectRestore: |
| 3154 | case SK_ProduceObjCObject: |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 3155 | case SK_StdInitializerList: |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3156 | case SK_StdInitializerListConstructorCall: |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 3157 | case SK_OCLSamplerInit: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 3158 | case SK_OCLZeroEvent: |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 3159 | case SK_OCLZeroQueue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3160 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3161 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3162 | case SK_ConversionSequence: |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 3163 | case SK_ConversionSequenceNoNarrowing: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3164 | delete ICS; |
| 3165 | } |
| 3166 | } |
| 3167 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3168 | bool InitializationSequence::isDirectReferenceBinding() const { |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 3169 | // There can be some lvalue adjustments after the SK_BindReference step. |
| 3170 | for (auto I = Steps.rbegin(); I != Steps.rend(); ++I) { |
| 3171 | if (I->Kind == SK_BindReference) |
| 3172 | return true; |
| 3173 | if (I->Kind == SK_BindReferenceToTemporary) |
| 3174 | return false; |
| 3175 | } |
| 3176 | return false; |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3177 | } |
| 3178 | |
| 3179 | bool InitializationSequence::isAmbiguous() const { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 3180 | if (!Failed()) |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3181 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3182 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3183 | switch (getFailureKind()) { |
| 3184 | case FK_TooManyInitsForReference: |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 3185 | case FK_ParenthesizedListInitForReference: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3186 | case FK_ArrayNeedsInitList: |
| 3187 | case FK_ArrayNeedsInitListOrStringLiteral: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 3188 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 3189 | case FK_NarrowStringIntoWideCharArray: |
| 3190 | case FK_WideStringIntoCharArray: |
| 3191 | case FK_IncompatWideStringIntoWideChar: |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 3192 | case FK_PlainStringIntoUTF8Char: |
| 3193 | case FK_UTF8StringIntoPlainChar: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3194 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 3195 | case FK_NonConstLValueReferenceBindingToTemporary: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 3196 | case FK_NonConstLValueReferenceBindingToBitfield: |
| 3197 | case FK_NonConstLValueReferenceBindingToVectorElement: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3198 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 3199 | case FK_RValueReferenceBindingToLValue: |
| 3200 | case FK_ReferenceInitDropsQualifiers: |
| 3201 | case FK_ReferenceInitFailed: |
| 3202 | case FK_ConversionFailed: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3203 | case FK_ConversionFromPropertyFailed: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3204 | case FK_TooManyInitsForScalar: |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 3205 | case FK_ParenthesizedListInitForScalar: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3206 | case FK_ReferenceBindingToInitList: |
| 3207 | case FK_InitListBadDestinationType: |
| 3208 | case FK_DefaultInitOfConst: |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 3209 | case FK_Incomplete: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3210 | case FK_ArrayTypeMismatch: |
| 3211 | case FK_NonConstantArrayInit: |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 3212 | case FK_ListInitializationFailed: |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 3213 | case FK_VariableLengthArrayHasInitializer: |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3214 | case FK_PlaceholderType: |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3215 | case FK_ExplicitConstructor: |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 3216 | case FK_AddressOfUnaddressableFunction: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3217 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3218 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3219 | case FK_ReferenceInitOverloadFailed: |
| 3220 | case FK_UserConversionOverloadFailed: |
| 3221 | case FK_ConstructorOverloadFailed: |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3222 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3223 | return FailedOverloadResult == OR_Ambiguous; |
| 3224 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3225 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3226 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3227 | } |
| 3228 | |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 3229 | bool InitializationSequence::isConstructorInitialization() const { |
| 3230 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 3231 | } |
| 3232 | |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3233 | void |
| 3234 | InitializationSequence |
| 3235 | ::AddAddressOverloadResolutionStep(FunctionDecl *Function, |
| 3236 | DeclAccessPair Found, |
| 3237 | bool HadMultipleCandidates) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3238 | Step S; |
| 3239 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 3240 | S.Type = Function->getType(); |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3241 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3242 | S.Function.Function = Function; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3243 | S.Function.FoundDecl = Found; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3244 | Steps.push_back(S); |
| 3245 | } |
| 3246 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3247 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3248 | ExprValueKind VK) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3249 | Step S; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3250 | switch (VK) { |
| 3251 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 3252 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 3253 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3254 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3255 | S.Type = BaseType; |
| 3256 | Steps.push_back(S); |
| 3257 | } |
| 3258 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3259 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3260 | bool BindingTemporary) { |
| 3261 | Step S; |
| 3262 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 3263 | S.Type = T; |
| 3264 | Steps.push_back(S); |
| 3265 | } |
| 3266 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 3267 | void InitializationSequence::AddFinalCopy(QualType T) { |
| 3268 | Step S; |
| 3269 | S.Kind = SK_FinalCopy; |
| 3270 | S.Type = T; |
| 3271 | Steps.push_back(S); |
| 3272 | } |
| 3273 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3274 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 3275 | Step S; |
| 3276 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 3277 | S.Type = T; |
| 3278 | Steps.push_back(S); |
| 3279 | } |
| 3280 | |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3281 | void |
| 3282 | InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
| 3283 | DeclAccessPair FoundDecl, |
| 3284 | QualType T, |
| 3285 | bool HadMultipleCandidates) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3286 | Step S; |
| 3287 | S.Kind = SK_UserConversion; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3288 | S.Type = T; |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3289 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3290 | S.Function.Function = Function; |
| 3291 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3292 | Steps.push_back(S); |
| 3293 | } |
| 3294 | |
| 3295 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3296 | ExprValueKind VK) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3297 | Step S; |
John McCall | 7a1da89 | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 3298 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3299 | switch (VK) { |
| 3300 | case VK_RValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3301 | S.Kind = SK_QualificationConversionRValue; |
| 3302 | break; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3303 | case VK_XValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3304 | S.Kind = SK_QualificationConversionXValue; |
| 3305 | break; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3306 | case VK_LValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3307 | S.Kind = SK_QualificationConversionLValue; |
| 3308 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3309 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3310 | S.Type = Ty; |
| 3311 | Steps.push_back(S); |
| 3312 | } |
| 3313 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 3314 | void InitializationSequence::AddAtomicConversionStep(QualType Ty) { |
| 3315 | Step S; |
| 3316 | S.Kind = SK_AtomicConversion; |
| 3317 | S.Type = Ty; |
| 3318 | Steps.push_back(S); |
| 3319 | } |
| 3320 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3321 | void InitializationSequence::AddLValueToRValueStep(QualType Ty) { |
| 3322 | assert(!Ty.hasQualifiers() && "rvalues may not have qualifiers"); |
| 3323 | |
| 3324 | Step S; |
| 3325 | S.Kind = SK_LValueToRValue; |
| 3326 | S.Type = Ty; |
| 3327 | Steps.push_back(S); |
| 3328 | } |
| 3329 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3330 | void InitializationSequence::AddConversionSequenceStep( |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 3331 | const ImplicitConversionSequence &ICS, QualType T, |
| 3332 | bool TopLevelOfInitList) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3333 | Step S; |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 3334 | S.Kind = TopLevelOfInitList ? SK_ConversionSequenceNoNarrowing |
| 3335 | : SK_ConversionSequence; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3336 | S.Type = T; |
| 3337 | S.ICS = new ImplicitConversionSequence(ICS); |
| 3338 | Steps.push_back(S); |
| 3339 | } |
| 3340 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3341 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 3342 | Step S; |
| 3343 | S.Kind = SK_ListInitialization; |
| 3344 | S.Type = T; |
| 3345 | Steps.push_back(S); |
| 3346 | } |
| 3347 | |
Richard Smith | 55c2888 | 2016-05-12 23:45:49 +0000 | [diff] [blame] | 3348 | void InitializationSequence::AddConstructorInitializationStep( |
| 3349 | DeclAccessPair FoundDecl, CXXConstructorDecl *Constructor, QualType T, |
| 3350 | bool HadMultipleCandidates, bool FromInitList, bool AsInitList) { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3351 | Step S; |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3352 | S.Kind = FromInitList ? AsInitList ? SK_StdInitializerListConstructorCall |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 3353 | : SK_ConstructorInitializationFromList |
| 3354 | : SK_ConstructorInitialization; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3355 | S.Type = T; |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3356 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3357 | S.Function.Function = Constructor; |
Richard Smith | 55c2888 | 2016-05-12 23:45:49 +0000 | [diff] [blame] | 3358 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3359 | Steps.push_back(S); |
| 3360 | } |
| 3361 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3362 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 3363 | Step S; |
| 3364 | S.Kind = SK_ZeroInitialization; |
| 3365 | S.Type = T; |
| 3366 | Steps.push_back(S); |
| 3367 | } |
| 3368 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3369 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 3370 | Step S; |
| 3371 | S.Kind = SK_CAssignment; |
| 3372 | S.Type = T; |
| 3373 | Steps.push_back(S); |
| 3374 | } |
| 3375 | |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3376 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 3377 | Step S; |
| 3378 | S.Kind = SK_StringInit; |
| 3379 | S.Type = T; |
| 3380 | Steps.push_back(S); |
| 3381 | } |
| 3382 | |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3383 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 3384 | Step S; |
| 3385 | S.Kind = SK_ObjCObjectConversion; |
| 3386 | S.Type = T; |
| 3387 | Steps.push_back(S); |
| 3388 | } |
| 3389 | |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 3390 | void InitializationSequence::AddArrayInitStep(QualType T, bool IsGNUExtension) { |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3391 | Step S; |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 3392 | S.Kind = IsGNUExtension ? SK_GNUArrayInit : SK_ArrayInit; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3393 | S.Type = T; |
| 3394 | Steps.push_back(S); |
| 3395 | } |
| 3396 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3397 | void InitializationSequence::AddArrayInitLoopStep(QualType T, QualType EltT) { |
| 3398 | Step S; |
| 3399 | S.Kind = SK_ArrayLoopIndex; |
| 3400 | S.Type = EltT; |
| 3401 | Steps.insert(Steps.begin(), S); |
| 3402 | |
| 3403 | S.Kind = SK_ArrayLoopInit; |
| 3404 | S.Type = T; |
| 3405 | Steps.push_back(S); |
| 3406 | } |
| 3407 | |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 3408 | void InitializationSequence::AddParenthesizedArrayInitStep(QualType T) { |
| 3409 | Step S; |
| 3410 | S.Kind = SK_ParenthesizedArrayInit; |
| 3411 | S.Type = T; |
| 3412 | Steps.push_back(S); |
| 3413 | } |
| 3414 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3415 | void InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type, |
| 3416 | bool shouldCopy) { |
| 3417 | Step s; |
| 3418 | s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore |
| 3419 | : SK_PassByIndirectRestore); |
| 3420 | s.Type = type; |
| 3421 | Steps.push_back(s); |
| 3422 | } |
| 3423 | |
| 3424 | void InitializationSequence::AddProduceObjCObjectStep(QualType T) { |
| 3425 | Step S; |
| 3426 | S.Kind = SK_ProduceObjCObject; |
| 3427 | S.Type = T; |
| 3428 | Steps.push_back(S); |
| 3429 | } |
| 3430 | |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 3431 | void InitializationSequence::AddStdInitializerListConstructionStep(QualType T) { |
| 3432 | Step S; |
| 3433 | S.Kind = SK_StdInitializerList; |
| 3434 | S.Type = T; |
| 3435 | Steps.push_back(S); |
| 3436 | } |
| 3437 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 3438 | void InitializationSequence::AddOCLSamplerInitStep(QualType T) { |
| 3439 | Step S; |
| 3440 | S.Kind = SK_OCLSamplerInit; |
| 3441 | S.Type = T; |
| 3442 | Steps.push_back(S); |
| 3443 | } |
| 3444 | |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 3445 | void InitializationSequence::AddOCLZeroEventStep(QualType T) { |
| 3446 | Step S; |
| 3447 | S.Kind = SK_OCLZeroEvent; |
| 3448 | S.Type = T; |
| 3449 | Steps.push_back(S); |
| 3450 | } |
| 3451 | |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 3452 | void InitializationSequence::AddOCLZeroQueueStep(QualType T) { |
| 3453 | Step S; |
| 3454 | S.Kind = SK_OCLZeroQueue; |
| 3455 | S.Type = T; |
| 3456 | Steps.push_back(S); |
| 3457 | } |
| 3458 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3459 | void InitializationSequence::RewrapReferenceInitList(QualType T, |
| 3460 | InitListExpr *Syntactic) { |
| 3461 | assert(Syntactic->getNumInits() == 1 && |
| 3462 | "Can only rewrap trivial init lists."); |
| 3463 | Step S; |
| 3464 | S.Kind = SK_UnwrapInitList; |
| 3465 | S.Type = Syntactic->getInit(0)->getType(); |
| 3466 | Steps.insert(Steps.begin(), S); |
| 3467 | |
| 3468 | S.Kind = SK_RewrapInitList; |
| 3469 | S.Type = T; |
| 3470 | S.WrappingSyntacticList = Syntactic; |
| 3471 | Steps.push_back(S); |
| 3472 | } |
| 3473 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3474 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3475 | OverloadingResult Result) { |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 3476 | setSequenceKind(FailedSequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3477 | this->Failure = Failure; |
| 3478 | this->FailedOverloadResult = Result; |
| 3479 | } |
| 3480 | |
| 3481 | //===----------------------------------------------------------------------===// |
| 3482 | // Attempt initialization |
| 3483 | //===----------------------------------------------------------------------===// |
| 3484 | |
Nico Weber | 337d5aa | 2015-04-17 08:32:38 +0000 | [diff] [blame] | 3485 | /// Tries to add a zero initializer. Returns true if that worked. |
| 3486 | static bool |
| 3487 | maybeRecoverWithZeroInitialization(Sema &S, InitializationSequence &Sequence, |
| 3488 | const InitializedEntity &Entity) { |
| 3489 | if (Entity.getKind() != InitializedEntity::EK_Variable) |
| 3490 | return false; |
| 3491 | |
| 3492 | VarDecl *VD = cast<VarDecl>(Entity.getDecl()); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 3493 | if (VD->getInit() || VD->getEndLoc().isMacroID()) |
Nico Weber | 337d5aa | 2015-04-17 08:32:38 +0000 | [diff] [blame] | 3494 | return false; |
| 3495 | |
| 3496 | QualType VariableTy = VD->getType().getCanonicalType(); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 3497 | SourceLocation Loc = S.getLocForEndOfToken(VD->getEndLoc()); |
Nico Weber | 337d5aa | 2015-04-17 08:32:38 +0000 | [diff] [blame] | 3498 | std::string Init = S.getFixItZeroInitializerForType(VariableTy, Loc); |
| 3499 | if (!Init.empty()) { |
| 3500 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3501 | Sequence.SetZeroInitializationFixit(Init, Loc); |
| 3502 | return true; |
| 3503 | } |
| 3504 | return false; |
| 3505 | } |
| 3506 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3507 | static void MaybeProduceObjCObject(Sema &S, |
| 3508 | InitializationSequence &Sequence, |
| 3509 | const InitializedEntity &Entity) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3510 | if (!S.getLangOpts().ObjCAutoRefCount) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3511 | |
| 3512 | /// When initializing a parameter, produce the value if it's marked |
| 3513 | /// __attribute__((ns_consumed)). |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3514 | if (Entity.isParameterKind()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3515 | if (!Entity.isParameterConsumed()) |
| 3516 | return; |
| 3517 | |
| 3518 | assert(Entity.getType()->isObjCRetainableType() && |
| 3519 | "consuming an object of unretainable type?"); |
| 3520 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 3521 | |
| 3522 | /// When initializing a return value, if the return type is a |
| 3523 | /// retainable type, then returns need to immediately retain the |
| 3524 | /// object. If an autorelease is required, it will be done at the |
| 3525 | /// last instant. |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 3526 | } else if (Entity.getKind() == InitializedEntity::EK_Result || |
| 3527 | Entity.getKind() == InitializedEntity::EK_StmtExprResult) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3528 | if (!Entity.getType()->isObjCRetainableType()) |
| 3529 | return; |
| 3530 | |
| 3531 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 3532 | } |
| 3533 | } |
| 3534 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3535 | static void TryListInitialization(Sema &S, |
| 3536 | const InitializedEntity &Entity, |
| 3537 | const InitializationKind &Kind, |
| 3538 | InitListExpr *InitList, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 3539 | InitializationSequence &Sequence, |
| 3540 | bool TreatUnavailableAsInvalid); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3541 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3542 | /// When initializing from init list via constructor, handle |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3543 | /// initialization of an object of type std::initializer_list<T>. |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3544 | /// |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3545 | /// \return true if we have handled initialization of an object of type |
| 3546 | /// std::initializer_list<T>, false otherwise. |
| 3547 | static bool TryInitializerListConstruction(Sema &S, |
| 3548 | InitListExpr *List, |
| 3549 | QualType DestType, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 3550 | InitializationSequence &Sequence, |
| 3551 | bool TreatUnavailableAsInvalid) { |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3552 | QualType E; |
| 3553 | if (!S.isStdInitializerList(DestType, &E)) |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3554 | return false; |
| 3555 | |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3556 | if (!S.isCompleteType(List->getExprLoc(), E)) { |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3557 | Sequence.setIncompleteTypeFailure(E); |
| 3558 | return true; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3559 | } |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3560 | |
| 3561 | // Try initializing a temporary array from the init list. |
| 3562 | QualType ArrayType = S.Context.getConstantArrayType( |
| 3563 | E.withConst(), llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 3564 | List->getNumInits()), |
| 3565 | clang::ArrayType::Normal, 0); |
| 3566 | InitializedEntity HiddenArray = |
| 3567 | InitializedEntity::InitializeTemporary(ArrayType); |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 3568 | InitializationKind Kind = InitializationKind::CreateDirectList( |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 3569 | List->getExprLoc(), List->getBeginLoc(), List->getEndLoc()); |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 3570 | TryListInitialization(S, HiddenArray, Kind, List, Sequence, |
| 3571 | TreatUnavailableAsInvalid); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3572 | if (Sequence) |
| 3573 | Sequence.AddStdInitializerListConstructionStep(DestType); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3574 | return true; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3575 | } |
| 3576 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3577 | /// Determine if the constructor has the signature of a copy or move |
| 3578 | /// constructor for the type T of the class in which it was found. That is, |
| 3579 | /// determine if its first parameter is of type T or reference to (possibly |
| 3580 | /// cv-qualified) T. |
| 3581 | static bool hasCopyOrMoveCtorParam(ASTContext &Ctx, |
| 3582 | const ConstructorInfo &Info) { |
| 3583 | if (Info.Constructor->getNumParams() == 0) |
| 3584 | return false; |
| 3585 | |
| 3586 | QualType ParmT = |
| 3587 | Info.Constructor->getParamDecl(0)->getType().getNonReferenceType(); |
| 3588 | QualType ClassT = |
| 3589 | Ctx.getRecordType(cast<CXXRecordDecl>(Info.FoundDecl->getDeclContext())); |
| 3590 | |
| 3591 | return Ctx.hasSameUnqualifiedType(ParmT, ClassT); |
| 3592 | } |
| 3593 | |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3594 | static OverloadingResult |
| 3595 | ResolveConstructorOverload(Sema &S, SourceLocation DeclLoc, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3596 | MultiExprArg Args, |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3597 | OverloadCandidateSet &CandidateSet, |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3598 | QualType DestType, |
Richard Smith | 40c7806 | 2015-02-21 02:31:57 +0000 | [diff] [blame] | 3599 | DeclContext::lookup_result Ctors, |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3600 | OverloadCandidateSet::iterator &Best, |
| 3601 | bool CopyInitializing, bool AllowExplicit, |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3602 | bool OnlyListConstructors, bool IsListInit, |
| 3603 | bool SecondStepOfCopyInit = false) { |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3604 | CandidateSet.clear(OverloadCandidateSet::CSK_InitByConstructor); |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3605 | |
Richard Smith | 40c7806 | 2015-02-21 02:31:57 +0000 | [diff] [blame] | 3606 | for (NamedDecl *D : Ctors) { |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3607 | auto Info = getConstructorInfo(D); |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3608 | if (!Info.Constructor || Info.Constructor->isInvalidDecl()) |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3609 | continue; |
| 3610 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3611 | if (!AllowExplicit && Info.Constructor->isExplicit()) |
| 3612 | continue; |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3613 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3614 | if (OnlyListConstructors && !S.isInitListConstructor(Info.Constructor)) |
| 3615 | continue; |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3616 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3617 | // C++11 [over.best.ics]p4: |
| 3618 | // ... and the constructor or user-defined conversion function is a |
| 3619 | // candidate by |
| 3620 | // - 13.3.1.3, when the argument is the temporary in the second step |
| 3621 | // of a class copy-initialization, or |
| 3622 | // - 13.3.1.4, 13.3.1.5, or 13.3.1.6 (in all cases), [not handled here] |
| 3623 | // - the second phase of 13.3.1.7 when the initializer list has exactly |
| 3624 | // one element that is itself an initializer list, and the target is |
| 3625 | // the first parameter of a constructor of class X, and the conversion |
| 3626 | // is to X or reference to (possibly cv-qualified X), |
| 3627 | // user-defined conversion sequences are not considered. |
| 3628 | bool SuppressUserConversions = |
| 3629 | SecondStepOfCopyInit || |
| 3630 | (IsListInit && Args.size() == 1 && isa<InitListExpr>(Args[0]) && |
| 3631 | hasCopyOrMoveCtorParam(S.Context, Info)); |
| 3632 | |
| 3633 | if (Info.ConstructorTmpl) |
| 3634 | S.AddTemplateOverloadCandidate(Info.ConstructorTmpl, Info.FoundDecl, |
| 3635 | /*ExplicitArgs*/ nullptr, Args, |
| 3636 | CandidateSet, SuppressUserConversions); |
| 3637 | else { |
| 3638 | // C++ [over.match.copy]p1: |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3639 | // - When initializing a temporary to be bound to the first parameter |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3640 | // of a constructor [for type T] that takes a reference to possibly |
| 3641 | // cv-qualified T as its first argument, called with a single |
| 3642 | // argument in the context of direct-initialization, explicit |
| 3643 | // conversion functions are also considered. |
| 3644 | // FIXME: What if a constructor template instantiates to such a signature? |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3645 | bool AllowExplicitConv = AllowExplicit && !CopyInitializing && |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3646 | Args.size() == 1 && |
| 3647 | hasCopyOrMoveCtorParam(S.Context, Info); |
| 3648 | S.AddOverloadCandidate(Info.Constructor, Info.FoundDecl, Args, |
| 3649 | CandidateSet, SuppressUserConversions, |
| 3650 | /*PartialOverloading=*/false, |
| 3651 | /*AllowExplicit=*/AllowExplicitConv); |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3652 | } |
| 3653 | } |
| 3654 | |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3655 | // FIXME: Work around a bug in C++17 guaranteed copy elision. |
| 3656 | // |
| 3657 | // When initializing an object of class type T by constructor |
| 3658 | // ([over.match.ctor]) or by list-initialization ([over.match.list]) |
| 3659 | // from a single expression of class type U, conversion functions of |
| 3660 | // U that convert to the non-reference type cv T are candidates. |
| 3661 | // Explicit conversion functions are only candidates during |
| 3662 | // direct-initialization. |
| 3663 | // |
| 3664 | // Note: SecondStepOfCopyInit is only ever true in this case when |
| 3665 | // evaluating whether to produce a C++98 compatibility warning. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 3666 | if (S.getLangOpts().CPlusPlus17 && Args.size() == 1 && |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3667 | !SecondStepOfCopyInit) { |
| 3668 | Expr *Initializer = Args[0]; |
| 3669 | auto *SourceRD = Initializer->getType()->getAsCXXRecordDecl(); |
| 3670 | if (SourceRD && S.isCompleteType(DeclLoc, Initializer->getType())) { |
| 3671 | const auto &Conversions = SourceRD->getVisibleConversionFunctions(); |
| 3672 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
| 3673 | NamedDecl *D = *I; |
| 3674 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3675 | D = D->getUnderlyingDecl(); |
| 3676 | |
| 3677 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3678 | CXXConversionDecl *Conv; |
| 3679 | if (ConvTemplate) |
| 3680 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3681 | else |
| 3682 | Conv = cast<CXXConversionDecl>(D); |
| 3683 | |
| 3684 | if ((AllowExplicit && !CopyInitializing) || !Conv->isExplicit()) { |
| 3685 | if (ConvTemplate) |
| 3686 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
| 3687 | ActingDC, Initializer, DestType, |
| 3688 | CandidateSet, AllowExplicit, |
| 3689 | /*AllowResultConversion*/false); |
| 3690 | else |
| 3691 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Initializer, |
| 3692 | DestType, CandidateSet, AllowExplicit, |
| 3693 | /*AllowResultConversion*/false); |
| 3694 | } |
| 3695 | } |
| 3696 | } |
| 3697 | } |
| 3698 | |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3699 | // Perform overload resolution and return the result. |
| 3700 | return CandidateSet.BestViableFunction(S, DeclLoc, Best); |
| 3701 | } |
| 3702 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3703 | /// Attempt initialization by constructor (C++ [dcl.init]), which |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3704 | /// enumerates the constructors of the initialized entity and performs overload |
| 3705 | /// resolution to select the best. |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3706 | /// \param DestType The destination class type. |
| 3707 | /// \param DestArrayType The destination type, which is either DestType or |
| 3708 | /// a (possibly multidimensional) array of DestType. |
NAKAMURA Takumi | ffcc98a | 2015-02-05 23:12:13 +0000 | [diff] [blame] | 3709 | /// \param IsListInit Is this list-initialization? |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3710 | /// \param IsInitListCopy Is this non-list-initialization resulting from a |
| 3711 | /// list-initialization from {x} where x is the same |
| 3712 | /// type as the entity? |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3713 | static void TryConstructorInitialization(Sema &S, |
| 3714 | const InitializedEntity &Entity, |
| 3715 | const InitializationKind &Kind, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3716 | MultiExprArg Args, QualType DestType, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3717 | QualType DestArrayType, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3718 | InitializationSequence &Sequence, |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3719 | bool IsListInit = false, |
| 3720 | bool IsInitListCopy = false) { |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3721 | assert(((!IsListInit && !IsInitListCopy) || |
| 3722 | (Args.size() == 1 && isa<InitListExpr>(Args[0]))) && |
| 3723 | "IsListInit/IsInitListCopy must come with a single initializer list " |
| 3724 | "argument."); |
| 3725 | InitListExpr *ILE = |
| 3726 | (IsListInit || IsInitListCopy) ? cast<InitListExpr>(Args[0]) : nullptr; |
| 3727 | MultiExprArg UnwrappedArgs = |
| 3728 | ILE ? MultiExprArg(ILE->getInits(), ILE->getNumInits()) : Args; |
Sebastian Redl | 88e4d49 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3729 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3730 | // The type we're constructing needs to be complete. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3731 | if (!S.isCompleteType(Kind.getLocation(), DestType)) { |
Douglas Gregor | 85f3423 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 3732 | Sequence.setIncompleteTypeFailure(DestType); |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3733 | return; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3734 | } |
| 3735 | |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 3736 | // C++17 [dcl.init]p17: |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3737 | // - If the initializer expression is a prvalue and the cv-unqualified |
| 3738 | // version of the source type is the same class as the class of the |
| 3739 | // destination, the initializer expression is used to initialize the |
| 3740 | // destination object. |
| 3741 | // Per DR (no number yet), this does not apply when initializing a base |
| 3742 | // class or delegating to another constructor from a mem-initializer. |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3743 | // ObjC++: Lambda captured by the block in the lambda to block conversion |
| 3744 | // should avoid copy elision. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 3745 | if (S.getLangOpts().CPlusPlus17 && |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3746 | Entity.getKind() != InitializedEntity::EK_Base && |
| 3747 | Entity.getKind() != InitializedEntity::EK_Delegating && |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3748 | Entity.getKind() != |
| 3749 | InitializedEntity::EK_LambdaToBlockConversionBlockElement && |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3750 | UnwrappedArgs.size() == 1 && UnwrappedArgs[0]->isRValue() && |
| 3751 | S.Context.hasSameUnqualifiedType(UnwrappedArgs[0]->getType(), DestType)) { |
| 3752 | // Convert qualifications if necessary. |
Richard Smith | 16d3150 | 2016-12-21 01:31:56 +0000 | [diff] [blame] | 3753 | Sequence.AddQualificationConversionStep(DestType, VK_RValue); |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3754 | if (ILE) |
| 3755 | Sequence.RewrapReferenceInitList(DestType, ILE); |
| 3756 | return; |
| 3757 | } |
| 3758 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3759 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
| 3760 | assert(DestRecordType && "Constructor initialization requires record type"); |
| 3761 | CXXRecordDecl *DestRecordDecl |
| 3762 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
| 3763 | |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3764 | // Build the candidate set directly in the initialization sequence |
| 3765 | // structure, so that it will persist if we fail. |
| 3766 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3767 | |
| 3768 | // Determine whether we are allowed to call explicit constructors or |
| 3769 | // explicit conversion operators. |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3770 | bool AllowExplicit = Kind.AllowExplicit() || IsListInit; |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3771 | bool CopyInitialization = Kind.getKind() == InitializationKind::IK_Copy; |
Sebastian Redl | 88e4d49 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3772 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3773 | // - Otherwise, if T is a class type, constructors are considered. The |
| 3774 | // applicable constructors are enumerated, and the best one is chosen |
| 3775 | // through overload resolution. |
Richard Smith | 40c7806 | 2015-02-21 02:31:57 +0000 | [diff] [blame] | 3776 | DeclContext::lookup_result Ctors = S.LookupConstructors(DestRecordDecl); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3777 | |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3778 | OverloadingResult Result = OR_No_Viable_Function; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3779 | OverloadCandidateSet::iterator Best; |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3780 | bool AsInitializerList = false; |
| 3781 | |
Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 3782 | // C++11 [over.match.list]p1, per DR1467: |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 3783 | // When objects of non-aggregate type T are list-initialized, such that |
| 3784 | // 8.5.4 [dcl.init.list] specifies that overload resolution is performed |
| 3785 | // according to the rules in this section, overload resolution selects |
| 3786 | // the constructor in two phases: |
| 3787 | // |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3788 | // - Initially, the candidate functions are the initializer-list |
| 3789 | // constructors of the class T and the argument list consists of the |
| 3790 | // initializer list as a single argument. |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3791 | if (IsListInit) { |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3792 | AsInitializerList = true; |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3793 | |
| 3794 | // If the initializer list has no elements and T has a default constructor, |
| 3795 | // the first phase is omitted. |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3796 | if (!(UnwrappedArgs.empty() && DestRecordDecl->hasDefaultConstructor())) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3797 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3798 | CandidateSet, DestType, Ctors, Best, |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3799 | CopyInitialization, AllowExplicit, |
Larisse Voufo | bcf327a | 2015-02-10 02:20:14 +0000 | [diff] [blame] | 3800 | /*OnlyListConstructor=*/true, |
| 3801 | IsListInit); |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3802 | } |
| 3803 | |
| 3804 | // C++11 [over.match.list]p1: |
| 3805 | // - If no viable initializer-list constructor is found, overload resolution |
| 3806 | // is performed again, where the candidate functions are all the |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3807 | // constructors of the class T and the argument list consists of the |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3808 | // elements of the initializer list. |
| 3809 | if (Result == OR_No_Viable_Function) { |
| 3810 | AsInitializerList = false; |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3811 | Result = ResolveConstructorOverload(S, Kind.getLocation(), UnwrappedArgs, |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3812 | CandidateSet, DestType, Ctors, Best, |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3813 | CopyInitialization, AllowExplicit, |
Larisse Voufo | bcf327a | 2015-02-10 02:20:14 +0000 | [diff] [blame] | 3814 | /*OnlyListConstructors=*/false, |
| 3815 | IsListInit); |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3816 | } |
| 3817 | if (Result) { |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3818 | Sequence.SetOverloadFailure(IsListInit ? |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3819 | InitializationSequence::FK_ListConstructorOverloadFailed : |
| 3820 | InitializationSequence::FK_ConstructorOverloadFailed, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3821 | Result); |
| 3822 | return; |
| 3823 | } |
| 3824 | |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3825 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 3826 | |
| 3827 | // In C++17, ResolveConstructorOverload can select a conversion function |
| 3828 | // instead of a constructor. |
| 3829 | if (auto *CD = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 3830 | // Add the user-defined conversion step that calls the conversion function. |
| 3831 | QualType ConvType = CD->getConversionType(); |
| 3832 | assert(S.Context.hasSameUnqualifiedType(ConvType, DestType) && |
| 3833 | "should not have selected this conversion function"); |
| 3834 | Sequence.AddUserConversionStep(CD, Best->FoundDecl, ConvType, |
| 3835 | HadMultipleCandidates); |
| 3836 | if (!S.Context.hasSameType(ConvType, DestType)) |
| 3837 | Sequence.AddQualificationConversionStep(DestType, VK_RValue); |
| 3838 | if (IsListInit) |
| 3839 | Sequence.RewrapReferenceInitList(Entity.getType(), ILE); |
| 3840 | return; |
| 3841 | } |
| 3842 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3843 | // C++11 [dcl.init]p6: |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3844 | // If a program calls for the default initialization of an object |
| 3845 | // of a const-qualified type T, T shall be a class type with a |
| 3846 | // user-provided default constructor. |
Nico Weber | 6a6376b | 2016-02-19 01:52:46 +0000 | [diff] [blame] | 3847 | // C++ core issue 253 proposal: |
| 3848 | // If the implicit default constructor initializes all subobjects, no |
| 3849 | // initializer should be required. |
| 3850 | // The 253 proposal is for example needed to process libstdc++ headers in 5.x. |
| 3851 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3852 | if (Kind.getKind() == InitializationKind::IK_Default && |
Nico Weber | 6a6376b | 2016-02-19 01:52:46 +0000 | [diff] [blame] | 3853 | Entity.getType().isConstQualified()) { |
| 3854 | if (!CtorDecl->getParent()->allowConstDefaultInit()) { |
| 3855 | if (!maybeRecoverWithZeroInitialization(S, Sequence, Entity)) |
| 3856 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 3857 | return; |
| 3858 | } |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3859 | } |
| 3860 | |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3861 | // C++11 [over.match.list]p1: |
| 3862 | // In copy-list-initialization, if an explicit constructor is chosen, the |
| 3863 | // initializer is ill-formed. |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3864 | if (IsListInit && !Kind.AllowExplicit() && CtorDecl->isExplicit()) { |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3865 | Sequence.SetFailed(InitializationSequence::FK_ExplicitConstructor); |
| 3866 | return; |
| 3867 | } |
| 3868 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3869 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 3870 | // subsumed by the initialization. |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3871 | Sequence.AddConstructorInitializationStep( |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3872 | Best->FoundDecl, CtorDecl, DestArrayType, HadMultipleCandidates, |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3873 | IsListInit | IsInitListCopy, AsInitializerList); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3874 | } |
| 3875 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3876 | static bool |
| 3877 | ResolveOverloadedFunctionForReferenceBinding(Sema &S, |
| 3878 | Expr *Initializer, |
| 3879 | QualType &SourceType, |
| 3880 | QualType &UnqualifiedSourceType, |
| 3881 | QualType UnqualifiedTargetType, |
| 3882 | InitializationSequence &Sequence) { |
| 3883 | if (S.Context.getCanonicalType(UnqualifiedSourceType) == |
| 3884 | S.Context.OverloadTy) { |
| 3885 | DeclAccessPair Found; |
| 3886 | bool HadMultipleCandidates = false; |
| 3887 | if (FunctionDecl *Fn |
| 3888 | = S.ResolveAddressOfOverloadedFunction(Initializer, |
| 3889 | UnqualifiedTargetType, |
| 3890 | false, Found, |
| 3891 | &HadMultipleCandidates)) { |
| 3892 | Sequence.AddAddressOverloadResolutionStep(Fn, Found, |
| 3893 | HadMultipleCandidates); |
| 3894 | SourceType = Fn->getType(); |
| 3895 | UnqualifiedSourceType = SourceType.getUnqualifiedType(); |
| 3896 | } else if (!UnqualifiedTargetType->isRecordType()) { |
| 3897 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3898 | return true; |
| 3899 | } |
| 3900 | } |
| 3901 | return false; |
| 3902 | } |
| 3903 | |
| 3904 | static void TryReferenceInitializationCore(Sema &S, |
| 3905 | const InitializedEntity &Entity, |
| 3906 | const InitializationKind &Kind, |
| 3907 | Expr *Initializer, |
| 3908 | QualType cv1T1, QualType T1, |
| 3909 | Qualifiers T1Quals, |
| 3910 | QualType cv2T2, QualType T2, |
| 3911 | Qualifiers T2Quals, |
| 3912 | InitializationSequence &Sequence); |
| 3913 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3914 | static void TryValueInitialization(Sema &S, |
| 3915 | const InitializedEntity &Entity, |
| 3916 | const InitializationKind &Kind, |
| 3917 | InitializationSequence &Sequence, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3918 | InitListExpr *InitList = nullptr); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3919 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3920 | /// Attempt list initialization of a reference. |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3921 | static void TryReferenceListInitialization(Sema &S, |
| 3922 | const InitializedEntity &Entity, |
| 3923 | const InitializationKind &Kind, |
| 3924 | InitListExpr *InitList, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 3925 | InitializationSequence &Sequence, |
| 3926 | bool TreatUnavailableAsInvalid) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3927 | // First, catch C++03 where this isn't possible. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3928 | if (!S.getLangOpts().CPlusPlus11) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3929 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 3930 | return; |
| 3931 | } |
David Majnemer | 9370dc2 | 2015-04-26 07:35:03 +0000 | [diff] [blame] | 3932 | // Can't reference initialize a compound literal. |
| 3933 | if (Entity.getKind() == InitializedEntity::EK_CompoundLiteralInit) { |
| 3934 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 3935 | return; |
| 3936 | } |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3937 | |
| 3938 | QualType DestType = Entity.getType(); |
| 3939 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 3940 | Qualifiers T1Quals; |
| 3941 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
| 3942 | |
| 3943 | // Reference initialization via an initializer list works thus: |
| 3944 | // If the initializer list consists of a single element that is |
| 3945 | // reference-related to the referenced type, bind directly to that element |
| 3946 | // (possibly creating temporaries). |
| 3947 | // Otherwise, initialize a temporary with the initializer list and |
| 3948 | // bind to that. |
| 3949 | if (InitList->getNumInits() == 1) { |
| 3950 | Expr *Initializer = InitList->getInit(0); |
| 3951 | QualType cv2T2 = Initializer->getType(); |
| 3952 | Qualifiers T2Quals; |
| 3953 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
| 3954 | |
| 3955 | // If this fails, creating a temporary wouldn't work either. |
| 3956 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 3957 | T1, Sequence)) |
| 3958 | return; |
| 3959 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3960 | SourceLocation DeclLoc = Initializer->getBeginLoc(); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3961 | bool dummy1, dummy2, dummy3; |
| 3962 | Sema::ReferenceCompareResult RefRelationship |
| 3963 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, dummy1, |
| 3964 | dummy2, dummy3); |
| 3965 | if (RefRelationship >= Sema::Ref_Related) { |
| 3966 | // Try to bind the reference here. |
| 3967 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 3968 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 3969 | if (Sequence) |
| 3970 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
| 3971 | return; |
| 3972 | } |
Richard Smith | 03d9393 | 2013-01-15 07:58:29 +0000 | [diff] [blame] | 3973 | |
| 3974 | // Update the initializer if we've resolved an overloaded function. |
| 3975 | if (Sequence.step_begin() != Sequence.step_end()) |
| 3976 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3977 | } |
| 3978 | |
| 3979 | // Not reference-related. Create a temporary and bind to that. |
| 3980 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3981 | |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 3982 | TryListInitialization(S, TempEntity, Kind, InitList, Sequence, |
| 3983 | TreatUnavailableAsInvalid); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3984 | if (Sequence) { |
| 3985 | if (DestType->isRValueReferenceType() || |
| 3986 | (T1Quals.hasConst() && !T1Quals.hasVolatile())) |
| 3987 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3988 | else |
| 3989 | Sequence.SetFailed( |
| 3990 | InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
| 3991 | } |
| 3992 | } |
| 3993 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3994 | /// Attempt list initialization (C++0x [dcl.init.list]) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3995 | static void TryListInitialization(Sema &S, |
| 3996 | const InitializedEntity &Entity, |
| 3997 | const InitializationKind &Kind, |
| 3998 | InitListExpr *InitList, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 3999 | InitializationSequence &Sequence, |
| 4000 | bool TreatUnavailableAsInvalid) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4001 | QualType DestType = Entity.getType(); |
| 4002 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4003 | // C++ doesn't allow scalar initialization with more than one argument. |
| 4004 | // But C99 complex numbers are scalars and it makes sense there. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4005 | if (S.getLangOpts().CPlusPlus && DestType->isScalarType() && |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4006 | !DestType->isAnyComplexType() && InitList->getNumInits() > 1) { |
| 4007 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 4008 | return; |
| 4009 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4010 | if (DestType->isReferenceType()) { |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4011 | TryReferenceListInitialization(S, Entity, Kind, InitList, Sequence, |
| 4012 | TreatUnavailableAsInvalid); |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4013 | return; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4014 | } |
Sebastian Redl | 4f28b58 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 4015 | |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4016 | if (DestType->isRecordType() && |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4017 | !S.isCompleteType(InitList->getBeginLoc(), DestType)) { |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4018 | Sequence.setIncompleteTypeFailure(DestType); |
| 4019 | return; |
| 4020 | } |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4021 | |
Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 4022 | // C++11 [dcl.init.list]p3, per DR1467: |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4023 | // - If T is a class type and the initializer list has a single element of |
| 4024 | // type cv U, where U is T or a class derived from T, the object is |
| 4025 | // initialized from that element (by copy-initialization for |
| 4026 | // copy-list-initialization, or by direct-initialization for |
| 4027 | // direct-list-initialization). |
| 4028 | // - Otherwise, if T is a character array and the initializer list has a |
| 4029 | // single element that is an appropriately-typed string literal |
| 4030 | // (8.5.2 [dcl.init.string]), initialization is performed as described |
| 4031 | // in that section. |
Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 4032 | // - Otherwise, if T is an aggregate, [...] (continue below). |
| 4033 | if (S.getLangOpts().CPlusPlus11 && InitList->getNumInits() == 1) { |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4034 | if (DestType->isRecordType()) { |
| 4035 | QualType InitType = InitList->getInit(0)->getType(); |
| 4036 | if (S.Context.hasSameUnqualifiedType(InitType, DestType) || |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4037 | S.IsDerivedFrom(InitList->getBeginLoc(), InitType, DestType)) { |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 4038 | Expr *InitListAsExpr = InitList; |
| 4039 | TryConstructorInitialization(S, Entity, Kind, InitListAsExpr, DestType, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 4040 | DestType, Sequence, |
| 4041 | /*InitListSyntax*/false, |
| 4042 | /*IsInitListCopy*/true); |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4043 | return; |
| 4044 | } |
| 4045 | } |
| 4046 | if (const ArrayType *DestAT = S.Context.getAsArrayType(DestType)) { |
| 4047 | Expr *SubInit[1] = {InitList->getInit(0)}; |
| 4048 | if (!isa<VariableArrayType>(DestAT) && |
| 4049 | IsStringInit(SubInit[0], DestAT, S.Context) == SIF_None) { |
| 4050 | InitializationKind SubKind = |
| 4051 | Kind.getKind() == InitializationKind::IK_DirectList |
| 4052 | ? InitializationKind::CreateDirect(Kind.getLocation(), |
| 4053 | InitList->getLBraceLoc(), |
| 4054 | InitList->getRBraceLoc()) |
| 4055 | : Kind; |
| 4056 | Sequence.InitializeFrom(S, Entity, SubKind, SubInit, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4057 | /*TopLevelOfInitList*/ true, |
| 4058 | TreatUnavailableAsInvalid); |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4059 | |
| 4060 | // TryStringLiteralInitialization() (in InitializeFrom()) will fail if |
| 4061 | // the element is not an appropriately-typed string literal, in which |
| 4062 | // case we should proceed as in C++11 (below). |
| 4063 | if (Sequence) { |
| 4064 | Sequence.RewrapReferenceInitList(Entity.getType(), InitList); |
| 4065 | return; |
| 4066 | } |
| 4067 | } |
Sebastian Redl | 4f28b58 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 4068 | } |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4069 | } |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4070 | |
| 4071 | // C++11 [dcl.init.list]p3: |
| 4072 | // - If T is an aggregate, aggregate initialization is performed. |
Faisal Vali | 30622bb | 2015-12-07 02:37:44 +0000 | [diff] [blame] | 4073 | if ((DestType->isRecordType() && !DestType->isAggregateType()) || |
| 4074 | (S.getLangOpts().CPlusPlus11 && |
| 4075 | S.isStdInitializerList(DestType, nullptr))) { |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4076 | if (S.getLangOpts().CPlusPlus11) { |
| 4077 | // - Otherwise, if the initializer list has no elements and T is a |
| 4078 | // class type with a default constructor, the object is |
| 4079 | // value-initialized. |
| 4080 | if (InitList->getNumInits() == 0) { |
| 4081 | CXXRecordDecl *RD = DestType->getAsCXXRecordDecl(); |
| 4082 | if (RD->hasDefaultConstructor()) { |
| 4083 | TryValueInitialization(S, Entity, Kind, Sequence, InitList); |
| 4084 | return; |
| 4085 | } |
| 4086 | } |
| 4087 | |
| 4088 | // - Otherwise, if T is a specialization of std::initializer_list<E>, |
| 4089 | // an initializer_list object constructed [...] |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4090 | if (TryInitializerListConstruction(S, InitList, DestType, Sequence, |
| 4091 | TreatUnavailableAsInvalid)) |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4092 | return; |
| 4093 | |
| 4094 | // - Otherwise, if T is a class type, constructors are considered. |
| 4095 | Expr *InitListAsExpr = InitList; |
| 4096 | TryConstructorInitialization(S, Entity, Kind, InitListAsExpr, DestType, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 4097 | DestType, Sequence, /*InitListSyntax*/true); |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4098 | } else |
| 4099 | Sequence.SetFailed(InitializationSequence::FK_InitListBadDestinationType); |
| 4100 | return; |
| 4101 | } |
| 4102 | |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 4103 | if (S.getLangOpts().CPlusPlus && !DestType->isAggregateType() && |
Richard Smith | ed63886 | 2016-03-28 06:08:37 +0000 | [diff] [blame] | 4104 | InitList->getNumInits() == 1) { |
| 4105 | Expr *E = InitList->getInit(0); |
| 4106 | |
| 4107 | // - Otherwise, if T is an enumeration with a fixed underlying type, |
| 4108 | // the initializer-list has a single element v, and the initialization |
| 4109 | // is direct-list-initialization, the object is initialized with the |
| 4110 | // value T(v); if a narrowing conversion is required to convert v to |
| 4111 | // the underlying type of T, the program is ill-formed. |
| 4112 | auto *ET = DestType->getAs<EnumType>(); |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 4113 | if (S.getLangOpts().CPlusPlus17 && |
Richard Smith | ed63886 | 2016-03-28 06:08:37 +0000 | [diff] [blame] | 4114 | Kind.getKind() == InitializationKind::IK_DirectList && |
| 4115 | ET && ET->getDecl()->isFixed() && |
| 4116 | !S.Context.hasSameUnqualifiedType(E->getType(), DestType) && |
| 4117 | (E->getType()->isIntegralOrEnumerationType() || |
| 4118 | E->getType()->isFloatingType())) { |
| 4119 | // There are two ways that T(v) can work when T is an enumeration type. |
| 4120 | // If there is either an implicit conversion sequence from v to T or |
| 4121 | // a conversion function that can convert from v to T, then we use that. |
| 4122 | // Otherwise, if v is of integral, enumeration, or floating-point type, |
| 4123 | // it is converted to the enumeration type via its underlying type. |
| 4124 | // There is no overlap possible between these two cases (except when the |
| 4125 | // source value is already of the destination type), and the first |
| 4126 | // case is handled by the general case for single-element lists below. |
| 4127 | ImplicitConversionSequence ICS; |
| 4128 | ICS.setStandard(); |
| 4129 | ICS.Standard.setAsIdentityConversion(); |
Vedant Kumar | f4217f8 | 2017-02-16 01:20:00 +0000 | [diff] [blame] | 4130 | if (!E->isRValue()) |
| 4131 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
Richard Smith | ed63886 | 2016-03-28 06:08:37 +0000 | [diff] [blame] | 4132 | // If E is of a floating-point type, then the conversion is ill-formed |
| 4133 | // due to narrowing, but go through the motions in order to produce the |
| 4134 | // right diagnostic. |
| 4135 | ICS.Standard.Second = E->getType()->isFloatingType() |
| 4136 | ? ICK_Floating_Integral |
| 4137 | : ICK_Integral_Conversion; |
| 4138 | ICS.Standard.setFromType(E->getType()); |
| 4139 | ICS.Standard.setToType(0, E->getType()); |
| 4140 | ICS.Standard.setToType(1, DestType); |
| 4141 | ICS.Standard.setToType(2, DestType); |
| 4142 | Sequence.AddConversionSequenceStep(ICS, ICS.Standard.getToType(2), |
| 4143 | /*TopLevelOfInitList*/true); |
| 4144 | Sequence.RewrapReferenceInitList(Entity.getType(), InitList); |
| 4145 | return; |
| 4146 | } |
| 4147 | |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 4148 | // - Otherwise, if the initializer list has a single element of type E |
| 4149 | // [...references are handled above...], the object or reference is |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4150 | // initialized from that element (by copy-initialization for |
| 4151 | // copy-list-initialization, or by direct-initialization for |
| 4152 | // direct-list-initialization); if a narrowing conversion is required |
| 4153 | // to convert the element to T, the program is ill-formed. |
| 4154 | // |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 4155 | // Per core-24034, this is direct-initialization if we were performing |
| 4156 | // direct-list-initialization and copy-initialization otherwise. |
| 4157 | // We can't use InitListChecker for this, because it always performs |
| 4158 | // copy-initialization. This only matters if we might use an 'explicit' |
| 4159 | // conversion operator, so we only need to handle the cases where the source |
| 4160 | // is of record type. |
Richard Smith | ed63886 | 2016-03-28 06:08:37 +0000 | [diff] [blame] | 4161 | if (InitList->getInit(0)->getType()->isRecordType()) { |
| 4162 | InitializationKind SubKind = |
| 4163 | Kind.getKind() == InitializationKind::IK_DirectList |
| 4164 | ? InitializationKind::CreateDirect(Kind.getLocation(), |
| 4165 | InitList->getLBraceLoc(), |
| 4166 | InitList->getRBraceLoc()) |
| 4167 | : Kind; |
| 4168 | Expr *SubInit[1] = { InitList->getInit(0) }; |
| 4169 | Sequence.InitializeFrom(S, Entity, SubKind, SubInit, |
| 4170 | /*TopLevelOfInitList*/true, |
| 4171 | TreatUnavailableAsInvalid); |
| 4172 | if (Sequence) |
| 4173 | Sequence.RewrapReferenceInitList(Entity.getType(), InitList); |
| 4174 | return; |
| 4175 | } |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 4176 | } |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4177 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4178 | InitListChecker CheckInitList(S, Entity, InitList, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4179 | DestType, /*VerifyOnly=*/true, TreatUnavailableAsInvalid); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4180 | if (CheckInitList.HadError()) { |
| 4181 | Sequence.SetFailed(InitializationSequence::FK_ListInitializationFailed); |
| 4182 | return; |
| 4183 | } |
| 4184 | |
| 4185 | // Add the list initialization step with the built init list. |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4186 | Sequence.AddListInitializationStep(DestType); |
| 4187 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4188 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4189 | /// Try a reference initialization that involves calling a conversion |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4190 | /// function. |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4191 | static OverloadingResult TryRefInitWithConversionFunction( |
| 4192 | Sema &S, const InitializedEntity &Entity, const InitializationKind &Kind, |
| 4193 | Expr *Initializer, bool AllowRValues, bool IsLValueRef, |
| 4194 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4195 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4196 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 4197 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 4198 | QualType cv2T2 = Initializer->getType(); |
| 4199 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 4200 | |
| 4201 | bool DerivedToBase; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4202 | bool ObjCConversion; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4203 | bool ObjCLifetimeConversion; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4204 | assert(!S.CompareReferenceRelationship(Initializer->getBeginLoc(), T1, T2, |
| 4205 | DerivedToBase, ObjCConversion, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4206 | ObjCLifetimeConversion) && |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4207 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 8abbc65 | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 4208 | (void)DerivedToBase; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4209 | (void)ObjCConversion; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4210 | (void)ObjCLifetimeConversion; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4211 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4212 | // Build the candidate set directly in the initialization sequence |
| 4213 | // structure, so that it will persist if we fail. |
| 4214 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 4215 | CandidateSet.clear(OverloadCandidateSet::CSK_InitByUserDefinedConversion); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4216 | |
Richard Smith | b368ea8 | 2018-07-02 23:25:22 +0000 | [diff] [blame] | 4217 | // Determine whether we are allowed to call explicit conversion operators. |
| 4218 | // Note that none of [over.match.copy], [over.match.conv], nor |
| 4219 | // [over.match.ref] permit an explicit constructor to be chosen when |
| 4220 | // initializing a reference, not even for direct-initialization. |
| 4221 | bool AllowExplicitCtors = false; |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4222 | bool AllowExplicitConvs = Kind.allowExplicitConversionFunctionsInRefBinding(); |
| 4223 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4224 | const RecordType *T1RecordType = nullptr; |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 4225 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4226 | S.isCompleteType(Kind.getLocation(), T1)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4227 | // The type we're converting to is a class type. Enumerate its constructors |
| 4228 | // to see if there is a suitable conversion. |
| 4229 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 4230 | |
Richard Smith | 40c7806 | 2015-02-21 02:31:57 +0000 | [diff] [blame] | 4231 | for (NamedDecl *D : S.LookupConstructors(T1RecordDecl)) { |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4232 | auto Info = getConstructorInfo(D); |
| 4233 | if (!Info.Constructor) |
| 4234 | continue; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4235 | |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4236 | if (!Info.Constructor->isInvalidDecl() && |
Richard Smith | b368ea8 | 2018-07-02 23:25:22 +0000 | [diff] [blame] | 4237 | Info.Constructor->isConvertingConstructor(AllowExplicitCtors)) { |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4238 | if (Info.ConstructorTmpl) |
| 4239 | S.AddTemplateOverloadCandidate(Info.ConstructorTmpl, Info.FoundDecl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4240 | /*ExplicitArgs*/ nullptr, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4241 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | dfbdfbb | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 4242 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4243 | else |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4244 | S.AddOverloadCandidate(Info.Constructor, Info.FoundDecl, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4245 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | dfbdfbb | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 4246 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4247 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4248 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4249 | } |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 4250 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 4251 | return OR_No_Viable_Function; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4252 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4253 | const RecordType *T2RecordType = nullptr; |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 4254 | if ((T2RecordType = T2->getAs<RecordType>()) && |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4255 | S.isCompleteType(Kind.getLocation(), T2)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4256 | // The type we're converting from is a class type, enumerate its conversion |
| 4257 | // functions. |
| 4258 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 4259 | |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 4260 | const auto &Conversions = T2RecordDecl->getVisibleConversionFunctions(); |
| 4261 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4262 | NamedDecl *D = *I; |
| 4263 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 4264 | if (isa<UsingShadowDecl>(D)) |
| 4265 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4266 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4267 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 4268 | CXXConversionDecl *Conv; |
| 4269 | if (ConvTemplate) |
| 4270 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 4271 | else |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4272 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4273 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4274 | // If the conversion function doesn't return a reference type, |
| 4275 | // it can't be considered for this conversion unless we're allowed to |
| 4276 | // consider rvalues. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4277 | // FIXME: Do we need to make sure that we only consider conversion |
| 4278 | // candidates with reference-compatible results? That might be needed to |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4279 | // break recursion. |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4280 | if ((AllowExplicitConvs || !Conv->isExplicit()) && |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4281 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 4282 | if (ConvTemplate) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4283 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4284 | ActingDC, Initializer, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 4285 | DestType, CandidateSet, |
| 4286 | /*AllowObjCConversionOnExplicit=*/ |
| 4287 | false); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4288 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4289 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 4290 | Initializer, DestType, CandidateSet, |
| 4291 | /*AllowObjCConversionOnExplicit=*/false); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4292 | } |
| 4293 | } |
| 4294 | } |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 4295 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 4296 | return OR_No_Viable_Function; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4297 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4298 | SourceLocation DeclLoc = Initializer->getBeginLoc(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4299 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4300 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4301 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4302 | if (OverloadingResult Result |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 4303 | = CandidateSet.BestViableFunction(S, DeclLoc, Best)) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4304 | return Result; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 4305 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4306 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 4307 | // This is the overload that will be used for this initialization step if we |
| 4308 | // use this initialization. Mark it as referenced. |
| 4309 | Function->setReferenced(); |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4310 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4311 | // Compute the returned type and value kind of the conversion. |
| 4312 | QualType cv3T3; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4313 | if (isa<CXXConversionDecl>(Function)) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4314 | cv3T3 = Function->getReturnType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4315 | else |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4316 | cv3T3 = T1; |
| 4317 | |
| 4318 | ExprValueKind VK = VK_RValue; |
| 4319 | if (cv3T3->isLValueReferenceType()) |
| 4320 | VK = VK_LValue; |
| 4321 | else if (const auto *RRef = cv3T3->getAs<RValueReferenceType>()) |
| 4322 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
| 4323 | cv3T3 = cv3T3.getNonLValueExprType(S.Context); |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 4324 | |
| 4325 | // Add the user-defined conversion step. |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4326 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4327 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, cv3T3, |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4328 | HadMultipleCandidates); |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 4329 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4330 | // Determine whether we'll need to perform derived-to-base adjustments or |
| 4331 | // other conversions. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4332 | bool NewDerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4333 | bool NewObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4334 | bool NewObjCLifetimeConversion = false; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4335 | Sema::ReferenceCompareResult NewRefRelationship |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4336 | = S.CompareReferenceRelationship(DeclLoc, T1, cv3T3, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4337 | NewDerivedToBase, NewObjCConversion, |
| 4338 | NewObjCLifetimeConversion); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4339 | |
| 4340 | // Add the final conversion sequence, if necessary. |
Douglas Gregor | 1ce52ca | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 4341 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4342 | assert(!isa<CXXConstructorDecl>(Function) && |
| 4343 | "should not have conversion after constructor"); |
| 4344 | |
Douglas Gregor | 1ce52ca | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 4345 | ImplicitConversionSequence ICS; |
| 4346 | ICS.setStandard(); |
| 4347 | ICS.Standard = Best->FinalConversion; |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4348 | Sequence.AddConversionSequenceStep(ICS, ICS.Standard.getToType(2)); |
| 4349 | |
| 4350 | // Every implicit conversion results in a prvalue, except for a glvalue |
| 4351 | // derived-to-base conversion, which we handle below. |
| 4352 | cv3T3 = ICS.Standard.getToType(2); |
| 4353 | VK = VK_RValue; |
| 4354 | } |
| 4355 | |
| 4356 | // If the converted initializer is a prvalue, its type T4 is adjusted to |
| 4357 | // type "cv1 T4" and the temporary materialization conversion is applied. |
| 4358 | // |
| 4359 | // We adjust the cv-qualifications to match the reference regardless of |
| 4360 | // whether we have a prvalue so that the AST records the change. In this |
| 4361 | // case, T4 is "cv3 T3". |
| 4362 | QualType cv1T4 = S.Context.getQualifiedType(cv3T3, cv1T1.getQualifiers()); |
| 4363 | if (cv1T4.getQualifiers() != cv3T3.getQualifiers()) |
| 4364 | Sequence.AddQualificationConversionStep(cv1T4, VK); |
| 4365 | Sequence.AddReferenceBindingStep(cv1T4, VK == VK_RValue); |
| 4366 | VK = IsLValueRef ? VK_LValue : VK_XValue; |
| 4367 | |
| 4368 | if (NewDerivedToBase) |
| 4369 | Sequence.AddDerivedToBaseCastStep(cv1T1, VK); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4370 | else if (NewObjCConversion) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4371 | Sequence.AddObjCObjectConversionStep(cv1T1); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4372 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4373 | return OR_Success; |
| 4374 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4375 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4376 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 4377 | const InitializedEntity &Entity, |
| 4378 | Expr *CurInitExpr); |
| 4379 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4380 | /// Attempt reference initialization (C++0x [dcl.init.ref]) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4381 | static void TryReferenceInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4382 | const InitializedEntity &Entity, |
| 4383 | const InitializationKind &Kind, |
| 4384 | Expr *Initializer, |
| 4385 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4386 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4387 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 4388 | Qualifiers T1Quals; |
| 4389 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4390 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 4391 | Qualifiers T2Quals; |
| 4392 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4393 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4394 | // If the initializer is the address of an overloaded function, try |
| 4395 | // to resolve the overloaded function. If all goes well, T2 is the |
| 4396 | // type of the resulting function. |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4397 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 4398 | T1, Sequence)) |
| 4399 | return; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4400 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4401 | // Delegate everything else to a subfunction. |
| 4402 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 4403 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 4404 | } |
| 4405 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4406 | /// Determine whether an expression is a non-referenceable glvalue (one to |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 4407 | /// which a reference can never bind). Attempting to bind a reference to |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4408 | /// such a glvalue will always create a temporary. |
| 4409 | static bool isNonReferenceableGLValue(Expr *E) { |
| 4410 | return E->refersToBitField() || E->refersToVectorElement(); |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 4411 | } |
| 4412 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4413 | /// Reference initialization without resolving overloaded functions. |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4414 | static void TryReferenceInitializationCore(Sema &S, |
| 4415 | const InitializedEntity &Entity, |
| 4416 | const InitializationKind &Kind, |
| 4417 | Expr *Initializer, |
| 4418 | QualType cv1T1, QualType T1, |
| 4419 | Qualifiers T1Quals, |
| 4420 | QualType cv2T2, QualType T2, |
| 4421 | Qualifiers T2Quals, |
| 4422 | InitializationSequence &Sequence) { |
| 4423 | QualType DestType = Entity.getType(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4424 | SourceLocation DeclLoc = Initializer->getBeginLoc(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4425 | // Compute some basic properties of the types and the initializer. |
| 4426 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 4427 | bool isRValueRef = !isLValueRef; |
| 4428 | bool DerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4429 | bool ObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4430 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4431 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4432 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4433 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4434 | ObjCConversion, ObjCLifetimeConversion); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4435 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4436 | // C++0x [dcl.init.ref]p5: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4437 | // 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] | 4438 | // "cv2 T2" as follows: |
| 4439 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4440 | // - If the reference is an lvalue reference and the initializer |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4441 | // expression |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4442 | // Note the analogous bullet points for rvalue refs to functions. Because |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4443 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 4444 | // like lvalue refs. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4445 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4446 | bool T1Function = T1->isFunctionType(); |
| 4447 | if (isLValueRef || T1Function) { |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4448 | if (InitCategory.isLValue() && !isNonReferenceableGLValue(Initializer) && |
Richard Smith | ce76629 | 2016-10-21 23:01:55 +0000 | [diff] [blame] | 4449 | (RefRelationship == Sema::Ref_Compatible || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4450 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4451 | RefRelationship == Sema::Ref_Related))) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4452 | // - 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] | 4453 | // reference-compatible with "cv2 T2," or |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4454 | if (T1Quals != T2Quals) |
| 4455 | // Convert to cv1 T2. This should only add qualifiers unless this is a |
| 4456 | // c-style cast. The removal of qualifiers in that case notionally |
| 4457 | // happens after the reference binding, but that doesn't matter. |
| 4458 | Sequence.AddQualificationConversionStep( |
| 4459 | S.Context.getQualifiedType(T2, T1Quals), |
| 4460 | Initializer->getValueKind()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4461 | if (DerivedToBase) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4462 | Sequence.AddDerivedToBaseCastStep(cv1T1, VK_LValue); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4463 | else if (ObjCConversion) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4464 | Sequence.AddObjCObjectConversionStep(cv1T1); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4465 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4466 | // We only create a temporary here when binding a reference to a |
| 4467 | // bit-field or vector element. Those cases are't supposed to be |
| 4468 | // handled by this bullet, but the outcome is the same either way. |
| 4469 | Sequence.AddReferenceBindingStep(cv1T1, false); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4470 | return; |
| 4471 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4472 | |
| 4473 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 4474 | // reference-related to T2, and can be implicitly converted to an |
| 4475 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 4476 | // with "cv3 T3" (this conversion is selected by enumerating the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4477 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 4478 | // one through overload resolution (13.3)), |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4479 | // 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] | 4480 | // an rvalue. DR1287 removed the "implicitly" here. |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4481 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 4482 | (isLValueRef || InitCategory.isRValue())) { |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4483 | ConvOvlResult = TryRefInitWithConversionFunction( |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4484 | S, Entity, Kind, Initializer, /*AllowRValues*/ isRValueRef, |
| 4485 | /*IsLValueRef*/ isLValueRef, Sequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4486 | if (ConvOvlResult == OR_Success) |
| 4487 | return; |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4488 | if (ConvOvlResult != OR_No_Viable_Function) |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4489 | Sequence.SetOverloadFailure( |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4490 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4491 | ConvOvlResult); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4492 | } |
| 4493 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4494 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4495 | // - Otherwise, the reference shall be an lvalue reference to a |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4496 | // 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] | 4497 | // shall be an rvalue reference. |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 4498 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) { |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 4499 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 4500 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 4501 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4502 | Sequence.SetOverloadFailure( |
| 4503 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4504 | ConvOvlResult); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4505 | else if (!InitCategory.isLValue()) |
| 4506 | Sequence.SetFailed( |
| 4507 | InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
| 4508 | else { |
| 4509 | InitializationSequence::FailureKind FK; |
| 4510 | switch (RefRelationship) { |
| 4511 | case Sema::Ref_Compatible: |
| 4512 | if (Initializer->refersToBitField()) |
| 4513 | FK = InitializationSequence:: |
| 4514 | FK_NonConstLValueReferenceBindingToBitfield; |
| 4515 | else if (Initializer->refersToVectorElement()) |
| 4516 | FK = InitializationSequence:: |
| 4517 | FK_NonConstLValueReferenceBindingToVectorElement; |
| 4518 | else |
| 4519 | llvm_unreachable("unexpected kind of compatible initializer"); |
| 4520 | break; |
| 4521 | case Sema::Ref_Related: |
| 4522 | FK = InitializationSequence::FK_ReferenceInitDropsQualifiers; |
| 4523 | break; |
| 4524 | case Sema::Ref_Incompatible: |
| 4525 | FK = InitializationSequence:: |
| 4526 | FK_NonConstLValueReferenceBindingToUnrelated; |
| 4527 | break; |
| 4528 | } |
| 4529 | Sequence.SetFailed(FK); |
| 4530 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4531 | return; |
| 4532 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4533 | |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4534 | // - If the initializer expression |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4535 | // - is an |
| 4536 | // [<=14] xvalue (but not a bit-field), class prvalue, array prvalue, or |
| 4537 | // [1z] rvalue (but not a bit-field) or |
| 4538 | // function lvalue and "cv1 T1" is reference-compatible with "cv2 T2" |
| 4539 | // |
| 4540 | // Note: functions are handled above and below rather than here... |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4541 | if (!T1Function && |
Richard Smith | ce76629 | 2016-10-21 23:01:55 +0000 | [diff] [blame] | 4542 | (RefRelationship == Sema::Ref_Compatible || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4543 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4544 | RefRelationship == Sema::Ref_Related)) && |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4545 | ((InitCategory.isXValue() && !isNonReferenceableGLValue(Initializer)) || |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 4546 | (InitCategory.isPRValue() && |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 4547 | (S.getLangOpts().CPlusPlus17 || T2->isRecordType() || |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 4548 | T2->isArrayType())))) { |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4549 | ExprValueKind ValueKind = InitCategory.isXValue() ? VK_XValue : VK_RValue; |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4550 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4551 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 4552 | // compiler the freedom to perform a copy here or bind to the |
| 4553 | // object, while C++0x requires that we bind directly to the |
| 4554 | // object. Hence, we always bind to the object without making an |
| 4555 | // extra copy. However, in C++03 requires that we check for the |
| 4556 | // presence of a suitable copy constructor: |
| 4557 | // |
| 4558 | // The constructor that would be used to make the copy shall |
| 4559 | // be callable whether or not the copy is actually done. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4560 | if (!S.getLangOpts().CPlusPlus11 && !S.getLangOpts().MicrosoftExt) |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4561 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4562 | else if (S.getLangOpts().CPlusPlus11) |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4563 | CheckCXX98CompatAccessibleCopy(S, Entity, Initializer); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4564 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4565 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4566 | // C++1z [dcl.init.ref]/5.2.1.2: |
| 4567 | // If the converted initializer is a prvalue, its type T4 is adjusted |
| 4568 | // to type "cv1 T4" and the temporary materialization conversion is |
| 4569 | // applied. |
| 4570 | QualType cv1T4 = S.Context.getQualifiedType(cv2T2, T1Quals); |
| 4571 | if (T1Quals != T2Quals) |
| 4572 | Sequence.AddQualificationConversionStep(cv1T4, ValueKind); |
| 4573 | Sequence.AddReferenceBindingStep(cv1T4, ValueKind == VK_RValue); |
| 4574 | ValueKind = isLValueRef ? VK_LValue : VK_XValue; |
| 4575 | |
| 4576 | // In any case, the reference is bound to the resulting glvalue (or to |
| 4577 | // an appropriate base class subobject). |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4578 | if (DerivedToBase) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4579 | Sequence.AddDerivedToBaseCastStep(cv1T1, ValueKind); |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4580 | else if (ObjCConversion) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4581 | Sequence.AddObjCObjectConversionStep(cv1T1); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4582 | return; |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4583 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4584 | |
| 4585 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 4586 | // reference-related to T2, and can be implicitly converted to an |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4587 | // xvalue, class prvalue, or function lvalue of type "cv3 T3", |
| 4588 | // where "cv1 T1" is reference-compatible with "cv3 T3", |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4589 | // |
| 4590 | // DR1287 removes the "implicitly" here. |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4591 | if (T2->isRecordType()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4592 | if (RefRelationship == Sema::Ref_Incompatible) { |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4593 | ConvOvlResult = TryRefInitWithConversionFunction( |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4594 | S, Entity, Kind, Initializer, /*AllowRValues*/ true, |
| 4595 | /*IsLValueRef*/ isLValueRef, Sequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4596 | if (ConvOvlResult) |
| 4597 | Sequence.SetOverloadFailure( |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4598 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4599 | ConvOvlResult); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4600 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4601 | return; |
| 4602 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4603 | |
Richard Smith | ce76629 | 2016-10-21 23:01:55 +0000 | [diff] [blame] | 4604 | if (RefRelationship == Sema::Ref_Compatible && |
Douglas Gregor | 6fa6ab0 | 2013-03-26 23:59:23 +0000 | [diff] [blame] | 4605 | isRValueRef && InitCategory.isLValue()) { |
| 4606 | Sequence.SetFailed( |
| 4607 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 4608 | return; |
| 4609 | } |
| 4610 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4611 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 4612 | return; |
| 4613 | } |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4614 | |
| 4615 | // - Otherwise, a temporary of type "cv1 T1" is created and initialized |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4616 | // from the initializer expression using the rules for a non-reference |
Richard Smith | 2eabf78 | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 4617 | // copy-initialization (8.5). The reference is then bound to the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4618 | // temporary. [...] |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 4619 | |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 4620 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 4621 | |
Richard Smith | 2eabf78 | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 4622 | // FIXME: Why do we use an implicit conversion here rather than trying |
| 4623 | // copy-initialization? |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4624 | ImplicitConversionSequence ICS |
| 4625 | = S.TryImplicitConversion(Initializer, TempEntity.getType(), |
Richard Smith | 2eabf78 | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 4626 | /*SuppressUserConversions=*/false, |
| 4627 | /*AllowExplicit=*/false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4628 | /*FIXME:InOverloadResolution=*/false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4629 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 4630 | /*AllowObjCWritebackConversion=*/false); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4631 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4632 | if (ICS.isBad()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4633 | // FIXME: Use the conversion function set stored in ICS to turn |
| 4634 | // this into an overloading ambiguity diagnostic. However, we need |
| 4635 | // to keep that set as an OverloadCandidateSet rather than as some |
| 4636 | // other kind of set. |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4637 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 4638 | Sequence.SetOverloadFailure( |
| 4639 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4640 | ConvOvlResult); |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 4641 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 4642 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4643 | else |
| 4644 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4645 | return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4646 | } else { |
| 4647 | Sequence.AddConversionSequenceStep(ICS, TempEntity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4648 | } |
| 4649 | |
| 4650 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 4651 | // same cv-qualification as, or greater cv-qualification |
| 4652 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 4653 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 4654 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4655 | if (RefRelationship == Sema::Ref_Related && |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 4656 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4657 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 4658 | return; |
| 4659 | } |
| 4660 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4661 | // [...] 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] | 4662 | // reference, the initializer expression shall not be an lvalue. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4663 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 4664 | InitCategory.isLValue()) { |
| 4665 | Sequence.SetFailed( |
| 4666 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 4667 | return; |
| 4668 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4669 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4670 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4671 | } |
| 4672 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4673 | /// Attempt character array initialization from a string literal |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4674 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 4675 | static void TryStringLiteralInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4676 | const InitializedEntity &Entity, |
| 4677 | const InitializationKind &Kind, |
| 4678 | Expr *Initializer, |
| 4679 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4680 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4681 | } |
| 4682 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4683 | /// Attempt value initialization (C++ [dcl.init]p7). |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4684 | static void TryValueInitialization(Sema &S, |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4685 | const InitializedEntity &Entity, |
| 4686 | const InitializationKind &Kind, |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4687 | InitializationSequence &Sequence, |
| 4688 | InitListExpr *InitList) { |
| 4689 | assert((!InitList || InitList->getNumInits() == 0) && |
| 4690 | "Shouldn't use value-init for non-empty init lists"); |
| 4691 | |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4692 | // C++98 [dcl.init]p5, C++11 [dcl.init]p7: |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4693 | // |
| 4694 | // To value-initialize an object of type T means: |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4695 | QualType T = Entity.getType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4696 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4697 | // -- if T is an array type, then each element is value-initialized; |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4698 | T = S.Context.getBaseElementType(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4699 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4700 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 4701 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4702 | bool NeedZeroInitialization = true; |
Richard Smith | 505ef81 | 2016-12-21 01:57:02 +0000 | [diff] [blame] | 4703 | // C++98: |
| 4704 | // -- if T is a class type (clause 9) with a user-declared constructor |
| 4705 | // (12.1), then the default constructor for T is called (and the |
| 4706 | // initialization is ill-formed if T has no accessible default |
| 4707 | // constructor); |
| 4708 | // C++11: |
| 4709 | // -- if T is a class type (clause 9) with either no default constructor |
| 4710 | // (12.1 [class.ctor]) or a default constructor that is user-provided |
| 4711 | // or deleted, then the object is default-initialized; |
| 4712 | // |
| 4713 | // Note that the C++11 rule is the same as the C++98 rule if there are no |
| 4714 | // defaulted or deleted constructors, so we just use it unconditionally. |
| 4715 | CXXConstructorDecl *CD = S.LookupDefaultConstructor(ClassDecl); |
| 4716 | if (!CD || !CD->getCanonicalDecl()->isDefaulted() || CD->isDeleted()) |
| 4717 | NeedZeroInitialization = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4718 | |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4719 | // -- if T is a (possibly cv-qualified) non-union class type without a |
| 4720 | // user-provided or deleted default constructor, then the object is |
| 4721 | // zero-initialized and, if T has a non-trivial default constructor, |
| 4722 | // default-initialized; |
Richard Smith | fb26652 | 2012-10-18 00:44:17 +0000 | [diff] [blame] | 4723 | // The 'non-union' here was removed by DR1502. The 'non-trivial default |
| 4724 | // constructor' part was removed by DR1507. |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4725 | if (NeedZeroInitialization) |
| 4726 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 4727 | |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 4728 | // C++03: |
| 4729 | // -- if T is a non-union class type without a user-declared constructor, |
| 4730 | // then every non-static data member and base class component of T is |
| 4731 | // value-initialized; |
| 4732 | // [...] A program that calls for [...] value-initialization of an |
| 4733 | // entity of reference type is ill-formed. |
| 4734 | // |
| 4735 | // C++11 doesn't need this handling, because value-initialization does not |
| 4736 | // occur recursively there, and the implicit default constructor is |
| 4737 | // defined as deleted in the problematic cases. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4738 | if (!S.getLangOpts().CPlusPlus11 && |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 4739 | ClassDecl->hasUninitializedReferenceMember()) { |
| 4740 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForReference); |
| 4741 | return; |
| 4742 | } |
| 4743 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4744 | // If this is list-value-initialization, pass the empty init list on when |
| 4745 | // building the constructor call. This affects the semantics of a few |
| 4746 | // things (such as whether an explicit default constructor can be called). |
| 4747 | Expr *InitListAsExpr = InitList; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4748 | MultiExprArg Args(&InitListAsExpr, InitList ? 1 : 0); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4749 | bool InitListSyntax = InitList; |
| 4750 | |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 4751 | // FIXME: Instead of creating a CXXConstructExpr of array type here, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 4752 | // wrap a class-typed CXXConstructExpr in an ArrayInitLoopExpr. |
| 4753 | return TryConstructorInitialization( |
| 4754 | S, Entity, Kind, Args, T, Entity.getType(), Sequence, InitListSyntax); |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4755 | } |
| 4756 | } |
| 4757 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4758 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4759 | } |
| 4760 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4761 | /// Attempt default initialization (C++ [dcl.init]p6). |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4762 | static void TryDefaultInitialization(Sema &S, |
| 4763 | const InitializedEntity &Entity, |
| 4764 | const InitializationKind &Kind, |
| 4765 | InitializationSequence &Sequence) { |
| 4766 | assert(Kind.getKind() == InitializationKind::IK_Default); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4767 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4768 | // C++ [dcl.init]p6: |
| 4769 | // To default-initialize an object of type T means: |
| 4770 | // - if T is an array type, each element is default-initialized; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4771 | QualType DestType = S.Context.getBaseElementType(Entity.getType()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4772 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4773 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 4774 | // constructor for T is called (and the initialization is ill-formed if |
| 4775 | // T has no accessible default constructor); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4776 | if (DestType->isRecordType() && S.getLangOpts().CPlusPlus) { |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 4777 | TryConstructorInitialization(S, Entity, Kind, None, DestType, |
| 4778 | Entity.getType(), Sequence); |
Chandler Carruth | c926240 | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 4779 | return; |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4780 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4781 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4782 | // - otherwise, no initialization is performed. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4783 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4784 | // If a program calls for the default initialization of an object of |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4785 | // 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] | 4786 | // default constructor. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4787 | if (DestType.isConstQualified() && S.getLangOpts().CPlusPlus) { |
Nico Weber | 337d5aa | 2015-04-17 08:32:38 +0000 | [diff] [blame] | 4788 | if (!maybeRecoverWithZeroInitialization(S, Sequence, Entity)) |
| 4789 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4790 | return; |
| 4791 | } |
| 4792 | |
| 4793 | // If the destination type has a lifetime property, zero-initialize it. |
| 4794 | if (DestType.getQualifiers().hasObjCLifetime()) { |
| 4795 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 4796 | return; |
| 4797 | } |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4798 | } |
| 4799 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4800 | /// Attempt a user-defined conversion between two types (C++ [dcl.init]), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4801 | /// which enumerates all conversion functions and performs overload resolution |
| 4802 | /// to select the best. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4803 | static void TryUserDefinedConversion(Sema &S, |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 4804 | QualType DestType, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4805 | const InitializationKind &Kind, |
| 4806 | Expr *Initializer, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4807 | InitializationSequence &Sequence, |
| 4808 | bool TopLevelOfInitList) { |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4809 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 4810 | QualType SourceType = Initializer->getType(); |
| 4811 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 4812 | "Must have a class type to perform a user-defined conversion"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4813 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4814 | // Build the candidate set directly in the initialization sequence |
| 4815 | // structure, so that it will persist if we fail. |
| 4816 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 4817 | CandidateSet.clear(OverloadCandidateSet::CSK_InitByUserDefinedConversion); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4818 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4819 | // Determine whether we are allowed to call explicit constructors or |
| 4820 | // explicit conversion operators. |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 4821 | bool AllowExplicit = Kind.AllowExplicit(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4822 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4823 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 4824 | // The type we're converting to is a class type. Enumerate its constructors |
| 4825 | // to see if there is a suitable conversion. |
| 4826 | CXXRecordDecl *DestRecordDecl |
| 4827 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4828 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4829 | // Try to complete the type we're converting to. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4830 | if (S.isCompleteType(Kind.getLocation(), DestType)) { |
Richard Smith | 776e9c3 | 2017-02-01 03:28:59 +0000 | [diff] [blame] | 4831 | for (NamedDecl *D : S.LookupConstructors(DestRecordDecl)) { |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4832 | auto Info = getConstructorInfo(D); |
| 4833 | if (!Info.Constructor) |
| 4834 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4835 | |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4836 | if (!Info.Constructor->isInvalidDecl() && |
| 4837 | Info.Constructor->isConvertingConstructor(AllowExplicit)) { |
| 4838 | if (Info.ConstructorTmpl) |
| 4839 | S.AddTemplateOverloadCandidate(Info.ConstructorTmpl, Info.FoundDecl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4840 | /*ExplicitArgs*/ nullptr, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4841 | Initializer, CandidateSet, |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 4842 | /*SuppressUserConversions=*/true); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4843 | else |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4844 | S.AddOverloadCandidate(Info.Constructor, Info.FoundDecl, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4845 | Initializer, CandidateSet, |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 4846 | /*SuppressUserConversions=*/true); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4847 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4848 | } |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4849 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4850 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4851 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4852 | SourceLocation DeclLoc = Initializer->getBeginLoc(); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4853 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4854 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 4855 | // The type we're converting from is a class type, enumerate its conversion |
| 4856 | // functions. |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4857 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4858 | // We can only enumerate the conversion functions for a complete type; if |
| 4859 | // the type isn't complete, simply skip this step. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4860 | if (S.isCompleteType(DeclLoc, SourceType)) { |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4861 | CXXRecordDecl *SourceRecordDecl |
| 4862 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4863 | |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 4864 | const auto &Conversions = |
| 4865 | SourceRecordDecl->getVisibleConversionFunctions(); |
| 4866 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4867 | NamedDecl *D = *I; |
| 4868 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 4869 | if (isa<UsingShadowDecl>(D)) |
| 4870 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4871 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4872 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 4873 | CXXConversionDecl *Conv; |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4874 | if (ConvTemplate) |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4875 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4876 | else |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4877 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4878 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4879 | if (AllowExplicit || !Conv->isExplicit()) { |
| 4880 | if (ConvTemplate) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4881 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4882 | ActingDC, Initializer, DestType, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 4883 | CandidateSet, AllowExplicit); |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4884 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4885 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 4886 | Initializer, DestType, CandidateSet, |
| 4887 | AllowExplicit); |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4888 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4889 | } |
| 4890 | } |
| 4891 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4892 | |
| 4893 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4894 | OverloadCandidateSet::iterator Best; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4895 | if (OverloadingResult Result |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 4896 | = CandidateSet.BestViableFunction(S, DeclLoc, Best)) { |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4897 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4898 | InitializationSequence::FK_UserConversionOverloadFailed, |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4899 | Result); |
| 4900 | return; |
| 4901 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4902 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4903 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 4904 | Function->setReferenced(); |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4905 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4906 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4907 | if (isa<CXXConstructorDecl>(Function)) { |
| 4908 | // Add the user-defined conversion step. Any cv-qualification conversion is |
Richard Smith | b24f067 | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 4909 | // subsumed by the initialization. Per DR5, the created temporary is of the |
| 4910 | // cv-unqualified type of the destination. |
| 4911 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
| 4912 | DestType.getUnqualifiedType(), |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4913 | HadMultipleCandidates); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4914 | |
| 4915 | // C++14 and before: |
| 4916 | // - if the function is a constructor, the call initializes a temporary |
| 4917 | // of the cv-unqualified version of the destination type. The [...] |
| 4918 | // temporary [...] is then used to direct-initialize, according to the |
| 4919 | // rules above, the object that is the destination of the |
| 4920 | // copy-initialization. |
| 4921 | // Note that this just performs a simple object copy from the temporary. |
| 4922 | // |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 4923 | // C++17: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4924 | // - if the function is a constructor, the call is a prvalue of the |
| 4925 | // cv-unqualified version of the destination type whose return object |
| 4926 | // is initialized by the constructor. The call is used to |
| 4927 | // direct-initialize, according to the rules above, the object that |
| 4928 | // is the destination of the copy-initialization. |
| 4929 | // Therefore we need to do nothing further. |
| 4930 | // |
| 4931 | // FIXME: Mark this copy as extraneous. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 4932 | if (!S.getLangOpts().CPlusPlus17) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4933 | Sequence.AddFinalCopy(DestType); |
Richard Smith | 16d3150 | 2016-12-21 01:31:56 +0000 | [diff] [blame] | 4934 | else if (DestType.hasQualifiers()) |
| 4935 | Sequence.AddQualificationConversionStep(DestType, VK_RValue); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4936 | return; |
| 4937 | } |
| 4938 | |
| 4939 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 4940 | QualType ConvType = Function->getCallResultType(); |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4941 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType, |
| 4942 | HadMultipleCandidates); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4943 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4944 | if (ConvType->getAs<RecordType>()) { |
| 4945 | // The call is used to direct-initialize [...] the object that is the |
| 4946 | // destination of the copy-initialization. |
| 4947 | // |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 4948 | // 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] | 4949 | // - If the initializer expression is a prvalue and the cv-unqualified |
| 4950 | // version of the source type is the same as the class of the |
| 4951 | // destination [... do not make an extra copy] |
| 4952 | // |
| 4953 | // FIXME: Mark this copy as extraneous. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 4954 | if (!S.getLangOpts().CPlusPlus17 || |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4955 | Function->getReturnType()->isReferenceType() || |
| 4956 | !S.Context.hasSameUnqualifiedType(ConvType, DestType)) |
| 4957 | Sequence.AddFinalCopy(DestType); |
Richard Smith | 16d3150 | 2016-12-21 01:31:56 +0000 | [diff] [blame] | 4958 | else if (!S.Context.hasSameType(ConvType, DestType)) |
| 4959 | Sequence.AddQualificationConversionStep(DestType, VK_RValue); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4960 | return; |
| 4961 | } |
| 4962 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4963 | // If the conversion following the call to the conversion function |
| 4964 | // is interesting, add it as a separate step. |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4965 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 4966 | Best->FinalConversion.Third) { |
| 4967 | ImplicitConversionSequence ICS; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4968 | ICS.setStandard(); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4969 | ICS.Standard = Best->FinalConversion; |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4970 | Sequence.AddConversionSequenceStep(ICS, DestType, TopLevelOfInitList); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4971 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4972 | } |
| 4973 | |
Richard Smith | f032001b | 2013-06-20 02:18:31 +0000 | [diff] [blame] | 4974 | /// An egregious hack for compatibility with libstdc++-4.2: in <tr1/hashtable>, |
| 4975 | /// a function with a pointer return type contains a 'return false;' statement. |
| 4976 | /// In C++11, 'false' is not a null pointer, so this breaks the build of any |
| 4977 | /// code using that header. |
| 4978 | /// |
| 4979 | /// Work around this by treating 'return false;' as zero-initializing the result |
| 4980 | /// if it's used in a pointer-returning function in a system header. |
| 4981 | static bool isLibstdcxxPointerReturnFalseHack(Sema &S, |
| 4982 | const InitializedEntity &Entity, |
| 4983 | const Expr *Init) { |
| 4984 | return S.getLangOpts().CPlusPlus11 && |
| 4985 | Entity.getKind() == InitializedEntity::EK_Result && |
| 4986 | Entity.getType()->isPointerType() && |
| 4987 | isa<CXXBoolLiteralExpr>(Init) && |
| 4988 | !cast<CXXBoolLiteralExpr>(Init)->getValue() && |
| 4989 | S.getSourceManager().isInSystemHeader(Init->getExprLoc()); |
| 4990 | } |
| 4991 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4992 | /// The non-zero enum values here are indexes into diagnostic alternatives. |
| 4993 | enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar }; |
| 4994 | |
| 4995 | /// Determines whether this expression is an acceptable ICR source. |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4996 | static InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e, |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4997 | bool isAddressOf, bool &isWeakAccess) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4998 | // Skip parens. |
| 4999 | e = e->IgnoreParens(); |
| 5000 | |
| 5001 | // Skip address-of nodes. |
| 5002 | if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 5003 | if (op->getOpcode() == UO_AddrOf) |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5004 | return isInvalidICRSource(C, op->getSubExpr(), /*addressof*/ true, |
| 5005 | isWeakAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5006 | |
| 5007 | // Skip certain casts. |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 5008 | } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 5009 | switch (ce->getCastKind()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5010 | case CK_Dependent: |
| 5011 | case CK_BitCast: |
| 5012 | case CK_LValueBitCast: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5013 | case CK_NoOp: |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5014 | return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf, isWeakAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5015 | |
| 5016 | case CK_ArrayToPointerDecay: |
| 5017 | return IIK_nonscalar; |
| 5018 | |
| 5019 | case CK_NullToPointer: |
| 5020 | return IIK_okay; |
| 5021 | |
| 5022 | default: |
| 5023 | break; |
| 5024 | } |
| 5025 | |
| 5026 | // 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] | 5027 | } else if (isa<DeclRefExpr>(e)) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5028 | // set isWeakAccess to true, to mean that there will be an implicit |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5029 | // load which requires a cleanup. |
| 5030 | if (e->getType().getObjCLifetime() == Qualifiers::OCL_Weak) |
| 5031 | isWeakAccess = true; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5032 | |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 5033 | if (!isAddressOf) return IIK_nonlocal; |
| 5034 | |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5035 | VarDecl *var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl()); |
| 5036 | if (!var) return IIK_nonlocal; |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 5037 | |
| 5038 | return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5039 | |
| 5040 | // If we have a conditional operator, check both sides. |
| 5041 | } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) { |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5042 | if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf, |
| 5043 | isWeakAccess)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5044 | return iik; |
| 5045 | |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5046 | return isInvalidICRSource(C, cond->getRHS(), isAddressOf, isWeakAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5047 | |
| 5048 | // These are never scalar. |
| 5049 | } else if (isa<ArraySubscriptExpr>(e)) { |
| 5050 | return IIK_nonscalar; |
| 5051 | |
| 5052 | // Otherwise, it needs to be a null pointer constant. |
| 5053 | } else { |
| 5054 | return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull) |
| 5055 | ? IIK_okay : IIK_nonlocal); |
| 5056 | } |
| 5057 | |
| 5058 | return IIK_nonlocal; |
| 5059 | } |
| 5060 | |
| 5061 | /// Check whether the given expression is a valid operand for an |
| 5062 | /// indirect copy/restore. |
| 5063 | static void checkIndirectCopyRestoreSource(Sema &S, Expr *src) { |
| 5064 | assert(src->isRValue()); |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5065 | bool isWeakAccess = false; |
| 5066 | InvalidICRKind iik = isInvalidICRSource(S.Context, src, false, isWeakAccess); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5067 | // If isWeakAccess to true, there will be an implicit |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5068 | // load which requires a cleanup. |
| 5069 | if (S.getLangOpts().ObjCAutoRefCount && isWeakAccess) |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 5070 | S.Cleanup.setExprNeedsCleanups(true); |
| 5071 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5072 | if (iik == IIK_okay) return; |
| 5073 | |
| 5074 | S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback) |
| 5075 | << ((unsigned) iik - 1) // shift index into diagnostic explanations |
| 5076 | << src->getSourceRange(); |
| 5077 | } |
| 5078 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5079 | /// Determine whether we have compatible array types for the |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5080 | /// purposes of GNU by-copy array initialization. |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 5081 | static bool hasCompatibleArrayTypes(ASTContext &Context, const ArrayType *Dest, |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5082 | const ArrayType *Source) { |
| 5083 | // If the source and destination array types are equivalent, we're |
| 5084 | // done. |
| 5085 | if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0))) |
| 5086 | return true; |
| 5087 | |
| 5088 | // Make sure that the element types are the same. |
| 5089 | if (!Context.hasSameType(Dest->getElementType(), Source->getElementType())) |
| 5090 | return false; |
| 5091 | |
| 5092 | // The only mismatch we allow is when the destination is an |
| 5093 | // incomplete array type and the source is a constant array type. |
| 5094 | return Source->isConstantArrayType() && Dest->isIncompleteArrayType(); |
| 5095 | } |
| 5096 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5097 | static bool tryObjCWritebackConversion(Sema &S, |
| 5098 | InitializationSequence &Sequence, |
| 5099 | const InitializedEntity &Entity, |
| 5100 | Expr *Initializer) { |
| 5101 | bool ArrayDecay = false; |
| 5102 | QualType ArgType = Initializer->getType(); |
| 5103 | QualType ArgPointee; |
| 5104 | if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) { |
| 5105 | ArrayDecay = true; |
| 5106 | ArgPointee = ArgArrayType->getElementType(); |
| 5107 | ArgType = S.Context.getPointerType(ArgPointee); |
| 5108 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5109 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5110 | // Handle write-back conversion. |
| 5111 | QualType ConvertedArgType; |
| 5112 | if (!S.isObjCWritebackConversion(ArgType, Entity.getType(), |
| 5113 | ConvertedArgType)) |
| 5114 | return false; |
| 5115 | |
| 5116 | // We should copy unless we're passing to an argument explicitly |
| 5117 | // marked 'out'. |
| 5118 | bool ShouldCopy = true; |
| 5119 | if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 5120 | ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 5121 | |
| 5122 | // Do we need an lvalue conversion? |
| 5123 | if (ArrayDecay || Initializer->isGLValue()) { |
| 5124 | ImplicitConversionSequence ICS; |
| 5125 | ICS.setStandard(); |
| 5126 | ICS.Standard.setAsIdentityConversion(); |
| 5127 | |
| 5128 | QualType ResultType; |
| 5129 | if (ArrayDecay) { |
| 5130 | ICS.Standard.First = ICK_Array_To_Pointer; |
| 5131 | ResultType = S.Context.getPointerType(ArgPointee); |
| 5132 | } else { |
| 5133 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
| 5134 | ResultType = Initializer->getType().getNonLValueExprType(S.Context); |
| 5135 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5136 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5137 | Sequence.AddConversionSequenceStep(ICS, ResultType); |
| 5138 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5139 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5140 | Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
| 5141 | return true; |
| 5142 | } |
| 5143 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5144 | static bool TryOCLSamplerInitialization(Sema &S, |
| 5145 | InitializationSequence &Sequence, |
| 5146 | QualType DestType, |
| 5147 | Expr *Initializer) { |
| 5148 | if (!S.getLangOpts().OpenCL || !DestType->isSamplerT() || |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 5149 | (!Initializer->isIntegerConstantExpr(S.Context) && |
| 5150 | !Initializer->getType()->isSamplerT())) |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5151 | return false; |
| 5152 | |
| 5153 | Sequence.AddOCLSamplerInitStep(DestType); |
| 5154 | return true; |
| 5155 | } |
| 5156 | |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5157 | // |
| 5158 | // OpenCL 1.2 spec, s6.12.10 |
| 5159 | // |
| 5160 | // The event argument can also be used to associate the |
| 5161 | // async_work_group_copy with a previous async copy allowing |
| 5162 | // an event to be shared by multiple async copies; otherwise |
| 5163 | // event should be zero. |
| 5164 | // |
| 5165 | static bool TryOCLZeroEventInitialization(Sema &S, |
| 5166 | InitializationSequence &Sequence, |
| 5167 | QualType DestType, |
| 5168 | Expr *Initializer) { |
| 5169 | if (!S.getLangOpts().OpenCL || !DestType->isEventT() || |
| 5170 | !Initializer->isIntegerConstantExpr(S.getASTContext()) || |
| 5171 | (Initializer->EvaluateKnownConstInt(S.getASTContext()) != 0)) |
| 5172 | return false; |
| 5173 | |
| 5174 | Sequence.AddOCLZeroEventStep(DestType); |
| 5175 | return true; |
| 5176 | } |
| 5177 | |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 5178 | static bool TryOCLZeroQueueInitialization(Sema &S, |
| 5179 | InitializationSequence &Sequence, |
| 5180 | QualType DestType, |
| 5181 | Expr *Initializer) { |
| 5182 | if (!S.getLangOpts().OpenCL || S.getLangOpts().OpenCLVersion < 200 || |
| 5183 | !DestType->isQueueT() || |
| 5184 | !Initializer->isIntegerConstantExpr(S.getASTContext()) || |
| 5185 | (Initializer->EvaluateKnownConstInt(S.getASTContext()) != 0)) |
| 5186 | return false; |
| 5187 | |
| 5188 | Sequence.AddOCLZeroQueueStep(DestType); |
| 5189 | return true; |
| 5190 | } |
| 5191 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5192 | InitializationSequence::InitializationSequence(Sema &S, |
| 5193 | const InitializedEntity &Entity, |
| 5194 | const InitializationKind &Kind, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5195 | MultiExprArg Args, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 5196 | bool TopLevelOfInitList, |
| 5197 | bool TreatUnavailableAsInvalid) |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 5198 | : FailedCandidateSet(Kind.getLocation(), OverloadCandidateSet::CSK_Normal) { |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 5199 | InitializeFrom(S, Entity, Kind, Args, TopLevelOfInitList, |
| 5200 | TreatUnavailableAsInvalid); |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 5201 | } |
| 5202 | |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 5203 | /// Tries to get a FunctionDecl out of `E`. If it succeeds and we can take the |
| 5204 | /// address of that function, this returns true. Otherwise, it returns false. |
| 5205 | static bool isExprAnUnaddressableFunction(Sema &S, const Expr *E) { |
| 5206 | auto *DRE = dyn_cast<DeclRefExpr>(E); |
| 5207 | if (!DRE || !isa<FunctionDecl>(DRE->getDecl())) |
| 5208 | return false; |
| 5209 | |
| 5210 | return !S.checkAddressOfFunctionIsAvailable( |
| 5211 | cast<FunctionDecl>(DRE->getDecl())); |
| 5212 | } |
| 5213 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5214 | /// Determine whether we can perform an elementwise array copy for this kind |
| 5215 | /// of entity. |
| 5216 | static bool canPerformArrayCopy(const InitializedEntity &Entity) { |
| 5217 | switch (Entity.getKind()) { |
| 5218 | case InitializedEntity::EK_LambdaCapture: |
| 5219 | // C++ [expr.prim.lambda]p24: |
| 5220 | // For array members, the array elements are direct-initialized in |
| 5221 | // increasing subscript order. |
| 5222 | return true; |
| 5223 | |
| 5224 | case InitializedEntity::EK_Variable: |
| 5225 | // C++ [dcl.decomp]p1: |
| 5226 | // [...] each element is copy-initialized or direct-initialized from the |
| 5227 | // corresponding element of the assignment-expression [...] |
| 5228 | return isa<DecompositionDecl>(Entity.getDecl()); |
| 5229 | |
| 5230 | case InitializedEntity::EK_Member: |
| 5231 | // C++ [class.copy.ctor]p14: |
| 5232 | // - if the member is an array, each element is direct-initialized with |
| 5233 | // the corresponding subobject of x |
| 5234 | return Entity.isImplicitMemberInitializer(); |
| 5235 | |
| 5236 | case InitializedEntity::EK_ArrayElement: |
| 5237 | // All the above cases are intended to apply recursively, even though none |
| 5238 | // of them actually say that. |
| 5239 | if (auto *E = Entity.getParent()) |
| 5240 | return canPerformArrayCopy(*E); |
| 5241 | break; |
| 5242 | |
| 5243 | default: |
| 5244 | break; |
| 5245 | } |
| 5246 | |
| 5247 | return false; |
| 5248 | } |
| 5249 | |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 5250 | void InitializationSequence::InitializeFrom(Sema &S, |
| 5251 | const InitializedEntity &Entity, |
| 5252 | const InitializationKind &Kind, |
| 5253 | MultiExprArg Args, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 5254 | bool TopLevelOfInitList, |
| 5255 | bool TreatUnavailableAsInvalid) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5256 | ASTContext &Context = S.Context; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5257 | |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 5258 | // Eliminate non-overload placeholder types in the arguments. We |
| 5259 | // need to do this before checking whether types are dependent |
| 5260 | // because lowering a pseudo-object expression might well give us |
| 5261 | // something of dependent type. |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5262 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 5263 | if (Args[I]->getType()->isNonOverloadPlaceholderType()) { |
| 5264 | // FIXME: should we be doing this here? |
| 5265 | ExprResult result = S.CheckPlaceholderExpr(Args[I]); |
| 5266 | if (result.isInvalid()) { |
| 5267 | SetFailed(FK_PlaceholderType); |
| 5268 | return; |
| 5269 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5270 | Args[I] = result.get(); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 5271 | } |
| 5272 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5273 | // C++0x [dcl.init]p16: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5274 | // The semantics of initializers are as follows. The destination type is |
| 5275 | // the type of the object or reference being initialized and the source |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5276 | // 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] | 5277 | // 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] | 5278 | // parenthesized list of expressions. |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5279 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5280 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5281 | if (DestType->isDependentType() || |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5282 | Expr::hasAnyTypeDependentArguments(Args)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5283 | SequenceKind = DependentSequence; |
| 5284 | return; |
| 5285 | } |
| 5286 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5287 | // Almost everything is a normal sequence. |
| 5288 | setSequenceKind(NormalSequence); |
| 5289 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5290 | QualType SourceType; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5291 | Expr *Initializer = nullptr; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5292 | if (Args.size() == 1) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5293 | Initializer = Args[0]; |
Fariborz Jahanian | 283bf89 | 2013-12-18 21:04:43 +0000 | [diff] [blame] | 5294 | if (S.getLangOpts().ObjC1) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5295 | if (S.CheckObjCBridgeRelatedConversions(Initializer->getBeginLoc(), |
Fariborz Jahanian | 283bf89 | 2013-12-18 21:04:43 +0000 | [diff] [blame] | 5296 | DestType, Initializer->getType(), |
| 5297 | Initializer) || |
| 5298 | S.ConversionToObjCStringLiteralCheck(DestType, Initializer)) |
| 5299 | Args[0] = Initializer; |
Fariborz Jahanian | 283bf89 | 2013-12-18 21:04:43 +0000 | [diff] [blame] | 5300 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5301 | if (!isa<InitListExpr>(Initializer)) |
| 5302 | SourceType = Initializer->getType(); |
| 5303 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5304 | |
Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 5305 | // - If the initializer is a (non-parenthesized) braced-init-list, the |
| 5306 | // object is list-initialized (8.5.4). |
| 5307 | if (Kind.getKind() != InitializationKind::IK_Direct) { |
| 5308 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 5309 | TryListInitialization(S, Entity, Kind, InitList, *this, |
| 5310 | TreatUnavailableAsInvalid); |
Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 5311 | return; |
| 5312 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5313 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5314 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5315 | // - If the destination type is a reference type, see 8.5.3. |
| 5316 | if (DestType->isReferenceType()) { |
| 5317 | // C++0x [dcl.init.ref]p1: |
| 5318 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 5319 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 5320 | // by an object that can be converted into a T. |
| 5321 | // (Therefore, multiple arguments are not permitted.) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5322 | if (Args.size() != 1) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5323 | SetFailed(FK_TooManyInitsForReference); |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 5324 | // C++17 [dcl.init.ref]p5: |
| 5325 | // A reference [...] is initialized by an expression [...] as follows: |
| 5326 | // If the initializer is not an expression, presumably we should reject, |
| 5327 | // but the standard fails to actually say so. |
| 5328 | else if (isa<InitListExpr>(Args[0])) |
| 5329 | SetFailed(FK_ParenthesizedListInitForReference); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5330 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5331 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5332 | return; |
| 5333 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5334 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5335 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5336 | if (Kind.getKind() == InitializationKind::IK_Value || |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5337 | (Kind.getKind() == InitializationKind::IK_Direct && Args.empty())) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5338 | TryValueInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5339 | return; |
| 5340 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5341 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5342 | // Handle default initialization. |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 5343 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5344 | TryDefaultInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5345 | return; |
| 5346 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5347 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 5348 | // - If the destination type is an array of characters, an array of |
| 5349 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 5350 | // initializer is a string literal, see 8.5.2. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5351 | // - Otherwise, if the destination type is an array, the program is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5352 | // ill-formed. |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5353 | if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) { |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 5354 | if (Initializer && isa<VariableArrayType>(DestAT)) { |
| 5355 | SetFailed(FK_VariableLengthArrayHasInitializer); |
| 5356 | return; |
| 5357 | } |
| 5358 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 5359 | if (Initializer) { |
| 5360 | switch (IsStringInit(Initializer, DestAT, Context)) { |
| 5361 | case SIF_None: |
| 5362 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
| 5363 | return; |
| 5364 | case SIF_NarrowStringIntoWideChar: |
| 5365 | SetFailed(FK_NarrowStringIntoWideCharArray); |
| 5366 | return; |
| 5367 | case SIF_WideStringIntoChar: |
| 5368 | SetFailed(FK_WideStringIntoCharArray); |
| 5369 | return; |
| 5370 | case SIF_IncompatWideStringIntoWideChar: |
| 5371 | SetFailed(FK_IncompatWideStringIntoWideChar); |
| 5372 | return; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 5373 | case SIF_PlainStringIntoUTF8Char: |
| 5374 | SetFailed(FK_PlainStringIntoUTF8Char); |
| 5375 | return; |
| 5376 | case SIF_UTF8StringIntoPlainChar: |
| 5377 | SetFailed(FK_UTF8StringIntoPlainChar); |
| 5378 | return; |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 5379 | case SIF_Other: |
| 5380 | break; |
| 5381 | } |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 5382 | } |
| 5383 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5384 | // Some kinds of initialization permit an array to be initialized from |
| 5385 | // another array of the same type, and perform elementwise initialization. |
| 5386 | if (Initializer && isa<ConstantArrayType>(DestAT) && |
| 5387 | S.Context.hasSameUnqualifiedType(Initializer->getType(), |
| 5388 | Entity.getType()) && |
| 5389 | canPerformArrayCopy(Entity)) { |
| 5390 | // If source is a prvalue, use it directly. |
| 5391 | if (Initializer->getValueKind() == VK_RValue) { |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 5392 | AddArrayInitStep(DestType, /*IsGNUExtension*/false); |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5393 | return; |
| 5394 | } |
| 5395 | |
| 5396 | // Emit element-at-a-time copy loop. |
| 5397 | InitializedEntity Element = |
| 5398 | InitializedEntity::InitializeElement(S.Context, 0, Entity); |
| 5399 | QualType InitEltT = |
| 5400 | Context.getAsArrayType(Initializer->getType())->getElementType(); |
Richard Smith | 30e304e | 2016-12-14 00:03:17 +0000 | [diff] [blame] | 5401 | OpaqueValueExpr OVE(Initializer->getExprLoc(), InitEltT, |
| 5402 | Initializer->getValueKind(), |
| 5403 | Initializer->getObjectKind()); |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5404 | Expr *OVEAsExpr = &OVE; |
| 5405 | InitializeFrom(S, Element, Kind, OVEAsExpr, TopLevelOfInitList, |
| 5406 | TreatUnavailableAsInvalid); |
| 5407 | if (!Failed()) |
| 5408 | AddArrayInitLoopStep(Entity.getType(), InitEltT); |
| 5409 | return; |
| 5410 | } |
| 5411 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5412 | // Note: as an GNU C extension, we allow initialization of an |
| 5413 | // array from a compound literal that creates an array of the same |
| 5414 | // type, so long as the initializer has no side effects. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5415 | if (!S.getLangOpts().CPlusPlus && Initializer && |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5416 | isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) && |
| 5417 | Initializer->getType()->isArrayType()) { |
| 5418 | const ArrayType *SourceAT |
| 5419 | = Context.getAsArrayType(Initializer->getType()); |
| 5420 | if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5421 | SetFailed(FK_ArrayTypeMismatch); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5422 | else if (Initializer->HasSideEffects(S.Context)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5423 | SetFailed(FK_NonConstantArrayInit); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5424 | else { |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 5425 | AddArrayInitStep(DestType, /*IsGNUExtension*/true); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5426 | } |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5427 | } |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5428 | // Note: as a GNU C++ extension, we allow list-initialization of a |
| 5429 | // class member of array type from a parenthesized initializer list. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5430 | else if (S.getLangOpts().CPlusPlus && |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5431 | Entity.getKind() == InitializedEntity::EK_Member && |
| 5432 | Initializer && isa<InitListExpr>(Initializer)) { |
| 5433 | TryListInitialization(S, Entity, Kind, cast<InitListExpr>(Initializer), |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 5434 | *this, TreatUnavailableAsInvalid); |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5435 | AddParenthesizedArrayInitStep(DestType); |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 5436 | } else if (DestAT->getElementType()->isCharType()) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5437 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 5438 | else if (IsWideCharCompatible(DestAT->getElementType(), Context)) |
| 5439 | SetFailed(FK_ArrayNeedsInitListOrWideStringLiteral); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5440 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5441 | SetFailed(FK_ArrayNeedsInitList); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5442 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5443 | return; |
| 5444 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5445 | |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 5446 | // Determine whether we should consider writeback conversions for |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5447 | // Objective-C ARC. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5448 | bool allowObjCWritebackConversion = S.getLangOpts().ObjCAutoRefCount && |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5449 | Entity.isParameterKind(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5450 | |
| 5451 | // We're at the end of the line for C: it's either a write-back conversion |
| 5452 | // 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] | 5453 | if (!S.getLangOpts().CPlusPlus) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5454 | // If allowed, check whether this is an Objective-C writeback conversion. |
| 5455 | if (allowObjCWritebackConversion && |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5456 | tryObjCWritebackConversion(S, *this, Entity, Initializer)) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5457 | return; |
| 5458 | } |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5459 | |
| 5460 | if (TryOCLSamplerInitialization(S, *this, DestType, Initializer)) |
| 5461 | return; |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5462 | |
| 5463 | if (TryOCLZeroEventInitialization(S, *this, DestType, Initializer)) |
| 5464 | return; |
| 5465 | |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 5466 | if (TryOCLZeroQueueInitialization(S, *this, DestType, Initializer)) |
| 5467 | return; |
| 5468 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5469 | // Handle initialization in C |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5470 | AddCAssignmentStep(DestType); |
| 5471 | MaybeProduceObjCObject(S, *this, Entity); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5472 | return; |
| 5473 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5474 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5475 | assert(S.getLangOpts().CPlusPlus); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 5476 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5477 | // - If the destination type is a (possibly cv-qualified) class type: |
| 5478 | if (DestType->isRecordType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5479 | // - If the initialization is direct-initialization, or if it is |
| 5480 | // copy-initialization where the cv-unqualified version of the |
| 5481 | // 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] | 5482 | // class of the destination, constructors are considered. [...] |
| 5483 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 5484 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 5485 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5486 | S.IsDerivedFrom(Initializer->getBeginLoc(), SourceType, DestType)))) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5487 | TryConstructorInitialization(S, Entity, Kind, Args, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5488 | DestType, DestType, *this); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5489 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5490 | // user-defined conversion sequences that can convert from the source |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5491 | // type to the destination type or (when a conversion function is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5492 | // used) to a derived class thereof are enumerated as described in |
| 5493 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 5494 | // (13.3). |
| 5495 | else |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5496 | TryUserDefinedConversion(S, DestType, Kind, Initializer, *this, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5497 | TopLevelOfInitList); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5498 | return; |
| 5499 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5500 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 5501 | assert(Args.size() >= 1 && "Zero-argument case handled above"); |
| 5502 | |
| 5503 | // The remaining cases all need a source type. |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5504 | if (Args.size() > 1) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5505 | SetFailed(FK_TooManyInitsForScalar); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5506 | return; |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 5507 | } else if (isa<InitListExpr>(Args[0])) { |
| 5508 | SetFailed(FK_ParenthesizedListInitForScalar); |
| 5509 | return; |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5510 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5511 | |
| 5512 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5513 | // type, conversion functions are considered. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5514 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5515 | // For a conversion to _Atomic(T) from either T or a class type derived |
| 5516 | // from T, initialize the T object then convert to _Atomic type. |
| 5517 | bool NeedAtomicConversion = false; |
| 5518 | if (const AtomicType *Atomic = DestType->getAs<AtomicType>()) { |
| 5519 | if (Context.hasSameUnqualifiedType(SourceType, Atomic->getValueType()) || |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5520 | S.IsDerivedFrom(Initializer->getBeginLoc(), SourceType, |
Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 5521 | Atomic->getValueType())) { |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5522 | DestType = Atomic->getValueType(); |
| 5523 | NeedAtomicConversion = true; |
| 5524 | } |
| 5525 | } |
| 5526 | |
| 5527 | TryUserDefinedConversion(S, DestType, Kind, Initializer, *this, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5528 | TopLevelOfInitList); |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5529 | MaybeProduceObjCObject(S, *this, Entity); |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5530 | if (!Failed() && NeedAtomicConversion) |
| 5531 | AddAtomicConversionStep(Entity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5532 | return; |
| 5533 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5534 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5535 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5536 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5537 | // conversions (Clause 4) will be used, if necessary, to convert the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5538 | // initializer expression to the cv-unqualified version of the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5539 | // destination type; no user-defined conversions are considered. |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5540 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5541 | ImplicitConversionSequence ICS |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5542 | = S.TryImplicitConversion(Initializer, DestType, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5543 | /*SuppressUserConversions*/true, |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 5544 | /*AllowExplicitConversions*/ false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 5545 | /*InOverloadResolution*/ false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5546 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 5547 | allowObjCWritebackConversion); |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5548 | |
| 5549 | if (ICS.isStandard() && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5550 | ICS.Standard.Second == ICK_Writeback_Conversion) { |
| 5551 | // Objective-C ARC writeback conversion. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5552 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5553 | // We should copy unless we're passing to an argument explicitly |
| 5554 | // marked 'out'. |
| 5555 | bool ShouldCopy = true; |
| 5556 | if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 5557 | ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5558 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5559 | // If there was an lvalue adjustment, add it as a separate conversion. |
| 5560 | if (ICS.Standard.First == ICK_Array_To_Pointer || |
| 5561 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 5562 | ImplicitConversionSequence LvalueICS; |
| 5563 | LvalueICS.setStandard(); |
| 5564 | LvalueICS.Standard.setAsIdentityConversion(); |
| 5565 | LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0)); |
| 5566 | LvalueICS.Standard.First = ICS.Standard.First; |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5567 | AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5568 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5569 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5570 | AddPassByIndirectCopyRestoreStep(DestType, ShouldCopy); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5571 | } else if (ICS.isBad()) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 5572 | DeclAccessPair dap; |
Richard Smith | f032001b | 2013-06-20 02:18:31 +0000 | [diff] [blame] | 5573 | if (isLibstdcxxPointerReturnFalseHack(S, Entity, Initializer)) { |
| 5574 | AddZeroInitializationStep(Entity.getType()); |
| 5575 | } else if (Initializer->getType() == Context.OverloadTy && |
| 5576 | !S.ResolveAddressOfOverloadedFunction(Initializer, DestType, |
| 5577 | false, dap)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5578 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 5579 | else if (Initializer->getType()->isFunctionType() && |
| 5580 | isExprAnUnaddressableFunction(S, Initializer)) |
| 5581 | SetFailed(InitializationSequence::FK_AddressOfUnaddressableFunction); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 5582 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5583 | SetFailed(InitializationSequence::FK_ConversionFailed); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5584 | } else { |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5585 | AddConversionSequenceStep(ICS, DestType, TopLevelOfInitList); |
John McCall | fa27234 | 2011-06-16 23:24:51 +0000 | [diff] [blame] | 5586 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5587 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 5588 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5589 | } |
| 5590 | |
| 5591 | InitializationSequence::~InitializationSequence() { |
Davide Italiano | 67bb9f7 | 2015-07-01 21:51:58 +0000 | [diff] [blame] | 5592 | for (auto &S : Steps) |
| 5593 | S.Destroy(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5594 | } |
| 5595 | |
| 5596 | //===----------------------------------------------------------------------===// |
| 5597 | // Perform initialization |
| 5598 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5599 | static Sema::AssignmentAction |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 5600 | getAssignmentAction(const InitializedEntity &Entity, bool Diagnose = false) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5601 | switch(Entity.getKind()) { |
| 5602 | case InitializedEntity::EK_Variable: |
| 5603 | case InitializedEntity::EK_New: |
Douglas Gregor | 6dd3a6a | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 5604 | case InitializedEntity::EK_Exception: |
| 5605 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 5606 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5607 | return Sema::AA_Initializing; |
| 5608 | |
| 5609 | case InitializedEntity::EK_Parameter: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5610 | if (Entity.getDecl() && |
Douglas Gregor | 6b7f12c | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 5611 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 5612 | return Sema::AA_Sending; |
| 5613 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5614 | return Sema::AA_Passing; |
| 5615 | |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 5616 | case InitializedEntity::EK_Parameter_CF_Audited: |
| 5617 | if (Entity.getDecl() && |
| 5618 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 5619 | return Sema::AA_Sending; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5620 | |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 5621 | return !Diagnose ? Sema::AA_Passing : Sema::AA_Passing_CFAudited; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5622 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5623 | case InitializedEntity::EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 5624 | case InitializedEntity::EK_StmtExprResult: // FIXME: Not quite right. |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5625 | return Sema::AA_Returning; |
| 5626 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5627 | case InitializedEntity::EK_Temporary: |
Fariborz Jahanian | 14e9541 | 2013-07-11 19:13:34 +0000 | [diff] [blame] | 5628 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5629 | // FIXME: Can we tell apart casting vs. converting? |
| 5630 | return Sema::AA_Casting; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5631 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5632 | case InitializedEntity::EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 5633 | case InitializedEntity::EK_Binding: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 5634 | case InitializedEntity::EK_ArrayElement: |
| 5635 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 5636 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 5637 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 5638 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 5639 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5640 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5641 | return Sema::AA_Initializing; |
| 5642 | } |
| 5643 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 5644 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5645 | } |
| 5646 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5647 | /// Whether we should bind a created object as a temporary when |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5648 | /// initializing the given entity. |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5649 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5650 | switch (Entity.getKind()) { |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 5651 | case InitializedEntity::EK_ArrayElement: |
| 5652 | case InitializedEntity::EK_Member: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5653 | case InitializedEntity::EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 5654 | case InitializedEntity::EK_StmtExprResult: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5655 | case InitializedEntity::EK_New: |
| 5656 | case InitializedEntity::EK_Variable: |
| 5657 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 5658 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 5659 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 5660 | case InitializedEntity::EK_ComplexElement: |
Anders Carlsson | fcd764a | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 5661 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 5662 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 5663 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 5664 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5665 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5666 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5667 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5668 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5669 | case InitializedEntity::EK_Parameter_CF_Audited: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5670 | case InitializedEntity::EK_Temporary: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5671 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 5672 | case InitializedEntity::EK_Binding: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5673 | return true; |
| 5674 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5675 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5676 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 5677 | } |
| 5678 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5679 | /// Whether the given entity, when initialized with an object |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5680 | /// created for that initialization, requires destruction. |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5681 | static bool shouldDestroyEntity(const InitializedEntity &Entity) { |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5682 | switch (Entity.getKind()) { |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5683 | case InitializedEntity::EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 5684 | case InitializedEntity::EK_StmtExprResult: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5685 | case InitializedEntity::EK_New: |
| 5686 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 5687 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5688 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 5689 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 5690 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 5691 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 5692 | case InitializedEntity::EK_LambdaCapture: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5693 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5694 | |
Richard Smith | 27874d6 | 2013-01-08 00:08:23 +0000 | [diff] [blame] | 5695 | case InitializedEntity::EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 5696 | case InitializedEntity::EK_Binding: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5697 | case InitializedEntity::EK_Variable: |
| 5698 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5699 | case InitializedEntity::EK_Parameter_CF_Audited: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5700 | case InitializedEntity::EK_Temporary: |
| 5701 | case InitializedEntity::EK_ArrayElement: |
| 5702 | case InitializedEntity::EK_Exception: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5703 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5704 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5705 | return true; |
| 5706 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5707 | |
| 5708 | llvm_unreachable("missed an InitializedEntity kind?"); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5709 | } |
| 5710 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5711 | /// Get the location at which initialization diagnostics should appear. |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5712 | static SourceLocation getInitializationLoc(const InitializedEntity &Entity, |
| 5713 | Expr *Initializer) { |
| 5714 | switch (Entity.getKind()) { |
| 5715 | case InitializedEntity::EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 5716 | case InitializedEntity::EK_StmtExprResult: |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5717 | return Entity.getReturnLoc(); |
| 5718 | |
| 5719 | case InitializedEntity::EK_Exception: |
| 5720 | return Entity.getThrowLoc(); |
| 5721 | |
| 5722 | case InitializedEntity::EK_Variable: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 5723 | case InitializedEntity::EK_Binding: |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5724 | return Entity.getDecl()->getLocation(); |
| 5725 | |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 5726 | case InitializedEntity::EK_LambdaCapture: |
| 5727 | return Entity.getCaptureLoc(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5728 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5729 | case InitializedEntity::EK_ArrayElement: |
| 5730 | case InitializedEntity::EK_Member: |
| 5731 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5732 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5733 | case InitializedEntity::EK_Temporary: |
| 5734 | case InitializedEntity::EK_New: |
| 5735 | case InitializedEntity::EK_Base: |
| 5736 | case InitializedEntity::EK_Delegating: |
| 5737 | case InitializedEntity::EK_VectorElement: |
| 5738 | case InitializedEntity::EK_ComplexElement: |
| 5739 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 5740 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5741 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5742 | case InitializedEntity::EK_RelatedResult: |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5743 | return Initializer->getBeginLoc(); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5744 | } |
| 5745 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 5746 | } |
| 5747 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5748 | /// Make a (potentially elidable) temporary copy of the object |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5749 | /// provided by the given initializer by calling the appropriate copy |
| 5750 | /// constructor. |
| 5751 | /// |
| 5752 | /// \param S The Sema object used for type-checking. |
| 5753 | /// |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 5754 | /// \param T The type of the temporary object, which must either be |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5755 | /// the type of the initializer expression or a superclass thereof. |
| 5756 | /// |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5757 | /// \param Entity The entity being initialized. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5758 | /// |
| 5759 | /// \param CurInit The initializer expression. |
| 5760 | /// |
| 5761 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 5762 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 5763 | /// an rvalue. |
| 5764 | /// |
| 5765 | /// \returns An expression that copies the initializer expression into |
| 5766 | /// a temporary object, or an error expression if a copy could not be |
| 5767 | /// created. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5768 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 5769 | QualType T, |
| 5770 | const InitializedEntity &Entity, |
| 5771 | ExprResult CurInit, |
| 5772 | bool IsExtraneousCopy) { |
Fariborz Jahanian | 36f7f13 | 2015-01-28 22:08:10 +0000 | [diff] [blame] | 5773 | if (CurInit.isInvalid()) |
| 5774 | return CurInit; |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5775 | // Determine which class type we're copying to. |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 5776 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5777 | CXXRecordDecl *Class = nullptr; |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5778 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5779 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 5780 | if (!Class) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5781 | return CurInit; |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5782 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5783 | SourceLocation Loc = getInitializationLoc(Entity, CurInit.get()); |
Douglas Gregor | d5c231e | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 5784 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5785 | // Make sure that the type we are copying is complete. |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5786 | if (S.RequireCompleteType(Loc, T, diag::err_temp_copy_incomplete)) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5787 | return CurInit; |
Douglas Gregor | d5c231e | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 5788 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5789 | // Perform overload resolution using the class's constructors. Per |
| 5790 | // C++11 [dcl.init]p16, second bullet for class types, this initialization |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5791 | // is direct-initialization. |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 5792 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5793 | DeclContext::lookup_result Ctors = S.LookupConstructors(Class); |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5794 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5795 | OverloadCandidateSet::iterator Best; |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5796 | switch (ResolveConstructorOverload( |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 5797 | S, Loc, CurInitExpr, CandidateSet, T, Ctors, Best, |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5798 | /*CopyInitializing=*/false, /*AllowExplicit=*/true, |
| 5799 | /*OnlyListConstructors=*/false, /*IsListInit=*/false, |
| 5800 | /*SecondStepOfCopyInit=*/true)) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5801 | case OR_Success: |
| 5802 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5803 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5804 | case OR_No_Viable_Function: |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 5805 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 5806 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 5807 | : diag::err_temp_copy_no_viable) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 5808 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5809 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5810 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 5811 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5812 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5813 | return CurInit; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5814 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5815 | case OR_Ambiguous: |
| 5816 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 5817 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5818 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5819 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5820 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5821 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5822 | case OR_Deleted: |
| 5823 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 5824 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5825 | << CurInitExpr->getSourceRange(); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5826 | S.NoteDeletedFunction(Best->Function); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5827 | return ExprError(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5828 | } |
| 5829 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5830 | bool HadMultipleCandidates = CandidateSet.size() > 1; |
| 5831 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5832 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 5833 | SmallVector<Expr*, 8> ConstructorArgs; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5834 | CurInit.get(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5835 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 5836 | S.CheckConstructorAccess(Loc, Constructor, Best->FoundDecl, Entity, |
| 5837 | IsExtraneousCopy); |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5838 | |
| 5839 | if (IsExtraneousCopy) { |
| 5840 | // If this is a totally extraneous copy for C++03 reference |
| 5841 | // binding purposes, just return the original initialization |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 5842 | // expression. We don't generate an (elided) copy operation here |
| 5843 | // because doing so would require us to pass down a flag to avoid |
| 5844 | // infinite recursion, where each step adds another extraneous, |
| 5845 | // elidable copy. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5846 | |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 5847 | // Instantiate the default arguments of any extra parameters in |
| 5848 | // the selected copy constructor, as if we were going to create a |
| 5849 | // proper call to the copy constructor. |
| 5850 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 5851 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 5852 | if (S.RequireCompleteType(Loc, Parm->getType(), |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5853 | diag::err_call_incomplete_argument)) |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 5854 | break; |
| 5855 | |
| 5856 | // Build the default argument expression; we don't actually care |
| 5857 | // if this succeeds or not, because this routine will complain |
| 5858 | // if there was a problem. |
| 5859 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 5860 | } |
| 5861 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5862 | return CurInitExpr; |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5863 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5864 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5865 | // Determine the arguments required to actually perform the |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5866 | // constructor call (we might have derived-to-base conversions, or |
| 5867 | // the copy constructor may have default arguments). |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5868 | if (S.CompleteConstructorCall(Constructor, CurInitExpr, Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5869 | return ExprError(); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5870 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5871 | // C++0x [class.copy]p32: |
| 5872 | // When certain criteria are met, an implementation is allowed to |
| 5873 | // omit the copy/move construction of a class object, even if the |
| 5874 | // copy/move constructor and/or destructor for the object have |
| 5875 | // side effects. [...] |
| 5876 | // - when a temporary class object that has not been bound to a |
| 5877 | // reference (12.2) would be copied/moved to a class object |
| 5878 | // with the same cv-unqualified type, the copy/move operation |
| 5879 | // can be omitted by constructing the temporary object |
| 5880 | // directly into the target of the omitted copy/move |
| 5881 | // |
| 5882 | // Note that the other three bullets are handled elsewhere. Copy |
| 5883 | // elision for return statements and throw expressions are handled as part |
| 5884 | // of constructor initialization, while copy elision for exception handlers |
| 5885 | // is handled by the run-time. |
| 5886 | // |
| 5887 | // FIXME: If the function parameter is not the same type as the temporary, we |
| 5888 | // should still be able to elide the copy, but we don't have a way to |
| 5889 | // represent in the AST how much should be elided in this case. |
| 5890 | bool Elidable = |
| 5891 | CurInitExpr->isTemporaryObject(S.Context, Class) && |
| 5892 | S.Context.hasSameUnqualifiedType( |
| 5893 | Best->Function->getParamDecl(0)->getType().getNonReferenceType(), |
| 5894 | CurInitExpr->getType()); |
| 5895 | |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 5896 | // Actually perform the constructor call. |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 5897 | CurInit = S.BuildCXXConstructExpr(Loc, T, Best->FoundDecl, Constructor, |
| 5898 | Elidable, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5899 | ConstructorArgs, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5900 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5901 | /*ListInit*/ false, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 5902 | /*StdInitListInit*/ false, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 5903 | /*ZeroInit*/ false, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5904 | CXXConstructExpr::CK_Complete, |
| 5905 | SourceRange()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5906 | |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 5907 | // If we're supposed to bind temporaries, do so. |
| 5908 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5909 | CurInit = S.MaybeBindToTemporary(CurInit.getAs<Expr>()); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5910 | return CurInit; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5911 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5912 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5913 | /// Check whether elidable copy construction for binding a reference to |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5914 | /// a temporary would have succeeded if we were building in C++98 mode, for |
| 5915 | /// -Wc++98-compat. |
| 5916 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 5917 | const InitializedEntity &Entity, |
| 5918 | Expr *CurInitExpr) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5919 | assert(S.getLangOpts().CPlusPlus11); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5920 | |
| 5921 | const RecordType *Record = CurInitExpr->getType()->getAs<RecordType>(); |
| 5922 | if (!Record) |
| 5923 | return; |
| 5924 | |
| 5925 | SourceLocation Loc = getInitializationLoc(Entity, CurInitExpr); |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 5926 | if (S.Diags.isIgnored(diag::warn_cxx98_compat_temp_copy, Loc)) |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5927 | return; |
| 5928 | |
| 5929 | // Find constructors which would have been considered. |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 5930 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5931 | DeclContext::lookup_result Ctors = |
| 5932 | S.LookupConstructors(cast<CXXRecordDecl>(Record->getDecl())); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5933 | |
| 5934 | // Perform overload resolution. |
| 5935 | OverloadCandidateSet::iterator Best; |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5936 | OverloadingResult OR = ResolveConstructorOverload( |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 5937 | S, Loc, CurInitExpr, CandidateSet, CurInitExpr->getType(), Ctors, Best, |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5938 | /*CopyInitializing=*/false, /*AllowExplicit=*/true, |
| 5939 | /*OnlyListConstructors=*/false, /*IsListInit=*/false, |
| 5940 | /*SecondStepOfCopyInit=*/true); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5941 | |
| 5942 | PartialDiagnostic Diag = S.PDiag(diag::warn_cxx98_compat_temp_copy) |
| 5943 | << OR << (int)Entity.getKind() << CurInitExpr->getType() |
| 5944 | << CurInitExpr->getSourceRange(); |
| 5945 | |
| 5946 | switch (OR) { |
| 5947 | case OR_Success: |
| 5948 | S.CheckConstructorAccess(Loc, cast<CXXConstructorDecl>(Best->Function), |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 5949 | Best->FoundDecl, Entity, Diag); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5950 | // FIXME: Check default arguments as far as that's possible. |
| 5951 | break; |
| 5952 | |
| 5953 | case OR_No_Viable_Function: |
| 5954 | S.Diag(Loc, Diag); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5955 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5956 | break; |
| 5957 | |
| 5958 | case OR_Ambiguous: |
| 5959 | S.Diag(Loc, Diag); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5960 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5961 | break; |
| 5962 | |
| 5963 | case OR_Deleted: |
| 5964 | S.Diag(Loc, Diag); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5965 | S.NoteDeletedFunction(Best->Function); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5966 | break; |
| 5967 | } |
| 5968 | } |
| 5969 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5970 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 5971 | const InitializedEntity &Entity) { |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5972 | if (Entity.isParameterKind() && Entity.getDecl()) { |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5973 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 5974 | return; |
| 5975 | |
| 5976 | if (Entity.getDecl()->getDeclName()) |
| 5977 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 5978 | << Entity.getDecl()->getDeclName(); |
| 5979 | else |
| 5980 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 5981 | } |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5982 | else if (Entity.getKind() == InitializedEntity::EK_RelatedResult && |
| 5983 | Entity.getMethodDecl()) |
| 5984 | S.Diag(Entity.getMethodDecl()->getLocation(), |
| 5985 | diag::note_method_return_type_change) |
| 5986 | << Entity.getMethodDecl()->getDeclName(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5987 | } |
| 5988 | |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5989 | /// Returns true if the parameters describe a constructor initialization of |
| 5990 | /// an explicit temporary object, e.g. "Point(x, y)". |
| 5991 | static bool isExplicitTemporary(const InitializedEntity &Entity, |
| 5992 | const InitializationKind &Kind, |
| 5993 | unsigned NumArgs) { |
| 5994 | switch (Entity.getKind()) { |
| 5995 | case InitializedEntity::EK_Temporary: |
| 5996 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5997 | case InitializedEntity::EK_RelatedResult: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5998 | break; |
| 5999 | default: |
| 6000 | return false; |
| 6001 | } |
| 6002 | |
| 6003 | switch (Kind.getKind()) { |
| 6004 | case InitializationKind::IK_DirectList: |
| 6005 | return true; |
| 6006 | // FIXME: Hack to work around cast weirdness. |
| 6007 | case InitializationKind::IK_Direct: |
| 6008 | case InitializationKind::IK_Value: |
| 6009 | return NumArgs != 1; |
| 6010 | default: |
| 6011 | return false; |
| 6012 | } |
| 6013 | } |
| 6014 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6015 | static ExprResult |
| 6016 | PerformConstructorInitialization(Sema &S, |
| 6017 | const InitializedEntity &Entity, |
| 6018 | const InitializationKind &Kind, |
| 6019 | MultiExprArg Args, |
| 6020 | const InitializationSequence::Step& Step, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6021 | bool &ConstructorInitRequiresZeroInit, |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 6022 | bool IsListInitialization, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6023 | bool IsStdInitListInitialization, |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 6024 | SourceLocation LBraceLoc, |
| 6025 | SourceLocation RBraceLoc) { |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6026 | unsigned NumArgs = Args.size(); |
| 6027 | CXXConstructorDecl *Constructor |
| 6028 | = cast<CXXConstructorDecl>(Step.Function.Function); |
| 6029 | bool HadMultipleCandidates = Step.Function.HadMultipleCandidates; |
| 6030 | |
| 6031 | // Build a call to the selected constructor. |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 6032 | SmallVector<Expr*, 8> ConstructorArgs; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6033 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 6034 | ? Kind.getEqualLoc() |
| 6035 | : Kind.getLocation(); |
| 6036 | |
| 6037 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 6038 | // Force even a trivial, implicit default constructor to be |
| 6039 | // semantically checked. We do this explicitly because we don't build |
| 6040 | // the definition for completely trivial constructors. |
Matt Beaumont-Gay | 47ff122 | 2012-02-24 08:37:56 +0000 | [diff] [blame] | 6041 | assert(Constructor->getParent() && "No parent class for constructor."); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6042 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Douglas Gregor | f704ade | 2012-02-24 07:48:37 +0000 | [diff] [blame] | 6043 | Constructor->isTrivial() && !Constructor->isUsed(false)) |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6044 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 6045 | } |
| 6046 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6047 | ExprResult CurInit((Expr *)nullptr); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6048 | |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6049 | // C++ [over.match.copy]p1: |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6050 | // - When initializing a temporary to be bound to the first parameter |
| 6051 | // of a constructor that takes a reference to possibly cv-qualified |
| 6052 | // T as its first argument, called with a single argument in the |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6053 | // context of direct-initialization, explicit conversion functions |
| 6054 | // are also considered. |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 6055 | bool AllowExplicitConv = |
| 6056 | Kind.AllowExplicit() && !Kind.isCopyInit() && Args.size() == 1 && |
| 6057 | hasCopyOrMoveCtorParam(S.Context, |
| 6058 | getConstructorInfo(Step.Function.FoundDecl)); |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6059 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6060 | // Determine the arguments required to actually perform the constructor |
| 6061 | // call. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6062 | if (S.CompleteConstructorCall(Constructor, Args, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6063 | Loc, ConstructorArgs, |
Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 6064 | AllowExplicitConv, |
| 6065 | IsListInitialization)) |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6066 | return ExprError(); |
| 6067 | |
| 6068 | |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 6069 | if (isExplicitTemporary(Entity, Kind, NumArgs)) { |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6070 | // An explicitly-constructed temporary, e.g., X(1, 2). |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 6071 | if (S.DiagnoseUseOfDecl(Constructor, Loc)) |
| 6072 | return ExprError(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6073 | |
| 6074 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 6075 | if (!TSInfo) |
| 6076 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 6077 | SourceRange ParenOrBraceRange = Kind.getParenOrBraceRange(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6078 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6079 | if (auto *Shadow = dyn_cast<ConstructorUsingShadowDecl>( |
Richard Smith | 80a4702 | 2016-06-29 01:10:27 +0000 | [diff] [blame] | 6080 | Step.Function.FoundDecl.getDecl())) { |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6081 | Constructor = S.findInheritingConstructor(Loc, Constructor, Shadow); |
Richard Smith | 80a4702 | 2016-06-29 01:10:27 +0000 | [diff] [blame] | 6082 | if (S.DiagnoseUseOfDecl(Constructor, Loc)) |
| 6083 | return ExprError(); |
| 6084 | } |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6085 | S.MarkFunctionReferenced(Loc, Constructor); |
| 6086 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6087 | CurInit = new (S.Context) CXXTemporaryObjectExpr( |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 6088 | S.Context, Constructor, |
| 6089 | Entity.getType().getNonLValueExprType(S.Context), TSInfo, |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 6090 | ConstructorArgs, ParenOrBraceRange, HadMultipleCandidates, |
| 6091 | IsListInitialization, IsStdInitListInitialization, |
| 6092 | ConstructorInitRequiresZeroInit); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6093 | } else { |
| 6094 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 6095 | CXXConstructExpr::CK_Complete; |
| 6096 | |
| 6097 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 6098 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
| 6099 | CXXConstructExpr::CK_VirtualBase : |
| 6100 | CXXConstructExpr::CK_NonVirtualBase; |
| 6101 | } else if (Entity.getKind() == InitializedEntity::EK_Delegating) { |
| 6102 | ConstructKind = CXXConstructExpr::CK_Delegating; |
| 6103 | } |
| 6104 | |
Peter Collingbourne | b8d17e7 | 2014-02-22 02:59:41 +0000 | [diff] [blame] | 6105 | // Only get the parenthesis or brace range if it is a list initialization or |
| 6106 | // direct construction. |
| 6107 | SourceRange ParenOrBraceRange; |
| 6108 | if (IsListInitialization) |
| 6109 | ParenOrBraceRange = SourceRange(LBraceLoc, RBraceLoc); |
| 6110 | else if (Kind.getKind() == InitializationKind::IK_Direct) |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 6111 | ParenOrBraceRange = Kind.getParenOrBraceRange(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6112 | |
| 6113 | // If the entity allows NRVO, mark the construction as elidable |
| 6114 | // unconditionally. |
| 6115 | if (Entity.allowsNRVO()) |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 6116 | CurInit = S.BuildCXXConstructExpr(Loc, Step.Type, |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 6117 | Step.Function.FoundDecl, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6118 | Constructor, /*Elidable=*/true, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6119 | ConstructorArgs, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6120 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6121 | IsListInitialization, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6122 | IsStdInitListInitialization, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6123 | ConstructorInitRequiresZeroInit, |
| 6124 | ConstructKind, |
Peter Collingbourne | b8d17e7 | 2014-02-22 02:59:41 +0000 | [diff] [blame] | 6125 | ParenOrBraceRange); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6126 | else |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 6127 | CurInit = S.BuildCXXConstructExpr(Loc, Step.Type, |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 6128 | Step.Function.FoundDecl, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6129 | Constructor, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6130 | ConstructorArgs, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6131 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6132 | IsListInitialization, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6133 | IsStdInitListInitialization, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6134 | ConstructorInitRequiresZeroInit, |
| 6135 | ConstructKind, |
Peter Collingbourne | b8d17e7 | 2014-02-22 02:59:41 +0000 | [diff] [blame] | 6136 | ParenOrBraceRange); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6137 | } |
| 6138 | if (CurInit.isInvalid()) |
| 6139 | return ExprError(); |
| 6140 | |
| 6141 | // Only check access if all of that succeeded. |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6142 | S.CheckConstructorAccess(Loc, Constructor, Step.Function.FoundDecl, Entity); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 6143 | if (S.DiagnoseUseOfDecl(Step.Function.FoundDecl, Loc)) |
| 6144 | return ExprError(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6145 | |
| 6146 | if (shouldBindAsTemporary(Entity)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6147 | CurInit = S.MaybeBindToTemporary(CurInit.get()); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6148 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6149 | return CurInit; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6150 | } |
| 6151 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6152 | namespace { |
| 6153 | enum LifetimeKind { |
| 6154 | /// The lifetime of a temporary bound to this entity ends at the end of the |
| 6155 | /// full-expression, and that's (probably) fine. |
| 6156 | LK_FullExpression, |
| 6157 | |
| 6158 | /// The lifetime of a temporary bound to this entity is extended to the |
| 6159 | /// lifeitme of the entity itself. |
| 6160 | LK_Extended, |
| 6161 | |
| 6162 | /// The lifetime of a temporary bound to this entity probably ends too soon, |
| 6163 | /// because the entity is allocated in a new-expression. |
| 6164 | LK_New, |
| 6165 | |
| 6166 | /// The lifetime of a temporary bound to this entity ends too soon, because |
| 6167 | /// the entity is a return object. |
| 6168 | LK_Return, |
| 6169 | |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 6170 | /// The lifetime of a temporary bound to this entity ends too soon, because |
| 6171 | /// the entity is the result of a statement expression. |
| 6172 | LK_StmtExprResult, |
| 6173 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6174 | /// This is a mem-initializer: if it would extend a temporary (other than via |
| 6175 | /// a default member initializer), the program is ill-formed. |
| 6176 | LK_MemInitializer, |
| 6177 | }; |
| 6178 | using LifetimeResult = |
| 6179 | llvm::PointerIntPair<const InitializedEntity *, 3, LifetimeKind>; |
| 6180 | } |
| 6181 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6182 | /// Determine the declaration which an initialized entity ultimately refers to, |
| 6183 | /// for the purpose of lifetime-extending a temporary bound to a reference in |
| 6184 | /// the initialization of \p Entity. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6185 | static LifetimeResult getEntityLifetime( |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 6186 | const InitializedEntity *Entity, |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6187 | const InitializedEntity *InitField = nullptr) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6188 | // C++11 [class.temporary]p5: |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 6189 | switch (Entity->getKind()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6190 | case InitializedEntity::EK_Variable: |
| 6191 | // The temporary [...] persists for the lifetime of the reference |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6192 | return {Entity, LK_Extended}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6193 | |
| 6194 | case InitializedEntity::EK_Member: |
| 6195 | // For subobjects, we look at the complete object. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 6196 | if (Entity->getParent()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6197 | return getEntityLifetime(Entity->getParent(), Entity); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6198 | |
| 6199 | // except: |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6200 | // C++17 [class.base.init]p8: |
| 6201 | // A temporary expression bound to a reference member in a |
| 6202 | // mem-initializer is ill-formed. |
| 6203 | // C++17 [class.base.init]p11: |
| 6204 | // A temporary expression bound to a reference member from a |
| 6205 | // default member initializer is ill-formed. |
| 6206 | // |
| 6207 | // The context of p11 and its example suggest that it's only the use of a |
| 6208 | // default member initializer from a constructor that makes the program |
| 6209 | // ill-formed, not its mere existence, and that it can even be used by |
| 6210 | // aggregate initialization. |
| 6211 | return {Entity, Entity->isDefaultMemberInitializer() ? LK_Extended |
| 6212 | : LK_MemInitializer}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6213 | |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 6214 | case InitializedEntity::EK_Binding: |
Richard Smith | 3997b1b | 2016-08-12 01:55:21 +0000 | [diff] [blame] | 6215 | // Per [dcl.decomp]p3, the binding is treated as a variable of reference |
| 6216 | // type. |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6217 | return {Entity, LK_Extended}; |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 6218 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6219 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6220 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6221 | // -- A temporary bound to a reference parameter in a function call |
| 6222 | // persists until the completion of the full-expression containing |
| 6223 | // the call. |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6224 | return {nullptr, LK_FullExpression}; |
| 6225 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6226 | case InitializedEntity::EK_Result: |
| 6227 | // -- The lifetime of a temporary bound to the returned value in a |
| 6228 | // function return statement is not extended; the temporary is |
| 6229 | // destroyed at the end of the full-expression in the return statement. |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6230 | return {nullptr, LK_Return}; |
| 6231 | |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 6232 | case InitializedEntity::EK_StmtExprResult: |
| 6233 | // FIXME: Should we lifetime-extend through the result of a statement |
| 6234 | // expression? |
| 6235 | return {nullptr, LK_StmtExprResult}; |
| 6236 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6237 | case InitializedEntity::EK_New: |
| 6238 | // -- A temporary bound to a reference in a new-initializer persists |
| 6239 | // until the completion of the full-expression containing the |
| 6240 | // new-initializer. |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6241 | return {nullptr, LK_New}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6242 | |
| 6243 | case InitializedEntity::EK_Temporary: |
| 6244 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 6245 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6246 | // We don't yet know the storage duration of the surrounding temporary. |
| 6247 | // Assume it's got full-expression duration for now, it will patch up our |
| 6248 | // storage duration if that's not correct. |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6249 | return {nullptr, LK_FullExpression}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6250 | |
| 6251 | case InitializedEntity::EK_ArrayElement: |
| 6252 | // For subobjects, we look at the complete object. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6253 | return getEntityLifetime(Entity->getParent(), InitField); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6254 | |
| 6255 | case InitializedEntity::EK_Base: |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 6256 | // For subobjects, we look at the complete object. |
| 6257 | if (Entity->getParent()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6258 | return getEntityLifetime(Entity->getParent(), InitField); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6259 | return {InitField, LK_MemInitializer}; |
| 6260 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6261 | case InitializedEntity::EK_Delegating: |
| 6262 | // We can reach this case for aggregate initialization in a constructor: |
| 6263 | // struct A { int &&r; }; |
| 6264 | // struct B : A { B() : A{0} {} }; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6265 | // In this case, use the outermost field decl as the context. |
| 6266 | return {InitField, LK_MemInitializer}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6267 | |
| 6268 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 6269 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6270 | case InitializedEntity::EK_LambdaCapture: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6271 | case InitializedEntity::EK_VectorElement: |
| 6272 | case InitializedEntity::EK_ComplexElement: |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6273 | return {nullptr, LK_FullExpression}; |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6274 | |
| 6275 | case InitializedEntity::EK_Exception: |
| 6276 | // FIXME: Can we diagnose lifetime problems with exceptions? |
| 6277 | return {nullptr, LK_FullExpression}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6278 | } |
Benjamin Kramer | cabc882 | 2013-06-05 15:37:50 +0000 | [diff] [blame] | 6279 | llvm_unreachable("unknown entity kind"); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6280 | } |
| 6281 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6282 | namespace { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6283 | enum ReferenceKind { |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6284 | /// Lifetime would be extended by a reference binding to a temporary. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6285 | RK_ReferenceBinding, |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6286 | /// Lifetime would be extended by a std::initializer_list object binding to |
| 6287 | /// its backing array. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6288 | RK_StdInitializerList, |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6289 | }; |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6290 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6291 | /// A temporary or local variable. This will be one of: |
| 6292 | /// * A MaterializeTemporaryExpr. |
| 6293 | /// * A DeclRefExpr whose declaration is a local. |
| 6294 | /// * An AddrLabelExpr. |
| 6295 | /// * A BlockExpr for a block with captures. |
| 6296 | using Local = Expr*; |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6297 | |
| 6298 | /// Expressions we stepped over when looking for the local state. Any steps |
| 6299 | /// that would inhibit lifetime extension or take us out of subexpressions of |
| 6300 | /// the initializer are included. |
| 6301 | struct IndirectLocalPathEntry { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6302 | enum EntryKind { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6303 | DefaultInit, |
| 6304 | AddressOf, |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6305 | VarInit, |
| 6306 | LValToRVal, |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6307 | LifetimeBoundCall, |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6308 | } Kind; |
| 6309 | Expr *E; |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6310 | const Decl *D = nullptr; |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6311 | IndirectLocalPathEntry() {} |
| 6312 | IndirectLocalPathEntry(EntryKind K, Expr *E) : Kind(K), E(E) {} |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6313 | IndirectLocalPathEntry(EntryKind K, Expr *E, const Decl *D) |
| 6314 | : Kind(K), E(E), D(D) {} |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6315 | }; |
| 6316 | |
| 6317 | using IndirectLocalPath = llvm::SmallVectorImpl<IndirectLocalPathEntry>; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6318 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6319 | struct RevertToOldSizeRAII { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6320 | IndirectLocalPath &Path; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6321 | unsigned OldSize = Path.size(); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6322 | RevertToOldSizeRAII(IndirectLocalPath &Path) : Path(Path) {} |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6323 | ~RevertToOldSizeRAII() { Path.resize(OldSize); } |
| 6324 | }; |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6325 | |
| 6326 | using LocalVisitor = llvm::function_ref<bool(IndirectLocalPath &Path, Local L, |
| 6327 | ReferenceKind RK)>; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6328 | } |
| 6329 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6330 | static bool isVarOnPath(IndirectLocalPath &Path, VarDecl *VD) { |
| 6331 | for (auto E : Path) |
| 6332 | if (E.Kind == IndirectLocalPathEntry::VarInit && E.D == VD) |
| 6333 | return true; |
| 6334 | return false; |
| 6335 | } |
| 6336 | |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6337 | static bool pathContainsInit(IndirectLocalPath &Path) { |
| 6338 | return std::any_of(Path.begin(), Path.end(), [=](IndirectLocalPathEntry E) { |
| 6339 | return E.Kind == IndirectLocalPathEntry::DefaultInit || |
| 6340 | E.Kind == IndirectLocalPathEntry::VarInit; |
| 6341 | }); |
| 6342 | } |
| 6343 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6344 | static void visitLocalsRetainedByInitializer(IndirectLocalPath &Path, |
| 6345 | Expr *Init, LocalVisitor Visit, |
| 6346 | bool RevisitSubinits); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6347 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6348 | static void visitLocalsRetainedByReferenceBinding(IndirectLocalPath &Path, |
| 6349 | Expr *Init, ReferenceKind RK, |
| 6350 | LocalVisitor Visit); |
| 6351 | |
| 6352 | static bool implicitObjectParamIsLifetimeBound(const FunctionDecl *FD) { |
| 6353 | const TypeSourceInfo *TSI = FD->getTypeSourceInfo(); |
| 6354 | if (!TSI) |
| 6355 | return false; |
Martin Storsjo | d03fa99 | 2018-08-02 18:12:08 +0000 | [diff] [blame] | 6356 | // Don't declare this variable in the second operand of the for-statement; |
| 6357 | // GCC miscompiles that by ending its lifetime before evaluating the |
| 6358 | // third operand. See gcc.gnu.org/PR86769. |
| 6359 | AttributedTypeLoc ATL; |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6360 | for (TypeLoc TL = TSI->getTypeLoc(); |
Martin Storsjo | d03fa99 | 2018-08-02 18:12:08 +0000 | [diff] [blame] | 6361 | (ATL = TL.getAsAdjusted<AttributedTypeLoc>()); |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6362 | TL = ATL.getModifiedLoc()) { |
Reid Kleckner | 11f9f8a | 2018-08-14 01:55:37 +0000 | [diff] [blame] | 6363 | if (ATL.getAttrKind() == AttributedType::attr_lifetimebound) |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6364 | return true; |
| 6365 | } |
| 6366 | return false; |
| 6367 | } |
| 6368 | |
| 6369 | static void visitLifetimeBoundArguments(IndirectLocalPath &Path, Expr *Call, |
| 6370 | LocalVisitor Visit) { |
| 6371 | const FunctionDecl *Callee; |
| 6372 | ArrayRef<Expr*> Args; |
| 6373 | |
| 6374 | if (auto *CE = dyn_cast<CallExpr>(Call)) { |
| 6375 | Callee = CE->getDirectCallee(); |
| 6376 | Args = llvm::makeArrayRef(CE->getArgs(), CE->getNumArgs()); |
| 6377 | } else { |
| 6378 | auto *CCE = cast<CXXConstructExpr>(Call); |
| 6379 | Callee = CCE->getConstructor(); |
| 6380 | Args = llvm::makeArrayRef(CCE->getArgs(), CCE->getNumArgs()); |
| 6381 | } |
| 6382 | if (!Callee) |
| 6383 | return; |
| 6384 | |
| 6385 | Expr *ObjectArg = nullptr; |
| 6386 | if (isa<CXXOperatorCallExpr>(Call) && Callee->isCXXInstanceMember()) { |
| 6387 | ObjectArg = Args[0]; |
| 6388 | Args = Args.slice(1); |
| 6389 | } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(Call)) { |
| 6390 | ObjectArg = MCE->getImplicitObjectArgument(); |
| 6391 | } |
| 6392 | |
| 6393 | auto VisitLifetimeBoundArg = [&](const Decl *D, Expr *Arg) { |
| 6394 | Path.push_back({IndirectLocalPathEntry::LifetimeBoundCall, Arg, D}); |
| 6395 | if (Arg->isGLValue()) |
| 6396 | visitLocalsRetainedByReferenceBinding(Path, Arg, RK_ReferenceBinding, |
| 6397 | Visit); |
| 6398 | else |
| 6399 | visitLocalsRetainedByInitializer(Path, Arg, Visit, true); |
| 6400 | Path.pop_back(); |
| 6401 | }; |
| 6402 | |
| 6403 | if (ObjectArg && implicitObjectParamIsLifetimeBound(Callee)) |
| 6404 | VisitLifetimeBoundArg(Callee, ObjectArg); |
| 6405 | |
| 6406 | for (unsigned I = 0, |
| 6407 | N = std::min<unsigned>(Callee->getNumParams(), Args.size()); |
| 6408 | I != N; ++I) { |
| 6409 | if (Callee->getParamDecl(I)->hasAttr<LifetimeBoundAttr>()) |
| 6410 | VisitLifetimeBoundArg(Callee->getParamDecl(I), Args[I]); |
| 6411 | } |
| 6412 | } |
| 6413 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6414 | /// Visit the locals that would be reachable through a reference bound to the |
| 6415 | /// glvalue expression \c Init. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6416 | static void visitLocalsRetainedByReferenceBinding(IndirectLocalPath &Path, |
| 6417 | Expr *Init, ReferenceKind RK, |
| 6418 | LocalVisitor Visit) { |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6419 | RevertToOldSizeRAII RAII(Path); |
| 6420 | |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 6421 | // Walk past any constructs which we can lifetime-extend across. |
| 6422 | Expr *Old; |
| 6423 | do { |
| 6424 | Old = Init; |
| 6425 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6426 | if (auto *EWC = dyn_cast<ExprWithCleanups>(Init)) |
| 6427 | Init = EWC->getSubExpr(); |
| 6428 | |
Richard Smith | dbc8249 | 2015-01-10 01:28:13 +0000 | [diff] [blame] | 6429 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6430 | // If this is just redundant braces around an initializer, step over it. |
| 6431 | if (ILE->isTransparent()) |
Richard Smith | dbc8249 | 2015-01-10 01:28:13 +0000 | [diff] [blame] | 6432 | Init = ILE->getInit(0); |
Richard Smith | dbc8249 | 2015-01-10 01:28:13 +0000 | [diff] [blame] | 6433 | } |
| 6434 | |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 6435 | // Step over any subobject adjustments; we may have a materialized |
| 6436 | // temporary inside them. |
Richard Smith | 4baaa5a | 2016-12-03 01:14:32 +0000 | [diff] [blame] | 6437 | Init = const_cast<Expr *>(Init->skipRValueSubobjectAdjustments()); |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 6438 | |
| 6439 | // Per current approach for DR1376, look through casts to reference type |
| 6440 | // when performing lifetime extension. |
| 6441 | if (CastExpr *CE = dyn_cast<CastExpr>(Init)) |
| 6442 | if (CE->getSubExpr()->isGLValue()) |
| 6443 | Init = CE->getSubExpr(); |
| 6444 | |
Richard Smith | b3189a1 | 2016-12-05 07:49:14 +0000 | [diff] [blame] | 6445 | // Per the current approach for DR1299, look through array element access |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6446 | // on array glvalues when performing lifetime extension. |
| 6447 | if (auto *ASE = dyn_cast<ArraySubscriptExpr>(Init)) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6448 | Init = ASE->getBase(); |
| 6449 | auto *ICE = dyn_cast<ImplicitCastExpr>(Init); |
| 6450 | if (ICE && ICE->getCastKind() == CK_ArrayToPointerDecay) |
| 6451 | Init = ICE->getSubExpr(); |
| 6452 | else |
| 6453 | // We can't lifetime extend through this but we might still find some |
| 6454 | // retained temporaries. |
| 6455 | return visitLocalsRetainedByInitializer(Path, Init, Visit, true); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6456 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6457 | |
| 6458 | // Step into CXXDefaultInitExprs so we can diagnose cases where a |
| 6459 | // constructor inherits one as an implicit mem-initializer. |
| 6460 | if (auto *DIE = dyn_cast<CXXDefaultInitExpr>(Init)) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6461 | Path.push_back( |
| 6462 | {IndirectLocalPathEntry::DefaultInit, DIE, DIE->getField()}); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6463 | Init = DIE->getExpr(); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6464 | } |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 6465 | } while (Init != Old); |
| 6466 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6467 | if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(Init)) { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6468 | if (Visit(Path, Local(MTE), RK)) |
| 6469 | visitLocalsRetainedByInitializer(Path, MTE->GetTemporaryExpr(), Visit, |
| 6470 | true); |
| 6471 | } |
| 6472 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6473 | if (isa<CallExpr>(Init)) |
| 6474 | return visitLifetimeBoundArguments(Path, Init, Visit); |
| 6475 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6476 | switch (Init->getStmtClass()) { |
| 6477 | case Stmt::DeclRefExprClass: { |
| 6478 | // If we find the name of a local non-reference parameter, we could have a |
| 6479 | // lifetime problem. |
| 6480 | auto *DRE = cast<DeclRefExpr>(Init); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6481 | auto *VD = dyn_cast<VarDecl>(DRE->getDecl()); |
| 6482 | if (VD && VD->hasLocalStorage() && |
| 6483 | !DRE->refersToEnclosingVariableOrCapture()) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6484 | if (!VD->getType()->isReferenceType()) { |
| 6485 | Visit(Path, Local(DRE), RK); |
| 6486 | } else if (isa<ParmVarDecl>(DRE->getDecl())) { |
| 6487 | // The lifetime of a reference parameter is unknown; assume it's OK |
| 6488 | // for now. |
| 6489 | break; |
| 6490 | } else if (VD->getInit() && !isVarOnPath(Path, VD)) { |
| 6491 | Path.push_back({IndirectLocalPathEntry::VarInit, DRE, VD}); |
| 6492 | visitLocalsRetainedByReferenceBinding(Path, VD->getInit(), |
| 6493 | RK_ReferenceBinding, Visit); |
| 6494 | } |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6495 | } |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6496 | break; |
| 6497 | } |
| 6498 | |
| 6499 | case Stmt::UnaryOperatorClass: { |
| 6500 | // The only unary operator that make sense to handle here |
| 6501 | // is Deref. All others don't resolve to a "name." This includes |
| 6502 | // handling all sorts of rvalues passed to a unary operator. |
| 6503 | const UnaryOperator *U = cast<UnaryOperator>(Init); |
| 6504 | if (U->getOpcode() == UO_Deref) |
| 6505 | visitLocalsRetainedByInitializer(Path, U->getSubExpr(), Visit, true); |
| 6506 | break; |
| 6507 | } |
| 6508 | |
| 6509 | case Stmt::OMPArraySectionExprClass: { |
| 6510 | visitLocalsRetainedByInitializer( |
| 6511 | Path, cast<OMPArraySectionExpr>(Init)->getBase(), Visit, true); |
| 6512 | break; |
| 6513 | } |
| 6514 | |
| 6515 | case Stmt::ConditionalOperatorClass: |
| 6516 | case Stmt::BinaryConditionalOperatorClass: { |
| 6517 | auto *C = cast<AbstractConditionalOperator>(Init); |
| 6518 | if (!C->getTrueExpr()->getType()->isVoidType()) |
| 6519 | visitLocalsRetainedByReferenceBinding(Path, C->getTrueExpr(), RK, Visit); |
| 6520 | if (!C->getFalseExpr()->getType()->isVoidType()) |
| 6521 | visitLocalsRetainedByReferenceBinding(Path, C->getFalseExpr(), RK, Visit); |
| 6522 | break; |
| 6523 | } |
| 6524 | |
| 6525 | // FIXME: Visit the left-hand side of an -> or ->*. |
| 6526 | |
| 6527 | default: |
| 6528 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6529 | } |
| 6530 | } |
| 6531 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6532 | /// Visit the locals that would be reachable through an object initialized by |
| 6533 | /// the prvalue expression \c Init. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6534 | static void visitLocalsRetainedByInitializer(IndirectLocalPath &Path, |
| 6535 | Expr *Init, LocalVisitor Visit, |
| 6536 | bool RevisitSubinits) { |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6537 | RevertToOldSizeRAII RAII(Path); |
| 6538 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6539 | Expr *Old; |
| 6540 | do { |
| 6541 | Old = Init; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6542 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6543 | // Step into CXXDefaultInitExprs so we can diagnose cases where a |
| 6544 | // constructor inherits one as an implicit mem-initializer. |
| 6545 | if (auto *DIE = dyn_cast<CXXDefaultInitExpr>(Init)) { |
| 6546 | Path.push_back({IndirectLocalPathEntry::DefaultInit, DIE, DIE->getField()}); |
| 6547 | Init = DIE->getExpr(); |
| 6548 | } |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6549 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6550 | if (auto *EWC = dyn_cast<ExprWithCleanups>(Init)) |
| 6551 | Init = EWC->getSubExpr(); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6552 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6553 | // Dig out the expression which constructs the extended temporary. |
| 6554 | Init = const_cast<Expr *>(Init->skipRValueSubobjectAdjustments()); |
| 6555 | |
| 6556 | if (CXXBindTemporaryExpr *BTE = dyn_cast<CXXBindTemporaryExpr>(Init)) |
| 6557 | Init = BTE->getSubExpr(); |
| 6558 | |
| 6559 | Init = Init->IgnoreParens(); |
| 6560 | |
| 6561 | // Step over value-preserving rvalue casts. |
| 6562 | if (auto *CE = dyn_cast<CastExpr>(Init)) { |
| 6563 | switch (CE->getCastKind()) { |
| 6564 | case CK_LValueToRValue: |
| 6565 | // If we can match the lvalue to a const object, we can look at its |
| 6566 | // initializer. |
| 6567 | Path.push_back({IndirectLocalPathEntry::LValToRVal, CE}); |
| 6568 | return visitLocalsRetainedByReferenceBinding( |
| 6569 | Path, Init, RK_ReferenceBinding, |
| 6570 | [&](IndirectLocalPath &Path, Local L, ReferenceKind RK) -> bool { |
| 6571 | if (auto *DRE = dyn_cast<DeclRefExpr>(L)) { |
| 6572 | auto *VD = dyn_cast<VarDecl>(DRE->getDecl()); |
| 6573 | if (VD && VD->getType().isConstQualified() && VD->getInit() && |
| 6574 | !isVarOnPath(Path, VD)) { |
| 6575 | Path.push_back({IndirectLocalPathEntry::VarInit, DRE, VD}); |
| 6576 | visitLocalsRetainedByInitializer(Path, VD->getInit(), Visit, true); |
| 6577 | } |
| 6578 | } else if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(L)) { |
| 6579 | if (MTE->getType().isConstQualified()) |
| 6580 | visitLocalsRetainedByInitializer(Path, MTE->GetTemporaryExpr(), |
| 6581 | Visit, true); |
| 6582 | } |
| 6583 | return false; |
| 6584 | }); |
| 6585 | |
| 6586 | // We assume that objects can be retained by pointers cast to integers, |
| 6587 | // but not if the integer is cast to floating-point type or to _Complex. |
| 6588 | // We assume that casts to 'bool' do not preserve enough information to |
| 6589 | // retain a local object. |
| 6590 | case CK_NoOp: |
| 6591 | case CK_BitCast: |
| 6592 | case CK_BaseToDerived: |
| 6593 | case CK_DerivedToBase: |
| 6594 | case CK_UncheckedDerivedToBase: |
| 6595 | case CK_Dynamic: |
| 6596 | case CK_ToUnion: |
| 6597 | case CK_UserDefinedConversion: |
| 6598 | case CK_ConstructorConversion: |
| 6599 | case CK_IntegralToPointer: |
| 6600 | case CK_PointerToIntegral: |
| 6601 | case CK_VectorSplat: |
| 6602 | case CK_IntegralCast: |
| 6603 | case CK_CPointerToObjCPointerCast: |
| 6604 | case CK_BlockPointerToObjCPointerCast: |
| 6605 | case CK_AnyPointerToBlockPointerCast: |
| 6606 | case CK_AddressSpaceConversion: |
| 6607 | break; |
| 6608 | |
| 6609 | case CK_ArrayToPointerDecay: |
| 6610 | // Model array-to-pointer decay as taking the address of the array |
| 6611 | // lvalue. |
| 6612 | Path.push_back({IndirectLocalPathEntry::AddressOf, CE}); |
| 6613 | return visitLocalsRetainedByReferenceBinding(Path, CE->getSubExpr(), |
| 6614 | RK_ReferenceBinding, Visit); |
| 6615 | |
| 6616 | default: |
| 6617 | return; |
| 6618 | } |
| 6619 | |
| 6620 | Init = CE->getSubExpr(); |
| 6621 | } |
| 6622 | } while (Old != Init); |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 6623 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6624 | // C++17 [dcl.init.list]p6: |
| 6625 | // initializing an initializer_list object from the array extends the |
| 6626 | // lifetime of the array exactly like binding a reference to a temporary. |
| 6627 | if (auto *ILE = dyn_cast<CXXStdInitializerListExpr>(Init)) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6628 | return visitLocalsRetainedByReferenceBinding(Path, ILE->getSubExpr(), |
| 6629 | RK_StdInitializerList, Visit); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6630 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6631 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6632 | // We already visited the elements of this initializer list while |
| 6633 | // performing the initialization. Don't visit them again unless we've |
| 6634 | // changed the lifetime of the initialized entity. |
| 6635 | if (!RevisitSubinits) |
| 6636 | return; |
| 6637 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6638 | if (ILE->isTransparent()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6639 | return visitLocalsRetainedByInitializer(Path, ILE->getInit(0), Visit, |
| 6640 | RevisitSubinits); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6641 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6642 | if (ILE->getType()->isArrayType()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6643 | for (unsigned I = 0, N = ILE->getNumInits(); I != N; ++I) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6644 | visitLocalsRetainedByInitializer(Path, ILE->getInit(I), Visit, |
| 6645 | RevisitSubinits); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6646 | return; |
| 6647 | } |
| 6648 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6649 | if (CXXRecordDecl *RD = ILE->getType()->getAsCXXRecordDecl()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6650 | assert(RD->isAggregate() && "aggregate init on non-aggregate"); |
| 6651 | |
| 6652 | // If we lifetime-extend a braced initializer which is initializing an |
| 6653 | // aggregate, and that aggregate contains reference members which are |
| 6654 | // bound to temporaries, those temporaries are also lifetime-extended. |
| 6655 | if (RD->isUnion() && ILE->getInitializedFieldInUnion() && |
| 6656 | ILE->getInitializedFieldInUnion()->getType()->isReferenceType()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6657 | visitLocalsRetainedByReferenceBinding(Path, ILE->getInit(0), |
| 6658 | RK_ReferenceBinding, Visit); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6659 | else { |
| 6660 | unsigned Index = 0; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6661 | for (const auto *I : RD->fields()) { |
Richard Smith | 0bca59d | 2013-07-01 06:08:20 +0000 | [diff] [blame] | 6662 | if (Index >= ILE->getNumInits()) |
| 6663 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6664 | if (I->isUnnamedBitfield()) |
| 6665 | continue; |
Richard Smith | 8d7f11d | 2013-06-27 22:54:33 +0000 | [diff] [blame] | 6666 | Expr *SubInit = ILE->getInit(Index); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6667 | if (I->getType()->isReferenceType()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6668 | visitLocalsRetainedByReferenceBinding(Path, SubInit, |
| 6669 | RK_ReferenceBinding, Visit); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6670 | else |
| 6671 | // This might be either aggregate-initialization of a member or |
| 6672 | // initialization of a std::initializer_list object. Regardless, |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6673 | // we should recursively lifetime-extend that initializer. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6674 | visitLocalsRetainedByInitializer(Path, SubInit, Visit, |
| 6675 | RevisitSubinits); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6676 | ++Index; |
| 6677 | } |
| 6678 | } |
| 6679 | } |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6680 | return; |
| 6681 | } |
| 6682 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6683 | if (isa<CallExpr>(Init) || isa<CXXConstructExpr>(Init)) |
| 6684 | return visitLifetimeBoundArguments(Path, Init, Visit); |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6685 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6686 | switch (Init->getStmtClass()) { |
| 6687 | case Stmt::UnaryOperatorClass: { |
| 6688 | auto *UO = cast<UnaryOperator>(Init); |
| 6689 | // If the initializer is the address of a local, we could have a lifetime |
| 6690 | // problem. |
| 6691 | if (UO->getOpcode() == UO_AddrOf) { |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6692 | // If this is &rvalue, then it's ill-formed and we have already diagnosed |
| 6693 | // it. Don't produce a redundant warning about the lifetime of the |
| 6694 | // temporary. |
| 6695 | if (isa<MaterializeTemporaryExpr>(UO->getSubExpr())) |
| 6696 | return; |
| 6697 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6698 | Path.push_back({IndirectLocalPathEntry::AddressOf, UO}); |
| 6699 | visitLocalsRetainedByReferenceBinding(Path, UO->getSubExpr(), |
| 6700 | RK_ReferenceBinding, Visit); |
| 6701 | } |
| 6702 | break; |
| 6703 | } |
| 6704 | |
| 6705 | case Stmt::BinaryOperatorClass: { |
| 6706 | // Handle pointer arithmetic. |
| 6707 | auto *BO = cast<BinaryOperator>(Init); |
| 6708 | BinaryOperatorKind BOK = BO->getOpcode(); |
| 6709 | if (!BO->getType()->isPointerType() || (BOK != BO_Add && BOK != BO_Sub)) |
| 6710 | break; |
| 6711 | |
| 6712 | if (BO->getLHS()->getType()->isPointerType()) |
| 6713 | visitLocalsRetainedByInitializer(Path, BO->getLHS(), Visit, true); |
| 6714 | else if (BO->getRHS()->getType()->isPointerType()) |
| 6715 | visitLocalsRetainedByInitializer(Path, BO->getRHS(), Visit, true); |
| 6716 | break; |
| 6717 | } |
| 6718 | |
| 6719 | case Stmt::ConditionalOperatorClass: |
| 6720 | case Stmt::BinaryConditionalOperatorClass: { |
| 6721 | auto *C = cast<AbstractConditionalOperator>(Init); |
| 6722 | // In C++, we can have a throw-expression operand, which has 'void' type |
| 6723 | // and isn't interesting from a lifetime perspective. |
| 6724 | if (!C->getTrueExpr()->getType()->isVoidType()) |
| 6725 | visitLocalsRetainedByInitializer(Path, C->getTrueExpr(), Visit, true); |
| 6726 | if (!C->getFalseExpr()->getType()->isVoidType()) |
| 6727 | visitLocalsRetainedByInitializer(Path, C->getFalseExpr(), Visit, true); |
| 6728 | break; |
| 6729 | } |
| 6730 | |
| 6731 | case Stmt::BlockExprClass: |
| 6732 | if (cast<BlockExpr>(Init)->getBlockDecl()->hasCaptures()) { |
| 6733 | // This is a local block, whose lifetime is that of the function. |
| 6734 | Visit(Path, Local(cast<BlockExpr>(Init)), RK_ReferenceBinding); |
| 6735 | } |
| 6736 | break; |
| 6737 | |
| 6738 | case Stmt::AddrLabelExprClass: |
| 6739 | // We want to warn if the address of a label would escape the function. |
| 6740 | Visit(Path, Local(cast<AddrLabelExpr>(Init)), RK_ReferenceBinding); |
| 6741 | break; |
| 6742 | |
| 6743 | default: |
| 6744 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6745 | } |
| 6746 | } |
| 6747 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6748 | /// Determine whether this is an indirect path to a temporary that we are |
| 6749 | /// supposed to lifetime-extend along (but don't). |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6750 | static bool shouldLifetimeExtendThroughPath(const IndirectLocalPath &Path) { |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6751 | for (auto Elem : Path) { |
Richard Smith | f66e4f7 | 2018-07-23 22:56:45 +0000 | [diff] [blame] | 6752 | if (Elem.Kind != IndirectLocalPathEntry::DefaultInit) |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6753 | return false; |
| 6754 | } |
| 6755 | return true; |
| 6756 | } |
| 6757 | |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 6758 | /// Find the range for the first interesting entry in the path at or after I. |
| 6759 | static SourceRange nextPathEntryRange(const IndirectLocalPath &Path, unsigned I, |
| 6760 | Expr *E) { |
| 6761 | for (unsigned N = Path.size(); I != N; ++I) { |
| 6762 | switch (Path[I].Kind) { |
| 6763 | case IndirectLocalPathEntry::AddressOf: |
| 6764 | case IndirectLocalPathEntry::LValToRVal: |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6765 | case IndirectLocalPathEntry::LifetimeBoundCall: |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 6766 | // These exist primarily to mark the path as not permitting or |
| 6767 | // supporting lifetime extension. |
| 6768 | break; |
| 6769 | |
| 6770 | case IndirectLocalPathEntry::DefaultInit: |
| 6771 | case IndirectLocalPathEntry::VarInit: |
| 6772 | return Path[I].E->getSourceRange(); |
| 6773 | } |
| 6774 | } |
| 6775 | return E->getSourceRange(); |
| 6776 | } |
| 6777 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6778 | void Sema::checkInitializerLifetime(const InitializedEntity &Entity, |
| 6779 | Expr *Init) { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6780 | LifetimeResult LR = getEntityLifetime(&Entity); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6781 | LifetimeKind LK = LR.getInt(); |
| 6782 | const InitializedEntity *ExtendingEntity = LR.getPointer(); |
| 6783 | |
| 6784 | // If this entity doesn't have an interesting lifetime, don't bother looking |
| 6785 | // for temporaries within its initializer. |
| 6786 | if (LK == LK_FullExpression) |
| 6787 | return; |
| 6788 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6789 | auto TemporaryVisitor = [&](IndirectLocalPath &Path, Local L, |
| 6790 | ReferenceKind RK) -> bool { |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 6791 | SourceRange DiagRange = nextPathEntryRange(Path, 0, L); |
| 6792 | SourceLocation DiagLoc = DiagRange.getBegin(); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6793 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6794 | switch (LK) { |
| 6795 | case LK_FullExpression: |
| 6796 | llvm_unreachable("already handled this"); |
| 6797 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6798 | case LK_Extended: { |
| 6799 | auto *MTE = dyn_cast<MaterializeTemporaryExpr>(L); |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6800 | if (!MTE) { |
| 6801 | // The initialized entity has lifetime beyond the full-expression, |
| 6802 | // and the local entity does too, so don't warn. |
| 6803 | // |
| 6804 | // FIXME: We should consider warning if a static / thread storage |
| 6805 | // duration variable retains an automatic storage duration local. |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6806 | return false; |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6807 | } |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6808 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6809 | // Lifetime-extend the temporary. |
| 6810 | if (Path.empty()) { |
| 6811 | // Update the storage duration of the materialized temporary. |
| 6812 | // FIXME: Rebuild the expression instead of mutating it. |
| 6813 | MTE->setExtendingDecl(ExtendingEntity->getDecl(), |
| 6814 | ExtendingEntity->allocateManglingNumber()); |
| 6815 | // Also visit the temporaries lifetime-extended by this initializer. |
| 6816 | return true; |
| 6817 | } |
| 6818 | |
| 6819 | if (shouldLifetimeExtendThroughPath(Path)) { |
| 6820 | // We're supposed to lifetime-extend the temporary along this path (per |
| 6821 | // the resolution of DR1815), but we don't support that yet. |
| 6822 | // |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6823 | // FIXME: Properly handle this situation. Perhaps the easiest approach |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6824 | // would be to clone the initializer expression on each use that would |
| 6825 | // lifetime extend its temporaries. |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6826 | Diag(DiagLoc, diag::warn_unsupported_lifetime_extension) |
| 6827 | << RK << DiagRange; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6828 | } else { |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6829 | // If the path goes through the initialization of a variable or field, |
| 6830 | // it can't possibly reach a temporary created in this full-expression. |
| 6831 | // We will have already diagnosed any problems with the initializer. |
| 6832 | if (pathContainsInit(Path)) |
| 6833 | return false; |
| 6834 | |
| 6835 | Diag(DiagLoc, diag::warn_dangling_variable) |
Richard Smith | ad5bbcc | 2018-08-01 01:03:33 +0000 | [diff] [blame] | 6836 | << RK << !Entity.getParent() |
| 6837 | << ExtendingEntity->getDecl()->isImplicit() |
| 6838 | << ExtendingEntity->getDecl() << Init->isGLValue() << DiagRange; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6839 | } |
| 6840 | break; |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6841 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6842 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6843 | case LK_MemInitializer: { |
George Burgess IV | 06df229 | 2018-07-24 02:10:53 +0000 | [diff] [blame] | 6844 | if (isa<MaterializeTemporaryExpr>(L)) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6845 | // Under C++ DR1696, if a mem-initializer (or a default member |
| 6846 | // initializer used by the absence of one) would lifetime-extend a |
| 6847 | // temporary, the program is ill-formed. |
| 6848 | if (auto *ExtendingDecl = |
| 6849 | ExtendingEntity ? ExtendingEntity->getDecl() : nullptr) { |
| 6850 | bool IsSubobjectMember = ExtendingEntity != &Entity; |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6851 | Diag(DiagLoc, shouldLifetimeExtendThroughPath(Path) |
| 6852 | ? diag::err_dangling_member |
| 6853 | : diag::warn_dangling_member) |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6854 | << ExtendingDecl << IsSubobjectMember << RK << DiagRange; |
| 6855 | // Don't bother adding a note pointing to the field if we're inside |
| 6856 | // its default member initializer; our primary diagnostic points to |
| 6857 | // the same place in that case. |
| 6858 | if (Path.empty() || |
| 6859 | Path.back().Kind != IndirectLocalPathEntry::DefaultInit) { |
| 6860 | Diag(ExtendingDecl->getLocation(), |
| 6861 | diag::note_lifetime_extending_member_declared_here) |
| 6862 | << RK << IsSubobjectMember; |
| 6863 | } |
| 6864 | } else { |
| 6865 | // We have a mem-initializer but no particular field within it; this |
| 6866 | // is either a base class or a delegating initializer directly |
| 6867 | // initializing the base-class from something that doesn't live long |
| 6868 | // enough. |
| 6869 | // |
| 6870 | // FIXME: Warn on this. |
| 6871 | return false; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6872 | } |
| 6873 | } else { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6874 | // Paths via a default initializer can only occur during error recovery |
| 6875 | // (there's no other way that a default initializer can refer to a |
| 6876 | // local). Don't produce a bogus warning on those cases. |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6877 | if (pathContainsInit(Path)) |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6878 | return false; |
| 6879 | |
| 6880 | auto *DRE = dyn_cast<DeclRefExpr>(L); |
| 6881 | auto *VD = DRE ? dyn_cast<VarDecl>(DRE->getDecl()) : nullptr; |
| 6882 | if (!VD) { |
| 6883 | // A member was initialized to a local block. |
| 6884 | // FIXME: Warn on this. |
| 6885 | return false; |
| 6886 | } |
| 6887 | |
| 6888 | if (auto *Member = |
| 6889 | ExtendingEntity ? ExtendingEntity->getDecl() : nullptr) { |
| 6890 | bool IsPointer = Member->getType()->isAnyPointerType(); |
| 6891 | Diag(DiagLoc, IsPointer ? diag::warn_init_ptr_member_to_parameter_addr |
| 6892 | : diag::warn_bind_ref_member_to_parameter) |
| 6893 | << Member << VD << isa<ParmVarDecl>(VD) << DiagRange; |
| 6894 | Diag(Member->getLocation(), |
| 6895 | diag::note_ref_or_ptr_member_declared_here) |
| 6896 | << (unsigned)IsPointer; |
| 6897 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6898 | } |
| 6899 | break; |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6900 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6901 | |
| 6902 | case LK_New: |
George Burgess IV | 06df229 | 2018-07-24 02:10:53 +0000 | [diff] [blame] | 6903 | if (isa<MaterializeTemporaryExpr>(L)) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6904 | Diag(DiagLoc, RK == RK_ReferenceBinding |
| 6905 | ? diag::warn_new_dangling_reference |
| 6906 | : diag::warn_new_dangling_initializer_list) |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6907 | << !Entity.getParent() << DiagRange; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6908 | } else { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6909 | // We can't determine if the allocation outlives the local declaration. |
| 6910 | return false; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6911 | } |
| 6912 | break; |
| 6913 | |
| 6914 | case LK_Return: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 6915 | case LK_StmtExprResult: |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6916 | if (auto *DRE = dyn_cast<DeclRefExpr>(L)) { |
| 6917 | // We can't determine if the local variable outlives the statement |
| 6918 | // expression. |
| 6919 | if (LK == LK_StmtExprResult) |
| 6920 | return false; |
| 6921 | Diag(DiagLoc, diag::warn_ret_stack_addr_ref) |
| 6922 | << Entity.getType()->isReferenceType() << DRE->getDecl() |
| 6923 | << isa<ParmVarDecl>(DRE->getDecl()) << DiagRange; |
| 6924 | } else if (isa<BlockExpr>(L)) { |
| 6925 | Diag(DiagLoc, diag::err_ret_local_block) << DiagRange; |
| 6926 | } else if (isa<AddrLabelExpr>(L)) { |
Reid Kleckner | 4c33d19 | 2018-08-17 22:11:31 +0000 | [diff] [blame] | 6927 | // Don't warn when returning a label from a statement expression. |
| 6928 | // Leaving the scope doesn't end its lifetime. |
| 6929 | if (LK == LK_StmtExprResult) |
| 6930 | return false; |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6931 | Diag(DiagLoc, diag::warn_ret_addr_label) << DiagRange; |
| 6932 | } else { |
| 6933 | Diag(DiagLoc, diag::warn_ret_local_temp_addr_ref) |
| 6934 | << Entity.getType()->isReferenceType() << DiagRange; |
| 6935 | } |
| 6936 | break; |
Florian Hahn | 0aa117d | 2018-07-17 09:23:31 +0000 | [diff] [blame] | 6937 | } |
| 6938 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6939 | for (unsigned I = 0; I != Path.size(); ++I) { |
| 6940 | auto Elem = Path[I]; |
| 6941 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6942 | switch (Elem.Kind) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6943 | case IndirectLocalPathEntry::AddressOf: |
| 6944 | case IndirectLocalPathEntry::LValToRVal: |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 6945 | // These exist primarily to mark the path as not permitting or |
| 6946 | // supporting lifetime extension. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6947 | break; |
| 6948 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6949 | case IndirectLocalPathEntry::LifetimeBoundCall: |
| 6950 | // FIXME: Consider adding a note for this. |
| 6951 | break; |
| 6952 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6953 | case IndirectLocalPathEntry::DefaultInit: { |
| 6954 | auto *FD = cast<FieldDecl>(Elem.D); |
| 6955 | Diag(FD->getLocation(), diag::note_init_with_default_member_initalizer) |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 6956 | << FD << nextPathEntryRange(Path, I + 1, L); |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6957 | break; |
| 6958 | } |
| 6959 | |
| 6960 | case IndirectLocalPathEntry::VarInit: |
| 6961 | const VarDecl *VD = cast<VarDecl>(Elem.D); |
| 6962 | Diag(VD->getLocation(), diag::note_local_var_initializer) |
Richard Smith | ad5bbcc | 2018-08-01 01:03:33 +0000 | [diff] [blame] | 6963 | << VD->getType()->isReferenceType() |
| 6964 | << VD->isImplicit() << VD->getDeclName() |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 6965 | << nextPathEntryRange(Path, I + 1, L); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6966 | break; |
Florian Hahn | 0aa117d | 2018-07-17 09:23:31 +0000 | [diff] [blame] | 6967 | } |
| 6968 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6969 | |
| 6970 | // We didn't lifetime-extend, so don't go any further; we don't need more |
| 6971 | // warnings or errors on inner temporaries within this one's initializer. |
| 6972 | return false; |
| 6973 | }; |
| 6974 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6975 | llvm::SmallVector<IndirectLocalPathEntry, 8> Path; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6976 | if (Init->isGLValue()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6977 | visitLocalsRetainedByReferenceBinding(Path, Init, RK_ReferenceBinding, |
| 6978 | TemporaryVisitor); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6979 | else |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6980 | visitLocalsRetainedByInitializer(Path, Init, TemporaryVisitor, false); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6981 | } |
| 6982 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 6983 | static void DiagnoseNarrowingInInitList(Sema &S, |
| 6984 | const ImplicitConversionSequence &ICS, |
| 6985 | QualType PreNarrowingType, |
| 6986 | QualType EntityType, |
| 6987 | const Expr *PostInit); |
| 6988 | |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 6989 | /// Provide warnings when std::move is used on construction. |
| 6990 | static void CheckMoveOnConstruction(Sema &S, const Expr *InitExpr, |
| 6991 | bool IsReturnStmt) { |
| 6992 | if (!InitExpr) |
| 6993 | return; |
| 6994 | |
Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 6995 | if (S.inTemplateInstantiation()) |
Richard Trieu | 6093d14 | 2015-07-29 17:03:34 +0000 | [diff] [blame] | 6996 | return; |
| 6997 | |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 6998 | QualType DestType = InitExpr->getType(); |
| 6999 | if (!DestType->isRecordType()) |
| 7000 | return; |
| 7001 | |
| 7002 | unsigned DiagID = 0; |
| 7003 | if (IsReturnStmt) { |
| 7004 | const CXXConstructExpr *CCE = |
| 7005 | dyn_cast<CXXConstructExpr>(InitExpr->IgnoreParens()); |
| 7006 | if (!CCE || CCE->getNumArgs() != 1) |
| 7007 | return; |
| 7008 | |
| 7009 | if (!CCE->getConstructor()->isCopyOrMoveConstructor()) |
| 7010 | return; |
| 7011 | |
| 7012 | InitExpr = CCE->getArg(0)->IgnoreImpCasts(); |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7013 | } |
| 7014 | |
| 7015 | // Find the std::move call and get the argument. |
| 7016 | const CallExpr *CE = dyn_cast<CallExpr>(InitExpr->IgnoreParens()); |
Nico Weber | 192184c | 2018-06-20 15:57:38 +0000 | [diff] [blame] | 7017 | if (!CE || !CE->isCallToStdMove()) |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7018 | return; |
| 7019 | |
| 7020 | const Expr *Arg = CE->getArg(0)->IgnoreImplicit(); |
| 7021 | |
| 7022 | if (IsReturnStmt) { |
| 7023 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg->IgnoreParenImpCasts()); |
| 7024 | if (!DRE || DRE->refersToEnclosingVariableOrCapture()) |
| 7025 | return; |
| 7026 | |
| 7027 | const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()); |
| 7028 | if (!VD || !VD->hasLocalStorage()) |
| 7029 | return; |
| 7030 | |
Alex Lorenz | bbe51d8 | 2017-11-07 21:40:11 +0000 | [diff] [blame] | 7031 | // __block variables are not moved implicitly. |
| 7032 | if (VD->hasAttr<BlocksAttr>()) |
| 7033 | return; |
| 7034 | |
Richard Trieu | 8d4006a | 2015-07-28 19:06:16 +0000 | [diff] [blame] | 7035 | QualType SourceType = VD->getType(); |
| 7036 | if (!SourceType->isRecordType()) |
Richard Trieu | 1d4911bc | 2015-05-18 19:54:08 +0000 | [diff] [blame] | 7037 | return; |
| 7038 | |
Richard Trieu | 8d4006a | 2015-07-28 19:06:16 +0000 | [diff] [blame] | 7039 | if (!S.Context.hasSameUnqualifiedType(DestType, SourceType)) { |
Richard Trieu | 1993dc8 | 2015-07-29 23:47:19 +0000 | [diff] [blame] | 7040 | return; |
Richard Trieu | 8d4006a | 2015-07-28 19:06:16 +0000 | [diff] [blame] | 7041 | } |
| 7042 | |
Davide Italiano | 7842c3f | 2015-07-18 01:15:19 +0000 | [diff] [blame] | 7043 | // If we're returning a function parameter, copy elision |
| 7044 | // is not possible. |
| 7045 | if (isa<ParmVarDecl>(VD)) |
| 7046 | DiagID = diag::warn_redundant_move_on_return; |
Richard Trieu | 1993dc8 | 2015-07-29 23:47:19 +0000 | [diff] [blame] | 7047 | else |
| 7048 | DiagID = diag::warn_pessimizing_move_on_return; |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7049 | } else { |
| 7050 | DiagID = diag::warn_pessimizing_move_on_initialization; |
| 7051 | const Expr *ArgStripped = Arg->IgnoreImplicit()->IgnoreParens(); |
| 7052 | if (!ArgStripped->isRValue() || !ArgStripped->getType()->isRecordType()) |
| 7053 | return; |
| 7054 | } |
| 7055 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7056 | S.Diag(CE->getBeginLoc(), DiagID); |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7057 | |
| 7058 | // Get all the locations for a fix-it. Don't emit the fix-it if any location |
| 7059 | // is within a macro. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7060 | SourceLocation CallBegin = CE->getCallee()->getBeginLoc(); |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7061 | if (CallBegin.isMacroID()) |
| 7062 | return; |
| 7063 | SourceLocation RParen = CE->getRParenLoc(); |
| 7064 | if (RParen.isMacroID()) |
| 7065 | return; |
| 7066 | SourceLocation LParen; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7067 | SourceLocation ArgLoc = Arg->getBeginLoc(); |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7068 | |
| 7069 | // Special testing for the argument location. Since the fix-it needs the |
| 7070 | // location right before the argument, the argument location can be in a |
| 7071 | // macro only if it is at the beginning of the macro. |
| 7072 | while (ArgLoc.isMacroID() && |
| 7073 | S.getSourceManager().isAtStartOfImmediateMacroExpansion(ArgLoc)) { |
Richard Smith | b5f8171 | 2018-04-30 05:25:48 +0000 | [diff] [blame] | 7074 | ArgLoc = S.getSourceManager().getImmediateExpansionRange(ArgLoc).getBegin(); |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7075 | } |
| 7076 | |
| 7077 | if (LParen.isMacroID()) |
| 7078 | return; |
| 7079 | |
| 7080 | LParen = ArgLoc.getLocWithOffset(-1); |
| 7081 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7082 | S.Diag(CE->getBeginLoc(), diag::note_remove_move) |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7083 | << FixItHint::CreateRemoval(SourceRange(CallBegin, LParen)) |
| 7084 | << FixItHint::CreateRemoval(SourceRange(RParen, RParen)); |
| 7085 | } |
| 7086 | |
Nick Lewycky | 2eeddfb | 2016-05-14 17:44:14 +0000 | [diff] [blame] | 7087 | static void CheckForNullPointerDereference(Sema &S, const Expr *E) { |
| 7088 | // Check to see if we are dereferencing a null pointer. If so, this is |
| 7089 | // undefined behavior, so warn about it. This only handles the pattern |
| 7090 | // "*null", which is a very syntactic check. |
| 7091 | if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E->IgnoreParenCasts())) |
| 7092 | if (UO->getOpcode() == UO_Deref && |
| 7093 | UO->getSubExpr()->IgnoreParenCasts()-> |
| 7094 | isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull)) { |
| 7095 | S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
| 7096 | S.PDiag(diag::warn_binding_null_to_reference) |
| 7097 | << UO->getSubExpr()->getSourceRange()); |
| 7098 | } |
| 7099 | } |
| 7100 | |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 7101 | MaterializeTemporaryExpr * |
| 7102 | Sema::CreateMaterializeTemporaryExpr(QualType T, Expr *Temporary, |
| 7103 | bool BoundToLvalueReference) { |
| 7104 | auto MTE = new (Context) |
| 7105 | MaterializeTemporaryExpr(T, Temporary, BoundToLvalueReference); |
| 7106 | |
| 7107 | // Order an ExprWithCleanups for lifetime marks. |
| 7108 | // |
| 7109 | // TODO: It'll be good to have a single place to check the access of the |
| 7110 | // destructor and generate ExprWithCleanups for various uses. Currently these |
| 7111 | // are done in both CreateMaterializeTemporaryExpr and MaybeBindToTemporary, |
| 7112 | // but there may be a chance to merge them. |
| 7113 | Cleanup.setExprNeedsCleanups(false); |
| 7114 | return MTE; |
| 7115 | } |
| 7116 | |
Richard Smith | 4baaa5a | 2016-12-03 01:14:32 +0000 | [diff] [blame] | 7117 | ExprResult Sema::TemporaryMaterializationConversion(Expr *E) { |
| 7118 | // In C++98, we don't want to implicitly create an xvalue. |
| 7119 | // FIXME: This means that AST consumers need to deal with "prvalues" that |
| 7120 | // denote materialized temporaries. Maybe we should add another ValueKind |
| 7121 | // for "xvalue pretending to be a prvalue" for C++98 support. |
| 7122 | if (!E->isRValue() || !getLangOpts().CPlusPlus11) |
| 7123 | return E; |
| 7124 | |
| 7125 | // C++1z [conv.rval]/1: T shall be a complete type. |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7126 | // FIXME: Does this ever matter (can we form a prvalue of incomplete type)? |
| 7127 | // 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] | 7128 | QualType T = E->getType(); |
| 7129 | if (RequireCompleteType(E->getExprLoc(), T, diag::err_incomplete_type)) |
| 7130 | return ExprError(); |
| 7131 | |
| 7132 | return CreateMaterializeTemporaryExpr(E->getType(), E, false); |
| 7133 | } |
| 7134 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7135 | ExprResult |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7136 | InitializationSequence::Perform(Sema &S, |
| 7137 | const InitializedEntity &Entity, |
| 7138 | const InitializationKind &Kind, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7139 | MultiExprArg Args, |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7140 | QualType *ResultType) { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 7141 | if (Failed()) { |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 7142 | Diagnose(S, Entity, Kind, Args); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7143 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7144 | } |
Nico Weber | 337d5aa | 2015-04-17 08:32:38 +0000 | [diff] [blame] | 7145 | if (!ZeroInitializationFixit.empty()) { |
| 7146 | unsigned DiagID = diag::err_default_init_const; |
| 7147 | if (Decl *D = Entity.getDecl()) |
| 7148 | if (S.getLangOpts().MSVCCompat && D->hasAttr<SelectAnyAttr>()) |
| 7149 | DiagID = diag::ext_default_init_const; |
| 7150 | |
| 7151 | // The initialization would have succeeded with this fixit. Since the fixit |
| 7152 | // is on the error, we need to build a valid AST in this case, so this isn't |
| 7153 | // handled in the Failed() branch above. |
| 7154 | QualType DestType = Entity.getType(); |
| 7155 | S.Diag(Kind.getLocation(), DiagID) |
| 7156 | << DestType << (bool)DestType->getAs<RecordType>() |
| 7157 | << FixItHint::CreateInsertion(ZeroInitializationFixitLoc, |
| 7158 | ZeroInitializationFixit); |
| 7159 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7160 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 7161 | if (getKind() == DependentSequence) { |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7162 | // If the declaration is a non-dependent, incomplete array type |
| 7163 | // that has an initializer, then its type will be completed once |
| 7164 | // the initializer is instantiated. |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7165 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7166 | Args.size() == 1) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7167 | QualType DeclType = Entity.getType(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7168 | if (const IncompleteArrayType *ArrayT |
| 7169 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 7170 | // FIXME: We don't currently have the ability to accurately |
| 7171 | // compute the length of an initializer list without |
| 7172 | // performing full type-checking of the initializer list |
| 7173 | // (since we have to determine where braces are implicitly |
| 7174 | // introduced and such). So, we fall back to making the array |
| 7175 | // type a dependently-sized array type with no specified |
| 7176 | // bound. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7177 | if (isa<InitListExpr>((Expr *)Args[0])) { |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7178 | SourceRange Brackets; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7179 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7180 | // Scavange the location of the brackets from the entity, if we can. |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 7181 | if (auto *DD = dyn_cast_or_null<DeclaratorDecl>(Entity.getDecl())) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7182 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 7183 | TypeLoc TL = TInfo->getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7184 | if (IncompleteArrayTypeLoc ArrayLoc = |
| 7185 | TL.getAs<IncompleteArrayTypeLoc>()) |
| 7186 | Brackets = ArrayLoc.getBracketsRange(); |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7187 | } |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7188 | } |
| 7189 | |
| 7190 | *ResultType |
| 7191 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7192 | /*NumElts=*/nullptr, |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7193 | ArrayT->getSizeModifier(), |
| 7194 | ArrayT->getIndexTypeCVRQualifiers(), |
| 7195 | Brackets); |
| 7196 | } |
| 7197 | |
| 7198 | } |
| 7199 | } |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 7200 | if (Kind.getKind() == InitializationKind::IK_Direct && |
| 7201 | !Kind.isExplicitCast()) { |
| 7202 | // Rebuild the ParenListExpr. |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 7203 | SourceRange ParenRange = Kind.getParenOrBraceRange(); |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 7204 | return S.ActOnParenListExpr(ParenRange.getBegin(), ParenRange.getEnd(), |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7205 | Args); |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 7206 | } |
Manuel Klimek | f2b4b69 | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 7207 | assert(Kind.getKind() == InitializationKind::IK_Copy || |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7208 | Kind.isExplicitCast() || |
Douglas Gregor | bf13895 | 2012-04-04 04:06:51 +0000 | [diff] [blame] | 7209 | Kind.getKind() == InitializationKind::IK_DirectList); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7210 | return ExprResult(Args[0]); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7211 | } |
| 7212 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 7213 | // No steps means no initialization. |
| 7214 | if (Steps.empty()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7215 | return ExprResult((Expr *)nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7216 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7217 | if (S.getLangOpts().CPlusPlus11 && Entity.getType()->isReferenceType() && |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7218 | Args.size() == 1 && isa<InitListExpr>(Args[0]) && |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 7219 | !Entity.isParameterKind()) { |
Richard Smith | 2b349ae | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 7220 | // Produce a C++98 compatibility warning if we are initializing a reference |
| 7221 | // from an initializer list. For parameters, we produce a better warning |
| 7222 | // elsewhere. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7223 | Expr *Init = Args[0]; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7224 | S.Diag(Init->getBeginLoc(), diag::warn_cxx98_compat_reference_list_init) |
| 7225 | << Init->getSourceRange(); |
Richard Smith | 2b349ae | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 7226 | } |
| 7227 | |
Egor Churaev | 3bccec5 | 2017-04-05 12:47:10 +0000 | [diff] [blame] | 7228 | // OpenCL v2.0 s6.13.11.1. atomic variables can be initialized in global scope |
| 7229 | QualType ETy = Entity.getType(); |
| 7230 | Qualifiers TyQualifiers = ETy.getQualifiers(); |
| 7231 | bool HasGlobalAS = TyQualifiers.hasAddressSpace() && |
| 7232 | TyQualifiers.getAddressSpace() == LangAS::opencl_global; |
| 7233 | |
| 7234 | if (S.getLangOpts().OpenCLVersion >= 200 && |
| 7235 | ETy->isAtomicType() && !HasGlobalAS && |
| 7236 | Entity.getKind() == InitializedEntity::EK_Variable && Args.size() > 0) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7237 | S.Diag(Args[0]->getBeginLoc(), diag::err_opencl_atomic_init) |
| 7238 | << 1 |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 7239 | << SourceRange(Entity.getDecl()->getBeginLoc(), Args[0]->getEndLoc()); |
Egor Churaev | 3bccec5 | 2017-04-05 12:47:10 +0000 | [diff] [blame] | 7240 | return ExprError(); |
| 7241 | } |
| 7242 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7243 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 7244 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 7245 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 7246 | // and we want latter when it makes sense. |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7247 | if (ResultType) |
Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 7248 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7249 | Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7250 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7251 | ExprResult CurInit((Expr *)nullptr); |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7252 | SmallVector<Expr*, 4> ArrayLoopCommonExprs; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7253 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7254 | // For initialization steps that start with a single initializer, |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 7255 | // grab the only argument out the Args and place it into the "current" |
| 7256 | // initializer. |
| 7257 | switch (Steps.front().Kind) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7258 | case SK_ResolveAddressOfOverloadedFunction: |
| 7259 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7260 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7261 | case SK_CastDerivedToBaseLValue: |
| 7262 | case SK_BindReference: |
| 7263 | case SK_BindReferenceToTemporary: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7264 | case SK_FinalCopy: |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 7265 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7266 | case SK_UserConversion: |
| 7267 | case SK_QualificationConversionLValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7268 | case SK_QualificationConversionXValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7269 | case SK_QualificationConversionRValue: |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 7270 | case SK_AtomicConversion: |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 7271 | case SK_LValueToRValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7272 | case SK_ConversionSequence: |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7273 | case SK_ConversionSequenceNoNarrowing: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7274 | case SK_ListInitialization: |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7275 | case SK_UnwrapInitList: |
| 7276 | case SK_RewrapInitList: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7277 | case SK_CAssignment: |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 7278 | case SK_StringInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7279 | case SK_ObjCObjectConversion: |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7280 | case SK_ArrayLoopIndex: |
| 7281 | case SK_ArrayLoopInit: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7282 | case SK_ArrayInit: |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 7283 | case SK_GNUArrayInit: |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 7284 | case SK_ParenthesizedArrayInit: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7285 | case SK_PassByIndirectCopyRestore: |
| 7286 | case SK_PassByIndirectRestore: |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 7287 | case SK_ProduceObjCObject: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7288 | case SK_StdInitializerList: |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 7289 | case SK_OCLSamplerInit: |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 7290 | case SK_OCLZeroEvent: |
| 7291 | case SK_OCLZeroQueue: { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7292 | assert(Args.size() == 1); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7293 | CurInit = Args[0]; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7294 | if (!CurInit.get()) return ExprError(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7295 | break; |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7296 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7297 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7298 | case SK_ConstructorInitialization: |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7299 | case SK_ConstructorInitializationFromList: |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 7300 | case SK_StdInitializerListConstructorCall: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7301 | case SK_ZeroInitialization: |
| 7302 | break; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7303 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7304 | |
Richard Smith | d6a1508 | 2017-01-07 00:48:55 +0000 | [diff] [blame] | 7305 | // Promote from an unevaluated context to an unevaluated list context in |
| 7306 | // C++11 list-initialization; we need to instantiate entities usable in |
| 7307 | // constant expressions here in order to perform narrowing checks =( |
| 7308 | EnterExpressionEvaluationContext Evaluated( |
| 7309 | S, EnterExpressionEvaluationContext::InitList, |
| 7310 | CurInit.get() && isa<InitListExpr>(CurInit.get())); |
| 7311 | |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7312 | // C++ [class.abstract]p2: |
| 7313 | // no objects of an abstract class can be created except as subobjects |
| 7314 | // of a class derived from it |
| 7315 | auto checkAbstractType = [&](QualType T) -> bool { |
| 7316 | if (Entity.getKind() == InitializedEntity::EK_Base || |
| 7317 | Entity.getKind() == InitializedEntity::EK_Delegating) |
| 7318 | return false; |
| 7319 | return S.RequireNonAbstractType(Kind.getLocation(), T, |
| 7320 | diag::err_allocation_of_abstract_type); |
| 7321 | }; |
| 7322 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7323 | // Walk through the computed steps for the initialization sequence, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7324 | // performing the specified conversions along the way. |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7325 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7326 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 7327 | Step != StepEnd; ++Step) { |
| 7328 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7329 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7330 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7331 | QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7332 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7333 | switch (Step->Kind) { |
| 7334 | case SK_ResolveAddressOfOverloadedFunction: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7335 | // Overload resolution determined which function invoke; update the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7336 | // initializer to reflect that choice. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7337 | S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 7338 | if (S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation())) |
| 7339 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7340 | CurInit = S.FixOverloadedFunctionReference(CurInit, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7341 | Step->Function.FoundDecl, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7342 | Step->Function.Function); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7343 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7344 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7345 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7346 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7347 | case SK_CastDerivedToBaseLValue: { |
| 7348 | // We have a derived-to-base cast that produces either an rvalue or an |
| 7349 | // lvalue. Perform that cast. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7350 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 7351 | CXXCastPath BasePath; |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 7352 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7353 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 7354 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7355 | if (S.CheckDerivedToBaseConversion( |
| 7356 | SourceType, Step->Type, CurInit.get()->getBeginLoc(), |
| 7357 | CurInit.get()->getSourceRange(), &BasePath, IgnoreBaseAccess)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7358 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7359 | |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7360 | ExprValueKind VK = |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7361 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7362 | VK_LValue : |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7363 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7364 | VK_XValue : |
| 7365 | VK_RValue); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7366 | CurInit = |
| 7367 | ImplicitCastExpr::Create(S.Context, Step->Type, CK_DerivedToBase, |
| 7368 | CurInit.get(), &BasePath, VK); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7369 | break; |
| 7370 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7371 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7372 | case SK_BindReference: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7373 | // Reference binding does not have any corresponding ASTs. |
| 7374 | |
| 7375 | // Check exception specifications |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7376 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7377 | return ExprError(); |
Anders Carlsson | ab0ddb5 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 7378 | |
George Burgess IV | cfd48d9 | 2017-04-13 23:47:08 +0000 | [diff] [blame] | 7379 | // We don't check for e.g. function pointers here, since address |
| 7380 | // availability checks should only occur when the function first decays |
| 7381 | // into a pointer or reference. |
| 7382 | if (CurInit.get()->getType()->isFunctionProtoType()) { |
| 7383 | if (auto *DRE = dyn_cast<DeclRefExpr>(CurInit.get()->IgnoreParens())) { |
| 7384 | if (auto *FD = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
| 7385 | if (!S.checkAddressOfFunctionIsAvailable(FD, /*Complain=*/true, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7386 | DRE->getBeginLoc())) |
George Burgess IV | cfd48d9 | 2017-04-13 23:47:08 +0000 | [diff] [blame] | 7387 | return ExprError(); |
| 7388 | } |
| 7389 | } |
| 7390 | } |
| 7391 | |
Nick Lewycky | 2eeddfb | 2016-05-14 17:44:14 +0000 | [diff] [blame] | 7392 | CheckForNullPointerDereference(S, CurInit.get()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7393 | break; |
Anders Carlsson | ab0ddb5 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 7394 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 7395 | case SK_BindReferenceToTemporary: { |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 7396 | // Make sure the "temporary" is actually an rvalue. |
| 7397 | assert(CurInit.get()->isRValue() && "not a temporary"); |
| 7398 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7399 | // Check exception specifications |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7400 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7401 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7402 | |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 7403 | // Materialize the temporary into memory. |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 7404 | MaterializeTemporaryExpr *MTE = S.CreateMaterializeTemporaryExpr( |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7405 | Step->Type, CurInit.get(), Entity.getType()->isLValueReferenceType()); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7406 | CurInit = MTE; |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 7407 | |
Brian Kelley | 762f928 | 2017-03-29 18:16:38 +0000 | [diff] [blame] | 7408 | // If we're extending this temporary to automatic storage duration -- we |
| 7409 | // need to register its cleanup during the full-expression's cleanups. |
| 7410 | if (MTE->getStorageDuration() == SD_Automatic && |
| 7411 | MTE->getType().isDestructedType()) |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 7412 | S.Cleanup.setExprNeedsCleanups(true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7413 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 7414 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7415 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7416 | case SK_FinalCopy: |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7417 | if (checkAbstractType(Step->Type)) |
| 7418 | return ExprError(); |
| 7419 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7420 | // If the overall initialization is initializing a temporary, we already |
| 7421 | // bound our argument if it was necessary to do so. If not (if we're |
| 7422 | // ultimately initializing a non-temporary), our argument needs to be |
| 7423 | // bound since it's initializing a function parameter. |
| 7424 | // FIXME: This is a mess. Rationalize temporary destruction. |
| 7425 | if (!shouldBindAsTemporary(Entity)) |
| 7426 | CurInit = S.MaybeBindToTemporary(CurInit.get()); |
| 7427 | CurInit = CopyObject(S, Step->Type, Entity, CurInit, |
| 7428 | /*IsExtraneousCopy=*/false); |
| 7429 | break; |
| 7430 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 7431 | case SK_ExtraneousCopyToTemporary: |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7432 | CurInit = CopyObject(S, Step->Type, Entity, CurInit, |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 7433 | /*IsExtraneousCopy=*/true); |
| 7434 | break; |
| 7435 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7436 | case SK_UserConversion: { |
| 7437 | // We have a user-defined conversion that invokes either a constructor |
| 7438 | // or a conversion function. |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 7439 | CastKind CastKind; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7440 | FunctionDecl *Fn = Step->Function.Function; |
| 7441 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 7442 | bool HadMultipleCandidates = Step->Function.HadMultipleCandidates; |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 7443 | bool CreatedObject = false; |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 7444 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7445 | // Build a call to the selected constructor. |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 7446 | SmallVector<Expr*, 8> ConstructorArgs; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7447 | SourceLocation Loc = CurInit.get()->getBeginLoc(); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 7448 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7449 | // Determine the arguments required to actually perform the constructor |
| 7450 | // call. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7451 | Expr *Arg = CurInit.get(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7452 | if (S.CompleteConstructorCall(Constructor, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7453 | MultiExprArg(&Arg, 1), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7454 | Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7455 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7456 | |
Richard Smith | b24f067 | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 7457 | // Build an expression that constructs a temporary. |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 7458 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, |
| 7459 | FoundFn, Constructor, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7460 | ConstructorArgs, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 7461 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 7462 | /*ListInit*/ false, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 7463 | /*StdInitListInit*/ false, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 7464 | /*ZeroInit*/ false, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 7465 | CXXConstructExpr::CK_Complete, |
| 7466 | SourceRange()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7467 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7468 | return ExprError(); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 7469 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 7470 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, FoundFn, |
| 7471 | Entity); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 7472 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 7473 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7474 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7475 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 7476 | CreatedObject = true; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7477 | } else { |
| 7478 | // Build a call to the conversion function. |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 7479 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7480 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), nullptr, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7481 | FoundFn); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 7482 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 7483 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7484 | |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 7485 | CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion, |
| 7486 | HadMultipleCandidates); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7487 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7488 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7489 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7490 | CastKind = CK_UserDefinedConversion; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7491 | CreatedObject = Conversion->getReturnType()->isRecordType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7492 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7493 | |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7494 | if (CreatedObject && checkAbstractType(CurInit.get()->getType())) |
| 7495 | return ExprError(); |
| 7496 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7497 | CurInit = ImplicitCastExpr::Create(S.Context, CurInit.get()->getType(), |
| 7498 | CastKind, CurInit.get(), nullptr, |
| 7499 | CurInit.get()->getValueKind()); |
Abramo Bagnara | b0cf297 | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 7500 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7501 | if (shouldBindAsTemporary(Entity)) |
| 7502 | // The overall entity is temporary, so this expression should be |
| 7503 | // destroyed at the end of its full-expression. |
| 7504 | CurInit = S.MaybeBindToTemporary(CurInit.getAs<Expr>()); |
| 7505 | else if (CreatedObject && shouldDestroyEntity(Entity)) { |
| 7506 | // The object outlasts the full-expression, but we need to prepare for |
| 7507 | // a destructor being run on it. |
| 7508 | // FIXME: It makes no sense to do this here. This should happen |
| 7509 | // regardless of how we initialized the entity. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7510 | QualType T = CurInit.get()->getType(); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 7511 | if (const RecordType *Record = T->getAs<RecordType>()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7512 | CXXDestructorDecl *Destructor |
Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 7513 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7514 | S.CheckDestructorAccess(CurInit.get()->getBeginLoc(), Destructor, |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 7515 | S.PDiag(diag::err_access_dtor_temp) << T); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7516 | S.MarkFunctionReferenced(CurInit.get()->getBeginLoc(), Destructor); |
| 7517 | if (S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getBeginLoc())) |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 7518 | return ExprError(); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 7519 | } |
| 7520 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7521 | break; |
| 7522 | } |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7523 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7524 | case SK_QualificationConversionLValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7525 | case SK_QualificationConversionXValue: |
| 7526 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7527 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7528 | ExprValueKind VK = |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7529 | Step->Kind == SK_QualificationConversionLValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7530 | VK_LValue : |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7531 | (Step->Kind == SK_QualificationConversionXValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7532 | VK_XValue : |
| 7533 | VK_RValue); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7534 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, CK_NoOp, VK); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7535 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7536 | } |
| 7537 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 7538 | case SK_AtomicConversion: { |
| 7539 | assert(CurInit.get()->isRValue() && "cannot convert glvalue to atomic"); |
| 7540 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
| 7541 | CK_NonAtomicToAtomic, VK_RValue); |
| 7542 | break; |
| 7543 | } |
| 7544 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 7545 | case SK_LValueToRValue: { |
| 7546 | assert(CurInit.get()->isGLValue() && "cannot load from a prvalue"); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7547 | CurInit = ImplicitCastExpr::Create(S.Context, Step->Type, |
| 7548 | CK_LValueToRValue, CurInit.get(), |
| 7549 | /*BasePath=*/nullptr, VK_RValue); |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 7550 | break; |
| 7551 | } |
| 7552 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7553 | case SK_ConversionSequence: |
| 7554 | case SK_ConversionSequenceNoNarrowing: { |
| 7555 | Sema::CheckedConversionKind CCK |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7556 | = Kind.isCStyleCast()? Sema::CCK_CStyleCast |
| 7557 | : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 7558 | : Kind.isExplicitCast()? Sema::CCK_OtherCast |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7559 | : Sema::CCK_ImplicitConversion; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7560 | ExprResult CurInitExprRes = |
| 7561 | S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7562 | getAssignmentAction(Entity), CCK); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7563 | if (CurInitExprRes.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7564 | return ExprError(); |
Roger Ferrer Ibanez | 722a4db | 2016-08-12 08:04:13 +0000 | [diff] [blame] | 7565 | |
| 7566 | S.DiscardMisalignedMemberAddress(Step->Type.getTypePtr(), CurInit.get()); |
| 7567 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7568 | CurInit = CurInitExprRes; |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7569 | |
| 7570 | if (Step->Kind == SK_ConversionSequenceNoNarrowing && |
Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 7571 | S.getLangOpts().CPlusPlus) |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7572 | DiagnoseNarrowingInInitList(S, *Step->ICS, SourceType, Entity.getType(), |
| 7573 | CurInit.get()); |
Roger Ferrer Ibanez | 722a4db | 2016-08-12 08:04:13 +0000 | [diff] [blame] | 7574 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7575 | break; |
Douglas Gregor | 5c8ffab | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 7576 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7577 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7578 | case SK_ListInitialization: { |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7579 | if (checkAbstractType(Step->Type)) |
| 7580 | return ExprError(); |
| 7581 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7582 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 7583 | // If we're not initializing the top-level entity, we need to create an |
| 7584 | // InitializeTemporary entity for our target type. |
| 7585 | QualType Ty = Step->Type; |
| 7586 | bool IsTemporary = !S.Context.hasSameType(Entity.getType(), Ty); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7587 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(Ty); |
Richard Smith | d712d0d | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 7588 | InitializedEntity InitEntity = IsTemporary ? TempEntity : Entity; |
| 7589 | InitListChecker PerformInitList(S, InitEntity, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 7590 | InitList, Ty, /*VerifyOnly=*/false, |
| 7591 | /*TreatUnavailableAsInvalid=*/false); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 7592 | if (PerformInitList.HadError()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7593 | return ExprError(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7594 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 7595 | // Hack: We must update *ResultType if available in order to set the |
| 7596 | // bounds of arrays, e.g. in 'int ar[] = {1, 2, 3};'. |
| 7597 | // Worst case: 'const int (&arref)[] = {1, 2, 3};'. |
| 7598 | if (ResultType && |
| 7599 | ResultType->getNonReferenceType()->isIncompleteArrayType()) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7600 | if ((*ResultType)->isRValueReferenceType()) |
| 7601 | Ty = S.Context.getRValueReferenceType(Ty); |
| 7602 | else if ((*ResultType)->isLValueReferenceType()) |
| 7603 | Ty = S.Context.getLValueReferenceType(Ty, |
| 7604 | (*ResultType)->getAs<LValueReferenceType>()->isSpelledAsLValue()); |
| 7605 | *ResultType = Ty; |
| 7606 | } |
| 7607 | |
| 7608 | InitListExpr *StructuredInitList = |
| 7609 | PerformInitList.getFullyStructuredList(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7610 | CurInit.get(); |
Richard Smith | d712d0d | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 7611 | CurInit = shouldBindAsTemporary(InitEntity) |
| 7612 | ? S.MaybeBindToTemporary(StructuredInitList) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7613 | : StructuredInitList; |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7614 | break; |
| 7615 | } |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 7616 | |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7617 | case SK_ConstructorInitializationFromList: { |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7618 | if (checkAbstractType(Step->Type)) |
| 7619 | return ExprError(); |
| 7620 | |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 7621 | // When an initializer list is passed for a parameter of type "reference |
| 7622 | // to object", we don't get an EK_Temporary entity, but instead an |
| 7623 | // EK_Parameter entity with reference type. |
Sebastian Redl | 99f6616 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 7624 | // FIXME: This is a hack. What we really should do is create a user |
| 7625 | // conversion step for this case, but this makes it considerably more |
| 7626 | // complicated. For now, this will do. |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 7627 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 7628 | Entity.getType().getNonReferenceType()); |
| 7629 | bool UseTemporary = Entity.getType()->isReferenceType(); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 7630 | assert(Args.size() == 1 && "expected a single argument for list init"); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7631 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Richard Smith | 2b349ae | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 7632 | S.Diag(InitList->getExprLoc(), diag::warn_cxx98_compat_ctor_list_init) |
| 7633 | << InitList->getSourceRange(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 7634 | MultiExprArg Arg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 7635 | CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity : |
| 7636 | Entity, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7637 | Kind, Arg, *Step, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 7638 | ConstructorInitRequiresZeroInit, |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7639 | /*IsListInitialization*/true, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 7640 | /*IsStdInitListInit*/false, |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 7641 | InitList->getLBraceLoc(), |
| 7642 | InitList->getRBraceLoc()); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 7643 | break; |
| 7644 | } |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 7645 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7646 | case SK_UnwrapInitList: |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7647 | CurInit = cast<InitListExpr>(CurInit.get())->getInit(0); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7648 | break; |
| 7649 | |
| 7650 | case SK_RewrapInitList: { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7651 | Expr *E = CurInit.get(); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7652 | InitListExpr *Syntactic = Step->WrappingSyntacticList; |
| 7653 | InitListExpr *ILE = new (S.Context) InitListExpr(S.Context, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 7654 | Syntactic->getLBraceLoc(), E, Syntactic->getRBraceLoc()); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7655 | ILE->setSyntacticForm(Syntactic); |
| 7656 | ILE->setType(E->getType()); |
| 7657 | ILE->setValueKind(E->getValueKind()); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7658 | CurInit = ILE; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7659 | break; |
| 7660 | } |
| 7661 | |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7662 | case SK_ConstructorInitialization: |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 7663 | case SK_StdInitializerListConstructorCall: { |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7664 | if (checkAbstractType(Step->Type)) |
| 7665 | return ExprError(); |
| 7666 | |
Sebastian Redl | 99f6616 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 7667 | // When an initializer list is passed for a parameter of type "reference |
| 7668 | // to object", we don't get an EK_Temporary entity, but instead an |
| 7669 | // EK_Parameter entity with reference type. |
| 7670 | // FIXME: This is a hack. What we really should do is create a user |
| 7671 | // conversion step for this case, but this makes it considerably more |
| 7672 | // complicated. For now, this will do. |
| 7673 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 7674 | Entity.getType().getNonReferenceType()); |
| 7675 | bool UseTemporary = Entity.getType()->isReferenceType(); |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 7676 | bool IsStdInitListInit = |
| 7677 | Step->Kind == SK_StdInitializerListConstructorCall; |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7678 | Expr *Source = CurInit.get(); |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 7679 | SourceRange Range = Kind.hasParenOrBraceRange() |
| 7680 | ? Kind.getParenOrBraceRange() |
| 7681 | : SourceRange(); |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7682 | CurInit = PerformConstructorInitialization( |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7683 | S, UseTemporary ? TempEntity : Entity, Kind, |
| 7684 | Source ? MultiExprArg(Source) : Args, *Step, |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7685 | ConstructorInitRequiresZeroInit, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7686 | /*IsListInitialization*/ IsStdInitListInit, |
| 7687 | /*IsStdInitListInitialization*/ IsStdInitListInit, |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 7688 | /*LBraceLoc*/ Range.getBegin(), |
| 7689 | /*RBraceLoc*/ Range.getEnd()); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 7690 | break; |
Sebastian Redl | 99f6616 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 7691 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7692 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 7693 | case SK_ZeroInitialization: { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7694 | step_iterator NextStep = Step; |
| 7695 | ++NextStep; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7696 | if (NextStep != StepEnd && |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 7697 | (NextStep->Kind == SK_ConstructorInitialization || |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7698 | NextStep->Kind == SK_ConstructorInitializationFromList)) { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7699 | // The need for zero-initialization is recorded directly into |
| 7700 | // the call to the object's constructor within the next step. |
| 7701 | ConstructorInitRequiresZeroInit = true; |
| 7702 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7703 | S.getLangOpts().CPlusPlus && |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7704 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 7705 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 7706 | if (!TSInfo) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7707 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 7708 | Kind.getRange().getBegin()); |
| 7709 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7710 | CurInit = new (S.Context) CXXScalarValueInitExpr( |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 7711 | Entity.getType().getNonLValueExprType(S.Context), TSInfo, |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7712 | Kind.getRange().getEnd()); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7713 | } else { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7714 | CurInit = new (S.Context) ImplicitValueInitExpr(Step->Type); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7715 | } |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 7716 | break; |
| 7717 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7718 | |
| 7719 | case SK_CAssignment: { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7720 | QualType SourceType = CurInit.get()->getType(); |
George Burgess IV | 5f21c71 | 2015-10-12 19:57:04 +0000 | [diff] [blame] | 7721 | // Save off the initial CurInit in case we need to emit a diagnostic |
| 7722 | ExprResult InitialCurInit = CurInit; |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7723 | ExprResult Result = CurInit; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7724 | Sema::AssignConvertType ConvTy = |
Fariborz Jahanian | 25eef19 | 2013-07-31 21:40:51 +0000 | [diff] [blame] | 7725 | S.CheckSingleAssignmentConstraints(Step->Type, Result, true, |
| 7726 | Entity.getKind() == InitializedEntity::EK_Parameter_CF_Audited); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7727 | if (Result.isInvalid()) |
| 7728 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7729 | CurInit = Result; |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 7730 | |
| 7731 | // If this is a call, allow conversion to a transparent union. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7732 | ExprResult CurInitExprRes = CurInit; |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 7733 | if (ConvTy != Sema::Compatible && |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 7734 | Entity.isParameterKind() && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7735 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes) |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 7736 | == Sema::Compatible) |
| 7737 | ConvTy = Sema::Compatible; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7738 | if (CurInitExprRes.isInvalid()) |
| 7739 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7740 | CurInit = CurInitExprRes; |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 7741 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 7742 | bool Complained; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7743 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 7744 | Step->Type, SourceType, |
George Burgess IV | 5f21c71 | 2015-10-12 19:57:04 +0000 | [diff] [blame] | 7745 | InitialCurInit.get(), |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 7746 | getAssignmentAction(Entity, true), |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 7747 | &Complained)) { |
| 7748 | PrintInitLocationNote(S, Entity); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7749 | return ExprError(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 7750 | } else if (Complained) |
| 7751 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7752 | break; |
| 7753 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 7754 | |
| 7755 | case SK_StringInit: { |
| 7756 | QualType Ty = Step->Type; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7757 | CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty, |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 7758 | S.Context.getAsArrayType(Ty), S); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 7759 | break; |
| 7760 | } |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 7761 | |
| 7762 | case SK_ObjCObjectConversion: |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7763 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7764 | CK_ObjCObjectLValueCast, |
Eli Friedman | be4b363 | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 7765 | CurInit.get()->getValueKind()); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 7766 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7767 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7768 | case SK_ArrayLoopIndex: { |
| 7769 | Expr *Cur = CurInit.get(); |
| 7770 | Expr *BaseExpr = new (S.Context) |
| 7771 | OpaqueValueExpr(Cur->getExprLoc(), Cur->getType(), |
| 7772 | Cur->getValueKind(), Cur->getObjectKind(), Cur); |
| 7773 | Expr *IndexExpr = |
| 7774 | new (S.Context) ArrayInitIndexExpr(S.Context.getSizeType()); |
| 7775 | CurInit = S.CreateBuiltinArraySubscriptExpr( |
| 7776 | BaseExpr, Kind.getLocation(), IndexExpr, Kind.getLocation()); |
| 7777 | ArrayLoopCommonExprs.push_back(BaseExpr); |
| 7778 | break; |
| 7779 | } |
| 7780 | |
| 7781 | case SK_ArrayLoopInit: { |
| 7782 | assert(!ArrayLoopCommonExprs.empty() && |
| 7783 | "mismatched SK_ArrayLoopIndex and SK_ArrayLoopInit"); |
| 7784 | Expr *Common = ArrayLoopCommonExprs.pop_back_val(); |
| 7785 | CurInit = new (S.Context) ArrayInitLoopExpr(Step->Type, Common, |
| 7786 | CurInit.get()); |
| 7787 | break; |
| 7788 | } |
| 7789 | |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 7790 | case SK_GNUArrayInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7791 | // Okay: we checked everything before creating this step. Note that |
| 7792 | // this is a GNU extension. |
| 7793 | S.Diag(Kind.getLocation(), diag::ext_array_init_copy) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7794 | << Step->Type << CurInit.get()->getType() |
| 7795 | << CurInit.get()->getSourceRange(); |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 7796 | LLVM_FALLTHROUGH; |
| 7797 | case SK_ArrayInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7798 | // If the destination type is an incomplete array type, update the |
| 7799 | // type accordingly. |
| 7800 | if (ResultType) { |
| 7801 | if (const IncompleteArrayType *IncompleteDest |
| 7802 | = S.Context.getAsIncompleteArrayType(Step->Type)) { |
| 7803 | if (const ConstantArrayType *ConstantSource |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7804 | = S.Context.getAsConstantArrayType(CurInit.get()->getType())) { |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7805 | *ResultType = S.Context.getConstantArrayType( |
| 7806 | IncompleteDest->getElementType(), |
| 7807 | ConstantSource->getSize(), |
| 7808 | ArrayType::Normal, 0); |
| 7809 | } |
| 7810 | } |
| 7811 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7812 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7813 | |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 7814 | case SK_ParenthesizedArrayInit: |
| 7815 | // Okay: we checked everything before creating this step. Note that |
| 7816 | // this is a GNU extension. |
| 7817 | S.Diag(Kind.getLocation(), diag::ext_array_init_parens) |
| 7818 | << CurInit.get()->getSourceRange(); |
| 7819 | break; |
| 7820 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7821 | case SK_PassByIndirectCopyRestore: |
| 7822 | case SK_PassByIndirectRestore: |
| 7823 | checkIndirectCopyRestoreSource(S, CurInit.get()); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7824 | CurInit = new (S.Context) ObjCIndirectCopyRestoreExpr( |
| 7825 | CurInit.get(), Step->Type, |
| 7826 | Step->Kind == SK_PassByIndirectCopyRestore); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7827 | break; |
| 7828 | |
| 7829 | case SK_ProduceObjCObject: |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7830 | CurInit = |
| 7831 | ImplicitCastExpr::Create(S.Context, Step->Type, CK_ARCProduceObject, |
| 7832 | CurInit.get(), nullptr, VK_RValue); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7833 | break; |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 7834 | |
| 7835 | case SK_StdInitializerList: { |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 7836 | S.Diag(CurInit.get()->getExprLoc(), |
| 7837 | diag::warn_cxx98_compat_initializer_list_init) |
| 7838 | << CurInit.get()->getSourceRange(); |
Sebastian Redl | 249dee5 | 2012-03-05 19:35:43 +0000 | [diff] [blame] | 7839 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 7840 | // Materialize the temporary into memory. |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 7841 | MaterializeTemporaryExpr *MTE = S.CreateMaterializeTemporaryExpr( |
| 7842 | CurInit.get()->getType(), CurInit.get(), |
| 7843 | /*BoundToLvalueReference=*/false); |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 7844 | |
Florian Hahn | 0aa117d | 2018-07-17 09:23:31 +0000 | [diff] [blame] | 7845 | // Wrap it in a construction of a std::initializer_list<T>. |
| 7846 | CurInit = new (S.Context) CXXStdInitializerListExpr(Step->Type, MTE); |
Richard Smith | 0a9969b | 2018-07-17 00:11:41 +0000 | [diff] [blame] | 7847 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 7848 | // Bind the result, in case the library has given initializer_list a |
| 7849 | // non-trivial destructor. |
| 7850 | if (shouldBindAsTemporary(Entity)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7851 | CurInit = S.MaybeBindToTemporary(CurInit.get()); |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 7852 | break; |
| 7853 | } |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 7854 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 7855 | case SK_OCLSamplerInit: { |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 7856 | // Sampler initialzation have 5 cases: |
| 7857 | // 1. function argument passing |
| 7858 | // 1a. argument is a file-scope variable |
| 7859 | // 1b. argument is a function-scope variable |
| 7860 | // 1c. argument is one of caller function's parameters |
| 7861 | // 2. variable initialization |
| 7862 | // 2a. initializing a file-scope variable |
| 7863 | // 2b. initializing a function-scope variable |
| 7864 | // |
| 7865 | // For file-scope variables, since they cannot be initialized by function |
| 7866 | // call of __translate_sampler_initializer in LLVM IR, their references |
| 7867 | // need to be replaced by a cast from their literal initializers to |
| 7868 | // sampler type. Since sampler variables can only be used in function |
| 7869 | // calls as arguments, we only need to replace them when handling the |
| 7870 | // argument passing. |
| 7871 | assert(Step->Type->isSamplerT() && |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 7872 | "Sampler initialization on non-sampler type."); |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 7873 | Expr *Init = CurInit.get(); |
| 7874 | QualType SourceType = Init->getType(); |
| 7875 | // Case 1 |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 7876 | if (Entity.isParameterKind()) { |
Egor Churaev | a8d2451 | 2017-04-05 09:02:56 +0000 | [diff] [blame] | 7877 | if (!SourceType->isSamplerT() && !SourceType->isIntegerType()) { |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 7878 | S.Diag(Kind.getLocation(), diag::err_sampler_argument_required) |
| 7879 | << SourceType; |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 7880 | break; |
| 7881 | } else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Init)) { |
| 7882 | auto Var = cast<VarDecl>(DRE->getDecl()); |
| 7883 | // Case 1b and 1c |
| 7884 | // No cast from integer to sampler is needed. |
| 7885 | if (!Var->hasGlobalStorage()) { |
| 7886 | CurInit = ImplicitCastExpr::Create(S.Context, Step->Type, |
| 7887 | CK_LValueToRValue, Init, |
| 7888 | /*BasePath=*/nullptr, VK_RValue); |
| 7889 | break; |
| 7890 | } |
| 7891 | // Case 1a |
| 7892 | // For function call with a file-scope sampler variable as argument, |
| 7893 | // get the integer literal. |
| 7894 | // Do not diagnose if the file-scope variable does not have initializer |
| 7895 | // since this has already been diagnosed when parsing the variable |
| 7896 | // declaration. |
| 7897 | if (!Var->getInit() || !isa<ImplicitCastExpr>(Var->getInit())) |
| 7898 | break; |
| 7899 | Init = cast<ImplicitCastExpr>(const_cast<Expr*>( |
| 7900 | Var->getInit()))->getSubExpr(); |
| 7901 | SourceType = Init->getType(); |
| 7902 | } |
| 7903 | } else { |
| 7904 | // Case 2 |
| 7905 | // Check initializer is 32 bit integer constant. |
| 7906 | // If the initializer is taken from global variable, do not diagnose since |
| 7907 | // this has already been done when parsing the variable declaration. |
| 7908 | if (!Init->isConstantInitializer(S.Context, false)) |
| 7909 | break; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7910 | |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 7911 | if (!SourceType->isIntegerType() || |
| 7912 | 32 != S.Context.getIntWidth(SourceType)) { |
| 7913 | S.Diag(Kind.getLocation(), diag::err_sampler_initializer_not_integer) |
| 7914 | << SourceType; |
| 7915 | break; |
| 7916 | } |
| 7917 | |
| 7918 | llvm::APSInt Result; |
| 7919 | Init->EvaluateAsInt(Result, S.Context); |
| 7920 | const uint64_t SamplerValue = Result.getLimitedValue(); |
| 7921 | // 32-bit value of sampler's initializer is interpreted as |
| 7922 | // bit-field with the following structure: |
| 7923 | // |unspecified|Filter|Addressing Mode| Normalized Coords| |
| 7924 | // |31 6|5 4|3 1| 0| |
| 7925 | // This structure corresponds to enum values of sampler properties |
| 7926 | // defined in SPIR spec v1.2 and also opencl-c.h |
| 7927 | unsigned AddressingMode = (0x0E & SamplerValue) >> 1; |
| 7928 | unsigned FilterMode = (0x30 & SamplerValue) >> 4; |
| 7929 | if (FilterMode != 1 && FilterMode != 2) |
| 7930 | S.Diag(Kind.getLocation(), |
| 7931 | diag::warn_sampler_initializer_invalid_bits) |
| 7932 | << "Filter Mode"; |
| 7933 | if (AddressingMode > 4) |
| 7934 | S.Diag(Kind.getLocation(), |
| 7935 | diag::warn_sampler_initializer_invalid_bits) |
| 7936 | << "Addressing Mode"; |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 7937 | } |
| 7938 | |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 7939 | // Cases 1a, 2a and 2b |
| 7940 | // Insert cast from integer to sampler. |
| 7941 | CurInit = S.ImpCastExprToType(Init, S.Context.OCLSamplerTy, |
| 7942 | CK_IntToOCLSampler); |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 7943 | break; |
| 7944 | } |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7945 | case SK_OCLZeroEvent: { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7946 | assert(Step->Type->isEventT() && |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 7947 | "Event initialization on non-event type."); |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7948 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7949 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7950 | CK_ZeroToOCLEvent, |
| 7951 | CurInit.get()->getValueKind()); |
| 7952 | break; |
| 7953 | } |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 7954 | case SK_OCLZeroQueue: { |
| 7955 | assert(Step->Type->isQueueT() && |
| 7956 | "Event initialization on non queue type."); |
| 7957 | |
| 7958 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
| 7959 | CK_ZeroToOCLQueue, |
| 7960 | CurInit.get()->getValueKind()); |
| 7961 | break; |
| 7962 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7963 | } |
| 7964 | } |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 7965 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7966 | // Check whether the initializer has a shorter lifetime than the initialized |
| 7967 | // entity, and if not, either lifetime-extend or warn as appropriate. |
| 7968 | if (auto *Init = CurInit.get()) |
| 7969 | S.checkInitializerLifetime(Entity, Init); |
| 7970 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 7971 | // Diagnose non-fatal problems with the completed initialization. |
| 7972 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 7973 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 7974 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 7975 | cast<FieldDecl>(Entity.getDecl()), |
| 7976 | CurInit.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7977 | |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7978 | // Check for std::move on construction. |
| 7979 | if (const Expr *E = CurInit.get()) { |
| 7980 | CheckMoveOnConstruction(S, E, |
| 7981 | Entity.getKind() == InitializedEntity::EK_Result); |
| 7982 | } |
| 7983 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7984 | return CurInit; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7985 | } |
| 7986 | |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 7987 | /// Somewhere within T there is an uninitialized reference subobject. |
| 7988 | /// Dig it out and diagnose it. |
Benjamin Kramer | 3e35026 | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 7989 | static bool DiagnoseUninitializedReference(Sema &S, SourceLocation Loc, |
| 7990 | QualType T) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 7991 | if (T->isReferenceType()) { |
| 7992 | S.Diag(Loc, diag::err_reference_without_init) |
| 7993 | << T.getNonReferenceType(); |
| 7994 | return true; |
| 7995 | } |
| 7996 | |
| 7997 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 7998 | if (!RD || !RD->hasUninitializedReferenceMember()) |
| 7999 | return false; |
| 8000 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 8001 | for (const auto *FI : RD->fields()) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 8002 | if (FI->isUnnamedBitfield()) |
| 8003 | continue; |
| 8004 | |
| 8005 | if (DiagnoseUninitializedReference(S, FI->getLocation(), FI->getType())) { |
| 8006 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 8007 | return true; |
| 8008 | } |
| 8009 | } |
| 8010 | |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 8011 | for (const auto &BI : RD->bases()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8012 | if (DiagnoseUninitializedReference(S, BI.getBeginLoc(), BI.getType())) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 8013 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 8014 | return true; |
| 8015 | } |
| 8016 | } |
| 8017 | |
| 8018 | return false; |
| 8019 | } |
| 8020 | |
| 8021 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8022 | //===----------------------------------------------------------------------===// |
| 8023 | // Diagnose initialization failures |
| 8024 | //===----------------------------------------------------------------------===// |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 8025 | |
| 8026 | /// Emit notes associated with an initialization that failed due to a |
| 8027 | /// "simple" conversion failure. |
| 8028 | static void emitBadConversionNotes(Sema &S, const InitializedEntity &entity, |
| 8029 | Expr *op) { |
| 8030 | QualType destType = entity.getType(); |
| 8031 | if (destType.getNonReferenceType()->isObjCObjectPointerType() && |
| 8032 | op->getType()->isObjCObjectPointerType()) { |
| 8033 | |
| 8034 | // Emit a possible note about the conversion failing because the |
| 8035 | // operand is a message send with a related result type. |
| 8036 | S.EmitRelatedResultTypeNote(op); |
| 8037 | |
| 8038 | // Emit a possible note about a return failing because we're |
| 8039 | // expecting a related result type. |
| 8040 | if (entity.getKind() == InitializedEntity::EK_Result) |
| 8041 | S.EmitRelatedResultTypeNoteForReturn(destType); |
| 8042 | } |
| 8043 | } |
| 8044 | |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 8045 | static void diagnoseListInit(Sema &S, const InitializedEntity &Entity, |
| 8046 | InitListExpr *InitList) { |
| 8047 | QualType DestType = Entity.getType(); |
| 8048 | |
| 8049 | QualType E; |
| 8050 | if (S.getLangOpts().CPlusPlus11 && S.isStdInitializerList(DestType, &E)) { |
| 8051 | QualType ArrayType = S.Context.getConstantArrayType( |
| 8052 | E.withConst(), |
| 8053 | llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 8054 | InitList->getNumInits()), |
| 8055 | clang::ArrayType::Normal, 0); |
| 8056 | InitializedEntity HiddenArray = |
| 8057 | InitializedEntity::InitializeTemporary(ArrayType); |
| 8058 | return diagnoseListInit(S, HiddenArray, InitList); |
| 8059 | } |
| 8060 | |
Richard Smith | 8d082d1 | 2014-09-04 22:13:39 +0000 | [diff] [blame] | 8061 | if (DestType->isReferenceType()) { |
| 8062 | // A list-initialization failure for a reference means that we tried to |
| 8063 | // create a temporary of the inner type (per [dcl.init.list]p3.6) and the |
| 8064 | // inner initialization failed. |
| 8065 | QualType T = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 8066 | diagnoseListInit(S, InitializedEntity::InitializeTemporary(T), InitList); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8067 | SourceLocation Loc = InitList->getBeginLoc(); |
Richard Smith | 8d082d1 | 2014-09-04 22:13:39 +0000 | [diff] [blame] | 8068 | if (auto *D = Entity.getDecl()) |
| 8069 | Loc = D->getLocation(); |
| 8070 | S.Diag(Loc, diag::note_in_reference_temporary_list_initializer) << T; |
| 8071 | return; |
| 8072 | } |
| 8073 | |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 8074 | InitListChecker DiagnoseInitList(S, Entity, InitList, DestType, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 8075 | /*VerifyOnly=*/false, |
| 8076 | /*TreatUnavailableAsInvalid=*/false); |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 8077 | assert(DiagnoseInitList.HadError() && |
| 8078 | "Inconsistent init list check result."); |
| 8079 | } |
| 8080 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8081 | bool InitializationSequence::Diagnose(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8082 | const InitializedEntity &Entity, |
| 8083 | const InitializationKind &Kind, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8084 | ArrayRef<Expr *> Args) { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 8085 | if (!Failed()) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8086 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8087 | |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8088 | // When we want to diagnose only one element of a braced-init-list, |
| 8089 | // we need to factor it out. |
| 8090 | Expr *OnlyArg; |
| 8091 | if (Args.size() == 1) { |
| 8092 | auto *List = dyn_cast<InitListExpr>(Args[0]); |
| 8093 | if (List && List->getNumInits() == 1) |
| 8094 | OnlyArg = List->getInit(0); |
| 8095 | else |
| 8096 | OnlyArg = Args[0]; |
| 8097 | } |
| 8098 | else |
| 8099 | OnlyArg = nullptr; |
| 8100 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 8101 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8102 | switch (Failure) { |
| 8103 | case FK_TooManyInitsForReference: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8104 | // FIXME: Customize for the initialized entity? |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8105 | if (Args.empty()) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 8106 | // Dig out the reference subobject which is uninitialized and diagnose it. |
| 8107 | // If this is value-initialization, this could be nested some way within |
| 8108 | // the target type. |
| 8109 | assert(Kind.getKind() == InitializationKind::IK_Value || |
| 8110 | DestType->isReferenceType()); |
| 8111 | bool Diagnosed = |
| 8112 | DiagnoseUninitializedReference(S, Kind.getLocation(), DestType); |
| 8113 | assert(Diagnosed && "couldn't find uninitialized reference to diagnose"); |
| 8114 | (void)Diagnosed; |
| 8115 | } else // FIXME: diagnostic below could be better! |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8116 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 8117 | << SourceRange(Args.front()->getBeginLoc(), Args.back()->getEndLoc()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8118 | break; |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 8119 | case FK_ParenthesizedListInitForReference: |
| 8120 | S.Diag(Kind.getLocation(), diag::err_list_init_in_parens) |
| 8121 | << 1 << Entity.getType() << Args[0]->getSourceRange(); |
| 8122 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8123 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8124 | case FK_ArrayNeedsInitList: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 8125 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 0; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8126 | break; |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 8127 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 8128 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 1; |
| 8129 | break; |
| 8130 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 8131 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 2; |
| 8132 | break; |
| 8133 | case FK_NarrowStringIntoWideCharArray: |
| 8134 | S.Diag(Kind.getLocation(), diag::err_array_init_narrow_string_into_wchar); |
| 8135 | break; |
| 8136 | case FK_WideStringIntoCharArray: |
| 8137 | S.Diag(Kind.getLocation(), diag::err_array_init_wide_string_into_char); |
| 8138 | break; |
| 8139 | case FK_IncompatWideStringIntoWideChar: |
| 8140 | S.Diag(Kind.getLocation(), |
| 8141 | diag::err_array_init_incompat_wide_string_into_wchar); |
| 8142 | break; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 8143 | case FK_PlainStringIntoUTF8Char: |
| 8144 | S.Diag(Kind.getLocation(), |
| 8145 | diag::err_array_init_plain_string_into_char8_t); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8146 | S.Diag(Args.front()->getBeginLoc(), |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 8147 | diag::note_array_init_plain_string_into_char8_t) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8148 | << FixItHint::CreateInsertion(Args.front()->getBeginLoc(), "u8"); |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 8149 | break; |
| 8150 | case FK_UTF8StringIntoPlainChar: |
| 8151 | S.Diag(Kind.getLocation(), |
| 8152 | diag::err_array_init_utf8_string_into_char); |
| 8153 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8154 | case FK_ArrayTypeMismatch: |
| 8155 | case FK_NonConstantArrayInit: |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 8156 | S.Diag(Kind.getLocation(), |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8157 | (Failure == FK_ArrayTypeMismatch |
| 8158 | ? diag::err_array_init_different_type |
| 8159 | : diag::err_array_init_non_constant_array)) |
| 8160 | << DestType.getNonReferenceType() |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8161 | << OnlyArg->getType() |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8162 | << Args[0]->getSourceRange(); |
| 8163 | break; |
| 8164 | |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 8165 | case FK_VariableLengthArrayHasInitializer: |
| 8166 | S.Diag(Kind.getLocation(), diag::err_variable_object_no_init) |
| 8167 | << Args[0]->getSourceRange(); |
| 8168 | break; |
| 8169 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8170 | case FK_AddressOfOverloadFailed: { |
| 8171 | DeclAccessPair Found; |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8172 | S.ResolveAddressOfOverloadedFunction(OnlyArg, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8173 | DestType.getNonReferenceType(), |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8174 | true, |
| 8175 | Found); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8176 | break; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8177 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8178 | |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 8179 | case FK_AddressOfUnaddressableFunction: { |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8180 | auto *FD = cast<FunctionDecl>(cast<DeclRefExpr>(OnlyArg)->getDecl()); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 8181 | S.checkAddressOfFunctionIsAvailable(FD, /*Complain=*/true, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8182 | OnlyArg->getBeginLoc()); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 8183 | break; |
| 8184 | } |
| 8185 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8186 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 8187 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8188 | switch (FailedOverloadResult) { |
| 8189 | case OR_Ambiguous: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8190 | if (Failure == FK_UserConversionOverloadFailed) |
| 8191 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8192 | << OnlyArg->getType() << DestType |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8193 | << Args[0]->getSourceRange(); |
| 8194 | else |
| 8195 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8196 | << DestType << OnlyArg->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8197 | << Args[0]->getSourceRange(); |
| 8198 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8199 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8200 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8201 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8202 | case OR_No_Viable_Function: |
Larisse Voufo | 70bb43a | 2013-06-27 03:36:30 +0000 | [diff] [blame] | 8203 | if (!S.RequireCompleteType(Kind.getLocation(), |
Larisse Voufo | 64cf3ef | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 8204 | DestType.getNonReferenceType(), |
| 8205 | diag::err_typecheck_nonviable_condition_incomplete, |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8206 | OnlyArg->getType(), Args[0]->getSourceRange())) |
Larisse Voufo | 64cf3ef | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 8207 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
Nick Lewycky | 08426e2 | 2015-08-25 22:18:46 +0000 | [diff] [blame] | 8208 | << (Entity.getKind() == InitializedEntity::EK_Result) |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8209 | << OnlyArg->getType() << Args[0]->getSourceRange() |
Larisse Voufo | 64cf3ef | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 8210 | << DestType.getNonReferenceType(); |
| 8211 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8212 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8213 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8214 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8215 | case OR_Deleted: { |
| 8216 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8217 | << OnlyArg->getType() << DestType.getNonReferenceType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8218 | << Args[0]->getSourceRange(); |
| 8219 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8220 | OverloadingResult Ovl |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 8221 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8222 | if (Ovl == OR_Deleted) { |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 8223 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8224 | } else { |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 8225 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8226 | } |
| 8227 | break; |
| 8228 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8229 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8230 | case OR_Success: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 8231 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8232 | } |
| 8233 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8234 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8235 | case FK_NonConstLValueReferenceBindingToTemporary: |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8236 | if (isa<InitListExpr>(Args[0])) { |
| 8237 | S.Diag(Kind.getLocation(), |
| 8238 | diag::err_lvalue_reference_bind_to_initlist) |
| 8239 | << DestType.getNonReferenceType().isVolatileQualified() |
| 8240 | << DestType.getNonReferenceType() |
| 8241 | << Args[0]->getSourceRange(); |
| 8242 | break; |
| 8243 | } |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 8244 | LLVM_FALLTHROUGH; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8245 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8246 | case FK_NonConstLValueReferenceBindingToUnrelated: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8247 | S.Diag(Kind.getLocation(), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8248 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 8249 | ? diag::err_lvalue_reference_bind_to_temporary |
| 8250 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | d1e0864 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 8251 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8252 | << DestType.getNonReferenceType() |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8253 | << OnlyArg->getType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8254 | << Args[0]->getSourceRange(); |
| 8255 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8256 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8257 | case FK_NonConstLValueReferenceBindingToBitfield: { |
| 8258 | // We don't necessarily have an unambiguous source bit-field. |
| 8259 | FieldDecl *BitField = Args[0]->getSourceBitField(); |
| 8260 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
| 8261 | << DestType.isVolatileQualified() |
| 8262 | << (BitField ? BitField->getDeclName() : DeclarationName()) |
| 8263 | << (BitField != nullptr) |
| 8264 | << Args[0]->getSourceRange(); |
| 8265 | if (BitField) |
| 8266 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
| 8267 | break; |
| 8268 | } |
| 8269 | |
| 8270 | case FK_NonConstLValueReferenceBindingToVectorElement: |
| 8271 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 8272 | << DestType.isVolatileQualified() |
| 8273 | << Args[0]->getSourceRange(); |
| 8274 | break; |
| 8275 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8276 | case FK_RValueReferenceBindingToLValue: |
| 8277 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8278 | << DestType.getNonReferenceType() << OnlyArg->getType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8279 | << Args[0]->getSourceRange(); |
| 8280 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8281 | |
Richard Trieu | f956a49 | 2015-05-16 01:27:03 +0000 | [diff] [blame] | 8282 | case FK_ReferenceInitDropsQualifiers: { |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8283 | QualType SourceType = OnlyArg->getType(); |
Richard Trieu | f956a49 | 2015-05-16 01:27:03 +0000 | [diff] [blame] | 8284 | QualType NonRefType = DestType.getNonReferenceType(); |
| 8285 | Qualifiers DroppedQualifiers = |
| 8286 | SourceType.getQualifiers() - NonRefType.getQualifiers(); |
| 8287 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8288 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
Richard Trieu | f956a49 | 2015-05-16 01:27:03 +0000 | [diff] [blame] | 8289 | << SourceType |
| 8290 | << NonRefType |
| 8291 | << DroppedQualifiers.getCVRQualifiers() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8292 | << Args[0]->getSourceRange(); |
| 8293 | break; |
Richard Trieu | f956a49 | 2015-05-16 01:27:03 +0000 | [diff] [blame] | 8294 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8295 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8296 | case FK_ReferenceInitFailed: |
| 8297 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 8298 | << DestType.getNonReferenceType() |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8299 | << OnlyArg->isLValue() |
| 8300 | << OnlyArg->getType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8301 | << Args[0]->getSourceRange(); |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 8302 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8303 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8304 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8305 | case FK_ConversionFailed: { |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8306 | QualType FromType = OnlyArg->getType(); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8307 | PartialDiagnostic PDiag = S.PDiag(diag::err_init_conversion_failed) |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8308 | << (int)Entity.getKind() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8309 | << DestType |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8310 | << OnlyArg->isLValue() |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8311 | << FromType |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8312 | << Args[0]->getSourceRange(); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8313 | S.HandleFunctionTypeMismatch(PDiag, FromType, DestType); |
| 8314 | S.Diag(Kind.getLocation(), PDiag); |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 8315 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8316 | break; |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8317 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8318 | |
| 8319 | case FK_ConversionFromPropertyFailed: |
| 8320 | // No-op. This error has already been reported. |
| 8321 | break; |
| 8322 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8323 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 8324 | SourceRange R; |
| 8325 | |
David Majnemer | bd38544 | 2015-04-10 04:52:06 +0000 | [diff] [blame] | 8326 | auto *InitList = dyn_cast<InitListExpr>(Args[0]); |
Benjamin Kramer | c4284e3 | 2015-09-23 16:03:53 +0000 | [diff] [blame] | 8327 | if (InitList && InitList->getNumInits() >= 1) { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 8328 | R = SourceRange(InitList->getInit(0)->getEndLoc(), InitList->getEndLoc()); |
Benjamin Kramer | c4284e3 | 2015-09-23 16:03:53 +0000 | [diff] [blame] | 8329 | } else { |
| 8330 | assert(Args.size() > 1 && "Expected multiple initializers!"); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 8331 | R = SourceRange(Args.front()->getEndLoc(), Args.back()->getEndLoc()); |
Benjamin Kramer | c4284e3 | 2015-09-23 16:03:53 +0000 | [diff] [blame] | 8332 | } |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8333 | |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 8334 | R.setBegin(S.getLocForEndOfToken(R.getBegin())); |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 8335 | if (Kind.isCStyleOrFunctionalCast()) |
| 8336 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 8337 | << R; |
| 8338 | else |
| 8339 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 8340 | << /*scalar=*/2 << R; |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8341 | break; |
| 8342 | } |
| 8343 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 8344 | case FK_ParenthesizedListInitForScalar: |
| 8345 | S.Diag(Kind.getLocation(), diag::err_list_init_in_parens) |
| 8346 | << 0 << Entity.getType() << Args[0]->getSourceRange(); |
| 8347 | break; |
| 8348 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8349 | case FK_ReferenceBindingToInitList: |
| 8350 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 8351 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 8352 | break; |
| 8353 | |
| 8354 | case FK_InitListBadDestinationType: |
| 8355 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 8356 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 8357 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8358 | |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 8359 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8360 | case FK_ConstructorOverloadFailed: { |
| 8361 | SourceRange ArgsRange; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8362 | if (Args.size()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8363 | ArgsRange = |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 8364 | SourceRange(Args.front()->getBeginLoc(), Args.back()->getEndLoc()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8365 | |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 8366 | if (Failure == FK_ListConstructorOverloadFailed) { |
Nico Weber | 9709ebf | 2014-07-08 23:54:25 +0000 | [diff] [blame] | 8367 | assert(Args.size() == 1 && |
| 8368 | "List construction from other than 1 argument."); |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 8369 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8370 | Args = MultiExprArg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 8371 | } |
| 8372 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8373 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 8374 | // bad. |
| 8375 | switch (FailedOverloadResult) { |
| 8376 | case OR_Ambiguous: |
| 8377 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 8378 | << DestType << ArgsRange; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8379 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8380 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8381 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8382 | case OR_No_Viable_Function: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8383 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 8384 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 8385 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 8386 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 8387 | // This is implicit default initialization of a member or |
| 8388 | // base within a constructor. If no viable function was |
Nico Weber | a691689 | 2016-06-10 18:53:04 +0000 | [diff] [blame] | 8389 | // found, notify the user that they need to explicitly |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8390 | // initialize this base/member. |
| 8391 | CXXConstructorDecl *Constructor |
| 8392 | = cast<CXXConstructorDecl>(S.CurContext); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 8393 | const CXXRecordDecl *InheritedFrom = nullptr; |
| 8394 | if (auto Inherited = Constructor->getInheritedConstructor()) |
| 8395 | InheritedFrom = Inherited.getShadowDecl()->getNominatedBaseClass(); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8396 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 8397 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 8398 | << (InheritedFrom ? 2 : Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8399 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 8400 | << /*base=*/0 |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 8401 | << Entity.getType() |
| 8402 | << InheritedFrom; |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8403 | |
| 8404 | RecordDecl *BaseDecl |
| 8405 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 8406 | ->getDecl(); |
| 8407 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 8408 | << S.Context.getTagDeclType(BaseDecl); |
| 8409 | } else { |
| 8410 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 8411 | << (InheritedFrom ? 2 : Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8412 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 8413 | << /*member=*/1 |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 8414 | << Entity.getName() |
| 8415 | << InheritedFrom; |
Alp Toker | 2afa878 | 2014-05-28 12:20:14 +0000 | [diff] [blame] | 8416 | S.Diag(Entity.getDecl()->getLocation(), |
| 8417 | diag::note_member_declared_at); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8418 | |
| 8419 | if (const RecordType *Record |
| 8420 | = Entity.getType()->getAs<RecordType>()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8421 | S.Diag(Record->getDecl()->getLocation(), |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8422 | diag::note_previous_decl) |
| 8423 | << S.Context.getTagDeclType(Record->getDecl()); |
| 8424 | } |
| 8425 | break; |
| 8426 | } |
| 8427 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8428 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 8429 | << DestType << ArgsRange; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8430 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8431 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8432 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8433 | case OR_Deleted: { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8434 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8435 | OverloadingResult Ovl |
| 8436 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 8437 | if (Ovl != OR_Deleted) { |
| 8438 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 8439 | << true << DestType << ArgsRange; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8440 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 8441 | break; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8442 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8443 | |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 8444 | // If this is a defaulted or implicitly-declared function, then |
| 8445 | // it was implicitly deleted. Make it clear that the deletion was |
| 8446 | // implicit. |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 8447 | if (S.isImplicitlyDeleted(Best->Function)) |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 8448 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_special_init) |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 8449 | << S.getSpecialMember(cast<CXXMethodDecl>(Best->Function)) |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 8450 | << DestType << ArgsRange; |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 8451 | else |
| 8452 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 8453 | << true << DestType << ArgsRange; |
| 8454 | |
| 8455 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8456 | break; |
| 8457 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8458 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8459 | case OR_Success: |
| 8460 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8461 | } |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8462 | } |
David Blaikie | 60deeee | 2012-01-17 08:24:58 +0000 | [diff] [blame] | 8463 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8464 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 8465 | case FK_DefaultInitOfConst: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8466 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 8467 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 8468 | // This is implicit default-initialization of a const member in |
| 8469 | // a constructor. Complain that it needs to be explicitly |
| 8470 | // initialized. |
| 8471 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 8472 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 8473 | << (Constructor->getInheritedConstructor() ? 2 : |
| 8474 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8475 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 8476 | << /*const=*/1 |
| 8477 | << Entity.getName(); |
| 8478 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 8479 | << Entity.getName(); |
| 8480 | } else { |
| 8481 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
Nico Weber | 9386c82 | 2014-07-23 05:16:10 +0000 | [diff] [blame] | 8482 | << DestType << (bool)DestType->getAs<RecordType>(); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8483 | } |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 8484 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8485 | |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 8486 | case FK_Incomplete: |
Douglas Gregor | 85f3423 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 8487 | S.RequireCompleteType(Kind.getLocation(), FailedIncompleteType, |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 8488 | diag::err_init_incomplete_type); |
| 8489 | break; |
| 8490 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 8491 | case FK_ListInitializationFailed: { |
| 8492 | // Run the init list checker again to emit diagnostics. |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 8493 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
| 8494 | diagnoseListInit(S, Entity, InitList); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 8495 | break; |
| 8496 | } |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 8497 | |
| 8498 | case FK_PlaceholderType: { |
| 8499 | // FIXME: Already diagnosed! |
| 8500 | break; |
| 8501 | } |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 8502 | |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 8503 | case FK_ExplicitConstructor: { |
| 8504 | S.Diag(Kind.getLocation(), diag::err_selected_explicit_constructor) |
| 8505 | << Args[0]->getSourceRange(); |
| 8506 | OverloadCandidateSet::iterator Best; |
| 8507 | OverloadingResult Ovl |
| 8508 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Matt Beaumont-Gay | 5dcce09 | 2012-04-02 19:05:35 +0000 | [diff] [blame] | 8509 | (void)Ovl; |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 8510 | assert(Ovl == OR_Success && "Inconsistent overload resolution"); |
| 8511 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 8512 | S.Diag(CtorDecl->getLocation(), |
| 8513 | diag::note_explicit_ctor_deduction_guide_here) << false; |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 8514 | break; |
| 8515 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8516 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8517 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 8518 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8519 | return true; |
| 8520 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8521 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8522 | void InitializationSequence::dump(raw_ostream &OS) const { |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8523 | switch (SequenceKind) { |
| 8524 | case FailedSequence: { |
| 8525 | OS << "Failed sequence: "; |
| 8526 | switch (Failure) { |
| 8527 | case FK_TooManyInitsForReference: |
| 8528 | OS << "too many initializers for reference"; |
| 8529 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8530 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 8531 | case FK_ParenthesizedListInitForReference: |
| 8532 | OS << "parenthesized list init for reference"; |
| 8533 | break; |
| 8534 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8535 | case FK_ArrayNeedsInitList: |
| 8536 | OS << "array requires initializer list"; |
| 8537 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8538 | |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 8539 | case FK_AddressOfUnaddressableFunction: |
| 8540 | OS << "address of unaddressable function was taken"; |
| 8541 | break; |
| 8542 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8543 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 8544 | OS << "array requires initializer list or string literal"; |
| 8545 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8546 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 8547 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 8548 | OS << "array requires initializer list or wide string literal"; |
| 8549 | break; |
| 8550 | |
| 8551 | case FK_NarrowStringIntoWideCharArray: |
| 8552 | OS << "narrow string into wide char array"; |
| 8553 | break; |
| 8554 | |
| 8555 | case FK_WideStringIntoCharArray: |
| 8556 | OS << "wide string into char array"; |
| 8557 | break; |
| 8558 | |
| 8559 | case FK_IncompatWideStringIntoWideChar: |
| 8560 | OS << "incompatible wide string into wide char array"; |
| 8561 | break; |
| 8562 | |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 8563 | case FK_PlainStringIntoUTF8Char: |
| 8564 | OS << "plain string literal into char8_t array"; |
| 8565 | break; |
| 8566 | |
| 8567 | case FK_UTF8StringIntoPlainChar: |
| 8568 | OS << "u8 string literal into char array"; |
| 8569 | break; |
| 8570 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8571 | case FK_ArrayTypeMismatch: |
| 8572 | OS << "array type mismatch"; |
| 8573 | break; |
| 8574 | |
| 8575 | case FK_NonConstantArrayInit: |
| 8576 | OS << "non-constant array initializer"; |
| 8577 | break; |
| 8578 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8579 | case FK_AddressOfOverloadFailed: |
| 8580 | OS << "address of overloaded function failed"; |
| 8581 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8582 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8583 | case FK_ReferenceInitOverloadFailed: |
| 8584 | OS << "overload resolution for reference initialization failed"; |
| 8585 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8586 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8587 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 8588 | OS << "non-const lvalue reference bound to temporary"; |
| 8589 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8590 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8591 | case FK_NonConstLValueReferenceBindingToBitfield: |
| 8592 | OS << "non-const lvalue reference bound to bit-field"; |
| 8593 | break; |
| 8594 | |
| 8595 | case FK_NonConstLValueReferenceBindingToVectorElement: |
| 8596 | OS << "non-const lvalue reference bound to vector element"; |
| 8597 | break; |
| 8598 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8599 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 8600 | OS << "non-const lvalue reference bound to unrelated type"; |
| 8601 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8602 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8603 | case FK_RValueReferenceBindingToLValue: |
| 8604 | OS << "rvalue reference bound to an lvalue"; |
| 8605 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8606 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8607 | case FK_ReferenceInitDropsQualifiers: |
| 8608 | OS << "reference initialization drops qualifiers"; |
| 8609 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8610 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8611 | case FK_ReferenceInitFailed: |
| 8612 | OS << "reference initialization failed"; |
| 8613 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8614 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8615 | case FK_ConversionFailed: |
| 8616 | OS << "conversion failed"; |
| 8617 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8618 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8619 | case FK_ConversionFromPropertyFailed: |
| 8620 | OS << "conversion from property failed"; |
| 8621 | break; |
| 8622 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8623 | case FK_TooManyInitsForScalar: |
| 8624 | OS << "too many initializers for scalar"; |
| 8625 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8626 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 8627 | case FK_ParenthesizedListInitForScalar: |
| 8628 | OS << "parenthesized list init for reference"; |
| 8629 | break; |
| 8630 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8631 | case FK_ReferenceBindingToInitList: |
| 8632 | OS << "referencing binding to initializer list"; |
| 8633 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8634 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8635 | case FK_InitListBadDestinationType: |
| 8636 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 8637 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8638 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8639 | case FK_UserConversionOverloadFailed: |
| 8640 | OS << "overloading failed for user-defined conversion"; |
| 8641 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8642 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8643 | case FK_ConstructorOverloadFailed: |
| 8644 | OS << "constructor overloading failed"; |
| 8645 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8646 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8647 | case FK_DefaultInitOfConst: |
| 8648 | OS << "default initialization of a const variable"; |
| 8649 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8650 | |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 8651 | case FK_Incomplete: |
| 8652 | OS << "initialization of incomplete type"; |
| 8653 | break; |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 8654 | |
| 8655 | case FK_ListInitializationFailed: |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 8656 | OS << "list initialization checker failure"; |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 8657 | break; |
| 8658 | |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 8659 | case FK_VariableLengthArrayHasInitializer: |
| 8660 | OS << "variable length array has an initializer"; |
| 8661 | break; |
| 8662 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 8663 | case FK_PlaceholderType: |
| 8664 | OS << "initializer expression isn't contextually valid"; |
| 8665 | break; |
Nick Lewycky | 097f47c | 2011-12-22 20:21:32 +0000 | [diff] [blame] | 8666 | |
| 8667 | case FK_ListConstructorOverloadFailed: |
| 8668 | OS << "list constructor overloading failed"; |
| 8669 | break; |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 8670 | |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 8671 | case FK_ExplicitConstructor: |
| 8672 | OS << "list copy initialization chose explicit constructor"; |
| 8673 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8674 | } |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8675 | OS << '\n'; |
| 8676 | return; |
| 8677 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8678 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8679 | case DependentSequence: |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 8680 | OS << "Dependent sequence\n"; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8681 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8682 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 8683 | case NormalSequence: |
| 8684 | OS << "Normal sequence: "; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8685 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8686 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8687 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8688 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 8689 | if (S != step_begin()) { |
| 8690 | OS << " -> "; |
| 8691 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8692 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8693 | switch (S->Kind) { |
| 8694 | case SK_ResolveAddressOfOverloadedFunction: |
| 8695 | OS << "resolve address of overloaded function"; |
| 8696 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8697 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8698 | case SK_CastDerivedToBaseRValue: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8699 | OS << "derived-to-base (rvalue)"; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8700 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8701 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 8702 | case SK_CastDerivedToBaseXValue: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8703 | OS << "derived-to-base (xvalue)"; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 8704 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8705 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8706 | case SK_CastDerivedToBaseLValue: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8707 | OS << "derived-to-base (lvalue)"; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8708 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8709 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8710 | case SK_BindReference: |
| 8711 | OS << "bind reference to lvalue"; |
| 8712 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8713 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8714 | case SK_BindReferenceToTemporary: |
| 8715 | OS << "bind reference to a temporary"; |
| 8716 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8717 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8718 | case SK_FinalCopy: |
| 8719 | OS << "final copy in class direct-initialization"; |
| 8720 | break; |
| 8721 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 8722 | case SK_ExtraneousCopyToTemporary: |
| 8723 | OS << "extraneous C++03 copy to temporary"; |
| 8724 | break; |
| 8725 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8726 | case SK_UserConversion: |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 8727 | OS << "user-defined conversion via " << *S->Function.Function; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8728 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 8729 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8730 | case SK_QualificationConversionRValue: |
| 8731 | OS << "qualification conversion (rvalue)"; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8732 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8733 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 8734 | case SK_QualificationConversionXValue: |
| 8735 | OS << "qualification conversion (xvalue)"; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8736 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 8737 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8738 | case SK_QualificationConversionLValue: |
| 8739 | OS << "qualification conversion (lvalue)"; |
| 8740 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8741 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 8742 | case SK_AtomicConversion: |
| 8743 | OS << "non-atomic-to-atomic conversion"; |
| 8744 | break; |
| 8745 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 8746 | case SK_LValueToRValue: |
| 8747 | OS << "load (lvalue to rvalue)"; |
| 8748 | break; |
| 8749 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8750 | case SK_ConversionSequence: |
| 8751 | OS << "implicit conversion sequence ("; |
Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 8752 | S->ICS->dump(); // FIXME: use OS |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8753 | OS << ")"; |
| 8754 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8755 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 8756 | case SK_ConversionSequenceNoNarrowing: |
| 8757 | OS << "implicit conversion sequence with narrowing prohibited ("; |
Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 8758 | S->ICS->dump(); // FIXME: use OS |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 8759 | OS << ")"; |
| 8760 | break; |
| 8761 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8762 | case SK_ListInitialization: |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 8763 | OS << "list aggregate initialization"; |
| 8764 | break; |
| 8765 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8766 | case SK_UnwrapInitList: |
| 8767 | OS << "unwrap reference initializer list"; |
| 8768 | break; |
| 8769 | |
| 8770 | case SK_RewrapInitList: |
| 8771 | OS << "rewrap reference initializer list"; |
| 8772 | break; |
| 8773 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8774 | case SK_ConstructorInitialization: |
| 8775 | OS << "constructor initialization"; |
| 8776 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8777 | |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 8778 | case SK_ConstructorInitializationFromList: |
| 8779 | OS << "list initialization via constructor"; |
| 8780 | break; |
| 8781 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8782 | case SK_ZeroInitialization: |
| 8783 | OS << "zero initialization"; |
| 8784 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8785 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8786 | case SK_CAssignment: |
| 8787 | OS << "C assignment"; |
| 8788 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8789 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8790 | case SK_StringInit: |
| 8791 | OS << "string initialization"; |
| 8792 | break; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 8793 | |
| 8794 | case SK_ObjCObjectConversion: |
| 8795 | OS << "Objective-C object conversion"; |
| 8796 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8797 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 8798 | case SK_ArrayLoopIndex: |
| 8799 | OS << "indexing for array initialization loop"; |
| 8800 | break; |
| 8801 | |
| 8802 | case SK_ArrayLoopInit: |
| 8803 | OS << "array initialization loop"; |
| 8804 | break; |
| 8805 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8806 | case SK_ArrayInit: |
| 8807 | OS << "array initialization"; |
| 8808 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8809 | |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 8810 | case SK_GNUArrayInit: |
| 8811 | OS << "array initialization (GNU extension)"; |
| 8812 | break; |
| 8813 | |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 8814 | case SK_ParenthesizedArrayInit: |
| 8815 | OS << "parenthesized array initialization"; |
| 8816 | break; |
| 8817 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8818 | case SK_PassByIndirectCopyRestore: |
| 8819 | OS << "pass by indirect copy and restore"; |
| 8820 | break; |
| 8821 | |
| 8822 | case SK_PassByIndirectRestore: |
| 8823 | OS << "pass by indirect restore"; |
| 8824 | break; |
| 8825 | |
| 8826 | case SK_ProduceObjCObject: |
| 8827 | OS << "Objective-C object retension"; |
| 8828 | break; |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 8829 | |
| 8830 | case SK_StdInitializerList: |
| 8831 | OS << "std::initializer_list from initializer list"; |
| 8832 | break; |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 8833 | |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 8834 | case SK_StdInitializerListConstructorCall: |
| 8835 | OS << "list initialization from std::initializer_list"; |
| 8836 | break; |
| 8837 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 8838 | case SK_OCLSamplerInit: |
| 8839 | OS << "OpenCL sampler_t from integer constant"; |
| 8840 | break; |
| 8841 | |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 8842 | case SK_OCLZeroEvent: |
| 8843 | OS << "OpenCL event_t from zero"; |
| 8844 | break; |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 8845 | |
| 8846 | case SK_OCLZeroQueue: |
| 8847 | OS << "OpenCL queue_t from zero"; |
| 8848 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8849 | } |
Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 8850 | |
| 8851 | OS << " [" << S->Type.getAsString() << ']'; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8852 | } |
Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 8853 | |
| 8854 | OS << '\n'; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8855 | } |
| 8856 | |
| 8857 | void InitializationSequence::dump() const { |
| 8858 | dump(llvm::errs()); |
| 8859 | } |
| 8860 | |
Nico Weber | 3d7f00d | 2018-06-19 23:19:34 +0000 | [diff] [blame] | 8861 | static bool NarrowingErrs(const LangOptions &L) { |
| 8862 | return L.CPlusPlus11 && |
| 8863 | (!L.MicrosoftExt || L.isCompatibleWithMSVC(LangOptions::MSVC2015)); |
| 8864 | } |
| 8865 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 8866 | static void DiagnoseNarrowingInInitList(Sema &S, |
| 8867 | const ImplicitConversionSequence &ICS, |
| 8868 | QualType PreNarrowingType, |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8869 | QualType EntityType, |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8870 | const Expr *PostInit) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8871 | const StandardConversionSequence *SCS = nullptr; |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8872 | switch (ICS.getKind()) { |
| 8873 | case ImplicitConversionSequence::StandardConversion: |
| 8874 | SCS = &ICS.Standard; |
| 8875 | break; |
| 8876 | case ImplicitConversionSequence::UserDefinedConversion: |
| 8877 | SCS = &ICS.UserDefined.After; |
| 8878 | break; |
| 8879 | case ImplicitConversionSequence::AmbiguousConversion: |
| 8880 | case ImplicitConversionSequence::EllipsisConversion: |
| 8881 | case ImplicitConversionSequence::BadConversion: |
| 8882 | return; |
| 8883 | } |
| 8884 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8885 | // C++11 [dcl.init.list]p7: Check whether this is a narrowing conversion. |
| 8886 | APValue ConstantValue; |
Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 8887 | QualType ConstantType; |
| 8888 | switch (SCS->getNarrowingKind(S.Context, PostInit, ConstantValue, |
| 8889 | ConstantType)) { |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8890 | case NK_Not_Narrowing: |
Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 8891 | case NK_Dependent_Narrowing: |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8892 | // No narrowing occurred. |
| 8893 | return; |
| 8894 | |
| 8895 | case NK_Type_Narrowing: |
| 8896 | // This was a floating-to-integer conversion, which is always considered a |
| 8897 | // narrowing conversion even if the value is a constant and can be |
| 8898 | // represented exactly as an integer. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8899 | S.Diag(PostInit->getBeginLoc(), NarrowingErrs(S.getLangOpts()) |
Nico Weber | 3d7f00d | 2018-06-19 23:19:34 +0000 | [diff] [blame] | 8900 | ? diag::ext_init_list_type_narrowing |
| 8901 | : diag::warn_init_list_type_narrowing) |
| 8902 | << PostInit->getSourceRange() |
| 8903 | << PreNarrowingType.getLocalUnqualifiedType() |
| 8904 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8905 | break; |
| 8906 | |
| 8907 | case NK_Constant_Narrowing: |
| 8908 | // A constant value was narrowed. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8909 | S.Diag(PostInit->getBeginLoc(), |
Nico Weber | 3d7f00d | 2018-06-19 23:19:34 +0000 | [diff] [blame] | 8910 | NarrowingErrs(S.getLangOpts()) |
| 8911 | ? diag::ext_init_list_constant_narrowing |
| 8912 | : diag::warn_init_list_constant_narrowing) |
| 8913 | << PostInit->getSourceRange() |
| 8914 | << ConstantValue.getAsString(S.getASTContext(), ConstantType) |
| 8915 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8916 | break; |
| 8917 | |
| 8918 | case NK_Variable_Narrowing: |
| 8919 | // A variable's value may have been narrowed. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8920 | S.Diag(PostInit->getBeginLoc(), |
Nico Weber | 3d7f00d | 2018-06-19 23:19:34 +0000 | [diff] [blame] | 8921 | NarrowingErrs(S.getLangOpts()) |
| 8922 | ? diag::ext_init_list_variable_narrowing |
| 8923 | : diag::warn_init_list_variable_narrowing) |
| 8924 | << PostInit->getSourceRange() |
| 8925 | << PreNarrowingType.getLocalUnqualifiedType() |
| 8926 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8927 | break; |
| 8928 | } |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 8929 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 8930 | SmallString<128> StaticCast; |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 8931 | llvm::raw_svector_ostream OS(StaticCast); |
| 8932 | OS << "static_cast<"; |
| 8933 | if (const TypedefType *TT = EntityType->getAs<TypedefType>()) { |
| 8934 | // It's important to use the typedef's name if there is one so that the |
| 8935 | // fixit doesn't break code using types like int64_t. |
| 8936 | // |
| 8937 | // FIXME: This will break if the typedef requires qualification. But |
| 8938 | // getQualifiedNameAsString() includes non-machine-parsable components. |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 8939 | OS << *TT->getDecl(); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 8940 | } else if (const BuiltinType *BT = EntityType->getAs<BuiltinType>()) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8941 | OS << BT->getName(S.getLangOpts()); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 8942 | else { |
| 8943 | // Oops, we didn't find the actual type of the variable. Don't emit a fixit |
| 8944 | // with a broken cast. |
| 8945 | return; |
| 8946 | } |
| 8947 | OS << ">("; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8948 | S.Diag(PostInit->getBeginLoc(), diag::note_init_list_narrowing_silence) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 8949 | << PostInit->getSourceRange() |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8950 | << FixItHint::CreateInsertion(PostInit->getBeginLoc(), OS.str()) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 8951 | << FixItHint::CreateInsertion( |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 8952 | S.getLocForEndOfToken(PostInit->getEndLoc()), ")"); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 8953 | } |
| 8954 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8955 | //===----------------------------------------------------------------------===// |
| 8956 | // Initialization helper functions |
| 8957 | //===----------------------------------------------------------------------===// |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 8958 | bool |
| 8959 | Sema::CanPerformCopyInitialization(const InitializedEntity &Entity, |
| 8960 | ExprResult Init) { |
| 8961 | if (Init.isInvalid()) |
| 8962 | return false; |
| 8963 | |
| 8964 | Expr *InitE = Init.get(); |
| 8965 | assert(InitE && "No initialization expression"); |
| 8966 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8967 | InitializationKind Kind = |
| 8968 | InitializationKind::CreateCopy(InitE->getBeginLoc(), SourceLocation()); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8969 | InitializationSequence Seq(*this, Entity, Kind, InitE); |
Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 8970 | return !Seq.Failed(); |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 8971 | } |
| 8972 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8973 | ExprResult |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8974 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 8975 | SourceLocation EqualLoc, |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 8976 | ExprResult Init, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 8977 | bool TopLevelOfInitList, |
| 8978 | bool AllowExplicit) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8979 | if (Init.isInvalid()) |
| 8980 | return ExprError(); |
| 8981 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 8982 | Expr *InitE = Init.get(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8983 | assert(InitE && "No initialization expression?"); |
| 8984 | |
| 8985 | if (EqualLoc.isInvalid()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8986 | EqualLoc = InitE->getBeginLoc(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8987 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8988 | InitializationKind Kind = InitializationKind::CreateCopy( |
| 8989 | InitE->getBeginLoc(), EqualLoc, AllowExplicit); |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 8990 | InitializationSequence Seq(*this, Entity, Kind, InitE, TopLevelOfInitList); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 8991 | |
Alex Lorenz | de69ff9 | 2017-05-16 10:23:58 +0000 | [diff] [blame] | 8992 | // Prevent infinite recursion when performing parameter copy-initialization. |
| 8993 | const bool ShouldTrackCopy = |
| 8994 | Entity.isParameterKind() && Seq.isConstructorInitialization(); |
| 8995 | if (ShouldTrackCopy) { |
| 8996 | if (llvm::find(CurrentParameterCopyTypes, Entity.getType()) != |
| 8997 | CurrentParameterCopyTypes.end()) { |
| 8998 | Seq.SetOverloadFailure( |
| 8999 | InitializationSequence::FK_ConstructorOverloadFailed, |
| 9000 | OR_No_Viable_Function); |
| 9001 | |
| 9002 | // Try to give a meaningful diagnostic note for the problematic |
| 9003 | // constructor. |
| 9004 | const auto LastStep = Seq.step_end() - 1; |
| 9005 | assert(LastStep->Kind == |
| 9006 | InitializationSequence::SK_ConstructorInitialization); |
| 9007 | const FunctionDecl *Function = LastStep->Function.Function; |
| 9008 | auto Candidate = |
| 9009 | llvm::find_if(Seq.getFailedCandidateSet(), |
| 9010 | [Function](const OverloadCandidate &Candidate) -> bool { |
| 9011 | return Candidate.Viable && |
| 9012 | Candidate.Function == Function && |
| 9013 | Candidate.Conversions.size() > 0; |
| 9014 | }); |
| 9015 | if (Candidate != Seq.getFailedCandidateSet().end() && |
| 9016 | Function->getNumParams() > 0) { |
| 9017 | Candidate->Viable = false; |
| 9018 | Candidate->FailureKind = ovl_fail_bad_conversion; |
| 9019 | Candidate->Conversions[0].setBad(BadConversionSequence::no_conversion, |
| 9020 | InitE, |
| 9021 | Function->getParamDecl(0)->getType()); |
| 9022 | } |
| 9023 | } |
| 9024 | CurrentParameterCopyTypes.push_back(Entity.getType()); |
| 9025 | } |
| 9026 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 9027 | ExprResult Result = Seq.Perform(*this, Entity, Kind, InitE); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9028 | |
Alex Lorenz | de69ff9 | 2017-05-16 10:23:58 +0000 | [diff] [blame] | 9029 | if (ShouldTrackCopy) |
| 9030 | CurrentParameterCopyTypes.pop_back(); |
| 9031 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9032 | return Result; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 9033 | } |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9034 | |
Richard Smith | 1363e8f | 2017-09-07 07:22:36 +0000 | [diff] [blame] | 9035 | /// Determine whether RD is, or is derived from, a specialization of CTD. |
| 9036 | static bool isOrIsDerivedFromSpecializationOf(CXXRecordDecl *RD, |
| 9037 | ClassTemplateDecl *CTD) { |
| 9038 | auto NotSpecialization = [&] (const CXXRecordDecl *Candidate) { |
| 9039 | auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Candidate); |
| 9040 | return !CTSD || !declaresSameEntity(CTSD->getSpecializedTemplate(), CTD); |
| 9041 | }; |
| 9042 | return !(NotSpecialization(RD) && RD->forallBases(NotSpecialization)); |
| 9043 | } |
| 9044 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9045 | QualType Sema::DeduceTemplateSpecializationFromInitializer( |
| 9046 | TypeSourceInfo *TSInfo, const InitializedEntity &Entity, |
| 9047 | const InitializationKind &Kind, MultiExprArg Inits) { |
| 9048 | auto *DeducedTST = dyn_cast<DeducedTemplateSpecializationType>( |
| 9049 | TSInfo->getType()->getContainedDeducedType()); |
| 9050 | assert(DeducedTST && "not a deduced template specialization type"); |
| 9051 | |
| 9052 | // We can only perform deduction for class templates. |
| 9053 | auto TemplateName = DeducedTST->getTemplateName(); |
| 9054 | auto *Template = |
| 9055 | dyn_cast_or_null<ClassTemplateDecl>(TemplateName.getAsTemplateDecl()); |
| 9056 | if (!Template) { |
| 9057 | Diag(Kind.getLocation(), |
| 9058 | diag::err_deduced_non_class_template_specialization_type) |
| 9059 | << (int)getTemplateNameKindForDiagnostics(TemplateName) << TemplateName; |
| 9060 | if (auto *TD = TemplateName.getAsTemplateDecl()) |
| 9061 | Diag(TD->getLocation(), diag::note_template_decl_here); |
| 9062 | return QualType(); |
| 9063 | } |
| 9064 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9065 | // Can't deduce from dependent arguments. |
| 9066 | if (Expr::hasAnyTypeDependentArguments(Inits)) |
| 9067 | return Context.DependentTy; |
| 9068 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9069 | // FIXME: Perform "exact type" matching first, per CWG discussion? |
| 9070 | // Or implement this via an implied 'T(T) -> T' deduction guide? |
| 9071 | |
| 9072 | // FIXME: Do we need/want a std::initializer_list<T> special case? |
| 9073 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9074 | // Look up deduction guides, including those synthesized from constructors. |
| 9075 | // |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9076 | // C++1z [over.match.class.deduct]p1: |
| 9077 | // A set of functions and function templates is formed comprising: |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9078 | // - For each constructor of the class template designated by the |
| 9079 | // template-name, a function template [...] |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9080 | // - For each deduction-guide, a function or function template [...] |
| 9081 | DeclarationNameInfo NameInfo( |
| 9082 | Context.DeclarationNames.getCXXDeductionGuideName(Template), |
| 9083 | TSInfo->getTypeLoc().getEndLoc()); |
| 9084 | LookupResult Guides(*this, NameInfo, LookupOrdinaryName); |
| 9085 | LookupQualifiedName(Guides, Template->getDeclContext()); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9086 | |
| 9087 | // FIXME: Do not diagnose inaccessible deduction guides. The standard isn't |
| 9088 | // clear on this, but they're not found by name so access does not apply. |
| 9089 | Guides.suppressDiagnostics(); |
| 9090 | |
| 9091 | // Figure out if this is list-initialization. |
| 9092 | InitListExpr *ListInit = |
| 9093 | (Inits.size() == 1 && Kind.getKind() != InitializationKind::IK_Direct) |
| 9094 | ? dyn_cast<InitListExpr>(Inits[0]) |
| 9095 | : nullptr; |
| 9096 | |
| 9097 | // C++1z [over.match.class.deduct]p1: |
| 9098 | // Initialization and overload resolution are performed as described in |
| 9099 | // [dcl.init] and [over.match.ctor], [over.match.copy], or [over.match.list] |
| 9100 | // (as appropriate for the type of initialization performed) for an object |
| 9101 | // of a hypothetical class type, where the selected functions and function |
| 9102 | // templates are considered to be the constructors of that class type |
| 9103 | // |
| 9104 | // Since we know we're initializing a class type of a type unrelated to that |
| 9105 | // of the initializer, this reduces to something fairly reasonable. |
| 9106 | OverloadCandidateSet Candidates(Kind.getLocation(), |
| 9107 | OverloadCandidateSet::CSK_Normal); |
| 9108 | OverloadCandidateSet::iterator Best; |
| 9109 | auto tryToResolveOverload = |
| 9110 | [&](bool OnlyListConstructors) -> OverloadingResult { |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 9111 | Candidates.clear(OverloadCandidateSet::CSK_Normal); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9112 | for (auto I = Guides.begin(), E = Guides.end(); I != E; ++I) { |
| 9113 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9114 | if (D->isInvalidDecl()) |
| 9115 | continue; |
| 9116 | |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9117 | auto *TD = dyn_cast<FunctionTemplateDecl>(D); |
| 9118 | auto *GD = dyn_cast_or_null<CXXDeductionGuideDecl>( |
| 9119 | TD ? TD->getTemplatedDecl() : dyn_cast<FunctionDecl>(D)); |
| 9120 | if (!GD) |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9121 | continue; |
| 9122 | |
| 9123 | // C++ [over.match.ctor]p1: (non-list copy-initialization from non-class) |
| 9124 | // For copy-initialization, the candidate functions are all the |
| 9125 | // converting constructors (12.3.1) of that class. |
| 9126 | // C++ [over.match.copy]p1: (non-list copy-initialization from class) |
| 9127 | // The converting constructors of T are candidate functions. |
| 9128 | if (Kind.isCopyInit() && !ListInit) { |
Richard Smith | afe4aa8 | 2017-02-10 02:19:05 +0000 | [diff] [blame] | 9129 | // Only consider converting constructors. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9130 | if (GD->isExplicit()) |
Richard Smith | afe4aa8 | 2017-02-10 02:19:05 +0000 | [diff] [blame] | 9131 | continue; |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9132 | |
| 9133 | // When looking for a converting constructor, deduction guides that |
Richard Smith | afe4aa8 | 2017-02-10 02:19:05 +0000 | [diff] [blame] | 9134 | // could never be called with one argument are not interesting to |
| 9135 | // check or note. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9136 | if (GD->getMinRequiredArguments() > 1 || |
| 9137 | (GD->getNumParams() == 0 && !GD->isVariadic())) |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9138 | continue; |
| 9139 | } |
| 9140 | |
| 9141 | // C++ [over.match.list]p1.1: (first phase list initialization) |
| 9142 | // Initially, the candidate functions are the initializer-list |
| 9143 | // constructors of the class T |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9144 | if (OnlyListConstructors && !isInitListConstructor(GD)) |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9145 | continue; |
| 9146 | |
| 9147 | // C++ [over.match.list]p1.2: (second phase list initialization) |
| 9148 | // the candidate functions are all the constructors of the class T |
| 9149 | // C++ [over.match.ctor]p1: (all other cases) |
| 9150 | // the candidate functions are all the constructors of the class of |
| 9151 | // the object being initialized |
| 9152 | |
| 9153 | // C++ [over.best.ics]p4: |
| 9154 | // When [...] the constructor [...] is a candidate by |
| 9155 | // - [over.match.copy] (in all cases) |
| 9156 | // FIXME: The "second phase of [over.match.list] case can also |
| 9157 | // theoretically happen here, but it's not clear whether we can |
| 9158 | // ever have a parameter of the right type. |
| 9159 | bool SuppressUserConversions = Kind.isCopyInit(); |
| 9160 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9161 | if (TD) |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9162 | AddTemplateOverloadCandidate(TD, I.getPair(), /*ExplicitArgs*/ nullptr, |
| 9163 | Inits, Candidates, |
| 9164 | SuppressUserConversions); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9165 | else |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9166 | AddOverloadCandidate(GD, I.getPair(), Inits, Candidates, |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9167 | SuppressUserConversions); |
| 9168 | } |
| 9169 | return Candidates.BestViableFunction(*this, Kind.getLocation(), Best); |
| 9170 | }; |
| 9171 | |
| 9172 | OverloadingResult Result = OR_No_Viable_Function; |
| 9173 | |
| 9174 | // C++11 [over.match.list]p1, per DR1467: for list-initialization, first |
| 9175 | // try initializer-list constructors. |
| 9176 | if (ListInit) { |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9177 | bool TryListConstructors = true; |
| 9178 | |
| 9179 | // Try list constructors unless the list is empty and the class has one or |
| 9180 | // more default constructors, in which case those constructors win. |
| 9181 | if (!ListInit->getNumInits()) { |
| 9182 | for (NamedDecl *D : Guides) { |
| 9183 | auto *FD = dyn_cast<FunctionDecl>(D->getUnderlyingDecl()); |
| 9184 | if (FD && FD->getMinRequiredArguments() == 0) { |
| 9185 | TryListConstructors = false; |
| 9186 | break; |
| 9187 | } |
| 9188 | } |
Richard Smith | 1363e8f | 2017-09-07 07:22:36 +0000 | [diff] [blame] | 9189 | } else if (ListInit->getNumInits() == 1) { |
| 9190 | // C++ [over.match.class.deduct]: |
| 9191 | // As an exception, the first phase in [over.match.list] (considering |
| 9192 | // initializer-list constructors) is omitted if the initializer list |
| 9193 | // consists of a single expression of type cv U, where U is a |
| 9194 | // specialization of C or a class derived from a specialization of C. |
| 9195 | Expr *E = ListInit->getInit(0); |
| 9196 | auto *RD = E->getType()->getAsCXXRecordDecl(); |
| 9197 | if (!isa<InitListExpr>(E) && RD && |
Erik Pilkington | dd0b344 | 2018-07-26 23:40:42 +0000 | [diff] [blame] | 9198 | isCompleteType(Kind.getLocation(), E->getType()) && |
Richard Smith | 1363e8f | 2017-09-07 07:22:36 +0000 | [diff] [blame] | 9199 | isOrIsDerivedFromSpecializationOf(RD, Template)) |
| 9200 | TryListConstructors = false; |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9201 | } |
| 9202 | |
| 9203 | if (TryListConstructors) |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9204 | Result = tryToResolveOverload(/*OnlyListConstructor*/true); |
| 9205 | // Then unwrap the initializer list and try again considering all |
| 9206 | // constructors. |
| 9207 | Inits = MultiExprArg(ListInit->getInits(), ListInit->getNumInits()); |
| 9208 | } |
| 9209 | |
| 9210 | // If list-initialization fails, or if we're doing any other kind of |
| 9211 | // initialization, we (eventually) consider constructors. |
| 9212 | if (Result == OR_No_Viable_Function) |
| 9213 | Result = tryToResolveOverload(/*OnlyListConstructor*/false); |
| 9214 | |
| 9215 | switch (Result) { |
| 9216 | case OR_Ambiguous: |
| 9217 | Diag(Kind.getLocation(), diag::err_deduced_class_template_ctor_ambiguous) |
| 9218 | << TemplateName; |
| 9219 | // FIXME: For list-initialization candidates, it'd usually be better to |
| 9220 | // list why they were not viable when given the initializer list itself as |
| 9221 | // an argument. |
| 9222 | Candidates.NoteCandidates(*this, OCD_ViableCandidates, Inits); |
| 9223 | return QualType(); |
| 9224 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9225 | case OR_No_Viable_Function: { |
| 9226 | CXXRecordDecl *Primary = |
| 9227 | cast<ClassTemplateDecl>(Template)->getTemplatedDecl(); |
| 9228 | bool Complete = |
| 9229 | isCompleteType(Kind.getLocation(), Context.getTypeDeclType(Primary)); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9230 | Diag(Kind.getLocation(), |
| 9231 | Complete ? diag::err_deduced_class_template_ctor_no_viable |
| 9232 | : diag::err_deduced_class_template_incomplete) |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9233 | << TemplateName << !Guides.empty(); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9234 | Candidates.NoteCandidates(*this, OCD_AllCandidates, Inits); |
| 9235 | return QualType(); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9236 | } |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9237 | |
| 9238 | case OR_Deleted: { |
| 9239 | Diag(Kind.getLocation(), diag::err_deduced_class_template_deleted) |
| 9240 | << TemplateName; |
| 9241 | NoteDeletedFunction(Best->Function); |
| 9242 | return QualType(); |
| 9243 | } |
| 9244 | |
| 9245 | case OR_Success: |
| 9246 | // C++ [over.match.list]p1: |
| 9247 | // In copy-list-initialization, if an explicit constructor is chosen, the |
| 9248 | // initialization is ill-formed. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9249 | if (Kind.isCopyInit() && ListInit && |
| 9250 | cast<CXXDeductionGuideDecl>(Best->Function)->isExplicit()) { |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9251 | bool IsDeductionGuide = !Best->Function->isImplicit(); |
| 9252 | Diag(Kind.getLocation(), diag::err_deduced_class_template_explicit) |
| 9253 | << TemplateName << IsDeductionGuide; |
| 9254 | Diag(Best->Function->getLocation(), |
| 9255 | diag::note_explicit_ctor_deduction_guide_here) |
| 9256 | << IsDeductionGuide; |
| 9257 | return QualType(); |
| 9258 | } |
| 9259 | |
| 9260 | // Make sure we didn't select an unusable deduction guide, and mark it |
| 9261 | // as referenced. |
| 9262 | DiagnoseUseOfDecl(Best->Function, Kind.getLocation()); |
| 9263 | MarkFunctionReferenced(Kind.getLocation(), Best->Function); |
| 9264 | break; |
| 9265 | } |
| 9266 | |
| 9267 | // C++ [dcl.type.class.deduct]p1: |
| 9268 | // The placeholder is replaced by the return type of the function selected |
| 9269 | // by overload resolution for class template deduction. |
| 9270 | return SubstAutoType(TSInfo->getType(), Best->Function->getReturnType()); |
| 9271 | } |