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 | } |
Richard Smith | 79c88c3 | 2018-09-26 19:00:16 +0000 | [diff] [blame] | 967 | |
| 968 | // Warn if this type won't be an aggregate in future versions of C++. |
| 969 | auto *CXXRD = T->getAsCXXRecordDecl(); |
| 970 | if (CXXRD && CXXRD->hasUserDeclaredConstructor()) { |
| 971 | SemaRef.Diag(StructuredSubobjectInitList->getBeginLoc(), |
| 972 | diag::warn_cxx2a_compat_aggregate_init_with_ctors) |
| 973 | << StructuredSubobjectInitList->getSourceRange() << T; |
| 974 | } |
Tanya Lattner | 5029d56 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 975 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 976 | } |
| 977 | |
Richard Smith | 420fa12 | 2015-02-12 01:50:05 +0000 | [diff] [blame] | 978 | /// Warn that \p Entity was of scalar type and was initialized by a |
| 979 | /// single-element braced initializer list. |
| 980 | static void warnBracedScalarInit(Sema &S, const InitializedEntity &Entity, |
| 981 | SourceRange Braces) { |
| 982 | // Don't warn during template instantiation. If the initialization was |
| 983 | // non-dependent, we warned during the initial parse; otherwise, the |
| 984 | // type might not be scalar in some uses of the template. |
Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 985 | if (S.inTemplateInstantiation()) |
Richard Smith | 420fa12 | 2015-02-12 01:50:05 +0000 | [diff] [blame] | 986 | return; |
| 987 | |
| 988 | unsigned DiagID = 0; |
| 989 | |
| 990 | switch (Entity.getKind()) { |
| 991 | case InitializedEntity::EK_VectorElement: |
| 992 | case InitializedEntity::EK_ComplexElement: |
| 993 | case InitializedEntity::EK_ArrayElement: |
| 994 | case InitializedEntity::EK_Parameter: |
| 995 | case InitializedEntity::EK_Parameter_CF_Audited: |
| 996 | case InitializedEntity::EK_Result: |
| 997 | // Extra braces here are suspicious. |
| 998 | DiagID = diag::warn_braces_around_scalar_init; |
| 999 | break; |
| 1000 | |
| 1001 | case InitializedEntity::EK_Member: |
| 1002 | // Warn on aggregate initialization but not on ctor init list or |
| 1003 | // default member initializer. |
| 1004 | if (Entity.getParent()) |
| 1005 | DiagID = diag::warn_braces_around_scalar_init; |
| 1006 | break; |
| 1007 | |
| 1008 | case InitializedEntity::EK_Variable: |
| 1009 | case InitializedEntity::EK_LambdaCapture: |
| 1010 | // No warning, might be direct-list-initialization. |
| 1011 | // FIXME: Should we warn for copy-list-initialization in these cases? |
| 1012 | break; |
| 1013 | |
| 1014 | case InitializedEntity::EK_New: |
| 1015 | case InitializedEntity::EK_Temporary: |
| 1016 | case InitializedEntity::EK_CompoundLiteralInit: |
| 1017 | // No warning, braces are part of the syntax of the underlying construct. |
| 1018 | break; |
| 1019 | |
| 1020 | case InitializedEntity::EK_RelatedResult: |
| 1021 | // No warning, we already warned when initializing the result. |
| 1022 | break; |
| 1023 | |
| 1024 | case InitializedEntity::EK_Exception: |
| 1025 | case InitializedEntity::EK_Base: |
| 1026 | case InitializedEntity::EK_Delegating: |
| 1027 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 1028 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 1029 | case InitializedEntity::EK_Binding: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 1030 | case InitializedEntity::EK_StmtExprResult: |
Richard Smith | 420fa12 | 2015-02-12 01:50:05 +0000 | [diff] [blame] | 1031 | llvm_unreachable("unexpected braced scalar init"); |
| 1032 | } |
| 1033 | |
| 1034 | if (DiagID) { |
| 1035 | S.Diag(Braces.getBegin(), DiagID) |
| 1036 | << Braces |
| 1037 | << FixItHint::CreateRemoval(Braces.getBegin()) |
| 1038 | << FixItHint::CreateRemoval(Braces.getEnd()); |
| 1039 | } |
| 1040 | } |
| 1041 | |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 1042 | /// Check whether the initializer \p IList (that was written with explicit |
| 1043 | /// braces) can be used to initialize an object of type \p T. |
| 1044 | /// |
| 1045 | /// This also fills in \p StructuredList with the fully-braced, desugared |
| 1046 | /// form of the initialization. |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1047 | void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1048 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1049 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1050 | bool TopLevelObject) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1051 | if (!VerifyOnly) { |
| 1052 | SyntacticToSemantic[IList] = StructuredList; |
| 1053 | StructuredList->setSyntacticForm(IList); |
| 1054 | } |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 1055 | |
| 1056 | unsigned Index = 0, StructuredIndex = 0; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1057 | CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1058 | Index, StructuredList, StructuredIndex, TopLevelObject); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1059 | if (!VerifyOnly) { |
Eli Friedman | 91f5ae5 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 1060 | QualType ExprTy = T; |
| 1061 | if (!ExprTy->isArrayType()) |
| 1062 | ExprTy = ExprTy.getNonLValueExprType(SemaRef.Context); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1063 | IList->setType(ExprTy); |
| 1064 | StructuredList->setType(ExprTy); |
| 1065 | } |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1066 | if (hadError) |
| 1067 | return; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 1068 | |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1069 | if (Index < IList->getNumInits()) { |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 1070 | // We have leftover initializers |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1071 | if (VerifyOnly) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1072 | if (SemaRef.getLangOpts().CPlusPlus || |
| 1073 | (SemaRef.getLangOpts().OpenCL && |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1074 | IList->getType()->isVectorType())) { |
| 1075 | hadError = true; |
| 1076 | } |
| 1077 | return; |
| 1078 | } |
| 1079 | |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 1080 | if (StructuredIndex == 1 && |
Hans Wennborg | 950f318 | 2013-05-16 09:22:40 +0000 | [diff] [blame] | 1081 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context) == |
| 1082 | SIF_None) { |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 1083 | unsigned DK = diag::ext_excess_initializers_in_char_array_initializer; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1084 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 1085 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 1086 | hadError = true; |
| 1087 | } |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 1088 | // Special-case |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1089 | SemaRef.Diag(IList->getInit(Index)->getBeginLoc(), DK) |
| 1090 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d0e48ea | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 1091 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | d42a0fb | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 1092 | // Don't complain for incomplete types, since we'll get an error |
| 1093 | // elsewhere |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1094 | QualType CurrentObjectType = StructuredList->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1095 | int initKind = |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1096 | CurrentObjectType->isArrayType()? 0 : |
| 1097 | CurrentObjectType->isVectorType()? 1 : |
| 1098 | CurrentObjectType->isScalarType()? 2 : |
| 1099 | CurrentObjectType->isUnionType()? 3 : |
| 1100 | 4; |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 1101 | |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 1102 | unsigned DK = diag::ext_excess_initializers; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1103 | if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 1104 | DK = diag::err_excess_initializers; |
| 1105 | hadError = true; |
| 1106 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1107 | if (SemaRef.getLangOpts().OpenCL && initKind == 1) { |
Nate Begeman | 425038c | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 1108 | DK = diag::err_excess_initializers; |
| 1109 | hadError = true; |
| 1110 | } |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 1111 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1112 | SemaRef.Diag(IList->getInit(Index)->getBeginLoc(), DK) |
| 1113 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 1114 | } |
| 1115 | } |
Eli Friedman | 6fcdec2 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 1116 | |
Richard Smith | 79c88c3 | 2018-09-26 19:00:16 +0000 | [diff] [blame] | 1117 | if (!VerifyOnly) { |
| 1118 | if (T->isScalarType() && IList->getNumInits() == 1 && |
| 1119 | !isa<InitListExpr>(IList->getInit(0))) |
| 1120 | warnBracedScalarInit(SemaRef, Entity, IList->getSourceRange()); |
| 1121 | |
| 1122 | // Warn if this is a class type that won't be an aggregate in future |
| 1123 | // versions of C++. |
| 1124 | auto *CXXRD = T->getAsCXXRecordDecl(); |
| 1125 | if (CXXRD && CXXRD->hasUserDeclaredConstructor()) { |
| 1126 | // Don't warn if there's an equivalent default constructor that would be |
| 1127 | // used instead. |
| 1128 | bool HasEquivCtor = false; |
| 1129 | if (IList->getNumInits() == 0) { |
| 1130 | auto *CD = SemaRef.LookupDefaultConstructor(CXXRD); |
| 1131 | HasEquivCtor = CD && !CD->isDeleted(); |
| 1132 | } |
| 1133 | |
| 1134 | if (!HasEquivCtor) { |
| 1135 | SemaRef.Diag(IList->getBeginLoc(), |
| 1136 | diag::warn_cxx2a_compat_aggregate_init_with_ctors) |
| 1137 | << IList->getSourceRange() << T; |
| 1138 | } |
| 1139 | } |
| 1140 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1143 | void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1144 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1145 | QualType &DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1146 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1147 | unsigned &Index, |
| 1148 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1149 | unsigned &StructuredIndex, |
| 1150 | bool TopLevelObject) { |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 1151 | if (DeclType->isAnyComplexType() && SubobjectIsDesignatorContext) { |
| 1152 | // Explicitly braced initializer for complex type can be real+imaginary |
| 1153 | // parts. |
| 1154 | CheckComplexType(Entity, IList, DeclType, Index, |
| 1155 | StructuredList, StructuredIndex); |
| 1156 | } else if (DeclType->isScalarType()) { |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1157 | CheckScalarType(Entity, IList, DeclType, Index, |
| 1158 | StructuredList, StructuredIndex); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 1159 | } else if (DeclType->isVectorType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1160 | CheckVectorType(Entity, IList, DeclType, Index, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1161 | StructuredList, StructuredIndex); |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 1162 | } else if (DeclType->isRecordType()) { |
| 1163 | assert(DeclType->isAggregateType() && |
| 1164 | "non-aggregate records should be handed in CheckSubElementType"); |
| 1165 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 1166 | auto Bases = |
| 1167 | CXXRecordDecl::base_class_range(CXXRecordDecl::base_class_iterator(), |
| 1168 | CXXRecordDecl::base_class_iterator()); |
| 1169 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
| 1170 | Bases = CXXRD->bases(); |
| 1171 | CheckStructUnionTypes(Entity, IList, DeclType, Bases, RD->field_begin(), |
| 1172 | SubobjectIsDesignatorContext, Index, StructuredList, |
| 1173 | StructuredIndex, TopLevelObject); |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 1174 | } else if (DeclType->isArrayType()) { |
| 1175 | llvm::APSInt Zero( |
| 1176 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
| 1177 | false); |
| 1178 | CheckArrayType(Entity, IList, DeclType, Zero, |
| 1179 | SubobjectIsDesignatorContext, Index, |
| 1180 | StructuredList, StructuredIndex); |
Steve Naroff | eaf5853 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 1181 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 1182 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1183 | ++Index; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1184 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1185 | SemaRef.Diag(IList->getBeginLoc(), diag::err_illegal_initializer_type) |
| 1186 | << DeclType; |
Eli Friedman | d0e48ea | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 1187 | hadError = true; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1188 | } else if (DeclType->isReferenceType()) { |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1189 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 1190 | StructuredList, StructuredIndex); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1191 | } else if (DeclType->isObjCObjectType()) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1192 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1193 | SemaRef.Diag(IList->getBeginLoc(), diag::err_init_objc_class) << DeclType; |
Douglas Gregor | 50ec46d | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 1194 | hadError = true; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1195 | } else { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1196 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1197 | SemaRef.Diag(IList->getBeginLoc(), diag::err_illegal_initializer_type) |
| 1198 | << DeclType; |
Douglas Gregor | 50ec46d | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 1199 | hadError = true; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1200 | } |
| 1201 | } |
| 1202 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1203 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1204 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1205 | QualType ElemType, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1206 | unsigned &Index, |
| 1207 | InitListExpr *StructuredList, |
| 1208 | unsigned &StructuredIndex) { |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1209 | Expr *expr = IList->getInit(Index); |
Richard Smith | 72752e8 | 2013-05-31 02:56:17 +0000 | [diff] [blame] | 1210 | |
| 1211 | if (ElemType->isReferenceType()) |
| 1212 | return CheckReferenceType(Entity, IList, ElemType, Index, |
| 1213 | StructuredList, StructuredIndex); |
| 1214 | |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 1215 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1216 | if (SubInitList->getNumInits() == 1 && |
| 1217 | IsStringInit(SubInitList->getInit(0), ElemType, SemaRef.Context) == |
| 1218 | SIF_None) { |
| 1219 | expr = SubInitList->getInit(0); |
| 1220 | } else if (!SemaRef.getLangOpts().CPlusPlus) { |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 1221 | InitListExpr *InnerStructuredList |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 1222 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 1223 | StructuredList, StructuredIndex, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1224 | SubInitList->getSourceRange(), true); |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 1225 | CheckExplicitInitList(Entity, SubInitList, ElemType, |
| 1226 | InnerStructuredList); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1227 | |
| 1228 | if (!hadError && !VerifyOnly) { |
| 1229 | bool RequiresSecondPass = false; |
| 1230 | FillInEmptyInitializations(Entity, InnerStructuredList, |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 1231 | RequiresSecondPass, StructuredList, |
| 1232 | StructuredIndex); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1233 | if (RequiresSecondPass && !hadError) |
| 1234 | FillInEmptyInitializations(Entity, InnerStructuredList, |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 1235 | RequiresSecondPass, StructuredList, |
| 1236 | StructuredIndex); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1237 | } |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 1238 | ++StructuredIndex; |
| 1239 | ++Index; |
| 1240 | return; |
| 1241 | } |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 1242 | // C++ initialization is handled later. |
Richard Smith | c4158e86 | 2014-07-18 04:47:25 +0000 | [diff] [blame] | 1243 | } else if (isa<ImplicitValueInitExpr>(expr)) { |
Richard Smith | 8aa561b | 2014-07-17 23:12:06 +0000 | [diff] [blame] | 1244 | // This happens during template instantiation when we see an InitListExpr |
| 1245 | // that we've already checked once. |
Richard Smith | c4158e86 | 2014-07-18 04:47:25 +0000 | [diff] [blame] | 1246 | assert(SemaRef.Context.hasSameType(expr->getType(), ElemType) && |
Richard Smith | 8aa561b | 2014-07-17 23:12:06 +0000 | [diff] [blame] | 1247 | "found implicit initialization for the wrong type"); |
| 1248 | if (!VerifyOnly) |
| 1249 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 1250 | ++Index; |
| 1251 | return; |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1254 | if (SemaRef.getLangOpts().CPlusPlus) { |
| 1255 | // C++ [dcl.init.aggr]p2: |
| 1256 | // Each member is copy-initialized from the corresponding |
| 1257 | // initializer-clause. |
| 1258 | |
| 1259 | // FIXME: Better EqualLoc? |
| 1260 | InitializationKind Kind = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1261 | InitializationKind::CreateCopy(expr->getBeginLoc(), SourceLocation()); |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1262 | InitializationSequence Seq(SemaRef, Entity, Kind, expr, |
| 1263 | /*TopLevelOfInitList*/ true); |
| 1264 | |
| 1265 | // C++14 [dcl.init.aggr]p13: |
| 1266 | // If the assignment-expression can initialize a member, the member is |
| 1267 | // initialized. Otherwise [...] brace elision is assumed |
| 1268 | // |
| 1269 | // Brace elision is never performed if the element is not an |
| 1270 | // assignment-expression. |
| 1271 | if (Seq || isa<InitListExpr>(expr)) { |
| 1272 | if (!VerifyOnly) { |
| 1273 | ExprResult Result = |
| 1274 | Seq.Perform(SemaRef, Entity, Kind, expr); |
| 1275 | if (Result.isInvalid()) |
| 1276 | hadError = true; |
| 1277 | |
| 1278 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1279 | Result.getAs<Expr>()); |
Richard Smith | 40574cc | 2015-02-16 04:42:59 +0000 | [diff] [blame] | 1280 | } else if (!Seq) |
| 1281 | hadError = true; |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1282 | ++Index; |
| 1283 | return; |
| 1284 | } |
| 1285 | |
| 1286 | // Fall through for subaggregate initialization |
| 1287 | } else if (ElemType->isScalarType() || ElemType->isAtomicType()) { |
| 1288 | // FIXME: Need to handle atomic aggregate types with implicit init lists. |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1289 | return CheckScalarType(Entity, IList, ElemType, Index, |
| 1290 | StructuredList, StructuredIndex); |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1291 | } else if (const ArrayType *arrayType = |
| 1292 | SemaRef.Context.getAsArrayType(ElemType)) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1293 | // arrayType can be incomplete if we're initializing a flexible |
| 1294 | // array member. There's nothing we can do with the completed |
| 1295 | // type here, though. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1296 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1297 | if (IsStringInit(expr, arrayType, SemaRef.Context) == SIF_None) { |
Eli Friedman | d8d7a37 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 1298 | if (!VerifyOnly) { |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1299 | CheckStringInit(expr, ElemType, arrayType, SemaRef); |
| 1300 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
Eli Friedman | d8d7a37 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 1301 | } |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1302 | ++Index; |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1303 | return; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1304 | } |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1305 | |
| 1306 | // Fall through for subaggregate initialization. |
| 1307 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1308 | } else { |
Anastasia Stulova | db7a31c | 2016-07-05 11:31:24 +0000 | [diff] [blame] | 1309 | assert((ElemType->isRecordType() || ElemType->isVectorType() || |
Egor Churaev | 45fe70f | 2017-05-10 10:28:34 +0000 | [diff] [blame] | 1310 | ElemType->isOpenCLSpecificType()) && "Unexpected type"); |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1311 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1312 | // C99 6.7.8p13: |
| 1313 | // |
| 1314 | // The initializer for a structure or union object that has |
| 1315 | // automatic storage duration shall be either an initializer |
| 1316 | // list as described below, or a single expression that has |
| 1317 | // compatible structure or union type. In the latter case, the |
| 1318 | // initial value of the object, including unnamed members, is |
| 1319 | // that of the expression. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1320 | ExprResult ExprRes = expr; |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1321 | if (SemaRef.CheckSingleAssignmentConstraints( |
| 1322 | ElemType, ExprRes, !VerifyOnly) != Sema::Incompatible) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1323 | if (ExprRes.isInvalid()) |
| 1324 | hadError = true; |
| 1325 | else { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1326 | ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.get()); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 1327 | if (ExprRes.isInvalid()) |
| 1328 | hadError = true; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1329 | } |
| 1330 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1331 | ExprRes.getAs<Expr>()); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1332 | ++Index; |
| 1333 | return; |
| 1334 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1335 | ExprRes.get(); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1336 | // Fall through for subaggregate initialization |
| 1337 | } |
| 1338 | |
| 1339 | // C++ [dcl.init.aggr]p12: |
| 1340 | // |
| 1341 | // [...] Otherwise, if the member is itself a non-empty |
| 1342 | // subaggregate, brace elision is assumed and the initializer is |
| 1343 | // considered for the initialization of the first member of |
| 1344 | // the subaggregate. |
Yaxun Liu | a91da4b | 2016-10-11 15:53:28 +0000 | [diff] [blame] | 1345 | // OpenCL vector initializer is handled elsewhere. |
| 1346 | if ((!SemaRef.getLangOpts().OpenCL && ElemType->isVectorType()) || |
| 1347 | ElemType->isAggregateType()) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1348 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
| 1349 | StructuredIndex); |
| 1350 | ++StructuredIndex; |
| 1351 | } else { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1352 | if (!VerifyOnly) { |
| 1353 | // We cannot initialize this element, so let |
| 1354 | // PerformCopyInitialization produce the appropriate diagnostic. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1355 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), expr, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1356 | /*TopLevelOfInitList=*/true); |
| 1357 | } |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1358 | hadError = true; |
| 1359 | ++Index; |
| 1360 | ++StructuredIndex; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1361 | } |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 1364 | void InitListChecker::CheckComplexType(const InitializedEntity &Entity, |
| 1365 | InitListExpr *IList, QualType DeclType, |
| 1366 | unsigned &Index, |
| 1367 | InitListExpr *StructuredList, |
| 1368 | unsigned &StructuredIndex) { |
| 1369 | assert(Index == 0 && "Index in explicit init list must be zero"); |
| 1370 | |
| 1371 | // As an extension, clang supports complex initializers, which initialize |
| 1372 | // a complex number component-wise. When an explicit initializer list for |
| 1373 | // a complex number contains two two initializers, this extension kicks in: |
| 1374 | // it exepcts the initializer list to contain two elements convertible to |
| 1375 | // the element type of the complex type. The first element initializes |
| 1376 | // the real part, and the second element intitializes the imaginary part. |
| 1377 | |
| 1378 | if (IList->getNumInits() != 2) |
| 1379 | return CheckScalarType(Entity, IList, DeclType, Index, StructuredList, |
| 1380 | StructuredIndex); |
| 1381 | |
| 1382 | // This is an extension in C. (The builtin _Complex type does not exist |
| 1383 | // in the C++ standard.) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1384 | if (!SemaRef.getLangOpts().CPlusPlus && !VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1385 | SemaRef.Diag(IList->getBeginLoc(), diag::ext_complex_component_init) |
| 1386 | << IList->getSourceRange(); |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 1387 | |
| 1388 | // Initialize the complex number. |
| 1389 | QualType elementType = DeclType->getAs<ComplexType>()->getElementType(); |
| 1390 | InitializedEntity ElementEntity = |
| 1391 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
| 1392 | |
| 1393 | for (unsigned i = 0; i < 2; ++i) { |
| 1394 | ElementEntity.setElementIndex(Index); |
| 1395 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1396 | StructuredList, StructuredIndex); |
| 1397 | } |
| 1398 | } |
| 1399 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1400 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1401 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1402 | unsigned &Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1403 | InitListExpr *StructuredList, |
| 1404 | unsigned &StructuredIndex) { |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1405 | if (Index >= IList->getNumInits()) { |
Richard Smith | c823973 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 1406 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1407 | SemaRef.Diag(IList->getBeginLoc(), |
| 1408 | SemaRef.getLangOpts().CPlusPlus11 |
| 1409 | ? diag::warn_cxx98_compat_empty_scalar_initializer |
| 1410 | : diag::err_empty_scalar_initializer) |
| 1411 | << IList->getSourceRange(); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1412 | hadError = !SemaRef.getLangOpts().CPlusPlus11; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1413 | ++Index; |
| 1414 | ++StructuredIndex; |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 1415 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1416 | } |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1417 | |
| 1418 | Expr *expr = IList->getInit(Index); |
| 1419 | if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) { |
Richard Smith | fe9d2c0 | 2013-11-19 03:41:32 +0000 | [diff] [blame] | 1420 | // FIXME: This is invalid, and accepting it causes overload resolution |
| 1421 | // to pick the wrong overload in some corner cases. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1422 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1423 | SemaRef.Diag(SubIList->getBeginLoc(), |
Richard Smith | fe9d2c0 | 2013-11-19 03:41:32 +0000 | [diff] [blame] | 1424 | diag::ext_many_braces_around_scalar_init) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1425 | << SubIList->getSourceRange(); |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1426 | |
| 1427 | CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList, |
| 1428 | StructuredIndex); |
| 1429 | return; |
| 1430 | } else if (isa<DesignatedInitExpr>(expr)) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1431 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1432 | SemaRef.Diag(expr->getBeginLoc(), diag::err_designator_for_scalar_init) |
| 1433 | << DeclType << expr->getSourceRange(); |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1434 | hadError = true; |
| 1435 | ++Index; |
| 1436 | ++StructuredIndex; |
| 1437 | return; |
| 1438 | } |
| 1439 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1440 | if (VerifyOnly) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1441 | if (!SemaRef.CanPerformCopyInitialization(Entity,expr)) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1442 | hadError = true; |
| 1443 | ++Index; |
| 1444 | return; |
| 1445 | } |
| 1446 | |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1447 | ExprResult Result = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1448 | SemaRef.PerformCopyInitialization(Entity, expr->getBeginLoc(), expr, |
| 1449 | /*TopLevelOfInitList=*/true); |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1450 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1451 | Expr *ResultExpr = nullptr; |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1452 | |
| 1453 | if (Result.isInvalid()) |
| 1454 | hadError = true; // types weren't compatible. |
| 1455 | else { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1456 | ResultExpr = Result.getAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1457 | |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1458 | if (ResultExpr != expr) { |
| 1459 | // The type was promoted, update initializer list. |
| 1460 | IList->setInit(Index, ResultExpr); |
| 1461 | } |
| 1462 | } |
| 1463 | if (hadError) |
| 1464 | ++StructuredIndex; |
| 1465 | else |
| 1466 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 1467 | ++Index; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1470 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 1471 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1472 | unsigned &Index, |
| 1473 | InitListExpr *StructuredList, |
| 1474 | unsigned &StructuredIndex) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1475 | if (Index >= IList->getNumInits()) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1476 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 1477 | // general, it would be useful to pass location information down the stack, |
| 1478 | // so that we know the location (or decl) of the "current object" being |
| 1479 | // initialized. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1480 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1481 | SemaRef.Diag(IList->getBeginLoc(), |
| 1482 | diag::err_init_reference_member_uninitialized) |
| 1483 | << DeclType << IList->getSourceRange(); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1484 | hadError = true; |
| 1485 | ++Index; |
| 1486 | ++StructuredIndex; |
| 1487 | return; |
| 1488 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1489 | |
| 1490 | Expr *expr = IList->getInit(Index); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1491 | if (isa<InitListExpr>(expr) && !SemaRef.getLangOpts().CPlusPlus11) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1492 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1493 | SemaRef.Diag(IList->getBeginLoc(), diag::err_init_non_aggr_init_list) |
| 1494 | << DeclType << IList->getSourceRange(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1495 | hadError = true; |
| 1496 | ++Index; |
| 1497 | ++StructuredIndex; |
| 1498 | return; |
| 1499 | } |
| 1500 | |
| 1501 | if (VerifyOnly) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1502 | if (!SemaRef.CanPerformCopyInitialization(Entity,expr)) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1503 | hadError = true; |
| 1504 | ++Index; |
| 1505 | return; |
| 1506 | } |
| 1507 | |
| 1508 | ExprResult Result = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1509 | SemaRef.PerformCopyInitialization(Entity, expr->getBeginLoc(), expr, |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1510 | /*TopLevelOfInitList=*/true); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1511 | |
| 1512 | if (Result.isInvalid()) |
| 1513 | hadError = true; |
| 1514 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1515 | expr = Result.getAs<Expr>(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1516 | IList->setInit(Index, expr); |
| 1517 | |
| 1518 | if (hadError) |
| 1519 | ++StructuredIndex; |
| 1520 | else |
| 1521 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 1522 | ++Index; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1523 | } |
| 1524 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1525 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1526 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1527 | unsigned &Index, |
| 1528 | InitListExpr *StructuredList, |
| 1529 | unsigned &StructuredIndex) { |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1530 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 1531 | unsigned maxElements = VT->getNumElements(); |
| 1532 | unsigned numEltsInit = 0; |
| 1533 | QualType elementType = VT->getElementType(); |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1534 | |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1535 | if (Index >= IList->getNumInits()) { |
| 1536 | // Make sure the element type can be value-initialized. |
| 1537 | if (VerifyOnly) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1538 | CheckEmptyInitializable( |
| 1539 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity), |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1540 | IList->getEndLoc()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1541 | return; |
| 1542 | } |
| 1543 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1544 | if (!SemaRef.getLangOpts().OpenCL) { |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1545 | // If the initializing element is a vector, try to copy-initialize |
| 1546 | // instead of breaking it apart (which is doomed to failure anyway). |
| 1547 | Expr *Init = IList->getInit(Index); |
| 1548 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1549 | if (VerifyOnly) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1550 | if (!SemaRef.CanPerformCopyInitialization(Entity, Init)) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1551 | hadError = true; |
| 1552 | ++Index; |
| 1553 | return; |
| 1554 | } |
| 1555 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1556 | ExprResult Result = |
| 1557 | SemaRef.PerformCopyInitialization(Entity, Init->getBeginLoc(), Init, |
| 1558 | /*TopLevelOfInitList=*/true); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1559 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1560 | Expr *ResultExpr = nullptr; |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1561 | if (Result.isInvalid()) |
| 1562 | hadError = true; // types weren't compatible. |
| 1563 | else { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1564 | ResultExpr = Result.getAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1565 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1566 | if (ResultExpr != Init) { |
| 1567 | // The type was promoted, update initializer list. |
| 1568 | IList->setInit(Index, ResultExpr); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1569 | } |
| 1570 | } |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1571 | if (hadError) |
| 1572 | ++StructuredIndex; |
| 1573 | else |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1574 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1575 | ResultExpr); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1576 | ++Index; |
| 1577 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1578 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1579 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1580 | InitializedEntity ElementEntity = |
| 1581 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1582 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1583 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 1584 | // Don't attempt to go past the end of the init list |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1585 | if (Index >= IList->getNumInits()) { |
| 1586 | if (VerifyOnly) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1587 | CheckEmptyInitializable(ElementEntity, IList->getEndLoc()); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1588 | break; |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1589 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1590 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1591 | ElementEntity.setElementIndex(Index); |
| 1592 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1593 | StructuredList, StructuredIndex); |
| 1594 | } |
James Molloy | 9eef265 | 2014-06-20 14:35:13 +0000 | [diff] [blame] | 1595 | |
| 1596 | if (VerifyOnly) |
| 1597 | return; |
| 1598 | |
| 1599 | bool isBigEndian = SemaRef.Context.getTargetInfo().isBigEndian(); |
| 1600 | const VectorType *T = Entity.getType()->getAs<VectorType>(); |
| 1601 | if (isBigEndian && (T->getVectorKind() == VectorType::NeonVector || |
| 1602 | T->getVectorKind() == VectorType::NeonPolyVector)) { |
| 1603 | // The ability to use vector initializer lists is a GNU vector extension |
| 1604 | // 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] | 1605 | // endian machines it works fine, however on big endian machines it |
James Molloy | 9eef265 | 2014-06-20 14:35:13 +0000 | [diff] [blame] | 1606 | // exhibits surprising behaviour: |
| 1607 | // |
| 1608 | // uint32x2_t x = {42, 64}; |
| 1609 | // return vget_lane_u32(x, 0); // Will return 64. |
| 1610 | // |
| 1611 | // Because of this, explicitly call out that it is non-portable. |
| 1612 | // |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1613 | SemaRef.Diag(IList->getBeginLoc(), |
James Molloy | 9eef265 | 2014-06-20 14:35:13 +0000 | [diff] [blame] | 1614 | diag::warn_neon_vector_initializer_non_portable); |
| 1615 | |
| 1616 | const char *typeCode; |
| 1617 | unsigned typeSize = SemaRef.Context.getTypeSize(elementType); |
| 1618 | |
| 1619 | if (elementType->isFloatingType()) |
| 1620 | typeCode = "f"; |
| 1621 | else if (elementType->isSignedIntegerType()) |
| 1622 | typeCode = "s"; |
| 1623 | else if (elementType->isUnsignedIntegerType()) |
| 1624 | typeCode = "u"; |
| 1625 | else |
| 1626 | llvm_unreachable("Invalid element type!"); |
| 1627 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1628 | SemaRef.Diag(IList->getBeginLoc(), |
| 1629 | SemaRef.Context.getTypeSize(VT) > 64 |
| 1630 | ? diag::note_neon_vector_initializer_non_portable_q |
| 1631 | : diag::note_neon_vector_initializer_non_portable) |
| 1632 | << typeCode << typeSize; |
James Molloy | 9eef265 | 2014-06-20 14:35:13 +0000 | [diff] [blame] | 1633 | } |
| 1634 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1635 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1636 | } |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1637 | |
| 1638 | InitializedEntity ElementEntity = |
| 1639 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1640 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1641 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 1642 | for (unsigned i = 0; i < maxElements; ++i) { |
| 1643 | // Don't attempt to go past the end of the init list |
| 1644 | if (Index >= IList->getNumInits()) |
| 1645 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1646 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1647 | ElementEntity.setElementIndex(Index); |
| 1648 | |
| 1649 | QualType IType = IList->getInit(Index)->getType(); |
| 1650 | if (!IType->isVectorType()) { |
| 1651 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1652 | StructuredList, StructuredIndex); |
| 1653 | ++numEltsInit; |
| 1654 | } else { |
| 1655 | QualType VecType; |
| 1656 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 1657 | unsigned numIElts = IVT->getNumElements(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1658 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1659 | if (IType->isExtVectorType()) |
| 1660 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 1661 | else |
| 1662 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1663 | IVT->getVectorKind()); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1664 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 1665 | StructuredList, StructuredIndex); |
| 1666 | numEltsInit += numIElts; |
| 1667 | } |
| 1668 | } |
| 1669 | |
| 1670 | // OpenCL requires all elements to be initialized. |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1671 | if (numEltsInit != maxElements) { |
| 1672 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1673 | SemaRef.Diag(IList->getBeginLoc(), |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1674 | diag::err_vector_incorrect_num_initializers) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1675 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1676 | hadError = true; |
| 1677 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1678 | } |
| 1679 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1680 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1681 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1682 | llvm::APSInt elementIndex, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1683 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1684 | unsigned &Index, |
| 1685 | InitListExpr *StructuredList, |
| 1686 | unsigned &StructuredIndex) { |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1687 | const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType); |
| 1688 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1689 | // Check for the special-case of initializing an array with a string. |
| 1690 | if (Index < IList->getNumInits()) { |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1691 | if (IsStringInit(IList->getInit(Index), arrayType, SemaRef.Context) == |
| 1692 | SIF_None) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1693 | // We place the string literal directly into the resulting |
| 1694 | // initializer list. This is the only place where the structure |
| 1695 | // of the structured initializer list doesn't match exactly, |
| 1696 | // because doing so would involve allocating one character |
| 1697 | // constant for each string. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1698 | if (!VerifyOnly) { |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1699 | CheckStringInit(IList->getInit(Index), DeclType, arrayType, SemaRef); |
| 1700 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1701 | IList->getInit(Index)); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1702 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
| 1703 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1704 | ++Index; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1705 | return; |
| 1706 | } |
| 1707 | } |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1708 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1709 | // Check for VLAs; in standard C it would be possible to check this |
| 1710 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 1711 | // them in all sorts of strange places). |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1712 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1713 | SemaRef.Diag(VAT->getSizeExpr()->getBeginLoc(), |
| 1714 | diag::err_variable_object_no_init) |
| 1715 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1716 | hadError = true; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1717 | ++Index; |
| 1718 | ++StructuredIndex; |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1719 | return; |
| 1720 | } |
| 1721 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1722 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1723 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 1724 | elementIndex.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1725 | bool maxElementsKnown = false; |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1726 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1727 | maxElements = CAT->getSize(); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1728 | elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1729 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1730 | maxElementsKnown = true; |
| 1731 | } |
| 1732 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1733 | QualType elementType = arrayType->getElementType(); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1734 | while (Index < IList->getNumInits()) { |
| 1735 | Expr *Init = IList->getInit(Index); |
| 1736 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1737 | // If we're not the subobject that matches up with the '{' for |
| 1738 | // the designator, we shouldn't be handling the |
| 1739 | // designator. Return immediately. |
| 1740 | if (!SubobjectIsDesignatorContext) |
| 1741 | return; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1742 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1743 | // Handle this designated initializer. elementIndex will be |
| 1744 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1745 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1746 | DeclType, nullptr, &elementIndex, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1747 | StructuredList, StructuredIndex, true, |
| 1748 | false)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1749 | hadError = true; |
| 1750 | continue; |
| 1751 | } |
| 1752 | |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1753 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1754 | maxElements = maxElements.extend(elementIndex.getBitWidth()); |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1755 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1756 | elementIndex = elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1757 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1758 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1759 | // If the array is of incomplete type, keep track of the number of |
| 1760 | // elements in the initializer. |
| 1761 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1762 | maxElements = elementIndex; |
| 1763 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1764 | continue; |
| 1765 | } |
| 1766 | |
| 1767 | // If we know the maximum number of elements, and we've already |
| 1768 | // hit it, stop consuming elements in the initializer list. |
| 1769 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1770 | break; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1771 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1772 | InitializedEntity ElementEntity = |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1773 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1774 | Entity); |
| 1775 | // Check this element. |
| 1776 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1777 | StructuredList, StructuredIndex); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1778 | ++elementIndex; |
| 1779 | |
| 1780 | // If the array is of incomplete type, keep track of the number of |
| 1781 | // elements in the initializer. |
| 1782 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1783 | maxElements = elementIndex; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1784 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1785 | if (!hadError && DeclType->isIncompleteArrayType() && !VerifyOnly) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1786 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1787 | // be calculated here. |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1788 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Richard Smith | 73edb6d | 2017-01-24 23:18:28 +0000 | [diff] [blame] | 1789 | if (maxElements == Zero && !Entity.isVariableLengthArrayNew()) { |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1790 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1791 | // but is supported by GNU. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1792 | SemaRef.Diag(IList->getBeginLoc(), diag::ext_typecheck_zero_array_size); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1793 | } |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1794 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1795 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1796 | ArrayType::Normal, 0); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1797 | } |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1798 | if (!hadError && VerifyOnly) { |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1799 | // If there are any members of the array that get value-initialized, check |
| 1800 | // that is possible. That happens if we know the bound and don't have |
| 1801 | // enough elements, or if we're performing an array new with an unknown |
| 1802 | // bound. |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1803 | // FIXME: This needs to detect holes left by designated initializers too. |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1804 | if ((maxElementsKnown && elementIndex < maxElements) || |
| 1805 | Entity.isVariableLengthArrayNew()) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1806 | CheckEmptyInitializable( |
| 1807 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity), |
| 1808 | IList->getEndLoc()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1809 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1810 | } |
| 1811 | |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1812 | bool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 1813 | Expr *InitExpr, |
| 1814 | FieldDecl *Field, |
| 1815 | bool TopLevelObject) { |
| 1816 | // Handle GNU flexible array initializers. |
| 1817 | unsigned FlexArrayDiag; |
| 1818 | if (isa<InitListExpr>(InitExpr) && |
| 1819 | cast<InitListExpr>(InitExpr)->getNumInits() == 0) { |
| 1820 | // Empty flexible array init always allowed as an extension |
| 1821 | FlexArrayDiag = diag::ext_flexible_array_init; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1822 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1823 | // Disallow flexible array init in C++; it is not required for gcc |
| 1824 | // compatibility, and it needs work to IRGen correctly in general. |
| 1825 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1826 | } else if (!TopLevelObject) { |
| 1827 | // Disallow flexible array init on non-top-level object |
| 1828 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1829 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
| 1830 | // Disallow flexible array init on anything which is not a variable. |
| 1831 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1832 | } else if (cast<VarDecl>(Entity.getDecl())->hasLocalStorage()) { |
| 1833 | // Disallow flexible array init on local variables. |
| 1834 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1835 | } else { |
| 1836 | // Allow other cases. |
| 1837 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1838 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1839 | |
| 1840 | if (!VerifyOnly) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1841 | SemaRef.Diag(InitExpr->getBeginLoc(), FlexArrayDiag) |
| 1842 | << InitExpr->getBeginLoc(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1843 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1844 | << Field; |
| 1845 | } |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1846 | |
| 1847 | return FlexArrayDiag != diag::ext_flexible_array_init; |
| 1848 | } |
| 1849 | |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 1850 | /// Check if the type of a class element has an accessible destructor. |
| 1851 | /// |
| 1852 | /// Aggregate initialization requires a class element's destructor be |
| 1853 | /// accessible per 11.6.1 [dcl.init.aggr]: |
| 1854 | /// |
| 1855 | /// The destructor for each element of class type is potentially invoked |
| 1856 | /// (15.4 [class.dtor]) from the context where the aggregate initialization |
| 1857 | /// occurs. |
| 1858 | static bool hasAccessibleDestructor(QualType ElementType, SourceLocation Loc, |
| 1859 | Sema &SemaRef) { |
| 1860 | auto *CXXRD = ElementType->getAsCXXRecordDecl(); |
| 1861 | if (!CXXRD) |
| 1862 | return false; |
| 1863 | |
| 1864 | CXXDestructorDecl *Destructor = SemaRef.LookupDestructor(CXXRD); |
| 1865 | SemaRef.CheckDestructorAccess(Loc, Destructor, |
| 1866 | SemaRef.PDiag(diag::err_access_dtor_temp) |
| 1867 | << ElementType); |
| 1868 | SemaRef.MarkFunctionReferenced(Loc, Destructor); |
| 1869 | if (SemaRef.DiagnoseUseOfDecl(Destructor, Loc)) |
| 1870 | return true; |
| 1871 | return false; |
| 1872 | } |
| 1873 | |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 1874 | void InitListChecker::CheckStructUnionTypes( |
| 1875 | const InitializedEntity &Entity, InitListExpr *IList, QualType DeclType, |
| 1876 | CXXRecordDecl::base_class_range Bases, RecordDecl::field_iterator Field, |
| 1877 | bool SubobjectIsDesignatorContext, unsigned &Index, |
| 1878 | InitListExpr *StructuredList, unsigned &StructuredIndex, |
| 1879 | bool TopLevelObject) { |
| 1880 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1881 | |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1882 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1883 | // confusion, we forgo checking the intializer for the entire record. |
| 1884 | if (structDecl->isInvalidDecl()) { |
Richard Smith | 845aa66 | 2012-09-28 21:23:50 +0000 | [diff] [blame] | 1885 | // Assume it was supposed to consume a single initializer. |
| 1886 | ++Index; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1887 | hadError = true; |
| 1888 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1889 | } |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1890 | |
| 1891 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1892 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1893 | |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 1894 | if (!VerifyOnly) |
| 1895 | for (FieldDecl *FD : RD->fields()) { |
| 1896 | QualType ET = SemaRef.Context.getBaseElementType(FD->getType()); |
| 1897 | if (hasAccessibleDestructor(ET, IList->getEndLoc(), SemaRef)) { |
| 1898 | hadError = true; |
| 1899 | return; |
| 1900 | } |
| 1901 | } |
| 1902 | |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1903 | // If there's a default initializer, use it. |
| 1904 | if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->hasInClassInitializer()) { |
| 1905 | if (VerifyOnly) |
| 1906 | return; |
| 1907 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1908 | Field != FieldEnd; ++Field) { |
| 1909 | if (Field->hasInClassInitializer()) { |
| 1910 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1911 | // FIXME: Actually build a CXXDefaultInitExpr? |
| 1912 | return; |
| 1913 | } |
| 1914 | } |
| 1915 | } |
| 1916 | |
Reid Kleckner | 6d829bd | 2014-11-12 21:30:23 +0000 | [diff] [blame] | 1917 | // Value-initialize the first member of the union that isn't an unnamed |
| 1918 | // bitfield. |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1919 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1920 | Field != FieldEnd; ++Field) { |
Reid Kleckner | 6d829bd | 2014-11-12 21:30:23 +0000 | [diff] [blame] | 1921 | if (!Field->isUnnamedBitfield()) { |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1922 | if (VerifyOnly) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1923 | CheckEmptyInitializable( |
| 1924 | InitializedEntity::InitializeMember(*Field, &Entity), |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1925 | IList->getEndLoc()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1926 | else |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1927 | StructuredList->setInitializedFieldInUnion(*Field); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1928 | break; |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1929 | } |
| 1930 | } |
| 1931 | return; |
| 1932 | } |
| 1933 | |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 1934 | bool InitializedSomething = false; |
| 1935 | |
| 1936 | // If we have any base classes, they are initialized prior to the fields. |
| 1937 | for (auto &Base : Bases) { |
| 1938 | Expr *Init = Index < IList->getNumInits() ? IList->getInit(Index) : nullptr; |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 1939 | |
| 1940 | // Designated inits always initialize fields, so if we see one, all |
| 1941 | // remaining base classes have no explicit initializer. |
| 1942 | if (Init && isa<DesignatedInitExpr>(Init)) |
| 1943 | Init = nullptr; |
| 1944 | |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 1945 | SourceLocation InitLoc = Init ? Init->getBeginLoc() : IList->getEndLoc(); |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 1946 | InitializedEntity BaseEntity = InitializedEntity::InitializeBase( |
| 1947 | SemaRef.Context, &Base, false, &Entity); |
| 1948 | if (Init) { |
| 1949 | CheckSubElementType(BaseEntity, IList, Base.getType(), Index, |
| 1950 | StructuredList, StructuredIndex); |
| 1951 | InitializedSomething = true; |
| 1952 | } else if (VerifyOnly) { |
| 1953 | CheckEmptyInitializable(BaseEntity, InitLoc); |
| 1954 | } |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 1955 | |
| 1956 | if (!VerifyOnly) |
| 1957 | if (hasAccessibleDestructor(Base.getType(), InitLoc, SemaRef)) { |
| 1958 | hadError = true; |
| 1959 | return; |
| 1960 | } |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 1961 | } |
| 1962 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1963 | // If structDecl is a forward declaration, this loop won't do |
| 1964 | // anything except look at designated initializers; That's okay, |
| 1965 | // because an error should get printed out elsewhere. It might be |
| 1966 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1967 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1968 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Daniel Marjamaki | 817a3bf | 2017-09-29 09:44:41 +0000 | [diff] [blame] | 1969 | bool CheckForMissingFields = |
| 1970 | !IList->isIdiomaticZeroInitializer(SemaRef.getLangOpts()); |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 1971 | bool HasDesignatedInit = false; |
Daniel Marjamaki | 817a3bf | 2017-09-29 09:44:41 +0000 | [diff] [blame] | 1972 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1973 | while (Index < IList->getNumInits()) { |
| 1974 | Expr *Init = IList->getInit(Index); |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 1975 | SourceLocation InitLoc = Init->getBeginLoc(); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1976 | |
| 1977 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1978 | // If we're not the subobject that matches up with the '{' for |
| 1979 | // the designator, we shouldn't be handling the |
| 1980 | // designator. Return immediately. |
| 1981 | if (!SubobjectIsDesignatorContext) |
| 1982 | return; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1983 | |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 1984 | HasDesignatedInit = true; |
| 1985 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1986 | // Handle this designated initializer. Field will be updated to |
| 1987 | // the next field that we'll be initializing. |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1988 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1989 | DeclType, &Field, nullptr, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1990 | StructuredList, StructuredIndex, |
| 1991 | true, TopLevelObject)) |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1992 | hadError = true; |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 1993 | else if (!VerifyOnly) { |
| 1994 | // Find the field named by the designated initializer. |
| 1995 | RecordDecl::field_iterator F = RD->field_begin(); |
| 1996 | while (std::next(F) != Field) |
| 1997 | ++F; |
| 1998 | QualType ET = SemaRef.Context.getBaseElementType(F->getType()); |
| 1999 | if (hasAccessibleDestructor(ET, InitLoc, SemaRef)) { |
| 2000 | hadError = true; |
| 2001 | return; |
| 2002 | } |
| 2003 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2004 | |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 2005 | InitializedSomething = true; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 2006 | |
| 2007 | // Disable check for missing fields when designators are used. |
| 2008 | // This matches gcc behaviour. |
| 2009 | CheckForMissingFields = false; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2010 | continue; |
| 2011 | } |
| 2012 | |
| 2013 | if (Field == FieldEnd) { |
| 2014 | // We've run out of fields. We're done. |
| 2015 | break; |
| 2016 | } |
| 2017 | |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 2018 | // We've already initialized a member of a union. We're done. |
| 2019 | if (InitializedSomething && DeclType->isUnionType()) |
| 2020 | break; |
| 2021 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2022 | // If we've hit the flexible array member at the end, we're done. |
| 2023 | if (Field->getType()->isIncompleteArrayType()) |
| 2024 | break; |
| 2025 | |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 2026 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2027 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2028 | ++Field; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 2029 | continue; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 2030 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2031 | |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2032 | // Make sure we can use this declaration. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2033 | bool InvalidUse; |
| 2034 | if (VerifyOnly) |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 2035 | InvalidUse = !SemaRef.CanUseDecl(*Field, TreatUnavailableAsInvalid); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2036 | else |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2037 | InvalidUse = SemaRef.DiagnoseUseOfDecl( |
| 2038 | *Field, IList->getInit(Index)->getBeginLoc()); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2039 | if (InvalidUse) { |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2040 | ++Index; |
| 2041 | ++Field; |
| 2042 | hadError = true; |
| 2043 | continue; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2044 | } |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2045 | |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 2046 | if (!VerifyOnly) { |
| 2047 | QualType ET = SemaRef.Context.getBaseElementType(Field->getType()); |
| 2048 | if (hasAccessibleDestructor(ET, InitLoc, SemaRef)) { |
| 2049 | hadError = true; |
| 2050 | return; |
| 2051 | } |
| 2052 | } |
| 2053 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2054 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2055 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2056 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 2057 | StructuredList, StructuredIndex); |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 2058 | InitializedSomething = true; |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 2059 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2060 | if (DeclType->isUnionType() && !VerifyOnly) { |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 2061 | // Initialize the first field within the union. |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2062 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 2063 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2064 | |
| 2065 | ++Field; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 2066 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2067 | |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 2068 | // Emit warnings for missing struct field initializers. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2069 | if (!VerifyOnly && InitializedSomething && CheckForMissingFields && |
| 2070 | Field != FieldEnd && !Field->getType()->isIncompleteArrayType() && |
| 2071 | !DeclType->isUnionType()) { |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 2072 | // It is possible we have one or more unnamed bitfields remaining. |
| 2073 | // Find first (if any) named field and emit warning. |
| 2074 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 2075 | it != end; ++it) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 2076 | if (!it->isUnnamedBitfield() && !it->hasInClassInitializer()) { |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 2077 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
Aaron Ballman | b9bb201 | 2014-01-03 14:54:10 +0000 | [diff] [blame] | 2078 | diag::warn_missing_field_initializers) << *it; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 2079 | break; |
| 2080 | } |
| 2081 | } |
| 2082 | } |
| 2083 | |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 2084 | // Check that any remaining fields can be value-initialized. |
| 2085 | if (VerifyOnly && Field != FieldEnd && !DeclType->isUnionType() && |
| 2086 | !Field->getType()->isIncompleteArrayType()) { |
| 2087 | // FIXME: Should check for holes left by designated initializers too. |
| 2088 | for (; Field != FieldEnd && !hadError; ++Field) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 2089 | if (!Field->isUnnamedBitfield() && !Field->hasInClassInitializer()) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 2090 | CheckEmptyInitializable( |
| 2091 | InitializedEntity::InitializeMember(*Field, &Entity), |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2092 | IList->getEndLoc()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 2093 | } |
| 2094 | } |
| 2095 | |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 2096 | // Check that the types of the remaining fields have accessible destructors. |
| 2097 | if (!VerifyOnly) { |
| 2098 | // If the initializer expression has a designated initializer, check the |
| 2099 | // elements for which a designated initializer is not provided too. |
| 2100 | RecordDecl::field_iterator I = HasDesignatedInit ? RD->field_begin() |
| 2101 | : Field; |
| 2102 | for (RecordDecl::field_iterator E = RD->field_end(); I != E; ++I) { |
| 2103 | QualType ET = SemaRef.Context.getBaseElementType(I->getType()); |
| 2104 | if (hasAccessibleDestructor(ET, IList->getEndLoc(), SemaRef)) { |
| 2105 | hadError = true; |
| 2106 | return; |
| 2107 | } |
| 2108 | } |
| 2109 | } |
| 2110 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2111 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 2112 | Index >= IList->getNumInits()) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2113 | return; |
| 2114 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2115 | if (CheckFlexibleArrayInit(Entity, IList->getInit(Index), *Field, |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 2116 | TopLevelObject)) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2117 | hadError = true; |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 2118 | ++Index; |
| 2119 | return; |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2120 | } |
| 2121 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2122 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2123 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2124 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2125 | if (isa<InitListExpr>(IList->getInit(Index))) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2126 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2127 | StructuredList, StructuredIndex); |
| 2128 | else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2129 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 2130 | StructuredList, StructuredIndex); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 2131 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 2132 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2133 | /// Expand a field designator that refers to a member of an |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2134 | /// anonymous struct or union into a series of field designators that |
| 2135 | /// refers to the field within the appropriate subobject. |
| 2136 | /// |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2137 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2138 | DesignatedInitExpr *DIE, |
| 2139 | unsigned DesigIdx, |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2140 | IndirectFieldDecl *IndirectField) { |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2141 | typedef DesignatedInitExpr::Designator Designator; |
| 2142 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2143 | // Build the replacement designators. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2144 | SmallVector<Designator, 4> Replacements; |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2145 | for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), |
| 2146 | PE = IndirectField->chain_end(); PI != PE; ++PI) { |
| 2147 | if (PI + 1 == PE) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2148 | Replacements.push_back(Designator((IdentifierInfo *)nullptr, |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2149 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 2150 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 2151 | else |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2152 | Replacements.push_back(Designator((IdentifierInfo *)nullptr, |
| 2153 | SourceLocation(), SourceLocation())); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2154 | assert(isa<FieldDecl>(*PI)); |
| 2155 | Replacements.back().setField(cast<FieldDecl>(*PI)); |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2156 | } |
| 2157 | |
| 2158 | // Expand the current designator into the set of replacement |
| 2159 | // designators, so we have a full subobject path down to where the |
| 2160 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 03e8bdc | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 2161 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2162 | &Replacements[0] + Replacements.size()); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2163 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2164 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2165 | static DesignatedInitExpr *CloneDesignatedInitExpr(Sema &SemaRef, |
| 2166 | DesignatedInitExpr *DIE) { |
| 2167 | unsigned NumIndexExprs = DIE->getNumSubExprs() - 1; |
| 2168 | SmallVector<Expr*, 4> IndexExprs(NumIndexExprs); |
| 2169 | for (unsigned I = 0; I < NumIndexExprs; ++I) |
| 2170 | IndexExprs[I] = DIE->getSubExpr(I + 1); |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 2171 | return DesignatedInitExpr::Create(SemaRef.Context, DIE->designators(), |
| 2172 | IndexExprs, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2173 | DIE->getEqualOrColonLoc(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2174 | DIE->usesGNUSyntax(), DIE->getInit()); |
| 2175 | } |
| 2176 | |
Kaelyn Uhrain | b02c5e9 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 2177 | namespace { |
| 2178 | |
| 2179 | // Callback to only accept typo corrections that are for field members of |
| 2180 | // the given struct or union. |
| 2181 | class FieldInitializerValidatorCCC : public CorrectionCandidateCallback { |
| 2182 | public: |
| 2183 | explicit FieldInitializerValidatorCCC(RecordDecl *RD) |
| 2184 | : Record(RD) {} |
| 2185 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 2186 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
Kaelyn Uhrain | b02c5e9 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 2187 | FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>(); |
| 2188 | return FD && FD->getDeclContext()->getRedeclContext()->Equals(Record); |
| 2189 | } |
| 2190 | |
| 2191 | private: |
| 2192 | RecordDecl *Record; |
| 2193 | }; |
| 2194 | |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 2195 | } // end anonymous namespace |
Kaelyn Uhrain | b02c5e9 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 2196 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2197 | /// Check the well-formedness of a C99 designated initializer. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2198 | /// |
| 2199 | /// Determines whether the designated initializer @p DIE, which |
| 2200 | /// resides at the given @p Index within the initializer list @p |
| 2201 | /// IList, is well-formed for a current object of type @p DeclType |
| 2202 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2203 | /// within the current subobject is returned in either |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2204 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2205 | /// |
| 2206 | /// @param IList The initializer list in which this designated |
| 2207 | /// initializer occurs. |
| 2208 | /// |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 2209 | /// @param DIE The designated initializer expression. |
| 2210 | /// |
| 2211 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2212 | /// |
Dmitri Gribenko | adba9be | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 2213 | /// @param CurrentObjectType The type of the "current object" (C99 6.7.8p17), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2214 | /// into which the designation in @p DIE should refer. |
| 2215 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2216 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 2217 | /// a field, this will be set to the field declaration corresponding |
| 2218 | /// to the field named by the designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2219 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2220 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 2221 | /// DIE is an array designator or GNU array-range designator, this |
| 2222 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2223 | /// |
| 2224 | /// @param Index Index into @p IList where the designated initializer |
| 2225 | /// @p DIE occurs. |
| 2226 | /// |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2227 | /// @param StructuredList The initializer list expression that |
| 2228 | /// describes all of the subobject initializers in the order they'll |
| 2229 | /// actually be initialized. |
| 2230 | /// |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2231 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2232 | bool |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2233 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2234 | InitListExpr *IList, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2235 | DesignatedInitExpr *DIE, |
| 2236 | unsigned DesigIdx, |
| 2237 | QualType &CurrentObjectType, |
| 2238 | RecordDecl::field_iterator *NextField, |
| 2239 | llvm::APSInt *NextElementIndex, |
| 2240 | unsigned &Index, |
| 2241 | InitListExpr *StructuredList, |
| 2242 | unsigned &StructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2243 | bool FinishSubobjectInit, |
| 2244 | bool TopLevelObject) { |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 2245 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2246 | // Check the actual initialization for the designated object type. |
| 2247 | bool prevHadError = hadError; |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 2248 | |
| 2249 | // Temporarily remove the designator expression from the |
| 2250 | // initializer list that the child calls see, so that we don't try |
| 2251 | // to re-process the designator. |
| 2252 | unsigned OldIndex = Index; |
| 2253 | IList->setInit(OldIndex, DIE->getInit()); |
| 2254 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2255 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2256 | StructuredList, StructuredIndex); |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 2257 | |
| 2258 | // Restore the designated initializer expression in the syntactic |
| 2259 | // form of the initializer list. |
| 2260 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 2261 | DIE->setInit(IList->getInit(OldIndex)); |
| 2262 | IList->setInit(OldIndex, DIE); |
| 2263 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2264 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 2267 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2268 | bool IsFirstDesignator = (DesigIdx == 0); |
| 2269 | if (!VerifyOnly) { |
| 2270 | assert((IsFirstDesignator || StructuredList) && |
| 2271 | "Need a non-designated initializer list to start from"); |
| 2272 | |
| 2273 | // Determine the structural initializer list that corresponds to the |
| 2274 | // current subobject. |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2275 | if (IsFirstDesignator) |
| 2276 | StructuredList = SyntacticToSemantic.lookup(IList); |
| 2277 | else { |
| 2278 | Expr *ExistingInit = StructuredIndex < StructuredList->getNumInits() ? |
| 2279 | StructuredList->getInit(StructuredIndex) : nullptr; |
| 2280 | if (!ExistingInit && StructuredList->hasArrayFiller()) |
| 2281 | ExistingInit = StructuredList->getArrayFiller(); |
| 2282 | |
| 2283 | if (!ExistingInit) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2284 | StructuredList = getStructuredSubobjectInit( |
| 2285 | IList, Index, CurrentObjectType, StructuredList, StructuredIndex, |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2286 | SourceRange(D->getBeginLoc(), DIE->getEndLoc())); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2287 | else if (InitListExpr *Result = dyn_cast<InitListExpr>(ExistingInit)) |
| 2288 | StructuredList = Result; |
| 2289 | else { |
| 2290 | if (DesignatedInitUpdateExpr *E = |
| 2291 | dyn_cast<DesignatedInitUpdateExpr>(ExistingInit)) |
| 2292 | StructuredList = E->getUpdater(); |
| 2293 | else { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2294 | DesignatedInitUpdateExpr *DIUE = new (SemaRef.Context) |
| 2295 | DesignatedInitUpdateExpr(SemaRef.Context, D->getBeginLoc(), |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2296 | ExistingInit, DIE->getEndLoc()); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2297 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, DIUE); |
| 2298 | StructuredList = DIUE->getUpdater(); |
| 2299 | } |
| 2300 | |
| 2301 | // We need to check on source range validity because the previous |
| 2302 | // initializer does not have to be an explicit initializer. e.g., |
| 2303 | // |
| 2304 | // struct P { int a, b; }; |
| 2305 | // struct PP { struct P p } l = { { .a = 2 }, .p.b = 3 }; |
| 2306 | // |
| 2307 | // There is an overwrite taking place because the first braced initializer |
| 2308 | // list "{ .a = 2 }" already provides value for .p.b (which is zero). |
| 2309 | if (ExistingInit->getSourceRange().isValid()) { |
| 2310 | // We are creating an initializer list that initializes the |
| 2311 | // subobjects of the current object, but there was already an |
| 2312 | // initialization that completely initialized the current |
| 2313 | // subobject, e.g., by a compound literal: |
| 2314 | // |
| 2315 | // struct X { int a, b; }; |
| 2316 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
| 2317 | // |
| 2318 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 2319 | // designated initializer re-initializes the whole |
| 2320 | // subobject [0], overwriting previous initializers. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2321 | SemaRef.Diag(D->getBeginLoc(), |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2322 | diag::warn_subobject_initializer_overrides) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2323 | << SourceRange(D->getBeginLoc(), DIE->getEndLoc()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2324 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2325 | SemaRef.Diag(ExistingInit->getBeginLoc(), |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2326 | diag::note_previous_initializer) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2327 | << /*FIXME:has side effects=*/0 << ExistingInit->getSourceRange(); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2328 | } |
| 2329 | } |
| 2330 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2331 | assert(StructuredList && "Expected a structured initializer list"); |
| 2332 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2333 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2334 | if (D->isFieldDesignator()) { |
| 2335 | // C99 6.7.8p7: |
| 2336 | // |
| 2337 | // If a designator has the form |
| 2338 | // |
| 2339 | // . identifier |
| 2340 | // |
| 2341 | // then the current object (defined below) shall have |
| 2342 | // structure or union type and the identifier shall be the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2343 | // name of a member of that type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2344 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2345 | if (!RT) { |
| 2346 | SourceLocation Loc = D->getDotLoc(); |
| 2347 | if (Loc.isInvalid()) |
| 2348 | Loc = D->getFieldLoc(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2349 | if (!VerifyOnly) |
| 2350 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2351 | << SemaRef.getLangOpts().CPlusPlus << CurrentObjectType; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2352 | ++Index; |
| 2353 | return true; |
| 2354 | } |
| 2355 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2356 | FieldDecl *KnownField = D->getField(); |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2357 | if (!KnownField) { |
| 2358 | IdentifierInfo *FieldName = D->getFieldName(); |
| 2359 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
| 2360 | for (NamedDecl *ND : Lookup) { |
| 2361 | if (auto *FD = dyn_cast<FieldDecl>(ND)) { |
| 2362 | KnownField = FD; |
| 2363 | break; |
| 2364 | } |
| 2365 | if (auto *IFD = dyn_cast<IndirectFieldDecl>(ND)) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2366 | // In verify mode, don't modify the original. |
| 2367 | if (VerifyOnly) |
| 2368 | DIE = CloneDesignatedInitExpr(SemaRef, DIE); |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2369 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IFD); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2370 | D = DIE->getDesignator(DesigIdx); |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2371 | KnownField = cast<FieldDecl>(*IFD->chain_begin()); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2372 | break; |
| 2373 | } |
| 2374 | } |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2375 | if (!KnownField) { |
| 2376 | if (VerifyOnly) { |
| 2377 | ++Index; |
| 2378 | return true; // No typo correction when just trying this out. |
| 2379 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2380 | |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2381 | // Name lookup found something, but it wasn't a field. |
| 2382 | if (!Lookup.empty()) { |
| 2383 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
| 2384 | << FieldName; |
| 2385 | SemaRef.Diag(Lookup.front()->getLocation(), |
| 2386 | diag::note_field_designator_found); |
| 2387 | ++Index; |
| 2388 | return true; |
| 2389 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2390 | |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2391 | // Name lookup didn't find anything. |
| 2392 | // Determine whether this was a typo for another field name. |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 2393 | if (TypoCorrection Corrected = SemaRef.CorrectTypo( |
| 2394 | DeclarationNameInfo(FieldName, D->getFieldLoc()), |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2395 | Sema::LookupMemberName, /*Scope=*/nullptr, /*SS=*/nullptr, |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 2396 | llvm::make_unique<FieldInitializerValidatorCCC>(RT->getDecl()), |
| 2397 | Sema::CTK_ErrorRecovery, RT->getDecl())) { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 2398 | SemaRef.diagnoseTypo( |
| 2399 | Corrected, |
| 2400 | SemaRef.PDiag(diag::err_field_designator_unknown_suggest) |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2401 | << FieldName << CurrentObjectType); |
| 2402 | KnownField = Corrected.getCorrectionDeclAs<FieldDecl>(); |
Benjamin Kramer | afe718f | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 2403 | hadError = true; |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 2404 | } else { |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2405 | // Typo correction didn't find anything. |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 2406 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 2407 | << FieldName << CurrentObjectType; |
| 2408 | ++Index; |
| 2409 | return true; |
| 2410 | } |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 2411 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2412 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2413 | |
David Majnemer | 58e4ea9 | 2014-08-23 01:48:50 +0000 | [diff] [blame] | 2414 | unsigned FieldIndex = 0; |
Akira Hatanaka | 8eccb9b | 2017-01-17 19:35:54 +0000 | [diff] [blame] | 2415 | |
| 2416 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) |
| 2417 | FieldIndex = CXXRD->getNumBases(); |
| 2418 | |
David Majnemer | 58e4ea9 | 2014-08-23 01:48:50 +0000 | [diff] [blame] | 2419 | for (auto *FI : RT->getDecl()->fields()) { |
| 2420 | if (FI->isUnnamedBitfield()) |
| 2421 | continue; |
Richard Smith | fe1bc70 | 2016-04-08 19:57:40 +0000 | [diff] [blame] | 2422 | if (declaresSameEntity(KnownField, FI)) { |
| 2423 | KnownField = FI; |
David Majnemer | 58e4ea9 | 2014-08-23 01:48:50 +0000 | [diff] [blame] | 2424 | break; |
Richard Smith | fe1bc70 | 2016-04-08 19:57:40 +0000 | [diff] [blame] | 2425 | } |
David Majnemer | 58e4ea9 | 2014-08-23 01:48:50 +0000 | [diff] [blame] | 2426 | ++FieldIndex; |
| 2427 | } |
| 2428 | |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2429 | RecordDecl::field_iterator Field = |
| 2430 | RecordDecl::field_iterator(DeclContext::decl_iterator(KnownField)); |
| 2431 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2432 | // All of the fields of a union are located at the same place in |
| 2433 | // the initializer list. |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 2434 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2435 | FieldIndex = 0; |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2436 | if (!VerifyOnly) { |
| 2437 | FieldDecl *CurrentField = StructuredList->getInitializedFieldInUnion(); |
Richard Smith | fe1bc70 | 2016-04-08 19:57:40 +0000 | [diff] [blame] | 2438 | if (CurrentField && !declaresSameEntity(CurrentField, *Field)) { |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2439 | assert(StructuredList->getNumInits() == 1 |
| 2440 | && "A union should never have more than one initializer!"); |
| 2441 | |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2442 | Expr *ExistingInit = StructuredList->getInit(0); |
Vassil Vassilev | 1a1678e | 2017-04-14 08:48:08 +0000 | [diff] [blame] | 2443 | if (ExistingInit) { |
| 2444 | // We're about to throw away an initializer, emit warning. |
| 2445 | SemaRef.Diag(D->getFieldLoc(), |
| 2446 | diag::warn_initializer_overrides) |
| 2447 | << D->getSourceRange(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2448 | SemaRef.Diag(ExistingInit->getBeginLoc(), |
Vassil Vassilev | 1a1678e | 2017-04-14 08:48:08 +0000 | [diff] [blame] | 2449 | diag::note_previous_initializer) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2450 | << /*FIXME:has side effects=*/0 |
| 2451 | << ExistingInit->getSourceRange(); |
Vassil Vassilev | 1a1678e | 2017-04-14 08:48:08 +0000 | [diff] [blame] | 2452 | } |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2453 | |
| 2454 | // remove existing initializer |
| 2455 | StructuredList->resizeInits(SemaRef.Context, 0); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2456 | StructuredList->setInitializedFieldInUnion(nullptr); |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2457 | } |
| 2458 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2459 | StructuredList->setInitializedFieldInUnion(*Field); |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2460 | } |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 2461 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2462 | |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2463 | // Make sure we can use this declaration. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2464 | bool InvalidUse; |
| 2465 | if (VerifyOnly) |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 2466 | InvalidUse = !SemaRef.CanUseDecl(*Field, TreatUnavailableAsInvalid); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2467 | else |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2468 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, D->getFieldLoc()); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2469 | if (InvalidUse) { |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2470 | ++Index; |
| 2471 | return true; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2472 | } |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2473 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2474 | if (!VerifyOnly) { |
| 2475 | // Update the designator with the field declaration. |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2476 | D->setField(*Field); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2477 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2478 | // Make sure that our non-designated initializer list has space |
| 2479 | // for a subobject corresponding to this field. |
| 2480 | if (FieldIndex >= StructuredList->getNumInits()) |
| 2481 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
| 2482 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2483 | |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2484 | // This designator names a flexible array member. |
| 2485 | if (Field->getType()->isIncompleteArrayType()) { |
| 2486 | bool Invalid = false; |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 2487 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2488 | // We can't designate an object within the flexible array |
| 2489 | // member (because GCC doesn't allow it). |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2490 | if (!VerifyOnly) { |
| 2491 | DesignatedInitExpr::Designator *NextD |
| 2492 | = DIE->getDesignator(DesigIdx + 1); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2493 | SemaRef.Diag(NextD->getBeginLoc(), |
| 2494 | diag::err_designator_into_flexible_array_member) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2495 | << SourceRange(NextD->getBeginLoc(), DIE->getEndLoc()); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2496 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2497 | << *Field; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2498 | } |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2499 | Invalid = true; |
| 2500 | } |
| 2501 | |
Chris Lattner | 001b29c | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 2502 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 2503 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2504 | // The initializer is not an initializer list. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2505 | if (!VerifyOnly) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2506 | SemaRef.Diag(DIE->getInit()->getBeginLoc(), |
| 2507 | diag::err_flexible_array_init_needs_braces) |
| 2508 | << DIE->getInit()->getSourceRange(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2509 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2510 | << *Field; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2511 | } |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2512 | Invalid = true; |
| 2513 | } |
| 2514 | |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 2515 | // Check GNU flexible array initializer. |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2516 | if (!Invalid && CheckFlexibleArrayInit(Entity, DIE->getInit(), *Field, |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 2517 | TopLevelObject)) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2518 | Invalid = true; |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2519 | |
| 2520 | if (Invalid) { |
| 2521 | ++Index; |
| 2522 | return true; |
| 2523 | } |
| 2524 | |
| 2525 | // Initialize the array. |
| 2526 | bool prevHadError = hadError; |
| 2527 | unsigned newStructuredIndex = FieldIndex; |
| 2528 | unsigned OldIndex = Index; |
| 2529 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2530 | |
| 2531 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2532 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2533 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2534 | StructuredList, newStructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2535 | |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2536 | IList->setInit(OldIndex, DIE); |
| 2537 | if (hadError && !prevHadError) { |
| 2538 | ++Field; |
| 2539 | ++FieldIndex; |
| 2540 | if (NextField) |
| 2541 | *NextField = Field; |
| 2542 | StructuredIndex = FieldIndex; |
| 2543 | return true; |
| 2544 | } |
| 2545 | } else { |
| 2546 | // Recurse to check later designated subobjects. |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 2547 | QualType FieldType = Field->getType(); |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2548 | unsigned newStructuredIndex = FieldIndex; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2549 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2550 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2551 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2552 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2553 | FieldType, nullptr, nullptr, Index, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2554 | StructuredList, newStructuredIndex, |
Alexey Bataev | 86a489e | 2016-01-25 05:14:03 +0000 | [diff] [blame] | 2555 | FinishSubobjectInit, false)) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2556 | return true; |
| 2557 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2558 | |
| 2559 | // Find the position of the next field to be initialized in this |
| 2560 | // subobject. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2561 | ++Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2562 | ++FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2563 | |
| 2564 | // If this the first designator, our caller will continue checking |
| 2565 | // the rest of this struct/class/union subobject. |
| 2566 | if (IsFirstDesignator) { |
| 2567 | if (NextField) |
| 2568 | *NextField = Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2569 | StructuredIndex = FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2570 | return false; |
| 2571 | } |
| 2572 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2573 | if (!FinishSubobjectInit) |
| 2574 | return false; |
| 2575 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2576 | // We've already initialized something in the union; we're done. |
| 2577 | if (RT->getDecl()->isUnion()) |
| 2578 | return hadError; |
| 2579 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2580 | // Check the remaining fields within this class/struct/union subobject. |
| 2581 | bool prevHadError = hadError; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2582 | |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 2583 | auto NoBases = |
| 2584 | CXXRecordDecl::base_class_range(CXXRecordDecl::base_class_iterator(), |
| 2585 | CXXRecordDecl::base_class_iterator()); |
| 2586 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, NoBases, Field, |
| 2587 | false, Index, StructuredList, FieldIndex); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2588 | return hadError && !prevHadError; |
| 2589 | } |
| 2590 | |
| 2591 | // C99 6.7.8p6: |
| 2592 | // |
| 2593 | // If a designator has the form |
| 2594 | // |
| 2595 | // [ constant-expression ] |
| 2596 | // |
| 2597 | // then the current object (defined below) shall have array |
| 2598 | // type and the expression shall be an integer constant |
| 2599 | // expression. If the array is of unknown size, any |
| 2600 | // nonnegative value is valid. |
| 2601 | // |
| 2602 | // Additionally, cope with the GNU extension that permits |
| 2603 | // designators of the form |
| 2604 | // |
| 2605 | // [ constant-expression ... constant-expression ] |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 2606 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2607 | if (!AT) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2608 | if (!VerifyOnly) |
| 2609 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
| 2610 | << CurrentObjectType; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2611 | ++Index; |
| 2612 | return true; |
| 2613 | } |
| 2614 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2615 | Expr *IndexExpr = nullptr; |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2616 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 2617 | if (D->isArrayDesignator()) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2618 | IndexExpr = DIE->getArrayIndex(*D); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2619 | DesignatedStartIndex = IndexExpr->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2620 | DesignatedEndIndex = DesignatedStartIndex; |
| 2621 | } else { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2622 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2623 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2624 | DesignatedStartIndex = |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2625 | DIE->getArrayRangeStart(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2626 | DesignatedEndIndex = |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2627 | DIE->getArrayRangeEnd(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2628 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2629 | |
Chris Lattner | b0ed51d | 2011-02-19 22:28:58 +0000 | [diff] [blame] | 2630 | // Codegen can't handle evaluating array range designators that have side |
| 2631 | // effects, because we replicate the AST value for each initialized element. |
| 2632 | // As such, set the sawArrayRangeDesignator() bit if we initialize multiple |
| 2633 | // elements with something that has a side effect, so codegen can emit an |
| 2634 | // "error unsupported" error instead of miscompiling the app. |
| 2635 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2636 | DIE->getInit()->HasSideEffects(SemaRef.Context) && !VerifyOnly) |
Douglas Gregor | bf7207a | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 2637 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2638 | } |
| 2639 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2640 | if (isa<ConstantArrayType>(AT)) { |
| 2641 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2642 | DesignatedStartIndex |
| 2643 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2644 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2645 | DesignatedEndIndex |
| 2646 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2647 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 2648 | if (DesignatedEndIndex >= MaxElements) { |
Eli Friedman | ed0f916 | 2011-09-26 18:53:43 +0000 | [diff] [blame] | 2649 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2650 | SemaRef.Diag(IndexExpr->getBeginLoc(), |
| 2651 | diag::err_array_designator_too_large) |
| 2652 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
| 2653 | << IndexExpr->getSourceRange(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2654 | ++Index; |
| 2655 | return true; |
| 2656 | } |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2657 | } else { |
Argyrios Kyrtzidis | 4746c2f | 2015-07-27 23:16:53 +0000 | [diff] [blame] | 2658 | unsigned DesignatedIndexBitWidth = |
| 2659 | ConstantArrayType::getMaxSizeBits(SemaRef.Context); |
| 2660 | DesignatedStartIndex = |
| 2661 | DesignatedStartIndex.extOrTrunc(DesignatedIndexBitWidth); |
| 2662 | DesignatedEndIndex = |
| 2663 | DesignatedEndIndex.extOrTrunc(DesignatedIndexBitWidth); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2664 | DesignatedStartIndex.setIsUnsigned(true); |
| 2665 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2666 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2667 | |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2668 | if (!VerifyOnly && StructuredList->isStringLiteralInit()) { |
| 2669 | // We're modifying a string literal init; we have to decompose the string |
| 2670 | // so we can modify the individual characters. |
| 2671 | ASTContext &Context = SemaRef.Context; |
| 2672 | Expr *SubExpr = StructuredList->getInit(0)->IgnoreParens(); |
| 2673 | |
| 2674 | // Compute the character type |
| 2675 | QualType CharTy = AT->getElementType(); |
| 2676 | |
| 2677 | // Compute the type of the integer literals. |
| 2678 | QualType PromotedCharTy = CharTy; |
| 2679 | if (CharTy->isPromotableIntegerType()) |
| 2680 | PromotedCharTy = Context.getPromotedIntegerType(CharTy); |
| 2681 | unsigned PromotedCharTyWidth = Context.getTypeSize(PromotedCharTy); |
| 2682 | |
| 2683 | if (StringLiteral *SL = dyn_cast<StringLiteral>(SubExpr)) { |
| 2684 | // Get the length of the string. |
| 2685 | uint64_t StrLen = SL->getLength(); |
| 2686 | if (cast<ConstantArrayType>(AT)->getSize().ult(StrLen)) |
| 2687 | StrLen = cast<ConstantArrayType>(AT)->getSize().getZExtValue(); |
| 2688 | StructuredList->resizeInits(Context, StrLen); |
| 2689 | |
| 2690 | // Build a literal for each character in the string, and put them into |
| 2691 | // the init list. |
| 2692 | for (unsigned i = 0, e = StrLen; i != e; ++i) { |
| 2693 | llvm::APInt CodeUnit(PromotedCharTyWidth, SL->getCodeUnit(i)); |
| 2694 | Expr *Init = new (Context) IntegerLiteral( |
Eli Friedman | 6cc05f7 | 2013-06-11 22:26:34 +0000 | [diff] [blame] | 2695 | Context, CodeUnit, PromotedCharTy, SubExpr->getExprLoc()); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2696 | if (CharTy != PromotedCharTy) |
| 2697 | Init = ImplicitCastExpr::Create(Context, CharTy, CK_IntegralCast, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2698 | Init, nullptr, VK_RValue); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2699 | StructuredList->updateInit(Context, i, Init); |
| 2700 | } |
| 2701 | } else { |
| 2702 | ObjCEncodeExpr *E = cast<ObjCEncodeExpr>(SubExpr); |
| 2703 | std::string Str; |
| 2704 | Context.getObjCEncodingForType(E->getEncodedType(), Str); |
| 2705 | |
| 2706 | // Get the length of the string. |
| 2707 | uint64_t StrLen = Str.size(); |
| 2708 | if (cast<ConstantArrayType>(AT)->getSize().ult(StrLen)) |
| 2709 | StrLen = cast<ConstantArrayType>(AT)->getSize().getZExtValue(); |
| 2710 | StructuredList->resizeInits(Context, StrLen); |
| 2711 | |
| 2712 | // Build a literal for each character in the string, and put them into |
| 2713 | // the init list. |
| 2714 | for (unsigned i = 0, e = StrLen; i != e; ++i) { |
| 2715 | llvm::APInt CodeUnit(PromotedCharTyWidth, Str[i]); |
| 2716 | Expr *Init = new (Context) IntegerLiteral( |
Eli Friedman | 6cc05f7 | 2013-06-11 22:26:34 +0000 | [diff] [blame] | 2717 | Context, CodeUnit, PromotedCharTy, SubExpr->getExprLoc()); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2718 | if (CharTy != PromotedCharTy) |
| 2719 | Init = ImplicitCastExpr::Create(Context, CharTy, CK_IntegralCast, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2720 | Init, nullptr, VK_RValue); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2721 | StructuredList->updateInit(Context, i, Init); |
| 2722 | } |
| 2723 | } |
| 2724 | } |
| 2725 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2726 | // Make sure that our non-designated initializer list has space |
| 2727 | // for a subobject corresponding to this array element. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2728 | if (!VerifyOnly && |
| 2729 | DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2730 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2731 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2732 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2733 | // Repeatedly perform subobject initializations in the range |
| 2734 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2735 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2736 | // Move to the next designator |
| 2737 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2738 | unsigned OldIndex = Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2739 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2740 | InitializedEntity ElementEntity = |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2741 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2742 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2743 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 2744 | // Recurse to check later designated subobjects. |
| 2745 | QualType ElementType = AT->getElementType(); |
| 2746 | Index = OldIndex; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2747 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2748 | ElementEntity.setElementIndex(ElementIndex); |
Alexey Bataev | 86a489e | 2016-01-25 05:14:03 +0000 | [diff] [blame] | 2749 | if (CheckDesignatedInitializer( |
| 2750 | ElementEntity, IList, DIE, DesigIdx + 1, ElementType, nullptr, |
| 2751 | nullptr, Index, StructuredList, ElementIndex, |
| 2752 | FinishSubobjectInit && (DesignatedStartIndex == DesignatedEndIndex), |
| 2753 | false)) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2754 | return true; |
| 2755 | |
| 2756 | // Move to the next index in the array that we'll be initializing. |
| 2757 | ++DesignatedStartIndex; |
| 2758 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2759 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2760 | |
| 2761 | // If this the first designator, our caller will continue checking |
| 2762 | // the rest of this array subobject. |
| 2763 | if (IsFirstDesignator) { |
| 2764 | if (NextElementIndex) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2765 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2766 | StructuredIndex = ElementIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2767 | return false; |
| 2768 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2769 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2770 | if (!FinishSubobjectInit) |
| 2771 | return false; |
| 2772 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2773 | // Check the remaining elements within this array subobject. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2774 | bool prevHadError = hadError; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2775 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 2776 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2777 | StructuredList, ElementIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2778 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2779 | } |
| 2780 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2781 | // Get the structured initializer list for a subobject of type |
| 2782 | // @p CurrentObjectType. |
| 2783 | InitListExpr * |
| 2784 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 2785 | QualType CurrentObjectType, |
| 2786 | InitListExpr *StructuredList, |
| 2787 | unsigned StructuredIndex, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2788 | SourceRange InitRange, |
| 2789 | bool IsFullyOverwritten) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2790 | if (VerifyOnly) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2791 | return nullptr; // No structured list in verification-only mode. |
| 2792 | Expr *ExistingInit = nullptr; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2793 | if (!StructuredList) |
Benjamin Kramer | 6b441d6 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 2794 | ExistingInit = SyntacticToSemantic.lookup(IList); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2795 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 2796 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2797 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2798 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2799 | // There might have already been initializers for subobjects of the current |
| 2800 | // object, but a subsequent initializer list will overwrite the entirety |
| 2801 | // of the current object. (See DR 253 and C99 6.7.8p21). e.g., |
| 2802 | // |
| 2803 | // struct P { char x[6]; }; |
| 2804 | // struct P l = { .x[2] = 'x', .x = { [0] = 'f' } }; |
| 2805 | // |
| 2806 | // The first designated initializer is ignored, and l.x is just "f". |
| 2807 | if (!IsFullyOverwritten) |
| 2808 | return Result; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2809 | |
| 2810 | if (ExistingInit) { |
| 2811 | // We are creating an initializer list that initializes the |
| 2812 | // subobjects of the current object, but there was already an |
| 2813 | // initialization that completely initialized the current |
| 2814 | // subobject, e.g., by a compound literal: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2815 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2816 | // struct X { int a, b; }; |
| 2817 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2818 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2819 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 2820 | // designated initializer re-initializes the whole |
| 2821 | // subobject [0], overwriting previous initializers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2822 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2823 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2824 | << InitRange; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2825 | SemaRef.Diag(ExistingInit->getBeginLoc(), diag::note_previous_initializer) |
| 2826 | << /*FIXME:has side effects=*/0 << ExistingInit->getSourceRange(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2827 | } |
| 2828 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2829 | InitListExpr *Result |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2830 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
Dmitri Gribenko | 78852e9 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 2831 | InitRange.getBegin(), None, |
Ted Kremenek | 013041e | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 2832 | InitRange.getEnd()); |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2833 | |
Eli Friedman | 91f5ae5 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 2834 | QualType ResultType = CurrentObjectType; |
| 2835 | if (!ResultType->isArrayType()) |
| 2836 | ResultType = ResultType.getNonLValueExprType(SemaRef.Context); |
| 2837 | Result->setType(ResultType); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2838 | |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2839 | // Pre-allocate storage for the structured initializer list. |
| 2840 | unsigned NumElements = 0; |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2841 | unsigned NumInits = 0; |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2842 | bool GotNumInits = false; |
| 2843 | if (!StructuredList) { |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2844 | NumInits = IList->getNumInits(); |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2845 | GotNumInits = true; |
| 2846 | } else if (Index < IList->getNumInits()) { |
| 2847 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) { |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2848 | NumInits = SubList->getNumInits(); |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2849 | GotNumInits = true; |
| 2850 | } |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2851 | } |
| 2852 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2853 | if (const ArrayType *AType |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2854 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 2855 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 2856 | NumElements = CAType->getSize().getZExtValue(); |
| 2857 | // Simple heuristic so that we don't allocate a very large |
| 2858 | // initializer with many empty entries at the end. |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2859 | if (GotNumInits && NumElements > NumInits) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2860 | NumElements = 0; |
| 2861 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2862 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2863 | NumElements = VType->getNumElements(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2864 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2865 | RecordDecl *RDecl = RType->getDecl(); |
| 2866 | if (RDecl->isUnion()) |
| 2867 | NumElements = 1; |
| 2868 | else |
Aaron Ballman | 62e47c4 | 2014-03-10 13:43:55 +0000 | [diff] [blame] | 2869 | NumElements = std::distance(RDecl->field_begin(), RDecl->field_end()); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2870 | } |
| 2871 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2872 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2873 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2874 | // Link this new initializer list into the structured initializer |
| 2875 | // lists. |
| 2876 | if (StructuredList) |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2877 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2878 | else { |
| 2879 | Result->setSyntacticForm(IList); |
| 2880 | SyntacticToSemantic[IList] = Result; |
| 2881 | } |
| 2882 | |
| 2883 | return Result; |
| 2884 | } |
| 2885 | |
| 2886 | /// Update the initializer at index @p StructuredIndex within the |
| 2887 | /// structured initializer list to the value @p expr. |
| 2888 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 2889 | unsigned &StructuredIndex, |
| 2890 | Expr *expr) { |
| 2891 | // No structured initializer list to update |
| 2892 | if (!StructuredList) |
| 2893 | return; |
| 2894 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2895 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 2896 | StructuredIndex, expr)) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2897 | // This initializer overwrites a previous initializer. Warn. |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2898 | // We need to check on source range validity because the previous |
| 2899 | // initializer does not have to be an explicit initializer. |
| 2900 | // struct P { int a, b; }; |
| 2901 | // struct PP { struct P p } l = { { .a = 2 }, .p.b = 3 }; |
| 2902 | // There is an overwrite taking place because the first braced initializer |
| 2903 | // list "{ .a = 2 }' already provides value for .p.b (which is zero). |
| 2904 | if (PrevInit->getSourceRange().isValid()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2905 | SemaRef.Diag(expr->getBeginLoc(), diag::warn_initializer_overrides) |
| 2906 | << expr->getSourceRange(); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2907 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2908 | SemaRef.Diag(PrevInit->getBeginLoc(), diag::note_previous_initializer) |
| 2909 | << /*FIXME:has side effects=*/0 << PrevInit->getSourceRange(); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2910 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2911 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2912 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2913 | ++StructuredIndex; |
| 2914 | } |
| 2915 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2916 | /// Check that the given Index expression is a valid array designator |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2917 | /// value. This is essentially just a wrapper around |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2918 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2919 | /// and produces a reasonable diagnostic if there is a |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2920 | /// failure. Returns the index expression, possibly with an implicit cast |
| 2921 | /// added, on success. If everything went okay, Value will receive the |
| 2922 | /// value of the constant expression. |
| 2923 | static ExprResult |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2924 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2925 | SourceLocation Loc = Index->getBeginLoc(); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2926 | |
| 2927 | // Make sure this is an integer constant expression. |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2928 | ExprResult Result = S.VerifyIntegerConstantExpression(Index, &Value); |
| 2929 | if (Result.isInvalid()) |
| 2930 | return Result; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2931 | |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2932 | if (Value.isSigned() && Value.isNegative()) |
| 2933 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2934 | << Value.toString(10) << Index->getSourceRange(); |
| 2935 | |
Douglas Gregor | 51650d3 | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 2936 | Value.setIsUnsigned(true); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2937 | return Result; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2938 | } |
| 2939 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2940 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 2941 | SourceLocation Loc, |
| 2942 | bool GNUSyntax, |
| 2943 | ExprResult Init) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2944 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 2945 | |
| 2946 | bool Invalid = false; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2947 | SmallVector<ASTDesignator, 32> Designators; |
| 2948 | SmallVector<Expr *, 32> InitExpressions; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2949 | |
| 2950 | // Build designators and check array designator expressions. |
| 2951 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 2952 | const Designator &D = Desig.getDesignator(Idx); |
| 2953 | switch (D.getKind()) { |
| 2954 | case Designator::FieldDesignator: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2955 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2956 | D.getFieldLoc())); |
| 2957 | break; |
| 2958 | |
| 2959 | case Designator::ArrayDesignator: { |
| 2960 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 2961 | llvm::APSInt IndexValue; |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2962 | if (!Index->isTypeDependent() && !Index->isValueDependent()) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2963 | Index = CheckArrayDesignatorExpr(*this, Index, IndexValue).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2964 | if (!Index) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2965 | Invalid = true; |
| 2966 | else { |
| 2967 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2968 | D.getLBracketLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2969 | D.getRBracketLoc())); |
| 2970 | InitExpressions.push_back(Index); |
| 2971 | } |
| 2972 | break; |
| 2973 | } |
| 2974 | |
| 2975 | case Designator::ArrayRangeDesignator: { |
| 2976 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 2977 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 2978 | llvm::APSInt StartValue; |
| 2979 | llvm::APSInt EndValue; |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2980 | bool StartDependent = StartIndex->isTypeDependent() || |
| 2981 | StartIndex->isValueDependent(); |
| 2982 | bool EndDependent = EndIndex->isTypeDependent() || |
| 2983 | EndIndex->isValueDependent(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2984 | if (!StartDependent) |
| 2985 | StartIndex = |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2986 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2987 | if (!EndDependent) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2988 | EndIndex = CheckArrayDesignatorExpr(*this, EndIndex, EndValue).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2989 | |
| 2990 | if (!StartIndex || !EndIndex) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2991 | Invalid = true; |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2992 | else { |
| 2993 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2994 | if (StartDependent || EndDependent) { |
| 2995 | // Nothing to compute. |
| 2996 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2997 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2998 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2999 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 3000 | |
Douglas Gregor | 0f9d400 | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 3001 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 3002 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3003 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 3004 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 3005 | Invalid = true; |
| 3006 | } else { |
| 3007 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3008 | D.getLBracketLoc(), |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 3009 | D.getEllipsisLoc(), |
| 3010 | D.getRBracketLoc())); |
| 3011 | InitExpressions.push_back(StartIndex); |
| 3012 | InitExpressions.push_back(EndIndex); |
| 3013 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3014 | } |
| 3015 | break; |
| 3016 | } |
| 3017 | } |
| 3018 | } |
| 3019 | |
| 3020 | if (Invalid || Init.isInvalid()) |
| 3021 | return ExprError(); |
| 3022 | |
| 3023 | // Clear out the expressions within the designation. |
| 3024 | Desig.ClearExprs(*this); |
| 3025 | |
| 3026 | DesignatedInitExpr *DIE |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 3027 | = DesignatedInitExpr::Create(Context, |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 3028 | Designators, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3029 | InitExpressions, Loc, GNUSyntax, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3030 | Init.getAs<Expr>()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3031 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3032 | if (!getLangOpts().C99) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3033 | Diag(DIE->getBeginLoc(), diag::ext_designated_init) |
| 3034 | << DIE->getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3035 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3036 | return DIE; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3037 | } |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 3038 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3039 | //===----------------------------------------------------------------------===// |
| 3040 | // Initialization entity |
| 3041 | //===----------------------------------------------------------------------===// |
| 3042 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3043 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 3044 | const InitializedEntity &Parent) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3045 | : Parent(&Parent), Index(Index) |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 3046 | { |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3047 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 3048 | Kind = EK_ArrayElement; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3049 | Type = AT->getElementType(); |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3050 | } else if (const VectorType *VT = Parent.getType()->getAs<VectorType>()) { |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3051 | Kind = EK_VectorElement; |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3052 | Type = VT->getElementType(); |
| 3053 | } else { |
| 3054 | const ComplexType *CT = Parent.getType()->getAs<ComplexType>(); |
| 3055 | assert(CT && "Unexpected type"); |
| 3056 | Kind = EK_ComplexElement; |
| 3057 | Type = CT->getElementType(); |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3058 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3059 | } |
| 3060 | |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 3061 | InitializedEntity |
| 3062 | InitializedEntity::InitializeBase(ASTContext &Context, |
| 3063 | const CXXBaseSpecifier *Base, |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 3064 | bool IsInheritedVirtualBase, |
| 3065 | const InitializedEntity *Parent) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3066 | InitializedEntity Result; |
| 3067 | Result.Kind = EK_Base; |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 3068 | Result.Parent = Parent; |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 3069 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 3070 | if (IsInheritedVirtualBase) |
| 3071 | Result.Base |= 0x01; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3072 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3073 | Result.Type = Base->getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3074 | return Result; |
| 3075 | } |
| 3076 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3077 | DeclarationName InitializedEntity::getName() const { |
| 3078 | switch (getKind()) { |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3079 | case EK_Parameter: |
| 3080 | case EK_Parameter_CF_Audited: { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3081 | ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 3082 | return (D ? D->getDeclName() : DeclarationName()); |
| 3083 | } |
Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3084 | |
| 3085 | case EK_Variable: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3086 | case EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3087 | case EK_Binding: |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3088 | return Variable.VariableOrMember->getDeclName(); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3089 | |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 3090 | case EK_LambdaCapture: |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 3091 | return DeclarationName(Capture.VarID); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3092 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3093 | case EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 3094 | case EK_StmtExprResult: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3095 | case EK_Exception: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3096 | case EK_New: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3097 | case EK_Temporary: |
| 3098 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3099 | case EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3100 | case EK_ArrayElement: |
| 3101 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3102 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3103 | case EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3104 | case EK_LambdaToBlockConversionBlockElement: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 3105 | case EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 3106 | case EK_RelatedResult: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3107 | return DeclarationName(); |
| 3108 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3109 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3110 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3111 | } |
| 3112 | |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3113 | ValueDecl *InitializedEntity::getDecl() const { |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3114 | switch (getKind()) { |
| 3115 | case EK_Variable: |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3116 | case EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3117 | case EK_Binding: |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3118 | return Variable.VariableOrMember; |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3119 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3120 | case EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3121 | case EK_Parameter_CF_Audited: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3122 | return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 3123 | |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3124 | case EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 3125 | case EK_StmtExprResult: |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3126 | case EK_Exception: |
| 3127 | case EK_New: |
| 3128 | case EK_Temporary: |
| 3129 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3130 | case EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3131 | case EK_ArrayElement: |
| 3132 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3133 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3134 | case EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3135 | case EK_LambdaToBlockConversionBlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 3136 | case EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 3137 | case EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 3138 | case EK_RelatedResult: |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3139 | return nullptr; |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3140 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3141 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3142 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3143 | } |
| 3144 | |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3145 | bool InitializedEntity::allowsNRVO() const { |
| 3146 | switch (getKind()) { |
| 3147 | case EK_Result: |
| 3148 | case EK_Exception: |
| 3149 | return LocAndNRVO.NRVO; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3150 | |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 3151 | case EK_StmtExprResult: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3152 | case EK_Variable: |
| 3153 | case EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3154 | case EK_Parameter_CF_Audited: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3155 | case EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3156 | case EK_Binding: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3157 | case EK_New: |
| 3158 | case EK_Temporary: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 3159 | case EK_CompoundLiteralInit: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3160 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3161 | case EK_Delegating: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3162 | case EK_ArrayElement: |
| 3163 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3164 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3165 | case EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3166 | case EK_LambdaToBlockConversionBlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 3167 | case EK_LambdaCapture: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 3168 | case EK_RelatedResult: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3169 | break; |
| 3170 | } |
| 3171 | |
| 3172 | return false; |
| 3173 | } |
| 3174 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3175 | unsigned InitializedEntity::dumpImpl(raw_ostream &OS) const { |
Richard Smith | e3b28bc | 2013-06-12 21:51:50 +0000 | [diff] [blame] | 3176 | assert(getParent() != this); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3177 | unsigned Depth = getParent() ? getParent()->dumpImpl(OS) : 0; |
| 3178 | for (unsigned I = 0; I != Depth; ++I) |
| 3179 | OS << "`-"; |
| 3180 | |
| 3181 | switch (getKind()) { |
| 3182 | case EK_Variable: OS << "Variable"; break; |
| 3183 | case EK_Parameter: OS << "Parameter"; break; |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3184 | case EK_Parameter_CF_Audited: OS << "CF audited function Parameter"; |
| 3185 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3186 | case EK_Result: OS << "Result"; break; |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 3187 | case EK_StmtExprResult: OS << "StmtExprResult"; break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3188 | case EK_Exception: OS << "Exception"; break; |
| 3189 | case EK_Member: OS << "Member"; break; |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3190 | case EK_Binding: OS << "Binding"; break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3191 | case EK_New: OS << "New"; break; |
| 3192 | case EK_Temporary: OS << "Temporary"; break; |
| 3193 | case EK_CompoundLiteralInit: OS << "CompoundLiteral";break; |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 3194 | case EK_RelatedResult: OS << "RelatedResult"; break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3195 | case EK_Base: OS << "Base"; break; |
| 3196 | case EK_Delegating: OS << "Delegating"; break; |
| 3197 | case EK_ArrayElement: OS << "ArrayElement " << Index; break; |
| 3198 | case EK_VectorElement: OS << "VectorElement " << Index; break; |
| 3199 | case EK_ComplexElement: OS << "ComplexElement " << Index; break; |
| 3200 | case EK_BlockElement: OS << "Block"; break; |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3201 | case EK_LambdaToBlockConversionBlockElement: |
| 3202 | OS << "Block (lambda)"; |
| 3203 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3204 | case EK_LambdaCapture: |
| 3205 | OS << "LambdaCapture "; |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 3206 | OS << DeclarationName(Capture.VarID); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3207 | break; |
| 3208 | } |
| 3209 | |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3210 | if (auto *D = getDecl()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3211 | OS << " "; |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3212 | D->printQualifiedName(OS); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3213 | } |
| 3214 | |
| 3215 | OS << " '" << getType().getAsString() << "'\n"; |
| 3216 | |
| 3217 | return Depth + 1; |
| 3218 | } |
| 3219 | |
Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 3220 | LLVM_DUMP_METHOD void InitializedEntity::dump() const { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3221 | dumpImpl(llvm::errs()); |
| 3222 | } |
| 3223 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3224 | //===----------------------------------------------------------------------===// |
| 3225 | // Initialization sequence |
| 3226 | //===----------------------------------------------------------------------===// |
| 3227 | |
| 3228 | void InitializationSequence::Step::Destroy() { |
| 3229 | switch (Kind) { |
| 3230 | case SK_ResolveAddressOfOverloadedFunction: |
| 3231 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3232 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3233 | case SK_CastDerivedToBaseLValue: |
| 3234 | case SK_BindReference: |
| 3235 | case SK_BindReferenceToTemporary: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 3236 | case SK_FinalCopy: |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3237 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3238 | case SK_UserConversion: |
| 3239 | case SK_QualificationConversionRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3240 | case SK_QualificationConversionXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3241 | case SK_QualificationConversionLValue: |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 3242 | case SK_AtomicConversion: |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3243 | case SK_LValueToRValue: |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3244 | case SK_ListInitialization: |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3245 | case SK_UnwrapInitList: |
| 3246 | case SK_RewrapInitList: |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3247 | case SK_ConstructorInitialization: |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 3248 | case SK_ConstructorInitializationFromList: |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3249 | case SK_ZeroInitialization: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3250 | case SK_CAssignment: |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3251 | case SK_StringInit: |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3252 | case SK_ObjCObjectConversion: |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3253 | case SK_ArrayLoopIndex: |
| 3254 | case SK_ArrayLoopInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3255 | case SK_ArrayInit: |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 3256 | case SK_GNUArrayInit: |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 3257 | case SK_ParenthesizedArrayInit: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3258 | case SK_PassByIndirectCopyRestore: |
| 3259 | case SK_PassByIndirectRestore: |
| 3260 | case SK_ProduceObjCObject: |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 3261 | case SK_StdInitializerList: |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3262 | case SK_StdInitializerListConstructorCall: |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 3263 | case SK_OCLSamplerInit: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 3264 | case SK_OCLZeroEvent: |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 3265 | case SK_OCLZeroQueue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3266 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3267 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3268 | case SK_ConversionSequence: |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 3269 | case SK_ConversionSequenceNoNarrowing: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3270 | delete ICS; |
| 3271 | } |
| 3272 | } |
| 3273 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3274 | bool InitializationSequence::isDirectReferenceBinding() const { |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 3275 | // There can be some lvalue adjustments after the SK_BindReference step. |
| 3276 | for (auto I = Steps.rbegin(); I != Steps.rend(); ++I) { |
| 3277 | if (I->Kind == SK_BindReference) |
| 3278 | return true; |
| 3279 | if (I->Kind == SK_BindReferenceToTemporary) |
| 3280 | return false; |
| 3281 | } |
| 3282 | return false; |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3283 | } |
| 3284 | |
| 3285 | bool InitializationSequence::isAmbiguous() const { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 3286 | if (!Failed()) |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3287 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3288 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3289 | switch (getFailureKind()) { |
| 3290 | case FK_TooManyInitsForReference: |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 3291 | case FK_ParenthesizedListInitForReference: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3292 | case FK_ArrayNeedsInitList: |
| 3293 | case FK_ArrayNeedsInitListOrStringLiteral: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 3294 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 3295 | case FK_NarrowStringIntoWideCharArray: |
| 3296 | case FK_WideStringIntoCharArray: |
| 3297 | case FK_IncompatWideStringIntoWideChar: |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 3298 | case FK_PlainStringIntoUTF8Char: |
| 3299 | case FK_UTF8StringIntoPlainChar: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3300 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 3301 | case FK_NonConstLValueReferenceBindingToTemporary: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 3302 | case FK_NonConstLValueReferenceBindingToBitfield: |
| 3303 | case FK_NonConstLValueReferenceBindingToVectorElement: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3304 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 3305 | case FK_RValueReferenceBindingToLValue: |
| 3306 | case FK_ReferenceInitDropsQualifiers: |
| 3307 | case FK_ReferenceInitFailed: |
| 3308 | case FK_ConversionFailed: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3309 | case FK_ConversionFromPropertyFailed: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3310 | case FK_TooManyInitsForScalar: |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 3311 | case FK_ParenthesizedListInitForScalar: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3312 | case FK_ReferenceBindingToInitList: |
| 3313 | case FK_InitListBadDestinationType: |
| 3314 | case FK_DefaultInitOfConst: |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 3315 | case FK_Incomplete: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3316 | case FK_ArrayTypeMismatch: |
| 3317 | case FK_NonConstantArrayInit: |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 3318 | case FK_ListInitializationFailed: |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 3319 | case FK_VariableLengthArrayHasInitializer: |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3320 | case FK_PlaceholderType: |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3321 | case FK_ExplicitConstructor: |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 3322 | case FK_AddressOfUnaddressableFunction: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3323 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3324 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3325 | case FK_ReferenceInitOverloadFailed: |
| 3326 | case FK_UserConversionOverloadFailed: |
| 3327 | case FK_ConstructorOverloadFailed: |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3328 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3329 | return FailedOverloadResult == OR_Ambiguous; |
| 3330 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3331 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3332 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3333 | } |
| 3334 | |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 3335 | bool InitializationSequence::isConstructorInitialization() const { |
| 3336 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 3337 | } |
| 3338 | |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3339 | void |
| 3340 | InitializationSequence |
| 3341 | ::AddAddressOverloadResolutionStep(FunctionDecl *Function, |
| 3342 | DeclAccessPair Found, |
| 3343 | bool HadMultipleCandidates) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3344 | Step S; |
| 3345 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 3346 | S.Type = Function->getType(); |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3347 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3348 | S.Function.Function = Function; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3349 | S.Function.FoundDecl = Found; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3350 | Steps.push_back(S); |
| 3351 | } |
| 3352 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3353 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3354 | ExprValueKind VK) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3355 | Step S; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3356 | switch (VK) { |
| 3357 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 3358 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 3359 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3360 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3361 | S.Type = BaseType; |
| 3362 | Steps.push_back(S); |
| 3363 | } |
| 3364 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3365 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3366 | bool BindingTemporary) { |
| 3367 | Step S; |
| 3368 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 3369 | S.Type = T; |
| 3370 | Steps.push_back(S); |
| 3371 | } |
| 3372 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 3373 | void InitializationSequence::AddFinalCopy(QualType T) { |
| 3374 | Step S; |
| 3375 | S.Kind = SK_FinalCopy; |
| 3376 | S.Type = T; |
| 3377 | Steps.push_back(S); |
| 3378 | } |
| 3379 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3380 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 3381 | Step S; |
| 3382 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 3383 | S.Type = T; |
| 3384 | Steps.push_back(S); |
| 3385 | } |
| 3386 | |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3387 | void |
| 3388 | InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
| 3389 | DeclAccessPair FoundDecl, |
| 3390 | QualType T, |
| 3391 | bool HadMultipleCandidates) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3392 | Step S; |
| 3393 | S.Kind = SK_UserConversion; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3394 | S.Type = T; |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3395 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3396 | S.Function.Function = Function; |
| 3397 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3398 | Steps.push_back(S); |
| 3399 | } |
| 3400 | |
| 3401 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3402 | ExprValueKind VK) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3403 | Step S; |
John McCall | 7a1da89 | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 3404 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3405 | switch (VK) { |
| 3406 | case VK_RValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3407 | S.Kind = SK_QualificationConversionRValue; |
| 3408 | break; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3409 | case VK_XValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3410 | S.Kind = SK_QualificationConversionXValue; |
| 3411 | break; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3412 | case VK_LValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3413 | S.Kind = SK_QualificationConversionLValue; |
| 3414 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3415 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3416 | S.Type = Ty; |
| 3417 | Steps.push_back(S); |
| 3418 | } |
| 3419 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 3420 | void InitializationSequence::AddAtomicConversionStep(QualType Ty) { |
| 3421 | Step S; |
| 3422 | S.Kind = SK_AtomicConversion; |
| 3423 | S.Type = Ty; |
| 3424 | Steps.push_back(S); |
| 3425 | } |
| 3426 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3427 | void InitializationSequence::AddLValueToRValueStep(QualType Ty) { |
| 3428 | assert(!Ty.hasQualifiers() && "rvalues may not have qualifiers"); |
| 3429 | |
| 3430 | Step S; |
| 3431 | S.Kind = SK_LValueToRValue; |
| 3432 | S.Type = Ty; |
| 3433 | Steps.push_back(S); |
| 3434 | } |
| 3435 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3436 | void InitializationSequence::AddConversionSequenceStep( |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 3437 | const ImplicitConversionSequence &ICS, QualType T, |
| 3438 | bool TopLevelOfInitList) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3439 | Step S; |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 3440 | S.Kind = TopLevelOfInitList ? SK_ConversionSequenceNoNarrowing |
| 3441 | : SK_ConversionSequence; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3442 | S.Type = T; |
| 3443 | S.ICS = new ImplicitConversionSequence(ICS); |
| 3444 | Steps.push_back(S); |
| 3445 | } |
| 3446 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3447 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 3448 | Step S; |
| 3449 | S.Kind = SK_ListInitialization; |
| 3450 | S.Type = T; |
| 3451 | Steps.push_back(S); |
| 3452 | } |
| 3453 | |
Richard Smith | 55c2888 | 2016-05-12 23:45:49 +0000 | [diff] [blame] | 3454 | void InitializationSequence::AddConstructorInitializationStep( |
| 3455 | DeclAccessPair FoundDecl, CXXConstructorDecl *Constructor, QualType T, |
| 3456 | bool HadMultipleCandidates, bool FromInitList, bool AsInitList) { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3457 | Step S; |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3458 | S.Kind = FromInitList ? AsInitList ? SK_StdInitializerListConstructorCall |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 3459 | : SK_ConstructorInitializationFromList |
| 3460 | : SK_ConstructorInitialization; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3461 | S.Type = T; |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3462 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3463 | S.Function.Function = Constructor; |
Richard Smith | 55c2888 | 2016-05-12 23:45:49 +0000 | [diff] [blame] | 3464 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3465 | Steps.push_back(S); |
| 3466 | } |
| 3467 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3468 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 3469 | Step S; |
| 3470 | S.Kind = SK_ZeroInitialization; |
| 3471 | S.Type = T; |
| 3472 | Steps.push_back(S); |
| 3473 | } |
| 3474 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3475 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 3476 | Step S; |
| 3477 | S.Kind = SK_CAssignment; |
| 3478 | S.Type = T; |
| 3479 | Steps.push_back(S); |
| 3480 | } |
| 3481 | |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3482 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 3483 | Step S; |
| 3484 | S.Kind = SK_StringInit; |
| 3485 | S.Type = T; |
| 3486 | Steps.push_back(S); |
| 3487 | } |
| 3488 | |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3489 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 3490 | Step S; |
| 3491 | S.Kind = SK_ObjCObjectConversion; |
| 3492 | S.Type = T; |
| 3493 | Steps.push_back(S); |
| 3494 | } |
| 3495 | |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 3496 | void InitializationSequence::AddArrayInitStep(QualType T, bool IsGNUExtension) { |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3497 | Step S; |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 3498 | S.Kind = IsGNUExtension ? SK_GNUArrayInit : SK_ArrayInit; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3499 | S.Type = T; |
| 3500 | Steps.push_back(S); |
| 3501 | } |
| 3502 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3503 | void InitializationSequence::AddArrayInitLoopStep(QualType T, QualType EltT) { |
| 3504 | Step S; |
| 3505 | S.Kind = SK_ArrayLoopIndex; |
| 3506 | S.Type = EltT; |
| 3507 | Steps.insert(Steps.begin(), S); |
| 3508 | |
| 3509 | S.Kind = SK_ArrayLoopInit; |
| 3510 | S.Type = T; |
| 3511 | Steps.push_back(S); |
| 3512 | } |
| 3513 | |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 3514 | void InitializationSequence::AddParenthesizedArrayInitStep(QualType T) { |
| 3515 | Step S; |
| 3516 | S.Kind = SK_ParenthesizedArrayInit; |
| 3517 | S.Type = T; |
| 3518 | Steps.push_back(S); |
| 3519 | } |
| 3520 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3521 | void InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type, |
| 3522 | bool shouldCopy) { |
| 3523 | Step s; |
| 3524 | s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore |
| 3525 | : SK_PassByIndirectRestore); |
| 3526 | s.Type = type; |
| 3527 | Steps.push_back(s); |
| 3528 | } |
| 3529 | |
| 3530 | void InitializationSequence::AddProduceObjCObjectStep(QualType T) { |
| 3531 | Step S; |
| 3532 | S.Kind = SK_ProduceObjCObject; |
| 3533 | S.Type = T; |
| 3534 | Steps.push_back(S); |
| 3535 | } |
| 3536 | |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 3537 | void InitializationSequence::AddStdInitializerListConstructionStep(QualType T) { |
| 3538 | Step S; |
| 3539 | S.Kind = SK_StdInitializerList; |
| 3540 | S.Type = T; |
| 3541 | Steps.push_back(S); |
| 3542 | } |
| 3543 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 3544 | void InitializationSequence::AddOCLSamplerInitStep(QualType T) { |
| 3545 | Step S; |
| 3546 | S.Kind = SK_OCLSamplerInit; |
| 3547 | S.Type = T; |
| 3548 | Steps.push_back(S); |
| 3549 | } |
| 3550 | |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 3551 | void InitializationSequence::AddOCLZeroEventStep(QualType T) { |
| 3552 | Step S; |
| 3553 | S.Kind = SK_OCLZeroEvent; |
| 3554 | S.Type = T; |
| 3555 | Steps.push_back(S); |
| 3556 | } |
| 3557 | |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 3558 | void InitializationSequence::AddOCLZeroQueueStep(QualType T) { |
| 3559 | Step S; |
| 3560 | S.Kind = SK_OCLZeroQueue; |
| 3561 | S.Type = T; |
| 3562 | Steps.push_back(S); |
| 3563 | } |
| 3564 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3565 | void InitializationSequence::RewrapReferenceInitList(QualType T, |
| 3566 | InitListExpr *Syntactic) { |
| 3567 | assert(Syntactic->getNumInits() == 1 && |
| 3568 | "Can only rewrap trivial init lists."); |
| 3569 | Step S; |
| 3570 | S.Kind = SK_UnwrapInitList; |
| 3571 | S.Type = Syntactic->getInit(0)->getType(); |
| 3572 | Steps.insert(Steps.begin(), S); |
| 3573 | |
| 3574 | S.Kind = SK_RewrapInitList; |
| 3575 | S.Type = T; |
| 3576 | S.WrappingSyntacticList = Syntactic; |
| 3577 | Steps.push_back(S); |
| 3578 | } |
| 3579 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3580 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3581 | OverloadingResult Result) { |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 3582 | setSequenceKind(FailedSequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3583 | this->Failure = Failure; |
| 3584 | this->FailedOverloadResult = Result; |
| 3585 | } |
| 3586 | |
| 3587 | //===----------------------------------------------------------------------===// |
| 3588 | // Attempt initialization |
| 3589 | //===----------------------------------------------------------------------===// |
| 3590 | |
Nico Weber | 337d5aa | 2015-04-17 08:32:38 +0000 | [diff] [blame] | 3591 | /// Tries to add a zero initializer. Returns true if that worked. |
| 3592 | static bool |
| 3593 | maybeRecoverWithZeroInitialization(Sema &S, InitializationSequence &Sequence, |
| 3594 | const InitializedEntity &Entity) { |
| 3595 | if (Entity.getKind() != InitializedEntity::EK_Variable) |
| 3596 | return false; |
| 3597 | |
| 3598 | VarDecl *VD = cast<VarDecl>(Entity.getDecl()); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 3599 | if (VD->getInit() || VD->getEndLoc().isMacroID()) |
Nico Weber | 337d5aa | 2015-04-17 08:32:38 +0000 | [diff] [blame] | 3600 | return false; |
| 3601 | |
| 3602 | QualType VariableTy = VD->getType().getCanonicalType(); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 3603 | SourceLocation Loc = S.getLocForEndOfToken(VD->getEndLoc()); |
Nico Weber | 337d5aa | 2015-04-17 08:32:38 +0000 | [diff] [blame] | 3604 | std::string Init = S.getFixItZeroInitializerForType(VariableTy, Loc); |
| 3605 | if (!Init.empty()) { |
| 3606 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3607 | Sequence.SetZeroInitializationFixit(Init, Loc); |
| 3608 | return true; |
| 3609 | } |
| 3610 | return false; |
| 3611 | } |
| 3612 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3613 | static void MaybeProduceObjCObject(Sema &S, |
| 3614 | InitializationSequence &Sequence, |
| 3615 | const InitializedEntity &Entity) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3616 | if (!S.getLangOpts().ObjCAutoRefCount) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3617 | |
| 3618 | /// When initializing a parameter, produce the value if it's marked |
| 3619 | /// __attribute__((ns_consumed)). |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3620 | if (Entity.isParameterKind()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3621 | if (!Entity.isParameterConsumed()) |
| 3622 | return; |
| 3623 | |
| 3624 | assert(Entity.getType()->isObjCRetainableType() && |
| 3625 | "consuming an object of unretainable type?"); |
| 3626 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 3627 | |
| 3628 | /// When initializing a return value, if the return type is a |
| 3629 | /// retainable type, then returns need to immediately retain the |
| 3630 | /// object. If an autorelease is required, it will be done at the |
| 3631 | /// last instant. |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 3632 | } else if (Entity.getKind() == InitializedEntity::EK_Result || |
| 3633 | Entity.getKind() == InitializedEntity::EK_StmtExprResult) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3634 | if (!Entity.getType()->isObjCRetainableType()) |
| 3635 | return; |
| 3636 | |
| 3637 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 3638 | } |
| 3639 | } |
| 3640 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3641 | static void TryListInitialization(Sema &S, |
| 3642 | const InitializedEntity &Entity, |
| 3643 | const InitializationKind &Kind, |
| 3644 | InitListExpr *InitList, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 3645 | InitializationSequence &Sequence, |
| 3646 | bool TreatUnavailableAsInvalid); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3647 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3648 | /// When initializing from init list via constructor, handle |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3649 | /// initialization of an object of type std::initializer_list<T>. |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3650 | /// |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3651 | /// \return true if we have handled initialization of an object of type |
| 3652 | /// std::initializer_list<T>, false otherwise. |
| 3653 | static bool TryInitializerListConstruction(Sema &S, |
| 3654 | InitListExpr *List, |
| 3655 | QualType DestType, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 3656 | InitializationSequence &Sequence, |
| 3657 | bool TreatUnavailableAsInvalid) { |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3658 | QualType E; |
| 3659 | if (!S.isStdInitializerList(DestType, &E)) |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3660 | return false; |
| 3661 | |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3662 | if (!S.isCompleteType(List->getExprLoc(), E)) { |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3663 | Sequence.setIncompleteTypeFailure(E); |
| 3664 | return true; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3665 | } |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3666 | |
| 3667 | // Try initializing a temporary array from the init list. |
| 3668 | QualType ArrayType = S.Context.getConstantArrayType( |
| 3669 | E.withConst(), llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 3670 | List->getNumInits()), |
| 3671 | clang::ArrayType::Normal, 0); |
| 3672 | InitializedEntity HiddenArray = |
| 3673 | InitializedEntity::InitializeTemporary(ArrayType); |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 3674 | InitializationKind Kind = InitializationKind::CreateDirectList( |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 3675 | List->getExprLoc(), List->getBeginLoc(), List->getEndLoc()); |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 3676 | TryListInitialization(S, HiddenArray, Kind, List, Sequence, |
| 3677 | TreatUnavailableAsInvalid); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3678 | if (Sequence) |
| 3679 | Sequence.AddStdInitializerListConstructionStep(DestType); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3680 | return true; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3681 | } |
| 3682 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3683 | /// Determine if the constructor has the signature of a copy or move |
| 3684 | /// constructor for the type T of the class in which it was found. That is, |
| 3685 | /// determine if its first parameter is of type T or reference to (possibly |
| 3686 | /// cv-qualified) T. |
| 3687 | static bool hasCopyOrMoveCtorParam(ASTContext &Ctx, |
| 3688 | const ConstructorInfo &Info) { |
| 3689 | if (Info.Constructor->getNumParams() == 0) |
| 3690 | return false; |
| 3691 | |
| 3692 | QualType ParmT = |
| 3693 | Info.Constructor->getParamDecl(0)->getType().getNonReferenceType(); |
| 3694 | QualType ClassT = |
| 3695 | Ctx.getRecordType(cast<CXXRecordDecl>(Info.FoundDecl->getDeclContext())); |
| 3696 | |
| 3697 | return Ctx.hasSameUnqualifiedType(ParmT, ClassT); |
| 3698 | } |
| 3699 | |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3700 | static OverloadingResult |
| 3701 | ResolveConstructorOverload(Sema &S, SourceLocation DeclLoc, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3702 | MultiExprArg Args, |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3703 | OverloadCandidateSet &CandidateSet, |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3704 | QualType DestType, |
Richard Smith | 40c7806 | 2015-02-21 02:31:57 +0000 | [diff] [blame] | 3705 | DeclContext::lookup_result Ctors, |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3706 | OverloadCandidateSet::iterator &Best, |
| 3707 | bool CopyInitializing, bool AllowExplicit, |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3708 | bool OnlyListConstructors, bool IsListInit, |
| 3709 | bool SecondStepOfCopyInit = false) { |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3710 | CandidateSet.clear(OverloadCandidateSet::CSK_InitByConstructor); |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3711 | |
Richard Smith | 40c7806 | 2015-02-21 02:31:57 +0000 | [diff] [blame] | 3712 | for (NamedDecl *D : Ctors) { |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3713 | auto Info = getConstructorInfo(D); |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3714 | if (!Info.Constructor || Info.Constructor->isInvalidDecl()) |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3715 | continue; |
| 3716 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3717 | if (!AllowExplicit && Info.Constructor->isExplicit()) |
| 3718 | continue; |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3719 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3720 | if (OnlyListConstructors && !S.isInitListConstructor(Info.Constructor)) |
| 3721 | continue; |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3722 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3723 | // C++11 [over.best.ics]p4: |
| 3724 | // ... and the constructor or user-defined conversion function is a |
| 3725 | // candidate by |
| 3726 | // - 13.3.1.3, when the argument is the temporary in the second step |
| 3727 | // of a class copy-initialization, or |
| 3728 | // - 13.3.1.4, 13.3.1.5, or 13.3.1.6 (in all cases), [not handled here] |
| 3729 | // - the second phase of 13.3.1.7 when the initializer list has exactly |
| 3730 | // one element that is itself an initializer list, and the target is |
| 3731 | // the first parameter of a constructor of class X, and the conversion |
| 3732 | // is to X or reference to (possibly cv-qualified X), |
| 3733 | // user-defined conversion sequences are not considered. |
| 3734 | bool SuppressUserConversions = |
| 3735 | SecondStepOfCopyInit || |
| 3736 | (IsListInit && Args.size() == 1 && isa<InitListExpr>(Args[0]) && |
| 3737 | hasCopyOrMoveCtorParam(S.Context, Info)); |
| 3738 | |
| 3739 | if (Info.ConstructorTmpl) |
| 3740 | S.AddTemplateOverloadCandidate(Info.ConstructorTmpl, Info.FoundDecl, |
| 3741 | /*ExplicitArgs*/ nullptr, Args, |
| 3742 | CandidateSet, SuppressUserConversions); |
| 3743 | else { |
| 3744 | // C++ [over.match.copy]p1: |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3745 | // - When initializing a temporary to be bound to the first parameter |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3746 | // of a constructor [for type T] that takes a reference to possibly |
| 3747 | // cv-qualified T as its first argument, called with a single |
| 3748 | // argument in the context of direct-initialization, explicit |
| 3749 | // conversion functions are also considered. |
| 3750 | // FIXME: What if a constructor template instantiates to such a signature? |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3751 | bool AllowExplicitConv = AllowExplicit && !CopyInitializing && |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3752 | Args.size() == 1 && |
| 3753 | hasCopyOrMoveCtorParam(S.Context, Info); |
| 3754 | S.AddOverloadCandidate(Info.Constructor, Info.FoundDecl, Args, |
| 3755 | CandidateSet, SuppressUserConversions, |
| 3756 | /*PartialOverloading=*/false, |
| 3757 | /*AllowExplicit=*/AllowExplicitConv); |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3758 | } |
| 3759 | } |
| 3760 | |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3761 | // FIXME: Work around a bug in C++17 guaranteed copy elision. |
| 3762 | // |
| 3763 | // When initializing an object of class type T by constructor |
| 3764 | // ([over.match.ctor]) or by list-initialization ([over.match.list]) |
| 3765 | // from a single expression of class type U, conversion functions of |
| 3766 | // U that convert to the non-reference type cv T are candidates. |
| 3767 | // Explicit conversion functions are only candidates during |
| 3768 | // direct-initialization. |
| 3769 | // |
| 3770 | // Note: SecondStepOfCopyInit is only ever true in this case when |
| 3771 | // evaluating whether to produce a C++98 compatibility warning. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 3772 | if (S.getLangOpts().CPlusPlus17 && Args.size() == 1 && |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3773 | !SecondStepOfCopyInit) { |
| 3774 | Expr *Initializer = Args[0]; |
| 3775 | auto *SourceRD = Initializer->getType()->getAsCXXRecordDecl(); |
| 3776 | if (SourceRD && S.isCompleteType(DeclLoc, Initializer->getType())) { |
| 3777 | const auto &Conversions = SourceRD->getVisibleConversionFunctions(); |
| 3778 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
| 3779 | NamedDecl *D = *I; |
| 3780 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3781 | D = D->getUnderlyingDecl(); |
| 3782 | |
| 3783 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3784 | CXXConversionDecl *Conv; |
| 3785 | if (ConvTemplate) |
| 3786 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3787 | else |
| 3788 | Conv = cast<CXXConversionDecl>(D); |
| 3789 | |
| 3790 | if ((AllowExplicit && !CopyInitializing) || !Conv->isExplicit()) { |
| 3791 | if (ConvTemplate) |
| 3792 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
| 3793 | ActingDC, Initializer, DestType, |
| 3794 | CandidateSet, AllowExplicit, |
| 3795 | /*AllowResultConversion*/false); |
| 3796 | else |
| 3797 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Initializer, |
| 3798 | DestType, CandidateSet, AllowExplicit, |
| 3799 | /*AllowResultConversion*/false); |
| 3800 | } |
| 3801 | } |
| 3802 | } |
| 3803 | } |
| 3804 | |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3805 | // Perform overload resolution and return the result. |
| 3806 | return CandidateSet.BestViableFunction(S, DeclLoc, Best); |
| 3807 | } |
| 3808 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3809 | /// Attempt initialization by constructor (C++ [dcl.init]), which |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3810 | /// enumerates the constructors of the initialized entity and performs overload |
| 3811 | /// resolution to select the best. |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3812 | /// \param DestType The destination class type. |
| 3813 | /// \param DestArrayType The destination type, which is either DestType or |
| 3814 | /// a (possibly multidimensional) array of DestType. |
NAKAMURA Takumi | ffcc98a | 2015-02-05 23:12:13 +0000 | [diff] [blame] | 3815 | /// \param IsListInit Is this list-initialization? |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3816 | /// \param IsInitListCopy Is this non-list-initialization resulting from a |
| 3817 | /// list-initialization from {x} where x is the same |
| 3818 | /// type as the entity? |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3819 | static void TryConstructorInitialization(Sema &S, |
| 3820 | const InitializedEntity &Entity, |
| 3821 | const InitializationKind &Kind, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3822 | MultiExprArg Args, QualType DestType, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3823 | QualType DestArrayType, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3824 | InitializationSequence &Sequence, |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3825 | bool IsListInit = false, |
| 3826 | bool IsInitListCopy = false) { |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3827 | assert(((!IsListInit && !IsInitListCopy) || |
| 3828 | (Args.size() == 1 && isa<InitListExpr>(Args[0]))) && |
| 3829 | "IsListInit/IsInitListCopy must come with a single initializer list " |
| 3830 | "argument."); |
| 3831 | InitListExpr *ILE = |
| 3832 | (IsListInit || IsInitListCopy) ? cast<InitListExpr>(Args[0]) : nullptr; |
| 3833 | MultiExprArg UnwrappedArgs = |
| 3834 | ILE ? MultiExprArg(ILE->getInits(), ILE->getNumInits()) : Args; |
Sebastian Redl | 88e4d49 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3835 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3836 | // The type we're constructing needs to be complete. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3837 | if (!S.isCompleteType(Kind.getLocation(), DestType)) { |
Douglas Gregor | 85f3423 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 3838 | Sequence.setIncompleteTypeFailure(DestType); |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3839 | return; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3840 | } |
| 3841 | |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 3842 | // C++17 [dcl.init]p17: |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3843 | // - If the initializer expression is a prvalue and the cv-unqualified |
| 3844 | // version of the source type is the same class as the class of the |
| 3845 | // destination, the initializer expression is used to initialize the |
| 3846 | // destination object. |
| 3847 | // Per DR (no number yet), this does not apply when initializing a base |
| 3848 | // class or delegating to another constructor from a mem-initializer. |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3849 | // ObjC++: Lambda captured by the block in the lambda to block conversion |
| 3850 | // should avoid copy elision. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 3851 | if (S.getLangOpts().CPlusPlus17 && |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3852 | Entity.getKind() != InitializedEntity::EK_Base && |
| 3853 | Entity.getKind() != InitializedEntity::EK_Delegating && |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3854 | Entity.getKind() != |
| 3855 | InitializedEntity::EK_LambdaToBlockConversionBlockElement && |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3856 | UnwrappedArgs.size() == 1 && UnwrappedArgs[0]->isRValue() && |
| 3857 | S.Context.hasSameUnqualifiedType(UnwrappedArgs[0]->getType(), DestType)) { |
| 3858 | // Convert qualifications if necessary. |
Richard Smith | 16d3150 | 2016-12-21 01:31:56 +0000 | [diff] [blame] | 3859 | Sequence.AddQualificationConversionStep(DestType, VK_RValue); |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3860 | if (ILE) |
| 3861 | Sequence.RewrapReferenceInitList(DestType, ILE); |
| 3862 | return; |
| 3863 | } |
| 3864 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3865 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
| 3866 | assert(DestRecordType && "Constructor initialization requires record type"); |
| 3867 | CXXRecordDecl *DestRecordDecl |
| 3868 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
| 3869 | |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3870 | // Build the candidate set directly in the initialization sequence |
| 3871 | // structure, so that it will persist if we fail. |
| 3872 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3873 | |
| 3874 | // Determine whether we are allowed to call explicit constructors or |
| 3875 | // explicit conversion operators. |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3876 | bool AllowExplicit = Kind.AllowExplicit() || IsListInit; |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3877 | bool CopyInitialization = Kind.getKind() == InitializationKind::IK_Copy; |
Sebastian Redl | 88e4d49 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3878 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3879 | // - Otherwise, if T is a class type, constructors are considered. The |
| 3880 | // applicable constructors are enumerated, and the best one is chosen |
| 3881 | // through overload resolution. |
Richard Smith | 40c7806 | 2015-02-21 02:31:57 +0000 | [diff] [blame] | 3882 | DeclContext::lookup_result Ctors = S.LookupConstructors(DestRecordDecl); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3883 | |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3884 | OverloadingResult Result = OR_No_Viable_Function; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3885 | OverloadCandidateSet::iterator Best; |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3886 | bool AsInitializerList = false; |
| 3887 | |
Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 3888 | // C++11 [over.match.list]p1, per DR1467: |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 3889 | // When objects of non-aggregate type T are list-initialized, such that |
| 3890 | // 8.5.4 [dcl.init.list] specifies that overload resolution is performed |
| 3891 | // according to the rules in this section, overload resolution selects |
| 3892 | // the constructor in two phases: |
| 3893 | // |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3894 | // - Initially, the candidate functions are the initializer-list |
| 3895 | // constructors of the class T and the argument list consists of the |
| 3896 | // initializer list as a single argument. |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3897 | if (IsListInit) { |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3898 | AsInitializerList = true; |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3899 | |
| 3900 | // If the initializer list has no elements and T has a default constructor, |
| 3901 | // the first phase is omitted. |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3902 | if (!(UnwrappedArgs.empty() && DestRecordDecl->hasDefaultConstructor())) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3903 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3904 | CandidateSet, DestType, Ctors, Best, |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3905 | CopyInitialization, AllowExplicit, |
Larisse Voufo | bcf327a | 2015-02-10 02:20:14 +0000 | [diff] [blame] | 3906 | /*OnlyListConstructor=*/true, |
| 3907 | IsListInit); |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3908 | } |
| 3909 | |
| 3910 | // C++11 [over.match.list]p1: |
| 3911 | // - If no viable initializer-list constructor is found, overload resolution |
| 3912 | // is performed again, where the candidate functions are all the |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3913 | // constructors of the class T and the argument list consists of the |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3914 | // elements of the initializer list. |
| 3915 | if (Result == OR_No_Viable_Function) { |
| 3916 | AsInitializerList = false; |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3917 | Result = ResolveConstructorOverload(S, Kind.getLocation(), UnwrappedArgs, |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3918 | CandidateSet, DestType, Ctors, Best, |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3919 | CopyInitialization, AllowExplicit, |
Larisse Voufo | bcf327a | 2015-02-10 02:20:14 +0000 | [diff] [blame] | 3920 | /*OnlyListConstructors=*/false, |
| 3921 | IsListInit); |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3922 | } |
| 3923 | if (Result) { |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3924 | Sequence.SetOverloadFailure(IsListInit ? |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3925 | InitializationSequence::FK_ListConstructorOverloadFailed : |
| 3926 | InitializationSequence::FK_ConstructorOverloadFailed, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3927 | Result); |
| 3928 | return; |
| 3929 | } |
| 3930 | |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3931 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 3932 | |
| 3933 | // In C++17, ResolveConstructorOverload can select a conversion function |
| 3934 | // instead of a constructor. |
| 3935 | if (auto *CD = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 3936 | // Add the user-defined conversion step that calls the conversion function. |
| 3937 | QualType ConvType = CD->getConversionType(); |
| 3938 | assert(S.Context.hasSameUnqualifiedType(ConvType, DestType) && |
| 3939 | "should not have selected this conversion function"); |
| 3940 | Sequence.AddUserConversionStep(CD, Best->FoundDecl, ConvType, |
| 3941 | HadMultipleCandidates); |
| 3942 | if (!S.Context.hasSameType(ConvType, DestType)) |
| 3943 | Sequence.AddQualificationConversionStep(DestType, VK_RValue); |
| 3944 | if (IsListInit) |
| 3945 | Sequence.RewrapReferenceInitList(Entity.getType(), ILE); |
| 3946 | return; |
| 3947 | } |
| 3948 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3949 | // C++11 [dcl.init]p6: |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3950 | // If a program calls for the default initialization of an object |
| 3951 | // of a const-qualified type T, T shall be a class type with a |
| 3952 | // user-provided default constructor. |
Nico Weber | 6a6376b | 2016-02-19 01:52:46 +0000 | [diff] [blame] | 3953 | // C++ core issue 253 proposal: |
| 3954 | // If the implicit default constructor initializes all subobjects, no |
| 3955 | // initializer should be required. |
| 3956 | // The 253 proposal is for example needed to process libstdc++ headers in 5.x. |
| 3957 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3958 | if (Kind.getKind() == InitializationKind::IK_Default && |
Nico Weber | 6a6376b | 2016-02-19 01:52:46 +0000 | [diff] [blame] | 3959 | Entity.getType().isConstQualified()) { |
| 3960 | if (!CtorDecl->getParent()->allowConstDefaultInit()) { |
| 3961 | if (!maybeRecoverWithZeroInitialization(S, Sequence, Entity)) |
| 3962 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 3963 | return; |
| 3964 | } |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3965 | } |
| 3966 | |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3967 | // C++11 [over.match.list]p1: |
| 3968 | // In copy-list-initialization, if an explicit constructor is chosen, the |
| 3969 | // initializer is ill-formed. |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3970 | if (IsListInit && !Kind.AllowExplicit() && CtorDecl->isExplicit()) { |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3971 | Sequence.SetFailed(InitializationSequence::FK_ExplicitConstructor); |
| 3972 | return; |
| 3973 | } |
| 3974 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3975 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 3976 | // subsumed by the initialization. |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3977 | Sequence.AddConstructorInitializationStep( |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3978 | Best->FoundDecl, CtorDecl, DestArrayType, HadMultipleCandidates, |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3979 | IsListInit | IsInitListCopy, AsInitializerList); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3980 | } |
| 3981 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3982 | static bool |
| 3983 | ResolveOverloadedFunctionForReferenceBinding(Sema &S, |
| 3984 | Expr *Initializer, |
| 3985 | QualType &SourceType, |
| 3986 | QualType &UnqualifiedSourceType, |
| 3987 | QualType UnqualifiedTargetType, |
| 3988 | InitializationSequence &Sequence) { |
| 3989 | if (S.Context.getCanonicalType(UnqualifiedSourceType) == |
| 3990 | S.Context.OverloadTy) { |
| 3991 | DeclAccessPair Found; |
| 3992 | bool HadMultipleCandidates = false; |
| 3993 | if (FunctionDecl *Fn |
| 3994 | = S.ResolveAddressOfOverloadedFunction(Initializer, |
| 3995 | UnqualifiedTargetType, |
| 3996 | false, Found, |
| 3997 | &HadMultipleCandidates)) { |
| 3998 | Sequence.AddAddressOverloadResolutionStep(Fn, Found, |
| 3999 | HadMultipleCandidates); |
| 4000 | SourceType = Fn->getType(); |
| 4001 | UnqualifiedSourceType = SourceType.getUnqualifiedType(); |
| 4002 | } else if (!UnqualifiedTargetType->isRecordType()) { |
| 4003 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 4004 | return true; |
| 4005 | } |
| 4006 | } |
| 4007 | return false; |
| 4008 | } |
| 4009 | |
| 4010 | static void TryReferenceInitializationCore(Sema &S, |
| 4011 | const InitializedEntity &Entity, |
| 4012 | const InitializationKind &Kind, |
| 4013 | Expr *Initializer, |
| 4014 | QualType cv1T1, QualType T1, |
| 4015 | Qualifiers T1Quals, |
| 4016 | QualType cv2T2, QualType T2, |
| 4017 | Qualifiers T2Quals, |
| 4018 | InitializationSequence &Sequence); |
| 4019 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4020 | static void TryValueInitialization(Sema &S, |
| 4021 | const InitializedEntity &Entity, |
| 4022 | const InitializationKind &Kind, |
| 4023 | InitializationSequence &Sequence, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4024 | InitListExpr *InitList = nullptr); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4025 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4026 | /// Attempt list initialization of a reference. |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4027 | static void TryReferenceListInitialization(Sema &S, |
| 4028 | const InitializedEntity &Entity, |
| 4029 | const InitializationKind &Kind, |
| 4030 | InitListExpr *InitList, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4031 | InitializationSequence &Sequence, |
| 4032 | bool TreatUnavailableAsInvalid) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4033 | // First, catch C++03 where this isn't possible. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4034 | if (!S.getLangOpts().CPlusPlus11) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4035 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 4036 | return; |
| 4037 | } |
David Majnemer | 9370dc2 | 2015-04-26 07:35:03 +0000 | [diff] [blame] | 4038 | // Can't reference initialize a compound literal. |
| 4039 | if (Entity.getKind() == InitializedEntity::EK_CompoundLiteralInit) { |
| 4040 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 4041 | return; |
| 4042 | } |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4043 | |
| 4044 | QualType DestType = Entity.getType(); |
| 4045 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 4046 | Qualifiers T1Quals; |
| 4047 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
| 4048 | |
| 4049 | // Reference initialization via an initializer list works thus: |
| 4050 | // If the initializer list consists of a single element that is |
| 4051 | // reference-related to the referenced type, bind directly to that element |
| 4052 | // (possibly creating temporaries). |
| 4053 | // Otherwise, initialize a temporary with the initializer list and |
| 4054 | // bind to that. |
| 4055 | if (InitList->getNumInits() == 1) { |
| 4056 | Expr *Initializer = InitList->getInit(0); |
| 4057 | QualType cv2T2 = Initializer->getType(); |
| 4058 | Qualifiers T2Quals; |
| 4059 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
| 4060 | |
| 4061 | // If this fails, creating a temporary wouldn't work either. |
| 4062 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 4063 | T1, Sequence)) |
| 4064 | return; |
| 4065 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4066 | SourceLocation DeclLoc = Initializer->getBeginLoc(); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4067 | bool dummy1, dummy2, dummy3; |
| 4068 | Sema::ReferenceCompareResult RefRelationship |
| 4069 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, dummy1, |
| 4070 | dummy2, dummy3); |
| 4071 | if (RefRelationship >= Sema::Ref_Related) { |
| 4072 | // Try to bind the reference here. |
| 4073 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 4074 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 4075 | if (Sequence) |
| 4076 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
| 4077 | return; |
| 4078 | } |
Richard Smith | 03d9393 | 2013-01-15 07:58:29 +0000 | [diff] [blame] | 4079 | |
| 4080 | // Update the initializer if we've resolved an overloaded function. |
| 4081 | if (Sequence.step_begin() != Sequence.step_end()) |
| 4082 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4083 | } |
| 4084 | |
| 4085 | // Not reference-related. Create a temporary and bind to that. |
| 4086 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 4087 | |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4088 | TryListInitialization(S, TempEntity, Kind, InitList, Sequence, |
| 4089 | TreatUnavailableAsInvalid); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4090 | if (Sequence) { |
| 4091 | if (DestType->isRValueReferenceType() || |
| 4092 | (T1Quals.hasConst() && !T1Quals.hasVolatile())) |
| 4093 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 4094 | else |
| 4095 | Sequence.SetFailed( |
| 4096 | InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
| 4097 | } |
| 4098 | } |
| 4099 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4100 | /// Attempt list initialization (C++0x [dcl.init.list]) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4101 | static void TryListInitialization(Sema &S, |
| 4102 | const InitializedEntity &Entity, |
| 4103 | const InitializationKind &Kind, |
| 4104 | InitListExpr *InitList, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4105 | InitializationSequence &Sequence, |
| 4106 | bool TreatUnavailableAsInvalid) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4107 | QualType DestType = Entity.getType(); |
| 4108 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4109 | // C++ doesn't allow scalar initialization with more than one argument. |
| 4110 | // But C99 complex numbers are scalars and it makes sense there. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4111 | if (S.getLangOpts().CPlusPlus && DestType->isScalarType() && |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4112 | !DestType->isAnyComplexType() && InitList->getNumInits() > 1) { |
| 4113 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 4114 | return; |
| 4115 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4116 | if (DestType->isReferenceType()) { |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4117 | TryReferenceListInitialization(S, Entity, Kind, InitList, Sequence, |
| 4118 | TreatUnavailableAsInvalid); |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4119 | return; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4120 | } |
Sebastian Redl | 4f28b58 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 4121 | |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4122 | if (DestType->isRecordType() && |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4123 | !S.isCompleteType(InitList->getBeginLoc(), DestType)) { |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4124 | Sequence.setIncompleteTypeFailure(DestType); |
| 4125 | return; |
| 4126 | } |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4127 | |
Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 4128 | // C++11 [dcl.init.list]p3, per DR1467: |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4129 | // - If T is a class type and the initializer list has a single element of |
| 4130 | // type cv U, where U is T or a class derived from T, the object is |
| 4131 | // initialized from that element (by copy-initialization for |
| 4132 | // copy-list-initialization, or by direct-initialization for |
| 4133 | // direct-list-initialization). |
| 4134 | // - Otherwise, if T is a character array and the initializer list has a |
| 4135 | // single element that is an appropriately-typed string literal |
| 4136 | // (8.5.2 [dcl.init.string]), initialization is performed as described |
| 4137 | // in that section. |
Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 4138 | // - Otherwise, if T is an aggregate, [...] (continue below). |
| 4139 | if (S.getLangOpts().CPlusPlus11 && InitList->getNumInits() == 1) { |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4140 | if (DestType->isRecordType()) { |
| 4141 | QualType InitType = InitList->getInit(0)->getType(); |
| 4142 | if (S.Context.hasSameUnqualifiedType(InitType, DestType) || |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4143 | S.IsDerivedFrom(InitList->getBeginLoc(), InitType, DestType)) { |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 4144 | Expr *InitListAsExpr = InitList; |
| 4145 | TryConstructorInitialization(S, Entity, Kind, InitListAsExpr, DestType, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 4146 | DestType, Sequence, |
| 4147 | /*InitListSyntax*/false, |
| 4148 | /*IsInitListCopy*/true); |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4149 | return; |
| 4150 | } |
| 4151 | } |
| 4152 | if (const ArrayType *DestAT = S.Context.getAsArrayType(DestType)) { |
| 4153 | Expr *SubInit[1] = {InitList->getInit(0)}; |
| 4154 | if (!isa<VariableArrayType>(DestAT) && |
| 4155 | IsStringInit(SubInit[0], DestAT, S.Context) == SIF_None) { |
| 4156 | InitializationKind SubKind = |
| 4157 | Kind.getKind() == InitializationKind::IK_DirectList |
| 4158 | ? InitializationKind::CreateDirect(Kind.getLocation(), |
| 4159 | InitList->getLBraceLoc(), |
| 4160 | InitList->getRBraceLoc()) |
| 4161 | : Kind; |
| 4162 | Sequence.InitializeFrom(S, Entity, SubKind, SubInit, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4163 | /*TopLevelOfInitList*/ true, |
| 4164 | TreatUnavailableAsInvalid); |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4165 | |
| 4166 | // TryStringLiteralInitialization() (in InitializeFrom()) will fail if |
| 4167 | // the element is not an appropriately-typed string literal, in which |
| 4168 | // case we should proceed as in C++11 (below). |
| 4169 | if (Sequence) { |
| 4170 | Sequence.RewrapReferenceInitList(Entity.getType(), InitList); |
| 4171 | return; |
| 4172 | } |
| 4173 | } |
Sebastian Redl | 4f28b58 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 4174 | } |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4175 | } |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4176 | |
| 4177 | // C++11 [dcl.init.list]p3: |
| 4178 | // - If T is an aggregate, aggregate initialization is performed. |
Faisal Vali | 30622bb | 2015-12-07 02:37:44 +0000 | [diff] [blame] | 4179 | if ((DestType->isRecordType() && !DestType->isAggregateType()) || |
| 4180 | (S.getLangOpts().CPlusPlus11 && |
| 4181 | S.isStdInitializerList(DestType, nullptr))) { |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4182 | if (S.getLangOpts().CPlusPlus11) { |
| 4183 | // - Otherwise, if the initializer list has no elements and T is a |
| 4184 | // class type with a default constructor, the object is |
| 4185 | // value-initialized. |
| 4186 | if (InitList->getNumInits() == 0) { |
| 4187 | CXXRecordDecl *RD = DestType->getAsCXXRecordDecl(); |
| 4188 | if (RD->hasDefaultConstructor()) { |
| 4189 | TryValueInitialization(S, Entity, Kind, Sequence, InitList); |
| 4190 | return; |
| 4191 | } |
| 4192 | } |
| 4193 | |
| 4194 | // - Otherwise, if T is a specialization of std::initializer_list<E>, |
| 4195 | // an initializer_list object constructed [...] |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4196 | if (TryInitializerListConstruction(S, InitList, DestType, Sequence, |
| 4197 | TreatUnavailableAsInvalid)) |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4198 | return; |
| 4199 | |
| 4200 | // - Otherwise, if T is a class type, constructors are considered. |
| 4201 | Expr *InitListAsExpr = InitList; |
| 4202 | TryConstructorInitialization(S, Entity, Kind, InitListAsExpr, DestType, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 4203 | DestType, Sequence, /*InitListSyntax*/true); |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4204 | } else |
| 4205 | Sequence.SetFailed(InitializationSequence::FK_InitListBadDestinationType); |
| 4206 | return; |
| 4207 | } |
| 4208 | |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 4209 | if (S.getLangOpts().CPlusPlus && !DestType->isAggregateType() && |
Richard Smith | ed63886 | 2016-03-28 06:08:37 +0000 | [diff] [blame] | 4210 | InitList->getNumInits() == 1) { |
| 4211 | Expr *E = InitList->getInit(0); |
| 4212 | |
| 4213 | // - Otherwise, if T is an enumeration with a fixed underlying type, |
| 4214 | // the initializer-list has a single element v, and the initialization |
| 4215 | // is direct-list-initialization, the object is initialized with the |
| 4216 | // value T(v); if a narrowing conversion is required to convert v to |
| 4217 | // the underlying type of T, the program is ill-formed. |
| 4218 | auto *ET = DestType->getAs<EnumType>(); |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 4219 | if (S.getLangOpts().CPlusPlus17 && |
Richard Smith | ed63886 | 2016-03-28 06:08:37 +0000 | [diff] [blame] | 4220 | Kind.getKind() == InitializationKind::IK_DirectList && |
| 4221 | ET && ET->getDecl()->isFixed() && |
| 4222 | !S.Context.hasSameUnqualifiedType(E->getType(), DestType) && |
| 4223 | (E->getType()->isIntegralOrEnumerationType() || |
| 4224 | E->getType()->isFloatingType())) { |
| 4225 | // There are two ways that T(v) can work when T is an enumeration type. |
| 4226 | // If there is either an implicit conversion sequence from v to T or |
| 4227 | // a conversion function that can convert from v to T, then we use that. |
| 4228 | // Otherwise, if v is of integral, enumeration, or floating-point type, |
| 4229 | // it is converted to the enumeration type via its underlying type. |
| 4230 | // There is no overlap possible between these two cases (except when the |
| 4231 | // source value is already of the destination type), and the first |
| 4232 | // case is handled by the general case for single-element lists below. |
| 4233 | ImplicitConversionSequence ICS; |
| 4234 | ICS.setStandard(); |
| 4235 | ICS.Standard.setAsIdentityConversion(); |
Vedant Kumar | f4217f8 | 2017-02-16 01:20:00 +0000 | [diff] [blame] | 4236 | if (!E->isRValue()) |
| 4237 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
Richard Smith | ed63886 | 2016-03-28 06:08:37 +0000 | [diff] [blame] | 4238 | // If E is of a floating-point type, then the conversion is ill-formed |
| 4239 | // due to narrowing, but go through the motions in order to produce the |
| 4240 | // right diagnostic. |
| 4241 | ICS.Standard.Second = E->getType()->isFloatingType() |
| 4242 | ? ICK_Floating_Integral |
| 4243 | : ICK_Integral_Conversion; |
| 4244 | ICS.Standard.setFromType(E->getType()); |
| 4245 | ICS.Standard.setToType(0, E->getType()); |
| 4246 | ICS.Standard.setToType(1, DestType); |
| 4247 | ICS.Standard.setToType(2, DestType); |
| 4248 | Sequence.AddConversionSequenceStep(ICS, ICS.Standard.getToType(2), |
| 4249 | /*TopLevelOfInitList*/true); |
| 4250 | Sequence.RewrapReferenceInitList(Entity.getType(), InitList); |
| 4251 | return; |
| 4252 | } |
| 4253 | |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 4254 | // - Otherwise, if the initializer list has a single element of type E |
| 4255 | // [...references are handled above...], the object or reference is |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4256 | // initialized from that element (by copy-initialization for |
| 4257 | // copy-list-initialization, or by direct-initialization for |
| 4258 | // direct-list-initialization); if a narrowing conversion is required |
| 4259 | // to convert the element to T, the program is ill-formed. |
| 4260 | // |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 4261 | // Per core-24034, this is direct-initialization if we were performing |
| 4262 | // direct-list-initialization and copy-initialization otherwise. |
| 4263 | // We can't use InitListChecker for this, because it always performs |
| 4264 | // copy-initialization. This only matters if we might use an 'explicit' |
| 4265 | // conversion operator, so we only need to handle the cases where the source |
| 4266 | // is of record type. |
Richard Smith | ed63886 | 2016-03-28 06:08:37 +0000 | [diff] [blame] | 4267 | if (InitList->getInit(0)->getType()->isRecordType()) { |
| 4268 | InitializationKind SubKind = |
| 4269 | Kind.getKind() == InitializationKind::IK_DirectList |
| 4270 | ? InitializationKind::CreateDirect(Kind.getLocation(), |
| 4271 | InitList->getLBraceLoc(), |
| 4272 | InitList->getRBraceLoc()) |
| 4273 | : Kind; |
| 4274 | Expr *SubInit[1] = { InitList->getInit(0) }; |
| 4275 | Sequence.InitializeFrom(S, Entity, SubKind, SubInit, |
| 4276 | /*TopLevelOfInitList*/true, |
| 4277 | TreatUnavailableAsInvalid); |
| 4278 | if (Sequence) |
| 4279 | Sequence.RewrapReferenceInitList(Entity.getType(), InitList); |
| 4280 | return; |
| 4281 | } |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 4282 | } |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4283 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4284 | InitListChecker CheckInitList(S, Entity, InitList, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4285 | DestType, /*VerifyOnly=*/true, TreatUnavailableAsInvalid); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4286 | if (CheckInitList.HadError()) { |
| 4287 | Sequence.SetFailed(InitializationSequence::FK_ListInitializationFailed); |
| 4288 | return; |
| 4289 | } |
| 4290 | |
| 4291 | // Add the list initialization step with the built init list. |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4292 | Sequence.AddListInitializationStep(DestType); |
| 4293 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4294 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4295 | /// Try a reference initialization that involves calling a conversion |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4296 | /// function. |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4297 | static OverloadingResult TryRefInitWithConversionFunction( |
| 4298 | Sema &S, const InitializedEntity &Entity, const InitializationKind &Kind, |
| 4299 | Expr *Initializer, bool AllowRValues, bool IsLValueRef, |
| 4300 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4301 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4302 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 4303 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 4304 | QualType cv2T2 = Initializer->getType(); |
| 4305 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 4306 | |
| 4307 | bool DerivedToBase; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4308 | bool ObjCConversion; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4309 | bool ObjCLifetimeConversion; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4310 | assert(!S.CompareReferenceRelationship(Initializer->getBeginLoc(), T1, T2, |
| 4311 | DerivedToBase, ObjCConversion, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4312 | ObjCLifetimeConversion) && |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4313 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 8abbc65 | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 4314 | (void)DerivedToBase; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4315 | (void)ObjCConversion; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4316 | (void)ObjCLifetimeConversion; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4317 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4318 | // Build the candidate set directly in the initialization sequence |
| 4319 | // structure, so that it will persist if we fail. |
| 4320 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 4321 | CandidateSet.clear(OverloadCandidateSet::CSK_InitByUserDefinedConversion); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4322 | |
Richard Smith | b368ea8 | 2018-07-02 23:25:22 +0000 | [diff] [blame] | 4323 | // Determine whether we are allowed to call explicit conversion operators. |
| 4324 | // Note that none of [over.match.copy], [over.match.conv], nor |
| 4325 | // [over.match.ref] permit an explicit constructor to be chosen when |
| 4326 | // initializing a reference, not even for direct-initialization. |
| 4327 | bool AllowExplicitCtors = false; |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4328 | bool AllowExplicitConvs = Kind.allowExplicitConversionFunctionsInRefBinding(); |
| 4329 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4330 | const RecordType *T1RecordType = nullptr; |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 4331 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4332 | S.isCompleteType(Kind.getLocation(), T1)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4333 | // The type we're converting to is a class type. Enumerate its constructors |
| 4334 | // to see if there is a suitable conversion. |
| 4335 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 4336 | |
Richard Smith | 40c7806 | 2015-02-21 02:31:57 +0000 | [diff] [blame] | 4337 | for (NamedDecl *D : S.LookupConstructors(T1RecordDecl)) { |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4338 | auto Info = getConstructorInfo(D); |
| 4339 | if (!Info.Constructor) |
| 4340 | continue; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4341 | |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4342 | if (!Info.Constructor->isInvalidDecl() && |
Richard Smith | b368ea8 | 2018-07-02 23:25:22 +0000 | [diff] [blame] | 4343 | Info.Constructor->isConvertingConstructor(AllowExplicitCtors)) { |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4344 | if (Info.ConstructorTmpl) |
| 4345 | S.AddTemplateOverloadCandidate(Info.ConstructorTmpl, Info.FoundDecl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4346 | /*ExplicitArgs*/ nullptr, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4347 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | dfbdfbb | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 4348 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4349 | else |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4350 | S.AddOverloadCandidate(Info.Constructor, Info.FoundDecl, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4351 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | dfbdfbb | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 4352 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4353 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4354 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4355 | } |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 4356 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 4357 | return OR_No_Viable_Function; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4358 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4359 | const RecordType *T2RecordType = nullptr; |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 4360 | if ((T2RecordType = T2->getAs<RecordType>()) && |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4361 | S.isCompleteType(Kind.getLocation(), T2)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4362 | // The type we're converting from is a class type, enumerate its conversion |
| 4363 | // functions. |
| 4364 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 4365 | |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 4366 | const auto &Conversions = T2RecordDecl->getVisibleConversionFunctions(); |
| 4367 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4368 | NamedDecl *D = *I; |
| 4369 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 4370 | if (isa<UsingShadowDecl>(D)) |
| 4371 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4372 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4373 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 4374 | CXXConversionDecl *Conv; |
| 4375 | if (ConvTemplate) |
| 4376 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 4377 | else |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4378 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4379 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4380 | // If the conversion function doesn't return a reference type, |
| 4381 | // it can't be considered for this conversion unless we're allowed to |
| 4382 | // consider rvalues. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4383 | // FIXME: Do we need to make sure that we only consider conversion |
| 4384 | // candidates with reference-compatible results? That might be needed to |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4385 | // break recursion. |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4386 | if ((AllowExplicitConvs || !Conv->isExplicit()) && |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4387 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 4388 | if (ConvTemplate) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4389 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4390 | ActingDC, Initializer, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 4391 | DestType, CandidateSet, |
| 4392 | /*AllowObjCConversionOnExplicit=*/ |
| 4393 | false); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4394 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4395 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 4396 | Initializer, DestType, CandidateSet, |
| 4397 | /*AllowObjCConversionOnExplicit=*/false); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4398 | } |
| 4399 | } |
| 4400 | } |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 4401 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 4402 | return OR_No_Viable_Function; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4403 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4404 | SourceLocation DeclLoc = Initializer->getBeginLoc(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4405 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4406 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4407 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4408 | if (OverloadingResult Result |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 4409 | = CandidateSet.BestViableFunction(S, DeclLoc, Best)) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4410 | return Result; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 4411 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4412 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 4413 | // This is the overload that will be used for this initialization step if we |
| 4414 | // use this initialization. Mark it as referenced. |
| 4415 | Function->setReferenced(); |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4416 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4417 | // Compute the returned type and value kind of the conversion. |
| 4418 | QualType cv3T3; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4419 | if (isa<CXXConversionDecl>(Function)) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4420 | cv3T3 = Function->getReturnType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4421 | else |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4422 | cv3T3 = T1; |
| 4423 | |
| 4424 | ExprValueKind VK = VK_RValue; |
| 4425 | if (cv3T3->isLValueReferenceType()) |
| 4426 | VK = VK_LValue; |
| 4427 | else if (const auto *RRef = cv3T3->getAs<RValueReferenceType>()) |
| 4428 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
| 4429 | cv3T3 = cv3T3.getNonLValueExprType(S.Context); |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 4430 | |
| 4431 | // Add the user-defined conversion step. |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4432 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4433 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, cv3T3, |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4434 | HadMultipleCandidates); |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 4435 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4436 | // Determine whether we'll need to perform derived-to-base adjustments or |
| 4437 | // other conversions. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4438 | bool NewDerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4439 | bool NewObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4440 | bool NewObjCLifetimeConversion = false; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4441 | Sema::ReferenceCompareResult NewRefRelationship |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4442 | = S.CompareReferenceRelationship(DeclLoc, T1, cv3T3, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4443 | NewDerivedToBase, NewObjCConversion, |
| 4444 | NewObjCLifetimeConversion); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4445 | |
| 4446 | // Add the final conversion sequence, if necessary. |
Douglas Gregor | 1ce52ca | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 4447 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4448 | assert(!isa<CXXConstructorDecl>(Function) && |
| 4449 | "should not have conversion after constructor"); |
| 4450 | |
Douglas Gregor | 1ce52ca | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 4451 | ImplicitConversionSequence ICS; |
| 4452 | ICS.setStandard(); |
| 4453 | ICS.Standard = Best->FinalConversion; |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4454 | Sequence.AddConversionSequenceStep(ICS, ICS.Standard.getToType(2)); |
| 4455 | |
| 4456 | // Every implicit conversion results in a prvalue, except for a glvalue |
| 4457 | // derived-to-base conversion, which we handle below. |
| 4458 | cv3T3 = ICS.Standard.getToType(2); |
| 4459 | VK = VK_RValue; |
| 4460 | } |
| 4461 | |
| 4462 | // If the converted initializer is a prvalue, its type T4 is adjusted to |
| 4463 | // type "cv1 T4" and the temporary materialization conversion is applied. |
| 4464 | // |
| 4465 | // We adjust the cv-qualifications to match the reference regardless of |
| 4466 | // whether we have a prvalue so that the AST records the change. In this |
| 4467 | // case, T4 is "cv3 T3". |
| 4468 | QualType cv1T4 = S.Context.getQualifiedType(cv3T3, cv1T1.getQualifiers()); |
| 4469 | if (cv1T4.getQualifiers() != cv3T3.getQualifiers()) |
| 4470 | Sequence.AddQualificationConversionStep(cv1T4, VK); |
| 4471 | Sequence.AddReferenceBindingStep(cv1T4, VK == VK_RValue); |
| 4472 | VK = IsLValueRef ? VK_LValue : VK_XValue; |
| 4473 | |
| 4474 | if (NewDerivedToBase) |
| 4475 | Sequence.AddDerivedToBaseCastStep(cv1T1, VK); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4476 | else if (NewObjCConversion) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4477 | Sequence.AddObjCObjectConversionStep(cv1T1); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4478 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4479 | return OR_Success; |
| 4480 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4481 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4482 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 4483 | const InitializedEntity &Entity, |
| 4484 | Expr *CurInitExpr); |
| 4485 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4486 | /// Attempt reference initialization (C++0x [dcl.init.ref]) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4487 | static void TryReferenceInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4488 | const InitializedEntity &Entity, |
| 4489 | const InitializationKind &Kind, |
| 4490 | Expr *Initializer, |
| 4491 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4492 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4493 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 4494 | Qualifiers T1Quals; |
| 4495 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4496 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 4497 | Qualifiers T2Quals; |
| 4498 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4499 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4500 | // If the initializer is the address of an overloaded function, try |
| 4501 | // to resolve the overloaded function. If all goes well, T2 is the |
| 4502 | // type of the resulting function. |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4503 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 4504 | T1, Sequence)) |
| 4505 | return; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4506 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4507 | // Delegate everything else to a subfunction. |
| 4508 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 4509 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 4510 | } |
| 4511 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4512 | /// Determine whether an expression is a non-referenceable glvalue (one to |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 4513 | /// which a reference can never bind). Attempting to bind a reference to |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4514 | /// such a glvalue will always create a temporary. |
| 4515 | static bool isNonReferenceableGLValue(Expr *E) { |
| 4516 | return E->refersToBitField() || E->refersToVectorElement(); |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 4517 | } |
| 4518 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4519 | /// Reference initialization without resolving overloaded functions. |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4520 | static void TryReferenceInitializationCore(Sema &S, |
| 4521 | const InitializedEntity &Entity, |
| 4522 | const InitializationKind &Kind, |
| 4523 | Expr *Initializer, |
| 4524 | QualType cv1T1, QualType T1, |
| 4525 | Qualifiers T1Quals, |
| 4526 | QualType cv2T2, QualType T2, |
| 4527 | Qualifiers T2Quals, |
| 4528 | InitializationSequence &Sequence) { |
| 4529 | QualType DestType = Entity.getType(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4530 | SourceLocation DeclLoc = Initializer->getBeginLoc(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4531 | // Compute some basic properties of the types and the initializer. |
| 4532 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 4533 | bool isRValueRef = !isLValueRef; |
| 4534 | bool DerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4535 | bool ObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4536 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4537 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4538 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4539 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4540 | ObjCConversion, ObjCLifetimeConversion); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4541 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4542 | // C++0x [dcl.init.ref]p5: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4543 | // 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] | 4544 | // "cv2 T2" as follows: |
| 4545 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4546 | // - If the reference is an lvalue reference and the initializer |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4547 | // expression |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4548 | // Note the analogous bullet points for rvalue refs to functions. Because |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4549 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 4550 | // like lvalue refs. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4551 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4552 | bool T1Function = T1->isFunctionType(); |
| 4553 | if (isLValueRef || T1Function) { |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4554 | if (InitCategory.isLValue() && !isNonReferenceableGLValue(Initializer) && |
Richard Smith | ce76629 | 2016-10-21 23:01:55 +0000 | [diff] [blame] | 4555 | (RefRelationship == Sema::Ref_Compatible || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4556 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4557 | RefRelationship == Sema::Ref_Related))) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4558 | // - 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] | 4559 | // reference-compatible with "cv2 T2," or |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4560 | if (T1Quals != T2Quals) |
| 4561 | // Convert to cv1 T2. This should only add qualifiers unless this is a |
| 4562 | // c-style cast. The removal of qualifiers in that case notionally |
| 4563 | // happens after the reference binding, but that doesn't matter. |
| 4564 | Sequence.AddQualificationConversionStep( |
| 4565 | S.Context.getQualifiedType(T2, T1Quals), |
| 4566 | Initializer->getValueKind()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4567 | if (DerivedToBase) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4568 | Sequence.AddDerivedToBaseCastStep(cv1T1, VK_LValue); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4569 | else if (ObjCConversion) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4570 | Sequence.AddObjCObjectConversionStep(cv1T1); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4571 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4572 | // We only create a temporary here when binding a reference to a |
| 4573 | // bit-field or vector element. Those cases are't supposed to be |
| 4574 | // handled by this bullet, but the outcome is the same either way. |
| 4575 | Sequence.AddReferenceBindingStep(cv1T1, false); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4576 | return; |
| 4577 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4578 | |
| 4579 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 4580 | // reference-related to T2, and can be implicitly converted to an |
| 4581 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 4582 | // with "cv3 T3" (this conversion is selected by enumerating the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4583 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 4584 | // one through overload resolution (13.3)), |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4585 | // 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] | 4586 | // an rvalue. DR1287 removed the "implicitly" here. |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4587 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 4588 | (isLValueRef || InitCategory.isRValue())) { |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4589 | ConvOvlResult = TryRefInitWithConversionFunction( |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4590 | S, Entity, Kind, Initializer, /*AllowRValues*/ isRValueRef, |
| 4591 | /*IsLValueRef*/ isLValueRef, Sequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4592 | if (ConvOvlResult == OR_Success) |
| 4593 | return; |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4594 | if (ConvOvlResult != OR_No_Viable_Function) |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4595 | Sequence.SetOverloadFailure( |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4596 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4597 | ConvOvlResult); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4598 | } |
| 4599 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4600 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4601 | // - Otherwise, the reference shall be an lvalue reference to a |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4602 | // 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] | 4603 | // shall be an rvalue reference. |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 4604 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) { |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 4605 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 4606 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 4607 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4608 | Sequence.SetOverloadFailure( |
| 4609 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4610 | ConvOvlResult); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4611 | else if (!InitCategory.isLValue()) |
| 4612 | Sequence.SetFailed( |
| 4613 | InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
| 4614 | else { |
| 4615 | InitializationSequence::FailureKind FK; |
| 4616 | switch (RefRelationship) { |
| 4617 | case Sema::Ref_Compatible: |
| 4618 | if (Initializer->refersToBitField()) |
| 4619 | FK = InitializationSequence:: |
| 4620 | FK_NonConstLValueReferenceBindingToBitfield; |
| 4621 | else if (Initializer->refersToVectorElement()) |
| 4622 | FK = InitializationSequence:: |
| 4623 | FK_NonConstLValueReferenceBindingToVectorElement; |
| 4624 | else |
| 4625 | llvm_unreachable("unexpected kind of compatible initializer"); |
| 4626 | break; |
| 4627 | case Sema::Ref_Related: |
| 4628 | FK = InitializationSequence::FK_ReferenceInitDropsQualifiers; |
| 4629 | break; |
| 4630 | case Sema::Ref_Incompatible: |
| 4631 | FK = InitializationSequence:: |
| 4632 | FK_NonConstLValueReferenceBindingToUnrelated; |
| 4633 | break; |
| 4634 | } |
| 4635 | Sequence.SetFailed(FK); |
| 4636 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4637 | return; |
| 4638 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4639 | |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4640 | // - If the initializer expression |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4641 | // - is an |
| 4642 | // [<=14] xvalue (but not a bit-field), class prvalue, array prvalue, or |
| 4643 | // [1z] rvalue (but not a bit-field) or |
| 4644 | // function lvalue and "cv1 T1" is reference-compatible with "cv2 T2" |
| 4645 | // |
| 4646 | // Note: functions are handled above and below rather than here... |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4647 | if (!T1Function && |
Richard Smith | ce76629 | 2016-10-21 23:01:55 +0000 | [diff] [blame] | 4648 | (RefRelationship == Sema::Ref_Compatible || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4649 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4650 | RefRelationship == Sema::Ref_Related)) && |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4651 | ((InitCategory.isXValue() && !isNonReferenceableGLValue(Initializer)) || |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 4652 | (InitCategory.isPRValue() && |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 4653 | (S.getLangOpts().CPlusPlus17 || T2->isRecordType() || |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 4654 | T2->isArrayType())))) { |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4655 | ExprValueKind ValueKind = InitCategory.isXValue() ? VK_XValue : VK_RValue; |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4656 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4657 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 4658 | // compiler the freedom to perform a copy here or bind to the |
| 4659 | // object, while C++0x requires that we bind directly to the |
| 4660 | // object. Hence, we always bind to the object without making an |
| 4661 | // extra copy. However, in C++03 requires that we check for the |
| 4662 | // presence of a suitable copy constructor: |
| 4663 | // |
| 4664 | // The constructor that would be used to make the copy shall |
| 4665 | // be callable whether or not the copy is actually done. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4666 | if (!S.getLangOpts().CPlusPlus11 && !S.getLangOpts().MicrosoftExt) |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4667 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4668 | else if (S.getLangOpts().CPlusPlus11) |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4669 | CheckCXX98CompatAccessibleCopy(S, Entity, Initializer); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4670 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4671 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4672 | // C++1z [dcl.init.ref]/5.2.1.2: |
| 4673 | // If the converted initializer is a prvalue, its type T4 is adjusted |
| 4674 | // to type "cv1 T4" and the temporary materialization conversion is |
| 4675 | // applied. |
| 4676 | QualType cv1T4 = S.Context.getQualifiedType(cv2T2, T1Quals); |
| 4677 | if (T1Quals != T2Quals) |
| 4678 | Sequence.AddQualificationConversionStep(cv1T4, ValueKind); |
| 4679 | Sequence.AddReferenceBindingStep(cv1T4, ValueKind == VK_RValue); |
| 4680 | ValueKind = isLValueRef ? VK_LValue : VK_XValue; |
| 4681 | |
| 4682 | // In any case, the reference is bound to the resulting glvalue (or to |
| 4683 | // an appropriate base class subobject). |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4684 | if (DerivedToBase) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4685 | Sequence.AddDerivedToBaseCastStep(cv1T1, ValueKind); |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4686 | else if (ObjCConversion) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4687 | Sequence.AddObjCObjectConversionStep(cv1T1); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4688 | return; |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4689 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4690 | |
| 4691 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 4692 | // reference-related to T2, and can be implicitly converted to an |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4693 | // xvalue, class prvalue, or function lvalue of type "cv3 T3", |
| 4694 | // where "cv1 T1" is reference-compatible with "cv3 T3", |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4695 | // |
| 4696 | // DR1287 removes the "implicitly" here. |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4697 | if (T2->isRecordType()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4698 | if (RefRelationship == Sema::Ref_Incompatible) { |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4699 | ConvOvlResult = TryRefInitWithConversionFunction( |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4700 | S, Entity, Kind, Initializer, /*AllowRValues*/ true, |
| 4701 | /*IsLValueRef*/ isLValueRef, Sequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4702 | if (ConvOvlResult) |
| 4703 | Sequence.SetOverloadFailure( |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4704 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4705 | ConvOvlResult); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4706 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4707 | return; |
| 4708 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4709 | |
Richard Smith | ce76629 | 2016-10-21 23:01:55 +0000 | [diff] [blame] | 4710 | if (RefRelationship == Sema::Ref_Compatible && |
Douglas Gregor | 6fa6ab0 | 2013-03-26 23:59:23 +0000 | [diff] [blame] | 4711 | isRValueRef && InitCategory.isLValue()) { |
| 4712 | Sequence.SetFailed( |
| 4713 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 4714 | return; |
| 4715 | } |
| 4716 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4717 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 4718 | return; |
| 4719 | } |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4720 | |
| 4721 | // - Otherwise, a temporary of type "cv1 T1" is created and initialized |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4722 | // from the initializer expression using the rules for a non-reference |
Richard Smith | 2eabf78 | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 4723 | // copy-initialization (8.5). The reference is then bound to the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4724 | // temporary. [...] |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 4725 | |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 4726 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 4727 | |
Richard Smith | 2eabf78 | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 4728 | // FIXME: Why do we use an implicit conversion here rather than trying |
| 4729 | // copy-initialization? |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4730 | ImplicitConversionSequence ICS |
| 4731 | = S.TryImplicitConversion(Initializer, TempEntity.getType(), |
Richard Smith | 2eabf78 | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 4732 | /*SuppressUserConversions=*/false, |
| 4733 | /*AllowExplicit=*/false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4734 | /*FIXME:InOverloadResolution=*/false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4735 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 4736 | /*AllowObjCWritebackConversion=*/false); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4737 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4738 | if (ICS.isBad()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4739 | // FIXME: Use the conversion function set stored in ICS to turn |
| 4740 | // this into an overloading ambiguity diagnostic. However, we need |
| 4741 | // to keep that set as an OverloadCandidateSet rather than as some |
| 4742 | // other kind of set. |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4743 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 4744 | Sequence.SetOverloadFailure( |
| 4745 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4746 | ConvOvlResult); |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 4747 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 4748 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4749 | else |
| 4750 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4751 | return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4752 | } else { |
| 4753 | Sequence.AddConversionSequenceStep(ICS, TempEntity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4754 | } |
| 4755 | |
| 4756 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 4757 | // same cv-qualification as, or greater cv-qualification |
| 4758 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 4759 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 4760 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4761 | if (RefRelationship == Sema::Ref_Related && |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 4762 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4763 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 4764 | return; |
| 4765 | } |
| 4766 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4767 | // [...] 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] | 4768 | // reference, the initializer expression shall not be an lvalue. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4769 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 4770 | InitCategory.isLValue()) { |
| 4771 | Sequence.SetFailed( |
| 4772 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 4773 | return; |
| 4774 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4775 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4776 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4777 | } |
| 4778 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4779 | /// Attempt character array initialization from a string literal |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4780 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 4781 | static void TryStringLiteralInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4782 | const InitializedEntity &Entity, |
| 4783 | const InitializationKind &Kind, |
| 4784 | Expr *Initializer, |
| 4785 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4786 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4787 | } |
| 4788 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4789 | /// Attempt value initialization (C++ [dcl.init]p7). |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4790 | static void TryValueInitialization(Sema &S, |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4791 | const InitializedEntity &Entity, |
| 4792 | const InitializationKind &Kind, |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4793 | InitializationSequence &Sequence, |
| 4794 | InitListExpr *InitList) { |
| 4795 | assert((!InitList || InitList->getNumInits() == 0) && |
| 4796 | "Shouldn't use value-init for non-empty init lists"); |
| 4797 | |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4798 | // C++98 [dcl.init]p5, C++11 [dcl.init]p7: |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4799 | // |
| 4800 | // To value-initialize an object of type T means: |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4801 | QualType T = Entity.getType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4802 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4803 | // -- if T is an array type, then each element is value-initialized; |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4804 | T = S.Context.getBaseElementType(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4805 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4806 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 4807 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4808 | bool NeedZeroInitialization = true; |
Richard Smith | 505ef81 | 2016-12-21 01:57:02 +0000 | [diff] [blame] | 4809 | // C++98: |
| 4810 | // -- if T is a class type (clause 9) with a user-declared constructor |
| 4811 | // (12.1), then the default constructor for T is called (and the |
| 4812 | // initialization is ill-formed if T has no accessible default |
| 4813 | // constructor); |
| 4814 | // C++11: |
| 4815 | // -- if T is a class type (clause 9) with either no default constructor |
| 4816 | // (12.1 [class.ctor]) or a default constructor that is user-provided |
| 4817 | // or deleted, then the object is default-initialized; |
| 4818 | // |
| 4819 | // Note that the C++11 rule is the same as the C++98 rule if there are no |
| 4820 | // defaulted or deleted constructors, so we just use it unconditionally. |
| 4821 | CXXConstructorDecl *CD = S.LookupDefaultConstructor(ClassDecl); |
| 4822 | if (!CD || !CD->getCanonicalDecl()->isDefaulted() || CD->isDeleted()) |
| 4823 | NeedZeroInitialization = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4824 | |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4825 | // -- if T is a (possibly cv-qualified) non-union class type without a |
| 4826 | // user-provided or deleted default constructor, then the object is |
| 4827 | // zero-initialized and, if T has a non-trivial default constructor, |
| 4828 | // default-initialized; |
Richard Smith | fb26652 | 2012-10-18 00:44:17 +0000 | [diff] [blame] | 4829 | // The 'non-union' here was removed by DR1502. The 'non-trivial default |
| 4830 | // constructor' part was removed by DR1507. |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4831 | if (NeedZeroInitialization) |
| 4832 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 4833 | |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 4834 | // C++03: |
| 4835 | // -- if T is a non-union class type without a user-declared constructor, |
| 4836 | // then every non-static data member and base class component of T is |
| 4837 | // value-initialized; |
| 4838 | // [...] A program that calls for [...] value-initialization of an |
| 4839 | // entity of reference type is ill-formed. |
| 4840 | // |
| 4841 | // C++11 doesn't need this handling, because value-initialization does not |
| 4842 | // occur recursively there, and the implicit default constructor is |
| 4843 | // defined as deleted in the problematic cases. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4844 | if (!S.getLangOpts().CPlusPlus11 && |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 4845 | ClassDecl->hasUninitializedReferenceMember()) { |
| 4846 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForReference); |
| 4847 | return; |
| 4848 | } |
| 4849 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4850 | // If this is list-value-initialization, pass the empty init list on when |
| 4851 | // building the constructor call. This affects the semantics of a few |
| 4852 | // things (such as whether an explicit default constructor can be called). |
| 4853 | Expr *InitListAsExpr = InitList; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4854 | MultiExprArg Args(&InitListAsExpr, InitList ? 1 : 0); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4855 | bool InitListSyntax = InitList; |
| 4856 | |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 4857 | // FIXME: Instead of creating a CXXConstructExpr of array type here, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 4858 | // wrap a class-typed CXXConstructExpr in an ArrayInitLoopExpr. |
| 4859 | return TryConstructorInitialization( |
| 4860 | S, Entity, Kind, Args, T, Entity.getType(), Sequence, InitListSyntax); |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4861 | } |
| 4862 | } |
| 4863 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4864 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4865 | } |
| 4866 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4867 | /// Attempt default initialization (C++ [dcl.init]p6). |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4868 | static void TryDefaultInitialization(Sema &S, |
| 4869 | const InitializedEntity &Entity, |
| 4870 | const InitializationKind &Kind, |
| 4871 | InitializationSequence &Sequence) { |
| 4872 | assert(Kind.getKind() == InitializationKind::IK_Default); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4873 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4874 | // C++ [dcl.init]p6: |
| 4875 | // To default-initialize an object of type T means: |
| 4876 | // - if T is an array type, each element is default-initialized; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4877 | QualType DestType = S.Context.getBaseElementType(Entity.getType()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4878 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4879 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 4880 | // constructor for T is called (and the initialization is ill-formed if |
| 4881 | // T has no accessible default constructor); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4882 | if (DestType->isRecordType() && S.getLangOpts().CPlusPlus) { |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 4883 | TryConstructorInitialization(S, Entity, Kind, None, DestType, |
| 4884 | Entity.getType(), Sequence); |
Chandler Carruth | c926240 | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 4885 | return; |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4886 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4887 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4888 | // - otherwise, no initialization is performed. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4889 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4890 | // If a program calls for the default initialization of an object of |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4891 | // 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] | 4892 | // default constructor. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4893 | if (DestType.isConstQualified() && S.getLangOpts().CPlusPlus) { |
Nico Weber | 337d5aa | 2015-04-17 08:32:38 +0000 | [diff] [blame] | 4894 | if (!maybeRecoverWithZeroInitialization(S, Sequence, Entity)) |
| 4895 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4896 | return; |
| 4897 | } |
| 4898 | |
| 4899 | // If the destination type has a lifetime property, zero-initialize it. |
| 4900 | if (DestType.getQualifiers().hasObjCLifetime()) { |
| 4901 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 4902 | return; |
| 4903 | } |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4904 | } |
| 4905 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4906 | /// Attempt a user-defined conversion between two types (C++ [dcl.init]), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4907 | /// which enumerates all conversion functions and performs overload resolution |
| 4908 | /// to select the best. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4909 | static void TryUserDefinedConversion(Sema &S, |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 4910 | QualType DestType, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4911 | const InitializationKind &Kind, |
| 4912 | Expr *Initializer, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4913 | InitializationSequence &Sequence, |
| 4914 | bool TopLevelOfInitList) { |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4915 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 4916 | QualType SourceType = Initializer->getType(); |
| 4917 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 4918 | "Must have a class type to perform a user-defined conversion"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4919 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4920 | // Build the candidate set directly in the initialization sequence |
| 4921 | // structure, so that it will persist if we fail. |
| 4922 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 4923 | CandidateSet.clear(OverloadCandidateSet::CSK_InitByUserDefinedConversion); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4924 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4925 | // Determine whether we are allowed to call explicit constructors or |
| 4926 | // explicit conversion operators. |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 4927 | bool AllowExplicit = Kind.AllowExplicit(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4928 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4929 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 4930 | // The type we're converting to is a class type. Enumerate its constructors |
| 4931 | // to see if there is a suitable conversion. |
| 4932 | CXXRecordDecl *DestRecordDecl |
| 4933 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4934 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4935 | // Try to complete the type we're converting to. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4936 | if (S.isCompleteType(Kind.getLocation(), DestType)) { |
Richard Smith | 776e9c3 | 2017-02-01 03:28:59 +0000 | [diff] [blame] | 4937 | for (NamedDecl *D : S.LookupConstructors(DestRecordDecl)) { |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4938 | auto Info = getConstructorInfo(D); |
| 4939 | if (!Info.Constructor) |
| 4940 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4941 | |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4942 | if (!Info.Constructor->isInvalidDecl() && |
| 4943 | Info.Constructor->isConvertingConstructor(AllowExplicit)) { |
| 4944 | if (Info.ConstructorTmpl) |
| 4945 | S.AddTemplateOverloadCandidate(Info.ConstructorTmpl, Info.FoundDecl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4946 | /*ExplicitArgs*/ nullptr, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4947 | Initializer, CandidateSet, |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 4948 | /*SuppressUserConversions=*/true); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4949 | else |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4950 | S.AddOverloadCandidate(Info.Constructor, Info.FoundDecl, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4951 | Initializer, CandidateSet, |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 4952 | /*SuppressUserConversions=*/true); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4953 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4954 | } |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4955 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4956 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4957 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4958 | SourceLocation DeclLoc = Initializer->getBeginLoc(); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4959 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4960 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 4961 | // The type we're converting from is a class type, enumerate its conversion |
| 4962 | // functions. |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4963 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4964 | // We can only enumerate the conversion functions for a complete type; if |
| 4965 | // the type isn't complete, simply skip this step. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4966 | if (S.isCompleteType(DeclLoc, SourceType)) { |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4967 | CXXRecordDecl *SourceRecordDecl |
| 4968 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4969 | |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 4970 | const auto &Conversions = |
| 4971 | SourceRecordDecl->getVisibleConversionFunctions(); |
| 4972 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4973 | NamedDecl *D = *I; |
| 4974 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 4975 | if (isa<UsingShadowDecl>(D)) |
| 4976 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4977 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4978 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 4979 | CXXConversionDecl *Conv; |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4980 | if (ConvTemplate) |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4981 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4982 | else |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4983 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4984 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4985 | if (AllowExplicit || !Conv->isExplicit()) { |
| 4986 | if (ConvTemplate) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4987 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4988 | ActingDC, Initializer, DestType, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 4989 | CandidateSet, AllowExplicit); |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4990 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4991 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 4992 | Initializer, DestType, CandidateSet, |
| 4993 | AllowExplicit); |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4994 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4995 | } |
| 4996 | } |
| 4997 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4998 | |
| 4999 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5000 | OverloadCandidateSet::iterator Best; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5001 | if (OverloadingResult Result |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 5002 | = CandidateSet.BestViableFunction(S, DeclLoc, Best)) { |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5003 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5004 | InitializationSequence::FK_UserConversionOverloadFailed, |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5005 | Result); |
| 5006 | return; |
| 5007 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5008 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5009 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 5010 | Function->setReferenced(); |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 5011 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5012 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5013 | if (isa<CXXConstructorDecl>(Function)) { |
| 5014 | // Add the user-defined conversion step. Any cv-qualification conversion is |
Richard Smith | b24f067 | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 5015 | // subsumed by the initialization. Per DR5, the created temporary is of the |
| 5016 | // cv-unqualified type of the destination. |
| 5017 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
| 5018 | DestType.getUnqualifiedType(), |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 5019 | HadMultipleCandidates); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5020 | |
| 5021 | // C++14 and before: |
| 5022 | // - if the function is a constructor, the call initializes a temporary |
| 5023 | // of the cv-unqualified version of the destination type. The [...] |
| 5024 | // temporary [...] is then used to direct-initialize, according to the |
| 5025 | // rules above, the object that is the destination of the |
| 5026 | // copy-initialization. |
| 5027 | // Note that this just performs a simple object copy from the temporary. |
| 5028 | // |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 5029 | // C++17: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5030 | // - if the function is a constructor, the call is a prvalue of the |
| 5031 | // cv-unqualified version of the destination type whose return object |
| 5032 | // is initialized by the constructor. The call is used to |
| 5033 | // direct-initialize, according to the rules above, the object that |
| 5034 | // is the destination of the copy-initialization. |
| 5035 | // Therefore we need to do nothing further. |
| 5036 | // |
| 5037 | // FIXME: Mark this copy as extraneous. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 5038 | if (!S.getLangOpts().CPlusPlus17) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5039 | Sequence.AddFinalCopy(DestType); |
Richard Smith | 16d3150 | 2016-12-21 01:31:56 +0000 | [diff] [blame] | 5040 | else if (DestType.hasQualifiers()) |
| 5041 | Sequence.AddQualificationConversionStep(DestType, VK_RValue); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5042 | return; |
| 5043 | } |
| 5044 | |
| 5045 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 5046 | QualType ConvType = Function->getCallResultType(); |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 5047 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType, |
| 5048 | HadMultipleCandidates); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5049 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5050 | if (ConvType->getAs<RecordType>()) { |
| 5051 | // The call is used to direct-initialize [...] the object that is the |
| 5052 | // destination of the copy-initialization. |
| 5053 | // |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 5054 | // 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] | 5055 | // - If the initializer expression is a prvalue and the cv-unqualified |
| 5056 | // version of the source type is the same as the class of the |
| 5057 | // destination [... do not make an extra copy] |
| 5058 | // |
| 5059 | // FIXME: Mark this copy as extraneous. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 5060 | if (!S.getLangOpts().CPlusPlus17 || |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5061 | Function->getReturnType()->isReferenceType() || |
| 5062 | !S.Context.hasSameUnqualifiedType(ConvType, DestType)) |
| 5063 | Sequence.AddFinalCopy(DestType); |
Richard Smith | 16d3150 | 2016-12-21 01:31:56 +0000 | [diff] [blame] | 5064 | else if (!S.Context.hasSameType(ConvType, DestType)) |
| 5065 | Sequence.AddQualificationConversionStep(DestType, VK_RValue); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5066 | return; |
| 5067 | } |
| 5068 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5069 | // If the conversion following the call to the conversion function |
| 5070 | // is interesting, add it as a separate step. |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5071 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 5072 | Best->FinalConversion.Third) { |
| 5073 | ImplicitConversionSequence ICS; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5074 | ICS.setStandard(); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5075 | ICS.Standard = Best->FinalConversion; |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5076 | Sequence.AddConversionSequenceStep(ICS, DestType, TopLevelOfInitList); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5077 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5078 | } |
| 5079 | |
Richard Smith | f032001b | 2013-06-20 02:18:31 +0000 | [diff] [blame] | 5080 | /// An egregious hack for compatibility with libstdc++-4.2: in <tr1/hashtable>, |
| 5081 | /// a function with a pointer return type contains a 'return false;' statement. |
| 5082 | /// In C++11, 'false' is not a null pointer, so this breaks the build of any |
| 5083 | /// code using that header. |
| 5084 | /// |
| 5085 | /// Work around this by treating 'return false;' as zero-initializing the result |
| 5086 | /// if it's used in a pointer-returning function in a system header. |
| 5087 | static bool isLibstdcxxPointerReturnFalseHack(Sema &S, |
| 5088 | const InitializedEntity &Entity, |
| 5089 | const Expr *Init) { |
| 5090 | return S.getLangOpts().CPlusPlus11 && |
| 5091 | Entity.getKind() == InitializedEntity::EK_Result && |
| 5092 | Entity.getType()->isPointerType() && |
| 5093 | isa<CXXBoolLiteralExpr>(Init) && |
| 5094 | !cast<CXXBoolLiteralExpr>(Init)->getValue() && |
| 5095 | S.getSourceManager().isInSystemHeader(Init->getExprLoc()); |
| 5096 | } |
| 5097 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5098 | /// The non-zero enum values here are indexes into diagnostic alternatives. |
| 5099 | enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar }; |
| 5100 | |
| 5101 | /// Determines whether this expression is an acceptable ICR source. |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 5102 | static InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e, |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5103 | bool isAddressOf, bool &isWeakAccess) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5104 | // Skip parens. |
| 5105 | e = e->IgnoreParens(); |
| 5106 | |
| 5107 | // Skip address-of nodes. |
| 5108 | if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 5109 | if (op->getOpcode() == UO_AddrOf) |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5110 | return isInvalidICRSource(C, op->getSubExpr(), /*addressof*/ true, |
| 5111 | isWeakAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5112 | |
| 5113 | // Skip certain casts. |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 5114 | } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 5115 | switch (ce->getCastKind()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5116 | case CK_Dependent: |
| 5117 | case CK_BitCast: |
| 5118 | case CK_LValueBitCast: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5119 | case CK_NoOp: |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5120 | return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf, isWeakAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5121 | |
| 5122 | case CK_ArrayToPointerDecay: |
| 5123 | return IIK_nonscalar; |
| 5124 | |
| 5125 | case CK_NullToPointer: |
| 5126 | return IIK_okay; |
| 5127 | |
| 5128 | default: |
| 5129 | break; |
| 5130 | } |
| 5131 | |
| 5132 | // 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] | 5133 | } else if (isa<DeclRefExpr>(e)) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5134 | // set isWeakAccess to true, to mean that there will be an implicit |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5135 | // load which requires a cleanup. |
| 5136 | if (e->getType().getObjCLifetime() == Qualifiers::OCL_Weak) |
| 5137 | isWeakAccess = true; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5138 | |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 5139 | if (!isAddressOf) return IIK_nonlocal; |
| 5140 | |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5141 | VarDecl *var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl()); |
| 5142 | if (!var) return IIK_nonlocal; |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 5143 | |
| 5144 | return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5145 | |
| 5146 | // If we have a conditional operator, check both sides. |
| 5147 | } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) { |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5148 | if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf, |
| 5149 | isWeakAccess)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5150 | return iik; |
| 5151 | |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5152 | return isInvalidICRSource(C, cond->getRHS(), isAddressOf, isWeakAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5153 | |
| 5154 | // These are never scalar. |
| 5155 | } else if (isa<ArraySubscriptExpr>(e)) { |
| 5156 | return IIK_nonscalar; |
| 5157 | |
| 5158 | // Otherwise, it needs to be a null pointer constant. |
| 5159 | } else { |
| 5160 | return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull) |
| 5161 | ? IIK_okay : IIK_nonlocal); |
| 5162 | } |
| 5163 | |
| 5164 | return IIK_nonlocal; |
| 5165 | } |
| 5166 | |
| 5167 | /// Check whether the given expression is a valid operand for an |
| 5168 | /// indirect copy/restore. |
| 5169 | static void checkIndirectCopyRestoreSource(Sema &S, Expr *src) { |
| 5170 | assert(src->isRValue()); |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5171 | bool isWeakAccess = false; |
| 5172 | InvalidICRKind iik = isInvalidICRSource(S.Context, src, false, isWeakAccess); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5173 | // If isWeakAccess to true, there will be an implicit |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5174 | // load which requires a cleanup. |
| 5175 | if (S.getLangOpts().ObjCAutoRefCount && isWeakAccess) |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 5176 | S.Cleanup.setExprNeedsCleanups(true); |
| 5177 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5178 | if (iik == IIK_okay) return; |
| 5179 | |
| 5180 | S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback) |
| 5181 | << ((unsigned) iik - 1) // shift index into diagnostic explanations |
| 5182 | << src->getSourceRange(); |
| 5183 | } |
| 5184 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5185 | /// Determine whether we have compatible array types for the |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5186 | /// purposes of GNU by-copy array initialization. |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 5187 | static bool hasCompatibleArrayTypes(ASTContext &Context, const ArrayType *Dest, |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5188 | const ArrayType *Source) { |
| 5189 | // If the source and destination array types are equivalent, we're |
| 5190 | // done. |
| 5191 | if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0))) |
| 5192 | return true; |
| 5193 | |
| 5194 | // Make sure that the element types are the same. |
| 5195 | if (!Context.hasSameType(Dest->getElementType(), Source->getElementType())) |
| 5196 | return false; |
| 5197 | |
| 5198 | // The only mismatch we allow is when the destination is an |
| 5199 | // incomplete array type and the source is a constant array type. |
| 5200 | return Source->isConstantArrayType() && Dest->isIncompleteArrayType(); |
| 5201 | } |
| 5202 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5203 | static bool tryObjCWritebackConversion(Sema &S, |
| 5204 | InitializationSequence &Sequence, |
| 5205 | const InitializedEntity &Entity, |
| 5206 | Expr *Initializer) { |
| 5207 | bool ArrayDecay = false; |
| 5208 | QualType ArgType = Initializer->getType(); |
| 5209 | QualType ArgPointee; |
| 5210 | if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) { |
| 5211 | ArrayDecay = true; |
| 5212 | ArgPointee = ArgArrayType->getElementType(); |
| 5213 | ArgType = S.Context.getPointerType(ArgPointee); |
| 5214 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5215 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5216 | // Handle write-back conversion. |
| 5217 | QualType ConvertedArgType; |
| 5218 | if (!S.isObjCWritebackConversion(ArgType, Entity.getType(), |
| 5219 | ConvertedArgType)) |
| 5220 | return false; |
| 5221 | |
| 5222 | // We should copy unless we're passing to an argument explicitly |
| 5223 | // marked 'out'. |
| 5224 | bool ShouldCopy = true; |
| 5225 | if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 5226 | ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 5227 | |
| 5228 | // Do we need an lvalue conversion? |
| 5229 | if (ArrayDecay || Initializer->isGLValue()) { |
| 5230 | ImplicitConversionSequence ICS; |
| 5231 | ICS.setStandard(); |
| 5232 | ICS.Standard.setAsIdentityConversion(); |
| 5233 | |
| 5234 | QualType ResultType; |
| 5235 | if (ArrayDecay) { |
| 5236 | ICS.Standard.First = ICK_Array_To_Pointer; |
| 5237 | ResultType = S.Context.getPointerType(ArgPointee); |
| 5238 | } else { |
| 5239 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
| 5240 | ResultType = Initializer->getType().getNonLValueExprType(S.Context); |
| 5241 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5242 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5243 | Sequence.AddConversionSequenceStep(ICS, ResultType); |
| 5244 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5245 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5246 | Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
| 5247 | return true; |
| 5248 | } |
| 5249 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5250 | static bool TryOCLSamplerInitialization(Sema &S, |
| 5251 | InitializationSequence &Sequence, |
| 5252 | QualType DestType, |
| 5253 | Expr *Initializer) { |
| 5254 | if (!S.getLangOpts().OpenCL || !DestType->isSamplerT() || |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 5255 | (!Initializer->isIntegerConstantExpr(S.Context) && |
| 5256 | !Initializer->getType()->isSamplerT())) |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5257 | return false; |
| 5258 | |
| 5259 | Sequence.AddOCLSamplerInitStep(DestType); |
| 5260 | return true; |
| 5261 | } |
| 5262 | |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5263 | // |
| 5264 | // OpenCL 1.2 spec, s6.12.10 |
| 5265 | // |
| 5266 | // The event argument can also be used to associate the |
| 5267 | // async_work_group_copy with a previous async copy allowing |
| 5268 | // an event to be shared by multiple async copies; otherwise |
| 5269 | // event should be zero. |
| 5270 | // |
| 5271 | static bool TryOCLZeroEventInitialization(Sema &S, |
| 5272 | InitializationSequence &Sequence, |
| 5273 | QualType DestType, |
| 5274 | Expr *Initializer) { |
| 5275 | if (!S.getLangOpts().OpenCL || !DestType->isEventT() || |
| 5276 | !Initializer->isIntegerConstantExpr(S.getASTContext()) || |
| 5277 | (Initializer->EvaluateKnownConstInt(S.getASTContext()) != 0)) |
| 5278 | return false; |
| 5279 | |
| 5280 | Sequence.AddOCLZeroEventStep(DestType); |
| 5281 | return true; |
| 5282 | } |
| 5283 | |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 5284 | static bool TryOCLZeroQueueInitialization(Sema &S, |
| 5285 | InitializationSequence &Sequence, |
| 5286 | QualType DestType, |
| 5287 | Expr *Initializer) { |
| 5288 | if (!S.getLangOpts().OpenCL || S.getLangOpts().OpenCLVersion < 200 || |
| 5289 | !DestType->isQueueT() || |
| 5290 | !Initializer->isIntegerConstantExpr(S.getASTContext()) || |
| 5291 | (Initializer->EvaluateKnownConstInt(S.getASTContext()) != 0)) |
| 5292 | return false; |
| 5293 | |
| 5294 | Sequence.AddOCLZeroQueueStep(DestType); |
| 5295 | return true; |
| 5296 | } |
| 5297 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5298 | InitializationSequence::InitializationSequence(Sema &S, |
| 5299 | const InitializedEntity &Entity, |
| 5300 | const InitializationKind &Kind, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5301 | MultiExprArg Args, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 5302 | bool TopLevelOfInitList, |
| 5303 | bool TreatUnavailableAsInvalid) |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 5304 | : FailedCandidateSet(Kind.getLocation(), OverloadCandidateSet::CSK_Normal) { |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 5305 | InitializeFrom(S, Entity, Kind, Args, TopLevelOfInitList, |
| 5306 | TreatUnavailableAsInvalid); |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 5307 | } |
| 5308 | |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 5309 | /// Tries to get a FunctionDecl out of `E`. If it succeeds and we can take the |
| 5310 | /// address of that function, this returns true. Otherwise, it returns false. |
| 5311 | static bool isExprAnUnaddressableFunction(Sema &S, const Expr *E) { |
| 5312 | auto *DRE = dyn_cast<DeclRefExpr>(E); |
| 5313 | if (!DRE || !isa<FunctionDecl>(DRE->getDecl())) |
| 5314 | return false; |
| 5315 | |
| 5316 | return !S.checkAddressOfFunctionIsAvailable( |
| 5317 | cast<FunctionDecl>(DRE->getDecl())); |
| 5318 | } |
| 5319 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5320 | /// Determine whether we can perform an elementwise array copy for this kind |
| 5321 | /// of entity. |
| 5322 | static bool canPerformArrayCopy(const InitializedEntity &Entity) { |
| 5323 | switch (Entity.getKind()) { |
| 5324 | case InitializedEntity::EK_LambdaCapture: |
| 5325 | // C++ [expr.prim.lambda]p24: |
| 5326 | // For array members, the array elements are direct-initialized in |
| 5327 | // increasing subscript order. |
| 5328 | return true; |
| 5329 | |
| 5330 | case InitializedEntity::EK_Variable: |
| 5331 | // C++ [dcl.decomp]p1: |
| 5332 | // [...] each element is copy-initialized or direct-initialized from the |
| 5333 | // corresponding element of the assignment-expression [...] |
| 5334 | return isa<DecompositionDecl>(Entity.getDecl()); |
| 5335 | |
| 5336 | case InitializedEntity::EK_Member: |
| 5337 | // C++ [class.copy.ctor]p14: |
| 5338 | // - if the member is an array, each element is direct-initialized with |
| 5339 | // the corresponding subobject of x |
| 5340 | return Entity.isImplicitMemberInitializer(); |
| 5341 | |
| 5342 | case InitializedEntity::EK_ArrayElement: |
| 5343 | // All the above cases are intended to apply recursively, even though none |
| 5344 | // of them actually say that. |
| 5345 | if (auto *E = Entity.getParent()) |
| 5346 | return canPerformArrayCopy(*E); |
| 5347 | break; |
| 5348 | |
| 5349 | default: |
| 5350 | break; |
| 5351 | } |
| 5352 | |
| 5353 | return false; |
| 5354 | } |
| 5355 | |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 5356 | void InitializationSequence::InitializeFrom(Sema &S, |
| 5357 | const InitializedEntity &Entity, |
| 5358 | const InitializationKind &Kind, |
| 5359 | MultiExprArg Args, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 5360 | bool TopLevelOfInitList, |
| 5361 | bool TreatUnavailableAsInvalid) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5362 | ASTContext &Context = S.Context; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5363 | |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 5364 | // Eliminate non-overload placeholder types in the arguments. We |
| 5365 | // need to do this before checking whether types are dependent |
| 5366 | // because lowering a pseudo-object expression might well give us |
| 5367 | // something of dependent type. |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5368 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 5369 | if (Args[I]->getType()->isNonOverloadPlaceholderType()) { |
| 5370 | // FIXME: should we be doing this here? |
| 5371 | ExprResult result = S.CheckPlaceholderExpr(Args[I]); |
| 5372 | if (result.isInvalid()) { |
| 5373 | SetFailed(FK_PlaceholderType); |
| 5374 | return; |
| 5375 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5376 | Args[I] = result.get(); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 5377 | } |
| 5378 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5379 | // C++0x [dcl.init]p16: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5380 | // The semantics of initializers are as follows. The destination type is |
| 5381 | // the type of the object or reference being initialized and the source |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5382 | // 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] | 5383 | // 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] | 5384 | // parenthesized list of expressions. |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5385 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5386 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5387 | if (DestType->isDependentType() || |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5388 | Expr::hasAnyTypeDependentArguments(Args)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5389 | SequenceKind = DependentSequence; |
| 5390 | return; |
| 5391 | } |
| 5392 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5393 | // Almost everything is a normal sequence. |
| 5394 | setSequenceKind(NormalSequence); |
| 5395 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5396 | QualType SourceType; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5397 | Expr *Initializer = nullptr; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5398 | if (Args.size() == 1) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5399 | Initializer = Args[0]; |
Fariborz Jahanian | 283bf89 | 2013-12-18 21:04:43 +0000 | [diff] [blame] | 5400 | if (S.getLangOpts().ObjC1) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5401 | if (S.CheckObjCBridgeRelatedConversions(Initializer->getBeginLoc(), |
Fariborz Jahanian | 283bf89 | 2013-12-18 21:04:43 +0000 | [diff] [blame] | 5402 | DestType, Initializer->getType(), |
| 5403 | Initializer) || |
| 5404 | S.ConversionToObjCStringLiteralCheck(DestType, Initializer)) |
| 5405 | Args[0] = Initializer; |
Fariborz Jahanian | 283bf89 | 2013-12-18 21:04:43 +0000 | [diff] [blame] | 5406 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5407 | if (!isa<InitListExpr>(Initializer)) |
| 5408 | SourceType = Initializer->getType(); |
| 5409 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5410 | |
Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 5411 | // - If the initializer is a (non-parenthesized) braced-init-list, the |
| 5412 | // object is list-initialized (8.5.4). |
| 5413 | if (Kind.getKind() != InitializationKind::IK_Direct) { |
| 5414 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 5415 | TryListInitialization(S, Entity, Kind, InitList, *this, |
| 5416 | TreatUnavailableAsInvalid); |
Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 5417 | return; |
| 5418 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5419 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5420 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5421 | // - If the destination type is a reference type, see 8.5.3. |
| 5422 | if (DestType->isReferenceType()) { |
| 5423 | // C++0x [dcl.init.ref]p1: |
| 5424 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 5425 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 5426 | // by an object that can be converted into a T. |
| 5427 | // (Therefore, multiple arguments are not permitted.) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5428 | if (Args.size() != 1) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5429 | SetFailed(FK_TooManyInitsForReference); |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 5430 | // C++17 [dcl.init.ref]p5: |
| 5431 | // A reference [...] is initialized by an expression [...] as follows: |
| 5432 | // If the initializer is not an expression, presumably we should reject, |
| 5433 | // but the standard fails to actually say so. |
| 5434 | else if (isa<InitListExpr>(Args[0])) |
| 5435 | SetFailed(FK_ParenthesizedListInitForReference); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5436 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5437 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5438 | return; |
| 5439 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5440 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5441 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5442 | if (Kind.getKind() == InitializationKind::IK_Value || |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5443 | (Kind.getKind() == InitializationKind::IK_Direct && Args.empty())) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5444 | TryValueInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5445 | return; |
| 5446 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5447 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5448 | // Handle default initialization. |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 5449 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5450 | TryDefaultInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5451 | return; |
| 5452 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5453 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 5454 | // - If the destination type is an array of characters, an array of |
| 5455 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 5456 | // initializer is a string literal, see 8.5.2. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5457 | // - Otherwise, if the destination type is an array, the program is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5458 | // ill-formed. |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5459 | if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) { |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 5460 | if (Initializer && isa<VariableArrayType>(DestAT)) { |
| 5461 | SetFailed(FK_VariableLengthArrayHasInitializer); |
| 5462 | return; |
| 5463 | } |
| 5464 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 5465 | if (Initializer) { |
| 5466 | switch (IsStringInit(Initializer, DestAT, Context)) { |
| 5467 | case SIF_None: |
| 5468 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
| 5469 | return; |
| 5470 | case SIF_NarrowStringIntoWideChar: |
| 5471 | SetFailed(FK_NarrowStringIntoWideCharArray); |
| 5472 | return; |
| 5473 | case SIF_WideStringIntoChar: |
| 5474 | SetFailed(FK_WideStringIntoCharArray); |
| 5475 | return; |
| 5476 | case SIF_IncompatWideStringIntoWideChar: |
| 5477 | SetFailed(FK_IncompatWideStringIntoWideChar); |
| 5478 | return; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 5479 | case SIF_PlainStringIntoUTF8Char: |
| 5480 | SetFailed(FK_PlainStringIntoUTF8Char); |
| 5481 | return; |
| 5482 | case SIF_UTF8StringIntoPlainChar: |
| 5483 | SetFailed(FK_UTF8StringIntoPlainChar); |
| 5484 | return; |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 5485 | case SIF_Other: |
| 5486 | break; |
| 5487 | } |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 5488 | } |
| 5489 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5490 | // Some kinds of initialization permit an array to be initialized from |
| 5491 | // another array of the same type, and perform elementwise initialization. |
| 5492 | if (Initializer && isa<ConstantArrayType>(DestAT) && |
| 5493 | S.Context.hasSameUnqualifiedType(Initializer->getType(), |
| 5494 | Entity.getType()) && |
| 5495 | canPerformArrayCopy(Entity)) { |
| 5496 | // If source is a prvalue, use it directly. |
| 5497 | if (Initializer->getValueKind() == VK_RValue) { |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 5498 | AddArrayInitStep(DestType, /*IsGNUExtension*/false); |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5499 | return; |
| 5500 | } |
| 5501 | |
| 5502 | // Emit element-at-a-time copy loop. |
| 5503 | InitializedEntity Element = |
| 5504 | InitializedEntity::InitializeElement(S.Context, 0, Entity); |
| 5505 | QualType InitEltT = |
| 5506 | Context.getAsArrayType(Initializer->getType())->getElementType(); |
Richard Smith | 30e304e | 2016-12-14 00:03:17 +0000 | [diff] [blame] | 5507 | OpaqueValueExpr OVE(Initializer->getExprLoc(), InitEltT, |
| 5508 | Initializer->getValueKind(), |
| 5509 | Initializer->getObjectKind()); |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5510 | Expr *OVEAsExpr = &OVE; |
| 5511 | InitializeFrom(S, Element, Kind, OVEAsExpr, TopLevelOfInitList, |
| 5512 | TreatUnavailableAsInvalid); |
| 5513 | if (!Failed()) |
| 5514 | AddArrayInitLoopStep(Entity.getType(), InitEltT); |
| 5515 | return; |
| 5516 | } |
| 5517 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5518 | // Note: as an GNU C extension, we allow initialization of an |
| 5519 | // array from a compound literal that creates an array of the same |
| 5520 | // type, so long as the initializer has no side effects. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5521 | if (!S.getLangOpts().CPlusPlus && Initializer && |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5522 | isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) && |
| 5523 | Initializer->getType()->isArrayType()) { |
| 5524 | const ArrayType *SourceAT |
| 5525 | = Context.getAsArrayType(Initializer->getType()); |
| 5526 | if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5527 | SetFailed(FK_ArrayTypeMismatch); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5528 | else if (Initializer->HasSideEffects(S.Context)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5529 | SetFailed(FK_NonConstantArrayInit); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5530 | else { |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 5531 | AddArrayInitStep(DestType, /*IsGNUExtension*/true); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5532 | } |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5533 | } |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5534 | // Note: as a GNU C++ extension, we allow list-initialization of a |
| 5535 | // class member of array type from a parenthesized initializer list. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5536 | else if (S.getLangOpts().CPlusPlus && |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5537 | Entity.getKind() == InitializedEntity::EK_Member && |
| 5538 | Initializer && isa<InitListExpr>(Initializer)) { |
| 5539 | TryListInitialization(S, Entity, Kind, cast<InitListExpr>(Initializer), |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 5540 | *this, TreatUnavailableAsInvalid); |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5541 | AddParenthesizedArrayInitStep(DestType); |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 5542 | } else if (DestAT->getElementType()->isCharType()) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5543 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 5544 | else if (IsWideCharCompatible(DestAT->getElementType(), Context)) |
| 5545 | SetFailed(FK_ArrayNeedsInitListOrWideStringLiteral); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5546 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5547 | SetFailed(FK_ArrayNeedsInitList); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5548 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5549 | return; |
| 5550 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5551 | |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 5552 | // Determine whether we should consider writeback conversions for |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5553 | // Objective-C ARC. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5554 | bool allowObjCWritebackConversion = S.getLangOpts().ObjCAutoRefCount && |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5555 | Entity.isParameterKind(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5556 | |
| 5557 | // We're at the end of the line for C: it's either a write-back conversion |
| 5558 | // 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] | 5559 | if (!S.getLangOpts().CPlusPlus) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5560 | // If allowed, check whether this is an Objective-C writeback conversion. |
| 5561 | if (allowObjCWritebackConversion && |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5562 | tryObjCWritebackConversion(S, *this, Entity, Initializer)) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5563 | return; |
| 5564 | } |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5565 | |
| 5566 | if (TryOCLSamplerInitialization(S, *this, DestType, Initializer)) |
| 5567 | return; |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5568 | |
| 5569 | if (TryOCLZeroEventInitialization(S, *this, DestType, Initializer)) |
| 5570 | return; |
| 5571 | |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 5572 | if (TryOCLZeroQueueInitialization(S, *this, DestType, Initializer)) |
| 5573 | return; |
| 5574 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5575 | // Handle initialization in C |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5576 | AddCAssignmentStep(DestType); |
| 5577 | MaybeProduceObjCObject(S, *this, Entity); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5578 | return; |
| 5579 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5580 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5581 | assert(S.getLangOpts().CPlusPlus); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 5582 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5583 | // - If the destination type is a (possibly cv-qualified) class type: |
| 5584 | if (DestType->isRecordType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5585 | // - If the initialization is direct-initialization, or if it is |
| 5586 | // copy-initialization where the cv-unqualified version of the |
| 5587 | // 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] | 5588 | // class of the destination, constructors are considered. [...] |
| 5589 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 5590 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 5591 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5592 | S.IsDerivedFrom(Initializer->getBeginLoc(), SourceType, DestType)))) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5593 | TryConstructorInitialization(S, Entity, Kind, Args, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5594 | DestType, DestType, *this); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5595 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5596 | // user-defined conversion sequences that can convert from the source |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5597 | // type to the destination type or (when a conversion function is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5598 | // used) to a derived class thereof are enumerated as described in |
| 5599 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 5600 | // (13.3). |
| 5601 | else |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5602 | TryUserDefinedConversion(S, DestType, Kind, Initializer, *this, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5603 | TopLevelOfInitList); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5604 | return; |
| 5605 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5606 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 5607 | assert(Args.size() >= 1 && "Zero-argument case handled above"); |
| 5608 | |
| 5609 | // The remaining cases all need a source type. |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5610 | if (Args.size() > 1) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5611 | SetFailed(FK_TooManyInitsForScalar); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5612 | return; |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 5613 | } else if (isa<InitListExpr>(Args[0])) { |
| 5614 | SetFailed(FK_ParenthesizedListInitForScalar); |
| 5615 | return; |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5616 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5617 | |
| 5618 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5619 | // type, conversion functions are considered. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5620 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5621 | // For a conversion to _Atomic(T) from either T or a class type derived |
| 5622 | // from T, initialize the T object then convert to _Atomic type. |
| 5623 | bool NeedAtomicConversion = false; |
| 5624 | if (const AtomicType *Atomic = DestType->getAs<AtomicType>()) { |
| 5625 | if (Context.hasSameUnqualifiedType(SourceType, Atomic->getValueType()) || |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5626 | S.IsDerivedFrom(Initializer->getBeginLoc(), SourceType, |
Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 5627 | Atomic->getValueType())) { |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5628 | DestType = Atomic->getValueType(); |
| 5629 | NeedAtomicConversion = true; |
| 5630 | } |
| 5631 | } |
| 5632 | |
| 5633 | TryUserDefinedConversion(S, DestType, Kind, Initializer, *this, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5634 | TopLevelOfInitList); |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5635 | MaybeProduceObjCObject(S, *this, Entity); |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5636 | if (!Failed() && NeedAtomicConversion) |
| 5637 | AddAtomicConversionStep(Entity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5638 | return; |
| 5639 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5640 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5641 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5642 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5643 | // conversions (Clause 4) will be used, if necessary, to convert the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5644 | // initializer expression to the cv-unqualified version of the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5645 | // destination type; no user-defined conversions are considered. |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5646 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5647 | ImplicitConversionSequence ICS |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5648 | = S.TryImplicitConversion(Initializer, DestType, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5649 | /*SuppressUserConversions*/true, |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 5650 | /*AllowExplicitConversions*/ false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 5651 | /*InOverloadResolution*/ false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5652 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 5653 | allowObjCWritebackConversion); |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5654 | |
| 5655 | if (ICS.isStandard() && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5656 | ICS.Standard.Second == ICK_Writeback_Conversion) { |
| 5657 | // Objective-C ARC writeback conversion. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5658 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5659 | // We should copy unless we're passing to an argument explicitly |
| 5660 | // marked 'out'. |
| 5661 | bool ShouldCopy = true; |
| 5662 | if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 5663 | ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5664 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5665 | // If there was an lvalue adjustment, add it as a separate conversion. |
| 5666 | if (ICS.Standard.First == ICK_Array_To_Pointer || |
| 5667 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 5668 | ImplicitConversionSequence LvalueICS; |
| 5669 | LvalueICS.setStandard(); |
| 5670 | LvalueICS.Standard.setAsIdentityConversion(); |
| 5671 | LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0)); |
| 5672 | LvalueICS.Standard.First = ICS.Standard.First; |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5673 | AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5674 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5675 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5676 | AddPassByIndirectCopyRestoreStep(DestType, ShouldCopy); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5677 | } else if (ICS.isBad()) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 5678 | DeclAccessPair dap; |
Richard Smith | f032001b | 2013-06-20 02:18:31 +0000 | [diff] [blame] | 5679 | if (isLibstdcxxPointerReturnFalseHack(S, Entity, Initializer)) { |
| 5680 | AddZeroInitializationStep(Entity.getType()); |
| 5681 | } else if (Initializer->getType() == Context.OverloadTy && |
| 5682 | !S.ResolveAddressOfOverloadedFunction(Initializer, DestType, |
| 5683 | false, dap)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5684 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 5685 | else if (Initializer->getType()->isFunctionType() && |
| 5686 | isExprAnUnaddressableFunction(S, Initializer)) |
| 5687 | SetFailed(InitializationSequence::FK_AddressOfUnaddressableFunction); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 5688 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5689 | SetFailed(InitializationSequence::FK_ConversionFailed); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5690 | } else { |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5691 | AddConversionSequenceStep(ICS, DestType, TopLevelOfInitList); |
John McCall | fa27234 | 2011-06-16 23:24:51 +0000 | [diff] [blame] | 5692 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5693 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 5694 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5695 | } |
| 5696 | |
| 5697 | InitializationSequence::~InitializationSequence() { |
Davide Italiano | 67bb9f7 | 2015-07-01 21:51:58 +0000 | [diff] [blame] | 5698 | for (auto &S : Steps) |
| 5699 | S.Destroy(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5700 | } |
| 5701 | |
| 5702 | //===----------------------------------------------------------------------===// |
| 5703 | // Perform initialization |
| 5704 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5705 | static Sema::AssignmentAction |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 5706 | getAssignmentAction(const InitializedEntity &Entity, bool Diagnose = false) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5707 | switch(Entity.getKind()) { |
| 5708 | case InitializedEntity::EK_Variable: |
| 5709 | case InitializedEntity::EK_New: |
Douglas Gregor | 6dd3a6a | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 5710 | case InitializedEntity::EK_Exception: |
| 5711 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 5712 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5713 | return Sema::AA_Initializing; |
| 5714 | |
| 5715 | case InitializedEntity::EK_Parameter: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5716 | if (Entity.getDecl() && |
Douglas Gregor | 6b7f12c | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 5717 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 5718 | return Sema::AA_Sending; |
| 5719 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5720 | return Sema::AA_Passing; |
| 5721 | |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 5722 | case InitializedEntity::EK_Parameter_CF_Audited: |
| 5723 | if (Entity.getDecl() && |
| 5724 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 5725 | return Sema::AA_Sending; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5726 | |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 5727 | return !Diagnose ? Sema::AA_Passing : Sema::AA_Passing_CFAudited; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5728 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5729 | case InitializedEntity::EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 5730 | case InitializedEntity::EK_StmtExprResult: // FIXME: Not quite right. |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5731 | return Sema::AA_Returning; |
| 5732 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5733 | case InitializedEntity::EK_Temporary: |
Fariborz Jahanian | 14e9541 | 2013-07-11 19:13:34 +0000 | [diff] [blame] | 5734 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5735 | // FIXME: Can we tell apart casting vs. converting? |
| 5736 | return Sema::AA_Casting; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5737 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5738 | case InitializedEntity::EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 5739 | case InitializedEntity::EK_Binding: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 5740 | case InitializedEntity::EK_ArrayElement: |
| 5741 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 5742 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 5743 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 5744 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 5745 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5746 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5747 | return Sema::AA_Initializing; |
| 5748 | } |
| 5749 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 5750 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5751 | } |
| 5752 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5753 | /// Whether we should bind a created object as a temporary when |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5754 | /// initializing the given entity. |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5755 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5756 | switch (Entity.getKind()) { |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 5757 | case InitializedEntity::EK_ArrayElement: |
| 5758 | case InitializedEntity::EK_Member: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5759 | case InitializedEntity::EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 5760 | case InitializedEntity::EK_StmtExprResult: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5761 | case InitializedEntity::EK_New: |
| 5762 | case InitializedEntity::EK_Variable: |
| 5763 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 5764 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 5765 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 5766 | case InitializedEntity::EK_ComplexElement: |
Anders Carlsson | fcd764a | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 5767 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 5768 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 5769 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 5770 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5771 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5772 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5773 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5774 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5775 | case InitializedEntity::EK_Parameter_CF_Audited: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5776 | case InitializedEntity::EK_Temporary: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5777 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 5778 | case InitializedEntity::EK_Binding: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5779 | return true; |
| 5780 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5781 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5782 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 5783 | } |
| 5784 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5785 | /// Whether the given entity, when initialized with an object |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5786 | /// created for that initialization, requires destruction. |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5787 | static bool shouldDestroyEntity(const InitializedEntity &Entity) { |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5788 | switch (Entity.getKind()) { |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5789 | case InitializedEntity::EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 5790 | case InitializedEntity::EK_StmtExprResult: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5791 | case InitializedEntity::EK_New: |
| 5792 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 5793 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5794 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 5795 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 5796 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 5797 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 5798 | case InitializedEntity::EK_LambdaCapture: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5799 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5800 | |
Richard Smith | 27874d6 | 2013-01-08 00:08:23 +0000 | [diff] [blame] | 5801 | case InitializedEntity::EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 5802 | case InitializedEntity::EK_Binding: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5803 | case InitializedEntity::EK_Variable: |
| 5804 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5805 | case InitializedEntity::EK_Parameter_CF_Audited: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5806 | case InitializedEntity::EK_Temporary: |
| 5807 | case InitializedEntity::EK_ArrayElement: |
| 5808 | case InitializedEntity::EK_Exception: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5809 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5810 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5811 | return true; |
| 5812 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5813 | |
| 5814 | llvm_unreachable("missed an InitializedEntity kind?"); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5815 | } |
| 5816 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5817 | /// Get the location at which initialization diagnostics should appear. |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5818 | static SourceLocation getInitializationLoc(const InitializedEntity &Entity, |
| 5819 | Expr *Initializer) { |
| 5820 | switch (Entity.getKind()) { |
| 5821 | case InitializedEntity::EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 5822 | case InitializedEntity::EK_StmtExprResult: |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5823 | return Entity.getReturnLoc(); |
| 5824 | |
| 5825 | case InitializedEntity::EK_Exception: |
| 5826 | return Entity.getThrowLoc(); |
| 5827 | |
| 5828 | case InitializedEntity::EK_Variable: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 5829 | case InitializedEntity::EK_Binding: |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5830 | return Entity.getDecl()->getLocation(); |
| 5831 | |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 5832 | case InitializedEntity::EK_LambdaCapture: |
| 5833 | return Entity.getCaptureLoc(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5834 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5835 | case InitializedEntity::EK_ArrayElement: |
| 5836 | case InitializedEntity::EK_Member: |
| 5837 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5838 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5839 | case InitializedEntity::EK_Temporary: |
| 5840 | case InitializedEntity::EK_New: |
| 5841 | case InitializedEntity::EK_Base: |
| 5842 | case InitializedEntity::EK_Delegating: |
| 5843 | case InitializedEntity::EK_VectorElement: |
| 5844 | case InitializedEntity::EK_ComplexElement: |
| 5845 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 5846 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5847 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5848 | case InitializedEntity::EK_RelatedResult: |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5849 | return Initializer->getBeginLoc(); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5850 | } |
| 5851 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 5852 | } |
| 5853 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5854 | /// Make a (potentially elidable) temporary copy of the object |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5855 | /// provided by the given initializer by calling the appropriate copy |
| 5856 | /// constructor. |
| 5857 | /// |
| 5858 | /// \param S The Sema object used for type-checking. |
| 5859 | /// |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 5860 | /// \param T The type of the temporary object, which must either be |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5861 | /// the type of the initializer expression or a superclass thereof. |
| 5862 | /// |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5863 | /// \param Entity The entity being initialized. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5864 | /// |
| 5865 | /// \param CurInit The initializer expression. |
| 5866 | /// |
| 5867 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 5868 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 5869 | /// an rvalue. |
| 5870 | /// |
| 5871 | /// \returns An expression that copies the initializer expression into |
| 5872 | /// a temporary object, or an error expression if a copy could not be |
| 5873 | /// created. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5874 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 5875 | QualType T, |
| 5876 | const InitializedEntity &Entity, |
| 5877 | ExprResult CurInit, |
| 5878 | bool IsExtraneousCopy) { |
Fariborz Jahanian | 36f7f13 | 2015-01-28 22:08:10 +0000 | [diff] [blame] | 5879 | if (CurInit.isInvalid()) |
| 5880 | return CurInit; |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5881 | // Determine which class type we're copying to. |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 5882 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5883 | CXXRecordDecl *Class = nullptr; |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5884 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5885 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 5886 | if (!Class) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5887 | return CurInit; |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5888 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5889 | SourceLocation Loc = getInitializationLoc(Entity, CurInit.get()); |
Douglas Gregor | d5c231e | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 5890 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5891 | // Make sure that the type we are copying is complete. |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5892 | if (S.RequireCompleteType(Loc, T, diag::err_temp_copy_incomplete)) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5893 | return CurInit; |
Douglas Gregor | d5c231e | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 5894 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5895 | // Perform overload resolution using the class's constructors. Per |
| 5896 | // C++11 [dcl.init]p16, second bullet for class types, this initialization |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5897 | // is direct-initialization. |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 5898 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5899 | DeclContext::lookup_result Ctors = S.LookupConstructors(Class); |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5900 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5901 | OverloadCandidateSet::iterator Best; |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5902 | switch (ResolveConstructorOverload( |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 5903 | S, Loc, CurInitExpr, CandidateSet, T, Ctors, Best, |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5904 | /*CopyInitializing=*/false, /*AllowExplicit=*/true, |
| 5905 | /*OnlyListConstructors=*/false, /*IsListInit=*/false, |
| 5906 | /*SecondStepOfCopyInit=*/true)) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5907 | case OR_Success: |
| 5908 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5909 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5910 | case OR_No_Viable_Function: |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 5911 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 5912 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 5913 | : diag::err_temp_copy_no_viable) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 5914 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5915 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5916 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 5917 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5918 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5919 | return CurInit; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5920 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5921 | case OR_Ambiguous: |
| 5922 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 5923 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5924 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5925 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5926 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5927 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5928 | case OR_Deleted: |
| 5929 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 5930 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5931 | << CurInitExpr->getSourceRange(); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5932 | S.NoteDeletedFunction(Best->Function); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5933 | return ExprError(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5934 | } |
| 5935 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5936 | bool HadMultipleCandidates = CandidateSet.size() > 1; |
| 5937 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5938 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 5939 | SmallVector<Expr*, 8> ConstructorArgs; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5940 | CurInit.get(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5941 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 5942 | S.CheckConstructorAccess(Loc, Constructor, Best->FoundDecl, Entity, |
| 5943 | IsExtraneousCopy); |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5944 | |
| 5945 | if (IsExtraneousCopy) { |
| 5946 | // If this is a totally extraneous copy for C++03 reference |
| 5947 | // binding purposes, just return the original initialization |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 5948 | // expression. We don't generate an (elided) copy operation here |
| 5949 | // because doing so would require us to pass down a flag to avoid |
| 5950 | // infinite recursion, where each step adds another extraneous, |
| 5951 | // elidable copy. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5952 | |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 5953 | // Instantiate the default arguments of any extra parameters in |
| 5954 | // the selected copy constructor, as if we were going to create a |
| 5955 | // proper call to the copy constructor. |
| 5956 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 5957 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 5958 | if (S.RequireCompleteType(Loc, Parm->getType(), |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5959 | diag::err_call_incomplete_argument)) |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 5960 | break; |
| 5961 | |
| 5962 | // Build the default argument expression; we don't actually care |
| 5963 | // if this succeeds or not, because this routine will complain |
| 5964 | // if there was a problem. |
| 5965 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 5966 | } |
| 5967 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5968 | return CurInitExpr; |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5969 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5970 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5971 | // Determine the arguments required to actually perform the |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5972 | // constructor call (we might have derived-to-base conversions, or |
| 5973 | // the copy constructor may have default arguments). |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5974 | if (S.CompleteConstructorCall(Constructor, CurInitExpr, Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5975 | return ExprError(); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5976 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5977 | // C++0x [class.copy]p32: |
| 5978 | // When certain criteria are met, an implementation is allowed to |
| 5979 | // omit the copy/move construction of a class object, even if the |
| 5980 | // copy/move constructor and/or destructor for the object have |
| 5981 | // side effects. [...] |
| 5982 | // - when a temporary class object that has not been bound to a |
| 5983 | // reference (12.2) would be copied/moved to a class object |
| 5984 | // with the same cv-unqualified type, the copy/move operation |
| 5985 | // can be omitted by constructing the temporary object |
| 5986 | // directly into the target of the omitted copy/move |
| 5987 | // |
| 5988 | // Note that the other three bullets are handled elsewhere. Copy |
| 5989 | // elision for return statements and throw expressions are handled as part |
| 5990 | // of constructor initialization, while copy elision for exception handlers |
| 5991 | // is handled by the run-time. |
| 5992 | // |
| 5993 | // FIXME: If the function parameter is not the same type as the temporary, we |
| 5994 | // should still be able to elide the copy, but we don't have a way to |
| 5995 | // represent in the AST how much should be elided in this case. |
| 5996 | bool Elidable = |
| 5997 | CurInitExpr->isTemporaryObject(S.Context, Class) && |
| 5998 | S.Context.hasSameUnqualifiedType( |
| 5999 | Best->Function->getParamDecl(0)->getType().getNonReferenceType(), |
| 6000 | CurInitExpr->getType()); |
| 6001 | |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 6002 | // Actually perform the constructor call. |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 6003 | CurInit = S.BuildCXXConstructExpr(Loc, T, Best->FoundDecl, Constructor, |
| 6004 | Elidable, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6005 | ConstructorArgs, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 6006 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6007 | /*ListInit*/ false, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6008 | /*StdInitListInit*/ false, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 6009 | /*ZeroInit*/ false, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 6010 | CXXConstructExpr::CK_Complete, |
| 6011 | SourceRange()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6012 | |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 6013 | // If we're supposed to bind temporaries, do so. |
| 6014 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6015 | CurInit = S.MaybeBindToTemporary(CurInit.getAs<Expr>()); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6016 | return CurInit; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6017 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6018 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6019 | /// Check whether elidable copy construction for binding a reference to |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6020 | /// a temporary would have succeeded if we were building in C++98 mode, for |
| 6021 | /// -Wc++98-compat. |
| 6022 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 6023 | const InitializedEntity &Entity, |
| 6024 | Expr *CurInitExpr) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6025 | assert(S.getLangOpts().CPlusPlus11); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6026 | |
| 6027 | const RecordType *Record = CurInitExpr->getType()->getAs<RecordType>(); |
| 6028 | if (!Record) |
| 6029 | return; |
| 6030 | |
| 6031 | SourceLocation Loc = getInitializationLoc(Entity, CurInitExpr); |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 6032 | if (S.Diags.isIgnored(diag::warn_cxx98_compat_temp_copy, Loc)) |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6033 | return; |
| 6034 | |
| 6035 | // Find constructors which would have been considered. |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 6036 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 6037 | DeclContext::lookup_result Ctors = |
| 6038 | S.LookupConstructors(cast<CXXRecordDecl>(Record->getDecl())); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6039 | |
| 6040 | // Perform overload resolution. |
| 6041 | OverloadCandidateSet::iterator Best; |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 6042 | OverloadingResult OR = ResolveConstructorOverload( |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 6043 | S, Loc, CurInitExpr, CandidateSet, CurInitExpr->getType(), Ctors, Best, |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 6044 | /*CopyInitializing=*/false, /*AllowExplicit=*/true, |
| 6045 | /*OnlyListConstructors=*/false, /*IsListInit=*/false, |
| 6046 | /*SecondStepOfCopyInit=*/true); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6047 | |
| 6048 | PartialDiagnostic Diag = S.PDiag(diag::warn_cxx98_compat_temp_copy) |
| 6049 | << OR << (int)Entity.getKind() << CurInitExpr->getType() |
| 6050 | << CurInitExpr->getSourceRange(); |
| 6051 | |
| 6052 | switch (OR) { |
| 6053 | case OR_Success: |
| 6054 | S.CheckConstructorAccess(Loc, cast<CXXConstructorDecl>(Best->Function), |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6055 | Best->FoundDecl, Entity, Diag); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6056 | // FIXME: Check default arguments as far as that's possible. |
| 6057 | break; |
| 6058 | |
| 6059 | case OR_No_Viable_Function: |
| 6060 | S.Diag(Loc, Diag); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6061 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6062 | break; |
| 6063 | |
| 6064 | case OR_Ambiguous: |
| 6065 | S.Diag(Loc, Diag); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6066 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6067 | break; |
| 6068 | |
| 6069 | case OR_Deleted: |
| 6070 | S.Diag(Loc, Diag); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6071 | S.NoteDeletedFunction(Best->Function); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6072 | break; |
| 6073 | } |
| 6074 | } |
| 6075 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6076 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 6077 | const InitializedEntity &Entity) { |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6078 | if (Entity.isParameterKind() && Entity.getDecl()) { |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6079 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 6080 | return; |
| 6081 | |
| 6082 | if (Entity.getDecl()->getDeclName()) |
| 6083 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 6084 | << Entity.getDecl()->getDeclName(); |
| 6085 | else |
| 6086 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 6087 | } |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 6088 | else if (Entity.getKind() == InitializedEntity::EK_RelatedResult && |
| 6089 | Entity.getMethodDecl()) |
| 6090 | S.Diag(Entity.getMethodDecl()->getLocation(), |
| 6091 | diag::note_method_return_type_change) |
| 6092 | << Entity.getMethodDecl()->getDeclName(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6093 | } |
| 6094 | |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 6095 | /// Returns true if the parameters describe a constructor initialization of |
| 6096 | /// an explicit temporary object, e.g. "Point(x, y)". |
| 6097 | static bool isExplicitTemporary(const InitializedEntity &Entity, |
| 6098 | const InitializationKind &Kind, |
| 6099 | unsigned NumArgs) { |
| 6100 | switch (Entity.getKind()) { |
| 6101 | case InitializedEntity::EK_Temporary: |
| 6102 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 6103 | case InitializedEntity::EK_RelatedResult: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 6104 | break; |
| 6105 | default: |
| 6106 | return false; |
| 6107 | } |
| 6108 | |
| 6109 | switch (Kind.getKind()) { |
| 6110 | case InitializationKind::IK_DirectList: |
| 6111 | return true; |
| 6112 | // FIXME: Hack to work around cast weirdness. |
| 6113 | case InitializationKind::IK_Direct: |
| 6114 | case InitializationKind::IK_Value: |
| 6115 | return NumArgs != 1; |
| 6116 | default: |
| 6117 | return false; |
| 6118 | } |
| 6119 | } |
| 6120 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6121 | static ExprResult |
| 6122 | PerformConstructorInitialization(Sema &S, |
| 6123 | const InitializedEntity &Entity, |
| 6124 | const InitializationKind &Kind, |
| 6125 | MultiExprArg Args, |
| 6126 | const InitializationSequence::Step& Step, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6127 | bool &ConstructorInitRequiresZeroInit, |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 6128 | bool IsListInitialization, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6129 | bool IsStdInitListInitialization, |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 6130 | SourceLocation LBraceLoc, |
| 6131 | SourceLocation RBraceLoc) { |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6132 | unsigned NumArgs = Args.size(); |
| 6133 | CXXConstructorDecl *Constructor |
| 6134 | = cast<CXXConstructorDecl>(Step.Function.Function); |
| 6135 | bool HadMultipleCandidates = Step.Function.HadMultipleCandidates; |
| 6136 | |
| 6137 | // Build a call to the selected constructor. |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 6138 | SmallVector<Expr*, 8> ConstructorArgs; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6139 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 6140 | ? Kind.getEqualLoc() |
| 6141 | : Kind.getLocation(); |
| 6142 | |
| 6143 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 6144 | // Force even a trivial, implicit default constructor to be |
| 6145 | // semantically checked. We do this explicitly because we don't build |
| 6146 | // the definition for completely trivial constructors. |
Matt Beaumont-Gay | 47ff122 | 2012-02-24 08:37:56 +0000 | [diff] [blame] | 6147 | assert(Constructor->getParent() && "No parent class for constructor."); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6148 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Douglas Gregor | f704ade | 2012-02-24 07:48:37 +0000 | [diff] [blame] | 6149 | Constructor->isTrivial() && !Constructor->isUsed(false)) |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6150 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 6151 | } |
| 6152 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6153 | ExprResult CurInit((Expr *)nullptr); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6154 | |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6155 | // C++ [over.match.copy]p1: |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6156 | // - When initializing a temporary to be bound to the first parameter |
| 6157 | // of a constructor that takes a reference to possibly cv-qualified |
| 6158 | // T as its first argument, called with a single argument in the |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6159 | // context of direct-initialization, explicit conversion functions |
| 6160 | // are also considered. |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 6161 | bool AllowExplicitConv = |
| 6162 | Kind.AllowExplicit() && !Kind.isCopyInit() && Args.size() == 1 && |
| 6163 | hasCopyOrMoveCtorParam(S.Context, |
| 6164 | getConstructorInfo(Step.Function.FoundDecl)); |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6165 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6166 | // Determine the arguments required to actually perform the constructor |
| 6167 | // call. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6168 | if (S.CompleteConstructorCall(Constructor, Args, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6169 | Loc, ConstructorArgs, |
Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 6170 | AllowExplicitConv, |
| 6171 | IsListInitialization)) |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6172 | return ExprError(); |
| 6173 | |
| 6174 | |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 6175 | if (isExplicitTemporary(Entity, Kind, NumArgs)) { |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6176 | // An explicitly-constructed temporary, e.g., X(1, 2). |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 6177 | if (S.DiagnoseUseOfDecl(Constructor, Loc)) |
| 6178 | return ExprError(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6179 | |
| 6180 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 6181 | if (!TSInfo) |
| 6182 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 6183 | SourceRange ParenOrBraceRange = Kind.getParenOrBraceRange(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6184 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6185 | if (auto *Shadow = dyn_cast<ConstructorUsingShadowDecl>( |
Richard Smith | 80a4702 | 2016-06-29 01:10:27 +0000 | [diff] [blame] | 6186 | Step.Function.FoundDecl.getDecl())) { |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6187 | Constructor = S.findInheritingConstructor(Loc, Constructor, Shadow); |
Richard Smith | 80a4702 | 2016-06-29 01:10:27 +0000 | [diff] [blame] | 6188 | if (S.DiagnoseUseOfDecl(Constructor, Loc)) |
| 6189 | return ExprError(); |
| 6190 | } |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6191 | S.MarkFunctionReferenced(Loc, Constructor); |
| 6192 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6193 | CurInit = new (S.Context) CXXTemporaryObjectExpr( |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 6194 | S.Context, Constructor, |
| 6195 | Entity.getType().getNonLValueExprType(S.Context), TSInfo, |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 6196 | ConstructorArgs, ParenOrBraceRange, HadMultipleCandidates, |
| 6197 | IsListInitialization, IsStdInitListInitialization, |
| 6198 | ConstructorInitRequiresZeroInit); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6199 | } else { |
| 6200 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 6201 | CXXConstructExpr::CK_Complete; |
| 6202 | |
| 6203 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 6204 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
| 6205 | CXXConstructExpr::CK_VirtualBase : |
| 6206 | CXXConstructExpr::CK_NonVirtualBase; |
| 6207 | } else if (Entity.getKind() == InitializedEntity::EK_Delegating) { |
| 6208 | ConstructKind = CXXConstructExpr::CK_Delegating; |
| 6209 | } |
| 6210 | |
Peter Collingbourne | b8d17e7 | 2014-02-22 02:59:41 +0000 | [diff] [blame] | 6211 | // Only get the parenthesis or brace range if it is a list initialization or |
| 6212 | // direct construction. |
| 6213 | SourceRange ParenOrBraceRange; |
| 6214 | if (IsListInitialization) |
| 6215 | ParenOrBraceRange = SourceRange(LBraceLoc, RBraceLoc); |
| 6216 | else if (Kind.getKind() == InitializationKind::IK_Direct) |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 6217 | ParenOrBraceRange = Kind.getParenOrBraceRange(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6218 | |
| 6219 | // If the entity allows NRVO, mark the construction as elidable |
| 6220 | // unconditionally. |
| 6221 | if (Entity.allowsNRVO()) |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 6222 | CurInit = S.BuildCXXConstructExpr(Loc, Step.Type, |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 6223 | Step.Function.FoundDecl, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6224 | Constructor, /*Elidable=*/true, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6225 | ConstructorArgs, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6226 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6227 | IsListInitialization, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6228 | IsStdInitListInitialization, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6229 | ConstructorInitRequiresZeroInit, |
| 6230 | ConstructKind, |
Peter Collingbourne | b8d17e7 | 2014-02-22 02:59:41 +0000 | [diff] [blame] | 6231 | ParenOrBraceRange); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6232 | else |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 6233 | CurInit = S.BuildCXXConstructExpr(Loc, Step.Type, |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 6234 | Step.Function.FoundDecl, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6235 | Constructor, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6236 | ConstructorArgs, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6237 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6238 | IsListInitialization, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6239 | IsStdInitListInitialization, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6240 | ConstructorInitRequiresZeroInit, |
| 6241 | ConstructKind, |
Peter Collingbourne | b8d17e7 | 2014-02-22 02:59:41 +0000 | [diff] [blame] | 6242 | ParenOrBraceRange); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6243 | } |
| 6244 | if (CurInit.isInvalid()) |
| 6245 | return ExprError(); |
| 6246 | |
| 6247 | // Only check access if all of that succeeded. |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6248 | S.CheckConstructorAccess(Loc, Constructor, Step.Function.FoundDecl, Entity); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 6249 | if (S.DiagnoseUseOfDecl(Step.Function.FoundDecl, Loc)) |
| 6250 | return ExprError(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6251 | |
| 6252 | if (shouldBindAsTemporary(Entity)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6253 | CurInit = S.MaybeBindToTemporary(CurInit.get()); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6254 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6255 | return CurInit; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6256 | } |
| 6257 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6258 | namespace { |
| 6259 | enum LifetimeKind { |
| 6260 | /// The lifetime of a temporary bound to this entity ends at the end of the |
| 6261 | /// full-expression, and that's (probably) fine. |
| 6262 | LK_FullExpression, |
| 6263 | |
| 6264 | /// The lifetime of a temporary bound to this entity is extended to the |
| 6265 | /// lifeitme of the entity itself. |
| 6266 | LK_Extended, |
| 6267 | |
| 6268 | /// The lifetime of a temporary bound to this entity probably ends too soon, |
| 6269 | /// because the entity is allocated in a new-expression. |
| 6270 | LK_New, |
| 6271 | |
| 6272 | /// The lifetime of a temporary bound to this entity ends too soon, because |
| 6273 | /// the entity is a return object. |
| 6274 | LK_Return, |
| 6275 | |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 6276 | /// The lifetime of a temporary bound to this entity ends too soon, because |
| 6277 | /// the entity is the result of a statement expression. |
| 6278 | LK_StmtExprResult, |
| 6279 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6280 | /// This is a mem-initializer: if it would extend a temporary (other than via |
| 6281 | /// a default member initializer), the program is ill-formed. |
| 6282 | LK_MemInitializer, |
| 6283 | }; |
| 6284 | using LifetimeResult = |
| 6285 | llvm::PointerIntPair<const InitializedEntity *, 3, LifetimeKind>; |
| 6286 | } |
| 6287 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6288 | /// Determine the declaration which an initialized entity ultimately refers to, |
| 6289 | /// for the purpose of lifetime-extending a temporary bound to a reference in |
| 6290 | /// the initialization of \p Entity. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6291 | static LifetimeResult getEntityLifetime( |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 6292 | const InitializedEntity *Entity, |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6293 | const InitializedEntity *InitField = nullptr) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6294 | // C++11 [class.temporary]p5: |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 6295 | switch (Entity->getKind()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6296 | case InitializedEntity::EK_Variable: |
| 6297 | // The temporary [...] persists for the lifetime of the reference |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6298 | return {Entity, LK_Extended}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6299 | |
| 6300 | case InitializedEntity::EK_Member: |
| 6301 | // For subobjects, we look at the complete object. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 6302 | if (Entity->getParent()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6303 | return getEntityLifetime(Entity->getParent(), Entity); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6304 | |
| 6305 | // except: |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6306 | // C++17 [class.base.init]p8: |
| 6307 | // A temporary expression bound to a reference member in a |
| 6308 | // mem-initializer is ill-formed. |
| 6309 | // C++17 [class.base.init]p11: |
| 6310 | // A temporary expression bound to a reference member from a |
| 6311 | // default member initializer is ill-formed. |
| 6312 | // |
| 6313 | // The context of p11 and its example suggest that it's only the use of a |
| 6314 | // default member initializer from a constructor that makes the program |
| 6315 | // ill-formed, not its mere existence, and that it can even be used by |
| 6316 | // aggregate initialization. |
| 6317 | return {Entity, Entity->isDefaultMemberInitializer() ? LK_Extended |
| 6318 | : LK_MemInitializer}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6319 | |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 6320 | case InitializedEntity::EK_Binding: |
Richard Smith | 3997b1b | 2016-08-12 01:55:21 +0000 | [diff] [blame] | 6321 | // Per [dcl.decomp]p3, the binding is treated as a variable of reference |
| 6322 | // type. |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6323 | return {Entity, LK_Extended}; |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 6324 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6325 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6326 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6327 | // -- A temporary bound to a reference parameter in a function call |
| 6328 | // persists until the completion of the full-expression containing |
| 6329 | // the call. |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6330 | return {nullptr, LK_FullExpression}; |
| 6331 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6332 | case InitializedEntity::EK_Result: |
| 6333 | // -- The lifetime of a temporary bound to the returned value in a |
| 6334 | // function return statement is not extended; the temporary is |
| 6335 | // destroyed at the end of the full-expression in the return statement. |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6336 | return {nullptr, LK_Return}; |
| 6337 | |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 6338 | case InitializedEntity::EK_StmtExprResult: |
| 6339 | // FIXME: Should we lifetime-extend through the result of a statement |
| 6340 | // expression? |
| 6341 | return {nullptr, LK_StmtExprResult}; |
| 6342 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6343 | case InitializedEntity::EK_New: |
| 6344 | // -- A temporary bound to a reference in a new-initializer persists |
| 6345 | // until the completion of the full-expression containing the |
| 6346 | // new-initializer. |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6347 | return {nullptr, LK_New}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6348 | |
| 6349 | case InitializedEntity::EK_Temporary: |
| 6350 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 6351 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6352 | // We don't yet know the storage duration of the surrounding temporary. |
| 6353 | // Assume it's got full-expression duration for now, it will patch up our |
| 6354 | // storage duration if that's not correct. |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6355 | return {nullptr, LK_FullExpression}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6356 | |
| 6357 | case InitializedEntity::EK_ArrayElement: |
| 6358 | // For subobjects, we look at the complete object. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6359 | return getEntityLifetime(Entity->getParent(), InitField); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6360 | |
| 6361 | case InitializedEntity::EK_Base: |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 6362 | // For subobjects, we look at the complete object. |
| 6363 | if (Entity->getParent()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6364 | return getEntityLifetime(Entity->getParent(), InitField); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6365 | return {InitField, LK_MemInitializer}; |
| 6366 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6367 | case InitializedEntity::EK_Delegating: |
| 6368 | // We can reach this case for aggregate initialization in a constructor: |
| 6369 | // struct A { int &&r; }; |
| 6370 | // struct B : A { B() : A{0} {} }; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6371 | // In this case, use the outermost field decl as the context. |
| 6372 | return {InitField, LK_MemInitializer}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6373 | |
| 6374 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 6375 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6376 | case InitializedEntity::EK_LambdaCapture: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6377 | case InitializedEntity::EK_VectorElement: |
| 6378 | case InitializedEntity::EK_ComplexElement: |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6379 | return {nullptr, LK_FullExpression}; |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6380 | |
| 6381 | case InitializedEntity::EK_Exception: |
| 6382 | // FIXME: Can we diagnose lifetime problems with exceptions? |
| 6383 | return {nullptr, LK_FullExpression}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6384 | } |
Benjamin Kramer | cabc882 | 2013-06-05 15:37:50 +0000 | [diff] [blame] | 6385 | llvm_unreachable("unknown entity kind"); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6386 | } |
| 6387 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6388 | namespace { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6389 | enum ReferenceKind { |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6390 | /// Lifetime would be extended by a reference binding to a temporary. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6391 | RK_ReferenceBinding, |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6392 | /// Lifetime would be extended by a std::initializer_list object binding to |
| 6393 | /// its backing array. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6394 | RK_StdInitializerList, |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6395 | }; |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6396 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6397 | /// A temporary or local variable. This will be one of: |
| 6398 | /// * A MaterializeTemporaryExpr. |
| 6399 | /// * A DeclRefExpr whose declaration is a local. |
| 6400 | /// * An AddrLabelExpr. |
| 6401 | /// * A BlockExpr for a block with captures. |
| 6402 | using Local = Expr*; |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6403 | |
| 6404 | /// Expressions we stepped over when looking for the local state. Any steps |
| 6405 | /// that would inhibit lifetime extension or take us out of subexpressions of |
| 6406 | /// the initializer are included. |
| 6407 | struct IndirectLocalPathEntry { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6408 | enum EntryKind { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6409 | DefaultInit, |
| 6410 | AddressOf, |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6411 | VarInit, |
| 6412 | LValToRVal, |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6413 | LifetimeBoundCall, |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6414 | } Kind; |
| 6415 | Expr *E; |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6416 | const Decl *D = nullptr; |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6417 | IndirectLocalPathEntry() {} |
| 6418 | IndirectLocalPathEntry(EntryKind K, Expr *E) : Kind(K), E(E) {} |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6419 | IndirectLocalPathEntry(EntryKind K, Expr *E, const Decl *D) |
| 6420 | : Kind(K), E(E), D(D) {} |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6421 | }; |
| 6422 | |
| 6423 | using IndirectLocalPath = llvm::SmallVectorImpl<IndirectLocalPathEntry>; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6424 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6425 | struct RevertToOldSizeRAII { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6426 | IndirectLocalPath &Path; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6427 | unsigned OldSize = Path.size(); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6428 | RevertToOldSizeRAII(IndirectLocalPath &Path) : Path(Path) {} |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6429 | ~RevertToOldSizeRAII() { Path.resize(OldSize); } |
| 6430 | }; |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6431 | |
| 6432 | using LocalVisitor = llvm::function_ref<bool(IndirectLocalPath &Path, Local L, |
| 6433 | ReferenceKind RK)>; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6434 | } |
| 6435 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6436 | static bool isVarOnPath(IndirectLocalPath &Path, VarDecl *VD) { |
| 6437 | for (auto E : Path) |
| 6438 | if (E.Kind == IndirectLocalPathEntry::VarInit && E.D == VD) |
| 6439 | return true; |
| 6440 | return false; |
| 6441 | } |
| 6442 | |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6443 | static bool pathContainsInit(IndirectLocalPath &Path) { |
| 6444 | return std::any_of(Path.begin(), Path.end(), [=](IndirectLocalPathEntry E) { |
| 6445 | return E.Kind == IndirectLocalPathEntry::DefaultInit || |
| 6446 | E.Kind == IndirectLocalPathEntry::VarInit; |
| 6447 | }); |
| 6448 | } |
| 6449 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6450 | static void visitLocalsRetainedByInitializer(IndirectLocalPath &Path, |
| 6451 | Expr *Init, LocalVisitor Visit, |
| 6452 | bool RevisitSubinits); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6453 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6454 | static void visitLocalsRetainedByReferenceBinding(IndirectLocalPath &Path, |
| 6455 | Expr *Init, ReferenceKind RK, |
| 6456 | LocalVisitor Visit); |
| 6457 | |
| 6458 | static bool implicitObjectParamIsLifetimeBound(const FunctionDecl *FD) { |
| 6459 | const TypeSourceInfo *TSI = FD->getTypeSourceInfo(); |
| 6460 | if (!TSI) |
| 6461 | return false; |
Martin Storsjo | d03fa99 | 2018-08-02 18:12:08 +0000 | [diff] [blame] | 6462 | // Don't declare this variable in the second operand of the for-statement; |
| 6463 | // GCC miscompiles that by ending its lifetime before evaluating the |
| 6464 | // third operand. See gcc.gnu.org/PR86769. |
| 6465 | AttributedTypeLoc ATL; |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6466 | for (TypeLoc TL = TSI->getTypeLoc(); |
Martin Storsjo | d03fa99 | 2018-08-02 18:12:08 +0000 | [diff] [blame] | 6467 | (ATL = TL.getAsAdjusted<AttributedTypeLoc>()); |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6468 | TL = ATL.getModifiedLoc()) { |
Richard Smith | e43e2b3 | 2018-08-20 21:47:29 +0000 | [diff] [blame] | 6469 | if (ATL.getAttrAs<LifetimeBoundAttr>()) |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6470 | return true; |
| 6471 | } |
| 6472 | return false; |
| 6473 | } |
| 6474 | |
| 6475 | static void visitLifetimeBoundArguments(IndirectLocalPath &Path, Expr *Call, |
| 6476 | LocalVisitor Visit) { |
| 6477 | const FunctionDecl *Callee; |
| 6478 | ArrayRef<Expr*> Args; |
| 6479 | |
| 6480 | if (auto *CE = dyn_cast<CallExpr>(Call)) { |
| 6481 | Callee = CE->getDirectCallee(); |
| 6482 | Args = llvm::makeArrayRef(CE->getArgs(), CE->getNumArgs()); |
| 6483 | } else { |
| 6484 | auto *CCE = cast<CXXConstructExpr>(Call); |
| 6485 | Callee = CCE->getConstructor(); |
| 6486 | Args = llvm::makeArrayRef(CCE->getArgs(), CCE->getNumArgs()); |
| 6487 | } |
| 6488 | if (!Callee) |
| 6489 | return; |
| 6490 | |
| 6491 | Expr *ObjectArg = nullptr; |
| 6492 | if (isa<CXXOperatorCallExpr>(Call) && Callee->isCXXInstanceMember()) { |
| 6493 | ObjectArg = Args[0]; |
| 6494 | Args = Args.slice(1); |
| 6495 | } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(Call)) { |
| 6496 | ObjectArg = MCE->getImplicitObjectArgument(); |
| 6497 | } |
| 6498 | |
| 6499 | auto VisitLifetimeBoundArg = [&](const Decl *D, Expr *Arg) { |
| 6500 | Path.push_back({IndirectLocalPathEntry::LifetimeBoundCall, Arg, D}); |
| 6501 | if (Arg->isGLValue()) |
| 6502 | visitLocalsRetainedByReferenceBinding(Path, Arg, RK_ReferenceBinding, |
| 6503 | Visit); |
| 6504 | else |
| 6505 | visitLocalsRetainedByInitializer(Path, Arg, Visit, true); |
| 6506 | Path.pop_back(); |
| 6507 | }; |
| 6508 | |
| 6509 | if (ObjectArg && implicitObjectParamIsLifetimeBound(Callee)) |
| 6510 | VisitLifetimeBoundArg(Callee, ObjectArg); |
| 6511 | |
| 6512 | for (unsigned I = 0, |
| 6513 | N = std::min<unsigned>(Callee->getNumParams(), Args.size()); |
| 6514 | I != N; ++I) { |
| 6515 | if (Callee->getParamDecl(I)->hasAttr<LifetimeBoundAttr>()) |
| 6516 | VisitLifetimeBoundArg(Callee->getParamDecl(I), Args[I]); |
| 6517 | } |
| 6518 | } |
| 6519 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6520 | /// Visit the locals that would be reachable through a reference bound to the |
| 6521 | /// glvalue expression \c Init. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6522 | static void visitLocalsRetainedByReferenceBinding(IndirectLocalPath &Path, |
| 6523 | Expr *Init, ReferenceKind RK, |
| 6524 | LocalVisitor Visit) { |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6525 | RevertToOldSizeRAII RAII(Path); |
| 6526 | |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 6527 | // Walk past any constructs which we can lifetime-extend across. |
| 6528 | Expr *Old; |
| 6529 | do { |
| 6530 | Old = Init; |
| 6531 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6532 | if (auto *EWC = dyn_cast<ExprWithCleanups>(Init)) |
| 6533 | Init = EWC->getSubExpr(); |
| 6534 | |
Richard Smith | dbc8249 | 2015-01-10 01:28:13 +0000 | [diff] [blame] | 6535 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6536 | // If this is just redundant braces around an initializer, step over it. |
| 6537 | if (ILE->isTransparent()) |
Richard Smith | dbc8249 | 2015-01-10 01:28:13 +0000 | [diff] [blame] | 6538 | Init = ILE->getInit(0); |
Richard Smith | dbc8249 | 2015-01-10 01:28:13 +0000 | [diff] [blame] | 6539 | } |
| 6540 | |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 6541 | // Step over any subobject adjustments; we may have a materialized |
| 6542 | // temporary inside them. |
Richard Smith | 4baaa5a | 2016-12-03 01:14:32 +0000 | [diff] [blame] | 6543 | Init = const_cast<Expr *>(Init->skipRValueSubobjectAdjustments()); |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 6544 | |
| 6545 | // Per current approach for DR1376, look through casts to reference type |
| 6546 | // when performing lifetime extension. |
| 6547 | if (CastExpr *CE = dyn_cast<CastExpr>(Init)) |
| 6548 | if (CE->getSubExpr()->isGLValue()) |
| 6549 | Init = CE->getSubExpr(); |
| 6550 | |
Richard Smith | b3189a1 | 2016-12-05 07:49:14 +0000 | [diff] [blame] | 6551 | // Per the current approach for DR1299, look through array element access |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6552 | // on array glvalues when performing lifetime extension. |
| 6553 | if (auto *ASE = dyn_cast<ArraySubscriptExpr>(Init)) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6554 | Init = ASE->getBase(); |
| 6555 | auto *ICE = dyn_cast<ImplicitCastExpr>(Init); |
| 6556 | if (ICE && ICE->getCastKind() == CK_ArrayToPointerDecay) |
| 6557 | Init = ICE->getSubExpr(); |
| 6558 | else |
| 6559 | // We can't lifetime extend through this but we might still find some |
| 6560 | // retained temporaries. |
| 6561 | return visitLocalsRetainedByInitializer(Path, Init, Visit, true); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6562 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6563 | |
| 6564 | // Step into CXXDefaultInitExprs so we can diagnose cases where a |
| 6565 | // constructor inherits one as an implicit mem-initializer. |
| 6566 | if (auto *DIE = dyn_cast<CXXDefaultInitExpr>(Init)) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6567 | Path.push_back( |
| 6568 | {IndirectLocalPathEntry::DefaultInit, DIE, DIE->getField()}); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6569 | Init = DIE->getExpr(); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6570 | } |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 6571 | } while (Init != Old); |
| 6572 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6573 | if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(Init)) { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6574 | if (Visit(Path, Local(MTE), RK)) |
| 6575 | visitLocalsRetainedByInitializer(Path, MTE->GetTemporaryExpr(), Visit, |
| 6576 | true); |
| 6577 | } |
| 6578 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6579 | if (isa<CallExpr>(Init)) |
| 6580 | return visitLifetimeBoundArguments(Path, Init, Visit); |
| 6581 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6582 | switch (Init->getStmtClass()) { |
| 6583 | case Stmt::DeclRefExprClass: { |
| 6584 | // If we find the name of a local non-reference parameter, we could have a |
| 6585 | // lifetime problem. |
| 6586 | auto *DRE = cast<DeclRefExpr>(Init); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6587 | auto *VD = dyn_cast<VarDecl>(DRE->getDecl()); |
| 6588 | if (VD && VD->hasLocalStorage() && |
| 6589 | !DRE->refersToEnclosingVariableOrCapture()) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6590 | if (!VD->getType()->isReferenceType()) { |
| 6591 | Visit(Path, Local(DRE), RK); |
| 6592 | } else if (isa<ParmVarDecl>(DRE->getDecl())) { |
| 6593 | // The lifetime of a reference parameter is unknown; assume it's OK |
| 6594 | // for now. |
| 6595 | break; |
| 6596 | } else if (VD->getInit() && !isVarOnPath(Path, VD)) { |
| 6597 | Path.push_back({IndirectLocalPathEntry::VarInit, DRE, VD}); |
| 6598 | visitLocalsRetainedByReferenceBinding(Path, VD->getInit(), |
| 6599 | RK_ReferenceBinding, Visit); |
| 6600 | } |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6601 | } |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6602 | break; |
| 6603 | } |
| 6604 | |
| 6605 | case Stmt::UnaryOperatorClass: { |
| 6606 | // The only unary operator that make sense to handle here |
| 6607 | // is Deref. All others don't resolve to a "name." This includes |
| 6608 | // handling all sorts of rvalues passed to a unary operator. |
| 6609 | const UnaryOperator *U = cast<UnaryOperator>(Init); |
| 6610 | if (U->getOpcode() == UO_Deref) |
| 6611 | visitLocalsRetainedByInitializer(Path, U->getSubExpr(), Visit, true); |
| 6612 | break; |
| 6613 | } |
| 6614 | |
| 6615 | case Stmt::OMPArraySectionExprClass: { |
| 6616 | visitLocalsRetainedByInitializer( |
| 6617 | Path, cast<OMPArraySectionExpr>(Init)->getBase(), Visit, true); |
| 6618 | break; |
| 6619 | } |
| 6620 | |
| 6621 | case Stmt::ConditionalOperatorClass: |
| 6622 | case Stmt::BinaryConditionalOperatorClass: { |
| 6623 | auto *C = cast<AbstractConditionalOperator>(Init); |
| 6624 | if (!C->getTrueExpr()->getType()->isVoidType()) |
| 6625 | visitLocalsRetainedByReferenceBinding(Path, C->getTrueExpr(), RK, Visit); |
| 6626 | if (!C->getFalseExpr()->getType()->isVoidType()) |
| 6627 | visitLocalsRetainedByReferenceBinding(Path, C->getFalseExpr(), RK, Visit); |
| 6628 | break; |
| 6629 | } |
| 6630 | |
| 6631 | // FIXME: Visit the left-hand side of an -> or ->*. |
| 6632 | |
| 6633 | default: |
| 6634 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6635 | } |
| 6636 | } |
| 6637 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6638 | /// Visit the locals that would be reachable through an object initialized by |
| 6639 | /// the prvalue expression \c Init. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6640 | static void visitLocalsRetainedByInitializer(IndirectLocalPath &Path, |
| 6641 | Expr *Init, LocalVisitor Visit, |
| 6642 | bool RevisitSubinits) { |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6643 | RevertToOldSizeRAII RAII(Path); |
| 6644 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6645 | Expr *Old; |
| 6646 | do { |
| 6647 | Old = Init; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6648 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6649 | // Step into CXXDefaultInitExprs so we can diagnose cases where a |
| 6650 | // constructor inherits one as an implicit mem-initializer. |
| 6651 | if (auto *DIE = dyn_cast<CXXDefaultInitExpr>(Init)) { |
| 6652 | Path.push_back({IndirectLocalPathEntry::DefaultInit, DIE, DIE->getField()}); |
| 6653 | Init = DIE->getExpr(); |
| 6654 | } |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6655 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6656 | if (auto *EWC = dyn_cast<ExprWithCleanups>(Init)) |
| 6657 | Init = EWC->getSubExpr(); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6658 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6659 | // Dig out the expression which constructs the extended temporary. |
| 6660 | Init = const_cast<Expr *>(Init->skipRValueSubobjectAdjustments()); |
| 6661 | |
| 6662 | if (CXXBindTemporaryExpr *BTE = dyn_cast<CXXBindTemporaryExpr>(Init)) |
| 6663 | Init = BTE->getSubExpr(); |
| 6664 | |
| 6665 | Init = Init->IgnoreParens(); |
| 6666 | |
| 6667 | // Step over value-preserving rvalue casts. |
| 6668 | if (auto *CE = dyn_cast<CastExpr>(Init)) { |
| 6669 | switch (CE->getCastKind()) { |
| 6670 | case CK_LValueToRValue: |
| 6671 | // If we can match the lvalue to a const object, we can look at its |
| 6672 | // initializer. |
| 6673 | Path.push_back({IndirectLocalPathEntry::LValToRVal, CE}); |
| 6674 | return visitLocalsRetainedByReferenceBinding( |
| 6675 | Path, Init, RK_ReferenceBinding, |
| 6676 | [&](IndirectLocalPath &Path, Local L, ReferenceKind RK) -> bool { |
| 6677 | if (auto *DRE = dyn_cast<DeclRefExpr>(L)) { |
| 6678 | auto *VD = dyn_cast<VarDecl>(DRE->getDecl()); |
| 6679 | if (VD && VD->getType().isConstQualified() && VD->getInit() && |
| 6680 | !isVarOnPath(Path, VD)) { |
| 6681 | Path.push_back({IndirectLocalPathEntry::VarInit, DRE, VD}); |
| 6682 | visitLocalsRetainedByInitializer(Path, VD->getInit(), Visit, true); |
| 6683 | } |
| 6684 | } else if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(L)) { |
| 6685 | if (MTE->getType().isConstQualified()) |
| 6686 | visitLocalsRetainedByInitializer(Path, MTE->GetTemporaryExpr(), |
| 6687 | Visit, true); |
| 6688 | } |
| 6689 | return false; |
| 6690 | }); |
| 6691 | |
| 6692 | // We assume that objects can be retained by pointers cast to integers, |
| 6693 | // but not if the integer is cast to floating-point type or to _Complex. |
| 6694 | // We assume that casts to 'bool' do not preserve enough information to |
| 6695 | // retain a local object. |
| 6696 | case CK_NoOp: |
| 6697 | case CK_BitCast: |
| 6698 | case CK_BaseToDerived: |
| 6699 | case CK_DerivedToBase: |
| 6700 | case CK_UncheckedDerivedToBase: |
| 6701 | case CK_Dynamic: |
| 6702 | case CK_ToUnion: |
| 6703 | case CK_UserDefinedConversion: |
| 6704 | case CK_ConstructorConversion: |
| 6705 | case CK_IntegralToPointer: |
| 6706 | case CK_PointerToIntegral: |
| 6707 | case CK_VectorSplat: |
| 6708 | case CK_IntegralCast: |
| 6709 | case CK_CPointerToObjCPointerCast: |
| 6710 | case CK_BlockPointerToObjCPointerCast: |
| 6711 | case CK_AnyPointerToBlockPointerCast: |
| 6712 | case CK_AddressSpaceConversion: |
| 6713 | break; |
| 6714 | |
| 6715 | case CK_ArrayToPointerDecay: |
| 6716 | // Model array-to-pointer decay as taking the address of the array |
| 6717 | // lvalue. |
| 6718 | Path.push_back({IndirectLocalPathEntry::AddressOf, CE}); |
| 6719 | return visitLocalsRetainedByReferenceBinding(Path, CE->getSubExpr(), |
| 6720 | RK_ReferenceBinding, Visit); |
| 6721 | |
| 6722 | default: |
| 6723 | return; |
| 6724 | } |
| 6725 | |
| 6726 | Init = CE->getSubExpr(); |
| 6727 | } |
| 6728 | } while (Old != Init); |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 6729 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6730 | // C++17 [dcl.init.list]p6: |
| 6731 | // initializing an initializer_list object from the array extends the |
| 6732 | // lifetime of the array exactly like binding a reference to a temporary. |
| 6733 | if (auto *ILE = dyn_cast<CXXStdInitializerListExpr>(Init)) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6734 | return visitLocalsRetainedByReferenceBinding(Path, ILE->getSubExpr(), |
| 6735 | RK_StdInitializerList, Visit); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6736 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6737 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6738 | // We already visited the elements of this initializer list while |
| 6739 | // performing the initialization. Don't visit them again unless we've |
| 6740 | // changed the lifetime of the initialized entity. |
| 6741 | if (!RevisitSubinits) |
| 6742 | return; |
| 6743 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6744 | if (ILE->isTransparent()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6745 | return visitLocalsRetainedByInitializer(Path, ILE->getInit(0), Visit, |
| 6746 | RevisitSubinits); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6747 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6748 | if (ILE->getType()->isArrayType()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6749 | for (unsigned I = 0, N = ILE->getNumInits(); I != N; ++I) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6750 | visitLocalsRetainedByInitializer(Path, ILE->getInit(I), Visit, |
| 6751 | RevisitSubinits); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6752 | return; |
| 6753 | } |
| 6754 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6755 | if (CXXRecordDecl *RD = ILE->getType()->getAsCXXRecordDecl()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6756 | assert(RD->isAggregate() && "aggregate init on non-aggregate"); |
| 6757 | |
| 6758 | // If we lifetime-extend a braced initializer which is initializing an |
| 6759 | // aggregate, and that aggregate contains reference members which are |
| 6760 | // bound to temporaries, those temporaries are also lifetime-extended. |
| 6761 | if (RD->isUnion() && ILE->getInitializedFieldInUnion() && |
| 6762 | ILE->getInitializedFieldInUnion()->getType()->isReferenceType()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6763 | visitLocalsRetainedByReferenceBinding(Path, ILE->getInit(0), |
| 6764 | RK_ReferenceBinding, Visit); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6765 | else { |
| 6766 | unsigned Index = 0; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6767 | for (const auto *I : RD->fields()) { |
Richard Smith | 0bca59d | 2013-07-01 06:08:20 +0000 | [diff] [blame] | 6768 | if (Index >= ILE->getNumInits()) |
| 6769 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6770 | if (I->isUnnamedBitfield()) |
| 6771 | continue; |
Richard Smith | 8d7f11d | 2013-06-27 22:54:33 +0000 | [diff] [blame] | 6772 | Expr *SubInit = ILE->getInit(Index); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6773 | if (I->getType()->isReferenceType()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6774 | visitLocalsRetainedByReferenceBinding(Path, SubInit, |
| 6775 | RK_ReferenceBinding, Visit); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6776 | else |
| 6777 | // This might be either aggregate-initialization of a member or |
| 6778 | // initialization of a std::initializer_list object. Regardless, |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6779 | // we should recursively lifetime-extend that initializer. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6780 | visitLocalsRetainedByInitializer(Path, SubInit, Visit, |
| 6781 | RevisitSubinits); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6782 | ++Index; |
| 6783 | } |
| 6784 | } |
| 6785 | } |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6786 | return; |
| 6787 | } |
| 6788 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6789 | if (isa<CallExpr>(Init) || isa<CXXConstructExpr>(Init)) |
| 6790 | return visitLifetimeBoundArguments(Path, Init, Visit); |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6791 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6792 | switch (Init->getStmtClass()) { |
| 6793 | case Stmt::UnaryOperatorClass: { |
| 6794 | auto *UO = cast<UnaryOperator>(Init); |
| 6795 | // If the initializer is the address of a local, we could have a lifetime |
| 6796 | // problem. |
| 6797 | if (UO->getOpcode() == UO_AddrOf) { |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6798 | // If this is &rvalue, then it's ill-formed and we have already diagnosed |
| 6799 | // it. Don't produce a redundant warning about the lifetime of the |
| 6800 | // temporary. |
| 6801 | if (isa<MaterializeTemporaryExpr>(UO->getSubExpr())) |
| 6802 | return; |
| 6803 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6804 | Path.push_back({IndirectLocalPathEntry::AddressOf, UO}); |
| 6805 | visitLocalsRetainedByReferenceBinding(Path, UO->getSubExpr(), |
| 6806 | RK_ReferenceBinding, Visit); |
| 6807 | } |
| 6808 | break; |
| 6809 | } |
| 6810 | |
| 6811 | case Stmt::BinaryOperatorClass: { |
| 6812 | // Handle pointer arithmetic. |
| 6813 | auto *BO = cast<BinaryOperator>(Init); |
| 6814 | BinaryOperatorKind BOK = BO->getOpcode(); |
| 6815 | if (!BO->getType()->isPointerType() || (BOK != BO_Add && BOK != BO_Sub)) |
| 6816 | break; |
| 6817 | |
| 6818 | if (BO->getLHS()->getType()->isPointerType()) |
| 6819 | visitLocalsRetainedByInitializer(Path, BO->getLHS(), Visit, true); |
| 6820 | else if (BO->getRHS()->getType()->isPointerType()) |
| 6821 | visitLocalsRetainedByInitializer(Path, BO->getRHS(), Visit, true); |
| 6822 | break; |
| 6823 | } |
| 6824 | |
| 6825 | case Stmt::ConditionalOperatorClass: |
| 6826 | case Stmt::BinaryConditionalOperatorClass: { |
| 6827 | auto *C = cast<AbstractConditionalOperator>(Init); |
| 6828 | // In C++, we can have a throw-expression operand, which has 'void' type |
| 6829 | // and isn't interesting from a lifetime perspective. |
| 6830 | if (!C->getTrueExpr()->getType()->isVoidType()) |
| 6831 | visitLocalsRetainedByInitializer(Path, C->getTrueExpr(), Visit, true); |
| 6832 | if (!C->getFalseExpr()->getType()->isVoidType()) |
| 6833 | visitLocalsRetainedByInitializer(Path, C->getFalseExpr(), Visit, true); |
| 6834 | break; |
| 6835 | } |
| 6836 | |
| 6837 | case Stmt::BlockExprClass: |
| 6838 | if (cast<BlockExpr>(Init)->getBlockDecl()->hasCaptures()) { |
| 6839 | // This is a local block, whose lifetime is that of the function. |
| 6840 | Visit(Path, Local(cast<BlockExpr>(Init)), RK_ReferenceBinding); |
| 6841 | } |
| 6842 | break; |
| 6843 | |
| 6844 | case Stmt::AddrLabelExprClass: |
| 6845 | // We want to warn if the address of a label would escape the function. |
| 6846 | Visit(Path, Local(cast<AddrLabelExpr>(Init)), RK_ReferenceBinding); |
| 6847 | break; |
| 6848 | |
| 6849 | default: |
| 6850 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6851 | } |
| 6852 | } |
| 6853 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6854 | /// Determine whether this is an indirect path to a temporary that we are |
| 6855 | /// supposed to lifetime-extend along (but don't). |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6856 | static bool shouldLifetimeExtendThroughPath(const IndirectLocalPath &Path) { |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6857 | for (auto Elem : Path) { |
Richard Smith | f66e4f7 | 2018-07-23 22:56:45 +0000 | [diff] [blame] | 6858 | if (Elem.Kind != IndirectLocalPathEntry::DefaultInit) |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6859 | return false; |
| 6860 | } |
| 6861 | return true; |
| 6862 | } |
| 6863 | |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 6864 | /// Find the range for the first interesting entry in the path at or after I. |
| 6865 | static SourceRange nextPathEntryRange(const IndirectLocalPath &Path, unsigned I, |
| 6866 | Expr *E) { |
| 6867 | for (unsigned N = Path.size(); I != N; ++I) { |
| 6868 | switch (Path[I].Kind) { |
| 6869 | case IndirectLocalPathEntry::AddressOf: |
| 6870 | case IndirectLocalPathEntry::LValToRVal: |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6871 | case IndirectLocalPathEntry::LifetimeBoundCall: |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 6872 | // These exist primarily to mark the path as not permitting or |
| 6873 | // supporting lifetime extension. |
| 6874 | break; |
| 6875 | |
| 6876 | case IndirectLocalPathEntry::DefaultInit: |
| 6877 | case IndirectLocalPathEntry::VarInit: |
| 6878 | return Path[I].E->getSourceRange(); |
| 6879 | } |
| 6880 | } |
| 6881 | return E->getSourceRange(); |
| 6882 | } |
| 6883 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6884 | void Sema::checkInitializerLifetime(const InitializedEntity &Entity, |
| 6885 | Expr *Init) { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6886 | LifetimeResult LR = getEntityLifetime(&Entity); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6887 | LifetimeKind LK = LR.getInt(); |
| 6888 | const InitializedEntity *ExtendingEntity = LR.getPointer(); |
| 6889 | |
| 6890 | // If this entity doesn't have an interesting lifetime, don't bother looking |
| 6891 | // for temporaries within its initializer. |
| 6892 | if (LK == LK_FullExpression) |
| 6893 | return; |
| 6894 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6895 | auto TemporaryVisitor = [&](IndirectLocalPath &Path, Local L, |
| 6896 | ReferenceKind RK) -> bool { |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 6897 | SourceRange DiagRange = nextPathEntryRange(Path, 0, L); |
| 6898 | SourceLocation DiagLoc = DiagRange.getBegin(); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6899 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6900 | switch (LK) { |
| 6901 | case LK_FullExpression: |
| 6902 | llvm_unreachable("already handled this"); |
| 6903 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6904 | case LK_Extended: { |
| 6905 | auto *MTE = dyn_cast<MaterializeTemporaryExpr>(L); |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6906 | if (!MTE) { |
| 6907 | // The initialized entity has lifetime beyond the full-expression, |
| 6908 | // and the local entity does too, so don't warn. |
| 6909 | // |
| 6910 | // FIXME: We should consider warning if a static / thread storage |
| 6911 | // duration variable retains an automatic storage duration local. |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6912 | return false; |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6913 | } |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6914 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6915 | // Lifetime-extend the temporary. |
| 6916 | if (Path.empty()) { |
| 6917 | // Update the storage duration of the materialized temporary. |
| 6918 | // FIXME: Rebuild the expression instead of mutating it. |
| 6919 | MTE->setExtendingDecl(ExtendingEntity->getDecl(), |
| 6920 | ExtendingEntity->allocateManglingNumber()); |
| 6921 | // Also visit the temporaries lifetime-extended by this initializer. |
| 6922 | return true; |
| 6923 | } |
| 6924 | |
| 6925 | if (shouldLifetimeExtendThroughPath(Path)) { |
| 6926 | // We're supposed to lifetime-extend the temporary along this path (per |
| 6927 | // the resolution of DR1815), but we don't support that yet. |
| 6928 | // |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6929 | // FIXME: Properly handle this situation. Perhaps the easiest approach |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6930 | // would be to clone the initializer expression on each use that would |
| 6931 | // lifetime extend its temporaries. |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6932 | Diag(DiagLoc, diag::warn_unsupported_lifetime_extension) |
| 6933 | << RK << DiagRange; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6934 | } else { |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6935 | // If the path goes through the initialization of a variable or field, |
| 6936 | // it can't possibly reach a temporary created in this full-expression. |
| 6937 | // We will have already diagnosed any problems with the initializer. |
| 6938 | if (pathContainsInit(Path)) |
| 6939 | return false; |
| 6940 | |
| 6941 | Diag(DiagLoc, diag::warn_dangling_variable) |
Richard Smith | ad5bbcc | 2018-08-01 01:03:33 +0000 | [diff] [blame] | 6942 | << RK << !Entity.getParent() |
| 6943 | << ExtendingEntity->getDecl()->isImplicit() |
| 6944 | << ExtendingEntity->getDecl() << Init->isGLValue() << DiagRange; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6945 | } |
| 6946 | break; |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6947 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6948 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6949 | case LK_MemInitializer: { |
George Burgess IV | 06df229 | 2018-07-24 02:10:53 +0000 | [diff] [blame] | 6950 | if (isa<MaterializeTemporaryExpr>(L)) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6951 | // Under C++ DR1696, if a mem-initializer (or a default member |
| 6952 | // initializer used by the absence of one) would lifetime-extend a |
| 6953 | // temporary, the program is ill-formed. |
| 6954 | if (auto *ExtendingDecl = |
| 6955 | ExtendingEntity ? ExtendingEntity->getDecl() : nullptr) { |
| 6956 | bool IsSubobjectMember = ExtendingEntity != &Entity; |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6957 | Diag(DiagLoc, shouldLifetimeExtendThroughPath(Path) |
| 6958 | ? diag::err_dangling_member |
| 6959 | : diag::warn_dangling_member) |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6960 | << ExtendingDecl << IsSubobjectMember << RK << DiagRange; |
| 6961 | // Don't bother adding a note pointing to the field if we're inside |
| 6962 | // its default member initializer; our primary diagnostic points to |
| 6963 | // the same place in that case. |
| 6964 | if (Path.empty() || |
| 6965 | Path.back().Kind != IndirectLocalPathEntry::DefaultInit) { |
| 6966 | Diag(ExtendingDecl->getLocation(), |
| 6967 | diag::note_lifetime_extending_member_declared_here) |
| 6968 | << RK << IsSubobjectMember; |
| 6969 | } |
| 6970 | } else { |
| 6971 | // We have a mem-initializer but no particular field within it; this |
| 6972 | // is either a base class or a delegating initializer directly |
| 6973 | // initializing the base-class from something that doesn't live long |
| 6974 | // enough. |
| 6975 | // |
| 6976 | // FIXME: Warn on this. |
| 6977 | return false; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6978 | } |
| 6979 | } else { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6980 | // Paths via a default initializer can only occur during error recovery |
| 6981 | // (there's no other way that a default initializer can refer to a |
| 6982 | // local). Don't produce a bogus warning on those cases. |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6983 | if (pathContainsInit(Path)) |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6984 | return false; |
| 6985 | |
| 6986 | auto *DRE = dyn_cast<DeclRefExpr>(L); |
| 6987 | auto *VD = DRE ? dyn_cast<VarDecl>(DRE->getDecl()) : nullptr; |
| 6988 | if (!VD) { |
| 6989 | // A member was initialized to a local block. |
| 6990 | // FIXME: Warn on this. |
| 6991 | return false; |
| 6992 | } |
| 6993 | |
| 6994 | if (auto *Member = |
| 6995 | ExtendingEntity ? ExtendingEntity->getDecl() : nullptr) { |
| 6996 | bool IsPointer = Member->getType()->isAnyPointerType(); |
| 6997 | Diag(DiagLoc, IsPointer ? diag::warn_init_ptr_member_to_parameter_addr |
| 6998 | : diag::warn_bind_ref_member_to_parameter) |
| 6999 | << Member << VD << isa<ParmVarDecl>(VD) << DiagRange; |
| 7000 | Diag(Member->getLocation(), |
| 7001 | diag::note_ref_or_ptr_member_declared_here) |
| 7002 | << (unsigned)IsPointer; |
| 7003 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7004 | } |
| 7005 | break; |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7006 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7007 | |
| 7008 | case LK_New: |
George Burgess IV | 06df229 | 2018-07-24 02:10:53 +0000 | [diff] [blame] | 7009 | if (isa<MaterializeTemporaryExpr>(L)) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7010 | Diag(DiagLoc, RK == RK_ReferenceBinding |
| 7011 | ? diag::warn_new_dangling_reference |
| 7012 | : diag::warn_new_dangling_initializer_list) |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 7013 | << !Entity.getParent() << DiagRange; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7014 | } else { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7015 | // We can't determine if the allocation outlives the local declaration. |
| 7016 | return false; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7017 | } |
| 7018 | break; |
| 7019 | |
| 7020 | case LK_Return: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 7021 | case LK_StmtExprResult: |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7022 | if (auto *DRE = dyn_cast<DeclRefExpr>(L)) { |
| 7023 | // We can't determine if the local variable outlives the statement |
| 7024 | // expression. |
| 7025 | if (LK == LK_StmtExprResult) |
| 7026 | return false; |
| 7027 | Diag(DiagLoc, diag::warn_ret_stack_addr_ref) |
| 7028 | << Entity.getType()->isReferenceType() << DRE->getDecl() |
| 7029 | << isa<ParmVarDecl>(DRE->getDecl()) << DiagRange; |
| 7030 | } else if (isa<BlockExpr>(L)) { |
| 7031 | Diag(DiagLoc, diag::err_ret_local_block) << DiagRange; |
| 7032 | } else if (isa<AddrLabelExpr>(L)) { |
Reid Kleckner | 4c33d19 | 2018-08-17 22:11:31 +0000 | [diff] [blame] | 7033 | // Don't warn when returning a label from a statement expression. |
| 7034 | // Leaving the scope doesn't end its lifetime. |
| 7035 | if (LK == LK_StmtExprResult) |
| 7036 | return false; |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7037 | Diag(DiagLoc, diag::warn_ret_addr_label) << DiagRange; |
| 7038 | } else { |
| 7039 | Diag(DiagLoc, diag::warn_ret_local_temp_addr_ref) |
| 7040 | << Entity.getType()->isReferenceType() << DiagRange; |
| 7041 | } |
| 7042 | break; |
Florian Hahn | 0aa117d | 2018-07-17 09:23:31 +0000 | [diff] [blame] | 7043 | } |
| 7044 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7045 | for (unsigned I = 0; I != Path.size(); ++I) { |
| 7046 | auto Elem = Path[I]; |
| 7047 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7048 | switch (Elem.Kind) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7049 | case IndirectLocalPathEntry::AddressOf: |
| 7050 | case IndirectLocalPathEntry::LValToRVal: |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 7051 | // These exist primarily to mark the path as not permitting or |
| 7052 | // supporting lifetime extension. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7053 | break; |
| 7054 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 7055 | case IndirectLocalPathEntry::LifetimeBoundCall: |
| 7056 | // FIXME: Consider adding a note for this. |
| 7057 | break; |
| 7058 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7059 | case IndirectLocalPathEntry::DefaultInit: { |
| 7060 | auto *FD = cast<FieldDecl>(Elem.D); |
| 7061 | Diag(FD->getLocation(), diag::note_init_with_default_member_initalizer) |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 7062 | << FD << nextPathEntryRange(Path, I + 1, L); |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7063 | break; |
| 7064 | } |
| 7065 | |
| 7066 | case IndirectLocalPathEntry::VarInit: |
| 7067 | const VarDecl *VD = cast<VarDecl>(Elem.D); |
| 7068 | Diag(VD->getLocation(), diag::note_local_var_initializer) |
Richard Smith | ad5bbcc | 2018-08-01 01:03:33 +0000 | [diff] [blame] | 7069 | << VD->getType()->isReferenceType() |
| 7070 | << VD->isImplicit() << VD->getDeclName() |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 7071 | << nextPathEntryRange(Path, I + 1, L); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7072 | break; |
Florian Hahn | 0aa117d | 2018-07-17 09:23:31 +0000 | [diff] [blame] | 7073 | } |
| 7074 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7075 | |
| 7076 | // We didn't lifetime-extend, so don't go any further; we don't need more |
| 7077 | // warnings or errors on inner temporaries within this one's initializer. |
| 7078 | return false; |
| 7079 | }; |
| 7080 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7081 | llvm::SmallVector<IndirectLocalPathEntry, 8> Path; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7082 | if (Init->isGLValue()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7083 | visitLocalsRetainedByReferenceBinding(Path, Init, RK_ReferenceBinding, |
| 7084 | TemporaryVisitor); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7085 | else |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7086 | visitLocalsRetainedByInitializer(Path, Init, TemporaryVisitor, false); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 7087 | } |
| 7088 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7089 | static void DiagnoseNarrowingInInitList(Sema &S, |
| 7090 | const ImplicitConversionSequence &ICS, |
| 7091 | QualType PreNarrowingType, |
| 7092 | QualType EntityType, |
| 7093 | const Expr *PostInit); |
| 7094 | |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7095 | /// Provide warnings when std::move is used on construction. |
| 7096 | static void CheckMoveOnConstruction(Sema &S, const Expr *InitExpr, |
| 7097 | bool IsReturnStmt) { |
| 7098 | if (!InitExpr) |
| 7099 | return; |
| 7100 | |
Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 7101 | if (S.inTemplateInstantiation()) |
Richard Trieu | 6093d14 | 2015-07-29 17:03:34 +0000 | [diff] [blame] | 7102 | return; |
| 7103 | |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7104 | QualType DestType = InitExpr->getType(); |
| 7105 | if (!DestType->isRecordType()) |
| 7106 | return; |
| 7107 | |
| 7108 | unsigned DiagID = 0; |
| 7109 | if (IsReturnStmt) { |
| 7110 | const CXXConstructExpr *CCE = |
| 7111 | dyn_cast<CXXConstructExpr>(InitExpr->IgnoreParens()); |
| 7112 | if (!CCE || CCE->getNumArgs() != 1) |
| 7113 | return; |
| 7114 | |
| 7115 | if (!CCE->getConstructor()->isCopyOrMoveConstructor()) |
| 7116 | return; |
| 7117 | |
| 7118 | InitExpr = CCE->getArg(0)->IgnoreImpCasts(); |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7119 | } |
| 7120 | |
| 7121 | // Find the std::move call and get the argument. |
| 7122 | const CallExpr *CE = dyn_cast<CallExpr>(InitExpr->IgnoreParens()); |
Nico Weber | 192184c | 2018-06-20 15:57:38 +0000 | [diff] [blame] | 7123 | if (!CE || !CE->isCallToStdMove()) |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7124 | return; |
| 7125 | |
| 7126 | const Expr *Arg = CE->getArg(0)->IgnoreImplicit(); |
| 7127 | |
| 7128 | if (IsReturnStmt) { |
| 7129 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg->IgnoreParenImpCasts()); |
| 7130 | if (!DRE || DRE->refersToEnclosingVariableOrCapture()) |
| 7131 | return; |
| 7132 | |
| 7133 | const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()); |
| 7134 | if (!VD || !VD->hasLocalStorage()) |
| 7135 | return; |
| 7136 | |
Alex Lorenz | bbe51d8 | 2017-11-07 21:40:11 +0000 | [diff] [blame] | 7137 | // __block variables are not moved implicitly. |
| 7138 | if (VD->hasAttr<BlocksAttr>()) |
| 7139 | return; |
| 7140 | |
Richard Trieu | 8d4006a | 2015-07-28 19:06:16 +0000 | [diff] [blame] | 7141 | QualType SourceType = VD->getType(); |
| 7142 | if (!SourceType->isRecordType()) |
Richard Trieu | 1d4911bc | 2015-05-18 19:54:08 +0000 | [diff] [blame] | 7143 | return; |
| 7144 | |
Richard Trieu | 8d4006a | 2015-07-28 19:06:16 +0000 | [diff] [blame] | 7145 | if (!S.Context.hasSameUnqualifiedType(DestType, SourceType)) { |
Richard Trieu | 1993dc8 | 2015-07-29 23:47:19 +0000 | [diff] [blame] | 7146 | return; |
Richard Trieu | 8d4006a | 2015-07-28 19:06:16 +0000 | [diff] [blame] | 7147 | } |
| 7148 | |
Davide Italiano | 7842c3f | 2015-07-18 01:15:19 +0000 | [diff] [blame] | 7149 | // If we're returning a function parameter, copy elision |
| 7150 | // is not possible. |
| 7151 | if (isa<ParmVarDecl>(VD)) |
| 7152 | DiagID = diag::warn_redundant_move_on_return; |
Richard Trieu | 1993dc8 | 2015-07-29 23:47:19 +0000 | [diff] [blame] | 7153 | else |
| 7154 | DiagID = diag::warn_pessimizing_move_on_return; |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7155 | } else { |
| 7156 | DiagID = diag::warn_pessimizing_move_on_initialization; |
| 7157 | const Expr *ArgStripped = Arg->IgnoreImplicit()->IgnoreParens(); |
| 7158 | if (!ArgStripped->isRValue() || !ArgStripped->getType()->isRecordType()) |
| 7159 | return; |
| 7160 | } |
| 7161 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7162 | S.Diag(CE->getBeginLoc(), DiagID); |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7163 | |
| 7164 | // Get all the locations for a fix-it. Don't emit the fix-it if any location |
| 7165 | // is within a macro. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7166 | SourceLocation CallBegin = CE->getCallee()->getBeginLoc(); |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7167 | if (CallBegin.isMacroID()) |
| 7168 | return; |
| 7169 | SourceLocation RParen = CE->getRParenLoc(); |
| 7170 | if (RParen.isMacroID()) |
| 7171 | return; |
| 7172 | SourceLocation LParen; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7173 | SourceLocation ArgLoc = Arg->getBeginLoc(); |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7174 | |
| 7175 | // Special testing for the argument location. Since the fix-it needs the |
| 7176 | // location right before the argument, the argument location can be in a |
| 7177 | // macro only if it is at the beginning of the macro. |
| 7178 | while (ArgLoc.isMacroID() && |
| 7179 | S.getSourceManager().isAtStartOfImmediateMacroExpansion(ArgLoc)) { |
Richard Smith | b5f8171 | 2018-04-30 05:25:48 +0000 | [diff] [blame] | 7180 | ArgLoc = S.getSourceManager().getImmediateExpansionRange(ArgLoc).getBegin(); |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7181 | } |
| 7182 | |
| 7183 | if (LParen.isMacroID()) |
| 7184 | return; |
| 7185 | |
| 7186 | LParen = ArgLoc.getLocWithOffset(-1); |
| 7187 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7188 | S.Diag(CE->getBeginLoc(), diag::note_remove_move) |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7189 | << FixItHint::CreateRemoval(SourceRange(CallBegin, LParen)) |
| 7190 | << FixItHint::CreateRemoval(SourceRange(RParen, RParen)); |
| 7191 | } |
| 7192 | |
Nick Lewycky | 2eeddfb | 2016-05-14 17:44:14 +0000 | [diff] [blame] | 7193 | static void CheckForNullPointerDereference(Sema &S, const Expr *E) { |
| 7194 | // Check to see if we are dereferencing a null pointer. If so, this is |
| 7195 | // undefined behavior, so warn about it. This only handles the pattern |
| 7196 | // "*null", which is a very syntactic check. |
| 7197 | if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E->IgnoreParenCasts())) |
| 7198 | if (UO->getOpcode() == UO_Deref && |
| 7199 | UO->getSubExpr()->IgnoreParenCasts()-> |
| 7200 | isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull)) { |
| 7201 | S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
| 7202 | S.PDiag(diag::warn_binding_null_to_reference) |
| 7203 | << UO->getSubExpr()->getSourceRange()); |
| 7204 | } |
| 7205 | } |
| 7206 | |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 7207 | MaterializeTemporaryExpr * |
| 7208 | Sema::CreateMaterializeTemporaryExpr(QualType T, Expr *Temporary, |
| 7209 | bool BoundToLvalueReference) { |
| 7210 | auto MTE = new (Context) |
| 7211 | MaterializeTemporaryExpr(T, Temporary, BoundToLvalueReference); |
| 7212 | |
| 7213 | // Order an ExprWithCleanups for lifetime marks. |
| 7214 | // |
| 7215 | // TODO: It'll be good to have a single place to check the access of the |
| 7216 | // destructor and generate ExprWithCleanups for various uses. Currently these |
| 7217 | // are done in both CreateMaterializeTemporaryExpr and MaybeBindToTemporary, |
| 7218 | // but there may be a chance to merge them. |
| 7219 | Cleanup.setExprNeedsCleanups(false); |
| 7220 | return MTE; |
| 7221 | } |
| 7222 | |
Richard Smith | 4baaa5a | 2016-12-03 01:14:32 +0000 | [diff] [blame] | 7223 | ExprResult Sema::TemporaryMaterializationConversion(Expr *E) { |
| 7224 | // In C++98, we don't want to implicitly create an xvalue. |
| 7225 | // FIXME: This means that AST consumers need to deal with "prvalues" that |
| 7226 | // denote materialized temporaries. Maybe we should add another ValueKind |
| 7227 | // for "xvalue pretending to be a prvalue" for C++98 support. |
| 7228 | if (!E->isRValue() || !getLangOpts().CPlusPlus11) |
| 7229 | return E; |
| 7230 | |
| 7231 | // C++1z [conv.rval]/1: T shall be a complete type. |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7232 | // FIXME: Does this ever matter (can we form a prvalue of incomplete type)? |
| 7233 | // 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] | 7234 | QualType T = E->getType(); |
| 7235 | if (RequireCompleteType(E->getExprLoc(), T, diag::err_incomplete_type)) |
| 7236 | return ExprError(); |
| 7237 | |
| 7238 | return CreateMaterializeTemporaryExpr(E->getType(), E, false); |
| 7239 | } |
| 7240 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7241 | ExprResult |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7242 | InitializationSequence::Perform(Sema &S, |
| 7243 | const InitializedEntity &Entity, |
| 7244 | const InitializationKind &Kind, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7245 | MultiExprArg Args, |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7246 | QualType *ResultType) { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 7247 | if (Failed()) { |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 7248 | Diagnose(S, Entity, Kind, Args); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7249 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7250 | } |
Nico Weber | 337d5aa | 2015-04-17 08:32:38 +0000 | [diff] [blame] | 7251 | if (!ZeroInitializationFixit.empty()) { |
| 7252 | unsigned DiagID = diag::err_default_init_const; |
| 7253 | if (Decl *D = Entity.getDecl()) |
| 7254 | if (S.getLangOpts().MSVCCompat && D->hasAttr<SelectAnyAttr>()) |
| 7255 | DiagID = diag::ext_default_init_const; |
| 7256 | |
| 7257 | // The initialization would have succeeded with this fixit. Since the fixit |
| 7258 | // is on the error, we need to build a valid AST in this case, so this isn't |
| 7259 | // handled in the Failed() branch above. |
| 7260 | QualType DestType = Entity.getType(); |
| 7261 | S.Diag(Kind.getLocation(), DiagID) |
| 7262 | << DestType << (bool)DestType->getAs<RecordType>() |
| 7263 | << FixItHint::CreateInsertion(ZeroInitializationFixitLoc, |
| 7264 | ZeroInitializationFixit); |
| 7265 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7266 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 7267 | if (getKind() == DependentSequence) { |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7268 | // If the declaration is a non-dependent, incomplete array type |
| 7269 | // that has an initializer, then its type will be completed once |
| 7270 | // the initializer is instantiated. |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7271 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7272 | Args.size() == 1) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7273 | QualType DeclType = Entity.getType(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7274 | if (const IncompleteArrayType *ArrayT |
| 7275 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 7276 | // FIXME: We don't currently have the ability to accurately |
| 7277 | // compute the length of an initializer list without |
| 7278 | // performing full type-checking of the initializer list |
| 7279 | // (since we have to determine where braces are implicitly |
| 7280 | // introduced and such). So, we fall back to making the array |
| 7281 | // type a dependently-sized array type with no specified |
| 7282 | // bound. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7283 | if (isa<InitListExpr>((Expr *)Args[0])) { |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7284 | SourceRange Brackets; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7285 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7286 | // Scavange the location of the brackets from the entity, if we can. |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 7287 | if (auto *DD = dyn_cast_or_null<DeclaratorDecl>(Entity.getDecl())) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7288 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 7289 | TypeLoc TL = TInfo->getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7290 | if (IncompleteArrayTypeLoc ArrayLoc = |
| 7291 | TL.getAs<IncompleteArrayTypeLoc>()) |
| 7292 | Brackets = ArrayLoc.getBracketsRange(); |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7293 | } |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7294 | } |
| 7295 | |
| 7296 | *ResultType |
| 7297 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7298 | /*NumElts=*/nullptr, |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7299 | ArrayT->getSizeModifier(), |
| 7300 | ArrayT->getIndexTypeCVRQualifiers(), |
| 7301 | Brackets); |
| 7302 | } |
| 7303 | |
| 7304 | } |
| 7305 | } |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 7306 | if (Kind.getKind() == InitializationKind::IK_Direct && |
| 7307 | !Kind.isExplicitCast()) { |
| 7308 | // Rebuild the ParenListExpr. |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 7309 | SourceRange ParenRange = Kind.getParenOrBraceRange(); |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 7310 | return S.ActOnParenListExpr(ParenRange.getBegin(), ParenRange.getEnd(), |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7311 | Args); |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 7312 | } |
Manuel Klimek | f2b4b69 | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 7313 | assert(Kind.getKind() == InitializationKind::IK_Copy || |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7314 | Kind.isExplicitCast() || |
Douglas Gregor | bf13895 | 2012-04-04 04:06:51 +0000 | [diff] [blame] | 7315 | Kind.getKind() == InitializationKind::IK_DirectList); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7316 | return ExprResult(Args[0]); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7317 | } |
| 7318 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 7319 | // No steps means no initialization. |
| 7320 | if (Steps.empty()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7321 | return ExprResult((Expr *)nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7322 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7323 | if (S.getLangOpts().CPlusPlus11 && Entity.getType()->isReferenceType() && |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7324 | Args.size() == 1 && isa<InitListExpr>(Args[0]) && |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 7325 | !Entity.isParameterKind()) { |
Richard Smith | 2b349ae | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 7326 | // Produce a C++98 compatibility warning if we are initializing a reference |
| 7327 | // from an initializer list. For parameters, we produce a better warning |
| 7328 | // elsewhere. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7329 | Expr *Init = Args[0]; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7330 | S.Diag(Init->getBeginLoc(), diag::warn_cxx98_compat_reference_list_init) |
| 7331 | << Init->getSourceRange(); |
Richard Smith | 2b349ae | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 7332 | } |
| 7333 | |
Egor Churaev | 3bccec5 | 2017-04-05 12:47:10 +0000 | [diff] [blame] | 7334 | // OpenCL v2.0 s6.13.11.1. atomic variables can be initialized in global scope |
| 7335 | QualType ETy = Entity.getType(); |
| 7336 | Qualifiers TyQualifiers = ETy.getQualifiers(); |
| 7337 | bool HasGlobalAS = TyQualifiers.hasAddressSpace() && |
| 7338 | TyQualifiers.getAddressSpace() == LangAS::opencl_global; |
| 7339 | |
| 7340 | if (S.getLangOpts().OpenCLVersion >= 200 && |
| 7341 | ETy->isAtomicType() && !HasGlobalAS && |
| 7342 | Entity.getKind() == InitializedEntity::EK_Variable && Args.size() > 0) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7343 | S.Diag(Args[0]->getBeginLoc(), diag::err_opencl_atomic_init) |
| 7344 | << 1 |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 7345 | << SourceRange(Entity.getDecl()->getBeginLoc(), Args[0]->getEndLoc()); |
Egor Churaev | 3bccec5 | 2017-04-05 12:47:10 +0000 | [diff] [blame] | 7346 | return ExprError(); |
| 7347 | } |
| 7348 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7349 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 7350 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 7351 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 7352 | // and we want latter when it makes sense. |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7353 | if (ResultType) |
Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 7354 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7355 | Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7356 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7357 | ExprResult CurInit((Expr *)nullptr); |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7358 | SmallVector<Expr*, 4> ArrayLoopCommonExprs; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7359 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7360 | // For initialization steps that start with a single initializer, |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 7361 | // grab the only argument out the Args and place it into the "current" |
| 7362 | // initializer. |
| 7363 | switch (Steps.front().Kind) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7364 | case SK_ResolveAddressOfOverloadedFunction: |
| 7365 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7366 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7367 | case SK_CastDerivedToBaseLValue: |
| 7368 | case SK_BindReference: |
| 7369 | case SK_BindReferenceToTemporary: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7370 | case SK_FinalCopy: |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 7371 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7372 | case SK_UserConversion: |
| 7373 | case SK_QualificationConversionLValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7374 | case SK_QualificationConversionXValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7375 | case SK_QualificationConversionRValue: |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 7376 | case SK_AtomicConversion: |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 7377 | case SK_LValueToRValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7378 | case SK_ConversionSequence: |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7379 | case SK_ConversionSequenceNoNarrowing: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7380 | case SK_ListInitialization: |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7381 | case SK_UnwrapInitList: |
| 7382 | case SK_RewrapInitList: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7383 | case SK_CAssignment: |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 7384 | case SK_StringInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7385 | case SK_ObjCObjectConversion: |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7386 | case SK_ArrayLoopIndex: |
| 7387 | case SK_ArrayLoopInit: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7388 | case SK_ArrayInit: |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 7389 | case SK_GNUArrayInit: |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 7390 | case SK_ParenthesizedArrayInit: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7391 | case SK_PassByIndirectCopyRestore: |
| 7392 | case SK_PassByIndirectRestore: |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 7393 | case SK_ProduceObjCObject: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7394 | case SK_StdInitializerList: |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 7395 | case SK_OCLSamplerInit: |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 7396 | case SK_OCLZeroEvent: |
| 7397 | case SK_OCLZeroQueue: { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7398 | assert(Args.size() == 1); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7399 | CurInit = Args[0]; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7400 | if (!CurInit.get()) return ExprError(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7401 | break; |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7402 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7403 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7404 | case SK_ConstructorInitialization: |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7405 | case SK_ConstructorInitializationFromList: |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 7406 | case SK_StdInitializerListConstructorCall: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7407 | case SK_ZeroInitialization: |
| 7408 | break; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7409 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7410 | |
Richard Smith | d6a1508 | 2017-01-07 00:48:55 +0000 | [diff] [blame] | 7411 | // Promote from an unevaluated context to an unevaluated list context in |
| 7412 | // C++11 list-initialization; we need to instantiate entities usable in |
| 7413 | // constant expressions here in order to perform narrowing checks =( |
| 7414 | EnterExpressionEvaluationContext Evaluated( |
| 7415 | S, EnterExpressionEvaluationContext::InitList, |
| 7416 | CurInit.get() && isa<InitListExpr>(CurInit.get())); |
| 7417 | |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7418 | // C++ [class.abstract]p2: |
| 7419 | // no objects of an abstract class can be created except as subobjects |
| 7420 | // of a class derived from it |
| 7421 | auto checkAbstractType = [&](QualType T) -> bool { |
| 7422 | if (Entity.getKind() == InitializedEntity::EK_Base || |
| 7423 | Entity.getKind() == InitializedEntity::EK_Delegating) |
| 7424 | return false; |
| 7425 | return S.RequireNonAbstractType(Kind.getLocation(), T, |
| 7426 | diag::err_allocation_of_abstract_type); |
| 7427 | }; |
| 7428 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7429 | // Walk through the computed steps for the initialization sequence, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7430 | // performing the specified conversions along the way. |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7431 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7432 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 7433 | Step != StepEnd; ++Step) { |
| 7434 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7435 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7436 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7437 | QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7438 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7439 | switch (Step->Kind) { |
| 7440 | case SK_ResolveAddressOfOverloadedFunction: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7441 | // Overload resolution determined which function invoke; update the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7442 | // initializer to reflect that choice. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7443 | S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 7444 | if (S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation())) |
| 7445 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7446 | CurInit = S.FixOverloadedFunctionReference(CurInit, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7447 | Step->Function.FoundDecl, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7448 | Step->Function.Function); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7449 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7450 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7451 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7452 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7453 | case SK_CastDerivedToBaseLValue: { |
| 7454 | // We have a derived-to-base cast that produces either an rvalue or an |
| 7455 | // lvalue. Perform that cast. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7456 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 7457 | CXXCastPath BasePath; |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 7458 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7459 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 7460 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7461 | if (S.CheckDerivedToBaseConversion( |
| 7462 | SourceType, Step->Type, CurInit.get()->getBeginLoc(), |
| 7463 | CurInit.get()->getSourceRange(), &BasePath, IgnoreBaseAccess)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7464 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7465 | |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7466 | ExprValueKind VK = |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7467 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7468 | VK_LValue : |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7469 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7470 | VK_XValue : |
| 7471 | VK_RValue); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7472 | CurInit = |
| 7473 | ImplicitCastExpr::Create(S.Context, Step->Type, CK_DerivedToBase, |
| 7474 | CurInit.get(), &BasePath, VK); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7475 | break; |
| 7476 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7477 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7478 | case SK_BindReference: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7479 | // Reference binding does not have any corresponding ASTs. |
| 7480 | |
| 7481 | // Check exception specifications |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7482 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7483 | return ExprError(); |
Anders Carlsson | ab0ddb5 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 7484 | |
George Burgess IV | cfd48d9 | 2017-04-13 23:47:08 +0000 | [diff] [blame] | 7485 | // We don't check for e.g. function pointers here, since address |
| 7486 | // availability checks should only occur when the function first decays |
| 7487 | // into a pointer or reference. |
| 7488 | if (CurInit.get()->getType()->isFunctionProtoType()) { |
| 7489 | if (auto *DRE = dyn_cast<DeclRefExpr>(CurInit.get()->IgnoreParens())) { |
| 7490 | if (auto *FD = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
| 7491 | if (!S.checkAddressOfFunctionIsAvailable(FD, /*Complain=*/true, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7492 | DRE->getBeginLoc())) |
George Burgess IV | cfd48d9 | 2017-04-13 23:47:08 +0000 | [diff] [blame] | 7493 | return ExprError(); |
| 7494 | } |
| 7495 | } |
| 7496 | } |
| 7497 | |
Nick Lewycky | 2eeddfb | 2016-05-14 17:44:14 +0000 | [diff] [blame] | 7498 | CheckForNullPointerDereference(S, CurInit.get()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7499 | break; |
Anders Carlsson | ab0ddb5 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 7500 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 7501 | case SK_BindReferenceToTemporary: { |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 7502 | // Make sure the "temporary" is actually an rvalue. |
| 7503 | assert(CurInit.get()->isRValue() && "not a temporary"); |
| 7504 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7505 | // Check exception specifications |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7506 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7507 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7508 | |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 7509 | // Materialize the temporary into memory. |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 7510 | MaterializeTemporaryExpr *MTE = S.CreateMaterializeTemporaryExpr( |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7511 | Step->Type, CurInit.get(), Entity.getType()->isLValueReferenceType()); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7512 | CurInit = MTE; |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 7513 | |
Brian Kelley | 762f928 | 2017-03-29 18:16:38 +0000 | [diff] [blame] | 7514 | // If we're extending this temporary to automatic storage duration -- we |
| 7515 | // need to register its cleanup during the full-expression's cleanups. |
| 7516 | if (MTE->getStorageDuration() == SD_Automatic && |
| 7517 | MTE->getType().isDestructedType()) |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 7518 | S.Cleanup.setExprNeedsCleanups(true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7519 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 7520 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7521 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7522 | case SK_FinalCopy: |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7523 | if (checkAbstractType(Step->Type)) |
| 7524 | return ExprError(); |
| 7525 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7526 | // If the overall initialization is initializing a temporary, we already |
| 7527 | // bound our argument if it was necessary to do so. If not (if we're |
| 7528 | // ultimately initializing a non-temporary), our argument needs to be |
| 7529 | // bound since it's initializing a function parameter. |
| 7530 | // FIXME: This is a mess. Rationalize temporary destruction. |
| 7531 | if (!shouldBindAsTemporary(Entity)) |
| 7532 | CurInit = S.MaybeBindToTemporary(CurInit.get()); |
| 7533 | CurInit = CopyObject(S, Step->Type, Entity, CurInit, |
| 7534 | /*IsExtraneousCopy=*/false); |
| 7535 | break; |
| 7536 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 7537 | case SK_ExtraneousCopyToTemporary: |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7538 | CurInit = CopyObject(S, Step->Type, Entity, CurInit, |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 7539 | /*IsExtraneousCopy=*/true); |
| 7540 | break; |
| 7541 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7542 | case SK_UserConversion: { |
| 7543 | // We have a user-defined conversion that invokes either a constructor |
| 7544 | // or a conversion function. |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 7545 | CastKind CastKind; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7546 | FunctionDecl *Fn = Step->Function.Function; |
| 7547 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 7548 | bool HadMultipleCandidates = Step->Function.HadMultipleCandidates; |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 7549 | bool CreatedObject = false; |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 7550 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7551 | // Build a call to the selected constructor. |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 7552 | SmallVector<Expr*, 8> ConstructorArgs; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7553 | SourceLocation Loc = CurInit.get()->getBeginLoc(); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 7554 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7555 | // Determine the arguments required to actually perform the constructor |
| 7556 | // call. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7557 | Expr *Arg = CurInit.get(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7558 | if (S.CompleteConstructorCall(Constructor, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7559 | MultiExprArg(&Arg, 1), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7560 | Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7561 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7562 | |
Richard Smith | b24f067 | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 7563 | // Build an expression that constructs a temporary. |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 7564 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, |
| 7565 | FoundFn, Constructor, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7566 | ConstructorArgs, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 7567 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 7568 | /*ListInit*/ false, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 7569 | /*StdInitListInit*/ false, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 7570 | /*ZeroInit*/ false, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 7571 | CXXConstructExpr::CK_Complete, |
| 7572 | SourceRange()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7573 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7574 | return ExprError(); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 7575 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 7576 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, FoundFn, |
| 7577 | Entity); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 7578 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 7579 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7580 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7581 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 7582 | CreatedObject = true; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7583 | } else { |
| 7584 | // Build a call to the conversion function. |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 7585 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7586 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), nullptr, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7587 | FoundFn); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 7588 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 7589 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7590 | |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 7591 | CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion, |
| 7592 | HadMultipleCandidates); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7593 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7594 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7595 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7596 | CastKind = CK_UserDefinedConversion; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7597 | CreatedObject = Conversion->getReturnType()->isRecordType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7598 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7599 | |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7600 | if (CreatedObject && checkAbstractType(CurInit.get()->getType())) |
| 7601 | return ExprError(); |
| 7602 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7603 | CurInit = ImplicitCastExpr::Create(S.Context, CurInit.get()->getType(), |
| 7604 | CastKind, CurInit.get(), nullptr, |
| 7605 | CurInit.get()->getValueKind()); |
Abramo Bagnara | b0cf297 | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 7606 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7607 | if (shouldBindAsTemporary(Entity)) |
| 7608 | // The overall entity is temporary, so this expression should be |
| 7609 | // destroyed at the end of its full-expression. |
| 7610 | CurInit = S.MaybeBindToTemporary(CurInit.getAs<Expr>()); |
| 7611 | else if (CreatedObject && shouldDestroyEntity(Entity)) { |
| 7612 | // The object outlasts the full-expression, but we need to prepare for |
| 7613 | // a destructor being run on it. |
| 7614 | // FIXME: It makes no sense to do this here. This should happen |
| 7615 | // regardless of how we initialized the entity. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7616 | QualType T = CurInit.get()->getType(); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 7617 | if (const RecordType *Record = T->getAs<RecordType>()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7618 | CXXDestructorDecl *Destructor |
Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 7619 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7620 | S.CheckDestructorAccess(CurInit.get()->getBeginLoc(), Destructor, |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 7621 | S.PDiag(diag::err_access_dtor_temp) << T); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7622 | S.MarkFunctionReferenced(CurInit.get()->getBeginLoc(), Destructor); |
| 7623 | if (S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getBeginLoc())) |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 7624 | return ExprError(); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 7625 | } |
| 7626 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7627 | break; |
| 7628 | } |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7629 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7630 | case SK_QualificationConversionLValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7631 | case SK_QualificationConversionXValue: |
| 7632 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7633 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7634 | ExprValueKind VK = |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7635 | Step->Kind == SK_QualificationConversionLValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7636 | VK_LValue : |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7637 | (Step->Kind == SK_QualificationConversionXValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7638 | VK_XValue : |
| 7639 | VK_RValue); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7640 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, CK_NoOp, VK); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7641 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7642 | } |
| 7643 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 7644 | case SK_AtomicConversion: { |
| 7645 | assert(CurInit.get()->isRValue() && "cannot convert glvalue to atomic"); |
| 7646 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
| 7647 | CK_NonAtomicToAtomic, VK_RValue); |
| 7648 | break; |
| 7649 | } |
| 7650 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 7651 | case SK_LValueToRValue: { |
| 7652 | assert(CurInit.get()->isGLValue() && "cannot load from a prvalue"); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7653 | CurInit = ImplicitCastExpr::Create(S.Context, Step->Type, |
| 7654 | CK_LValueToRValue, CurInit.get(), |
| 7655 | /*BasePath=*/nullptr, VK_RValue); |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 7656 | break; |
| 7657 | } |
| 7658 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7659 | case SK_ConversionSequence: |
| 7660 | case SK_ConversionSequenceNoNarrowing: { |
| 7661 | Sema::CheckedConversionKind CCK |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7662 | = Kind.isCStyleCast()? Sema::CCK_CStyleCast |
| 7663 | : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 7664 | : Kind.isExplicitCast()? Sema::CCK_OtherCast |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7665 | : Sema::CCK_ImplicitConversion; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7666 | ExprResult CurInitExprRes = |
| 7667 | S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7668 | getAssignmentAction(Entity), CCK); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7669 | if (CurInitExprRes.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7670 | return ExprError(); |
Roger Ferrer Ibanez | 722a4db | 2016-08-12 08:04:13 +0000 | [diff] [blame] | 7671 | |
| 7672 | S.DiscardMisalignedMemberAddress(Step->Type.getTypePtr(), CurInit.get()); |
| 7673 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7674 | CurInit = CurInitExprRes; |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7675 | |
| 7676 | if (Step->Kind == SK_ConversionSequenceNoNarrowing && |
Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 7677 | S.getLangOpts().CPlusPlus) |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7678 | DiagnoseNarrowingInInitList(S, *Step->ICS, SourceType, Entity.getType(), |
| 7679 | CurInit.get()); |
Roger Ferrer Ibanez | 722a4db | 2016-08-12 08:04:13 +0000 | [diff] [blame] | 7680 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7681 | break; |
Douglas Gregor | 5c8ffab | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 7682 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7683 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7684 | case SK_ListInitialization: { |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7685 | if (checkAbstractType(Step->Type)) |
| 7686 | return ExprError(); |
| 7687 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7688 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 7689 | // If we're not initializing the top-level entity, we need to create an |
| 7690 | // InitializeTemporary entity for our target type. |
| 7691 | QualType Ty = Step->Type; |
| 7692 | bool IsTemporary = !S.Context.hasSameType(Entity.getType(), Ty); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7693 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(Ty); |
Richard Smith | d712d0d | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 7694 | InitializedEntity InitEntity = IsTemporary ? TempEntity : Entity; |
| 7695 | InitListChecker PerformInitList(S, InitEntity, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 7696 | InitList, Ty, /*VerifyOnly=*/false, |
| 7697 | /*TreatUnavailableAsInvalid=*/false); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 7698 | if (PerformInitList.HadError()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7699 | return ExprError(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7700 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 7701 | // Hack: We must update *ResultType if available in order to set the |
| 7702 | // bounds of arrays, e.g. in 'int ar[] = {1, 2, 3};'. |
| 7703 | // Worst case: 'const int (&arref)[] = {1, 2, 3};'. |
| 7704 | if (ResultType && |
| 7705 | ResultType->getNonReferenceType()->isIncompleteArrayType()) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7706 | if ((*ResultType)->isRValueReferenceType()) |
| 7707 | Ty = S.Context.getRValueReferenceType(Ty); |
| 7708 | else if ((*ResultType)->isLValueReferenceType()) |
| 7709 | Ty = S.Context.getLValueReferenceType(Ty, |
| 7710 | (*ResultType)->getAs<LValueReferenceType>()->isSpelledAsLValue()); |
| 7711 | *ResultType = Ty; |
| 7712 | } |
| 7713 | |
| 7714 | InitListExpr *StructuredInitList = |
| 7715 | PerformInitList.getFullyStructuredList(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7716 | CurInit.get(); |
Richard Smith | d712d0d | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 7717 | CurInit = shouldBindAsTemporary(InitEntity) |
| 7718 | ? S.MaybeBindToTemporary(StructuredInitList) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7719 | : StructuredInitList; |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7720 | break; |
| 7721 | } |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 7722 | |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7723 | case SK_ConstructorInitializationFromList: { |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7724 | if (checkAbstractType(Step->Type)) |
| 7725 | return ExprError(); |
| 7726 | |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 7727 | // When an initializer list is passed for a parameter of type "reference |
| 7728 | // to object", we don't get an EK_Temporary entity, but instead an |
| 7729 | // EK_Parameter entity with reference type. |
Sebastian Redl | 99f6616 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 7730 | // FIXME: This is a hack. What we really should do is create a user |
| 7731 | // conversion step for this case, but this makes it considerably more |
| 7732 | // complicated. For now, this will do. |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 7733 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 7734 | Entity.getType().getNonReferenceType()); |
| 7735 | bool UseTemporary = Entity.getType()->isReferenceType(); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 7736 | assert(Args.size() == 1 && "expected a single argument for list init"); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7737 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Richard Smith | 2b349ae | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 7738 | S.Diag(InitList->getExprLoc(), diag::warn_cxx98_compat_ctor_list_init) |
| 7739 | << InitList->getSourceRange(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 7740 | MultiExprArg Arg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 7741 | CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity : |
| 7742 | Entity, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7743 | Kind, Arg, *Step, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 7744 | ConstructorInitRequiresZeroInit, |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7745 | /*IsListInitialization*/true, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 7746 | /*IsStdInitListInit*/false, |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 7747 | InitList->getLBraceLoc(), |
| 7748 | InitList->getRBraceLoc()); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 7749 | break; |
| 7750 | } |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 7751 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7752 | case SK_UnwrapInitList: |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7753 | CurInit = cast<InitListExpr>(CurInit.get())->getInit(0); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7754 | break; |
| 7755 | |
| 7756 | case SK_RewrapInitList: { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7757 | Expr *E = CurInit.get(); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7758 | InitListExpr *Syntactic = Step->WrappingSyntacticList; |
| 7759 | InitListExpr *ILE = new (S.Context) InitListExpr(S.Context, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 7760 | Syntactic->getLBraceLoc(), E, Syntactic->getRBraceLoc()); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7761 | ILE->setSyntacticForm(Syntactic); |
| 7762 | ILE->setType(E->getType()); |
| 7763 | ILE->setValueKind(E->getValueKind()); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7764 | CurInit = ILE; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7765 | break; |
| 7766 | } |
| 7767 | |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7768 | case SK_ConstructorInitialization: |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 7769 | case SK_StdInitializerListConstructorCall: { |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7770 | if (checkAbstractType(Step->Type)) |
| 7771 | return ExprError(); |
| 7772 | |
Sebastian Redl | 99f6616 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 7773 | // When an initializer list is passed for a parameter of type "reference |
| 7774 | // to object", we don't get an EK_Temporary entity, but instead an |
| 7775 | // EK_Parameter entity with reference type. |
| 7776 | // FIXME: This is a hack. What we really should do is create a user |
| 7777 | // conversion step for this case, but this makes it considerably more |
| 7778 | // complicated. For now, this will do. |
| 7779 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 7780 | Entity.getType().getNonReferenceType()); |
| 7781 | bool UseTemporary = Entity.getType()->isReferenceType(); |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 7782 | bool IsStdInitListInit = |
| 7783 | Step->Kind == SK_StdInitializerListConstructorCall; |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7784 | Expr *Source = CurInit.get(); |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 7785 | SourceRange Range = Kind.hasParenOrBraceRange() |
| 7786 | ? Kind.getParenOrBraceRange() |
| 7787 | : SourceRange(); |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7788 | CurInit = PerformConstructorInitialization( |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7789 | S, UseTemporary ? TempEntity : Entity, Kind, |
| 7790 | Source ? MultiExprArg(Source) : Args, *Step, |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7791 | ConstructorInitRequiresZeroInit, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7792 | /*IsListInitialization*/ IsStdInitListInit, |
| 7793 | /*IsStdInitListInitialization*/ IsStdInitListInit, |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 7794 | /*LBraceLoc*/ Range.getBegin(), |
| 7795 | /*RBraceLoc*/ Range.getEnd()); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 7796 | break; |
Sebastian Redl | 99f6616 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 7797 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7798 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 7799 | case SK_ZeroInitialization: { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7800 | step_iterator NextStep = Step; |
| 7801 | ++NextStep; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7802 | if (NextStep != StepEnd && |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 7803 | (NextStep->Kind == SK_ConstructorInitialization || |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7804 | NextStep->Kind == SK_ConstructorInitializationFromList)) { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7805 | // The need for zero-initialization is recorded directly into |
| 7806 | // the call to the object's constructor within the next step. |
| 7807 | ConstructorInitRequiresZeroInit = true; |
| 7808 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7809 | S.getLangOpts().CPlusPlus && |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7810 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 7811 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 7812 | if (!TSInfo) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7813 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 7814 | Kind.getRange().getBegin()); |
| 7815 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7816 | CurInit = new (S.Context) CXXScalarValueInitExpr( |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 7817 | Entity.getType().getNonLValueExprType(S.Context), TSInfo, |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7818 | Kind.getRange().getEnd()); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7819 | } else { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7820 | CurInit = new (S.Context) ImplicitValueInitExpr(Step->Type); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7821 | } |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 7822 | break; |
| 7823 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7824 | |
| 7825 | case SK_CAssignment: { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7826 | QualType SourceType = CurInit.get()->getType(); |
George Burgess IV | 5f21c71 | 2015-10-12 19:57:04 +0000 | [diff] [blame] | 7827 | // Save off the initial CurInit in case we need to emit a diagnostic |
| 7828 | ExprResult InitialCurInit = CurInit; |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7829 | ExprResult Result = CurInit; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7830 | Sema::AssignConvertType ConvTy = |
Fariborz Jahanian | 25eef19 | 2013-07-31 21:40:51 +0000 | [diff] [blame] | 7831 | S.CheckSingleAssignmentConstraints(Step->Type, Result, true, |
| 7832 | Entity.getKind() == InitializedEntity::EK_Parameter_CF_Audited); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7833 | if (Result.isInvalid()) |
| 7834 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7835 | CurInit = Result; |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 7836 | |
| 7837 | // If this is a call, allow conversion to a transparent union. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7838 | ExprResult CurInitExprRes = CurInit; |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 7839 | if (ConvTy != Sema::Compatible && |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 7840 | Entity.isParameterKind() && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7841 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes) |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 7842 | == Sema::Compatible) |
| 7843 | ConvTy = Sema::Compatible; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7844 | if (CurInitExprRes.isInvalid()) |
| 7845 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7846 | CurInit = CurInitExprRes; |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 7847 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 7848 | bool Complained; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7849 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 7850 | Step->Type, SourceType, |
George Burgess IV | 5f21c71 | 2015-10-12 19:57:04 +0000 | [diff] [blame] | 7851 | InitialCurInit.get(), |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 7852 | getAssignmentAction(Entity, true), |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 7853 | &Complained)) { |
| 7854 | PrintInitLocationNote(S, Entity); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7855 | return ExprError(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 7856 | } else if (Complained) |
| 7857 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7858 | break; |
| 7859 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 7860 | |
| 7861 | case SK_StringInit: { |
| 7862 | QualType Ty = Step->Type; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7863 | CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty, |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 7864 | S.Context.getAsArrayType(Ty), S); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 7865 | break; |
| 7866 | } |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 7867 | |
| 7868 | case SK_ObjCObjectConversion: |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7869 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7870 | CK_ObjCObjectLValueCast, |
Eli Friedman | be4b363 | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 7871 | CurInit.get()->getValueKind()); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 7872 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7873 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7874 | case SK_ArrayLoopIndex: { |
| 7875 | Expr *Cur = CurInit.get(); |
| 7876 | Expr *BaseExpr = new (S.Context) |
| 7877 | OpaqueValueExpr(Cur->getExprLoc(), Cur->getType(), |
| 7878 | Cur->getValueKind(), Cur->getObjectKind(), Cur); |
| 7879 | Expr *IndexExpr = |
| 7880 | new (S.Context) ArrayInitIndexExpr(S.Context.getSizeType()); |
| 7881 | CurInit = S.CreateBuiltinArraySubscriptExpr( |
| 7882 | BaseExpr, Kind.getLocation(), IndexExpr, Kind.getLocation()); |
| 7883 | ArrayLoopCommonExprs.push_back(BaseExpr); |
| 7884 | break; |
| 7885 | } |
| 7886 | |
| 7887 | case SK_ArrayLoopInit: { |
| 7888 | assert(!ArrayLoopCommonExprs.empty() && |
| 7889 | "mismatched SK_ArrayLoopIndex and SK_ArrayLoopInit"); |
| 7890 | Expr *Common = ArrayLoopCommonExprs.pop_back_val(); |
| 7891 | CurInit = new (S.Context) ArrayInitLoopExpr(Step->Type, Common, |
| 7892 | CurInit.get()); |
| 7893 | break; |
| 7894 | } |
| 7895 | |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 7896 | case SK_GNUArrayInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7897 | // Okay: we checked everything before creating this step. Note that |
| 7898 | // this is a GNU extension. |
| 7899 | S.Diag(Kind.getLocation(), diag::ext_array_init_copy) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7900 | << Step->Type << CurInit.get()->getType() |
| 7901 | << CurInit.get()->getSourceRange(); |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 7902 | LLVM_FALLTHROUGH; |
| 7903 | case SK_ArrayInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7904 | // If the destination type is an incomplete array type, update the |
| 7905 | // type accordingly. |
| 7906 | if (ResultType) { |
| 7907 | if (const IncompleteArrayType *IncompleteDest |
| 7908 | = S.Context.getAsIncompleteArrayType(Step->Type)) { |
| 7909 | if (const ConstantArrayType *ConstantSource |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7910 | = S.Context.getAsConstantArrayType(CurInit.get()->getType())) { |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7911 | *ResultType = S.Context.getConstantArrayType( |
| 7912 | IncompleteDest->getElementType(), |
| 7913 | ConstantSource->getSize(), |
| 7914 | ArrayType::Normal, 0); |
| 7915 | } |
| 7916 | } |
| 7917 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7918 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7919 | |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 7920 | case SK_ParenthesizedArrayInit: |
| 7921 | // Okay: we checked everything before creating this step. Note that |
| 7922 | // this is a GNU extension. |
| 7923 | S.Diag(Kind.getLocation(), diag::ext_array_init_parens) |
| 7924 | << CurInit.get()->getSourceRange(); |
| 7925 | break; |
| 7926 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7927 | case SK_PassByIndirectCopyRestore: |
| 7928 | case SK_PassByIndirectRestore: |
| 7929 | checkIndirectCopyRestoreSource(S, CurInit.get()); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7930 | CurInit = new (S.Context) ObjCIndirectCopyRestoreExpr( |
| 7931 | CurInit.get(), Step->Type, |
| 7932 | Step->Kind == SK_PassByIndirectCopyRestore); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7933 | break; |
| 7934 | |
| 7935 | case SK_ProduceObjCObject: |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7936 | CurInit = |
| 7937 | ImplicitCastExpr::Create(S.Context, Step->Type, CK_ARCProduceObject, |
| 7938 | CurInit.get(), nullptr, VK_RValue); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7939 | break; |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 7940 | |
| 7941 | case SK_StdInitializerList: { |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 7942 | S.Diag(CurInit.get()->getExprLoc(), |
| 7943 | diag::warn_cxx98_compat_initializer_list_init) |
| 7944 | << CurInit.get()->getSourceRange(); |
Sebastian Redl | 249dee5 | 2012-03-05 19:35:43 +0000 | [diff] [blame] | 7945 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 7946 | // Materialize the temporary into memory. |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 7947 | MaterializeTemporaryExpr *MTE = S.CreateMaterializeTemporaryExpr( |
| 7948 | CurInit.get()->getType(), CurInit.get(), |
| 7949 | /*BoundToLvalueReference=*/false); |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 7950 | |
Florian Hahn | 0aa117d | 2018-07-17 09:23:31 +0000 | [diff] [blame] | 7951 | // Wrap it in a construction of a std::initializer_list<T>. |
| 7952 | CurInit = new (S.Context) CXXStdInitializerListExpr(Step->Type, MTE); |
Richard Smith | 0a9969b | 2018-07-17 00:11:41 +0000 | [diff] [blame] | 7953 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 7954 | // Bind the result, in case the library has given initializer_list a |
| 7955 | // non-trivial destructor. |
| 7956 | if (shouldBindAsTemporary(Entity)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7957 | CurInit = S.MaybeBindToTemporary(CurInit.get()); |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 7958 | break; |
| 7959 | } |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 7960 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 7961 | case SK_OCLSamplerInit: { |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 7962 | // Sampler initialzation have 5 cases: |
| 7963 | // 1. function argument passing |
| 7964 | // 1a. argument is a file-scope variable |
| 7965 | // 1b. argument is a function-scope variable |
| 7966 | // 1c. argument is one of caller function's parameters |
| 7967 | // 2. variable initialization |
| 7968 | // 2a. initializing a file-scope variable |
| 7969 | // 2b. initializing a function-scope variable |
| 7970 | // |
| 7971 | // For file-scope variables, since they cannot be initialized by function |
| 7972 | // call of __translate_sampler_initializer in LLVM IR, their references |
| 7973 | // need to be replaced by a cast from their literal initializers to |
| 7974 | // sampler type. Since sampler variables can only be used in function |
| 7975 | // calls as arguments, we only need to replace them when handling the |
| 7976 | // argument passing. |
| 7977 | assert(Step->Type->isSamplerT() && |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 7978 | "Sampler initialization on non-sampler type."); |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 7979 | Expr *Init = CurInit.get(); |
| 7980 | QualType SourceType = Init->getType(); |
| 7981 | // Case 1 |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 7982 | if (Entity.isParameterKind()) { |
Egor Churaev | a8d2451 | 2017-04-05 09:02:56 +0000 | [diff] [blame] | 7983 | if (!SourceType->isSamplerT() && !SourceType->isIntegerType()) { |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 7984 | S.Diag(Kind.getLocation(), diag::err_sampler_argument_required) |
| 7985 | << SourceType; |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 7986 | break; |
| 7987 | } else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Init)) { |
| 7988 | auto Var = cast<VarDecl>(DRE->getDecl()); |
| 7989 | // Case 1b and 1c |
| 7990 | // No cast from integer to sampler is needed. |
| 7991 | if (!Var->hasGlobalStorage()) { |
| 7992 | CurInit = ImplicitCastExpr::Create(S.Context, Step->Type, |
| 7993 | CK_LValueToRValue, Init, |
| 7994 | /*BasePath=*/nullptr, VK_RValue); |
| 7995 | break; |
| 7996 | } |
| 7997 | // Case 1a |
| 7998 | // For function call with a file-scope sampler variable as argument, |
| 7999 | // get the integer literal. |
| 8000 | // Do not diagnose if the file-scope variable does not have initializer |
| 8001 | // since this has already been diagnosed when parsing the variable |
| 8002 | // declaration. |
| 8003 | if (!Var->getInit() || !isa<ImplicitCastExpr>(Var->getInit())) |
| 8004 | break; |
| 8005 | Init = cast<ImplicitCastExpr>(const_cast<Expr*>( |
| 8006 | Var->getInit()))->getSubExpr(); |
| 8007 | SourceType = Init->getType(); |
| 8008 | } |
| 8009 | } else { |
| 8010 | // Case 2 |
| 8011 | // Check initializer is 32 bit integer constant. |
| 8012 | // If the initializer is taken from global variable, do not diagnose since |
| 8013 | // this has already been done when parsing the variable declaration. |
| 8014 | if (!Init->isConstantInitializer(S.Context, false)) |
| 8015 | break; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8016 | |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 8017 | if (!SourceType->isIntegerType() || |
| 8018 | 32 != S.Context.getIntWidth(SourceType)) { |
| 8019 | S.Diag(Kind.getLocation(), diag::err_sampler_initializer_not_integer) |
| 8020 | << SourceType; |
| 8021 | break; |
| 8022 | } |
| 8023 | |
| 8024 | llvm::APSInt Result; |
| 8025 | Init->EvaluateAsInt(Result, S.Context); |
| 8026 | const uint64_t SamplerValue = Result.getLimitedValue(); |
| 8027 | // 32-bit value of sampler's initializer is interpreted as |
| 8028 | // bit-field with the following structure: |
| 8029 | // |unspecified|Filter|Addressing Mode| Normalized Coords| |
| 8030 | // |31 6|5 4|3 1| 0| |
| 8031 | // This structure corresponds to enum values of sampler properties |
| 8032 | // defined in SPIR spec v1.2 and also opencl-c.h |
| 8033 | unsigned AddressingMode = (0x0E & SamplerValue) >> 1; |
| 8034 | unsigned FilterMode = (0x30 & SamplerValue) >> 4; |
| 8035 | if (FilterMode != 1 && FilterMode != 2) |
| 8036 | S.Diag(Kind.getLocation(), |
| 8037 | diag::warn_sampler_initializer_invalid_bits) |
| 8038 | << "Filter Mode"; |
| 8039 | if (AddressingMode > 4) |
| 8040 | S.Diag(Kind.getLocation(), |
| 8041 | diag::warn_sampler_initializer_invalid_bits) |
| 8042 | << "Addressing Mode"; |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 8043 | } |
| 8044 | |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 8045 | // Cases 1a, 2a and 2b |
| 8046 | // Insert cast from integer to sampler. |
| 8047 | CurInit = S.ImpCastExprToType(Init, S.Context.OCLSamplerTy, |
| 8048 | CK_IntToOCLSampler); |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 8049 | break; |
| 8050 | } |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 8051 | case SK_OCLZeroEvent: { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8052 | assert(Step->Type->isEventT() && |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 8053 | "Event initialization on non-event type."); |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 8054 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 8055 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 8056 | CK_ZeroToOCLEvent, |
| 8057 | CurInit.get()->getValueKind()); |
| 8058 | break; |
| 8059 | } |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 8060 | case SK_OCLZeroQueue: { |
| 8061 | assert(Step->Type->isQueueT() && |
| 8062 | "Event initialization on non queue type."); |
| 8063 | |
| 8064 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
| 8065 | CK_ZeroToOCLQueue, |
| 8066 | CurInit.get()->getValueKind()); |
| 8067 | break; |
| 8068 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8069 | } |
| 8070 | } |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 8071 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 8072 | // Check whether the initializer has a shorter lifetime than the initialized |
| 8073 | // entity, and if not, either lifetime-extend or warn as appropriate. |
| 8074 | if (auto *Init = CurInit.get()) |
| 8075 | S.checkInitializerLifetime(Entity, Init); |
| 8076 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 8077 | // Diagnose non-fatal problems with the completed initialization. |
| 8078 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 8079 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 8080 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 8081 | cast<FieldDecl>(Entity.getDecl()), |
| 8082 | CurInit.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8083 | |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 8084 | // Check for std::move on construction. |
| 8085 | if (const Expr *E = CurInit.get()) { |
| 8086 | CheckMoveOnConstruction(S, E, |
| 8087 | Entity.getKind() == InitializedEntity::EK_Result); |
| 8088 | } |
| 8089 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 8090 | return CurInit; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8091 | } |
| 8092 | |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 8093 | /// Somewhere within T there is an uninitialized reference subobject. |
| 8094 | /// Dig it out and diagnose it. |
Benjamin Kramer | 3e35026 | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 8095 | static bool DiagnoseUninitializedReference(Sema &S, SourceLocation Loc, |
| 8096 | QualType T) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 8097 | if (T->isReferenceType()) { |
| 8098 | S.Diag(Loc, diag::err_reference_without_init) |
| 8099 | << T.getNonReferenceType(); |
| 8100 | return true; |
| 8101 | } |
| 8102 | |
| 8103 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 8104 | if (!RD || !RD->hasUninitializedReferenceMember()) |
| 8105 | return false; |
| 8106 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 8107 | for (const auto *FI : RD->fields()) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 8108 | if (FI->isUnnamedBitfield()) |
| 8109 | continue; |
| 8110 | |
| 8111 | if (DiagnoseUninitializedReference(S, FI->getLocation(), FI->getType())) { |
| 8112 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 8113 | return true; |
| 8114 | } |
| 8115 | } |
| 8116 | |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 8117 | for (const auto &BI : RD->bases()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8118 | if (DiagnoseUninitializedReference(S, BI.getBeginLoc(), BI.getType())) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 8119 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 8120 | return true; |
| 8121 | } |
| 8122 | } |
| 8123 | |
| 8124 | return false; |
| 8125 | } |
| 8126 | |
| 8127 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8128 | //===----------------------------------------------------------------------===// |
| 8129 | // Diagnose initialization failures |
| 8130 | //===----------------------------------------------------------------------===// |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 8131 | |
| 8132 | /// Emit notes associated with an initialization that failed due to a |
| 8133 | /// "simple" conversion failure. |
| 8134 | static void emitBadConversionNotes(Sema &S, const InitializedEntity &entity, |
| 8135 | Expr *op) { |
| 8136 | QualType destType = entity.getType(); |
| 8137 | if (destType.getNonReferenceType()->isObjCObjectPointerType() && |
| 8138 | op->getType()->isObjCObjectPointerType()) { |
| 8139 | |
| 8140 | // Emit a possible note about the conversion failing because the |
| 8141 | // operand is a message send with a related result type. |
| 8142 | S.EmitRelatedResultTypeNote(op); |
| 8143 | |
| 8144 | // Emit a possible note about a return failing because we're |
| 8145 | // expecting a related result type. |
| 8146 | if (entity.getKind() == InitializedEntity::EK_Result) |
| 8147 | S.EmitRelatedResultTypeNoteForReturn(destType); |
| 8148 | } |
| 8149 | } |
| 8150 | |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 8151 | static void diagnoseListInit(Sema &S, const InitializedEntity &Entity, |
| 8152 | InitListExpr *InitList) { |
| 8153 | QualType DestType = Entity.getType(); |
| 8154 | |
| 8155 | QualType E; |
| 8156 | if (S.getLangOpts().CPlusPlus11 && S.isStdInitializerList(DestType, &E)) { |
| 8157 | QualType ArrayType = S.Context.getConstantArrayType( |
| 8158 | E.withConst(), |
| 8159 | llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 8160 | InitList->getNumInits()), |
| 8161 | clang::ArrayType::Normal, 0); |
| 8162 | InitializedEntity HiddenArray = |
| 8163 | InitializedEntity::InitializeTemporary(ArrayType); |
| 8164 | return diagnoseListInit(S, HiddenArray, InitList); |
| 8165 | } |
| 8166 | |
Richard Smith | 8d082d1 | 2014-09-04 22:13:39 +0000 | [diff] [blame] | 8167 | if (DestType->isReferenceType()) { |
| 8168 | // A list-initialization failure for a reference means that we tried to |
| 8169 | // create a temporary of the inner type (per [dcl.init.list]p3.6) and the |
| 8170 | // inner initialization failed. |
| 8171 | QualType T = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 8172 | diagnoseListInit(S, InitializedEntity::InitializeTemporary(T), InitList); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8173 | SourceLocation Loc = InitList->getBeginLoc(); |
Richard Smith | 8d082d1 | 2014-09-04 22:13:39 +0000 | [diff] [blame] | 8174 | if (auto *D = Entity.getDecl()) |
| 8175 | Loc = D->getLocation(); |
| 8176 | S.Diag(Loc, diag::note_in_reference_temporary_list_initializer) << T; |
| 8177 | return; |
| 8178 | } |
| 8179 | |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 8180 | InitListChecker DiagnoseInitList(S, Entity, InitList, DestType, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 8181 | /*VerifyOnly=*/false, |
| 8182 | /*TreatUnavailableAsInvalid=*/false); |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 8183 | assert(DiagnoseInitList.HadError() && |
| 8184 | "Inconsistent init list check result."); |
| 8185 | } |
| 8186 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8187 | bool InitializationSequence::Diagnose(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8188 | const InitializedEntity &Entity, |
| 8189 | const InitializationKind &Kind, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8190 | ArrayRef<Expr *> Args) { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 8191 | if (!Failed()) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8192 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8193 | |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8194 | // When we want to diagnose only one element of a braced-init-list, |
| 8195 | // we need to factor it out. |
| 8196 | Expr *OnlyArg; |
| 8197 | if (Args.size() == 1) { |
| 8198 | auto *List = dyn_cast<InitListExpr>(Args[0]); |
| 8199 | if (List && List->getNumInits() == 1) |
| 8200 | OnlyArg = List->getInit(0); |
| 8201 | else |
| 8202 | OnlyArg = Args[0]; |
| 8203 | } |
| 8204 | else |
| 8205 | OnlyArg = nullptr; |
| 8206 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 8207 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8208 | switch (Failure) { |
| 8209 | case FK_TooManyInitsForReference: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8210 | // FIXME: Customize for the initialized entity? |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8211 | if (Args.empty()) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 8212 | // Dig out the reference subobject which is uninitialized and diagnose it. |
| 8213 | // If this is value-initialization, this could be nested some way within |
| 8214 | // the target type. |
| 8215 | assert(Kind.getKind() == InitializationKind::IK_Value || |
| 8216 | DestType->isReferenceType()); |
| 8217 | bool Diagnosed = |
| 8218 | DiagnoseUninitializedReference(S, Kind.getLocation(), DestType); |
| 8219 | assert(Diagnosed && "couldn't find uninitialized reference to diagnose"); |
| 8220 | (void)Diagnosed; |
| 8221 | } else // FIXME: diagnostic below could be better! |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8222 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 8223 | << SourceRange(Args.front()->getBeginLoc(), Args.back()->getEndLoc()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8224 | break; |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 8225 | case FK_ParenthesizedListInitForReference: |
| 8226 | S.Diag(Kind.getLocation(), diag::err_list_init_in_parens) |
| 8227 | << 1 << Entity.getType() << Args[0]->getSourceRange(); |
| 8228 | break; |
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 FK_ArrayNeedsInitList: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 8231 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 0; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8232 | break; |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 8233 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 8234 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 1; |
| 8235 | break; |
| 8236 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 8237 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 2; |
| 8238 | break; |
| 8239 | case FK_NarrowStringIntoWideCharArray: |
| 8240 | S.Diag(Kind.getLocation(), diag::err_array_init_narrow_string_into_wchar); |
| 8241 | break; |
| 8242 | case FK_WideStringIntoCharArray: |
| 8243 | S.Diag(Kind.getLocation(), diag::err_array_init_wide_string_into_char); |
| 8244 | break; |
| 8245 | case FK_IncompatWideStringIntoWideChar: |
| 8246 | S.Diag(Kind.getLocation(), |
| 8247 | diag::err_array_init_incompat_wide_string_into_wchar); |
| 8248 | break; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 8249 | case FK_PlainStringIntoUTF8Char: |
| 8250 | S.Diag(Kind.getLocation(), |
| 8251 | diag::err_array_init_plain_string_into_char8_t); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8252 | S.Diag(Args.front()->getBeginLoc(), |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 8253 | diag::note_array_init_plain_string_into_char8_t) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8254 | << FixItHint::CreateInsertion(Args.front()->getBeginLoc(), "u8"); |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 8255 | break; |
| 8256 | case FK_UTF8StringIntoPlainChar: |
| 8257 | S.Diag(Kind.getLocation(), |
| 8258 | diag::err_array_init_utf8_string_into_char); |
| 8259 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8260 | case FK_ArrayTypeMismatch: |
| 8261 | case FK_NonConstantArrayInit: |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 8262 | S.Diag(Kind.getLocation(), |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8263 | (Failure == FK_ArrayTypeMismatch |
| 8264 | ? diag::err_array_init_different_type |
| 8265 | : diag::err_array_init_non_constant_array)) |
| 8266 | << DestType.getNonReferenceType() |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8267 | << OnlyArg->getType() |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8268 | << Args[0]->getSourceRange(); |
| 8269 | break; |
| 8270 | |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 8271 | case FK_VariableLengthArrayHasInitializer: |
| 8272 | S.Diag(Kind.getLocation(), diag::err_variable_object_no_init) |
| 8273 | << Args[0]->getSourceRange(); |
| 8274 | break; |
| 8275 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8276 | case FK_AddressOfOverloadFailed: { |
| 8277 | DeclAccessPair Found; |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8278 | S.ResolveAddressOfOverloadedFunction(OnlyArg, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8279 | DestType.getNonReferenceType(), |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8280 | true, |
| 8281 | Found); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8282 | break; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8283 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8284 | |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 8285 | case FK_AddressOfUnaddressableFunction: { |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8286 | auto *FD = cast<FunctionDecl>(cast<DeclRefExpr>(OnlyArg)->getDecl()); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 8287 | S.checkAddressOfFunctionIsAvailable(FD, /*Complain=*/true, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8288 | OnlyArg->getBeginLoc()); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 8289 | break; |
| 8290 | } |
| 8291 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8292 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 8293 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8294 | switch (FailedOverloadResult) { |
| 8295 | case OR_Ambiguous: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8296 | if (Failure == FK_UserConversionOverloadFailed) |
| 8297 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8298 | << OnlyArg->getType() << DestType |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8299 | << Args[0]->getSourceRange(); |
| 8300 | else |
| 8301 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8302 | << DestType << OnlyArg->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8303 | << Args[0]->getSourceRange(); |
| 8304 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8305 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8306 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8307 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8308 | case OR_No_Viable_Function: |
Larisse Voufo | 70bb43a | 2013-06-27 03:36:30 +0000 | [diff] [blame] | 8309 | if (!S.RequireCompleteType(Kind.getLocation(), |
Larisse Voufo | 64cf3ef | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 8310 | DestType.getNonReferenceType(), |
| 8311 | diag::err_typecheck_nonviable_condition_incomplete, |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8312 | OnlyArg->getType(), Args[0]->getSourceRange())) |
Larisse Voufo | 64cf3ef | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 8313 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
Nick Lewycky | 08426e2 | 2015-08-25 22:18:46 +0000 | [diff] [blame] | 8314 | << (Entity.getKind() == InitializedEntity::EK_Result) |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8315 | << OnlyArg->getType() << Args[0]->getSourceRange() |
Larisse Voufo | 64cf3ef | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 8316 | << DestType.getNonReferenceType(); |
| 8317 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8318 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8319 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8320 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8321 | case OR_Deleted: { |
| 8322 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8323 | << OnlyArg->getType() << DestType.getNonReferenceType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8324 | << Args[0]->getSourceRange(); |
| 8325 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8326 | OverloadingResult Ovl |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 8327 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8328 | if (Ovl == OR_Deleted) { |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 8329 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8330 | } else { |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 8331 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8332 | } |
| 8333 | break; |
| 8334 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8335 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8336 | case OR_Success: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 8337 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8338 | } |
| 8339 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8340 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8341 | case FK_NonConstLValueReferenceBindingToTemporary: |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8342 | if (isa<InitListExpr>(Args[0])) { |
| 8343 | S.Diag(Kind.getLocation(), |
| 8344 | diag::err_lvalue_reference_bind_to_initlist) |
| 8345 | << DestType.getNonReferenceType().isVolatileQualified() |
| 8346 | << DestType.getNonReferenceType() |
| 8347 | << Args[0]->getSourceRange(); |
| 8348 | break; |
| 8349 | } |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 8350 | LLVM_FALLTHROUGH; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8351 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8352 | case FK_NonConstLValueReferenceBindingToUnrelated: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8353 | S.Diag(Kind.getLocation(), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8354 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 8355 | ? diag::err_lvalue_reference_bind_to_temporary |
| 8356 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | d1e0864 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 8357 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8358 | << DestType.getNonReferenceType() |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8359 | << OnlyArg->getType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8360 | << Args[0]->getSourceRange(); |
| 8361 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8362 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8363 | case FK_NonConstLValueReferenceBindingToBitfield: { |
| 8364 | // We don't necessarily have an unambiguous source bit-field. |
| 8365 | FieldDecl *BitField = Args[0]->getSourceBitField(); |
| 8366 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
| 8367 | << DestType.isVolatileQualified() |
| 8368 | << (BitField ? BitField->getDeclName() : DeclarationName()) |
| 8369 | << (BitField != nullptr) |
| 8370 | << Args[0]->getSourceRange(); |
| 8371 | if (BitField) |
| 8372 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
| 8373 | break; |
| 8374 | } |
| 8375 | |
| 8376 | case FK_NonConstLValueReferenceBindingToVectorElement: |
| 8377 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 8378 | << DestType.isVolatileQualified() |
| 8379 | << Args[0]->getSourceRange(); |
| 8380 | break; |
| 8381 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8382 | case FK_RValueReferenceBindingToLValue: |
| 8383 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8384 | << DestType.getNonReferenceType() << OnlyArg->getType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8385 | << Args[0]->getSourceRange(); |
| 8386 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8387 | |
Richard Trieu | f956a49 | 2015-05-16 01:27:03 +0000 | [diff] [blame] | 8388 | case FK_ReferenceInitDropsQualifiers: { |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8389 | QualType SourceType = OnlyArg->getType(); |
Richard Trieu | f956a49 | 2015-05-16 01:27:03 +0000 | [diff] [blame] | 8390 | QualType NonRefType = DestType.getNonReferenceType(); |
| 8391 | Qualifiers DroppedQualifiers = |
| 8392 | SourceType.getQualifiers() - NonRefType.getQualifiers(); |
| 8393 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8394 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
Richard Trieu | f956a49 | 2015-05-16 01:27:03 +0000 | [diff] [blame] | 8395 | << SourceType |
| 8396 | << NonRefType |
| 8397 | << DroppedQualifiers.getCVRQualifiers() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8398 | << Args[0]->getSourceRange(); |
| 8399 | break; |
Richard Trieu | f956a49 | 2015-05-16 01:27:03 +0000 | [diff] [blame] | 8400 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8401 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8402 | case FK_ReferenceInitFailed: |
| 8403 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 8404 | << DestType.getNonReferenceType() |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8405 | << OnlyArg->isLValue() |
| 8406 | << OnlyArg->getType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8407 | << Args[0]->getSourceRange(); |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 8408 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8409 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8410 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8411 | case FK_ConversionFailed: { |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8412 | QualType FromType = OnlyArg->getType(); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8413 | PartialDiagnostic PDiag = S.PDiag(diag::err_init_conversion_failed) |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8414 | << (int)Entity.getKind() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8415 | << DestType |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8416 | << OnlyArg->isLValue() |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8417 | << FromType |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8418 | << Args[0]->getSourceRange(); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8419 | S.HandleFunctionTypeMismatch(PDiag, FromType, DestType); |
| 8420 | S.Diag(Kind.getLocation(), PDiag); |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 8421 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8422 | break; |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8423 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8424 | |
| 8425 | case FK_ConversionFromPropertyFailed: |
| 8426 | // No-op. This error has already been reported. |
| 8427 | break; |
| 8428 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8429 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 8430 | SourceRange R; |
| 8431 | |
David Majnemer | bd38544 | 2015-04-10 04:52:06 +0000 | [diff] [blame] | 8432 | auto *InitList = dyn_cast<InitListExpr>(Args[0]); |
Benjamin Kramer | c4284e3 | 2015-09-23 16:03:53 +0000 | [diff] [blame] | 8433 | if (InitList && InitList->getNumInits() >= 1) { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 8434 | R = SourceRange(InitList->getInit(0)->getEndLoc(), InitList->getEndLoc()); |
Benjamin Kramer | c4284e3 | 2015-09-23 16:03:53 +0000 | [diff] [blame] | 8435 | } else { |
| 8436 | assert(Args.size() > 1 && "Expected multiple initializers!"); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 8437 | R = SourceRange(Args.front()->getEndLoc(), Args.back()->getEndLoc()); |
Benjamin Kramer | c4284e3 | 2015-09-23 16:03:53 +0000 | [diff] [blame] | 8438 | } |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8439 | |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 8440 | R.setBegin(S.getLocForEndOfToken(R.getBegin())); |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 8441 | if (Kind.isCStyleOrFunctionalCast()) |
| 8442 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 8443 | << R; |
| 8444 | else |
| 8445 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 8446 | << /*scalar=*/2 << R; |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8447 | break; |
| 8448 | } |
| 8449 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 8450 | case FK_ParenthesizedListInitForScalar: |
| 8451 | S.Diag(Kind.getLocation(), diag::err_list_init_in_parens) |
| 8452 | << 0 << Entity.getType() << Args[0]->getSourceRange(); |
| 8453 | break; |
| 8454 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8455 | case FK_ReferenceBindingToInitList: |
| 8456 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 8457 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 8458 | break; |
| 8459 | |
| 8460 | case FK_InitListBadDestinationType: |
| 8461 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 8462 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 8463 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8464 | |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 8465 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8466 | case FK_ConstructorOverloadFailed: { |
| 8467 | SourceRange ArgsRange; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8468 | if (Args.size()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8469 | ArgsRange = |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 8470 | SourceRange(Args.front()->getBeginLoc(), Args.back()->getEndLoc()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8471 | |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 8472 | if (Failure == FK_ListConstructorOverloadFailed) { |
Nico Weber | 9709ebf | 2014-07-08 23:54:25 +0000 | [diff] [blame] | 8473 | assert(Args.size() == 1 && |
| 8474 | "List construction from other than 1 argument."); |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 8475 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8476 | Args = MultiExprArg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 8477 | } |
| 8478 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8479 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 8480 | // bad. |
| 8481 | switch (FailedOverloadResult) { |
| 8482 | case OR_Ambiguous: |
| 8483 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 8484 | << DestType << ArgsRange; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8485 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8486 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8487 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8488 | case OR_No_Viable_Function: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8489 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 8490 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 8491 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 8492 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 8493 | // This is implicit default initialization of a member or |
| 8494 | // base within a constructor. If no viable function was |
Nico Weber | a691689 | 2016-06-10 18:53:04 +0000 | [diff] [blame] | 8495 | // found, notify the user that they need to explicitly |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8496 | // initialize this base/member. |
| 8497 | CXXConstructorDecl *Constructor |
| 8498 | = cast<CXXConstructorDecl>(S.CurContext); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 8499 | const CXXRecordDecl *InheritedFrom = nullptr; |
| 8500 | if (auto Inherited = Constructor->getInheritedConstructor()) |
| 8501 | InheritedFrom = Inherited.getShadowDecl()->getNominatedBaseClass(); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8502 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 8503 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 8504 | << (InheritedFrom ? 2 : Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8505 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 8506 | << /*base=*/0 |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 8507 | << Entity.getType() |
| 8508 | << InheritedFrom; |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8509 | |
| 8510 | RecordDecl *BaseDecl |
| 8511 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 8512 | ->getDecl(); |
| 8513 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 8514 | << S.Context.getTagDeclType(BaseDecl); |
| 8515 | } else { |
| 8516 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 8517 | << (InheritedFrom ? 2 : Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8518 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 8519 | << /*member=*/1 |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 8520 | << Entity.getName() |
| 8521 | << InheritedFrom; |
Alp Toker | 2afa878 | 2014-05-28 12:20:14 +0000 | [diff] [blame] | 8522 | S.Diag(Entity.getDecl()->getLocation(), |
| 8523 | diag::note_member_declared_at); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8524 | |
| 8525 | if (const RecordType *Record |
| 8526 | = Entity.getType()->getAs<RecordType>()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8527 | S.Diag(Record->getDecl()->getLocation(), |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8528 | diag::note_previous_decl) |
| 8529 | << S.Context.getTagDeclType(Record->getDecl()); |
| 8530 | } |
| 8531 | break; |
| 8532 | } |
| 8533 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8534 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 8535 | << DestType << ArgsRange; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8536 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8537 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8538 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8539 | case OR_Deleted: { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8540 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8541 | OverloadingResult Ovl |
| 8542 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 8543 | if (Ovl != OR_Deleted) { |
| 8544 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 8545 | << true << DestType << ArgsRange; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8546 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 8547 | break; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8548 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8549 | |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 8550 | // If this is a defaulted or implicitly-declared function, then |
| 8551 | // it was implicitly deleted. Make it clear that the deletion was |
| 8552 | // implicit. |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 8553 | if (S.isImplicitlyDeleted(Best->Function)) |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 8554 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_special_init) |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 8555 | << S.getSpecialMember(cast<CXXMethodDecl>(Best->Function)) |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 8556 | << DestType << ArgsRange; |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 8557 | else |
| 8558 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 8559 | << true << DestType << ArgsRange; |
| 8560 | |
| 8561 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8562 | break; |
| 8563 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8564 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8565 | case OR_Success: |
| 8566 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8567 | } |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8568 | } |
David Blaikie | 60deeee | 2012-01-17 08:24:58 +0000 | [diff] [blame] | 8569 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8570 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 8571 | case FK_DefaultInitOfConst: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8572 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 8573 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 8574 | // This is implicit default-initialization of a const member in |
| 8575 | // a constructor. Complain that it needs to be explicitly |
| 8576 | // initialized. |
| 8577 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 8578 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 8579 | << (Constructor->getInheritedConstructor() ? 2 : |
| 8580 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8581 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 8582 | << /*const=*/1 |
| 8583 | << Entity.getName(); |
| 8584 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 8585 | << Entity.getName(); |
| 8586 | } else { |
| 8587 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
Nico Weber | 9386c82 | 2014-07-23 05:16:10 +0000 | [diff] [blame] | 8588 | << DestType << (bool)DestType->getAs<RecordType>(); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8589 | } |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 8590 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8591 | |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 8592 | case FK_Incomplete: |
Douglas Gregor | 85f3423 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 8593 | S.RequireCompleteType(Kind.getLocation(), FailedIncompleteType, |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 8594 | diag::err_init_incomplete_type); |
| 8595 | break; |
| 8596 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 8597 | case FK_ListInitializationFailed: { |
| 8598 | // Run the init list checker again to emit diagnostics. |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 8599 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
| 8600 | diagnoseListInit(S, Entity, InitList); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 8601 | break; |
| 8602 | } |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 8603 | |
| 8604 | case FK_PlaceholderType: { |
| 8605 | // FIXME: Already diagnosed! |
| 8606 | break; |
| 8607 | } |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 8608 | |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 8609 | case FK_ExplicitConstructor: { |
| 8610 | S.Diag(Kind.getLocation(), diag::err_selected_explicit_constructor) |
| 8611 | << Args[0]->getSourceRange(); |
| 8612 | OverloadCandidateSet::iterator Best; |
| 8613 | OverloadingResult Ovl |
| 8614 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Matt Beaumont-Gay | 5dcce09 | 2012-04-02 19:05:35 +0000 | [diff] [blame] | 8615 | (void)Ovl; |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 8616 | assert(Ovl == OR_Success && "Inconsistent overload resolution"); |
| 8617 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 8618 | S.Diag(CtorDecl->getLocation(), |
| 8619 | diag::note_explicit_ctor_deduction_guide_here) << false; |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 8620 | break; |
| 8621 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8622 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8623 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 8624 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8625 | return true; |
| 8626 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8627 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8628 | void InitializationSequence::dump(raw_ostream &OS) const { |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8629 | switch (SequenceKind) { |
| 8630 | case FailedSequence: { |
| 8631 | OS << "Failed sequence: "; |
| 8632 | switch (Failure) { |
| 8633 | case FK_TooManyInitsForReference: |
| 8634 | OS << "too many initializers for reference"; |
| 8635 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8636 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 8637 | case FK_ParenthesizedListInitForReference: |
| 8638 | OS << "parenthesized list init for reference"; |
| 8639 | break; |
| 8640 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8641 | case FK_ArrayNeedsInitList: |
| 8642 | OS << "array requires initializer list"; |
| 8643 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8644 | |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 8645 | case FK_AddressOfUnaddressableFunction: |
| 8646 | OS << "address of unaddressable function was taken"; |
| 8647 | break; |
| 8648 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8649 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 8650 | OS << "array requires initializer list or string literal"; |
| 8651 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8652 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 8653 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 8654 | OS << "array requires initializer list or wide string literal"; |
| 8655 | break; |
| 8656 | |
| 8657 | case FK_NarrowStringIntoWideCharArray: |
| 8658 | OS << "narrow string into wide char array"; |
| 8659 | break; |
| 8660 | |
| 8661 | case FK_WideStringIntoCharArray: |
| 8662 | OS << "wide string into char array"; |
| 8663 | break; |
| 8664 | |
| 8665 | case FK_IncompatWideStringIntoWideChar: |
| 8666 | OS << "incompatible wide string into wide char array"; |
| 8667 | break; |
| 8668 | |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 8669 | case FK_PlainStringIntoUTF8Char: |
| 8670 | OS << "plain string literal into char8_t array"; |
| 8671 | break; |
| 8672 | |
| 8673 | case FK_UTF8StringIntoPlainChar: |
| 8674 | OS << "u8 string literal into char array"; |
| 8675 | break; |
| 8676 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8677 | case FK_ArrayTypeMismatch: |
| 8678 | OS << "array type mismatch"; |
| 8679 | break; |
| 8680 | |
| 8681 | case FK_NonConstantArrayInit: |
| 8682 | OS << "non-constant array initializer"; |
| 8683 | break; |
| 8684 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8685 | case FK_AddressOfOverloadFailed: |
| 8686 | OS << "address of overloaded function failed"; |
| 8687 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8688 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8689 | case FK_ReferenceInitOverloadFailed: |
| 8690 | OS << "overload resolution for reference initialization failed"; |
| 8691 | break; |
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 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 8694 | OS << "non-const lvalue reference bound to temporary"; |
| 8695 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8696 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8697 | case FK_NonConstLValueReferenceBindingToBitfield: |
| 8698 | OS << "non-const lvalue reference bound to bit-field"; |
| 8699 | break; |
| 8700 | |
| 8701 | case FK_NonConstLValueReferenceBindingToVectorElement: |
| 8702 | OS << "non-const lvalue reference bound to vector element"; |
| 8703 | break; |
| 8704 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8705 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 8706 | OS << "non-const lvalue reference bound to unrelated type"; |
| 8707 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8708 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8709 | case FK_RValueReferenceBindingToLValue: |
| 8710 | OS << "rvalue reference bound to an lvalue"; |
| 8711 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8712 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8713 | case FK_ReferenceInitDropsQualifiers: |
| 8714 | OS << "reference initialization drops qualifiers"; |
| 8715 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8716 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8717 | case FK_ReferenceInitFailed: |
| 8718 | OS << "reference initialization failed"; |
| 8719 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8720 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8721 | case FK_ConversionFailed: |
| 8722 | OS << "conversion failed"; |
| 8723 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8724 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8725 | case FK_ConversionFromPropertyFailed: |
| 8726 | OS << "conversion from property failed"; |
| 8727 | break; |
| 8728 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8729 | case FK_TooManyInitsForScalar: |
| 8730 | OS << "too many initializers for scalar"; |
| 8731 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8732 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 8733 | case FK_ParenthesizedListInitForScalar: |
| 8734 | OS << "parenthesized list init for reference"; |
| 8735 | break; |
| 8736 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8737 | case FK_ReferenceBindingToInitList: |
| 8738 | OS << "referencing binding to initializer list"; |
| 8739 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8740 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8741 | case FK_InitListBadDestinationType: |
| 8742 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 8743 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8744 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8745 | case FK_UserConversionOverloadFailed: |
| 8746 | OS << "overloading failed for user-defined conversion"; |
| 8747 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8748 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8749 | case FK_ConstructorOverloadFailed: |
| 8750 | OS << "constructor overloading failed"; |
| 8751 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8752 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8753 | case FK_DefaultInitOfConst: |
| 8754 | OS << "default initialization of a const variable"; |
| 8755 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8756 | |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 8757 | case FK_Incomplete: |
| 8758 | OS << "initialization of incomplete type"; |
| 8759 | break; |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 8760 | |
| 8761 | case FK_ListInitializationFailed: |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 8762 | OS << "list initialization checker failure"; |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 8763 | break; |
| 8764 | |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 8765 | case FK_VariableLengthArrayHasInitializer: |
| 8766 | OS << "variable length array has an initializer"; |
| 8767 | break; |
| 8768 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 8769 | case FK_PlaceholderType: |
| 8770 | OS << "initializer expression isn't contextually valid"; |
| 8771 | break; |
Nick Lewycky | 097f47c | 2011-12-22 20:21:32 +0000 | [diff] [blame] | 8772 | |
| 8773 | case FK_ListConstructorOverloadFailed: |
| 8774 | OS << "list constructor overloading failed"; |
| 8775 | break; |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 8776 | |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 8777 | case FK_ExplicitConstructor: |
| 8778 | OS << "list copy initialization chose explicit constructor"; |
| 8779 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8780 | } |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8781 | OS << '\n'; |
| 8782 | return; |
| 8783 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8784 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8785 | case DependentSequence: |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 8786 | OS << "Dependent sequence\n"; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8787 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8788 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 8789 | case NormalSequence: |
| 8790 | OS << "Normal sequence: "; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8791 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8792 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8793 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8794 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 8795 | if (S != step_begin()) { |
| 8796 | OS << " -> "; |
| 8797 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8798 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8799 | switch (S->Kind) { |
| 8800 | case SK_ResolveAddressOfOverloadedFunction: |
| 8801 | OS << "resolve address of overloaded function"; |
| 8802 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8803 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8804 | case SK_CastDerivedToBaseRValue: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8805 | OS << "derived-to-base (rvalue)"; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8806 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8807 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 8808 | case SK_CastDerivedToBaseXValue: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8809 | OS << "derived-to-base (xvalue)"; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 8810 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8811 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8812 | case SK_CastDerivedToBaseLValue: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8813 | OS << "derived-to-base (lvalue)"; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8814 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8815 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8816 | case SK_BindReference: |
| 8817 | OS << "bind reference to lvalue"; |
| 8818 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8819 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8820 | case SK_BindReferenceToTemporary: |
| 8821 | OS << "bind reference to a temporary"; |
| 8822 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8823 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8824 | case SK_FinalCopy: |
| 8825 | OS << "final copy in class direct-initialization"; |
| 8826 | break; |
| 8827 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 8828 | case SK_ExtraneousCopyToTemporary: |
| 8829 | OS << "extraneous C++03 copy to temporary"; |
| 8830 | break; |
| 8831 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8832 | case SK_UserConversion: |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 8833 | OS << "user-defined conversion via " << *S->Function.Function; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8834 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 8835 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8836 | case SK_QualificationConversionRValue: |
| 8837 | OS << "qualification conversion (rvalue)"; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8838 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8839 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 8840 | case SK_QualificationConversionXValue: |
| 8841 | OS << "qualification conversion (xvalue)"; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8842 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 8843 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8844 | case SK_QualificationConversionLValue: |
| 8845 | OS << "qualification conversion (lvalue)"; |
| 8846 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8847 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 8848 | case SK_AtomicConversion: |
| 8849 | OS << "non-atomic-to-atomic conversion"; |
| 8850 | break; |
| 8851 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 8852 | case SK_LValueToRValue: |
| 8853 | OS << "load (lvalue to rvalue)"; |
| 8854 | break; |
| 8855 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8856 | case SK_ConversionSequence: |
| 8857 | OS << "implicit conversion sequence ("; |
Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 8858 | S->ICS->dump(); // FIXME: use OS |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8859 | OS << ")"; |
| 8860 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8861 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 8862 | case SK_ConversionSequenceNoNarrowing: |
| 8863 | OS << "implicit conversion sequence with narrowing prohibited ("; |
Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 8864 | S->ICS->dump(); // FIXME: use OS |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 8865 | OS << ")"; |
| 8866 | break; |
| 8867 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8868 | case SK_ListInitialization: |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 8869 | OS << "list aggregate initialization"; |
| 8870 | break; |
| 8871 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8872 | case SK_UnwrapInitList: |
| 8873 | OS << "unwrap reference initializer list"; |
| 8874 | break; |
| 8875 | |
| 8876 | case SK_RewrapInitList: |
| 8877 | OS << "rewrap reference initializer list"; |
| 8878 | break; |
| 8879 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8880 | case SK_ConstructorInitialization: |
| 8881 | OS << "constructor initialization"; |
| 8882 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8883 | |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 8884 | case SK_ConstructorInitializationFromList: |
| 8885 | OS << "list initialization via constructor"; |
| 8886 | break; |
| 8887 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8888 | case SK_ZeroInitialization: |
| 8889 | OS << "zero initialization"; |
| 8890 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8891 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8892 | case SK_CAssignment: |
| 8893 | OS << "C assignment"; |
| 8894 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8895 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8896 | case SK_StringInit: |
| 8897 | OS << "string initialization"; |
| 8898 | break; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 8899 | |
| 8900 | case SK_ObjCObjectConversion: |
| 8901 | OS << "Objective-C object conversion"; |
| 8902 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8903 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 8904 | case SK_ArrayLoopIndex: |
| 8905 | OS << "indexing for array initialization loop"; |
| 8906 | break; |
| 8907 | |
| 8908 | case SK_ArrayLoopInit: |
| 8909 | OS << "array initialization loop"; |
| 8910 | break; |
| 8911 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8912 | case SK_ArrayInit: |
| 8913 | OS << "array initialization"; |
| 8914 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8915 | |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 8916 | case SK_GNUArrayInit: |
| 8917 | OS << "array initialization (GNU extension)"; |
| 8918 | break; |
| 8919 | |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 8920 | case SK_ParenthesizedArrayInit: |
| 8921 | OS << "parenthesized array initialization"; |
| 8922 | break; |
| 8923 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8924 | case SK_PassByIndirectCopyRestore: |
| 8925 | OS << "pass by indirect copy and restore"; |
| 8926 | break; |
| 8927 | |
| 8928 | case SK_PassByIndirectRestore: |
| 8929 | OS << "pass by indirect restore"; |
| 8930 | break; |
| 8931 | |
| 8932 | case SK_ProduceObjCObject: |
| 8933 | OS << "Objective-C object retension"; |
| 8934 | break; |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 8935 | |
| 8936 | case SK_StdInitializerList: |
| 8937 | OS << "std::initializer_list from initializer list"; |
| 8938 | break; |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 8939 | |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 8940 | case SK_StdInitializerListConstructorCall: |
| 8941 | OS << "list initialization from std::initializer_list"; |
| 8942 | break; |
| 8943 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 8944 | case SK_OCLSamplerInit: |
| 8945 | OS << "OpenCL sampler_t from integer constant"; |
| 8946 | break; |
| 8947 | |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 8948 | case SK_OCLZeroEvent: |
| 8949 | OS << "OpenCL event_t from zero"; |
| 8950 | break; |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 8951 | |
| 8952 | case SK_OCLZeroQueue: |
| 8953 | OS << "OpenCL queue_t from zero"; |
| 8954 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8955 | } |
Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 8956 | |
| 8957 | OS << " [" << S->Type.getAsString() << ']'; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8958 | } |
Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 8959 | |
| 8960 | OS << '\n'; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8961 | } |
| 8962 | |
| 8963 | void InitializationSequence::dump() const { |
| 8964 | dump(llvm::errs()); |
| 8965 | } |
| 8966 | |
Nico Weber | 3d7f00d | 2018-06-19 23:19:34 +0000 | [diff] [blame] | 8967 | static bool NarrowingErrs(const LangOptions &L) { |
| 8968 | return L.CPlusPlus11 && |
| 8969 | (!L.MicrosoftExt || L.isCompatibleWithMSVC(LangOptions::MSVC2015)); |
| 8970 | } |
| 8971 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 8972 | static void DiagnoseNarrowingInInitList(Sema &S, |
| 8973 | const ImplicitConversionSequence &ICS, |
| 8974 | QualType PreNarrowingType, |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8975 | QualType EntityType, |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8976 | const Expr *PostInit) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8977 | const StandardConversionSequence *SCS = nullptr; |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8978 | switch (ICS.getKind()) { |
| 8979 | case ImplicitConversionSequence::StandardConversion: |
| 8980 | SCS = &ICS.Standard; |
| 8981 | break; |
| 8982 | case ImplicitConversionSequence::UserDefinedConversion: |
| 8983 | SCS = &ICS.UserDefined.After; |
| 8984 | break; |
| 8985 | case ImplicitConversionSequence::AmbiguousConversion: |
| 8986 | case ImplicitConversionSequence::EllipsisConversion: |
| 8987 | case ImplicitConversionSequence::BadConversion: |
| 8988 | return; |
| 8989 | } |
| 8990 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8991 | // C++11 [dcl.init.list]p7: Check whether this is a narrowing conversion. |
| 8992 | APValue ConstantValue; |
Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 8993 | QualType ConstantType; |
| 8994 | switch (SCS->getNarrowingKind(S.Context, PostInit, ConstantValue, |
| 8995 | ConstantType)) { |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8996 | case NK_Not_Narrowing: |
Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 8997 | case NK_Dependent_Narrowing: |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8998 | // No narrowing occurred. |
| 8999 | return; |
| 9000 | |
| 9001 | case NK_Type_Narrowing: |
| 9002 | // This was a floating-to-integer conversion, which is always considered a |
| 9003 | // narrowing conversion even if the value is a constant and can be |
| 9004 | // represented exactly as an integer. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9005 | S.Diag(PostInit->getBeginLoc(), NarrowingErrs(S.getLangOpts()) |
Nico Weber | 3d7f00d | 2018-06-19 23:19:34 +0000 | [diff] [blame] | 9006 | ? diag::ext_init_list_type_narrowing |
| 9007 | : diag::warn_init_list_type_narrowing) |
| 9008 | << PostInit->getSourceRange() |
| 9009 | << PreNarrowingType.getLocalUnqualifiedType() |
| 9010 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9011 | break; |
| 9012 | |
| 9013 | case NK_Constant_Narrowing: |
| 9014 | // A constant value was narrowed. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9015 | S.Diag(PostInit->getBeginLoc(), |
Nico Weber | 3d7f00d | 2018-06-19 23:19:34 +0000 | [diff] [blame] | 9016 | NarrowingErrs(S.getLangOpts()) |
| 9017 | ? diag::ext_init_list_constant_narrowing |
| 9018 | : diag::warn_init_list_constant_narrowing) |
| 9019 | << PostInit->getSourceRange() |
| 9020 | << ConstantValue.getAsString(S.getASTContext(), ConstantType) |
| 9021 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9022 | break; |
| 9023 | |
| 9024 | case NK_Variable_Narrowing: |
| 9025 | // A variable's value may have been narrowed. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9026 | S.Diag(PostInit->getBeginLoc(), |
Nico Weber | 3d7f00d | 2018-06-19 23:19:34 +0000 | [diff] [blame] | 9027 | NarrowingErrs(S.getLangOpts()) |
| 9028 | ? diag::ext_init_list_variable_narrowing |
| 9029 | : diag::warn_init_list_variable_narrowing) |
| 9030 | << PostInit->getSourceRange() |
| 9031 | << PreNarrowingType.getLocalUnqualifiedType() |
| 9032 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9033 | break; |
| 9034 | } |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 9035 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 9036 | SmallString<128> StaticCast; |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 9037 | llvm::raw_svector_ostream OS(StaticCast); |
| 9038 | OS << "static_cast<"; |
| 9039 | if (const TypedefType *TT = EntityType->getAs<TypedefType>()) { |
| 9040 | // It's important to use the typedef's name if there is one so that the |
| 9041 | // fixit doesn't break code using types like int64_t. |
| 9042 | // |
| 9043 | // FIXME: This will break if the typedef requires qualification. But |
| 9044 | // getQualifiedNameAsString() includes non-machine-parsable components. |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 9045 | OS << *TT->getDecl(); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 9046 | } else if (const BuiltinType *BT = EntityType->getAs<BuiltinType>()) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9047 | OS << BT->getName(S.getLangOpts()); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 9048 | else { |
| 9049 | // Oops, we didn't find the actual type of the variable. Don't emit a fixit |
| 9050 | // with a broken cast. |
| 9051 | return; |
| 9052 | } |
| 9053 | OS << ">("; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9054 | S.Diag(PostInit->getBeginLoc(), diag::note_init_list_narrowing_silence) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 9055 | << PostInit->getSourceRange() |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9056 | << FixItHint::CreateInsertion(PostInit->getBeginLoc(), OS.str()) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 9057 | << FixItHint::CreateInsertion( |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 9058 | S.getLocForEndOfToken(PostInit->getEndLoc()), ")"); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 9059 | } |
| 9060 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 9061 | //===----------------------------------------------------------------------===// |
| 9062 | // Initialization helper functions |
| 9063 | //===----------------------------------------------------------------------===// |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 9064 | bool |
| 9065 | Sema::CanPerformCopyInitialization(const InitializedEntity &Entity, |
| 9066 | ExprResult Init) { |
| 9067 | if (Init.isInvalid()) |
| 9068 | return false; |
| 9069 | |
| 9070 | Expr *InitE = Init.get(); |
| 9071 | assert(InitE && "No initialization expression"); |
| 9072 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9073 | InitializationKind Kind = |
| 9074 | InitializationKind::CreateCopy(InitE->getBeginLoc(), SourceLocation()); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 9075 | InitializationSequence Seq(*this, Entity, Kind, InitE); |
Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 9076 | return !Seq.Failed(); |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 9077 | } |
| 9078 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9079 | ExprResult |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 9080 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 9081 | SourceLocation EqualLoc, |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 9082 | ExprResult Init, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 9083 | bool TopLevelOfInitList, |
| 9084 | bool AllowExplicit) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 9085 | if (Init.isInvalid()) |
| 9086 | return ExprError(); |
| 9087 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 9088 | Expr *InitE = Init.get(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 9089 | assert(InitE && "No initialization expression?"); |
| 9090 | |
| 9091 | if (EqualLoc.isInvalid()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9092 | EqualLoc = InitE->getBeginLoc(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 9093 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9094 | InitializationKind Kind = InitializationKind::CreateCopy( |
| 9095 | InitE->getBeginLoc(), EqualLoc, AllowExplicit); |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 9096 | InitializationSequence Seq(*this, Entity, Kind, InitE, TopLevelOfInitList); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 9097 | |
Alex Lorenz | de69ff9 | 2017-05-16 10:23:58 +0000 | [diff] [blame] | 9098 | // Prevent infinite recursion when performing parameter copy-initialization. |
| 9099 | const bool ShouldTrackCopy = |
| 9100 | Entity.isParameterKind() && Seq.isConstructorInitialization(); |
| 9101 | if (ShouldTrackCopy) { |
| 9102 | if (llvm::find(CurrentParameterCopyTypes, Entity.getType()) != |
| 9103 | CurrentParameterCopyTypes.end()) { |
| 9104 | Seq.SetOverloadFailure( |
| 9105 | InitializationSequence::FK_ConstructorOverloadFailed, |
| 9106 | OR_No_Viable_Function); |
| 9107 | |
| 9108 | // Try to give a meaningful diagnostic note for the problematic |
| 9109 | // constructor. |
| 9110 | const auto LastStep = Seq.step_end() - 1; |
| 9111 | assert(LastStep->Kind == |
| 9112 | InitializationSequence::SK_ConstructorInitialization); |
| 9113 | const FunctionDecl *Function = LastStep->Function.Function; |
| 9114 | auto Candidate = |
| 9115 | llvm::find_if(Seq.getFailedCandidateSet(), |
| 9116 | [Function](const OverloadCandidate &Candidate) -> bool { |
| 9117 | return Candidate.Viable && |
| 9118 | Candidate.Function == Function && |
| 9119 | Candidate.Conversions.size() > 0; |
| 9120 | }); |
| 9121 | if (Candidate != Seq.getFailedCandidateSet().end() && |
| 9122 | Function->getNumParams() > 0) { |
| 9123 | Candidate->Viable = false; |
| 9124 | Candidate->FailureKind = ovl_fail_bad_conversion; |
| 9125 | Candidate->Conversions[0].setBad(BadConversionSequence::no_conversion, |
| 9126 | InitE, |
| 9127 | Function->getParamDecl(0)->getType()); |
| 9128 | } |
| 9129 | } |
| 9130 | CurrentParameterCopyTypes.push_back(Entity.getType()); |
| 9131 | } |
| 9132 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 9133 | ExprResult Result = Seq.Perform(*this, Entity, Kind, InitE); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9134 | |
Alex Lorenz | de69ff9 | 2017-05-16 10:23:58 +0000 | [diff] [blame] | 9135 | if (ShouldTrackCopy) |
| 9136 | CurrentParameterCopyTypes.pop_back(); |
| 9137 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9138 | return Result; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 9139 | } |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9140 | |
Richard Smith | 1363e8f | 2017-09-07 07:22:36 +0000 | [diff] [blame] | 9141 | /// Determine whether RD is, or is derived from, a specialization of CTD. |
| 9142 | static bool isOrIsDerivedFromSpecializationOf(CXXRecordDecl *RD, |
| 9143 | ClassTemplateDecl *CTD) { |
| 9144 | auto NotSpecialization = [&] (const CXXRecordDecl *Candidate) { |
| 9145 | auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Candidate); |
| 9146 | return !CTSD || !declaresSameEntity(CTSD->getSpecializedTemplate(), CTD); |
| 9147 | }; |
| 9148 | return !(NotSpecialization(RD) && RD->forallBases(NotSpecialization)); |
| 9149 | } |
| 9150 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9151 | QualType Sema::DeduceTemplateSpecializationFromInitializer( |
| 9152 | TypeSourceInfo *TSInfo, const InitializedEntity &Entity, |
| 9153 | const InitializationKind &Kind, MultiExprArg Inits) { |
| 9154 | auto *DeducedTST = dyn_cast<DeducedTemplateSpecializationType>( |
| 9155 | TSInfo->getType()->getContainedDeducedType()); |
| 9156 | assert(DeducedTST && "not a deduced template specialization type"); |
| 9157 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9158 | auto TemplateName = DeducedTST->getTemplateName(); |
Richard Smith | cff4201 | 2018-09-28 03:18:53 +0000 | [diff] [blame] | 9159 | if (TemplateName.isDependent()) |
| 9160 | return Context.DependentTy; |
| 9161 | |
| 9162 | // We can only perform deduction for class templates. |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9163 | auto *Template = |
| 9164 | dyn_cast_or_null<ClassTemplateDecl>(TemplateName.getAsTemplateDecl()); |
| 9165 | if (!Template) { |
| 9166 | Diag(Kind.getLocation(), |
| 9167 | diag::err_deduced_non_class_template_specialization_type) |
| 9168 | << (int)getTemplateNameKindForDiagnostics(TemplateName) << TemplateName; |
| 9169 | if (auto *TD = TemplateName.getAsTemplateDecl()) |
| 9170 | Diag(TD->getLocation(), diag::note_template_decl_here); |
| 9171 | return QualType(); |
| 9172 | } |
| 9173 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9174 | // Can't deduce from dependent arguments. |
Richard Smith | 8eeb16f | 2018-09-10 20:31:03 +0000 | [diff] [blame] | 9175 | if (Expr::hasAnyTypeDependentArguments(Inits)) { |
| 9176 | Diag(TSInfo->getTypeLoc().getBeginLoc(), |
| 9177 | diag::warn_cxx14_compat_class_template_argument_deduction) |
| 9178 | << TSInfo->getTypeLoc().getSourceRange() << 0; |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9179 | return Context.DependentTy; |
Richard Smith | 8eeb16f | 2018-09-10 20:31:03 +0000 | [diff] [blame] | 9180 | } |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9181 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9182 | // FIXME: Perform "exact type" matching first, per CWG discussion? |
| 9183 | // Or implement this via an implied 'T(T) -> T' deduction guide? |
| 9184 | |
| 9185 | // FIXME: Do we need/want a std::initializer_list<T> special case? |
| 9186 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9187 | // Look up deduction guides, including those synthesized from constructors. |
| 9188 | // |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9189 | // C++1z [over.match.class.deduct]p1: |
| 9190 | // A set of functions and function templates is formed comprising: |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9191 | // - For each constructor of the class template designated by the |
| 9192 | // template-name, a function template [...] |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9193 | // - For each deduction-guide, a function or function template [...] |
| 9194 | DeclarationNameInfo NameInfo( |
| 9195 | Context.DeclarationNames.getCXXDeductionGuideName(Template), |
| 9196 | TSInfo->getTypeLoc().getEndLoc()); |
| 9197 | LookupResult Guides(*this, NameInfo, LookupOrdinaryName); |
| 9198 | LookupQualifiedName(Guides, Template->getDeclContext()); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9199 | |
| 9200 | // FIXME: Do not diagnose inaccessible deduction guides. The standard isn't |
| 9201 | // clear on this, but they're not found by name so access does not apply. |
| 9202 | Guides.suppressDiagnostics(); |
| 9203 | |
| 9204 | // Figure out if this is list-initialization. |
| 9205 | InitListExpr *ListInit = |
| 9206 | (Inits.size() == 1 && Kind.getKind() != InitializationKind::IK_Direct) |
| 9207 | ? dyn_cast<InitListExpr>(Inits[0]) |
| 9208 | : nullptr; |
| 9209 | |
| 9210 | // C++1z [over.match.class.deduct]p1: |
| 9211 | // Initialization and overload resolution are performed as described in |
| 9212 | // [dcl.init] and [over.match.ctor], [over.match.copy], or [over.match.list] |
| 9213 | // (as appropriate for the type of initialization performed) for an object |
| 9214 | // of a hypothetical class type, where the selected functions and function |
| 9215 | // templates are considered to be the constructors of that class type |
| 9216 | // |
| 9217 | // Since we know we're initializing a class type of a type unrelated to that |
| 9218 | // of the initializer, this reduces to something fairly reasonable. |
| 9219 | OverloadCandidateSet Candidates(Kind.getLocation(), |
| 9220 | OverloadCandidateSet::CSK_Normal); |
| 9221 | OverloadCandidateSet::iterator Best; |
| 9222 | auto tryToResolveOverload = |
| 9223 | [&](bool OnlyListConstructors) -> OverloadingResult { |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 9224 | Candidates.clear(OverloadCandidateSet::CSK_Normal); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9225 | for (auto I = Guides.begin(), E = Guides.end(); I != E; ++I) { |
| 9226 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9227 | if (D->isInvalidDecl()) |
| 9228 | continue; |
| 9229 | |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9230 | auto *TD = dyn_cast<FunctionTemplateDecl>(D); |
| 9231 | auto *GD = dyn_cast_or_null<CXXDeductionGuideDecl>( |
| 9232 | TD ? TD->getTemplatedDecl() : dyn_cast<FunctionDecl>(D)); |
| 9233 | if (!GD) |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9234 | continue; |
| 9235 | |
| 9236 | // C++ [over.match.ctor]p1: (non-list copy-initialization from non-class) |
| 9237 | // For copy-initialization, the candidate functions are all the |
| 9238 | // converting constructors (12.3.1) of that class. |
| 9239 | // C++ [over.match.copy]p1: (non-list copy-initialization from class) |
| 9240 | // The converting constructors of T are candidate functions. |
| 9241 | if (Kind.isCopyInit() && !ListInit) { |
Richard Smith | afe4aa8 | 2017-02-10 02:19:05 +0000 | [diff] [blame] | 9242 | // Only consider converting constructors. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9243 | if (GD->isExplicit()) |
Richard Smith | afe4aa8 | 2017-02-10 02:19:05 +0000 | [diff] [blame] | 9244 | continue; |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9245 | |
| 9246 | // When looking for a converting constructor, deduction guides that |
Richard Smith | afe4aa8 | 2017-02-10 02:19:05 +0000 | [diff] [blame] | 9247 | // could never be called with one argument are not interesting to |
| 9248 | // check or note. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9249 | if (GD->getMinRequiredArguments() > 1 || |
| 9250 | (GD->getNumParams() == 0 && !GD->isVariadic())) |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9251 | continue; |
| 9252 | } |
| 9253 | |
| 9254 | // C++ [over.match.list]p1.1: (first phase list initialization) |
| 9255 | // Initially, the candidate functions are the initializer-list |
| 9256 | // constructors of the class T |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9257 | if (OnlyListConstructors && !isInitListConstructor(GD)) |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9258 | continue; |
| 9259 | |
| 9260 | // C++ [over.match.list]p1.2: (second phase list initialization) |
| 9261 | // the candidate functions are all the constructors of the class T |
| 9262 | // C++ [over.match.ctor]p1: (all other cases) |
| 9263 | // the candidate functions are all the constructors of the class of |
| 9264 | // the object being initialized |
| 9265 | |
| 9266 | // C++ [over.best.ics]p4: |
| 9267 | // When [...] the constructor [...] is a candidate by |
| 9268 | // - [over.match.copy] (in all cases) |
| 9269 | // FIXME: The "second phase of [over.match.list] case can also |
| 9270 | // theoretically happen here, but it's not clear whether we can |
| 9271 | // ever have a parameter of the right type. |
| 9272 | bool SuppressUserConversions = Kind.isCopyInit(); |
| 9273 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9274 | if (TD) |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9275 | AddTemplateOverloadCandidate(TD, I.getPair(), /*ExplicitArgs*/ nullptr, |
| 9276 | Inits, Candidates, |
| 9277 | SuppressUserConversions); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9278 | else |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9279 | AddOverloadCandidate(GD, I.getPair(), Inits, Candidates, |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9280 | SuppressUserConversions); |
| 9281 | } |
| 9282 | return Candidates.BestViableFunction(*this, Kind.getLocation(), Best); |
| 9283 | }; |
| 9284 | |
| 9285 | OverloadingResult Result = OR_No_Viable_Function; |
| 9286 | |
| 9287 | // C++11 [over.match.list]p1, per DR1467: for list-initialization, first |
| 9288 | // try initializer-list constructors. |
| 9289 | if (ListInit) { |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9290 | bool TryListConstructors = true; |
| 9291 | |
| 9292 | // Try list constructors unless the list is empty and the class has one or |
| 9293 | // more default constructors, in which case those constructors win. |
| 9294 | if (!ListInit->getNumInits()) { |
| 9295 | for (NamedDecl *D : Guides) { |
| 9296 | auto *FD = dyn_cast<FunctionDecl>(D->getUnderlyingDecl()); |
| 9297 | if (FD && FD->getMinRequiredArguments() == 0) { |
| 9298 | TryListConstructors = false; |
| 9299 | break; |
| 9300 | } |
| 9301 | } |
Richard Smith | 1363e8f | 2017-09-07 07:22:36 +0000 | [diff] [blame] | 9302 | } else if (ListInit->getNumInits() == 1) { |
| 9303 | // C++ [over.match.class.deduct]: |
| 9304 | // As an exception, the first phase in [over.match.list] (considering |
| 9305 | // initializer-list constructors) is omitted if the initializer list |
| 9306 | // consists of a single expression of type cv U, where U is a |
| 9307 | // specialization of C or a class derived from a specialization of C. |
| 9308 | Expr *E = ListInit->getInit(0); |
| 9309 | auto *RD = E->getType()->getAsCXXRecordDecl(); |
| 9310 | if (!isa<InitListExpr>(E) && RD && |
Erik Pilkington | dd0b344 | 2018-07-26 23:40:42 +0000 | [diff] [blame] | 9311 | isCompleteType(Kind.getLocation(), E->getType()) && |
Richard Smith | 1363e8f | 2017-09-07 07:22:36 +0000 | [diff] [blame] | 9312 | isOrIsDerivedFromSpecializationOf(RD, Template)) |
| 9313 | TryListConstructors = false; |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9314 | } |
| 9315 | |
| 9316 | if (TryListConstructors) |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9317 | Result = tryToResolveOverload(/*OnlyListConstructor*/true); |
| 9318 | // Then unwrap the initializer list and try again considering all |
| 9319 | // constructors. |
| 9320 | Inits = MultiExprArg(ListInit->getInits(), ListInit->getNumInits()); |
| 9321 | } |
| 9322 | |
| 9323 | // If list-initialization fails, or if we're doing any other kind of |
| 9324 | // initialization, we (eventually) consider constructors. |
| 9325 | if (Result == OR_No_Viable_Function) |
| 9326 | Result = tryToResolveOverload(/*OnlyListConstructor*/false); |
| 9327 | |
| 9328 | switch (Result) { |
| 9329 | case OR_Ambiguous: |
| 9330 | Diag(Kind.getLocation(), diag::err_deduced_class_template_ctor_ambiguous) |
| 9331 | << TemplateName; |
| 9332 | // FIXME: For list-initialization candidates, it'd usually be better to |
| 9333 | // list why they were not viable when given the initializer list itself as |
| 9334 | // an argument. |
| 9335 | Candidates.NoteCandidates(*this, OCD_ViableCandidates, Inits); |
| 9336 | return QualType(); |
| 9337 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9338 | case OR_No_Viable_Function: { |
| 9339 | CXXRecordDecl *Primary = |
| 9340 | cast<ClassTemplateDecl>(Template)->getTemplatedDecl(); |
| 9341 | bool Complete = |
| 9342 | isCompleteType(Kind.getLocation(), Context.getTypeDeclType(Primary)); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9343 | Diag(Kind.getLocation(), |
| 9344 | Complete ? diag::err_deduced_class_template_ctor_no_viable |
| 9345 | : diag::err_deduced_class_template_incomplete) |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9346 | << TemplateName << !Guides.empty(); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9347 | Candidates.NoteCandidates(*this, OCD_AllCandidates, Inits); |
| 9348 | return QualType(); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9349 | } |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9350 | |
| 9351 | case OR_Deleted: { |
| 9352 | Diag(Kind.getLocation(), diag::err_deduced_class_template_deleted) |
| 9353 | << TemplateName; |
| 9354 | NoteDeletedFunction(Best->Function); |
| 9355 | return QualType(); |
| 9356 | } |
| 9357 | |
| 9358 | case OR_Success: |
| 9359 | // C++ [over.match.list]p1: |
| 9360 | // In copy-list-initialization, if an explicit constructor is chosen, the |
| 9361 | // initialization is ill-formed. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9362 | if (Kind.isCopyInit() && ListInit && |
| 9363 | cast<CXXDeductionGuideDecl>(Best->Function)->isExplicit()) { |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9364 | bool IsDeductionGuide = !Best->Function->isImplicit(); |
| 9365 | Diag(Kind.getLocation(), diag::err_deduced_class_template_explicit) |
| 9366 | << TemplateName << IsDeductionGuide; |
| 9367 | Diag(Best->Function->getLocation(), |
| 9368 | diag::note_explicit_ctor_deduction_guide_here) |
| 9369 | << IsDeductionGuide; |
| 9370 | return QualType(); |
| 9371 | } |
| 9372 | |
| 9373 | // Make sure we didn't select an unusable deduction guide, and mark it |
| 9374 | // as referenced. |
| 9375 | DiagnoseUseOfDecl(Best->Function, Kind.getLocation()); |
| 9376 | MarkFunctionReferenced(Kind.getLocation(), Best->Function); |
| 9377 | break; |
| 9378 | } |
| 9379 | |
| 9380 | // C++ [dcl.type.class.deduct]p1: |
| 9381 | // The placeholder is replaced by the return type of the function selected |
| 9382 | // by overload resolution for class template deduction. |
Richard Smith | 8eeb16f | 2018-09-10 20:31:03 +0000 | [diff] [blame] | 9383 | QualType DeducedType = |
| 9384 | SubstAutoType(TSInfo->getType(), Best->Function->getReturnType()); |
| 9385 | Diag(TSInfo->getTypeLoc().getBeginLoc(), |
| 9386 | diag::warn_cxx14_compat_class_template_argument_deduction) |
| 9387 | << TSInfo->getTypeLoc().getSourceRange() << 1 << DeducedType; |
| 9388 | return DeducedType; |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9389 | } |