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 | } |
| 194 | |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 195 | // [dcl.init.string]p2 |
| 196 | if (StrLength > CAT->getSize().getZExtValue()) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 197 | S.Diag(Str->getLocStart(), |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 198 | diag::err_initializer_string_for_char_array_too_long) |
| 199 | << Str->getSourceRange(); |
| 200 | } else { |
| 201 | // C99 6.7.8p14. |
| 202 | if (StrLength-1 > CAT->getSize().getZExtValue()) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 203 | S.Diag(Str->getLocStart(), |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 204 | diag::ext_initializer_string_for_char_array_too_long) |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 205 | << Str->getSourceRange(); |
| 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 | |
| 453 | if (IsInStd && llvm::StringSwitch<bool>(R->getName()) |
| 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 = |
| 521 | FillWithNoInit ? new (SemaRef.Context) NoInitExpr(Base.getType()) |
| 522 | : PerformEmptyInit(SemaRef, ILE->getLocEnd(), BaseEntity, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 523 | /*VerifyOnly*/ false, |
| 524 | 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) { |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 548 | SourceLocation Loc = ILE->getLocEnd(); |
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 { |
| 768 | ExprResult ElementInit = PerformEmptyInit(SemaRef, ILE->getLocEnd(), |
| 769 | ElementEntity, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 770 | /*VerifyOnly*/false, |
| 771 | TreatUnavailableAsInvalid); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 772 | if (ElementInit.isInvalid()) { |
| 773 | hadError = true; |
| 774 | return; |
| 775 | } |
| 776 | |
| 777 | Filler = ElementInit.getAs<Expr>(); |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | if (hadError) { |
| 781 | // Do nothing |
| 782 | } else if (Init < NumInits) { |
Argyrios Kyrtzidis | 446bcf2 | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 783 | // For arrays, just set the expression used for value-initialization |
| 784 | // of the "holes" in the array. |
| 785 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 786 | ILE->setArrayFiller(Filler); |
Argyrios Kyrtzidis | 446bcf2 | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 787 | else |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 788 | ILE->setInit(Init, Filler); |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 789 | } else { |
| 790 | // For arrays, just set the expression used for value-initialization |
| 791 | // of the rest of elements and exit. |
| 792 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) { |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 793 | ILE->setArrayFiller(Filler); |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 794 | return; |
| 795 | } |
| 796 | |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 797 | if (!isa<ImplicitValueInitExpr>(Filler) && !isa<NoInitExpr>(Filler)) { |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 798 | // Empty initialization requires a constructor call, so |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 799 | // extend the initializer list to include the constructor |
| 800 | // call and make a note that we'll need to take another pass |
| 801 | // through the initializer list. |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 802 | ILE->updateInit(SemaRef.Context, Init, Filler); |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 803 | RequiresSecondPass = true; |
| 804 | } |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 805 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 806 | } else if (InitListExpr *InnerILE |
Argyrios Kyrtzidis | d4590a5d | 2011-10-21 23:02:22 +0000 | [diff] [blame] | 807 | = dyn_cast_or_null<InitListExpr>(InitExpr)) |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 808 | FillInEmptyInitializations(ElementEntity, InnerILE, RequiresSecondPass, |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 809 | ILE, Init, FillWithNoInit); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 810 | else if (DesignatedInitUpdateExpr *InnerDIUE |
| 811 | = dyn_cast_or_null<DesignatedInitUpdateExpr>(InitExpr)) |
| 812 | FillInEmptyInitializations(ElementEntity, InnerDIUE->getUpdater(), |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 813 | RequiresSecondPass, ILE, Init, |
| 814 | /*FillWithNoInit =*/true); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 815 | } |
| 816 | } |
| 817 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 818 | InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 819 | InitListExpr *IL, QualType &T, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 820 | bool VerifyOnly, |
| 821 | bool TreatUnavailableAsInvalid) |
| 822 | : SemaRef(S), VerifyOnly(VerifyOnly), |
| 823 | TreatUnavailableAsInvalid(TreatUnavailableAsInvalid) { |
Richard Smith | 520449d | 2015-02-05 06:15:50 +0000 | [diff] [blame] | 824 | // FIXME: Check that IL isn't already the semantic form of some other |
| 825 | // InitListExpr. If it is, we'd create a broken AST. |
| 826 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 827 | hadError = false; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 828 | |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 829 | FullyStructuredList = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 830 | getStructuredSubobjectInit(IL, 0, T, nullptr, 0, IL->getSourceRange()); |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 831 | CheckExplicitInitList(Entity, IL, T, FullyStructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 832 | /*TopLevelObject=*/true); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 833 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 834 | if (!hadError && !VerifyOnly) { |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 835 | bool RequiresSecondPass = false; |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 836 | FillInEmptyInitializations(Entity, FullyStructuredList, RequiresSecondPass, |
| 837 | /*OuterILE=*/nullptr, /*OuterIndex=*/0); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 838 | if (RequiresSecondPass && !hadError) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 839 | FillInEmptyInitializations(Entity, FullyStructuredList, |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 840 | RequiresSecondPass, nullptr, 0); |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 841 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 845 | // FIXME: use a proper constant |
| 846 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 847 | if (const ConstantArrayType *CAT = |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 848 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 849 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 850 | } |
| 851 | return maxElements; |
| 852 | } |
| 853 | |
| 854 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 855 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 856 | int InitializableMembers = 0; |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 857 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(structDecl)) |
| 858 | InitializableMembers += CXXRD->getNumBases(); |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 859 | for (const auto *Field : structDecl->fields()) |
Douglas Gregor | 556e586 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 860 | if (!Field->isUnnamedBitfield()) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 861 | ++InitializableMembers; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 862 | |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 863 | if (structDecl->isUnion()) |
Eli Friedman | 0e56c82 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 864 | return std::min(InitializableMembers, 1); |
| 865 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 866 | } |
| 867 | |
Richard Smith | 283e207 | 2017-10-03 20:36:00 +0000 | [diff] [blame] | 868 | /// Determine whether Entity is an entity for which it is idiomatic to elide |
| 869 | /// the braces in aggregate initialization. |
| 870 | static bool isIdiomaticBraceElisionEntity(const InitializedEntity &Entity) { |
| 871 | // Recursive initialization of the one and only field within an aggregate |
| 872 | // class is considered idiomatic. This case arises in particular for |
| 873 | // initialization of std::array, where the C++ standard suggests the idiom of |
| 874 | // |
| 875 | // std::array<T, N> arr = {1, 2, 3}; |
| 876 | // |
| 877 | // (where std::array is an aggregate struct containing a single array field. |
| 878 | |
| 879 | // FIXME: Should aggregate initialization of a struct with a single |
| 880 | // base class and no members also suppress the warning? |
| 881 | if (Entity.getKind() != InitializedEntity::EK_Member || !Entity.getParent()) |
| 882 | return false; |
| 883 | |
| 884 | auto *ParentRD = |
| 885 | Entity.getParent()->getType()->castAs<RecordType>()->getDecl(); |
| 886 | if (CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(ParentRD)) |
| 887 | if (CXXRD->getNumBases()) |
| 888 | return false; |
| 889 | |
| 890 | auto FieldIt = ParentRD->field_begin(); |
| 891 | assert(FieldIt != ParentRD->field_end() && |
| 892 | "no fields but have initializer for member?"); |
| 893 | return ++FieldIt == ParentRD->field_end(); |
| 894 | } |
| 895 | |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 896 | /// Check whether the range of the initializer \p ParentIList from element |
| 897 | /// \p Index onwards can be used to initialize an object of type \p T. Update |
| 898 | /// \p Index to indicate how many elements of the list were consumed. |
| 899 | /// |
| 900 | /// This also fills in \p StructuredList, from element \p StructuredIndex |
| 901 | /// onwards, with the fully-braced, desugared form of the initialization. |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 902 | void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 903 | InitListExpr *ParentIList, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 904 | QualType T, unsigned &Index, |
| 905 | InitListExpr *StructuredList, |
Eli Friedman | c616c5f | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 906 | unsigned &StructuredIndex) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 907 | int maxElements = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 908 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 909 | if (T->isArrayType()) |
| 910 | maxElements = numArrayElements(T); |
Douglas Gregor | 8385a06 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 911 | else if (T->isRecordType()) |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 912 | maxElements = numStructUnionElements(T); |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 913 | else if (T->isVectorType()) |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 914 | maxElements = T->getAs<VectorType>()->getNumElements(); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 915 | else |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 916 | llvm_unreachable("CheckImplicitInitList(): Illegal type"); |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 917 | |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 918 | if (maxElements == 0) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 919 | if (!VerifyOnly) |
| 920 | SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(), |
| 921 | diag::err_implicit_empty_initializer); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 922 | ++Index; |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 923 | hadError = true; |
| 924 | return; |
| 925 | } |
| 926 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 927 | // Build a structured initializer list corresponding to this subobject. |
| 928 | InitListExpr *StructuredSubobjectInitList |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 929 | = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList, |
| 930 | StructuredIndex, |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 931 | SourceRange(ParentIList->getInit(Index)->getLocStart(), |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 932 | ParentIList->getSourceRange().getEnd())); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 933 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 934 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 935 | // Check the element types and build the structural subobject. |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 936 | unsigned StartIndex = Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 937 | CheckListElementTypes(Entity, ParentIList, T, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 938 | /*SubobjectIsDesignatorContext=*/false, Index, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 939 | StructuredSubobjectInitList, |
Eli Friedman | c616c5f | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 940 | StructuredSubobjectInitIndex); |
Sebastian Redl | 8b6412a | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 941 | |
Richard Smith | de22923 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 942 | if (!VerifyOnly) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 943 | StructuredSubobjectInitList->setType(T); |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 944 | |
Sebastian Redl | 8b6412a | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 945 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 946 | // Update the structured sub-object initializer so that it's ending |
| 947 | // range corresponds with the end of the last initializer it used. |
Reid Kleckner | 4a09e88 | 2015-12-09 23:18:38 +0000 | [diff] [blame] | 948 | if (EndIndex < ParentIList->getNumInits() && |
| 949 | ParentIList->getInit(EndIndex)) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 950 | SourceLocation EndLoc |
| 951 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 952 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 953 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 954 | |
Sebastian Redl | 8b6412a | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 955 | // Complain about missing braces. |
Daniel Marjamaki | 817a3bf | 2017-09-29 09:44:41 +0000 | [diff] [blame] | 956 | if ((T->isArrayType() || T->isRecordType()) && |
Richard Smith | 283e207 | 2017-10-03 20:36:00 +0000 | [diff] [blame] | 957 | !ParentIList->isIdiomaticZeroInitializer(SemaRef.getLangOpts()) && |
| 958 | !isIdiomaticBraceElisionEntity(Entity)) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 959 | SemaRef.Diag(StructuredSubobjectInitList->getLocStart(), |
Richard Smith | de22923 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 960 | diag::warn_missing_braces) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 961 | << StructuredSubobjectInitList->getSourceRange() |
| 962 | << FixItHint::CreateInsertion( |
| 963 | StructuredSubobjectInitList->getLocStart(), "{") |
| 964 | << FixItHint::CreateInsertion( |
| 965 | SemaRef.getLocForEndOfToken( |
| 966 | StructuredSubobjectInitList->getLocEnd()), |
| 967 | "}"); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 968 | } |
Tanya Lattner | 5029d56 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 969 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 970 | } |
| 971 | |
Richard Smith | 420fa12 | 2015-02-12 01:50:05 +0000 | [diff] [blame] | 972 | /// Warn that \p Entity was of scalar type and was initialized by a |
| 973 | /// single-element braced initializer list. |
| 974 | static void warnBracedScalarInit(Sema &S, const InitializedEntity &Entity, |
| 975 | SourceRange Braces) { |
| 976 | // Don't warn during template instantiation. If the initialization was |
| 977 | // non-dependent, we warned during the initial parse; otherwise, the |
| 978 | // type might not be scalar in some uses of the template. |
Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 979 | if (S.inTemplateInstantiation()) |
Richard Smith | 420fa12 | 2015-02-12 01:50:05 +0000 | [diff] [blame] | 980 | return; |
| 981 | |
| 982 | unsigned DiagID = 0; |
| 983 | |
| 984 | switch (Entity.getKind()) { |
| 985 | case InitializedEntity::EK_VectorElement: |
| 986 | case InitializedEntity::EK_ComplexElement: |
| 987 | case InitializedEntity::EK_ArrayElement: |
| 988 | case InitializedEntity::EK_Parameter: |
| 989 | case InitializedEntity::EK_Parameter_CF_Audited: |
| 990 | case InitializedEntity::EK_Result: |
| 991 | // Extra braces here are suspicious. |
| 992 | DiagID = diag::warn_braces_around_scalar_init; |
| 993 | break; |
| 994 | |
| 995 | case InitializedEntity::EK_Member: |
| 996 | // Warn on aggregate initialization but not on ctor init list or |
| 997 | // default member initializer. |
| 998 | if (Entity.getParent()) |
| 999 | DiagID = diag::warn_braces_around_scalar_init; |
| 1000 | break; |
| 1001 | |
| 1002 | case InitializedEntity::EK_Variable: |
| 1003 | case InitializedEntity::EK_LambdaCapture: |
| 1004 | // No warning, might be direct-list-initialization. |
| 1005 | // FIXME: Should we warn for copy-list-initialization in these cases? |
| 1006 | break; |
| 1007 | |
| 1008 | case InitializedEntity::EK_New: |
| 1009 | case InitializedEntity::EK_Temporary: |
| 1010 | case InitializedEntity::EK_CompoundLiteralInit: |
| 1011 | // No warning, braces are part of the syntax of the underlying construct. |
| 1012 | break; |
| 1013 | |
| 1014 | case InitializedEntity::EK_RelatedResult: |
| 1015 | // No warning, we already warned when initializing the result. |
| 1016 | break; |
| 1017 | |
| 1018 | case InitializedEntity::EK_Exception: |
| 1019 | case InitializedEntity::EK_Base: |
| 1020 | case InitializedEntity::EK_Delegating: |
| 1021 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 1022 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 1023 | case InitializedEntity::EK_Binding: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 1024 | case InitializedEntity::EK_StmtExprResult: |
Richard Smith | 420fa12 | 2015-02-12 01:50:05 +0000 | [diff] [blame] | 1025 | llvm_unreachable("unexpected braced scalar init"); |
| 1026 | } |
| 1027 | |
| 1028 | if (DiagID) { |
| 1029 | S.Diag(Braces.getBegin(), DiagID) |
| 1030 | << Braces |
| 1031 | << FixItHint::CreateRemoval(Braces.getBegin()) |
| 1032 | << FixItHint::CreateRemoval(Braces.getEnd()); |
| 1033 | } |
| 1034 | } |
| 1035 | |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 1036 | /// Check whether the initializer \p IList (that was written with explicit |
| 1037 | /// braces) can be used to initialize an object of type \p T. |
| 1038 | /// |
| 1039 | /// This also fills in \p StructuredList with the fully-braced, desugared |
| 1040 | /// form of the initialization. |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1041 | void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1042 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1043 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1044 | bool TopLevelObject) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1045 | if (!VerifyOnly) { |
| 1046 | SyntacticToSemantic[IList] = StructuredList; |
| 1047 | StructuredList->setSyntacticForm(IList); |
| 1048 | } |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 1049 | |
| 1050 | unsigned Index = 0, StructuredIndex = 0; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1051 | CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1052 | Index, StructuredList, StructuredIndex, TopLevelObject); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1053 | if (!VerifyOnly) { |
Eli Friedman | 91f5ae5 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 1054 | QualType ExprTy = T; |
| 1055 | if (!ExprTy->isArrayType()) |
| 1056 | ExprTy = ExprTy.getNonLValueExprType(SemaRef.Context); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1057 | IList->setType(ExprTy); |
| 1058 | StructuredList->setType(ExprTy); |
| 1059 | } |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1060 | if (hadError) |
| 1061 | return; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 1062 | |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1063 | if (Index < IList->getNumInits()) { |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 1064 | // We have leftover initializers |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1065 | if (VerifyOnly) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1066 | if (SemaRef.getLangOpts().CPlusPlus || |
| 1067 | (SemaRef.getLangOpts().OpenCL && |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1068 | IList->getType()->isVectorType())) { |
| 1069 | hadError = true; |
| 1070 | } |
| 1071 | return; |
| 1072 | } |
| 1073 | |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 1074 | if (StructuredIndex == 1 && |
Hans Wennborg | 950f318 | 2013-05-16 09:22:40 +0000 | [diff] [blame] | 1075 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context) == |
| 1076 | SIF_None) { |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 1077 | unsigned DK = diag::ext_excess_initializers_in_char_array_initializer; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1078 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 1079 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 1080 | hadError = true; |
| 1081 | } |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 1082 | // Special-case |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1083 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1084 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d0e48ea | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 1085 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | d42a0fb | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 1086 | // Don't complain for incomplete types, since we'll get an error |
| 1087 | // elsewhere |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1088 | QualType CurrentObjectType = StructuredList->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1089 | int initKind = |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1090 | CurrentObjectType->isArrayType()? 0 : |
| 1091 | CurrentObjectType->isVectorType()? 1 : |
| 1092 | CurrentObjectType->isScalarType()? 2 : |
| 1093 | CurrentObjectType->isUnionType()? 3 : |
| 1094 | 4; |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 1095 | |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 1096 | unsigned DK = diag::ext_excess_initializers; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1097 | if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 1098 | DK = diag::err_excess_initializers; |
| 1099 | hadError = true; |
| 1100 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1101 | if (SemaRef.getLangOpts().OpenCL && initKind == 1) { |
Nate Begeman | 425038c | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 1102 | DK = diag::err_excess_initializers; |
| 1103 | hadError = true; |
| 1104 | } |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 1105 | |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1106 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1107 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 1108 | } |
| 1109 | } |
Eli Friedman | 6fcdec2 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 1110 | |
Richard Smith | 420fa12 | 2015-02-12 01:50:05 +0000 | [diff] [blame] | 1111 | if (!VerifyOnly && T->isScalarType() && |
| 1112 | IList->getNumInits() == 1 && !isa<InitListExpr>(IList->getInit(0))) |
| 1113 | warnBracedScalarInit(SemaRef, Entity, IList->getSourceRange()); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1116 | void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1117 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1118 | QualType &DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1119 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1120 | unsigned &Index, |
| 1121 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1122 | unsigned &StructuredIndex, |
| 1123 | bool TopLevelObject) { |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 1124 | if (DeclType->isAnyComplexType() && SubobjectIsDesignatorContext) { |
| 1125 | // Explicitly braced initializer for complex type can be real+imaginary |
| 1126 | // parts. |
| 1127 | CheckComplexType(Entity, IList, DeclType, Index, |
| 1128 | StructuredList, StructuredIndex); |
| 1129 | } else if (DeclType->isScalarType()) { |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1130 | CheckScalarType(Entity, IList, DeclType, Index, |
| 1131 | StructuredList, StructuredIndex); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 1132 | } else if (DeclType->isVectorType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1133 | CheckVectorType(Entity, IList, DeclType, Index, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1134 | StructuredList, StructuredIndex); |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 1135 | } else if (DeclType->isRecordType()) { |
| 1136 | assert(DeclType->isAggregateType() && |
| 1137 | "non-aggregate records should be handed in CheckSubElementType"); |
| 1138 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 1139 | auto Bases = |
| 1140 | CXXRecordDecl::base_class_range(CXXRecordDecl::base_class_iterator(), |
| 1141 | CXXRecordDecl::base_class_iterator()); |
| 1142 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
| 1143 | Bases = CXXRD->bases(); |
| 1144 | CheckStructUnionTypes(Entity, IList, DeclType, Bases, RD->field_begin(), |
| 1145 | SubobjectIsDesignatorContext, Index, StructuredList, |
| 1146 | StructuredIndex, TopLevelObject); |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 1147 | } else if (DeclType->isArrayType()) { |
| 1148 | llvm::APSInt Zero( |
| 1149 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
| 1150 | false); |
| 1151 | CheckArrayType(Entity, IList, DeclType, Zero, |
| 1152 | SubobjectIsDesignatorContext, Index, |
| 1153 | StructuredList, StructuredIndex); |
Steve Naroff | eaf5853 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 1154 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 1155 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1156 | ++Index; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1157 | if (!VerifyOnly) |
| 1158 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 1159 | << DeclType; |
Eli Friedman | d0e48ea | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 1160 | hadError = true; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1161 | } else if (DeclType->isReferenceType()) { |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1162 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 1163 | StructuredList, StructuredIndex); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1164 | } else if (DeclType->isObjCObjectType()) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1165 | if (!VerifyOnly) |
| 1166 | SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class) |
| 1167 | << DeclType; |
Douglas Gregor | 50ec46d | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 1168 | hadError = true; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1169 | } else { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1170 | if (!VerifyOnly) |
| 1171 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 1172 | << DeclType; |
Douglas Gregor | 50ec46d | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 1173 | hadError = true; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1174 | } |
| 1175 | } |
| 1176 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1177 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1178 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1179 | QualType ElemType, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1180 | unsigned &Index, |
| 1181 | InitListExpr *StructuredList, |
| 1182 | unsigned &StructuredIndex) { |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1183 | Expr *expr = IList->getInit(Index); |
Richard Smith | 72752e8 | 2013-05-31 02:56:17 +0000 | [diff] [blame] | 1184 | |
| 1185 | if (ElemType->isReferenceType()) |
| 1186 | return CheckReferenceType(Entity, IList, ElemType, Index, |
| 1187 | StructuredList, StructuredIndex); |
| 1188 | |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 1189 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1190 | if (SubInitList->getNumInits() == 1 && |
| 1191 | IsStringInit(SubInitList->getInit(0), ElemType, SemaRef.Context) == |
| 1192 | SIF_None) { |
| 1193 | expr = SubInitList->getInit(0); |
| 1194 | } else if (!SemaRef.getLangOpts().CPlusPlus) { |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 1195 | InitListExpr *InnerStructuredList |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 1196 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 1197 | StructuredList, StructuredIndex, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1198 | SubInitList->getSourceRange(), true); |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 1199 | CheckExplicitInitList(Entity, SubInitList, ElemType, |
| 1200 | InnerStructuredList); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1201 | |
| 1202 | if (!hadError && !VerifyOnly) { |
| 1203 | bool RequiresSecondPass = false; |
| 1204 | FillInEmptyInitializations(Entity, InnerStructuredList, |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 1205 | RequiresSecondPass, StructuredList, |
| 1206 | StructuredIndex); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1207 | if (RequiresSecondPass && !hadError) |
| 1208 | FillInEmptyInitializations(Entity, InnerStructuredList, |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 1209 | RequiresSecondPass, StructuredList, |
| 1210 | StructuredIndex); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1211 | } |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 1212 | ++StructuredIndex; |
| 1213 | ++Index; |
| 1214 | return; |
| 1215 | } |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 1216 | // C++ initialization is handled later. |
Richard Smith | c4158e86 | 2014-07-18 04:47:25 +0000 | [diff] [blame] | 1217 | } else if (isa<ImplicitValueInitExpr>(expr)) { |
Richard Smith | 8aa561b | 2014-07-17 23:12:06 +0000 | [diff] [blame] | 1218 | // This happens during template instantiation when we see an InitListExpr |
| 1219 | // that we've already checked once. |
Richard Smith | c4158e86 | 2014-07-18 04:47:25 +0000 | [diff] [blame] | 1220 | assert(SemaRef.Context.hasSameType(expr->getType(), ElemType) && |
Richard Smith | 8aa561b | 2014-07-17 23:12:06 +0000 | [diff] [blame] | 1221 | "found implicit initialization for the wrong type"); |
| 1222 | if (!VerifyOnly) |
| 1223 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 1224 | ++Index; |
| 1225 | return; |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1228 | if (SemaRef.getLangOpts().CPlusPlus) { |
| 1229 | // C++ [dcl.init.aggr]p2: |
| 1230 | // Each member is copy-initialized from the corresponding |
| 1231 | // initializer-clause. |
| 1232 | |
| 1233 | // FIXME: Better EqualLoc? |
| 1234 | InitializationKind Kind = |
| 1235 | InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation()); |
| 1236 | InitializationSequence Seq(SemaRef, Entity, Kind, expr, |
| 1237 | /*TopLevelOfInitList*/ true); |
| 1238 | |
| 1239 | // C++14 [dcl.init.aggr]p13: |
| 1240 | // If the assignment-expression can initialize a member, the member is |
| 1241 | // initialized. Otherwise [...] brace elision is assumed |
| 1242 | // |
| 1243 | // Brace elision is never performed if the element is not an |
| 1244 | // assignment-expression. |
| 1245 | if (Seq || isa<InitListExpr>(expr)) { |
| 1246 | if (!VerifyOnly) { |
| 1247 | ExprResult Result = |
| 1248 | Seq.Perform(SemaRef, Entity, Kind, expr); |
| 1249 | if (Result.isInvalid()) |
| 1250 | hadError = true; |
| 1251 | |
| 1252 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1253 | Result.getAs<Expr>()); |
Richard Smith | 40574cc | 2015-02-16 04:42:59 +0000 | [diff] [blame] | 1254 | } else if (!Seq) |
| 1255 | hadError = true; |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1256 | ++Index; |
| 1257 | return; |
| 1258 | } |
| 1259 | |
| 1260 | // Fall through for subaggregate initialization |
| 1261 | } else if (ElemType->isScalarType() || ElemType->isAtomicType()) { |
| 1262 | // FIXME: Need to handle atomic aggregate types with implicit init lists. |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1263 | return CheckScalarType(Entity, IList, ElemType, Index, |
| 1264 | StructuredList, StructuredIndex); |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1265 | } else if (const ArrayType *arrayType = |
| 1266 | SemaRef.Context.getAsArrayType(ElemType)) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1267 | // arrayType can be incomplete if we're initializing a flexible |
| 1268 | // array member. There's nothing we can do with the completed |
| 1269 | // type here, though. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1270 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1271 | if (IsStringInit(expr, arrayType, SemaRef.Context) == SIF_None) { |
Eli Friedman | d8d7a37 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 1272 | if (!VerifyOnly) { |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1273 | CheckStringInit(expr, ElemType, arrayType, SemaRef); |
| 1274 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
Eli Friedman | d8d7a37 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 1275 | } |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1276 | ++Index; |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1277 | return; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1278 | } |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1279 | |
| 1280 | // Fall through for subaggregate initialization. |
| 1281 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1282 | } else { |
Anastasia Stulova | db7a31c | 2016-07-05 11:31:24 +0000 | [diff] [blame] | 1283 | assert((ElemType->isRecordType() || ElemType->isVectorType() || |
Egor Churaev | 45fe70f | 2017-05-10 10:28:34 +0000 | [diff] [blame] | 1284 | ElemType->isOpenCLSpecificType()) && "Unexpected type"); |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1285 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1286 | // C99 6.7.8p13: |
| 1287 | // |
| 1288 | // The initializer for a structure or union object that has |
| 1289 | // automatic storage duration shall be either an initializer |
| 1290 | // list as described below, or a single expression that has |
| 1291 | // compatible structure or union type. In the latter case, the |
| 1292 | // initial value of the object, including unnamed members, is |
| 1293 | // that of the expression. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1294 | ExprResult ExprRes = expr; |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1295 | if (SemaRef.CheckSingleAssignmentConstraints( |
| 1296 | ElemType, ExprRes, !VerifyOnly) != Sema::Incompatible) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1297 | if (ExprRes.isInvalid()) |
| 1298 | hadError = true; |
| 1299 | else { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1300 | ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.get()); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 1301 | if (ExprRes.isInvalid()) |
| 1302 | hadError = true; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1303 | } |
| 1304 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1305 | ExprRes.getAs<Expr>()); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1306 | ++Index; |
| 1307 | return; |
| 1308 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1309 | ExprRes.get(); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1310 | // Fall through for subaggregate initialization |
| 1311 | } |
| 1312 | |
| 1313 | // C++ [dcl.init.aggr]p12: |
| 1314 | // |
| 1315 | // [...] Otherwise, if the member is itself a non-empty |
| 1316 | // subaggregate, brace elision is assumed and the initializer is |
| 1317 | // considered for the initialization of the first member of |
| 1318 | // the subaggregate. |
Yaxun Liu | a91da4b | 2016-10-11 15:53:28 +0000 | [diff] [blame] | 1319 | // OpenCL vector initializer is handled elsewhere. |
| 1320 | if ((!SemaRef.getLangOpts().OpenCL && ElemType->isVectorType()) || |
| 1321 | ElemType->isAggregateType()) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1322 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
| 1323 | StructuredIndex); |
| 1324 | ++StructuredIndex; |
| 1325 | } else { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1326 | if (!VerifyOnly) { |
| 1327 | // We cannot initialize this element, so let |
| 1328 | // PerformCopyInitialization produce the appropriate diagnostic. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1329 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), expr, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1330 | /*TopLevelOfInitList=*/true); |
| 1331 | } |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1332 | hadError = true; |
| 1333 | ++Index; |
| 1334 | ++StructuredIndex; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1335 | } |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 1338 | void InitListChecker::CheckComplexType(const InitializedEntity &Entity, |
| 1339 | InitListExpr *IList, QualType DeclType, |
| 1340 | unsigned &Index, |
| 1341 | InitListExpr *StructuredList, |
| 1342 | unsigned &StructuredIndex) { |
| 1343 | assert(Index == 0 && "Index in explicit init list must be zero"); |
| 1344 | |
| 1345 | // As an extension, clang supports complex initializers, which initialize |
| 1346 | // a complex number component-wise. When an explicit initializer list for |
| 1347 | // a complex number contains two two initializers, this extension kicks in: |
| 1348 | // it exepcts the initializer list to contain two elements convertible to |
| 1349 | // the element type of the complex type. The first element initializes |
| 1350 | // the real part, and the second element intitializes the imaginary part. |
| 1351 | |
| 1352 | if (IList->getNumInits() != 2) |
| 1353 | return CheckScalarType(Entity, IList, DeclType, Index, StructuredList, |
| 1354 | StructuredIndex); |
| 1355 | |
| 1356 | // This is an extension in C. (The builtin _Complex type does not exist |
| 1357 | // in the C++ standard.) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1358 | if (!SemaRef.getLangOpts().CPlusPlus && !VerifyOnly) |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 1359 | SemaRef.Diag(IList->getLocStart(), diag::ext_complex_component_init) |
| 1360 | << IList->getSourceRange(); |
| 1361 | |
| 1362 | // Initialize the complex number. |
| 1363 | QualType elementType = DeclType->getAs<ComplexType>()->getElementType(); |
| 1364 | InitializedEntity ElementEntity = |
| 1365 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
| 1366 | |
| 1367 | for (unsigned i = 0; i < 2; ++i) { |
| 1368 | ElementEntity.setElementIndex(Index); |
| 1369 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1370 | StructuredList, StructuredIndex); |
| 1371 | } |
| 1372 | } |
| 1373 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1374 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1375 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1376 | unsigned &Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1377 | InitListExpr *StructuredList, |
| 1378 | unsigned &StructuredIndex) { |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1379 | if (Index >= IList->getNumInits()) { |
Richard Smith | c823973 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 1380 | if (!VerifyOnly) |
| 1381 | SemaRef.Diag(IList->getLocStart(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1382 | SemaRef.getLangOpts().CPlusPlus11 ? |
Richard Smith | c823973 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 1383 | diag::warn_cxx98_compat_empty_scalar_initializer : |
| 1384 | diag::err_empty_scalar_initializer) |
| 1385 | << IList->getSourceRange(); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1386 | hadError = !SemaRef.getLangOpts().CPlusPlus11; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1387 | ++Index; |
| 1388 | ++StructuredIndex; |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 1389 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1390 | } |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1391 | |
| 1392 | Expr *expr = IList->getInit(Index); |
| 1393 | if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) { |
Richard Smith | fe9d2c0 | 2013-11-19 03:41:32 +0000 | [diff] [blame] | 1394 | // FIXME: This is invalid, and accepting it causes overload resolution |
| 1395 | // to pick the wrong overload in some corner cases. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1396 | if (!VerifyOnly) |
| 1397 | SemaRef.Diag(SubIList->getLocStart(), |
Richard Smith | fe9d2c0 | 2013-11-19 03:41:32 +0000 | [diff] [blame] | 1398 | diag::ext_many_braces_around_scalar_init) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1399 | << SubIList->getSourceRange(); |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1400 | |
| 1401 | CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList, |
| 1402 | StructuredIndex); |
| 1403 | return; |
| 1404 | } else if (isa<DesignatedInitExpr>(expr)) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1405 | if (!VerifyOnly) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1406 | SemaRef.Diag(expr->getLocStart(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1407 | diag::err_designator_for_scalar_init) |
| 1408 | << DeclType << expr->getSourceRange(); |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1409 | hadError = true; |
| 1410 | ++Index; |
| 1411 | ++StructuredIndex; |
| 1412 | return; |
| 1413 | } |
| 1414 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1415 | if (VerifyOnly) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1416 | if (!SemaRef.CanPerformCopyInitialization(Entity,expr)) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1417 | hadError = true; |
| 1418 | ++Index; |
| 1419 | return; |
| 1420 | } |
| 1421 | |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1422 | ExprResult Result = |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1423 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), expr, |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 1424 | /*TopLevelOfInitList=*/true); |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1425 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1426 | Expr *ResultExpr = nullptr; |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1427 | |
| 1428 | if (Result.isInvalid()) |
| 1429 | hadError = true; // types weren't compatible. |
| 1430 | else { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1431 | ResultExpr = Result.getAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1432 | |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1433 | if (ResultExpr != expr) { |
| 1434 | // The type was promoted, update initializer list. |
| 1435 | IList->setInit(Index, ResultExpr); |
| 1436 | } |
| 1437 | } |
| 1438 | if (hadError) |
| 1439 | ++StructuredIndex; |
| 1440 | else |
| 1441 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 1442 | ++Index; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1445 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 1446 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1447 | unsigned &Index, |
| 1448 | InitListExpr *StructuredList, |
| 1449 | unsigned &StructuredIndex) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1450 | if (Index >= IList->getNumInits()) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1451 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 1452 | // general, it would be useful to pass location information down the stack, |
| 1453 | // so that we know the location (or decl) of the "current object" being |
| 1454 | // initialized. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1455 | if (!VerifyOnly) |
| 1456 | SemaRef.Diag(IList->getLocStart(), |
| 1457 | diag::err_init_reference_member_uninitialized) |
| 1458 | << DeclType |
| 1459 | << IList->getSourceRange(); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1460 | hadError = true; |
| 1461 | ++Index; |
| 1462 | ++StructuredIndex; |
| 1463 | return; |
| 1464 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1465 | |
| 1466 | Expr *expr = IList->getInit(Index); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1467 | if (isa<InitListExpr>(expr) && !SemaRef.getLangOpts().CPlusPlus11) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1468 | if (!VerifyOnly) |
| 1469 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
| 1470 | << DeclType << IList->getSourceRange(); |
| 1471 | hadError = true; |
| 1472 | ++Index; |
| 1473 | ++StructuredIndex; |
| 1474 | return; |
| 1475 | } |
| 1476 | |
| 1477 | if (VerifyOnly) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1478 | if (!SemaRef.CanPerformCopyInitialization(Entity,expr)) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1479 | hadError = true; |
| 1480 | ++Index; |
| 1481 | return; |
| 1482 | } |
| 1483 | |
| 1484 | ExprResult Result = |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1485 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), expr, |
| 1486 | /*TopLevelOfInitList=*/true); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1487 | |
| 1488 | if (Result.isInvalid()) |
| 1489 | hadError = true; |
| 1490 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1491 | expr = Result.getAs<Expr>(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1492 | IList->setInit(Index, expr); |
| 1493 | |
| 1494 | if (hadError) |
| 1495 | ++StructuredIndex; |
| 1496 | else |
| 1497 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 1498 | ++Index; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1501 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1502 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1503 | unsigned &Index, |
| 1504 | InitListExpr *StructuredList, |
| 1505 | unsigned &StructuredIndex) { |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1506 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 1507 | unsigned maxElements = VT->getNumElements(); |
| 1508 | unsigned numEltsInit = 0; |
| 1509 | QualType elementType = VT->getElementType(); |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1510 | |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1511 | if (Index >= IList->getNumInits()) { |
| 1512 | // Make sure the element type can be value-initialized. |
| 1513 | if (VerifyOnly) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1514 | CheckEmptyInitializable( |
| 1515 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity), |
| 1516 | IList->getLocEnd()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1517 | return; |
| 1518 | } |
| 1519 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1520 | if (!SemaRef.getLangOpts().OpenCL) { |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1521 | // If the initializing element is a vector, try to copy-initialize |
| 1522 | // instead of breaking it apart (which is doomed to failure anyway). |
| 1523 | Expr *Init = IList->getInit(Index); |
| 1524 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1525 | if (VerifyOnly) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1526 | if (!SemaRef.CanPerformCopyInitialization(Entity, Init)) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1527 | hadError = true; |
| 1528 | ++Index; |
| 1529 | return; |
| 1530 | } |
| 1531 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1532 | ExprResult Result = |
| 1533 | SemaRef.PerformCopyInitialization(Entity, Init->getLocStart(), Init, |
| 1534 | /*TopLevelOfInitList=*/true); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1535 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1536 | Expr *ResultExpr = nullptr; |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1537 | if (Result.isInvalid()) |
| 1538 | hadError = true; // types weren't compatible. |
| 1539 | else { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1540 | ResultExpr = Result.getAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1541 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1542 | if (ResultExpr != Init) { |
| 1543 | // The type was promoted, update initializer list. |
| 1544 | IList->setInit(Index, ResultExpr); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1545 | } |
| 1546 | } |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1547 | if (hadError) |
| 1548 | ++StructuredIndex; |
| 1549 | else |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1550 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1551 | ResultExpr); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1552 | ++Index; |
| 1553 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1554 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1555 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1556 | InitializedEntity ElementEntity = |
| 1557 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1558 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1559 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 1560 | // Don't attempt to go past the end of the init list |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1561 | if (Index >= IList->getNumInits()) { |
| 1562 | if (VerifyOnly) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1563 | CheckEmptyInitializable(ElementEntity, IList->getLocEnd()); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1564 | break; |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1565 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1566 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1567 | ElementEntity.setElementIndex(Index); |
| 1568 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1569 | StructuredList, StructuredIndex); |
| 1570 | } |
James Molloy | 9eef265 | 2014-06-20 14:35:13 +0000 | [diff] [blame] | 1571 | |
| 1572 | if (VerifyOnly) |
| 1573 | return; |
| 1574 | |
| 1575 | bool isBigEndian = SemaRef.Context.getTargetInfo().isBigEndian(); |
| 1576 | const VectorType *T = Entity.getType()->getAs<VectorType>(); |
| 1577 | if (isBigEndian && (T->getVectorKind() == VectorType::NeonVector || |
| 1578 | T->getVectorKind() == VectorType::NeonPolyVector)) { |
| 1579 | // The ability to use vector initializer lists is a GNU vector extension |
| 1580 | // and is unrelated to the NEON intrinsics in arm_neon.h. On little |
| 1581 | // endian machines it works fine, however on big endian machines it |
| 1582 | // exhibits surprising behaviour: |
| 1583 | // |
| 1584 | // uint32x2_t x = {42, 64}; |
| 1585 | // return vget_lane_u32(x, 0); // Will return 64. |
| 1586 | // |
| 1587 | // Because of this, explicitly call out that it is non-portable. |
| 1588 | // |
| 1589 | SemaRef.Diag(IList->getLocStart(), |
| 1590 | diag::warn_neon_vector_initializer_non_portable); |
| 1591 | |
| 1592 | const char *typeCode; |
| 1593 | unsigned typeSize = SemaRef.Context.getTypeSize(elementType); |
| 1594 | |
| 1595 | if (elementType->isFloatingType()) |
| 1596 | typeCode = "f"; |
| 1597 | else if (elementType->isSignedIntegerType()) |
| 1598 | typeCode = "s"; |
| 1599 | else if (elementType->isUnsignedIntegerType()) |
| 1600 | typeCode = "u"; |
| 1601 | else |
| 1602 | llvm_unreachable("Invalid element type!"); |
| 1603 | |
| 1604 | SemaRef.Diag(IList->getLocStart(), |
| 1605 | SemaRef.Context.getTypeSize(VT) > 64 ? |
| 1606 | diag::note_neon_vector_initializer_non_portable_q : |
| 1607 | diag::note_neon_vector_initializer_non_portable) |
| 1608 | << typeCode << typeSize; |
| 1609 | } |
| 1610 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1611 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1612 | } |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1613 | |
| 1614 | InitializedEntity ElementEntity = |
| 1615 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1616 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1617 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 1618 | for (unsigned i = 0; i < maxElements; ++i) { |
| 1619 | // Don't attempt to go past the end of the init list |
| 1620 | if (Index >= IList->getNumInits()) |
| 1621 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1622 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1623 | ElementEntity.setElementIndex(Index); |
| 1624 | |
| 1625 | QualType IType = IList->getInit(Index)->getType(); |
| 1626 | if (!IType->isVectorType()) { |
| 1627 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1628 | StructuredList, StructuredIndex); |
| 1629 | ++numEltsInit; |
| 1630 | } else { |
| 1631 | QualType VecType; |
| 1632 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 1633 | unsigned numIElts = IVT->getNumElements(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1634 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1635 | if (IType->isExtVectorType()) |
| 1636 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 1637 | else |
| 1638 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1639 | IVT->getVectorKind()); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1640 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 1641 | StructuredList, StructuredIndex); |
| 1642 | numEltsInit += numIElts; |
| 1643 | } |
| 1644 | } |
| 1645 | |
| 1646 | // OpenCL requires all elements to be initialized. |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1647 | if (numEltsInit != maxElements) { |
| 1648 | if (!VerifyOnly) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1649 | SemaRef.Diag(IList->getLocStart(), |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1650 | diag::err_vector_incorrect_num_initializers) |
| 1651 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
| 1652 | hadError = true; |
| 1653 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1654 | } |
| 1655 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1656 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1657 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1658 | llvm::APSInt elementIndex, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1659 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1660 | unsigned &Index, |
| 1661 | InitListExpr *StructuredList, |
| 1662 | unsigned &StructuredIndex) { |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1663 | const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType); |
| 1664 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1665 | // Check for the special-case of initializing an array with a string. |
| 1666 | if (Index < IList->getNumInits()) { |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1667 | if (IsStringInit(IList->getInit(Index), arrayType, SemaRef.Context) == |
| 1668 | SIF_None) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1669 | // We place the string literal directly into the resulting |
| 1670 | // initializer list. This is the only place where the structure |
| 1671 | // of the structured initializer list doesn't match exactly, |
| 1672 | // because doing so would involve allocating one character |
| 1673 | // constant for each string. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1674 | if (!VerifyOnly) { |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1675 | CheckStringInit(IList->getInit(Index), DeclType, arrayType, SemaRef); |
| 1676 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1677 | IList->getInit(Index)); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1678 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
| 1679 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1680 | ++Index; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1681 | return; |
| 1682 | } |
| 1683 | } |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1684 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1685 | // Check for VLAs; in standard C it would be possible to check this |
| 1686 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 1687 | // them in all sorts of strange places). |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1688 | if (!VerifyOnly) |
| 1689 | SemaRef.Diag(VAT->getSizeExpr()->getLocStart(), |
| 1690 | diag::err_variable_object_no_init) |
| 1691 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1692 | hadError = true; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1693 | ++Index; |
| 1694 | ++StructuredIndex; |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1695 | return; |
| 1696 | } |
| 1697 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1698 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1699 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 1700 | elementIndex.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1701 | bool maxElementsKnown = false; |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1702 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1703 | maxElements = CAT->getSize(); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1704 | elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1705 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1706 | maxElementsKnown = true; |
| 1707 | } |
| 1708 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1709 | QualType elementType = arrayType->getElementType(); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1710 | while (Index < IList->getNumInits()) { |
| 1711 | Expr *Init = IList->getInit(Index); |
| 1712 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1713 | // If we're not the subobject that matches up with the '{' for |
| 1714 | // the designator, we shouldn't be handling the |
| 1715 | // designator. Return immediately. |
| 1716 | if (!SubobjectIsDesignatorContext) |
| 1717 | return; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1718 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1719 | // Handle this designated initializer. elementIndex will be |
| 1720 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1721 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1722 | DeclType, nullptr, &elementIndex, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1723 | StructuredList, StructuredIndex, true, |
| 1724 | false)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1725 | hadError = true; |
| 1726 | continue; |
| 1727 | } |
| 1728 | |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1729 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1730 | maxElements = maxElements.extend(elementIndex.getBitWidth()); |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1731 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1732 | elementIndex = elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1733 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1734 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1735 | // If the array is of incomplete type, keep track of the number of |
| 1736 | // elements in the initializer. |
| 1737 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1738 | maxElements = elementIndex; |
| 1739 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1740 | continue; |
| 1741 | } |
| 1742 | |
| 1743 | // If we know the maximum number of elements, and we've already |
| 1744 | // hit it, stop consuming elements in the initializer list. |
| 1745 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1746 | break; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1747 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1748 | InitializedEntity ElementEntity = |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1749 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1750 | Entity); |
| 1751 | // Check this element. |
| 1752 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1753 | StructuredList, StructuredIndex); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1754 | ++elementIndex; |
| 1755 | |
| 1756 | // If the array is of incomplete type, keep track of the number of |
| 1757 | // elements in the initializer. |
| 1758 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1759 | maxElements = elementIndex; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1760 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1761 | if (!hadError && DeclType->isIncompleteArrayType() && !VerifyOnly) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1762 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1763 | // be calculated here. |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1764 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Richard Smith | 73edb6d | 2017-01-24 23:18:28 +0000 | [diff] [blame] | 1765 | if (maxElements == Zero && !Entity.isVariableLengthArrayNew()) { |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1766 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1767 | // but is supported by GNU. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1768 | SemaRef.Diag(IList->getLocStart(), |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1769 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1770 | } |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1771 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1772 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1773 | ArrayType::Normal, 0); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1774 | } |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1775 | if (!hadError && VerifyOnly) { |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1776 | // If there are any members of the array that get value-initialized, check |
| 1777 | // that is possible. That happens if we know the bound and don't have |
| 1778 | // enough elements, or if we're performing an array new with an unknown |
| 1779 | // bound. |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1780 | // FIXME: This needs to detect holes left by designated initializers too. |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1781 | if ((maxElementsKnown && elementIndex < maxElements) || |
| 1782 | Entity.isVariableLengthArrayNew()) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1783 | CheckEmptyInitializable(InitializedEntity::InitializeElement( |
| 1784 | SemaRef.Context, 0, Entity), |
| 1785 | IList->getLocEnd()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1786 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1787 | } |
| 1788 | |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1789 | bool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 1790 | Expr *InitExpr, |
| 1791 | FieldDecl *Field, |
| 1792 | bool TopLevelObject) { |
| 1793 | // Handle GNU flexible array initializers. |
| 1794 | unsigned FlexArrayDiag; |
| 1795 | if (isa<InitListExpr>(InitExpr) && |
| 1796 | cast<InitListExpr>(InitExpr)->getNumInits() == 0) { |
| 1797 | // Empty flexible array init always allowed as an extension |
| 1798 | FlexArrayDiag = diag::ext_flexible_array_init; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1799 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1800 | // Disallow flexible array init in C++; it is not required for gcc |
| 1801 | // compatibility, and it needs work to IRGen correctly in general. |
| 1802 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1803 | } else if (!TopLevelObject) { |
| 1804 | // Disallow flexible array init on non-top-level object |
| 1805 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1806 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
| 1807 | // Disallow flexible array init on anything which is not a variable. |
| 1808 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1809 | } else if (cast<VarDecl>(Entity.getDecl())->hasLocalStorage()) { |
| 1810 | // Disallow flexible array init on local variables. |
| 1811 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1812 | } else { |
| 1813 | // Allow other cases. |
| 1814 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1815 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1816 | |
| 1817 | if (!VerifyOnly) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1818 | SemaRef.Diag(InitExpr->getLocStart(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1819 | FlexArrayDiag) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1820 | << InitExpr->getLocStart(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1821 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1822 | << Field; |
| 1823 | } |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1824 | |
| 1825 | return FlexArrayDiag != diag::ext_flexible_array_init; |
| 1826 | } |
| 1827 | |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 1828 | void InitListChecker::CheckStructUnionTypes( |
| 1829 | const InitializedEntity &Entity, InitListExpr *IList, QualType DeclType, |
| 1830 | CXXRecordDecl::base_class_range Bases, RecordDecl::field_iterator Field, |
| 1831 | bool SubobjectIsDesignatorContext, unsigned &Index, |
| 1832 | InitListExpr *StructuredList, unsigned &StructuredIndex, |
| 1833 | bool TopLevelObject) { |
| 1834 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1835 | |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1836 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1837 | // confusion, we forgo checking the intializer for the entire record. |
| 1838 | if (structDecl->isInvalidDecl()) { |
Richard Smith | 845aa66 | 2012-09-28 21:23:50 +0000 | [diff] [blame] | 1839 | // Assume it was supposed to consume a single initializer. |
| 1840 | ++Index; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1841 | hadError = true; |
| 1842 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1843 | } |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1844 | |
| 1845 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1846 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1847 | |
| 1848 | // If there's a default initializer, use it. |
| 1849 | if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->hasInClassInitializer()) { |
| 1850 | if (VerifyOnly) |
| 1851 | return; |
| 1852 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1853 | Field != FieldEnd; ++Field) { |
| 1854 | if (Field->hasInClassInitializer()) { |
| 1855 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1856 | // FIXME: Actually build a CXXDefaultInitExpr? |
| 1857 | return; |
| 1858 | } |
| 1859 | } |
| 1860 | } |
| 1861 | |
Reid Kleckner | 6d829bd | 2014-11-12 21:30:23 +0000 | [diff] [blame] | 1862 | // Value-initialize the first member of the union that isn't an unnamed |
| 1863 | // bitfield. |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1864 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1865 | Field != FieldEnd; ++Field) { |
Reid Kleckner | 6d829bd | 2014-11-12 21:30:23 +0000 | [diff] [blame] | 1866 | if (!Field->isUnnamedBitfield()) { |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1867 | if (VerifyOnly) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1868 | CheckEmptyInitializable( |
| 1869 | InitializedEntity::InitializeMember(*Field, &Entity), |
| 1870 | IList->getLocEnd()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1871 | else |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1872 | StructuredList->setInitializedFieldInUnion(*Field); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1873 | break; |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1874 | } |
| 1875 | } |
| 1876 | return; |
| 1877 | } |
| 1878 | |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 1879 | bool InitializedSomething = false; |
| 1880 | |
| 1881 | // If we have any base classes, they are initialized prior to the fields. |
| 1882 | for (auto &Base : Bases) { |
| 1883 | Expr *Init = Index < IList->getNumInits() ? IList->getInit(Index) : nullptr; |
| 1884 | SourceLocation InitLoc = Init ? Init->getLocStart() : IList->getLocEnd(); |
| 1885 | |
| 1886 | // Designated inits always initialize fields, so if we see one, all |
| 1887 | // remaining base classes have no explicit initializer. |
| 1888 | if (Init && isa<DesignatedInitExpr>(Init)) |
| 1889 | Init = nullptr; |
| 1890 | |
| 1891 | InitializedEntity BaseEntity = InitializedEntity::InitializeBase( |
| 1892 | SemaRef.Context, &Base, false, &Entity); |
| 1893 | if (Init) { |
| 1894 | CheckSubElementType(BaseEntity, IList, Base.getType(), Index, |
| 1895 | StructuredList, StructuredIndex); |
| 1896 | InitializedSomething = true; |
| 1897 | } else if (VerifyOnly) { |
| 1898 | CheckEmptyInitializable(BaseEntity, InitLoc); |
| 1899 | } |
| 1900 | } |
| 1901 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1902 | // If structDecl is a forward declaration, this loop won't do |
| 1903 | // anything except look at designated initializers; That's okay, |
| 1904 | // because an error should get printed out elsewhere. It might be |
| 1905 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1906 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1907 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Daniel Marjamaki | 817a3bf | 2017-09-29 09:44:41 +0000 | [diff] [blame] | 1908 | bool CheckForMissingFields = |
| 1909 | !IList->isIdiomaticZeroInitializer(SemaRef.getLangOpts()); |
| 1910 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1911 | while (Index < IList->getNumInits()) { |
| 1912 | Expr *Init = IList->getInit(Index); |
| 1913 | |
| 1914 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1915 | // If we're not the subobject that matches up with the '{' for |
| 1916 | // the designator, we shouldn't be handling the |
| 1917 | // designator. Return immediately. |
| 1918 | if (!SubobjectIsDesignatorContext) |
| 1919 | return; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1920 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1921 | // Handle this designated initializer. Field will be updated to |
| 1922 | // the next field that we'll be initializing. |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1923 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1924 | DeclType, &Field, nullptr, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1925 | StructuredList, StructuredIndex, |
| 1926 | true, TopLevelObject)) |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1927 | hadError = true; |
| 1928 | |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1929 | InitializedSomething = true; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1930 | |
| 1931 | // Disable check for missing fields when designators are used. |
| 1932 | // This matches gcc behaviour. |
| 1933 | CheckForMissingFields = false; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1934 | continue; |
| 1935 | } |
| 1936 | |
| 1937 | if (Field == FieldEnd) { |
| 1938 | // We've run out of fields. We're done. |
| 1939 | break; |
| 1940 | } |
| 1941 | |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1942 | // We've already initialized a member of a union. We're done. |
| 1943 | if (InitializedSomething && DeclType->isUnionType()) |
| 1944 | break; |
| 1945 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1946 | // If we've hit the flexible array member at the end, we're done. |
| 1947 | if (Field->getType()->isIncompleteArrayType()) |
| 1948 | break; |
| 1949 | |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1950 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1951 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1952 | ++Field; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1953 | continue; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1954 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1955 | |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1956 | // Make sure we can use this declaration. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1957 | bool InvalidUse; |
| 1958 | if (VerifyOnly) |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 1959 | InvalidUse = !SemaRef.CanUseDecl(*Field, TreatUnavailableAsInvalid); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1960 | else |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1961 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1962 | IList->getInit(Index)->getLocStart()); |
| 1963 | if (InvalidUse) { |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1964 | ++Index; |
| 1965 | ++Field; |
| 1966 | hadError = true; |
| 1967 | continue; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1968 | } |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1969 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1970 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1971 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1972 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 1973 | StructuredList, StructuredIndex); |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1974 | InitializedSomething = true; |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1975 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1976 | if (DeclType->isUnionType() && !VerifyOnly) { |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1977 | // Initialize the first field within the union. |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1978 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1979 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1980 | |
| 1981 | ++Field; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1982 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1983 | |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1984 | // Emit warnings for missing struct field initializers. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1985 | if (!VerifyOnly && InitializedSomething && CheckForMissingFields && |
| 1986 | Field != FieldEnd && !Field->getType()->isIncompleteArrayType() && |
| 1987 | !DeclType->isUnionType()) { |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1988 | // It is possible we have one or more unnamed bitfields remaining. |
| 1989 | // Find first (if any) named field and emit warning. |
| 1990 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 1991 | it != end; ++it) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1992 | if (!it->isUnnamedBitfield() && !it->hasInClassInitializer()) { |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1993 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
Aaron Ballman | b9bb201 | 2014-01-03 14:54:10 +0000 | [diff] [blame] | 1994 | diag::warn_missing_field_initializers) << *it; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1995 | break; |
| 1996 | } |
| 1997 | } |
| 1998 | } |
| 1999 | |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 2000 | // Check that any remaining fields can be value-initialized. |
| 2001 | if (VerifyOnly && Field != FieldEnd && !DeclType->isUnionType() && |
| 2002 | !Field->getType()->isIncompleteArrayType()) { |
| 2003 | // FIXME: Should check for holes left by designated initializers too. |
| 2004 | for (; Field != FieldEnd && !hadError; ++Field) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 2005 | if (!Field->isUnnamedBitfield() && !Field->hasInClassInitializer()) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 2006 | CheckEmptyInitializable( |
| 2007 | InitializedEntity::InitializeMember(*Field, &Entity), |
| 2008 | IList->getLocEnd()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 2009 | } |
| 2010 | } |
| 2011 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2012 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 2013 | Index >= IList->getNumInits()) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2014 | return; |
| 2015 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2016 | if (CheckFlexibleArrayInit(Entity, IList->getInit(Index), *Field, |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 2017 | TopLevelObject)) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2018 | hadError = true; |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 2019 | ++Index; |
| 2020 | return; |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2021 | } |
| 2022 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2023 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2024 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2025 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2026 | if (isa<InitListExpr>(IList->getInit(Index))) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2027 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2028 | StructuredList, StructuredIndex); |
| 2029 | else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2030 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 2031 | StructuredList, StructuredIndex); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 2032 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 2033 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2034 | /// Expand a field designator that refers to a member of an |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2035 | /// anonymous struct or union into a series of field designators that |
| 2036 | /// refers to the field within the appropriate subobject. |
| 2037 | /// |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2038 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2039 | DesignatedInitExpr *DIE, |
| 2040 | unsigned DesigIdx, |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2041 | IndirectFieldDecl *IndirectField) { |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2042 | typedef DesignatedInitExpr::Designator Designator; |
| 2043 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2044 | // Build the replacement designators. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2045 | SmallVector<Designator, 4> Replacements; |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2046 | for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), |
| 2047 | PE = IndirectField->chain_end(); PI != PE; ++PI) { |
| 2048 | if (PI + 1 == PE) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2049 | Replacements.push_back(Designator((IdentifierInfo *)nullptr, |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2050 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 2051 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 2052 | else |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2053 | Replacements.push_back(Designator((IdentifierInfo *)nullptr, |
| 2054 | SourceLocation(), SourceLocation())); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2055 | assert(isa<FieldDecl>(*PI)); |
| 2056 | Replacements.back().setField(cast<FieldDecl>(*PI)); |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2057 | } |
| 2058 | |
| 2059 | // Expand the current designator into the set of replacement |
| 2060 | // designators, so we have a full subobject path down to where the |
| 2061 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 03e8bdc | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 2062 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2063 | &Replacements[0] + Replacements.size()); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2064 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2065 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2066 | static DesignatedInitExpr *CloneDesignatedInitExpr(Sema &SemaRef, |
| 2067 | DesignatedInitExpr *DIE) { |
| 2068 | unsigned NumIndexExprs = DIE->getNumSubExprs() - 1; |
| 2069 | SmallVector<Expr*, 4> IndexExprs(NumIndexExprs); |
| 2070 | for (unsigned I = 0; I < NumIndexExprs; ++I) |
| 2071 | IndexExprs[I] = DIE->getSubExpr(I + 1); |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 2072 | return DesignatedInitExpr::Create(SemaRef.Context, DIE->designators(), |
| 2073 | IndexExprs, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2074 | DIE->getEqualOrColonLoc(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2075 | DIE->usesGNUSyntax(), DIE->getInit()); |
| 2076 | } |
| 2077 | |
Kaelyn Uhrain | b02c5e9 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 2078 | namespace { |
| 2079 | |
| 2080 | // Callback to only accept typo corrections that are for field members of |
| 2081 | // the given struct or union. |
| 2082 | class FieldInitializerValidatorCCC : public CorrectionCandidateCallback { |
| 2083 | public: |
| 2084 | explicit FieldInitializerValidatorCCC(RecordDecl *RD) |
| 2085 | : Record(RD) {} |
| 2086 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 2087 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
Kaelyn Uhrain | b02c5e9 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 2088 | FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>(); |
| 2089 | return FD && FD->getDeclContext()->getRedeclContext()->Equals(Record); |
| 2090 | } |
| 2091 | |
| 2092 | private: |
| 2093 | RecordDecl *Record; |
| 2094 | }; |
| 2095 | |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 2096 | } // end anonymous namespace |
Kaelyn Uhrain | b02c5e9 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 2097 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2098 | /// Check the well-formedness of a C99 designated initializer. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2099 | /// |
| 2100 | /// Determines whether the designated initializer @p DIE, which |
| 2101 | /// resides at the given @p Index within the initializer list @p |
| 2102 | /// IList, is well-formed for a current object of type @p DeclType |
| 2103 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2104 | /// within the current subobject is returned in either |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2105 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2106 | /// |
| 2107 | /// @param IList The initializer list in which this designated |
| 2108 | /// initializer occurs. |
| 2109 | /// |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 2110 | /// @param DIE The designated initializer expression. |
| 2111 | /// |
| 2112 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2113 | /// |
Dmitri Gribenko | adba9be | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 2114 | /// @param CurrentObjectType The type of the "current object" (C99 6.7.8p17), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2115 | /// into which the designation in @p DIE should refer. |
| 2116 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2117 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 2118 | /// a field, this will be set to the field declaration corresponding |
| 2119 | /// to the field named by the designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2120 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2121 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 2122 | /// DIE is an array designator or GNU array-range designator, this |
| 2123 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2124 | /// |
| 2125 | /// @param Index Index into @p IList where the designated initializer |
| 2126 | /// @p DIE occurs. |
| 2127 | /// |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2128 | /// @param StructuredList The initializer list expression that |
| 2129 | /// describes all of the subobject initializers in the order they'll |
| 2130 | /// actually be initialized. |
| 2131 | /// |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2132 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2133 | bool |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2134 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2135 | InitListExpr *IList, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2136 | DesignatedInitExpr *DIE, |
| 2137 | unsigned DesigIdx, |
| 2138 | QualType &CurrentObjectType, |
| 2139 | RecordDecl::field_iterator *NextField, |
| 2140 | llvm::APSInt *NextElementIndex, |
| 2141 | unsigned &Index, |
| 2142 | InitListExpr *StructuredList, |
| 2143 | unsigned &StructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2144 | bool FinishSubobjectInit, |
| 2145 | bool TopLevelObject) { |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 2146 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2147 | // Check the actual initialization for the designated object type. |
| 2148 | bool prevHadError = hadError; |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 2149 | |
| 2150 | // Temporarily remove the designator expression from the |
| 2151 | // initializer list that the child calls see, so that we don't try |
| 2152 | // to re-process the designator. |
| 2153 | unsigned OldIndex = Index; |
| 2154 | IList->setInit(OldIndex, DIE->getInit()); |
| 2155 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2156 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2157 | StructuredList, StructuredIndex); |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 2158 | |
| 2159 | // Restore the designated initializer expression in the syntactic |
| 2160 | // form of the initializer list. |
| 2161 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 2162 | DIE->setInit(IList->getInit(OldIndex)); |
| 2163 | IList->setInit(OldIndex, DIE); |
| 2164 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2165 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2166 | } |
| 2167 | |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 2168 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2169 | bool IsFirstDesignator = (DesigIdx == 0); |
| 2170 | if (!VerifyOnly) { |
| 2171 | assert((IsFirstDesignator || StructuredList) && |
| 2172 | "Need a non-designated initializer list to start from"); |
| 2173 | |
| 2174 | // Determine the structural initializer list that corresponds to the |
| 2175 | // current subobject. |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2176 | if (IsFirstDesignator) |
| 2177 | StructuredList = SyntacticToSemantic.lookup(IList); |
| 2178 | else { |
| 2179 | Expr *ExistingInit = StructuredIndex < StructuredList->getNumInits() ? |
| 2180 | StructuredList->getInit(StructuredIndex) : nullptr; |
| 2181 | if (!ExistingInit && StructuredList->hasArrayFiller()) |
| 2182 | ExistingInit = StructuredList->getArrayFiller(); |
| 2183 | |
| 2184 | if (!ExistingInit) |
| 2185 | StructuredList = |
| 2186 | getStructuredSubobjectInit(IList, Index, CurrentObjectType, |
| 2187 | StructuredList, StructuredIndex, |
| 2188 | SourceRange(D->getLocStart(), |
| 2189 | DIE->getLocEnd())); |
| 2190 | else if (InitListExpr *Result = dyn_cast<InitListExpr>(ExistingInit)) |
| 2191 | StructuredList = Result; |
| 2192 | else { |
| 2193 | if (DesignatedInitUpdateExpr *E = |
| 2194 | dyn_cast<DesignatedInitUpdateExpr>(ExistingInit)) |
| 2195 | StructuredList = E->getUpdater(); |
| 2196 | else { |
| 2197 | DesignatedInitUpdateExpr *DIUE = |
| 2198 | new (SemaRef.Context) DesignatedInitUpdateExpr(SemaRef.Context, |
| 2199 | D->getLocStart(), ExistingInit, |
| 2200 | DIE->getLocEnd()); |
| 2201 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, DIUE); |
| 2202 | StructuredList = DIUE->getUpdater(); |
| 2203 | } |
| 2204 | |
| 2205 | // We need to check on source range validity because the previous |
| 2206 | // initializer does not have to be an explicit initializer. e.g., |
| 2207 | // |
| 2208 | // struct P { int a, b; }; |
| 2209 | // struct PP { struct P p } l = { { .a = 2 }, .p.b = 3 }; |
| 2210 | // |
| 2211 | // There is an overwrite taking place because the first braced initializer |
| 2212 | // list "{ .a = 2 }" already provides value for .p.b (which is zero). |
| 2213 | if (ExistingInit->getSourceRange().isValid()) { |
| 2214 | // We are creating an initializer list that initializes the |
| 2215 | // subobjects of the current object, but there was already an |
| 2216 | // initialization that completely initialized the current |
| 2217 | // subobject, e.g., by a compound literal: |
| 2218 | // |
| 2219 | // struct X { int a, b; }; |
| 2220 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
| 2221 | // |
| 2222 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 2223 | // designated initializer re-initializes the whole |
| 2224 | // subobject [0], overwriting previous initializers. |
| 2225 | SemaRef.Diag(D->getLocStart(), |
| 2226 | diag::warn_subobject_initializer_overrides) |
| 2227 | << SourceRange(D->getLocStart(), DIE->getLocEnd()); |
| 2228 | |
| 2229 | SemaRef.Diag(ExistingInit->getLocStart(), |
| 2230 | diag::note_previous_initializer) |
| 2231 | << /*FIXME:has side effects=*/0 |
| 2232 | << ExistingInit->getSourceRange(); |
| 2233 | } |
| 2234 | } |
| 2235 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2236 | assert(StructuredList && "Expected a structured initializer list"); |
| 2237 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2238 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2239 | if (D->isFieldDesignator()) { |
| 2240 | // C99 6.7.8p7: |
| 2241 | // |
| 2242 | // If a designator has the form |
| 2243 | // |
| 2244 | // . identifier |
| 2245 | // |
| 2246 | // then the current object (defined below) shall have |
| 2247 | // structure or union type and the identifier shall be the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2248 | // name of a member of that type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2249 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2250 | if (!RT) { |
| 2251 | SourceLocation Loc = D->getDotLoc(); |
| 2252 | if (Loc.isInvalid()) |
| 2253 | Loc = D->getFieldLoc(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2254 | if (!VerifyOnly) |
| 2255 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2256 | << SemaRef.getLangOpts().CPlusPlus << CurrentObjectType; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2257 | ++Index; |
| 2258 | return true; |
| 2259 | } |
| 2260 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2261 | FieldDecl *KnownField = D->getField(); |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2262 | if (!KnownField) { |
| 2263 | IdentifierInfo *FieldName = D->getFieldName(); |
| 2264 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
| 2265 | for (NamedDecl *ND : Lookup) { |
| 2266 | if (auto *FD = dyn_cast<FieldDecl>(ND)) { |
| 2267 | KnownField = FD; |
| 2268 | break; |
| 2269 | } |
| 2270 | if (auto *IFD = dyn_cast<IndirectFieldDecl>(ND)) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2271 | // In verify mode, don't modify the original. |
| 2272 | if (VerifyOnly) |
| 2273 | DIE = CloneDesignatedInitExpr(SemaRef, DIE); |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2274 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IFD); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2275 | D = DIE->getDesignator(DesigIdx); |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2276 | KnownField = cast<FieldDecl>(*IFD->chain_begin()); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2277 | break; |
| 2278 | } |
| 2279 | } |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2280 | if (!KnownField) { |
| 2281 | if (VerifyOnly) { |
| 2282 | ++Index; |
| 2283 | return true; // No typo correction when just trying this out. |
| 2284 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2285 | |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2286 | // Name lookup found something, but it wasn't a field. |
| 2287 | if (!Lookup.empty()) { |
| 2288 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
| 2289 | << FieldName; |
| 2290 | SemaRef.Diag(Lookup.front()->getLocation(), |
| 2291 | diag::note_field_designator_found); |
| 2292 | ++Index; |
| 2293 | return true; |
| 2294 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2295 | |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2296 | // Name lookup didn't find anything. |
| 2297 | // Determine whether this was a typo for another field name. |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 2298 | if (TypoCorrection Corrected = SemaRef.CorrectTypo( |
| 2299 | DeclarationNameInfo(FieldName, D->getFieldLoc()), |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2300 | Sema::LookupMemberName, /*Scope=*/nullptr, /*SS=*/nullptr, |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 2301 | llvm::make_unique<FieldInitializerValidatorCCC>(RT->getDecl()), |
| 2302 | Sema::CTK_ErrorRecovery, RT->getDecl())) { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 2303 | SemaRef.diagnoseTypo( |
| 2304 | Corrected, |
| 2305 | SemaRef.PDiag(diag::err_field_designator_unknown_suggest) |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2306 | << FieldName << CurrentObjectType); |
| 2307 | KnownField = Corrected.getCorrectionDeclAs<FieldDecl>(); |
Benjamin Kramer | afe718f | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 2308 | hadError = true; |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 2309 | } else { |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2310 | // Typo correction didn't find anything. |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 2311 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 2312 | << FieldName << CurrentObjectType; |
| 2313 | ++Index; |
| 2314 | return true; |
| 2315 | } |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 2316 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2317 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2318 | |
David Majnemer | 58e4ea9 | 2014-08-23 01:48:50 +0000 | [diff] [blame] | 2319 | unsigned FieldIndex = 0; |
Akira Hatanaka | 8eccb9b | 2017-01-17 19:35:54 +0000 | [diff] [blame] | 2320 | |
| 2321 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) |
| 2322 | FieldIndex = CXXRD->getNumBases(); |
| 2323 | |
David Majnemer | 58e4ea9 | 2014-08-23 01:48:50 +0000 | [diff] [blame] | 2324 | for (auto *FI : RT->getDecl()->fields()) { |
| 2325 | if (FI->isUnnamedBitfield()) |
| 2326 | continue; |
Richard Smith | fe1bc70 | 2016-04-08 19:57:40 +0000 | [diff] [blame] | 2327 | if (declaresSameEntity(KnownField, FI)) { |
| 2328 | KnownField = FI; |
David Majnemer | 58e4ea9 | 2014-08-23 01:48:50 +0000 | [diff] [blame] | 2329 | break; |
Richard Smith | fe1bc70 | 2016-04-08 19:57:40 +0000 | [diff] [blame] | 2330 | } |
David Majnemer | 58e4ea9 | 2014-08-23 01:48:50 +0000 | [diff] [blame] | 2331 | ++FieldIndex; |
| 2332 | } |
| 2333 | |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2334 | RecordDecl::field_iterator Field = |
| 2335 | RecordDecl::field_iterator(DeclContext::decl_iterator(KnownField)); |
| 2336 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2337 | // All of the fields of a union are located at the same place in |
| 2338 | // the initializer list. |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 2339 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2340 | FieldIndex = 0; |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2341 | if (!VerifyOnly) { |
| 2342 | FieldDecl *CurrentField = StructuredList->getInitializedFieldInUnion(); |
Richard Smith | fe1bc70 | 2016-04-08 19:57:40 +0000 | [diff] [blame] | 2343 | if (CurrentField && !declaresSameEntity(CurrentField, *Field)) { |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2344 | assert(StructuredList->getNumInits() == 1 |
| 2345 | && "A union should never have more than one initializer!"); |
| 2346 | |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2347 | Expr *ExistingInit = StructuredList->getInit(0); |
Vassil Vassilev | 1a1678e | 2017-04-14 08:48:08 +0000 | [diff] [blame] | 2348 | if (ExistingInit) { |
| 2349 | // We're about to throw away an initializer, emit warning. |
| 2350 | SemaRef.Diag(D->getFieldLoc(), |
| 2351 | diag::warn_initializer_overrides) |
| 2352 | << D->getSourceRange(); |
| 2353 | SemaRef.Diag(ExistingInit->getLocStart(), |
| 2354 | diag::note_previous_initializer) |
| 2355 | << /*FIXME:has side effects=*/0 |
| 2356 | << ExistingInit->getSourceRange(); |
| 2357 | } |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2358 | |
| 2359 | // remove existing initializer |
| 2360 | StructuredList->resizeInits(SemaRef.Context, 0); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2361 | StructuredList->setInitializedFieldInUnion(nullptr); |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2362 | } |
| 2363 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2364 | StructuredList->setInitializedFieldInUnion(*Field); |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2365 | } |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 2366 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2367 | |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2368 | // Make sure we can use this declaration. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2369 | bool InvalidUse; |
| 2370 | if (VerifyOnly) |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 2371 | InvalidUse = !SemaRef.CanUseDecl(*Field, TreatUnavailableAsInvalid); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2372 | else |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2373 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, D->getFieldLoc()); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2374 | if (InvalidUse) { |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2375 | ++Index; |
| 2376 | return true; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2377 | } |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2378 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2379 | if (!VerifyOnly) { |
| 2380 | // Update the designator with the field declaration. |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2381 | D->setField(*Field); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2382 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2383 | // Make sure that our non-designated initializer list has space |
| 2384 | // for a subobject corresponding to this field. |
| 2385 | if (FieldIndex >= StructuredList->getNumInits()) |
| 2386 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
| 2387 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2388 | |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2389 | // This designator names a flexible array member. |
| 2390 | if (Field->getType()->isIncompleteArrayType()) { |
| 2391 | bool Invalid = false; |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 2392 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2393 | // We can't designate an object within the flexible array |
| 2394 | // member (because GCC doesn't allow it). |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2395 | if (!VerifyOnly) { |
| 2396 | DesignatedInitExpr::Designator *NextD |
| 2397 | = DIE->getDesignator(DesigIdx + 1); |
Erik Verbruggen | 11a2ecc | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 2398 | SemaRef.Diag(NextD->getLocStart(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2399 | diag::err_designator_into_flexible_array_member) |
Erik Verbruggen | 11a2ecc | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 2400 | << SourceRange(NextD->getLocStart(), |
| 2401 | DIE->getLocEnd()); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2402 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2403 | << *Field; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2404 | } |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2405 | Invalid = true; |
| 2406 | } |
| 2407 | |
Chris Lattner | 001b29c | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 2408 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 2409 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2410 | // The initializer is not an initializer list. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2411 | if (!VerifyOnly) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2412 | SemaRef.Diag(DIE->getInit()->getLocStart(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2413 | diag::err_flexible_array_init_needs_braces) |
| 2414 | << DIE->getInit()->getSourceRange(); |
| 2415 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2416 | << *Field; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2417 | } |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2418 | Invalid = true; |
| 2419 | } |
| 2420 | |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 2421 | // Check GNU flexible array initializer. |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2422 | if (!Invalid && CheckFlexibleArrayInit(Entity, DIE->getInit(), *Field, |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 2423 | TopLevelObject)) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2424 | Invalid = true; |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2425 | |
| 2426 | if (Invalid) { |
| 2427 | ++Index; |
| 2428 | return true; |
| 2429 | } |
| 2430 | |
| 2431 | // Initialize the array. |
| 2432 | bool prevHadError = hadError; |
| 2433 | unsigned newStructuredIndex = FieldIndex; |
| 2434 | unsigned OldIndex = Index; |
| 2435 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2436 | |
| 2437 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2438 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2439 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2440 | StructuredList, newStructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2441 | |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2442 | IList->setInit(OldIndex, DIE); |
| 2443 | if (hadError && !prevHadError) { |
| 2444 | ++Field; |
| 2445 | ++FieldIndex; |
| 2446 | if (NextField) |
| 2447 | *NextField = Field; |
| 2448 | StructuredIndex = FieldIndex; |
| 2449 | return true; |
| 2450 | } |
| 2451 | } else { |
| 2452 | // Recurse to check later designated subobjects. |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 2453 | QualType FieldType = Field->getType(); |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2454 | unsigned newStructuredIndex = FieldIndex; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2455 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2456 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2457 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2458 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2459 | FieldType, nullptr, nullptr, Index, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2460 | StructuredList, newStructuredIndex, |
Alexey Bataev | 86a489e | 2016-01-25 05:14:03 +0000 | [diff] [blame] | 2461 | FinishSubobjectInit, false)) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2462 | return true; |
| 2463 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2464 | |
| 2465 | // Find the position of the next field to be initialized in this |
| 2466 | // subobject. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2467 | ++Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2468 | ++FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2469 | |
| 2470 | // If this the first designator, our caller will continue checking |
| 2471 | // the rest of this struct/class/union subobject. |
| 2472 | if (IsFirstDesignator) { |
| 2473 | if (NextField) |
| 2474 | *NextField = Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2475 | StructuredIndex = FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2476 | return false; |
| 2477 | } |
| 2478 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2479 | if (!FinishSubobjectInit) |
| 2480 | return false; |
| 2481 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2482 | // We've already initialized something in the union; we're done. |
| 2483 | if (RT->getDecl()->isUnion()) |
| 2484 | return hadError; |
| 2485 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2486 | // Check the remaining fields within this class/struct/union subobject. |
| 2487 | bool prevHadError = hadError; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2488 | |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 2489 | auto NoBases = |
| 2490 | CXXRecordDecl::base_class_range(CXXRecordDecl::base_class_iterator(), |
| 2491 | CXXRecordDecl::base_class_iterator()); |
| 2492 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, NoBases, Field, |
| 2493 | false, Index, StructuredList, FieldIndex); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2494 | return hadError && !prevHadError; |
| 2495 | } |
| 2496 | |
| 2497 | // C99 6.7.8p6: |
| 2498 | // |
| 2499 | // If a designator has the form |
| 2500 | // |
| 2501 | // [ constant-expression ] |
| 2502 | // |
| 2503 | // then the current object (defined below) shall have array |
| 2504 | // type and the expression shall be an integer constant |
| 2505 | // expression. If the array is of unknown size, any |
| 2506 | // nonnegative value is valid. |
| 2507 | // |
| 2508 | // Additionally, cope with the GNU extension that permits |
| 2509 | // designators of the form |
| 2510 | // |
| 2511 | // [ constant-expression ... constant-expression ] |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 2512 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2513 | if (!AT) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2514 | if (!VerifyOnly) |
| 2515 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
| 2516 | << CurrentObjectType; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2517 | ++Index; |
| 2518 | return true; |
| 2519 | } |
| 2520 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2521 | Expr *IndexExpr = nullptr; |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2522 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 2523 | if (D->isArrayDesignator()) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2524 | IndexExpr = DIE->getArrayIndex(*D); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2525 | DesignatedStartIndex = IndexExpr->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2526 | DesignatedEndIndex = DesignatedStartIndex; |
| 2527 | } else { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2528 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2529 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2530 | DesignatedStartIndex = |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2531 | DIE->getArrayRangeStart(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2532 | DesignatedEndIndex = |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2533 | DIE->getArrayRangeEnd(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2534 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2535 | |
Chris Lattner | b0ed51d | 2011-02-19 22:28:58 +0000 | [diff] [blame] | 2536 | // Codegen can't handle evaluating array range designators that have side |
| 2537 | // effects, because we replicate the AST value for each initialized element. |
| 2538 | // As such, set the sawArrayRangeDesignator() bit if we initialize multiple |
| 2539 | // elements with something that has a side effect, so codegen can emit an |
| 2540 | // "error unsupported" error instead of miscompiling the app. |
| 2541 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2542 | DIE->getInit()->HasSideEffects(SemaRef.Context) && !VerifyOnly) |
Douglas Gregor | bf7207a | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 2543 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2544 | } |
| 2545 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2546 | if (isa<ConstantArrayType>(AT)) { |
| 2547 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2548 | DesignatedStartIndex |
| 2549 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2550 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2551 | DesignatedEndIndex |
| 2552 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2553 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 2554 | if (DesignatedEndIndex >= MaxElements) { |
Eli Friedman | ed0f916 | 2011-09-26 18:53:43 +0000 | [diff] [blame] | 2555 | if (!VerifyOnly) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2556 | SemaRef.Diag(IndexExpr->getLocStart(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2557 | diag::err_array_designator_too_large) |
| 2558 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
| 2559 | << IndexExpr->getSourceRange(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2560 | ++Index; |
| 2561 | return true; |
| 2562 | } |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2563 | } else { |
Argyrios Kyrtzidis | 4746c2f | 2015-07-27 23:16:53 +0000 | [diff] [blame] | 2564 | unsigned DesignatedIndexBitWidth = |
| 2565 | ConstantArrayType::getMaxSizeBits(SemaRef.Context); |
| 2566 | DesignatedStartIndex = |
| 2567 | DesignatedStartIndex.extOrTrunc(DesignatedIndexBitWidth); |
| 2568 | DesignatedEndIndex = |
| 2569 | DesignatedEndIndex.extOrTrunc(DesignatedIndexBitWidth); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2570 | DesignatedStartIndex.setIsUnsigned(true); |
| 2571 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2572 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2573 | |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2574 | if (!VerifyOnly && StructuredList->isStringLiteralInit()) { |
| 2575 | // We're modifying a string literal init; we have to decompose the string |
| 2576 | // so we can modify the individual characters. |
| 2577 | ASTContext &Context = SemaRef.Context; |
| 2578 | Expr *SubExpr = StructuredList->getInit(0)->IgnoreParens(); |
| 2579 | |
| 2580 | // Compute the character type |
| 2581 | QualType CharTy = AT->getElementType(); |
| 2582 | |
| 2583 | // Compute the type of the integer literals. |
| 2584 | QualType PromotedCharTy = CharTy; |
| 2585 | if (CharTy->isPromotableIntegerType()) |
| 2586 | PromotedCharTy = Context.getPromotedIntegerType(CharTy); |
| 2587 | unsigned PromotedCharTyWidth = Context.getTypeSize(PromotedCharTy); |
| 2588 | |
| 2589 | if (StringLiteral *SL = dyn_cast<StringLiteral>(SubExpr)) { |
| 2590 | // Get the length of the string. |
| 2591 | uint64_t StrLen = SL->getLength(); |
| 2592 | if (cast<ConstantArrayType>(AT)->getSize().ult(StrLen)) |
| 2593 | StrLen = cast<ConstantArrayType>(AT)->getSize().getZExtValue(); |
| 2594 | StructuredList->resizeInits(Context, StrLen); |
| 2595 | |
| 2596 | // Build a literal for each character in the string, and put them into |
| 2597 | // the init list. |
| 2598 | for (unsigned i = 0, e = StrLen; i != e; ++i) { |
| 2599 | llvm::APInt CodeUnit(PromotedCharTyWidth, SL->getCodeUnit(i)); |
| 2600 | Expr *Init = new (Context) IntegerLiteral( |
Eli Friedman | 6cc05f7 | 2013-06-11 22:26:34 +0000 | [diff] [blame] | 2601 | Context, CodeUnit, PromotedCharTy, SubExpr->getExprLoc()); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2602 | if (CharTy != PromotedCharTy) |
| 2603 | Init = ImplicitCastExpr::Create(Context, CharTy, CK_IntegralCast, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2604 | Init, nullptr, VK_RValue); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2605 | StructuredList->updateInit(Context, i, Init); |
| 2606 | } |
| 2607 | } else { |
| 2608 | ObjCEncodeExpr *E = cast<ObjCEncodeExpr>(SubExpr); |
| 2609 | std::string Str; |
| 2610 | Context.getObjCEncodingForType(E->getEncodedType(), Str); |
| 2611 | |
| 2612 | // Get the length of the string. |
| 2613 | uint64_t StrLen = Str.size(); |
| 2614 | if (cast<ConstantArrayType>(AT)->getSize().ult(StrLen)) |
| 2615 | StrLen = cast<ConstantArrayType>(AT)->getSize().getZExtValue(); |
| 2616 | StructuredList->resizeInits(Context, StrLen); |
| 2617 | |
| 2618 | // Build a literal for each character in the string, and put them into |
| 2619 | // the init list. |
| 2620 | for (unsigned i = 0, e = StrLen; i != e; ++i) { |
| 2621 | llvm::APInt CodeUnit(PromotedCharTyWidth, Str[i]); |
| 2622 | Expr *Init = new (Context) IntegerLiteral( |
Eli Friedman | 6cc05f7 | 2013-06-11 22:26:34 +0000 | [diff] [blame] | 2623 | Context, CodeUnit, PromotedCharTy, SubExpr->getExprLoc()); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2624 | if (CharTy != PromotedCharTy) |
| 2625 | Init = ImplicitCastExpr::Create(Context, CharTy, CK_IntegralCast, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2626 | Init, nullptr, VK_RValue); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2627 | StructuredList->updateInit(Context, i, Init); |
| 2628 | } |
| 2629 | } |
| 2630 | } |
| 2631 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2632 | // Make sure that our non-designated initializer list has space |
| 2633 | // for a subobject corresponding to this array element. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2634 | if (!VerifyOnly && |
| 2635 | DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2636 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2637 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2638 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2639 | // Repeatedly perform subobject initializations in the range |
| 2640 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2641 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2642 | // Move to the next designator |
| 2643 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2644 | unsigned OldIndex = Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2645 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2646 | InitializedEntity ElementEntity = |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2647 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2648 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2649 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 2650 | // Recurse to check later designated subobjects. |
| 2651 | QualType ElementType = AT->getElementType(); |
| 2652 | Index = OldIndex; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2653 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2654 | ElementEntity.setElementIndex(ElementIndex); |
Alexey Bataev | 86a489e | 2016-01-25 05:14:03 +0000 | [diff] [blame] | 2655 | if (CheckDesignatedInitializer( |
| 2656 | ElementEntity, IList, DIE, DesigIdx + 1, ElementType, nullptr, |
| 2657 | nullptr, Index, StructuredList, ElementIndex, |
| 2658 | FinishSubobjectInit && (DesignatedStartIndex == DesignatedEndIndex), |
| 2659 | false)) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2660 | return true; |
| 2661 | |
| 2662 | // Move to the next index in the array that we'll be initializing. |
| 2663 | ++DesignatedStartIndex; |
| 2664 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2665 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2666 | |
| 2667 | // If this the first designator, our caller will continue checking |
| 2668 | // the rest of this array subobject. |
| 2669 | if (IsFirstDesignator) { |
| 2670 | if (NextElementIndex) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2671 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2672 | StructuredIndex = ElementIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2673 | return false; |
| 2674 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2675 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2676 | if (!FinishSubobjectInit) |
| 2677 | return false; |
| 2678 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2679 | // Check the remaining elements within this array subobject. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2680 | bool prevHadError = hadError; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2681 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 2682 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2683 | StructuredList, ElementIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2684 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2685 | } |
| 2686 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2687 | // Get the structured initializer list for a subobject of type |
| 2688 | // @p CurrentObjectType. |
| 2689 | InitListExpr * |
| 2690 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 2691 | QualType CurrentObjectType, |
| 2692 | InitListExpr *StructuredList, |
| 2693 | unsigned StructuredIndex, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2694 | SourceRange InitRange, |
| 2695 | bool IsFullyOverwritten) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2696 | if (VerifyOnly) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2697 | return nullptr; // No structured list in verification-only mode. |
| 2698 | Expr *ExistingInit = nullptr; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2699 | if (!StructuredList) |
Benjamin Kramer | 6b441d6 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 2700 | ExistingInit = SyntacticToSemantic.lookup(IList); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2701 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 2702 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2703 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2704 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2705 | // There might have already been initializers for subobjects of the current |
| 2706 | // object, but a subsequent initializer list will overwrite the entirety |
| 2707 | // of the current object. (See DR 253 and C99 6.7.8p21). e.g., |
| 2708 | // |
| 2709 | // struct P { char x[6]; }; |
| 2710 | // struct P l = { .x[2] = 'x', .x = { [0] = 'f' } }; |
| 2711 | // |
| 2712 | // The first designated initializer is ignored, and l.x is just "f". |
| 2713 | if (!IsFullyOverwritten) |
| 2714 | return Result; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2715 | |
| 2716 | if (ExistingInit) { |
| 2717 | // We are creating an initializer list that initializes the |
| 2718 | // subobjects of the current object, but there was already an |
| 2719 | // initialization that completely initialized the current |
| 2720 | // subobject, e.g., by a compound literal: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2721 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2722 | // struct X { int a, b; }; |
| 2723 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2724 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2725 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 2726 | // designated initializer re-initializes the whole |
| 2727 | // subobject [0], overwriting previous initializers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2728 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2729 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2730 | << InitRange; |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2731 | SemaRef.Diag(ExistingInit->getLocStart(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2732 | diag::note_previous_initializer) |
Douglas Gregor | e6af7a0 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2733 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2734 | << ExistingInit->getSourceRange(); |
| 2735 | } |
| 2736 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2737 | InitListExpr *Result |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2738 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
Dmitri Gribenko | 78852e9 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 2739 | InitRange.getBegin(), None, |
Ted Kremenek | 013041e | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 2740 | InitRange.getEnd()); |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2741 | |
Eli Friedman | 91f5ae5 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 2742 | QualType ResultType = CurrentObjectType; |
| 2743 | if (!ResultType->isArrayType()) |
| 2744 | ResultType = ResultType.getNonLValueExprType(SemaRef.Context); |
| 2745 | Result->setType(ResultType); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2746 | |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2747 | // Pre-allocate storage for the structured initializer list. |
| 2748 | unsigned NumElements = 0; |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2749 | unsigned NumInits = 0; |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2750 | bool GotNumInits = false; |
| 2751 | if (!StructuredList) { |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2752 | NumInits = IList->getNumInits(); |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2753 | GotNumInits = true; |
| 2754 | } else if (Index < IList->getNumInits()) { |
| 2755 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) { |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2756 | NumInits = SubList->getNumInits(); |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2757 | GotNumInits = true; |
| 2758 | } |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2759 | } |
| 2760 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2761 | if (const ArrayType *AType |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2762 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 2763 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 2764 | NumElements = CAType->getSize().getZExtValue(); |
| 2765 | // Simple heuristic so that we don't allocate a very large |
| 2766 | // initializer with many empty entries at the end. |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2767 | if (GotNumInits && NumElements > NumInits) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2768 | NumElements = 0; |
| 2769 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2770 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2771 | NumElements = VType->getNumElements(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2772 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2773 | RecordDecl *RDecl = RType->getDecl(); |
| 2774 | if (RDecl->isUnion()) |
| 2775 | NumElements = 1; |
| 2776 | else |
Aaron Ballman | 62e47c4 | 2014-03-10 13:43:55 +0000 | [diff] [blame] | 2777 | NumElements = std::distance(RDecl->field_begin(), RDecl->field_end()); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2778 | } |
| 2779 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2780 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2781 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2782 | // Link this new initializer list into the structured initializer |
| 2783 | // lists. |
| 2784 | if (StructuredList) |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2785 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2786 | else { |
| 2787 | Result->setSyntacticForm(IList); |
| 2788 | SyntacticToSemantic[IList] = Result; |
| 2789 | } |
| 2790 | |
| 2791 | return Result; |
| 2792 | } |
| 2793 | |
| 2794 | /// Update the initializer at index @p StructuredIndex within the |
| 2795 | /// structured initializer list to the value @p expr. |
| 2796 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 2797 | unsigned &StructuredIndex, |
| 2798 | Expr *expr) { |
| 2799 | // No structured initializer list to update |
| 2800 | if (!StructuredList) |
| 2801 | return; |
| 2802 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2803 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 2804 | StructuredIndex, expr)) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2805 | // This initializer overwrites a previous initializer. Warn. |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2806 | // We need to check on source range validity because the previous |
| 2807 | // initializer does not have to be an explicit initializer. |
| 2808 | // struct P { int a, b; }; |
| 2809 | // struct PP { struct P p } l = { { .a = 2 }, .p.b = 3 }; |
| 2810 | // There is an overwrite taking place because the first braced initializer |
| 2811 | // list "{ .a = 2 }' already provides value for .p.b (which is zero). |
| 2812 | if (PrevInit->getSourceRange().isValid()) { |
| 2813 | SemaRef.Diag(expr->getLocStart(), |
| 2814 | diag::warn_initializer_overrides) |
| 2815 | << expr->getSourceRange(); |
| 2816 | |
| 2817 | SemaRef.Diag(PrevInit->getLocStart(), |
| 2818 | diag::note_previous_initializer) |
| 2819 | << /*FIXME:has side effects=*/0 |
| 2820 | << PrevInit->getSourceRange(); |
| 2821 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2822 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2823 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2824 | ++StructuredIndex; |
| 2825 | } |
| 2826 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2827 | /// Check that the given Index expression is a valid array designator |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2828 | /// value. This is essentially just a wrapper around |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2829 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2830 | /// and produces a reasonable diagnostic if there is a |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2831 | /// failure. Returns the index expression, possibly with an implicit cast |
| 2832 | /// added, on success. If everything went okay, Value will receive the |
| 2833 | /// value of the constant expression. |
| 2834 | static ExprResult |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2835 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2836 | SourceLocation Loc = Index->getLocStart(); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2837 | |
| 2838 | // Make sure this is an integer constant expression. |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2839 | ExprResult Result = S.VerifyIntegerConstantExpression(Index, &Value); |
| 2840 | if (Result.isInvalid()) |
| 2841 | return Result; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2842 | |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2843 | if (Value.isSigned() && Value.isNegative()) |
| 2844 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2845 | << Value.toString(10) << Index->getSourceRange(); |
| 2846 | |
Douglas Gregor | 51650d3 | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 2847 | Value.setIsUnsigned(true); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2848 | return Result; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2849 | } |
| 2850 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2851 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 2852 | SourceLocation Loc, |
| 2853 | bool GNUSyntax, |
| 2854 | ExprResult Init) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2855 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 2856 | |
| 2857 | bool Invalid = false; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2858 | SmallVector<ASTDesignator, 32> Designators; |
| 2859 | SmallVector<Expr *, 32> InitExpressions; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2860 | |
| 2861 | // Build designators and check array designator expressions. |
| 2862 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 2863 | const Designator &D = Desig.getDesignator(Idx); |
| 2864 | switch (D.getKind()) { |
| 2865 | case Designator::FieldDesignator: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2866 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2867 | D.getFieldLoc())); |
| 2868 | break; |
| 2869 | |
| 2870 | case Designator::ArrayDesignator: { |
| 2871 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 2872 | llvm::APSInt IndexValue; |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2873 | if (!Index->isTypeDependent() && !Index->isValueDependent()) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2874 | Index = CheckArrayDesignatorExpr(*this, Index, IndexValue).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2875 | if (!Index) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2876 | Invalid = true; |
| 2877 | else { |
| 2878 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2879 | D.getLBracketLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2880 | D.getRBracketLoc())); |
| 2881 | InitExpressions.push_back(Index); |
| 2882 | } |
| 2883 | break; |
| 2884 | } |
| 2885 | |
| 2886 | case Designator::ArrayRangeDesignator: { |
| 2887 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 2888 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 2889 | llvm::APSInt StartValue; |
| 2890 | llvm::APSInt EndValue; |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2891 | bool StartDependent = StartIndex->isTypeDependent() || |
| 2892 | StartIndex->isValueDependent(); |
| 2893 | bool EndDependent = EndIndex->isTypeDependent() || |
| 2894 | EndIndex->isValueDependent(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2895 | if (!StartDependent) |
| 2896 | StartIndex = |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2897 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2898 | if (!EndDependent) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2899 | EndIndex = CheckArrayDesignatorExpr(*this, EndIndex, EndValue).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2900 | |
| 2901 | if (!StartIndex || !EndIndex) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2902 | Invalid = true; |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2903 | else { |
| 2904 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2905 | if (StartDependent || EndDependent) { |
| 2906 | // Nothing to compute. |
| 2907 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2908 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2909 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2910 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2911 | |
Douglas Gregor | 0f9d400 | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 2912 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2913 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2914 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2915 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 2916 | Invalid = true; |
| 2917 | } else { |
| 2918 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2919 | D.getLBracketLoc(), |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2920 | D.getEllipsisLoc(), |
| 2921 | D.getRBracketLoc())); |
| 2922 | InitExpressions.push_back(StartIndex); |
| 2923 | InitExpressions.push_back(EndIndex); |
| 2924 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2925 | } |
| 2926 | break; |
| 2927 | } |
| 2928 | } |
| 2929 | } |
| 2930 | |
| 2931 | if (Invalid || Init.isInvalid()) |
| 2932 | return ExprError(); |
| 2933 | |
| 2934 | // Clear out the expressions within the designation. |
| 2935 | Desig.ClearExprs(*this); |
| 2936 | |
| 2937 | DesignatedInitExpr *DIE |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2938 | = DesignatedInitExpr::Create(Context, |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 2939 | Designators, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2940 | InitExpressions, Loc, GNUSyntax, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2941 | Init.getAs<Expr>()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2942 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2943 | if (!getLangOpts().C99) |
Douglas Gregor | c124e59 | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 2944 | Diag(DIE->getLocStart(), diag::ext_designated_init) |
| 2945 | << DIE->getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2946 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2947 | return DIE; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2948 | } |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 2949 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2950 | //===----------------------------------------------------------------------===// |
| 2951 | // Initialization entity |
| 2952 | //===----------------------------------------------------------------------===// |
| 2953 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2954 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2955 | const InitializedEntity &Parent) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2956 | : Parent(&Parent), Index(Index) |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2957 | { |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2958 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 2959 | Kind = EK_ArrayElement; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2960 | Type = AT->getElementType(); |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2961 | } else if (const VectorType *VT = Parent.getType()->getAs<VectorType>()) { |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2962 | Kind = EK_VectorElement; |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2963 | Type = VT->getElementType(); |
| 2964 | } else { |
| 2965 | const ComplexType *CT = Parent.getType()->getAs<ComplexType>(); |
| 2966 | assert(CT && "Unexpected type"); |
| 2967 | Kind = EK_ComplexElement; |
| 2968 | Type = CT->getElementType(); |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2969 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2970 | } |
| 2971 | |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2972 | InitializedEntity |
| 2973 | InitializedEntity::InitializeBase(ASTContext &Context, |
| 2974 | const CXXBaseSpecifier *Base, |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 2975 | bool IsInheritedVirtualBase, |
| 2976 | const InitializedEntity *Parent) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2977 | InitializedEntity Result; |
| 2978 | Result.Kind = EK_Base; |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 2979 | Result.Parent = Parent; |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2980 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 2981 | if (IsInheritedVirtualBase) |
| 2982 | Result.Base |= 0x01; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2983 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2984 | Result.Type = Base->getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2985 | return Result; |
| 2986 | } |
| 2987 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2988 | DeclarationName InitializedEntity::getName() const { |
| 2989 | switch (getKind()) { |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2990 | case EK_Parameter: |
| 2991 | case EK_Parameter_CF_Audited: { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2992 | ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2993 | return (D ? D->getDeclName() : DeclarationName()); |
| 2994 | } |
Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 2995 | |
| 2996 | case EK_Variable: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2997 | case EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2998 | case EK_Binding: |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 2999 | return Variable.VariableOrMember->getDeclName(); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3000 | |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 3001 | case EK_LambdaCapture: |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 3002 | return DeclarationName(Capture.VarID); |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 3003 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3004 | case EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 3005 | case EK_StmtExprResult: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3006 | case EK_Exception: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3007 | case EK_New: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3008 | case EK_Temporary: |
| 3009 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3010 | case EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3011 | case EK_ArrayElement: |
| 3012 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3013 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3014 | case EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3015 | case EK_LambdaToBlockConversionBlockElement: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 3016 | case EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 3017 | case EK_RelatedResult: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3018 | return DeclarationName(); |
| 3019 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3020 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3021 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3022 | } |
| 3023 | |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3024 | ValueDecl *InitializedEntity::getDecl() const { |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3025 | switch (getKind()) { |
| 3026 | case EK_Variable: |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3027 | case EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3028 | case EK_Binding: |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3029 | return Variable.VariableOrMember; |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3030 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3031 | case EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3032 | case EK_Parameter_CF_Audited: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3033 | return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 3034 | |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3035 | case EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 3036 | case EK_StmtExprResult: |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3037 | case EK_Exception: |
| 3038 | case EK_New: |
| 3039 | case EK_Temporary: |
| 3040 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3041 | case EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3042 | case EK_ArrayElement: |
| 3043 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3044 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3045 | case EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3046 | case EK_LambdaToBlockConversionBlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 3047 | case EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 3048 | case EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 3049 | case EK_RelatedResult: |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3050 | return nullptr; |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3051 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3052 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3053 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3054 | } |
| 3055 | |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3056 | bool InitializedEntity::allowsNRVO() const { |
| 3057 | switch (getKind()) { |
| 3058 | case EK_Result: |
| 3059 | case EK_Exception: |
| 3060 | return LocAndNRVO.NRVO; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3061 | |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 3062 | case EK_StmtExprResult: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3063 | case EK_Variable: |
| 3064 | case EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3065 | case EK_Parameter_CF_Audited: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3066 | case EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3067 | case EK_Binding: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3068 | case EK_New: |
| 3069 | case EK_Temporary: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 3070 | case EK_CompoundLiteralInit: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3071 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3072 | case EK_Delegating: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3073 | case EK_ArrayElement: |
| 3074 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3075 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3076 | case EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3077 | case EK_LambdaToBlockConversionBlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 3078 | case EK_LambdaCapture: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 3079 | case EK_RelatedResult: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3080 | break; |
| 3081 | } |
| 3082 | |
| 3083 | return false; |
| 3084 | } |
| 3085 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3086 | unsigned InitializedEntity::dumpImpl(raw_ostream &OS) const { |
Richard Smith | e3b28bc | 2013-06-12 21:51:50 +0000 | [diff] [blame] | 3087 | assert(getParent() != this); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3088 | unsigned Depth = getParent() ? getParent()->dumpImpl(OS) : 0; |
| 3089 | for (unsigned I = 0; I != Depth; ++I) |
| 3090 | OS << "`-"; |
| 3091 | |
| 3092 | switch (getKind()) { |
| 3093 | case EK_Variable: OS << "Variable"; break; |
| 3094 | case EK_Parameter: OS << "Parameter"; break; |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3095 | case EK_Parameter_CF_Audited: OS << "CF audited function Parameter"; |
| 3096 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3097 | case EK_Result: OS << "Result"; break; |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 3098 | case EK_StmtExprResult: OS << "StmtExprResult"; break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3099 | case EK_Exception: OS << "Exception"; break; |
| 3100 | case EK_Member: OS << "Member"; break; |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3101 | case EK_Binding: OS << "Binding"; break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3102 | case EK_New: OS << "New"; break; |
| 3103 | case EK_Temporary: OS << "Temporary"; break; |
| 3104 | case EK_CompoundLiteralInit: OS << "CompoundLiteral";break; |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 3105 | case EK_RelatedResult: OS << "RelatedResult"; break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3106 | case EK_Base: OS << "Base"; break; |
| 3107 | case EK_Delegating: OS << "Delegating"; break; |
| 3108 | case EK_ArrayElement: OS << "ArrayElement " << Index; break; |
| 3109 | case EK_VectorElement: OS << "VectorElement " << Index; break; |
| 3110 | case EK_ComplexElement: OS << "ComplexElement " << Index; break; |
| 3111 | case EK_BlockElement: OS << "Block"; break; |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3112 | case EK_LambdaToBlockConversionBlockElement: |
| 3113 | OS << "Block (lambda)"; |
| 3114 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3115 | case EK_LambdaCapture: |
| 3116 | OS << "LambdaCapture "; |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 3117 | OS << DeclarationName(Capture.VarID); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3118 | break; |
| 3119 | } |
| 3120 | |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3121 | if (auto *D = getDecl()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3122 | OS << " "; |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3123 | D->printQualifiedName(OS); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3124 | } |
| 3125 | |
| 3126 | OS << " '" << getType().getAsString() << "'\n"; |
| 3127 | |
| 3128 | return Depth + 1; |
| 3129 | } |
| 3130 | |
Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 3131 | LLVM_DUMP_METHOD void InitializedEntity::dump() const { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3132 | dumpImpl(llvm::errs()); |
| 3133 | } |
| 3134 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3135 | //===----------------------------------------------------------------------===// |
| 3136 | // Initialization sequence |
| 3137 | //===----------------------------------------------------------------------===// |
| 3138 | |
| 3139 | void InitializationSequence::Step::Destroy() { |
| 3140 | switch (Kind) { |
| 3141 | case SK_ResolveAddressOfOverloadedFunction: |
| 3142 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3143 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3144 | case SK_CastDerivedToBaseLValue: |
| 3145 | case SK_BindReference: |
| 3146 | case SK_BindReferenceToTemporary: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 3147 | case SK_FinalCopy: |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3148 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3149 | case SK_UserConversion: |
| 3150 | case SK_QualificationConversionRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3151 | case SK_QualificationConversionXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3152 | case SK_QualificationConversionLValue: |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 3153 | case SK_AtomicConversion: |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3154 | case SK_LValueToRValue: |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3155 | case SK_ListInitialization: |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3156 | case SK_UnwrapInitList: |
| 3157 | case SK_RewrapInitList: |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3158 | case SK_ConstructorInitialization: |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 3159 | case SK_ConstructorInitializationFromList: |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3160 | case SK_ZeroInitialization: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3161 | case SK_CAssignment: |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3162 | case SK_StringInit: |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3163 | case SK_ObjCObjectConversion: |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3164 | case SK_ArrayLoopIndex: |
| 3165 | case SK_ArrayLoopInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3166 | case SK_ArrayInit: |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 3167 | case SK_GNUArrayInit: |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 3168 | case SK_ParenthesizedArrayInit: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3169 | case SK_PassByIndirectCopyRestore: |
| 3170 | case SK_PassByIndirectRestore: |
| 3171 | case SK_ProduceObjCObject: |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 3172 | case SK_StdInitializerList: |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3173 | case SK_StdInitializerListConstructorCall: |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 3174 | case SK_OCLSamplerInit: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 3175 | case SK_OCLZeroEvent: |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 3176 | case SK_OCLZeroQueue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3177 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3178 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3179 | case SK_ConversionSequence: |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 3180 | case SK_ConversionSequenceNoNarrowing: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3181 | delete ICS; |
| 3182 | } |
| 3183 | } |
| 3184 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3185 | bool InitializationSequence::isDirectReferenceBinding() const { |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 3186 | // There can be some lvalue adjustments after the SK_BindReference step. |
| 3187 | for (auto I = Steps.rbegin(); I != Steps.rend(); ++I) { |
| 3188 | if (I->Kind == SK_BindReference) |
| 3189 | return true; |
| 3190 | if (I->Kind == SK_BindReferenceToTemporary) |
| 3191 | return false; |
| 3192 | } |
| 3193 | return false; |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3194 | } |
| 3195 | |
| 3196 | bool InitializationSequence::isAmbiguous() const { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 3197 | if (!Failed()) |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3198 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3199 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3200 | switch (getFailureKind()) { |
| 3201 | case FK_TooManyInitsForReference: |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 3202 | case FK_ParenthesizedListInitForReference: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3203 | case FK_ArrayNeedsInitList: |
| 3204 | case FK_ArrayNeedsInitListOrStringLiteral: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 3205 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 3206 | case FK_NarrowStringIntoWideCharArray: |
| 3207 | case FK_WideStringIntoCharArray: |
| 3208 | case FK_IncompatWideStringIntoWideChar: |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 3209 | case FK_PlainStringIntoUTF8Char: |
| 3210 | case FK_UTF8StringIntoPlainChar: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3211 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 3212 | case FK_NonConstLValueReferenceBindingToTemporary: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 3213 | case FK_NonConstLValueReferenceBindingToBitfield: |
| 3214 | case FK_NonConstLValueReferenceBindingToVectorElement: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3215 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 3216 | case FK_RValueReferenceBindingToLValue: |
| 3217 | case FK_ReferenceInitDropsQualifiers: |
| 3218 | case FK_ReferenceInitFailed: |
| 3219 | case FK_ConversionFailed: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3220 | case FK_ConversionFromPropertyFailed: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3221 | case FK_TooManyInitsForScalar: |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 3222 | case FK_ParenthesizedListInitForScalar: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3223 | case FK_ReferenceBindingToInitList: |
| 3224 | case FK_InitListBadDestinationType: |
| 3225 | case FK_DefaultInitOfConst: |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 3226 | case FK_Incomplete: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3227 | case FK_ArrayTypeMismatch: |
| 3228 | case FK_NonConstantArrayInit: |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 3229 | case FK_ListInitializationFailed: |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 3230 | case FK_VariableLengthArrayHasInitializer: |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3231 | case FK_PlaceholderType: |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3232 | case FK_ExplicitConstructor: |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 3233 | case FK_AddressOfUnaddressableFunction: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3234 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3235 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3236 | case FK_ReferenceInitOverloadFailed: |
| 3237 | case FK_UserConversionOverloadFailed: |
| 3238 | case FK_ConstructorOverloadFailed: |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3239 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3240 | return FailedOverloadResult == OR_Ambiguous; |
| 3241 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3242 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3243 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3244 | } |
| 3245 | |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 3246 | bool InitializationSequence::isConstructorInitialization() const { |
| 3247 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 3248 | } |
| 3249 | |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3250 | void |
| 3251 | InitializationSequence |
| 3252 | ::AddAddressOverloadResolutionStep(FunctionDecl *Function, |
| 3253 | DeclAccessPair Found, |
| 3254 | bool HadMultipleCandidates) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3255 | Step S; |
| 3256 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 3257 | S.Type = Function->getType(); |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3258 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3259 | S.Function.Function = Function; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3260 | S.Function.FoundDecl = Found; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3261 | Steps.push_back(S); |
| 3262 | } |
| 3263 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3264 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3265 | ExprValueKind VK) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3266 | Step S; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3267 | switch (VK) { |
| 3268 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 3269 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 3270 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3271 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3272 | S.Type = BaseType; |
| 3273 | Steps.push_back(S); |
| 3274 | } |
| 3275 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3276 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3277 | bool BindingTemporary) { |
| 3278 | Step S; |
| 3279 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 3280 | S.Type = T; |
| 3281 | Steps.push_back(S); |
| 3282 | } |
| 3283 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 3284 | void InitializationSequence::AddFinalCopy(QualType T) { |
| 3285 | Step S; |
| 3286 | S.Kind = SK_FinalCopy; |
| 3287 | S.Type = T; |
| 3288 | Steps.push_back(S); |
| 3289 | } |
| 3290 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3291 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 3292 | Step S; |
| 3293 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 3294 | S.Type = T; |
| 3295 | Steps.push_back(S); |
| 3296 | } |
| 3297 | |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3298 | void |
| 3299 | InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
| 3300 | DeclAccessPair FoundDecl, |
| 3301 | QualType T, |
| 3302 | bool HadMultipleCandidates) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3303 | Step S; |
| 3304 | S.Kind = SK_UserConversion; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3305 | S.Type = T; |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3306 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3307 | S.Function.Function = Function; |
| 3308 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3309 | Steps.push_back(S); |
| 3310 | } |
| 3311 | |
| 3312 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3313 | ExprValueKind VK) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3314 | Step S; |
John McCall | 7a1da89 | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 3315 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3316 | switch (VK) { |
| 3317 | case VK_RValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3318 | S.Kind = SK_QualificationConversionRValue; |
| 3319 | break; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3320 | case VK_XValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3321 | S.Kind = SK_QualificationConversionXValue; |
| 3322 | break; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3323 | case VK_LValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3324 | S.Kind = SK_QualificationConversionLValue; |
| 3325 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3326 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3327 | S.Type = Ty; |
| 3328 | Steps.push_back(S); |
| 3329 | } |
| 3330 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 3331 | void InitializationSequence::AddAtomicConversionStep(QualType Ty) { |
| 3332 | Step S; |
| 3333 | S.Kind = SK_AtomicConversion; |
| 3334 | S.Type = Ty; |
| 3335 | Steps.push_back(S); |
| 3336 | } |
| 3337 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3338 | void InitializationSequence::AddLValueToRValueStep(QualType Ty) { |
| 3339 | assert(!Ty.hasQualifiers() && "rvalues may not have qualifiers"); |
| 3340 | |
| 3341 | Step S; |
| 3342 | S.Kind = SK_LValueToRValue; |
| 3343 | S.Type = Ty; |
| 3344 | Steps.push_back(S); |
| 3345 | } |
| 3346 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3347 | void InitializationSequence::AddConversionSequenceStep( |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 3348 | const ImplicitConversionSequence &ICS, QualType T, |
| 3349 | bool TopLevelOfInitList) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3350 | Step S; |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 3351 | S.Kind = TopLevelOfInitList ? SK_ConversionSequenceNoNarrowing |
| 3352 | : SK_ConversionSequence; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3353 | S.Type = T; |
| 3354 | S.ICS = new ImplicitConversionSequence(ICS); |
| 3355 | Steps.push_back(S); |
| 3356 | } |
| 3357 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3358 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 3359 | Step S; |
| 3360 | S.Kind = SK_ListInitialization; |
| 3361 | S.Type = T; |
| 3362 | Steps.push_back(S); |
| 3363 | } |
| 3364 | |
Richard Smith | 55c2888 | 2016-05-12 23:45:49 +0000 | [diff] [blame] | 3365 | void InitializationSequence::AddConstructorInitializationStep( |
| 3366 | DeclAccessPair FoundDecl, CXXConstructorDecl *Constructor, QualType T, |
| 3367 | bool HadMultipleCandidates, bool FromInitList, bool AsInitList) { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3368 | Step S; |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3369 | S.Kind = FromInitList ? AsInitList ? SK_StdInitializerListConstructorCall |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 3370 | : SK_ConstructorInitializationFromList |
| 3371 | : SK_ConstructorInitialization; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3372 | S.Type = T; |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3373 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3374 | S.Function.Function = Constructor; |
Richard Smith | 55c2888 | 2016-05-12 23:45:49 +0000 | [diff] [blame] | 3375 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3376 | Steps.push_back(S); |
| 3377 | } |
| 3378 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3379 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 3380 | Step S; |
| 3381 | S.Kind = SK_ZeroInitialization; |
| 3382 | S.Type = T; |
| 3383 | Steps.push_back(S); |
| 3384 | } |
| 3385 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3386 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 3387 | Step S; |
| 3388 | S.Kind = SK_CAssignment; |
| 3389 | S.Type = T; |
| 3390 | Steps.push_back(S); |
| 3391 | } |
| 3392 | |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3393 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 3394 | Step S; |
| 3395 | S.Kind = SK_StringInit; |
| 3396 | S.Type = T; |
| 3397 | Steps.push_back(S); |
| 3398 | } |
| 3399 | |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3400 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 3401 | Step S; |
| 3402 | S.Kind = SK_ObjCObjectConversion; |
| 3403 | S.Type = T; |
| 3404 | Steps.push_back(S); |
| 3405 | } |
| 3406 | |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 3407 | void InitializationSequence::AddArrayInitStep(QualType T, bool IsGNUExtension) { |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3408 | Step S; |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 3409 | S.Kind = IsGNUExtension ? SK_GNUArrayInit : SK_ArrayInit; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3410 | S.Type = T; |
| 3411 | Steps.push_back(S); |
| 3412 | } |
| 3413 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3414 | void InitializationSequence::AddArrayInitLoopStep(QualType T, QualType EltT) { |
| 3415 | Step S; |
| 3416 | S.Kind = SK_ArrayLoopIndex; |
| 3417 | S.Type = EltT; |
| 3418 | Steps.insert(Steps.begin(), S); |
| 3419 | |
| 3420 | S.Kind = SK_ArrayLoopInit; |
| 3421 | S.Type = T; |
| 3422 | Steps.push_back(S); |
| 3423 | } |
| 3424 | |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 3425 | void InitializationSequence::AddParenthesizedArrayInitStep(QualType T) { |
| 3426 | Step S; |
| 3427 | S.Kind = SK_ParenthesizedArrayInit; |
| 3428 | S.Type = T; |
| 3429 | Steps.push_back(S); |
| 3430 | } |
| 3431 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3432 | void InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type, |
| 3433 | bool shouldCopy) { |
| 3434 | Step s; |
| 3435 | s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore |
| 3436 | : SK_PassByIndirectRestore); |
| 3437 | s.Type = type; |
| 3438 | Steps.push_back(s); |
| 3439 | } |
| 3440 | |
| 3441 | void InitializationSequence::AddProduceObjCObjectStep(QualType T) { |
| 3442 | Step S; |
| 3443 | S.Kind = SK_ProduceObjCObject; |
| 3444 | S.Type = T; |
| 3445 | Steps.push_back(S); |
| 3446 | } |
| 3447 | |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 3448 | void InitializationSequence::AddStdInitializerListConstructionStep(QualType T) { |
| 3449 | Step S; |
| 3450 | S.Kind = SK_StdInitializerList; |
| 3451 | S.Type = T; |
| 3452 | Steps.push_back(S); |
| 3453 | } |
| 3454 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 3455 | void InitializationSequence::AddOCLSamplerInitStep(QualType T) { |
| 3456 | Step S; |
| 3457 | S.Kind = SK_OCLSamplerInit; |
| 3458 | S.Type = T; |
| 3459 | Steps.push_back(S); |
| 3460 | } |
| 3461 | |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 3462 | void InitializationSequence::AddOCLZeroEventStep(QualType T) { |
| 3463 | Step S; |
| 3464 | S.Kind = SK_OCLZeroEvent; |
| 3465 | S.Type = T; |
| 3466 | Steps.push_back(S); |
| 3467 | } |
| 3468 | |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 3469 | void InitializationSequence::AddOCLZeroQueueStep(QualType T) { |
| 3470 | Step S; |
| 3471 | S.Kind = SK_OCLZeroQueue; |
| 3472 | S.Type = T; |
| 3473 | Steps.push_back(S); |
| 3474 | } |
| 3475 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3476 | void InitializationSequence::RewrapReferenceInitList(QualType T, |
| 3477 | InitListExpr *Syntactic) { |
| 3478 | assert(Syntactic->getNumInits() == 1 && |
| 3479 | "Can only rewrap trivial init lists."); |
| 3480 | Step S; |
| 3481 | S.Kind = SK_UnwrapInitList; |
| 3482 | S.Type = Syntactic->getInit(0)->getType(); |
| 3483 | Steps.insert(Steps.begin(), S); |
| 3484 | |
| 3485 | S.Kind = SK_RewrapInitList; |
| 3486 | S.Type = T; |
| 3487 | S.WrappingSyntacticList = Syntactic; |
| 3488 | Steps.push_back(S); |
| 3489 | } |
| 3490 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3491 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3492 | OverloadingResult Result) { |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 3493 | setSequenceKind(FailedSequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3494 | this->Failure = Failure; |
| 3495 | this->FailedOverloadResult = Result; |
| 3496 | } |
| 3497 | |
| 3498 | //===----------------------------------------------------------------------===// |
| 3499 | // Attempt initialization |
| 3500 | //===----------------------------------------------------------------------===// |
| 3501 | |
Nico Weber | 337d5aa | 2015-04-17 08:32:38 +0000 | [diff] [blame] | 3502 | /// Tries to add a zero initializer. Returns true if that worked. |
| 3503 | static bool |
| 3504 | maybeRecoverWithZeroInitialization(Sema &S, InitializationSequence &Sequence, |
| 3505 | const InitializedEntity &Entity) { |
| 3506 | if (Entity.getKind() != InitializedEntity::EK_Variable) |
| 3507 | return false; |
| 3508 | |
| 3509 | VarDecl *VD = cast<VarDecl>(Entity.getDecl()); |
| 3510 | if (VD->getInit() || VD->getLocEnd().isMacroID()) |
| 3511 | return false; |
| 3512 | |
| 3513 | QualType VariableTy = VD->getType().getCanonicalType(); |
| 3514 | SourceLocation Loc = S.getLocForEndOfToken(VD->getLocEnd()); |
| 3515 | std::string Init = S.getFixItZeroInitializerForType(VariableTy, Loc); |
| 3516 | if (!Init.empty()) { |
| 3517 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3518 | Sequence.SetZeroInitializationFixit(Init, Loc); |
| 3519 | return true; |
| 3520 | } |
| 3521 | return false; |
| 3522 | } |
| 3523 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3524 | static void MaybeProduceObjCObject(Sema &S, |
| 3525 | InitializationSequence &Sequence, |
| 3526 | const InitializedEntity &Entity) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3527 | if (!S.getLangOpts().ObjCAutoRefCount) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3528 | |
| 3529 | /// When initializing a parameter, produce the value if it's marked |
| 3530 | /// __attribute__((ns_consumed)). |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3531 | if (Entity.isParameterKind()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3532 | if (!Entity.isParameterConsumed()) |
| 3533 | return; |
| 3534 | |
| 3535 | assert(Entity.getType()->isObjCRetainableType() && |
| 3536 | "consuming an object of unretainable type?"); |
| 3537 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 3538 | |
| 3539 | /// When initializing a return value, if the return type is a |
| 3540 | /// retainable type, then returns need to immediately retain the |
| 3541 | /// object. If an autorelease is required, it will be done at the |
| 3542 | /// last instant. |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 3543 | } else if (Entity.getKind() == InitializedEntity::EK_Result || |
| 3544 | Entity.getKind() == InitializedEntity::EK_StmtExprResult) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3545 | if (!Entity.getType()->isObjCRetainableType()) |
| 3546 | return; |
| 3547 | |
| 3548 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 3549 | } |
| 3550 | } |
| 3551 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3552 | static void TryListInitialization(Sema &S, |
| 3553 | const InitializedEntity &Entity, |
| 3554 | const InitializationKind &Kind, |
| 3555 | InitListExpr *InitList, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 3556 | InitializationSequence &Sequence, |
| 3557 | bool TreatUnavailableAsInvalid); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3558 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3559 | /// When initializing from init list via constructor, handle |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3560 | /// initialization of an object of type std::initializer_list<T>. |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3561 | /// |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3562 | /// \return true if we have handled initialization of an object of type |
| 3563 | /// std::initializer_list<T>, false otherwise. |
| 3564 | static bool TryInitializerListConstruction(Sema &S, |
| 3565 | InitListExpr *List, |
| 3566 | QualType DestType, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 3567 | InitializationSequence &Sequence, |
| 3568 | bool TreatUnavailableAsInvalid) { |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3569 | QualType E; |
| 3570 | if (!S.isStdInitializerList(DestType, &E)) |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3571 | return false; |
| 3572 | |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3573 | if (!S.isCompleteType(List->getExprLoc(), E)) { |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3574 | Sequence.setIncompleteTypeFailure(E); |
| 3575 | return true; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3576 | } |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3577 | |
| 3578 | // Try initializing a temporary array from the init list. |
| 3579 | QualType ArrayType = S.Context.getConstantArrayType( |
| 3580 | E.withConst(), llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 3581 | List->getNumInits()), |
| 3582 | clang::ArrayType::Normal, 0); |
| 3583 | InitializedEntity HiddenArray = |
| 3584 | InitializedEntity::InitializeTemporary(ArrayType); |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 3585 | InitializationKind Kind = InitializationKind::CreateDirectList( |
| 3586 | List->getExprLoc(), List->getLocStart(), List->getLocEnd()); |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 3587 | TryListInitialization(S, HiddenArray, Kind, List, Sequence, |
| 3588 | TreatUnavailableAsInvalid); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3589 | if (Sequence) |
| 3590 | Sequence.AddStdInitializerListConstructionStep(DestType); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3591 | return true; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3592 | } |
| 3593 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3594 | /// Determine if the constructor has the signature of a copy or move |
| 3595 | /// constructor for the type T of the class in which it was found. That is, |
| 3596 | /// determine if its first parameter is of type T or reference to (possibly |
| 3597 | /// cv-qualified) T. |
| 3598 | static bool hasCopyOrMoveCtorParam(ASTContext &Ctx, |
| 3599 | const ConstructorInfo &Info) { |
| 3600 | if (Info.Constructor->getNumParams() == 0) |
| 3601 | return false; |
| 3602 | |
| 3603 | QualType ParmT = |
| 3604 | Info.Constructor->getParamDecl(0)->getType().getNonReferenceType(); |
| 3605 | QualType ClassT = |
| 3606 | Ctx.getRecordType(cast<CXXRecordDecl>(Info.FoundDecl->getDeclContext())); |
| 3607 | |
| 3608 | return Ctx.hasSameUnqualifiedType(ParmT, ClassT); |
| 3609 | } |
| 3610 | |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3611 | static OverloadingResult |
| 3612 | ResolveConstructorOverload(Sema &S, SourceLocation DeclLoc, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3613 | MultiExprArg Args, |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3614 | OverloadCandidateSet &CandidateSet, |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3615 | QualType DestType, |
Richard Smith | 40c7806 | 2015-02-21 02:31:57 +0000 | [diff] [blame] | 3616 | DeclContext::lookup_result Ctors, |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3617 | OverloadCandidateSet::iterator &Best, |
| 3618 | bool CopyInitializing, bool AllowExplicit, |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3619 | bool OnlyListConstructors, bool IsListInit, |
| 3620 | bool SecondStepOfCopyInit = false) { |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3621 | CandidateSet.clear(OverloadCandidateSet::CSK_InitByConstructor); |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3622 | |
Richard Smith | 40c7806 | 2015-02-21 02:31:57 +0000 | [diff] [blame] | 3623 | for (NamedDecl *D : Ctors) { |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3624 | auto Info = getConstructorInfo(D); |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3625 | if (!Info.Constructor || Info.Constructor->isInvalidDecl()) |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3626 | continue; |
| 3627 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3628 | if (!AllowExplicit && Info.Constructor->isExplicit()) |
| 3629 | continue; |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3630 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3631 | if (OnlyListConstructors && !S.isInitListConstructor(Info.Constructor)) |
| 3632 | continue; |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3633 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3634 | // C++11 [over.best.ics]p4: |
| 3635 | // ... and the constructor or user-defined conversion function is a |
| 3636 | // candidate by |
| 3637 | // - 13.3.1.3, when the argument is the temporary in the second step |
| 3638 | // of a class copy-initialization, or |
| 3639 | // - 13.3.1.4, 13.3.1.5, or 13.3.1.6 (in all cases), [not handled here] |
| 3640 | // - the second phase of 13.3.1.7 when the initializer list has exactly |
| 3641 | // one element that is itself an initializer list, and the target is |
| 3642 | // the first parameter of a constructor of class X, and the conversion |
| 3643 | // is to X or reference to (possibly cv-qualified X), |
| 3644 | // user-defined conversion sequences are not considered. |
| 3645 | bool SuppressUserConversions = |
| 3646 | SecondStepOfCopyInit || |
| 3647 | (IsListInit && Args.size() == 1 && isa<InitListExpr>(Args[0]) && |
| 3648 | hasCopyOrMoveCtorParam(S.Context, Info)); |
| 3649 | |
| 3650 | if (Info.ConstructorTmpl) |
| 3651 | S.AddTemplateOverloadCandidate(Info.ConstructorTmpl, Info.FoundDecl, |
| 3652 | /*ExplicitArgs*/ nullptr, Args, |
| 3653 | CandidateSet, SuppressUserConversions); |
| 3654 | else { |
| 3655 | // C++ [over.match.copy]p1: |
| 3656 | // - When initializing a temporary to be bound to the first parameter |
| 3657 | // of a constructor [for type T] that takes a reference to possibly |
| 3658 | // cv-qualified T as its first argument, called with a single |
| 3659 | // argument in the context of direct-initialization, explicit |
| 3660 | // conversion functions are also considered. |
| 3661 | // FIXME: What if a constructor template instantiates to such a signature? |
| 3662 | bool AllowExplicitConv = AllowExplicit && !CopyInitializing && |
| 3663 | Args.size() == 1 && |
| 3664 | hasCopyOrMoveCtorParam(S.Context, Info); |
| 3665 | S.AddOverloadCandidate(Info.Constructor, Info.FoundDecl, Args, |
| 3666 | CandidateSet, SuppressUserConversions, |
| 3667 | /*PartialOverloading=*/false, |
| 3668 | /*AllowExplicit=*/AllowExplicitConv); |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3669 | } |
| 3670 | } |
| 3671 | |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3672 | // FIXME: Work around a bug in C++17 guaranteed copy elision. |
| 3673 | // |
| 3674 | // When initializing an object of class type T by constructor |
| 3675 | // ([over.match.ctor]) or by list-initialization ([over.match.list]) |
| 3676 | // from a single expression of class type U, conversion functions of |
| 3677 | // U that convert to the non-reference type cv T are candidates. |
| 3678 | // Explicit conversion functions are only candidates during |
| 3679 | // direct-initialization. |
| 3680 | // |
| 3681 | // Note: SecondStepOfCopyInit is only ever true in this case when |
| 3682 | // evaluating whether to produce a C++98 compatibility warning. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 3683 | if (S.getLangOpts().CPlusPlus17 && Args.size() == 1 && |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3684 | !SecondStepOfCopyInit) { |
| 3685 | Expr *Initializer = Args[0]; |
| 3686 | auto *SourceRD = Initializer->getType()->getAsCXXRecordDecl(); |
| 3687 | if (SourceRD && S.isCompleteType(DeclLoc, Initializer->getType())) { |
| 3688 | const auto &Conversions = SourceRD->getVisibleConversionFunctions(); |
| 3689 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
| 3690 | NamedDecl *D = *I; |
| 3691 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3692 | D = D->getUnderlyingDecl(); |
| 3693 | |
| 3694 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3695 | CXXConversionDecl *Conv; |
| 3696 | if (ConvTemplate) |
| 3697 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3698 | else |
| 3699 | Conv = cast<CXXConversionDecl>(D); |
| 3700 | |
| 3701 | if ((AllowExplicit && !CopyInitializing) || !Conv->isExplicit()) { |
| 3702 | if (ConvTemplate) |
| 3703 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
| 3704 | ActingDC, Initializer, DestType, |
| 3705 | CandidateSet, AllowExplicit, |
| 3706 | /*AllowResultConversion*/false); |
| 3707 | else |
| 3708 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Initializer, |
| 3709 | DestType, CandidateSet, AllowExplicit, |
| 3710 | /*AllowResultConversion*/false); |
| 3711 | } |
| 3712 | } |
| 3713 | } |
| 3714 | } |
| 3715 | |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3716 | // Perform overload resolution and return the result. |
| 3717 | return CandidateSet.BestViableFunction(S, DeclLoc, Best); |
| 3718 | } |
| 3719 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3720 | /// Attempt initialization by constructor (C++ [dcl.init]), which |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3721 | /// enumerates the constructors of the initialized entity and performs overload |
| 3722 | /// resolution to select the best. |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3723 | /// \param DestType The destination class type. |
| 3724 | /// \param DestArrayType The destination type, which is either DestType or |
| 3725 | /// a (possibly multidimensional) array of DestType. |
NAKAMURA Takumi | ffcc98a | 2015-02-05 23:12:13 +0000 | [diff] [blame] | 3726 | /// \param IsListInit Is this list-initialization? |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3727 | /// \param IsInitListCopy Is this non-list-initialization resulting from a |
| 3728 | /// list-initialization from {x} where x is the same |
| 3729 | /// type as the entity? |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3730 | static void TryConstructorInitialization(Sema &S, |
| 3731 | const InitializedEntity &Entity, |
| 3732 | const InitializationKind &Kind, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3733 | MultiExprArg Args, QualType DestType, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3734 | QualType DestArrayType, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3735 | InitializationSequence &Sequence, |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3736 | bool IsListInit = false, |
| 3737 | bool IsInitListCopy = false) { |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3738 | assert(((!IsListInit && !IsInitListCopy) || |
| 3739 | (Args.size() == 1 && isa<InitListExpr>(Args[0]))) && |
| 3740 | "IsListInit/IsInitListCopy must come with a single initializer list " |
| 3741 | "argument."); |
| 3742 | InitListExpr *ILE = |
| 3743 | (IsListInit || IsInitListCopy) ? cast<InitListExpr>(Args[0]) : nullptr; |
| 3744 | MultiExprArg UnwrappedArgs = |
| 3745 | ILE ? MultiExprArg(ILE->getInits(), ILE->getNumInits()) : Args; |
Sebastian Redl | 88e4d49 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3746 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3747 | // The type we're constructing needs to be complete. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3748 | if (!S.isCompleteType(Kind.getLocation(), DestType)) { |
Douglas Gregor | 85f3423 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 3749 | Sequence.setIncompleteTypeFailure(DestType); |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3750 | return; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3751 | } |
| 3752 | |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 3753 | // C++17 [dcl.init]p17: |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3754 | // - If the initializer expression is a prvalue and the cv-unqualified |
| 3755 | // version of the source type is the same class as the class of the |
| 3756 | // destination, the initializer expression is used to initialize the |
| 3757 | // destination object. |
| 3758 | // Per DR (no number yet), this does not apply when initializing a base |
| 3759 | // class or delegating to another constructor from a mem-initializer. |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3760 | // ObjC++: Lambda captured by the block in the lambda to block conversion |
| 3761 | // should avoid copy elision. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 3762 | if (S.getLangOpts().CPlusPlus17 && |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3763 | Entity.getKind() != InitializedEntity::EK_Base && |
| 3764 | Entity.getKind() != InitializedEntity::EK_Delegating && |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3765 | Entity.getKind() != |
| 3766 | InitializedEntity::EK_LambdaToBlockConversionBlockElement && |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3767 | UnwrappedArgs.size() == 1 && UnwrappedArgs[0]->isRValue() && |
| 3768 | S.Context.hasSameUnqualifiedType(UnwrappedArgs[0]->getType(), DestType)) { |
| 3769 | // Convert qualifications if necessary. |
Richard Smith | 16d3150 | 2016-12-21 01:31:56 +0000 | [diff] [blame] | 3770 | Sequence.AddQualificationConversionStep(DestType, VK_RValue); |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3771 | if (ILE) |
| 3772 | Sequence.RewrapReferenceInitList(DestType, ILE); |
| 3773 | return; |
| 3774 | } |
| 3775 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3776 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
| 3777 | assert(DestRecordType && "Constructor initialization requires record type"); |
| 3778 | CXXRecordDecl *DestRecordDecl |
| 3779 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
| 3780 | |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3781 | // Build the candidate set directly in the initialization sequence |
| 3782 | // structure, so that it will persist if we fail. |
| 3783 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3784 | |
| 3785 | // Determine whether we are allowed to call explicit constructors or |
| 3786 | // explicit conversion operators. |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3787 | bool AllowExplicit = Kind.AllowExplicit() || IsListInit; |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3788 | bool CopyInitialization = Kind.getKind() == InitializationKind::IK_Copy; |
Sebastian Redl | 88e4d49 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3789 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3790 | // - Otherwise, if T is a class type, constructors are considered. The |
| 3791 | // applicable constructors are enumerated, and the best one is chosen |
| 3792 | // through overload resolution. |
Richard Smith | 40c7806 | 2015-02-21 02:31:57 +0000 | [diff] [blame] | 3793 | DeclContext::lookup_result Ctors = S.LookupConstructors(DestRecordDecl); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3794 | |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3795 | OverloadingResult Result = OR_No_Viable_Function; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3796 | OverloadCandidateSet::iterator Best; |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3797 | bool AsInitializerList = false; |
| 3798 | |
Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 3799 | // C++11 [over.match.list]p1, per DR1467: |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 3800 | // When objects of non-aggregate type T are list-initialized, such that |
| 3801 | // 8.5.4 [dcl.init.list] specifies that overload resolution is performed |
| 3802 | // according to the rules in this section, overload resolution selects |
| 3803 | // the constructor in two phases: |
| 3804 | // |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3805 | // - Initially, the candidate functions are the initializer-list |
| 3806 | // constructors of the class T and the argument list consists of the |
| 3807 | // initializer list as a single argument. |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3808 | if (IsListInit) { |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3809 | AsInitializerList = true; |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3810 | |
| 3811 | // If the initializer list has no elements and T has a default constructor, |
| 3812 | // the first phase is omitted. |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3813 | if (!(UnwrappedArgs.empty() && DestRecordDecl->hasDefaultConstructor())) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3814 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3815 | CandidateSet, DestType, Ctors, Best, |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3816 | CopyInitialization, AllowExplicit, |
Larisse Voufo | bcf327a | 2015-02-10 02:20:14 +0000 | [diff] [blame] | 3817 | /*OnlyListConstructor=*/true, |
| 3818 | IsListInit); |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3819 | } |
| 3820 | |
| 3821 | // C++11 [over.match.list]p1: |
| 3822 | // - If no viable initializer-list constructor is found, overload resolution |
| 3823 | // is performed again, where the candidate functions are all the |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3824 | // constructors of the class T and the argument list consists of the |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3825 | // elements of the initializer list. |
| 3826 | if (Result == OR_No_Viable_Function) { |
| 3827 | AsInitializerList = false; |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3828 | Result = ResolveConstructorOverload(S, Kind.getLocation(), UnwrappedArgs, |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3829 | CandidateSet, DestType, Ctors, Best, |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3830 | CopyInitialization, AllowExplicit, |
Larisse Voufo | bcf327a | 2015-02-10 02:20:14 +0000 | [diff] [blame] | 3831 | /*OnlyListConstructors=*/false, |
| 3832 | IsListInit); |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3833 | } |
| 3834 | if (Result) { |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3835 | Sequence.SetOverloadFailure(IsListInit ? |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3836 | InitializationSequence::FK_ListConstructorOverloadFailed : |
| 3837 | InitializationSequence::FK_ConstructorOverloadFailed, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3838 | Result); |
| 3839 | return; |
| 3840 | } |
| 3841 | |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3842 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 3843 | |
| 3844 | // In C++17, ResolveConstructorOverload can select a conversion function |
| 3845 | // instead of a constructor. |
| 3846 | if (auto *CD = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 3847 | // Add the user-defined conversion step that calls the conversion function. |
| 3848 | QualType ConvType = CD->getConversionType(); |
| 3849 | assert(S.Context.hasSameUnqualifiedType(ConvType, DestType) && |
| 3850 | "should not have selected this conversion function"); |
| 3851 | Sequence.AddUserConversionStep(CD, Best->FoundDecl, ConvType, |
| 3852 | HadMultipleCandidates); |
| 3853 | if (!S.Context.hasSameType(ConvType, DestType)) |
| 3854 | Sequence.AddQualificationConversionStep(DestType, VK_RValue); |
| 3855 | if (IsListInit) |
| 3856 | Sequence.RewrapReferenceInitList(Entity.getType(), ILE); |
| 3857 | return; |
| 3858 | } |
| 3859 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3860 | // C++11 [dcl.init]p6: |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3861 | // If a program calls for the default initialization of an object |
| 3862 | // of a const-qualified type T, T shall be a class type with a |
| 3863 | // user-provided default constructor. |
Nico Weber | 6a6376b | 2016-02-19 01:52:46 +0000 | [diff] [blame] | 3864 | // C++ core issue 253 proposal: |
| 3865 | // If the implicit default constructor initializes all subobjects, no |
| 3866 | // initializer should be required. |
| 3867 | // The 253 proposal is for example needed to process libstdc++ headers in 5.x. |
| 3868 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3869 | if (Kind.getKind() == InitializationKind::IK_Default && |
Nico Weber | 6a6376b | 2016-02-19 01:52:46 +0000 | [diff] [blame] | 3870 | Entity.getType().isConstQualified()) { |
| 3871 | if (!CtorDecl->getParent()->allowConstDefaultInit()) { |
| 3872 | if (!maybeRecoverWithZeroInitialization(S, Sequence, Entity)) |
| 3873 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 3874 | return; |
| 3875 | } |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3876 | } |
| 3877 | |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3878 | // C++11 [over.match.list]p1: |
| 3879 | // In copy-list-initialization, if an explicit constructor is chosen, the |
| 3880 | // initializer is ill-formed. |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3881 | if (IsListInit && !Kind.AllowExplicit() && CtorDecl->isExplicit()) { |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3882 | Sequence.SetFailed(InitializationSequence::FK_ExplicitConstructor); |
| 3883 | return; |
| 3884 | } |
| 3885 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3886 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 3887 | // subsumed by the initialization. |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3888 | Sequence.AddConstructorInitializationStep( |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3889 | Best->FoundDecl, CtorDecl, DestArrayType, HadMultipleCandidates, |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3890 | IsListInit | IsInitListCopy, AsInitializerList); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3891 | } |
| 3892 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3893 | static bool |
| 3894 | ResolveOverloadedFunctionForReferenceBinding(Sema &S, |
| 3895 | Expr *Initializer, |
| 3896 | QualType &SourceType, |
| 3897 | QualType &UnqualifiedSourceType, |
| 3898 | QualType UnqualifiedTargetType, |
| 3899 | InitializationSequence &Sequence) { |
| 3900 | if (S.Context.getCanonicalType(UnqualifiedSourceType) == |
| 3901 | S.Context.OverloadTy) { |
| 3902 | DeclAccessPair Found; |
| 3903 | bool HadMultipleCandidates = false; |
| 3904 | if (FunctionDecl *Fn |
| 3905 | = S.ResolveAddressOfOverloadedFunction(Initializer, |
| 3906 | UnqualifiedTargetType, |
| 3907 | false, Found, |
| 3908 | &HadMultipleCandidates)) { |
| 3909 | Sequence.AddAddressOverloadResolutionStep(Fn, Found, |
| 3910 | HadMultipleCandidates); |
| 3911 | SourceType = Fn->getType(); |
| 3912 | UnqualifiedSourceType = SourceType.getUnqualifiedType(); |
| 3913 | } else if (!UnqualifiedTargetType->isRecordType()) { |
| 3914 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3915 | return true; |
| 3916 | } |
| 3917 | } |
| 3918 | return false; |
| 3919 | } |
| 3920 | |
| 3921 | static void TryReferenceInitializationCore(Sema &S, |
| 3922 | const InitializedEntity &Entity, |
| 3923 | const InitializationKind &Kind, |
| 3924 | Expr *Initializer, |
| 3925 | QualType cv1T1, QualType T1, |
| 3926 | Qualifiers T1Quals, |
| 3927 | QualType cv2T2, QualType T2, |
| 3928 | Qualifiers T2Quals, |
| 3929 | InitializationSequence &Sequence); |
| 3930 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3931 | static void TryValueInitialization(Sema &S, |
| 3932 | const InitializedEntity &Entity, |
| 3933 | const InitializationKind &Kind, |
| 3934 | InitializationSequence &Sequence, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3935 | InitListExpr *InitList = nullptr); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3936 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3937 | /// Attempt list initialization of a reference. |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3938 | static void TryReferenceListInitialization(Sema &S, |
| 3939 | const InitializedEntity &Entity, |
| 3940 | const InitializationKind &Kind, |
| 3941 | InitListExpr *InitList, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 3942 | InitializationSequence &Sequence, |
| 3943 | bool TreatUnavailableAsInvalid) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3944 | // First, catch C++03 where this isn't possible. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3945 | if (!S.getLangOpts().CPlusPlus11) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3946 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 3947 | return; |
| 3948 | } |
David Majnemer | 9370dc2 | 2015-04-26 07:35:03 +0000 | [diff] [blame] | 3949 | // Can't reference initialize a compound literal. |
| 3950 | if (Entity.getKind() == InitializedEntity::EK_CompoundLiteralInit) { |
| 3951 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 3952 | return; |
| 3953 | } |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3954 | |
| 3955 | QualType DestType = Entity.getType(); |
| 3956 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 3957 | Qualifiers T1Quals; |
| 3958 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
| 3959 | |
| 3960 | // Reference initialization via an initializer list works thus: |
| 3961 | // If the initializer list consists of a single element that is |
| 3962 | // reference-related to the referenced type, bind directly to that element |
| 3963 | // (possibly creating temporaries). |
| 3964 | // Otherwise, initialize a temporary with the initializer list and |
| 3965 | // bind to that. |
| 3966 | if (InitList->getNumInits() == 1) { |
| 3967 | Expr *Initializer = InitList->getInit(0); |
| 3968 | QualType cv2T2 = Initializer->getType(); |
| 3969 | Qualifiers T2Quals; |
| 3970 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
| 3971 | |
| 3972 | // If this fails, creating a temporary wouldn't work either. |
| 3973 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 3974 | T1, Sequence)) |
| 3975 | return; |
| 3976 | |
| 3977 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3978 | bool dummy1, dummy2, dummy3; |
| 3979 | Sema::ReferenceCompareResult RefRelationship |
| 3980 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, dummy1, |
| 3981 | dummy2, dummy3); |
| 3982 | if (RefRelationship >= Sema::Ref_Related) { |
| 3983 | // Try to bind the reference here. |
| 3984 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 3985 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 3986 | if (Sequence) |
| 3987 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
| 3988 | return; |
| 3989 | } |
Richard Smith | 03d9393 | 2013-01-15 07:58:29 +0000 | [diff] [blame] | 3990 | |
| 3991 | // Update the initializer if we've resolved an overloaded function. |
| 3992 | if (Sequence.step_begin() != Sequence.step_end()) |
| 3993 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3994 | } |
| 3995 | |
| 3996 | // Not reference-related. Create a temporary and bind to that. |
| 3997 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3998 | |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 3999 | TryListInitialization(S, TempEntity, Kind, InitList, Sequence, |
| 4000 | TreatUnavailableAsInvalid); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4001 | if (Sequence) { |
| 4002 | if (DestType->isRValueReferenceType() || |
| 4003 | (T1Quals.hasConst() && !T1Quals.hasVolatile())) |
| 4004 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 4005 | else |
| 4006 | Sequence.SetFailed( |
| 4007 | InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
| 4008 | } |
| 4009 | } |
| 4010 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4011 | /// Attempt list initialization (C++0x [dcl.init.list]) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4012 | static void TryListInitialization(Sema &S, |
| 4013 | const InitializedEntity &Entity, |
| 4014 | const InitializationKind &Kind, |
| 4015 | InitListExpr *InitList, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4016 | InitializationSequence &Sequence, |
| 4017 | bool TreatUnavailableAsInvalid) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4018 | QualType DestType = Entity.getType(); |
| 4019 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4020 | // C++ doesn't allow scalar initialization with more than one argument. |
| 4021 | // But C99 complex numbers are scalars and it makes sense there. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4022 | if (S.getLangOpts().CPlusPlus && DestType->isScalarType() && |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4023 | !DestType->isAnyComplexType() && InitList->getNumInits() > 1) { |
| 4024 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 4025 | return; |
| 4026 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4027 | if (DestType->isReferenceType()) { |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4028 | TryReferenceListInitialization(S, Entity, Kind, InitList, Sequence, |
| 4029 | TreatUnavailableAsInvalid); |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4030 | return; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4031 | } |
Sebastian Redl | 4f28b58 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 4032 | |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4033 | if (DestType->isRecordType() && |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4034 | !S.isCompleteType(InitList->getLocStart(), DestType)) { |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4035 | Sequence.setIncompleteTypeFailure(DestType); |
| 4036 | return; |
| 4037 | } |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4038 | |
Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 4039 | // C++11 [dcl.init.list]p3, per DR1467: |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4040 | // - If T is a class type and the initializer list has a single element of |
| 4041 | // type cv U, where U is T or a class derived from T, the object is |
| 4042 | // initialized from that element (by copy-initialization for |
| 4043 | // copy-list-initialization, or by direct-initialization for |
| 4044 | // direct-list-initialization). |
| 4045 | // - Otherwise, if T is a character array and the initializer list has a |
| 4046 | // single element that is an appropriately-typed string literal |
| 4047 | // (8.5.2 [dcl.init.string]), initialization is performed as described |
| 4048 | // in that section. |
Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 4049 | // - Otherwise, if T is an aggregate, [...] (continue below). |
| 4050 | if (S.getLangOpts().CPlusPlus11 && InitList->getNumInits() == 1) { |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4051 | if (DestType->isRecordType()) { |
| 4052 | QualType InitType = InitList->getInit(0)->getType(); |
| 4053 | if (S.Context.hasSameUnqualifiedType(InitType, DestType) || |
Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 4054 | S.IsDerivedFrom(InitList->getLocStart(), InitType, DestType)) { |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 4055 | Expr *InitListAsExpr = InitList; |
| 4056 | TryConstructorInitialization(S, Entity, Kind, InitListAsExpr, DestType, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 4057 | DestType, Sequence, |
| 4058 | /*InitListSyntax*/false, |
| 4059 | /*IsInitListCopy*/true); |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4060 | return; |
| 4061 | } |
| 4062 | } |
| 4063 | if (const ArrayType *DestAT = S.Context.getAsArrayType(DestType)) { |
| 4064 | Expr *SubInit[1] = {InitList->getInit(0)}; |
| 4065 | if (!isa<VariableArrayType>(DestAT) && |
| 4066 | IsStringInit(SubInit[0], DestAT, S.Context) == SIF_None) { |
| 4067 | InitializationKind SubKind = |
| 4068 | Kind.getKind() == InitializationKind::IK_DirectList |
| 4069 | ? InitializationKind::CreateDirect(Kind.getLocation(), |
| 4070 | InitList->getLBraceLoc(), |
| 4071 | InitList->getRBraceLoc()) |
| 4072 | : Kind; |
| 4073 | Sequence.InitializeFrom(S, Entity, SubKind, SubInit, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4074 | /*TopLevelOfInitList*/ true, |
| 4075 | TreatUnavailableAsInvalid); |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4076 | |
| 4077 | // TryStringLiteralInitialization() (in InitializeFrom()) will fail if |
| 4078 | // the element is not an appropriately-typed string literal, in which |
| 4079 | // case we should proceed as in C++11 (below). |
| 4080 | if (Sequence) { |
| 4081 | Sequence.RewrapReferenceInitList(Entity.getType(), InitList); |
| 4082 | return; |
| 4083 | } |
| 4084 | } |
Sebastian Redl | 4f28b58 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 4085 | } |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4086 | } |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4087 | |
| 4088 | // C++11 [dcl.init.list]p3: |
| 4089 | // - If T is an aggregate, aggregate initialization is performed. |
Faisal Vali | 30622bb | 2015-12-07 02:37:44 +0000 | [diff] [blame] | 4090 | if ((DestType->isRecordType() && !DestType->isAggregateType()) || |
| 4091 | (S.getLangOpts().CPlusPlus11 && |
| 4092 | S.isStdInitializerList(DestType, nullptr))) { |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4093 | if (S.getLangOpts().CPlusPlus11) { |
| 4094 | // - Otherwise, if the initializer list has no elements and T is a |
| 4095 | // class type with a default constructor, the object is |
| 4096 | // value-initialized. |
| 4097 | if (InitList->getNumInits() == 0) { |
| 4098 | CXXRecordDecl *RD = DestType->getAsCXXRecordDecl(); |
| 4099 | if (RD->hasDefaultConstructor()) { |
| 4100 | TryValueInitialization(S, Entity, Kind, Sequence, InitList); |
| 4101 | return; |
| 4102 | } |
| 4103 | } |
| 4104 | |
| 4105 | // - Otherwise, if T is a specialization of std::initializer_list<E>, |
| 4106 | // an initializer_list object constructed [...] |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4107 | if (TryInitializerListConstruction(S, InitList, DestType, Sequence, |
| 4108 | TreatUnavailableAsInvalid)) |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4109 | return; |
| 4110 | |
| 4111 | // - Otherwise, if T is a class type, constructors are considered. |
| 4112 | Expr *InitListAsExpr = InitList; |
| 4113 | TryConstructorInitialization(S, Entity, Kind, InitListAsExpr, DestType, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 4114 | DestType, Sequence, /*InitListSyntax*/true); |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4115 | } else |
| 4116 | Sequence.SetFailed(InitializationSequence::FK_InitListBadDestinationType); |
| 4117 | return; |
| 4118 | } |
| 4119 | |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 4120 | if (S.getLangOpts().CPlusPlus && !DestType->isAggregateType() && |
Richard Smith | ed63886 | 2016-03-28 06:08:37 +0000 | [diff] [blame] | 4121 | InitList->getNumInits() == 1) { |
| 4122 | Expr *E = InitList->getInit(0); |
| 4123 | |
| 4124 | // - Otherwise, if T is an enumeration with a fixed underlying type, |
| 4125 | // the initializer-list has a single element v, and the initialization |
| 4126 | // is direct-list-initialization, the object is initialized with the |
| 4127 | // value T(v); if a narrowing conversion is required to convert v to |
| 4128 | // the underlying type of T, the program is ill-formed. |
| 4129 | auto *ET = DestType->getAs<EnumType>(); |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 4130 | if (S.getLangOpts().CPlusPlus17 && |
Richard Smith | ed63886 | 2016-03-28 06:08:37 +0000 | [diff] [blame] | 4131 | Kind.getKind() == InitializationKind::IK_DirectList && |
| 4132 | ET && ET->getDecl()->isFixed() && |
| 4133 | !S.Context.hasSameUnqualifiedType(E->getType(), DestType) && |
| 4134 | (E->getType()->isIntegralOrEnumerationType() || |
| 4135 | E->getType()->isFloatingType())) { |
| 4136 | // There are two ways that T(v) can work when T is an enumeration type. |
| 4137 | // If there is either an implicit conversion sequence from v to T or |
| 4138 | // a conversion function that can convert from v to T, then we use that. |
| 4139 | // Otherwise, if v is of integral, enumeration, or floating-point type, |
| 4140 | // it is converted to the enumeration type via its underlying type. |
| 4141 | // There is no overlap possible between these two cases (except when the |
| 4142 | // source value is already of the destination type), and the first |
| 4143 | // case is handled by the general case for single-element lists below. |
| 4144 | ImplicitConversionSequence ICS; |
| 4145 | ICS.setStandard(); |
| 4146 | ICS.Standard.setAsIdentityConversion(); |
Vedant Kumar | f4217f8 | 2017-02-16 01:20:00 +0000 | [diff] [blame] | 4147 | if (!E->isRValue()) |
| 4148 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
Richard Smith | ed63886 | 2016-03-28 06:08:37 +0000 | [diff] [blame] | 4149 | // If E is of a floating-point type, then the conversion is ill-formed |
| 4150 | // due to narrowing, but go through the motions in order to produce the |
| 4151 | // right diagnostic. |
| 4152 | ICS.Standard.Second = E->getType()->isFloatingType() |
| 4153 | ? ICK_Floating_Integral |
| 4154 | : ICK_Integral_Conversion; |
| 4155 | ICS.Standard.setFromType(E->getType()); |
| 4156 | ICS.Standard.setToType(0, E->getType()); |
| 4157 | ICS.Standard.setToType(1, DestType); |
| 4158 | ICS.Standard.setToType(2, DestType); |
| 4159 | Sequence.AddConversionSequenceStep(ICS, ICS.Standard.getToType(2), |
| 4160 | /*TopLevelOfInitList*/true); |
| 4161 | Sequence.RewrapReferenceInitList(Entity.getType(), InitList); |
| 4162 | return; |
| 4163 | } |
| 4164 | |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 4165 | // - Otherwise, if the initializer list has a single element of type E |
| 4166 | // [...references are handled above...], the object or reference is |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4167 | // initialized from that element (by copy-initialization for |
| 4168 | // copy-list-initialization, or by direct-initialization for |
| 4169 | // direct-list-initialization); if a narrowing conversion is required |
| 4170 | // to convert the element to T, the program is ill-formed. |
| 4171 | // |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 4172 | // Per core-24034, this is direct-initialization if we were performing |
| 4173 | // direct-list-initialization and copy-initialization otherwise. |
| 4174 | // We can't use InitListChecker for this, because it always performs |
| 4175 | // copy-initialization. This only matters if we might use an 'explicit' |
| 4176 | // conversion operator, so we only need to handle the cases where the source |
| 4177 | // is of record type. |
Richard Smith | ed63886 | 2016-03-28 06:08:37 +0000 | [diff] [blame] | 4178 | if (InitList->getInit(0)->getType()->isRecordType()) { |
| 4179 | InitializationKind SubKind = |
| 4180 | Kind.getKind() == InitializationKind::IK_DirectList |
| 4181 | ? InitializationKind::CreateDirect(Kind.getLocation(), |
| 4182 | InitList->getLBraceLoc(), |
| 4183 | InitList->getRBraceLoc()) |
| 4184 | : Kind; |
| 4185 | Expr *SubInit[1] = { InitList->getInit(0) }; |
| 4186 | Sequence.InitializeFrom(S, Entity, SubKind, SubInit, |
| 4187 | /*TopLevelOfInitList*/true, |
| 4188 | TreatUnavailableAsInvalid); |
| 4189 | if (Sequence) |
| 4190 | Sequence.RewrapReferenceInitList(Entity.getType(), InitList); |
| 4191 | return; |
| 4192 | } |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 4193 | } |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4194 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4195 | InitListChecker CheckInitList(S, Entity, InitList, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4196 | DestType, /*VerifyOnly=*/true, TreatUnavailableAsInvalid); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4197 | if (CheckInitList.HadError()) { |
| 4198 | Sequence.SetFailed(InitializationSequence::FK_ListInitializationFailed); |
| 4199 | return; |
| 4200 | } |
| 4201 | |
| 4202 | // Add the list initialization step with the built init list. |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4203 | Sequence.AddListInitializationStep(DestType); |
| 4204 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4205 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4206 | /// Try a reference initialization that involves calling a conversion |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4207 | /// function. |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4208 | static OverloadingResult TryRefInitWithConversionFunction( |
| 4209 | Sema &S, const InitializedEntity &Entity, const InitializationKind &Kind, |
| 4210 | Expr *Initializer, bool AllowRValues, bool IsLValueRef, |
| 4211 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4212 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4213 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 4214 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 4215 | QualType cv2T2 = Initializer->getType(); |
| 4216 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 4217 | |
| 4218 | bool DerivedToBase; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4219 | bool ObjCConversion; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4220 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4221 | assert(!S.CompareReferenceRelationship(Initializer->getLocStart(), |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4222 | T1, T2, DerivedToBase, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4223 | ObjCConversion, |
| 4224 | ObjCLifetimeConversion) && |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4225 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 8abbc65 | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 4226 | (void)DerivedToBase; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4227 | (void)ObjCConversion; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4228 | (void)ObjCLifetimeConversion; |
| 4229 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4230 | // Build the candidate set directly in the initialization sequence |
| 4231 | // structure, so that it will persist if we fail. |
| 4232 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 4233 | CandidateSet.clear(OverloadCandidateSet::CSK_InitByUserDefinedConversion); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4234 | |
Richard Smith | b368ea8 | 2018-07-02 23:25:22 +0000 | [diff] [blame] | 4235 | // Determine whether we are allowed to call explicit conversion operators. |
| 4236 | // Note that none of [over.match.copy], [over.match.conv], nor |
| 4237 | // [over.match.ref] permit an explicit constructor to be chosen when |
| 4238 | // initializing a reference, not even for direct-initialization. |
| 4239 | bool AllowExplicitCtors = false; |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4240 | bool AllowExplicitConvs = Kind.allowExplicitConversionFunctionsInRefBinding(); |
| 4241 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4242 | const RecordType *T1RecordType = nullptr; |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 4243 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4244 | S.isCompleteType(Kind.getLocation(), T1)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4245 | // The type we're converting to is a class type. Enumerate its constructors |
| 4246 | // to see if there is a suitable conversion. |
| 4247 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 4248 | |
Richard Smith | 40c7806 | 2015-02-21 02:31:57 +0000 | [diff] [blame] | 4249 | for (NamedDecl *D : S.LookupConstructors(T1RecordDecl)) { |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4250 | auto Info = getConstructorInfo(D); |
| 4251 | if (!Info.Constructor) |
| 4252 | continue; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4253 | |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4254 | if (!Info.Constructor->isInvalidDecl() && |
Richard Smith | b368ea8 | 2018-07-02 23:25:22 +0000 | [diff] [blame] | 4255 | Info.Constructor->isConvertingConstructor(AllowExplicitCtors)) { |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4256 | if (Info.ConstructorTmpl) |
| 4257 | S.AddTemplateOverloadCandidate(Info.ConstructorTmpl, Info.FoundDecl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4258 | /*ExplicitArgs*/ nullptr, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4259 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | dfbdfbb | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 4260 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4261 | else |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4262 | S.AddOverloadCandidate(Info.Constructor, Info.FoundDecl, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4263 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | dfbdfbb | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 4264 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4265 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4266 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4267 | } |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 4268 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 4269 | return OR_No_Viable_Function; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4270 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4271 | const RecordType *T2RecordType = nullptr; |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 4272 | if ((T2RecordType = T2->getAs<RecordType>()) && |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4273 | S.isCompleteType(Kind.getLocation(), T2)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4274 | // The type we're converting from is a class type, enumerate its conversion |
| 4275 | // functions. |
| 4276 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 4277 | |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 4278 | const auto &Conversions = T2RecordDecl->getVisibleConversionFunctions(); |
| 4279 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4280 | NamedDecl *D = *I; |
| 4281 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 4282 | if (isa<UsingShadowDecl>(D)) |
| 4283 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4284 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4285 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 4286 | CXXConversionDecl *Conv; |
| 4287 | if (ConvTemplate) |
| 4288 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 4289 | else |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4290 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4291 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4292 | // If the conversion function doesn't return a reference type, |
| 4293 | // it can't be considered for this conversion unless we're allowed to |
| 4294 | // consider rvalues. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4295 | // FIXME: Do we need to make sure that we only consider conversion |
| 4296 | // candidates with reference-compatible results? That might be needed to |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4297 | // break recursion. |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4298 | if ((AllowExplicitConvs || !Conv->isExplicit()) && |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4299 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 4300 | if (ConvTemplate) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4301 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4302 | ActingDC, Initializer, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 4303 | DestType, CandidateSet, |
| 4304 | /*AllowObjCConversionOnExplicit=*/ |
| 4305 | false); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4306 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4307 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 4308 | Initializer, DestType, CandidateSet, |
| 4309 | /*AllowObjCConversionOnExplicit=*/false); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4310 | } |
| 4311 | } |
| 4312 | } |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 4313 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 4314 | return OR_No_Viable_Function; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4315 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4316 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 4317 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4318 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4319 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4320 | if (OverloadingResult Result |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 4321 | = CandidateSet.BestViableFunction(S, DeclLoc, Best)) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4322 | return Result; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 4323 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4324 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 4325 | // This is the overload that will be used for this initialization step if we |
| 4326 | // use this initialization. Mark it as referenced. |
| 4327 | Function->setReferenced(); |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4328 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4329 | // Compute the returned type and value kind of the conversion. |
| 4330 | QualType cv3T3; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4331 | if (isa<CXXConversionDecl>(Function)) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4332 | cv3T3 = Function->getReturnType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4333 | else |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4334 | cv3T3 = T1; |
| 4335 | |
| 4336 | ExprValueKind VK = VK_RValue; |
| 4337 | if (cv3T3->isLValueReferenceType()) |
| 4338 | VK = VK_LValue; |
| 4339 | else if (const auto *RRef = cv3T3->getAs<RValueReferenceType>()) |
| 4340 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
| 4341 | cv3T3 = cv3T3.getNonLValueExprType(S.Context); |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 4342 | |
| 4343 | // Add the user-defined conversion step. |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4344 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4345 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, cv3T3, |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4346 | HadMultipleCandidates); |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 4347 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4348 | // Determine whether we'll need to perform derived-to-base adjustments or |
| 4349 | // other conversions. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4350 | bool NewDerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4351 | bool NewObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4352 | bool NewObjCLifetimeConversion = false; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4353 | Sema::ReferenceCompareResult NewRefRelationship |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4354 | = S.CompareReferenceRelationship(DeclLoc, T1, cv3T3, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4355 | NewDerivedToBase, NewObjCConversion, |
| 4356 | NewObjCLifetimeConversion); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4357 | |
| 4358 | // Add the final conversion sequence, if necessary. |
Douglas Gregor | 1ce52ca | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 4359 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4360 | assert(!isa<CXXConstructorDecl>(Function) && |
| 4361 | "should not have conversion after constructor"); |
| 4362 | |
Douglas Gregor | 1ce52ca | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 4363 | ImplicitConversionSequence ICS; |
| 4364 | ICS.setStandard(); |
| 4365 | ICS.Standard = Best->FinalConversion; |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4366 | Sequence.AddConversionSequenceStep(ICS, ICS.Standard.getToType(2)); |
| 4367 | |
| 4368 | // Every implicit conversion results in a prvalue, except for a glvalue |
| 4369 | // derived-to-base conversion, which we handle below. |
| 4370 | cv3T3 = ICS.Standard.getToType(2); |
| 4371 | VK = VK_RValue; |
| 4372 | } |
| 4373 | |
| 4374 | // If the converted initializer is a prvalue, its type T4 is adjusted to |
| 4375 | // type "cv1 T4" and the temporary materialization conversion is applied. |
| 4376 | // |
| 4377 | // We adjust the cv-qualifications to match the reference regardless of |
| 4378 | // whether we have a prvalue so that the AST records the change. In this |
| 4379 | // case, T4 is "cv3 T3". |
| 4380 | QualType cv1T4 = S.Context.getQualifiedType(cv3T3, cv1T1.getQualifiers()); |
| 4381 | if (cv1T4.getQualifiers() != cv3T3.getQualifiers()) |
| 4382 | Sequence.AddQualificationConversionStep(cv1T4, VK); |
| 4383 | Sequence.AddReferenceBindingStep(cv1T4, VK == VK_RValue); |
| 4384 | VK = IsLValueRef ? VK_LValue : VK_XValue; |
| 4385 | |
| 4386 | if (NewDerivedToBase) |
| 4387 | Sequence.AddDerivedToBaseCastStep(cv1T1, VK); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4388 | else if (NewObjCConversion) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4389 | Sequence.AddObjCObjectConversionStep(cv1T1); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4390 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4391 | return OR_Success; |
| 4392 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4393 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4394 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 4395 | const InitializedEntity &Entity, |
| 4396 | Expr *CurInitExpr); |
| 4397 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4398 | /// Attempt reference initialization (C++0x [dcl.init.ref]) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4399 | static void TryReferenceInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4400 | const InitializedEntity &Entity, |
| 4401 | const InitializationKind &Kind, |
| 4402 | Expr *Initializer, |
| 4403 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4404 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4405 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 4406 | Qualifiers T1Quals; |
| 4407 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4408 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 4409 | Qualifiers T2Quals; |
| 4410 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4411 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4412 | // If the initializer is the address of an overloaded function, try |
| 4413 | // to resolve the overloaded function. If all goes well, T2 is the |
| 4414 | // type of the resulting function. |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4415 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 4416 | T1, Sequence)) |
| 4417 | return; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4418 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4419 | // Delegate everything else to a subfunction. |
| 4420 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 4421 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 4422 | } |
| 4423 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4424 | /// Determine whether an expression is a non-referenceable glvalue (one to |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 4425 | /// which a reference can never bind). Attempting to bind a reference to |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4426 | /// such a glvalue will always create a temporary. |
| 4427 | static bool isNonReferenceableGLValue(Expr *E) { |
| 4428 | return E->refersToBitField() || E->refersToVectorElement(); |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 4429 | } |
| 4430 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4431 | /// Reference initialization without resolving overloaded functions. |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4432 | static void TryReferenceInitializationCore(Sema &S, |
| 4433 | const InitializedEntity &Entity, |
| 4434 | const InitializationKind &Kind, |
| 4435 | Expr *Initializer, |
| 4436 | QualType cv1T1, QualType T1, |
| 4437 | Qualifiers T1Quals, |
| 4438 | QualType cv2T2, QualType T2, |
| 4439 | Qualifiers T2Quals, |
| 4440 | InitializationSequence &Sequence) { |
| 4441 | QualType DestType = Entity.getType(); |
| 4442 | SourceLocation DeclLoc = Initializer->getLocStart(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4443 | // Compute some basic properties of the types and the initializer. |
| 4444 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 4445 | bool isRValueRef = !isLValueRef; |
| 4446 | bool DerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4447 | bool ObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4448 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4449 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4450 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4451 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4452 | ObjCConversion, ObjCLifetimeConversion); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4453 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4454 | // C++0x [dcl.init.ref]p5: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4455 | // 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] | 4456 | // "cv2 T2" as follows: |
| 4457 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4458 | // - If the reference is an lvalue reference and the initializer |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4459 | // expression |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4460 | // Note the analogous bullet points for rvalue refs to functions. Because |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4461 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 4462 | // like lvalue refs. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4463 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4464 | bool T1Function = T1->isFunctionType(); |
| 4465 | if (isLValueRef || T1Function) { |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4466 | if (InitCategory.isLValue() && !isNonReferenceableGLValue(Initializer) && |
Richard Smith | ce76629 | 2016-10-21 23:01:55 +0000 | [diff] [blame] | 4467 | (RefRelationship == Sema::Ref_Compatible || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4468 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4469 | RefRelationship == Sema::Ref_Related))) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4470 | // - 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] | 4471 | // reference-compatible with "cv2 T2," or |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4472 | if (T1Quals != T2Quals) |
| 4473 | // Convert to cv1 T2. This should only add qualifiers unless this is a |
| 4474 | // c-style cast. The removal of qualifiers in that case notionally |
| 4475 | // happens after the reference binding, but that doesn't matter. |
| 4476 | Sequence.AddQualificationConversionStep( |
| 4477 | S.Context.getQualifiedType(T2, T1Quals), |
| 4478 | Initializer->getValueKind()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4479 | if (DerivedToBase) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4480 | Sequence.AddDerivedToBaseCastStep(cv1T1, VK_LValue); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4481 | else if (ObjCConversion) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4482 | Sequence.AddObjCObjectConversionStep(cv1T1); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4483 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4484 | // We only create a temporary here when binding a reference to a |
| 4485 | // bit-field or vector element. Those cases are't supposed to be |
| 4486 | // handled by this bullet, but the outcome is the same either way. |
| 4487 | Sequence.AddReferenceBindingStep(cv1T1, false); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4488 | return; |
| 4489 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4490 | |
| 4491 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 4492 | // reference-related to T2, and can be implicitly converted to an |
| 4493 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 4494 | // with "cv3 T3" (this conversion is selected by enumerating the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4495 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 4496 | // one through overload resolution (13.3)), |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4497 | // 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] | 4498 | // an rvalue. DR1287 removed the "implicitly" here. |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4499 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 4500 | (isLValueRef || InitCategory.isRValue())) { |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4501 | ConvOvlResult = TryRefInitWithConversionFunction( |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4502 | S, Entity, Kind, Initializer, /*AllowRValues*/ isRValueRef, |
| 4503 | /*IsLValueRef*/ isLValueRef, Sequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4504 | if (ConvOvlResult == OR_Success) |
| 4505 | return; |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4506 | if (ConvOvlResult != OR_No_Viable_Function) |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4507 | Sequence.SetOverloadFailure( |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4508 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4509 | ConvOvlResult); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4510 | } |
| 4511 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4512 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4513 | // - Otherwise, the reference shall be an lvalue reference to a |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4514 | // 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] | 4515 | // shall be an rvalue reference. |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 4516 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) { |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 4517 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 4518 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 4519 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4520 | Sequence.SetOverloadFailure( |
| 4521 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4522 | ConvOvlResult); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4523 | else if (!InitCategory.isLValue()) |
| 4524 | Sequence.SetFailed( |
| 4525 | InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
| 4526 | else { |
| 4527 | InitializationSequence::FailureKind FK; |
| 4528 | switch (RefRelationship) { |
| 4529 | case Sema::Ref_Compatible: |
| 4530 | if (Initializer->refersToBitField()) |
| 4531 | FK = InitializationSequence:: |
| 4532 | FK_NonConstLValueReferenceBindingToBitfield; |
| 4533 | else if (Initializer->refersToVectorElement()) |
| 4534 | FK = InitializationSequence:: |
| 4535 | FK_NonConstLValueReferenceBindingToVectorElement; |
| 4536 | else |
| 4537 | llvm_unreachable("unexpected kind of compatible initializer"); |
| 4538 | break; |
| 4539 | case Sema::Ref_Related: |
| 4540 | FK = InitializationSequence::FK_ReferenceInitDropsQualifiers; |
| 4541 | break; |
| 4542 | case Sema::Ref_Incompatible: |
| 4543 | FK = InitializationSequence:: |
| 4544 | FK_NonConstLValueReferenceBindingToUnrelated; |
| 4545 | break; |
| 4546 | } |
| 4547 | Sequence.SetFailed(FK); |
| 4548 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4549 | return; |
| 4550 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4551 | |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4552 | // - If the initializer expression |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4553 | // - is an |
| 4554 | // [<=14] xvalue (but not a bit-field), class prvalue, array prvalue, or |
| 4555 | // [1z] rvalue (but not a bit-field) or |
| 4556 | // function lvalue and "cv1 T1" is reference-compatible with "cv2 T2" |
| 4557 | // |
| 4558 | // Note: functions are handled above and below rather than here... |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4559 | if (!T1Function && |
Richard Smith | ce76629 | 2016-10-21 23:01:55 +0000 | [diff] [blame] | 4560 | (RefRelationship == Sema::Ref_Compatible || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4561 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4562 | RefRelationship == Sema::Ref_Related)) && |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4563 | ((InitCategory.isXValue() && !isNonReferenceableGLValue(Initializer)) || |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 4564 | (InitCategory.isPRValue() && |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 4565 | (S.getLangOpts().CPlusPlus17 || T2->isRecordType() || |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 4566 | T2->isArrayType())))) { |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4567 | ExprValueKind ValueKind = InitCategory.isXValue() ? VK_XValue : VK_RValue; |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4568 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4569 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 4570 | // compiler the freedom to perform a copy here or bind to the |
| 4571 | // object, while C++0x requires that we bind directly to the |
| 4572 | // object. Hence, we always bind to the object without making an |
| 4573 | // extra copy. However, in C++03 requires that we check for the |
| 4574 | // presence of a suitable copy constructor: |
| 4575 | // |
| 4576 | // The constructor that would be used to make the copy shall |
| 4577 | // be callable whether or not the copy is actually done. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4578 | if (!S.getLangOpts().CPlusPlus11 && !S.getLangOpts().MicrosoftExt) |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4579 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4580 | else if (S.getLangOpts().CPlusPlus11) |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4581 | CheckCXX98CompatAccessibleCopy(S, Entity, Initializer); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4582 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4583 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4584 | // C++1z [dcl.init.ref]/5.2.1.2: |
| 4585 | // If the converted initializer is a prvalue, its type T4 is adjusted |
| 4586 | // to type "cv1 T4" and the temporary materialization conversion is |
| 4587 | // applied. |
| 4588 | QualType cv1T4 = S.Context.getQualifiedType(cv2T2, T1Quals); |
| 4589 | if (T1Quals != T2Quals) |
| 4590 | Sequence.AddQualificationConversionStep(cv1T4, ValueKind); |
| 4591 | Sequence.AddReferenceBindingStep(cv1T4, ValueKind == VK_RValue); |
| 4592 | ValueKind = isLValueRef ? VK_LValue : VK_XValue; |
| 4593 | |
| 4594 | // In any case, the reference is bound to the resulting glvalue (or to |
| 4595 | // an appropriate base class subobject). |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4596 | if (DerivedToBase) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4597 | Sequence.AddDerivedToBaseCastStep(cv1T1, ValueKind); |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4598 | else if (ObjCConversion) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4599 | Sequence.AddObjCObjectConversionStep(cv1T1); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4600 | return; |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4601 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4602 | |
| 4603 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 4604 | // reference-related to T2, and can be implicitly converted to an |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4605 | // xvalue, class prvalue, or function lvalue of type "cv3 T3", |
| 4606 | // where "cv1 T1" is reference-compatible with "cv3 T3", |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4607 | // |
| 4608 | // DR1287 removes the "implicitly" here. |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4609 | if (T2->isRecordType()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4610 | if (RefRelationship == Sema::Ref_Incompatible) { |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4611 | ConvOvlResult = TryRefInitWithConversionFunction( |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4612 | S, Entity, Kind, Initializer, /*AllowRValues*/ true, |
| 4613 | /*IsLValueRef*/ isLValueRef, Sequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4614 | if (ConvOvlResult) |
| 4615 | Sequence.SetOverloadFailure( |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4616 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4617 | ConvOvlResult); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4618 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4619 | return; |
| 4620 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4621 | |
Richard Smith | ce76629 | 2016-10-21 23:01:55 +0000 | [diff] [blame] | 4622 | if (RefRelationship == Sema::Ref_Compatible && |
Douglas Gregor | 6fa6ab0 | 2013-03-26 23:59:23 +0000 | [diff] [blame] | 4623 | isRValueRef && InitCategory.isLValue()) { |
| 4624 | Sequence.SetFailed( |
| 4625 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 4626 | return; |
| 4627 | } |
| 4628 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4629 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 4630 | return; |
| 4631 | } |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4632 | |
| 4633 | // - Otherwise, a temporary of type "cv1 T1" is created and initialized |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4634 | // from the initializer expression using the rules for a non-reference |
Richard Smith | 2eabf78 | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 4635 | // copy-initialization (8.5). The reference is then bound to the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4636 | // temporary. [...] |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 4637 | |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 4638 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 4639 | |
Richard Smith | 2eabf78 | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 4640 | // FIXME: Why do we use an implicit conversion here rather than trying |
| 4641 | // copy-initialization? |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4642 | ImplicitConversionSequence ICS |
| 4643 | = S.TryImplicitConversion(Initializer, TempEntity.getType(), |
Richard Smith | 2eabf78 | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 4644 | /*SuppressUserConversions=*/false, |
| 4645 | /*AllowExplicit=*/false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4646 | /*FIXME:InOverloadResolution=*/false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4647 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 4648 | /*AllowObjCWritebackConversion=*/false); |
| 4649 | |
| 4650 | if (ICS.isBad()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4651 | // FIXME: Use the conversion function set stored in ICS to turn |
| 4652 | // this into an overloading ambiguity diagnostic. However, we need |
| 4653 | // to keep that set as an OverloadCandidateSet rather than as some |
| 4654 | // other kind of set. |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4655 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 4656 | Sequence.SetOverloadFailure( |
| 4657 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4658 | ConvOvlResult); |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 4659 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 4660 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4661 | else |
| 4662 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4663 | return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4664 | } else { |
| 4665 | Sequence.AddConversionSequenceStep(ICS, TempEntity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4666 | } |
| 4667 | |
| 4668 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 4669 | // same cv-qualification as, or greater cv-qualification |
| 4670 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 4671 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 4672 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4673 | if (RefRelationship == Sema::Ref_Related && |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 4674 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4675 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 4676 | return; |
| 4677 | } |
| 4678 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4679 | // [...] 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] | 4680 | // reference, the initializer expression shall not be an lvalue. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4681 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 4682 | InitCategory.isLValue()) { |
| 4683 | Sequence.SetFailed( |
| 4684 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 4685 | return; |
| 4686 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4687 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4688 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4689 | } |
| 4690 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4691 | /// Attempt character array initialization from a string literal |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4692 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 4693 | static void TryStringLiteralInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4694 | const InitializedEntity &Entity, |
| 4695 | const InitializationKind &Kind, |
| 4696 | Expr *Initializer, |
| 4697 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4698 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4699 | } |
| 4700 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4701 | /// Attempt value initialization (C++ [dcl.init]p7). |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4702 | static void TryValueInitialization(Sema &S, |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4703 | const InitializedEntity &Entity, |
| 4704 | const InitializationKind &Kind, |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4705 | InitializationSequence &Sequence, |
| 4706 | InitListExpr *InitList) { |
| 4707 | assert((!InitList || InitList->getNumInits() == 0) && |
| 4708 | "Shouldn't use value-init for non-empty init lists"); |
| 4709 | |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4710 | // C++98 [dcl.init]p5, C++11 [dcl.init]p7: |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4711 | // |
| 4712 | // To value-initialize an object of type T means: |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4713 | QualType T = Entity.getType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4714 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4715 | // -- if T is an array type, then each element is value-initialized; |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4716 | T = S.Context.getBaseElementType(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4717 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4718 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 4719 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4720 | bool NeedZeroInitialization = true; |
Richard Smith | 505ef81 | 2016-12-21 01:57:02 +0000 | [diff] [blame] | 4721 | // C++98: |
| 4722 | // -- if T is a class type (clause 9) with a user-declared constructor |
| 4723 | // (12.1), then the default constructor for T is called (and the |
| 4724 | // initialization is ill-formed if T has no accessible default |
| 4725 | // constructor); |
| 4726 | // C++11: |
| 4727 | // -- if T is a class type (clause 9) with either no default constructor |
| 4728 | // (12.1 [class.ctor]) or a default constructor that is user-provided |
| 4729 | // or deleted, then the object is default-initialized; |
| 4730 | // |
| 4731 | // Note that the C++11 rule is the same as the C++98 rule if there are no |
| 4732 | // defaulted or deleted constructors, so we just use it unconditionally. |
| 4733 | CXXConstructorDecl *CD = S.LookupDefaultConstructor(ClassDecl); |
| 4734 | if (!CD || !CD->getCanonicalDecl()->isDefaulted() || CD->isDeleted()) |
| 4735 | NeedZeroInitialization = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4736 | |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4737 | // -- if T is a (possibly cv-qualified) non-union class type without a |
| 4738 | // user-provided or deleted default constructor, then the object is |
| 4739 | // zero-initialized and, if T has a non-trivial default constructor, |
| 4740 | // default-initialized; |
Richard Smith | fb26652 | 2012-10-18 00:44:17 +0000 | [diff] [blame] | 4741 | // The 'non-union' here was removed by DR1502. The 'non-trivial default |
| 4742 | // constructor' part was removed by DR1507. |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4743 | if (NeedZeroInitialization) |
| 4744 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 4745 | |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 4746 | // C++03: |
| 4747 | // -- if T is a non-union class type without a user-declared constructor, |
| 4748 | // then every non-static data member and base class component of T is |
| 4749 | // value-initialized; |
| 4750 | // [...] A program that calls for [...] value-initialization of an |
| 4751 | // entity of reference type is ill-formed. |
| 4752 | // |
| 4753 | // C++11 doesn't need this handling, because value-initialization does not |
| 4754 | // occur recursively there, and the implicit default constructor is |
| 4755 | // defined as deleted in the problematic cases. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4756 | if (!S.getLangOpts().CPlusPlus11 && |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 4757 | ClassDecl->hasUninitializedReferenceMember()) { |
| 4758 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForReference); |
| 4759 | return; |
| 4760 | } |
| 4761 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4762 | // If this is list-value-initialization, pass the empty init list on when |
| 4763 | // building the constructor call. This affects the semantics of a few |
| 4764 | // things (such as whether an explicit default constructor can be called). |
| 4765 | Expr *InitListAsExpr = InitList; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4766 | MultiExprArg Args(&InitListAsExpr, InitList ? 1 : 0); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4767 | bool InitListSyntax = InitList; |
| 4768 | |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 4769 | // FIXME: Instead of creating a CXXConstructExpr of array type here, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 4770 | // wrap a class-typed CXXConstructExpr in an ArrayInitLoopExpr. |
| 4771 | return TryConstructorInitialization( |
| 4772 | S, Entity, Kind, Args, T, Entity.getType(), Sequence, InitListSyntax); |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4773 | } |
| 4774 | } |
| 4775 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4776 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4777 | } |
| 4778 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4779 | /// Attempt default initialization (C++ [dcl.init]p6). |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4780 | static void TryDefaultInitialization(Sema &S, |
| 4781 | const InitializedEntity &Entity, |
| 4782 | const InitializationKind &Kind, |
| 4783 | InitializationSequence &Sequence) { |
| 4784 | assert(Kind.getKind() == InitializationKind::IK_Default); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4785 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4786 | // C++ [dcl.init]p6: |
| 4787 | // To default-initialize an object of type T means: |
| 4788 | // - if T is an array type, each element is default-initialized; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4789 | QualType DestType = S.Context.getBaseElementType(Entity.getType()); |
| 4790 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4791 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 4792 | // constructor for T is called (and the initialization is ill-formed if |
| 4793 | // T has no accessible default constructor); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4794 | if (DestType->isRecordType() && S.getLangOpts().CPlusPlus) { |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 4795 | TryConstructorInitialization(S, Entity, Kind, None, DestType, |
| 4796 | Entity.getType(), Sequence); |
Chandler Carruth | c926240 | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 4797 | return; |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4798 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4799 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4800 | // - otherwise, no initialization is performed. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4801 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4802 | // If a program calls for the default initialization of an object of |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4803 | // 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] | 4804 | // default constructor. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4805 | if (DestType.isConstQualified() && S.getLangOpts().CPlusPlus) { |
Nico Weber | 337d5aa | 2015-04-17 08:32:38 +0000 | [diff] [blame] | 4806 | if (!maybeRecoverWithZeroInitialization(S, Sequence, Entity)) |
| 4807 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4808 | return; |
| 4809 | } |
| 4810 | |
| 4811 | // If the destination type has a lifetime property, zero-initialize it. |
| 4812 | if (DestType.getQualifiers().hasObjCLifetime()) { |
| 4813 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 4814 | return; |
| 4815 | } |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4816 | } |
| 4817 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4818 | /// Attempt a user-defined conversion between two types (C++ [dcl.init]), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4819 | /// which enumerates all conversion functions and performs overload resolution |
| 4820 | /// to select the best. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4821 | static void TryUserDefinedConversion(Sema &S, |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 4822 | QualType DestType, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4823 | const InitializationKind &Kind, |
| 4824 | Expr *Initializer, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4825 | InitializationSequence &Sequence, |
| 4826 | bool TopLevelOfInitList) { |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4827 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 4828 | QualType SourceType = Initializer->getType(); |
| 4829 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 4830 | "Must have a class type to perform a user-defined conversion"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4831 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4832 | // Build the candidate set directly in the initialization sequence |
| 4833 | // structure, so that it will persist if we fail. |
| 4834 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 4835 | CandidateSet.clear(OverloadCandidateSet::CSK_InitByUserDefinedConversion); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4836 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4837 | // Determine whether we are allowed to call explicit constructors or |
| 4838 | // explicit conversion operators. |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 4839 | bool AllowExplicit = Kind.AllowExplicit(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4840 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4841 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 4842 | // The type we're converting to is a class type. Enumerate its constructors |
| 4843 | // to see if there is a suitable conversion. |
| 4844 | CXXRecordDecl *DestRecordDecl |
| 4845 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4846 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4847 | // Try to complete the type we're converting to. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4848 | if (S.isCompleteType(Kind.getLocation(), DestType)) { |
Richard Smith | 776e9c3 | 2017-02-01 03:28:59 +0000 | [diff] [blame] | 4849 | for (NamedDecl *D : S.LookupConstructors(DestRecordDecl)) { |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4850 | auto Info = getConstructorInfo(D); |
| 4851 | if (!Info.Constructor) |
| 4852 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4853 | |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4854 | if (!Info.Constructor->isInvalidDecl() && |
| 4855 | Info.Constructor->isConvertingConstructor(AllowExplicit)) { |
| 4856 | if (Info.ConstructorTmpl) |
| 4857 | S.AddTemplateOverloadCandidate(Info.ConstructorTmpl, Info.FoundDecl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4858 | /*ExplicitArgs*/ nullptr, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4859 | Initializer, CandidateSet, |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 4860 | /*SuppressUserConversions=*/true); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4861 | else |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4862 | S.AddOverloadCandidate(Info.Constructor, Info.FoundDecl, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4863 | Initializer, CandidateSet, |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 4864 | /*SuppressUserConversions=*/true); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4865 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4866 | } |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4867 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4868 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4869 | |
| 4870 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 4871 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4872 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 4873 | // The type we're converting from is a class type, enumerate its conversion |
| 4874 | // functions. |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4875 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4876 | // We can only enumerate the conversion functions for a complete type; if |
| 4877 | // the type isn't complete, simply skip this step. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4878 | if (S.isCompleteType(DeclLoc, SourceType)) { |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4879 | CXXRecordDecl *SourceRecordDecl |
| 4880 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4881 | |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 4882 | const auto &Conversions = |
| 4883 | SourceRecordDecl->getVisibleConversionFunctions(); |
| 4884 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4885 | NamedDecl *D = *I; |
| 4886 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 4887 | if (isa<UsingShadowDecl>(D)) |
| 4888 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4889 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4890 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 4891 | CXXConversionDecl *Conv; |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4892 | if (ConvTemplate) |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4893 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4894 | else |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4895 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4896 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4897 | if (AllowExplicit || !Conv->isExplicit()) { |
| 4898 | if (ConvTemplate) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4899 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4900 | ActingDC, Initializer, DestType, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 4901 | CandidateSet, AllowExplicit); |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4902 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4903 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 4904 | Initializer, DestType, CandidateSet, |
| 4905 | AllowExplicit); |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4906 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4907 | } |
| 4908 | } |
| 4909 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4910 | |
| 4911 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4912 | OverloadCandidateSet::iterator Best; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4913 | if (OverloadingResult Result |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 4914 | = CandidateSet.BestViableFunction(S, DeclLoc, Best)) { |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4915 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4916 | InitializationSequence::FK_UserConversionOverloadFailed, |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4917 | Result); |
| 4918 | return; |
| 4919 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4920 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4921 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 4922 | Function->setReferenced(); |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4923 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
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 | if (isa<CXXConstructorDecl>(Function)) { |
| 4926 | // Add the user-defined conversion step. Any cv-qualification conversion is |
Richard Smith | b24f067 | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 4927 | // subsumed by the initialization. Per DR5, the created temporary is of the |
| 4928 | // cv-unqualified type of the destination. |
| 4929 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
| 4930 | DestType.getUnqualifiedType(), |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4931 | HadMultipleCandidates); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4932 | |
| 4933 | // C++14 and before: |
| 4934 | // - if the function is a constructor, the call initializes a temporary |
| 4935 | // of the cv-unqualified version of the destination type. The [...] |
| 4936 | // temporary [...] is then used to direct-initialize, according to the |
| 4937 | // rules above, the object that is the destination of the |
| 4938 | // copy-initialization. |
| 4939 | // Note that this just performs a simple object copy from the temporary. |
| 4940 | // |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 4941 | // C++17: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4942 | // - if the function is a constructor, the call is a prvalue of the |
| 4943 | // cv-unqualified version of the destination type whose return object |
| 4944 | // is initialized by the constructor. The call is used to |
| 4945 | // direct-initialize, according to the rules above, the object that |
| 4946 | // is the destination of the copy-initialization. |
| 4947 | // Therefore we need to do nothing further. |
| 4948 | // |
| 4949 | // FIXME: Mark this copy as extraneous. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 4950 | if (!S.getLangOpts().CPlusPlus17) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4951 | Sequence.AddFinalCopy(DestType); |
Richard Smith | 16d3150 | 2016-12-21 01:31:56 +0000 | [diff] [blame] | 4952 | else if (DestType.hasQualifiers()) |
| 4953 | Sequence.AddQualificationConversionStep(DestType, VK_RValue); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4954 | return; |
| 4955 | } |
| 4956 | |
| 4957 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 4958 | QualType ConvType = Function->getCallResultType(); |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4959 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType, |
| 4960 | HadMultipleCandidates); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4961 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4962 | if (ConvType->getAs<RecordType>()) { |
| 4963 | // The call is used to direct-initialize [...] the object that is the |
| 4964 | // destination of the copy-initialization. |
| 4965 | // |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 4966 | // 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] | 4967 | // - If the initializer expression is a prvalue and the cv-unqualified |
| 4968 | // version of the source type is the same as the class of the |
| 4969 | // destination [... do not make an extra copy] |
| 4970 | // |
| 4971 | // FIXME: Mark this copy as extraneous. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 4972 | if (!S.getLangOpts().CPlusPlus17 || |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4973 | Function->getReturnType()->isReferenceType() || |
| 4974 | !S.Context.hasSameUnqualifiedType(ConvType, DestType)) |
| 4975 | Sequence.AddFinalCopy(DestType); |
Richard Smith | 16d3150 | 2016-12-21 01:31:56 +0000 | [diff] [blame] | 4976 | else if (!S.Context.hasSameType(ConvType, DestType)) |
| 4977 | Sequence.AddQualificationConversionStep(DestType, VK_RValue); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4978 | return; |
| 4979 | } |
| 4980 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4981 | // If the conversion following the call to the conversion function |
| 4982 | // is interesting, add it as a separate step. |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4983 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 4984 | Best->FinalConversion.Third) { |
| 4985 | ImplicitConversionSequence ICS; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4986 | ICS.setStandard(); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4987 | ICS.Standard = Best->FinalConversion; |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4988 | Sequence.AddConversionSequenceStep(ICS, DestType, TopLevelOfInitList); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4989 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4990 | } |
| 4991 | |
Richard Smith | f032001b | 2013-06-20 02:18:31 +0000 | [diff] [blame] | 4992 | /// An egregious hack for compatibility with libstdc++-4.2: in <tr1/hashtable>, |
| 4993 | /// a function with a pointer return type contains a 'return false;' statement. |
| 4994 | /// In C++11, 'false' is not a null pointer, so this breaks the build of any |
| 4995 | /// code using that header. |
| 4996 | /// |
| 4997 | /// Work around this by treating 'return false;' as zero-initializing the result |
| 4998 | /// if it's used in a pointer-returning function in a system header. |
| 4999 | static bool isLibstdcxxPointerReturnFalseHack(Sema &S, |
| 5000 | const InitializedEntity &Entity, |
| 5001 | const Expr *Init) { |
| 5002 | return S.getLangOpts().CPlusPlus11 && |
| 5003 | Entity.getKind() == InitializedEntity::EK_Result && |
| 5004 | Entity.getType()->isPointerType() && |
| 5005 | isa<CXXBoolLiteralExpr>(Init) && |
| 5006 | !cast<CXXBoolLiteralExpr>(Init)->getValue() && |
| 5007 | S.getSourceManager().isInSystemHeader(Init->getExprLoc()); |
| 5008 | } |
| 5009 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5010 | /// The non-zero enum values here are indexes into diagnostic alternatives. |
| 5011 | enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar }; |
| 5012 | |
| 5013 | /// Determines whether this expression is an acceptable ICR source. |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 5014 | static InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e, |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5015 | bool isAddressOf, bool &isWeakAccess) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5016 | // Skip parens. |
| 5017 | e = e->IgnoreParens(); |
| 5018 | |
| 5019 | // Skip address-of nodes. |
| 5020 | if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 5021 | if (op->getOpcode() == UO_AddrOf) |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5022 | return isInvalidICRSource(C, op->getSubExpr(), /*addressof*/ true, |
| 5023 | isWeakAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5024 | |
| 5025 | // Skip certain casts. |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 5026 | } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 5027 | switch (ce->getCastKind()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5028 | case CK_Dependent: |
| 5029 | case CK_BitCast: |
| 5030 | case CK_LValueBitCast: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5031 | case CK_NoOp: |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5032 | return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf, isWeakAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5033 | |
| 5034 | case CK_ArrayToPointerDecay: |
| 5035 | return IIK_nonscalar; |
| 5036 | |
| 5037 | case CK_NullToPointer: |
| 5038 | return IIK_okay; |
| 5039 | |
| 5040 | default: |
| 5041 | break; |
| 5042 | } |
| 5043 | |
| 5044 | // 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] | 5045 | } else if (isa<DeclRefExpr>(e)) { |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5046 | // set isWeakAccess to true, to mean that there will be an implicit |
| 5047 | // load which requires a cleanup. |
| 5048 | if (e->getType().getObjCLifetime() == Qualifiers::OCL_Weak) |
| 5049 | isWeakAccess = true; |
| 5050 | |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 5051 | if (!isAddressOf) return IIK_nonlocal; |
| 5052 | |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5053 | VarDecl *var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl()); |
| 5054 | if (!var) return IIK_nonlocal; |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 5055 | |
| 5056 | return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5057 | |
| 5058 | // If we have a conditional operator, check both sides. |
| 5059 | } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) { |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5060 | if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf, |
| 5061 | isWeakAccess)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5062 | return iik; |
| 5063 | |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5064 | return isInvalidICRSource(C, cond->getRHS(), isAddressOf, isWeakAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5065 | |
| 5066 | // These are never scalar. |
| 5067 | } else if (isa<ArraySubscriptExpr>(e)) { |
| 5068 | return IIK_nonscalar; |
| 5069 | |
| 5070 | // Otherwise, it needs to be a null pointer constant. |
| 5071 | } else { |
| 5072 | return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull) |
| 5073 | ? IIK_okay : IIK_nonlocal); |
| 5074 | } |
| 5075 | |
| 5076 | return IIK_nonlocal; |
| 5077 | } |
| 5078 | |
| 5079 | /// Check whether the given expression is a valid operand for an |
| 5080 | /// indirect copy/restore. |
| 5081 | static void checkIndirectCopyRestoreSource(Sema &S, Expr *src) { |
| 5082 | assert(src->isRValue()); |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5083 | bool isWeakAccess = false; |
| 5084 | InvalidICRKind iik = isInvalidICRSource(S.Context, src, false, isWeakAccess); |
| 5085 | // If isWeakAccess to true, there will be an implicit |
| 5086 | // load which requires a cleanup. |
| 5087 | if (S.getLangOpts().ObjCAutoRefCount && isWeakAccess) |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 5088 | S.Cleanup.setExprNeedsCleanups(true); |
| 5089 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5090 | if (iik == IIK_okay) return; |
| 5091 | |
| 5092 | S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback) |
| 5093 | << ((unsigned) iik - 1) // shift index into diagnostic explanations |
| 5094 | << src->getSourceRange(); |
| 5095 | } |
| 5096 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5097 | /// Determine whether we have compatible array types for the |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5098 | /// purposes of GNU by-copy array initialization. |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 5099 | static bool hasCompatibleArrayTypes(ASTContext &Context, const ArrayType *Dest, |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5100 | const ArrayType *Source) { |
| 5101 | // If the source and destination array types are equivalent, we're |
| 5102 | // done. |
| 5103 | if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0))) |
| 5104 | return true; |
| 5105 | |
| 5106 | // Make sure that the element types are the same. |
| 5107 | if (!Context.hasSameType(Dest->getElementType(), Source->getElementType())) |
| 5108 | return false; |
| 5109 | |
| 5110 | // The only mismatch we allow is when the destination is an |
| 5111 | // incomplete array type and the source is a constant array type. |
| 5112 | return Source->isConstantArrayType() && Dest->isIncompleteArrayType(); |
| 5113 | } |
| 5114 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5115 | static bool tryObjCWritebackConversion(Sema &S, |
| 5116 | InitializationSequence &Sequence, |
| 5117 | const InitializedEntity &Entity, |
| 5118 | Expr *Initializer) { |
| 5119 | bool ArrayDecay = false; |
| 5120 | QualType ArgType = Initializer->getType(); |
| 5121 | QualType ArgPointee; |
| 5122 | if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) { |
| 5123 | ArrayDecay = true; |
| 5124 | ArgPointee = ArgArrayType->getElementType(); |
| 5125 | ArgType = S.Context.getPointerType(ArgPointee); |
| 5126 | } |
| 5127 | |
| 5128 | // Handle write-back conversion. |
| 5129 | QualType ConvertedArgType; |
| 5130 | if (!S.isObjCWritebackConversion(ArgType, Entity.getType(), |
| 5131 | ConvertedArgType)) |
| 5132 | return false; |
| 5133 | |
| 5134 | // We should copy unless we're passing to an argument explicitly |
| 5135 | // marked 'out'. |
| 5136 | bool ShouldCopy = true; |
| 5137 | if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 5138 | ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 5139 | |
| 5140 | // Do we need an lvalue conversion? |
| 5141 | if (ArrayDecay || Initializer->isGLValue()) { |
| 5142 | ImplicitConversionSequence ICS; |
| 5143 | ICS.setStandard(); |
| 5144 | ICS.Standard.setAsIdentityConversion(); |
| 5145 | |
| 5146 | QualType ResultType; |
| 5147 | if (ArrayDecay) { |
| 5148 | ICS.Standard.First = ICK_Array_To_Pointer; |
| 5149 | ResultType = S.Context.getPointerType(ArgPointee); |
| 5150 | } else { |
| 5151 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
| 5152 | ResultType = Initializer->getType().getNonLValueExprType(S.Context); |
| 5153 | } |
| 5154 | |
| 5155 | Sequence.AddConversionSequenceStep(ICS, ResultType); |
| 5156 | } |
| 5157 | |
| 5158 | Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
| 5159 | return true; |
| 5160 | } |
| 5161 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5162 | static bool TryOCLSamplerInitialization(Sema &S, |
| 5163 | InitializationSequence &Sequence, |
| 5164 | QualType DestType, |
| 5165 | Expr *Initializer) { |
| 5166 | if (!S.getLangOpts().OpenCL || !DestType->isSamplerT() || |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 5167 | (!Initializer->isIntegerConstantExpr(S.Context) && |
| 5168 | !Initializer->getType()->isSamplerT())) |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5169 | return false; |
| 5170 | |
| 5171 | Sequence.AddOCLSamplerInitStep(DestType); |
| 5172 | return true; |
| 5173 | } |
| 5174 | |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5175 | // |
| 5176 | // OpenCL 1.2 spec, s6.12.10 |
| 5177 | // |
| 5178 | // The event argument can also be used to associate the |
| 5179 | // async_work_group_copy with a previous async copy allowing |
| 5180 | // an event to be shared by multiple async copies; otherwise |
| 5181 | // event should be zero. |
| 5182 | // |
| 5183 | static bool TryOCLZeroEventInitialization(Sema &S, |
| 5184 | InitializationSequence &Sequence, |
| 5185 | QualType DestType, |
| 5186 | Expr *Initializer) { |
| 5187 | if (!S.getLangOpts().OpenCL || !DestType->isEventT() || |
| 5188 | !Initializer->isIntegerConstantExpr(S.getASTContext()) || |
| 5189 | (Initializer->EvaluateKnownConstInt(S.getASTContext()) != 0)) |
| 5190 | return false; |
| 5191 | |
| 5192 | Sequence.AddOCLZeroEventStep(DestType); |
| 5193 | return true; |
| 5194 | } |
| 5195 | |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 5196 | static bool TryOCLZeroQueueInitialization(Sema &S, |
| 5197 | InitializationSequence &Sequence, |
| 5198 | QualType DestType, |
| 5199 | Expr *Initializer) { |
| 5200 | if (!S.getLangOpts().OpenCL || S.getLangOpts().OpenCLVersion < 200 || |
| 5201 | !DestType->isQueueT() || |
| 5202 | !Initializer->isIntegerConstantExpr(S.getASTContext()) || |
| 5203 | (Initializer->EvaluateKnownConstInt(S.getASTContext()) != 0)) |
| 5204 | return false; |
| 5205 | |
| 5206 | Sequence.AddOCLZeroQueueStep(DestType); |
| 5207 | return true; |
| 5208 | } |
| 5209 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5210 | InitializationSequence::InitializationSequence(Sema &S, |
| 5211 | const InitializedEntity &Entity, |
| 5212 | const InitializationKind &Kind, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5213 | MultiExprArg Args, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 5214 | bool TopLevelOfInitList, |
| 5215 | bool TreatUnavailableAsInvalid) |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 5216 | : FailedCandidateSet(Kind.getLocation(), OverloadCandidateSet::CSK_Normal) { |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 5217 | InitializeFrom(S, Entity, Kind, Args, TopLevelOfInitList, |
| 5218 | TreatUnavailableAsInvalid); |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 5219 | } |
| 5220 | |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 5221 | /// Tries to get a FunctionDecl out of `E`. If it succeeds and we can take the |
| 5222 | /// address of that function, this returns true. Otherwise, it returns false. |
| 5223 | static bool isExprAnUnaddressableFunction(Sema &S, const Expr *E) { |
| 5224 | auto *DRE = dyn_cast<DeclRefExpr>(E); |
| 5225 | if (!DRE || !isa<FunctionDecl>(DRE->getDecl())) |
| 5226 | return false; |
| 5227 | |
| 5228 | return !S.checkAddressOfFunctionIsAvailable( |
| 5229 | cast<FunctionDecl>(DRE->getDecl())); |
| 5230 | } |
| 5231 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5232 | /// Determine whether we can perform an elementwise array copy for this kind |
| 5233 | /// of entity. |
| 5234 | static bool canPerformArrayCopy(const InitializedEntity &Entity) { |
| 5235 | switch (Entity.getKind()) { |
| 5236 | case InitializedEntity::EK_LambdaCapture: |
| 5237 | // C++ [expr.prim.lambda]p24: |
| 5238 | // For array members, the array elements are direct-initialized in |
| 5239 | // increasing subscript order. |
| 5240 | return true; |
| 5241 | |
| 5242 | case InitializedEntity::EK_Variable: |
| 5243 | // C++ [dcl.decomp]p1: |
| 5244 | // [...] each element is copy-initialized or direct-initialized from the |
| 5245 | // corresponding element of the assignment-expression [...] |
| 5246 | return isa<DecompositionDecl>(Entity.getDecl()); |
| 5247 | |
| 5248 | case InitializedEntity::EK_Member: |
| 5249 | // C++ [class.copy.ctor]p14: |
| 5250 | // - if the member is an array, each element is direct-initialized with |
| 5251 | // the corresponding subobject of x |
| 5252 | return Entity.isImplicitMemberInitializer(); |
| 5253 | |
| 5254 | case InitializedEntity::EK_ArrayElement: |
| 5255 | // All the above cases are intended to apply recursively, even though none |
| 5256 | // of them actually say that. |
| 5257 | if (auto *E = Entity.getParent()) |
| 5258 | return canPerformArrayCopy(*E); |
| 5259 | break; |
| 5260 | |
| 5261 | default: |
| 5262 | break; |
| 5263 | } |
| 5264 | |
| 5265 | return false; |
| 5266 | } |
| 5267 | |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 5268 | void InitializationSequence::InitializeFrom(Sema &S, |
| 5269 | const InitializedEntity &Entity, |
| 5270 | const InitializationKind &Kind, |
| 5271 | MultiExprArg Args, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 5272 | bool TopLevelOfInitList, |
| 5273 | bool TreatUnavailableAsInvalid) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5274 | ASTContext &Context = S.Context; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5275 | |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 5276 | // Eliminate non-overload placeholder types in the arguments. We |
| 5277 | // need to do this before checking whether types are dependent |
| 5278 | // because lowering a pseudo-object expression might well give us |
| 5279 | // something of dependent type. |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5280 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 5281 | if (Args[I]->getType()->isNonOverloadPlaceholderType()) { |
| 5282 | // FIXME: should we be doing this here? |
| 5283 | ExprResult result = S.CheckPlaceholderExpr(Args[I]); |
| 5284 | if (result.isInvalid()) { |
| 5285 | SetFailed(FK_PlaceholderType); |
| 5286 | return; |
| 5287 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5288 | Args[I] = result.get(); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 5289 | } |
| 5290 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5291 | // C++0x [dcl.init]p16: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5292 | // The semantics of initializers are as follows. The destination type is |
| 5293 | // the type of the object or reference being initialized and the source |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5294 | // 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] | 5295 | // 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] | 5296 | // parenthesized list of expressions. |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5297 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5298 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5299 | if (DestType->isDependentType() || |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5300 | Expr::hasAnyTypeDependentArguments(Args)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5301 | SequenceKind = DependentSequence; |
| 5302 | return; |
| 5303 | } |
| 5304 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5305 | // Almost everything is a normal sequence. |
| 5306 | setSequenceKind(NormalSequence); |
| 5307 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5308 | QualType SourceType; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5309 | Expr *Initializer = nullptr; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5310 | if (Args.size() == 1) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5311 | Initializer = Args[0]; |
Fariborz Jahanian | 283bf89 | 2013-12-18 21:04:43 +0000 | [diff] [blame] | 5312 | if (S.getLangOpts().ObjC1) { |
| 5313 | if (S.CheckObjCBridgeRelatedConversions(Initializer->getLocStart(), |
| 5314 | DestType, Initializer->getType(), |
| 5315 | Initializer) || |
| 5316 | S.ConversionToObjCStringLiteralCheck(DestType, Initializer)) |
| 5317 | Args[0] = Initializer; |
Fariborz Jahanian | 283bf89 | 2013-12-18 21:04:43 +0000 | [diff] [blame] | 5318 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5319 | if (!isa<InitListExpr>(Initializer)) |
| 5320 | SourceType = Initializer->getType(); |
| 5321 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5322 | |
Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 5323 | // - If the initializer is a (non-parenthesized) braced-init-list, the |
| 5324 | // object is list-initialized (8.5.4). |
| 5325 | if (Kind.getKind() != InitializationKind::IK_Direct) { |
| 5326 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 5327 | TryListInitialization(S, Entity, Kind, InitList, *this, |
| 5328 | TreatUnavailableAsInvalid); |
Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 5329 | return; |
| 5330 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5331 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5332 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5333 | // - If the destination type is a reference type, see 8.5.3. |
| 5334 | if (DestType->isReferenceType()) { |
| 5335 | // C++0x [dcl.init.ref]p1: |
| 5336 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 5337 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 5338 | // by an object that can be converted into a T. |
| 5339 | // (Therefore, multiple arguments are not permitted.) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5340 | if (Args.size() != 1) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5341 | SetFailed(FK_TooManyInitsForReference); |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 5342 | // C++17 [dcl.init.ref]p5: |
| 5343 | // A reference [...] is initialized by an expression [...] as follows: |
| 5344 | // If the initializer is not an expression, presumably we should reject, |
| 5345 | // but the standard fails to actually say so. |
| 5346 | else if (isa<InitListExpr>(Args[0])) |
| 5347 | SetFailed(FK_ParenthesizedListInitForReference); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5348 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5349 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5350 | return; |
| 5351 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5352 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5353 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5354 | if (Kind.getKind() == InitializationKind::IK_Value || |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5355 | (Kind.getKind() == InitializationKind::IK_Direct && Args.empty())) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5356 | TryValueInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5357 | return; |
| 5358 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5359 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5360 | // Handle default initialization. |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 5361 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5362 | TryDefaultInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5363 | return; |
| 5364 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5365 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 5366 | // - If the destination type is an array of characters, an array of |
| 5367 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 5368 | // initializer is a string literal, see 8.5.2. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5369 | // - Otherwise, if the destination type is an array, the program is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5370 | // ill-formed. |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5371 | if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) { |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 5372 | if (Initializer && isa<VariableArrayType>(DestAT)) { |
| 5373 | SetFailed(FK_VariableLengthArrayHasInitializer); |
| 5374 | return; |
| 5375 | } |
| 5376 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 5377 | if (Initializer) { |
| 5378 | switch (IsStringInit(Initializer, DestAT, Context)) { |
| 5379 | case SIF_None: |
| 5380 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
| 5381 | return; |
| 5382 | case SIF_NarrowStringIntoWideChar: |
| 5383 | SetFailed(FK_NarrowStringIntoWideCharArray); |
| 5384 | return; |
| 5385 | case SIF_WideStringIntoChar: |
| 5386 | SetFailed(FK_WideStringIntoCharArray); |
| 5387 | return; |
| 5388 | case SIF_IncompatWideStringIntoWideChar: |
| 5389 | SetFailed(FK_IncompatWideStringIntoWideChar); |
| 5390 | return; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 5391 | case SIF_PlainStringIntoUTF8Char: |
| 5392 | SetFailed(FK_PlainStringIntoUTF8Char); |
| 5393 | return; |
| 5394 | case SIF_UTF8StringIntoPlainChar: |
| 5395 | SetFailed(FK_UTF8StringIntoPlainChar); |
| 5396 | return; |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 5397 | case SIF_Other: |
| 5398 | break; |
| 5399 | } |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 5400 | } |
| 5401 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5402 | // Some kinds of initialization permit an array to be initialized from |
| 5403 | // another array of the same type, and perform elementwise initialization. |
| 5404 | if (Initializer && isa<ConstantArrayType>(DestAT) && |
| 5405 | S.Context.hasSameUnqualifiedType(Initializer->getType(), |
| 5406 | Entity.getType()) && |
| 5407 | canPerformArrayCopy(Entity)) { |
| 5408 | // If source is a prvalue, use it directly. |
| 5409 | if (Initializer->getValueKind() == VK_RValue) { |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 5410 | AddArrayInitStep(DestType, /*IsGNUExtension*/false); |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5411 | return; |
| 5412 | } |
| 5413 | |
| 5414 | // Emit element-at-a-time copy loop. |
| 5415 | InitializedEntity Element = |
| 5416 | InitializedEntity::InitializeElement(S.Context, 0, Entity); |
| 5417 | QualType InitEltT = |
| 5418 | Context.getAsArrayType(Initializer->getType())->getElementType(); |
Richard Smith | 30e304e | 2016-12-14 00:03:17 +0000 | [diff] [blame] | 5419 | OpaqueValueExpr OVE(Initializer->getExprLoc(), InitEltT, |
| 5420 | Initializer->getValueKind(), |
| 5421 | Initializer->getObjectKind()); |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5422 | Expr *OVEAsExpr = &OVE; |
| 5423 | InitializeFrom(S, Element, Kind, OVEAsExpr, TopLevelOfInitList, |
| 5424 | TreatUnavailableAsInvalid); |
| 5425 | if (!Failed()) |
| 5426 | AddArrayInitLoopStep(Entity.getType(), InitEltT); |
| 5427 | return; |
| 5428 | } |
| 5429 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5430 | // Note: as an GNU C extension, we allow initialization of an |
| 5431 | // array from a compound literal that creates an array of the same |
| 5432 | // type, so long as the initializer has no side effects. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5433 | if (!S.getLangOpts().CPlusPlus && Initializer && |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5434 | isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) && |
| 5435 | Initializer->getType()->isArrayType()) { |
| 5436 | const ArrayType *SourceAT |
| 5437 | = Context.getAsArrayType(Initializer->getType()); |
| 5438 | if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5439 | SetFailed(FK_ArrayTypeMismatch); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5440 | else if (Initializer->HasSideEffects(S.Context)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5441 | SetFailed(FK_NonConstantArrayInit); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5442 | else { |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 5443 | AddArrayInitStep(DestType, /*IsGNUExtension*/true); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5444 | } |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5445 | } |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5446 | // Note: as a GNU C++ extension, we allow list-initialization of a |
| 5447 | // class member of array type from a parenthesized initializer list. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5448 | else if (S.getLangOpts().CPlusPlus && |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5449 | Entity.getKind() == InitializedEntity::EK_Member && |
| 5450 | Initializer && isa<InitListExpr>(Initializer)) { |
| 5451 | TryListInitialization(S, Entity, Kind, cast<InitListExpr>(Initializer), |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 5452 | *this, TreatUnavailableAsInvalid); |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5453 | AddParenthesizedArrayInitStep(DestType); |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 5454 | } else if (DestAT->getElementType()->isCharType()) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5455 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 5456 | else if (IsWideCharCompatible(DestAT->getElementType(), Context)) |
| 5457 | SetFailed(FK_ArrayNeedsInitListOrWideStringLiteral); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5458 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5459 | SetFailed(FK_ArrayNeedsInitList); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5460 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5461 | return; |
| 5462 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5463 | |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 5464 | // Determine whether we should consider writeback conversions for |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5465 | // Objective-C ARC. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5466 | bool allowObjCWritebackConversion = S.getLangOpts().ObjCAutoRefCount && |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5467 | Entity.isParameterKind(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5468 | |
| 5469 | // We're at the end of the line for C: it's either a write-back conversion |
| 5470 | // 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] | 5471 | if (!S.getLangOpts().CPlusPlus) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5472 | // If allowed, check whether this is an Objective-C writeback conversion. |
| 5473 | if (allowObjCWritebackConversion && |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5474 | tryObjCWritebackConversion(S, *this, Entity, Initializer)) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5475 | return; |
| 5476 | } |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5477 | |
| 5478 | if (TryOCLSamplerInitialization(S, *this, DestType, Initializer)) |
| 5479 | return; |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5480 | |
| 5481 | if (TryOCLZeroEventInitialization(S, *this, DestType, Initializer)) |
| 5482 | return; |
| 5483 | |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 5484 | if (TryOCLZeroQueueInitialization(S, *this, DestType, Initializer)) |
| 5485 | return; |
| 5486 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5487 | // Handle initialization in C |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5488 | AddCAssignmentStep(DestType); |
| 5489 | MaybeProduceObjCObject(S, *this, Entity); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5490 | return; |
| 5491 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5492 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5493 | assert(S.getLangOpts().CPlusPlus); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 5494 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5495 | // - If the destination type is a (possibly cv-qualified) class type: |
| 5496 | if (DestType->isRecordType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5497 | // - If the initialization is direct-initialization, or if it is |
| 5498 | // copy-initialization where the cv-unqualified version of the |
| 5499 | // 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] | 5500 | // class of the destination, constructors are considered. [...] |
| 5501 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 5502 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 5503 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 5504 | S.IsDerivedFrom(Initializer->getLocStart(), SourceType, DestType)))) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5505 | TryConstructorInitialization(S, Entity, Kind, Args, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5506 | DestType, DestType, *this); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5507 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5508 | // user-defined conversion sequences that can convert from the source |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5509 | // type to the destination type or (when a conversion function is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5510 | // used) to a derived class thereof are enumerated as described in |
| 5511 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 5512 | // (13.3). |
| 5513 | else |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5514 | TryUserDefinedConversion(S, DestType, Kind, Initializer, *this, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5515 | TopLevelOfInitList); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5516 | return; |
| 5517 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5518 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 5519 | assert(Args.size() >= 1 && "Zero-argument case handled above"); |
| 5520 | |
| 5521 | // The remaining cases all need a source type. |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5522 | if (Args.size() > 1) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5523 | SetFailed(FK_TooManyInitsForScalar); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5524 | return; |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 5525 | } else if (isa<InitListExpr>(Args[0])) { |
| 5526 | SetFailed(FK_ParenthesizedListInitForScalar); |
| 5527 | return; |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5528 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5529 | |
| 5530 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5531 | // type, conversion functions are considered. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5532 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5533 | // For a conversion to _Atomic(T) from either T or a class type derived |
| 5534 | // from T, initialize the T object then convert to _Atomic type. |
| 5535 | bool NeedAtomicConversion = false; |
| 5536 | if (const AtomicType *Atomic = DestType->getAs<AtomicType>()) { |
| 5537 | if (Context.hasSameUnqualifiedType(SourceType, Atomic->getValueType()) || |
Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 5538 | S.IsDerivedFrom(Initializer->getLocStart(), SourceType, |
| 5539 | Atomic->getValueType())) { |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5540 | DestType = Atomic->getValueType(); |
| 5541 | NeedAtomicConversion = true; |
| 5542 | } |
| 5543 | } |
| 5544 | |
| 5545 | TryUserDefinedConversion(S, DestType, Kind, Initializer, *this, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5546 | TopLevelOfInitList); |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5547 | MaybeProduceObjCObject(S, *this, Entity); |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5548 | if (!Failed() && NeedAtomicConversion) |
| 5549 | AddAtomicConversionStep(Entity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5550 | return; |
| 5551 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5552 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5553 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5554 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5555 | // conversions (Clause 4) will be used, if necessary, to convert the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5556 | // initializer expression to the cv-unqualified version of the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5557 | // destination type; no user-defined conversions are considered. |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5558 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5559 | ImplicitConversionSequence ICS |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5560 | = S.TryImplicitConversion(Initializer, DestType, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5561 | /*SuppressUserConversions*/true, |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 5562 | /*AllowExplicitConversions*/ false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 5563 | /*InOverloadResolution*/ false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5564 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 5565 | allowObjCWritebackConversion); |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5566 | |
| 5567 | if (ICS.isStandard() && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5568 | ICS.Standard.Second == ICK_Writeback_Conversion) { |
| 5569 | // Objective-C ARC writeback conversion. |
| 5570 | |
| 5571 | // We should copy unless we're passing to an argument explicitly |
| 5572 | // marked 'out'. |
| 5573 | bool ShouldCopy = true; |
| 5574 | if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 5575 | ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 5576 | |
| 5577 | // If there was an lvalue adjustment, add it as a separate conversion. |
| 5578 | if (ICS.Standard.First == ICK_Array_To_Pointer || |
| 5579 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 5580 | ImplicitConversionSequence LvalueICS; |
| 5581 | LvalueICS.setStandard(); |
| 5582 | LvalueICS.Standard.setAsIdentityConversion(); |
| 5583 | LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0)); |
| 5584 | LvalueICS.Standard.First = ICS.Standard.First; |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5585 | AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5586 | } |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5587 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5588 | AddPassByIndirectCopyRestoreStep(DestType, ShouldCopy); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5589 | } else if (ICS.isBad()) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 5590 | DeclAccessPair dap; |
Richard Smith | f032001b | 2013-06-20 02:18:31 +0000 | [diff] [blame] | 5591 | if (isLibstdcxxPointerReturnFalseHack(S, Entity, Initializer)) { |
| 5592 | AddZeroInitializationStep(Entity.getType()); |
| 5593 | } else if (Initializer->getType() == Context.OverloadTy && |
| 5594 | !S.ResolveAddressOfOverloadedFunction(Initializer, DestType, |
| 5595 | false, dap)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5596 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 5597 | else if (Initializer->getType()->isFunctionType() && |
| 5598 | isExprAnUnaddressableFunction(S, Initializer)) |
| 5599 | SetFailed(InitializationSequence::FK_AddressOfUnaddressableFunction); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 5600 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5601 | SetFailed(InitializationSequence::FK_ConversionFailed); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5602 | } else { |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5603 | AddConversionSequenceStep(ICS, DestType, TopLevelOfInitList); |
John McCall | fa27234 | 2011-06-16 23:24:51 +0000 | [diff] [blame] | 5604 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5605 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 5606 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5607 | } |
| 5608 | |
| 5609 | InitializationSequence::~InitializationSequence() { |
Davide Italiano | 67bb9f7 | 2015-07-01 21:51:58 +0000 | [diff] [blame] | 5610 | for (auto &S : Steps) |
| 5611 | S.Destroy(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5612 | } |
| 5613 | |
| 5614 | //===----------------------------------------------------------------------===// |
| 5615 | // Perform initialization |
| 5616 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5617 | static Sema::AssignmentAction |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 5618 | getAssignmentAction(const InitializedEntity &Entity, bool Diagnose = false) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5619 | switch(Entity.getKind()) { |
| 5620 | case InitializedEntity::EK_Variable: |
| 5621 | case InitializedEntity::EK_New: |
Douglas Gregor | 6dd3a6a | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 5622 | case InitializedEntity::EK_Exception: |
| 5623 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 5624 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5625 | return Sema::AA_Initializing; |
| 5626 | |
| 5627 | case InitializedEntity::EK_Parameter: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5628 | if (Entity.getDecl() && |
Douglas Gregor | 6b7f12c | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 5629 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 5630 | return Sema::AA_Sending; |
| 5631 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5632 | return Sema::AA_Passing; |
| 5633 | |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 5634 | case InitializedEntity::EK_Parameter_CF_Audited: |
| 5635 | if (Entity.getDecl() && |
| 5636 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 5637 | return Sema::AA_Sending; |
| 5638 | |
| 5639 | return !Diagnose ? Sema::AA_Passing : Sema::AA_Passing_CFAudited; |
| 5640 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5641 | case InitializedEntity::EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 5642 | case InitializedEntity::EK_StmtExprResult: // FIXME: Not quite right. |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5643 | return Sema::AA_Returning; |
| 5644 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5645 | case InitializedEntity::EK_Temporary: |
Fariborz Jahanian | 14e9541 | 2013-07-11 19:13:34 +0000 | [diff] [blame] | 5646 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5647 | // FIXME: Can we tell apart casting vs. converting? |
| 5648 | return Sema::AA_Casting; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5649 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5650 | case InitializedEntity::EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 5651 | case InitializedEntity::EK_Binding: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 5652 | case InitializedEntity::EK_ArrayElement: |
| 5653 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 5654 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 5655 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 5656 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 5657 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5658 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5659 | return Sema::AA_Initializing; |
| 5660 | } |
| 5661 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 5662 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5663 | } |
| 5664 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5665 | /// Whether we should bind a created object as a temporary when |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5666 | /// initializing the given entity. |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5667 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5668 | switch (Entity.getKind()) { |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 5669 | case InitializedEntity::EK_ArrayElement: |
| 5670 | case InitializedEntity::EK_Member: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5671 | case InitializedEntity::EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 5672 | case InitializedEntity::EK_StmtExprResult: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5673 | case InitializedEntity::EK_New: |
| 5674 | case InitializedEntity::EK_Variable: |
| 5675 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 5676 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 5677 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 5678 | case InitializedEntity::EK_ComplexElement: |
Anders Carlsson | fcd764a | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 5679 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 5680 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 5681 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 5682 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5683 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5684 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5685 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5686 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5687 | case InitializedEntity::EK_Parameter_CF_Audited: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5688 | case InitializedEntity::EK_Temporary: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5689 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 5690 | case InitializedEntity::EK_Binding: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5691 | return true; |
| 5692 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5693 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5694 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 5695 | } |
| 5696 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5697 | /// Whether the given entity, when initialized with an object |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5698 | /// created for that initialization, requires destruction. |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5699 | static bool shouldDestroyEntity(const InitializedEntity &Entity) { |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5700 | switch (Entity.getKind()) { |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5701 | case InitializedEntity::EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 5702 | case InitializedEntity::EK_StmtExprResult: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5703 | case InitializedEntity::EK_New: |
| 5704 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 5705 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5706 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 5707 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 5708 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 5709 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 5710 | case InitializedEntity::EK_LambdaCapture: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5711 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5712 | |
Richard Smith | 27874d6 | 2013-01-08 00:08:23 +0000 | [diff] [blame] | 5713 | case InitializedEntity::EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 5714 | case InitializedEntity::EK_Binding: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5715 | case InitializedEntity::EK_Variable: |
| 5716 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5717 | case InitializedEntity::EK_Parameter_CF_Audited: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5718 | case InitializedEntity::EK_Temporary: |
| 5719 | case InitializedEntity::EK_ArrayElement: |
| 5720 | case InitializedEntity::EK_Exception: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5721 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5722 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5723 | return true; |
| 5724 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5725 | |
| 5726 | llvm_unreachable("missed an InitializedEntity kind?"); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5727 | } |
| 5728 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5729 | /// Get the location at which initialization diagnostics should appear. |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5730 | static SourceLocation getInitializationLoc(const InitializedEntity &Entity, |
| 5731 | Expr *Initializer) { |
| 5732 | switch (Entity.getKind()) { |
| 5733 | case InitializedEntity::EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 5734 | case InitializedEntity::EK_StmtExprResult: |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5735 | return Entity.getReturnLoc(); |
| 5736 | |
| 5737 | case InitializedEntity::EK_Exception: |
| 5738 | return Entity.getThrowLoc(); |
| 5739 | |
| 5740 | case InitializedEntity::EK_Variable: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 5741 | case InitializedEntity::EK_Binding: |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5742 | return Entity.getDecl()->getLocation(); |
| 5743 | |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 5744 | case InitializedEntity::EK_LambdaCapture: |
| 5745 | return Entity.getCaptureLoc(); |
| 5746 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5747 | case InitializedEntity::EK_ArrayElement: |
| 5748 | case InitializedEntity::EK_Member: |
| 5749 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5750 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5751 | case InitializedEntity::EK_Temporary: |
| 5752 | case InitializedEntity::EK_New: |
| 5753 | case InitializedEntity::EK_Base: |
| 5754 | case InitializedEntity::EK_Delegating: |
| 5755 | case InitializedEntity::EK_VectorElement: |
| 5756 | case InitializedEntity::EK_ComplexElement: |
| 5757 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 5758 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5759 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5760 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5761 | return Initializer->getLocStart(); |
| 5762 | } |
| 5763 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 5764 | } |
| 5765 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5766 | /// Make a (potentially elidable) temporary copy of the object |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5767 | /// provided by the given initializer by calling the appropriate copy |
| 5768 | /// constructor. |
| 5769 | /// |
| 5770 | /// \param S The Sema object used for type-checking. |
| 5771 | /// |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 5772 | /// \param T The type of the temporary object, which must either be |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5773 | /// the type of the initializer expression or a superclass thereof. |
| 5774 | /// |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5775 | /// \param Entity The entity being initialized. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5776 | /// |
| 5777 | /// \param CurInit The initializer expression. |
| 5778 | /// |
| 5779 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 5780 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 5781 | /// an rvalue. |
| 5782 | /// |
| 5783 | /// \returns An expression that copies the initializer expression into |
| 5784 | /// a temporary object, or an error expression if a copy could not be |
| 5785 | /// created. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5786 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 5787 | QualType T, |
| 5788 | const InitializedEntity &Entity, |
| 5789 | ExprResult CurInit, |
| 5790 | bool IsExtraneousCopy) { |
Fariborz Jahanian | 36f7f13 | 2015-01-28 22:08:10 +0000 | [diff] [blame] | 5791 | if (CurInit.isInvalid()) |
| 5792 | return CurInit; |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5793 | // Determine which class type we're copying to. |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 5794 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5795 | CXXRecordDecl *Class = nullptr; |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5796 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5797 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 5798 | if (!Class) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5799 | return CurInit; |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5800 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5801 | SourceLocation Loc = getInitializationLoc(Entity, CurInit.get()); |
Douglas Gregor | d5c231e | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 5802 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5803 | // Make sure that the type we are copying is complete. |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5804 | if (S.RequireCompleteType(Loc, T, diag::err_temp_copy_incomplete)) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5805 | return CurInit; |
Douglas Gregor | d5c231e | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 5806 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5807 | // Perform overload resolution using the class's constructors. Per |
| 5808 | // C++11 [dcl.init]p16, second bullet for class types, this initialization |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5809 | // is direct-initialization. |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 5810 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5811 | DeclContext::lookup_result Ctors = S.LookupConstructors(Class); |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5812 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5813 | OverloadCandidateSet::iterator Best; |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5814 | switch (ResolveConstructorOverload( |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 5815 | S, Loc, CurInitExpr, CandidateSet, T, Ctors, Best, |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5816 | /*CopyInitializing=*/false, /*AllowExplicit=*/true, |
| 5817 | /*OnlyListConstructors=*/false, /*IsListInit=*/false, |
| 5818 | /*SecondStepOfCopyInit=*/true)) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5819 | case OR_Success: |
| 5820 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5821 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5822 | case OR_No_Viable_Function: |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 5823 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 5824 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 5825 | : diag::err_temp_copy_no_viable) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 5826 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5827 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5828 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 5829 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5830 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5831 | return CurInit; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5832 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5833 | case OR_Ambiguous: |
| 5834 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 5835 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5836 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5837 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5838 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5839 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5840 | case OR_Deleted: |
| 5841 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 5842 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5843 | << CurInitExpr->getSourceRange(); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5844 | S.NoteDeletedFunction(Best->Function); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5845 | return ExprError(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5846 | } |
| 5847 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5848 | bool HadMultipleCandidates = CandidateSet.size() > 1; |
| 5849 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5850 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 5851 | SmallVector<Expr*, 8> ConstructorArgs; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5852 | CurInit.get(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5853 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 5854 | S.CheckConstructorAccess(Loc, Constructor, Best->FoundDecl, Entity, |
| 5855 | IsExtraneousCopy); |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5856 | |
| 5857 | if (IsExtraneousCopy) { |
| 5858 | // If this is a totally extraneous copy for C++03 reference |
| 5859 | // binding purposes, just return the original initialization |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 5860 | // expression. We don't generate an (elided) copy operation here |
| 5861 | // because doing so would require us to pass down a flag to avoid |
| 5862 | // infinite recursion, where each step adds another extraneous, |
| 5863 | // elidable copy. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5864 | |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 5865 | // Instantiate the default arguments of any extra parameters in |
| 5866 | // the selected copy constructor, as if we were going to create a |
| 5867 | // proper call to the copy constructor. |
| 5868 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 5869 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 5870 | if (S.RequireCompleteType(Loc, Parm->getType(), |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5871 | diag::err_call_incomplete_argument)) |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 5872 | break; |
| 5873 | |
| 5874 | // Build the default argument expression; we don't actually care |
| 5875 | // if this succeeds or not, because this routine will complain |
| 5876 | // if there was a problem. |
| 5877 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 5878 | } |
| 5879 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5880 | return CurInitExpr; |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5881 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5882 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5883 | // Determine the arguments required to actually perform the |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5884 | // constructor call (we might have derived-to-base conversions, or |
| 5885 | // the copy constructor may have default arguments). |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5886 | if (S.CompleteConstructorCall(Constructor, CurInitExpr, Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5887 | return ExprError(); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5888 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5889 | // C++0x [class.copy]p32: |
| 5890 | // When certain criteria are met, an implementation is allowed to |
| 5891 | // omit the copy/move construction of a class object, even if the |
| 5892 | // copy/move constructor and/or destructor for the object have |
| 5893 | // side effects. [...] |
| 5894 | // - when a temporary class object that has not been bound to a |
| 5895 | // reference (12.2) would be copied/moved to a class object |
| 5896 | // with the same cv-unqualified type, the copy/move operation |
| 5897 | // can be omitted by constructing the temporary object |
| 5898 | // directly into the target of the omitted copy/move |
| 5899 | // |
| 5900 | // Note that the other three bullets are handled elsewhere. Copy |
| 5901 | // elision for return statements and throw expressions are handled as part |
| 5902 | // of constructor initialization, while copy elision for exception handlers |
| 5903 | // is handled by the run-time. |
| 5904 | // |
| 5905 | // FIXME: If the function parameter is not the same type as the temporary, we |
| 5906 | // should still be able to elide the copy, but we don't have a way to |
| 5907 | // represent in the AST how much should be elided in this case. |
| 5908 | bool Elidable = |
| 5909 | CurInitExpr->isTemporaryObject(S.Context, Class) && |
| 5910 | S.Context.hasSameUnqualifiedType( |
| 5911 | Best->Function->getParamDecl(0)->getType().getNonReferenceType(), |
| 5912 | CurInitExpr->getType()); |
| 5913 | |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 5914 | // Actually perform the constructor call. |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 5915 | CurInit = S.BuildCXXConstructExpr(Loc, T, Best->FoundDecl, Constructor, |
| 5916 | Elidable, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5917 | ConstructorArgs, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5918 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5919 | /*ListInit*/ false, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 5920 | /*StdInitListInit*/ false, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 5921 | /*ZeroInit*/ false, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5922 | CXXConstructExpr::CK_Complete, |
| 5923 | SourceRange()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5924 | |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 5925 | // If we're supposed to bind temporaries, do so. |
| 5926 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5927 | CurInit = S.MaybeBindToTemporary(CurInit.getAs<Expr>()); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5928 | return CurInit; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5929 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5930 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5931 | /// Check whether elidable copy construction for binding a reference to |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5932 | /// a temporary would have succeeded if we were building in C++98 mode, for |
| 5933 | /// -Wc++98-compat. |
| 5934 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 5935 | const InitializedEntity &Entity, |
| 5936 | Expr *CurInitExpr) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5937 | assert(S.getLangOpts().CPlusPlus11); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5938 | |
| 5939 | const RecordType *Record = CurInitExpr->getType()->getAs<RecordType>(); |
| 5940 | if (!Record) |
| 5941 | return; |
| 5942 | |
| 5943 | SourceLocation Loc = getInitializationLoc(Entity, CurInitExpr); |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 5944 | if (S.Diags.isIgnored(diag::warn_cxx98_compat_temp_copy, Loc)) |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5945 | return; |
| 5946 | |
| 5947 | // Find constructors which would have been considered. |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 5948 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5949 | DeclContext::lookup_result Ctors = |
| 5950 | S.LookupConstructors(cast<CXXRecordDecl>(Record->getDecl())); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5951 | |
| 5952 | // Perform overload resolution. |
| 5953 | OverloadCandidateSet::iterator Best; |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5954 | OverloadingResult OR = ResolveConstructorOverload( |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 5955 | S, Loc, CurInitExpr, CandidateSet, CurInitExpr->getType(), Ctors, Best, |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5956 | /*CopyInitializing=*/false, /*AllowExplicit=*/true, |
| 5957 | /*OnlyListConstructors=*/false, /*IsListInit=*/false, |
| 5958 | /*SecondStepOfCopyInit=*/true); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5959 | |
| 5960 | PartialDiagnostic Diag = S.PDiag(diag::warn_cxx98_compat_temp_copy) |
| 5961 | << OR << (int)Entity.getKind() << CurInitExpr->getType() |
| 5962 | << CurInitExpr->getSourceRange(); |
| 5963 | |
| 5964 | switch (OR) { |
| 5965 | case OR_Success: |
| 5966 | S.CheckConstructorAccess(Loc, cast<CXXConstructorDecl>(Best->Function), |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 5967 | Best->FoundDecl, Entity, Diag); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5968 | // FIXME: Check default arguments as far as that's possible. |
| 5969 | break; |
| 5970 | |
| 5971 | case OR_No_Viable_Function: |
| 5972 | S.Diag(Loc, Diag); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5973 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5974 | break; |
| 5975 | |
| 5976 | case OR_Ambiguous: |
| 5977 | S.Diag(Loc, Diag); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5978 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5979 | break; |
| 5980 | |
| 5981 | case OR_Deleted: |
| 5982 | S.Diag(Loc, Diag); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5983 | S.NoteDeletedFunction(Best->Function); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5984 | break; |
| 5985 | } |
| 5986 | } |
| 5987 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5988 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 5989 | const InitializedEntity &Entity) { |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5990 | if (Entity.isParameterKind() && Entity.getDecl()) { |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5991 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 5992 | return; |
| 5993 | |
| 5994 | if (Entity.getDecl()->getDeclName()) |
| 5995 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 5996 | << Entity.getDecl()->getDeclName(); |
| 5997 | else |
| 5998 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 5999 | } |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 6000 | else if (Entity.getKind() == InitializedEntity::EK_RelatedResult && |
| 6001 | Entity.getMethodDecl()) |
| 6002 | S.Diag(Entity.getMethodDecl()->getLocation(), |
| 6003 | diag::note_method_return_type_change) |
| 6004 | << Entity.getMethodDecl()->getDeclName(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6005 | } |
| 6006 | |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 6007 | /// Returns true if the parameters describe a constructor initialization of |
| 6008 | /// an explicit temporary object, e.g. "Point(x, y)". |
| 6009 | static bool isExplicitTemporary(const InitializedEntity &Entity, |
| 6010 | const InitializationKind &Kind, |
| 6011 | unsigned NumArgs) { |
| 6012 | switch (Entity.getKind()) { |
| 6013 | case InitializedEntity::EK_Temporary: |
| 6014 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 6015 | case InitializedEntity::EK_RelatedResult: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 6016 | break; |
| 6017 | default: |
| 6018 | return false; |
| 6019 | } |
| 6020 | |
| 6021 | switch (Kind.getKind()) { |
| 6022 | case InitializationKind::IK_DirectList: |
| 6023 | return true; |
| 6024 | // FIXME: Hack to work around cast weirdness. |
| 6025 | case InitializationKind::IK_Direct: |
| 6026 | case InitializationKind::IK_Value: |
| 6027 | return NumArgs != 1; |
| 6028 | default: |
| 6029 | return false; |
| 6030 | } |
| 6031 | } |
| 6032 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6033 | static ExprResult |
| 6034 | PerformConstructorInitialization(Sema &S, |
| 6035 | const InitializedEntity &Entity, |
| 6036 | const InitializationKind &Kind, |
| 6037 | MultiExprArg Args, |
| 6038 | const InitializationSequence::Step& Step, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6039 | bool &ConstructorInitRequiresZeroInit, |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 6040 | bool IsListInitialization, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6041 | bool IsStdInitListInitialization, |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 6042 | SourceLocation LBraceLoc, |
| 6043 | SourceLocation RBraceLoc) { |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6044 | unsigned NumArgs = Args.size(); |
| 6045 | CXXConstructorDecl *Constructor |
| 6046 | = cast<CXXConstructorDecl>(Step.Function.Function); |
| 6047 | bool HadMultipleCandidates = Step.Function.HadMultipleCandidates; |
| 6048 | |
| 6049 | // Build a call to the selected constructor. |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 6050 | SmallVector<Expr*, 8> ConstructorArgs; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6051 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 6052 | ? Kind.getEqualLoc() |
| 6053 | : Kind.getLocation(); |
| 6054 | |
| 6055 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 6056 | // Force even a trivial, implicit default constructor to be |
| 6057 | // semantically checked. We do this explicitly because we don't build |
| 6058 | // the definition for completely trivial constructors. |
Matt Beaumont-Gay | 47ff122 | 2012-02-24 08:37:56 +0000 | [diff] [blame] | 6059 | assert(Constructor->getParent() && "No parent class for constructor."); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6060 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Douglas Gregor | f704ade | 2012-02-24 07:48:37 +0000 | [diff] [blame] | 6061 | Constructor->isTrivial() && !Constructor->isUsed(false)) |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6062 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 6063 | } |
| 6064 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6065 | ExprResult CurInit((Expr *)nullptr); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6066 | |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6067 | // C++ [over.match.copy]p1: |
| 6068 | // - When initializing a temporary to be bound to the first parameter |
| 6069 | // of a constructor that takes a reference to possibly cv-qualified |
| 6070 | // T as its first argument, called with a single argument in the |
| 6071 | // context of direct-initialization, explicit conversion functions |
| 6072 | // are also considered. |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 6073 | bool AllowExplicitConv = |
| 6074 | Kind.AllowExplicit() && !Kind.isCopyInit() && Args.size() == 1 && |
| 6075 | hasCopyOrMoveCtorParam(S.Context, |
| 6076 | getConstructorInfo(Step.Function.FoundDecl)); |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6077 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6078 | // Determine the arguments required to actually perform the constructor |
| 6079 | // call. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6080 | if (S.CompleteConstructorCall(Constructor, Args, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6081 | Loc, ConstructorArgs, |
Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 6082 | AllowExplicitConv, |
| 6083 | IsListInitialization)) |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6084 | return ExprError(); |
| 6085 | |
| 6086 | |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 6087 | if (isExplicitTemporary(Entity, Kind, NumArgs)) { |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6088 | // An explicitly-constructed temporary, e.g., X(1, 2). |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 6089 | if (S.DiagnoseUseOfDecl(Constructor, Loc)) |
| 6090 | return ExprError(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6091 | |
| 6092 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 6093 | if (!TSInfo) |
| 6094 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 6095 | SourceRange ParenOrBraceRange = Kind.getParenOrBraceRange(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6096 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6097 | if (auto *Shadow = dyn_cast<ConstructorUsingShadowDecl>( |
Richard Smith | 80a4702 | 2016-06-29 01:10:27 +0000 | [diff] [blame] | 6098 | Step.Function.FoundDecl.getDecl())) { |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6099 | Constructor = S.findInheritingConstructor(Loc, Constructor, Shadow); |
Richard Smith | 80a4702 | 2016-06-29 01:10:27 +0000 | [diff] [blame] | 6100 | if (S.DiagnoseUseOfDecl(Constructor, Loc)) |
| 6101 | return ExprError(); |
| 6102 | } |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6103 | S.MarkFunctionReferenced(Loc, Constructor); |
| 6104 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6105 | CurInit = new (S.Context) CXXTemporaryObjectExpr( |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 6106 | S.Context, Constructor, |
| 6107 | Entity.getType().getNonLValueExprType(S.Context), TSInfo, |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 6108 | ConstructorArgs, ParenOrBraceRange, HadMultipleCandidates, |
| 6109 | IsListInitialization, IsStdInitListInitialization, |
| 6110 | ConstructorInitRequiresZeroInit); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6111 | } else { |
| 6112 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 6113 | CXXConstructExpr::CK_Complete; |
| 6114 | |
| 6115 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 6116 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
| 6117 | CXXConstructExpr::CK_VirtualBase : |
| 6118 | CXXConstructExpr::CK_NonVirtualBase; |
| 6119 | } else if (Entity.getKind() == InitializedEntity::EK_Delegating) { |
| 6120 | ConstructKind = CXXConstructExpr::CK_Delegating; |
| 6121 | } |
| 6122 | |
Peter Collingbourne | b8d17e7 | 2014-02-22 02:59:41 +0000 | [diff] [blame] | 6123 | // Only get the parenthesis or brace range if it is a list initialization or |
| 6124 | // direct construction. |
| 6125 | SourceRange ParenOrBraceRange; |
| 6126 | if (IsListInitialization) |
| 6127 | ParenOrBraceRange = SourceRange(LBraceLoc, RBraceLoc); |
| 6128 | else if (Kind.getKind() == InitializationKind::IK_Direct) |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 6129 | ParenOrBraceRange = Kind.getParenOrBraceRange(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6130 | |
| 6131 | // If the entity allows NRVO, mark the construction as elidable |
| 6132 | // unconditionally. |
| 6133 | if (Entity.allowsNRVO()) |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 6134 | CurInit = S.BuildCXXConstructExpr(Loc, Step.Type, |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 6135 | Step.Function.FoundDecl, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6136 | Constructor, /*Elidable=*/true, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6137 | ConstructorArgs, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6138 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6139 | IsListInitialization, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6140 | IsStdInitListInitialization, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6141 | ConstructorInitRequiresZeroInit, |
| 6142 | ConstructKind, |
Peter Collingbourne | b8d17e7 | 2014-02-22 02:59:41 +0000 | [diff] [blame] | 6143 | ParenOrBraceRange); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6144 | else |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 6145 | CurInit = S.BuildCXXConstructExpr(Loc, Step.Type, |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 6146 | Step.Function.FoundDecl, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6147 | Constructor, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6148 | ConstructorArgs, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6149 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6150 | IsListInitialization, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6151 | IsStdInitListInitialization, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6152 | ConstructorInitRequiresZeroInit, |
| 6153 | ConstructKind, |
Peter Collingbourne | b8d17e7 | 2014-02-22 02:59:41 +0000 | [diff] [blame] | 6154 | ParenOrBraceRange); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6155 | } |
| 6156 | if (CurInit.isInvalid()) |
| 6157 | return ExprError(); |
| 6158 | |
| 6159 | // Only check access if all of that succeeded. |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6160 | S.CheckConstructorAccess(Loc, Constructor, Step.Function.FoundDecl, Entity); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 6161 | if (S.DiagnoseUseOfDecl(Step.Function.FoundDecl, Loc)) |
| 6162 | return ExprError(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6163 | |
| 6164 | if (shouldBindAsTemporary(Entity)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6165 | CurInit = S.MaybeBindToTemporary(CurInit.get()); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6166 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6167 | return CurInit; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6168 | } |
| 6169 | |
Richard Smith | eb3cad5 | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 6170 | /// Determine whether the specified InitializedEntity definitely has a lifetime |
| 6171 | /// longer than the current full-expression. Conservatively returns false if |
| 6172 | /// it's unclear. |
| 6173 | static bool |
| 6174 | InitializedEntityOutlivesFullExpression(const InitializedEntity &Entity) { |
| 6175 | const InitializedEntity *Top = &Entity; |
| 6176 | while (Top->getParent()) |
| 6177 | Top = Top->getParent(); |
| 6178 | |
| 6179 | switch (Top->getKind()) { |
| 6180 | case InitializedEntity::EK_Variable: |
| 6181 | case InitializedEntity::EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 6182 | case InitializedEntity::EK_StmtExprResult: |
Richard Smith | eb3cad5 | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 6183 | case InitializedEntity::EK_Exception: |
| 6184 | case InitializedEntity::EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 6185 | case InitializedEntity::EK_Binding: |
Richard Smith | eb3cad5 | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 6186 | case InitializedEntity::EK_New: |
| 6187 | case InitializedEntity::EK_Base: |
| 6188 | case InitializedEntity::EK_Delegating: |
| 6189 | return true; |
| 6190 | |
| 6191 | case InitializedEntity::EK_ArrayElement: |
| 6192 | case InitializedEntity::EK_VectorElement: |
| 6193 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 6194 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Richard Smith | eb3cad5 | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 6195 | case InitializedEntity::EK_ComplexElement: |
| 6196 | // Could not determine what the full initialization is. Assume it might not |
| 6197 | // outlive the full-expression. |
| 6198 | return false; |
| 6199 | |
| 6200 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6201 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | eb3cad5 | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 6202 | case InitializedEntity::EK_Temporary: |
| 6203 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 6204 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 6205 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | eb3cad5 | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 6206 | // The entity being initialized might not outlive the full-expression. |
| 6207 | return false; |
| 6208 | } |
| 6209 | |
| 6210 | llvm_unreachable("unknown entity kind"); |
| 6211 | } |
| 6212 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6213 | namespace { |
| 6214 | enum LifetimeKind { |
| 6215 | /// The lifetime of a temporary bound to this entity ends at the end of the |
| 6216 | /// full-expression, and that's (probably) fine. |
| 6217 | LK_FullExpression, |
| 6218 | |
| 6219 | /// The lifetime of a temporary bound to this entity is extended to the |
| 6220 | /// lifeitme of the entity itself. |
| 6221 | LK_Extended, |
| 6222 | |
| 6223 | /// The lifetime of a temporary bound to this entity probably ends too soon, |
| 6224 | /// because the entity is allocated in a new-expression. |
| 6225 | LK_New, |
| 6226 | |
| 6227 | /// The lifetime of a temporary bound to this entity ends too soon, because |
| 6228 | /// the entity is a return object. |
| 6229 | LK_Return, |
| 6230 | |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 6231 | /// The lifetime of a temporary bound to this entity ends too soon, because |
| 6232 | /// the entity is the result of a statement expression. |
| 6233 | LK_StmtExprResult, |
| 6234 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6235 | /// This is a mem-initializer: if it would extend a temporary (other than via |
| 6236 | /// a default member initializer), the program is ill-formed. |
| 6237 | LK_MemInitializer, |
| 6238 | }; |
| 6239 | using LifetimeResult = |
| 6240 | llvm::PointerIntPair<const InitializedEntity *, 3, LifetimeKind>; |
| 6241 | } |
| 6242 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6243 | /// Determine the declaration which an initialized entity ultimately refers to, |
| 6244 | /// for the purpose of lifetime-extending a temporary bound to a reference in |
| 6245 | /// the initialization of \p Entity. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6246 | static LifetimeResult getEntityLifetime( |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 6247 | const InitializedEntity *Entity, |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6248 | const InitializedEntity *InitField = nullptr) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6249 | // C++11 [class.temporary]p5: |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 6250 | switch (Entity->getKind()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6251 | case InitializedEntity::EK_Variable: |
| 6252 | // The temporary [...] persists for the lifetime of the reference |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6253 | return {Entity, LK_Extended}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6254 | |
| 6255 | case InitializedEntity::EK_Member: |
| 6256 | // For subobjects, we look at the complete object. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 6257 | if (Entity->getParent()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6258 | return getEntityLifetime(Entity->getParent(), Entity); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6259 | |
| 6260 | // except: |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6261 | // C++17 [class.base.init]p8: |
| 6262 | // A temporary expression bound to a reference member in a |
| 6263 | // mem-initializer is ill-formed. |
| 6264 | // C++17 [class.base.init]p11: |
| 6265 | // A temporary expression bound to a reference member from a |
| 6266 | // default member initializer is ill-formed. |
| 6267 | // |
| 6268 | // The context of p11 and its example suggest that it's only the use of a |
| 6269 | // default member initializer from a constructor that makes the program |
| 6270 | // ill-formed, not its mere existence, and that it can even be used by |
| 6271 | // aggregate initialization. |
| 6272 | return {Entity, Entity->isDefaultMemberInitializer() ? LK_Extended |
| 6273 | : LK_MemInitializer}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6274 | |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 6275 | case InitializedEntity::EK_Binding: |
Richard Smith | 3997b1b | 2016-08-12 01:55:21 +0000 | [diff] [blame] | 6276 | // Per [dcl.decomp]p3, the binding is treated as a variable of reference |
| 6277 | // type. |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6278 | return {Entity, LK_Extended}; |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 6279 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6280 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6281 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6282 | // -- A temporary bound to a reference parameter in a function call |
| 6283 | // persists until the completion of the full-expression containing |
| 6284 | // the call. |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6285 | return {nullptr, LK_FullExpression}; |
| 6286 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6287 | case InitializedEntity::EK_Result: |
| 6288 | // -- The lifetime of a temporary bound to the returned value in a |
| 6289 | // function return statement is not extended; the temporary is |
| 6290 | // destroyed at the end of the full-expression in the return statement. |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6291 | return {nullptr, LK_Return}; |
| 6292 | |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 6293 | case InitializedEntity::EK_StmtExprResult: |
| 6294 | // FIXME: Should we lifetime-extend through the result of a statement |
| 6295 | // expression? |
| 6296 | return {nullptr, LK_StmtExprResult}; |
| 6297 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6298 | case InitializedEntity::EK_New: |
| 6299 | // -- A temporary bound to a reference in a new-initializer persists |
| 6300 | // until the completion of the full-expression containing the |
| 6301 | // new-initializer. |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6302 | return {nullptr, LK_New}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6303 | |
| 6304 | case InitializedEntity::EK_Temporary: |
| 6305 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 6306 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6307 | // We don't yet know the storage duration of the surrounding temporary. |
| 6308 | // Assume it's got full-expression duration for now, it will patch up our |
| 6309 | // storage duration if that's not correct. |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6310 | return {nullptr, LK_FullExpression}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6311 | |
| 6312 | case InitializedEntity::EK_ArrayElement: |
| 6313 | // For subobjects, we look at the complete object. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6314 | return getEntityLifetime(Entity->getParent(), InitField); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6315 | |
| 6316 | case InitializedEntity::EK_Base: |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 6317 | // For subobjects, we look at the complete object. |
| 6318 | if (Entity->getParent()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6319 | return getEntityLifetime(Entity->getParent(), InitField); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6320 | return {InitField, LK_MemInitializer}; |
| 6321 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6322 | case InitializedEntity::EK_Delegating: |
| 6323 | // We can reach this case for aggregate initialization in a constructor: |
| 6324 | // struct A { int &&r; }; |
| 6325 | // struct B : A { B() : A{0} {} }; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6326 | // In this case, use the outermost field decl as the context. |
| 6327 | return {InitField, LK_MemInitializer}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6328 | |
| 6329 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 6330 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6331 | case InitializedEntity::EK_LambdaCapture: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6332 | case InitializedEntity::EK_VectorElement: |
| 6333 | case InitializedEntity::EK_ComplexElement: |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6334 | return {nullptr, LK_FullExpression}; |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6335 | |
| 6336 | case InitializedEntity::EK_Exception: |
| 6337 | // FIXME: Can we diagnose lifetime problems with exceptions? |
| 6338 | return {nullptr, LK_FullExpression}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6339 | } |
Benjamin Kramer | cabc882 | 2013-06-05 15:37:50 +0000 | [diff] [blame] | 6340 | llvm_unreachable("unknown entity kind"); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6341 | } |
| 6342 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6343 | namespace { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6344 | enum ReferenceKind { |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6345 | /// Lifetime would be extended by a reference binding to a temporary. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6346 | RK_ReferenceBinding, |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6347 | /// Lifetime would be extended by a std::initializer_list object binding to |
| 6348 | /// its backing array. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6349 | RK_StdInitializerList, |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6350 | }; |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6351 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6352 | /// A temporary or local variable. This will be one of: |
| 6353 | /// * A MaterializeTemporaryExpr. |
| 6354 | /// * A DeclRefExpr whose declaration is a local. |
| 6355 | /// * An AddrLabelExpr. |
| 6356 | /// * A BlockExpr for a block with captures. |
| 6357 | using Local = Expr*; |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6358 | |
| 6359 | /// Expressions we stepped over when looking for the local state. Any steps |
| 6360 | /// that would inhibit lifetime extension or take us out of subexpressions of |
| 6361 | /// the initializer are included. |
| 6362 | struct IndirectLocalPathEntry { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6363 | enum EntryKind { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6364 | DefaultInit, |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame^] | 6365 | Conditional, |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6366 | AddressOf, |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6367 | VarInit, |
| 6368 | LValToRVal, |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6369 | } Kind; |
| 6370 | Expr *E; |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6371 | Decl *D = nullptr; |
| 6372 | IndirectLocalPathEntry() {} |
| 6373 | IndirectLocalPathEntry(EntryKind K, Expr *E) : Kind(K), E(E) {} |
| 6374 | IndirectLocalPathEntry(EntryKind K, Expr *E, Decl *D) : Kind(K), E(E), D(D) {} |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6375 | }; |
| 6376 | |
| 6377 | using IndirectLocalPath = llvm::SmallVectorImpl<IndirectLocalPathEntry>; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6378 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6379 | struct RevertToOldSizeRAII { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6380 | IndirectLocalPath &Path; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6381 | unsigned OldSize = Path.size(); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6382 | RevertToOldSizeRAII(IndirectLocalPath &Path) : Path(Path) {} |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6383 | ~RevertToOldSizeRAII() { Path.resize(OldSize); } |
| 6384 | }; |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6385 | |
| 6386 | using LocalVisitor = llvm::function_ref<bool(IndirectLocalPath &Path, Local L, |
| 6387 | ReferenceKind RK)>; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6388 | } |
| 6389 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6390 | static bool isVarOnPath(IndirectLocalPath &Path, VarDecl *VD) { |
| 6391 | for (auto E : Path) |
| 6392 | if (E.Kind == IndirectLocalPathEntry::VarInit && E.D == VD) |
| 6393 | return true; |
| 6394 | return false; |
| 6395 | } |
| 6396 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6397 | static void visitLocalsRetainedByInitializer(IndirectLocalPath &Path, |
| 6398 | Expr *Init, LocalVisitor Visit, |
| 6399 | bool RevisitSubinits); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6400 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6401 | /// Visit the locals that would be reachable through a reference bound to the |
| 6402 | /// glvalue expression \c Init. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6403 | static void visitLocalsRetainedByReferenceBinding(IndirectLocalPath &Path, |
| 6404 | Expr *Init, ReferenceKind RK, |
| 6405 | LocalVisitor Visit) { |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6406 | RevertToOldSizeRAII RAII(Path); |
| 6407 | |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 6408 | // Walk past any constructs which we can lifetime-extend across. |
| 6409 | Expr *Old; |
| 6410 | do { |
| 6411 | Old = Init; |
| 6412 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6413 | if (auto *EWC = dyn_cast<ExprWithCleanups>(Init)) |
| 6414 | Init = EWC->getSubExpr(); |
| 6415 | |
Richard Smith | dbc8249 | 2015-01-10 01:28:13 +0000 | [diff] [blame] | 6416 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6417 | // If this is just redundant braces around an initializer, step over it. |
| 6418 | if (ILE->isTransparent()) |
Richard Smith | dbc8249 | 2015-01-10 01:28:13 +0000 | [diff] [blame] | 6419 | Init = ILE->getInit(0); |
Richard Smith | dbc8249 | 2015-01-10 01:28:13 +0000 | [diff] [blame] | 6420 | } |
| 6421 | |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 6422 | // Step over any subobject adjustments; we may have a materialized |
| 6423 | // temporary inside them. |
Richard Smith | 4baaa5a | 2016-12-03 01:14:32 +0000 | [diff] [blame] | 6424 | Init = const_cast<Expr *>(Init->skipRValueSubobjectAdjustments()); |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 6425 | |
| 6426 | // Per current approach for DR1376, look through casts to reference type |
| 6427 | // when performing lifetime extension. |
| 6428 | if (CastExpr *CE = dyn_cast<CastExpr>(Init)) |
| 6429 | if (CE->getSubExpr()->isGLValue()) |
| 6430 | Init = CE->getSubExpr(); |
| 6431 | |
Richard Smith | b3189a1 | 2016-12-05 07:49:14 +0000 | [diff] [blame] | 6432 | // Per the current approach for DR1299, look through array element access |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6433 | // on array glvalues when performing lifetime extension. |
| 6434 | if (auto *ASE = dyn_cast<ArraySubscriptExpr>(Init)) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6435 | Init = ASE->getBase(); |
| 6436 | auto *ICE = dyn_cast<ImplicitCastExpr>(Init); |
| 6437 | if (ICE && ICE->getCastKind() == CK_ArrayToPointerDecay) |
| 6438 | Init = ICE->getSubExpr(); |
| 6439 | else |
| 6440 | // We can't lifetime extend through this but we might still find some |
| 6441 | // retained temporaries. |
| 6442 | return visitLocalsRetainedByInitializer(Path, Init, Visit, true); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6443 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6444 | |
| 6445 | // Step into CXXDefaultInitExprs so we can diagnose cases where a |
| 6446 | // constructor inherits one as an implicit mem-initializer. |
| 6447 | if (auto *DIE = dyn_cast<CXXDefaultInitExpr>(Init)) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6448 | Path.push_back( |
| 6449 | {IndirectLocalPathEntry::DefaultInit, DIE, DIE->getField()}); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6450 | Init = DIE->getExpr(); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6451 | } |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 6452 | } while (Init != Old); |
| 6453 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6454 | if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(Init)) { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6455 | if (Visit(Path, Local(MTE), RK)) |
| 6456 | visitLocalsRetainedByInitializer(Path, MTE->GetTemporaryExpr(), Visit, |
| 6457 | true); |
| 6458 | } |
| 6459 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6460 | switch (Init->getStmtClass()) { |
| 6461 | case Stmt::DeclRefExprClass: { |
| 6462 | // If we find the name of a local non-reference parameter, we could have a |
| 6463 | // lifetime problem. |
| 6464 | auto *DRE = cast<DeclRefExpr>(Init); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6465 | auto *VD = dyn_cast<VarDecl>(DRE->getDecl()); |
| 6466 | if (VD && VD->hasLocalStorage() && |
| 6467 | !DRE->refersToEnclosingVariableOrCapture()) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6468 | if (!VD->getType()->isReferenceType()) { |
| 6469 | Visit(Path, Local(DRE), RK); |
| 6470 | } else if (isa<ParmVarDecl>(DRE->getDecl())) { |
| 6471 | // The lifetime of a reference parameter is unknown; assume it's OK |
| 6472 | // for now. |
| 6473 | break; |
| 6474 | } else if (VD->getInit() && !isVarOnPath(Path, VD)) { |
| 6475 | Path.push_back({IndirectLocalPathEntry::VarInit, DRE, VD}); |
| 6476 | visitLocalsRetainedByReferenceBinding(Path, VD->getInit(), |
| 6477 | RK_ReferenceBinding, Visit); |
| 6478 | } |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6479 | } |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6480 | break; |
| 6481 | } |
| 6482 | |
| 6483 | case Stmt::UnaryOperatorClass: { |
| 6484 | // The only unary operator that make sense to handle here |
| 6485 | // is Deref. All others don't resolve to a "name." This includes |
| 6486 | // handling all sorts of rvalues passed to a unary operator. |
| 6487 | const UnaryOperator *U = cast<UnaryOperator>(Init); |
| 6488 | if (U->getOpcode() == UO_Deref) |
| 6489 | visitLocalsRetainedByInitializer(Path, U->getSubExpr(), Visit, true); |
| 6490 | break; |
| 6491 | } |
| 6492 | |
| 6493 | case Stmt::OMPArraySectionExprClass: { |
| 6494 | visitLocalsRetainedByInitializer( |
| 6495 | Path, cast<OMPArraySectionExpr>(Init)->getBase(), Visit, true); |
| 6496 | break; |
| 6497 | } |
| 6498 | |
| 6499 | case Stmt::ConditionalOperatorClass: |
| 6500 | case Stmt::BinaryConditionalOperatorClass: { |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame^] | 6501 | Path.push_back({IndirectLocalPathEntry::Conditional, Init}); |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6502 | auto *C = cast<AbstractConditionalOperator>(Init); |
| 6503 | if (!C->getTrueExpr()->getType()->isVoidType()) |
| 6504 | visitLocalsRetainedByReferenceBinding(Path, C->getTrueExpr(), RK, Visit); |
| 6505 | if (!C->getFalseExpr()->getType()->isVoidType()) |
| 6506 | visitLocalsRetainedByReferenceBinding(Path, C->getFalseExpr(), RK, Visit); |
| 6507 | break; |
| 6508 | } |
| 6509 | |
| 6510 | // FIXME: Visit the left-hand side of an -> or ->*. |
| 6511 | |
| 6512 | default: |
| 6513 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6514 | } |
| 6515 | } |
| 6516 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6517 | /// Visit the locals that would be reachable through an object initialized by |
| 6518 | /// the prvalue expression \c Init. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6519 | static void visitLocalsRetainedByInitializer(IndirectLocalPath &Path, |
| 6520 | Expr *Init, LocalVisitor Visit, |
| 6521 | bool RevisitSubinits) { |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6522 | RevertToOldSizeRAII RAII(Path); |
| 6523 | |
| 6524 | // Step into CXXDefaultInitExprs so we can diagnose cases where a |
| 6525 | // constructor inherits one as an implicit mem-initializer. |
| 6526 | if (auto *DIE = dyn_cast<CXXDefaultInitExpr>(Init)) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6527 | Path.push_back({IndirectLocalPathEntry::DefaultInit, DIE, DIE->getField()}); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6528 | Init = DIE->getExpr(); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6529 | } |
| 6530 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6531 | if (auto *EWC = dyn_cast<ExprWithCleanups>(Init)) |
| 6532 | Init = EWC->getSubExpr(); |
| 6533 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6534 | // Dig out the expression which constructs the extended temporary. |
Richard Smith | 4baaa5a | 2016-12-03 01:14:32 +0000 | [diff] [blame] | 6535 | Init = const_cast<Expr *>(Init->skipRValueSubobjectAdjustments()); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6536 | |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 6537 | if (CXXBindTemporaryExpr *BTE = dyn_cast<CXXBindTemporaryExpr>(Init)) |
| 6538 | Init = BTE->getSubExpr(); |
| 6539 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6540 | // C++17 [dcl.init.list]p6: |
| 6541 | // initializing an initializer_list object from the array extends the |
| 6542 | // lifetime of the array exactly like binding a reference to a temporary. |
| 6543 | if (auto *ILE = dyn_cast<CXXStdInitializerListExpr>(Init)) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6544 | return visitLocalsRetainedByReferenceBinding(Path, ILE->getSubExpr(), |
| 6545 | RK_StdInitializerList, Visit); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6546 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6547 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6548 | // We already visited the elements of this initializer list while |
| 6549 | // performing the initialization. Don't visit them again unless we've |
| 6550 | // changed the lifetime of the initialized entity. |
| 6551 | if (!RevisitSubinits) |
| 6552 | return; |
| 6553 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6554 | if (ILE->isTransparent()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6555 | return visitLocalsRetainedByInitializer(Path, ILE->getInit(0), Visit, |
| 6556 | RevisitSubinits); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6557 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6558 | if (ILE->getType()->isArrayType()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6559 | for (unsigned I = 0, N = ILE->getNumInits(); I != N; ++I) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6560 | visitLocalsRetainedByInitializer(Path, ILE->getInit(I), Visit, |
| 6561 | RevisitSubinits); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6562 | return; |
| 6563 | } |
| 6564 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6565 | if (CXXRecordDecl *RD = ILE->getType()->getAsCXXRecordDecl()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6566 | assert(RD->isAggregate() && "aggregate init on non-aggregate"); |
| 6567 | |
| 6568 | // If we lifetime-extend a braced initializer which is initializing an |
| 6569 | // aggregate, and that aggregate contains reference members which are |
| 6570 | // bound to temporaries, those temporaries are also lifetime-extended. |
| 6571 | if (RD->isUnion() && ILE->getInitializedFieldInUnion() && |
| 6572 | ILE->getInitializedFieldInUnion()->getType()->isReferenceType()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6573 | visitLocalsRetainedByReferenceBinding(Path, ILE->getInit(0), |
| 6574 | RK_ReferenceBinding, Visit); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6575 | else { |
| 6576 | unsigned Index = 0; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6577 | for (const auto *I : RD->fields()) { |
Richard Smith | 0bca59d | 2013-07-01 06:08:20 +0000 | [diff] [blame] | 6578 | if (Index >= ILE->getNumInits()) |
| 6579 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6580 | if (I->isUnnamedBitfield()) |
| 6581 | continue; |
Richard Smith | 8d7f11d | 2013-06-27 22:54:33 +0000 | [diff] [blame] | 6582 | Expr *SubInit = ILE->getInit(Index); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6583 | if (I->getType()->isReferenceType()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6584 | visitLocalsRetainedByReferenceBinding(Path, SubInit, |
| 6585 | RK_ReferenceBinding, Visit); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6586 | else |
| 6587 | // This might be either aggregate-initialization of a member or |
| 6588 | // initialization of a std::initializer_list object. Regardless, |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6589 | // we should recursively lifetime-extend that initializer. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6590 | visitLocalsRetainedByInitializer(Path, SubInit, Visit, |
| 6591 | RevisitSubinits); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6592 | ++Index; |
| 6593 | } |
| 6594 | } |
| 6595 | } |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6596 | return; |
| 6597 | } |
| 6598 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6599 | // Step over value-preserving rvalue casts. |
| 6600 | while (auto *CE = dyn_cast<CastExpr>(Init)) { |
| 6601 | switch (CE->getCastKind()) { |
| 6602 | case CK_LValueToRValue: |
| 6603 | // If we can match the lvalue to a const object, we can look at its |
| 6604 | // initializer. |
| 6605 | Path.push_back({IndirectLocalPathEntry::LValToRVal, CE}); |
| 6606 | return visitLocalsRetainedByReferenceBinding( |
| 6607 | Path, Init, RK_ReferenceBinding, |
| 6608 | [&](IndirectLocalPath &Path, Local L, ReferenceKind RK) -> bool { |
| 6609 | if (auto *DRE = dyn_cast<DeclRefExpr>(L)) { |
| 6610 | auto *VD = dyn_cast<VarDecl>(DRE->getDecl()); |
| 6611 | if (VD && VD->getType().isConstQualified() && VD->getInit()) { |
| 6612 | Path.push_back({IndirectLocalPathEntry::VarInit, DRE, VD}); |
| 6613 | visitLocalsRetainedByInitializer(Path, VD->getInit(), Visit, true); |
| 6614 | } |
| 6615 | } else if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(L)) { |
| 6616 | if (MTE->getType().isConstQualified()) |
| 6617 | visitLocalsRetainedByInitializer(Path, MTE->GetTemporaryExpr(), |
| 6618 | Visit, true); |
| 6619 | } |
| 6620 | return false; |
| 6621 | }); |
| 6622 | |
| 6623 | // We assume that objects can be retained by pointers cast to integers, |
| 6624 | // but not if the integer is cast to floating-point type or to _Complex. |
| 6625 | // We assume that casts to 'bool' do not preserve enough information to |
| 6626 | // retain a local object. |
| 6627 | case CK_NoOp: |
| 6628 | case CK_BitCast: |
| 6629 | case CK_BaseToDerived: |
| 6630 | case CK_DerivedToBase: |
| 6631 | case CK_UncheckedDerivedToBase: |
| 6632 | case CK_Dynamic: |
| 6633 | case CK_ToUnion: |
| 6634 | case CK_IntegralToPointer: |
| 6635 | case CK_PointerToIntegral: |
| 6636 | case CK_VectorSplat: |
| 6637 | case CK_IntegralCast: |
| 6638 | case CK_CPointerToObjCPointerCast: |
| 6639 | case CK_BlockPointerToObjCPointerCast: |
| 6640 | case CK_AnyPointerToBlockPointerCast: |
| 6641 | case CK_AddressSpaceConversion: |
| 6642 | break; |
| 6643 | |
| 6644 | case CK_ArrayToPointerDecay: |
| 6645 | // Model array-to-pointer decay as taking the address of the array |
| 6646 | // lvalue. |
| 6647 | Path.push_back({IndirectLocalPathEntry::AddressOf, CE}); |
| 6648 | return visitLocalsRetainedByReferenceBinding(Path, CE->getSubExpr(), |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6649 | RK_ReferenceBinding, Visit); |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6650 | |
| 6651 | default: |
| 6652 | return; |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6653 | } |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6654 | |
| 6655 | Init = CE->getSubExpr(); |
| 6656 | } |
| 6657 | |
| 6658 | Init = Init->IgnoreParens(); |
| 6659 | switch (Init->getStmtClass()) { |
| 6660 | case Stmt::UnaryOperatorClass: { |
| 6661 | auto *UO = cast<UnaryOperator>(Init); |
| 6662 | // If the initializer is the address of a local, we could have a lifetime |
| 6663 | // problem. |
| 6664 | if (UO->getOpcode() == UO_AddrOf) { |
| 6665 | Path.push_back({IndirectLocalPathEntry::AddressOf, UO}); |
| 6666 | visitLocalsRetainedByReferenceBinding(Path, UO->getSubExpr(), |
| 6667 | RK_ReferenceBinding, Visit); |
| 6668 | } |
| 6669 | break; |
| 6670 | } |
| 6671 | |
| 6672 | case Stmt::BinaryOperatorClass: { |
| 6673 | // Handle pointer arithmetic. |
| 6674 | auto *BO = cast<BinaryOperator>(Init); |
| 6675 | BinaryOperatorKind BOK = BO->getOpcode(); |
| 6676 | if (!BO->getType()->isPointerType() || (BOK != BO_Add && BOK != BO_Sub)) |
| 6677 | break; |
| 6678 | |
| 6679 | if (BO->getLHS()->getType()->isPointerType()) |
| 6680 | visitLocalsRetainedByInitializer(Path, BO->getLHS(), Visit, true); |
| 6681 | else if (BO->getRHS()->getType()->isPointerType()) |
| 6682 | visitLocalsRetainedByInitializer(Path, BO->getRHS(), Visit, true); |
| 6683 | break; |
| 6684 | } |
| 6685 | |
| 6686 | case Stmt::ConditionalOperatorClass: |
| 6687 | case Stmt::BinaryConditionalOperatorClass: { |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame^] | 6688 | Path.push_back({IndirectLocalPathEntry::Conditional, Init}); |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6689 | auto *C = cast<AbstractConditionalOperator>(Init); |
| 6690 | // In C++, we can have a throw-expression operand, which has 'void' type |
| 6691 | // and isn't interesting from a lifetime perspective. |
| 6692 | if (!C->getTrueExpr()->getType()->isVoidType()) |
| 6693 | visitLocalsRetainedByInitializer(Path, C->getTrueExpr(), Visit, true); |
| 6694 | if (!C->getFalseExpr()->getType()->isVoidType()) |
| 6695 | visitLocalsRetainedByInitializer(Path, C->getFalseExpr(), Visit, true); |
| 6696 | break; |
| 6697 | } |
| 6698 | |
| 6699 | case Stmt::BlockExprClass: |
| 6700 | if (cast<BlockExpr>(Init)->getBlockDecl()->hasCaptures()) { |
| 6701 | // This is a local block, whose lifetime is that of the function. |
| 6702 | Visit(Path, Local(cast<BlockExpr>(Init)), RK_ReferenceBinding); |
| 6703 | } |
| 6704 | break; |
| 6705 | |
| 6706 | case Stmt::AddrLabelExprClass: |
| 6707 | // We want to warn if the address of a label would escape the function. |
| 6708 | Visit(Path, Local(cast<AddrLabelExpr>(Init)), RK_ReferenceBinding); |
| 6709 | break; |
| 6710 | |
| 6711 | default: |
| 6712 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6713 | } |
| 6714 | } |
| 6715 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6716 | /// Determine whether this is an indirect path to a temporary that we are |
| 6717 | /// supposed to lifetime-extend along (but don't). |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6718 | static bool shouldLifetimeExtendThroughPath(const IndirectLocalPath &Path) { |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6719 | for (auto Elem : Path) { |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame^] | 6720 | if (Elem.Kind != IndirectLocalPathEntry::DefaultInit && |
| 6721 | Elem.Kind != IndirectLocalPathEntry::Conditional) |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6722 | return false; |
| 6723 | } |
| 6724 | return true; |
| 6725 | } |
| 6726 | |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame^] | 6727 | /// Find the range for the first interesting entry in the path at or after I. |
| 6728 | static SourceRange nextPathEntryRange(const IndirectLocalPath &Path, unsigned I, |
| 6729 | Expr *E) { |
| 6730 | for (unsigned N = Path.size(); I != N; ++I) { |
| 6731 | switch (Path[I].Kind) { |
| 6732 | case IndirectLocalPathEntry::AddressOf: |
| 6733 | case IndirectLocalPathEntry::LValToRVal: |
| 6734 | case IndirectLocalPathEntry::Conditional: |
| 6735 | // These exist primarily to mark the path as not permitting or |
| 6736 | // supporting lifetime extension. |
| 6737 | break; |
| 6738 | |
| 6739 | case IndirectLocalPathEntry::DefaultInit: |
| 6740 | case IndirectLocalPathEntry::VarInit: |
| 6741 | return Path[I].E->getSourceRange(); |
| 6742 | } |
| 6743 | } |
| 6744 | return E->getSourceRange(); |
| 6745 | } |
| 6746 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6747 | void Sema::checkInitializerLifetime(const InitializedEntity &Entity, |
| 6748 | Expr *Init) { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6749 | LifetimeResult LR = getEntityLifetime(&Entity); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6750 | LifetimeKind LK = LR.getInt(); |
| 6751 | const InitializedEntity *ExtendingEntity = LR.getPointer(); |
| 6752 | |
| 6753 | // If this entity doesn't have an interesting lifetime, don't bother looking |
| 6754 | // for temporaries within its initializer. |
| 6755 | if (LK == LK_FullExpression) |
| 6756 | return; |
| 6757 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6758 | auto TemporaryVisitor = [&](IndirectLocalPath &Path, Local L, |
| 6759 | ReferenceKind RK) -> bool { |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame^] | 6760 | SourceRange DiagRange = nextPathEntryRange(Path, 0, L); |
| 6761 | SourceLocation DiagLoc = DiagRange.getBegin(); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6762 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6763 | switch (LK) { |
| 6764 | case LK_FullExpression: |
| 6765 | llvm_unreachable("already handled this"); |
| 6766 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6767 | case LK_Extended: { |
| 6768 | auto *MTE = dyn_cast<MaterializeTemporaryExpr>(L); |
| 6769 | if (!MTE) |
| 6770 | // FIXME: Warn on this. |
| 6771 | return false; |
| 6772 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6773 | // Lifetime-extend the temporary. |
| 6774 | if (Path.empty()) { |
| 6775 | // Update the storage duration of the materialized temporary. |
| 6776 | // FIXME: Rebuild the expression instead of mutating it. |
| 6777 | MTE->setExtendingDecl(ExtendingEntity->getDecl(), |
| 6778 | ExtendingEntity->allocateManglingNumber()); |
| 6779 | // Also visit the temporaries lifetime-extended by this initializer. |
| 6780 | return true; |
| 6781 | } |
| 6782 | |
| 6783 | if (shouldLifetimeExtendThroughPath(Path)) { |
| 6784 | // We're supposed to lifetime-extend the temporary along this path (per |
| 6785 | // the resolution of DR1815), but we don't support that yet. |
| 6786 | // |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame^] | 6787 | // FIXME: Properly handle these situations. |
| 6788 | // For the default member initializer case, perhaps the easiest approach |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6789 | // would be to clone the initializer expression on each use that would |
| 6790 | // lifetime extend its temporaries. |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame^] | 6791 | Diag(DiagLoc, RK == RK_ReferenceBinding |
| 6792 | ? diag::warn_unsupported_temporary_not_extended |
| 6793 | : diag::warn_unsupported_init_list_not_extended) |
| 6794 | << (Path.front().Kind == IndirectLocalPathEntry::Conditional) |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6795 | << DiagRange; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6796 | } else { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6797 | // FIXME: Warn on this. |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6798 | return false; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6799 | } |
| 6800 | break; |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6801 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6802 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6803 | case LK_MemInitializer: { |
| 6804 | if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(L)) { |
| 6805 | // Under C++ DR1696, if a mem-initializer (or a default member |
| 6806 | // initializer used by the absence of one) would lifetime-extend a |
| 6807 | // temporary, the program is ill-formed. |
| 6808 | if (auto *ExtendingDecl = |
| 6809 | ExtendingEntity ? ExtendingEntity->getDecl() : nullptr) { |
| 6810 | bool IsSubobjectMember = ExtendingEntity != &Entity; |
| 6811 | Diag(DiagLoc, diag::err_bind_ref_member_to_temporary) |
| 6812 | << ExtendingDecl << IsSubobjectMember << RK << DiagRange; |
| 6813 | // Don't bother adding a note pointing to the field if we're inside |
| 6814 | // its default member initializer; our primary diagnostic points to |
| 6815 | // the same place in that case. |
| 6816 | if (Path.empty() || |
| 6817 | Path.back().Kind != IndirectLocalPathEntry::DefaultInit) { |
| 6818 | Diag(ExtendingDecl->getLocation(), |
| 6819 | diag::note_lifetime_extending_member_declared_here) |
| 6820 | << RK << IsSubobjectMember; |
| 6821 | } |
| 6822 | } else { |
| 6823 | // We have a mem-initializer but no particular field within it; this |
| 6824 | // is either a base class or a delegating initializer directly |
| 6825 | // initializing the base-class from something that doesn't live long |
| 6826 | // enough. |
| 6827 | // |
| 6828 | // FIXME: Warn on this. |
| 6829 | return false; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6830 | } |
| 6831 | } else { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6832 | // Paths via a default initializer can only occur during error recovery |
| 6833 | // (there's no other way that a default initializer can refer to a |
| 6834 | // local). Don't produce a bogus warning on those cases. |
| 6835 | if (std::any_of(Path.begin(), Path.end(), [](IndirectLocalPathEntry E) { |
| 6836 | return E.Kind == IndirectLocalPathEntry::DefaultInit; |
| 6837 | })) |
| 6838 | return false; |
| 6839 | |
| 6840 | auto *DRE = dyn_cast<DeclRefExpr>(L); |
| 6841 | auto *VD = DRE ? dyn_cast<VarDecl>(DRE->getDecl()) : nullptr; |
| 6842 | if (!VD) { |
| 6843 | // A member was initialized to a local block. |
| 6844 | // FIXME: Warn on this. |
| 6845 | return false; |
| 6846 | } |
| 6847 | |
| 6848 | if (auto *Member = |
| 6849 | ExtendingEntity ? ExtendingEntity->getDecl() : nullptr) { |
| 6850 | bool IsPointer = Member->getType()->isAnyPointerType(); |
| 6851 | Diag(DiagLoc, IsPointer ? diag::warn_init_ptr_member_to_parameter_addr |
| 6852 | : diag::warn_bind_ref_member_to_parameter) |
| 6853 | << Member << VD << isa<ParmVarDecl>(VD) << DiagRange; |
| 6854 | Diag(Member->getLocation(), |
| 6855 | diag::note_ref_or_ptr_member_declared_here) |
| 6856 | << (unsigned)IsPointer; |
| 6857 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6858 | } |
| 6859 | break; |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6860 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6861 | |
| 6862 | case LK_New: |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6863 | if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(L)) { |
| 6864 | Diag(DiagLoc, RK == RK_ReferenceBinding |
| 6865 | ? diag::warn_new_dangling_reference |
| 6866 | : diag::warn_new_dangling_initializer_list) |
| 6867 | << (ExtendingEntity != &Entity) << DiagRange; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6868 | } else { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6869 | // We can't determine if the allocation outlives the local declaration. |
| 6870 | return false; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6871 | } |
| 6872 | break; |
| 6873 | |
| 6874 | case LK_Return: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 6875 | case LK_StmtExprResult: |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6876 | if (auto *DRE = dyn_cast<DeclRefExpr>(L)) { |
| 6877 | // We can't determine if the local variable outlives the statement |
| 6878 | // expression. |
| 6879 | if (LK == LK_StmtExprResult) |
| 6880 | return false; |
| 6881 | Diag(DiagLoc, diag::warn_ret_stack_addr_ref) |
| 6882 | << Entity.getType()->isReferenceType() << DRE->getDecl() |
| 6883 | << isa<ParmVarDecl>(DRE->getDecl()) << DiagRange; |
| 6884 | } else if (isa<BlockExpr>(L)) { |
| 6885 | Diag(DiagLoc, diag::err_ret_local_block) << DiagRange; |
| 6886 | } else if (isa<AddrLabelExpr>(L)) { |
| 6887 | Diag(DiagLoc, diag::warn_ret_addr_label) << DiagRange; |
| 6888 | } else { |
| 6889 | Diag(DiagLoc, diag::warn_ret_local_temp_addr_ref) |
| 6890 | << Entity.getType()->isReferenceType() << DiagRange; |
| 6891 | } |
| 6892 | break; |
Florian Hahn | 0aa117d | 2018-07-17 09:23:31 +0000 | [diff] [blame] | 6893 | } |
| 6894 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6895 | for (unsigned I = 0; I != Path.size(); ++I) { |
| 6896 | auto Elem = Path[I]; |
| 6897 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6898 | switch (Elem.Kind) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6899 | case IndirectLocalPathEntry::AddressOf: |
| 6900 | case IndirectLocalPathEntry::LValToRVal: |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame^] | 6901 | case IndirectLocalPathEntry::Conditional: |
| 6902 | // These exist primarily to mark the path as not permitting or |
| 6903 | // supporting lifetime extension. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6904 | break; |
| 6905 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6906 | case IndirectLocalPathEntry::DefaultInit: { |
| 6907 | auto *FD = cast<FieldDecl>(Elem.D); |
| 6908 | Diag(FD->getLocation(), diag::note_init_with_default_member_initalizer) |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame^] | 6909 | << FD << nextPathEntryRange(Path, I + 1, L); |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6910 | break; |
| 6911 | } |
| 6912 | |
| 6913 | case IndirectLocalPathEntry::VarInit: |
| 6914 | const VarDecl *VD = cast<VarDecl>(Elem.D); |
| 6915 | Diag(VD->getLocation(), diag::note_local_var_initializer) |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame^] | 6916 | << VD->getType()->isReferenceType() << VD->getDeclName() |
| 6917 | << nextPathEntryRange(Path, I + 1, L); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6918 | break; |
Florian Hahn | 0aa117d | 2018-07-17 09:23:31 +0000 | [diff] [blame] | 6919 | } |
| 6920 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6921 | |
| 6922 | // We didn't lifetime-extend, so don't go any further; we don't need more |
| 6923 | // warnings or errors on inner temporaries within this one's initializer. |
| 6924 | return false; |
| 6925 | }; |
| 6926 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6927 | llvm::SmallVector<IndirectLocalPathEntry, 8> Path; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6928 | if (Init->isGLValue()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6929 | visitLocalsRetainedByReferenceBinding(Path, Init, RK_ReferenceBinding, |
| 6930 | TemporaryVisitor); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6931 | else |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6932 | visitLocalsRetainedByInitializer(Path, Init, TemporaryVisitor, false); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6933 | } |
| 6934 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 6935 | static void DiagnoseNarrowingInInitList(Sema &S, |
| 6936 | const ImplicitConversionSequence &ICS, |
| 6937 | QualType PreNarrowingType, |
| 6938 | QualType EntityType, |
| 6939 | const Expr *PostInit); |
| 6940 | |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 6941 | /// Provide warnings when std::move is used on construction. |
| 6942 | static void CheckMoveOnConstruction(Sema &S, const Expr *InitExpr, |
| 6943 | bool IsReturnStmt) { |
| 6944 | if (!InitExpr) |
| 6945 | return; |
| 6946 | |
Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 6947 | if (S.inTemplateInstantiation()) |
Richard Trieu | 6093d14 | 2015-07-29 17:03:34 +0000 | [diff] [blame] | 6948 | return; |
| 6949 | |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 6950 | QualType DestType = InitExpr->getType(); |
| 6951 | if (!DestType->isRecordType()) |
| 6952 | return; |
| 6953 | |
| 6954 | unsigned DiagID = 0; |
| 6955 | if (IsReturnStmt) { |
| 6956 | const CXXConstructExpr *CCE = |
| 6957 | dyn_cast<CXXConstructExpr>(InitExpr->IgnoreParens()); |
| 6958 | if (!CCE || CCE->getNumArgs() != 1) |
| 6959 | return; |
| 6960 | |
| 6961 | if (!CCE->getConstructor()->isCopyOrMoveConstructor()) |
| 6962 | return; |
| 6963 | |
| 6964 | InitExpr = CCE->getArg(0)->IgnoreImpCasts(); |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 6965 | } |
| 6966 | |
| 6967 | // Find the std::move call and get the argument. |
| 6968 | const CallExpr *CE = dyn_cast<CallExpr>(InitExpr->IgnoreParens()); |
Nico Weber | 192184c | 2018-06-20 15:57:38 +0000 | [diff] [blame] | 6969 | if (!CE || !CE->isCallToStdMove()) |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 6970 | return; |
| 6971 | |
| 6972 | const Expr *Arg = CE->getArg(0)->IgnoreImplicit(); |
| 6973 | |
| 6974 | if (IsReturnStmt) { |
| 6975 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg->IgnoreParenImpCasts()); |
| 6976 | if (!DRE || DRE->refersToEnclosingVariableOrCapture()) |
| 6977 | return; |
| 6978 | |
| 6979 | const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()); |
| 6980 | if (!VD || !VD->hasLocalStorage()) |
| 6981 | return; |
| 6982 | |
Alex Lorenz | bbe51d8 | 2017-11-07 21:40:11 +0000 | [diff] [blame] | 6983 | // __block variables are not moved implicitly. |
| 6984 | if (VD->hasAttr<BlocksAttr>()) |
| 6985 | return; |
| 6986 | |
Richard Trieu | 8d4006a | 2015-07-28 19:06:16 +0000 | [diff] [blame] | 6987 | QualType SourceType = VD->getType(); |
| 6988 | if (!SourceType->isRecordType()) |
Richard Trieu | 1d4911bc | 2015-05-18 19:54:08 +0000 | [diff] [blame] | 6989 | return; |
| 6990 | |
Richard Trieu | 8d4006a | 2015-07-28 19:06:16 +0000 | [diff] [blame] | 6991 | if (!S.Context.hasSameUnqualifiedType(DestType, SourceType)) { |
Richard Trieu | 1993dc8 | 2015-07-29 23:47:19 +0000 | [diff] [blame] | 6992 | return; |
Richard Trieu | 8d4006a | 2015-07-28 19:06:16 +0000 | [diff] [blame] | 6993 | } |
| 6994 | |
Davide Italiano | 7842c3f | 2015-07-18 01:15:19 +0000 | [diff] [blame] | 6995 | // If we're returning a function parameter, copy elision |
| 6996 | // is not possible. |
| 6997 | if (isa<ParmVarDecl>(VD)) |
| 6998 | DiagID = diag::warn_redundant_move_on_return; |
Richard Trieu | 1993dc8 | 2015-07-29 23:47:19 +0000 | [diff] [blame] | 6999 | else |
| 7000 | DiagID = diag::warn_pessimizing_move_on_return; |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7001 | } else { |
| 7002 | DiagID = diag::warn_pessimizing_move_on_initialization; |
| 7003 | const Expr *ArgStripped = Arg->IgnoreImplicit()->IgnoreParens(); |
| 7004 | if (!ArgStripped->isRValue() || !ArgStripped->getType()->isRecordType()) |
| 7005 | return; |
| 7006 | } |
| 7007 | |
| 7008 | S.Diag(CE->getLocStart(), DiagID); |
| 7009 | |
| 7010 | // Get all the locations for a fix-it. Don't emit the fix-it if any location |
| 7011 | // is within a macro. |
| 7012 | SourceLocation CallBegin = CE->getCallee()->getLocStart(); |
| 7013 | if (CallBegin.isMacroID()) |
| 7014 | return; |
| 7015 | SourceLocation RParen = CE->getRParenLoc(); |
| 7016 | if (RParen.isMacroID()) |
| 7017 | return; |
| 7018 | SourceLocation LParen; |
| 7019 | SourceLocation ArgLoc = Arg->getLocStart(); |
| 7020 | |
| 7021 | // Special testing for the argument location. Since the fix-it needs the |
| 7022 | // location right before the argument, the argument location can be in a |
| 7023 | // macro only if it is at the beginning of the macro. |
| 7024 | while (ArgLoc.isMacroID() && |
| 7025 | S.getSourceManager().isAtStartOfImmediateMacroExpansion(ArgLoc)) { |
Richard Smith | b5f8171 | 2018-04-30 05:25:48 +0000 | [diff] [blame] | 7026 | ArgLoc = S.getSourceManager().getImmediateExpansionRange(ArgLoc).getBegin(); |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7027 | } |
| 7028 | |
| 7029 | if (LParen.isMacroID()) |
| 7030 | return; |
| 7031 | |
| 7032 | LParen = ArgLoc.getLocWithOffset(-1); |
| 7033 | |
| 7034 | S.Diag(CE->getLocStart(), diag::note_remove_move) |
| 7035 | << FixItHint::CreateRemoval(SourceRange(CallBegin, LParen)) |
| 7036 | << FixItHint::CreateRemoval(SourceRange(RParen, RParen)); |
| 7037 | } |
| 7038 | |
Nick Lewycky | 2eeddfb | 2016-05-14 17:44:14 +0000 | [diff] [blame] | 7039 | static void CheckForNullPointerDereference(Sema &S, const Expr *E) { |
| 7040 | // Check to see if we are dereferencing a null pointer. If so, this is |
| 7041 | // undefined behavior, so warn about it. This only handles the pattern |
| 7042 | // "*null", which is a very syntactic check. |
| 7043 | if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E->IgnoreParenCasts())) |
| 7044 | if (UO->getOpcode() == UO_Deref && |
| 7045 | UO->getSubExpr()->IgnoreParenCasts()-> |
| 7046 | isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull)) { |
| 7047 | S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
| 7048 | S.PDiag(diag::warn_binding_null_to_reference) |
| 7049 | << UO->getSubExpr()->getSourceRange()); |
| 7050 | } |
| 7051 | } |
| 7052 | |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 7053 | MaterializeTemporaryExpr * |
| 7054 | Sema::CreateMaterializeTemporaryExpr(QualType T, Expr *Temporary, |
| 7055 | bool BoundToLvalueReference) { |
| 7056 | auto MTE = new (Context) |
| 7057 | MaterializeTemporaryExpr(T, Temporary, BoundToLvalueReference); |
| 7058 | |
| 7059 | // Order an ExprWithCleanups for lifetime marks. |
| 7060 | // |
| 7061 | // TODO: It'll be good to have a single place to check the access of the |
| 7062 | // destructor and generate ExprWithCleanups for various uses. Currently these |
| 7063 | // are done in both CreateMaterializeTemporaryExpr and MaybeBindToTemporary, |
| 7064 | // but there may be a chance to merge them. |
| 7065 | Cleanup.setExprNeedsCleanups(false); |
| 7066 | return MTE; |
| 7067 | } |
| 7068 | |
Richard Smith | 4baaa5a | 2016-12-03 01:14:32 +0000 | [diff] [blame] | 7069 | ExprResult Sema::TemporaryMaterializationConversion(Expr *E) { |
| 7070 | // In C++98, we don't want to implicitly create an xvalue. |
| 7071 | // FIXME: This means that AST consumers need to deal with "prvalues" that |
| 7072 | // denote materialized temporaries. Maybe we should add another ValueKind |
| 7073 | // for "xvalue pretending to be a prvalue" for C++98 support. |
| 7074 | if (!E->isRValue() || !getLangOpts().CPlusPlus11) |
| 7075 | return E; |
| 7076 | |
| 7077 | // C++1z [conv.rval]/1: T shall be a complete type. |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7078 | // FIXME: Does this ever matter (can we form a prvalue of incomplete type)? |
| 7079 | // 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] | 7080 | QualType T = E->getType(); |
| 7081 | if (RequireCompleteType(E->getExprLoc(), T, diag::err_incomplete_type)) |
| 7082 | return ExprError(); |
| 7083 | |
| 7084 | return CreateMaterializeTemporaryExpr(E->getType(), E, false); |
| 7085 | } |
| 7086 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7087 | ExprResult |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7088 | InitializationSequence::Perform(Sema &S, |
| 7089 | const InitializedEntity &Entity, |
| 7090 | const InitializationKind &Kind, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7091 | MultiExprArg Args, |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7092 | QualType *ResultType) { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 7093 | if (Failed()) { |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 7094 | Diagnose(S, Entity, Kind, Args); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7095 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7096 | } |
Nico Weber | 337d5aa | 2015-04-17 08:32:38 +0000 | [diff] [blame] | 7097 | if (!ZeroInitializationFixit.empty()) { |
| 7098 | unsigned DiagID = diag::err_default_init_const; |
| 7099 | if (Decl *D = Entity.getDecl()) |
| 7100 | if (S.getLangOpts().MSVCCompat && D->hasAttr<SelectAnyAttr>()) |
| 7101 | DiagID = diag::ext_default_init_const; |
| 7102 | |
| 7103 | // The initialization would have succeeded with this fixit. Since the fixit |
| 7104 | // is on the error, we need to build a valid AST in this case, so this isn't |
| 7105 | // handled in the Failed() branch above. |
| 7106 | QualType DestType = Entity.getType(); |
| 7107 | S.Diag(Kind.getLocation(), DiagID) |
| 7108 | << DestType << (bool)DestType->getAs<RecordType>() |
| 7109 | << FixItHint::CreateInsertion(ZeroInitializationFixitLoc, |
| 7110 | ZeroInitializationFixit); |
| 7111 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7112 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 7113 | if (getKind() == DependentSequence) { |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7114 | // If the declaration is a non-dependent, incomplete array type |
| 7115 | // that has an initializer, then its type will be completed once |
| 7116 | // the initializer is instantiated. |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7117 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7118 | Args.size() == 1) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7119 | QualType DeclType = Entity.getType(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7120 | if (const IncompleteArrayType *ArrayT |
| 7121 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 7122 | // FIXME: We don't currently have the ability to accurately |
| 7123 | // compute the length of an initializer list without |
| 7124 | // performing full type-checking of the initializer list |
| 7125 | // (since we have to determine where braces are implicitly |
| 7126 | // introduced and such). So, we fall back to making the array |
| 7127 | // type a dependently-sized array type with no specified |
| 7128 | // bound. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7129 | if (isa<InitListExpr>((Expr *)Args[0])) { |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7130 | SourceRange Brackets; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7131 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7132 | // Scavange the location of the brackets from the entity, if we can. |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 7133 | if (auto *DD = dyn_cast_or_null<DeclaratorDecl>(Entity.getDecl())) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7134 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 7135 | TypeLoc TL = TInfo->getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7136 | if (IncompleteArrayTypeLoc ArrayLoc = |
| 7137 | TL.getAs<IncompleteArrayTypeLoc>()) |
| 7138 | Brackets = ArrayLoc.getBracketsRange(); |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7139 | } |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7140 | } |
| 7141 | |
| 7142 | *ResultType |
| 7143 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7144 | /*NumElts=*/nullptr, |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7145 | ArrayT->getSizeModifier(), |
| 7146 | ArrayT->getIndexTypeCVRQualifiers(), |
| 7147 | Brackets); |
| 7148 | } |
| 7149 | |
| 7150 | } |
| 7151 | } |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 7152 | if (Kind.getKind() == InitializationKind::IK_Direct && |
| 7153 | !Kind.isExplicitCast()) { |
| 7154 | // Rebuild the ParenListExpr. |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 7155 | SourceRange ParenRange = Kind.getParenOrBraceRange(); |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 7156 | return S.ActOnParenListExpr(ParenRange.getBegin(), ParenRange.getEnd(), |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7157 | Args); |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 7158 | } |
Manuel Klimek | f2b4b69 | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 7159 | assert(Kind.getKind() == InitializationKind::IK_Copy || |
Douglas Gregor | bf13895 | 2012-04-04 04:06:51 +0000 | [diff] [blame] | 7160 | Kind.isExplicitCast() || |
| 7161 | Kind.getKind() == InitializationKind::IK_DirectList); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7162 | return ExprResult(Args[0]); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7163 | } |
| 7164 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 7165 | // No steps means no initialization. |
| 7166 | if (Steps.empty()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7167 | return ExprResult((Expr *)nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7168 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7169 | if (S.getLangOpts().CPlusPlus11 && Entity.getType()->isReferenceType() && |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7170 | Args.size() == 1 && isa<InitListExpr>(Args[0]) && |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 7171 | !Entity.isParameterKind()) { |
Richard Smith | 2b349ae | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 7172 | // Produce a C++98 compatibility warning if we are initializing a reference |
| 7173 | // from an initializer list. For parameters, we produce a better warning |
| 7174 | // elsewhere. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7175 | Expr *Init = Args[0]; |
Richard Smith | 2b349ae | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 7176 | S.Diag(Init->getLocStart(), diag::warn_cxx98_compat_reference_list_init) |
| 7177 | << Init->getSourceRange(); |
| 7178 | } |
| 7179 | |
Egor Churaev | 3bccec5 | 2017-04-05 12:47:10 +0000 | [diff] [blame] | 7180 | // OpenCL v2.0 s6.13.11.1. atomic variables can be initialized in global scope |
| 7181 | QualType ETy = Entity.getType(); |
| 7182 | Qualifiers TyQualifiers = ETy.getQualifiers(); |
| 7183 | bool HasGlobalAS = TyQualifiers.hasAddressSpace() && |
| 7184 | TyQualifiers.getAddressSpace() == LangAS::opencl_global; |
| 7185 | |
| 7186 | if (S.getLangOpts().OpenCLVersion >= 200 && |
| 7187 | ETy->isAtomicType() && !HasGlobalAS && |
| 7188 | Entity.getKind() == InitializedEntity::EK_Variable && Args.size() > 0) { |
| 7189 | S.Diag(Args[0]->getLocStart(), diag::err_opencl_atomic_init) << 1 << |
| 7190 | SourceRange(Entity.getDecl()->getLocStart(), Args[0]->getLocEnd()); |
| 7191 | return ExprError(); |
| 7192 | } |
| 7193 | |
Richard Smith | eb3cad5 | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 7194 | // Diagnose cases where we initialize a pointer to an array temporary, and the |
| 7195 | // pointer obviously outlives the temporary. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7196 | // FIXME: Fold this into checkInitializerLifetime. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7197 | if (Args.size() == 1 && Args[0]->getType()->isArrayType() && |
Richard Smith | eb3cad5 | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 7198 | Entity.getType()->isPointerType() && |
| 7199 | InitializedEntityOutlivesFullExpression(Entity)) { |
Richard Smith | 4baaa5a | 2016-12-03 01:14:32 +0000 | [diff] [blame] | 7200 | const Expr *Init = Args[0]->skipRValueSubobjectAdjustments(); |
| 7201 | if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(Init)) |
| 7202 | Init = MTE->GetTemporaryExpr(); |
Richard Smith | eb3cad5 | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 7203 | Expr::LValueClassification Kind = Init->ClassifyLValue(S.Context); |
| 7204 | if (Kind == Expr::LV_ClassTemporary || Kind == Expr::LV_ArrayTemporary) |
| 7205 | S.Diag(Init->getLocStart(), diag::warn_temporary_array_to_pointer_decay) |
| 7206 | << Init->getSourceRange(); |
| 7207 | } |
| 7208 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7209 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 7210 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 7211 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 7212 | // and we want latter when it makes sense. |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7213 | if (ResultType) |
Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 7214 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7215 | Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7216 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7217 | ExprResult CurInit((Expr *)nullptr); |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7218 | SmallVector<Expr*, 4> ArrayLoopCommonExprs; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7219 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7220 | // For initialization steps that start with a single initializer, |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 7221 | // grab the only argument out the Args and place it into the "current" |
| 7222 | // initializer. |
| 7223 | switch (Steps.front().Kind) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7224 | case SK_ResolveAddressOfOverloadedFunction: |
| 7225 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7226 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7227 | case SK_CastDerivedToBaseLValue: |
| 7228 | case SK_BindReference: |
| 7229 | case SK_BindReferenceToTemporary: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7230 | case SK_FinalCopy: |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 7231 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7232 | case SK_UserConversion: |
| 7233 | case SK_QualificationConversionLValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7234 | case SK_QualificationConversionXValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7235 | case SK_QualificationConversionRValue: |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 7236 | case SK_AtomicConversion: |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 7237 | case SK_LValueToRValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7238 | case SK_ConversionSequence: |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7239 | case SK_ConversionSequenceNoNarrowing: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7240 | case SK_ListInitialization: |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7241 | case SK_UnwrapInitList: |
| 7242 | case SK_RewrapInitList: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7243 | case SK_CAssignment: |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 7244 | case SK_StringInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7245 | case SK_ObjCObjectConversion: |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7246 | case SK_ArrayLoopIndex: |
| 7247 | case SK_ArrayLoopInit: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7248 | case SK_ArrayInit: |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 7249 | case SK_GNUArrayInit: |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 7250 | case SK_ParenthesizedArrayInit: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7251 | case SK_PassByIndirectCopyRestore: |
| 7252 | case SK_PassByIndirectRestore: |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 7253 | case SK_ProduceObjCObject: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7254 | case SK_StdInitializerList: |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 7255 | case SK_OCLSamplerInit: |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 7256 | case SK_OCLZeroEvent: |
| 7257 | case SK_OCLZeroQueue: { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7258 | assert(Args.size() == 1); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7259 | CurInit = Args[0]; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7260 | if (!CurInit.get()) return ExprError(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7261 | break; |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7262 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7263 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7264 | case SK_ConstructorInitialization: |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7265 | case SK_ConstructorInitializationFromList: |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 7266 | case SK_StdInitializerListConstructorCall: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7267 | case SK_ZeroInitialization: |
| 7268 | break; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7269 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7270 | |
Richard Smith | d6a1508 | 2017-01-07 00:48:55 +0000 | [diff] [blame] | 7271 | // Promote from an unevaluated context to an unevaluated list context in |
| 7272 | // C++11 list-initialization; we need to instantiate entities usable in |
| 7273 | // constant expressions here in order to perform narrowing checks =( |
| 7274 | EnterExpressionEvaluationContext Evaluated( |
| 7275 | S, EnterExpressionEvaluationContext::InitList, |
| 7276 | CurInit.get() && isa<InitListExpr>(CurInit.get())); |
| 7277 | |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7278 | // C++ [class.abstract]p2: |
| 7279 | // no objects of an abstract class can be created except as subobjects |
| 7280 | // of a class derived from it |
| 7281 | auto checkAbstractType = [&](QualType T) -> bool { |
| 7282 | if (Entity.getKind() == InitializedEntity::EK_Base || |
| 7283 | Entity.getKind() == InitializedEntity::EK_Delegating) |
| 7284 | return false; |
| 7285 | return S.RequireNonAbstractType(Kind.getLocation(), T, |
| 7286 | diag::err_allocation_of_abstract_type); |
| 7287 | }; |
| 7288 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7289 | // Walk through the computed steps for the initialization sequence, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7290 | // performing the specified conversions along the way. |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7291 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7292 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 7293 | Step != StepEnd; ++Step) { |
| 7294 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7295 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7296 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7297 | QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7298 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7299 | switch (Step->Kind) { |
| 7300 | case SK_ResolveAddressOfOverloadedFunction: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7301 | // Overload resolution determined which function invoke; update the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7302 | // initializer to reflect that choice. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7303 | S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 7304 | if (S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation())) |
| 7305 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7306 | CurInit = S.FixOverloadedFunctionReference(CurInit, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7307 | Step->Function.FoundDecl, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7308 | Step->Function.Function); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7309 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7310 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7311 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7312 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7313 | case SK_CastDerivedToBaseLValue: { |
| 7314 | // We have a derived-to-base cast that produces either an rvalue or an |
| 7315 | // lvalue. Perform that cast. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7316 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 7317 | CXXCastPath BasePath; |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 7318 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7319 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 7320 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
| 7321 | if (S.CheckDerivedToBaseConversion(SourceType, Step->Type, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7322 | CurInit.get()->getLocStart(), |
| 7323 | CurInit.get()->getSourceRange(), |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 7324 | &BasePath, IgnoreBaseAccess)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7325 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7326 | |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7327 | ExprValueKind VK = |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7328 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7329 | VK_LValue : |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7330 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7331 | VK_XValue : |
| 7332 | VK_RValue); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7333 | CurInit = |
| 7334 | ImplicitCastExpr::Create(S.Context, Step->Type, CK_DerivedToBase, |
| 7335 | CurInit.get(), &BasePath, VK); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7336 | break; |
| 7337 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7338 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7339 | case SK_BindReference: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7340 | // Reference binding does not have any corresponding ASTs. |
| 7341 | |
| 7342 | // Check exception specifications |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7343 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7344 | return ExprError(); |
Anders Carlsson | ab0ddb5 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 7345 | |
George Burgess IV | cfd48d9 | 2017-04-13 23:47:08 +0000 | [diff] [blame] | 7346 | // We don't check for e.g. function pointers here, since address |
| 7347 | // availability checks should only occur when the function first decays |
| 7348 | // into a pointer or reference. |
| 7349 | if (CurInit.get()->getType()->isFunctionProtoType()) { |
| 7350 | if (auto *DRE = dyn_cast<DeclRefExpr>(CurInit.get()->IgnoreParens())) { |
| 7351 | if (auto *FD = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
| 7352 | if (!S.checkAddressOfFunctionIsAvailable(FD, /*Complain=*/true, |
| 7353 | DRE->getLocStart())) |
| 7354 | return ExprError(); |
| 7355 | } |
| 7356 | } |
| 7357 | } |
| 7358 | |
Nick Lewycky | 2eeddfb | 2016-05-14 17:44:14 +0000 | [diff] [blame] | 7359 | CheckForNullPointerDereference(S, CurInit.get()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7360 | break; |
Anders Carlsson | ab0ddb5 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 7361 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 7362 | case SK_BindReferenceToTemporary: { |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 7363 | // Make sure the "temporary" is actually an rvalue. |
| 7364 | assert(CurInit.get()->isRValue() && "not a temporary"); |
| 7365 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7366 | // Check exception specifications |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7367 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7368 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7369 | |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 7370 | // Materialize the temporary into memory. |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 7371 | MaterializeTemporaryExpr *MTE = S.CreateMaterializeTemporaryExpr( |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7372 | Step->Type, CurInit.get(), Entity.getType()->isLValueReferenceType()); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7373 | CurInit = MTE; |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 7374 | |
Brian Kelley | 762f928 | 2017-03-29 18:16:38 +0000 | [diff] [blame] | 7375 | // If we're extending this temporary to automatic storage duration -- we |
| 7376 | // need to register its cleanup during the full-expression's cleanups. |
| 7377 | if (MTE->getStorageDuration() == SD_Automatic && |
| 7378 | MTE->getType().isDestructedType()) |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 7379 | S.Cleanup.setExprNeedsCleanups(true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7380 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 7381 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7382 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7383 | case SK_FinalCopy: |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7384 | if (checkAbstractType(Step->Type)) |
| 7385 | return ExprError(); |
| 7386 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7387 | // If the overall initialization is initializing a temporary, we already |
| 7388 | // bound our argument if it was necessary to do so. If not (if we're |
| 7389 | // ultimately initializing a non-temporary), our argument needs to be |
| 7390 | // bound since it's initializing a function parameter. |
| 7391 | // FIXME: This is a mess. Rationalize temporary destruction. |
| 7392 | if (!shouldBindAsTemporary(Entity)) |
| 7393 | CurInit = S.MaybeBindToTemporary(CurInit.get()); |
| 7394 | CurInit = CopyObject(S, Step->Type, Entity, CurInit, |
| 7395 | /*IsExtraneousCopy=*/false); |
| 7396 | break; |
| 7397 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 7398 | case SK_ExtraneousCopyToTemporary: |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7399 | CurInit = CopyObject(S, Step->Type, Entity, CurInit, |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 7400 | /*IsExtraneousCopy=*/true); |
| 7401 | break; |
| 7402 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7403 | case SK_UserConversion: { |
| 7404 | // We have a user-defined conversion that invokes either a constructor |
| 7405 | // or a conversion function. |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 7406 | CastKind CastKind; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7407 | FunctionDecl *Fn = Step->Function.Function; |
| 7408 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 7409 | bool HadMultipleCandidates = Step->Function.HadMultipleCandidates; |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 7410 | bool CreatedObject = false; |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 7411 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7412 | // Build a call to the selected constructor. |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 7413 | SmallVector<Expr*, 8> ConstructorArgs; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7414 | SourceLocation Loc = CurInit.get()->getLocStart(); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 7415 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7416 | // Determine the arguments required to actually perform the constructor |
| 7417 | // call. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7418 | Expr *Arg = CurInit.get(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7419 | if (S.CompleteConstructorCall(Constructor, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7420 | MultiExprArg(&Arg, 1), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7421 | Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7422 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7423 | |
Richard Smith | b24f067 | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 7424 | // Build an expression that constructs a temporary. |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 7425 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, |
| 7426 | FoundFn, Constructor, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7427 | ConstructorArgs, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 7428 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 7429 | /*ListInit*/ false, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 7430 | /*StdInitListInit*/ false, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 7431 | /*ZeroInit*/ false, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 7432 | CXXConstructExpr::CK_Complete, |
| 7433 | SourceRange()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7434 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7435 | return ExprError(); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 7436 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 7437 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, FoundFn, |
| 7438 | Entity); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 7439 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 7440 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7441 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7442 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 7443 | CreatedObject = true; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7444 | } else { |
| 7445 | // Build a call to the conversion function. |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 7446 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7447 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), nullptr, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7448 | FoundFn); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 7449 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 7450 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7451 | |
| 7452 | // FIXME: Should we move this initialization into a separate |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7453 | // derived-to-base conversion? I believe the answer is "no", because |
| 7454 | // we don't want to turn off access control here for c-style casts. |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7455 | CurInit = S.PerformObjectArgumentInitialization(CurInit.get(), |
| 7456 | /*Qualifier=*/nullptr, |
| 7457 | FoundFn, Conversion); |
| 7458 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7459 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7460 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7461 | // Build the actual call to the conversion function. |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 7462 | CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion, |
| 7463 | HadMultipleCandidates); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7464 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7465 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7466 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7467 | CastKind = CK_UserDefinedConversion; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7468 | CreatedObject = Conversion->getReturnType()->isRecordType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7469 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7470 | |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7471 | if (CreatedObject && checkAbstractType(CurInit.get()->getType())) |
| 7472 | return ExprError(); |
| 7473 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7474 | CurInit = ImplicitCastExpr::Create(S.Context, CurInit.get()->getType(), |
| 7475 | CastKind, CurInit.get(), nullptr, |
| 7476 | CurInit.get()->getValueKind()); |
Abramo Bagnara | b0cf297 | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 7477 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7478 | if (shouldBindAsTemporary(Entity)) |
| 7479 | // The overall entity is temporary, so this expression should be |
| 7480 | // destroyed at the end of its full-expression. |
| 7481 | CurInit = S.MaybeBindToTemporary(CurInit.getAs<Expr>()); |
| 7482 | else if (CreatedObject && shouldDestroyEntity(Entity)) { |
| 7483 | // The object outlasts the full-expression, but we need to prepare for |
| 7484 | // a destructor being run on it. |
| 7485 | // FIXME: It makes no sense to do this here. This should happen |
| 7486 | // regardless of how we initialized the entity. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7487 | QualType T = CurInit.get()->getType(); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 7488 | if (const RecordType *Record = T->getAs<RecordType>()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7489 | CXXDestructorDecl *Destructor |
Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 7490 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7491 | S.CheckDestructorAccess(CurInit.get()->getLocStart(), Destructor, |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 7492 | S.PDiag(diag::err_access_dtor_temp) << T); |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 7493 | S.MarkFunctionReferenced(CurInit.get()->getLocStart(), Destructor); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 7494 | if (S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getLocStart())) |
| 7495 | return ExprError(); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 7496 | } |
| 7497 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7498 | break; |
| 7499 | } |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7500 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7501 | case SK_QualificationConversionLValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7502 | case SK_QualificationConversionXValue: |
| 7503 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7504 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7505 | ExprValueKind VK = |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7506 | Step->Kind == SK_QualificationConversionLValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7507 | VK_LValue : |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7508 | (Step->Kind == SK_QualificationConversionXValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7509 | VK_XValue : |
| 7510 | VK_RValue); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7511 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, CK_NoOp, VK); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7512 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7513 | } |
| 7514 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 7515 | case SK_AtomicConversion: { |
| 7516 | assert(CurInit.get()->isRValue() && "cannot convert glvalue to atomic"); |
| 7517 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
| 7518 | CK_NonAtomicToAtomic, VK_RValue); |
| 7519 | break; |
| 7520 | } |
| 7521 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 7522 | case SK_LValueToRValue: { |
| 7523 | assert(CurInit.get()->isGLValue() && "cannot load from a prvalue"); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7524 | CurInit = ImplicitCastExpr::Create(S.Context, Step->Type, |
| 7525 | CK_LValueToRValue, CurInit.get(), |
| 7526 | /*BasePath=*/nullptr, VK_RValue); |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 7527 | break; |
| 7528 | } |
| 7529 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7530 | case SK_ConversionSequence: |
| 7531 | case SK_ConversionSequenceNoNarrowing: { |
| 7532 | Sema::CheckedConversionKind CCK |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7533 | = Kind.isCStyleCast()? Sema::CCK_CStyleCast |
| 7534 | : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 7535 | : Kind.isExplicitCast()? Sema::CCK_OtherCast |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7536 | : Sema::CCK_ImplicitConversion; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7537 | ExprResult CurInitExprRes = |
| 7538 | S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7539 | getAssignmentAction(Entity), CCK); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7540 | if (CurInitExprRes.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7541 | return ExprError(); |
Roger Ferrer Ibanez | 722a4db | 2016-08-12 08:04:13 +0000 | [diff] [blame] | 7542 | |
| 7543 | S.DiscardMisalignedMemberAddress(Step->Type.getTypePtr(), CurInit.get()); |
| 7544 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7545 | CurInit = CurInitExprRes; |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7546 | |
| 7547 | if (Step->Kind == SK_ConversionSequenceNoNarrowing && |
Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 7548 | S.getLangOpts().CPlusPlus) |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7549 | DiagnoseNarrowingInInitList(S, *Step->ICS, SourceType, Entity.getType(), |
| 7550 | CurInit.get()); |
Roger Ferrer Ibanez | 722a4db | 2016-08-12 08:04:13 +0000 | [diff] [blame] | 7551 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7552 | break; |
Douglas Gregor | 5c8ffab | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 7553 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7554 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7555 | case SK_ListInitialization: { |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7556 | if (checkAbstractType(Step->Type)) |
| 7557 | return ExprError(); |
| 7558 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7559 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 7560 | // If we're not initializing the top-level entity, we need to create an |
| 7561 | // InitializeTemporary entity for our target type. |
| 7562 | QualType Ty = Step->Type; |
| 7563 | bool IsTemporary = !S.Context.hasSameType(Entity.getType(), Ty); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7564 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(Ty); |
Richard Smith | d712d0d | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 7565 | InitializedEntity InitEntity = IsTemporary ? TempEntity : Entity; |
| 7566 | InitListChecker PerformInitList(S, InitEntity, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 7567 | InitList, Ty, /*VerifyOnly=*/false, |
| 7568 | /*TreatUnavailableAsInvalid=*/false); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 7569 | if (PerformInitList.HadError()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7570 | return ExprError(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7571 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 7572 | // Hack: We must update *ResultType if available in order to set the |
| 7573 | // bounds of arrays, e.g. in 'int ar[] = {1, 2, 3};'. |
| 7574 | // Worst case: 'const int (&arref)[] = {1, 2, 3};'. |
| 7575 | if (ResultType && |
| 7576 | ResultType->getNonReferenceType()->isIncompleteArrayType()) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7577 | if ((*ResultType)->isRValueReferenceType()) |
| 7578 | Ty = S.Context.getRValueReferenceType(Ty); |
| 7579 | else if ((*ResultType)->isLValueReferenceType()) |
| 7580 | Ty = S.Context.getLValueReferenceType(Ty, |
| 7581 | (*ResultType)->getAs<LValueReferenceType>()->isSpelledAsLValue()); |
| 7582 | *ResultType = Ty; |
| 7583 | } |
| 7584 | |
| 7585 | InitListExpr *StructuredInitList = |
| 7586 | PerformInitList.getFullyStructuredList(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7587 | CurInit.get(); |
Richard Smith | d712d0d | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 7588 | CurInit = shouldBindAsTemporary(InitEntity) |
| 7589 | ? S.MaybeBindToTemporary(StructuredInitList) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7590 | : StructuredInitList; |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7591 | break; |
| 7592 | } |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 7593 | |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7594 | case SK_ConstructorInitializationFromList: { |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7595 | if (checkAbstractType(Step->Type)) |
| 7596 | return ExprError(); |
| 7597 | |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 7598 | // When an initializer list is passed for a parameter of type "reference |
| 7599 | // to object", we don't get an EK_Temporary entity, but instead an |
| 7600 | // EK_Parameter entity with reference type. |
Sebastian Redl | 99f6616 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 7601 | // FIXME: This is a hack. What we really should do is create a user |
| 7602 | // conversion step for this case, but this makes it considerably more |
| 7603 | // complicated. For now, this will do. |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 7604 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 7605 | Entity.getType().getNonReferenceType()); |
| 7606 | bool UseTemporary = Entity.getType()->isReferenceType(); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 7607 | assert(Args.size() == 1 && "expected a single argument for list init"); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7608 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Richard Smith | 2b349ae | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 7609 | S.Diag(InitList->getExprLoc(), diag::warn_cxx98_compat_ctor_list_init) |
| 7610 | << InitList->getSourceRange(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 7611 | MultiExprArg Arg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 7612 | CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity : |
| 7613 | Entity, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7614 | Kind, Arg, *Step, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 7615 | ConstructorInitRequiresZeroInit, |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7616 | /*IsListInitialization*/true, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 7617 | /*IsStdInitListInit*/false, |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 7618 | InitList->getLBraceLoc(), |
| 7619 | InitList->getRBraceLoc()); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 7620 | break; |
| 7621 | } |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 7622 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7623 | case SK_UnwrapInitList: |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7624 | CurInit = cast<InitListExpr>(CurInit.get())->getInit(0); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7625 | break; |
| 7626 | |
| 7627 | case SK_RewrapInitList: { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7628 | Expr *E = CurInit.get(); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7629 | InitListExpr *Syntactic = Step->WrappingSyntacticList; |
| 7630 | InitListExpr *ILE = new (S.Context) InitListExpr(S.Context, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 7631 | Syntactic->getLBraceLoc(), E, Syntactic->getRBraceLoc()); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7632 | ILE->setSyntacticForm(Syntactic); |
| 7633 | ILE->setType(E->getType()); |
| 7634 | ILE->setValueKind(E->getValueKind()); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7635 | CurInit = ILE; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7636 | break; |
| 7637 | } |
| 7638 | |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7639 | case SK_ConstructorInitialization: |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 7640 | case SK_StdInitializerListConstructorCall: { |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7641 | if (checkAbstractType(Step->Type)) |
| 7642 | return ExprError(); |
| 7643 | |
Sebastian Redl | 99f6616 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 7644 | // When an initializer list is passed for a parameter of type "reference |
| 7645 | // to object", we don't get an EK_Temporary entity, but instead an |
| 7646 | // EK_Parameter entity with reference type. |
| 7647 | // FIXME: This is a hack. What we really should do is create a user |
| 7648 | // conversion step for this case, but this makes it considerably more |
| 7649 | // complicated. For now, this will do. |
| 7650 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 7651 | Entity.getType().getNonReferenceType()); |
| 7652 | bool UseTemporary = Entity.getType()->isReferenceType(); |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 7653 | bool IsStdInitListInit = |
| 7654 | Step->Kind == SK_StdInitializerListConstructorCall; |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7655 | Expr *Source = CurInit.get(); |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 7656 | SourceRange Range = Kind.hasParenOrBraceRange() |
| 7657 | ? Kind.getParenOrBraceRange() |
| 7658 | : SourceRange(); |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7659 | CurInit = PerformConstructorInitialization( |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7660 | S, UseTemporary ? TempEntity : Entity, Kind, |
| 7661 | Source ? MultiExprArg(Source) : Args, *Step, |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7662 | ConstructorInitRequiresZeroInit, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7663 | /*IsListInitialization*/ IsStdInitListInit, |
| 7664 | /*IsStdInitListInitialization*/ IsStdInitListInit, |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 7665 | /*LBraceLoc*/ Range.getBegin(), |
| 7666 | /*RBraceLoc*/ Range.getEnd()); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 7667 | break; |
Sebastian Redl | 99f6616 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 7668 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7669 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 7670 | case SK_ZeroInitialization: { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7671 | step_iterator NextStep = Step; |
| 7672 | ++NextStep; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7673 | if (NextStep != StepEnd && |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 7674 | (NextStep->Kind == SK_ConstructorInitialization || |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7675 | NextStep->Kind == SK_ConstructorInitializationFromList)) { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7676 | // The need for zero-initialization is recorded directly into |
| 7677 | // the call to the object's constructor within the next step. |
| 7678 | ConstructorInitRequiresZeroInit = true; |
| 7679 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7680 | S.getLangOpts().CPlusPlus && |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7681 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 7682 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 7683 | if (!TSInfo) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7684 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 7685 | Kind.getRange().getBegin()); |
| 7686 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7687 | CurInit = new (S.Context) CXXScalarValueInitExpr( |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 7688 | Entity.getType().getNonLValueExprType(S.Context), TSInfo, |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7689 | Kind.getRange().getEnd()); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7690 | } else { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7691 | CurInit = new (S.Context) ImplicitValueInitExpr(Step->Type); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7692 | } |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 7693 | break; |
| 7694 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7695 | |
| 7696 | case SK_CAssignment: { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7697 | QualType SourceType = CurInit.get()->getType(); |
George Burgess IV | 5f21c71 | 2015-10-12 19:57:04 +0000 | [diff] [blame] | 7698 | // Save off the initial CurInit in case we need to emit a diagnostic |
| 7699 | ExprResult InitialCurInit = CurInit; |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7700 | ExprResult Result = CurInit; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7701 | Sema::AssignConvertType ConvTy = |
Fariborz Jahanian | 25eef19 | 2013-07-31 21:40:51 +0000 | [diff] [blame] | 7702 | S.CheckSingleAssignmentConstraints(Step->Type, Result, true, |
| 7703 | Entity.getKind() == InitializedEntity::EK_Parameter_CF_Audited); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7704 | if (Result.isInvalid()) |
| 7705 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7706 | CurInit = Result; |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 7707 | |
| 7708 | // If this is a call, allow conversion to a transparent union. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7709 | ExprResult CurInitExprRes = CurInit; |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 7710 | if (ConvTy != Sema::Compatible && |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 7711 | Entity.isParameterKind() && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7712 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes) |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 7713 | == Sema::Compatible) |
| 7714 | ConvTy = Sema::Compatible; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7715 | if (CurInitExprRes.isInvalid()) |
| 7716 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7717 | CurInit = CurInitExprRes; |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 7718 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 7719 | bool Complained; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7720 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 7721 | Step->Type, SourceType, |
George Burgess IV | 5f21c71 | 2015-10-12 19:57:04 +0000 | [diff] [blame] | 7722 | InitialCurInit.get(), |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 7723 | getAssignmentAction(Entity, true), |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 7724 | &Complained)) { |
| 7725 | PrintInitLocationNote(S, Entity); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7726 | return ExprError(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 7727 | } else if (Complained) |
| 7728 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7729 | break; |
| 7730 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 7731 | |
| 7732 | case SK_StringInit: { |
| 7733 | QualType Ty = Step->Type; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7734 | CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty, |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 7735 | S.Context.getAsArrayType(Ty), S); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 7736 | break; |
| 7737 | } |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 7738 | |
| 7739 | case SK_ObjCObjectConversion: |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7740 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7741 | CK_ObjCObjectLValueCast, |
Eli Friedman | be4b363 | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 7742 | CurInit.get()->getValueKind()); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 7743 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7744 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7745 | case SK_ArrayLoopIndex: { |
| 7746 | Expr *Cur = CurInit.get(); |
| 7747 | Expr *BaseExpr = new (S.Context) |
| 7748 | OpaqueValueExpr(Cur->getExprLoc(), Cur->getType(), |
| 7749 | Cur->getValueKind(), Cur->getObjectKind(), Cur); |
| 7750 | Expr *IndexExpr = |
| 7751 | new (S.Context) ArrayInitIndexExpr(S.Context.getSizeType()); |
| 7752 | CurInit = S.CreateBuiltinArraySubscriptExpr( |
| 7753 | BaseExpr, Kind.getLocation(), IndexExpr, Kind.getLocation()); |
| 7754 | ArrayLoopCommonExprs.push_back(BaseExpr); |
| 7755 | break; |
| 7756 | } |
| 7757 | |
| 7758 | case SK_ArrayLoopInit: { |
| 7759 | assert(!ArrayLoopCommonExprs.empty() && |
| 7760 | "mismatched SK_ArrayLoopIndex and SK_ArrayLoopInit"); |
| 7761 | Expr *Common = ArrayLoopCommonExprs.pop_back_val(); |
| 7762 | CurInit = new (S.Context) ArrayInitLoopExpr(Step->Type, Common, |
| 7763 | CurInit.get()); |
| 7764 | break; |
| 7765 | } |
| 7766 | |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 7767 | case SK_GNUArrayInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7768 | // Okay: we checked everything before creating this step. Note that |
| 7769 | // this is a GNU extension. |
| 7770 | S.Diag(Kind.getLocation(), diag::ext_array_init_copy) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7771 | << Step->Type << CurInit.get()->getType() |
| 7772 | << CurInit.get()->getSourceRange(); |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 7773 | LLVM_FALLTHROUGH; |
| 7774 | case SK_ArrayInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7775 | // If the destination type is an incomplete array type, update the |
| 7776 | // type accordingly. |
| 7777 | if (ResultType) { |
| 7778 | if (const IncompleteArrayType *IncompleteDest |
| 7779 | = S.Context.getAsIncompleteArrayType(Step->Type)) { |
| 7780 | if (const ConstantArrayType *ConstantSource |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7781 | = S.Context.getAsConstantArrayType(CurInit.get()->getType())) { |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7782 | *ResultType = S.Context.getConstantArrayType( |
| 7783 | IncompleteDest->getElementType(), |
| 7784 | ConstantSource->getSize(), |
| 7785 | ArrayType::Normal, 0); |
| 7786 | } |
| 7787 | } |
| 7788 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7789 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7790 | |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 7791 | case SK_ParenthesizedArrayInit: |
| 7792 | // Okay: we checked everything before creating this step. Note that |
| 7793 | // this is a GNU extension. |
| 7794 | S.Diag(Kind.getLocation(), diag::ext_array_init_parens) |
| 7795 | << CurInit.get()->getSourceRange(); |
| 7796 | break; |
| 7797 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7798 | case SK_PassByIndirectCopyRestore: |
| 7799 | case SK_PassByIndirectRestore: |
| 7800 | checkIndirectCopyRestoreSource(S, CurInit.get()); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7801 | CurInit = new (S.Context) ObjCIndirectCopyRestoreExpr( |
| 7802 | CurInit.get(), Step->Type, |
| 7803 | Step->Kind == SK_PassByIndirectCopyRestore); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7804 | break; |
| 7805 | |
| 7806 | case SK_ProduceObjCObject: |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7807 | CurInit = |
| 7808 | ImplicitCastExpr::Create(S.Context, Step->Type, CK_ARCProduceObject, |
| 7809 | CurInit.get(), nullptr, VK_RValue); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7810 | break; |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 7811 | |
| 7812 | case SK_StdInitializerList: { |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 7813 | S.Diag(CurInit.get()->getExprLoc(), |
| 7814 | diag::warn_cxx98_compat_initializer_list_init) |
| 7815 | << CurInit.get()->getSourceRange(); |
Sebastian Redl | 249dee5 | 2012-03-05 19:35:43 +0000 | [diff] [blame] | 7816 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 7817 | // Materialize the temporary into memory. |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 7818 | MaterializeTemporaryExpr *MTE = S.CreateMaterializeTemporaryExpr( |
| 7819 | CurInit.get()->getType(), CurInit.get(), |
| 7820 | /*BoundToLvalueReference=*/false); |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 7821 | |
Florian Hahn | 0aa117d | 2018-07-17 09:23:31 +0000 | [diff] [blame] | 7822 | // Wrap it in a construction of a std::initializer_list<T>. |
| 7823 | CurInit = new (S.Context) CXXStdInitializerListExpr(Step->Type, MTE); |
Richard Smith | 0a9969b | 2018-07-17 00:11:41 +0000 | [diff] [blame] | 7824 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 7825 | // Bind the result, in case the library has given initializer_list a |
| 7826 | // non-trivial destructor. |
| 7827 | if (shouldBindAsTemporary(Entity)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7828 | CurInit = S.MaybeBindToTemporary(CurInit.get()); |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 7829 | break; |
| 7830 | } |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 7831 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 7832 | case SK_OCLSamplerInit: { |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 7833 | // Sampler initialzation have 5 cases: |
| 7834 | // 1. function argument passing |
| 7835 | // 1a. argument is a file-scope variable |
| 7836 | // 1b. argument is a function-scope variable |
| 7837 | // 1c. argument is one of caller function's parameters |
| 7838 | // 2. variable initialization |
| 7839 | // 2a. initializing a file-scope variable |
| 7840 | // 2b. initializing a function-scope variable |
| 7841 | // |
| 7842 | // For file-scope variables, since they cannot be initialized by function |
| 7843 | // call of __translate_sampler_initializer in LLVM IR, their references |
| 7844 | // need to be replaced by a cast from their literal initializers to |
| 7845 | // sampler type. Since sampler variables can only be used in function |
| 7846 | // calls as arguments, we only need to replace them when handling the |
| 7847 | // argument passing. |
| 7848 | assert(Step->Type->isSamplerT() && |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 7849 | "Sampler initialization on non-sampler type."); |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 7850 | Expr *Init = CurInit.get(); |
| 7851 | QualType SourceType = Init->getType(); |
| 7852 | // Case 1 |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 7853 | if (Entity.isParameterKind()) { |
Egor Churaev | a8d2451 | 2017-04-05 09:02:56 +0000 | [diff] [blame] | 7854 | if (!SourceType->isSamplerT() && !SourceType->isIntegerType()) { |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 7855 | S.Diag(Kind.getLocation(), diag::err_sampler_argument_required) |
| 7856 | << SourceType; |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 7857 | break; |
| 7858 | } else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Init)) { |
| 7859 | auto Var = cast<VarDecl>(DRE->getDecl()); |
| 7860 | // Case 1b and 1c |
| 7861 | // No cast from integer to sampler is needed. |
| 7862 | if (!Var->hasGlobalStorage()) { |
| 7863 | CurInit = ImplicitCastExpr::Create(S.Context, Step->Type, |
| 7864 | CK_LValueToRValue, Init, |
| 7865 | /*BasePath=*/nullptr, VK_RValue); |
| 7866 | break; |
| 7867 | } |
| 7868 | // Case 1a |
| 7869 | // For function call with a file-scope sampler variable as argument, |
| 7870 | // get the integer literal. |
| 7871 | // Do not diagnose if the file-scope variable does not have initializer |
| 7872 | // since this has already been diagnosed when parsing the variable |
| 7873 | // declaration. |
| 7874 | if (!Var->getInit() || !isa<ImplicitCastExpr>(Var->getInit())) |
| 7875 | break; |
| 7876 | Init = cast<ImplicitCastExpr>(const_cast<Expr*>( |
| 7877 | Var->getInit()))->getSubExpr(); |
| 7878 | SourceType = Init->getType(); |
| 7879 | } |
| 7880 | } else { |
| 7881 | // Case 2 |
| 7882 | // Check initializer is 32 bit integer constant. |
| 7883 | // If the initializer is taken from global variable, do not diagnose since |
| 7884 | // this has already been done when parsing the variable declaration. |
| 7885 | if (!Init->isConstantInitializer(S.Context, false)) |
| 7886 | break; |
| 7887 | |
| 7888 | if (!SourceType->isIntegerType() || |
| 7889 | 32 != S.Context.getIntWidth(SourceType)) { |
| 7890 | S.Diag(Kind.getLocation(), diag::err_sampler_initializer_not_integer) |
| 7891 | << SourceType; |
| 7892 | break; |
| 7893 | } |
| 7894 | |
| 7895 | llvm::APSInt Result; |
| 7896 | Init->EvaluateAsInt(Result, S.Context); |
| 7897 | const uint64_t SamplerValue = Result.getLimitedValue(); |
| 7898 | // 32-bit value of sampler's initializer is interpreted as |
| 7899 | // bit-field with the following structure: |
| 7900 | // |unspecified|Filter|Addressing Mode| Normalized Coords| |
| 7901 | // |31 6|5 4|3 1| 0| |
| 7902 | // This structure corresponds to enum values of sampler properties |
| 7903 | // defined in SPIR spec v1.2 and also opencl-c.h |
| 7904 | unsigned AddressingMode = (0x0E & SamplerValue) >> 1; |
| 7905 | unsigned FilterMode = (0x30 & SamplerValue) >> 4; |
| 7906 | if (FilterMode != 1 && FilterMode != 2) |
| 7907 | S.Diag(Kind.getLocation(), |
| 7908 | diag::warn_sampler_initializer_invalid_bits) |
| 7909 | << "Filter Mode"; |
| 7910 | if (AddressingMode > 4) |
| 7911 | S.Diag(Kind.getLocation(), |
| 7912 | diag::warn_sampler_initializer_invalid_bits) |
| 7913 | << "Addressing Mode"; |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 7914 | } |
| 7915 | |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 7916 | // Cases 1a, 2a and 2b |
| 7917 | // Insert cast from integer to sampler. |
| 7918 | CurInit = S.ImpCastExprToType(Init, S.Context.OCLSamplerTy, |
| 7919 | CK_IntToOCLSampler); |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 7920 | break; |
| 7921 | } |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7922 | case SK_OCLZeroEvent: { |
| 7923 | assert(Step->Type->isEventT() && |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 7924 | "Event initialization on non-event type."); |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7925 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7926 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7927 | CK_ZeroToOCLEvent, |
| 7928 | CurInit.get()->getValueKind()); |
| 7929 | break; |
| 7930 | } |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 7931 | case SK_OCLZeroQueue: { |
| 7932 | assert(Step->Type->isQueueT() && |
| 7933 | "Event initialization on non queue type."); |
| 7934 | |
| 7935 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
| 7936 | CK_ZeroToOCLQueue, |
| 7937 | CurInit.get()->getValueKind()); |
| 7938 | break; |
| 7939 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7940 | } |
| 7941 | } |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 7942 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7943 | // Check whether the initializer has a shorter lifetime than the initialized |
| 7944 | // entity, and if not, either lifetime-extend or warn as appropriate. |
| 7945 | if (auto *Init = CurInit.get()) |
| 7946 | S.checkInitializerLifetime(Entity, Init); |
| 7947 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 7948 | // Diagnose non-fatal problems with the completed initialization. |
| 7949 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 7950 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 7951 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 7952 | cast<FieldDecl>(Entity.getDecl()), |
| 7953 | CurInit.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7954 | |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7955 | // Check for std::move on construction. |
| 7956 | if (const Expr *E = CurInit.get()) { |
| 7957 | CheckMoveOnConstruction(S, E, |
| 7958 | Entity.getKind() == InitializedEntity::EK_Result); |
| 7959 | } |
| 7960 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7961 | return CurInit; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7962 | } |
| 7963 | |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 7964 | /// Somewhere within T there is an uninitialized reference subobject. |
| 7965 | /// Dig it out and diagnose it. |
Benjamin Kramer | 3e35026 | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 7966 | static bool DiagnoseUninitializedReference(Sema &S, SourceLocation Loc, |
| 7967 | QualType T) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 7968 | if (T->isReferenceType()) { |
| 7969 | S.Diag(Loc, diag::err_reference_without_init) |
| 7970 | << T.getNonReferenceType(); |
| 7971 | return true; |
| 7972 | } |
| 7973 | |
| 7974 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 7975 | if (!RD || !RD->hasUninitializedReferenceMember()) |
| 7976 | return false; |
| 7977 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 7978 | for (const auto *FI : RD->fields()) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 7979 | if (FI->isUnnamedBitfield()) |
| 7980 | continue; |
| 7981 | |
| 7982 | if (DiagnoseUninitializedReference(S, FI->getLocation(), FI->getType())) { |
| 7983 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 7984 | return true; |
| 7985 | } |
| 7986 | } |
| 7987 | |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 7988 | for (const auto &BI : RD->bases()) { |
| 7989 | if (DiagnoseUninitializedReference(S, BI.getLocStart(), BI.getType())) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 7990 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 7991 | return true; |
| 7992 | } |
| 7993 | } |
| 7994 | |
| 7995 | return false; |
| 7996 | } |
| 7997 | |
| 7998 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7999 | //===----------------------------------------------------------------------===// |
| 8000 | // Diagnose initialization failures |
| 8001 | //===----------------------------------------------------------------------===// |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 8002 | |
| 8003 | /// Emit notes associated with an initialization that failed due to a |
| 8004 | /// "simple" conversion failure. |
| 8005 | static void emitBadConversionNotes(Sema &S, const InitializedEntity &entity, |
| 8006 | Expr *op) { |
| 8007 | QualType destType = entity.getType(); |
| 8008 | if (destType.getNonReferenceType()->isObjCObjectPointerType() && |
| 8009 | op->getType()->isObjCObjectPointerType()) { |
| 8010 | |
| 8011 | // Emit a possible note about the conversion failing because the |
| 8012 | // operand is a message send with a related result type. |
| 8013 | S.EmitRelatedResultTypeNote(op); |
| 8014 | |
| 8015 | // Emit a possible note about a return failing because we're |
| 8016 | // expecting a related result type. |
| 8017 | if (entity.getKind() == InitializedEntity::EK_Result) |
| 8018 | S.EmitRelatedResultTypeNoteForReturn(destType); |
| 8019 | } |
| 8020 | } |
| 8021 | |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 8022 | static void diagnoseListInit(Sema &S, const InitializedEntity &Entity, |
| 8023 | InitListExpr *InitList) { |
| 8024 | QualType DestType = Entity.getType(); |
| 8025 | |
| 8026 | QualType E; |
| 8027 | if (S.getLangOpts().CPlusPlus11 && S.isStdInitializerList(DestType, &E)) { |
| 8028 | QualType ArrayType = S.Context.getConstantArrayType( |
| 8029 | E.withConst(), |
| 8030 | llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 8031 | InitList->getNumInits()), |
| 8032 | clang::ArrayType::Normal, 0); |
| 8033 | InitializedEntity HiddenArray = |
| 8034 | InitializedEntity::InitializeTemporary(ArrayType); |
| 8035 | return diagnoseListInit(S, HiddenArray, InitList); |
| 8036 | } |
| 8037 | |
Richard Smith | 8d082d1 | 2014-09-04 22:13:39 +0000 | [diff] [blame] | 8038 | if (DestType->isReferenceType()) { |
| 8039 | // A list-initialization failure for a reference means that we tried to |
| 8040 | // create a temporary of the inner type (per [dcl.init.list]p3.6) and the |
| 8041 | // inner initialization failed. |
| 8042 | QualType T = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 8043 | diagnoseListInit(S, InitializedEntity::InitializeTemporary(T), InitList); |
| 8044 | SourceLocation Loc = InitList->getLocStart(); |
| 8045 | if (auto *D = Entity.getDecl()) |
| 8046 | Loc = D->getLocation(); |
| 8047 | S.Diag(Loc, diag::note_in_reference_temporary_list_initializer) << T; |
| 8048 | return; |
| 8049 | } |
| 8050 | |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 8051 | InitListChecker DiagnoseInitList(S, Entity, InitList, DestType, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 8052 | /*VerifyOnly=*/false, |
| 8053 | /*TreatUnavailableAsInvalid=*/false); |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 8054 | assert(DiagnoseInitList.HadError() && |
| 8055 | "Inconsistent init list check result."); |
| 8056 | } |
| 8057 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8058 | bool InitializationSequence::Diagnose(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8059 | const InitializedEntity &Entity, |
| 8060 | const InitializationKind &Kind, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8061 | ArrayRef<Expr *> Args) { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 8062 | if (!Failed()) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8063 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8064 | |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8065 | // When we want to diagnose only one element of a braced-init-list, |
| 8066 | // we need to factor it out. |
| 8067 | Expr *OnlyArg; |
| 8068 | if (Args.size() == 1) { |
| 8069 | auto *List = dyn_cast<InitListExpr>(Args[0]); |
| 8070 | if (List && List->getNumInits() == 1) |
| 8071 | OnlyArg = List->getInit(0); |
| 8072 | else |
| 8073 | OnlyArg = Args[0]; |
| 8074 | } |
| 8075 | else |
| 8076 | OnlyArg = nullptr; |
| 8077 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 8078 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8079 | switch (Failure) { |
| 8080 | case FK_TooManyInitsForReference: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8081 | // FIXME: Customize for the initialized entity? |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8082 | if (Args.empty()) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 8083 | // Dig out the reference subobject which is uninitialized and diagnose it. |
| 8084 | // If this is value-initialization, this could be nested some way within |
| 8085 | // the target type. |
| 8086 | assert(Kind.getKind() == InitializationKind::IK_Value || |
| 8087 | DestType->isReferenceType()); |
| 8088 | bool Diagnosed = |
| 8089 | DiagnoseUninitializedReference(S, Kind.getLocation(), DestType); |
| 8090 | assert(Diagnosed && "couldn't find uninitialized reference to diagnose"); |
| 8091 | (void)Diagnosed; |
| 8092 | } else // FIXME: diagnostic below could be better! |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8093 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8094 | << SourceRange(Args.front()->getLocStart(), Args.back()->getLocEnd()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8095 | break; |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 8096 | case FK_ParenthesizedListInitForReference: |
| 8097 | S.Diag(Kind.getLocation(), diag::err_list_init_in_parens) |
| 8098 | << 1 << Entity.getType() << Args[0]->getSourceRange(); |
| 8099 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8100 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8101 | case FK_ArrayNeedsInitList: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 8102 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 0; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8103 | break; |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 8104 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 8105 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 1; |
| 8106 | break; |
| 8107 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 8108 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 2; |
| 8109 | break; |
| 8110 | case FK_NarrowStringIntoWideCharArray: |
| 8111 | S.Diag(Kind.getLocation(), diag::err_array_init_narrow_string_into_wchar); |
| 8112 | break; |
| 8113 | case FK_WideStringIntoCharArray: |
| 8114 | S.Diag(Kind.getLocation(), diag::err_array_init_wide_string_into_char); |
| 8115 | break; |
| 8116 | case FK_IncompatWideStringIntoWideChar: |
| 8117 | S.Diag(Kind.getLocation(), |
| 8118 | diag::err_array_init_incompat_wide_string_into_wchar); |
| 8119 | break; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 8120 | case FK_PlainStringIntoUTF8Char: |
| 8121 | S.Diag(Kind.getLocation(), |
| 8122 | diag::err_array_init_plain_string_into_char8_t); |
| 8123 | S.Diag(Args.front()->getLocStart(), |
| 8124 | diag::note_array_init_plain_string_into_char8_t) |
| 8125 | << FixItHint::CreateInsertion(Args.front()->getLocStart(), "u8"); |
| 8126 | break; |
| 8127 | case FK_UTF8StringIntoPlainChar: |
| 8128 | S.Diag(Kind.getLocation(), |
| 8129 | diag::err_array_init_utf8_string_into_char); |
| 8130 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8131 | case FK_ArrayTypeMismatch: |
| 8132 | case FK_NonConstantArrayInit: |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 8133 | S.Diag(Kind.getLocation(), |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8134 | (Failure == FK_ArrayTypeMismatch |
| 8135 | ? diag::err_array_init_different_type |
| 8136 | : diag::err_array_init_non_constant_array)) |
| 8137 | << DestType.getNonReferenceType() |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8138 | << OnlyArg->getType() |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8139 | << Args[0]->getSourceRange(); |
| 8140 | break; |
| 8141 | |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 8142 | case FK_VariableLengthArrayHasInitializer: |
| 8143 | S.Diag(Kind.getLocation(), diag::err_variable_object_no_init) |
| 8144 | << Args[0]->getSourceRange(); |
| 8145 | break; |
| 8146 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8147 | case FK_AddressOfOverloadFailed: { |
| 8148 | DeclAccessPair Found; |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8149 | S.ResolveAddressOfOverloadedFunction(OnlyArg, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8150 | DestType.getNonReferenceType(), |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8151 | true, |
| 8152 | Found); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8153 | break; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8154 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8155 | |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 8156 | case FK_AddressOfUnaddressableFunction: { |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8157 | auto *FD = cast<FunctionDecl>(cast<DeclRefExpr>(OnlyArg)->getDecl()); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 8158 | S.checkAddressOfFunctionIsAvailable(FD, /*Complain=*/true, |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8159 | OnlyArg->getLocStart()); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 8160 | break; |
| 8161 | } |
| 8162 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8163 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 8164 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8165 | switch (FailedOverloadResult) { |
| 8166 | case OR_Ambiguous: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8167 | if (Failure == FK_UserConversionOverloadFailed) |
| 8168 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8169 | << OnlyArg->getType() << DestType |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8170 | << Args[0]->getSourceRange(); |
| 8171 | else |
| 8172 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8173 | << DestType << OnlyArg->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8174 | << Args[0]->getSourceRange(); |
| 8175 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8176 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8177 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8178 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8179 | case OR_No_Viable_Function: |
Larisse Voufo | 70bb43a | 2013-06-27 03:36:30 +0000 | [diff] [blame] | 8180 | if (!S.RequireCompleteType(Kind.getLocation(), |
Larisse Voufo | 64cf3ef | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 8181 | DestType.getNonReferenceType(), |
| 8182 | diag::err_typecheck_nonviable_condition_incomplete, |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8183 | OnlyArg->getType(), Args[0]->getSourceRange())) |
Larisse Voufo | 64cf3ef | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 8184 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
Nick Lewycky | 08426e2 | 2015-08-25 22:18:46 +0000 | [diff] [blame] | 8185 | << (Entity.getKind() == InitializedEntity::EK_Result) |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8186 | << OnlyArg->getType() << Args[0]->getSourceRange() |
Larisse Voufo | 64cf3ef | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 8187 | << DestType.getNonReferenceType(); |
| 8188 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8189 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8190 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8191 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8192 | case OR_Deleted: { |
| 8193 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8194 | << OnlyArg->getType() << DestType.getNonReferenceType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8195 | << Args[0]->getSourceRange(); |
| 8196 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8197 | OverloadingResult Ovl |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 8198 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8199 | if (Ovl == OR_Deleted) { |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 8200 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8201 | } else { |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 8202 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8203 | } |
| 8204 | break; |
| 8205 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8206 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8207 | case OR_Success: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 8208 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8209 | } |
| 8210 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8211 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8212 | case FK_NonConstLValueReferenceBindingToTemporary: |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8213 | if (isa<InitListExpr>(Args[0])) { |
| 8214 | S.Diag(Kind.getLocation(), |
| 8215 | diag::err_lvalue_reference_bind_to_initlist) |
| 8216 | << DestType.getNonReferenceType().isVolatileQualified() |
| 8217 | << DestType.getNonReferenceType() |
| 8218 | << Args[0]->getSourceRange(); |
| 8219 | break; |
| 8220 | } |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 8221 | LLVM_FALLTHROUGH; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8222 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8223 | case FK_NonConstLValueReferenceBindingToUnrelated: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8224 | S.Diag(Kind.getLocation(), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8225 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 8226 | ? diag::err_lvalue_reference_bind_to_temporary |
| 8227 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | d1e0864 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 8228 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8229 | << DestType.getNonReferenceType() |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8230 | << OnlyArg->getType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8231 | << Args[0]->getSourceRange(); |
| 8232 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8233 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8234 | case FK_NonConstLValueReferenceBindingToBitfield: { |
| 8235 | // We don't necessarily have an unambiguous source bit-field. |
| 8236 | FieldDecl *BitField = Args[0]->getSourceBitField(); |
| 8237 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
| 8238 | << DestType.isVolatileQualified() |
| 8239 | << (BitField ? BitField->getDeclName() : DeclarationName()) |
| 8240 | << (BitField != nullptr) |
| 8241 | << Args[0]->getSourceRange(); |
| 8242 | if (BitField) |
| 8243 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
| 8244 | break; |
| 8245 | } |
| 8246 | |
| 8247 | case FK_NonConstLValueReferenceBindingToVectorElement: |
| 8248 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 8249 | << DestType.isVolatileQualified() |
| 8250 | << Args[0]->getSourceRange(); |
| 8251 | break; |
| 8252 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8253 | case FK_RValueReferenceBindingToLValue: |
| 8254 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8255 | << DestType.getNonReferenceType() << OnlyArg->getType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8256 | << Args[0]->getSourceRange(); |
| 8257 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8258 | |
Richard Trieu | f956a49 | 2015-05-16 01:27:03 +0000 | [diff] [blame] | 8259 | case FK_ReferenceInitDropsQualifiers: { |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8260 | QualType SourceType = OnlyArg->getType(); |
Richard Trieu | f956a49 | 2015-05-16 01:27:03 +0000 | [diff] [blame] | 8261 | QualType NonRefType = DestType.getNonReferenceType(); |
| 8262 | Qualifiers DroppedQualifiers = |
| 8263 | SourceType.getQualifiers() - NonRefType.getQualifiers(); |
| 8264 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8265 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
Richard Trieu | f956a49 | 2015-05-16 01:27:03 +0000 | [diff] [blame] | 8266 | << SourceType |
| 8267 | << NonRefType |
| 8268 | << DroppedQualifiers.getCVRQualifiers() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8269 | << Args[0]->getSourceRange(); |
| 8270 | break; |
Richard Trieu | f956a49 | 2015-05-16 01:27:03 +0000 | [diff] [blame] | 8271 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8272 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8273 | case FK_ReferenceInitFailed: |
| 8274 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 8275 | << DestType.getNonReferenceType() |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8276 | << OnlyArg->isLValue() |
| 8277 | << OnlyArg->getType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8278 | << Args[0]->getSourceRange(); |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 8279 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8280 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8281 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8282 | case FK_ConversionFailed: { |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8283 | QualType FromType = OnlyArg->getType(); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8284 | PartialDiagnostic PDiag = S.PDiag(diag::err_init_conversion_failed) |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8285 | << (int)Entity.getKind() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8286 | << DestType |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8287 | << OnlyArg->isLValue() |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8288 | << FromType |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8289 | << Args[0]->getSourceRange(); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8290 | S.HandleFunctionTypeMismatch(PDiag, FromType, DestType); |
| 8291 | S.Diag(Kind.getLocation(), PDiag); |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 8292 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8293 | break; |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8294 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8295 | |
| 8296 | case FK_ConversionFromPropertyFailed: |
| 8297 | // No-op. This error has already been reported. |
| 8298 | break; |
| 8299 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8300 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 8301 | SourceRange R; |
| 8302 | |
David Majnemer | bd38544 | 2015-04-10 04:52:06 +0000 | [diff] [blame] | 8303 | auto *InitList = dyn_cast<InitListExpr>(Args[0]); |
Benjamin Kramer | c4284e3 | 2015-09-23 16:03:53 +0000 | [diff] [blame] | 8304 | if (InitList && InitList->getNumInits() >= 1) { |
David Majnemer | bd38544 | 2015-04-10 04:52:06 +0000 | [diff] [blame] | 8305 | R = SourceRange(InitList->getInit(0)->getLocEnd(), InitList->getLocEnd()); |
Benjamin Kramer | c4284e3 | 2015-09-23 16:03:53 +0000 | [diff] [blame] | 8306 | } else { |
| 8307 | assert(Args.size() > 1 && "Expected multiple initializers!"); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8308 | R = SourceRange(Args.front()->getLocEnd(), Args.back()->getLocEnd()); |
Benjamin Kramer | c4284e3 | 2015-09-23 16:03:53 +0000 | [diff] [blame] | 8309 | } |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8310 | |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 8311 | R.setBegin(S.getLocForEndOfToken(R.getBegin())); |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 8312 | if (Kind.isCStyleOrFunctionalCast()) |
| 8313 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 8314 | << R; |
| 8315 | else |
| 8316 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 8317 | << /*scalar=*/2 << R; |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8318 | break; |
| 8319 | } |
| 8320 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 8321 | case FK_ParenthesizedListInitForScalar: |
| 8322 | S.Diag(Kind.getLocation(), diag::err_list_init_in_parens) |
| 8323 | << 0 << Entity.getType() << Args[0]->getSourceRange(); |
| 8324 | break; |
| 8325 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8326 | case FK_ReferenceBindingToInitList: |
| 8327 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 8328 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 8329 | break; |
| 8330 | |
| 8331 | case FK_InitListBadDestinationType: |
| 8332 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 8333 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 8334 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8335 | |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 8336 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8337 | case FK_ConstructorOverloadFailed: { |
| 8338 | SourceRange ArgsRange; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8339 | if (Args.size()) |
| 8340 | ArgsRange = SourceRange(Args.front()->getLocStart(), |
| 8341 | Args.back()->getLocEnd()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8342 | |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 8343 | if (Failure == FK_ListConstructorOverloadFailed) { |
Nico Weber | 9709ebf | 2014-07-08 23:54:25 +0000 | [diff] [blame] | 8344 | assert(Args.size() == 1 && |
| 8345 | "List construction from other than 1 argument."); |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 8346 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8347 | Args = MultiExprArg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 8348 | } |
| 8349 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8350 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 8351 | // bad. |
| 8352 | switch (FailedOverloadResult) { |
| 8353 | case OR_Ambiguous: |
| 8354 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 8355 | << DestType << ArgsRange; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8356 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8357 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8358 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8359 | case OR_No_Viable_Function: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8360 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 8361 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 8362 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 8363 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 8364 | // This is implicit default initialization of a member or |
| 8365 | // base within a constructor. If no viable function was |
Nico Weber | a691689 | 2016-06-10 18:53:04 +0000 | [diff] [blame] | 8366 | // found, notify the user that they need to explicitly |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8367 | // initialize this base/member. |
| 8368 | CXXConstructorDecl *Constructor |
| 8369 | = cast<CXXConstructorDecl>(S.CurContext); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 8370 | const CXXRecordDecl *InheritedFrom = nullptr; |
| 8371 | if (auto Inherited = Constructor->getInheritedConstructor()) |
| 8372 | InheritedFrom = Inherited.getShadowDecl()->getNominatedBaseClass(); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8373 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 8374 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 8375 | << (InheritedFrom ? 2 : Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8376 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 8377 | << /*base=*/0 |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 8378 | << Entity.getType() |
| 8379 | << InheritedFrom; |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8380 | |
| 8381 | RecordDecl *BaseDecl |
| 8382 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 8383 | ->getDecl(); |
| 8384 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 8385 | << S.Context.getTagDeclType(BaseDecl); |
| 8386 | } else { |
| 8387 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 8388 | << (InheritedFrom ? 2 : Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8389 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 8390 | << /*member=*/1 |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 8391 | << Entity.getName() |
| 8392 | << InheritedFrom; |
Alp Toker | 2afa878 | 2014-05-28 12:20:14 +0000 | [diff] [blame] | 8393 | S.Diag(Entity.getDecl()->getLocation(), |
| 8394 | diag::note_member_declared_at); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8395 | |
| 8396 | if (const RecordType *Record |
| 8397 | = Entity.getType()->getAs<RecordType>()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8398 | S.Diag(Record->getDecl()->getLocation(), |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8399 | diag::note_previous_decl) |
| 8400 | << S.Context.getTagDeclType(Record->getDecl()); |
| 8401 | } |
| 8402 | break; |
| 8403 | } |
| 8404 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8405 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 8406 | << DestType << ArgsRange; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8407 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8408 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8409 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8410 | case OR_Deleted: { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8411 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8412 | OverloadingResult Ovl |
| 8413 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 8414 | if (Ovl != OR_Deleted) { |
| 8415 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 8416 | << true << DestType << ArgsRange; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8417 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 8418 | break; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8419 | } |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 8420 | |
| 8421 | // If this is a defaulted or implicitly-declared function, then |
| 8422 | // it was implicitly deleted. Make it clear that the deletion was |
| 8423 | // implicit. |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 8424 | if (S.isImplicitlyDeleted(Best->Function)) |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 8425 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_special_init) |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 8426 | << S.getSpecialMember(cast<CXXMethodDecl>(Best->Function)) |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 8427 | << DestType << ArgsRange; |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 8428 | else |
| 8429 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 8430 | << true << DestType << ArgsRange; |
| 8431 | |
| 8432 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8433 | break; |
| 8434 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8435 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8436 | case OR_Success: |
| 8437 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8438 | } |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8439 | } |
David Blaikie | 60deeee | 2012-01-17 08:24:58 +0000 | [diff] [blame] | 8440 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8441 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 8442 | case FK_DefaultInitOfConst: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8443 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 8444 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 8445 | // This is implicit default-initialization of a const member in |
| 8446 | // a constructor. Complain that it needs to be explicitly |
| 8447 | // initialized. |
| 8448 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 8449 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 8450 | << (Constructor->getInheritedConstructor() ? 2 : |
| 8451 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8452 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 8453 | << /*const=*/1 |
| 8454 | << Entity.getName(); |
| 8455 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 8456 | << Entity.getName(); |
| 8457 | } else { |
| 8458 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
Nico Weber | 9386c82 | 2014-07-23 05:16:10 +0000 | [diff] [blame] | 8459 | << DestType << (bool)DestType->getAs<RecordType>(); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8460 | } |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 8461 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8462 | |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 8463 | case FK_Incomplete: |
Douglas Gregor | 85f3423 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 8464 | S.RequireCompleteType(Kind.getLocation(), FailedIncompleteType, |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 8465 | diag::err_init_incomplete_type); |
| 8466 | break; |
| 8467 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 8468 | case FK_ListInitializationFailed: { |
| 8469 | // Run the init list checker again to emit diagnostics. |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 8470 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
| 8471 | diagnoseListInit(S, Entity, InitList); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 8472 | break; |
| 8473 | } |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 8474 | |
| 8475 | case FK_PlaceholderType: { |
| 8476 | // FIXME: Already diagnosed! |
| 8477 | break; |
| 8478 | } |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 8479 | |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 8480 | case FK_ExplicitConstructor: { |
| 8481 | S.Diag(Kind.getLocation(), diag::err_selected_explicit_constructor) |
| 8482 | << Args[0]->getSourceRange(); |
| 8483 | OverloadCandidateSet::iterator Best; |
| 8484 | OverloadingResult Ovl |
| 8485 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Matt Beaumont-Gay | 5dcce09 | 2012-04-02 19:05:35 +0000 | [diff] [blame] | 8486 | (void)Ovl; |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 8487 | assert(Ovl == OR_Success && "Inconsistent overload resolution"); |
| 8488 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 8489 | S.Diag(CtorDecl->getLocation(), |
| 8490 | diag::note_explicit_ctor_deduction_guide_here) << false; |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 8491 | break; |
| 8492 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8493 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8494 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 8495 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8496 | return true; |
| 8497 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8498 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8499 | void InitializationSequence::dump(raw_ostream &OS) const { |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8500 | switch (SequenceKind) { |
| 8501 | case FailedSequence: { |
| 8502 | OS << "Failed sequence: "; |
| 8503 | switch (Failure) { |
| 8504 | case FK_TooManyInitsForReference: |
| 8505 | OS << "too many initializers for reference"; |
| 8506 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8507 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 8508 | case FK_ParenthesizedListInitForReference: |
| 8509 | OS << "parenthesized list init for reference"; |
| 8510 | break; |
| 8511 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8512 | case FK_ArrayNeedsInitList: |
| 8513 | OS << "array requires initializer list"; |
| 8514 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8515 | |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 8516 | case FK_AddressOfUnaddressableFunction: |
| 8517 | OS << "address of unaddressable function was taken"; |
| 8518 | break; |
| 8519 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8520 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 8521 | OS << "array requires initializer list or string literal"; |
| 8522 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8523 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 8524 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 8525 | OS << "array requires initializer list or wide string literal"; |
| 8526 | break; |
| 8527 | |
| 8528 | case FK_NarrowStringIntoWideCharArray: |
| 8529 | OS << "narrow string into wide char array"; |
| 8530 | break; |
| 8531 | |
| 8532 | case FK_WideStringIntoCharArray: |
| 8533 | OS << "wide string into char array"; |
| 8534 | break; |
| 8535 | |
| 8536 | case FK_IncompatWideStringIntoWideChar: |
| 8537 | OS << "incompatible wide string into wide char array"; |
| 8538 | break; |
| 8539 | |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 8540 | case FK_PlainStringIntoUTF8Char: |
| 8541 | OS << "plain string literal into char8_t array"; |
| 8542 | break; |
| 8543 | |
| 8544 | case FK_UTF8StringIntoPlainChar: |
| 8545 | OS << "u8 string literal into char array"; |
| 8546 | break; |
| 8547 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8548 | case FK_ArrayTypeMismatch: |
| 8549 | OS << "array type mismatch"; |
| 8550 | break; |
| 8551 | |
| 8552 | case FK_NonConstantArrayInit: |
| 8553 | OS << "non-constant array initializer"; |
| 8554 | break; |
| 8555 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8556 | case FK_AddressOfOverloadFailed: |
| 8557 | OS << "address of overloaded function failed"; |
| 8558 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8559 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8560 | case FK_ReferenceInitOverloadFailed: |
| 8561 | OS << "overload resolution for reference initialization failed"; |
| 8562 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8563 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8564 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 8565 | OS << "non-const lvalue reference bound to temporary"; |
| 8566 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8567 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8568 | case FK_NonConstLValueReferenceBindingToBitfield: |
| 8569 | OS << "non-const lvalue reference bound to bit-field"; |
| 8570 | break; |
| 8571 | |
| 8572 | case FK_NonConstLValueReferenceBindingToVectorElement: |
| 8573 | OS << "non-const lvalue reference bound to vector element"; |
| 8574 | break; |
| 8575 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8576 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 8577 | OS << "non-const lvalue reference bound to unrelated type"; |
| 8578 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8579 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8580 | case FK_RValueReferenceBindingToLValue: |
| 8581 | OS << "rvalue reference bound to an lvalue"; |
| 8582 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8583 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8584 | case FK_ReferenceInitDropsQualifiers: |
| 8585 | OS << "reference initialization drops qualifiers"; |
| 8586 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8587 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8588 | case FK_ReferenceInitFailed: |
| 8589 | OS << "reference initialization failed"; |
| 8590 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8591 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8592 | case FK_ConversionFailed: |
| 8593 | OS << "conversion failed"; |
| 8594 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8595 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8596 | case FK_ConversionFromPropertyFailed: |
| 8597 | OS << "conversion from property failed"; |
| 8598 | break; |
| 8599 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8600 | case FK_TooManyInitsForScalar: |
| 8601 | OS << "too many initializers for scalar"; |
| 8602 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8603 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 8604 | case FK_ParenthesizedListInitForScalar: |
| 8605 | OS << "parenthesized list init for reference"; |
| 8606 | break; |
| 8607 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8608 | case FK_ReferenceBindingToInitList: |
| 8609 | OS << "referencing binding to initializer list"; |
| 8610 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8611 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8612 | case FK_InitListBadDestinationType: |
| 8613 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 8614 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8615 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8616 | case FK_UserConversionOverloadFailed: |
| 8617 | OS << "overloading failed for user-defined conversion"; |
| 8618 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8619 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8620 | case FK_ConstructorOverloadFailed: |
| 8621 | OS << "constructor overloading failed"; |
| 8622 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8623 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8624 | case FK_DefaultInitOfConst: |
| 8625 | OS << "default initialization of a const variable"; |
| 8626 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8627 | |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 8628 | case FK_Incomplete: |
| 8629 | OS << "initialization of incomplete type"; |
| 8630 | break; |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 8631 | |
| 8632 | case FK_ListInitializationFailed: |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 8633 | OS << "list initialization checker failure"; |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 8634 | break; |
| 8635 | |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 8636 | case FK_VariableLengthArrayHasInitializer: |
| 8637 | OS << "variable length array has an initializer"; |
| 8638 | break; |
| 8639 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 8640 | case FK_PlaceholderType: |
| 8641 | OS << "initializer expression isn't contextually valid"; |
| 8642 | break; |
Nick Lewycky | 097f47c | 2011-12-22 20:21:32 +0000 | [diff] [blame] | 8643 | |
| 8644 | case FK_ListConstructorOverloadFailed: |
| 8645 | OS << "list constructor overloading failed"; |
| 8646 | break; |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 8647 | |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 8648 | case FK_ExplicitConstructor: |
| 8649 | OS << "list copy initialization chose explicit constructor"; |
| 8650 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8651 | } |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8652 | OS << '\n'; |
| 8653 | return; |
| 8654 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8655 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8656 | case DependentSequence: |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 8657 | OS << "Dependent sequence\n"; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8658 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8659 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 8660 | case NormalSequence: |
| 8661 | OS << "Normal sequence: "; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8662 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8663 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8664 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8665 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 8666 | if (S != step_begin()) { |
| 8667 | OS << " -> "; |
| 8668 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8669 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8670 | switch (S->Kind) { |
| 8671 | case SK_ResolveAddressOfOverloadedFunction: |
| 8672 | OS << "resolve address of overloaded function"; |
| 8673 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8674 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8675 | case SK_CastDerivedToBaseRValue: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8676 | OS << "derived-to-base (rvalue)"; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8677 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8678 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 8679 | case SK_CastDerivedToBaseXValue: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8680 | OS << "derived-to-base (xvalue)"; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 8681 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8682 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8683 | case SK_CastDerivedToBaseLValue: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8684 | OS << "derived-to-base (lvalue)"; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8685 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8686 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8687 | case SK_BindReference: |
| 8688 | OS << "bind reference to lvalue"; |
| 8689 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8690 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8691 | case SK_BindReferenceToTemporary: |
| 8692 | OS << "bind reference to a temporary"; |
| 8693 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8694 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8695 | case SK_FinalCopy: |
| 8696 | OS << "final copy in class direct-initialization"; |
| 8697 | break; |
| 8698 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 8699 | case SK_ExtraneousCopyToTemporary: |
| 8700 | OS << "extraneous C++03 copy to temporary"; |
| 8701 | break; |
| 8702 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8703 | case SK_UserConversion: |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 8704 | OS << "user-defined conversion via " << *S->Function.Function; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8705 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 8706 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8707 | case SK_QualificationConversionRValue: |
| 8708 | OS << "qualification conversion (rvalue)"; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8709 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8710 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 8711 | case SK_QualificationConversionXValue: |
| 8712 | OS << "qualification conversion (xvalue)"; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8713 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 8714 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8715 | case SK_QualificationConversionLValue: |
| 8716 | OS << "qualification conversion (lvalue)"; |
| 8717 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8718 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 8719 | case SK_AtomicConversion: |
| 8720 | OS << "non-atomic-to-atomic conversion"; |
| 8721 | break; |
| 8722 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 8723 | case SK_LValueToRValue: |
| 8724 | OS << "load (lvalue to rvalue)"; |
| 8725 | break; |
| 8726 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8727 | case SK_ConversionSequence: |
| 8728 | OS << "implicit conversion sequence ("; |
Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 8729 | S->ICS->dump(); // FIXME: use OS |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8730 | OS << ")"; |
| 8731 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8732 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 8733 | case SK_ConversionSequenceNoNarrowing: |
| 8734 | OS << "implicit conversion sequence with narrowing prohibited ("; |
Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 8735 | S->ICS->dump(); // FIXME: use OS |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 8736 | OS << ")"; |
| 8737 | break; |
| 8738 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8739 | case SK_ListInitialization: |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 8740 | OS << "list aggregate initialization"; |
| 8741 | break; |
| 8742 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8743 | case SK_UnwrapInitList: |
| 8744 | OS << "unwrap reference initializer list"; |
| 8745 | break; |
| 8746 | |
| 8747 | case SK_RewrapInitList: |
| 8748 | OS << "rewrap reference initializer list"; |
| 8749 | break; |
| 8750 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8751 | case SK_ConstructorInitialization: |
| 8752 | OS << "constructor initialization"; |
| 8753 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8754 | |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 8755 | case SK_ConstructorInitializationFromList: |
| 8756 | OS << "list initialization via constructor"; |
| 8757 | break; |
| 8758 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8759 | case SK_ZeroInitialization: |
| 8760 | OS << "zero initialization"; |
| 8761 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8762 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8763 | case SK_CAssignment: |
| 8764 | OS << "C assignment"; |
| 8765 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8766 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8767 | case SK_StringInit: |
| 8768 | OS << "string initialization"; |
| 8769 | break; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 8770 | |
| 8771 | case SK_ObjCObjectConversion: |
| 8772 | OS << "Objective-C object conversion"; |
| 8773 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8774 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 8775 | case SK_ArrayLoopIndex: |
| 8776 | OS << "indexing for array initialization loop"; |
| 8777 | break; |
| 8778 | |
| 8779 | case SK_ArrayLoopInit: |
| 8780 | OS << "array initialization loop"; |
| 8781 | break; |
| 8782 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8783 | case SK_ArrayInit: |
| 8784 | OS << "array initialization"; |
| 8785 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8786 | |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 8787 | case SK_GNUArrayInit: |
| 8788 | OS << "array initialization (GNU extension)"; |
| 8789 | break; |
| 8790 | |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 8791 | case SK_ParenthesizedArrayInit: |
| 8792 | OS << "parenthesized array initialization"; |
| 8793 | break; |
| 8794 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8795 | case SK_PassByIndirectCopyRestore: |
| 8796 | OS << "pass by indirect copy and restore"; |
| 8797 | break; |
| 8798 | |
| 8799 | case SK_PassByIndirectRestore: |
| 8800 | OS << "pass by indirect restore"; |
| 8801 | break; |
| 8802 | |
| 8803 | case SK_ProduceObjCObject: |
| 8804 | OS << "Objective-C object retension"; |
| 8805 | break; |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 8806 | |
| 8807 | case SK_StdInitializerList: |
| 8808 | OS << "std::initializer_list from initializer list"; |
| 8809 | break; |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 8810 | |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 8811 | case SK_StdInitializerListConstructorCall: |
| 8812 | OS << "list initialization from std::initializer_list"; |
| 8813 | break; |
| 8814 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 8815 | case SK_OCLSamplerInit: |
| 8816 | OS << "OpenCL sampler_t from integer constant"; |
| 8817 | break; |
| 8818 | |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 8819 | case SK_OCLZeroEvent: |
| 8820 | OS << "OpenCL event_t from zero"; |
| 8821 | break; |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 8822 | |
| 8823 | case SK_OCLZeroQueue: |
| 8824 | OS << "OpenCL queue_t from zero"; |
| 8825 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8826 | } |
Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 8827 | |
| 8828 | OS << " [" << S->Type.getAsString() << ']'; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8829 | } |
Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 8830 | |
| 8831 | OS << '\n'; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8832 | } |
| 8833 | |
| 8834 | void InitializationSequence::dump() const { |
| 8835 | dump(llvm::errs()); |
| 8836 | } |
| 8837 | |
Nico Weber | 3d7f00d | 2018-06-19 23:19:34 +0000 | [diff] [blame] | 8838 | static bool NarrowingErrs(const LangOptions &L) { |
| 8839 | return L.CPlusPlus11 && |
| 8840 | (!L.MicrosoftExt || L.isCompatibleWithMSVC(LangOptions::MSVC2015)); |
| 8841 | } |
| 8842 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 8843 | static void DiagnoseNarrowingInInitList(Sema &S, |
| 8844 | const ImplicitConversionSequence &ICS, |
| 8845 | QualType PreNarrowingType, |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8846 | QualType EntityType, |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8847 | const Expr *PostInit) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8848 | const StandardConversionSequence *SCS = nullptr; |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8849 | switch (ICS.getKind()) { |
| 8850 | case ImplicitConversionSequence::StandardConversion: |
| 8851 | SCS = &ICS.Standard; |
| 8852 | break; |
| 8853 | case ImplicitConversionSequence::UserDefinedConversion: |
| 8854 | SCS = &ICS.UserDefined.After; |
| 8855 | break; |
| 8856 | case ImplicitConversionSequence::AmbiguousConversion: |
| 8857 | case ImplicitConversionSequence::EllipsisConversion: |
| 8858 | case ImplicitConversionSequence::BadConversion: |
| 8859 | return; |
| 8860 | } |
| 8861 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8862 | // C++11 [dcl.init.list]p7: Check whether this is a narrowing conversion. |
| 8863 | APValue ConstantValue; |
Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 8864 | QualType ConstantType; |
| 8865 | switch (SCS->getNarrowingKind(S.Context, PostInit, ConstantValue, |
| 8866 | ConstantType)) { |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8867 | case NK_Not_Narrowing: |
Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 8868 | case NK_Dependent_Narrowing: |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8869 | // No narrowing occurred. |
| 8870 | return; |
| 8871 | |
| 8872 | case NK_Type_Narrowing: |
| 8873 | // This was a floating-to-integer conversion, which is always considered a |
| 8874 | // narrowing conversion even if the value is a constant and can be |
| 8875 | // represented exactly as an integer. |
Nico Weber | 3d7f00d | 2018-06-19 23:19:34 +0000 | [diff] [blame] | 8876 | S.Diag(PostInit->getLocStart(), NarrowingErrs(S.getLangOpts()) |
| 8877 | ? diag::ext_init_list_type_narrowing |
| 8878 | : diag::warn_init_list_type_narrowing) |
| 8879 | << PostInit->getSourceRange() |
| 8880 | << PreNarrowingType.getLocalUnqualifiedType() |
| 8881 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8882 | break; |
| 8883 | |
| 8884 | case NK_Constant_Narrowing: |
| 8885 | // A constant value was narrowed. |
| 8886 | S.Diag(PostInit->getLocStart(), |
Nico Weber | 3d7f00d | 2018-06-19 23:19:34 +0000 | [diff] [blame] | 8887 | NarrowingErrs(S.getLangOpts()) |
| 8888 | ? diag::ext_init_list_constant_narrowing |
| 8889 | : diag::warn_init_list_constant_narrowing) |
| 8890 | << PostInit->getSourceRange() |
| 8891 | << ConstantValue.getAsString(S.getASTContext(), ConstantType) |
| 8892 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8893 | break; |
| 8894 | |
| 8895 | case NK_Variable_Narrowing: |
| 8896 | // A variable's value may have been narrowed. |
| 8897 | S.Diag(PostInit->getLocStart(), |
Nico Weber | 3d7f00d | 2018-06-19 23:19:34 +0000 | [diff] [blame] | 8898 | NarrowingErrs(S.getLangOpts()) |
| 8899 | ? diag::ext_init_list_variable_narrowing |
| 8900 | : diag::warn_init_list_variable_narrowing) |
| 8901 | << PostInit->getSourceRange() |
| 8902 | << PreNarrowingType.getLocalUnqualifiedType() |
| 8903 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8904 | break; |
| 8905 | } |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 8906 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 8907 | SmallString<128> StaticCast; |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 8908 | llvm::raw_svector_ostream OS(StaticCast); |
| 8909 | OS << "static_cast<"; |
| 8910 | if (const TypedefType *TT = EntityType->getAs<TypedefType>()) { |
| 8911 | // It's important to use the typedef's name if there is one so that the |
| 8912 | // fixit doesn't break code using types like int64_t. |
| 8913 | // |
| 8914 | // FIXME: This will break if the typedef requires qualification. But |
| 8915 | // getQualifiedNameAsString() includes non-machine-parsable components. |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 8916 | OS << *TT->getDecl(); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 8917 | } else if (const BuiltinType *BT = EntityType->getAs<BuiltinType>()) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8918 | OS << BT->getName(S.getLangOpts()); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 8919 | else { |
| 8920 | // Oops, we didn't find the actual type of the variable. Don't emit a fixit |
| 8921 | // with a broken cast. |
| 8922 | return; |
| 8923 | } |
| 8924 | OS << ">("; |
Alp Toker | b086903 | 2014-05-17 01:13:18 +0000 | [diff] [blame] | 8925 | S.Diag(PostInit->getLocStart(), diag::note_init_list_narrowing_silence) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 8926 | << PostInit->getSourceRange() |
| 8927 | << FixItHint::CreateInsertion(PostInit->getLocStart(), OS.str()) |
| 8928 | << FixItHint::CreateInsertion( |
| 8929 | S.getLocForEndOfToken(PostInit->getLocEnd()), ")"); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 8930 | } |
| 8931 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8932 | //===----------------------------------------------------------------------===// |
| 8933 | // Initialization helper functions |
| 8934 | //===----------------------------------------------------------------------===// |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 8935 | bool |
| 8936 | Sema::CanPerformCopyInitialization(const InitializedEntity &Entity, |
| 8937 | ExprResult Init) { |
| 8938 | if (Init.isInvalid()) |
| 8939 | return false; |
| 8940 | |
| 8941 | Expr *InitE = Init.get(); |
| 8942 | assert(InitE && "No initialization expression"); |
| 8943 | |
Douglas Gregor | f4cc61d | 2012-07-31 22:15:04 +0000 | [diff] [blame] | 8944 | InitializationKind Kind |
| 8945 | = InitializationKind::CreateCopy(InitE->getLocStart(), SourceLocation()); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8946 | InitializationSequence Seq(*this, Entity, Kind, InitE); |
Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 8947 | return !Seq.Failed(); |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 8948 | } |
| 8949 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8950 | ExprResult |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8951 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 8952 | SourceLocation EqualLoc, |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 8953 | ExprResult Init, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 8954 | bool TopLevelOfInitList, |
| 8955 | bool AllowExplicit) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8956 | if (Init.isInvalid()) |
| 8957 | return ExprError(); |
| 8958 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 8959 | Expr *InitE = Init.get(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8960 | assert(InitE && "No initialization expression?"); |
| 8961 | |
| 8962 | if (EqualLoc.isInvalid()) |
| 8963 | EqualLoc = InitE->getLocStart(); |
| 8964 | |
| 8965 | InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(), |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 8966 | EqualLoc, |
| 8967 | AllowExplicit); |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 8968 | InitializationSequence Seq(*this, Entity, Kind, InitE, TopLevelOfInitList); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 8969 | |
Alex Lorenz | de69ff9 | 2017-05-16 10:23:58 +0000 | [diff] [blame] | 8970 | // Prevent infinite recursion when performing parameter copy-initialization. |
| 8971 | const bool ShouldTrackCopy = |
| 8972 | Entity.isParameterKind() && Seq.isConstructorInitialization(); |
| 8973 | if (ShouldTrackCopy) { |
| 8974 | if (llvm::find(CurrentParameterCopyTypes, Entity.getType()) != |
| 8975 | CurrentParameterCopyTypes.end()) { |
| 8976 | Seq.SetOverloadFailure( |
| 8977 | InitializationSequence::FK_ConstructorOverloadFailed, |
| 8978 | OR_No_Viable_Function); |
| 8979 | |
| 8980 | // Try to give a meaningful diagnostic note for the problematic |
| 8981 | // constructor. |
| 8982 | const auto LastStep = Seq.step_end() - 1; |
| 8983 | assert(LastStep->Kind == |
| 8984 | InitializationSequence::SK_ConstructorInitialization); |
| 8985 | const FunctionDecl *Function = LastStep->Function.Function; |
| 8986 | auto Candidate = |
| 8987 | llvm::find_if(Seq.getFailedCandidateSet(), |
| 8988 | [Function](const OverloadCandidate &Candidate) -> bool { |
| 8989 | return Candidate.Viable && |
| 8990 | Candidate.Function == Function && |
| 8991 | Candidate.Conversions.size() > 0; |
| 8992 | }); |
| 8993 | if (Candidate != Seq.getFailedCandidateSet().end() && |
| 8994 | Function->getNumParams() > 0) { |
| 8995 | Candidate->Viable = false; |
| 8996 | Candidate->FailureKind = ovl_fail_bad_conversion; |
| 8997 | Candidate->Conversions[0].setBad(BadConversionSequence::no_conversion, |
| 8998 | InitE, |
| 8999 | Function->getParamDecl(0)->getType()); |
| 9000 | } |
| 9001 | } |
| 9002 | CurrentParameterCopyTypes.push_back(Entity.getType()); |
| 9003 | } |
| 9004 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 9005 | ExprResult Result = Seq.Perform(*this, Entity, Kind, InitE); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9006 | |
Alex Lorenz | de69ff9 | 2017-05-16 10:23:58 +0000 | [diff] [blame] | 9007 | if (ShouldTrackCopy) |
| 9008 | CurrentParameterCopyTypes.pop_back(); |
| 9009 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9010 | return Result; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 9011 | } |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9012 | |
Richard Smith | 1363e8f | 2017-09-07 07:22:36 +0000 | [diff] [blame] | 9013 | /// Determine whether RD is, or is derived from, a specialization of CTD. |
| 9014 | static bool isOrIsDerivedFromSpecializationOf(CXXRecordDecl *RD, |
| 9015 | ClassTemplateDecl *CTD) { |
| 9016 | auto NotSpecialization = [&] (const CXXRecordDecl *Candidate) { |
| 9017 | auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Candidate); |
| 9018 | return !CTSD || !declaresSameEntity(CTSD->getSpecializedTemplate(), CTD); |
| 9019 | }; |
| 9020 | return !(NotSpecialization(RD) && RD->forallBases(NotSpecialization)); |
| 9021 | } |
| 9022 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9023 | QualType Sema::DeduceTemplateSpecializationFromInitializer( |
| 9024 | TypeSourceInfo *TSInfo, const InitializedEntity &Entity, |
| 9025 | const InitializationKind &Kind, MultiExprArg Inits) { |
| 9026 | auto *DeducedTST = dyn_cast<DeducedTemplateSpecializationType>( |
| 9027 | TSInfo->getType()->getContainedDeducedType()); |
| 9028 | assert(DeducedTST && "not a deduced template specialization type"); |
| 9029 | |
| 9030 | // We can only perform deduction for class templates. |
| 9031 | auto TemplateName = DeducedTST->getTemplateName(); |
| 9032 | auto *Template = |
| 9033 | dyn_cast_or_null<ClassTemplateDecl>(TemplateName.getAsTemplateDecl()); |
| 9034 | if (!Template) { |
| 9035 | Diag(Kind.getLocation(), |
| 9036 | diag::err_deduced_non_class_template_specialization_type) |
| 9037 | << (int)getTemplateNameKindForDiagnostics(TemplateName) << TemplateName; |
| 9038 | if (auto *TD = TemplateName.getAsTemplateDecl()) |
| 9039 | Diag(TD->getLocation(), diag::note_template_decl_here); |
| 9040 | return QualType(); |
| 9041 | } |
| 9042 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9043 | // Can't deduce from dependent arguments. |
| 9044 | if (Expr::hasAnyTypeDependentArguments(Inits)) |
| 9045 | return Context.DependentTy; |
| 9046 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9047 | // FIXME: Perform "exact type" matching first, per CWG discussion? |
| 9048 | // Or implement this via an implied 'T(T) -> T' deduction guide? |
| 9049 | |
| 9050 | // FIXME: Do we need/want a std::initializer_list<T> special case? |
| 9051 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9052 | // Look up deduction guides, including those synthesized from constructors. |
| 9053 | // |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9054 | // C++1z [over.match.class.deduct]p1: |
| 9055 | // A set of functions and function templates is formed comprising: |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9056 | // - For each constructor of the class template designated by the |
| 9057 | // template-name, a function template [...] |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9058 | // - For each deduction-guide, a function or function template [...] |
| 9059 | DeclarationNameInfo NameInfo( |
| 9060 | Context.DeclarationNames.getCXXDeductionGuideName(Template), |
| 9061 | TSInfo->getTypeLoc().getEndLoc()); |
| 9062 | LookupResult Guides(*this, NameInfo, LookupOrdinaryName); |
| 9063 | LookupQualifiedName(Guides, Template->getDeclContext()); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9064 | |
| 9065 | // FIXME: Do not diagnose inaccessible deduction guides. The standard isn't |
| 9066 | // clear on this, but they're not found by name so access does not apply. |
| 9067 | Guides.suppressDiagnostics(); |
| 9068 | |
| 9069 | // Figure out if this is list-initialization. |
| 9070 | InitListExpr *ListInit = |
| 9071 | (Inits.size() == 1 && Kind.getKind() != InitializationKind::IK_Direct) |
| 9072 | ? dyn_cast<InitListExpr>(Inits[0]) |
| 9073 | : nullptr; |
| 9074 | |
| 9075 | // C++1z [over.match.class.deduct]p1: |
| 9076 | // Initialization and overload resolution are performed as described in |
| 9077 | // [dcl.init] and [over.match.ctor], [over.match.copy], or [over.match.list] |
| 9078 | // (as appropriate for the type of initialization performed) for an object |
| 9079 | // of a hypothetical class type, where the selected functions and function |
| 9080 | // templates are considered to be the constructors of that class type |
| 9081 | // |
| 9082 | // Since we know we're initializing a class type of a type unrelated to that |
| 9083 | // of the initializer, this reduces to something fairly reasonable. |
| 9084 | OverloadCandidateSet Candidates(Kind.getLocation(), |
| 9085 | OverloadCandidateSet::CSK_Normal); |
| 9086 | OverloadCandidateSet::iterator Best; |
| 9087 | auto tryToResolveOverload = |
| 9088 | [&](bool OnlyListConstructors) -> OverloadingResult { |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 9089 | Candidates.clear(OverloadCandidateSet::CSK_Normal); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9090 | for (auto I = Guides.begin(), E = Guides.end(); I != E; ++I) { |
| 9091 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9092 | if (D->isInvalidDecl()) |
| 9093 | continue; |
| 9094 | |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9095 | auto *TD = dyn_cast<FunctionTemplateDecl>(D); |
| 9096 | auto *GD = dyn_cast_or_null<CXXDeductionGuideDecl>( |
| 9097 | TD ? TD->getTemplatedDecl() : dyn_cast<FunctionDecl>(D)); |
| 9098 | if (!GD) |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9099 | continue; |
| 9100 | |
| 9101 | // C++ [over.match.ctor]p1: (non-list copy-initialization from non-class) |
| 9102 | // For copy-initialization, the candidate functions are all the |
| 9103 | // converting constructors (12.3.1) of that class. |
| 9104 | // C++ [over.match.copy]p1: (non-list copy-initialization from class) |
| 9105 | // The converting constructors of T are candidate functions. |
| 9106 | if (Kind.isCopyInit() && !ListInit) { |
Richard Smith | afe4aa8 | 2017-02-10 02:19:05 +0000 | [diff] [blame] | 9107 | // Only consider converting constructors. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9108 | if (GD->isExplicit()) |
Richard Smith | afe4aa8 | 2017-02-10 02:19:05 +0000 | [diff] [blame] | 9109 | continue; |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9110 | |
| 9111 | // When looking for a converting constructor, deduction guides that |
Richard Smith | afe4aa8 | 2017-02-10 02:19:05 +0000 | [diff] [blame] | 9112 | // could never be called with one argument are not interesting to |
| 9113 | // check or note. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9114 | if (GD->getMinRequiredArguments() > 1 || |
| 9115 | (GD->getNumParams() == 0 && !GD->isVariadic())) |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9116 | continue; |
| 9117 | } |
| 9118 | |
| 9119 | // C++ [over.match.list]p1.1: (first phase list initialization) |
| 9120 | // Initially, the candidate functions are the initializer-list |
| 9121 | // constructors of the class T |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9122 | if (OnlyListConstructors && !isInitListConstructor(GD)) |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9123 | continue; |
| 9124 | |
| 9125 | // C++ [over.match.list]p1.2: (second phase list initialization) |
| 9126 | // the candidate functions are all the constructors of the class T |
| 9127 | // C++ [over.match.ctor]p1: (all other cases) |
| 9128 | // the candidate functions are all the constructors of the class of |
| 9129 | // the object being initialized |
| 9130 | |
| 9131 | // C++ [over.best.ics]p4: |
| 9132 | // When [...] the constructor [...] is a candidate by |
| 9133 | // - [over.match.copy] (in all cases) |
| 9134 | // FIXME: The "second phase of [over.match.list] case can also |
| 9135 | // theoretically happen here, but it's not clear whether we can |
| 9136 | // ever have a parameter of the right type. |
| 9137 | bool SuppressUserConversions = Kind.isCopyInit(); |
| 9138 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9139 | if (TD) |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9140 | AddTemplateOverloadCandidate(TD, I.getPair(), /*ExplicitArgs*/ nullptr, |
| 9141 | Inits, Candidates, |
| 9142 | SuppressUserConversions); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9143 | else |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9144 | AddOverloadCandidate(GD, I.getPair(), Inits, Candidates, |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9145 | SuppressUserConversions); |
| 9146 | } |
| 9147 | return Candidates.BestViableFunction(*this, Kind.getLocation(), Best); |
| 9148 | }; |
| 9149 | |
| 9150 | OverloadingResult Result = OR_No_Viable_Function; |
| 9151 | |
| 9152 | // C++11 [over.match.list]p1, per DR1467: for list-initialization, first |
| 9153 | // try initializer-list constructors. |
| 9154 | if (ListInit) { |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9155 | bool TryListConstructors = true; |
| 9156 | |
| 9157 | // Try list constructors unless the list is empty and the class has one or |
| 9158 | // more default constructors, in which case those constructors win. |
| 9159 | if (!ListInit->getNumInits()) { |
| 9160 | for (NamedDecl *D : Guides) { |
| 9161 | auto *FD = dyn_cast<FunctionDecl>(D->getUnderlyingDecl()); |
| 9162 | if (FD && FD->getMinRequiredArguments() == 0) { |
| 9163 | TryListConstructors = false; |
| 9164 | break; |
| 9165 | } |
| 9166 | } |
Richard Smith | 1363e8f | 2017-09-07 07:22:36 +0000 | [diff] [blame] | 9167 | } else if (ListInit->getNumInits() == 1) { |
| 9168 | // C++ [over.match.class.deduct]: |
| 9169 | // As an exception, the first phase in [over.match.list] (considering |
| 9170 | // initializer-list constructors) is omitted if the initializer list |
| 9171 | // consists of a single expression of type cv U, where U is a |
| 9172 | // specialization of C or a class derived from a specialization of C. |
| 9173 | Expr *E = ListInit->getInit(0); |
| 9174 | auto *RD = E->getType()->getAsCXXRecordDecl(); |
| 9175 | if (!isa<InitListExpr>(E) && RD && |
| 9176 | isOrIsDerivedFromSpecializationOf(RD, Template)) |
| 9177 | TryListConstructors = false; |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9178 | } |
| 9179 | |
| 9180 | if (TryListConstructors) |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9181 | Result = tryToResolveOverload(/*OnlyListConstructor*/true); |
| 9182 | // Then unwrap the initializer list and try again considering all |
| 9183 | // constructors. |
| 9184 | Inits = MultiExprArg(ListInit->getInits(), ListInit->getNumInits()); |
| 9185 | } |
| 9186 | |
| 9187 | // If list-initialization fails, or if we're doing any other kind of |
| 9188 | // initialization, we (eventually) consider constructors. |
| 9189 | if (Result == OR_No_Viable_Function) |
| 9190 | Result = tryToResolveOverload(/*OnlyListConstructor*/false); |
| 9191 | |
| 9192 | switch (Result) { |
| 9193 | case OR_Ambiguous: |
| 9194 | Diag(Kind.getLocation(), diag::err_deduced_class_template_ctor_ambiguous) |
| 9195 | << TemplateName; |
| 9196 | // FIXME: For list-initialization candidates, it'd usually be better to |
| 9197 | // list why they were not viable when given the initializer list itself as |
| 9198 | // an argument. |
| 9199 | Candidates.NoteCandidates(*this, OCD_ViableCandidates, Inits); |
| 9200 | return QualType(); |
| 9201 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9202 | case OR_No_Viable_Function: { |
| 9203 | CXXRecordDecl *Primary = |
| 9204 | cast<ClassTemplateDecl>(Template)->getTemplatedDecl(); |
| 9205 | bool Complete = |
| 9206 | isCompleteType(Kind.getLocation(), Context.getTypeDeclType(Primary)); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9207 | Diag(Kind.getLocation(), |
| 9208 | Complete ? diag::err_deduced_class_template_ctor_no_viable |
| 9209 | : diag::err_deduced_class_template_incomplete) |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9210 | << TemplateName << !Guides.empty(); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9211 | Candidates.NoteCandidates(*this, OCD_AllCandidates, Inits); |
| 9212 | return QualType(); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9213 | } |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9214 | |
| 9215 | case OR_Deleted: { |
| 9216 | Diag(Kind.getLocation(), diag::err_deduced_class_template_deleted) |
| 9217 | << TemplateName; |
| 9218 | NoteDeletedFunction(Best->Function); |
| 9219 | return QualType(); |
| 9220 | } |
| 9221 | |
| 9222 | case OR_Success: |
| 9223 | // C++ [over.match.list]p1: |
| 9224 | // In copy-list-initialization, if an explicit constructor is chosen, the |
| 9225 | // initialization is ill-formed. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9226 | if (Kind.isCopyInit() && ListInit && |
| 9227 | cast<CXXDeductionGuideDecl>(Best->Function)->isExplicit()) { |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9228 | bool IsDeductionGuide = !Best->Function->isImplicit(); |
| 9229 | Diag(Kind.getLocation(), diag::err_deduced_class_template_explicit) |
| 9230 | << TemplateName << IsDeductionGuide; |
| 9231 | Diag(Best->Function->getLocation(), |
| 9232 | diag::note_explicit_ctor_deduction_guide_here) |
| 9233 | << IsDeductionGuide; |
| 9234 | return QualType(); |
| 9235 | } |
| 9236 | |
| 9237 | // Make sure we didn't select an unusable deduction guide, and mark it |
| 9238 | // as referenced. |
| 9239 | DiagnoseUseOfDecl(Best->Function, Kind.getLocation()); |
| 9240 | MarkFunctionReferenced(Kind.getLocation(), Best->Function); |
| 9241 | break; |
| 9242 | } |
| 9243 | |
| 9244 | // C++ [dcl.type.class.deduct]p1: |
| 9245 | // The placeholder is replaced by the return type of the function selected |
| 9246 | // by overload resolution for class template deduction. |
| 9247 | return SubstAutoType(TSInfo->getType(), Best->Function->getReturnType()); |
| 9248 | } |