Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1 | //===--- SemaInit.cpp - Semantic Analysis for Initializers ----------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Sebastian Redl | 26bcc94 | 2011-09-24 17:47:39 +0000 | [diff] [blame] | 9 | // This file implements semantic analysis for initializers. |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 10 | // |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 13 | #include "clang/AST/ASTContext.h" |
John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 14 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 98cee2f | 2009-05-27 16:10:08 +0000 | [diff] [blame] | 15 | #include "clang/AST/ExprCXX.h" |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 16 | #include "clang/AST/ExprObjC.h" |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprOpenMP.h" |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 18 | #include "clang/AST/TypeLoc.h" |
Gabor Horvath | bfe0c37 | 2019-08-14 16:34:56 +0000 | [diff] [blame] | 19 | #include "clang/Basic/CharInfo.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); |
Eli Friedman | 3bf72d7 | 2019-02-08 21:18:46 +0000 | [diff] [blame] | 148 | E->setValueKind(VK_RValue); |
Eli Friedman | 88fccbd | 2019-02-11 22:54:27 +0000 | [diff] [blame] | 149 | if (isa<StringLiteral>(E) || isa<ObjCEncodeExpr>(E)) { |
Richard Smith | d74b1606 | 2013-05-06 00:35:47 +0000 | [diff] [blame] | 150 | break; |
Eli Friedman | 88fccbd | 2019-02-11 22:54:27 +0000 | [diff] [blame] | 151 | } else if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
Richard Smith | d74b1606 | 2013-05-06 00:35:47 +0000 | [diff] [blame] | 152 | E = PE->getSubExpr(); |
Eli Friedman | 88fccbd | 2019-02-11 22:54:27 +0000 | [diff] [blame] | 153 | } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
| 154 | assert(UO->getOpcode() == UO_Extension); |
Richard Smith | d74b1606 | 2013-05-06 00:35:47 +0000 | [diff] [blame] | 155 | E = UO->getSubExpr(); |
Eli Friedman | 88fccbd | 2019-02-11 22:54:27 +0000 | [diff] [blame] | 156 | } else if (GenericSelectionExpr *GSE = dyn_cast<GenericSelectionExpr>(E)) { |
Richard Smith | d74b1606 | 2013-05-06 00:35:47 +0000 | [diff] [blame] | 157 | E = GSE->getResultExpr(); |
Eli Friedman | 88fccbd | 2019-02-11 22:54:27 +0000 | [diff] [blame] | 158 | } else if (ChooseExpr *CE = dyn_cast<ChooseExpr>(E)) { |
| 159 | E = CE->getChosenSubExpr(); |
| 160 | } else { |
Richard Smith | d74b1606 | 2013-05-06 00:35:47 +0000 | [diff] [blame] | 161 | llvm_unreachable("unexpected expr in string literal init"); |
Eli Friedman | 88fccbd | 2019-02-11 22:54:27 +0000 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /// Fix a compound literal initializing an array so it's correctly marked |
| 167 | /// as an rvalue. |
| 168 | static void updateGNUCompoundLiteralRValue(Expr *E) { |
| 169 | while (true) { |
| 170 | E->setValueKind(VK_RValue); |
| 171 | if (isa<CompoundLiteralExpr>(E)) { |
| 172 | break; |
| 173 | } else if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
| 174 | E = PE->getSubExpr(); |
| 175 | } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
| 176 | assert(UO->getOpcode() == UO_Extension); |
| 177 | E = UO->getSubExpr(); |
| 178 | } else if (GenericSelectionExpr *GSE = dyn_cast<GenericSelectionExpr>(E)) { |
| 179 | E = GSE->getResultExpr(); |
| 180 | } else if (ChooseExpr *CE = dyn_cast<ChooseExpr>(E)) { |
| 181 | E = CE->getChosenSubExpr(); |
| 182 | } else { |
| 183 | llvm_unreachable("unexpected expr in array compound literal init"); |
| 184 | } |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 185 | } |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 186 | } |
| 187 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 188 | static void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT, |
| 189 | Sema &S) { |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 190 | // Get the length of the string as parsed. |
Ben Langmuir | 577b393 | 2015-01-26 19:04:10 +0000 | [diff] [blame] | 191 | auto *ConstantArrayTy = |
Ben Langmuir | 7b30f53 | 2015-01-26 20:01:17 +0000 | [diff] [blame] | 192 | cast<ConstantArrayType>(Str->getType()->getAsArrayTypeUnsafe()); |
Ben Langmuir | 577b393 | 2015-01-26 19:04:10 +0000 | [diff] [blame] | 193 | uint64_t StrLength = ConstantArrayTy->getSize().getZExtValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 194 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 195 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 196 | // 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] | 197 | // being initialized to a string literal. |
Benjamin Kramer | e073177 | 2012-08-04 17:00:46 +0000 | [diff] [blame] | 198 | llvm::APInt ConstVal(32, StrLength); |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 199 | // Return a new array type (C99 6.7.8p22). |
John McCall | c5b8225 | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 200 | DeclT = S.Context.getConstantArrayType(IAT->getElementType(), |
| 201 | ConstVal, |
| 202 | ArrayType::Normal, 0); |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 203 | updateStringLiteralType(Str, DeclT); |
Chris Lattner | 94e6c4b | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 204 | return; |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 205 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 206 | |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 207 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 208 | |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 209 | // We have an array of character type with known size. However, |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 210 | // the size may be smaller or larger than the string we are initializing. |
| 211 | // FIXME: Avoid truncation for 64-bit length strings. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 212 | if (S.getLangOpts().CPlusPlus) { |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 213 | if (StringLiteral *SL = dyn_cast<StringLiteral>(Str->IgnoreParens())) { |
Anders Carlsson | d162fb8 | 2011-04-14 00:41:11 +0000 | [diff] [blame] | 214 | // For Pascal strings it's OK to strip off the terminating null character, |
| 215 | // so the example below is valid: |
| 216 | // |
| 217 | // unsigned char a[2] = "\pa"; |
| 218 | if (SL->isPascal()) |
| 219 | StrLength--; |
| 220 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 221 | |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 222 | // [dcl.init.string]p2 |
| 223 | if (StrLength > CAT->getSize().getZExtValue()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 224 | S.Diag(Str->getBeginLoc(), |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 225 | diag::err_initializer_string_for_char_array_too_long) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 226 | << Str->getSourceRange(); |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 227 | } else { |
| 228 | // C99 6.7.8p14. |
| 229 | if (StrLength-1 > CAT->getSize().getZExtValue()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 230 | S.Diag(Str->getBeginLoc(), |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 231 | diag::ext_initializer_string_for_char_array_too_long) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 232 | << Str->getSourceRange(); |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 233 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 234 | |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 235 | // Set the type to the actual size that we are initializing. If we have |
| 236 | // something like: |
| 237 | // char x[1] = "foo"; |
| 238 | // then this will set the string literal's type to char[1]. |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 239 | updateStringLiteralType(Str, DeclT); |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 242 | //===----------------------------------------------------------------------===// |
| 243 | // Semantic checking for initializer lists. |
| 244 | //===----------------------------------------------------------------------===// |
| 245 | |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 246 | namespace { |
| 247 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 248 | /// Semantic checking for initializer lists. |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 249 | /// |
| 250 | /// The InitListChecker class contains a set of routines that each |
| 251 | /// handle the initialization of a certain kind of entity, e.g., |
| 252 | /// arrays, vectors, struct/union types, scalars, etc. The |
| 253 | /// InitListChecker itself performs a recursive walk of the subobject |
| 254 | /// structure of the type to be initialized, while stepping through |
| 255 | /// the initializer list one element at a time. The IList and Index |
| 256 | /// parameters to each of the Check* routines contain the active |
| 257 | /// (syntactic) initializer list and the index into that initializer |
| 258 | /// list that represents the current initializer. Each routine is |
| 259 | /// responsible for moving that Index forward as it consumes elements. |
| 260 | /// |
| 261 | /// Each Check* routine also has a StructuredList/StructuredIndex |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 262 | /// arguments, which contains the current "structured" (semantic) |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 263 | /// initializer list and the index into that initializer list where we |
| 264 | /// are copying initializers as we map them over to the semantic |
| 265 | /// list. Once we have completed our recursive walk of the subobject |
| 266 | /// structure, we will have constructed a full semantic initializer |
| 267 | /// list. |
| 268 | /// |
| 269 | /// C99 designators cause changes in the initializer list traversal, |
| 270 | /// because they make the initialization "jump" into a specific |
| 271 | /// subobject and then continue the initialization from that |
| 272 | /// point. CheckDesignatedInitializer() recursively steps into the |
| 273 | /// designated subobject and manages backing out the recursion to |
| 274 | /// initialize the subobjects after the one designated. |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 275 | /// |
| 276 | /// If an initializer list contains any designators, we build a placeholder |
| 277 | /// structured list even in 'verify only' mode, so that we can track which |
| 278 | /// elements need 'empty' initializtion. |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 279 | class InitListChecker { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 280 | Sema &SemaRef; |
Richard Smith | 33e9be6 | 2019-08-29 22:49:33 +0000 | [diff] [blame] | 281 | bool hadError = false; |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 282 | bool VerifyOnly; // No diagnostics. |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 283 | bool TreatUnavailableAsInvalid; // Used only in VerifyOnly mode. |
Richard Smith | 5030928 | 2019-08-30 22:52:55 +0000 | [diff] [blame] | 284 | bool InOverloadResolution; |
Richard Smith | 33e9be6 | 2019-08-29 22:49:33 +0000 | [diff] [blame] | 285 | InitListExpr *FullyStructuredList = nullptr; |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 286 | NoInitExpr *DummyExpr = nullptr; |
| 287 | |
| 288 | NoInitExpr *getDummyInit() { |
| 289 | if (!DummyExpr) |
| 290 | DummyExpr = new (SemaRef.Context) NoInitExpr(SemaRef.Context.VoidTy); |
| 291 | return DummyExpr; |
| 292 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 293 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 294 | void CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 295 | InitListExpr *ParentIList, QualType T, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 296 | unsigned &Index, InitListExpr *StructuredList, |
Eli Friedman | c616c5f | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 297 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 298 | void CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 299 | InitListExpr *IList, QualType &T, |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 300 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 301 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 302 | void CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 303 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 304 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 305 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 306 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 307 | unsigned &StructuredIndex, |
| 308 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 309 | void CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 310 | InitListExpr *IList, QualType ElemType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 311 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 312 | InitListExpr *StructuredList, |
| 313 | unsigned &StructuredIndex); |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 314 | void CheckComplexType(const InitializedEntity &Entity, |
| 315 | InitListExpr *IList, QualType DeclType, |
| 316 | unsigned &Index, |
| 317 | InitListExpr *StructuredList, |
| 318 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 319 | void CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 320 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 321 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 322 | InitListExpr *StructuredList, |
| 323 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 324 | void CheckReferenceType(const InitializedEntity &Entity, |
| 325 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 326 | unsigned &Index, |
| 327 | InitListExpr *StructuredList, |
| 328 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 329 | void CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 330 | InitListExpr *IList, QualType DeclType, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 331 | InitListExpr *StructuredList, |
| 332 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 333 | void CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 73eb7cd | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 334 | InitListExpr *IList, QualType DeclType, |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 335 | CXXRecordDecl::base_class_range Bases, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 336 | RecordDecl::field_iterator Field, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 337 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 338 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 339 | unsigned &StructuredIndex, |
| 340 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 341 | void CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 342 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 343 | llvm::APSInt elementIndex, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 344 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 345 | InitListExpr *StructuredList, |
| 346 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 347 | bool CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 348 | InitListExpr *IList, DesignatedInitExpr *DIE, |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 349 | unsigned DesigIdx, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 350 | QualType &CurrentObjectType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 351 | RecordDecl::field_iterator *NextField, |
| 352 | llvm::APSInt *NextElementIndex, |
| 353 | unsigned &Index, |
| 354 | InitListExpr *StructuredList, |
| 355 | unsigned &StructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 356 | bool FinishSubobjectInit, |
| 357 | bool TopLevelObject); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 358 | InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 359 | QualType CurrentObjectType, |
| 360 | InitListExpr *StructuredList, |
| 361 | unsigned StructuredIndex, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 362 | SourceRange InitRange, |
| 363 | bool IsFullyOverwritten = false); |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 364 | void UpdateStructuredListElement(InitListExpr *StructuredList, |
| 365 | unsigned &StructuredIndex, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 366 | Expr *expr); |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 367 | InitListExpr *createInitListExpr(QualType CurrentObjectType, |
| 368 | SourceRange InitRange, |
| 369 | unsigned ExpectedNumInits); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 370 | int numArrayElements(QualType DeclType); |
| 371 | int numStructUnionElements(QualType DeclType); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 372 | |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 373 | ExprResult PerformEmptyInit(SourceLocation Loc, |
| 374 | const InitializedEntity &Entity); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 375 | |
Richard Smith | 5030928 | 2019-08-30 22:52:55 +0000 | [diff] [blame] | 376 | /// Diagnose that OldInit (or part thereof) has been overridden by NewInit. |
| 377 | void diagnoseInitOverride(Expr *OldInit, SourceRange NewInitRange, |
| 378 | bool FullyOverwritten = true) { |
| 379 | // Overriding an initializer via a designator is valid with C99 designated |
| 380 | // initializers, but ill-formed with C++20 designated initializers. |
| 381 | unsigned DiagID = SemaRef.getLangOpts().CPlusPlus |
| 382 | ? diag::ext_initializer_overrides |
| 383 | : diag::warn_initializer_overrides; |
| 384 | |
| 385 | if (InOverloadResolution && SemaRef.getLangOpts().CPlusPlus) { |
| 386 | // In overload resolution, we have to strictly enforce the rules, and so |
| 387 | // don't allow any overriding of prior initializers. This matters for a |
| 388 | // case such as: |
| 389 | // |
| 390 | // union U { int a, b; }; |
| 391 | // struct S { int a, b; }; |
| 392 | // void f(U), f(S); |
| 393 | // |
| 394 | // Here, f({.a = 1, .b = 2}) is required to call the struct overload. For |
| 395 | // consistency, we disallow all overriding of prior initializers in |
| 396 | // overload resolution, not only overriding of union members. |
| 397 | hadError = true; |
| 398 | } else if (OldInit->getType().isDestructedType() && !FullyOverwritten) { |
| 399 | // If we'll be keeping around the old initializer but overwriting part of |
| 400 | // the object it initialized, and that object is not trivially |
| 401 | // destructible, this can leak. Don't allow that, not even as an |
| 402 | // extension. |
| 403 | // |
| 404 | // FIXME: It might be reasonable to allow this in cases where the part of |
| 405 | // the initializer that we're overriding has trivial destruction. |
| 406 | DiagID = diag::err_initializer_overrides_destructed; |
| 407 | } else if (!OldInit->getSourceRange().isValid()) { |
| 408 | // We need to check on source range validity because the previous |
| 409 | // initializer does not have to be an explicit initializer. e.g., |
| 410 | // |
| 411 | // struct P { int a, b; }; |
| 412 | // struct PP { struct P p } l = { { .a = 2 }, .p.b = 3 }; |
| 413 | // |
| 414 | // There is an overwrite taking place because the first braced initializer |
| 415 | // list "{ .a = 2 }" already provides value for .p.b (which is zero). |
| 416 | // |
| 417 | // Such overwrites are harmless, so we don't diagnose them. (Note that in |
| 418 | // C++, this cannot be reached unless we've already seen and diagnosed a |
| 419 | // different conformance issue, such as a mixture of designated and |
| 420 | // non-designated initializers or a multi-level designator.) |
| 421 | return; |
| 422 | } |
| 423 | |
| 424 | if (!VerifyOnly) { |
| 425 | SemaRef.Diag(NewInitRange.getBegin(), DiagID) |
| 426 | << NewInitRange << FullyOverwritten << OldInit->getType(); |
| 427 | SemaRef.Diag(OldInit->getBeginLoc(), diag::note_previous_initializer) |
| 428 | << (OldInit->HasSideEffects(SemaRef.Context) && FullyOverwritten) |
| 429 | << OldInit->getSourceRange(); |
| 430 | } |
| 431 | } |
| 432 | |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 433 | // Explanation on the "FillWithNoInit" mode: |
| 434 | // |
| 435 | // Assume we have the following definitions (Case#1): |
| 436 | // struct P { char x[6][6]; } xp = { .x[1] = "bar" }; |
| 437 | // struct PP { struct P lp; } l = { .lp = xp, .lp.x[1][2] = 'f' }; |
| 438 | // |
| 439 | // l.lp.x[1][0..1] should not be filled with implicit initializers because the |
| 440 | // "base" initializer "xp" will provide values for them; l.lp.x[1] will be "baf". |
| 441 | // |
| 442 | // But if we have (Case#2): |
| 443 | // struct PP l = { .lp = xp, .lp.x[1] = { [2] = 'f' } }; |
| 444 | // |
| 445 | // l.lp.x[1][0..1] are implicitly initialized and do not use values from the |
| 446 | // "base" initializer; l.lp.x[1] will be "\0\0f\0\0\0". |
| 447 | // |
| 448 | // To distinguish Case#1 from Case#2, and also to avoid leaving many "holes" |
| 449 | // in the InitListExpr, the "holes" in Case#1 are filled not with empty |
| 450 | // initializers but with special "NoInitExpr" place holders, which tells the |
| 451 | // CodeGen not to generate any initializers for these parts. |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 452 | void FillInEmptyInitForBase(unsigned Init, const CXXBaseSpecifier &Base, |
| 453 | const InitializedEntity &ParentEntity, |
| 454 | InitListExpr *ILE, bool &RequiresSecondPass, |
| 455 | bool FillWithNoInit); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 456 | void FillInEmptyInitForField(unsigned Init, FieldDecl *Field, |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 457 | const InitializedEntity &ParentEntity, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 458 | InitListExpr *ILE, bool &RequiresSecondPass, |
| 459 | bool FillWithNoInit = false); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 460 | void FillInEmptyInitializations(const InitializedEntity &Entity, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 461 | InitListExpr *ILE, bool &RequiresSecondPass, |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 462 | InitListExpr *OuterILE, unsigned OuterIndex, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 463 | bool FillWithNoInit = false); |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 464 | bool CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 465 | Expr *InitExpr, FieldDecl *Field, |
| 466 | bool TopLevelObject); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 467 | void CheckEmptyInitializable(const InitializedEntity &Entity, |
| 468 | SourceLocation Loc); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 469 | |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 470 | public: |
Richard Smith | 5030928 | 2019-08-30 22:52:55 +0000 | [diff] [blame] | 471 | InitListChecker(Sema &S, const InitializedEntity &Entity, InitListExpr *IL, |
| 472 | QualType &T, bool VerifyOnly, bool TreatUnavailableAsInvalid, |
| 473 | bool InOverloadResolution = false); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 474 | bool HadError() { return hadError; } |
| 475 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 476 | // Retrieves the fully-structured initializer list used for |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 477 | // semantic analysis and code generation. |
| 478 | InitListExpr *getFullyStructuredList() const { return FullyStructuredList; } |
| 479 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 480 | |
Chris Lattner | 9ececce | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 481 | } // end anonymous namespace |
Chris Lattner | d9ae05b | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 482 | |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 483 | ExprResult InitListChecker::PerformEmptyInit(SourceLocation Loc, |
| 484 | const InitializedEntity &Entity) { |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 485 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 486 | true); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 487 | MultiExprArg SubInit; |
| 488 | Expr *InitExpr; |
| 489 | InitListExpr DummyInitList(SemaRef.Context, Loc, None, Loc); |
| 490 | |
| 491 | // C++ [dcl.init.aggr]p7: |
| 492 | // If there are fewer initializer-clauses in the list than there are |
| 493 | // members in the aggregate, then each member not explicitly initialized |
| 494 | // ... |
Nico Weber | bcb70ee | 2014-07-02 23:51:09 +0000 | [diff] [blame] | 495 | bool EmptyInitList = SemaRef.getLangOpts().CPlusPlus11 && |
| 496 | Entity.getType()->getBaseElementTypeUnsafe()->isRecordType(); |
| 497 | if (EmptyInitList) { |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 498 | // C++1y / DR1070: |
| 499 | // shall be initialized [...] from an empty initializer list. |
| 500 | // |
| 501 | // We apply the resolution of this DR to C++11 but not C++98, since C++98 |
| 502 | // does not have useful semantics for initialization from an init list. |
| 503 | // We treat this as copy-initialization, because aggregate initialization |
| 504 | // always performs copy-initialization on its elements. |
| 505 | // |
| 506 | // Only do this if we're initializing a class type, to avoid filling in |
| 507 | // the initializer list where possible. |
| 508 | InitExpr = VerifyOnly ? &DummyInitList : new (SemaRef.Context) |
| 509 | InitListExpr(SemaRef.Context, Loc, None, Loc); |
| 510 | InitExpr->setType(SemaRef.Context.VoidTy); |
| 511 | SubInit = InitExpr; |
| 512 | Kind = InitializationKind::CreateCopy(Loc, Loc); |
| 513 | } else { |
| 514 | // C++03: |
| 515 | // shall be value-initialized. |
| 516 | } |
| 517 | |
| 518 | InitializationSequence InitSeq(SemaRef, Entity, Kind, SubInit); |
Nico Weber | bcb70ee | 2014-07-02 23:51:09 +0000 | [diff] [blame] | 519 | // libstdc++4.6 marks the vector default constructor as explicit in |
| 520 | // _GLIBCXX_DEBUG mode, so recover using the C++03 logic in that case. |
| 521 | // stlport does so too. Look for std::__debug for libstdc++, and for |
| 522 | // std:: for stlport. This is effectively a compiler-side implementation of |
| 523 | // LWG2193. |
| 524 | if (!InitSeq && EmptyInitList && InitSeq.getFailureKind() == |
| 525 | InitializationSequence::FK_ExplicitConstructor) { |
| 526 | OverloadCandidateSet::iterator Best; |
| 527 | OverloadingResult O = |
| 528 | InitSeq.getFailedCandidateSet() |
| 529 | .BestViableFunction(SemaRef, Kind.getLocation(), Best); |
| 530 | (void)O; |
| 531 | assert(O == OR_Success && "Inconsistent overload resolution"); |
| 532 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 533 | CXXRecordDecl *R = CtorDecl->getParent(); |
| 534 | |
| 535 | if (CtorDecl->getMinRequiredArguments() == 0 && |
| 536 | CtorDecl->isExplicit() && R->getDeclName() && |
| 537 | SemaRef.SourceMgr.isInSystemHeader(CtorDecl->getLocation())) { |
Nico Weber | bcb70ee | 2014-07-02 23:51:09 +0000 | [diff] [blame] | 538 | bool IsInStd = false; |
| 539 | for (NamespaceDecl *ND = dyn_cast<NamespaceDecl>(R->getDeclContext()); |
Nico Weber | 5752ad0 | 2014-07-03 00:38:25 +0000 | [diff] [blame] | 540 | ND && !IsInStd; ND = dyn_cast<NamespaceDecl>(ND->getParent())) { |
Nico Weber | bcb70ee | 2014-07-02 23:51:09 +0000 | [diff] [blame] | 541 | if (SemaRef.getStdNamespace()->InEnclosingNamespaceSetOf(ND)) |
| 542 | IsInStd = true; |
| 543 | } |
| 544 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 545 | if (IsInStd && llvm::StringSwitch<bool>(R->getName()) |
Nico Weber | bcb70ee | 2014-07-02 23:51:09 +0000 | [diff] [blame] | 546 | .Cases("basic_string", "deque", "forward_list", true) |
| 547 | .Cases("list", "map", "multimap", "multiset", true) |
| 548 | .Cases("priority_queue", "queue", "set", "stack", true) |
| 549 | .Cases("unordered_map", "unordered_set", "vector", true) |
| 550 | .Default(false)) { |
| 551 | InitSeq.InitializeFrom( |
| 552 | SemaRef, Entity, |
| 553 | InitializationKind::CreateValue(Loc, Loc, Loc, true), |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 554 | MultiExprArg(), /*TopLevelOfInitList=*/false, |
| 555 | TreatUnavailableAsInvalid); |
Nico Weber | bcb70ee | 2014-07-02 23:51:09 +0000 | [diff] [blame] | 556 | // Emit a warning for this. System header warnings aren't shown |
| 557 | // by default, but people working on system headers should see it. |
| 558 | if (!VerifyOnly) { |
| 559 | SemaRef.Diag(CtorDecl->getLocation(), |
| 560 | diag::warn_invalid_initializer_from_system_header); |
David Majnemer | 9588a95 | 2015-08-21 06:44:10 +0000 | [diff] [blame] | 561 | if (Entity.getKind() == InitializedEntity::EK_Member) |
| 562 | SemaRef.Diag(Entity.getDecl()->getLocation(), |
| 563 | diag::note_used_in_initialization_here); |
| 564 | else if (Entity.getKind() == InitializedEntity::EK_ArrayElement) |
| 565 | SemaRef.Diag(Loc, diag::note_used_in_initialization_here); |
Nico Weber | bcb70ee | 2014-07-02 23:51:09 +0000 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | } |
| 569 | } |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 570 | if (!InitSeq) { |
| 571 | if (!VerifyOnly) { |
| 572 | InitSeq.Diagnose(SemaRef, Entity, Kind, SubInit); |
| 573 | if (Entity.getKind() == InitializedEntity::EK_Member) |
| 574 | SemaRef.Diag(Entity.getDecl()->getLocation(), |
| 575 | diag::note_in_omitted_aggregate_initializer) |
| 576 | << /*field*/1 << Entity.getDecl(); |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 577 | else if (Entity.getKind() == InitializedEntity::EK_ArrayElement) { |
| 578 | bool IsTrailingArrayNewMember = |
| 579 | Entity.getParent() && |
| 580 | Entity.getParent()->isVariableLengthArrayNew(); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 581 | SemaRef.Diag(Loc, diag::note_in_omitted_aggregate_initializer) |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 582 | << (IsTrailingArrayNewMember ? 2 : /*array element*/0) |
| 583 | << Entity.getElementIndex(); |
| 584 | } |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 585 | } |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 586 | hadError = true; |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 587 | return ExprError(); |
| 588 | } |
| 589 | |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 590 | return VerifyOnly ? ExprResult() |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 591 | : InitSeq.Perform(SemaRef, Entity, Kind, SubInit); |
| 592 | } |
| 593 | |
| 594 | void InitListChecker::CheckEmptyInitializable(const InitializedEntity &Entity, |
| 595 | SourceLocation Loc) { |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 596 | // If we're building a fully-structured list, we'll check this at the end |
| 597 | // once we know which elements are actually initialized. Otherwise, we know |
| 598 | // that there are no designators so we can just check now. |
| 599 | if (FullyStructuredList) |
| 600 | return; |
| 601 | PerformEmptyInit(Loc, Entity); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 604 | void InitListChecker::FillInEmptyInitForBase( |
| 605 | unsigned Init, const CXXBaseSpecifier &Base, |
| 606 | const InitializedEntity &ParentEntity, InitListExpr *ILE, |
| 607 | bool &RequiresSecondPass, bool FillWithNoInit) { |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 608 | InitializedEntity BaseEntity = InitializedEntity::InitializeBase( |
| 609 | SemaRef.Context, &Base, false, &ParentEntity); |
| 610 | |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 611 | if (Init >= ILE->getNumInits() || !ILE->getInit(Init)) { |
| 612 | ExprResult BaseInit = FillWithNoInit |
| 613 | ? new (SemaRef.Context) NoInitExpr(Base.getType()) |
| 614 | : PerformEmptyInit(ILE->getEndLoc(), BaseEntity); |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 615 | if (BaseInit.isInvalid()) { |
| 616 | hadError = true; |
| 617 | return; |
| 618 | } |
| 619 | |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 620 | if (!VerifyOnly) { |
| 621 | assert(Init < ILE->getNumInits() && "should have been expanded"); |
| 622 | ILE->setInit(Init, BaseInit.getAs<Expr>()); |
| 623 | } |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 624 | } else if (InitListExpr *InnerILE = |
| 625 | dyn_cast<InitListExpr>(ILE->getInit(Init))) { |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 626 | FillInEmptyInitializations(BaseEntity, InnerILE, RequiresSecondPass, |
| 627 | ILE, Init, FillWithNoInit); |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 628 | } else if (DesignatedInitUpdateExpr *InnerDIUE = |
| 629 | dyn_cast<DesignatedInitUpdateExpr>(ILE->getInit(Init))) { |
| 630 | FillInEmptyInitializations(BaseEntity, InnerDIUE->getUpdater(), |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 631 | RequiresSecondPass, ILE, Init, |
| 632 | /*FillWithNoInit =*/true); |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 633 | } |
| 634 | } |
| 635 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 636 | void InitListChecker::FillInEmptyInitForField(unsigned Init, FieldDecl *Field, |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 637 | const InitializedEntity &ParentEntity, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 638 | InitListExpr *ILE, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 639 | bool &RequiresSecondPass, |
| 640 | bool FillWithNoInit) { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 641 | SourceLocation Loc = ILE->getEndLoc(); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 642 | unsigned NumInits = ILE->getNumInits(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 643 | InitializedEntity MemberEntity |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 644 | = InitializedEntity::InitializeMember(Field, &ParentEntity); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 645 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 646 | if (Init >= NumInits || !ILE->getInit(Init)) { |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 647 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) |
| 648 | if (!RType->getDecl()->isUnion()) |
| 649 | assert((Init < NumInits || VerifyOnly) && |
| 650 | "This ILE should have been expanded"); |
| 651 | |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 652 | if (FillWithNoInit) { |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 653 | assert(!VerifyOnly && "should not fill with no-init in verify-only mode"); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 654 | Expr *Filler = new (SemaRef.Context) NoInitExpr(Field->getType()); |
| 655 | if (Init < NumInits) |
| 656 | ILE->setInit(Init, Filler); |
| 657 | else |
| 658 | ILE->updateInit(SemaRef.Context, Init, Filler); |
| 659 | return; |
| 660 | } |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 661 | // C++1y [dcl.init.aggr]p7: |
| 662 | // If there are fewer initializer-clauses in the list than there are |
| 663 | // members in the aggregate, then each member not explicitly initialized |
| 664 | // shall be initialized from its brace-or-equal-initializer [...] |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 665 | if (Field->hasInClassInitializer()) { |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 666 | if (VerifyOnly) |
| 667 | return; |
| 668 | |
Reid Kleckner | d60b82f | 2014-11-17 23:36:45 +0000 | [diff] [blame] | 669 | ExprResult DIE = SemaRef.BuildCXXDefaultInitExpr(Loc, Field); |
| 670 | if (DIE.isInvalid()) { |
| 671 | hadError = true; |
| 672 | return; |
| 673 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 674 | SemaRef.checkInitializerLifetime(MemberEntity, DIE.get()); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 675 | if (Init < NumInits) |
Reid Kleckner | d60b82f | 2014-11-17 23:36:45 +0000 | [diff] [blame] | 676 | ILE->setInit(Init, DIE.get()); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 677 | else { |
Reid Kleckner | d60b82f | 2014-11-17 23:36:45 +0000 | [diff] [blame] | 678 | ILE->updateInit(SemaRef.Context, Init, DIE.get()); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 679 | RequiresSecondPass = true; |
| 680 | } |
| 681 | return; |
| 682 | } |
| 683 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 684 | if (Field->getType()->isReferenceType()) { |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 685 | if (!VerifyOnly) { |
| 686 | // C++ [dcl.init.aggr]p9: |
| 687 | // If an incomplete or empty initializer-list leaves a |
| 688 | // member of reference type uninitialized, the program is |
| 689 | // ill-formed. |
| 690 | SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized) |
| 691 | << Field->getType() |
| 692 | << ILE->getSyntacticForm()->getSourceRange(); |
| 693 | SemaRef.Diag(Field->getLocation(), |
| 694 | diag::note_uninit_reference_member); |
| 695 | } |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 696 | hadError = true; |
| 697 | return; |
| 698 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 699 | |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 700 | ExprResult MemberInit = PerformEmptyInit(Loc, MemberEntity); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 701 | if (MemberInit.isInvalid()) { |
| 702 | hadError = true; |
| 703 | return; |
| 704 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 705 | |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 706 | if (hadError || VerifyOnly) { |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 707 | // Do nothing |
| 708 | } else if (Init < NumInits) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 709 | ILE->setInit(Init, MemberInit.getAs<Expr>()); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 710 | } else if (!isa<ImplicitValueInitExpr>(MemberInit.get())) { |
| 711 | // Empty initialization requires a constructor call, so |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 712 | // extend the initializer list to include the constructor |
| 713 | // call and make a note that we'll need to take another pass |
| 714 | // through the initializer list. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 715 | ILE->updateInit(SemaRef.Context, Init, MemberInit.getAs<Expr>()); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 716 | RequiresSecondPass = true; |
| 717 | } |
| 718 | } else if (InitListExpr *InnerILE |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 719 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) { |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 720 | FillInEmptyInitializations(MemberEntity, InnerILE, |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 721 | RequiresSecondPass, ILE, Init, FillWithNoInit); |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 722 | } else if (DesignatedInitUpdateExpr *InnerDIUE = |
| 723 | dyn_cast<DesignatedInitUpdateExpr>(ILE->getInit(Init))) { |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 724 | FillInEmptyInitializations(MemberEntity, InnerDIUE->getUpdater(), |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 725 | RequiresSecondPass, ILE, Init, |
| 726 | /*FillWithNoInit =*/true); |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 727 | } |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 730 | /// Recursively replaces NULL values within the given initializer list |
| 731 | /// with expressions that perform value-initialization of the |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 732 | /// appropriate type, and finish off the InitListExpr formation. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 733 | void |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 734 | InitListChecker::FillInEmptyInitializations(const InitializedEntity &Entity, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 735 | InitListExpr *ILE, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 736 | bool &RequiresSecondPass, |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 737 | InitListExpr *OuterILE, |
| 738 | unsigned OuterIndex, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 739 | bool FillWithNoInit) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 740 | assert((ILE->getType() != SemaRef.Context.VoidTy) && |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 741 | "Should not have void type"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 742 | |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 743 | // We don't need to do any checks when just filling NoInitExprs; that can't |
| 744 | // fail. |
| 745 | if (FillWithNoInit && VerifyOnly) |
| 746 | return; |
| 747 | |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 748 | // If this is a nested initializer list, we might have changed its contents |
| 749 | // (and therefore some of its properties, such as instantiation-dependence) |
| 750 | // while filling it in. Inform the outer initializer list so that its state |
| 751 | // can be updated to match. |
| 752 | // FIXME: We should fully build the inner initializers before constructing |
| 753 | // the outer InitListExpr instead of mutating AST nodes after they have |
| 754 | // been used as subexpressions of other nodes. |
| 755 | struct UpdateOuterILEWithUpdatedInit { |
| 756 | InitListExpr *Outer; |
| 757 | unsigned OuterIndex; |
| 758 | ~UpdateOuterILEWithUpdatedInit() { |
| 759 | if (Outer) |
| 760 | Outer->setInit(OuterIndex, Outer->getInit(OuterIndex)); |
| 761 | } |
| 762 | } UpdateOuterRAII = {OuterILE, OuterIndex}; |
| 763 | |
Richard Smith | 382bc51 | 2017-02-23 22:41:47 +0000 | [diff] [blame] | 764 | // A transparent ILE is not performing aggregate initialization and should |
| 765 | // not be filled in. |
| 766 | if (ILE->isTransparent()) |
| 767 | return; |
| 768 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 769 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 770 | const RecordDecl *RDecl = RType->getDecl(); |
| 771 | if (RDecl->isUnion() && ILE->getInitializedFieldInUnion()) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 772 | FillInEmptyInitForField(0, ILE->getInitializedFieldInUnion(), |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 773 | Entity, ILE, RequiresSecondPass, FillWithNoInit); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 774 | else if (RDecl->isUnion() && isa<CXXRecordDecl>(RDecl) && |
| 775 | cast<CXXRecordDecl>(RDecl)->hasInClassInitializer()) { |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 776 | for (auto *Field : RDecl->fields()) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 777 | if (Field->hasInClassInitializer()) { |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 778 | FillInEmptyInitForField(0, Field, Entity, ILE, RequiresSecondPass, |
| 779 | FillWithNoInit); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 780 | break; |
| 781 | } |
| 782 | } |
| 783 | } else { |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 784 | // The fields beyond ILE->getNumInits() are default initialized, so in |
| 785 | // order to leave them uninitialized, the ILE is expanded and the extra |
| 786 | // fields are then filled with NoInitExpr. |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 787 | unsigned NumElems = numStructUnionElements(ILE->getType()); |
| 788 | if (RDecl->hasFlexibleArrayMember()) |
| 789 | ++NumElems; |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 790 | if (!VerifyOnly && ILE->getNumInits() < NumElems) |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 791 | ILE->resizeInits(SemaRef.Context, NumElems); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 792 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 793 | unsigned Init = 0; |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 794 | |
| 795 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RDecl)) { |
| 796 | for (auto &Base : CXXRD->bases()) { |
| 797 | if (hadError) |
| 798 | return; |
| 799 | |
| 800 | FillInEmptyInitForBase(Init, Base, Entity, ILE, RequiresSecondPass, |
| 801 | FillWithNoInit); |
| 802 | ++Init; |
| 803 | } |
| 804 | } |
| 805 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 806 | for (auto *Field : RDecl->fields()) { |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 807 | if (Field->isUnnamedBitfield()) |
| 808 | continue; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 809 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 810 | if (hadError) |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 811 | return; |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 812 | |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 813 | FillInEmptyInitForField(Init, Field, Entity, ILE, RequiresSecondPass, |
| 814 | FillWithNoInit); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 815 | if (hadError) |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 816 | return; |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 817 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 818 | ++Init; |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 819 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 820 | // Only look at the first initialization of a union. |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 821 | if (RDecl->isUnion()) |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 822 | break; |
| 823 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 827 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 828 | |
| 829 | QualType ElementType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 830 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 831 | InitializedEntity ElementEntity = Entity; |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 832 | unsigned NumInits = ILE->getNumInits(); |
| 833 | unsigned NumElements = NumInits; |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 834 | if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 835 | ElementType = AType->getElementType(); |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 836 | if (const auto *CAType = dyn_cast<ConstantArrayType>(AType)) |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 837 | NumElements = CAType->getSize().getZExtValue(); |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 838 | // For an array new with an unknown bound, ask for one additional element |
| 839 | // in order to populate the array filler. |
| 840 | if (Entity.isVariableLengthArrayNew()) |
| 841 | ++NumElements; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 842 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 843 | 0, Entity); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 844 | } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 845 | ElementType = VType->getElementType(); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 846 | NumElements = VType->getNumElements(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 847 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 848 | 0, Entity); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 849 | } else |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 850 | ElementType = ILE->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 851 | |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 852 | bool SkipEmptyInitChecks = false; |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 853 | for (unsigned Init = 0; Init != NumElements; ++Init) { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 854 | if (hadError) |
| 855 | return; |
| 856 | |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 857 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement || |
| 858 | ElementEntity.getKind() == InitializedEntity::EK_VectorElement) |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 859 | ElementEntity.setElementIndex(Init); |
| 860 | |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 861 | if (Init >= NumInits && (ILE->hasArrayFiller() || SkipEmptyInitChecks)) |
Richard Smith | 3e26863 | 2018-05-23 23:41:38 +0000 | [diff] [blame] | 862 | return; |
| 863 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 864 | Expr *InitExpr = (Init < NumInits ? ILE->getInit(Init) : nullptr); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 865 | if (!InitExpr && Init < NumInits && ILE->hasArrayFiller()) |
| 866 | ILE->setInit(Init, ILE->getArrayFiller()); |
| 867 | else if (!InitExpr && !ILE->hasArrayFiller()) { |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 868 | // In VerifyOnly mode, there's no point performing empty initialization |
| 869 | // more than once. |
| 870 | if (SkipEmptyInitChecks) |
| 871 | continue; |
| 872 | |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 873 | Expr *Filler = nullptr; |
| 874 | |
| 875 | if (FillWithNoInit) |
| 876 | Filler = new (SemaRef.Context) NoInitExpr(ElementType); |
| 877 | else { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 878 | ExprResult ElementInit = |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 879 | PerformEmptyInit(ILE->getEndLoc(), ElementEntity); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 880 | if (ElementInit.isInvalid()) { |
| 881 | hadError = true; |
| 882 | return; |
| 883 | } |
| 884 | |
| 885 | Filler = ElementInit.getAs<Expr>(); |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | if (hadError) { |
| 889 | // Do nothing |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 890 | } else if (VerifyOnly) { |
| 891 | SkipEmptyInitChecks = true; |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 892 | } else if (Init < NumInits) { |
Argyrios Kyrtzidis | 446bcf2 | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 893 | // For arrays, just set the expression used for value-initialization |
| 894 | // of the "holes" in the array. |
| 895 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 896 | ILE->setArrayFiller(Filler); |
Argyrios Kyrtzidis | 446bcf2 | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 897 | else |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 898 | ILE->setInit(Init, Filler); |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 899 | } else { |
| 900 | // For arrays, just set the expression used for value-initialization |
| 901 | // of the rest of elements and exit. |
| 902 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) { |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 903 | ILE->setArrayFiller(Filler); |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 904 | return; |
| 905 | } |
| 906 | |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 907 | if (!isa<ImplicitValueInitExpr>(Filler) && !isa<NoInitExpr>(Filler)) { |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 908 | // Empty initialization requires a constructor call, so |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 909 | // extend the initializer list to include the constructor |
| 910 | // call and make a note that we'll need to take another pass |
| 911 | // through the initializer list. |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 912 | ILE->updateInit(SemaRef.Context, Init, Filler); |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 913 | RequiresSecondPass = true; |
| 914 | } |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 915 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 916 | } else if (InitListExpr *InnerILE |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 917 | = dyn_cast_or_null<InitListExpr>(InitExpr)) { |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 918 | FillInEmptyInitializations(ElementEntity, InnerILE, RequiresSecondPass, |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 919 | ILE, Init, FillWithNoInit); |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 920 | } else if (DesignatedInitUpdateExpr *InnerDIUE = |
| 921 | dyn_cast_or_null<DesignatedInitUpdateExpr>(InitExpr)) { |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 922 | FillInEmptyInitializations(ElementEntity, InnerDIUE->getUpdater(), |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 923 | RequiresSecondPass, ILE, Init, |
| 924 | /*FillWithNoInit =*/true); |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 925 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 926 | } |
| 927 | } |
| 928 | |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 929 | static bool hasAnyDesignatedInits(const InitListExpr *IL) { |
| 930 | for (const Stmt *Init : *IL) |
| 931 | if (Init && isa<DesignatedInitExpr>(Init)) |
| 932 | return true; |
| 933 | return false; |
| 934 | } |
| 935 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 936 | InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity, |
Richard Smith | 33e9be6 | 2019-08-29 22:49:33 +0000 | [diff] [blame] | 937 | InitListExpr *IL, QualType &T, bool VerifyOnly, |
Richard Smith | 5030928 | 2019-08-30 22:52:55 +0000 | [diff] [blame] | 938 | bool TreatUnavailableAsInvalid, |
| 939 | bool InOverloadResolution) |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 940 | : SemaRef(S), VerifyOnly(VerifyOnly), |
Richard Smith | 5030928 | 2019-08-30 22:52:55 +0000 | [diff] [blame] | 941 | TreatUnavailableAsInvalid(TreatUnavailableAsInvalid), |
| 942 | InOverloadResolution(InOverloadResolution) { |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 943 | if (!VerifyOnly || hasAnyDesignatedInits(IL)) { |
Richard Smith | 33e9be6 | 2019-08-29 22:49:33 +0000 | [diff] [blame] | 944 | FullyStructuredList = |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 945 | createInitListExpr(T, IL->getSourceRange(), IL->getNumInits()); |
| 946 | |
| 947 | // FIXME: Check that IL isn't already the semantic form of some other |
| 948 | // InitListExpr. If it is, we'd create a broken AST. |
| 949 | if (!VerifyOnly) |
| 950 | FullyStructuredList->setSyntacticForm(IL); |
Richard Smith | 33e9be6 | 2019-08-29 22:49:33 +0000 | [diff] [blame] | 951 | } |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 952 | |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 953 | CheckExplicitInitList(Entity, IL, T, FullyStructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 954 | /*TopLevelObject=*/true); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 955 | |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 956 | if (!hadError && FullyStructuredList) { |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 957 | bool RequiresSecondPass = false; |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 958 | FillInEmptyInitializations(Entity, FullyStructuredList, RequiresSecondPass, |
| 959 | /*OuterILE=*/nullptr, /*OuterIndex=*/0); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 960 | if (RequiresSecondPass && !hadError) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 961 | FillInEmptyInitializations(Entity, FullyStructuredList, |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 962 | RequiresSecondPass, nullptr, 0); |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 963 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 967 | // FIXME: use a proper constant |
| 968 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 969 | if (const ConstantArrayType *CAT = |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 970 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 971 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 972 | } |
| 973 | return maxElements; |
| 974 | } |
| 975 | |
| 976 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 977 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 978 | int InitializableMembers = 0; |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 979 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(structDecl)) |
| 980 | InitializableMembers += CXXRD->getNumBases(); |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 981 | for (const auto *Field : structDecl->fields()) |
Douglas Gregor | 556e586 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 982 | if (!Field->isUnnamedBitfield()) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 983 | ++InitializableMembers; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 984 | |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 985 | if (structDecl->isUnion()) |
Eli Friedman | 0e56c82 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 986 | return std::min(InitializableMembers, 1); |
| 987 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 988 | } |
| 989 | |
Richard Smith | 283e207 | 2017-10-03 20:36:00 +0000 | [diff] [blame] | 990 | /// Determine whether Entity is an entity for which it is idiomatic to elide |
| 991 | /// the braces in aggregate initialization. |
| 992 | static bool isIdiomaticBraceElisionEntity(const InitializedEntity &Entity) { |
| 993 | // Recursive initialization of the one and only field within an aggregate |
| 994 | // class is considered idiomatic. This case arises in particular for |
| 995 | // initialization of std::array, where the C++ standard suggests the idiom of |
| 996 | // |
| 997 | // std::array<T, N> arr = {1, 2, 3}; |
| 998 | // |
| 999 | // (where std::array is an aggregate struct containing a single array field. |
| 1000 | |
| 1001 | // FIXME: Should aggregate initialization of a struct with a single |
| 1002 | // base class and no members also suppress the warning? |
| 1003 | if (Entity.getKind() != InitializedEntity::EK_Member || !Entity.getParent()) |
| 1004 | return false; |
| 1005 | |
| 1006 | auto *ParentRD = |
| 1007 | Entity.getParent()->getType()->castAs<RecordType>()->getDecl(); |
| 1008 | if (CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(ParentRD)) |
| 1009 | if (CXXRD->getNumBases()) |
| 1010 | return false; |
| 1011 | |
| 1012 | auto FieldIt = ParentRD->field_begin(); |
| 1013 | assert(FieldIt != ParentRD->field_end() && |
| 1014 | "no fields but have initializer for member?"); |
| 1015 | return ++FieldIt == ParentRD->field_end(); |
| 1016 | } |
| 1017 | |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 1018 | /// Check whether the range of the initializer \p ParentIList from element |
| 1019 | /// \p Index onwards can be used to initialize an object of type \p T. Update |
| 1020 | /// \p Index to indicate how many elements of the list were consumed. |
| 1021 | /// |
| 1022 | /// This also fills in \p StructuredList, from element \p StructuredIndex |
| 1023 | /// onwards, with the fully-braced, desugared form of the initialization. |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1024 | void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 1025 | InitListExpr *ParentIList, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1026 | QualType T, unsigned &Index, |
| 1027 | InitListExpr *StructuredList, |
Eli Friedman | c616c5f | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 1028 | unsigned &StructuredIndex) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1029 | int maxElements = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1030 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1031 | if (T->isArrayType()) |
| 1032 | maxElements = numArrayElements(T); |
Douglas Gregor | 8385a06 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 1033 | else if (T->isRecordType()) |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1034 | maxElements = numStructUnionElements(T); |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1035 | else if (T->isVectorType()) |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1036 | maxElements = T->getAs<VectorType>()->getNumElements(); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1037 | else |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1038 | llvm_unreachable("CheckImplicitInitList(): Illegal type"); |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1039 | |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 1040 | if (maxElements == 0) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1041 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1042 | SemaRef.Diag(ParentIList->getInit(Index)->getBeginLoc(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1043 | diag::err_implicit_empty_initializer); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1044 | ++Index; |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 1045 | hadError = true; |
| 1046 | return; |
| 1047 | } |
| 1048 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1049 | // Build a structured initializer list corresponding to this subobject. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1050 | InitListExpr *StructuredSubobjectInitList = getStructuredSubobjectInit( |
| 1051 | ParentIList, Index, T, StructuredList, StructuredIndex, |
| 1052 | SourceRange(ParentIList->getInit(Index)->getBeginLoc(), |
| 1053 | ParentIList->getSourceRange().getEnd())); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1054 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1055 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1056 | // Check the element types and build the structural subobject. |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 1057 | unsigned StartIndex = Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1058 | CheckListElementTypes(Entity, ParentIList, T, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 1059 | /*SubobjectIsDesignatorContext=*/false, Index, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1060 | StructuredSubobjectInitList, |
Eli Friedman | c616c5f | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 1061 | StructuredSubobjectInitIndex); |
Sebastian Redl | 8b6412a | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 1062 | |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 1063 | if (StructuredSubobjectInitList) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1064 | StructuredSubobjectInitList->setType(T); |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1065 | |
Sebastian Redl | 8b6412a | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 1066 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1067 | // Update the structured sub-object initializer so that it's ending |
| 1068 | // range corresponds with the end of the last initializer it used. |
Reid Kleckner | 4a09e88 | 2015-12-09 23:18:38 +0000 | [diff] [blame] | 1069 | if (EndIndex < ParentIList->getNumInits() && |
| 1070 | ParentIList->getInit(EndIndex)) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1071 | SourceLocation EndLoc |
| 1072 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 1073 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 1074 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1075 | |
Sebastian Redl | 8b6412a | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 1076 | // Complain about missing braces. |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 1077 | if (!VerifyOnly && (T->isArrayType() || T->isRecordType()) && |
Richard Smith | 283e207 | 2017-10-03 20:36:00 +0000 | [diff] [blame] | 1078 | !ParentIList->isIdiomaticZeroInitializer(SemaRef.getLangOpts()) && |
| 1079 | !isIdiomaticBraceElisionEntity(Entity)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1080 | SemaRef.Diag(StructuredSubobjectInitList->getBeginLoc(), |
Richard Smith | de22923 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 1081 | diag::warn_missing_braces) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 1082 | << StructuredSubobjectInitList->getSourceRange() |
| 1083 | << FixItHint::CreateInsertion( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1084 | StructuredSubobjectInitList->getBeginLoc(), "{") |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 1085 | << FixItHint::CreateInsertion( |
| 1086 | SemaRef.getLocForEndOfToken( |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1087 | StructuredSubobjectInitList->getEndLoc()), |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 1088 | "}"); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1089 | } |
Richard Smith | 79c88c3 | 2018-09-26 19:00:16 +0000 | [diff] [blame] | 1090 | |
| 1091 | // Warn if this type won't be an aggregate in future versions of C++. |
| 1092 | auto *CXXRD = T->getAsCXXRecordDecl(); |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 1093 | if (!VerifyOnly && CXXRD && CXXRD->hasUserDeclaredConstructor()) { |
Richard Smith | 79c88c3 | 2018-09-26 19:00:16 +0000 | [diff] [blame] | 1094 | SemaRef.Diag(StructuredSubobjectInitList->getBeginLoc(), |
| 1095 | diag::warn_cxx2a_compat_aggregate_init_with_ctors) |
| 1096 | << StructuredSubobjectInitList->getSourceRange() << T; |
| 1097 | } |
Tanya Lattner | 5029d56 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 1098 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
Richard Smith | 420fa12 | 2015-02-12 01:50:05 +0000 | [diff] [blame] | 1101 | /// Warn that \p Entity was of scalar type and was initialized by a |
| 1102 | /// single-element braced initializer list. |
| 1103 | static void warnBracedScalarInit(Sema &S, const InitializedEntity &Entity, |
| 1104 | SourceRange Braces) { |
| 1105 | // Don't warn during template instantiation. If the initialization was |
| 1106 | // non-dependent, we warned during the initial parse; otherwise, the |
| 1107 | // type might not be scalar in some uses of the template. |
Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 1108 | if (S.inTemplateInstantiation()) |
Richard Smith | 420fa12 | 2015-02-12 01:50:05 +0000 | [diff] [blame] | 1109 | return; |
| 1110 | |
| 1111 | unsigned DiagID = 0; |
| 1112 | |
| 1113 | switch (Entity.getKind()) { |
| 1114 | case InitializedEntity::EK_VectorElement: |
| 1115 | case InitializedEntity::EK_ComplexElement: |
| 1116 | case InitializedEntity::EK_ArrayElement: |
| 1117 | case InitializedEntity::EK_Parameter: |
| 1118 | case InitializedEntity::EK_Parameter_CF_Audited: |
| 1119 | case InitializedEntity::EK_Result: |
| 1120 | // Extra braces here are suspicious. |
| 1121 | DiagID = diag::warn_braces_around_scalar_init; |
| 1122 | break; |
| 1123 | |
| 1124 | case InitializedEntity::EK_Member: |
| 1125 | // Warn on aggregate initialization but not on ctor init list or |
| 1126 | // default member initializer. |
| 1127 | if (Entity.getParent()) |
| 1128 | DiagID = diag::warn_braces_around_scalar_init; |
| 1129 | break; |
| 1130 | |
| 1131 | case InitializedEntity::EK_Variable: |
| 1132 | case InitializedEntity::EK_LambdaCapture: |
| 1133 | // No warning, might be direct-list-initialization. |
| 1134 | // FIXME: Should we warn for copy-list-initialization in these cases? |
| 1135 | break; |
| 1136 | |
| 1137 | case InitializedEntity::EK_New: |
| 1138 | case InitializedEntity::EK_Temporary: |
| 1139 | case InitializedEntity::EK_CompoundLiteralInit: |
| 1140 | // No warning, braces are part of the syntax of the underlying construct. |
| 1141 | break; |
| 1142 | |
| 1143 | case InitializedEntity::EK_RelatedResult: |
| 1144 | // No warning, we already warned when initializing the result. |
| 1145 | break; |
| 1146 | |
| 1147 | case InitializedEntity::EK_Exception: |
| 1148 | case InitializedEntity::EK_Base: |
| 1149 | case InitializedEntity::EK_Delegating: |
| 1150 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 1151 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 1152 | case InitializedEntity::EK_Binding: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 1153 | case InitializedEntity::EK_StmtExprResult: |
Richard Smith | 420fa12 | 2015-02-12 01:50:05 +0000 | [diff] [blame] | 1154 | llvm_unreachable("unexpected braced scalar init"); |
| 1155 | } |
| 1156 | |
| 1157 | if (DiagID) { |
| 1158 | S.Diag(Braces.getBegin(), DiagID) |
| 1159 | << Braces |
| 1160 | << FixItHint::CreateRemoval(Braces.getBegin()) |
| 1161 | << FixItHint::CreateRemoval(Braces.getEnd()); |
| 1162 | } |
| 1163 | } |
| 1164 | |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 1165 | /// Check whether the initializer \p IList (that was written with explicit |
| 1166 | /// braces) can be used to initialize an object of type \p T. |
| 1167 | /// |
| 1168 | /// This also fills in \p StructuredList with the fully-braced, desugared |
| 1169 | /// form of the initialization. |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1170 | void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1171 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1172 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1173 | bool TopLevelObject) { |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 1174 | unsigned Index = 0, StructuredIndex = 0; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1175 | CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1176 | Index, StructuredList, StructuredIndex, TopLevelObject); |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 1177 | if (StructuredList) { |
Eli Friedman | 91f5ae5 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 1178 | QualType ExprTy = T; |
| 1179 | if (!ExprTy->isArrayType()) |
| 1180 | ExprTy = ExprTy.getNonLValueExprType(SemaRef.Context); |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 1181 | if (!VerifyOnly) |
| 1182 | IList->setType(ExprTy); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1183 | StructuredList->setType(ExprTy); |
| 1184 | } |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1185 | if (hadError) |
| 1186 | return; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 1187 | |
Richard Smith | 8823dbc | 2019-08-29 22:49:32 +0000 | [diff] [blame] | 1188 | // Don't complain for incomplete types, since we'll get an error elsewhere. |
| 1189 | if (Index < IList->getNumInits() && !T->isIncompleteType()) { |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 1190 | // We have leftover initializers |
Richard Smith | 8823dbc | 2019-08-29 22:49:32 +0000 | [diff] [blame] | 1191 | bool ExtraInitsIsError = SemaRef.getLangOpts().CPlusPlus || |
| 1192 | (SemaRef.getLangOpts().OpenCL && T->isVectorType()); |
| 1193 | hadError = ExtraInitsIsError; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1194 | if (VerifyOnly) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1195 | return; |
Richard Smith | 8823dbc | 2019-08-29 22:49:32 +0000 | [diff] [blame] | 1196 | } else if (StructuredIndex == 1 && |
| 1197 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context) == |
| 1198 | SIF_None) { |
| 1199 | unsigned DK = |
| 1200 | ExtraInitsIsError |
| 1201 | ? diag::err_excess_initializers_in_char_array_initializer |
| 1202 | : diag::ext_excess_initializers_in_char_array_initializer; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1203 | SemaRef.Diag(IList->getInit(Index)->getBeginLoc(), DK) |
| 1204 | << IList->getInit(Index)->getSourceRange(); |
Richard Smith | 8823dbc | 2019-08-29 22:49:32 +0000 | [diff] [blame] | 1205 | } else { |
| 1206 | int initKind = T->isArrayType() ? 0 : |
| 1207 | T->isVectorType() ? 1 : |
| 1208 | T->isScalarType() ? 2 : |
| 1209 | T->isUnionType() ? 3 : |
| 1210 | 4; |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 1211 | |
Richard Smith | 8823dbc | 2019-08-29 22:49:32 +0000 | [diff] [blame] | 1212 | unsigned DK = ExtraInitsIsError ? diag::err_excess_initializers |
| 1213 | : diag::ext_excess_initializers; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1214 | SemaRef.Diag(IList->getInit(Index)->getBeginLoc(), DK) |
| 1215 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 1216 | } |
| 1217 | } |
Eli Friedman | 6fcdec2 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 1218 | |
Richard Smith | 79c88c3 | 2018-09-26 19:00:16 +0000 | [diff] [blame] | 1219 | if (!VerifyOnly) { |
| 1220 | if (T->isScalarType() && IList->getNumInits() == 1 && |
| 1221 | !isa<InitListExpr>(IList->getInit(0))) |
| 1222 | warnBracedScalarInit(SemaRef, Entity, IList->getSourceRange()); |
| 1223 | |
| 1224 | // Warn if this is a class type that won't be an aggregate in future |
| 1225 | // versions of C++. |
| 1226 | auto *CXXRD = T->getAsCXXRecordDecl(); |
| 1227 | if (CXXRD && CXXRD->hasUserDeclaredConstructor()) { |
| 1228 | // Don't warn if there's an equivalent default constructor that would be |
| 1229 | // used instead. |
| 1230 | bool HasEquivCtor = false; |
| 1231 | if (IList->getNumInits() == 0) { |
| 1232 | auto *CD = SemaRef.LookupDefaultConstructor(CXXRD); |
| 1233 | HasEquivCtor = CD && !CD->isDeleted(); |
| 1234 | } |
| 1235 | |
| 1236 | if (!HasEquivCtor) { |
| 1237 | SemaRef.Diag(IList->getBeginLoc(), |
| 1238 | diag::warn_cxx2a_compat_aggregate_init_with_ctors) |
| 1239 | << IList->getSourceRange() << T; |
| 1240 | } |
| 1241 | } |
| 1242 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1245 | void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1246 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1247 | QualType &DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1248 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1249 | unsigned &Index, |
| 1250 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1251 | unsigned &StructuredIndex, |
| 1252 | bool TopLevelObject) { |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 1253 | if (DeclType->isAnyComplexType() && SubobjectIsDesignatorContext) { |
| 1254 | // Explicitly braced initializer for complex type can be real+imaginary |
| 1255 | // parts. |
| 1256 | CheckComplexType(Entity, IList, DeclType, Index, |
| 1257 | StructuredList, StructuredIndex); |
| 1258 | } else if (DeclType->isScalarType()) { |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1259 | CheckScalarType(Entity, IList, DeclType, Index, |
| 1260 | StructuredList, StructuredIndex); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 1261 | } else if (DeclType->isVectorType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1262 | CheckVectorType(Entity, IList, DeclType, Index, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1263 | StructuredList, StructuredIndex); |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 1264 | } else if (DeclType->isRecordType()) { |
| 1265 | assert(DeclType->isAggregateType() && |
| 1266 | "non-aggregate records should be handed in CheckSubElementType"); |
| 1267 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 1268 | auto Bases = |
| 1269 | CXXRecordDecl::base_class_range(CXXRecordDecl::base_class_iterator(), |
| 1270 | CXXRecordDecl::base_class_iterator()); |
| 1271 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
| 1272 | Bases = CXXRD->bases(); |
| 1273 | CheckStructUnionTypes(Entity, IList, DeclType, Bases, RD->field_begin(), |
| 1274 | SubobjectIsDesignatorContext, Index, StructuredList, |
| 1275 | StructuredIndex, TopLevelObject); |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 1276 | } else if (DeclType->isArrayType()) { |
| 1277 | llvm::APSInt Zero( |
| 1278 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
| 1279 | false); |
| 1280 | CheckArrayType(Entity, IList, DeclType, Zero, |
| 1281 | SubobjectIsDesignatorContext, Index, |
| 1282 | StructuredList, StructuredIndex); |
Steve Naroff | eaf5853 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 1283 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 1284 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1285 | ++Index; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1286 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1287 | SemaRef.Diag(IList->getBeginLoc(), diag::err_illegal_initializer_type) |
| 1288 | << DeclType; |
Eli Friedman | d0e48ea | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 1289 | hadError = true; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1290 | } else if (DeclType->isReferenceType()) { |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1291 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 1292 | StructuredList, StructuredIndex); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1293 | } else if (DeclType->isObjCObjectType()) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1294 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1295 | SemaRef.Diag(IList->getBeginLoc(), diag::err_init_objc_class) << DeclType; |
Douglas Gregor | 50ec46d | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 1296 | hadError = true; |
Andrew Savonichev | 3fee351 | 2018-11-08 11:25:41 +0000 | [diff] [blame] | 1297 | } else if (DeclType->isOCLIntelSubgroupAVCType()) { |
| 1298 | // Checks for scalar type are sufficient for these types too. |
| 1299 | CheckScalarType(Entity, IList, DeclType, Index, StructuredList, |
| 1300 | StructuredIndex); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1301 | } else { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1302 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1303 | SemaRef.Diag(IList->getBeginLoc(), diag::err_illegal_initializer_type) |
| 1304 | << DeclType; |
Douglas Gregor | 50ec46d | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 1305 | hadError = true; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1306 | } |
| 1307 | } |
| 1308 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1309 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1310 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1311 | QualType ElemType, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1312 | unsigned &Index, |
| 1313 | InitListExpr *StructuredList, |
| 1314 | unsigned &StructuredIndex) { |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1315 | Expr *expr = IList->getInit(Index); |
Richard Smith | 72752e8 | 2013-05-31 02:56:17 +0000 | [diff] [blame] | 1316 | |
| 1317 | if (ElemType->isReferenceType()) |
| 1318 | return CheckReferenceType(Entity, IList, ElemType, Index, |
| 1319 | StructuredList, StructuredIndex); |
| 1320 | |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 1321 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1322 | if (SubInitList->getNumInits() == 1 && |
| 1323 | IsStringInit(SubInitList->getInit(0), ElemType, SemaRef.Context) == |
| 1324 | SIF_None) { |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 1325 | // FIXME: It would be more faithful and no less correct to include an |
| 1326 | // InitListExpr in the semantic form of the initializer list in this case. |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1327 | expr = SubInitList->getInit(0); |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 1328 | } |
Richard Smith | 33e9be6 | 2019-08-29 22:49:33 +0000 | [diff] [blame] | 1329 | // Nested aggregate initialization and C++ initialization are handled later. |
Richard Smith | c4158e86 | 2014-07-18 04:47:25 +0000 | [diff] [blame] | 1330 | } else if (isa<ImplicitValueInitExpr>(expr)) { |
Richard Smith | 8aa561b | 2014-07-17 23:12:06 +0000 | [diff] [blame] | 1331 | // This happens during template instantiation when we see an InitListExpr |
| 1332 | // that we've already checked once. |
Richard Smith | c4158e86 | 2014-07-18 04:47:25 +0000 | [diff] [blame] | 1333 | assert(SemaRef.Context.hasSameType(expr->getType(), ElemType) && |
Richard Smith | 8aa561b | 2014-07-17 23:12:06 +0000 | [diff] [blame] | 1334 | "found implicit initialization for the wrong type"); |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 1335 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
Richard Smith | 8aa561b | 2014-07-17 23:12:06 +0000 | [diff] [blame] | 1336 | ++Index; |
| 1337 | return; |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
Richard Smith | 33e9be6 | 2019-08-29 22:49:33 +0000 | [diff] [blame] | 1340 | if (SemaRef.getLangOpts().CPlusPlus || isa<InitListExpr>(expr)) { |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1341 | // C++ [dcl.init.aggr]p2: |
| 1342 | // Each member is copy-initialized from the corresponding |
| 1343 | // initializer-clause. |
| 1344 | |
| 1345 | // FIXME: Better EqualLoc? |
| 1346 | InitializationKind Kind = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1347 | InitializationKind::CreateCopy(expr->getBeginLoc(), SourceLocation()); |
Anastasia Stulova | 8d99a5c0 | 2019-08-02 11:19:35 +0000 | [diff] [blame] | 1348 | |
| 1349 | // Vector elements can be initialized from other vectors in which case |
| 1350 | // we need initialization entity with a type of a vector (and not a vector |
| 1351 | // element!) initializing multiple vector elements. |
| 1352 | auto TmpEntity = |
| 1353 | (ElemType->isExtVectorType() && !Entity.getType()->isExtVectorType()) |
| 1354 | ? InitializedEntity::InitializeTemporary(ElemType) |
| 1355 | : Entity; |
| 1356 | |
| 1357 | InitializationSequence Seq(SemaRef, TmpEntity, Kind, expr, |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1358 | /*TopLevelOfInitList*/ true); |
| 1359 | |
| 1360 | // C++14 [dcl.init.aggr]p13: |
| 1361 | // If the assignment-expression can initialize a member, the member is |
| 1362 | // initialized. Otherwise [...] brace elision is assumed |
| 1363 | // |
| 1364 | // Brace elision is never performed if the element is not an |
| 1365 | // assignment-expression. |
| 1366 | if (Seq || isa<InitListExpr>(expr)) { |
| 1367 | if (!VerifyOnly) { |
Anastasia Stulova | 8d99a5c0 | 2019-08-02 11:19:35 +0000 | [diff] [blame] | 1368 | ExprResult Result = Seq.Perform(SemaRef, TmpEntity, Kind, expr); |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1369 | if (Result.isInvalid()) |
| 1370 | hadError = true; |
| 1371 | |
| 1372 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1373 | Result.getAs<Expr>()); |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 1374 | } else if (!Seq) { |
Richard Smith | 40574cc | 2015-02-16 04:42:59 +0000 | [diff] [blame] | 1375 | hadError = true; |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 1376 | } else if (StructuredList) { |
| 1377 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1378 | getDummyInit()); |
| 1379 | } |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1380 | ++Index; |
| 1381 | return; |
| 1382 | } |
| 1383 | |
| 1384 | // Fall through for subaggregate initialization |
| 1385 | } else if (ElemType->isScalarType() || ElemType->isAtomicType()) { |
| 1386 | // FIXME: Need to handle atomic aggregate types with implicit init lists. |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1387 | return CheckScalarType(Entity, IList, ElemType, Index, |
| 1388 | StructuredList, StructuredIndex); |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1389 | } else if (const ArrayType *arrayType = |
| 1390 | SemaRef.Context.getAsArrayType(ElemType)) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1391 | // arrayType can be incomplete if we're initializing a flexible |
| 1392 | // array member. There's nothing we can do with the completed |
| 1393 | // type here, though. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1394 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1395 | if (IsStringInit(expr, arrayType, SemaRef.Context) == SIF_None) { |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 1396 | // FIXME: Should we do this checking in verify-only mode? |
| 1397 | if (!VerifyOnly) |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1398 | CheckStringInit(expr, ElemType, arrayType, SemaRef); |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 1399 | if (StructuredList) |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1400 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1401 | ++Index; |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1402 | return; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1403 | } |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1404 | |
| 1405 | // Fall through for subaggregate initialization. |
| 1406 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1407 | } else { |
Anastasia Stulova | db7a31c | 2016-07-05 11:31:24 +0000 | [diff] [blame] | 1408 | assert((ElemType->isRecordType() || ElemType->isVectorType() || |
Egor Churaev | 45fe70f | 2017-05-10 10:28:34 +0000 | [diff] [blame] | 1409 | ElemType->isOpenCLSpecificType()) && "Unexpected type"); |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1410 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1411 | // C99 6.7.8p13: |
| 1412 | // |
| 1413 | // The initializer for a structure or union object that has |
| 1414 | // automatic storage duration shall be either an initializer |
| 1415 | // list as described below, or a single expression that has |
| 1416 | // compatible structure or union type. In the latter case, the |
| 1417 | // initial value of the object, including unnamed members, is |
| 1418 | // that of the expression. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1419 | ExprResult ExprRes = expr; |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1420 | if (SemaRef.CheckSingleAssignmentConstraints( |
| 1421 | ElemType, ExprRes, !VerifyOnly) != Sema::Incompatible) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1422 | if (ExprRes.isInvalid()) |
| 1423 | hadError = true; |
| 1424 | else { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1425 | ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.get()); |
Richard Smith | 8823dbc | 2019-08-29 22:49:32 +0000 | [diff] [blame] | 1426 | if (ExprRes.isInvalid()) |
| 1427 | hadError = true; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1428 | } |
| 1429 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1430 | ExprRes.getAs<Expr>()); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1431 | ++Index; |
| 1432 | return; |
| 1433 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1434 | ExprRes.get(); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1435 | // Fall through for subaggregate initialization |
| 1436 | } |
| 1437 | |
| 1438 | // C++ [dcl.init.aggr]p12: |
| 1439 | // |
| 1440 | // [...] Otherwise, if the member is itself a non-empty |
| 1441 | // subaggregate, brace elision is assumed and the initializer is |
| 1442 | // considered for the initialization of the first member of |
| 1443 | // the subaggregate. |
Yaxun Liu | a91da4b | 2016-10-11 15:53:28 +0000 | [diff] [blame] | 1444 | // OpenCL vector initializer is handled elsewhere. |
| 1445 | if ((!SemaRef.getLangOpts().OpenCL && ElemType->isVectorType()) || |
| 1446 | ElemType->isAggregateType()) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1447 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
| 1448 | StructuredIndex); |
| 1449 | ++StructuredIndex; |
| 1450 | } else { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1451 | if (!VerifyOnly) { |
Richard Smith | 8823dbc | 2019-08-29 22:49:32 +0000 | [diff] [blame] | 1452 | // We cannot initialize this element, so let PerformCopyInitialization |
| 1453 | // produce the appropriate diagnostic. We already checked that this |
| 1454 | // initialization will fail. |
| 1455 | ExprResult Copy = |
| 1456 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), expr, |
| 1457 | /*TopLevelOfInitList=*/true); |
| 1458 | (void)Copy; |
| 1459 | assert(Copy.isInvalid() && |
| 1460 | "expected non-aggregate initialization to fail"); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1461 | } |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1462 | hadError = true; |
| 1463 | ++Index; |
| 1464 | ++StructuredIndex; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1465 | } |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 1468 | void InitListChecker::CheckComplexType(const InitializedEntity &Entity, |
| 1469 | InitListExpr *IList, QualType DeclType, |
| 1470 | unsigned &Index, |
| 1471 | InitListExpr *StructuredList, |
| 1472 | unsigned &StructuredIndex) { |
| 1473 | assert(Index == 0 && "Index in explicit init list must be zero"); |
| 1474 | |
| 1475 | // As an extension, clang supports complex initializers, which initialize |
| 1476 | // a complex number component-wise. When an explicit initializer list for |
| 1477 | // a complex number contains two two initializers, this extension kicks in: |
| 1478 | // it exepcts the initializer list to contain two elements convertible to |
| 1479 | // the element type of the complex type. The first element initializes |
| 1480 | // the real part, and the second element intitializes the imaginary part. |
| 1481 | |
| 1482 | if (IList->getNumInits() != 2) |
| 1483 | return CheckScalarType(Entity, IList, DeclType, Index, StructuredList, |
| 1484 | StructuredIndex); |
| 1485 | |
| 1486 | // This is an extension in C. (The builtin _Complex type does not exist |
| 1487 | // in the C++ standard.) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1488 | if (!SemaRef.getLangOpts().CPlusPlus && !VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1489 | SemaRef.Diag(IList->getBeginLoc(), diag::ext_complex_component_init) |
| 1490 | << IList->getSourceRange(); |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 1491 | |
| 1492 | // Initialize the complex number. |
| 1493 | QualType elementType = DeclType->getAs<ComplexType>()->getElementType(); |
| 1494 | InitializedEntity ElementEntity = |
| 1495 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
| 1496 | |
| 1497 | for (unsigned i = 0; i < 2; ++i) { |
| 1498 | ElementEntity.setElementIndex(Index); |
| 1499 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1500 | StructuredList, StructuredIndex); |
| 1501 | } |
| 1502 | } |
| 1503 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1504 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1505 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1506 | unsigned &Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1507 | InitListExpr *StructuredList, |
| 1508 | unsigned &StructuredIndex) { |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1509 | if (Index >= IList->getNumInits()) { |
Richard Smith | c823973 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 1510 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1511 | SemaRef.Diag(IList->getBeginLoc(), |
| 1512 | SemaRef.getLangOpts().CPlusPlus11 |
| 1513 | ? diag::warn_cxx98_compat_empty_scalar_initializer |
| 1514 | : diag::err_empty_scalar_initializer) |
| 1515 | << IList->getSourceRange(); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1516 | hadError = !SemaRef.getLangOpts().CPlusPlus11; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1517 | ++Index; |
| 1518 | ++StructuredIndex; |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 1519 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1520 | } |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1521 | |
| 1522 | Expr *expr = IList->getInit(Index); |
| 1523 | if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) { |
Richard Smith | fe9d2c0 | 2013-11-19 03:41:32 +0000 | [diff] [blame] | 1524 | // FIXME: This is invalid, and accepting it causes overload resolution |
| 1525 | // to pick the wrong overload in some corner cases. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1526 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1527 | SemaRef.Diag(SubIList->getBeginLoc(), |
Richard Smith | fe9d2c0 | 2013-11-19 03:41:32 +0000 | [diff] [blame] | 1528 | diag::ext_many_braces_around_scalar_init) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1529 | << SubIList->getSourceRange(); |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1530 | |
| 1531 | CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList, |
| 1532 | StructuredIndex); |
| 1533 | return; |
| 1534 | } else if (isa<DesignatedInitExpr>(expr)) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1535 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1536 | SemaRef.Diag(expr->getBeginLoc(), diag::err_designator_for_scalar_init) |
| 1537 | << DeclType << expr->getSourceRange(); |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1538 | hadError = true; |
| 1539 | ++Index; |
| 1540 | ++StructuredIndex; |
| 1541 | return; |
| 1542 | } |
| 1543 | |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 1544 | ExprResult Result; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1545 | if (VerifyOnly) { |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 1546 | if (SemaRef.CanPerformCopyInitialization(Entity, expr)) |
| 1547 | Result = getDummyInit(); |
| 1548 | else |
| 1549 | Result = ExprError(); |
| 1550 | } else { |
| 1551 | Result = |
| 1552 | SemaRef.PerformCopyInitialization(Entity, expr->getBeginLoc(), expr, |
| 1553 | /*TopLevelOfInitList=*/true); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1556 | Expr *ResultExpr = nullptr; |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1557 | |
| 1558 | if (Result.isInvalid()) |
| 1559 | hadError = true; // types weren't compatible. |
| 1560 | else { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1561 | ResultExpr = Result.getAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1562 | |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 1563 | if (ResultExpr != expr && !VerifyOnly) { |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1564 | // The type was promoted, update initializer list. |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 1565 | // FIXME: Why are we updating the syntactic init list? |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1566 | IList->setInit(Index, ResultExpr); |
| 1567 | } |
| 1568 | } |
| 1569 | if (hadError) |
| 1570 | ++StructuredIndex; |
| 1571 | else |
| 1572 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 1573 | ++Index; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1574 | } |
| 1575 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1576 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 1577 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1578 | unsigned &Index, |
| 1579 | InitListExpr *StructuredList, |
| 1580 | unsigned &StructuredIndex) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1581 | if (Index >= IList->getNumInits()) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1582 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 1583 | // general, it would be useful to pass location information down the stack, |
| 1584 | // so that we know the location (or decl) of the "current object" being |
| 1585 | // initialized. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1586 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1587 | SemaRef.Diag(IList->getBeginLoc(), |
| 1588 | diag::err_init_reference_member_uninitialized) |
| 1589 | << DeclType << IList->getSourceRange(); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1590 | hadError = true; |
| 1591 | ++Index; |
| 1592 | ++StructuredIndex; |
| 1593 | return; |
| 1594 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1595 | |
| 1596 | Expr *expr = IList->getInit(Index); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1597 | if (isa<InitListExpr>(expr) && !SemaRef.getLangOpts().CPlusPlus11) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1598 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1599 | SemaRef.Diag(IList->getBeginLoc(), diag::err_init_non_aggr_init_list) |
| 1600 | << DeclType << IList->getSourceRange(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1601 | hadError = true; |
| 1602 | ++Index; |
| 1603 | ++StructuredIndex; |
| 1604 | return; |
| 1605 | } |
| 1606 | |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 1607 | ExprResult Result; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1608 | if (VerifyOnly) { |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 1609 | if (SemaRef.CanPerformCopyInitialization(Entity,expr)) |
| 1610 | Result = getDummyInit(); |
| 1611 | else |
| 1612 | Result = ExprError(); |
| 1613 | } else { |
| 1614 | Result = |
| 1615 | SemaRef.PerformCopyInitialization(Entity, expr->getBeginLoc(), expr, |
| 1616 | /*TopLevelOfInitList=*/true); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1619 | if (Result.isInvalid()) |
| 1620 | hadError = true; |
| 1621 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1622 | expr = Result.getAs<Expr>(); |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 1623 | // FIXME: Why are we updating the syntactic init list? |
| 1624 | if (!VerifyOnly) |
| 1625 | IList->setInit(Index, expr); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1626 | |
| 1627 | if (hadError) |
| 1628 | ++StructuredIndex; |
| 1629 | else |
| 1630 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 1631 | ++Index; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1634 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1635 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1636 | unsigned &Index, |
| 1637 | InitListExpr *StructuredList, |
| 1638 | unsigned &StructuredIndex) { |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1639 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 1640 | unsigned maxElements = VT->getNumElements(); |
| 1641 | unsigned numEltsInit = 0; |
| 1642 | QualType elementType = VT->getElementType(); |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1643 | |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1644 | if (Index >= IList->getNumInits()) { |
| 1645 | // Make sure the element type can be value-initialized. |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 1646 | CheckEmptyInitializable( |
| 1647 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity), |
| 1648 | IList->getEndLoc()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1649 | return; |
| 1650 | } |
| 1651 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1652 | if (!SemaRef.getLangOpts().OpenCL) { |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1653 | // If the initializing element is a vector, try to copy-initialize |
| 1654 | // instead of breaking it apart (which is doomed to failure anyway). |
| 1655 | Expr *Init = IList->getInit(Index); |
| 1656 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 1657 | ExprResult Result; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1658 | if (VerifyOnly) { |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 1659 | if (SemaRef.CanPerformCopyInitialization(Entity, Init)) |
| 1660 | Result = getDummyInit(); |
| 1661 | else |
| 1662 | Result = ExprError(); |
| 1663 | } else { |
| 1664 | Result = |
| 1665 | SemaRef.PerformCopyInitialization(Entity, Init->getBeginLoc(), Init, |
| 1666 | /*TopLevelOfInitList=*/true); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1669 | Expr *ResultExpr = nullptr; |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1670 | if (Result.isInvalid()) |
| 1671 | hadError = true; // types weren't compatible. |
| 1672 | else { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1673 | ResultExpr = Result.getAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1674 | |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 1675 | if (ResultExpr != Init && !VerifyOnly) { |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1676 | // The type was promoted, update initializer list. |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 1677 | // FIXME: Why are we updating the syntactic init list? |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1678 | IList->setInit(Index, ResultExpr); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1679 | } |
| 1680 | } |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1681 | if (hadError) |
| 1682 | ++StructuredIndex; |
| 1683 | else |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1684 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1685 | ResultExpr); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1686 | ++Index; |
| 1687 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1688 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1689 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1690 | InitializedEntity ElementEntity = |
| 1691 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1692 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1693 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 1694 | // Don't attempt to go past the end of the init list |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1695 | if (Index >= IList->getNumInits()) { |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 1696 | CheckEmptyInitializable(ElementEntity, IList->getEndLoc()); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1697 | break; |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1698 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1699 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1700 | ElementEntity.setElementIndex(Index); |
| 1701 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1702 | StructuredList, StructuredIndex); |
| 1703 | } |
James Molloy | 9eef265 | 2014-06-20 14:35:13 +0000 | [diff] [blame] | 1704 | |
| 1705 | if (VerifyOnly) |
| 1706 | return; |
| 1707 | |
| 1708 | bool isBigEndian = SemaRef.Context.getTargetInfo().isBigEndian(); |
| 1709 | const VectorType *T = Entity.getType()->getAs<VectorType>(); |
| 1710 | if (isBigEndian && (T->getVectorKind() == VectorType::NeonVector || |
| 1711 | T->getVectorKind() == VectorType::NeonPolyVector)) { |
| 1712 | // The ability to use vector initializer lists is a GNU vector extension |
| 1713 | // and is unrelated to the NEON intrinsics in arm_neon.h. On little |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1714 | // endian machines it works fine, however on big endian machines it |
James Molloy | 9eef265 | 2014-06-20 14:35:13 +0000 | [diff] [blame] | 1715 | // exhibits surprising behaviour: |
| 1716 | // |
| 1717 | // uint32x2_t x = {42, 64}; |
| 1718 | // return vget_lane_u32(x, 0); // Will return 64. |
| 1719 | // |
| 1720 | // Because of this, explicitly call out that it is non-portable. |
| 1721 | // |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1722 | SemaRef.Diag(IList->getBeginLoc(), |
James Molloy | 9eef265 | 2014-06-20 14:35:13 +0000 | [diff] [blame] | 1723 | diag::warn_neon_vector_initializer_non_portable); |
| 1724 | |
| 1725 | const char *typeCode; |
| 1726 | unsigned typeSize = SemaRef.Context.getTypeSize(elementType); |
| 1727 | |
| 1728 | if (elementType->isFloatingType()) |
| 1729 | typeCode = "f"; |
| 1730 | else if (elementType->isSignedIntegerType()) |
| 1731 | typeCode = "s"; |
| 1732 | else if (elementType->isUnsignedIntegerType()) |
| 1733 | typeCode = "u"; |
| 1734 | else |
| 1735 | llvm_unreachable("Invalid element type!"); |
| 1736 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1737 | SemaRef.Diag(IList->getBeginLoc(), |
| 1738 | SemaRef.Context.getTypeSize(VT) > 64 |
| 1739 | ? diag::note_neon_vector_initializer_non_portable_q |
| 1740 | : diag::note_neon_vector_initializer_non_portable) |
| 1741 | << typeCode << typeSize; |
James Molloy | 9eef265 | 2014-06-20 14:35:13 +0000 | [diff] [blame] | 1742 | } |
| 1743 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1744 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1745 | } |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1746 | |
| 1747 | InitializedEntity ElementEntity = |
| 1748 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1749 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1750 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 1751 | for (unsigned i = 0; i < maxElements; ++i) { |
| 1752 | // Don't attempt to go past the end of the init list |
| 1753 | if (Index >= IList->getNumInits()) |
| 1754 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1755 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1756 | ElementEntity.setElementIndex(Index); |
| 1757 | |
| 1758 | QualType IType = IList->getInit(Index)->getType(); |
| 1759 | if (!IType->isVectorType()) { |
| 1760 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1761 | StructuredList, StructuredIndex); |
| 1762 | ++numEltsInit; |
| 1763 | } else { |
| 1764 | QualType VecType; |
| 1765 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 1766 | unsigned numIElts = IVT->getNumElements(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1767 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1768 | if (IType->isExtVectorType()) |
| 1769 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 1770 | else |
| 1771 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1772 | IVT->getVectorKind()); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1773 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 1774 | StructuredList, StructuredIndex); |
| 1775 | numEltsInit += numIElts; |
| 1776 | } |
| 1777 | } |
| 1778 | |
| 1779 | // OpenCL requires all elements to be initialized. |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1780 | if (numEltsInit != maxElements) { |
| 1781 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1782 | SemaRef.Diag(IList->getBeginLoc(), |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1783 | diag::err_vector_incorrect_num_initializers) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1784 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1785 | hadError = true; |
| 1786 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1787 | } |
| 1788 | |
Erik Pilkington | f8ccf05 | 2019-05-10 17:52:26 +0000 | [diff] [blame] | 1789 | /// Check if the type of a class element has an accessible destructor, and marks |
| 1790 | /// it referenced. Returns true if we shouldn't form a reference to the |
| 1791 | /// destructor. |
| 1792 | /// |
| 1793 | /// Aggregate initialization requires a class element's destructor be |
| 1794 | /// accessible per 11.6.1 [dcl.init.aggr]: |
| 1795 | /// |
| 1796 | /// The destructor for each element of class type is potentially invoked |
| 1797 | /// (15.4 [class.dtor]) from the context where the aggregate initialization |
| 1798 | /// occurs. |
| 1799 | static bool checkDestructorReference(QualType ElementType, SourceLocation Loc, |
| 1800 | Sema &SemaRef) { |
| 1801 | auto *CXXRD = ElementType->getAsCXXRecordDecl(); |
| 1802 | if (!CXXRD) |
| 1803 | return false; |
| 1804 | |
| 1805 | CXXDestructorDecl *Destructor = SemaRef.LookupDestructor(CXXRD); |
| 1806 | SemaRef.CheckDestructorAccess(Loc, Destructor, |
| 1807 | SemaRef.PDiag(diag::err_access_dtor_temp) |
| 1808 | << ElementType); |
| 1809 | SemaRef.MarkFunctionReferenced(Loc, Destructor); |
| 1810 | return SemaRef.DiagnoseUseOfDecl(Destructor, Loc); |
| 1811 | } |
| 1812 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1813 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1814 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1815 | llvm::APSInt elementIndex, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1816 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1817 | unsigned &Index, |
| 1818 | InitListExpr *StructuredList, |
| 1819 | unsigned &StructuredIndex) { |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1820 | const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType); |
| 1821 | |
Erik Pilkington | f8ccf05 | 2019-05-10 17:52:26 +0000 | [diff] [blame] | 1822 | if (!VerifyOnly) { |
| 1823 | if (checkDestructorReference(arrayType->getElementType(), |
| 1824 | IList->getEndLoc(), SemaRef)) { |
| 1825 | hadError = true; |
| 1826 | return; |
| 1827 | } |
| 1828 | } |
| 1829 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1830 | // Check for the special-case of initializing an array with a string. |
| 1831 | if (Index < IList->getNumInits()) { |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1832 | if (IsStringInit(IList->getInit(Index), arrayType, SemaRef.Context) == |
| 1833 | SIF_None) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1834 | // We place the string literal directly into the resulting |
| 1835 | // initializer list. This is the only place where the structure |
| 1836 | // of the structured initializer list doesn't match exactly, |
| 1837 | // because doing so would involve allocating one character |
| 1838 | // constant for each string. |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 1839 | // FIXME: Should we do these checks in verify-only mode too? |
| 1840 | if (!VerifyOnly) |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1841 | CheckStringInit(IList->getInit(Index), DeclType, arrayType, SemaRef); |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 1842 | if (StructuredList) { |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1843 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1844 | IList->getInit(Index)); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1845 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
| 1846 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1847 | ++Index; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1848 | return; |
| 1849 | } |
| 1850 | } |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1851 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1852 | // Check for VLAs; in standard C it would be possible to check this |
| 1853 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 1854 | // them in all sorts of strange places). |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1855 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1856 | SemaRef.Diag(VAT->getSizeExpr()->getBeginLoc(), |
| 1857 | diag::err_variable_object_no_init) |
| 1858 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1859 | hadError = true; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1860 | ++Index; |
| 1861 | ++StructuredIndex; |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1862 | return; |
| 1863 | } |
| 1864 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1865 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1866 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 1867 | elementIndex.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1868 | bool maxElementsKnown = false; |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1869 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1870 | maxElements = CAT->getSize(); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1871 | elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1872 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1873 | maxElementsKnown = true; |
| 1874 | } |
| 1875 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1876 | QualType elementType = arrayType->getElementType(); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1877 | while (Index < IList->getNumInits()) { |
| 1878 | Expr *Init = IList->getInit(Index); |
| 1879 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1880 | // If we're not the subobject that matches up with the '{' for |
| 1881 | // the designator, we shouldn't be handling the |
| 1882 | // designator. Return immediately. |
| 1883 | if (!SubobjectIsDesignatorContext) |
| 1884 | return; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1885 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1886 | // Handle this designated initializer. elementIndex will be |
| 1887 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1888 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1889 | DeclType, nullptr, &elementIndex, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1890 | StructuredList, StructuredIndex, true, |
| 1891 | false)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1892 | hadError = true; |
| 1893 | continue; |
| 1894 | } |
| 1895 | |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1896 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1897 | maxElements = maxElements.extend(elementIndex.getBitWidth()); |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1898 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1899 | elementIndex = elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1900 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1901 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1902 | // If the array is of incomplete type, keep track of the number of |
| 1903 | // elements in the initializer. |
| 1904 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1905 | maxElements = elementIndex; |
| 1906 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1907 | continue; |
| 1908 | } |
| 1909 | |
| 1910 | // If we know the maximum number of elements, and we've already |
| 1911 | // hit it, stop consuming elements in the initializer list. |
| 1912 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1913 | break; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1914 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1915 | InitializedEntity ElementEntity = |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1916 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1917 | Entity); |
| 1918 | // Check this element. |
| 1919 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1920 | StructuredList, StructuredIndex); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1921 | ++elementIndex; |
| 1922 | |
| 1923 | // If the array is of incomplete type, keep track of the number of |
| 1924 | // elements in the initializer. |
| 1925 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1926 | maxElements = elementIndex; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1927 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1928 | if (!hadError && DeclType->isIncompleteArrayType() && !VerifyOnly) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1929 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1930 | // be calculated here. |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1931 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Richard Smith | 73edb6d | 2017-01-24 23:18:28 +0000 | [diff] [blame] | 1932 | if (maxElements == Zero && !Entity.isVariableLengthArrayNew()) { |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1933 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1934 | // but is supported by GNU. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1935 | SemaRef.Diag(IList->getBeginLoc(), diag::ext_typecheck_zero_array_size); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1936 | } |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1937 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1938 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1939 | ArrayType::Normal, 0); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1940 | } |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 1941 | if (!hadError) { |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1942 | // If there are any members of the array that get value-initialized, check |
| 1943 | // that is possible. That happens if we know the bound and don't have |
| 1944 | // enough elements, or if we're performing an array new with an unknown |
| 1945 | // bound. |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1946 | if ((maxElementsKnown && elementIndex < maxElements) || |
| 1947 | Entity.isVariableLengthArrayNew()) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1948 | CheckEmptyInitializable( |
| 1949 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity), |
| 1950 | IList->getEndLoc()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1951 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1954 | bool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 1955 | Expr *InitExpr, |
| 1956 | FieldDecl *Field, |
| 1957 | bool TopLevelObject) { |
| 1958 | // Handle GNU flexible array initializers. |
| 1959 | unsigned FlexArrayDiag; |
| 1960 | if (isa<InitListExpr>(InitExpr) && |
| 1961 | cast<InitListExpr>(InitExpr)->getNumInits() == 0) { |
| 1962 | // Empty flexible array init always allowed as an extension |
| 1963 | FlexArrayDiag = diag::ext_flexible_array_init; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1964 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1965 | // Disallow flexible array init in C++; it is not required for gcc |
| 1966 | // compatibility, and it needs work to IRGen correctly in general. |
| 1967 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1968 | } else if (!TopLevelObject) { |
| 1969 | // Disallow flexible array init on non-top-level object |
| 1970 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1971 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
| 1972 | // Disallow flexible array init on anything which is not a variable. |
| 1973 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1974 | } else if (cast<VarDecl>(Entity.getDecl())->hasLocalStorage()) { |
| 1975 | // Disallow flexible array init on local variables. |
| 1976 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1977 | } else { |
| 1978 | // Allow other cases. |
| 1979 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1980 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1981 | |
| 1982 | if (!VerifyOnly) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1983 | SemaRef.Diag(InitExpr->getBeginLoc(), FlexArrayDiag) |
| 1984 | << InitExpr->getBeginLoc(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1985 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1986 | << Field; |
| 1987 | } |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1988 | |
| 1989 | return FlexArrayDiag != diag::ext_flexible_array_init; |
| 1990 | } |
| 1991 | |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 1992 | void InitListChecker::CheckStructUnionTypes( |
| 1993 | const InitializedEntity &Entity, InitListExpr *IList, QualType DeclType, |
| 1994 | CXXRecordDecl::base_class_range Bases, RecordDecl::field_iterator Field, |
| 1995 | bool SubobjectIsDesignatorContext, unsigned &Index, |
| 1996 | InitListExpr *StructuredList, unsigned &StructuredIndex, |
| 1997 | bool TopLevelObject) { |
| 1998 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1999 | |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 2000 | // If the record is invalid, some of it's members are invalid. To avoid |
| 2001 | // confusion, we forgo checking the intializer for the entire record. |
| 2002 | if (structDecl->isInvalidDecl()) { |
Richard Smith | 845aa66 | 2012-09-28 21:23:50 +0000 | [diff] [blame] | 2003 | // Assume it was supposed to consume a single initializer. |
| 2004 | ++Index; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 2005 | hadError = true; |
| 2006 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2007 | } |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 2008 | |
| 2009 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 2010 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 2011 | |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 2012 | if (!VerifyOnly) |
| 2013 | for (FieldDecl *FD : RD->fields()) { |
| 2014 | QualType ET = SemaRef.Context.getBaseElementType(FD->getType()); |
Erik Pilkington | f8ccf05 | 2019-05-10 17:52:26 +0000 | [diff] [blame] | 2015 | if (checkDestructorReference(ET, IList->getEndLoc(), SemaRef)) { |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 2016 | hadError = true; |
| 2017 | return; |
| 2018 | } |
| 2019 | } |
| 2020 | |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 2021 | // If there's a default initializer, use it. |
Richard Smith | 5030928 | 2019-08-30 22:52:55 +0000 | [diff] [blame] | 2022 | if (isa<CXXRecordDecl>(RD) && |
| 2023 | cast<CXXRecordDecl>(RD)->hasInClassInitializer()) { |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 2024 | if (!StructuredList) |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 2025 | return; |
| 2026 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 2027 | Field != FieldEnd; ++Field) { |
| 2028 | if (Field->hasInClassInitializer()) { |
| 2029 | StructuredList->setInitializedFieldInUnion(*Field); |
| 2030 | // FIXME: Actually build a CXXDefaultInitExpr? |
| 2031 | return; |
| 2032 | } |
| 2033 | } |
| 2034 | } |
| 2035 | |
Reid Kleckner | 6d829bd | 2014-11-12 21:30:23 +0000 | [diff] [blame] | 2036 | // Value-initialize the first member of the union that isn't an unnamed |
| 2037 | // bitfield. |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 2038 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 2039 | Field != FieldEnd; ++Field) { |
Reid Kleckner | 6d829bd | 2014-11-12 21:30:23 +0000 | [diff] [blame] | 2040 | if (!Field->isUnnamedBitfield()) { |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 2041 | CheckEmptyInitializable( |
| 2042 | InitializedEntity::InitializeMember(*Field, &Entity), |
| 2043 | IList->getEndLoc()); |
| 2044 | if (StructuredList) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2045 | StructuredList->setInitializedFieldInUnion(*Field); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 2046 | break; |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 2047 | } |
| 2048 | } |
| 2049 | return; |
| 2050 | } |
| 2051 | |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 2052 | bool InitializedSomething = false; |
| 2053 | |
| 2054 | // If we have any base classes, they are initialized prior to the fields. |
| 2055 | for (auto &Base : Bases) { |
| 2056 | Expr *Init = Index < IList->getNumInits() ? IList->getInit(Index) : nullptr; |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 2057 | |
| 2058 | // Designated inits always initialize fields, so if we see one, all |
| 2059 | // remaining base classes have no explicit initializer. |
| 2060 | if (Init && isa<DesignatedInitExpr>(Init)) |
| 2061 | Init = nullptr; |
| 2062 | |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 2063 | SourceLocation InitLoc = Init ? Init->getBeginLoc() : IList->getEndLoc(); |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 2064 | InitializedEntity BaseEntity = InitializedEntity::InitializeBase( |
| 2065 | SemaRef.Context, &Base, false, &Entity); |
| 2066 | if (Init) { |
| 2067 | CheckSubElementType(BaseEntity, IList, Base.getType(), Index, |
| 2068 | StructuredList, StructuredIndex); |
| 2069 | InitializedSomething = true; |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 2070 | } else { |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 2071 | CheckEmptyInitializable(BaseEntity, InitLoc); |
| 2072 | } |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 2073 | |
| 2074 | if (!VerifyOnly) |
Erik Pilkington | f8ccf05 | 2019-05-10 17:52:26 +0000 | [diff] [blame] | 2075 | if (checkDestructorReference(Base.getType(), InitLoc, SemaRef)) { |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 2076 | hadError = true; |
| 2077 | return; |
| 2078 | } |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 2079 | } |
| 2080 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2081 | // If structDecl is a forward declaration, this loop won't do |
| 2082 | // anything except look at designated initializers; That's okay, |
| 2083 | // because an error should get printed out elsewhere. It might be |
| 2084 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2085 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2086 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Daniel Marjamaki | 817a3bf | 2017-09-29 09:44:41 +0000 | [diff] [blame] | 2087 | bool CheckForMissingFields = |
| 2088 | !IList->isIdiomaticZeroInitializer(SemaRef.getLangOpts()); |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 2089 | bool HasDesignatedInit = false; |
Daniel Marjamaki | 817a3bf | 2017-09-29 09:44:41 +0000 | [diff] [blame] | 2090 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2091 | while (Index < IList->getNumInits()) { |
| 2092 | Expr *Init = IList->getInit(Index); |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 2093 | SourceLocation InitLoc = Init->getBeginLoc(); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2094 | |
| 2095 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2096 | // If we're not the subobject that matches up with the '{' for |
| 2097 | // the designator, we shouldn't be handling the |
| 2098 | // designator. Return immediately. |
| 2099 | if (!SubobjectIsDesignatorContext) |
| 2100 | return; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2101 | |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 2102 | HasDesignatedInit = true; |
| 2103 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2104 | // Handle this designated initializer. Field will be updated to |
| 2105 | // the next field that we'll be initializing. |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2106 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2107 | DeclType, &Field, nullptr, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2108 | StructuredList, StructuredIndex, |
| 2109 | true, TopLevelObject)) |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2110 | hadError = true; |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 2111 | else if (!VerifyOnly) { |
| 2112 | // Find the field named by the designated initializer. |
| 2113 | RecordDecl::field_iterator F = RD->field_begin(); |
| 2114 | while (std::next(F) != Field) |
| 2115 | ++F; |
| 2116 | QualType ET = SemaRef.Context.getBaseElementType(F->getType()); |
Erik Pilkington | f8ccf05 | 2019-05-10 17:52:26 +0000 | [diff] [blame] | 2117 | if (checkDestructorReference(ET, InitLoc, SemaRef)) { |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 2118 | hadError = true; |
| 2119 | return; |
| 2120 | } |
| 2121 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2122 | |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 2123 | InitializedSomething = true; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 2124 | |
| 2125 | // Disable check for missing fields when designators are used. |
| 2126 | // This matches gcc behaviour. |
| 2127 | CheckForMissingFields = false; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2128 | continue; |
| 2129 | } |
| 2130 | |
| 2131 | if (Field == FieldEnd) { |
| 2132 | // We've run out of fields. We're done. |
| 2133 | break; |
| 2134 | } |
| 2135 | |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 2136 | // We've already initialized a member of a union. We're done. |
| 2137 | if (InitializedSomething && DeclType->isUnionType()) |
| 2138 | break; |
| 2139 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2140 | // If we've hit the flexible array member at the end, we're done. |
| 2141 | if (Field->getType()->isIncompleteArrayType()) |
| 2142 | break; |
| 2143 | |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 2144 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2145 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2146 | ++Field; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 2147 | continue; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 2148 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2149 | |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2150 | // Make sure we can use this declaration. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2151 | bool InvalidUse; |
| 2152 | if (VerifyOnly) |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 2153 | InvalidUse = !SemaRef.CanUseDecl(*Field, TreatUnavailableAsInvalid); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2154 | else |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2155 | InvalidUse = SemaRef.DiagnoseUseOfDecl( |
| 2156 | *Field, IList->getInit(Index)->getBeginLoc()); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2157 | if (InvalidUse) { |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2158 | ++Index; |
| 2159 | ++Field; |
| 2160 | hadError = true; |
| 2161 | continue; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2162 | } |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2163 | |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 2164 | if (!VerifyOnly) { |
| 2165 | QualType ET = SemaRef.Context.getBaseElementType(Field->getType()); |
Erik Pilkington | f8ccf05 | 2019-05-10 17:52:26 +0000 | [diff] [blame] | 2166 | if (checkDestructorReference(ET, InitLoc, SemaRef)) { |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 2167 | hadError = true; |
| 2168 | return; |
| 2169 | } |
| 2170 | } |
| 2171 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2172 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2173 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2174 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 2175 | StructuredList, StructuredIndex); |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 2176 | InitializedSomething = true; |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 2177 | |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 2178 | if (DeclType->isUnionType() && StructuredList) { |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 2179 | // Initialize the first field within the union. |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2180 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 2181 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2182 | |
| 2183 | ++Field; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 2184 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2185 | |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 2186 | // Emit warnings for missing struct field initializers. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2187 | if (!VerifyOnly && InitializedSomething && CheckForMissingFields && |
| 2188 | Field != FieldEnd && !Field->getType()->isIncompleteArrayType() && |
| 2189 | !DeclType->isUnionType()) { |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 2190 | // It is possible we have one or more unnamed bitfields remaining. |
| 2191 | // Find first (if any) named field and emit warning. |
| 2192 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 2193 | it != end; ++it) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 2194 | if (!it->isUnnamedBitfield() && !it->hasInClassInitializer()) { |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 2195 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
Aaron Ballman | b9bb201 | 2014-01-03 14:54:10 +0000 | [diff] [blame] | 2196 | diag::warn_missing_field_initializers) << *it; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 2197 | break; |
| 2198 | } |
| 2199 | } |
| 2200 | } |
| 2201 | |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 2202 | // Check that any remaining fields can be value-initialized if we're not |
| 2203 | // building a structured list. (If we are, we'll check this later.) |
| 2204 | if (!StructuredList && Field != FieldEnd && !DeclType->isUnionType() && |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 2205 | !Field->getType()->isIncompleteArrayType()) { |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 2206 | for (; Field != FieldEnd && !hadError; ++Field) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 2207 | if (!Field->isUnnamedBitfield() && !Field->hasInClassInitializer()) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 2208 | CheckEmptyInitializable( |
| 2209 | InitializedEntity::InitializeMember(*Field, &Entity), |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2210 | IList->getEndLoc()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 2211 | } |
| 2212 | } |
| 2213 | |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 2214 | // Check that the types of the remaining fields have accessible destructors. |
| 2215 | if (!VerifyOnly) { |
| 2216 | // If the initializer expression has a designated initializer, check the |
| 2217 | // elements for which a designated initializer is not provided too. |
| 2218 | RecordDecl::field_iterator I = HasDesignatedInit ? RD->field_begin() |
| 2219 | : Field; |
| 2220 | for (RecordDecl::field_iterator E = RD->field_end(); I != E; ++I) { |
| 2221 | QualType ET = SemaRef.Context.getBaseElementType(I->getType()); |
Erik Pilkington | f8ccf05 | 2019-05-10 17:52:26 +0000 | [diff] [blame] | 2222 | if (checkDestructorReference(ET, IList->getEndLoc(), SemaRef)) { |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 2223 | hadError = true; |
| 2224 | return; |
| 2225 | } |
| 2226 | } |
| 2227 | } |
| 2228 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2229 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 2230 | Index >= IList->getNumInits()) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2231 | return; |
| 2232 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2233 | if (CheckFlexibleArrayInit(Entity, IList->getInit(Index), *Field, |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 2234 | TopLevelObject)) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2235 | hadError = true; |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 2236 | ++Index; |
| 2237 | return; |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2238 | } |
| 2239 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2240 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2241 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2242 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2243 | if (isa<InitListExpr>(IList->getInit(Index))) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2244 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2245 | StructuredList, StructuredIndex); |
| 2246 | else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2247 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 2248 | StructuredList, StructuredIndex); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 2249 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 2250 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2251 | /// Expand a field designator that refers to a member of an |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2252 | /// anonymous struct or union into a series of field designators that |
| 2253 | /// refers to the field within the appropriate subobject. |
| 2254 | /// |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2255 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2256 | DesignatedInitExpr *DIE, |
| 2257 | unsigned DesigIdx, |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2258 | IndirectFieldDecl *IndirectField) { |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2259 | typedef DesignatedInitExpr::Designator Designator; |
| 2260 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2261 | // Build the replacement designators. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2262 | SmallVector<Designator, 4> Replacements; |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2263 | for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), |
| 2264 | PE = IndirectField->chain_end(); PI != PE; ++PI) { |
| 2265 | if (PI + 1 == PE) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2266 | Replacements.push_back(Designator((IdentifierInfo *)nullptr, |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2267 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 2268 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 2269 | else |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2270 | Replacements.push_back(Designator((IdentifierInfo *)nullptr, |
| 2271 | SourceLocation(), SourceLocation())); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2272 | assert(isa<FieldDecl>(*PI)); |
| 2273 | Replacements.back().setField(cast<FieldDecl>(*PI)); |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2274 | } |
| 2275 | |
| 2276 | // Expand the current designator into the set of replacement |
| 2277 | // designators, so we have a full subobject path down to where the |
| 2278 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 03e8bdc | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 2279 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2280 | &Replacements[0] + Replacements.size()); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2281 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2282 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2283 | static DesignatedInitExpr *CloneDesignatedInitExpr(Sema &SemaRef, |
| 2284 | DesignatedInitExpr *DIE) { |
| 2285 | unsigned NumIndexExprs = DIE->getNumSubExprs() - 1; |
| 2286 | SmallVector<Expr*, 4> IndexExprs(NumIndexExprs); |
| 2287 | for (unsigned I = 0; I < NumIndexExprs; ++I) |
| 2288 | IndexExprs[I] = DIE->getSubExpr(I + 1); |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 2289 | return DesignatedInitExpr::Create(SemaRef.Context, DIE->designators(), |
| 2290 | IndexExprs, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2291 | DIE->getEqualOrColonLoc(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2292 | DIE->usesGNUSyntax(), DIE->getInit()); |
| 2293 | } |
| 2294 | |
Kaelyn Uhrain | b02c5e9 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 2295 | namespace { |
| 2296 | |
| 2297 | // Callback to only accept typo corrections that are for field members of |
| 2298 | // the given struct or union. |
Bruno Ricci | 70ad396 | 2019-03-25 17:08:51 +0000 | [diff] [blame] | 2299 | class FieldInitializerValidatorCCC final : public CorrectionCandidateCallback { |
Kaelyn Uhrain | b02c5e9 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 2300 | public: |
| 2301 | explicit FieldInitializerValidatorCCC(RecordDecl *RD) |
| 2302 | : Record(RD) {} |
| 2303 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 2304 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
Kaelyn Uhrain | b02c5e9 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 2305 | FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>(); |
| 2306 | return FD && FD->getDeclContext()->getRedeclContext()->Equals(Record); |
| 2307 | } |
| 2308 | |
Bruno Ricci | 70ad396 | 2019-03-25 17:08:51 +0000 | [diff] [blame] | 2309 | std::unique_ptr<CorrectionCandidateCallback> clone() override { |
Jonas Devlieghere | 2b3d49b | 2019-08-14 23:04:18 +0000 | [diff] [blame] | 2310 | return std::make_unique<FieldInitializerValidatorCCC>(*this); |
Bruno Ricci | 70ad396 | 2019-03-25 17:08:51 +0000 | [diff] [blame] | 2311 | } |
| 2312 | |
Kaelyn Uhrain | b02c5e9 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 2313 | private: |
| 2314 | RecordDecl *Record; |
| 2315 | }; |
| 2316 | |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 2317 | } // end anonymous namespace |
Kaelyn Uhrain | b02c5e9 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 2318 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2319 | /// Check the well-formedness of a C99 designated initializer. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2320 | /// |
| 2321 | /// Determines whether the designated initializer @p DIE, which |
| 2322 | /// resides at the given @p Index within the initializer list @p |
| 2323 | /// IList, is well-formed for a current object of type @p DeclType |
| 2324 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2325 | /// within the current subobject is returned in either |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2326 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2327 | /// |
| 2328 | /// @param IList The initializer list in which this designated |
| 2329 | /// initializer occurs. |
| 2330 | /// |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 2331 | /// @param DIE The designated initializer expression. |
| 2332 | /// |
| 2333 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2334 | /// |
Dmitri Gribenko | adba9be | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 2335 | /// @param CurrentObjectType The type of the "current object" (C99 6.7.8p17), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2336 | /// into which the designation in @p DIE should refer. |
| 2337 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2338 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 2339 | /// a field, this will be set to the field declaration corresponding |
Richard Smith | 5030928 | 2019-08-30 22:52:55 +0000 | [diff] [blame] | 2340 | /// to the field named by the designator. On input, this is expected to be |
| 2341 | /// the next field that would be initialized in the absence of designation, |
| 2342 | /// if the complete object being initialized is a struct. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2343 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2344 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 2345 | /// DIE is an array designator or GNU array-range designator, this |
| 2346 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2347 | /// |
| 2348 | /// @param Index Index into @p IList where the designated initializer |
| 2349 | /// @p DIE occurs. |
| 2350 | /// |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2351 | /// @param StructuredList The initializer list expression that |
| 2352 | /// describes all of the subobject initializers in the order they'll |
| 2353 | /// actually be initialized. |
| 2354 | /// |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2355 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2356 | bool |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2357 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2358 | InitListExpr *IList, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2359 | DesignatedInitExpr *DIE, |
| 2360 | unsigned DesigIdx, |
| 2361 | QualType &CurrentObjectType, |
| 2362 | RecordDecl::field_iterator *NextField, |
| 2363 | llvm::APSInt *NextElementIndex, |
| 2364 | unsigned &Index, |
| 2365 | InitListExpr *StructuredList, |
| 2366 | unsigned &StructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2367 | bool FinishSubobjectInit, |
| 2368 | bool TopLevelObject) { |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 2369 | if (DesigIdx == DIE->size()) { |
Richard Smith | ff9bf92 | 2019-08-31 01:00:37 +0000 | [diff] [blame] | 2370 | // C++20 designated initialization can result in direct-list-initialization |
| 2371 | // of the designated subobject. This is the only way that we can end up |
| 2372 | // performing direct initialization as part of aggregate initialization, so |
| 2373 | // it needs special handling. |
| 2374 | if (DIE->isDirectInit()) { |
| 2375 | Expr *Init = DIE->getInit(); |
| 2376 | assert(isa<InitListExpr>(Init) && |
| 2377 | "designator result in direct non-list initialization?"); |
| 2378 | InitializationKind Kind = InitializationKind::CreateDirectList( |
| 2379 | DIE->getBeginLoc(), Init->getBeginLoc(), Init->getEndLoc()); |
| 2380 | InitializationSequence Seq(SemaRef, Entity, Kind, Init, |
| 2381 | /*TopLevelOfInitList*/ true); |
| 2382 | if (StructuredList) { |
| 2383 | ExprResult Result = VerifyOnly |
| 2384 | ? getDummyInit() |
| 2385 | : Seq.Perform(SemaRef, Entity, Kind, Init); |
| 2386 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 2387 | Result.get()); |
| 2388 | } |
| 2389 | ++Index; |
| 2390 | return !Seq; |
| 2391 | } |
| 2392 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2393 | // Check the actual initialization for the designated object type. |
| 2394 | bool prevHadError = hadError; |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 2395 | |
| 2396 | // Temporarily remove the designator expression from the |
| 2397 | // initializer list that the child calls see, so that we don't try |
| 2398 | // to re-process the designator. |
| 2399 | unsigned OldIndex = Index; |
| 2400 | IList->setInit(OldIndex, DIE->getInit()); |
| 2401 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2402 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2403 | StructuredList, StructuredIndex); |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 2404 | |
| 2405 | // Restore the designated initializer expression in the syntactic |
| 2406 | // form of the initializer list. |
| 2407 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 2408 | DIE->setInit(IList->getInit(OldIndex)); |
| 2409 | IList->setInit(OldIndex, DIE); |
| 2410 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2411 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2412 | } |
| 2413 | |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 2414 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2415 | bool IsFirstDesignator = (DesigIdx == 0); |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 2416 | if (IsFirstDesignator ? FullyStructuredList : StructuredList) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2417 | // Determine the structural initializer list that corresponds to the |
| 2418 | // current subobject. |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2419 | if (IsFirstDesignator) |
Richard Smith | 33e9be6 | 2019-08-29 22:49:33 +0000 | [diff] [blame] | 2420 | StructuredList = FullyStructuredList; |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2421 | else { |
| 2422 | Expr *ExistingInit = StructuredIndex < StructuredList->getNumInits() ? |
| 2423 | StructuredList->getInit(StructuredIndex) : nullptr; |
| 2424 | if (!ExistingInit && StructuredList->hasArrayFiller()) |
| 2425 | ExistingInit = StructuredList->getArrayFiller(); |
| 2426 | |
| 2427 | if (!ExistingInit) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2428 | StructuredList = getStructuredSubobjectInit( |
| 2429 | IList, Index, CurrentObjectType, StructuredList, StructuredIndex, |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2430 | SourceRange(D->getBeginLoc(), DIE->getEndLoc())); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2431 | else if (InitListExpr *Result = dyn_cast<InitListExpr>(ExistingInit)) |
| 2432 | StructuredList = Result; |
Richard Smith | 5030928 | 2019-08-30 22:52:55 +0000 | [diff] [blame] | 2433 | else { |
| 2434 | // We are creating an initializer list that initializes the |
| 2435 | // subobjects of the current object, but there was already an |
| 2436 | // initialization that completely initialized the current |
| 2437 | // subobject, e.g., by a compound literal: |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2438 | // |
Richard Smith | 5030928 | 2019-08-30 22:52:55 +0000 | [diff] [blame] | 2439 | // struct X { int a, b; }; |
| 2440 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2441 | // |
Richard Smith | 5030928 | 2019-08-30 22:52:55 +0000 | [diff] [blame] | 2442 | // Here, xs[0].a == 1 and xs[0].b == 3, since the second, |
| 2443 | // designated initializer re-initializes only its current object |
| 2444 | // subobject [0].b. |
| 2445 | diagnoseInitOverride(ExistingInit, |
| 2446 | SourceRange(D->getBeginLoc(), DIE->getEndLoc()), |
| 2447 | /*FullyOverwritten=*/false); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2448 | |
Richard Smith | 5030928 | 2019-08-30 22:52:55 +0000 | [diff] [blame] | 2449 | if (!VerifyOnly) { |
| 2450 | if (DesignatedInitUpdateExpr *E = |
| 2451 | dyn_cast<DesignatedInitUpdateExpr>(ExistingInit)) |
| 2452 | StructuredList = E->getUpdater(); |
| 2453 | else { |
| 2454 | DesignatedInitUpdateExpr *DIUE = new (SemaRef.Context) |
| 2455 | DesignatedInitUpdateExpr(SemaRef.Context, D->getBeginLoc(), |
| 2456 | ExistingInit, DIE->getEndLoc()); |
| 2457 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, DIUE); |
| 2458 | StructuredList = DIUE->getUpdater(); |
| 2459 | } |
| 2460 | } else { |
| 2461 | // We don't need to track the structured representation of a |
| 2462 | // designated init update of an already-fully-initialized object in |
| 2463 | // verify-only mode. The only reason we would need the structure is |
| 2464 | // to determine where the uninitialized "holes" are, and in this |
| 2465 | // case, we know there aren't any and we can't introduce any. |
| 2466 | StructuredList = nullptr; |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2467 | } |
| 2468 | } |
| 2469 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2470 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2471 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2472 | if (D->isFieldDesignator()) { |
| 2473 | // C99 6.7.8p7: |
| 2474 | // |
| 2475 | // If a designator has the form |
| 2476 | // |
| 2477 | // . identifier |
| 2478 | // |
| 2479 | // then the current object (defined below) shall have |
| 2480 | // structure or union type and the identifier shall be the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2481 | // name of a member of that type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2482 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2483 | if (!RT) { |
| 2484 | SourceLocation Loc = D->getDotLoc(); |
| 2485 | if (Loc.isInvalid()) |
| 2486 | Loc = D->getFieldLoc(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2487 | if (!VerifyOnly) |
| 2488 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2489 | << SemaRef.getLangOpts().CPlusPlus << CurrentObjectType; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2490 | ++Index; |
| 2491 | return true; |
| 2492 | } |
| 2493 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2494 | FieldDecl *KnownField = D->getField(); |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2495 | if (!KnownField) { |
| 2496 | IdentifierInfo *FieldName = D->getFieldName(); |
| 2497 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
| 2498 | for (NamedDecl *ND : Lookup) { |
| 2499 | if (auto *FD = dyn_cast<FieldDecl>(ND)) { |
| 2500 | KnownField = FD; |
| 2501 | break; |
| 2502 | } |
| 2503 | if (auto *IFD = dyn_cast<IndirectFieldDecl>(ND)) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2504 | // In verify mode, don't modify the original. |
| 2505 | if (VerifyOnly) |
| 2506 | DIE = CloneDesignatedInitExpr(SemaRef, DIE); |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2507 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IFD); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2508 | D = DIE->getDesignator(DesigIdx); |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2509 | KnownField = cast<FieldDecl>(*IFD->chain_begin()); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2510 | break; |
| 2511 | } |
| 2512 | } |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2513 | if (!KnownField) { |
| 2514 | if (VerifyOnly) { |
| 2515 | ++Index; |
| 2516 | return true; // No typo correction when just trying this out. |
| 2517 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2518 | |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2519 | // Name lookup found something, but it wasn't a field. |
| 2520 | if (!Lookup.empty()) { |
| 2521 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
| 2522 | << FieldName; |
| 2523 | SemaRef.Diag(Lookup.front()->getLocation(), |
| 2524 | diag::note_field_designator_found); |
| 2525 | ++Index; |
| 2526 | return true; |
| 2527 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2528 | |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2529 | // Name lookup didn't find anything. |
| 2530 | // Determine whether this was a typo for another field name. |
Bruno Ricci | 70ad396 | 2019-03-25 17:08:51 +0000 | [diff] [blame] | 2531 | FieldInitializerValidatorCCC CCC(RT->getDecl()); |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 2532 | if (TypoCorrection Corrected = SemaRef.CorrectTypo( |
| 2533 | DeclarationNameInfo(FieldName, D->getFieldLoc()), |
Bruno Ricci | 70ad396 | 2019-03-25 17:08:51 +0000 | [diff] [blame] | 2534 | Sema::LookupMemberName, /*Scope=*/nullptr, /*SS=*/nullptr, CCC, |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 2535 | Sema::CTK_ErrorRecovery, RT->getDecl())) { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 2536 | SemaRef.diagnoseTypo( |
| 2537 | Corrected, |
| 2538 | SemaRef.PDiag(diag::err_field_designator_unknown_suggest) |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2539 | << FieldName << CurrentObjectType); |
| 2540 | KnownField = Corrected.getCorrectionDeclAs<FieldDecl>(); |
Benjamin Kramer | afe718f | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 2541 | hadError = true; |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 2542 | } else { |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2543 | // Typo correction didn't find anything. |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 2544 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 2545 | << FieldName << CurrentObjectType; |
| 2546 | ++Index; |
| 2547 | return true; |
| 2548 | } |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 2549 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2550 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2551 | |
Richard Smith | 5030928 | 2019-08-30 22:52:55 +0000 | [diff] [blame] | 2552 | unsigned NumBases = 0; |
Akira Hatanaka | 8eccb9b | 2017-01-17 19:35:54 +0000 | [diff] [blame] | 2553 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) |
Richard Smith | 5030928 | 2019-08-30 22:52:55 +0000 | [diff] [blame] | 2554 | NumBases = CXXRD->getNumBases(); |
| 2555 | |
| 2556 | unsigned FieldIndex = NumBases; |
Akira Hatanaka | 8eccb9b | 2017-01-17 19:35:54 +0000 | [diff] [blame] | 2557 | |
David Majnemer | 58e4ea9 | 2014-08-23 01:48:50 +0000 | [diff] [blame] | 2558 | for (auto *FI : RT->getDecl()->fields()) { |
| 2559 | if (FI->isUnnamedBitfield()) |
| 2560 | continue; |
Richard Smith | fe1bc70 | 2016-04-08 19:57:40 +0000 | [diff] [blame] | 2561 | if (declaresSameEntity(KnownField, FI)) { |
| 2562 | KnownField = FI; |
David Majnemer | 58e4ea9 | 2014-08-23 01:48:50 +0000 | [diff] [blame] | 2563 | break; |
Richard Smith | fe1bc70 | 2016-04-08 19:57:40 +0000 | [diff] [blame] | 2564 | } |
David Majnemer | 58e4ea9 | 2014-08-23 01:48:50 +0000 | [diff] [blame] | 2565 | ++FieldIndex; |
| 2566 | } |
| 2567 | |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2568 | RecordDecl::field_iterator Field = |
| 2569 | RecordDecl::field_iterator(DeclContext::decl_iterator(KnownField)); |
| 2570 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2571 | // All of the fields of a union are located at the same place in |
| 2572 | // the initializer list. |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 2573 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2574 | FieldIndex = 0; |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 2575 | if (StructuredList) { |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2576 | FieldDecl *CurrentField = StructuredList->getInitializedFieldInUnion(); |
Richard Smith | fe1bc70 | 2016-04-08 19:57:40 +0000 | [diff] [blame] | 2577 | if (CurrentField && !declaresSameEntity(CurrentField, *Field)) { |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2578 | assert(StructuredList->getNumInits() == 1 |
| 2579 | && "A union should never have more than one initializer!"); |
| 2580 | |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2581 | Expr *ExistingInit = StructuredList->getInit(0); |
Richard Smith | 5030928 | 2019-08-30 22:52:55 +0000 | [diff] [blame] | 2582 | if (ExistingInit) { |
Vassil Vassilev | 1a1678e | 2017-04-14 08:48:08 +0000 | [diff] [blame] | 2583 | // We're about to throw away an initializer, emit warning. |
Richard Smith | 5030928 | 2019-08-30 22:52:55 +0000 | [diff] [blame] | 2584 | diagnoseInitOverride( |
| 2585 | ExistingInit, SourceRange(D->getBeginLoc(), DIE->getEndLoc())); |
Vassil Vassilev | 1a1678e | 2017-04-14 08:48:08 +0000 | [diff] [blame] | 2586 | } |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2587 | |
| 2588 | // remove existing initializer |
| 2589 | StructuredList->resizeInits(SemaRef.Context, 0); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2590 | StructuredList->setInitializedFieldInUnion(nullptr); |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2591 | } |
| 2592 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2593 | StructuredList->setInitializedFieldInUnion(*Field); |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2594 | } |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 2595 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2596 | |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2597 | // Make sure we can use this declaration. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2598 | bool InvalidUse; |
| 2599 | if (VerifyOnly) |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 2600 | InvalidUse = !SemaRef.CanUseDecl(*Field, TreatUnavailableAsInvalid); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2601 | else |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2602 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, D->getFieldLoc()); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2603 | if (InvalidUse) { |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2604 | ++Index; |
| 2605 | return true; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2606 | } |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2607 | |
Richard Smith | 5030928 | 2019-08-30 22:52:55 +0000 | [diff] [blame] | 2608 | // C++20 [dcl.init.list]p3: |
| 2609 | // The ordered identifiers in the designators of the designated- |
| 2610 | // initializer-list shall form a subsequence of the ordered identifiers |
| 2611 | // in the direct non-static data members of T. |
| 2612 | // |
| 2613 | // Note that this is not a condition on forming the aggregate |
| 2614 | // initialization, only on actually performing initialization, |
| 2615 | // so it is not checked in VerifyOnly mode. |
| 2616 | // |
| 2617 | // FIXME: This is the only reordering diagnostic we produce, and it only |
| 2618 | // catches cases where we have a top-level field designator that jumps |
| 2619 | // backwards. This is the only such case that is reachable in an |
| 2620 | // otherwise-valid C++20 program, so is the only case that's required for |
| 2621 | // conformance, but for consistency, we should diagnose all the other |
| 2622 | // cases where a designator takes us backwards too. |
| 2623 | if (IsFirstDesignator && !VerifyOnly && SemaRef.getLangOpts().CPlusPlus && |
| 2624 | NextField && |
| 2625 | (*NextField == RT->getDecl()->field_end() || |
| 2626 | (*NextField)->getFieldIndex() > Field->getFieldIndex() + 1)) { |
| 2627 | // Find the field that we just initialized. |
| 2628 | FieldDecl *PrevField = nullptr; |
| 2629 | for (auto FI = RT->getDecl()->field_begin(); |
| 2630 | FI != RT->getDecl()->field_end(); ++FI) { |
| 2631 | if (FI->isUnnamedBitfield()) |
| 2632 | continue; |
| 2633 | if (*NextField != RT->getDecl()->field_end() && |
| 2634 | declaresSameEntity(*FI, **NextField)) |
| 2635 | break; |
| 2636 | PrevField = *FI; |
| 2637 | } |
| 2638 | |
| 2639 | if (PrevField && |
| 2640 | PrevField->getFieldIndex() > KnownField->getFieldIndex()) { |
| 2641 | SemaRef.Diag(DIE->getBeginLoc(), diag::ext_designated_init_reordered) |
| 2642 | << KnownField << PrevField << DIE->getSourceRange(); |
| 2643 | |
| 2644 | unsigned OldIndex = NumBases + PrevField->getFieldIndex(); |
| 2645 | if (StructuredList && OldIndex <= StructuredList->getNumInits()) { |
| 2646 | if (Expr *PrevInit = StructuredList->getInit(OldIndex)) { |
| 2647 | SemaRef.Diag(PrevInit->getBeginLoc(), |
| 2648 | diag::note_previous_field_init) |
| 2649 | << PrevField << PrevInit->getSourceRange(); |
| 2650 | } |
| 2651 | } |
| 2652 | } |
| 2653 | } |
| 2654 | |
| 2655 | |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 2656 | // Update the designator with the field declaration. |
| 2657 | if (!VerifyOnly) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2658 | D->setField(*Field); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2659 | |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 2660 | // Make sure that our non-designated initializer list has space |
| 2661 | // for a subobject corresponding to this field. |
| 2662 | if (StructuredList && FieldIndex >= StructuredList->getNumInits()) |
| 2663 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2664 | |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2665 | // This designator names a flexible array member. |
| 2666 | if (Field->getType()->isIncompleteArrayType()) { |
| 2667 | bool Invalid = false; |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 2668 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2669 | // We can't designate an object within the flexible array |
| 2670 | // member (because GCC doesn't allow it). |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2671 | if (!VerifyOnly) { |
| 2672 | DesignatedInitExpr::Designator *NextD |
| 2673 | = DIE->getDesignator(DesigIdx + 1); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2674 | SemaRef.Diag(NextD->getBeginLoc(), |
| 2675 | diag::err_designator_into_flexible_array_member) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2676 | << SourceRange(NextD->getBeginLoc(), DIE->getEndLoc()); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2677 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2678 | << *Field; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2679 | } |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2680 | Invalid = true; |
| 2681 | } |
| 2682 | |
Chris Lattner | 001b29c | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 2683 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 2684 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2685 | // The initializer is not an initializer list. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2686 | if (!VerifyOnly) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2687 | SemaRef.Diag(DIE->getInit()->getBeginLoc(), |
| 2688 | diag::err_flexible_array_init_needs_braces) |
| 2689 | << DIE->getInit()->getSourceRange(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2690 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2691 | << *Field; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2692 | } |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2693 | Invalid = true; |
| 2694 | } |
| 2695 | |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 2696 | // Check GNU flexible array initializer. |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2697 | if (!Invalid && CheckFlexibleArrayInit(Entity, DIE->getInit(), *Field, |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 2698 | TopLevelObject)) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2699 | Invalid = true; |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2700 | |
| 2701 | if (Invalid) { |
| 2702 | ++Index; |
| 2703 | return true; |
| 2704 | } |
| 2705 | |
| 2706 | // Initialize the array. |
| 2707 | bool prevHadError = hadError; |
| 2708 | unsigned newStructuredIndex = FieldIndex; |
| 2709 | unsigned OldIndex = Index; |
| 2710 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2711 | |
| 2712 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2713 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2714 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2715 | StructuredList, newStructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2716 | |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2717 | IList->setInit(OldIndex, DIE); |
| 2718 | if (hadError && !prevHadError) { |
| 2719 | ++Field; |
| 2720 | ++FieldIndex; |
| 2721 | if (NextField) |
| 2722 | *NextField = Field; |
| 2723 | StructuredIndex = FieldIndex; |
| 2724 | return true; |
| 2725 | } |
| 2726 | } else { |
| 2727 | // Recurse to check later designated subobjects. |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 2728 | QualType FieldType = Field->getType(); |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2729 | unsigned newStructuredIndex = FieldIndex; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2730 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2731 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2732 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2733 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2734 | FieldType, nullptr, nullptr, Index, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2735 | StructuredList, newStructuredIndex, |
Alexey Bataev | 86a489e | 2016-01-25 05:14:03 +0000 | [diff] [blame] | 2736 | FinishSubobjectInit, false)) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2737 | return true; |
| 2738 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2739 | |
| 2740 | // Find the position of the next field to be initialized in this |
| 2741 | // subobject. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2742 | ++Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2743 | ++FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2744 | |
| 2745 | // If this the first designator, our caller will continue checking |
| 2746 | // the rest of this struct/class/union subobject. |
| 2747 | if (IsFirstDesignator) { |
| 2748 | if (NextField) |
| 2749 | *NextField = Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2750 | StructuredIndex = FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2751 | return false; |
| 2752 | } |
| 2753 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2754 | if (!FinishSubobjectInit) |
| 2755 | return false; |
| 2756 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2757 | // We've already initialized something in the union; we're done. |
| 2758 | if (RT->getDecl()->isUnion()) |
| 2759 | return hadError; |
| 2760 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2761 | // Check the remaining fields within this class/struct/union subobject. |
| 2762 | bool prevHadError = hadError; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2763 | |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 2764 | auto NoBases = |
| 2765 | CXXRecordDecl::base_class_range(CXXRecordDecl::base_class_iterator(), |
| 2766 | CXXRecordDecl::base_class_iterator()); |
| 2767 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, NoBases, Field, |
| 2768 | false, Index, StructuredList, FieldIndex); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2769 | return hadError && !prevHadError; |
| 2770 | } |
| 2771 | |
| 2772 | // C99 6.7.8p6: |
| 2773 | // |
| 2774 | // If a designator has the form |
| 2775 | // |
| 2776 | // [ constant-expression ] |
| 2777 | // |
| 2778 | // then the current object (defined below) shall have array |
| 2779 | // type and the expression shall be an integer constant |
| 2780 | // expression. If the array is of unknown size, any |
| 2781 | // nonnegative value is valid. |
| 2782 | // |
| 2783 | // Additionally, cope with the GNU extension that permits |
| 2784 | // designators of the form |
| 2785 | // |
| 2786 | // [ constant-expression ... constant-expression ] |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 2787 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2788 | if (!AT) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2789 | if (!VerifyOnly) |
| 2790 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
| 2791 | << CurrentObjectType; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2792 | ++Index; |
| 2793 | return true; |
| 2794 | } |
| 2795 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2796 | Expr *IndexExpr = nullptr; |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2797 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 2798 | if (D->isArrayDesignator()) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2799 | IndexExpr = DIE->getArrayIndex(*D); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2800 | DesignatedStartIndex = IndexExpr->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2801 | DesignatedEndIndex = DesignatedStartIndex; |
| 2802 | } else { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2803 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2804 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2805 | DesignatedStartIndex = |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2806 | DIE->getArrayRangeStart(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2807 | DesignatedEndIndex = |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2808 | DIE->getArrayRangeEnd(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2809 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2810 | |
Chris Lattner | b0ed51d | 2011-02-19 22:28:58 +0000 | [diff] [blame] | 2811 | // Codegen can't handle evaluating array range designators that have side |
| 2812 | // effects, because we replicate the AST value for each initialized element. |
| 2813 | // As such, set the sawArrayRangeDesignator() bit if we initialize multiple |
| 2814 | // elements with something that has a side effect, so codegen can emit an |
| 2815 | // "error unsupported" error instead of miscompiling the app. |
| 2816 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2817 | DIE->getInit()->HasSideEffects(SemaRef.Context) && !VerifyOnly) |
Douglas Gregor | bf7207a | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 2818 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2819 | } |
| 2820 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2821 | if (isa<ConstantArrayType>(AT)) { |
| 2822 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2823 | DesignatedStartIndex |
| 2824 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2825 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2826 | DesignatedEndIndex |
| 2827 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2828 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 2829 | if (DesignatedEndIndex >= MaxElements) { |
Eli Friedman | ed0f916 | 2011-09-26 18:53:43 +0000 | [diff] [blame] | 2830 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2831 | SemaRef.Diag(IndexExpr->getBeginLoc(), |
| 2832 | diag::err_array_designator_too_large) |
| 2833 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
| 2834 | << IndexExpr->getSourceRange(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2835 | ++Index; |
| 2836 | return true; |
| 2837 | } |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2838 | } else { |
Argyrios Kyrtzidis | 4746c2f | 2015-07-27 23:16:53 +0000 | [diff] [blame] | 2839 | unsigned DesignatedIndexBitWidth = |
| 2840 | ConstantArrayType::getMaxSizeBits(SemaRef.Context); |
| 2841 | DesignatedStartIndex = |
| 2842 | DesignatedStartIndex.extOrTrunc(DesignatedIndexBitWidth); |
| 2843 | DesignatedEndIndex = |
| 2844 | DesignatedEndIndex.extOrTrunc(DesignatedIndexBitWidth); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2845 | DesignatedStartIndex.setIsUnsigned(true); |
| 2846 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2847 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2848 | |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 2849 | bool IsStringLiteralInitUpdate = |
| 2850 | StructuredList && StructuredList->isStringLiteralInit(); |
| 2851 | if (IsStringLiteralInitUpdate && VerifyOnly) { |
| 2852 | // We're just verifying an update to a string literal init. We don't need |
| 2853 | // to split the string up into individual characters to do that. |
| 2854 | StructuredList = nullptr; |
| 2855 | } else if (IsStringLiteralInitUpdate) { |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2856 | // We're modifying a string literal init; we have to decompose the string |
| 2857 | // so we can modify the individual characters. |
| 2858 | ASTContext &Context = SemaRef.Context; |
| 2859 | Expr *SubExpr = StructuredList->getInit(0)->IgnoreParens(); |
| 2860 | |
| 2861 | // Compute the character type |
| 2862 | QualType CharTy = AT->getElementType(); |
| 2863 | |
| 2864 | // Compute the type of the integer literals. |
| 2865 | QualType PromotedCharTy = CharTy; |
| 2866 | if (CharTy->isPromotableIntegerType()) |
| 2867 | PromotedCharTy = Context.getPromotedIntegerType(CharTy); |
| 2868 | unsigned PromotedCharTyWidth = Context.getTypeSize(PromotedCharTy); |
| 2869 | |
| 2870 | if (StringLiteral *SL = dyn_cast<StringLiteral>(SubExpr)) { |
| 2871 | // Get the length of the string. |
| 2872 | uint64_t StrLen = SL->getLength(); |
| 2873 | if (cast<ConstantArrayType>(AT)->getSize().ult(StrLen)) |
| 2874 | StrLen = cast<ConstantArrayType>(AT)->getSize().getZExtValue(); |
| 2875 | StructuredList->resizeInits(Context, StrLen); |
| 2876 | |
| 2877 | // Build a literal for each character in the string, and put them into |
| 2878 | // the init list. |
| 2879 | for (unsigned i = 0, e = StrLen; i != e; ++i) { |
| 2880 | llvm::APInt CodeUnit(PromotedCharTyWidth, SL->getCodeUnit(i)); |
| 2881 | Expr *Init = new (Context) IntegerLiteral( |
Eli Friedman | 6cc05f7 | 2013-06-11 22:26:34 +0000 | [diff] [blame] | 2882 | Context, CodeUnit, PromotedCharTy, SubExpr->getExprLoc()); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2883 | if (CharTy != PromotedCharTy) |
| 2884 | Init = ImplicitCastExpr::Create(Context, CharTy, CK_IntegralCast, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2885 | Init, nullptr, VK_RValue); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2886 | StructuredList->updateInit(Context, i, Init); |
| 2887 | } |
| 2888 | } else { |
| 2889 | ObjCEncodeExpr *E = cast<ObjCEncodeExpr>(SubExpr); |
| 2890 | std::string Str; |
| 2891 | Context.getObjCEncodingForType(E->getEncodedType(), Str); |
| 2892 | |
| 2893 | // Get the length of the string. |
| 2894 | uint64_t StrLen = Str.size(); |
| 2895 | if (cast<ConstantArrayType>(AT)->getSize().ult(StrLen)) |
| 2896 | StrLen = cast<ConstantArrayType>(AT)->getSize().getZExtValue(); |
| 2897 | StructuredList->resizeInits(Context, StrLen); |
| 2898 | |
| 2899 | // Build a literal for each character in the string, and put them into |
| 2900 | // the init list. |
| 2901 | for (unsigned i = 0, e = StrLen; i != e; ++i) { |
| 2902 | llvm::APInt CodeUnit(PromotedCharTyWidth, Str[i]); |
| 2903 | Expr *Init = new (Context) IntegerLiteral( |
Eli Friedman | 6cc05f7 | 2013-06-11 22:26:34 +0000 | [diff] [blame] | 2904 | Context, CodeUnit, PromotedCharTy, SubExpr->getExprLoc()); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2905 | if (CharTy != PromotedCharTy) |
| 2906 | Init = ImplicitCastExpr::Create(Context, CharTy, CK_IntegralCast, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2907 | Init, nullptr, VK_RValue); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2908 | StructuredList->updateInit(Context, i, Init); |
| 2909 | } |
| 2910 | } |
| 2911 | } |
| 2912 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2913 | // Make sure that our non-designated initializer list has space |
| 2914 | // for a subobject corresponding to this array element. |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 2915 | if (StructuredList && |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2916 | DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2917 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2918 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2919 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2920 | // Repeatedly perform subobject initializations in the range |
| 2921 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2922 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2923 | // Move to the next designator |
| 2924 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2925 | unsigned OldIndex = Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2926 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2927 | InitializedEntity ElementEntity = |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2928 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2929 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2930 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 2931 | // Recurse to check later designated subobjects. |
| 2932 | QualType ElementType = AT->getElementType(); |
| 2933 | Index = OldIndex; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2934 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2935 | ElementEntity.setElementIndex(ElementIndex); |
Alexey Bataev | 86a489e | 2016-01-25 05:14:03 +0000 | [diff] [blame] | 2936 | if (CheckDesignatedInitializer( |
| 2937 | ElementEntity, IList, DIE, DesigIdx + 1, ElementType, nullptr, |
| 2938 | nullptr, Index, StructuredList, ElementIndex, |
| 2939 | FinishSubobjectInit && (DesignatedStartIndex == DesignatedEndIndex), |
| 2940 | false)) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2941 | return true; |
| 2942 | |
| 2943 | // Move to the next index in the array that we'll be initializing. |
| 2944 | ++DesignatedStartIndex; |
| 2945 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2946 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2947 | |
| 2948 | // If this the first designator, our caller will continue checking |
| 2949 | // the rest of this array subobject. |
| 2950 | if (IsFirstDesignator) { |
| 2951 | if (NextElementIndex) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2952 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2953 | StructuredIndex = ElementIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2954 | return false; |
| 2955 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2956 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2957 | if (!FinishSubobjectInit) |
| 2958 | return false; |
| 2959 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2960 | // Check the remaining elements within this array subobject. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2961 | bool prevHadError = hadError; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2962 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 2963 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2964 | StructuredList, ElementIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2965 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2966 | } |
| 2967 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2968 | // Get the structured initializer list for a subobject of type |
| 2969 | // @p CurrentObjectType. |
| 2970 | InitListExpr * |
| 2971 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 2972 | QualType CurrentObjectType, |
| 2973 | InitListExpr *StructuredList, |
| 2974 | unsigned StructuredIndex, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2975 | SourceRange InitRange, |
| 2976 | bool IsFullyOverwritten) { |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 2977 | if (!StructuredList) |
| 2978 | return nullptr; |
| 2979 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2980 | Expr *ExistingInit = nullptr; |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 2981 | if (StructuredIndex < StructuredList->getNumInits()) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2982 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2983 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2984 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2985 | // There might have already been initializers for subobjects of the current |
| 2986 | // object, but a subsequent initializer list will overwrite the entirety |
| 2987 | // of the current object. (See DR 253 and C99 6.7.8p21). e.g., |
| 2988 | // |
| 2989 | // struct P { char x[6]; }; |
| 2990 | // struct P l = { .x[2] = 'x', .x = { [0] = 'f' } }; |
| 2991 | // |
| 2992 | // The first designated initializer is ignored, and l.x is just "f". |
| 2993 | if (!IsFullyOverwritten) |
| 2994 | return Result; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2995 | |
Richard Smith | 5030928 | 2019-08-30 22:52:55 +0000 | [diff] [blame] | 2996 | if (ExistingInit) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2997 | // We are creating an initializer list that initializes the |
| 2998 | // subobjects of the current object, but there was already an |
| 2999 | // initialization that completely initialized the current |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 3000 | // subobject: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3001 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 3002 | // struct X { int a, b; }; |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 3003 | // struct X xs[] = { [0] = { 1, 2 }, [0].b = 3 }; |
| 3004 | // |
| 3005 | // Here, xs[0].a == 1 and xs[0].b == 3, since the second, |
| 3006 | // designated initializer overwrites the [0].b initializer |
| 3007 | // from the prior initialization. |
| 3008 | // |
| 3009 | // When the existing initializer is an expression rather than an |
| 3010 | // initializer list, we cannot decompose and update it in this way. |
| 3011 | // For example: |
| 3012 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 3013 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3014 | // |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 3015 | // This case is handled by CheckDesignatedInitializer. |
Richard Smith | 5030928 | 2019-08-30 22:52:55 +0000 | [diff] [blame] | 3016 | diagnoseInitOverride(ExistingInit, InitRange); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 3017 | } |
| 3018 | |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 3019 | unsigned ExpectedNumInits = 0; |
| 3020 | if (Index < IList->getNumInits()) { |
| 3021 | if (auto *Init = dyn_cast_or_null<InitListExpr>(IList->getInit(Index))) |
| 3022 | ExpectedNumInits = Init->getNumInits(); |
| 3023 | else |
| 3024 | ExpectedNumInits = IList->getNumInits() - Index; |
| 3025 | } |
| 3026 | |
| 3027 | InitListExpr *Result = |
| 3028 | createInitListExpr(CurrentObjectType, InitRange, ExpectedNumInits); |
| 3029 | |
| 3030 | // Link this new initializer list into the structured initializer |
| 3031 | // lists. |
| 3032 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
| 3033 | return Result; |
| 3034 | } |
| 3035 | |
| 3036 | InitListExpr * |
| 3037 | InitListChecker::createInitListExpr(QualType CurrentObjectType, |
| 3038 | SourceRange InitRange, |
| 3039 | unsigned ExpectedNumInits) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3040 | InitListExpr *Result |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 3041 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
Dmitri Gribenko | 78852e9 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 3042 | InitRange.getBegin(), None, |
Ted Kremenek | 013041e | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 3043 | InitRange.getEnd()); |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 3044 | |
Eli Friedman | 91f5ae5 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 3045 | QualType ResultType = CurrentObjectType; |
| 3046 | if (!ResultType->isArrayType()) |
| 3047 | ResultType = ResultType.getNonLValueExprType(SemaRef.Context); |
| 3048 | Result->setType(ResultType); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 3049 | |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 3050 | // Pre-allocate storage for the structured initializer list. |
| 3051 | unsigned NumElements = 0; |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 3052 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3053 | if (const ArrayType *AType |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 3054 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 3055 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 3056 | NumElements = CAType->getSize().getZExtValue(); |
| 3057 | // Simple heuristic so that we don't allocate a very large |
| 3058 | // initializer with many empty entries at the end. |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 3059 | if (NumElements > ExpectedNumInits) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 3060 | NumElements = 0; |
| 3061 | } |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 3062 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) { |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 3063 | NumElements = VType->getNumElements(); |
Richard Smith | cd839cc | 2019-08-29 22:49:34 +0000 | [diff] [blame] | 3064 | } else if (CurrentObjectType->isRecordType()) { |
| 3065 | NumElements = numStructUnionElements(CurrentObjectType); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 3066 | } |
| 3067 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 3068 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 3069 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 3070 | return Result; |
| 3071 | } |
| 3072 | |
| 3073 | /// Update the initializer at index @p StructuredIndex within the |
| 3074 | /// structured initializer list to the value @p expr. |
| 3075 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 3076 | unsigned &StructuredIndex, |
| 3077 | Expr *expr) { |
| 3078 | // No structured initializer list to update |
| 3079 | if (!StructuredList) |
| 3080 | return; |
| 3081 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 3082 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 3083 | StructuredIndex, expr)) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 3084 | // This initializer overwrites a previous initializer. Warn. |
Richard Smith | 5030928 | 2019-08-30 22:52:55 +0000 | [diff] [blame] | 3085 | diagnoseInitOverride(PrevInit, expr->getSourceRange()); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 3086 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3087 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 3088 | ++StructuredIndex; |
| 3089 | } |
| 3090 | |
Richard Smith | 5030928 | 2019-08-30 22:52:55 +0000 | [diff] [blame] | 3091 | /// Determine whether we can perform aggregate initialization for the purposes |
| 3092 | /// of overload resolution. |
| 3093 | bool Sema::CanPerformAggregateInitializationForOverloadResolution( |
| 3094 | const InitializedEntity &Entity, InitListExpr *From) { |
| 3095 | QualType Type = Entity.getType(); |
| 3096 | InitListChecker Check(*this, Entity, From, Type, /*VerifyOnly=*/true, |
| 3097 | /*TreatUnavailableAsInvalid=*/false, |
| 3098 | /*InOverloadResolution=*/true); |
| 3099 | return !Check.HadError(); |
| 3100 | } |
| 3101 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3102 | /// Check that the given Index expression is a valid array designator |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3103 | /// value. This is essentially just a wrapper around |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 3104 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3105 | /// and produces a reasonable diagnostic if there is a |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3106 | /// failure. Returns the index expression, possibly with an implicit cast |
| 3107 | /// added, on success. If everything went okay, Value will receive the |
| 3108 | /// value of the constant expression. |
| 3109 | static ExprResult |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 3110 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3111 | SourceLocation Loc = Index->getBeginLoc(); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3112 | |
| 3113 | // Make sure this is an integer constant expression. |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3114 | ExprResult Result = S.VerifyIntegerConstantExpression(Index, &Value); |
| 3115 | if (Result.isInvalid()) |
| 3116 | return Result; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3117 | |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 3118 | if (Value.isSigned() && Value.isNegative()) |
| 3119 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3120 | << Value.toString(10) << Index->getSourceRange(); |
| 3121 | |
Douglas Gregor | 51650d3 | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 3122 | Value.setIsUnsigned(true); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3123 | return Result; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3124 | } |
| 3125 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3126 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Richard Smith | ff9bf92 | 2019-08-31 01:00:37 +0000 | [diff] [blame] | 3127 | SourceLocation EqualOrColonLoc, |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 3128 | bool GNUSyntax, |
| 3129 | ExprResult Init) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3130 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 3131 | |
| 3132 | bool Invalid = false; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3133 | SmallVector<ASTDesignator, 32> Designators; |
| 3134 | SmallVector<Expr *, 32> InitExpressions; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3135 | |
| 3136 | // Build designators and check array designator expressions. |
| 3137 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 3138 | const Designator &D = Desig.getDesignator(Idx); |
| 3139 | switch (D.getKind()) { |
| 3140 | case Designator::FieldDesignator: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3141 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3142 | D.getFieldLoc())); |
| 3143 | break; |
| 3144 | |
| 3145 | case Designator::ArrayDesignator: { |
| 3146 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 3147 | llvm::APSInt IndexValue; |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3148 | if (!Index->isTypeDependent() && !Index->isValueDependent()) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3149 | Index = CheckArrayDesignatorExpr(*this, Index, IndexValue).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3150 | if (!Index) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3151 | Invalid = true; |
| 3152 | else { |
| 3153 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3154 | D.getLBracketLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3155 | D.getRBracketLoc())); |
| 3156 | InitExpressions.push_back(Index); |
| 3157 | } |
| 3158 | break; |
| 3159 | } |
| 3160 | |
| 3161 | case Designator::ArrayRangeDesignator: { |
| 3162 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 3163 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 3164 | llvm::APSInt StartValue; |
| 3165 | llvm::APSInt EndValue; |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 3166 | bool StartDependent = StartIndex->isTypeDependent() || |
| 3167 | StartIndex->isValueDependent(); |
| 3168 | bool EndDependent = EndIndex->isTypeDependent() || |
| 3169 | EndIndex->isValueDependent(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3170 | if (!StartDependent) |
| 3171 | StartIndex = |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3172 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3173 | if (!EndDependent) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3174 | EndIndex = CheckArrayDesignatorExpr(*this, EndIndex, EndValue).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3175 | |
| 3176 | if (!StartIndex || !EndIndex) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3177 | Invalid = true; |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 3178 | else { |
| 3179 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 3180 | if (StartDependent || EndDependent) { |
| 3181 | // Nothing to compute. |
| 3182 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3183 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 3184 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3185 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 3186 | |
Douglas Gregor | 0f9d400 | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 3187 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 3188 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3189 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 3190 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 3191 | Invalid = true; |
| 3192 | } else { |
| 3193 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3194 | D.getLBracketLoc(), |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 3195 | D.getEllipsisLoc(), |
| 3196 | D.getRBracketLoc())); |
| 3197 | InitExpressions.push_back(StartIndex); |
| 3198 | InitExpressions.push_back(EndIndex); |
| 3199 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3200 | } |
| 3201 | break; |
| 3202 | } |
| 3203 | } |
| 3204 | } |
| 3205 | |
| 3206 | if (Invalid || Init.isInvalid()) |
| 3207 | return ExprError(); |
| 3208 | |
| 3209 | // Clear out the expressions within the designation. |
| 3210 | Desig.ClearExprs(*this); |
| 3211 | |
Richard Smith | ff9bf92 | 2019-08-31 01:00:37 +0000 | [diff] [blame] | 3212 | return DesignatedInitExpr::Create(Context, Designators, InitExpressions, |
| 3213 | EqualOrColonLoc, GNUSyntax, |
| 3214 | Init.getAs<Expr>()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3215 | } |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 3216 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3217 | //===----------------------------------------------------------------------===// |
| 3218 | // Initialization entity |
| 3219 | //===----------------------------------------------------------------------===// |
| 3220 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3221 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 3222 | const InitializedEntity &Parent) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3223 | : Parent(&Parent), Index(Index) |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 3224 | { |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3225 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 3226 | Kind = EK_ArrayElement; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3227 | Type = AT->getElementType(); |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3228 | } else if (const VectorType *VT = Parent.getType()->getAs<VectorType>()) { |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3229 | Kind = EK_VectorElement; |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3230 | Type = VT->getElementType(); |
| 3231 | } else { |
| 3232 | const ComplexType *CT = Parent.getType()->getAs<ComplexType>(); |
| 3233 | assert(CT && "Unexpected type"); |
| 3234 | Kind = EK_ComplexElement; |
| 3235 | Type = CT->getElementType(); |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3236 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3237 | } |
| 3238 | |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 3239 | InitializedEntity |
| 3240 | InitializedEntity::InitializeBase(ASTContext &Context, |
| 3241 | const CXXBaseSpecifier *Base, |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 3242 | bool IsInheritedVirtualBase, |
| 3243 | const InitializedEntity *Parent) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3244 | InitializedEntity Result; |
| 3245 | Result.Kind = EK_Base; |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 3246 | Result.Parent = Parent; |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 3247 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 3248 | if (IsInheritedVirtualBase) |
| 3249 | Result.Base |= 0x01; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3250 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3251 | Result.Type = Base->getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3252 | return Result; |
| 3253 | } |
| 3254 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3255 | DeclarationName InitializedEntity::getName() const { |
| 3256 | switch (getKind()) { |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3257 | case EK_Parameter: |
| 3258 | case EK_Parameter_CF_Audited: { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3259 | ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 3260 | return (D ? D->getDeclName() : DeclarationName()); |
| 3261 | } |
Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3262 | |
| 3263 | case EK_Variable: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3264 | case EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3265 | case EK_Binding: |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3266 | return Variable.VariableOrMember->getDeclName(); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3267 | |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 3268 | case EK_LambdaCapture: |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 3269 | return DeclarationName(Capture.VarID); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3270 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3271 | case EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 3272 | case EK_StmtExprResult: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3273 | case EK_Exception: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3274 | case EK_New: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3275 | case EK_Temporary: |
| 3276 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3277 | case EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3278 | case EK_ArrayElement: |
| 3279 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3280 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3281 | case EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3282 | case EK_LambdaToBlockConversionBlockElement: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 3283 | case EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 3284 | case EK_RelatedResult: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3285 | return DeclarationName(); |
| 3286 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3287 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3288 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3289 | } |
| 3290 | |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3291 | ValueDecl *InitializedEntity::getDecl() const { |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3292 | switch (getKind()) { |
| 3293 | case EK_Variable: |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3294 | case EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3295 | case EK_Binding: |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3296 | return Variable.VariableOrMember; |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3297 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3298 | case EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3299 | case EK_Parameter_CF_Audited: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3300 | return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 3301 | |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3302 | case EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 3303 | case EK_StmtExprResult: |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3304 | case EK_Exception: |
| 3305 | case EK_New: |
| 3306 | case EK_Temporary: |
| 3307 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3308 | case EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3309 | case EK_ArrayElement: |
| 3310 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3311 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3312 | case EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3313 | case EK_LambdaToBlockConversionBlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 3314 | case EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 3315 | case EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 3316 | case EK_RelatedResult: |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3317 | return nullptr; |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3318 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3319 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3320 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3321 | } |
| 3322 | |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3323 | bool InitializedEntity::allowsNRVO() const { |
| 3324 | switch (getKind()) { |
| 3325 | case EK_Result: |
| 3326 | case EK_Exception: |
| 3327 | return LocAndNRVO.NRVO; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3328 | |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 3329 | case EK_StmtExprResult: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3330 | case EK_Variable: |
| 3331 | case EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3332 | case EK_Parameter_CF_Audited: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3333 | case EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3334 | case EK_Binding: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3335 | case EK_New: |
| 3336 | case EK_Temporary: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 3337 | case EK_CompoundLiteralInit: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3338 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3339 | case EK_Delegating: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3340 | case EK_ArrayElement: |
| 3341 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3342 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3343 | case EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3344 | case EK_LambdaToBlockConversionBlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 3345 | case EK_LambdaCapture: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 3346 | case EK_RelatedResult: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3347 | break; |
| 3348 | } |
| 3349 | |
| 3350 | return false; |
| 3351 | } |
| 3352 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3353 | unsigned InitializedEntity::dumpImpl(raw_ostream &OS) const { |
Richard Smith | e3b28bc | 2013-06-12 21:51:50 +0000 | [diff] [blame] | 3354 | assert(getParent() != this); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3355 | unsigned Depth = getParent() ? getParent()->dumpImpl(OS) : 0; |
| 3356 | for (unsigned I = 0; I != Depth; ++I) |
| 3357 | OS << "`-"; |
| 3358 | |
| 3359 | switch (getKind()) { |
| 3360 | case EK_Variable: OS << "Variable"; break; |
| 3361 | case EK_Parameter: OS << "Parameter"; break; |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3362 | case EK_Parameter_CF_Audited: OS << "CF audited function Parameter"; |
| 3363 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3364 | case EK_Result: OS << "Result"; break; |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 3365 | case EK_StmtExprResult: OS << "StmtExprResult"; break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3366 | case EK_Exception: OS << "Exception"; break; |
| 3367 | case EK_Member: OS << "Member"; break; |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3368 | case EK_Binding: OS << "Binding"; break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3369 | case EK_New: OS << "New"; break; |
| 3370 | case EK_Temporary: OS << "Temporary"; break; |
| 3371 | case EK_CompoundLiteralInit: OS << "CompoundLiteral";break; |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 3372 | case EK_RelatedResult: OS << "RelatedResult"; break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3373 | case EK_Base: OS << "Base"; break; |
| 3374 | case EK_Delegating: OS << "Delegating"; break; |
| 3375 | case EK_ArrayElement: OS << "ArrayElement " << Index; break; |
| 3376 | case EK_VectorElement: OS << "VectorElement " << Index; break; |
| 3377 | case EK_ComplexElement: OS << "ComplexElement " << Index; break; |
| 3378 | case EK_BlockElement: OS << "Block"; break; |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3379 | case EK_LambdaToBlockConversionBlockElement: |
| 3380 | OS << "Block (lambda)"; |
| 3381 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3382 | case EK_LambdaCapture: |
| 3383 | OS << "LambdaCapture "; |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 3384 | OS << DeclarationName(Capture.VarID); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3385 | break; |
| 3386 | } |
| 3387 | |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3388 | if (auto *D = getDecl()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3389 | OS << " "; |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3390 | D->printQualifiedName(OS); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3391 | } |
| 3392 | |
| 3393 | OS << " '" << getType().getAsString() << "'\n"; |
| 3394 | |
| 3395 | return Depth + 1; |
| 3396 | } |
| 3397 | |
Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 3398 | LLVM_DUMP_METHOD void InitializedEntity::dump() const { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3399 | dumpImpl(llvm::errs()); |
| 3400 | } |
| 3401 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3402 | //===----------------------------------------------------------------------===// |
| 3403 | // Initialization sequence |
| 3404 | //===----------------------------------------------------------------------===// |
| 3405 | |
| 3406 | void InitializationSequence::Step::Destroy() { |
| 3407 | switch (Kind) { |
| 3408 | case SK_ResolveAddressOfOverloadedFunction: |
| 3409 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3410 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3411 | case SK_CastDerivedToBaseLValue: |
| 3412 | case SK_BindReference: |
| 3413 | case SK_BindReferenceToTemporary: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 3414 | case SK_FinalCopy: |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3415 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3416 | case SK_UserConversion: |
| 3417 | case SK_QualificationConversionRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3418 | case SK_QualificationConversionXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3419 | case SK_QualificationConversionLValue: |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 3420 | case SK_AtomicConversion: |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3421 | case SK_ListInitialization: |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3422 | case SK_UnwrapInitList: |
| 3423 | case SK_RewrapInitList: |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3424 | case SK_ConstructorInitialization: |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 3425 | case SK_ConstructorInitializationFromList: |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3426 | case SK_ZeroInitialization: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3427 | case SK_CAssignment: |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3428 | case SK_StringInit: |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3429 | case SK_ObjCObjectConversion: |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3430 | case SK_ArrayLoopIndex: |
| 3431 | case SK_ArrayLoopInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3432 | case SK_ArrayInit: |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 3433 | case SK_GNUArrayInit: |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 3434 | case SK_ParenthesizedArrayInit: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3435 | case SK_PassByIndirectCopyRestore: |
| 3436 | case SK_PassByIndirectRestore: |
| 3437 | case SK_ProduceObjCObject: |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 3438 | case SK_StdInitializerList: |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3439 | case SK_StdInitializerListConstructorCall: |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 3440 | case SK_OCLSamplerInit: |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 3441 | case SK_OCLZeroOpaqueType: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3442 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3443 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3444 | case SK_ConversionSequence: |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 3445 | case SK_ConversionSequenceNoNarrowing: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3446 | delete ICS; |
| 3447 | } |
| 3448 | } |
| 3449 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3450 | bool InitializationSequence::isDirectReferenceBinding() const { |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 3451 | // There can be some lvalue adjustments after the SK_BindReference step. |
| 3452 | for (auto I = Steps.rbegin(); I != Steps.rend(); ++I) { |
| 3453 | if (I->Kind == SK_BindReference) |
| 3454 | return true; |
| 3455 | if (I->Kind == SK_BindReferenceToTemporary) |
| 3456 | return false; |
| 3457 | } |
| 3458 | return false; |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3459 | } |
| 3460 | |
| 3461 | bool InitializationSequence::isAmbiguous() const { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 3462 | if (!Failed()) |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3463 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3464 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3465 | switch (getFailureKind()) { |
| 3466 | case FK_TooManyInitsForReference: |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 3467 | case FK_ParenthesizedListInitForReference: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3468 | case FK_ArrayNeedsInitList: |
| 3469 | case FK_ArrayNeedsInitListOrStringLiteral: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 3470 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 3471 | case FK_NarrowStringIntoWideCharArray: |
| 3472 | case FK_WideStringIntoCharArray: |
| 3473 | case FK_IncompatWideStringIntoWideChar: |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 3474 | case FK_PlainStringIntoUTF8Char: |
| 3475 | case FK_UTF8StringIntoPlainChar: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3476 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 3477 | case FK_NonConstLValueReferenceBindingToTemporary: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 3478 | case FK_NonConstLValueReferenceBindingToBitfield: |
| 3479 | case FK_NonConstLValueReferenceBindingToVectorElement: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3480 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 3481 | case FK_RValueReferenceBindingToLValue: |
Anastasia Stulova | 5145b1e | 2019-06-05 14:03:34 +0000 | [diff] [blame] | 3482 | case FK_ReferenceAddrspaceMismatchTemporary: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3483 | case FK_ReferenceInitDropsQualifiers: |
| 3484 | case FK_ReferenceInitFailed: |
| 3485 | case FK_ConversionFailed: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3486 | case FK_ConversionFromPropertyFailed: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3487 | case FK_TooManyInitsForScalar: |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 3488 | case FK_ParenthesizedListInitForScalar: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3489 | case FK_ReferenceBindingToInitList: |
| 3490 | case FK_InitListBadDestinationType: |
| 3491 | case FK_DefaultInitOfConst: |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 3492 | case FK_Incomplete: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3493 | case FK_ArrayTypeMismatch: |
| 3494 | case FK_NonConstantArrayInit: |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 3495 | case FK_ListInitializationFailed: |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 3496 | case FK_VariableLengthArrayHasInitializer: |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3497 | case FK_PlaceholderType: |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3498 | case FK_ExplicitConstructor: |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 3499 | case FK_AddressOfUnaddressableFunction: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3500 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3501 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3502 | case FK_ReferenceInitOverloadFailed: |
| 3503 | case FK_UserConversionOverloadFailed: |
| 3504 | case FK_ConstructorOverloadFailed: |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3505 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3506 | return FailedOverloadResult == OR_Ambiguous; |
| 3507 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3508 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3509 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3510 | } |
| 3511 | |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 3512 | bool InitializationSequence::isConstructorInitialization() const { |
| 3513 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 3514 | } |
| 3515 | |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3516 | void |
| 3517 | InitializationSequence |
| 3518 | ::AddAddressOverloadResolutionStep(FunctionDecl *Function, |
| 3519 | DeclAccessPair Found, |
| 3520 | bool HadMultipleCandidates) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3521 | Step S; |
| 3522 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 3523 | S.Type = Function->getType(); |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3524 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3525 | S.Function.Function = Function; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3526 | S.Function.FoundDecl = Found; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3527 | Steps.push_back(S); |
| 3528 | } |
| 3529 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3530 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3531 | ExprValueKind VK) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3532 | Step S; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3533 | switch (VK) { |
| 3534 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 3535 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 3536 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3537 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3538 | S.Type = BaseType; |
| 3539 | Steps.push_back(S); |
| 3540 | } |
| 3541 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3542 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3543 | bool BindingTemporary) { |
| 3544 | Step S; |
| 3545 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 3546 | S.Type = T; |
| 3547 | Steps.push_back(S); |
| 3548 | } |
| 3549 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 3550 | void InitializationSequence::AddFinalCopy(QualType T) { |
| 3551 | Step S; |
| 3552 | S.Kind = SK_FinalCopy; |
| 3553 | S.Type = T; |
| 3554 | Steps.push_back(S); |
| 3555 | } |
| 3556 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3557 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 3558 | Step S; |
| 3559 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 3560 | S.Type = T; |
| 3561 | Steps.push_back(S); |
| 3562 | } |
| 3563 | |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3564 | void |
| 3565 | InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
| 3566 | DeclAccessPair FoundDecl, |
| 3567 | QualType T, |
| 3568 | bool HadMultipleCandidates) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3569 | Step S; |
| 3570 | S.Kind = SK_UserConversion; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3571 | S.Type = T; |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3572 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3573 | S.Function.Function = Function; |
| 3574 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3575 | Steps.push_back(S); |
| 3576 | } |
| 3577 | |
| 3578 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3579 | ExprValueKind VK) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3580 | Step S; |
John McCall | 7a1da89 | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 3581 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3582 | switch (VK) { |
| 3583 | case VK_RValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3584 | S.Kind = SK_QualificationConversionRValue; |
| 3585 | break; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3586 | case VK_XValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3587 | S.Kind = SK_QualificationConversionXValue; |
| 3588 | break; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3589 | case VK_LValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3590 | S.Kind = SK_QualificationConversionLValue; |
| 3591 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3592 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3593 | S.Type = Ty; |
| 3594 | Steps.push_back(S); |
| 3595 | } |
| 3596 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 3597 | void InitializationSequence::AddAtomicConversionStep(QualType Ty) { |
| 3598 | Step S; |
| 3599 | S.Kind = SK_AtomicConversion; |
| 3600 | S.Type = Ty; |
| 3601 | Steps.push_back(S); |
| 3602 | } |
| 3603 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3604 | void InitializationSequence::AddConversionSequenceStep( |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 3605 | const ImplicitConversionSequence &ICS, QualType T, |
| 3606 | bool TopLevelOfInitList) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3607 | Step S; |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 3608 | S.Kind = TopLevelOfInitList ? SK_ConversionSequenceNoNarrowing |
| 3609 | : SK_ConversionSequence; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3610 | S.Type = T; |
| 3611 | S.ICS = new ImplicitConversionSequence(ICS); |
| 3612 | Steps.push_back(S); |
| 3613 | } |
| 3614 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3615 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 3616 | Step S; |
| 3617 | S.Kind = SK_ListInitialization; |
| 3618 | S.Type = T; |
| 3619 | Steps.push_back(S); |
| 3620 | } |
| 3621 | |
Richard Smith | 55c2888 | 2016-05-12 23:45:49 +0000 | [diff] [blame] | 3622 | void InitializationSequence::AddConstructorInitializationStep( |
| 3623 | DeclAccessPair FoundDecl, CXXConstructorDecl *Constructor, QualType T, |
| 3624 | bool HadMultipleCandidates, bool FromInitList, bool AsInitList) { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3625 | Step S; |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3626 | S.Kind = FromInitList ? AsInitList ? SK_StdInitializerListConstructorCall |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 3627 | : SK_ConstructorInitializationFromList |
| 3628 | : SK_ConstructorInitialization; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3629 | S.Type = T; |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3630 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3631 | S.Function.Function = Constructor; |
Richard Smith | 55c2888 | 2016-05-12 23:45:49 +0000 | [diff] [blame] | 3632 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3633 | Steps.push_back(S); |
| 3634 | } |
| 3635 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3636 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 3637 | Step S; |
| 3638 | S.Kind = SK_ZeroInitialization; |
| 3639 | S.Type = T; |
| 3640 | Steps.push_back(S); |
| 3641 | } |
| 3642 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3643 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 3644 | Step S; |
| 3645 | S.Kind = SK_CAssignment; |
| 3646 | S.Type = T; |
| 3647 | Steps.push_back(S); |
| 3648 | } |
| 3649 | |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3650 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 3651 | Step S; |
| 3652 | S.Kind = SK_StringInit; |
| 3653 | S.Type = T; |
| 3654 | Steps.push_back(S); |
| 3655 | } |
| 3656 | |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3657 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 3658 | Step S; |
| 3659 | S.Kind = SK_ObjCObjectConversion; |
| 3660 | S.Type = T; |
| 3661 | Steps.push_back(S); |
| 3662 | } |
| 3663 | |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 3664 | void InitializationSequence::AddArrayInitStep(QualType T, bool IsGNUExtension) { |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3665 | Step S; |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 3666 | S.Kind = IsGNUExtension ? SK_GNUArrayInit : SK_ArrayInit; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3667 | S.Type = T; |
| 3668 | Steps.push_back(S); |
| 3669 | } |
| 3670 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3671 | void InitializationSequence::AddArrayInitLoopStep(QualType T, QualType EltT) { |
| 3672 | Step S; |
| 3673 | S.Kind = SK_ArrayLoopIndex; |
| 3674 | S.Type = EltT; |
| 3675 | Steps.insert(Steps.begin(), S); |
| 3676 | |
| 3677 | S.Kind = SK_ArrayLoopInit; |
| 3678 | S.Type = T; |
| 3679 | Steps.push_back(S); |
| 3680 | } |
| 3681 | |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 3682 | void InitializationSequence::AddParenthesizedArrayInitStep(QualType T) { |
| 3683 | Step S; |
| 3684 | S.Kind = SK_ParenthesizedArrayInit; |
| 3685 | S.Type = T; |
| 3686 | Steps.push_back(S); |
| 3687 | } |
| 3688 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3689 | void InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type, |
| 3690 | bool shouldCopy) { |
| 3691 | Step s; |
| 3692 | s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore |
| 3693 | : SK_PassByIndirectRestore); |
| 3694 | s.Type = type; |
| 3695 | Steps.push_back(s); |
| 3696 | } |
| 3697 | |
| 3698 | void InitializationSequence::AddProduceObjCObjectStep(QualType T) { |
| 3699 | Step S; |
| 3700 | S.Kind = SK_ProduceObjCObject; |
| 3701 | S.Type = T; |
| 3702 | Steps.push_back(S); |
| 3703 | } |
| 3704 | |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 3705 | void InitializationSequence::AddStdInitializerListConstructionStep(QualType T) { |
| 3706 | Step S; |
| 3707 | S.Kind = SK_StdInitializerList; |
| 3708 | S.Type = T; |
| 3709 | Steps.push_back(S); |
| 3710 | } |
| 3711 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 3712 | void InitializationSequence::AddOCLSamplerInitStep(QualType T) { |
| 3713 | Step S; |
| 3714 | S.Kind = SK_OCLSamplerInit; |
| 3715 | S.Type = T; |
| 3716 | Steps.push_back(S); |
| 3717 | } |
| 3718 | |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 3719 | void InitializationSequence::AddOCLZeroOpaqueTypeStep(QualType T) { |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 3720 | Step S; |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 3721 | S.Kind = SK_OCLZeroOpaqueType; |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 3722 | S.Type = T; |
| 3723 | Steps.push_back(S); |
| 3724 | } |
| 3725 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3726 | void InitializationSequence::RewrapReferenceInitList(QualType T, |
| 3727 | InitListExpr *Syntactic) { |
| 3728 | assert(Syntactic->getNumInits() == 1 && |
| 3729 | "Can only rewrap trivial init lists."); |
| 3730 | Step S; |
| 3731 | S.Kind = SK_UnwrapInitList; |
| 3732 | S.Type = Syntactic->getInit(0)->getType(); |
| 3733 | Steps.insert(Steps.begin(), S); |
| 3734 | |
| 3735 | S.Kind = SK_RewrapInitList; |
| 3736 | S.Type = T; |
| 3737 | S.WrappingSyntacticList = Syntactic; |
| 3738 | Steps.push_back(S); |
| 3739 | } |
| 3740 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3741 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3742 | OverloadingResult Result) { |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 3743 | setSequenceKind(FailedSequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3744 | this->Failure = Failure; |
| 3745 | this->FailedOverloadResult = Result; |
| 3746 | } |
| 3747 | |
| 3748 | //===----------------------------------------------------------------------===// |
| 3749 | // Attempt initialization |
| 3750 | //===----------------------------------------------------------------------===// |
| 3751 | |
Nico Weber | 337d5aa | 2015-04-17 08:32:38 +0000 | [diff] [blame] | 3752 | /// Tries to add a zero initializer. Returns true if that worked. |
| 3753 | static bool |
| 3754 | maybeRecoverWithZeroInitialization(Sema &S, InitializationSequence &Sequence, |
| 3755 | const InitializedEntity &Entity) { |
| 3756 | if (Entity.getKind() != InitializedEntity::EK_Variable) |
| 3757 | return false; |
| 3758 | |
| 3759 | VarDecl *VD = cast<VarDecl>(Entity.getDecl()); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 3760 | if (VD->getInit() || VD->getEndLoc().isMacroID()) |
Nico Weber | 337d5aa | 2015-04-17 08:32:38 +0000 | [diff] [blame] | 3761 | return false; |
| 3762 | |
| 3763 | QualType VariableTy = VD->getType().getCanonicalType(); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 3764 | SourceLocation Loc = S.getLocForEndOfToken(VD->getEndLoc()); |
Nico Weber | 337d5aa | 2015-04-17 08:32:38 +0000 | [diff] [blame] | 3765 | std::string Init = S.getFixItZeroInitializerForType(VariableTy, Loc); |
| 3766 | if (!Init.empty()) { |
| 3767 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3768 | Sequence.SetZeroInitializationFixit(Init, Loc); |
| 3769 | return true; |
| 3770 | } |
| 3771 | return false; |
| 3772 | } |
| 3773 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3774 | static void MaybeProduceObjCObject(Sema &S, |
| 3775 | InitializationSequence &Sequence, |
| 3776 | const InitializedEntity &Entity) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3777 | if (!S.getLangOpts().ObjCAutoRefCount) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3778 | |
| 3779 | /// When initializing a parameter, produce the value if it's marked |
| 3780 | /// __attribute__((ns_consumed)). |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3781 | if (Entity.isParameterKind()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3782 | if (!Entity.isParameterConsumed()) |
| 3783 | return; |
| 3784 | |
| 3785 | assert(Entity.getType()->isObjCRetainableType() && |
| 3786 | "consuming an object of unretainable type?"); |
| 3787 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 3788 | |
| 3789 | /// When initializing a return value, if the return type is a |
| 3790 | /// retainable type, then returns need to immediately retain the |
| 3791 | /// object. If an autorelease is required, it will be done at the |
| 3792 | /// last instant. |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 3793 | } else if (Entity.getKind() == InitializedEntity::EK_Result || |
| 3794 | Entity.getKind() == InitializedEntity::EK_StmtExprResult) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3795 | if (!Entity.getType()->isObjCRetainableType()) |
| 3796 | return; |
| 3797 | |
| 3798 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 3799 | } |
| 3800 | } |
| 3801 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3802 | static void TryListInitialization(Sema &S, |
| 3803 | const InitializedEntity &Entity, |
| 3804 | const InitializationKind &Kind, |
| 3805 | InitListExpr *InitList, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 3806 | InitializationSequence &Sequence, |
| 3807 | bool TreatUnavailableAsInvalid); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3808 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3809 | /// When initializing from init list via constructor, handle |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3810 | /// initialization of an object of type std::initializer_list<T>. |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3811 | /// |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3812 | /// \return true if we have handled initialization of an object of type |
| 3813 | /// std::initializer_list<T>, false otherwise. |
| 3814 | static bool TryInitializerListConstruction(Sema &S, |
| 3815 | InitListExpr *List, |
| 3816 | QualType DestType, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 3817 | InitializationSequence &Sequence, |
| 3818 | bool TreatUnavailableAsInvalid) { |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3819 | QualType E; |
| 3820 | if (!S.isStdInitializerList(DestType, &E)) |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3821 | return false; |
| 3822 | |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3823 | if (!S.isCompleteType(List->getExprLoc(), E)) { |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3824 | Sequence.setIncompleteTypeFailure(E); |
| 3825 | return true; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3826 | } |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3827 | |
| 3828 | // Try initializing a temporary array from the init list. |
| 3829 | QualType ArrayType = S.Context.getConstantArrayType( |
| 3830 | E.withConst(), llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 3831 | List->getNumInits()), |
| 3832 | clang::ArrayType::Normal, 0); |
| 3833 | InitializedEntity HiddenArray = |
| 3834 | InitializedEntity::InitializeTemporary(ArrayType); |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 3835 | InitializationKind Kind = InitializationKind::CreateDirectList( |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 3836 | List->getExprLoc(), List->getBeginLoc(), List->getEndLoc()); |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 3837 | TryListInitialization(S, HiddenArray, Kind, List, Sequence, |
| 3838 | TreatUnavailableAsInvalid); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3839 | if (Sequence) |
| 3840 | Sequence.AddStdInitializerListConstructionStep(DestType); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3841 | return true; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3842 | } |
| 3843 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3844 | /// Determine if the constructor has the signature of a copy or move |
| 3845 | /// constructor for the type T of the class in which it was found. That is, |
| 3846 | /// determine if its first parameter is of type T or reference to (possibly |
| 3847 | /// cv-qualified) T. |
| 3848 | static bool hasCopyOrMoveCtorParam(ASTContext &Ctx, |
| 3849 | const ConstructorInfo &Info) { |
| 3850 | if (Info.Constructor->getNumParams() == 0) |
| 3851 | return false; |
| 3852 | |
| 3853 | QualType ParmT = |
| 3854 | Info.Constructor->getParamDecl(0)->getType().getNonReferenceType(); |
| 3855 | QualType ClassT = |
| 3856 | Ctx.getRecordType(cast<CXXRecordDecl>(Info.FoundDecl->getDeclContext())); |
| 3857 | |
| 3858 | return Ctx.hasSameUnqualifiedType(ParmT, ClassT); |
| 3859 | } |
| 3860 | |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3861 | static OverloadingResult |
| 3862 | ResolveConstructorOverload(Sema &S, SourceLocation DeclLoc, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3863 | MultiExprArg Args, |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3864 | OverloadCandidateSet &CandidateSet, |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3865 | QualType DestType, |
Richard Smith | 40c7806 | 2015-02-21 02:31:57 +0000 | [diff] [blame] | 3866 | DeclContext::lookup_result Ctors, |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3867 | OverloadCandidateSet::iterator &Best, |
| 3868 | bool CopyInitializing, bool AllowExplicit, |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3869 | bool OnlyListConstructors, bool IsListInit, |
| 3870 | bool SecondStepOfCopyInit = false) { |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3871 | CandidateSet.clear(OverloadCandidateSet::CSK_InitByConstructor); |
Anastasia Stulova | c25ea86 | 2019-06-20 16:23:28 +0000 | [diff] [blame] | 3872 | CandidateSet.setDestAS(DestType.getQualifiers().getAddressSpace()); |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3873 | |
Richard Smith | 40c7806 | 2015-02-21 02:31:57 +0000 | [diff] [blame] | 3874 | for (NamedDecl *D : Ctors) { |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3875 | auto Info = getConstructorInfo(D); |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3876 | if (!Info.Constructor || Info.Constructor->isInvalidDecl()) |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3877 | continue; |
| 3878 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3879 | if (!AllowExplicit && Info.Constructor->isExplicit()) |
| 3880 | continue; |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3881 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3882 | if (OnlyListConstructors && !S.isInitListConstructor(Info.Constructor)) |
| 3883 | continue; |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3884 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3885 | // C++11 [over.best.ics]p4: |
| 3886 | // ... and the constructor or user-defined conversion function is a |
| 3887 | // candidate by |
| 3888 | // - 13.3.1.3, when the argument is the temporary in the second step |
| 3889 | // of a class copy-initialization, or |
| 3890 | // - 13.3.1.4, 13.3.1.5, or 13.3.1.6 (in all cases), [not handled here] |
| 3891 | // - the second phase of 13.3.1.7 when the initializer list has exactly |
| 3892 | // one element that is itself an initializer list, and the target is |
| 3893 | // the first parameter of a constructor of class X, and the conversion |
| 3894 | // is to X or reference to (possibly cv-qualified X), |
| 3895 | // user-defined conversion sequences are not considered. |
| 3896 | bool SuppressUserConversions = |
| 3897 | SecondStepOfCopyInit || |
| 3898 | (IsListInit && Args.size() == 1 && isa<InitListExpr>(Args[0]) && |
| 3899 | hasCopyOrMoveCtorParam(S.Context, Info)); |
| 3900 | |
| 3901 | if (Info.ConstructorTmpl) |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 3902 | S.AddTemplateOverloadCandidate( |
| 3903 | Info.ConstructorTmpl, Info.FoundDecl, |
| 3904 | /*ExplicitArgs*/ nullptr, Args, CandidateSet, SuppressUserConversions, |
| 3905 | /*PartialOverloading=*/false, AllowExplicit); |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3906 | else { |
| 3907 | // C++ [over.match.copy]p1: |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3908 | // - When initializing a temporary to be bound to the first parameter |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3909 | // of a constructor [for type T] that takes a reference to possibly |
| 3910 | // cv-qualified T as its first argument, called with a single |
| 3911 | // argument in the context of direct-initialization, explicit |
| 3912 | // conversion functions are also considered. |
| 3913 | // FIXME: What if a constructor template instantiates to such a signature? |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3914 | bool AllowExplicitConv = AllowExplicit && !CopyInitializing && |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3915 | Args.size() == 1 && |
| 3916 | hasCopyOrMoveCtorParam(S.Context, Info); |
| 3917 | S.AddOverloadCandidate(Info.Constructor, Info.FoundDecl, Args, |
| 3918 | CandidateSet, SuppressUserConversions, |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 3919 | /*PartialOverloading=*/false, AllowExplicit, |
| 3920 | AllowExplicitConv); |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3921 | } |
| 3922 | } |
| 3923 | |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3924 | // FIXME: Work around a bug in C++17 guaranteed copy elision. |
| 3925 | // |
| 3926 | // When initializing an object of class type T by constructor |
| 3927 | // ([over.match.ctor]) or by list-initialization ([over.match.list]) |
| 3928 | // from a single expression of class type U, conversion functions of |
| 3929 | // U that convert to the non-reference type cv T are candidates. |
| 3930 | // Explicit conversion functions are only candidates during |
| 3931 | // direct-initialization. |
| 3932 | // |
| 3933 | // Note: SecondStepOfCopyInit is only ever true in this case when |
| 3934 | // evaluating whether to produce a C++98 compatibility warning. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 3935 | if (S.getLangOpts().CPlusPlus17 && Args.size() == 1 && |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3936 | !SecondStepOfCopyInit) { |
| 3937 | Expr *Initializer = Args[0]; |
| 3938 | auto *SourceRD = Initializer->getType()->getAsCXXRecordDecl(); |
| 3939 | if (SourceRD && S.isCompleteType(DeclLoc, Initializer->getType())) { |
| 3940 | const auto &Conversions = SourceRD->getVisibleConversionFunctions(); |
| 3941 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
| 3942 | NamedDecl *D = *I; |
| 3943 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3944 | D = D->getUnderlyingDecl(); |
| 3945 | |
| 3946 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3947 | CXXConversionDecl *Conv; |
| 3948 | if (ConvTemplate) |
| 3949 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3950 | else |
| 3951 | Conv = cast<CXXConversionDecl>(D); |
| 3952 | |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 3953 | if (AllowExplicit || !Conv->isExplicit()) { |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3954 | if (ConvTemplate) |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 3955 | S.AddTemplateConversionCandidate( |
| 3956 | ConvTemplate, I.getPair(), ActingDC, Initializer, DestType, |
| 3957 | CandidateSet, AllowExplicit, AllowExplicit, |
| 3958 | /*AllowResultConversion*/ false); |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3959 | else |
| 3960 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Initializer, |
| 3961 | DestType, CandidateSet, AllowExplicit, |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 3962 | AllowExplicit, |
| 3963 | /*AllowResultConversion*/ false); |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3964 | } |
| 3965 | } |
| 3966 | } |
| 3967 | } |
| 3968 | |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3969 | // Perform overload resolution and return the result. |
| 3970 | return CandidateSet.BestViableFunction(S, DeclLoc, Best); |
| 3971 | } |
| 3972 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3973 | /// Attempt initialization by constructor (C++ [dcl.init]), which |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3974 | /// enumerates the constructors of the initialized entity and performs overload |
| 3975 | /// resolution to select the best. |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3976 | /// \param DestType The destination class type. |
| 3977 | /// \param DestArrayType The destination type, which is either DestType or |
| 3978 | /// a (possibly multidimensional) array of DestType. |
NAKAMURA Takumi | ffcc98a | 2015-02-05 23:12:13 +0000 | [diff] [blame] | 3979 | /// \param IsListInit Is this list-initialization? |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3980 | /// \param IsInitListCopy Is this non-list-initialization resulting from a |
| 3981 | /// list-initialization from {x} where x is the same |
| 3982 | /// type as the entity? |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3983 | static void TryConstructorInitialization(Sema &S, |
| 3984 | const InitializedEntity &Entity, |
| 3985 | const InitializationKind &Kind, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3986 | MultiExprArg Args, QualType DestType, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3987 | QualType DestArrayType, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3988 | InitializationSequence &Sequence, |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3989 | bool IsListInit = false, |
| 3990 | bool IsInitListCopy = false) { |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3991 | assert(((!IsListInit && !IsInitListCopy) || |
| 3992 | (Args.size() == 1 && isa<InitListExpr>(Args[0]))) && |
| 3993 | "IsListInit/IsInitListCopy must come with a single initializer list " |
| 3994 | "argument."); |
| 3995 | InitListExpr *ILE = |
| 3996 | (IsListInit || IsInitListCopy) ? cast<InitListExpr>(Args[0]) : nullptr; |
| 3997 | MultiExprArg UnwrappedArgs = |
| 3998 | ILE ? MultiExprArg(ILE->getInits(), ILE->getNumInits()) : Args; |
Sebastian Redl | 88e4d49 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3999 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4000 | // The type we're constructing needs to be complete. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4001 | if (!S.isCompleteType(Kind.getLocation(), DestType)) { |
Douglas Gregor | 85f3423 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 4002 | Sequence.setIncompleteTypeFailure(DestType); |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 4003 | return; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4004 | } |
| 4005 | |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 4006 | // C++17 [dcl.init]p17: |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 4007 | // - If the initializer expression is a prvalue and the cv-unqualified |
| 4008 | // version of the source type is the same class as the class of the |
| 4009 | // destination, the initializer expression is used to initialize the |
| 4010 | // destination object. |
| 4011 | // Per DR (no number yet), this does not apply when initializing a base |
| 4012 | // class or delegating to another constructor from a mem-initializer. |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 4013 | // ObjC++: Lambda captured by the block in the lambda to block conversion |
| 4014 | // should avoid copy elision. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 4015 | if (S.getLangOpts().CPlusPlus17 && |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 4016 | Entity.getKind() != InitializedEntity::EK_Base && |
| 4017 | Entity.getKind() != InitializedEntity::EK_Delegating && |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 4018 | Entity.getKind() != |
| 4019 | InitializedEntity::EK_LambdaToBlockConversionBlockElement && |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 4020 | UnwrappedArgs.size() == 1 && UnwrappedArgs[0]->isRValue() && |
| 4021 | S.Context.hasSameUnqualifiedType(UnwrappedArgs[0]->getType(), DestType)) { |
| 4022 | // Convert qualifications if necessary. |
Richard Smith | 16d3150 | 2016-12-21 01:31:56 +0000 | [diff] [blame] | 4023 | Sequence.AddQualificationConversionStep(DestType, VK_RValue); |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 4024 | if (ILE) |
| 4025 | Sequence.RewrapReferenceInitList(DestType, ILE); |
| 4026 | return; |
| 4027 | } |
| 4028 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4029 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
| 4030 | assert(DestRecordType && "Constructor initialization requires record type"); |
| 4031 | CXXRecordDecl *DestRecordDecl |
| 4032 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
| 4033 | |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 4034 | // Build the candidate set directly in the initialization sequence |
| 4035 | // structure, so that it will persist if we fail. |
| 4036 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 4037 | |
| 4038 | // Determine whether we are allowed to call explicit constructors or |
| 4039 | // explicit conversion operators. |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 4040 | bool AllowExplicit = Kind.AllowExplicit() || IsListInit; |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 4041 | bool CopyInitialization = Kind.getKind() == InitializationKind::IK_Copy; |
Sebastian Redl | 88e4d49 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 4042 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4043 | // - Otherwise, if T is a class type, constructors are considered. The |
| 4044 | // applicable constructors are enumerated, and the best one is chosen |
| 4045 | // through overload resolution. |
Richard Smith | 40c7806 | 2015-02-21 02:31:57 +0000 | [diff] [blame] | 4046 | DeclContext::lookup_result Ctors = S.LookupConstructors(DestRecordDecl); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4047 | |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 4048 | OverloadingResult Result = OR_No_Viable_Function; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4049 | OverloadCandidateSet::iterator Best; |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 4050 | bool AsInitializerList = false; |
| 4051 | |
Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 4052 | // C++11 [over.match.list]p1, per DR1467: |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4053 | // When objects of non-aggregate type T are list-initialized, such that |
| 4054 | // 8.5.4 [dcl.init.list] specifies that overload resolution is performed |
| 4055 | // according to the rules in this section, overload resolution selects |
| 4056 | // the constructor in two phases: |
| 4057 | // |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 4058 | // - Initially, the candidate functions are the initializer-list |
| 4059 | // constructors of the class T and the argument list consists of the |
| 4060 | // initializer list as a single argument. |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 4061 | if (IsListInit) { |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 4062 | AsInitializerList = true; |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4063 | |
| 4064 | // If the initializer list has no elements and T has a default constructor, |
| 4065 | // the first phase is omitted. |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 4066 | if (!(UnwrappedArgs.empty() && DestRecordDecl->hasDefaultConstructor())) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4067 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 4068 | CandidateSet, DestType, Ctors, Best, |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4069 | CopyInitialization, AllowExplicit, |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 4070 | /*OnlyListConstructors=*/true, |
Larisse Voufo | bcf327a | 2015-02-10 02:20:14 +0000 | [diff] [blame] | 4071 | IsListInit); |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 4072 | } |
| 4073 | |
| 4074 | // C++11 [over.match.list]p1: |
| 4075 | // - If no viable initializer-list constructor is found, overload resolution |
| 4076 | // is performed again, where the candidate functions are all the |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4077 | // constructors of the class T and the argument list consists of the |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 4078 | // elements of the initializer list. |
| 4079 | if (Result == OR_No_Viable_Function) { |
| 4080 | AsInitializerList = false; |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 4081 | Result = ResolveConstructorOverload(S, Kind.getLocation(), UnwrappedArgs, |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 4082 | CandidateSet, DestType, Ctors, Best, |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 4083 | CopyInitialization, AllowExplicit, |
Larisse Voufo | bcf327a | 2015-02-10 02:20:14 +0000 | [diff] [blame] | 4084 | /*OnlyListConstructors=*/false, |
| 4085 | IsListInit); |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 4086 | } |
| 4087 | if (Result) { |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 4088 | Sequence.SetOverloadFailure(IsListInit ? |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 4089 | InitializationSequence::FK_ListConstructorOverloadFailed : |
| 4090 | InitializationSequence::FK_ConstructorOverloadFailed, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4091 | Result); |
| 4092 | return; |
| 4093 | } |
| 4094 | |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 4095 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 4096 | |
| 4097 | // In C++17, ResolveConstructorOverload can select a conversion function |
| 4098 | // instead of a constructor. |
| 4099 | if (auto *CD = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 4100 | // Add the user-defined conversion step that calls the conversion function. |
| 4101 | QualType ConvType = CD->getConversionType(); |
| 4102 | assert(S.Context.hasSameUnqualifiedType(ConvType, DestType) && |
| 4103 | "should not have selected this conversion function"); |
| 4104 | Sequence.AddUserConversionStep(CD, Best->FoundDecl, ConvType, |
| 4105 | HadMultipleCandidates); |
| 4106 | if (!S.Context.hasSameType(ConvType, DestType)) |
| 4107 | Sequence.AddQualificationConversionStep(DestType, VK_RValue); |
| 4108 | if (IsListInit) |
| 4109 | Sequence.RewrapReferenceInitList(Entity.getType(), ILE); |
| 4110 | return; |
| 4111 | } |
| 4112 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4113 | // C++11 [dcl.init]p6: |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4114 | // If a program calls for the default initialization of an object |
| 4115 | // of a const-qualified type T, T shall be a class type with a |
| 4116 | // user-provided default constructor. |
Nico Weber | 6a6376b | 2016-02-19 01:52:46 +0000 | [diff] [blame] | 4117 | // C++ core issue 253 proposal: |
| 4118 | // If the implicit default constructor initializes all subobjects, no |
| 4119 | // initializer should be required. |
| 4120 | // The 253 proposal is for example needed to process libstdc++ headers in 5.x. |
| 4121 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4122 | if (Kind.getKind() == InitializationKind::IK_Default && |
Nico Weber | 6a6376b | 2016-02-19 01:52:46 +0000 | [diff] [blame] | 4123 | Entity.getType().isConstQualified()) { |
| 4124 | if (!CtorDecl->getParent()->allowConstDefaultInit()) { |
| 4125 | if (!maybeRecoverWithZeroInitialization(S, Sequence, Entity)) |
| 4126 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 4127 | return; |
| 4128 | } |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4129 | } |
| 4130 | |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 4131 | // C++11 [over.match.list]p1: |
| 4132 | // In copy-list-initialization, if an explicit constructor is chosen, the |
| 4133 | // initializer is ill-formed. |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 4134 | if (IsListInit && !Kind.AllowExplicit() && CtorDecl->isExplicit()) { |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 4135 | Sequence.SetFailed(InitializationSequence::FK_ExplicitConstructor); |
| 4136 | return; |
| 4137 | } |
| 4138 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4139 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 4140 | // subsumed by the initialization. |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 4141 | Sequence.AddConstructorInitializationStep( |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 4142 | Best->FoundDecl, CtorDecl, DestArrayType, HadMultipleCandidates, |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 4143 | IsListInit | IsInitListCopy, AsInitializerList); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4144 | } |
| 4145 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4146 | static bool |
| 4147 | ResolveOverloadedFunctionForReferenceBinding(Sema &S, |
| 4148 | Expr *Initializer, |
| 4149 | QualType &SourceType, |
| 4150 | QualType &UnqualifiedSourceType, |
| 4151 | QualType UnqualifiedTargetType, |
| 4152 | InitializationSequence &Sequence) { |
| 4153 | if (S.Context.getCanonicalType(UnqualifiedSourceType) == |
| 4154 | S.Context.OverloadTy) { |
| 4155 | DeclAccessPair Found; |
| 4156 | bool HadMultipleCandidates = false; |
| 4157 | if (FunctionDecl *Fn |
| 4158 | = S.ResolveAddressOfOverloadedFunction(Initializer, |
| 4159 | UnqualifiedTargetType, |
| 4160 | false, Found, |
| 4161 | &HadMultipleCandidates)) { |
| 4162 | Sequence.AddAddressOverloadResolutionStep(Fn, Found, |
| 4163 | HadMultipleCandidates); |
| 4164 | SourceType = Fn->getType(); |
| 4165 | UnqualifiedSourceType = SourceType.getUnqualifiedType(); |
| 4166 | } else if (!UnqualifiedTargetType->isRecordType()) { |
| 4167 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 4168 | return true; |
| 4169 | } |
| 4170 | } |
| 4171 | return false; |
| 4172 | } |
| 4173 | |
| 4174 | static void TryReferenceInitializationCore(Sema &S, |
| 4175 | const InitializedEntity &Entity, |
| 4176 | const InitializationKind &Kind, |
| 4177 | Expr *Initializer, |
| 4178 | QualType cv1T1, QualType T1, |
| 4179 | Qualifiers T1Quals, |
| 4180 | QualType cv2T2, QualType T2, |
| 4181 | Qualifiers T2Quals, |
| 4182 | InitializationSequence &Sequence); |
| 4183 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4184 | static void TryValueInitialization(Sema &S, |
| 4185 | const InitializedEntity &Entity, |
| 4186 | const InitializationKind &Kind, |
| 4187 | InitializationSequence &Sequence, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4188 | InitListExpr *InitList = nullptr); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4189 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4190 | /// Attempt list initialization of a reference. |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4191 | static void TryReferenceListInitialization(Sema &S, |
| 4192 | const InitializedEntity &Entity, |
| 4193 | const InitializationKind &Kind, |
| 4194 | InitListExpr *InitList, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4195 | InitializationSequence &Sequence, |
| 4196 | bool TreatUnavailableAsInvalid) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4197 | // First, catch C++03 where this isn't possible. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4198 | if (!S.getLangOpts().CPlusPlus11) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4199 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 4200 | return; |
| 4201 | } |
David Majnemer | 9370dc2 | 2015-04-26 07:35:03 +0000 | [diff] [blame] | 4202 | // Can't reference initialize a compound literal. |
| 4203 | if (Entity.getKind() == InitializedEntity::EK_CompoundLiteralInit) { |
| 4204 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 4205 | return; |
| 4206 | } |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4207 | |
| 4208 | QualType DestType = Entity.getType(); |
| 4209 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 4210 | Qualifiers T1Quals; |
| 4211 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
| 4212 | |
| 4213 | // Reference initialization via an initializer list works thus: |
| 4214 | // If the initializer list consists of a single element that is |
| 4215 | // reference-related to the referenced type, bind directly to that element |
| 4216 | // (possibly creating temporaries). |
| 4217 | // Otherwise, initialize a temporary with the initializer list and |
| 4218 | // bind to that. |
| 4219 | if (InitList->getNumInits() == 1) { |
| 4220 | Expr *Initializer = InitList->getInit(0); |
| 4221 | QualType cv2T2 = Initializer->getType(); |
| 4222 | Qualifiers T2Quals; |
| 4223 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
| 4224 | |
| 4225 | // If this fails, creating a temporary wouldn't work either. |
| 4226 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 4227 | T1, Sequence)) |
| 4228 | return; |
| 4229 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4230 | SourceLocation DeclLoc = Initializer->getBeginLoc(); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4231 | bool dummy1, dummy2, dummy3; |
| 4232 | Sema::ReferenceCompareResult RefRelationship |
| 4233 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, dummy1, |
| 4234 | dummy2, dummy3); |
| 4235 | if (RefRelationship >= Sema::Ref_Related) { |
| 4236 | // Try to bind the reference here. |
| 4237 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 4238 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 4239 | if (Sequence) |
| 4240 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
| 4241 | return; |
| 4242 | } |
Richard Smith | 03d9393 | 2013-01-15 07:58:29 +0000 | [diff] [blame] | 4243 | |
| 4244 | // Update the initializer if we've resolved an overloaded function. |
| 4245 | if (Sequence.step_begin() != Sequence.step_end()) |
| 4246 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4247 | } |
| 4248 | |
| 4249 | // Not reference-related. Create a temporary and bind to that. |
| 4250 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 4251 | |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4252 | TryListInitialization(S, TempEntity, Kind, InitList, Sequence, |
| 4253 | TreatUnavailableAsInvalid); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4254 | if (Sequence) { |
| 4255 | if (DestType->isRValueReferenceType() || |
| 4256 | (T1Quals.hasConst() && !T1Quals.hasVolatile())) |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 4257 | Sequence.AddReferenceBindingStep(cv1T1, /*BindingTemporary=*/true); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4258 | else |
| 4259 | Sequence.SetFailed( |
| 4260 | InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
| 4261 | } |
| 4262 | } |
| 4263 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4264 | /// Attempt list initialization (C++0x [dcl.init.list]) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4265 | static void TryListInitialization(Sema &S, |
| 4266 | const InitializedEntity &Entity, |
| 4267 | const InitializationKind &Kind, |
| 4268 | InitListExpr *InitList, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4269 | InitializationSequence &Sequence, |
| 4270 | bool TreatUnavailableAsInvalid) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4271 | QualType DestType = Entity.getType(); |
| 4272 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4273 | // C++ doesn't allow scalar initialization with more than one argument. |
| 4274 | // But C99 complex numbers are scalars and it makes sense there. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4275 | if (S.getLangOpts().CPlusPlus && DestType->isScalarType() && |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4276 | !DestType->isAnyComplexType() && InitList->getNumInits() > 1) { |
| 4277 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 4278 | return; |
| 4279 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4280 | if (DestType->isReferenceType()) { |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4281 | TryReferenceListInitialization(S, Entity, Kind, InitList, Sequence, |
| 4282 | TreatUnavailableAsInvalid); |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4283 | return; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4284 | } |
Sebastian Redl | 4f28b58 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 4285 | |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4286 | if (DestType->isRecordType() && |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4287 | !S.isCompleteType(InitList->getBeginLoc(), DestType)) { |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4288 | Sequence.setIncompleteTypeFailure(DestType); |
| 4289 | return; |
| 4290 | } |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4291 | |
Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 4292 | // C++11 [dcl.init.list]p3, per DR1467: |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4293 | // - If T is a class type and the initializer list has a single element of |
| 4294 | // type cv U, where U is T or a class derived from T, the object is |
| 4295 | // initialized from that element (by copy-initialization for |
| 4296 | // copy-list-initialization, or by direct-initialization for |
| 4297 | // direct-list-initialization). |
| 4298 | // - Otherwise, if T is a character array and the initializer list has a |
| 4299 | // single element that is an appropriately-typed string literal |
| 4300 | // (8.5.2 [dcl.init.string]), initialization is performed as described |
| 4301 | // in that section. |
Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 4302 | // - Otherwise, if T is an aggregate, [...] (continue below). |
| 4303 | if (S.getLangOpts().CPlusPlus11 && InitList->getNumInits() == 1) { |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4304 | if (DestType->isRecordType()) { |
| 4305 | QualType InitType = InitList->getInit(0)->getType(); |
| 4306 | if (S.Context.hasSameUnqualifiedType(InitType, DestType) || |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4307 | S.IsDerivedFrom(InitList->getBeginLoc(), InitType, DestType)) { |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 4308 | Expr *InitListAsExpr = InitList; |
| 4309 | TryConstructorInitialization(S, Entity, Kind, InitListAsExpr, DestType, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 4310 | DestType, Sequence, |
| 4311 | /*InitListSyntax*/false, |
| 4312 | /*IsInitListCopy*/true); |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4313 | return; |
| 4314 | } |
| 4315 | } |
| 4316 | if (const ArrayType *DestAT = S.Context.getAsArrayType(DestType)) { |
| 4317 | Expr *SubInit[1] = {InitList->getInit(0)}; |
| 4318 | if (!isa<VariableArrayType>(DestAT) && |
| 4319 | IsStringInit(SubInit[0], DestAT, S.Context) == SIF_None) { |
| 4320 | InitializationKind SubKind = |
| 4321 | Kind.getKind() == InitializationKind::IK_DirectList |
| 4322 | ? InitializationKind::CreateDirect(Kind.getLocation(), |
| 4323 | InitList->getLBraceLoc(), |
| 4324 | InitList->getRBraceLoc()) |
| 4325 | : Kind; |
| 4326 | Sequence.InitializeFrom(S, Entity, SubKind, SubInit, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4327 | /*TopLevelOfInitList*/ true, |
| 4328 | TreatUnavailableAsInvalid); |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4329 | |
| 4330 | // TryStringLiteralInitialization() (in InitializeFrom()) will fail if |
| 4331 | // the element is not an appropriately-typed string literal, in which |
| 4332 | // case we should proceed as in C++11 (below). |
| 4333 | if (Sequence) { |
| 4334 | Sequence.RewrapReferenceInitList(Entity.getType(), InitList); |
| 4335 | return; |
| 4336 | } |
| 4337 | } |
Sebastian Redl | 4f28b58 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 4338 | } |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4339 | } |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4340 | |
| 4341 | // C++11 [dcl.init.list]p3: |
| 4342 | // - If T is an aggregate, aggregate initialization is performed. |
Faisal Vali | 30622bb | 2015-12-07 02:37:44 +0000 | [diff] [blame] | 4343 | if ((DestType->isRecordType() && !DestType->isAggregateType()) || |
| 4344 | (S.getLangOpts().CPlusPlus11 && |
| 4345 | S.isStdInitializerList(DestType, nullptr))) { |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4346 | if (S.getLangOpts().CPlusPlus11) { |
| 4347 | // - Otherwise, if the initializer list has no elements and T is a |
| 4348 | // class type with a default constructor, the object is |
| 4349 | // value-initialized. |
| 4350 | if (InitList->getNumInits() == 0) { |
| 4351 | CXXRecordDecl *RD = DestType->getAsCXXRecordDecl(); |
| 4352 | if (RD->hasDefaultConstructor()) { |
| 4353 | TryValueInitialization(S, Entity, Kind, Sequence, InitList); |
| 4354 | return; |
| 4355 | } |
| 4356 | } |
| 4357 | |
| 4358 | // - Otherwise, if T is a specialization of std::initializer_list<E>, |
| 4359 | // an initializer_list object constructed [...] |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4360 | if (TryInitializerListConstruction(S, InitList, DestType, Sequence, |
| 4361 | TreatUnavailableAsInvalid)) |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4362 | return; |
| 4363 | |
| 4364 | // - Otherwise, if T is a class type, constructors are considered. |
| 4365 | Expr *InitListAsExpr = InitList; |
| 4366 | TryConstructorInitialization(S, Entity, Kind, InitListAsExpr, DestType, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 4367 | DestType, Sequence, /*InitListSyntax*/true); |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4368 | } else |
| 4369 | Sequence.SetFailed(InitializationSequence::FK_InitListBadDestinationType); |
| 4370 | return; |
| 4371 | } |
| 4372 | |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 4373 | if (S.getLangOpts().CPlusPlus && !DestType->isAggregateType() && |
Richard Smith | ed63886 | 2016-03-28 06:08:37 +0000 | [diff] [blame] | 4374 | InitList->getNumInits() == 1) { |
| 4375 | Expr *E = InitList->getInit(0); |
| 4376 | |
| 4377 | // - Otherwise, if T is an enumeration with a fixed underlying type, |
| 4378 | // the initializer-list has a single element v, and the initialization |
| 4379 | // is direct-list-initialization, the object is initialized with the |
| 4380 | // value T(v); if a narrowing conversion is required to convert v to |
| 4381 | // the underlying type of T, the program is ill-formed. |
| 4382 | auto *ET = DestType->getAs<EnumType>(); |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 4383 | if (S.getLangOpts().CPlusPlus17 && |
Richard Smith | ed63886 | 2016-03-28 06:08:37 +0000 | [diff] [blame] | 4384 | Kind.getKind() == InitializationKind::IK_DirectList && |
| 4385 | ET && ET->getDecl()->isFixed() && |
| 4386 | !S.Context.hasSameUnqualifiedType(E->getType(), DestType) && |
| 4387 | (E->getType()->isIntegralOrEnumerationType() || |
| 4388 | E->getType()->isFloatingType())) { |
| 4389 | // There are two ways that T(v) can work when T is an enumeration type. |
| 4390 | // If there is either an implicit conversion sequence from v to T or |
| 4391 | // a conversion function that can convert from v to T, then we use that. |
| 4392 | // Otherwise, if v is of integral, enumeration, or floating-point type, |
| 4393 | // it is converted to the enumeration type via its underlying type. |
| 4394 | // There is no overlap possible between these two cases (except when the |
| 4395 | // source value is already of the destination type), and the first |
| 4396 | // case is handled by the general case for single-element lists below. |
| 4397 | ImplicitConversionSequence ICS; |
| 4398 | ICS.setStandard(); |
| 4399 | ICS.Standard.setAsIdentityConversion(); |
Vedant Kumar | f4217f8 | 2017-02-16 01:20:00 +0000 | [diff] [blame] | 4400 | if (!E->isRValue()) |
| 4401 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
Richard Smith | ed63886 | 2016-03-28 06:08:37 +0000 | [diff] [blame] | 4402 | // If E is of a floating-point type, then the conversion is ill-formed |
| 4403 | // due to narrowing, but go through the motions in order to produce the |
| 4404 | // right diagnostic. |
| 4405 | ICS.Standard.Second = E->getType()->isFloatingType() |
| 4406 | ? ICK_Floating_Integral |
| 4407 | : ICK_Integral_Conversion; |
| 4408 | ICS.Standard.setFromType(E->getType()); |
| 4409 | ICS.Standard.setToType(0, E->getType()); |
| 4410 | ICS.Standard.setToType(1, DestType); |
| 4411 | ICS.Standard.setToType(2, DestType); |
| 4412 | Sequence.AddConversionSequenceStep(ICS, ICS.Standard.getToType(2), |
| 4413 | /*TopLevelOfInitList*/true); |
| 4414 | Sequence.RewrapReferenceInitList(Entity.getType(), InitList); |
| 4415 | return; |
| 4416 | } |
| 4417 | |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 4418 | // - Otherwise, if the initializer list has a single element of type E |
| 4419 | // [...references are handled above...], the object or reference is |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4420 | // initialized from that element (by copy-initialization for |
| 4421 | // copy-list-initialization, or by direct-initialization for |
| 4422 | // direct-list-initialization); if a narrowing conversion is required |
| 4423 | // to convert the element to T, the program is ill-formed. |
| 4424 | // |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 4425 | // Per core-24034, this is direct-initialization if we were performing |
| 4426 | // direct-list-initialization and copy-initialization otherwise. |
| 4427 | // We can't use InitListChecker for this, because it always performs |
| 4428 | // copy-initialization. This only matters if we might use an 'explicit' |
| 4429 | // conversion operator, so we only need to handle the cases where the source |
| 4430 | // is of record type. |
Richard Smith | ed63886 | 2016-03-28 06:08:37 +0000 | [diff] [blame] | 4431 | if (InitList->getInit(0)->getType()->isRecordType()) { |
| 4432 | InitializationKind SubKind = |
| 4433 | Kind.getKind() == InitializationKind::IK_DirectList |
| 4434 | ? InitializationKind::CreateDirect(Kind.getLocation(), |
| 4435 | InitList->getLBraceLoc(), |
| 4436 | InitList->getRBraceLoc()) |
| 4437 | : Kind; |
| 4438 | Expr *SubInit[1] = { InitList->getInit(0) }; |
| 4439 | Sequence.InitializeFrom(S, Entity, SubKind, SubInit, |
| 4440 | /*TopLevelOfInitList*/true, |
| 4441 | TreatUnavailableAsInvalid); |
| 4442 | if (Sequence) |
| 4443 | Sequence.RewrapReferenceInitList(Entity.getType(), InitList); |
| 4444 | return; |
| 4445 | } |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 4446 | } |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4447 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4448 | InitListChecker CheckInitList(S, Entity, InitList, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4449 | DestType, /*VerifyOnly=*/true, TreatUnavailableAsInvalid); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4450 | if (CheckInitList.HadError()) { |
| 4451 | Sequence.SetFailed(InitializationSequence::FK_ListInitializationFailed); |
| 4452 | return; |
| 4453 | } |
| 4454 | |
| 4455 | // Add the list initialization step with the built init list. |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4456 | Sequence.AddListInitializationStep(DestType); |
| 4457 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4458 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4459 | /// Try a reference initialization that involves calling a conversion |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4460 | /// function. |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4461 | static OverloadingResult TryRefInitWithConversionFunction( |
| 4462 | Sema &S, const InitializedEntity &Entity, const InitializationKind &Kind, |
| 4463 | Expr *Initializer, bool AllowRValues, bool IsLValueRef, |
| 4464 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4465 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4466 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 4467 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 4468 | QualType cv2T2 = Initializer->getType(); |
| 4469 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 4470 | |
| 4471 | bool DerivedToBase; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4472 | bool ObjCConversion; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4473 | bool ObjCLifetimeConversion; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4474 | assert(!S.CompareReferenceRelationship(Initializer->getBeginLoc(), T1, T2, |
| 4475 | DerivedToBase, ObjCConversion, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4476 | ObjCLifetimeConversion) && |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4477 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 8abbc65 | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 4478 | (void)DerivedToBase; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4479 | (void)ObjCConversion; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4480 | (void)ObjCLifetimeConversion; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4481 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4482 | // Build the candidate set directly in the initialization sequence |
| 4483 | // structure, so that it will persist if we fail. |
| 4484 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 4485 | CandidateSet.clear(OverloadCandidateSet::CSK_InitByUserDefinedConversion); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4486 | |
Richard Smith | b368ea8 | 2018-07-02 23:25:22 +0000 | [diff] [blame] | 4487 | // Determine whether we are allowed to call explicit conversion operators. |
| 4488 | // Note that none of [over.match.copy], [over.match.conv], nor |
| 4489 | // [over.match.ref] permit an explicit constructor to be chosen when |
| 4490 | // initializing a reference, not even for direct-initialization. |
| 4491 | bool AllowExplicitCtors = false; |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4492 | bool AllowExplicitConvs = Kind.allowExplicitConversionFunctionsInRefBinding(); |
| 4493 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4494 | const RecordType *T1RecordType = nullptr; |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 4495 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4496 | S.isCompleteType(Kind.getLocation(), T1)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4497 | // The type we're converting to is a class type. Enumerate its constructors |
| 4498 | // to see if there is a suitable conversion. |
| 4499 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 4500 | |
Richard Smith | 40c7806 | 2015-02-21 02:31:57 +0000 | [diff] [blame] | 4501 | for (NamedDecl *D : S.LookupConstructors(T1RecordDecl)) { |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4502 | auto Info = getConstructorInfo(D); |
| 4503 | if (!Info.Constructor) |
| 4504 | continue; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4505 | |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4506 | if (!Info.Constructor->isInvalidDecl() && |
Richard Smith | b368ea8 | 2018-07-02 23:25:22 +0000 | [diff] [blame] | 4507 | Info.Constructor->isConvertingConstructor(AllowExplicitCtors)) { |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4508 | if (Info.ConstructorTmpl) |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 4509 | S.AddTemplateOverloadCandidate( |
| 4510 | Info.ConstructorTmpl, Info.FoundDecl, |
| 4511 | /*ExplicitArgs*/ nullptr, Initializer, CandidateSet, |
| 4512 | /*SuppressUserConversions=*/true, |
| 4513 | /*PartialOverloading*/ false, AllowExplicitCtors); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4514 | else |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 4515 | S.AddOverloadCandidate( |
| 4516 | Info.Constructor, Info.FoundDecl, Initializer, CandidateSet, |
| 4517 | /*SuppressUserConversions=*/true, |
| 4518 | /*PartialOverloading*/ false, AllowExplicitCtors); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4519 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4520 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4521 | } |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 4522 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 4523 | return OR_No_Viable_Function; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4524 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4525 | const RecordType *T2RecordType = nullptr; |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 4526 | if ((T2RecordType = T2->getAs<RecordType>()) && |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4527 | S.isCompleteType(Kind.getLocation(), T2)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4528 | // The type we're converting from is a class type, enumerate its conversion |
| 4529 | // functions. |
| 4530 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 4531 | |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 4532 | const auto &Conversions = T2RecordDecl->getVisibleConversionFunctions(); |
| 4533 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4534 | NamedDecl *D = *I; |
| 4535 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 4536 | if (isa<UsingShadowDecl>(D)) |
| 4537 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4538 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4539 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 4540 | CXXConversionDecl *Conv; |
| 4541 | if (ConvTemplate) |
| 4542 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 4543 | else |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4544 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4545 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4546 | // If the conversion function doesn't return a reference type, |
| 4547 | // it can't be considered for this conversion unless we're allowed to |
| 4548 | // consider rvalues. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4549 | // FIXME: Do we need to make sure that we only consider conversion |
| 4550 | // candidates with reference-compatible results? That might be needed to |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4551 | // break recursion. |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4552 | if ((AllowExplicitConvs || !Conv->isExplicit()) && |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 4553 | (AllowRValues || |
| 4554 | Conv->getConversionType()->isLValueReferenceType())) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4555 | if (ConvTemplate) |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 4556 | S.AddTemplateConversionCandidate( |
| 4557 | ConvTemplate, I.getPair(), ActingDC, Initializer, DestType, |
| 4558 | CandidateSet, |
| 4559 | /*AllowObjCConversionOnExplicit=*/false, AllowExplicitConvs); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4560 | else |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 4561 | S.AddConversionCandidate( |
| 4562 | Conv, I.getPair(), ActingDC, Initializer, DestType, CandidateSet, |
| 4563 | /*AllowObjCConversionOnExplicit=*/false, AllowExplicitConvs); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4564 | } |
| 4565 | } |
| 4566 | } |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 4567 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 4568 | return OR_No_Viable_Function; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4569 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4570 | SourceLocation DeclLoc = Initializer->getBeginLoc(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4571 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4572 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4573 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4574 | if (OverloadingResult Result |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 4575 | = CandidateSet.BestViableFunction(S, DeclLoc, Best)) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4576 | return Result; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 4577 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4578 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 4579 | // This is the overload that will be used for this initialization step if we |
| 4580 | // use this initialization. Mark it as referenced. |
| 4581 | Function->setReferenced(); |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4582 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4583 | // Compute the returned type and value kind of the conversion. |
| 4584 | QualType cv3T3; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4585 | if (isa<CXXConversionDecl>(Function)) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4586 | cv3T3 = Function->getReturnType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4587 | else |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4588 | cv3T3 = T1; |
| 4589 | |
| 4590 | ExprValueKind VK = VK_RValue; |
| 4591 | if (cv3T3->isLValueReferenceType()) |
| 4592 | VK = VK_LValue; |
| 4593 | else if (const auto *RRef = cv3T3->getAs<RValueReferenceType>()) |
| 4594 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
| 4595 | cv3T3 = cv3T3.getNonLValueExprType(S.Context); |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 4596 | |
| 4597 | // Add the user-defined conversion step. |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4598 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4599 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, cv3T3, |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4600 | HadMultipleCandidates); |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 4601 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4602 | // Determine whether we'll need to perform derived-to-base adjustments or |
| 4603 | // other conversions. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4604 | bool NewDerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4605 | bool NewObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4606 | bool NewObjCLifetimeConversion = false; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4607 | Sema::ReferenceCompareResult NewRefRelationship |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4608 | = S.CompareReferenceRelationship(DeclLoc, T1, cv3T3, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4609 | NewDerivedToBase, NewObjCConversion, |
| 4610 | NewObjCLifetimeConversion); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4611 | |
| 4612 | // Add the final conversion sequence, if necessary. |
Douglas Gregor | 1ce52ca | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 4613 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4614 | assert(!isa<CXXConstructorDecl>(Function) && |
| 4615 | "should not have conversion after constructor"); |
| 4616 | |
Douglas Gregor | 1ce52ca | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 4617 | ImplicitConversionSequence ICS; |
| 4618 | ICS.setStandard(); |
| 4619 | ICS.Standard = Best->FinalConversion; |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4620 | Sequence.AddConversionSequenceStep(ICS, ICS.Standard.getToType(2)); |
| 4621 | |
| 4622 | // Every implicit conversion results in a prvalue, except for a glvalue |
| 4623 | // derived-to-base conversion, which we handle below. |
| 4624 | cv3T3 = ICS.Standard.getToType(2); |
| 4625 | VK = VK_RValue; |
| 4626 | } |
| 4627 | |
| 4628 | // If the converted initializer is a prvalue, its type T4 is adjusted to |
| 4629 | // type "cv1 T4" and the temporary materialization conversion is applied. |
| 4630 | // |
| 4631 | // We adjust the cv-qualifications to match the reference regardless of |
| 4632 | // whether we have a prvalue so that the AST records the change. In this |
| 4633 | // case, T4 is "cv3 T3". |
| 4634 | QualType cv1T4 = S.Context.getQualifiedType(cv3T3, cv1T1.getQualifiers()); |
| 4635 | if (cv1T4.getQualifiers() != cv3T3.getQualifiers()) |
| 4636 | Sequence.AddQualificationConversionStep(cv1T4, VK); |
| 4637 | Sequence.AddReferenceBindingStep(cv1T4, VK == VK_RValue); |
| 4638 | VK = IsLValueRef ? VK_LValue : VK_XValue; |
| 4639 | |
| 4640 | if (NewDerivedToBase) |
| 4641 | Sequence.AddDerivedToBaseCastStep(cv1T1, VK); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4642 | else if (NewObjCConversion) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4643 | Sequence.AddObjCObjectConversionStep(cv1T1); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4644 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4645 | return OR_Success; |
| 4646 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4647 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4648 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 4649 | const InitializedEntity &Entity, |
| 4650 | Expr *CurInitExpr); |
| 4651 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4652 | /// Attempt reference initialization (C++0x [dcl.init.ref]) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4653 | static void TryReferenceInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4654 | const InitializedEntity &Entity, |
| 4655 | const InitializationKind &Kind, |
| 4656 | Expr *Initializer, |
| 4657 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4658 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4659 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 4660 | Qualifiers T1Quals; |
| 4661 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4662 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 4663 | Qualifiers T2Quals; |
| 4664 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4665 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4666 | // If the initializer is the address of an overloaded function, try |
| 4667 | // to resolve the overloaded function. If all goes well, T2 is the |
| 4668 | // type of the resulting function. |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4669 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 4670 | T1, Sequence)) |
| 4671 | return; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4672 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4673 | // Delegate everything else to a subfunction. |
| 4674 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 4675 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 4676 | } |
| 4677 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4678 | /// Determine whether an expression is a non-referenceable glvalue (one to |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 4679 | /// which a reference can never bind). Attempting to bind a reference to |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4680 | /// such a glvalue will always create a temporary. |
| 4681 | static bool isNonReferenceableGLValue(Expr *E) { |
| 4682 | return E->refersToBitField() || E->refersToVectorElement(); |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 4683 | } |
| 4684 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4685 | /// Reference initialization without resolving overloaded functions. |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4686 | static void TryReferenceInitializationCore(Sema &S, |
| 4687 | const InitializedEntity &Entity, |
| 4688 | const InitializationKind &Kind, |
| 4689 | Expr *Initializer, |
| 4690 | QualType cv1T1, QualType T1, |
| 4691 | Qualifiers T1Quals, |
| 4692 | QualType cv2T2, QualType T2, |
| 4693 | Qualifiers T2Quals, |
| 4694 | InitializationSequence &Sequence) { |
| 4695 | QualType DestType = Entity.getType(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4696 | SourceLocation DeclLoc = Initializer->getBeginLoc(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4697 | // Compute some basic properties of the types and the initializer. |
| 4698 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 4699 | bool isRValueRef = !isLValueRef; |
| 4700 | bool DerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4701 | bool ObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4702 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4703 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4704 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4705 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4706 | ObjCConversion, ObjCLifetimeConversion); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4707 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4708 | // C++0x [dcl.init.ref]p5: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4709 | // 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] | 4710 | // "cv2 T2" as follows: |
| 4711 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4712 | // - If the reference is an lvalue reference and the initializer |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4713 | // expression |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4714 | // Note the analogous bullet points for rvalue refs to functions. Because |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4715 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 4716 | // like lvalue refs. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4717 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4718 | bool T1Function = T1->isFunctionType(); |
| 4719 | if (isLValueRef || T1Function) { |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4720 | if (InitCategory.isLValue() && !isNonReferenceableGLValue(Initializer) && |
Richard Smith | ce76629 | 2016-10-21 23:01:55 +0000 | [diff] [blame] | 4721 | (RefRelationship == Sema::Ref_Compatible || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4722 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4723 | RefRelationship == Sema::Ref_Related))) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4724 | // - 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] | 4725 | // reference-compatible with "cv2 T2," or |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4726 | if (T1Quals != T2Quals) |
| 4727 | // Convert to cv1 T2. This should only add qualifiers unless this is a |
| 4728 | // c-style cast. The removal of qualifiers in that case notionally |
| 4729 | // happens after the reference binding, but that doesn't matter. |
| 4730 | Sequence.AddQualificationConversionStep( |
| 4731 | S.Context.getQualifiedType(T2, T1Quals), |
| 4732 | Initializer->getValueKind()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4733 | if (DerivedToBase) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4734 | Sequence.AddDerivedToBaseCastStep(cv1T1, VK_LValue); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4735 | else if (ObjCConversion) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4736 | Sequence.AddObjCObjectConversionStep(cv1T1); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4737 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4738 | // We only create a temporary here when binding a reference to a |
| 4739 | // bit-field or vector element. Those cases are't supposed to be |
| 4740 | // handled by this bullet, but the outcome is the same either way. |
| 4741 | Sequence.AddReferenceBindingStep(cv1T1, false); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4742 | return; |
| 4743 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4744 | |
| 4745 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 4746 | // reference-related to T2, and can be implicitly converted to an |
| 4747 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 4748 | // with "cv3 T3" (this conversion is selected by enumerating the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4749 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 4750 | // one through overload resolution (13.3)), |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4751 | // 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] | 4752 | // an rvalue. DR1287 removed the "implicitly" here. |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4753 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 4754 | (isLValueRef || InitCategory.isRValue())) { |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4755 | ConvOvlResult = TryRefInitWithConversionFunction( |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4756 | S, Entity, Kind, Initializer, /*AllowRValues*/ isRValueRef, |
| 4757 | /*IsLValueRef*/ isLValueRef, Sequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4758 | if (ConvOvlResult == OR_Success) |
| 4759 | return; |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4760 | if (ConvOvlResult != OR_No_Viable_Function) |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4761 | Sequence.SetOverloadFailure( |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4762 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4763 | ConvOvlResult); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4764 | } |
| 4765 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4766 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4767 | // - Otherwise, the reference shall be an lvalue reference to a |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4768 | // 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] | 4769 | // shall be an rvalue reference. |
Anastasia Stulova | 3562edb | 2019-06-21 11:36:15 +0000 | [diff] [blame] | 4770 | // For address spaces, we interpret this to mean that an addr space |
| 4771 | // of a reference "cv1 T1" is a superset of addr space of "cv2 T2". |
| 4772 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile() && |
| 4773 | T1Quals.isAddressSpaceSupersetOf(T2Quals))) { |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 4774 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 4775 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 4776 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4777 | Sequence.SetOverloadFailure( |
| 4778 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4779 | ConvOvlResult); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4780 | else if (!InitCategory.isLValue()) |
| 4781 | Sequence.SetFailed( |
Anastasia Stulova | 3562edb | 2019-06-21 11:36:15 +0000 | [diff] [blame] | 4782 | T1Quals.isAddressSpaceSupersetOf(T2Quals) |
| 4783 | ? InitializationSequence:: |
| 4784 | FK_NonConstLValueReferenceBindingToTemporary |
| 4785 | : InitializationSequence::FK_ReferenceInitDropsQualifiers); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4786 | else { |
| 4787 | InitializationSequence::FailureKind FK; |
| 4788 | switch (RefRelationship) { |
| 4789 | case Sema::Ref_Compatible: |
| 4790 | if (Initializer->refersToBitField()) |
| 4791 | FK = InitializationSequence:: |
| 4792 | FK_NonConstLValueReferenceBindingToBitfield; |
| 4793 | else if (Initializer->refersToVectorElement()) |
| 4794 | FK = InitializationSequence:: |
| 4795 | FK_NonConstLValueReferenceBindingToVectorElement; |
| 4796 | else |
| 4797 | llvm_unreachable("unexpected kind of compatible initializer"); |
| 4798 | break; |
| 4799 | case Sema::Ref_Related: |
| 4800 | FK = InitializationSequence::FK_ReferenceInitDropsQualifiers; |
| 4801 | break; |
| 4802 | case Sema::Ref_Incompatible: |
| 4803 | FK = InitializationSequence:: |
| 4804 | FK_NonConstLValueReferenceBindingToUnrelated; |
| 4805 | break; |
| 4806 | } |
| 4807 | Sequence.SetFailed(FK); |
| 4808 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4809 | return; |
| 4810 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4811 | |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4812 | // - If the initializer expression |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4813 | // - is an |
| 4814 | // [<=14] xvalue (but not a bit-field), class prvalue, array prvalue, or |
| 4815 | // [1z] rvalue (but not a bit-field) or |
| 4816 | // function lvalue and "cv1 T1" is reference-compatible with "cv2 T2" |
| 4817 | // |
| 4818 | // Note: functions are handled above and below rather than here... |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4819 | if (!T1Function && |
Richard Smith | ce76629 | 2016-10-21 23:01:55 +0000 | [diff] [blame] | 4820 | (RefRelationship == Sema::Ref_Compatible || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4821 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4822 | RefRelationship == Sema::Ref_Related)) && |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4823 | ((InitCategory.isXValue() && !isNonReferenceableGLValue(Initializer)) || |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 4824 | (InitCategory.isPRValue() && |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 4825 | (S.getLangOpts().CPlusPlus17 || T2->isRecordType() || |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 4826 | T2->isArrayType())))) { |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4827 | ExprValueKind ValueKind = InitCategory.isXValue() ? VK_XValue : VK_RValue; |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4828 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4829 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 4830 | // compiler the freedom to perform a copy here or bind to the |
| 4831 | // object, while C++0x requires that we bind directly to the |
| 4832 | // object. Hence, we always bind to the object without making an |
| 4833 | // extra copy. However, in C++03 requires that we check for the |
| 4834 | // presence of a suitable copy constructor: |
| 4835 | // |
| 4836 | // The constructor that would be used to make the copy shall |
| 4837 | // be callable whether or not the copy is actually done. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4838 | if (!S.getLangOpts().CPlusPlus11 && !S.getLangOpts().MicrosoftExt) |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4839 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4840 | else if (S.getLangOpts().CPlusPlus11) |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4841 | CheckCXX98CompatAccessibleCopy(S, Entity, Initializer); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4842 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4843 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4844 | // C++1z [dcl.init.ref]/5.2.1.2: |
| 4845 | // If the converted initializer is a prvalue, its type T4 is adjusted |
| 4846 | // to type "cv1 T4" and the temporary materialization conversion is |
| 4847 | // applied. |
Anastasia Stulova | d1986d1 | 2019-01-14 11:44:22 +0000 | [diff] [blame] | 4848 | // Postpone address space conversions to after the temporary materialization |
| 4849 | // conversion to allow creating temporaries in the alloca address space. |
Anastasia Stulova | e368e4d | 2019-02-05 11:32:58 +0000 | [diff] [blame] | 4850 | auto T1QualsIgnoreAS = T1Quals; |
| 4851 | auto T2QualsIgnoreAS = T2Quals; |
| 4852 | if (T1Quals.getAddressSpace() != T2Quals.getAddressSpace()) { |
| 4853 | T1QualsIgnoreAS.removeAddressSpace(); |
| 4854 | T2QualsIgnoreAS.removeAddressSpace(); |
| 4855 | } |
| 4856 | QualType cv1T4 = S.Context.getQualifiedType(cv2T2, T1QualsIgnoreAS); |
| 4857 | if (T1QualsIgnoreAS != T2QualsIgnoreAS) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4858 | Sequence.AddQualificationConversionStep(cv1T4, ValueKind); |
| 4859 | Sequence.AddReferenceBindingStep(cv1T4, ValueKind == VK_RValue); |
| 4860 | ValueKind = isLValueRef ? VK_LValue : VK_XValue; |
Anastasia Stulova | e368e4d | 2019-02-05 11:32:58 +0000 | [diff] [blame] | 4861 | // Add addr space conversion if required. |
| 4862 | if (T1Quals.getAddressSpace() != T2Quals.getAddressSpace()) { |
| 4863 | auto T4Quals = cv1T4.getQualifiers(); |
| 4864 | T4Quals.addAddressSpace(T1Quals.getAddressSpace()); |
| 4865 | QualType cv1T4WithAS = S.Context.getQualifiedType(T2, T4Quals); |
| 4866 | Sequence.AddQualificationConversionStep(cv1T4WithAS, ValueKind); |
Anastasia Stulova | d1986d1 | 2019-01-14 11:44:22 +0000 | [diff] [blame] | 4867 | } |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4868 | |
| 4869 | // In any case, the reference is bound to the resulting glvalue (or to |
| 4870 | // an appropriate base class subobject). |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4871 | if (DerivedToBase) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4872 | Sequence.AddDerivedToBaseCastStep(cv1T1, ValueKind); |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4873 | else if (ObjCConversion) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4874 | Sequence.AddObjCObjectConversionStep(cv1T1); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4875 | return; |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4876 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4877 | |
| 4878 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 4879 | // reference-related to T2, and can be implicitly converted to an |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4880 | // xvalue, class prvalue, or function lvalue of type "cv3 T3", |
| 4881 | // where "cv1 T1" is reference-compatible with "cv3 T3", |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4882 | // |
| 4883 | // DR1287 removes the "implicitly" here. |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4884 | if (T2->isRecordType()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4885 | if (RefRelationship == Sema::Ref_Incompatible) { |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4886 | ConvOvlResult = TryRefInitWithConversionFunction( |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4887 | S, Entity, Kind, Initializer, /*AllowRValues*/ true, |
| 4888 | /*IsLValueRef*/ isLValueRef, Sequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4889 | if (ConvOvlResult) |
| 4890 | Sequence.SetOverloadFailure( |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4891 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4892 | ConvOvlResult); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4893 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4894 | return; |
| 4895 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4896 | |
Richard Smith | ce76629 | 2016-10-21 23:01:55 +0000 | [diff] [blame] | 4897 | if (RefRelationship == Sema::Ref_Compatible && |
Douglas Gregor | 6fa6ab0 | 2013-03-26 23:59:23 +0000 | [diff] [blame] | 4898 | isRValueRef && InitCategory.isLValue()) { |
| 4899 | Sequence.SetFailed( |
| 4900 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 4901 | return; |
| 4902 | } |
| 4903 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4904 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 4905 | return; |
| 4906 | } |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4907 | |
| 4908 | // - Otherwise, a temporary of type "cv1 T1" is created and initialized |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4909 | // from the initializer expression using the rules for a non-reference |
Richard Smith | 2eabf78 | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 4910 | // copy-initialization (8.5). The reference is then bound to the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4911 | // temporary. [...] |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 4912 | |
Anastasia Stulova | d3ae87e | 2019-03-06 13:02:41 +0000 | [diff] [blame] | 4913 | // Ignore address space of reference type at this point and perform address |
| 4914 | // space conversion after the reference binding step. |
| 4915 | QualType cv1T1IgnoreAS = |
| 4916 | T1Quals.hasAddressSpace() |
| 4917 | ? S.Context.getQualifiedType(T1, T1Quals.withoutAddressSpace()) |
| 4918 | : cv1T1; |
| 4919 | |
| 4920 | InitializedEntity TempEntity = |
| 4921 | InitializedEntity::InitializeTemporary(cv1T1IgnoreAS); |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 4922 | |
Richard Smith | 2eabf78 | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 4923 | // FIXME: Why do we use an implicit conversion here rather than trying |
| 4924 | // copy-initialization? |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4925 | ImplicitConversionSequence ICS |
| 4926 | = S.TryImplicitConversion(Initializer, TempEntity.getType(), |
Richard Smith | 2eabf78 | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 4927 | /*SuppressUserConversions=*/false, |
| 4928 | /*AllowExplicit=*/false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4929 | /*FIXME:InOverloadResolution=*/false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4930 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 4931 | /*AllowObjCWritebackConversion=*/false); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4932 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4933 | if (ICS.isBad()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4934 | // FIXME: Use the conversion function set stored in ICS to turn |
| 4935 | // this into an overloading ambiguity diagnostic. However, we need |
| 4936 | // to keep that set as an OverloadCandidateSet rather than as some |
| 4937 | // other kind of set. |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4938 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 4939 | Sequence.SetOverloadFailure( |
| 4940 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4941 | ConvOvlResult); |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 4942 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 4943 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4944 | else |
| 4945 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4946 | return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4947 | } else { |
| 4948 | Sequence.AddConversionSequenceStep(ICS, TempEntity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4949 | } |
| 4950 | |
| 4951 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 4952 | // same cv-qualification as, or greater cv-qualification |
| 4953 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 4954 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 4955 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
Anastasia Stulova | d3ae87e | 2019-03-06 13:02:41 +0000 | [diff] [blame] | 4956 | if ((RefRelationship == Sema::Ref_Related && |
| 4957 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) || |
| 4958 | !T1Quals.isAddressSpaceSupersetOf(T2Quals)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4959 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 4960 | return; |
| 4961 | } |
| 4962 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4963 | // [...] 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] | 4964 | // reference, the initializer expression shall not be an lvalue. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4965 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 4966 | InitCategory.isLValue()) { |
| 4967 | Sequence.SetFailed( |
| 4968 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 4969 | return; |
| 4970 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4971 | |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 4972 | Sequence.AddReferenceBindingStep(cv1T1IgnoreAS, /*BindingTemporary=*/true); |
Anastasia Stulova | d3ae87e | 2019-03-06 13:02:41 +0000 | [diff] [blame] | 4973 | |
Anastasia Stulova | 5145b1e | 2019-06-05 14:03:34 +0000 | [diff] [blame] | 4974 | if (T1Quals.hasAddressSpace()) { |
| 4975 | if (!Qualifiers::isAddressSpaceSupersetOf(T1Quals.getAddressSpace(), |
| 4976 | LangAS::Default)) { |
| 4977 | Sequence.SetFailed( |
| 4978 | InitializationSequence::FK_ReferenceAddrspaceMismatchTemporary); |
| 4979 | return; |
| 4980 | } |
Anastasia Stulova | d3ae87e | 2019-03-06 13:02:41 +0000 | [diff] [blame] | 4981 | Sequence.AddQualificationConversionStep(cv1T1, isLValueRef ? VK_LValue |
| 4982 | : VK_XValue); |
Anastasia Stulova | 5145b1e | 2019-06-05 14:03:34 +0000 | [diff] [blame] | 4983 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4984 | } |
| 4985 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4986 | /// Attempt character array initialization from a string literal |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4987 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 4988 | static void TryStringLiteralInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4989 | const InitializedEntity &Entity, |
| 4990 | const InitializationKind &Kind, |
| 4991 | Expr *Initializer, |
| 4992 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4993 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4994 | } |
| 4995 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4996 | /// Attempt value initialization (C++ [dcl.init]p7). |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4997 | static void TryValueInitialization(Sema &S, |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4998 | const InitializedEntity &Entity, |
| 4999 | const InitializationKind &Kind, |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5000 | InitializationSequence &Sequence, |
| 5001 | InitListExpr *InitList) { |
| 5002 | assert((!InitList || InitList->getNumInits() == 0) && |
| 5003 | "Shouldn't use value-init for non-empty init lists"); |
| 5004 | |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 5005 | // C++98 [dcl.init]p5, C++11 [dcl.init]p7: |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5006 | // |
| 5007 | // To value-initialize an object of type T means: |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5008 | QualType T = Entity.getType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5009 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5010 | // -- if T is an array type, then each element is value-initialized; |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 5011 | T = S.Context.getBaseElementType(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5012 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5013 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 5014 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5015 | bool NeedZeroInitialization = true; |
Richard Smith | 505ef81 | 2016-12-21 01:57:02 +0000 | [diff] [blame] | 5016 | // C++98: |
| 5017 | // -- if T is a class type (clause 9) with a user-declared constructor |
| 5018 | // (12.1), then the default constructor for T is called (and the |
| 5019 | // initialization is ill-formed if T has no accessible default |
| 5020 | // constructor); |
| 5021 | // C++11: |
| 5022 | // -- if T is a class type (clause 9) with either no default constructor |
| 5023 | // (12.1 [class.ctor]) or a default constructor that is user-provided |
| 5024 | // or deleted, then the object is default-initialized; |
| 5025 | // |
| 5026 | // Note that the C++11 rule is the same as the C++98 rule if there are no |
| 5027 | // defaulted or deleted constructors, so we just use it unconditionally. |
| 5028 | CXXConstructorDecl *CD = S.LookupDefaultConstructor(ClassDecl); |
| 5029 | if (!CD || !CD->getCanonicalDecl()->isDefaulted() || CD->isDeleted()) |
| 5030 | NeedZeroInitialization = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5031 | |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 5032 | // -- if T is a (possibly cv-qualified) non-union class type without a |
| 5033 | // user-provided or deleted default constructor, then the object is |
| 5034 | // zero-initialized and, if T has a non-trivial default constructor, |
| 5035 | // default-initialized; |
Richard Smith | fb26652 | 2012-10-18 00:44:17 +0000 | [diff] [blame] | 5036 | // The 'non-union' here was removed by DR1502. The 'non-trivial default |
| 5037 | // constructor' part was removed by DR1507. |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5038 | if (NeedZeroInitialization) |
| 5039 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 5040 | |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 5041 | // C++03: |
| 5042 | // -- if T is a non-union class type without a user-declared constructor, |
| 5043 | // then every non-static data member and base class component of T is |
| 5044 | // value-initialized; |
| 5045 | // [...] A program that calls for [...] value-initialization of an |
| 5046 | // entity of reference type is ill-formed. |
| 5047 | // |
| 5048 | // C++11 doesn't need this handling, because value-initialization does not |
| 5049 | // occur recursively there, and the implicit default constructor is |
| 5050 | // defined as deleted in the problematic cases. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5051 | if (!S.getLangOpts().CPlusPlus11 && |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 5052 | ClassDecl->hasUninitializedReferenceMember()) { |
| 5053 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForReference); |
| 5054 | return; |
| 5055 | } |
| 5056 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5057 | // If this is list-value-initialization, pass the empty init list on when |
| 5058 | // building the constructor call. This affects the semantics of a few |
| 5059 | // things (such as whether an explicit default constructor can be called). |
| 5060 | Expr *InitListAsExpr = InitList; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5061 | MultiExprArg Args(&InitListAsExpr, InitList ? 1 : 0); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5062 | bool InitListSyntax = InitList; |
| 5063 | |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 5064 | // FIXME: Instead of creating a CXXConstructExpr of array type here, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5065 | // wrap a class-typed CXXConstructExpr in an ArrayInitLoopExpr. |
| 5066 | return TryConstructorInitialization( |
| 5067 | S, Entity, Kind, Args, T, Entity.getType(), Sequence, InitListSyntax); |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5068 | } |
| 5069 | } |
| 5070 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5071 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5072 | } |
| 5073 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5074 | /// Attempt default initialization (C++ [dcl.init]p6). |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5075 | static void TryDefaultInitialization(Sema &S, |
| 5076 | const InitializedEntity &Entity, |
| 5077 | const InitializationKind &Kind, |
| 5078 | InitializationSequence &Sequence) { |
| 5079 | assert(Kind.getKind() == InitializationKind::IK_Default); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5080 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5081 | // C++ [dcl.init]p6: |
| 5082 | // To default-initialize an object of type T means: |
| 5083 | // - if T is an array type, each element is default-initialized; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5084 | QualType DestType = S.Context.getBaseElementType(Entity.getType()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5085 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5086 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 5087 | // constructor for T is called (and the initialization is ill-formed if |
| 5088 | // T has no accessible default constructor); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5089 | if (DestType->isRecordType() && S.getLangOpts().CPlusPlus) { |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5090 | TryConstructorInitialization(S, Entity, Kind, None, DestType, |
| 5091 | Entity.getType(), Sequence); |
Chandler Carruth | c926240 | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 5092 | return; |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5093 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5094 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5095 | // - otherwise, no initialization is performed. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5096 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5097 | // If a program calls for the default initialization of an object of |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5098 | // 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] | 5099 | // default constructor. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5100 | if (DestType.isConstQualified() && S.getLangOpts().CPlusPlus) { |
Nico Weber | 337d5aa | 2015-04-17 08:32:38 +0000 | [diff] [blame] | 5101 | if (!maybeRecoverWithZeroInitialization(S, Sequence, Entity)) |
| 5102 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5103 | return; |
| 5104 | } |
| 5105 | |
| 5106 | // If the destination type has a lifetime property, zero-initialize it. |
| 5107 | if (DestType.getQualifiers().hasObjCLifetime()) { |
| 5108 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 5109 | return; |
| 5110 | } |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5111 | } |
| 5112 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5113 | /// Attempt a user-defined conversion between two types (C++ [dcl.init]), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5114 | /// which enumerates all conversion functions and performs overload resolution |
| 5115 | /// to select the best. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5116 | static void TryUserDefinedConversion(Sema &S, |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5117 | QualType DestType, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5118 | const InitializationKind &Kind, |
| 5119 | Expr *Initializer, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5120 | InitializationSequence &Sequence, |
| 5121 | bool TopLevelOfInitList) { |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5122 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 5123 | QualType SourceType = Initializer->getType(); |
| 5124 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 5125 | "Must have a class type to perform a user-defined conversion"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5126 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5127 | // Build the candidate set directly in the initialization sequence |
| 5128 | // structure, so that it will persist if we fail. |
| 5129 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 5130 | CandidateSet.clear(OverloadCandidateSet::CSK_InitByUserDefinedConversion); |
Anastasia Stulova | c25ea86 | 2019-06-20 16:23:28 +0000 | [diff] [blame] | 5131 | CandidateSet.setDestAS(DestType.getQualifiers().getAddressSpace()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5132 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5133 | // Determine whether we are allowed to call explicit constructors or |
| 5134 | // explicit conversion operators. |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5135 | bool AllowExplicit = Kind.AllowExplicit(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5136 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5137 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 5138 | // The type we're converting to is a class type. Enumerate its constructors |
| 5139 | // to see if there is a suitable conversion. |
| 5140 | CXXRecordDecl *DestRecordDecl |
| 5141 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5142 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 5143 | // Try to complete the type we're converting to. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 5144 | if (S.isCompleteType(Kind.getLocation(), DestType)) { |
Richard Smith | 776e9c3 | 2017-02-01 03:28:59 +0000 | [diff] [blame] | 5145 | for (NamedDecl *D : S.LookupConstructors(DestRecordDecl)) { |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 5146 | auto Info = getConstructorInfo(D); |
| 5147 | if (!Info.Constructor) |
| 5148 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5149 | |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 5150 | if (!Info.Constructor->isInvalidDecl() && |
| 5151 | Info.Constructor->isConvertingConstructor(AllowExplicit)) { |
| 5152 | if (Info.ConstructorTmpl) |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 5153 | S.AddTemplateOverloadCandidate( |
| 5154 | Info.ConstructorTmpl, Info.FoundDecl, |
| 5155 | /*ExplicitArgs*/ nullptr, Initializer, CandidateSet, |
| 5156 | /*SuppressUserConversions=*/true, |
| 5157 | /*PartialOverloading*/ false, AllowExplicit); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 5158 | else |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 5159 | S.AddOverloadCandidate(Info.Constructor, Info.FoundDecl, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5160 | Initializer, CandidateSet, |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 5161 | /*SuppressUserConversions=*/true, |
| 5162 | /*PartialOverloading*/ false, AllowExplicit); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 5163 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5164 | } |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 5165 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5166 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5167 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5168 | SourceLocation DeclLoc = Initializer->getBeginLoc(); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5169 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5170 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 5171 | // The type we're converting from is a class type, enumerate its conversion |
| 5172 | // functions. |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5173 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 5174 | // We can only enumerate the conversion functions for a complete type; if |
| 5175 | // the type isn't complete, simply skip this step. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 5176 | if (S.isCompleteType(DeclLoc, SourceType)) { |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 5177 | CXXRecordDecl *SourceRecordDecl |
| 5178 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5179 | |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 5180 | const auto &Conversions = |
| 5181 | SourceRecordDecl->getVisibleConversionFunctions(); |
| 5182 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 5183 | NamedDecl *D = *I; |
| 5184 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 5185 | if (isa<UsingShadowDecl>(D)) |
| 5186 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5187 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 5188 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 5189 | CXXConversionDecl *Conv; |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5190 | if (ConvTemplate) |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 5191 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5192 | else |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 5193 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5194 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 5195 | if (AllowExplicit || !Conv->isExplicit()) { |
| 5196 | if (ConvTemplate) |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 5197 | S.AddTemplateConversionCandidate( |
| 5198 | ConvTemplate, I.getPair(), ActingDC, Initializer, DestType, |
| 5199 | CandidateSet, AllowExplicit, AllowExplicit); |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 5200 | else |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 5201 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Initializer, |
| 5202 | DestType, CandidateSet, AllowExplicit, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 5203 | AllowExplicit); |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 5204 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5205 | } |
| 5206 | } |
| 5207 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5208 | |
| 5209 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5210 | OverloadCandidateSet::iterator Best; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5211 | if (OverloadingResult Result |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 5212 | = CandidateSet.BestViableFunction(S, DeclLoc, Best)) { |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5213 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5214 | InitializationSequence::FK_UserConversionOverloadFailed, |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5215 | Result); |
| 5216 | return; |
| 5217 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5218 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5219 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 5220 | Function->setReferenced(); |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 5221 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5222 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5223 | if (isa<CXXConstructorDecl>(Function)) { |
| 5224 | // Add the user-defined conversion step. Any cv-qualification conversion is |
Richard Smith | b24f067 | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 5225 | // subsumed by the initialization. Per DR5, the created temporary is of the |
| 5226 | // cv-unqualified type of the destination. |
| 5227 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
| 5228 | DestType.getUnqualifiedType(), |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 5229 | HadMultipleCandidates); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5230 | |
| 5231 | // C++14 and before: |
| 5232 | // - if the function is a constructor, the call initializes a temporary |
| 5233 | // of the cv-unqualified version of the destination type. The [...] |
| 5234 | // temporary [...] is then used to direct-initialize, according to the |
| 5235 | // rules above, the object that is the destination of the |
| 5236 | // copy-initialization. |
| 5237 | // Note that this just performs a simple object copy from the temporary. |
| 5238 | // |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 5239 | // C++17: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5240 | // - if the function is a constructor, the call is a prvalue of the |
| 5241 | // cv-unqualified version of the destination type whose return object |
| 5242 | // is initialized by the constructor. The call is used to |
| 5243 | // direct-initialize, according to the rules above, the object that |
| 5244 | // is the destination of the copy-initialization. |
| 5245 | // Therefore we need to do nothing further. |
| 5246 | // |
| 5247 | // FIXME: Mark this copy as extraneous. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 5248 | if (!S.getLangOpts().CPlusPlus17) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5249 | Sequence.AddFinalCopy(DestType); |
Richard Smith | 16d3150 | 2016-12-21 01:31:56 +0000 | [diff] [blame] | 5250 | else if (DestType.hasQualifiers()) |
| 5251 | Sequence.AddQualificationConversionStep(DestType, VK_RValue); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5252 | return; |
| 5253 | } |
| 5254 | |
| 5255 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 5256 | QualType ConvType = Function->getCallResultType(); |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 5257 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType, |
| 5258 | HadMultipleCandidates); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5259 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5260 | if (ConvType->getAs<RecordType>()) { |
| 5261 | // The call is used to direct-initialize [...] the object that is the |
| 5262 | // destination of the copy-initialization. |
| 5263 | // |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 5264 | // 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] | 5265 | // - If the initializer expression is a prvalue and the cv-unqualified |
| 5266 | // version of the source type is the same as the class of the |
| 5267 | // destination [... do not make an extra copy] |
| 5268 | // |
| 5269 | // FIXME: Mark this copy as extraneous. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 5270 | if (!S.getLangOpts().CPlusPlus17 || |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5271 | Function->getReturnType()->isReferenceType() || |
| 5272 | !S.Context.hasSameUnqualifiedType(ConvType, DestType)) |
| 5273 | Sequence.AddFinalCopy(DestType); |
Richard Smith | 16d3150 | 2016-12-21 01:31:56 +0000 | [diff] [blame] | 5274 | else if (!S.Context.hasSameType(ConvType, DestType)) |
| 5275 | Sequence.AddQualificationConversionStep(DestType, VK_RValue); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5276 | return; |
| 5277 | } |
| 5278 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5279 | // If the conversion following the call to the conversion function |
| 5280 | // is interesting, add it as a separate step. |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5281 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 5282 | Best->FinalConversion.Third) { |
| 5283 | ImplicitConversionSequence ICS; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5284 | ICS.setStandard(); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5285 | ICS.Standard = Best->FinalConversion; |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5286 | Sequence.AddConversionSequenceStep(ICS, DestType, TopLevelOfInitList); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5287 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5288 | } |
| 5289 | |
Richard Smith | f032001b | 2013-06-20 02:18:31 +0000 | [diff] [blame] | 5290 | /// An egregious hack for compatibility with libstdc++-4.2: in <tr1/hashtable>, |
| 5291 | /// a function with a pointer return type contains a 'return false;' statement. |
| 5292 | /// In C++11, 'false' is not a null pointer, so this breaks the build of any |
| 5293 | /// code using that header. |
| 5294 | /// |
| 5295 | /// Work around this by treating 'return false;' as zero-initializing the result |
| 5296 | /// if it's used in a pointer-returning function in a system header. |
| 5297 | static bool isLibstdcxxPointerReturnFalseHack(Sema &S, |
| 5298 | const InitializedEntity &Entity, |
| 5299 | const Expr *Init) { |
| 5300 | return S.getLangOpts().CPlusPlus11 && |
| 5301 | Entity.getKind() == InitializedEntity::EK_Result && |
| 5302 | Entity.getType()->isPointerType() && |
| 5303 | isa<CXXBoolLiteralExpr>(Init) && |
| 5304 | !cast<CXXBoolLiteralExpr>(Init)->getValue() && |
| 5305 | S.getSourceManager().isInSystemHeader(Init->getExprLoc()); |
| 5306 | } |
| 5307 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5308 | /// The non-zero enum values here are indexes into diagnostic alternatives. |
| 5309 | enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar }; |
| 5310 | |
| 5311 | /// Determines whether this expression is an acceptable ICR source. |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 5312 | static InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e, |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5313 | bool isAddressOf, bool &isWeakAccess) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5314 | // Skip parens. |
| 5315 | e = e->IgnoreParens(); |
| 5316 | |
| 5317 | // Skip address-of nodes. |
| 5318 | if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 5319 | if (op->getOpcode() == UO_AddrOf) |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5320 | return isInvalidICRSource(C, op->getSubExpr(), /*addressof*/ true, |
| 5321 | isWeakAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5322 | |
| 5323 | // Skip certain casts. |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 5324 | } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 5325 | switch (ce->getCastKind()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5326 | case CK_Dependent: |
| 5327 | case CK_BitCast: |
| 5328 | case CK_LValueBitCast: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5329 | case CK_NoOp: |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5330 | return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf, isWeakAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5331 | |
| 5332 | case CK_ArrayToPointerDecay: |
| 5333 | return IIK_nonscalar; |
| 5334 | |
| 5335 | case CK_NullToPointer: |
| 5336 | return IIK_okay; |
| 5337 | |
| 5338 | default: |
| 5339 | break; |
| 5340 | } |
| 5341 | |
| 5342 | // 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] | 5343 | } else if (isa<DeclRefExpr>(e)) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5344 | // set isWeakAccess to true, to mean that there will be an implicit |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5345 | // load which requires a cleanup. |
| 5346 | if (e->getType().getObjCLifetime() == Qualifiers::OCL_Weak) |
| 5347 | isWeakAccess = true; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5348 | |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 5349 | if (!isAddressOf) return IIK_nonlocal; |
| 5350 | |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5351 | VarDecl *var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl()); |
| 5352 | if (!var) return IIK_nonlocal; |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 5353 | |
| 5354 | return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5355 | |
| 5356 | // If we have a conditional operator, check both sides. |
| 5357 | } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) { |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5358 | if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf, |
| 5359 | isWeakAccess)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5360 | return iik; |
| 5361 | |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5362 | return isInvalidICRSource(C, cond->getRHS(), isAddressOf, isWeakAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5363 | |
| 5364 | // These are never scalar. |
| 5365 | } else if (isa<ArraySubscriptExpr>(e)) { |
| 5366 | return IIK_nonscalar; |
| 5367 | |
| 5368 | // Otherwise, it needs to be a null pointer constant. |
| 5369 | } else { |
| 5370 | return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull) |
| 5371 | ? IIK_okay : IIK_nonlocal); |
| 5372 | } |
| 5373 | |
| 5374 | return IIK_nonlocal; |
| 5375 | } |
| 5376 | |
| 5377 | /// Check whether the given expression is a valid operand for an |
| 5378 | /// indirect copy/restore. |
| 5379 | static void checkIndirectCopyRestoreSource(Sema &S, Expr *src) { |
| 5380 | assert(src->isRValue()); |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5381 | bool isWeakAccess = false; |
| 5382 | InvalidICRKind iik = isInvalidICRSource(S.Context, src, false, isWeakAccess); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5383 | // If isWeakAccess to true, there will be an implicit |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5384 | // load which requires a cleanup. |
| 5385 | if (S.getLangOpts().ObjCAutoRefCount && isWeakAccess) |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 5386 | S.Cleanup.setExprNeedsCleanups(true); |
| 5387 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5388 | if (iik == IIK_okay) return; |
| 5389 | |
| 5390 | S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback) |
| 5391 | << ((unsigned) iik - 1) // shift index into diagnostic explanations |
| 5392 | << src->getSourceRange(); |
| 5393 | } |
| 5394 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5395 | /// Determine whether we have compatible array types for the |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5396 | /// purposes of GNU by-copy array initialization. |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 5397 | static bool hasCompatibleArrayTypes(ASTContext &Context, const ArrayType *Dest, |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5398 | const ArrayType *Source) { |
| 5399 | // If the source and destination array types are equivalent, we're |
| 5400 | // done. |
| 5401 | if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0))) |
| 5402 | return true; |
| 5403 | |
| 5404 | // Make sure that the element types are the same. |
| 5405 | if (!Context.hasSameType(Dest->getElementType(), Source->getElementType())) |
| 5406 | return false; |
| 5407 | |
| 5408 | // The only mismatch we allow is when the destination is an |
| 5409 | // incomplete array type and the source is a constant array type. |
| 5410 | return Source->isConstantArrayType() && Dest->isIncompleteArrayType(); |
| 5411 | } |
| 5412 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5413 | static bool tryObjCWritebackConversion(Sema &S, |
| 5414 | InitializationSequence &Sequence, |
| 5415 | const InitializedEntity &Entity, |
| 5416 | Expr *Initializer) { |
| 5417 | bool ArrayDecay = false; |
| 5418 | QualType ArgType = Initializer->getType(); |
| 5419 | QualType ArgPointee; |
| 5420 | if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) { |
| 5421 | ArrayDecay = true; |
| 5422 | ArgPointee = ArgArrayType->getElementType(); |
| 5423 | ArgType = S.Context.getPointerType(ArgPointee); |
| 5424 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5425 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5426 | // Handle write-back conversion. |
| 5427 | QualType ConvertedArgType; |
| 5428 | if (!S.isObjCWritebackConversion(ArgType, Entity.getType(), |
| 5429 | ConvertedArgType)) |
| 5430 | return false; |
| 5431 | |
| 5432 | // We should copy unless we're passing to an argument explicitly |
| 5433 | // marked 'out'. |
| 5434 | bool ShouldCopy = true; |
| 5435 | if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 5436 | ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 5437 | |
| 5438 | // Do we need an lvalue conversion? |
| 5439 | if (ArrayDecay || Initializer->isGLValue()) { |
| 5440 | ImplicitConversionSequence ICS; |
| 5441 | ICS.setStandard(); |
| 5442 | ICS.Standard.setAsIdentityConversion(); |
| 5443 | |
| 5444 | QualType ResultType; |
| 5445 | if (ArrayDecay) { |
| 5446 | ICS.Standard.First = ICK_Array_To_Pointer; |
| 5447 | ResultType = S.Context.getPointerType(ArgPointee); |
| 5448 | } else { |
| 5449 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
| 5450 | ResultType = Initializer->getType().getNonLValueExprType(S.Context); |
| 5451 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5452 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5453 | Sequence.AddConversionSequenceStep(ICS, ResultType); |
| 5454 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5455 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5456 | Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
| 5457 | return true; |
| 5458 | } |
| 5459 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5460 | static bool TryOCLSamplerInitialization(Sema &S, |
| 5461 | InitializationSequence &Sequence, |
| 5462 | QualType DestType, |
| 5463 | Expr *Initializer) { |
| 5464 | if (!S.getLangOpts().OpenCL || !DestType->isSamplerT() || |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 5465 | (!Initializer->isIntegerConstantExpr(S.Context) && |
| 5466 | !Initializer->getType()->isSamplerT())) |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5467 | return false; |
| 5468 | |
| 5469 | Sequence.AddOCLSamplerInitStep(DestType); |
| 5470 | return true; |
| 5471 | } |
| 5472 | |
Andrew Savonichev | 3fee351 | 2018-11-08 11:25:41 +0000 | [diff] [blame] | 5473 | static bool IsZeroInitializer(Expr *Initializer, Sema &S) { |
| 5474 | return Initializer->isIntegerConstantExpr(S.getASTContext()) && |
| 5475 | (Initializer->EvaluateKnownConstInt(S.getASTContext()) == 0); |
| 5476 | } |
| 5477 | |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 5478 | static bool TryOCLZeroOpaqueTypeInitialization(Sema &S, |
| 5479 | InitializationSequence &Sequence, |
| 5480 | QualType DestType, |
| 5481 | Expr *Initializer) { |
| 5482 | if (!S.getLangOpts().OpenCL) |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5483 | return false; |
| 5484 | |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 5485 | // |
| 5486 | // OpenCL 1.2 spec, s6.12.10 |
| 5487 | // |
| 5488 | // The event argument can also be used to associate the |
| 5489 | // async_work_group_copy with a previous async copy allowing |
| 5490 | // an event to be shared by multiple async copies; otherwise |
| 5491 | // event should be zero. |
| 5492 | // |
| 5493 | if (DestType->isEventT() || DestType->isQueueT()) { |
Andrew Savonichev | 3fee351 | 2018-11-08 11:25:41 +0000 | [diff] [blame] | 5494 | if (!IsZeroInitializer(Initializer, S)) |
| 5495 | return false; |
| 5496 | |
| 5497 | Sequence.AddOCLZeroOpaqueTypeStep(DestType); |
| 5498 | return true; |
| 5499 | } |
| 5500 | |
| 5501 | // We should allow zero initialization for all types defined in the |
| 5502 | // cl_intel_device_side_avc_motion_estimation extension, except |
| 5503 | // intel_sub_group_avc_mce_payload_t and intel_sub_group_avc_mce_result_t. |
| 5504 | if (S.getOpenCLOptions().isEnabled( |
| 5505 | "cl_intel_device_side_avc_motion_estimation") && |
| 5506 | DestType->isOCLIntelSubgroupAVCType()) { |
| 5507 | if (DestType->isOCLIntelSubgroupAVCMcePayloadType() || |
| 5508 | DestType->isOCLIntelSubgroupAVCMceResultType()) |
| 5509 | return false; |
| 5510 | if (!IsZeroInitializer(Initializer, S)) |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 5511 | return false; |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5512 | |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 5513 | Sequence.AddOCLZeroOpaqueTypeStep(DestType); |
| 5514 | return true; |
| 5515 | } |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 5516 | |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 5517 | return false; |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 5518 | } |
| 5519 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5520 | InitializationSequence::InitializationSequence(Sema &S, |
| 5521 | const InitializedEntity &Entity, |
| 5522 | const InitializationKind &Kind, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5523 | MultiExprArg Args, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 5524 | bool TopLevelOfInitList, |
| 5525 | bool TreatUnavailableAsInvalid) |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 5526 | : FailedCandidateSet(Kind.getLocation(), OverloadCandidateSet::CSK_Normal) { |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 5527 | InitializeFrom(S, Entity, Kind, Args, TopLevelOfInitList, |
| 5528 | TreatUnavailableAsInvalid); |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 5529 | } |
| 5530 | |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 5531 | /// Tries to get a FunctionDecl out of `E`. If it succeeds and we can take the |
| 5532 | /// address of that function, this returns true. Otherwise, it returns false. |
| 5533 | static bool isExprAnUnaddressableFunction(Sema &S, const Expr *E) { |
| 5534 | auto *DRE = dyn_cast<DeclRefExpr>(E); |
| 5535 | if (!DRE || !isa<FunctionDecl>(DRE->getDecl())) |
| 5536 | return false; |
| 5537 | |
| 5538 | return !S.checkAddressOfFunctionIsAvailable( |
| 5539 | cast<FunctionDecl>(DRE->getDecl())); |
| 5540 | } |
| 5541 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5542 | /// Determine whether we can perform an elementwise array copy for this kind |
| 5543 | /// of entity. |
| 5544 | static bool canPerformArrayCopy(const InitializedEntity &Entity) { |
| 5545 | switch (Entity.getKind()) { |
| 5546 | case InitializedEntity::EK_LambdaCapture: |
| 5547 | // C++ [expr.prim.lambda]p24: |
| 5548 | // For array members, the array elements are direct-initialized in |
| 5549 | // increasing subscript order. |
| 5550 | return true; |
| 5551 | |
| 5552 | case InitializedEntity::EK_Variable: |
| 5553 | // C++ [dcl.decomp]p1: |
| 5554 | // [...] each element is copy-initialized or direct-initialized from the |
| 5555 | // corresponding element of the assignment-expression [...] |
| 5556 | return isa<DecompositionDecl>(Entity.getDecl()); |
| 5557 | |
| 5558 | case InitializedEntity::EK_Member: |
| 5559 | // C++ [class.copy.ctor]p14: |
| 5560 | // - if the member is an array, each element is direct-initialized with |
| 5561 | // the corresponding subobject of x |
| 5562 | return Entity.isImplicitMemberInitializer(); |
| 5563 | |
| 5564 | case InitializedEntity::EK_ArrayElement: |
| 5565 | // All the above cases are intended to apply recursively, even though none |
| 5566 | // of them actually say that. |
| 5567 | if (auto *E = Entity.getParent()) |
| 5568 | return canPerformArrayCopy(*E); |
| 5569 | break; |
| 5570 | |
| 5571 | default: |
| 5572 | break; |
| 5573 | } |
| 5574 | |
| 5575 | return false; |
| 5576 | } |
| 5577 | |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 5578 | void InitializationSequence::InitializeFrom(Sema &S, |
| 5579 | const InitializedEntity &Entity, |
| 5580 | const InitializationKind &Kind, |
| 5581 | MultiExprArg Args, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 5582 | bool TopLevelOfInitList, |
| 5583 | bool TreatUnavailableAsInvalid) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5584 | ASTContext &Context = S.Context; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5585 | |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 5586 | // Eliminate non-overload placeholder types in the arguments. We |
| 5587 | // need to do this before checking whether types are dependent |
| 5588 | // because lowering a pseudo-object expression might well give us |
| 5589 | // something of dependent type. |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5590 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 5591 | if (Args[I]->getType()->isNonOverloadPlaceholderType()) { |
| 5592 | // FIXME: should we be doing this here? |
| 5593 | ExprResult result = S.CheckPlaceholderExpr(Args[I]); |
| 5594 | if (result.isInvalid()) { |
| 5595 | SetFailed(FK_PlaceholderType); |
| 5596 | return; |
| 5597 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5598 | Args[I] = result.get(); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 5599 | } |
| 5600 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5601 | // C++0x [dcl.init]p16: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5602 | // The semantics of initializers are as follows. The destination type is |
| 5603 | // the type of the object or reference being initialized and the source |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5604 | // 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] | 5605 | // 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] | 5606 | // parenthesized list of expressions. |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5607 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5608 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5609 | if (DestType->isDependentType() || |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5610 | Expr::hasAnyTypeDependentArguments(Args)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5611 | SequenceKind = DependentSequence; |
| 5612 | return; |
| 5613 | } |
| 5614 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5615 | // Almost everything is a normal sequence. |
| 5616 | setSequenceKind(NormalSequence); |
| 5617 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5618 | QualType SourceType; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5619 | Expr *Initializer = nullptr; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5620 | if (Args.size() == 1) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5621 | Initializer = Args[0]; |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 5622 | if (S.getLangOpts().ObjC) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5623 | if (S.CheckObjCBridgeRelatedConversions(Initializer->getBeginLoc(), |
Fariborz Jahanian | 283bf89 | 2013-12-18 21:04:43 +0000 | [diff] [blame] | 5624 | DestType, Initializer->getType(), |
| 5625 | Initializer) || |
| 5626 | S.ConversionToObjCStringLiteralCheck(DestType, Initializer)) |
| 5627 | Args[0] = Initializer; |
Fariborz Jahanian | 283bf89 | 2013-12-18 21:04:43 +0000 | [diff] [blame] | 5628 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5629 | if (!isa<InitListExpr>(Initializer)) |
| 5630 | SourceType = Initializer->getType(); |
| 5631 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5632 | |
Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 5633 | // - If the initializer is a (non-parenthesized) braced-init-list, the |
| 5634 | // object is list-initialized (8.5.4). |
| 5635 | if (Kind.getKind() != InitializationKind::IK_Direct) { |
| 5636 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 5637 | TryListInitialization(S, Entity, Kind, InitList, *this, |
| 5638 | TreatUnavailableAsInvalid); |
Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 5639 | return; |
| 5640 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5641 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5642 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5643 | // - If the destination type is a reference type, see 8.5.3. |
| 5644 | if (DestType->isReferenceType()) { |
| 5645 | // C++0x [dcl.init.ref]p1: |
| 5646 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 5647 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 5648 | // by an object that can be converted into a T. |
| 5649 | // (Therefore, multiple arguments are not permitted.) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5650 | if (Args.size() != 1) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5651 | SetFailed(FK_TooManyInitsForReference); |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 5652 | // C++17 [dcl.init.ref]p5: |
| 5653 | // A reference [...] is initialized by an expression [...] as follows: |
| 5654 | // If the initializer is not an expression, presumably we should reject, |
| 5655 | // but the standard fails to actually say so. |
| 5656 | else if (isa<InitListExpr>(Args[0])) |
| 5657 | SetFailed(FK_ParenthesizedListInitForReference); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5658 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5659 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5660 | return; |
| 5661 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5662 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5663 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5664 | if (Kind.getKind() == InitializationKind::IK_Value || |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5665 | (Kind.getKind() == InitializationKind::IK_Direct && Args.empty())) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5666 | TryValueInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5667 | return; |
| 5668 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5669 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5670 | // Handle default initialization. |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 5671 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5672 | TryDefaultInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5673 | return; |
| 5674 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5675 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 5676 | // - If the destination type is an array of characters, an array of |
| 5677 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 5678 | // initializer is a string literal, see 8.5.2. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5679 | // - Otherwise, if the destination type is an array, the program is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5680 | // ill-formed. |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5681 | if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) { |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 5682 | if (Initializer && isa<VariableArrayType>(DestAT)) { |
| 5683 | SetFailed(FK_VariableLengthArrayHasInitializer); |
| 5684 | return; |
| 5685 | } |
| 5686 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 5687 | if (Initializer) { |
| 5688 | switch (IsStringInit(Initializer, DestAT, Context)) { |
| 5689 | case SIF_None: |
| 5690 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
| 5691 | return; |
| 5692 | case SIF_NarrowStringIntoWideChar: |
| 5693 | SetFailed(FK_NarrowStringIntoWideCharArray); |
| 5694 | return; |
| 5695 | case SIF_WideStringIntoChar: |
| 5696 | SetFailed(FK_WideStringIntoCharArray); |
| 5697 | return; |
| 5698 | case SIF_IncompatWideStringIntoWideChar: |
| 5699 | SetFailed(FK_IncompatWideStringIntoWideChar); |
| 5700 | return; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 5701 | case SIF_PlainStringIntoUTF8Char: |
| 5702 | SetFailed(FK_PlainStringIntoUTF8Char); |
| 5703 | return; |
| 5704 | case SIF_UTF8StringIntoPlainChar: |
| 5705 | SetFailed(FK_UTF8StringIntoPlainChar); |
| 5706 | return; |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 5707 | case SIF_Other: |
| 5708 | break; |
| 5709 | } |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 5710 | } |
| 5711 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5712 | // Some kinds of initialization permit an array to be initialized from |
| 5713 | // another array of the same type, and perform elementwise initialization. |
| 5714 | if (Initializer && isa<ConstantArrayType>(DestAT) && |
| 5715 | S.Context.hasSameUnqualifiedType(Initializer->getType(), |
| 5716 | Entity.getType()) && |
| 5717 | canPerformArrayCopy(Entity)) { |
| 5718 | // If source is a prvalue, use it directly. |
| 5719 | if (Initializer->getValueKind() == VK_RValue) { |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 5720 | AddArrayInitStep(DestType, /*IsGNUExtension*/false); |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5721 | return; |
| 5722 | } |
| 5723 | |
| 5724 | // Emit element-at-a-time copy loop. |
| 5725 | InitializedEntity Element = |
| 5726 | InitializedEntity::InitializeElement(S.Context, 0, Entity); |
| 5727 | QualType InitEltT = |
| 5728 | Context.getAsArrayType(Initializer->getType())->getElementType(); |
Richard Smith | 30e304e | 2016-12-14 00:03:17 +0000 | [diff] [blame] | 5729 | OpaqueValueExpr OVE(Initializer->getExprLoc(), InitEltT, |
| 5730 | Initializer->getValueKind(), |
| 5731 | Initializer->getObjectKind()); |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5732 | Expr *OVEAsExpr = &OVE; |
| 5733 | InitializeFrom(S, Element, Kind, OVEAsExpr, TopLevelOfInitList, |
| 5734 | TreatUnavailableAsInvalid); |
| 5735 | if (!Failed()) |
| 5736 | AddArrayInitLoopStep(Entity.getType(), InitEltT); |
| 5737 | return; |
| 5738 | } |
| 5739 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5740 | // Note: as an GNU C extension, we allow initialization of an |
| 5741 | // array from a compound literal that creates an array of the same |
| 5742 | // type, so long as the initializer has no side effects. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5743 | if (!S.getLangOpts().CPlusPlus && Initializer && |
Eli Friedman | 88fccbd | 2019-02-11 22:54:27 +0000 | [diff] [blame] | 5744 | isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) && |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5745 | Initializer->getType()->isArrayType()) { |
| 5746 | const ArrayType *SourceAT |
| 5747 | = Context.getAsArrayType(Initializer->getType()); |
| 5748 | if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5749 | SetFailed(FK_ArrayTypeMismatch); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5750 | else if (Initializer->HasSideEffects(S.Context)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5751 | SetFailed(FK_NonConstantArrayInit); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5752 | else { |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 5753 | AddArrayInitStep(DestType, /*IsGNUExtension*/true); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5754 | } |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5755 | } |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5756 | // Note: as a GNU C++ extension, we allow list-initialization of a |
| 5757 | // class member of array type from a parenthesized initializer list. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5758 | else if (S.getLangOpts().CPlusPlus && |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5759 | Entity.getKind() == InitializedEntity::EK_Member && |
| 5760 | Initializer && isa<InitListExpr>(Initializer)) { |
| 5761 | TryListInitialization(S, Entity, Kind, cast<InitListExpr>(Initializer), |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 5762 | *this, TreatUnavailableAsInvalid); |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5763 | AddParenthesizedArrayInitStep(DestType); |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 5764 | } else if (DestAT->getElementType()->isCharType()) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5765 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 5766 | else if (IsWideCharCompatible(DestAT->getElementType(), Context)) |
| 5767 | SetFailed(FK_ArrayNeedsInitListOrWideStringLiteral); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5768 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5769 | SetFailed(FK_ArrayNeedsInitList); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5770 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5771 | return; |
| 5772 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5773 | |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 5774 | // Determine whether we should consider writeback conversions for |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5775 | // Objective-C ARC. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5776 | bool allowObjCWritebackConversion = S.getLangOpts().ObjCAutoRefCount && |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5777 | Entity.isParameterKind(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5778 | |
Neil Hickey | 8ece3b6 | 2019-07-16 14:57:32 +0000 | [diff] [blame] | 5779 | if (TryOCLSamplerInitialization(S, *this, DestType, Initializer)) |
| 5780 | return; |
| 5781 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5782 | // We're at the end of the line for C: it's either a write-back conversion |
| 5783 | // 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] | 5784 | if (!S.getLangOpts().CPlusPlus) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5785 | // If allowed, check whether this is an Objective-C writeback conversion. |
| 5786 | if (allowObjCWritebackConversion && |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5787 | tryObjCWritebackConversion(S, *this, Entity, Initializer)) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5788 | return; |
| 5789 | } |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5790 | |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 5791 | if (TryOCLZeroOpaqueTypeInitialization(S, *this, DestType, Initializer)) |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5792 | return; |
| 5793 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5794 | // Handle initialization in C |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5795 | AddCAssignmentStep(DestType); |
| 5796 | MaybeProduceObjCObject(S, *this, Entity); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5797 | return; |
| 5798 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5799 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5800 | assert(S.getLangOpts().CPlusPlus); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 5801 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5802 | // - If the destination type is a (possibly cv-qualified) class type: |
| 5803 | if (DestType->isRecordType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5804 | // - If the initialization is direct-initialization, or if it is |
| 5805 | // copy-initialization where the cv-unqualified version of the |
| 5806 | // 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] | 5807 | // class of the destination, constructors are considered. [...] |
| 5808 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 5809 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 5810 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5811 | S.IsDerivedFrom(Initializer->getBeginLoc(), SourceType, DestType)))) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5812 | TryConstructorInitialization(S, Entity, Kind, Args, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5813 | DestType, DestType, *this); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5814 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5815 | // user-defined conversion sequences that can convert from the source |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5816 | // type to the destination type or (when a conversion function is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5817 | // used) to a derived class thereof are enumerated as described in |
| 5818 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 5819 | // (13.3). |
| 5820 | else |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5821 | TryUserDefinedConversion(S, DestType, Kind, Initializer, *this, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5822 | TopLevelOfInitList); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5823 | return; |
| 5824 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5825 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 5826 | assert(Args.size() >= 1 && "Zero-argument case handled above"); |
| 5827 | |
| 5828 | // The remaining cases all need a source type. |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5829 | if (Args.size() > 1) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5830 | SetFailed(FK_TooManyInitsForScalar); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5831 | return; |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 5832 | } else if (isa<InitListExpr>(Args[0])) { |
| 5833 | SetFailed(FK_ParenthesizedListInitForScalar); |
| 5834 | return; |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5835 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5836 | |
| 5837 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5838 | // type, conversion functions are considered. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5839 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5840 | // For a conversion to _Atomic(T) from either T or a class type derived |
| 5841 | // from T, initialize the T object then convert to _Atomic type. |
| 5842 | bool NeedAtomicConversion = false; |
| 5843 | if (const AtomicType *Atomic = DestType->getAs<AtomicType>()) { |
| 5844 | if (Context.hasSameUnqualifiedType(SourceType, Atomic->getValueType()) || |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5845 | S.IsDerivedFrom(Initializer->getBeginLoc(), SourceType, |
Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 5846 | Atomic->getValueType())) { |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5847 | DestType = Atomic->getValueType(); |
| 5848 | NeedAtomicConversion = true; |
| 5849 | } |
| 5850 | } |
| 5851 | |
| 5852 | TryUserDefinedConversion(S, DestType, Kind, Initializer, *this, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5853 | TopLevelOfInitList); |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5854 | MaybeProduceObjCObject(S, *this, Entity); |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5855 | if (!Failed() && NeedAtomicConversion) |
| 5856 | AddAtomicConversionStep(Entity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5857 | return; |
| 5858 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5859 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5860 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5861 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5862 | // conversions (Clause 4) will be used, if necessary, to convert the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5863 | // initializer expression to the cv-unqualified version of the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5864 | // destination type; no user-defined conversions are considered. |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5865 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5866 | ImplicitConversionSequence ICS |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5867 | = S.TryImplicitConversion(Initializer, DestType, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5868 | /*SuppressUserConversions*/true, |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 5869 | /*AllowExplicitConversions*/ false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 5870 | /*InOverloadResolution*/ false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5871 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 5872 | allowObjCWritebackConversion); |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5873 | |
| 5874 | if (ICS.isStandard() && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5875 | ICS.Standard.Second == ICK_Writeback_Conversion) { |
| 5876 | // Objective-C ARC writeback conversion. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5877 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5878 | // We should copy unless we're passing to an argument explicitly |
| 5879 | // marked 'out'. |
| 5880 | bool ShouldCopy = true; |
| 5881 | if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 5882 | ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5883 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5884 | // If there was an lvalue adjustment, add it as a separate conversion. |
| 5885 | if (ICS.Standard.First == ICK_Array_To_Pointer || |
| 5886 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 5887 | ImplicitConversionSequence LvalueICS; |
| 5888 | LvalueICS.setStandard(); |
| 5889 | LvalueICS.Standard.setAsIdentityConversion(); |
| 5890 | LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0)); |
| 5891 | LvalueICS.Standard.First = ICS.Standard.First; |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5892 | AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5893 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5894 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5895 | AddPassByIndirectCopyRestoreStep(DestType, ShouldCopy); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5896 | } else if (ICS.isBad()) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 5897 | DeclAccessPair dap; |
Richard Smith | f032001b | 2013-06-20 02:18:31 +0000 | [diff] [blame] | 5898 | if (isLibstdcxxPointerReturnFalseHack(S, Entity, Initializer)) { |
| 5899 | AddZeroInitializationStep(Entity.getType()); |
| 5900 | } else if (Initializer->getType() == Context.OverloadTy && |
| 5901 | !S.ResolveAddressOfOverloadedFunction(Initializer, DestType, |
| 5902 | false, dap)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5903 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 5904 | else if (Initializer->getType()->isFunctionType() && |
| 5905 | isExprAnUnaddressableFunction(S, Initializer)) |
| 5906 | SetFailed(InitializationSequence::FK_AddressOfUnaddressableFunction); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 5907 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5908 | SetFailed(InitializationSequence::FK_ConversionFailed); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5909 | } else { |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5910 | AddConversionSequenceStep(ICS, DestType, TopLevelOfInitList); |
John McCall | fa27234 | 2011-06-16 23:24:51 +0000 | [diff] [blame] | 5911 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5912 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 5913 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5914 | } |
| 5915 | |
| 5916 | InitializationSequence::~InitializationSequence() { |
Davide Italiano | 67bb9f7 | 2015-07-01 21:51:58 +0000 | [diff] [blame] | 5917 | for (auto &S : Steps) |
| 5918 | S.Destroy(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5919 | } |
| 5920 | |
| 5921 | //===----------------------------------------------------------------------===// |
| 5922 | // Perform initialization |
| 5923 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5924 | static Sema::AssignmentAction |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 5925 | getAssignmentAction(const InitializedEntity &Entity, bool Diagnose = false) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5926 | switch(Entity.getKind()) { |
| 5927 | case InitializedEntity::EK_Variable: |
| 5928 | case InitializedEntity::EK_New: |
Douglas Gregor | 6dd3a6a | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 5929 | case InitializedEntity::EK_Exception: |
| 5930 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 5931 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5932 | return Sema::AA_Initializing; |
| 5933 | |
| 5934 | case InitializedEntity::EK_Parameter: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5935 | if (Entity.getDecl() && |
Douglas Gregor | 6b7f12c | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 5936 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 5937 | return Sema::AA_Sending; |
| 5938 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5939 | return Sema::AA_Passing; |
| 5940 | |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 5941 | case InitializedEntity::EK_Parameter_CF_Audited: |
| 5942 | if (Entity.getDecl() && |
| 5943 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 5944 | return Sema::AA_Sending; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5945 | |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 5946 | return !Diagnose ? Sema::AA_Passing : Sema::AA_Passing_CFAudited; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5947 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5948 | case InitializedEntity::EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 5949 | case InitializedEntity::EK_StmtExprResult: // FIXME: Not quite right. |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5950 | return Sema::AA_Returning; |
| 5951 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5952 | case InitializedEntity::EK_Temporary: |
Fariborz Jahanian | 14e9541 | 2013-07-11 19:13:34 +0000 | [diff] [blame] | 5953 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5954 | // FIXME: Can we tell apart casting vs. converting? |
| 5955 | return Sema::AA_Casting; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5956 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5957 | case InitializedEntity::EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 5958 | case InitializedEntity::EK_Binding: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 5959 | case InitializedEntity::EK_ArrayElement: |
| 5960 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 5961 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 5962 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 5963 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 5964 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5965 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5966 | return Sema::AA_Initializing; |
| 5967 | } |
| 5968 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 5969 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5970 | } |
| 5971 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5972 | /// Whether we should bind a created object as a temporary when |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5973 | /// initializing the given entity. |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5974 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5975 | switch (Entity.getKind()) { |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 5976 | case InitializedEntity::EK_ArrayElement: |
| 5977 | case InitializedEntity::EK_Member: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5978 | case InitializedEntity::EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 5979 | case InitializedEntity::EK_StmtExprResult: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5980 | case InitializedEntity::EK_New: |
| 5981 | case InitializedEntity::EK_Variable: |
| 5982 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 5983 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 5984 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 5985 | case InitializedEntity::EK_ComplexElement: |
Anders Carlsson | fcd764a | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 5986 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 5987 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 5988 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 5989 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5990 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5991 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5992 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5993 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5994 | case InitializedEntity::EK_Parameter_CF_Audited: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5995 | case InitializedEntity::EK_Temporary: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5996 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 5997 | case InitializedEntity::EK_Binding: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5998 | return true; |
| 5999 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6000 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6001 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 6002 | } |
| 6003 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6004 | /// Whether the given entity, when initialized with an object |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 6005 | /// created for that initialization, requires destruction. |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 6006 | static bool shouldDestroyEntity(const InitializedEntity &Entity) { |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 6007 | switch (Entity.getKind()) { |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 6008 | case InitializedEntity::EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 6009 | case InitializedEntity::EK_StmtExprResult: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 6010 | case InitializedEntity::EK_New: |
| 6011 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 6012 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 6013 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 6014 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 6015 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 6016 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 6017 | case InitializedEntity::EK_LambdaCapture: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 6018 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6019 | |
Richard Smith | 27874d6 | 2013-01-08 00:08:23 +0000 | [diff] [blame] | 6020 | case InitializedEntity::EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 6021 | case InitializedEntity::EK_Binding: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 6022 | case InitializedEntity::EK_Variable: |
| 6023 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6024 | case InitializedEntity::EK_Parameter_CF_Audited: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 6025 | case InitializedEntity::EK_Temporary: |
| 6026 | case InitializedEntity::EK_ArrayElement: |
| 6027 | case InitializedEntity::EK_Exception: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 6028 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 6029 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 6030 | return true; |
| 6031 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6032 | |
| 6033 | llvm_unreachable("missed an InitializedEntity kind?"); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 6034 | } |
| 6035 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6036 | /// Get the location at which initialization diagnostics should appear. |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6037 | static SourceLocation getInitializationLoc(const InitializedEntity &Entity, |
| 6038 | Expr *Initializer) { |
| 6039 | switch (Entity.getKind()) { |
| 6040 | case InitializedEntity::EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 6041 | case InitializedEntity::EK_StmtExprResult: |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6042 | return Entity.getReturnLoc(); |
| 6043 | |
| 6044 | case InitializedEntity::EK_Exception: |
| 6045 | return Entity.getThrowLoc(); |
| 6046 | |
| 6047 | case InitializedEntity::EK_Variable: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 6048 | case InitializedEntity::EK_Binding: |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6049 | return Entity.getDecl()->getLocation(); |
| 6050 | |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 6051 | case InitializedEntity::EK_LambdaCapture: |
| 6052 | return Entity.getCaptureLoc(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6053 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6054 | case InitializedEntity::EK_ArrayElement: |
| 6055 | case InitializedEntity::EK_Member: |
| 6056 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6057 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6058 | case InitializedEntity::EK_Temporary: |
| 6059 | case InitializedEntity::EK_New: |
| 6060 | case InitializedEntity::EK_Base: |
| 6061 | case InitializedEntity::EK_Delegating: |
| 6062 | case InitializedEntity::EK_VectorElement: |
| 6063 | case InitializedEntity::EK_ComplexElement: |
| 6064 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 6065 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 6066 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 6067 | case InitializedEntity::EK_RelatedResult: |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6068 | return Initializer->getBeginLoc(); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6069 | } |
| 6070 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 6071 | } |
| 6072 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6073 | /// Make a (potentially elidable) temporary copy of the object |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 6074 | /// provided by the given initializer by calling the appropriate copy |
| 6075 | /// constructor. |
| 6076 | /// |
| 6077 | /// \param S The Sema object used for type-checking. |
| 6078 | /// |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 6079 | /// \param T The type of the temporary object, which must either be |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 6080 | /// the type of the initializer expression or a superclass thereof. |
| 6081 | /// |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 6082 | /// \param Entity The entity being initialized. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 6083 | /// |
| 6084 | /// \param CurInit The initializer expression. |
| 6085 | /// |
| 6086 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 6087 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 6088 | /// an rvalue. |
| 6089 | /// |
| 6090 | /// \returns An expression that copies the initializer expression into |
| 6091 | /// a temporary object, or an error expression if a copy could not be |
| 6092 | /// created. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6093 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 6094 | QualType T, |
| 6095 | const InitializedEntity &Entity, |
| 6096 | ExprResult CurInit, |
| 6097 | bool IsExtraneousCopy) { |
Fariborz Jahanian | 36f7f13 | 2015-01-28 22:08:10 +0000 | [diff] [blame] | 6098 | if (CurInit.isInvalid()) |
| 6099 | return CurInit; |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 6100 | // Determine which class type we're copying to. |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 6101 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6102 | CXXRecordDecl *Class = nullptr; |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 6103 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 6104 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 6105 | if (!Class) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6106 | return CurInit; |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 6107 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6108 | SourceLocation Loc = getInitializationLoc(Entity, CurInit.get()); |
Douglas Gregor | d5c231e | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 6109 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6110 | // Make sure that the type we are copying is complete. |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 6111 | if (S.RequireCompleteType(Loc, T, diag::err_temp_copy_incomplete)) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6112 | return CurInit; |
Douglas Gregor | d5c231e | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 6113 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 6114 | // Perform overload resolution using the class's constructors. Per |
| 6115 | // C++11 [dcl.init]p16, second bullet for class types, this initialization |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6116 | // is direct-initialization. |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 6117 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 6118 | DeclContext::lookup_result Ctors = S.LookupConstructors(Class); |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 6119 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6120 | OverloadCandidateSet::iterator Best; |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 6121 | switch (ResolveConstructorOverload( |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 6122 | S, Loc, CurInitExpr, CandidateSet, T, Ctors, Best, |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 6123 | /*CopyInitializing=*/false, /*AllowExplicit=*/true, |
| 6124 | /*OnlyListConstructors=*/false, /*IsListInit=*/false, |
| 6125 | /*SecondStepOfCopyInit=*/true)) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6126 | case OR_Success: |
| 6127 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6128 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6129 | case OR_No_Viable_Function: |
David Blaikie | 5e32805 | 2019-05-03 00:44:50 +0000 | [diff] [blame] | 6130 | CandidateSet.NoteCandidates( |
| 6131 | PartialDiagnosticAt( |
| 6132 | Loc, S.PDiag(IsExtraneousCopy && !S.isSFINAEContext() |
| 6133 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 6134 | : diag::err_temp_copy_no_viable) |
| 6135 | << (int)Entity.getKind() << CurInitExpr->getType() |
| 6136 | << CurInitExpr->getSourceRange()), |
| 6137 | S, OCD_AllCandidates, CurInitExpr); |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 6138 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6139 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6140 | return CurInit; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6141 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6142 | case OR_Ambiguous: |
David Blaikie | 5e32805 | 2019-05-03 00:44:50 +0000 | [diff] [blame] | 6143 | CandidateSet.NoteCandidates( |
| 6144 | PartialDiagnosticAt(Loc, S.PDiag(diag::err_temp_copy_ambiguous) |
| 6145 | << (int)Entity.getKind() |
| 6146 | << CurInitExpr->getType() |
| 6147 | << CurInitExpr->getSourceRange()), |
| 6148 | S, OCD_ViableCandidates, CurInitExpr); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6149 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6150 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6151 | case OR_Deleted: |
| 6152 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 6153 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6154 | << CurInitExpr->getSourceRange(); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6155 | S.NoteDeletedFunction(Best->Function); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6156 | return ExprError(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6157 | } |
| 6158 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 6159 | bool HadMultipleCandidates = CandidateSet.size() > 1; |
| 6160 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 6161 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 6162 | SmallVector<Expr*, 8> ConstructorArgs; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6163 | CurInit.get(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 6164 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6165 | S.CheckConstructorAccess(Loc, Constructor, Best->FoundDecl, Entity, |
| 6166 | IsExtraneousCopy); |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 6167 | |
| 6168 | if (IsExtraneousCopy) { |
| 6169 | // If this is a totally extraneous copy for C++03 reference |
| 6170 | // binding purposes, just return the original initialization |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 6171 | // expression. We don't generate an (elided) copy operation here |
| 6172 | // because doing so would require us to pass down a flag to avoid |
| 6173 | // infinite recursion, where each step adds another extraneous, |
| 6174 | // elidable copy. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 6175 | |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 6176 | // Instantiate the default arguments of any extra parameters in |
| 6177 | // the selected copy constructor, as if we were going to create a |
| 6178 | // proper call to the copy constructor. |
| 6179 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 6180 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 6181 | if (S.RequireCompleteType(Loc, Parm->getType(), |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 6182 | diag::err_call_incomplete_argument)) |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 6183 | break; |
| 6184 | |
| 6185 | // Build the default argument expression; we don't actually care |
| 6186 | // if this succeeds or not, because this routine will complain |
| 6187 | // if there was a problem. |
| 6188 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 6189 | } |
| 6190 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6191 | return CurInitExpr; |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 6192 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6193 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 6194 | // Determine the arguments required to actually perform the |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 6195 | // constructor call (we might have derived-to-base conversions, or |
| 6196 | // the copy constructor may have default arguments). |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6197 | if (S.CompleteConstructorCall(Constructor, CurInitExpr, Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6198 | return ExprError(); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 6199 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 6200 | // C++0x [class.copy]p32: |
| 6201 | // When certain criteria are met, an implementation is allowed to |
| 6202 | // omit the copy/move construction of a class object, even if the |
| 6203 | // copy/move constructor and/or destructor for the object have |
| 6204 | // side effects. [...] |
| 6205 | // - when a temporary class object that has not been bound to a |
| 6206 | // reference (12.2) would be copied/moved to a class object |
| 6207 | // with the same cv-unqualified type, the copy/move operation |
| 6208 | // can be omitted by constructing the temporary object |
| 6209 | // directly into the target of the omitted copy/move |
| 6210 | // |
| 6211 | // Note that the other three bullets are handled elsewhere. Copy |
| 6212 | // elision for return statements and throw expressions are handled as part |
| 6213 | // of constructor initialization, while copy elision for exception handlers |
| 6214 | // is handled by the run-time. |
| 6215 | // |
| 6216 | // FIXME: If the function parameter is not the same type as the temporary, we |
| 6217 | // should still be able to elide the copy, but we don't have a way to |
| 6218 | // represent in the AST how much should be elided in this case. |
| 6219 | bool Elidable = |
| 6220 | CurInitExpr->isTemporaryObject(S.Context, Class) && |
| 6221 | S.Context.hasSameUnqualifiedType( |
| 6222 | Best->Function->getParamDecl(0)->getType().getNonReferenceType(), |
| 6223 | CurInitExpr->getType()); |
| 6224 | |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 6225 | // Actually perform the constructor call. |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 6226 | CurInit = S.BuildCXXConstructExpr(Loc, T, Best->FoundDecl, Constructor, |
| 6227 | Elidable, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6228 | ConstructorArgs, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 6229 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6230 | /*ListInit*/ false, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6231 | /*StdInitListInit*/ false, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 6232 | /*ZeroInit*/ false, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 6233 | CXXConstructExpr::CK_Complete, |
| 6234 | SourceRange()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6235 | |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 6236 | // If we're supposed to bind temporaries, do so. |
| 6237 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6238 | CurInit = S.MaybeBindToTemporary(CurInit.getAs<Expr>()); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6239 | return CurInit; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6240 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6241 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6242 | /// Check whether elidable copy construction for binding a reference to |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6243 | /// a temporary would have succeeded if we were building in C++98 mode, for |
| 6244 | /// -Wc++98-compat. |
| 6245 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 6246 | const InitializedEntity &Entity, |
| 6247 | Expr *CurInitExpr) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6248 | assert(S.getLangOpts().CPlusPlus11); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6249 | |
| 6250 | const RecordType *Record = CurInitExpr->getType()->getAs<RecordType>(); |
| 6251 | if (!Record) |
| 6252 | return; |
| 6253 | |
| 6254 | SourceLocation Loc = getInitializationLoc(Entity, CurInitExpr); |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 6255 | if (S.Diags.isIgnored(diag::warn_cxx98_compat_temp_copy, Loc)) |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6256 | return; |
| 6257 | |
| 6258 | // Find constructors which would have been considered. |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 6259 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 6260 | DeclContext::lookup_result Ctors = |
| 6261 | S.LookupConstructors(cast<CXXRecordDecl>(Record->getDecl())); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6262 | |
| 6263 | // Perform overload resolution. |
| 6264 | OverloadCandidateSet::iterator Best; |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 6265 | OverloadingResult OR = ResolveConstructorOverload( |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 6266 | S, Loc, CurInitExpr, CandidateSet, CurInitExpr->getType(), Ctors, Best, |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 6267 | /*CopyInitializing=*/false, /*AllowExplicit=*/true, |
| 6268 | /*OnlyListConstructors=*/false, /*IsListInit=*/false, |
| 6269 | /*SecondStepOfCopyInit=*/true); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6270 | |
| 6271 | PartialDiagnostic Diag = S.PDiag(diag::warn_cxx98_compat_temp_copy) |
| 6272 | << OR << (int)Entity.getKind() << CurInitExpr->getType() |
| 6273 | << CurInitExpr->getSourceRange(); |
| 6274 | |
| 6275 | switch (OR) { |
| 6276 | case OR_Success: |
| 6277 | S.CheckConstructorAccess(Loc, cast<CXXConstructorDecl>(Best->Function), |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6278 | Best->FoundDecl, Entity, Diag); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6279 | // FIXME: Check default arguments as far as that's possible. |
| 6280 | break; |
| 6281 | |
| 6282 | case OR_No_Viable_Function: |
David Blaikie | 5e32805 | 2019-05-03 00:44:50 +0000 | [diff] [blame] | 6283 | CandidateSet.NoteCandidates(PartialDiagnosticAt(Loc, Diag), S, |
| 6284 | OCD_AllCandidates, CurInitExpr); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6285 | break; |
| 6286 | |
| 6287 | case OR_Ambiguous: |
David Blaikie | 5e32805 | 2019-05-03 00:44:50 +0000 | [diff] [blame] | 6288 | CandidateSet.NoteCandidates(PartialDiagnosticAt(Loc, Diag), S, |
| 6289 | OCD_ViableCandidates, CurInitExpr); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6290 | break; |
| 6291 | |
| 6292 | case OR_Deleted: |
| 6293 | S.Diag(Loc, Diag); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6294 | S.NoteDeletedFunction(Best->Function); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6295 | break; |
| 6296 | } |
| 6297 | } |
| 6298 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6299 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 6300 | const InitializedEntity &Entity) { |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6301 | if (Entity.isParameterKind() && Entity.getDecl()) { |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6302 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 6303 | return; |
| 6304 | |
| 6305 | if (Entity.getDecl()->getDeclName()) |
| 6306 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 6307 | << Entity.getDecl()->getDeclName(); |
| 6308 | else |
| 6309 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 6310 | } |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 6311 | else if (Entity.getKind() == InitializedEntity::EK_RelatedResult && |
| 6312 | Entity.getMethodDecl()) |
| 6313 | S.Diag(Entity.getMethodDecl()->getLocation(), |
| 6314 | diag::note_method_return_type_change) |
| 6315 | << Entity.getMethodDecl()->getDeclName(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6316 | } |
| 6317 | |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 6318 | /// Returns true if the parameters describe a constructor initialization of |
| 6319 | /// an explicit temporary object, e.g. "Point(x, y)". |
| 6320 | static bool isExplicitTemporary(const InitializedEntity &Entity, |
| 6321 | const InitializationKind &Kind, |
| 6322 | unsigned NumArgs) { |
| 6323 | switch (Entity.getKind()) { |
| 6324 | case InitializedEntity::EK_Temporary: |
| 6325 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 6326 | case InitializedEntity::EK_RelatedResult: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 6327 | break; |
| 6328 | default: |
| 6329 | return false; |
| 6330 | } |
| 6331 | |
| 6332 | switch (Kind.getKind()) { |
| 6333 | case InitializationKind::IK_DirectList: |
| 6334 | return true; |
| 6335 | // FIXME: Hack to work around cast weirdness. |
| 6336 | case InitializationKind::IK_Direct: |
| 6337 | case InitializationKind::IK_Value: |
| 6338 | return NumArgs != 1; |
| 6339 | default: |
| 6340 | return false; |
| 6341 | } |
| 6342 | } |
| 6343 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6344 | static ExprResult |
| 6345 | PerformConstructorInitialization(Sema &S, |
| 6346 | const InitializedEntity &Entity, |
| 6347 | const InitializationKind &Kind, |
| 6348 | MultiExprArg Args, |
| 6349 | const InitializationSequence::Step& Step, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6350 | bool &ConstructorInitRequiresZeroInit, |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 6351 | bool IsListInitialization, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6352 | bool IsStdInitListInitialization, |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 6353 | SourceLocation LBraceLoc, |
| 6354 | SourceLocation RBraceLoc) { |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6355 | unsigned NumArgs = Args.size(); |
| 6356 | CXXConstructorDecl *Constructor |
| 6357 | = cast<CXXConstructorDecl>(Step.Function.Function); |
| 6358 | bool HadMultipleCandidates = Step.Function.HadMultipleCandidates; |
| 6359 | |
| 6360 | // Build a call to the selected constructor. |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 6361 | SmallVector<Expr*, 8> ConstructorArgs; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6362 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 6363 | ? Kind.getEqualLoc() |
| 6364 | : Kind.getLocation(); |
| 6365 | |
| 6366 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 6367 | // Force even a trivial, implicit default constructor to be |
| 6368 | // semantically checked. We do this explicitly because we don't build |
| 6369 | // the definition for completely trivial constructors. |
Matt Beaumont-Gay | 47ff122 | 2012-02-24 08:37:56 +0000 | [diff] [blame] | 6370 | assert(Constructor->getParent() && "No parent class for constructor."); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6371 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Richard Smith | 26a92d5 | 2019-08-26 18:18:07 +0000 | [diff] [blame] | 6372 | Constructor->isTrivial() && !Constructor->isUsed(false)) { |
| 6373 | S.runWithSufficientStackSpace(Loc, [&] { |
| 6374 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 6375 | }); |
| 6376 | } |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6377 | } |
| 6378 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6379 | ExprResult CurInit((Expr *)nullptr); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6380 | |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6381 | // C++ [over.match.copy]p1: |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6382 | // - When initializing a temporary to be bound to the first parameter |
| 6383 | // of a constructor that takes a reference to possibly cv-qualified |
| 6384 | // T as its first argument, called with a single argument in the |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6385 | // context of direct-initialization, explicit conversion functions |
| 6386 | // are also considered. |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 6387 | bool AllowExplicitConv = |
| 6388 | Kind.AllowExplicit() && !Kind.isCopyInit() && Args.size() == 1 && |
| 6389 | hasCopyOrMoveCtorParam(S.Context, |
| 6390 | getConstructorInfo(Step.Function.FoundDecl)); |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6391 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6392 | // Determine the arguments required to actually perform the constructor |
| 6393 | // call. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6394 | if (S.CompleteConstructorCall(Constructor, Args, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6395 | Loc, ConstructorArgs, |
Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 6396 | AllowExplicitConv, |
| 6397 | IsListInitialization)) |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6398 | return ExprError(); |
| 6399 | |
| 6400 | |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 6401 | if (isExplicitTemporary(Entity, Kind, NumArgs)) { |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6402 | // An explicitly-constructed temporary, e.g., X(1, 2). |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 6403 | if (S.DiagnoseUseOfDecl(Constructor, Loc)) |
| 6404 | return ExprError(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6405 | |
| 6406 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 6407 | if (!TSInfo) |
| 6408 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
Vedant Kumar | c9a9531 | 2018-11-19 20:10:21 +0000 | [diff] [blame] | 6409 | SourceRange ParenOrBraceRange = |
| 6410 | (Kind.getKind() == InitializationKind::IK_DirectList) |
| 6411 | ? SourceRange(LBraceLoc, RBraceLoc) |
| 6412 | : Kind.getParenOrBraceRange(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6413 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6414 | if (auto *Shadow = dyn_cast<ConstructorUsingShadowDecl>( |
Richard Smith | 80a4702 | 2016-06-29 01:10:27 +0000 | [diff] [blame] | 6415 | Step.Function.FoundDecl.getDecl())) { |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6416 | Constructor = S.findInheritingConstructor(Loc, Constructor, Shadow); |
Richard Smith | 80a4702 | 2016-06-29 01:10:27 +0000 | [diff] [blame] | 6417 | if (S.DiagnoseUseOfDecl(Constructor, Loc)) |
| 6418 | return ExprError(); |
| 6419 | } |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6420 | S.MarkFunctionReferenced(Loc, Constructor); |
| 6421 | |
Bruno Ricci | ddb8f6b | 2018-12-22 14:39:30 +0000 | [diff] [blame] | 6422 | CurInit = CXXTemporaryObjectExpr::Create( |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 6423 | S.Context, Constructor, |
| 6424 | Entity.getType().getNonLValueExprType(S.Context), TSInfo, |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 6425 | ConstructorArgs, ParenOrBraceRange, HadMultipleCandidates, |
| 6426 | IsListInitialization, IsStdInitListInitialization, |
| 6427 | ConstructorInitRequiresZeroInit); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6428 | } else { |
| 6429 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 6430 | CXXConstructExpr::CK_Complete; |
| 6431 | |
| 6432 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 6433 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
| 6434 | CXXConstructExpr::CK_VirtualBase : |
| 6435 | CXXConstructExpr::CK_NonVirtualBase; |
| 6436 | } else if (Entity.getKind() == InitializedEntity::EK_Delegating) { |
| 6437 | ConstructKind = CXXConstructExpr::CK_Delegating; |
| 6438 | } |
| 6439 | |
Peter Collingbourne | b8d17e7 | 2014-02-22 02:59:41 +0000 | [diff] [blame] | 6440 | // Only get the parenthesis or brace range if it is a list initialization or |
| 6441 | // direct construction. |
| 6442 | SourceRange ParenOrBraceRange; |
| 6443 | if (IsListInitialization) |
| 6444 | ParenOrBraceRange = SourceRange(LBraceLoc, RBraceLoc); |
| 6445 | else if (Kind.getKind() == InitializationKind::IK_Direct) |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 6446 | ParenOrBraceRange = Kind.getParenOrBraceRange(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6447 | |
| 6448 | // If the entity allows NRVO, mark the construction as elidable |
| 6449 | // unconditionally. |
| 6450 | if (Entity.allowsNRVO()) |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 6451 | CurInit = S.BuildCXXConstructExpr(Loc, Step.Type, |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 6452 | Step.Function.FoundDecl, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6453 | Constructor, /*Elidable=*/true, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6454 | ConstructorArgs, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6455 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6456 | IsListInitialization, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6457 | IsStdInitListInitialization, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6458 | ConstructorInitRequiresZeroInit, |
| 6459 | ConstructKind, |
Peter Collingbourne | b8d17e7 | 2014-02-22 02:59:41 +0000 | [diff] [blame] | 6460 | ParenOrBraceRange); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6461 | else |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 6462 | CurInit = S.BuildCXXConstructExpr(Loc, Step.Type, |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 6463 | Step.Function.FoundDecl, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6464 | Constructor, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6465 | ConstructorArgs, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6466 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6467 | IsListInitialization, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6468 | IsStdInitListInitialization, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6469 | ConstructorInitRequiresZeroInit, |
| 6470 | ConstructKind, |
Peter Collingbourne | b8d17e7 | 2014-02-22 02:59:41 +0000 | [diff] [blame] | 6471 | ParenOrBraceRange); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6472 | } |
| 6473 | if (CurInit.isInvalid()) |
| 6474 | return ExprError(); |
| 6475 | |
| 6476 | // Only check access if all of that succeeded. |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6477 | S.CheckConstructorAccess(Loc, Constructor, Step.Function.FoundDecl, Entity); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 6478 | if (S.DiagnoseUseOfDecl(Step.Function.FoundDecl, Loc)) |
| 6479 | return ExprError(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6480 | |
Erik Pilkington | f8ccf05 | 2019-05-10 17:52:26 +0000 | [diff] [blame] | 6481 | if (const ArrayType *AT = S.Context.getAsArrayType(Entity.getType())) |
| 6482 | if (checkDestructorReference(S.Context.getBaseElementType(AT), Loc, S)) |
| 6483 | return ExprError(); |
| 6484 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6485 | if (shouldBindAsTemporary(Entity)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6486 | CurInit = S.MaybeBindToTemporary(CurInit.get()); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6487 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6488 | return CurInit; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6489 | } |
| 6490 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6491 | namespace { |
| 6492 | enum LifetimeKind { |
| 6493 | /// The lifetime of a temporary bound to this entity ends at the end of the |
| 6494 | /// full-expression, and that's (probably) fine. |
| 6495 | LK_FullExpression, |
| 6496 | |
| 6497 | /// The lifetime of a temporary bound to this entity is extended to the |
| 6498 | /// lifeitme of the entity itself. |
| 6499 | LK_Extended, |
| 6500 | |
| 6501 | /// The lifetime of a temporary bound to this entity probably ends too soon, |
| 6502 | /// because the entity is allocated in a new-expression. |
| 6503 | LK_New, |
| 6504 | |
| 6505 | /// The lifetime of a temporary bound to this entity ends too soon, because |
| 6506 | /// the entity is a return object. |
| 6507 | LK_Return, |
| 6508 | |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 6509 | /// The lifetime of a temporary bound to this entity ends too soon, because |
| 6510 | /// the entity is the result of a statement expression. |
| 6511 | LK_StmtExprResult, |
| 6512 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6513 | /// This is a mem-initializer: if it would extend a temporary (other than via |
| 6514 | /// a default member initializer), the program is ill-formed. |
| 6515 | LK_MemInitializer, |
| 6516 | }; |
| 6517 | using LifetimeResult = |
| 6518 | llvm::PointerIntPair<const InitializedEntity *, 3, LifetimeKind>; |
| 6519 | } |
| 6520 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6521 | /// Determine the declaration which an initialized entity ultimately refers to, |
| 6522 | /// for the purpose of lifetime-extending a temporary bound to a reference in |
| 6523 | /// the initialization of \p Entity. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6524 | static LifetimeResult getEntityLifetime( |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 6525 | const InitializedEntity *Entity, |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6526 | const InitializedEntity *InitField = nullptr) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6527 | // C++11 [class.temporary]p5: |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 6528 | switch (Entity->getKind()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6529 | case InitializedEntity::EK_Variable: |
| 6530 | // The temporary [...] persists for the lifetime of the reference |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6531 | return {Entity, LK_Extended}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6532 | |
| 6533 | case InitializedEntity::EK_Member: |
| 6534 | // For subobjects, we look at the complete object. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 6535 | if (Entity->getParent()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6536 | return getEntityLifetime(Entity->getParent(), Entity); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6537 | |
| 6538 | // except: |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6539 | // C++17 [class.base.init]p8: |
| 6540 | // A temporary expression bound to a reference member in a |
| 6541 | // mem-initializer is ill-formed. |
| 6542 | // C++17 [class.base.init]p11: |
| 6543 | // A temporary expression bound to a reference member from a |
| 6544 | // default member initializer is ill-formed. |
| 6545 | // |
| 6546 | // The context of p11 and its example suggest that it's only the use of a |
| 6547 | // default member initializer from a constructor that makes the program |
| 6548 | // ill-formed, not its mere existence, and that it can even be used by |
| 6549 | // aggregate initialization. |
| 6550 | return {Entity, Entity->isDefaultMemberInitializer() ? LK_Extended |
| 6551 | : LK_MemInitializer}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6552 | |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 6553 | case InitializedEntity::EK_Binding: |
Richard Smith | 3997b1b | 2016-08-12 01:55:21 +0000 | [diff] [blame] | 6554 | // Per [dcl.decomp]p3, the binding is treated as a variable of reference |
| 6555 | // type. |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6556 | return {Entity, LK_Extended}; |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 6557 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6558 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6559 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6560 | // -- A temporary bound to a reference parameter in a function call |
| 6561 | // persists until the completion of the full-expression containing |
| 6562 | // the call. |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6563 | return {nullptr, LK_FullExpression}; |
| 6564 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6565 | case InitializedEntity::EK_Result: |
| 6566 | // -- The lifetime of a temporary bound to the returned value in a |
| 6567 | // function return statement is not extended; the temporary is |
| 6568 | // destroyed at the end of the full-expression in the return statement. |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6569 | return {nullptr, LK_Return}; |
| 6570 | |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 6571 | case InitializedEntity::EK_StmtExprResult: |
| 6572 | // FIXME: Should we lifetime-extend through the result of a statement |
| 6573 | // expression? |
| 6574 | return {nullptr, LK_StmtExprResult}; |
| 6575 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6576 | case InitializedEntity::EK_New: |
| 6577 | // -- A temporary bound to a reference in a new-initializer persists |
| 6578 | // until the completion of the full-expression containing the |
| 6579 | // new-initializer. |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6580 | return {nullptr, LK_New}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6581 | |
| 6582 | case InitializedEntity::EK_Temporary: |
| 6583 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 6584 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6585 | // We don't yet know the storage duration of the surrounding temporary. |
| 6586 | // Assume it's got full-expression duration for now, it will patch up our |
| 6587 | // storage duration if that's not correct. |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6588 | return {nullptr, LK_FullExpression}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6589 | |
| 6590 | case InitializedEntity::EK_ArrayElement: |
| 6591 | // For subobjects, we look at the complete object. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6592 | return getEntityLifetime(Entity->getParent(), InitField); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6593 | |
| 6594 | case InitializedEntity::EK_Base: |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 6595 | // For subobjects, we look at the complete object. |
| 6596 | if (Entity->getParent()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6597 | return getEntityLifetime(Entity->getParent(), InitField); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6598 | return {InitField, LK_MemInitializer}; |
| 6599 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6600 | case InitializedEntity::EK_Delegating: |
| 6601 | // We can reach this case for aggregate initialization in a constructor: |
| 6602 | // struct A { int &&r; }; |
| 6603 | // struct B : A { B() : A{0} {} }; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6604 | // In this case, use the outermost field decl as the context. |
| 6605 | return {InitField, LK_MemInitializer}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6606 | |
| 6607 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 6608 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6609 | case InitializedEntity::EK_LambdaCapture: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6610 | case InitializedEntity::EK_VectorElement: |
| 6611 | case InitializedEntity::EK_ComplexElement: |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6612 | return {nullptr, LK_FullExpression}; |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6613 | |
| 6614 | case InitializedEntity::EK_Exception: |
| 6615 | // FIXME: Can we diagnose lifetime problems with exceptions? |
| 6616 | return {nullptr, LK_FullExpression}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6617 | } |
Benjamin Kramer | cabc882 | 2013-06-05 15:37:50 +0000 | [diff] [blame] | 6618 | llvm_unreachable("unknown entity kind"); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6619 | } |
| 6620 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6621 | namespace { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6622 | enum ReferenceKind { |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6623 | /// Lifetime would be extended by a reference binding to a temporary. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6624 | RK_ReferenceBinding, |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6625 | /// Lifetime would be extended by a std::initializer_list object binding to |
| 6626 | /// its backing array. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6627 | RK_StdInitializerList, |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6628 | }; |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6629 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6630 | /// A temporary or local variable. This will be one of: |
| 6631 | /// * A MaterializeTemporaryExpr. |
| 6632 | /// * A DeclRefExpr whose declaration is a local. |
| 6633 | /// * An AddrLabelExpr. |
| 6634 | /// * A BlockExpr for a block with captures. |
| 6635 | using Local = Expr*; |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6636 | |
| 6637 | /// Expressions we stepped over when looking for the local state. Any steps |
| 6638 | /// that would inhibit lifetime extension or take us out of subexpressions of |
| 6639 | /// the initializer are included. |
| 6640 | struct IndirectLocalPathEntry { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6641 | enum EntryKind { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6642 | DefaultInit, |
| 6643 | AddressOf, |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6644 | VarInit, |
| 6645 | LValToRVal, |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6646 | LifetimeBoundCall, |
Gabor Horvath | e5e10b5 | 2019-08-06 19:13:29 +0000 | [diff] [blame] | 6647 | GslPointerInit |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6648 | } Kind; |
| 6649 | Expr *E; |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6650 | const Decl *D = nullptr; |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6651 | IndirectLocalPathEntry() {} |
| 6652 | IndirectLocalPathEntry(EntryKind K, Expr *E) : Kind(K), E(E) {} |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6653 | IndirectLocalPathEntry(EntryKind K, Expr *E, const Decl *D) |
| 6654 | : Kind(K), E(E), D(D) {} |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6655 | }; |
| 6656 | |
| 6657 | using IndirectLocalPath = llvm::SmallVectorImpl<IndirectLocalPathEntry>; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6658 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6659 | struct RevertToOldSizeRAII { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6660 | IndirectLocalPath &Path; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6661 | unsigned OldSize = Path.size(); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6662 | RevertToOldSizeRAII(IndirectLocalPath &Path) : Path(Path) {} |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6663 | ~RevertToOldSizeRAII() { Path.resize(OldSize); } |
| 6664 | }; |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6665 | |
| 6666 | using LocalVisitor = llvm::function_ref<bool(IndirectLocalPath &Path, Local L, |
| 6667 | ReferenceKind RK)>; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6668 | } |
| 6669 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6670 | static bool isVarOnPath(IndirectLocalPath &Path, VarDecl *VD) { |
| 6671 | for (auto E : Path) |
| 6672 | if (E.Kind == IndirectLocalPathEntry::VarInit && E.D == VD) |
| 6673 | return true; |
| 6674 | return false; |
| 6675 | } |
| 6676 | |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6677 | static bool pathContainsInit(IndirectLocalPath &Path) { |
Fangrui Song | 3117b17 | 2018-10-20 17:53:42 +0000 | [diff] [blame] | 6678 | return llvm::any_of(Path, [=](IndirectLocalPathEntry E) { |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6679 | return E.Kind == IndirectLocalPathEntry::DefaultInit || |
| 6680 | E.Kind == IndirectLocalPathEntry::VarInit; |
| 6681 | }); |
| 6682 | } |
| 6683 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6684 | static void visitLocalsRetainedByInitializer(IndirectLocalPath &Path, |
| 6685 | Expr *Init, LocalVisitor Visit, |
Gabor Horvath | 6379e5c | 2019-08-23 22:21:33 +0000 | [diff] [blame] | 6686 | bool RevisitSubinits, |
| 6687 | bool EnableLifetimeWarnings); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6688 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6689 | static void visitLocalsRetainedByReferenceBinding(IndirectLocalPath &Path, |
| 6690 | Expr *Init, ReferenceKind RK, |
Gabor Horvath | 6379e5c | 2019-08-23 22:21:33 +0000 | [diff] [blame] | 6691 | LocalVisitor Visit, |
| 6692 | bool EnableLifetimeWarnings); |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6693 | |
Gabor Horvath | e5e10b5 | 2019-08-06 19:13:29 +0000 | [diff] [blame] | 6694 | template <typename T> static bool isRecordWithAttr(QualType Type) { |
| 6695 | if (auto *RD = Type->getAsCXXRecordDecl()) |
Matthias Gehre | f64f488 | 2019-09-06 08:56:30 +0000 | [diff] [blame] | 6696 | return RD->hasAttr<T>(); |
Gabor Horvath | e5e10b5 | 2019-08-06 19:13:29 +0000 | [diff] [blame] | 6697 | return false; |
| 6698 | } |
| 6699 | |
Gabor Horvath | bfe0c37 | 2019-08-14 16:34:56 +0000 | [diff] [blame] | 6700 | // Decl::isInStdNamespace will return false for iterators in some STL |
| 6701 | // implementations due to them being defined in a namespace outside of the std |
| 6702 | // namespace. |
| 6703 | static bool isInStlNamespace(const Decl *D) { |
| 6704 | const DeclContext *DC = D->getDeclContext(); |
| 6705 | if (!DC) |
| 6706 | return false; |
| 6707 | if (const auto *ND = dyn_cast<NamespaceDecl>(DC)) |
| 6708 | if (const IdentifierInfo *II = ND->getIdentifier()) { |
| 6709 | StringRef Name = II->getName(); |
| 6710 | if (Name.size() >= 2 && Name.front() == '_' && |
| 6711 | (Name[1] == '_' || isUppercase(Name[1]))) |
| 6712 | return true; |
| 6713 | } |
| 6714 | |
| 6715 | return DC->isStdNamespace(); |
| 6716 | } |
| 6717 | |
Gabor Horvath | c1dafd7 | 2019-08-09 15:16:35 +0000 | [diff] [blame] | 6718 | static bool shouldTrackImplicitObjectArg(const CXXMethodDecl *Callee) { |
| 6719 | if (auto *Conv = dyn_cast_or_null<CXXConversionDecl>(Callee)) |
| 6720 | if (isRecordWithAttr<PointerAttr>(Conv->getConversionType())) |
| 6721 | return true; |
Gabor Horvath | bfe0c37 | 2019-08-14 16:34:56 +0000 | [diff] [blame] | 6722 | if (!isInStlNamespace(Callee->getParent())) |
Gabor Horvath | c1dafd7 | 2019-08-09 15:16:35 +0000 | [diff] [blame] | 6723 | return false; |
| 6724 | if (!isRecordWithAttr<PointerAttr>(Callee->getThisObjectType()) && |
| 6725 | !isRecordWithAttr<OwnerAttr>(Callee->getThisObjectType())) |
| 6726 | return false; |
Gabor Horvath | 795c366 | 2019-08-09 23:03:50 +0000 | [diff] [blame] | 6727 | if (Callee->getReturnType()->isPointerType() || |
| 6728 | isRecordWithAttr<PointerAttr>(Callee->getReturnType())) { |
| 6729 | if (!Callee->getIdentifier()) |
| 6730 | return false; |
| 6731 | return llvm::StringSwitch<bool>(Callee->getName()) |
| 6732 | .Cases("begin", "rbegin", "cbegin", "crbegin", true) |
| 6733 | .Cases("end", "rend", "cend", "crend", true) |
| 6734 | .Cases("c_str", "data", "get", true) |
| 6735 | // Map and set types. |
| 6736 | .Cases("find", "equal_range", "lower_bound", "upper_bound", true) |
| 6737 | .Default(false); |
| 6738 | } else if (Callee->getReturnType()->isReferenceType()) { |
| 6739 | if (!Callee->getIdentifier()) { |
| 6740 | auto OO = Callee->getOverloadedOperator(); |
| 6741 | return OO == OverloadedOperatorKind::OO_Subscript || |
| 6742 | OO == OverloadedOperatorKind::OO_Star; |
| 6743 | } |
| 6744 | return llvm::StringSwitch<bool>(Callee->getName()) |
Matthias Gehre | 1bebc22 | 2019-08-14 21:55:57 +0000 | [diff] [blame] | 6745 | .Cases("front", "back", "at", "top", "value", true) |
Gabor Horvath | 795c366 | 2019-08-09 23:03:50 +0000 | [diff] [blame] | 6746 | .Default(false); |
| 6747 | } |
| 6748 | return false; |
Gabor Horvath | c1dafd7 | 2019-08-09 15:16:35 +0000 | [diff] [blame] | 6749 | } |
| 6750 | |
Gabor Horvath | eaee4de | 2019-08-20 16:45:06 +0000 | [diff] [blame] | 6751 | static bool shouldTrackFirstArgument(const FunctionDecl *FD) { |
| 6752 | if (!FD->getIdentifier() || FD->getNumParams() != 1) |
| 6753 | return false; |
| 6754 | const auto *RD = FD->getParamDecl(0)->getType()->getPointeeCXXRecordDecl(); |
| 6755 | if (!FD->isInStdNamespace() || !RD || !RD->isInStdNamespace()) |
| 6756 | return false; |
| 6757 | if (!isRecordWithAttr<PointerAttr>(QualType(RD->getTypeForDecl(), 0)) && |
| 6758 | !isRecordWithAttr<OwnerAttr>(QualType(RD->getTypeForDecl(), 0))) |
| 6759 | return false; |
| 6760 | if (FD->getReturnType()->isPointerType() || |
| 6761 | isRecordWithAttr<PointerAttr>(FD->getReturnType())) { |
| 6762 | return llvm::StringSwitch<bool>(FD->getName()) |
| 6763 | .Cases("begin", "rbegin", "cbegin", "crbegin", true) |
| 6764 | .Cases("end", "rend", "cend", "crend", true) |
| 6765 | .Case("data", true) |
| 6766 | .Default(false); |
| 6767 | } else if (FD->getReturnType()->isReferenceType()) { |
| 6768 | return llvm::StringSwitch<bool>(FD->getName()) |
| 6769 | .Cases("get", "any_cast", true) |
| 6770 | .Default(false); |
| 6771 | } |
| 6772 | return false; |
| 6773 | } |
| 6774 | |
Gabor Horvath | e5e10b5 | 2019-08-06 19:13:29 +0000 | [diff] [blame] | 6775 | static void handleGslAnnotatedTypes(IndirectLocalPath &Path, Expr *Call, |
| 6776 | LocalVisitor Visit) { |
| 6777 | auto VisitPointerArg = [&](const Decl *D, Expr *Arg) { |
Gabor Horvath | 6f98400 | 2019-09-03 16:17:24 +0000 | [diff] [blame] | 6778 | // We are not interested in the temporary base objects of gsl Pointers: |
| 6779 | // Temp().ptr; // Here ptr might not dangle. |
| 6780 | if (isa<MemberExpr>(Arg->IgnoreImpCasts())) |
| 6781 | return; |
Gabor Horvath | e5e10b5 | 2019-08-06 19:13:29 +0000 | [diff] [blame] | 6782 | Path.push_back({IndirectLocalPathEntry::GslPointerInit, Arg, D}); |
| 6783 | if (Arg->isGLValue()) |
| 6784 | visitLocalsRetainedByReferenceBinding(Path, Arg, RK_ReferenceBinding, |
Gabor Horvath | 3ba0f3c9 | 2019-08-26 17:03:01 +0000 | [diff] [blame] | 6785 | Visit, |
| 6786 | /*EnableLifetimeWarnings=*/true); |
Gabor Horvath | e5e10b5 | 2019-08-06 19:13:29 +0000 | [diff] [blame] | 6787 | else |
Gabor Horvath | 3ba0f3c9 | 2019-08-26 17:03:01 +0000 | [diff] [blame] | 6788 | visitLocalsRetainedByInitializer(Path, Arg, Visit, true, |
| 6789 | /*EnableLifetimeWarnings=*/true); |
Gabor Horvath | e5e10b5 | 2019-08-06 19:13:29 +0000 | [diff] [blame] | 6790 | Path.pop_back(); |
| 6791 | }; |
| 6792 | |
| 6793 | if (auto *MCE = dyn_cast<CXXMemberCallExpr>(Call)) { |
Gabor Horvath | c1dafd7 | 2019-08-09 15:16:35 +0000 | [diff] [blame] | 6794 | const auto *MD = cast_or_null<CXXMethodDecl>(MCE->getDirectCallee()); |
| 6795 | if (MD && shouldTrackImplicitObjectArg(MD)) |
| 6796 | VisitPointerArg(MD, MCE->getImplicitObjectArgument()); |
Gabor Horvath | e5e10b5 | 2019-08-06 19:13:29 +0000 | [diff] [blame] | 6797 | return; |
Gabor Horvath | 795c366 | 2019-08-09 23:03:50 +0000 | [diff] [blame] | 6798 | } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(Call)) { |
| 6799 | FunctionDecl *Callee = OCE->getDirectCallee(); |
| 6800 | if (Callee && Callee->isCXXInstanceMember() && |
| 6801 | shouldTrackImplicitObjectArg(cast<CXXMethodDecl>(Callee))) |
| 6802 | VisitPointerArg(Callee, OCE->getArg(0)); |
| 6803 | return; |
Gabor Horvath | eaee4de | 2019-08-20 16:45:06 +0000 | [diff] [blame] | 6804 | } else if (auto *CE = dyn_cast<CallExpr>(Call)) { |
| 6805 | FunctionDecl *Callee = CE->getDirectCallee(); |
| 6806 | if (Callee && shouldTrackFirstArgument(Callee)) |
| 6807 | VisitPointerArg(Callee, CE->getArg(0)); |
| 6808 | return; |
Gabor Horvath | e5e10b5 | 2019-08-06 19:13:29 +0000 | [diff] [blame] | 6809 | } |
| 6810 | |
| 6811 | if (auto *CCE = dyn_cast<CXXConstructExpr>(Call)) { |
| 6812 | const auto *Ctor = CCE->getConstructor(); |
Matthias Gehre | f64f488 | 2019-09-06 08:56:30 +0000 | [diff] [blame] | 6813 | const CXXRecordDecl *RD = Ctor->getParent(); |
Gabor Horvath | e5e10b5 | 2019-08-06 19:13:29 +0000 | [diff] [blame] | 6814 | if (CCE->getNumArgs() > 0 && RD->hasAttr<PointerAttr>()) |
| 6815 | VisitPointerArg(Ctor->getParamDecl(0), CCE->getArgs()[0]); |
| 6816 | } |
| 6817 | } |
| 6818 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6819 | static bool implicitObjectParamIsLifetimeBound(const FunctionDecl *FD) { |
| 6820 | const TypeSourceInfo *TSI = FD->getTypeSourceInfo(); |
| 6821 | if (!TSI) |
| 6822 | return false; |
Martin Storsjo | d03fa99 | 2018-08-02 18:12:08 +0000 | [diff] [blame] | 6823 | // Don't declare this variable in the second operand of the for-statement; |
| 6824 | // GCC miscompiles that by ending its lifetime before evaluating the |
| 6825 | // third operand. See gcc.gnu.org/PR86769. |
| 6826 | AttributedTypeLoc ATL; |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6827 | for (TypeLoc TL = TSI->getTypeLoc(); |
Martin Storsjo | d03fa99 | 2018-08-02 18:12:08 +0000 | [diff] [blame] | 6828 | (ATL = TL.getAsAdjusted<AttributedTypeLoc>()); |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6829 | TL = ATL.getModifiedLoc()) { |
Richard Smith | e43e2b3 | 2018-08-20 21:47:29 +0000 | [diff] [blame] | 6830 | if (ATL.getAttrAs<LifetimeBoundAttr>()) |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6831 | return true; |
| 6832 | } |
| 6833 | return false; |
| 6834 | } |
| 6835 | |
| 6836 | static void visitLifetimeBoundArguments(IndirectLocalPath &Path, Expr *Call, |
| 6837 | LocalVisitor Visit) { |
| 6838 | const FunctionDecl *Callee; |
| 6839 | ArrayRef<Expr*> Args; |
| 6840 | |
| 6841 | if (auto *CE = dyn_cast<CallExpr>(Call)) { |
| 6842 | Callee = CE->getDirectCallee(); |
| 6843 | Args = llvm::makeArrayRef(CE->getArgs(), CE->getNumArgs()); |
| 6844 | } else { |
| 6845 | auto *CCE = cast<CXXConstructExpr>(Call); |
| 6846 | Callee = CCE->getConstructor(); |
| 6847 | Args = llvm::makeArrayRef(CCE->getArgs(), CCE->getNumArgs()); |
| 6848 | } |
| 6849 | if (!Callee) |
| 6850 | return; |
| 6851 | |
| 6852 | Expr *ObjectArg = nullptr; |
| 6853 | if (isa<CXXOperatorCallExpr>(Call) && Callee->isCXXInstanceMember()) { |
| 6854 | ObjectArg = Args[0]; |
| 6855 | Args = Args.slice(1); |
| 6856 | } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(Call)) { |
| 6857 | ObjectArg = MCE->getImplicitObjectArgument(); |
| 6858 | } |
| 6859 | |
| 6860 | auto VisitLifetimeBoundArg = [&](const Decl *D, Expr *Arg) { |
| 6861 | Path.push_back({IndirectLocalPathEntry::LifetimeBoundCall, Arg, D}); |
| 6862 | if (Arg->isGLValue()) |
| 6863 | visitLocalsRetainedByReferenceBinding(Path, Arg, RK_ReferenceBinding, |
Gabor Horvath | 3ba0f3c9 | 2019-08-26 17:03:01 +0000 | [diff] [blame] | 6864 | Visit, |
| 6865 | /*EnableLifetimeWarnings=*/false); |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6866 | else |
Gabor Horvath | 3ba0f3c9 | 2019-08-26 17:03:01 +0000 | [diff] [blame] | 6867 | visitLocalsRetainedByInitializer(Path, Arg, Visit, true, |
| 6868 | /*EnableLifetimeWarnings=*/false); |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6869 | Path.pop_back(); |
| 6870 | }; |
| 6871 | |
| 6872 | if (ObjectArg && implicitObjectParamIsLifetimeBound(Callee)) |
| 6873 | VisitLifetimeBoundArg(Callee, ObjectArg); |
| 6874 | |
| 6875 | for (unsigned I = 0, |
| 6876 | N = std::min<unsigned>(Callee->getNumParams(), Args.size()); |
| 6877 | I != N; ++I) { |
| 6878 | if (Callee->getParamDecl(I)->hasAttr<LifetimeBoundAttr>()) |
| 6879 | VisitLifetimeBoundArg(Callee->getParamDecl(I), Args[I]); |
| 6880 | } |
| 6881 | } |
| 6882 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6883 | /// Visit the locals that would be reachable through a reference bound to the |
| 6884 | /// glvalue expression \c Init. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6885 | static void visitLocalsRetainedByReferenceBinding(IndirectLocalPath &Path, |
| 6886 | Expr *Init, ReferenceKind RK, |
Gabor Horvath | 6379e5c | 2019-08-23 22:21:33 +0000 | [diff] [blame] | 6887 | LocalVisitor Visit, |
| 6888 | bool EnableLifetimeWarnings) { |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6889 | RevertToOldSizeRAII RAII(Path); |
| 6890 | |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 6891 | // Walk past any constructs which we can lifetime-extend across. |
| 6892 | Expr *Old; |
| 6893 | do { |
| 6894 | Old = Init; |
| 6895 | |
Bill Wendling | 7c44da2 | 2018-10-31 03:48:47 +0000 | [diff] [blame] | 6896 | if (auto *FE = dyn_cast<FullExpr>(Init)) |
| 6897 | Init = FE->getSubExpr(); |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6898 | |
Richard Smith | dbc8249 | 2015-01-10 01:28:13 +0000 | [diff] [blame] | 6899 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6900 | // If this is just redundant braces around an initializer, step over it. |
| 6901 | if (ILE->isTransparent()) |
Richard Smith | dbc8249 | 2015-01-10 01:28:13 +0000 | [diff] [blame] | 6902 | Init = ILE->getInit(0); |
Richard Smith | dbc8249 | 2015-01-10 01:28:13 +0000 | [diff] [blame] | 6903 | } |
| 6904 | |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 6905 | // Step over any subobject adjustments; we may have a materialized |
| 6906 | // temporary inside them. |
Richard Smith | 4baaa5a | 2016-12-03 01:14:32 +0000 | [diff] [blame] | 6907 | Init = const_cast<Expr *>(Init->skipRValueSubobjectAdjustments()); |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 6908 | |
| 6909 | // Per current approach for DR1376, look through casts to reference type |
| 6910 | // when performing lifetime extension. |
| 6911 | if (CastExpr *CE = dyn_cast<CastExpr>(Init)) |
| 6912 | if (CE->getSubExpr()->isGLValue()) |
| 6913 | Init = CE->getSubExpr(); |
| 6914 | |
Richard Smith | b3189a1 | 2016-12-05 07:49:14 +0000 | [diff] [blame] | 6915 | // Per the current approach for DR1299, look through array element access |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6916 | // on array glvalues when performing lifetime extension. |
| 6917 | if (auto *ASE = dyn_cast<ArraySubscriptExpr>(Init)) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6918 | Init = ASE->getBase(); |
| 6919 | auto *ICE = dyn_cast<ImplicitCastExpr>(Init); |
| 6920 | if (ICE && ICE->getCastKind() == CK_ArrayToPointerDecay) |
| 6921 | Init = ICE->getSubExpr(); |
| 6922 | else |
| 6923 | // We can't lifetime extend through this but we might still find some |
| 6924 | // retained temporaries. |
Gabor Horvath | 6379e5c | 2019-08-23 22:21:33 +0000 | [diff] [blame] | 6925 | return visitLocalsRetainedByInitializer(Path, Init, Visit, true, |
| 6926 | EnableLifetimeWarnings); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6927 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6928 | |
| 6929 | // Step into CXXDefaultInitExprs so we can diagnose cases where a |
| 6930 | // constructor inherits one as an implicit mem-initializer. |
| 6931 | if (auto *DIE = dyn_cast<CXXDefaultInitExpr>(Init)) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6932 | Path.push_back( |
| 6933 | {IndirectLocalPathEntry::DefaultInit, DIE, DIE->getField()}); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6934 | Init = DIE->getExpr(); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6935 | } |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 6936 | } while (Init != Old); |
| 6937 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6938 | if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(Init)) { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6939 | if (Visit(Path, Local(MTE), RK)) |
| 6940 | visitLocalsRetainedByInitializer(Path, MTE->GetTemporaryExpr(), Visit, |
Gabor Horvath | 6379e5c | 2019-08-23 22:21:33 +0000 | [diff] [blame] | 6941 | true, EnableLifetimeWarnings); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6942 | } |
| 6943 | |
Gabor Horvath | e5e10b5 | 2019-08-06 19:13:29 +0000 | [diff] [blame] | 6944 | if (isa<CallExpr>(Init)) { |
Gabor Horvath | 6379e5c | 2019-08-23 22:21:33 +0000 | [diff] [blame] | 6945 | if (EnableLifetimeWarnings) |
| 6946 | handleGslAnnotatedTypes(Path, Init, Visit); |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6947 | return visitLifetimeBoundArguments(Path, Init, Visit); |
Gabor Horvath | e5e10b5 | 2019-08-06 19:13:29 +0000 | [diff] [blame] | 6948 | } |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6949 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6950 | switch (Init->getStmtClass()) { |
| 6951 | case Stmt::DeclRefExprClass: { |
| 6952 | // If we find the name of a local non-reference parameter, we could have a |
| 6953 | // lifetime problem. |
| 6954 | auto *DRE = cast<DeclRefExpr>(Init); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6955 | auto *VD = dyn_cast<VarDecl>(DRE->getDecl()); |
| 6956 | if (VD && VD->hasLocalStorage() && |
| 6957 | !DRE->refersToEnclosingVariableOrCapture()) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6958 | if (!VD->getType()->isReferenceType()) { |
| 6959 | Visit(Path, Local(DRE), RK); |
| 6960 | } else if (isa<ParmVarDecl>(DRE->getDecl())) { |
| 6961 | // The lifetime of a reference parameter is unknown; assume it's OK |
| 6962 | // for now. |
| 6963 | break; |
| 6964 | } else if (VD->getInit() && !isVarOnPath(Path, VD)) { |
| 6965 | Path.push_back({IndirectLocalPathEntry::VarInit, DRE, VD}); |
| 6966 | visitLocalsRetainedByReferenceBinding(Path, VD->getInit(), |
Gabor Horvath | 6379e5c | 2019-08-23 22:21:33 +0000 | [diff] [blame] | 6967 | RK_ReferenceBinding, Visit, |
| 6968 | EnableLifetimeWarnings); |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6969 | } |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6970 | } |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6971 | break; |
| 6972 | } |
| 6973 | |
| 6974 | case Stmt::UnaryOperatorClass: { |
| 6975 | // The only unary operator that make sense to handle here |
| 6976 | // is Deref. All others don't resolve to a "name." This includes |
| 6977 | // handling all sorts of rvalues passed to a unary operator. |
| 6978 | const UnaryOperator *U = cast<UnaryOperator>(Init); |
| 6979 | if (U->getOpcode() == UO_Deref) |
Gabor Horvath | 6379e5c | 2019-08-23 22:21:33 +0000 | [diff] [blame] | 6980 | visitLocalsRetainedByInitializer(Path, U->getSubExpr(), Visit, true, |
| 6981 | EnableLifetimeWarnings); |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6982 | break; |
| 6983 | } |
| 6984 | |
| 6985 | case Stmt::OMPArraySectionExprClass: { |
Gabor Horvath | 6379e5c | 2019-08-23 22:21:33 +0000 | [diff] [blame] | 6986 | visitLocalsRetainedByInitializer(Path, |
| 6987 | cast<OMPArraySectionExpr>(Init)->getBase(), |
| 6988 | Visit, true, EnableLifetimeWarnings); |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6989 | break; |
| 6990 | } |
| 6991 | |
| 6992 | case Stmt::ConditionalOperatorClass: |
| 6993 | case Stmt::BinaryConditionalOperatorClass: { |
| 6994 | auto *C = cast<AbstractConditionalOperator>(Init); |
| 6995 | if (!C->getTrueExpr()->getType()->isVoidType()) |
Gabor Horvath | 6379e5c | 2019-08-23 22:21:33 +0000 | [diff] [blame] | 6996 | visitLocalsRetainedByReferenceBinding(Path, C->getTrueExpr(), RK, Visit, |
| 6997 | EnableLifetimeWarnings); |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6998 | if (!C->getFalseExpr()->getType()->isVoidType()) |
Gabor Horvath | 6379e5c | 2019-08-23 22:21:33 +0000 | [diff] [blame] | 6999 | visitLocalsRetainedByReferenceBinding(Path, C->getFalseExpr(), RK, Visit, |
| 7000 | EnableLifetimeWarnings); |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7001 | break; |
| 7002 | } |
| 7003 | |
| 7004 | // FIXME: Visit the left-hand side of an -> or ->*. |
| 7005 | |
| 7006 | default: |
| 7007 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 7008 | } |
| 7009 | } |
| 7010 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7011 | /// Visit the locals that would be reachable through an object initialized by |
| 7012 | /// the prvalue expression \c Init. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7013 | static void visitLocalsRetainedByInitializer(IndirectLocalPath &Path, |
| 7014 | Expr *Init, LocalVisitor Visit, |
Gabor Horvath | 6379e5c | 2019-08-23 22:21:33 +0000 | [diff] [blame] | 7015 | bool RevisitSubinits, |
| 7016 | bool EnableLifetimeWarnings) { |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7017 | RevertToOldSizeRAII RAII(Path); |
| 7018 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 7019 | Expr *Old; |
| 7020 | do { |
| 7021 | Old = Init; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7022 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 7023 | // Step into CXXDefaultInitExprs so we can diagnose cases where a |
| 7024 | // constructor inherits one as an implicit mem-initializer. |
| 7025 | if (auto *DIE = dyn_cast<CXXDefaultInitExpr>(Init)) { |
| 7026 | Path.push_back({IndirectLocalPathEntry::DefaultInit, DIE, DIE->getField()}); |
| 7027 | Init = DIE->getExpr(); |
| 7028 | } |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7029 | |
Bill Wendling | 7c44da2 | 2018-10-31 03:48:47 +0000 | [diff] [blame] | 7030 | if (auto *FE = dyn_cast<FullExpr>(Init)) |
| 7031 | Init = FE->getSubExpr(); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 7032 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 7033 | // Dig out the expression which constructs the extended temporary. |
| 7034 | Init = const_cast<Expr *>(Init->skipRValueSubobjectAdjustments()); |
| 7035 | |
| 7036 | if (CXXBindTemporaryExpr *BTE = dyn_cast<CXXBindTemporaryExpr>(Init)) |
| 7037 | Init = BTE->getSubExpr(); |
| 7038 | |
| 7039 | Init = Init->IgnoreParens(); |
| 7040 | |
| 7041 | // Step over value-preserving rvalue casts. |
| 7042 | if (auto *CE = dyn_cast<CastExpr>(Init)) { |
| 7043 | switch (CE->getCastKind()) { |
| 7044 | case CK_LValueToRValue: |
| 7045 | // If we can match the lvalue to a const object, we can look at its |
| 7046 | // initializer. |
| 7047 | Path.push_back({IndirectLocalPathEntry::LValToRVal, CE}); |
| 7048 | return visitLocalsRetainedByReferenceBinding( |
| 7049 | Path, Init, RK_ReferenceBinding, |
| 7050 | [&](IndirectLocalPath &Path, Local L, ReferenceKind RK) -> bool { |
| 7051 | if (auto *DRE = dyn_cast<DeclRefExpr>(L)) { |
| 7052 | auto *VD = dyn_cast<VarDecl>(DRE->getDecl()); |
| 7053 | if (VD && VD->getType().isConstQualified() && VD->getInit() && |
| 7054 | !isVarOnPath(Path, VD)) { |
| 7055 | Path.push_back({IndirectLocalPathEntry::VarInit, DRE, VD}); |
Gabor Horvath | 6379e5c | 2019-08-23 22:21:33 +0000 | [diff] [blame] | 7056 | visitLocalsRetainedByInitializer(Path, VD->getInit(), Visit, true, |
| 7057 | EnableLifetimeWarnings); |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 7058 | } |
| 7059 | } else if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(L)) { |
| 7060 | if (MTE->getType().isConstQualified()) |
| 7061 | visitLocalsRetainedByInitializer(Path, MTE->GetTemporaryExpr(), |
Gabor Horvath | 6379e5c | 2019-08-23 22:21:33 +0000 | [diff] [blame] | 7062 | Visit, true, |
| 7063 | EnableLifetimeWarnings); |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 7064 | } |
| 7065 | return false; |
Gabor Horvath | 6379e5c | 2019-08-23 22:21:33 +0000 | [diff] [blame] | 7066 | }, EnableLifetimeWarnings); |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 7067 | |
| 7068 | // We assume that objects can be retained by pointers cast to integers, |
| 7069 | // but not if the integer is cast to floating-point type or to _Complex. |
| 7070 | // We assume that casts to 'bool' do not preserve enough information to |
| 7071 | // retain a local object. |
| 7072 | case CK_NoOp: |
| 7073 | case CK_BitCast: |
| 7074 | case CK_BaseToDerived: |
| 7075 | case CK_DerivedToBase: |
| 7076 | case CK_UncheckedDerivedToBase: |
| 7077 | case CK_Dynamic: |
| 7078 | case CK_ToUnion: |
| 7079 | case CK_UserDefinedConversion: |
| 7080 | case CK_ConstructorConversion: |
| 7081 | case CK_IntegralToPointer: |
| 7082 | case CK_PointerToIntegral: |
| 7083 | case CK_VectorSplat: |
| 7084 | case CK_IntegralCast: |
| 7085 | case CK_CPointerToObjCPointerCast: |
| 7086 | case CK_BlockPointerToObjCPointerCast: |
| 7087 | case CK_AnyPointerToBlockPointerCast: |
| 7088 | case CK_AddressSpaceConversion: |
| 7089 | break; |
| 7090 | |
| 7091 | case CK_ArrayToPointerDecay: |
| 7092 | // Model array-to-pointer decay as taking the address of the array |
| 7093 | // lvalue. |
| 7094 | Path.push_back({IndirectLocalPathEntry::AddressOf, CE}); |
| 7095 | return visitLocalsRetainedByReferenceBinding(Path, CE->getSubExpr(), |
Gabor Horvath | 6379e5c | 2019-08-23 22:21:33 +0000 | [diff] [blame] | 7096 | RK_ReferenceBinding, Visit, |
| 7097 | EnableLifetimeWarnings); |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 7098 | |
| 7099 | default: |
| 7100 | return; |
| 7101 | } |
| 7102 | |
| 7103 | Init = CE->getSubExpr(); |
| 7104 | } |
| 7105 | } while (Old != Init); |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 7106 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7107 | // C++17 [dcl.init.list]p6: |
| 7108 | // initializing an initializer_list object from the array extends the |
| 7109 | // lifetime of the array exactly like binding a reference to a temporary. |
| 7110 | if (auto *ILE = dyn_cast<CXXStdInitializerListExpr>(Init)) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7111 | return visitLocalsRetainedByReferenceBinding(Path, ILE->getSubExpr(), |
Gabor Horvath | 6379e5c | 2019-08-23 22:21:33 +0000 | [diff] [blame] | 7112 | RK_StdInitializerList, Visit, |
| 7113 | EnableLifetimeWarnings); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 7114 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 7115 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7116 | // We already visited the elements of this initializer list while |
| 7117 | // performing the initialization. Don't visit them again unless we've |
| 7118 | // changed the lifetime of the initialized entity. |
| 7119 | if (!RevisitSubinits) |
| 7120 | return; |
| 7121 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7122 | if (ILE->isTransparent()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7123 | return visitLocalsRetainedByInitializer(Path, ILE->getInit(0), Visit, |
Gabor Horvath | 6379e5c | 2019-08-23 22:21:33 +0000 | [diff] [blame] | 7124 | RevisitSubinits, |
| 7125 | EnableLifetimeWarnings); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7126 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 7127 | if (ILE->getType()->isArrayType()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 7128 | for (unsigned I = 0, N = ILE->getNumInits(); I != N; ++I) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7129 | visitLocalsRetainedByInitializer(Path, ILE->getInit(I), Visit, |
Gabor Horvath | 6379e5c | 2019-08-23 22:21:33 +0000 | [diff] [blame] | 7130 | RevisitSubinits, |
| 7131 | EnableLifetimeWarnings); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 7132 | return; |
| 7133 | } |
| 7134 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 7135 | if (CXXRecordDecl *RD = ILE->getType()->getAsCXXRecordDecl()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 7136 | assert(RD->isAggregate() && "aggregate init on non-aggregate"); |
| 7137 | |
| 7138 | // If we lifetime-extend a braced initializer which is initializing an |
| 7139 | // aggregate, and that aggregate contains reference members which are |
| 7140 | // bound to temporaries, those temporaries are also lifetime-extended. |
| 7141 | if (RD->isUnion() && ILE->getInitializedFieldInUnion() && |
| 7142 | ILE->getInitializedFieldInUnion()->getType()->isReferenceType()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7143 | visitLocalsRetainedByReferenceBinding(Path, ILE->getInit(0), |
Gabor Horvath | 6379e5c | 2019-08-23 22:21:33 +0000 | [diff] [blame] | 7144 | RK_ReferenceBinding, Visit, |
| 7145 | EnableLifetimeWarnings); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 7146 | else { |
| 7147 | unsigned Index = 0; |
Richard Smith | c69cc84 | 2019-06-12 18:32:22 +0000 | [diff] [blame] | 7148 | for (; Index < RD->getNumBases() && Index < ILE->getNumInits(); ++Index) |
| 7149 | visitLocalsRetainedByInitializer(Path, ILE->getInit(Index), Visit, |
Gabor Horvath | 6379e5c | 2019-08-23 22:21:33 +0000 | [diff] [blame] | 7150 | RevisitSubinits, |
| 7151 | EnableLifetimeWarnings); |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 7152 | for (const auto *I : RD->fields()) { |
Richard Smith | 0bca59d | 2013-07-01 06:08:20 +0000 | [diff] [blame] | 7153 | if (Index >= ILE->getNumInits()) |
| 7154 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 7155 | if (I->isUnnamedBitfield()) |
| 7156 | continue; |
Richard Smith | 8d7f11d | 2013-06-27 22:54:33 +0000 | [diff] [blame] | 7157 | Expr *SubInit = ILE->getInit(Index); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 7158 | if (I->getType()->isReferenceType()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7159 | visitLocalsRetainedByReferenceBinding(Path, SubInit, |
Gabor Horvath | 6379e5c | 2019-08-23 22:21:33 +0000 | [diff] [blame] | 7160 | RK_ReferenceBinding, Visit, |
| 7161 | EnableLifetimeWarnings); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7162 | else |
| 7163 | // This might be either aggregate-initialization of a member or |
| 7164 | // initialization of a std::initializer_list object. Regardless, |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 7165 | // we should recursively lifetime-extend that initializer. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7166 | visitLocalsRetainedByInitializer(Path, SubInit, Visit, |
Gabor Horvath | 6379e5c | 2019-08-23 22:21:33 +0000 | [diff] [blame] | 7167 | RevisitSubinits, |
| 7168 | EnableLifetimeWarnings); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 7169 | ++Index; |
| 7170 | } |
| 7171 | } |
| 7172 | } |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7173 | return; |
| 7174 | } |
| 7175 | |
Richard Smith | b3d203f | 2018-10-19 19:01:34 +0000 | [diff] [blame] | 7176 | // The lifetime of an init-capture is that of the closure object constructed |
| 7177 | // by a lambda-expression. |
| 7178 | if (auto *LE = dyn_cast<LambdaExpr>(Init)) { |
| 7179 | for (Expr *E : LE->capture_inits()) { |
| 7180 | if (!E) |
| 7181 | continue; |
| 7182 | if (E->isGLValue()) |
| 7183 | visitLocalsRetainedByReferenceBinding(Path, E, RK_ReferenceBinding, |
Gabor Horvath | 6379e5c | 2019-08-23 22:21:33 +0000 | [diff] [blame] | 7184 | Visit, EnableLifetimeWarnings); |
Richard Smith | b3d203f | 2018-10-19 19:01:34 +0000 | [diff] [blame] | 7185 | else |
Gabor Horvath | 6379e5c | 2019-08-23 22:21:33 +0000 | [diff] [blame] | 7186 | visitLocalsRetainedByInitializer(Path, E, Visit, true, |
| 7187 | EnableLifetimeWarnings); |
Richard Smith | b3d203f | 2018-10-19 19:01:34 +0000 | [diff] [blame] | 7188 | } |
| 7189 | } |
| 7190 | |
Gabor Horvath | e5e10b5 | 2019-08-06 19:13:29 +0000 | [diff] [blame] | 7191 | if (isa<CallExpr>(Init) || isa<CXXConstructExpr>(Init)) { |
Gabor Horvath | 6379e5c | 2019-08-23 22:21:33 +0000 | [diff] [blame] | 7192 | if (EnableLifetimeWarnings) |
| 7193 | handleGslAnnotatedTypes(Path, Init, Visit); |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 7194 | return visitLifetimeBoundArguments(Path, Init, Visit); |
Gabor Horvath | e5e10b5 | 2019-08-06 19:13:29 +0000 | [diff] [blame] | 7195 | } |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7196 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7197 | switch (Init->getStmtClass()) { |
| 7198 | case Stmt::UnaryOperatorClass: { |
| 7199 | auto *UO = cast<UnaryOperator>(Init); |
| 7200 | // If the initializer is the address of a local, we could have a lifetime |
| 7201 | // problem. |
| 7202 | if (UO->getOpcode() == UO_AddrOf) { |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 7203 | // If this is &rvalue, then it's ill-formed and we have already diagnosed |
| 7204 | // it. Don't produce a redundant warning about the lifetime of the |
| 7205 | // temporary. |
| 7206 | if (isa<MaterializeTemporaryExpr>(UO->getSubExpr())) |
| 7207 | return; |
| 7208 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7209 | Path.push_back({IndirectLocalPathEntry::AddressOf, UO}); |
| 7210 | visitLocalsRetainedByReferenceBinding(Path, UO->getSubExpr(), |
Gabor Horvath | 6379e5c | 2019-08-23 22:21:33 +0000 | [diff] [blame] | 7211 | RK_ReferenceBinding, Visit, |
| 7212 | EnableLifetimeWarnings); |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7213 | } |
| 7214 | break; |
| 7215 | } |
| 7216 | |
| 7217 | case Stmt::BinaryOperatorClass: { |
| 7218 | // Handle pointer arithmetic. |
| 7219 | auto *BO = cast<BinaryOperator>(Init); |
| 7220 | BinaryOperatorKind BOK = BO->getOpcode(); |
| 7221 | if (!BO->getType()->isPointerType() || (BOK != BO_Add && BOK != BO_Sub)) |
| 7222 | break; |
| 7223 | |
| 7224 | if (BO->getLHS()->getType()->isPointerType()) |
Gabor Horvath | 6379e5c | 2019-08-23 22:21:33 +0000 | [diff] [blame] | 7225 | visitLocalsRetainedByInitializer(Path, BO->getLHS(), Visit, true, |
| 7226 | EnableLifetimeWarnings); |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7227 | else if (BO->getRHS()->getType()->isPointerType()) |
Gabor Horvath | 6379e5c | 2019-08-23 22:21:33 +0000 | [diff] [blame] | 7228 | visitLocalsRetainedByInitializer(Path, BO->getRHS(), Visit, true, |
| 7229 | EnableLifetimeWarnings); |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7230 | break; |
| 7231 | } |
| 7232 | |
| 7233 | case Stmt::ConditionalOperatorClass: |
| 7234 | case Stmt::BinaryConditionalOperatorClass: { |
| 7235 | auto *C = cast<AbstractConditionalOperator>(Init); |
| 7236 | // In C++, we can have a throw-expression operand, which has 'void' type |
| 7237 | // and isn't interesting from a lifetime perspective. |
| 7238 | if (!C->getTrueExpr()->getType()->isVoidType()) |
Gabor Horvath | 6379e5c | 2019-08-23 22:21:33 +0000 | [diff] [blame] | 7239 | visitLocalsRetainedByInitializer(Path, C->getTrueExpr(), Visit, true, |
| 7240 | EnableLifetimeWarnings); |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7241 | if (!C->getFalseExpr()->getType()->isVoidType()) |
Gabor Horvath | 6379e5c | 2019-08-23 22:21:33 +0000 | [diff] [blame] | 7242 | visitLocalsRetainedByInitializer(Path, C->getFalseExpr(), Visit, true, |
| 7243 | EnableLifetimeWarnings); |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7244 | break; |
| 7245 | } |
| 7246 | |
| 7247 | case Stmt::BlockExprClass: |
| 7248 | if (cast<BlockExpr>(Init)->getBlockDecl()->hasCaptures()) { |
| 7249 | // This is a local block, whose lifetime is that of the function. |
| 7250 | Visit(Path, Local(cast<BlockExpr>(Init)), RK_ReferenceBinding); |
| 7251 | } |
| 7252 | break; |
| 7253 | |
| 7254 | case Stmt::AddrLabelExprClass: |
| 7255 | // We want to warn if the address of a label would escape the function. |
| 7256 | Visit(Path, Local(cast<AddrLabelExpr>(Init)), RK_ReferenceBinding); |
| 7257 | break; |
| 7258 | |
| 7259 | default: |
| 7260 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 7261 | } |
| 7262 | } |
| 7263 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7264 | /// Determine whether this is an indirect path to a temporary that we are |
| 7265 | /// supposed to lifetime-extend along (but don't). |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7266 | static bool shouldLifetimeExtendThroughPath(const IndirectLocalPath &Path) { |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7267 | for (auto Elem : Path) { |
Richard Smith | f66e4f7 | 2018-07-23 22:56:45 +0000 | [diff] [blame] | 7268 | if (Elem.Kind != IndirectLocalPathEntry::DefaultInit) |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7269 | return false; |
| 7270 | } |
| 7271 | return true; |
| 7272 | } |
| 7273 | |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 7274 | /// Find the range for the first interesting entry in the path at or after I. |
| 7275 | static SourceRange nextPathEntryRange(const IndirectLocalPath &Path, unsigned I, |
| 7276 | Expr *E) { |
| 7277 | for (unsigned N = Path.size(); I != N; ++I) { |
| 7278 | switch (Path[I].Kind) { |
| 7279 | case IndirectLocalPathEntry::AddressOf: |
| 7280 | case IndirectLocalPathEntry::LValToRVal: |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 7281 | case IndirectLocalPathEntry::LifetimeBoundCall: |
Gabor Horvath | e5e10b5 | 2019-08-06 19:13:29 +0000 | [diff] [blame] | 7282 | case IndirectLocalPathEntry::GslPointerInit: |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 7283 | // These exist primarily to mark the path as not permitting or |
| 7284 | // supporting lifetime extension. |
| 7285 | break; |
| 7286 | |
Gabor Horvath | fd85c89 | 2019-08-09 18:58:09 +0000 | [diff] [blame] | 7287 | case IndirectLocalPathEntry::VarInit: |
Gabor Horvath | c6802b2 | 2019-08-12 16:19:39 +0000 | [diff] [blame] | 7288 | if (cast<VarDecl>(Path[I].D)->isImplicit()) |
| 7289 | return SourceRange(); |
| 7290 | LLVM_FALLTHROUGH; |
| 7291 | case IndirectLocalPathEntry::DefaultInit: |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 7292 | return Path[I].E->getSourceRange(); |
| 7293 | } |
| 7294 | } |
| 7295 | return E->getSourceRange(); |
| 7296 | } |
| 7297 | |
Gabor Horvath | e5e10b5 | 2019-08-06 19:13:29 +0000 | [diff] [blame] | 7298 | static bool pathOnlyInitializesGslPointer(IndirectLocalPath &Path) { |
Gabor Horvath | 3560ed0 | 2019-08-11 08:05:28 +0000 | [diff] [blame] | 7299 | for (auto It = Path.rbegin(), End = Path.rend(); It != End; ++It) { |
| 7300 | if (It->Kind == IndirectLocalPathEntry::VarInit) |
| 7301 | continue; |
Gabor Horvath | 6f98400 | 2019-09-03 16:17:24 +0000 | [diff] [blame] | 7302 | if (It->Kind == IndirectLocalPathEntry::AddressOf) |
| 7303 | continue; |
Gabor Horvath | 3560ed0 | 2019-08-11 08:05:28 +0000 | [diff] [blame] | 7304 | return It->Kind == IndirectLocalPathEntry::GslPointerInit; |
| 7305 | } |
| 7306 | return false; |
Gabor Horvath | e5e10b5 | 2019-08-06 19:13:29 +0000 | [diff] [blame] | 7307 | } |
| 7308 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7309 | void Sema::checkInitializerLifetime(const InitializedEntity &Entity, |
| 7310 | Expr *Init) { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7311 | LifetimeResult LR = getEntityLifetime(&Entity); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7312 | LifetimeKind LK = LR.getInt(); |
| 7313 | const InitializedEntity *ExtendingEntity = LR.getPointer(); |
| 7314 | |
| 7315 | // If this entity doesn't have an interesting lifetime, don't bother looking |
| 7316 | // for temporaries within its initializer. |
| 7317 | if (LK == LK_FullExpression) |
| 7318 | return; |
| 7319 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7320 | auto TemporaryVisitor = [&](IndirectLocalPath &Path, Local L, |
| 7321 | ReferenceKind RK) -> bool { |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 7322 | SourceRange DiagRange = nextPathEntryRange(Path, 0, L); |
| 7323 | SourceLocation DiagLoc = DiagRange.getBegin(); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7324 | |
Gabor Horvath | e5e10b5 | 2019-08-06 19:13:29 +0000 | [diff] [blame] | 7325 | auto *MTE = dyn_cast<MaterializeTemporaryExpr>(L); |
Gabor Horvath | e5e10b5 | 2019-08-06 19:13:29 +0000 | [diff] [blame] | 7326 | |
Gabor Horvath | bfe0c37 | 2019-08-14 16:34:56 +0000 | [diff] [blame] | 7327 | bool IsGslPtrInitWithGslTempOwner = false; |
| 7328 | bool IsLocalGslOwner = false; |
| 7329 | if (pathOnlyInitializesGslPointer(Path)) { |
| 7330 | if (isa<DeclRefExpr>(L)) { |
| 7331 | // We do not want to follow the references when returning a pointer originating |
| 7332 | // from a local owner to avoid the following false positive: |
| 7333 | // int &p = *localUniquePtr; |
| 7334 | // someContainer.add(std::move(localUniquePtr)); |
| 7335 | // return p; |
| 7336 | IsLocalGslOwner = isRecordWithAttr<OwnerAttr>(L->getType()); |
| 7337 | if (pathContainsInit(Path) || !IsLocalGslOwner) |
| 7338 | return false; |
| 7339 | } else { |
| 7340 | IsGslPtrInitWithGslTempOwner = MTE && !MTE->getExtendingDecl() && |
| 7341 | isRecordWithAttr<OwnerAttr>(MTE->getType()); |
| 7342 | // Skipping a chain of initializing gsl::Pointer annotated objects. |
| 7343 | // We are looking only for the final source to find out if it was |
| 7344 | // a local or temporary owner or the address of a local variable/param. |
| 7345 | if (!IsGslPtrInitWithGslTempOwner) |
| 7346 | return true; |
| 7347 | } |
| 7348 | } |
Gabor Horvath | e5e10b5 | 2019-08-06 19:13:29 +0000 | [diff] [blame] | 7349 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7350 | switch (LK) { |
| 7351 | case LK_FullExpression: |
| 7352 | llvm_unreachable("already handled this"); |
| 7353 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7354 | case LK_Extended: { |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 7355 | if (!MTE) { |
| 7356 | // The initialized entity has lifetime beyond the full-expression, |
| 7357 | // and the local entity does too, so don't warn. |
| 7358 | // |
| 7359 | // FIXME: We should consider warning if a static / thread storage |
| 7360 | // duration variable retains an automatic storage duration local. |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7361 | return false; |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 7362 | } |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7363 | |
Gabor Horvath | c6802b2 | 2019-08-12 16:19:39 +0000 | [diff] [blame] | 7364 | if (IsGslPtrInitWithGslTempOwner && DiagLoc.isValid()) { |
Gabor Horvath | e5e10b5 | 2019-08-06 19:13:29 +0000 | [diff] [blame] | 7365 | Diag(DiagLoc, diag::warn_dangling_lifetime_pointer) << DiagRange; |
| 7366 | return false; |
| 7367 | } |
| 7368 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7369 | // Lifetime-extend the temporary. |
| 7370 | if (Path.empty()) { |
| 7371 | // Update the storage duration of the materialized temporary. |
| 7372 | // FIXME: Rebuild the expression instead of mutating it. |
| 7373 | MTE->setExtendingDecl(ExtendingEntity->getDecl(), |
| 7374 | ExtendingEntity->allocateManglingNumber()); |
| 7375 | // Also visit the temporaries lifetime-extended by this initializer. |
| 7376 | return true; |
| 7377 | } |
| 7378 | |
| 7379 | if (shouldLifetimeExtendThroughPath(Path)) { |
| 7380 | // We're supposed to lifetime-extend the temporary along this path (per |
| 7381 | // the resolution of DR1815), but we don't support that yet. |
| 7382 | // |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 7383 | // FIXME: Properly handle this situation. Perhaps the easiest approach |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7384 | // would be to clone the initializer expression on each use that would |
| 7385 | // lifetime extend its temporaries. |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 7386 | Diag(DiagLoc, diag::warn_unsupported_lifetime_extension) |
| 7387 | << RK << DiagRange; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7388 | } else { |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 7389 | // If the path goes through the initialization of a variable or field, |
| 7390 | // it can't possibly reach a temporary created in this full-expression. |
| 7391 | // We will have already diagnosed any problems with the initializer. |
| 7392 | if (pathContainsInit(Path)) |
| 7393 | return false; |
| 7394 | |
| 7395 | Diag(DiagLoc, diag::warn_dangling_variable) |
Richard Smith | ad5bbcc | 2018-08-01 01:03:33 +0000 | [diff] [blame] | 7396 | << RK << !Entity.getParent() |
| 7397 | << ExtendingEntity->getDecl()->isImplicit() |
| 7398 | << ExtendingEntity->getDecl() << Init->isGLValue() << DiagRange; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7399 | } |
| 7400 | break; |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7401 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7402 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7403 | case LK_MemInitializer: { |
George Burgess IV | 06df229 | 2018-07-24 02:10:53 +0000 | [diff] [blame] | 7404 | if (isa<MaterializeTemporaryExpr>(L)) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7405 | // Under C++ DR1696, if a mem-initializer (or a default member |
| 7406 | // initializer used by the absence of one) would lifetime-extend a |
| 7407 | // temporary, the program is ill-formed. |
| 7408 | if (auto *ExtendingDecl = |
| 7409 | ExtendingEntity ? ExtendingEntity->getDecl() : nullptr) { |
Gabor Horvath | e5e10b5 | 2019-08-06 19:13:29 +0000 | [diff] [blame] | 7410 | if (IsGslPtrInitWithGslTempOwner) { |
| 7411 | Diag(DiagLoc, diag::warn_dangling_lifetime_pointer_member) |
| 7412 | << ExtendingDecl << DiagRange; |
| 7413 | Diag(ExtendingDecl->getLocation(), |
| 7414 | diag::note_ref_or_ptr_member_declared_here) |
| 7415 | << true; |
| 7416 | return false; |
| 7417 | } |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7418 | bool IsSubobjectMember = ExtendingEntity != &Entity; |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 7419 | Diag(DiagLoc, shouldLifetimeExtendThroughPath(Path) |
| 7420 | ? diag::err_dangling_member |
| 7421 | : diag::warn_dangling_member) |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7422 | << ExtendingDecl << IsSubobjectMember << RK << DiagRange; |
| 7423 | // Don't bother adding a note pointing to the field if we're inside |
| 7424 | // its default member initializer; our primary diagnostic points to |
| 7425 | // the same place in that case. |
| 7426 | if (Path.empty() || |
| 7427 | Path.back().Kind != IndirectLocalPathEntry::DefaultInit) { |
| 7428 | Diag(ExtendingDecl->getLocation(), |
| 7429 | diag::note_lifetime_extending_member_declared_here) |
| 7430 | << RK << IsSubobjectMember; |
| 7431 | } |
| 7432 | } else { |
| 7433 | // We have a mem-initializer but no particular field within it; this |
| 7434 | // is either a base class or a delegating initializer directly |
| 7435 | // initializing the base-class from something that doesn't live long |
| 7436 | // enough. |
| 7437 | // |
| 7438 | // FIXME: Warn on this. |
| 7439 | return false; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7440 | } |
| 7441 | } else { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7442 | // Paths via a default initializer can only occur during error recovery |
| 7443 | // (there's no other way that a default initializer can refer to a |
| 7444 | // local). Don't produce a bogus warning on those cases. |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 7445 | if (pathContainsInit(Path)) |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7446 | return false; |
| 7447 | |
Gabor Horvath | 3560ed0 | 2019-08-11 08:05:28 +0000 | [diff] [blame] | 7448 | // Suppress false positives for code like the one below: |
Gabor Horvath | eb563af | 2019-08-10 00:32:29 +0000 | [diff] [blame] | 7449 | // Ctor(unique_ptr<T> up) : member(*up), member2(move(up)) {} |
| 7450 | if (IsLocalGslOwner && pathOnlyInitializesGslPointer(Path)) |
| 7451 | return false; |
| 7452 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7453 | auto *DRE = dyn_cast<DeclRefExpr>(L); |
| 7454 | auto *VD = DRE ? dyn_cast<VarDecl>(DRE->getDecl()) : nullptr; |
| 7455 | if (!VD) { |
| 7456 | // A member was initialized to a local block. |
| 7457 | // FIXME: Warn on this. |
| 7458 | return false; |
| 7459 | } |
| 7460 | |
| 7461 | if (auto *Member = |
| 7462 | ExtendingEntity ? ExtendingEntity->getDecl() : nullptr) { |
Gabor Horvath | e5e10b5 | 2019-08-06 19:13:29 +0000 | [diff] [blame] | 7463 | bool IsPointer = !Member->getType()->isReferenceType(); |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7464 | Diag(DiagLoc, IsPointer ? diag::warn_init_ptr_member_to_parameter_addr |
| 7465 | : diag::warn_bind_ref_member_to_parameter) |
| 7466 | << Member << VD << isa<ParmVarDecl>(VD) << DiagRange; |
| 7467 | Diag(Member->getLocation(), |
| 7468 | diag::note_ref_or_ptr_member_declared_here) |
| 7469 | << (unsigned)IsPointer; |
| 7470 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7471 | } |
| 7472 | break; |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7473 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7474 | |
| 7475 | case LK_New: |
George Burgess IV | 06df229 | 2018-07-24 02:10:53 +0000 | [diff] [blame] | 7476 | if (isa<MaterializeTemporaryExpr>(L)) { |
Gabor Horvath | e5e10b5 | 2019-08-06 19:13:29 +0000 | [diff] [blame] | 7477 | if (IsGslPtrInitWithGslTempOwner) |
| 7478 | Diag(DiagLoc, diag::warn_dangling_lifetime_pointer) << DiagRange; |
| 7479 | else |
| 7480 | Diag(DiagLoc, RK == RK_ReferenceBinding |
| 7481 | ? diag::warn_new_dangling_reference |
| 7482 | : diag::warn_new_dangling_initializer_list) |
| 7483 | << !Entity.getParent() << DiagRange; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7484 | } else { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7485 | // We can't determine if the allocation outlives the local declaration. |
| 7486 | return false; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7487 | } |
| 7488 | break; |
| 7489 | |
| 7490 | case LK_Return: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 7491 | case LK_StmtExprResult: |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7492 | if (auto *DRE = dyn_cast<DeclRefExpr>(L)) { |
| 7493 | // We can't determine if the local variable outlives the statement |
| 7494 | // expression. |
| 7495 | if (LK == LK_StmtExprResult) |
| 7496 | return false; |
| 7497 | Diag(DiagLoc, diag::warn_ret_stack_addr_ref) |
| 7498 | << Entity.getType()->isReferenceType() << DRE->getDecl() |
| 7499 | << isa<ParmVarDecl>(DRE->getDecl()) << DiagRange; |
| 7500 | } else if (isa<BlockExpr>(L)) { |
| 7501 | Diag(DiagLoc, diag::err_ret_local_block) << DiagRange; |
| 7502 | } else if (isa<AddrLabelExpr>(L)) { |
Reid Kleckner | 4c33d19 | 2018-08-17 22:11:31 +0000 | [diff] [blame] | 7503 | // Don't warn when returning a label from a statement expression. |
| 7504 | // Leaving the scope doesn't end its lifetime. |
| 7505 | if (LK == LK_StmtExprResult) |
| 7506 | return false; |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7507 | Diag(DiagLoc, diag::warn_ret_addr_label) << DiagRange; |
| 7508 | } else { |
| 7509 | Diag(DiagLoc, diag::warn_ret_local_temp_addr_ref) |
| 7510 | << Entity.getType()->isReferenceType() << DiagRange; |
| 7511 | } |
| 7512 | break; |
Florian Hahn | 0aa117d | 2018-07-17 09:23:31 +0000 | [diff] [blame] | 7513 | } |
| 7514 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7515 | for (unsigned I = 0; I != Path.size(); ++I) { |
| 7516 | auto Elem = Path[I]; |
| 7517 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7518 | switch (Elem.Kind) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7519 | case IndirectLocalPathEntry::AddressOf: |
| 7520 | case IndirectLocalPathEntry::LValToRVal: |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 7521 | // These exist primarily to mark the path as not permitting or |
| 7522 | // supporting lifetime extension. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7523 | break; |
| 7524 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 7525 | case IndirectLocalPathEntry::LifetimeBoundCall: |
Gabor Horvath | e5e10b5 | 2019-08-06 19:13:29 +0000 | [diff] [blame] | 7526 | case IndirectLocalPathEntry::GslPointerInit: |
| 7527 | // FIXME: Consider adding a note for these. |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 7528 | break; |
| 7529 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7530 | case IndirectLocalPathEntry::DefaultInit: { |
| 7531 | auto *FD = cast<FieldDecl>(Elem.D); |
| 7532 | Diag(FD->getLocation(), diag::note_init_with_default_member_initalizer) |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 7533 | << FD << nextPathEntryRange(Path, I + 1, L); |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7534 | break; |
| 7535 | } |
| 7536 | |
| 7537 | case IndirectLocalPathEntry::VarInit: |
| 7538 | const VarDecl *VD = cast<VarDecl>(Elem.D); |
| 7539 | Diag(VD->getLocation(), diag::note_local_var_initializer) |
Richard Smith | ad5bbcc | 2018-08-01 01:03:33 +0000 | [diff] [blame] | 7540 | << VD->getType()->isReferenceType() |
| 7541 | << VD->isImplicit() << VD->getDeclName() |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 7542 | << nextPathEntryRange(Path, I + 1, L); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7543 | break; |
Florian Hahn | 0aa117d | 2018-07-17 09:23:31 +0000 | [diff] [blame] | 7544 | } |
| 7545 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7546 | |
| 7547 | // We didn't lifetime-extend, so don't go any further; we don't need more |
| 7548 | // warnings or errors on inner temporaries within this one's initializer. |
| 7549 | return false; |
| 7550 | }; |
| 7551 | |
Gabor Horvath | 6379e5c | 2019-08-23 22:21:33 +0000 | [diff] [blame] | 7552 | bool EnableLifetimeWarnings = !getDiagnostics().isIgnored( |
| 7553 | diag::warn_dangling_lifetime_pointer, SourceLocation()); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7554 | llvm::SmallVector<IndirectLocalPathEntry, 8> Path; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7555 | if (Init->isGLValue()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7556 | visitLocalsRetainedByReferenceBinding(Path, Init, RK_ReferenceBinding, |
Gabor Horvath | 6379e5c | 2019-08-23 22:21:33 +0000 | [diff] [blame] | 7557 | TemporaryVisitor, |
| 7558 | EnableLifetimeWarnings); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7559 | else |
Gabor Horvath | 6379e5c | 2019-08-23 22:21:33 +0000 | [diff] [blame] | 7560 | visitLocalsRetainedByInitializer(Path, Init, TemporaryVisitor, false, |
| 7561 | EnableLifetimeWarnings); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 7562 | } |
| 7563 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7564 | static void DiagnoseNarrowingInInitList(Sema &S, |
| 7565 | const ImplicitConversionSequence &ICS, |
| 7566 | QualType PreNarrowingType, |
| 7567 | QualType EntityType, |
| 7568 | const Expr *PostInit); |
| 7569 | |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7570 | /// Provide warnings when std::move is used on construction. |
| 7571 | static void CheckMoveOnConstruction(Sema &S, const Expr *InitExpr, |
| 7572 | bool IsReturnStmt) { |
| 7573 | if (!InitExpr) |
| 7574 | return; |
| 7575 | |
Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 7576 | if (S.inTemplateInstantiation()) |
Richard Trieu | 6093d14 | 2015-07-29 17:03:34 +0000 | [diff] [blame] | 7577 | return; |
| 7578 | |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7579 | QualType DestType = InitExpr->getType(); |
| 7580 | if (!DestType->isRecordType()) |
| 7581 | return; |
| 7582 | |
Richard Trieu | 155b8d0 | 2019-08-08 00:12:51 +0000 | [diff] [blame] | 7583 | const CXXConstructExpr *CCE = |
| 7584 | dyn_cast<CXXConstructExpr>(InitExpr->IgnoreParens()); |
| 7585 | if (!CCE || CCE->getNumArgs() != 1) |
| 7586 | return; |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7587 | |
Richard Trieu | 155b8d0 | 2019-08-08 00:12:51 +0000 | [diff] [blame] | 7588 | if (!CCE->getConstructor()->isCopyOrMoveConstructor()) |
| 7589 | return; |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7590 | |
Richard Trieu | 155b8d0 | 2019-08-08 00:12:51 +0000 | [diff] [blame] | 7591 | InitExpr = CCE->getArg(0)->IgnoreImpCasts(); |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7592 | |
| 7593 | // Find the std::move call and get the argument. |
| 7594 | const CallExpr *CE = dyn_cast<CallExpr>(InitExpr->IgnoreParens()); |
Nico Weber | 192184c | 2018-06-20 15:57:38 +0000 | [diff] [blame] | 7595 | if (!CE || !CE->isCallToStdMove()) |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7596 | return; |
| 7597 | |
Richard Trieu | 155b8d0 | 2019-08-08 00:12:51 +0000 | [diff] [blame] | 7598 | const Expr *Arg = CE->getArg(0); |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7599 | |
Richard Trieu | 155b8d0 | 2019-08-08 00:12:51 +0000 | [diff] [blame] | 7600 | unsigned DiagID = 0; |
| 7601 | |
| 7602 | if (!IsReturnStmt && !isa<MaterializeTemporaryExpr>(Arg)) |
| 7603 | return; |
| 7604 | |
| 7605 | if (isa<MaterializeTemporaryExpr>(Arg)) { |
| 7606 | DiagID = diag::warn_pessimizing_move_on_initialization; |
| 7607 | const Expr *ArgStripped = Arg->IgnoreImplicit()->IgnoreParens(); |
| 7608 | if (!ArgStripped->isRValue() || !ArgStripped->getType()->isRecordType()) |
| 7609 | return; |
| 7610 | } else { // IsReturnStmt |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7611 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg->IgnoreParenImpCasts()); |
| 7612 | if (!DRE || DRE->refersToEnclosingVariableOrCapture()) |
| 7613 | return; |
| 7614 | |
| 7615 | const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()); |
| 7616 | if (!VD || !VD->hasLocalStorage()) |
| 7617 | return; |
| 7618 | |
Alex Lorenz | bbe51d8 | 2017-11-07 21:40:11 +0000 | [diff] [blame] | 7619 | // __block variables are not moved implicitly. |
| 7620 | if (VD->hasAttr<BlocksAttr>()) |
| 7621 | return; |
| 7622 | |
Richard Trieu | 8d4006a | 2015-07-28 19:06:16 +0000 | [diff] [blame] | 7623 | QualType SourceType = VD->getType(); |
| 7624 | if (!SourceType->isRecordType()) |
Richard Trieu | 1d4911bc | 2015-05-18 19:54:08 +0000 | [diff] [blame] | 7625 | return; |
| 7626 | |
Richard Trieu | 8d4006a | 2015-07-28 19:06:16 +0000 | [diff] [blame] | 7627 | if (!S.Context.hasSameUnqualifiedType(DestType, SourceType)) { |
Richard Trieu | 1993dc8 | 2015-07-29 23:47:19 +0000 | [diff] [blame] | 7628 | return; |
Richard Trieu | 8d4006a | 2015-07-28 19:06:16 +0000 | [diff] [blame] | 7629 | } |
| 7630 | |
Davide Italiano | 7842c3f | 2015-07-18 01:15:19 +0000 | [diff] [blame] | 7631 | // If we're returning a function parameter, copy elision |
| 7632 | // is not possible. |
| 7633 | if (isa<ParmVarDecl>(VD)) |
| 7634 | DiagID = diag::warn_redundant_move_on_return; |
Richard Trieu | 1993dc8 | 2015-07-29 23:47:19 +0000 | [diff] [blame] | 7635 | else |
| 7636 | DiagID = diag::warn_pessimizing_move_on_return; |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7637 | } |
| 7638 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7639 | S.Diag(CE->getBeginLoc(), DiagID); |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7640 | |
| 7641 | // Get all the locations for a fix-it. Don't emit the fix-it if any location |
| 7642 | // is within a macro. |
Richard Trieu | 155b8d0 | 2019-08-08 00:12:51 +0000 | [diff] [blame] | 7643 | SourceLocation BeginLoc = CCE->getBeginLoc(); |
| 7644 | if (BeginLoc.isMacroID()) |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7645 | return; |
| 7646 | SourceLocation RParen = CE->getRParenLoc(); |
| 7647 | if (RParen.isMacroID()) |
| 7648 | return; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7649 | SourceLocation ArgLoc = Arg->getBeginLoc(); |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7650 | |
| 7651 | // Special testing for the argument location. Since the fix-it needs the |
| 7652 | // location right before the argument, the argument location can be in a |
| 7653 | // macro only if it is at the beginning of the macro. |
| 7654 | while (ArgLoc.isMacroID() && |
| 7655 | S.getSourceManager().isAtStartOfImmediateMacroExpansion(ArgLoc)) { |
Richard Smith | b5f8171 | 2018-04-30 05:25:48 +0000 | [diff] [blame] | 7656 | ArgLoc = S.getSourceManager().getImmediateExpansionRange(ArgLoc).getBegin(); |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7657 | } |
| 7658 | |
Richard Trieu | 155b8d0 | 2019-08-08 00:12:51 +0000 | [diff] [blame] | 7659 | SourceLocation LParen = ArgLoc.getLocWithOffset(-1); |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7660 | if (LParen.isMacroID()) |
| 7661 | return; |
Richard Trieu | 155b8d0 | 2019-08-08 00:12:51 +0000 | [diff] [blame] | 7662 | SourceLocation EndLoc = CCE->getEndLoc(); |
| 7663 | if (EndLoc.isMacroID()) |
| 7664 | return; |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7665 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7666 | S.Diag(CE->getBeginLoc(), diag::note_remove_move) |
Richard Trieu | 155b8d0 | 2019-08-08 00:12:51 +0000 | [diff] [blame] | 7667 | << FixItHint::CreateRemoval(SourceRange(BeginLoc, LParen)) |
| 7668 | << FixItHint::CreateRemoval(SourceRange(RParen, EndLoc)); |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7669 | } |
| 7670 | |
Nick Lewycky | 2eeddfb | 2016-05-14 17:44:14 +0000 | [diff] [blame] | 7671 | static void CheckForNullPointerDereference(Sema &S, const Expr *E) { |
| 7672 | // Check to see if we are dereferencing a null pointer. If so, this is |
| 7673 | // undefined behavior, so warn about it. This only handles the pattern |
| 7674 | // "*null", which is a very syntactic check. |
| 7675 | if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E->IgnoreParenCasts())) |
| 7676 | if (UO->getOpcode() == UO_Deref && |
| 7677 | UO->getSubExpr()->IgnoreParenCasts()-> |
| 7678 | isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull)) { |
| 7679 | S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
| 7680 | S.PDiag(diag::warn_binding_null_to_reference) |
| 7681 | << UO->getSubExpr()->getSourceRange()); |
| 7682 | } |
| 7683 | } |
| 7684 | |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 7685 | MaterializeTemporaryExpr * |
| 7686 | Sema::CreateMaterializeTemporaryExpr(QualType T, Expr *Temporary, |
| 7687 | bool BoundToLvalueReference) { |
| 7688 | auto MTE = new (Context) |
| 7689 | MaterializeTemporaryExpr(T, Temporary, BoundToLvalueReference); |
| 7690 | |
| 7691 | // Order an ExprWithCleanups for lifetime marks. |
| 7692 | // |
| 7693 | // TODO: It'll be good to have a single place to check the access of the |
| 7694 | // destructor and generate ExprWithCleanups for various uses. Currently these |
| 7695 | // are done in both CreateMaterializeTemporaryExpr and MaybeBindToTemporary, |
| 7696 | // but there may be a chance to merge them. |
| 7697 | Cleanup.setExprNeedsCleanups(false); |
| 7698 | return MTE; |
| 7699 | } |
| 7700 | |
Richard Smith | 4baaa5a | 2016-12-03 01:14:32 +0000 | [diff] [blame] | 7701 | ExprResult Sema::TemporaryMaterializationConversion(Expr *E) { |
| 7702 | // In C++98, we don't want to implicitly create an xvalue. |
| 7703 | // FIXME: This means that AST consumers need to deal with "prvalues" that |
| 7704 | // denote materialized temporaries. Maybe we should add another ValueKind |
| 7705 | // for "xvalue pretending to be a prvalue" for C++98 support. |
| 7706 | if (!E->isRValue() || !getLangOpts().CPlusPlus11) |
| 7707 | return E; |
| 7708 | |
| 7709 | // C++1z [conv.rval]/1: T shall be a complete type. |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7710 | // FIXME: Does this ever matter (can we form a prvalue of incomplete type)? |
| 7711 | // 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] | 7712 | QualType T = E->getType(); |
| 7713 | if (RequireCompleteType(E->getExprLoc(), T, diag::err_incomplete_type)) |
| 7714 | return ExprError(); |
| 7715 | |
| 7716 | return CreateMaterializeTemporaryExpr(E->getType(), E, false); |
| 7717 | } |
| 7718 | |
Anastasia Stulova | 0430794 | 2018-11-16 16:22:56 +0000 | [diff] [blame] | 7719 | ExprResult Sema::PerformQualificationConversion(Expr *E, QualType Ty, |
| 7720 | ExprValueKind VK, |
| 7721 | CheckedConversionKind CCK) { |
Anastasia Stulova | 094c726 | 2019-04-04 10:48:36 +0000 | [diff] [blame] | 7722 | |
| 7723 | CastKind CK = CK_NoOp; |
| 7724 | |
| 7725 | if (VK == VK_RValue) { |
| 7726 | auto PointeeTy = Ty->getPointeeType(); |
| 7727 | auto ExprPointeeTy = E->getType()->getPointeeType(); |
| 7728 | if (!PointeeTy.isNull() && |
| 7729 | PointeeTy.getAddressSpace() != ExprPointeeTy.getAddressSpace()) |
| 7730 | CK = CK_AddressSpaceConversion; |
| 7731 | } else if (Ty.getAddressSpace() != E->getType().getAddressSpace()) { |
| 7732 | CK = CK_AddressSpaceConversion; |
| 7733 | } |
| 7734 | |
Anastasia Stulova | 0430794 | 2018-11-16 16:22:56 +0000 | [diff] [blame] | 7735 | return ImpCastExprToType(E, Ty, CK, VK, /*BasePath=*/nullptr, CCK); |
| 7736 | } |
| 7737 | |
| 7738 | ExprResult InitializationSequence::Perform(Sema &S, |
| 7739 | const InitializedEntity &Entity, |
| 7740 | const InitializationKind &Kind, |
| 7741 | MultiExprArg Args, |
| 7742 | QualType *ResultType) { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 7743 | if (Failed()) { |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 7744 | Diagnose(S, Entity, Kind, Args); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7745 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7746 | } |
Nico Weber | 337d5aa | 2015-04-17 08:32:38 +0000 | [diff] [blame] | 7747 | if (!ZeroInitializationFixit.empty()) { |
| 7748 | unsigned DiagID = diag::err_default_init_const; |
| 7749 | if (Decl *D = Entity.getDecl()) |
| 7750 | if (S.getLangOpts().MSVCCompat && D->hasAttr<SelectAnyAttr>()) |
| 7751 | DiagID = diag::ext_default_init_const; |
| 7752 | |
| 7753 | // The initialization would have succeeded with this fixit. Since the fixit |
| 7754 | // is on the error, we need to build a valid AST in this case, so this isn't |
| 7755 | // handled in the Failed() branch above. |
| 7756 | QualType DestType = Entity.getType(); |
| 7757 | S.Diag(Kind.getLocation(), DiagID) |
| 7758 | << DestType << (bool)DestType->getAs<RecordType>() |
| 7759 | << FixItHint::CreateInsertion(ZeroInitializationFixitLoc, |
| 7760 | ZeroInitializationFixit); |
| 7761 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7762 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 7763 | if (getKind() == DependentSequence) { |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7764 | // If the declaration is a non-dependent, incomplete array type |
| 7765 | // that has an initializer, then its type will be completed once |
| 7766 | // the initializer is instantiated. |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7767 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7768 | Args.size() == 1) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7769 | QualType DeclType = Entity.getType(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7770 | if (const IncompleteArrayType *ArrayT |
| 7771 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 7772 | // FIXME: We don't currently have the ability to accurately |
| 7773 | // compute the length of an initializer list without |
| 7774 | // performing full type-checking of the initializer list |
| 7775 | // (since we have to determine where braces are implicitly |
| 7776 | // introduced and such). So, we fall back to making the array |
| 7777 | // type a dependently-sized array type with no specified |
| 7778 | // bound. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7779 | if (isa<InitListExpr>((Expr *)Args[0])) { |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7780 | SourceRange Brackets; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7781 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7782 | // Scavange the location of the brackets from the entity, if we can. |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 7783 | if (auto *DD = dyn_cast_or_null<DeclaratorDecl>(Entity.getDecl())) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7784 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 7785 | TypeLoc TL = TInfo->getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7786 | if (IncompleteArrayTypeLoc ArrayLoc = |
| 7787 | TL.getAs<IncompleteArrayTypeLoc>()) |
| 7788 | Brackets = ArrayLoc.getBracketsRange(); |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7789 | } |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7790 | } |
| 7791 | |
| 7792 | *ResultType |
| 7793 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7794 | /*NumElts=*/nullptr, |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7795 | ArrayT->getSizeModifier(), |
| 7796 | ArrayT->getIndexTypeCVRQualifiers(), |
| 7797 | Brackets); |
| 7798 | } |
| 7799 | |
| 7800 | } |
| 7801 | } |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 7802 | if (Kind.getKind() == InitializationKind::IK_Direct && |
| 7803 | !Kind.isExplicitCast()) { |
| 7804 | // Rebuild the ParenListExpr. |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 7805 | SourceRange ParenRange = Kind.getParenOrBraceRange(); |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 7806 | return S.ActOnParenListExpr(ParenRange.getBegin(), ParenRange.getEnd(), |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7807 | Args); |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 7808 | } |
Manuel Klimek | f2b4b69 | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 7809 | assert(Kind.getKind() == InitializationKind::IK_Copy || |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7810 | Kind.isExplicitCast() || |
Douglas Gregor | bf13895 | 2012-04-04 04:06:51 +0000 | [diff] [blame] | 7811 | Kind.getKind() == InitializationKind::IK_DirectList); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7812 | return ExprResult(Args[0]); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7813 | } |
| 7814 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 7815 | // No steps means no initialization. |
| 7816 | if (Steps.empty()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7817 | return ExprResult((Expr *)nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7818 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7819 | if (S.getLangOpts().CPlusPlus11 && Entity.getType()->isReferenceType() && |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7820 | Args.size() == 1 && isa<InitListExpr>(Args[0]) && |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 7821 | !Entity.isParameterKind()) { |
Richard Smith | 2b349ae | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 7822 | // Produce a C++98 compatibility warning if we are initializing a reference |
| 7823 | // from an initializer list. For parameters, we produce a better warning |
| 7824 | // elsewhere. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7825 | Expr *Init = Args[0]; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7826 | S.Diag(Init->getBeginLoc(), diag::warn_cxx98_compat_reference_list_init) |
| 7827 | << Init->getSourceRange(); |
Richard Smith | 2b349ae | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 7828 | } |
| 7829 | |
Egor Churaev | 3bccec5 | 2017-04-05 12:47:10 +0000 | [diff] [blame] | 7830 | // OpenCL v2.0 s6.13.11.1. atomic variables can be initialized in global scope |
| 7831 | QualType ETy = Entity.getType(); |
| 7832 | Qualifiers TyQualifiers = ETy.getQualifiers(); |
| 7833 | bool HasGlobalAS = TyQualifiers.hasAddressSpace() && |
| 7834 | TyQualifiers.getAddressSpace() == LangAS::opencl_global; |
| 7835 | |
| 7836 | if (S.getLangOpts().OpenCLVersion >= 200 && |
| 7837 | ETy->isAtomicType() && !HasGlobalAS && |
| 7838 | Entity.getKind() == InitializedEntity::EK_Variable && Args.size() > 0) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7839 | S.Diag(Args[0]->getBeginLoc(), diag::err_opencl_atomic_init) |
| 7840 | << 1 |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 7841 | << SourceRange(Entity.getDecl()->getBeginLoc(), Args[0]->getEndLoc()); |
Egor Churaev | 3bccec5 | 2017-04-05 12:47:10 +0000 | [diff] [blame] | 7842 | return ExprError(); |
| 7843 | } |
| 7844 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7845 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 7846 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 7847 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 7848 | // and we want latter when it makes sense. |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7849 | if (ResultType) |
Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 7850 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7851 | Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7852 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7853 | ExprResult CurInit((Expr *)nullptr); |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7854 | SmallVector<Expr*, 4> ArrayLoopCommonExprs; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7855 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7856 | // For initialization steps that start with a single initializer, |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 7857 | // grab the only argument out the Args and place it into the "current" |
| 7858 | // initializer. |
| 7859 | switch (Steps.front().Kind) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7860 | case SK_ResolveAddressOfOverloadedFunction: |
| 7861 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7862 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7863 | case SK_CastDerivedToBaseLValue: |
| 7864 | case SK_BindReference: |
| 7865 | case SK_BindReferenceToTemporary: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7866 | case SK_FinalCopy: |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 7867 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7868 | case SK_UserConversion: |
| 7869 | case SK_QualificationConversionLValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7870 | case SK_QualificationConversionXValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7871 | case SK_QualificationConversionRValue: |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 7872 | case SK_AtomicConversion: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7873 | case SK_ConversionSequence: |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7874 | case SK_ConversionSequenceNoNarrowing: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7875 | case SK_ListInitialization: |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7876 | case SK_UnwrapInitList: |
| 7877 | case SK_RewrapInitList: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7878 | case SK_CAssignment: |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 7879 | case SK_StringInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7880 | case SK_ObjCObjectConversion: |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7881 | case SK_ArrayLoopIndex: |
| 7882 | case SK_ArrayLoopInit: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7883 | case SK_ArrayInit: |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 7884 | case SK_GNUArrayInit: |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 7885 | case SK_ParenthesizedArrayInit: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7886 | case SK_PassByIndirectCopyRestore: |
| 7887 | case SK_PassByIndirectRestore: |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 7888 | case SK_ProduceObjCObject: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7889 | case SK_StdInitializerList: |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 7890 | case SK_OCLSamplerInit: |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 7891 | case SK_OCLZeroOpaqueType: { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7892 | assert(Args.size() == 1); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7893 | CurInit = Args[0]; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7894 | if (!CurInit.get()) return ExprError(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7895 | break; |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7896 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7897 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7898 | case SK_ConstructorInitialization: |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7899 | case SK_ConstructorInitializationFromList: |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 7900 | case SK_StdInitializerListConstructorCall: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7901 | case SK_ZeroInitialization: |
| 7902 | break; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7903 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7904 | |
Richard Smith | d6a1508 | 2017-01-07 00:48:55 +0000 | [diff] [blame] | 7905 | // Promote from an unevaluated context to an unevaluated list context in |
| 7906 | // C++11 list-initialization; we need to instantiate entities usable in |
| 7907 | // constant expressions here in order to perform narrowing checks =( |
| 7908 | EnterExpressionEvaluationContext Evaluated( |
| 7909 | S, EnterExpressionEvaluationContext::InitList, |
| 7910 | CurInit.get() && isa<InitListExpr>(CurInit.get())); |
| 7911 | |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7912 | // C++ [class.abstract]p2: |
| 7913 | // no objects of an abstract class can be created except as subobjects |
| 7914 | // of a class derived from it |
| 7915 | auto checkAbstractType = [&](QualType T) -> bool { |
| 7916 | if (Entity.getKind() == InitializedEntity::EK_Base || |
| 7917 | Entity.getKind() == InitializedEntity::EK_Delegating) |
| 7918 | return false; |
| 7919 | return S.RequireNonAbstractType(Kind.getLocation(), T, |
| 7920 | diag::err_allocation_of_abstract_type); |
| 7921 | }; |
| 7922 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7923 | // Walk through the computed steps for the initialization sequence, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7924 | // performing the specified conversions along the way. |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7925 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7926 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 7927 | Step != StepEnd; ++Step) { |
| 7928 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7929 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7930 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7931 | QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7932 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7933 | switch (Step->Kind) { |
| 7934 | case SK_ResolveAddressOfOverloadedFunction: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7935 | // Overload resolution determined which function invoke; update the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7936 | // initializer to reflect that choice. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7937 | S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 7938 | if (S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation())) |
| 7939 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7940 | CurInit = S.FixOverloadedFunctionReference(CurInit, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7941 | Step->Function.FoundDecl, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7942 | Step->Function.Function); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7943 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7944 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7945 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7946 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7947 | case SK_CastDerivedToBaseLValue: { |
| 7948 | // We have a derived-to-base cast that produces either an rvalue or an |
| 7949 | // lvalue. Perform that cast. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7950 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 7951 | CXXCastPath BasePath; |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 7952 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7953 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 7954 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7955 | if (S.CheckDerivedToBaseConversion( |
| 7956 | SourceType, Step->Type, CurInit.get()->getBeginLoc(), |
| 7957 | CurInit.get()->getSourceRange(), &BasePath, IgnoreBaseAccess)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7958 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7959 | |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7960 | ExprValueKind VK = |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7961 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7962 | VK_LValue : |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7963 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7964 | VK_XValue : |
| 7965 | VK_RValue); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7966 | CurInit = |
| 7967 | ImplicitCastExpr::Create(S.Context, Step->Type, CK_DerivedToBase, |
| 7968 | CurInit.get(), &BasePath, VK); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7969 | break; |
| 7970 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7971 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7972 | case SK_BindReference: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7973 | // Reference binding does not have any corresponding ASTs. |
| 7974 | |
| 7975 | // Check exception specifications |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7976 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7977 | return ExprError(); |
Anders Carlsson | ab0ddb5 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 7978 | |
George Burgess IV | cfd48d9 | 2017-04-13 23:47:08 +0000 | [diff] [blame] | 7979 | // We don't check for e.g. function pointers here, since address |
| 7980 | // availability checks should only occur when the function first decays |
| 7981 | // into a pointer or reference. |
| 7982 | if (CurInit.get()->getType()->isFunctionProtoType()) { |
| 7983 | if (auto *DRE = dyn_cast<DeclRefExpr>(CurInit.get()->IgnoreParens())) { |
| 7984 | if (auto *FD = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
| 7985 | if (!S.checkAddressOfFunctionIsAvailable(FD, /*Complain=*/true, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7986 | DRE->getBeginLoc())) |
George Burgess IV | cfd48d9 | 2017-04-13 23:47:08 +0000 | [diff] [blame] | 7987 | return ExprError(); |
| 7988 | } |
| 7989 | } |
| 7990 | } |
| 7991 | |
Nick Lewycky | 2eeddfb | 2016-05-14 17:44:14 +0000 | [diff] [blame] | 7992 | CheckForNullPointerDereference(S, CurInit.get()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7993 | break; |
Anders Carlsson | ab0ddb5 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 7994 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 7995 | case SK_BindReferenceToTemporary: { |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 7996 | // Make sure the "temporary" is actually an rvalue. |
| 7997 | assert(CurInit.get()->isRValue() && "not a temporary"); |
| 7998 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7999 | // Check exception specifications |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8000 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8001 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8002 | |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 8003 | // Materialize the temporary into memory. |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 8004 | MaterializeTemporaryExpr *MTE = S.CreateMaterializeTemporaryExpr( |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8005 | Step->Type, CurInit.get(), Entity.getType()->isLValueReferenceType()); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 8006 | CurInit = MTE; |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 8007 | |
Brian Kelley | 762f928 | 2017-03-29 18:16:38 +0000 | [diff] [blame] | 8008 | // If we're extending this temporary to automatic storage duration -- we |
| 8009 | // need to register its cleanup during the full-expression's cleanups. |
| 8010 | if (MTE->getStorageDuration() == SD_Automatic && |
| 8011 | MTE->getType().isDestructedType()) |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 8012 | S.Cleanup.setExprNeedsCleanups(true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8013 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 8014 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8015 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8016 | case SK_FinalCopy: |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 8017 | if (checkAbstractType(Step->Type)) |
| 8018 | return ExprError(); |
| 8019 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8020 | // If the overall initialization is initializing a temporary, we already |
| 8021 | // bound our argument if it was necessary to do so. If not (if we're |
| 8022 | // ultimately initializing a non-temporary), our argument needs to be |
| 8023 | // bound since it's initializing a function parameter. |
| 8024 | // FIXME: This is a mess. Rationalize temporary destruction. |
| 8025 | if (!shouldBindAsTemporary(Entity)) |
| 8026 | CurInit = S.MaybeBindToTemporary(CurInit.get()); |
| 8027 | CurInit = CopyObject(S, Step->Type, Entity, CurInit, |
| 8028 | /*IsExtraneousCopy=*/false); |
| 8029 | break; |
| 8030 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 8031 | case SK_ExtraneousCopyToTemporary: |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 8032 | CurInit = CopyObject(S, Step->Type, Entity, CurInit, |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 8033 | /*IsExtraneousCopy=*/true); |
| 8034 | break; |
| 8035 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8036 | case SK_UserConversion: { |
| 8037 | // We have a user-defined conversion that invokes either a constructor |
| 8038 | // or a conversion function. |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 8039 | CastKind CastKind; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8040 | FunctionDecl *Fn = Step->Function.Function; |
| 8041 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 8042 | bool HadMultipleCandidates = Step->Function.HadMultipleCandidates; |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 8043 | bool CreatedObject = false; |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 8044 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8045 | // Build a call to the selected constructor. |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 8046 | SmallVector<Expr*, 8> ConstructorArgs; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8047 | SourceLocation Loc = CurInit.get()->getBeginLoc(); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 8048 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8049 | // Determine the arguments required to actually perform the constructor |
| 8050 | // call. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8051 | Expr *Arg = CurInit.get(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8052 | if (S.CompleteConstructorCall(Constructor, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8053 | MultiExprArg(&Arg, 1), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8054 | Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8055 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8056 | |
Richard Smith | b24f067 | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 8057 | // Build an expression that constructs a temporary. |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 8058 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, |
| 8059 | FoundFn, Constructor, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 8060 | ConstructorArgs, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 8061 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 8062 | /*ListInit*/ false, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 8063 | /*StdInitListInit*/ false, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 8064 | /*ZeroInit*/ false, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 8065 | CXXConstructExpr::CK_Complete, |
| 8066 | SourceRange()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8067 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8068 | return ExprError(); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 8069 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 8070 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, FoundFn, |
| 8071 | Entity); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 8072 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 8073 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8074 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8075 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 8076 | CreatedObject = true; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8077 | } else { |
| 8078 | // Build a call to the conversion function. |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 8079 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8080 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), nullptr, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8081 | FoundFn); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 8082 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 8083 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8084 | |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 8085 | CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion, |
| 8086 | HadMultipleCandidates); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8087 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8088 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8089 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8090 | CastKind = CK_UserDefinedConversion; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 8091 | CreatedObject = Conversion->getReturnType()->isRecordType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8092 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8093 | |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 8094 | if (CreatedObject && checkAbstractType(CurInit.get()->getType())) |
| 8095 | return ExprError(); |
| 8096 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8097 | CurInit = ImplicitCastExpr::Create(S.Context, CurInit.get()->getType(), |
| 8098 | CastKind, CurInit.get(), nullptr, |
| 8099 | CurInit.get()->getValueKind()); |
Abramo Bagnara | b0cf297 | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 8100 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8101 | if (shouldBindAsTemporary(Entity)) |
| 8102 | // The overall entity is temporary, so this expression should be |
| 8103 | // destroyed at the end of its full-expression. |
| 8104 | CurInit = S.MaybeBindToTemporary(CurInit.getAs<Expr>()); |
| 8105 | else if (CreatedObject && shouldDestroyEntity(Entity)) { |
| 8106 | // The object outlasts the full-expression, but we need to prepare for |
| 8107 | // a destructor being run on it. |
| 8108 | // FIXME: It makes no sense to do this here. This should happen |
| 8109 | // regardless of how we initialized the entity. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8110 | QualType T = CurInit.get()->getType(); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 8111 | if (const RecordType *Record = T->getAs<RecordType>()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8112 | CXXDestructorDecl *Destructor |
Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 8113 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8114 | S.CheckDestructorAccess(CurInit.get()->getBeginLoc(), Destructor, |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 8115 | S.PDiag(diag::err_access_dtor_temp) << T); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8116 | S.MarkFunctionReferenced(CurInit.get()->getBeginLoc(), Destructor); |
| 8117 | if (S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getBeginLoc())) |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 8118 | return ExprError(); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 8119 | } |
| 8120 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8121 | break; |
| 8122 | } |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 8123 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8124 | case SK_QualificationConversionLValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 8125 | case SK_QualificationConversionXValue: |
| 8126 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8127 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 8128 | ExprValueKind VK = |
Anastasia Stulova | 0430794 | 2018-11-16 16:22:56 +0000 | [diff] [blame] | 8129 | Step->Kind == SK_QualificationConversionLValue |
| 8130 | ? VK_LValue |
| 8131 | : (Step->Kind == SK_QualificationConversionXValue ? VK_XValue |
| 8132 | : VK_RValue); |
| 8133 | CurInit = S.PerformQualificationConversion(CurInit.get(), Step->Type, VK); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8134 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 8135 | } |
| 8136 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 8137 | case SK_AtomicConversion: { |
| 8138 | assert(CurInit.get()->isRValue() && "cannot convert glvalue to atomic"); |
| 8139 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
| 8140 | CK_NonAtomicToAtomic, VK_RValue); |
| 8141 | break; |
| 8142 | } |
| 8143 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 8144 | case SK_ConversionSequence: |
| 8145 | case SK_ConversionSequenceNoNarrowing: { |
Leonard Chan | ad7ac96 | 2018-12-06 01:05:54 +0000 | [diff] [blame] | 8146 | if (const auto *FromPtrType = |
| 8147 | CurInit.get()->getType()->getAs<PointerType>()) { |
| 8148 | if (const auto *ToPtrType = Step->Type->getAs<PointerType>()) { |
| 8149 | if (FromPtrType->getPointeeType()->hasAttr(attr::NoDeref) && |
| 8150 | !ToPtrType->getPointeeType()->hasAttr(attr::NoDeref)) { |
| 8151 | S.Diag(CurInit.get()->getExprLoc(), |
| 8152 | diag::warn_noderef_to_dereferenceable_pointer) |
| 8153 | << CurInit.get()->getSourceRange(); |
| 8154 | } |
| 8155 | } |
| 8156 | } |
| 8157 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 8158 | Sema::CheckedConversionKind CCK |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8159 | = Kind.isCStyleCast()? Sema::CCK_CStyleCast |
| 8160 | : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 8161 | : Kind.isExplicitCast()? Sema::CCK_OtherCast |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8162 | : Sema::CCK_ImplicitConversion; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8163 | ExprResult CurInitExprRes = |
| 8164 | S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8165 | getAssignmentAction(Entity), CCK); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8166 | if (CurInitExprRes.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8167 | return ExprError(); |
Roger Ferrer Ibanez | 722a4db | 2016-08-12 08:04:13 +0000 | [diff] [blame] | 8168 | |
| 8169 | S.DiscardMisalignedMemberAddress(Step->Type.getTypePtr(), CurInit.get()); |
| 8170 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 8171 | CurInit = CurInitExprRes; |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 8172 | |
| 8173 | if (Step->Kind == SK_ConversionSequenceNoNarrowing && |
Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 8174 | S.getLangOpts().CPlusPlus) |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 8175 | DiagnoseNarrowingInInitList(S, *Step->ICS, SourceType, Entity.getType(), |
| 8176 | CurInit.get()); |
Roger Ferrer Ibanez | 722a4db | 2016-08-12 08:04:13 +0000 | [diff] [blame] | 8177 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8178 | break; |
Douglas Gregor | 5c8ffab | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 8179 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8180 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8181 | case SK_ListInitialization: { |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 8182 | if (checkAbstractType(Step->Type)) |
| 8183 | return ExprError(); |
| 8184 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8185 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 8186 | // If we're not initializing the top-level entity, we need to create an |
| 8187 | // InitializeTemporary entity for our target type. |
| 8188 | QualType Ty = Step->Type; |
| 8189 | bool IsTemporary = !S.Context.hasSameType(Entity.getType(), Ty); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8190 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(Ty); |
Richard Smith | d712d0d | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 8191 | InitializedEntity InitEntity = IsTemporary ? TempEntity : Entity; |
| 8192 | InitListChecker PerformInitList(S, InitEntity, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 8193 | InitList, Ty, /*VerifyOnly=*/false, |
| 8194 | /*TreatUnavailableAsInvalid=*/false); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 8195 | if (PerformInitList.HadError()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8196 | return ExprError(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8197 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 8198 | // Hack: We must update *ResultType if available in order to set the |
| 8199 | // bounds of arrays, e.g. in 'int ar[] = {1, 2, 3};'. |
| 8200 | // Worst case: 'const int (&arref)[] = {1, 2, 3};'. |
| 8201 | if (ResultType && |
| 8202 | ResultType->getNonReferenceType()->isIncompleteArrayType()) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8203 | if ((*ResultType)->isRValueReferenceType()) |
| 8204 | Ty = S.Context.getRValueReferenceType(Ty); |
| 8205 | else if ((*ResultType)->isLValueReferenceType()) |
| 8206 | Ty = S.Context.getLValueReferenceType(Ty, |
| 8207 | (*ResultType)->getAs<LValueReferenceType>()->isSpelledAsLValue()); |
| 8208 | *ResultType = Ty; |
| 8209 | } |
| 8210 | |
| 8211 | InitListExpr *StructuredInitList = |
| 8212 | PerformInitList.getFullyStructuredList(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 8213 | CurInit.get(); |
Richard Smith | d712d0d | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 8214 | CurInit = shouldBindAsTemporary(InitEntity) |
| 8215 | ? S.MaybeBindToTemporary(StructuredInitList) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 8216 | : StructuredInitList; |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8217 | break; |
| 8218 | } |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8219 | |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 8220 | case SK_ConstructorInitializationFromList: { |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 8221 | if (checkAbstractType(Step->Type)) |
| 8222 | return ExprError(); |
| 8223 | |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 8224 | // When an initializer list is passed for a parameter of type "reference |
| 8225 | // to object", we don't get an EK_Temporary entity, but instead an |
| 8226 | // EK_Parameter entity with reference type. |
Sebastian Redl | 99f6616 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 8227 | // FIXME: This is a hack. What we really should do is create a user |
| 8228 | // conversion step for this case, but this makes it considerably more |
| 8229 | // complicated. For now, this will do. |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 8230 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 8231 | Entity.getType().getNonReferenceType()); |
| 8232 | bool UseTemporary = Entity.getType()->isReferenceType(); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 8233 | assert(Args.size() == 1 && "expected a single argument for list init"); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 8234 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Richard Smith | 2b349ae | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 8235 | S.Diag(InitList->getExprLoc(), diag::warn_cxx98_compat_ctor_list_init) |
| 8236 | << InitList->getSourceRange(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 8237 | MultiExprArg Arg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 8238 | CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity : |
| 8239 | Entity, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 8240 | Kind, Arg, *Step, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 8241 | ConstructorInitRequiresZeroInit, |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 8242 | /*IsListInitialization*/true, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 8243 | /*IsStdInitListInit*/false, |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 8244 | InitList->getLBraceLoc(), |
| 8245 | InitList->getRBraceLoc()); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 8246 | break; |
| 8247 | } |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 8248 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8249 | case SK_UnwrapInitList: |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 8250 | CurInit = cast<InitListExpr>(CurInit.get())->getInit(0); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8251 | break; |
| 8252 | |
| 8253 | case SK_RewrapInitList: { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 8254 | Expr *E = CurInit.get(); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8255 | InitListExpr *Syntactic = Step->WrappingSyntacticList; |
| 8256 | InitListExpr *ILE = new (S.Context) InitListExpr(S.Context, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 8257 | Syntactic->getLBraceLoc(), E, Syntactic->getRBraceLoc()); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8258 | ILE->setSyntacticForm(Syntactic); |
| 8259 | ILE->setType(E->getType()); |
| 8260 | ILE->setValueKind(E->getValueKind()); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 8261 | CurInit = ILE; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8262 | break; |
| 8263 | } |
| 8264 | |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 8265 | case SK_ConstructorInitialization: |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 8266 | case SK_StdInitializerListConstructorCall: { |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 8267 | if (checkAbstractType(Step->Type)) |
| 8268 | return ExprError(); |
| 8269 | |
Sebastian Redl | 99f6616 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 8270 | // When an initializer list is passed for a parameter of type "reference |
| 8271 | // to object", we don't get an EK_Temporary entity, but instead an |
| 8272 | // EK_Parameter entity with reference type. |
| 8273 | // FIXME: This is a hack. What we really should do is create a user |
| 8274 | // conversion step for this case, but this makes it considerably more |
| 8275 | // complicated. For now, this will do. |
| 8276 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 8277 | Entity.getType().getNonReferenceType()); |
| 8278 | bool UseTemporary = Entity.getType()->isReferenceType(); |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 8279 | bool IsStdInitListInit = |
| 8280 | Step->Kind == SK_StdInitializerListConstructorCall; |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 8281 | Expr *Source = CurInit.get(); |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 8282 | SourceRange Range = Kind.hasParenOrBraceRange() |
| 8283 | ? Kind.getParenOrBraceRange() |
| 8284 | : SourceRange(); |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 8285 | CurInit = PerformConstructorInitialization( |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 8286 | S, UseTemporary ? TempEntity : Entity, Kind, |
| 8287 | Source ? MultiExprArg(Source) : Args, *Step, |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 8288 | ConstructorInitRequiresZeroInit, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 8289 | /*IsListInitialization*/ IsStdInitListInit, |
| 8290 | /*IsStdInitListInitialization*/ IsStdInitListInit, |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 8291 | /*LBraceLoc*/ Range.getBegin(), |
| 8292 | /*RBraceLoc*/ Range.getEnd()); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8293 | break; |
Sebastian Redl | 99f6616 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 8294 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8295 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 8296 | case SK_ZeroInitialization: { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 8297 | step_iterator NextStep = Step; |
| 8298 | ++NextStep; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8299 | if (NextStep != StepEnd && |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 8300 | (NextStep->Kind == SK_ConstructorInitialization || |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 8301 | NextStep->Kind == SK_ConstructorInitializationFromList)) { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 8302 | // The need for zero-initialization is recorded directly into |
| 8303 | // the call to the object's constructor within the next step. |
| 8304 | ConstructorInitRequiresZeroInit = true; |
| 8305 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8306 | S.getLangOpts().CPlusPlus && |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 8307 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 8308 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 8309 | if (!TSInfo) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8310 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 8311 | Kind.getRange().getBegin()); |
| 8312 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 8313 | CurInit = new (S.Context) CXXScalarValueInitExpr( |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 8314 | Entity.getType().getNonLValueExprType(S.Context), TSInfo, |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 8315 | Kind.getRange().getEnd()); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 8316 | } else { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 8317 | CurInit = new (S.Context) ImplicitValueInitExpr(Step->Type); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 8318 | } |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 8319 | break; |
| 8320 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8321 | |
| 8322 | case SK_CAssignment: { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8323 | QualType SourceType = CurInit.get()->getType(); |
Leonard Chan | ad7ac96 | 2018-12-06 01:05:54 +0000 | [diff] [blame] | 8324 | |
George Burgess IV | 5f21c71 | 2015-10-12 19:57:04 +0000 | [diff] [blame] | 8325 | // Save off the initial CurInit in case we need to emit a diagnostic |
| 8326 | ExprResult InitialCurInit = CurInit; |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 8327 | ExprResult Result = CurInit; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8328 | Sema::AssignConvertType ConvTy = |
Fariborz Jahanian | 25eef19 | 2013-07-31 21:40:51 +0000 | [diff] [blame] | 8329 | S.CheckSingleAssignmentConstraints(Step->Type, Result, true, |
| 8330 | Entity.getKind() == InitializedEntity::EK_Parameter_CF_Audited); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8331 | if (Result.isInvalid()) |
| 8332 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 8333 | CurInit = Result; |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 8334 | |
| 8335 | // If this is a call, allow conversion to a transparent union. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 8336 | ExprResult CurInitExprRes = CurInit; |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 8337 | if (ConvTy != Sema::Compatible && |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 8338 | Entity.isParameterKind() && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8339 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes) |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 8340 | == Sema::Compatible) |
| 8341 | ConvTy = Sema::Compatible; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8342 | if (CurInitExprRes.isInvalid()) |
| 8343 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 8344 | CurInit = CurInitExprRes; |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 8345 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 8346 | bool Complained; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8347 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 8348 | Step->Type, SourceType, |
George Burgess IV | 5f21c71 | 2015-10-12 19:57:04 +0000 | [diff] [blame] | 8349 | InitialCurInit.get(), |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 8350 | getAssignmentAction(Entity, true), |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 8351 | &Complained)) { |
| 8352 | PrintInitLocationNote(S, Entity); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8353 | return ExprError(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 8354 | } else if (Complained) |
| 8355 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8356 | break; |
| 8357 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 8358 | |
| 8359 | case SK_StringInit: { |
| 8360 | QualType Ty = Step->Type; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8361 | CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty, |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 8362 | S.Context.getAsArrayType(Ty), S); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 8363 | break; |
| 8364 | } |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 8365 | |
| 8366 | case SK_ObjCObjectConversion: |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 8367 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8368 | CK_ObjCObjectLValueCast, |
Eli Friedman | be4b363 | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 8369 | CurInit.get()->getValueKind()); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 8370 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8371 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 8372 | case SK_ArrayLoopIndex: { |
| 8373 | Expr *Cur = CurInit.get(); |
| 8374 | Expr *BaseExpr = new (S.Context) |
| 8375 | OpaqueValueExpr(Cur->getExprLoc(), Cur->getType(), |
| 8376 | Cur->getValueKind(), Cur->getObjectKind(), Cur); |
| 8377 | Expr *IndexExpr = |
| 8378 | new (S.Context) ArrayInitIndexExpr(S.Context.getSizeType()); |
| 8379 | CurInit = S.CreateBuiltinArraySubscriptExpr( |
| 8380 | BaseExpr, Kind.getLocation(), IndexExpr, Kind.getLocation()); |
| 8381 | ArrayLoopCommonExprs.push_back(BaseExpr); |
| 8382 | break; |
| 8383 | } |
| 8384 | |
| 8385 | case SK_ArrayLoopInit: { |
| 8386 | assert(!ArrayLoopCommonExprs.empty() && |
| 8387 | "mismatched SK_ArrayLoopIndex and SK_ArrayLoopInit"); |
| 8388 | Expr *Common = ArrayLoopCommonExprs.pop_back_val(); |
| 8389 | CurInit = new (S.Context) ArrayInitLoopExpr(Step->Type, Common, |
| 8390 | CurInit.get()); |
| 8391 | break; |
| 8392 | } |
| 8393 | |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 8394 | case SK_GNUArrayInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8395 | // Okay: we checked everything before creating this step. Note that |
| 8396 | // this is a GNU extension. |
| 8397 | S.Diag(Kind.getLocation(), diag::ext_array_init_copy) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8398 | << Step->Type << CurInit.get()->getType() |
| 8399 | << CurInit.get()->getSourceRange(); |
Eli Friedman | 88fccbd | 2019-02-11 22:54:27 +0000 | [diff] [blame] | 8400 | updateGNUCompoundLiteralRValue(CurInit.get()); |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 8401 | LLVM_FALLTHROUGH; |
| 8402 | case SK_ArrayInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8403 | // If the destination type is an incomplete array type, update the |
| 8404 | // type accordingly. |
| 8405 | if (ResultType) { |
| 8406 | if (const IncompleteArrayType *IncompleteDest |
| 8407 | = S.Context.getAsIncompleteArrayType(Step->Type)) { |
| 8408 | if (const ConstantArrayType *ConstantSource |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8409 | = S.Context.getAsConstantArrayType(CurInit.get()->getType())) { |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8410 | *ResultType = S.Context.getConstantArrayType( |
| 8411 | IncompleteDest->getElementType(), |
| 8412 | ConstantSource->getSize(), |
| 8413 | ArrayType::Normal, 0); |
| 8414 | } |
| 8415 | } |
| 8416 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8417 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8418 | |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 8419 | case SK_ParenthesizedArrayInit: |
| 8420 | // Okay: we checked everything before creating this step. Note that |
| 8421 | // this is a GNU extension. |
| 8422 | S.Diag(Kind.getLocation(), diag::ext_array_init_parens) |
| 8423 | << CurInit.get()->getSourceRange(); |
| 8424 | break; |
| 8425 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8426 | case SK_PassByIndirectCopyRestore: |
| 8427 | case SK_PassByIndirectRestore: |
| 8428 | checkIndirectCopyRestoreSource(S, CurInit.get()); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 8429 | CurInit = new (S.Context) ObjCIndirectCopyRestoreExpr( |
| 8430 | CurInit.get(), Step->Type, |
| 8431 | Step->Kind == SK_PassByIndirectCopyRestore); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8432 | break; |
| 8433 | |
| 8434 | case SK_ProduceObjCObject: |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 8435 | CurInit = |
| 8436 | ImplicitCastExpr::Create(S.Context, Step->Type, CK_ARCProduceObject, |
| 8437 | CurInit.get(), nullptr, VK_RValue); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8438 | break; |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 8439 | |
| 8440 | case SK_StdInitializerList: { |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 8441 | S.Diag(CurInit.get()->getExprLoc(), |
| 8442 | diag::warn_cxx98_compat_initializer_list_init) |
| 8443 | << CurInit.get()->getSourceRange(); |
Sebastian Redl | 249dee5 | 2012-03-05 19:35:43 +0000 | [diff] [blame] | 8444 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 8445 | // Materialize the temporary into memory. |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 8446 | MaterializeTemporaryExpr *MTE = S.CreateMaterializeTemporaryExpr( |
| 8447 | CurInit.get()->getType(), CurInit.get(), |
| 8448 | /*BoundToLvalueReference=*/false); |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 8449 | |
Florian Hahn | 0aa117d | 2018-07-17 09:23:31 +0000 | [diff] [blame] | 8450 | // Wrap it in a construction of a std::initializer_list<T>. |
| 8451 | CurInit = new (S.Context) CXXStdInitializerListExpr(Step->Type, MTE); |
Richard Smith | 0a9969b | 2018-07-17 00:11:41 +0000 | [diff] [blame] | 8452 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 8453 | // Bind the result, in case the library has given initializer_list a |
| 8454 | // non-trivial destructor. |
| 8455 | if (shouldBindAsTemporary(Entity)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 8456 | CurInit = S.MaybeBindToTemporary(CurInit.get()); |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 8457 | break; |
| 8458 | } |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 8459 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 8460 | case SK_OCLSamplerInit: { |
Raphael Isemann | b23ccec | 2018-12-10 12:37:46 +0000 | [diff] [blame] | 8461 | // Sampler initialization have 5 cases: |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 8462 | // 1. function argument passing |
| 8463 | // 1a. argument is a file-scope variable |
| 8464 | // 1b. argument is a function-scope variable |
| 8465 | // 1c. argument is one of caller function's parameters |
| 8466 | // 2. variable initialization |
| 8467 | // 2a. initializing a file-scope variable |
| 8468 | // 2b. initializing a function-scope variable |
| 8469 | // |
| 8470 | // For file-scope variables, since they cannot be initialized by function |
| 8471 | // call of __translate_sampler_initializer in LLVM IR, their references |
| 8472 | // need to be replaced by a cast from their literal initializers to |
| 8473 | // sampler type. Since sampler variables can only be used in function |
| 8474 | // calls as arguments, we only need to replace them when handling the |
| 8475 | // argument passing. |
| 8476 | assert(Step->Type->isSamplerT() && |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 8477 | "Sampler initialization on non-sampler type."); |
Sven van Haastregt | 06385d0 | 2019-08-12 12:44:26 +0000 | [diff] [blame] | 8478 | Expr *Init = CurInit.get()->IgnoreParens(); |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 8479 | QualType SourceType = Init->getType(); |
| 8480 | // Case 1 |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 8481 | if (Entity.isParameterKind()) { |
Egor Churaev | a8d2451 | 2017-04-05 09:02:56 +0000 | [diff] [blame] | 8482 | if (!SourceType->isSamplerT() && !SourceType->isIntegerType()) { |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 8483 | S.Diag(Kind.getLocation(), diag::err_sampler_argument_required) |
| 8484 | << SourceType; |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 8485 | break; |
| 8486 | } else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Init)) { |
| 8487 | auto Var = cast<VarDecl>(DRE->getDecl()); |
| 8488 | // Case 1b and 1c |
| 8489 | // No cast from integer to sampler is needed. |
| 8490 | if (!Var->hasGlobalStorage()) { |
| 8491 | CurInit = ImplicitCastExpr::Create(S.Context, Step->Type, |
| 8492 | CK_LValueToRValue, Init, |
| 8493 | /*BasePath=*/nullptr, VK_RValue); |
| 8494 | break; |
| 8495 | } |
| 8496 | // Case 1a |
| 8497 | // For function call with a file-scope sampler variable as argument, |
| 8498 | // get the integer literal. |
| 8499 | // Do not diagnose if the file-scope variable does not have initializer |
| 8500 | // since this has already been diagnosed when parsing the variable |
| 8501 | // declaration. |
| 8502 | if (!Var->getInit() || !isa<ImplicitCastExpr>(Var->getInit())) |
| 8503 | break; |
| 8504 | Init = cast<ImplicitCastExpr>(const_cast<Expr*>( |
| 8505 | Var->getInit()))->getSubExpr(); |
| 8506 | SourceType = Init->getType(); |
| 8507 | } |
| 8508 | } else { |
| 8509 | // Case 2 |
| 8510 | // Check initializer is 32 bit integer constant. |
| 8511 | // If the initializer is taken from global variable, do not diagnose since |
| 8512 | // this has already been done when parsing the variable declaration. |
| 8513 | if (!Init->isConstantInitializer(S.Context, false)) |
| 8514 | break; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8515 | |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 8516 | if (!SourceType->isIntegerType() || |
| 8517 | 32 != S.Context.getIntWidth(SourceType)) { |
| 8518 | S.Diag(Kind.getLocation(), diag::err_sampler_initializer_not_integer) |
| 8519 | << SourceType; |
| 8520 | break; |
| 8521 | } |
| 8522 | |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 8523 | Expr::EvalResult EVResult; |
| 8524 | Init->EvaluateAsInt(EVResult, S.Context); |
| 8525 | llvm::APSInt Result = EVResult.Val.getInt(); |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 8526 | const uint64_t SamplerValue = Result.getLimitedValue(); |
| 8527 | // 32-bit value of sampler's initializer is interpreted as |
| 8528 | // bit-field with the following structure: |
| 8529 | // |unspecified|Filter|Addressing Mode| Normalized Coords| |
| 8530 | // |31 6|5 4|3 1| 0| |
| 8531 | // This structure corresponds to enum values of sampler properties |
| 8532 | // defined in SPIR spec v1.2 and also opencl-c.h |
| 8533 | unsigned AddressingMode = (0x0E & SamplerValue) >> 1; |
| 8534 | unsigned FilterMode = (0x30 & SamplerValue) >> 4; |
Andrew Savonichev | 3fee351 | 2018-11-08 11:25:41 +0000 | [diff] [blame] | 8535 | if (FilterMode != 1 && FilterMode != 2 && |
| 8536 | !S.getOpenCLOptions().isEnabled( |
| 8537 | "cl_intel_device_side_avc_motion_estimation")) |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 8538 | S.Diag(Kind.getLocation(), |
| 8539 | diag::warn_sampler_initializer_invalid_bits) |
| 8540 | << "Filter Mode"; |
| 8541 | if (AddressingMode > 4) |
| 8542 | S.Diag(Kind.getLocation(), |
| 8543 | diag::warn_sampler_initializer_invalid_bits) |
| 8544 | << "Addressing Mode"; |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 8545 | } |
| 8546 | |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 8547 | // Cases 1a, 2a and 2b |
| 8548 | // Insert cast from integer to sampler. |
| 8549 | CurInit = S.ImpCastExprToType(Init, S.Context.OCLSamplerTy, |
| 8550 | CK_IntToOCLSampler); |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 8551 | break; |
| 8552 | } |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 8553 | case SK_OCLZeroOpaqueType: { |
Andrew Savonichev | 3fee351 | 2018-11-08 11:25:41 +0000 | [diff] [blame] | 8554 | assert((Step->Type->isEventT() || Step->Type->isQueueT() || |
| 8555 | Step->Type->isOCLIntelSubgroupAVCType()) && |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 8556 | "Wrong type for initialization of OpenCL opaque type."); |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 8557 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 8558 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 8559 | CK_ZeroToOCLOpaqueType, |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 8560 | CurInit.get()->getValueKind()); |
| 8561 | break; |
| 8562 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8563 | } |
| 8564 | } |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 8565 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 8566 | // Check whether the initializer has a shorter lifetime than the initialized |
| 8567 | // entity, and if not, either lifetime-extend or warn as appropriate. |
| 8568 | if (auto *Init = CurInit.get()) |
| 8569 | S.checkInitializerLifetime(Entity, Init); |
| 8570 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 8571 | // Diagnose non-fatal problems with the completed initialization. |
| 8572 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 8573 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 8574 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 8575 | cast<FieldDecl>(Entity.getDecl()), |
| 8576 | CurInit.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8577 | |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 8578 | // Check for std::move on construction. |
| 8579 | if (const Expr *E = CurInit.get()) { |
| 8580 | CheckMoveOnConstruction(S, E, |
| 8581 | Entity.getKind() == InitializedEntity::EK_Result); |
| 8582 | } |
| 8583 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 8584 | return CurInit; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8585 | } |
| 8586 | |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 8587 | /// Somewhere within T there is an uninitialized reference subobject. |
| 8588 | /// Dig it out and diagnose it. |
Benjamin Kramer | 3e35026 | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 8589 | static bool DiagnoseUninitializedReference(Sema &S, SourceLocation Loc, |
| 8590 | QualType T) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 8591 | if (T->isReferenceType()) { |
| 8592 | S.Diag(Loc, diag::err_reference_without_init) |
| 8593 | << T.getNonReferenceType(); |
| 8594 | return true; |
| 8595 | } |
| 8596 | |
| 8597 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 8598 | if (!RD || !RD->hasUninitializedReferenceMember()) |
| 8599 | return false; |
| 8600 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 8601 | for (const auto *FI : RD->fields()) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 8602 | if (FI->isUnnamedBitfield()) |
| 8603 | continue; |
| 8604 | |
| 8605 | if (DiagnoseUninitializedReference(S, FI->getLocation(), FI->getType())) { |
| 8606 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 8607 | return true; |
| 8608 | } |
| 8609 | } |
| 8610 | |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 8611 | for (const auto &BI : RD->bases()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8612 | if (DiagnoseUninitializedReference(S, BI.getBeginLoc(), BI.getType())) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 8613 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 8614 | return true; |
| 8615 | } |
| 8616 | } |
| 8617 | |
| 8618 | return false; |
| 8619 | } |
| 8620 | |
| 8621 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8622 | //===----------------------------------------------------------------------===// |
| 8623 | // Diagnose initialization failures |
| 8624 | //===----------------------------------------------------------------------===// |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 8625 | |
| 8626 | /// Emit notes associated with an initialization that failed due to a |
| 8627 | /// "simple" conversion failure. |
| 8628 | static void emitBadConversionNotes(Sema &S, const InitializedEntity &entity, |
| 8629 | Expr *op) { |
| 8630 | QualType destType = entity.getType(); |
| 8631 | if (destType.getNonReferenceType()->isObjCObjectPointerType() && |
| 8632 | op->getType()->isObjCObjectPointerType()) { |
| 8633 | |
| 8634 | // Emit a possible note about the conversion failing because the |
| 8635 | // operand is a message send with a related result type. |
| 8636 | S.EmitRelatedResultTypeNote(op); |
| 8637 | |
| 8638 | // Emit a possible note about a return failing because we're |
| 8639 | // expecting a related result type. |
| 8640 | if (entity.getKind() == InitializedEntity::EK_Result) |
| 8641 | S.EmitRelatedResultTypeNoteForReturn(destType); |
| 8642 | } |
| 8643 | } |
| 8644 | |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 8645 | static void diagnoseListInit(Sema &S, const InitializedEntity &Entity, |
| 8646 | InitListExpr *InitList) { |
| 8647 | QualType DestType = Entity.getType(); |
| 8648 | |
| 8649 | QualType E; |
| 8650 | if (S.getLangOpts().CPlusPlus11 && S.isStdInitializerList(DestType, &E)) { |
| 8651 | QualType ArrayType = S.Context.getConstantArrayType( |
| 8652 | E.withConst(), |
| 8653 | llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 8654 | InitList->getNumInits()), |
| 8655 | clang::ArrayType::Normal, 0); |
| 8656 | InitializedEntity HiddenArray = |
| 8657 | InitializedEntity::InitializeTemporary(ArrayType); |
| 8658 | return diagnoseListInit(S, HiddenArray, InitList); |
| 8659 | } |
| 8660 | |
Richard Smith | 8d082d1 | 2014-09-04 22:13:39 +0000 | [diff] [blame] | 8661 | if (DestType->isReferenceType()) { |
| 8662 | // A list-initialization failure for a reference means that we tried to |
| 8663 | // create a temporary of the inner type (per [dcl.init.list]p3.6) and the |
| 8664 | // inner initialization failed. |
| 8665 | QualType T = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 8666 | diagnoseListInit(S, InitializedEntity::InitializeTemporary(T), InitList); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8667 | SourceLocation Loc = InitList->getBeginLoc(); |
Richard Smith | 8d082d1 | 2014-09-04 22:13:39 +0000 | [diff] [blame] | 8668 | if (auto *D = Entity.getDecl()) |
| 8669 | Loc = D->getLocation(); |
| 8670 | S.Diag(Loc, diag::note_in_reference_temporary_list_initializer) << T; |
| 8671 | return; |
| 8672 | } |
| 8673 | |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 8674 | InitListChecker DiagnoseInitList(S, Entity, InitList, DestType, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 8675 | /*VerifyOnly=*/false, |
| 8676 | /*TreatUnavailableAsInvalid=*/false); |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 8677 | assert(DiagnoseInitList.HadError() && |
| 8678 | "Inconsistent init list check result."); |
| 8679 | } |
| 8680 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8681 | bool InitializationSequence::Diagnose(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8682 | const InitializedEntity &Entity, |
| 8683 | const InitializationKind &Kind, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8684 | ArrayRef<Expr *> Args) { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 8685 | if (!Failed()) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8686 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8687 | |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8688 | // When we want to diagnose only one element of a braced-init-list, |
| 8689 | // we need to factor it out. |
| 8690 | Expr *OnlyArg; |
| 8691 | if (Args.size() == 1) { |
| 8692 | auto *List = dyn_cast<InitListExpr>(Args[0]); |
| 8693 | if (List && List->getNumInits() == 1) |
| 8694 | OnlyArg = List->getInit(0); |
| 8695 | else |
| 8696 | OnlyArg = Args[0]; |
| 8697 | } |
| 8698 | else |
| 8699 | OnlyArg = nullptr; |
| 8700 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 8701 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8702 | switch (Failure) { |
| 8703 | case FK_TooManyInitsForReference: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8704 | // FIXME: Customize for the initialized entity? |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8705 | if (Args.empty()) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 8706 | // Dig out the reference subobject which is uninitialized and diagnose it. |
| 8707 | // If this is value-initialization, this could be nested some way within |
| 8708 | // the target type. |
| 8709 | assert(Kind.getKind() == InitializationKind::IK_Value || |
| 8710 | DestType->isReferenceType()); |
| 8711 | bool Diagnosed = |
| 8712 | DiagnoseUninitializedReference(S, Kind.getLocation(), DestType); |
| 8713 | assert(Diagnosed && "couldn't find uninitialized reference to diagnose"); |
| 8714 | (void)Diagnosed; |
| 8715 | } else // FIXME: diagnostic below could be better! |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8716 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 8717 | << SourceRange(Args.front()->getBeginLoc(), Args.back()->getEndLoc()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8718 | break; |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 8719 | case FK_ParenthesizedListInitForReference: |
| 8720 | S.Diag(Kind.getLocation(), diag::err_list_init_in_parens) |
| 8721 | << 1 << Entity.getType() << Args[0]->getSourceRange(); |
| 8722 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8723 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8724 | case FK_ArrayNeedsInitList: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 8725 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 0; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8726 | break; |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 8727 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 8728 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 1; |
| 8729 | break; |
| 8730 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 8731 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 2; |
| 8732 | break; |
| 8733 | case FK_NarrowStringIntoWideCharArray: |
| 8734 | S.Diag(Kind.getLocation(), diag::err_array_init_narrow_string_into_wchar); |
| 8735 | break; |
| 8736 | case FK_WideStringIntoCharArray: |
| 8737 | S.Diag(Kind.getLocation(), diag::err_array_init_wide_string_into_char); |
| 8738 | break; |
| 8739 | case FK_IncompatWideStringIntoWideChar: |
| 8740 | S.Diag(Kind.getLocation(), |
| 8741 | diag::err_array_init_incompat_wide_string_into_wchar); |
| 8742 | break; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 8743 | case FK_PlainStringIntoUTF8Char: |
| 8744 | S.Diag(Kind.getLocation(), |
| 8745 | diag::err_array_init_plain_string_into_char8_t); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8746 | S.Diag(Args.front()->getBeginLoc(), |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 8747 | diag::note_array_init_plain_string_into_char8_t) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8748 | << FixItHint::CreateInsertion(Args.front()->getBeginLoc(), "u8"); |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 8749 | break; |
| 8750 | case FK_UTF8StringIntoPlainChar: |
| 8751 | S.Diag(Kind.getLocation(), |
Richard Smith | 28ddb91 | 2018-11-14 21:04:34 +0000 | [diff] [blame] | 8752 | diag::err_array_init_utf8_string_into_char) |
| 8753 | << S.getLangOpts().CPlusPlus2a; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 8754 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8755 | case FK_ArrayTypeMismatch: |
| 8756 | case FK_NonConstantArrayInit: |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 8757 | S.Diag(Kind.getLocation(), |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8758 | (Failure == FK_ArrayTypeMismatch |
| 8759 | ? diag::err_array_init_different_type |
| 8760 | : diag::err_array_init_non_constant_array)) |
| 8761 | << DestType.getNonReferenceType() |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8762 | << OnlyArg->getType() |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8763 | << Args[0]->getSourceRange(); |
| 8764 | break; |
| 8765 | |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 8766 | case FK_VariableLengthArrayHasInitializer: |
| 8767 | S.Diag(Kind.getLocation(), diag::err_variable_object_no_init) |
| 8768 | << Args[0]->getSourceRange(); |
| 8769 | break; |
| 8770 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8771 | case FK_AddressOfOverloadFailed: { |
| 8772 | DeclAccessPair Found; |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8773 | S.ResolveAddressOfOverloadedFunction(OnlyArg, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8774 | DestType.getNonReferenceType(), |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8775 | true, |
| 8776 | Found); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8777 | break; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8778 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8779 | |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 8780 | case FK_AddressOfUnaddressableFunction: { |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8781 | auto *FD = cast<FunctionDecl>(cast<DeclRefExpr>(OnlyArg)->getDecl()); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 8782 | S.checkAddressOfFunctionIsAvailable(FD, /*Complain=*/true, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8783 | OnlyArg->getBeginLoc()); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 8784 | break; |
| 8785 | } |
| 8786 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8787 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 8788 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8789 | switch (FailedOverloadResult) { |
| 8790 | case OR_Ambiguous: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8791 | |
David Blaikie | 5e32805 | 2019-05-03 00:44:50 +0000 | [diff] [blame] | 8792 | FailedCandidateSet.NoteCandidates( |
| 8793 | PartialDiagnosticAt( |
| 8794 | Kind.getLocation(), |
| 8795 | Failure == FK_UserConversionOverloadFailed |
| 8796 | ? (S.PDiag(diag::err_typecheck_ambiguous_condition) |
| 8797 | << OnlyArg->getType() << DestType |
| 8798 | << Args[0]->getSourceRange()) |
| 8799 | : (S.PDiag(diag::err_ref_init_ambiguous) |
| 8800 | << DestType << OnlyArg->getType() |
| 8801 | << Args[0]->getSourceRange())), |
| 8802 | S, OCD_ViableCandidates, Args); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8803 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8804 | |
David Blaikie | 5e32805 | 2019-05-03 00:44:50 +0000 | [diff] [blame] | 8805 | case OR_No_Viable_Function: { |
| 8806 | auto Cands = FailedCandidateSet.CompleteCandidates(S, OCD_AllCandidates, Args); |
Larisse Voufo | 70bb43a | 2013-06-27 03:36:30 +0000 | [diff] [blame] | 8807 | if (!S.RequireCompleteType(Kind.getLocation(), |
Larisse Voufo | 64cf3ef | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 8808 | DestType.getNonReferenceType(), |
| 8809 | diag::err_typecheck_nonviable_condition_incomplete, |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8810 | OnlyArg->getType(), Args[0]->getSourceRange())) |
Larisse Voufo | 64cf3ef | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 8811 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
Nick Lewycky | 08426e2 | 2015-08-25 22:18:46 +0000 | [diff] [blame] | 8812 | << (Entity.getKind() == InitializedEntity::EK_Result) |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8813 | << OnlyArg->getType() << Args[0]->getSourceRange() |
Larisse Voufo | 64cf3ef | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 8814 | << DestType.getNonReferenceType(); |
| 8815 | |
David Blaikie | 5e32805 | 2019-05-03 00:44:50 +0000 | [diff] [blame] | 8816 | FailedCandidateSet.NoteCandidates(S, Args, Cands); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8817 | break; |
David Blaikie | 5e32805 | 2019-05-03 00:44:50 +0000 | [diff] [blame] | 8818 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8819 | case OR_Deleted: { |
| 8820 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8821 | << OnlyArg->getType() << DestType.getNonReferenceType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8822 | << Args[0]->getSourceRange(); |
| 8823 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8824 | OverloadingResult Ovl |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 8825 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8826 | if (Ovl == OR_Deleted) { |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 8827 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8828 | } else { |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 8829 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8830 | } |
| 8831 | break; |
| 8832 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8833 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8834 | case OR_Success: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 8835 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8836 | } |
| 8837 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8838 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8839 | case FK_NonConstLValueReferenceBindingToTemporary: |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8840 | if (isa<InitListExpr>(Args[0])) { |
| 8841 | S.Diag(Kind.getLocation(), |
| 8842 | diag::err_lvalue_reference_bind_to_initlist) |
| 8843 | << DestType.getNonReferenceType().isVolatileQualified() |
| 8844 | << DestType.getNonReferenceType() |
| 8845 | << Args[0]->getSourceRange(); |
| 8846 | break; |
| 8847 | } |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 8848 | LLVM_FALLTHROUGH; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8849 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8850 | case FK_NonConstLValueReferenceBindingToUnrelated: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8851 | S.Diag(Kind.getLocation(), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8852 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 8853 | ? diag::err_lvalue_reference_bind_to_temporary |
| 8854 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | d1e0864 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 8855 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8856 | << DestType.getNonReferenceType() |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8857 | << OnlyArg->getType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8858 | << Args[0]->getSourceRange(); |
| 8859 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8860 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8861 | case FK_NonConstLValueReferenceBindingToBitfield: { |
| 8862 | // We don't necessarily have an unambiguous source bit-field. |
| 8863 | FieldDecl *BitField = Args[0]->getSourceBitField(); |
| 8864 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
| 8865 | << DestType.isVolatileQualified() |
| 8866 | << (BitField ? BitField->getDeclName() : DeclarationName()) |
| 8867 | << (BitField != nullptr) |
| 8868 | << Args[0]->getSourceRange(); |
| 8869 | if (BitField) |
| 8870 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
| 8871 | break; |
| 8872 | } |
| 8873 | |
| 8874 | case FK_NonConstLValueReferenceBindingToVectorElement: |
| 8875 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 8876 | << DestType.isVolatileQualified() |
| 8877 | << Args[0]->getSourceRange(); |
| 8878 | break; |
| 8879 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8880 | case FK_RValueReferenceBindingToLValue: |
| 8881 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8882 | << DestType.getNonReferenceType() << OnlyArg->getType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8883 | << Args[0]->getSourceRange(); |
| 8884 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8885 | |
Anastasia Stulova | 5145b1e | 2019-06-05 14:03:34 +0000 | [diff] [blame] | 8886 | case FK_ReferenceAddrspaceMismatchTemporary: |
| 8887 | S.Diag(Kind.getLocation(), diag::err_reference_bind_temporary_addrspace) |
| 8888 | << DestType << Args[0]->getSourceRange(); |
| 8889 | break; |
| 8890 | |
Richard Trieu | f956a49 | 2015-05-16 01:27:03 +0000 | [diff] [blame] | 8891 | case FK_ReferenceInitDropsQualifiers: { |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8892 | QualType SourceType = OnlyArg->getType(); |
Richard Trieu | f956a49 | 2015-05-16 01:27:03 +0000 | [diff] [blame] | 8893 | QualType NonRefType = DestType.getNonReferenceType(); |
| 8894 | Qualifiers DroppedQualifiers = |
| 8895 | SourceType.getQualifiers() - NonRefType.getQualifiers(); |
| 8896 | |
Anastasia Stulova | 3562edb | 2019-06-21 11:36:15 +0000 | [diff] [blame] | 8897 | if (!NonRefType.getQualifiers().isAddressSpaceSupersetOf( |
| 8898 | SourceType.getQualifiers())) |
| 8899 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
| 8900 | << NonRefType << SourceType << 1 /*addr space*/ |
| 8901 | << Args[0]->getSourceRange(); |
| 8902 | else |
| 8903 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
| 8904 | << NonRefType << SourceType << 0 /*cv quals*/ |
| 8905 | << Qualifiers::fromCVRMask(DroppedQualifiers.getCVRQualifiers()) |
| 8906 | << DroppedQualifiers.getCVRQualifiers() << Args[0]->getSourceRange(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8907 | break; |
Richard Trieu | f956a49 | 2015-05-16 01:27:03 +0000 | [diff] [blame] | 8908 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8909 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8910 | case FK_ReferenceInitFailed: |
| 8911 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 8912 | << DestType.getNonReferenceType() |
Eric Fiselier | 1147f71 | 2019-02-01 22:06:02 +0000 | [diff] [blame] | 8913 | << DestType.getNonReferenceType()->isIncompleteType() |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8914 | << OnlyArg->isLValue() |
| 8915 | << OnlyArg->getType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8916 | << Args[0]->getSourceRange(); |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 8917 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8918 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8919 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8920 | case FK_ConversionFailed: { |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8921 | QualType FromType = OnlyArg->getType(); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8922 | PartialDiagnostic PDiag = S.PDiag(diag::err_init_conversion_failed) |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8923 | << (int)Entity.getKind() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8924 | << DestType |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8925 | << OnlyArg->isLValue() |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8926 | << FromType |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8927 | << Args[0]->getSourceRange(); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8928 | S.HandleFunctionTypeMismatch(PDiag, FromType, DestType); |
| 8929 | S.Diag(Kind.getLocation(), PDiag); |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 8930 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8931 | break; |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8932 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8933 | |
| 8934 | case FK_ConversionFromPropertyFailed: |
| 8935 | // No-op. This error has already been reported. |
| 8936 | break; |
| 8937 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8938 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 8939 | SourceRange R; |
| 8940 | |
David Majnemer | bd38544 | 2015-04-10 04:52:06 +0000 | [diff] [blame] | 8941 | auto *InitList = dyn_cast<InitListExpr>(Args[0]); |
Benjamin Kramer | c4284e3 | 2015-09-23 16:03:53 +0000 | [diff] [blame] | 8942 | if (InitList && InitList->getNumInits() >= 1) { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 8943 | R = SourceRange(InitList->getInit(0)->getEndLoc(), InitList->getEndLoc()); |
Benjamin Kramer | c4284e3 | 2015-09-23 16:03:53 +0000 | [diff] [blame] | 8944 | } else { |
| 8945 | assert(Args.size() > 1 && "Expected multiple initializers!"); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 8946 | R = SourceRange(Args.front()->getEndLoc(), Args.back()->getEndLoc()); |
Benjamin Kramer | c4284e3 | 2015-09-23 16:03:53 +0000 | [diff] [blame] | 8947 | } |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8948 | |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 8949 | R.setBegin(S.getLocForEndOfToken(R.getBegin())); |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 8950 | if (Kind.isCStyleOrFunctionalCast()) |
| 8951 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 8952 | << R; |
| 8953 | else |
| 8954 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 8955 | << /*scalar=*/2 << R; |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8956 | break; |
| 8957 | } |
| 8958 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 8959 | case FK_ParenthesizedListInitForScalar: |
| 8960 | S.Diag(Kind.getLocation(), diag::err_list_init_in_parens) |
| 8961 | << 0 << Entity.getType() << Args[0]->getSourceRange(); |
| 8962 | break; |
| 8963 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8964 | case FK_ReferenceBindingToInitList: |
| 8965 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 8966 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 8967 | break; |
| 8968 | |
| 8969 | case FK_InitListBadDestinationType: |
| 8970 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 8971 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 8972 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8973 | |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 8974 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8975 | case FK_ConstructorOverloadFailed: { |
| 8976 | SourceRange ArgsRange; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8977 | if (Args.size()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8978 | ArgsRange = |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 8979 | SourceRange(Args.front()->getBeginLoc(), Args.back()->getEndLoc()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8980 | |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 8981 | if (Failure == FK_ListConstructorOverloadFailed) { |
Nico Weber | 9709ebf | 2014-07-08 23:54:25 +0000 | [diff] [blame] | 8982 | assert(Args.size() == 1 && |
| 8983 | "List construction from other than 1 argument."); |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 8984 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8985 | Args = MultiExprArg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 8986 | } |
| 8987 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8988 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 8989 | // bad. |
| 8990 | switch (FailedOverloadResult) { |
| 8991 | case OR_Ambiguous: |
David Blaikie | 5e32805 | 2019-05-03 00:44:50 +0000 | [diff] [blame] | 8992 | FailedCandidateSet.NoteCandidates( |
| 8993 | PartialDiagnosticAt(Kind.getLocation(), |
| 8994 | S.PDiag(diag::err_ovl_ambiguous_init) |
| 8995 | << DestType << ArgsRange), |
| 8996 | S, OCD_ViableCandidates, Args); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8997 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8998 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8999 | case OR_No_Viable_Function: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 9000 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 9001 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 9002 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 9003 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 9004 | // This is implicit default initialization of a member or |
| 9005 | // base within a constructor. If no viable function was |
Nico Weber | a691689 | 2016-06-10 18:53:04 +0000 | [diff] [blame] | 9006 | // found, notify the user that they need to explicitly |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 9007 | // initialize this base/member. |
| 9008 | CXXConstructorDecl *Constructor |
| 9009 | = cast<CXXConstructorDecl>(S.CurContext); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 9010 | const CXXRecordDecl *InheritedFrom = nullptr; |
| 9011 | if (auto Inherited = Constructor->getInheritedConstructor()) |
| 9012 | InheritedFrom = Inherited.getShadowDecl()->getNominatedBaseClass(); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 9013 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 9014 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 9015 | << (InheritedFrom ? 2 : Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 9016 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 9017 | << /*base=*/0 |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 9018 | << Entity.getType() |
| 9019 | << InheritedFrom; |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 9020 | |
| 9021 | RecordDecl *BaseDecl |
| 9022 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 9023 | ->getDecl(); |
| 9024 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 9025 | << S.Context.getTagDeclType(BaseDecl); |
| 9026 | } else { |
| 9027 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 9028 | << (InheritedFrom ? 2 : Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 9029 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 9030 | << /*member=*/1 |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 9031 | << Entity.getName() |
| 9032 | << InheritedFrom; |
Alp Toker | 2afa878 | 2014-05-28 12:20:14 +0000 | [diff] [blame] | 9033 | S.Diag(Entity.getDecl()->getLocation(), |
| 9034 | diag::note_member_declared_at); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 9035 | |
| 9036 | if (const RecordType *Record |
| 9037 | = Entity.getType()->getAs<RecordType>()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9038 | S.Diag(Record->getDecl()->getLocation(), |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 9039 | diag::note_previous_decl) |
| 9040 | << S.Context.getTagDeclType(Record->getDecl()); |
| 9041 | } |
| 9042 | break; |
| 9043 | } |
| 9044 | |
David Blaikie | 5e32805 | 2019-05-03 00:44:50 +0000 | [diff] [blame] | 9045 | FailedCandidateSet.NoteCandidates( |
| 9046 | PartialDiagnosticAt( |
| 9047 | Kind.getLocation(), |
| 9048 | S.PDiag(diag::err_ovl_no_viable_function_in_init) |
| 9049 | << DestType << ArgsRange), |
| 9050 | S, OCD_AllCandidates, Args); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 9051 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9052 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 9053 | case OR_Deleted: { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 9054 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9055 | OverloadingResult Ovl |
| 9056 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 9057 | if (Ovl != OR_Deleted) { |
| 9058 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
Erik Pilkington | 13ee62f | 2019-03-20 19:26:33 +0000 | [diff] [blame] | 9059 | << DestType << ArgsRange; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 9060 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 9061 | break; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 9062 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9063 | |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 9064 | // If this is a defaulted or implicitly-declared function, then |
| 9065 | // it was implicitly deleted. Make it clear that the deletion was |
| 9066 | // implicit. |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 9067 | if (S.isImplicitlyDeleted(Best->Function)) |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 9068 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_special_init) |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 9069 | << S.getSpecialMember(cast<CXXMethodDecl>(Best->Function)) |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 9070 | << DestType << ArgsRange; |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 9071 | else |
| 9072 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
Erik Pilkington | 13ee62f | 2019-03-20 19:26:33 +0000 | [diff] [blame] | 9073 | << DestType << ArgsRange; |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 9074 | |
| 9075 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 9076 | break; |
| 9077 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9078 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 9079 | case OR_Success: |
| 9080 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 9081 | } |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 9082 | } |
David Blaikie | 60deeee | 2012-01-17 08:24:58 +0000 | [diff] [blame] | 9083 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9084 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 9085 | case FK_DefaultInitOfConst: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 9086 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 9087 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 9088 | // This is implicit default-initialization of a const member in |
| 9089 | // a constructor. Complain that it needs to be explicitly |
| 9090 | // initialized. |
| 9091 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 9092 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 9093 | << (Constructor->getInheritedConstructor() ? 2 : |
| 9094 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 9095 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 9096 | << /*const=*/1 |
| 9097 | << Entity.getName(); |
| 9098 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 9099 | << Entity.getName(); |
| 9100 | } else { |
| 9101 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
Nico Weber | 9386c82 | 2014-07-23 05:16:10 +0000 | [diff] [blame] | 9102 | << DestType << (bool)DestType->getAs<RecordType>(); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 9103 | } |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 9104 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9105 | |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 9106 | case FK_Incomplete: |
Douglas Gregor | 85f3423 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 9107 | S.RequireCompleteType(Kind.getLocation(), FailedIncompleteType, |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 9108 | diag::err_init_incomplete_type); |
| 9109 | break; |
| 9110 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 9111 | case FK_ListInitializationFailed: { |
| 9112 | // Run the init list checker again to emit diagnostics. |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 9113 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
| 9114 | diagnoseListInit(S, Entity, InitList); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 9115 | break; |
| 9116 | } |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 9117 | |
| 9118 | case FK_PlaceholderType: { |
| 9119 | // FIXME: Already diagnosed! |
| 9120 | break; |
| 9121 | } |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 9122 | |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 9123 | case FK_ExplicitConstructor: { |
| 9124 | S.Diag(Kind.getLocation(), diag::err_selected_explicit_constructor) |
| 9125 | << Args[0]->getSourceRange(); |
| 9126 | OverloadCandidateSet::iterator Best; |
| 9127 | OverloadingResult Ovl |
| 9128 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Matt Beaumont-Gay | 5dcce09 | 2012-04-02 19:05:35 +0000 | [diff] [blame] | 9129 | (void)Ovl; |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 9130 | assert(Ovl == OR_Success && "Inconsistent overload resolution"); |
| 9131 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9132 | S.Diag(CtorDecl->getLocation(), |
| 9133 | diag::note_explicit_ctor_deduction_guide_here) << false; |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 9134 | break; |
| 9135 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 9136 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9137 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 9138 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 9139 | return true; |
| 9140 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 9141 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9142 | void InitializationSequence::dump(raw_ostream &OS) const { |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9143 | switch (SequenceKind) { |
| 9144 | case FailedSequence: { |
| 9145 | OS << "Failed sequence: "; |
| 9146 | switch (Failure) { |
| 9147 | case FK_TooManyInitsForReference: |
| 9148 | OS << "too many initializers for reference"; |
| 9149 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9150 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 9151 | case FK_ParenthesizedListInitForReference: |
| 9152 | OS << "parenthesized list init for reference"; |
| 9153 | break; |
| 9154 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9155 | case FK_ArrayNeedsInitList: |
| 9156 | OS << "array requires initializer list"; |
| 9157 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9158 | |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 9159 | case FK_AddressOfUnaddressableFunction: |
| 9160 | OS << "address of unaddressable function was taken"; |
| 9161 | break; |
| 9162 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9163 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 9164 | OS << "array requires initializer list or string literal"; |
| 9165 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9166 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 9167 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 9168 | OS << "array requires initializer list or wide string literal"; |
| 9169 | break; |
| 9170 | |
| 9171 | case FK_NarrowStringIntoWideCharArray: |
| 9172 | OS << "narrow string into wide char array"; |
| 9173 | break; |
| 9174 | |
| 9175 | case FK_WideStringIntoCharArray: |
| 9176 | OS << "wide string into char array"; |
| 9177 | break; |
| 9178 | |
| 9179 | case FK_IncompatWideStringIntoWideChar: |
| 9180 | OS << "incompatible wide string into wide char array"; |
| 9181 | break; |
| 9182 | |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 9183 | case FK_PlainStringIntoUTF8Char: |
| 9184 | OS << "plain string literal into char8_t array"; |
| 9185 | break; |
| 9186 | |
| 9187 | case FK_UTF8StringIntoPlainChar: |
| 9188 | OS << "u8 string literal into char array"; |
| 9189 | break; |
| 9190 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 9191 | case FK_ArrayTypeMismatch: |
| 9192 | OS << "array type mismatch"; |
| 9193 | break; |
| 9194 | |
| 9195 | case FK_NonConstantArrayInit: |
| 9196 | OS << "non-constant array initializer"; |
| 9197 | break; |
| 9198 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9199 | case FK_AddressOfOverloadFailed: |
| 9200 | OS << "address of overloaded function failed"; |
| 9201 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9202 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9203 | case FK_ReferenceInitOverloadFailed: |
| 9204 | OS << "overload resolution for reference initialization failed"; |
| 9205 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9206 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9207 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 9208 | OS << "non-const lvalue reference bound to temporary"; |
| 9209 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9210 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 9211 | case FK_NonConstLValueReferenceBindingToBitfield: |
| 9212 | OS << "non-const lvalue reference bound to bit-field"; |
| 9213 | break; |
| 9214 | |
| 9215 | case FK_NonConstLValueReferenceBindingToVectorElement: |
| 9216 | OS << "non-const lvalue reference bound to vector element"; |
| 9217 | break; |
| 9218 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9219 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 9220 | OS << "non-const lvalue reference bound to unrelated type"; |
| 9221 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9222 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9223 | case FK_RValueReferenceBindingToLValue: |
| 9224 | OS << "rvalue reference bound to an lvalue"; |
| 9225 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9226 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9227 | case FK_ReferenceInitDropsQualifiers: |
| 9228 | OS << "reference initialization drops qualifiers"; |
| 9229 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9230 | |
Anastasia Stulova | 5145b1e | 2019-06-05 14:03:34 +0000 | [diff] [blame] | 9231 | case FK_ReferenceAddrspaceMismatchTemporary: |
| 9232 | OS << "reference with mismatching address space bound to temporary"; |
| 9233 | break; |
| 9234 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9235 | case FK_ReferenceInitFailed: |
| 9236 | OS << "reference initialization failed"; |
| 9237 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9238 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9239 | case FK_ConversionFailed: |
| 9240 | OS << "conversion failed"; |
| 9241 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9242 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9243 | case FK_ConversionFromPropertyFailed: |
| 9244 | OS << "conversion from property failed"; |
| 9245 | break; |
| 9246 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9247 | case FK_TooManyInitsForScalar: |
| 9248 | OS << "too many initializers for scalar"; |
| 9249 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9250 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 9251 | case FK_ParenthesizedListInitForScalar: |
| 9252 | OS << "parenthesized list init for reference"; |
| 9253 | break; |
| 9254 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9255 | case FK_ReferenceBindingToInitList: |
| 9256 | OS << "referencing binding to initializer list"; |
| 9257 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9258 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9259 | case FK_InitListBadDestinationType: |
| 9260 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 9261 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9262 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9263 | case FK_UserConversionOverloadFailed: |
| 9264 | OS << "overloading failed for user-defined conversion"; |
| 9265 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9266 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9267 | case FK_ConstructorOverloadFailed: |
| 9268 | OS << "constructor overloading failed"; |
| 9269 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9270 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9271 | case FK_DefaultInitOfConst: |
| 9272 | OS << "default initialization of a const variable"; |
| 9273 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9274 | |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 9275 | case FK_Incomplete: |
| 9276 | OS << "initialization of incomplete type"; |
| 9277 | break; |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 9278 | |
| 9279 | case FK_ListInitializationFailed: |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 9280 | OS << "list initialization checker failure"; |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 9281 | break; |
| 9282 | |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 9283 | case FK_VariableLengthArrayHasInitializer: |
| 9284 | OS << "variable length array has an initializer"; |
| 9285 | break; |
| 9286 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 9287 | case FK_PlaceholderType: |
| 9288 | OS << "initializer expression isn't contextually valid"; |
| 9289 | break; |
Nick Lewycky | 097f47c | 2011-12-22 20:21:32 +0000 | [diff] [blame] | 9290 | |
| 9291 | case FK_ListConstructorOverloadFailed: |
| 9292 | OS << "list constructor overloading failed"; |
| 9293 | break; |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 9294 | |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 9295 | case FK_ExplicitConstructor: |
| 9296 | OS << "list copy initialization chose explicit constructor"; |
| 9297 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9298 | } |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9299 | OS << '\n'; |
| 9300 | return; |
| 9301 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9302 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9303 | case DependentSequence: |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 9304 | OS << "Dependent sequence\n"; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9305 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9306 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 9307 | case NormalSequence: |
| 9308 | OS << "Normal sequence: "; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9309 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9310 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9311 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9312 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 9313 | if (S != step_begin()) { |
| 9314 | OS << " -> "; |
| 9315 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9316 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9317 | switch (S->Kind) { |
| 9318 | case SK_ResolveAddressOfOverloadedFunction: |
| 9319 | OS << "resolve address of overloaded function"; |
| 9320 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9321 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9322 | case SK_CastDerivedToBaseRValue: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 9323 | OS << "derived-to-base (rvalue)"; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9324 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9325 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 9326 | case SK_CastDerivedToBaseXValue: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 9327 | OS << "derived-to-base (xvalue)"; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 9328 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9329 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9330 | case SK_CastDerivedToBaseLValue: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 9331 | OS << "derived-to-base (lvalue)"; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9332 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9333 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9334 | case SK_BindReference: |
| 9335 | OS << "bind reference to lvalue"; |
| 9336 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9337 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9338 | case SK_BindReferenceToTemporary: |
| 9339 | OS << "bind reference to a temporary"; |
| 9340 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9341 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 9342 | case SK_FinalCopy: |
| 9343 | OS << "final copy in class direct-initialization"; |
| 9344 | break; |
| 9345 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 9346 | case SK_ExtraneousCopyToTemporary: |
| 9347 | OS << "extraneous C++03 copy to temporary"; |
| 9348 | break; |
| 9349 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9350 | case SK_UserConversion: |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 9351 | OS << "user-defined conversion via " << *S->Function.Function; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9352 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 9353 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9354 | case SK_QualificationConversionRValue: |
| 9355 | OS << "qualification conversion (rvalue)"; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 9356 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9357 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 9358 | case SK_QualificationConversionXValue: |
| 9359 | OS << "qualification conversion (xvalue)"; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 9360 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 9361 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9362 | case SK_QualificationConversionLValue: |
| 9363 | OS << "qualification conversion (lvalue)"; |
| 9364 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9365 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 9366 | case SK_AtomicConversion: |
| 9367 | OS << "non-atomic-to-atomic conversion"; |
| 9368 | break; |
| 9369 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9370 | case SK_ConversionSequence: |
| 9371 | OS << "implicit conversion sequence ("; |
Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 9372 | S->ICS->dump(); // FIXME: use OS |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9373 | OS << ")"; |
| 9374 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9375 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 9376 | case SK_ConversionSequenceNoNarrowing: |
| 9377 | OS << "implicit conversion sequence with narrowing prohibited ("; |
Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 9378 | S->ICS->dump(); // FIXME: use OS |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 9379 | OS << ")"; |
| 9380 | break; |
| 9381 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9382 | case SK_ListInitialization: |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 9383 | OS << "list aggregate initialization"; |
| 9384 | break; |
| 9385 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 9386 | case SK_UnwrapInitList: |
| 9387 | OS << "unwrap reference initializer list"; |
| 9388 | break; |
| 9389 | |
| 9390 | case SK_RewrapInitList: |
| 9391 | OS << "rewrap reference initializer list"; |
| 9392 | break; |
| 9393 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9394 | case SK_ConstructorInitialization: |
| 9395 | OS << "constructor initialization"; |
| 9396 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9397 | |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 9398 | case SK_ConstructorInitializationFromList: |
| 9399 | OS << "list initialization via constructor"; |
| 9400 | break; |
| 9401 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9402 | case SK_ZeroInitialization: |
| 9403 | OS << "zero initialization"; |
| 9404 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9405 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9406 | case SK_CAssignment: |
| 9407 | OS << "C assignment"; |
| 9408 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9409 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9410 | case SK_StringInit: |
| 9411 | OS << "string initialization"; |
| 9412 | break; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 9413 | |
| 9414 | case SK_ObjCObjectConversion: |
| 9415 | OS << "Objective-C object conversion"; |
| 9416 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 9417 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 9418 | case SK_ArrayLoopIndex: |
| 9419 | OS << "indexing for array initialization loop"; |
| 9420 | break; |
| 9421 | |
| 9422 | case SK_ArrayLoopInit: |
| 9423 | OS << "array initialization loop"; |
| 9424 | break; |
| 9425 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 9426 | case SK_ArrayInit: |
| 9427 | OS << "array initialization"; |
| 9428 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9429 | |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 9430 | case SK_GNUArrayInit: |
| 9431 | OS << "array initialization (GNU extension)"; |
| 9432 | break; |
| 9433 | |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 9434 | case SK_ParenthesizedArrayInit: |
| 9435 | OS << "parenthesized array initialization"; |
| 9436 | break; |
| 9437 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9438 | case SK_PassByIndirectCopyRestore: |
| 9439 | OS << "pass by indirect copy and restore"; |
| 9440 | break; |
| 9441 | |
| 9442 | case SK_PassByIndirectRestore: |
| 9443 | OS << "pass by indirect restore"; |
| 9444 | break; |
| 9445 | |
| 9446 | case SK_ProduceObjCObject: |
| 9447 | OS << "Objective-C object retension"; |
| 9448 | break; |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 9449 | |
| 9450 | case SK_StdInitializerList: |
| 9451 | OS << "std::initializer_list from initializer list"; |
| 9452 | break; |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 9453 | |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 9454 | case SK_StdInitializerListConstructorCall: |
| 9455 | OS << "list initialization from std::initializer_list"; |
| 9456 | break; |
| 9457 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 9458 | case SK_OCLSamplerInit: |
| 9459 | OS << "OpenCL sampler_t from integer constant"; |
| 9460 | break; |
| 9461 | |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 9462 | case SK_OCLZeroOpaqueType: |
| 9463 | OS << "OpenCL opaque type from zero"; |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 9464 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9465 | } |
Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 9466 | |
| 9467 | OS << " [" << S->Type.getAsString() << ']'; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9468 | } |
Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 9469 | |
| 9470 | OS << '\n'; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9471 | } |
| 9472 | |
| 9473 | void InitializationSequence::dump() const { |
| 9474 | dump(llvm::errs()); |
| 9475 | } |
| 9476 | |
Nico Weber | 3d7f00d | 2018-06-19 23:19:34 +0000 | [diff] [blame] | 9477 | static bool NarrowingErrs(const LangOptions &L) { |
| 9478 | return L.CPlusPlus11 && |
| 9479 | (!L.MicrosoftExt || L.isCompatibleWithMSVC(LangOptions::MSVC2015)); |
| 9480 | } |
| 9481 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 9482 | static void DiagnoseNarrowingInInitList(Sema &S, |
| 9483 | const ImplicitConversionSequence &ICS, |
| 9484 | QualType PreNarrowingType, |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9485 | QualType EntityType, |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9486 | const Expr *PostInit) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9487 | const StandardConversionSequence *SCS = nullptr; |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9488 | switch (ICS.getKind()) { |
| 9489 | case ImplicitConversionSequence::StandardConversion: |
| 9490 | SCS = &ICS.Standard; |
| 9491 | break; |
| 9492 | case ImplicitConversionSequence::UserDefinedConversion: |
| 9493 | SCS = &ICS.UserDefined.After; |
| 9494 | break; |
| 9495 | case ImplicitConversionSequence::AmbiguousConversion: |
| 9496 | case ImplicitConversionSequence::EllipsisConversion: |
| 9497 | case ImplicitConversionSequence::BadConversion: |
| 9498 | return; |
| 9499 | } |
| 9500 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9501 | // C++11 [dcl.init.list]p7: Check whether this is a narrowing conversion. |
| 9502 | APValue ConstantValue; |
Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 9503 | QualType ConstantType; |
| 9504 | switch (SCS->getNarrowingKind(S.Context, PostInit, ConstantValue, |
| 9505 | ConstantType)) { |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9506 | case NK_Not_Narrowing: |
Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 9507 | case NK_Dependent_Narrowing: |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9508 | // No narrowing occurred. |
| 9509 | return; |
| 9510 | |
| 9511 | case NK_Type_Narrowing: |
| 9512 | // This was a floating-to-integer conversion, which is always considered a |
| 9513 | // narrowing conversion even if the value is a constant and can be |
| 9514 | // represented exactly as an integer. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9515 | S.Diag(PostInit->getBeginLoc(), NarrowingErrs(S.getLangOpts()) |
Nico Weber | 3d7f00d | 2018-06-19 23:19:34 +0000 | [diff] [blame] | 9516 | ? diag::ext_init_list_type_narrowing |
| 9517 | : diag::warn_init_list_type_narrowing) |
| 9518 | << PostInit->getSourceRange() |
| 9519 | << PreNarrowingType.getLocalUnqualifiedType() |
| 9520 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9521 | break; |
| 9522 | |
| 9523 | case NK_Constant_Narrowing: |
| 9524 | // A constant value was narrowed. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9525 | S.Diag(PostInit->getBeginLoc(), |
Nico Weber | 3d7f00d | 2018-06-19 23:19:34 +0000 | [diff] [blame] | 9526 | NarrowingErrs(S.getLangOpts()) |
| 9527 | ? diag::ext_init_list_constant_narrowing |
| 9528 | : diag::warn_init_list_constant_narrowing) |
| 9529 | << PostInit->getSourceRange() |
| 9530 | << ConstantValue.getAsString(S.getASTContext(), ConstantType) |
| 9531 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9532 | break; |
| 9533 | |
| 9534 | case NK_Variable_Narrowing: |
| 9535 | // A variable's value may have been narrowed. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9536 | S.Diag(PostInit->getBeginLoc(), |
Nico Weber | 3d7f00d | 2018-06-19 23:19:34 +0000 | [diff] [blame] | 9537 | NarrowingErrs(S.getLangOpts()) |
| 9538 | ? diag::ext_init_list_variable_narrowing |
| 9539 | : diag::warn_init_list_variable_narrowing) |
| 9540 | << PostInit->getSourceRange() |
| 9541 | << PreNarrowingType.getLocalUnqualifiedType() |
| 9542 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9543 | break; |
| 9544 | } |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 9545 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 9546 | SmallString<128> StaticCast; |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 9547 | llvm::raw_svector_ostream OS(StaticCast); |
| 9548 | OS << "static_cast<"; |
| 9549 | if (const TypedefType *TT = EntityType->getAs<TypedefType>()) { |
| 9550 | // It's important to use the typedef's name if there is one so that the |
| 9551 | // fixit doesn't break code using types like int64_t. |
| 9552 | // |
| 9553 | // FIXME: This will break if the typedef requires qualification. But |
| 9554 | // getQualifiedNameAsString() includes non-machine-parsable components. |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 9555 | OS << *TT->getDecl(); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 9556 | } else if (const BuiltinType *BT = EntityType->getAs<BuiltinType>()) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9557 | OS << BT->getName(S.getLangOpts()); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 9558 | else { |
| 9559 | // Oops, we didn't find the actual type of the variable. Don't emit a fixit |
| 9560 | // with a broken cast. |
| 9561 | return; |
| 9562 | } |
| 9563 | OS << ">("; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9564 | S.Diag(PostInit->getBeginLoc(), diag::note_init_list_narrowing_silence) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 9565 | << PostInit->getSourceRange() |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9566 | << FixItHint::CreateInsertion(PostInit->getBeginLoc(), OS.str()) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 9567 | << FixItHint::CreateInsertion( |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 9568 | S.getLocForEndOfToken(PostInit->getEndLoc()), ")"); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 9569 | } |
| 9570 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 9571 | //===----------------------------------------------------------------------===// |
| 9572 | // Initialization helper functions |
| 9573 | //===----------------------------------------------------------------------===// |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 9574 | bool |
| 9575 | Sema::CanPerformCopyInitialization(const InitializedEntity &Entity, |
| 9576 | ExprResult Init) { |
| 9577 | if (Init.isInvalid()) |
| 9578 | return false; |
| 9579 | |
| 9580 | Expr *InitE = Init.get(); |
| 9581 | assert(InitE && "No initialization expression"); |
| 9582 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9583 | InitializationKind Kind = |
| 9584 | InitializationKind::CreateCopy(InitE->getBeginLoc(), SourceLocation()); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 9585 | InitializationSequence Seq(*this, Entity, Kind, InitE); |
Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 9586 | return !Seq.Failed(); |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 9587 | } |
| 9588 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9589 | ExprResult |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 9590 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 9591 | SourceLocation EqualLoc, |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 9592 | ExprResult Init, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 9593 | bool TopLevelOfInitList, |
| 9594 | bool AllowExplicit) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 9595 | if (Init.isInvalid()) |
| 9596 | return ExprError(); |
| 9597 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 9598 | Expr *InitE = Init.get(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 9599 | assert(InitE && "No initialization expression?"); |
| 9600 | |
| 9601 | if (EqualLoc.isInvalid()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9602 | EqualLoc = InitE->getBeginLoc(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 9603 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9604 | InitializationKind Kind = InitializationKind::CreateCopy( |
| 9605 | InitE->getBeginLoc(), EqualLoc, AllowExplicit); |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 9606 | InitializationSequence Seq(*this, Entity, Kind, InitE, TopLevelOfInitList); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 9607 | |
Alex Lorenz | de69ff9 | 2017-05-16 10:23:58 +0000 | [diff] [blame] | 9608 | // Prevent infinite recursion when performing parameter copy-initialization. |
| 9609 | const bool ShouldTrackCopy = |
| 9610 | Entity.isParameterKind() && Seq.isConstructorInitialization(); |
| 9611 | if (ShouldTrackCopy) { |
| 9612 | if (llvm::find(CurrentParameterCopyTypes, Entity.getType()) != |
| 9613 | CurrentParameterCopyTypes.end()) { |
| 9614 | Seq.SetOverloadFailure( |
| 9615 | InitializationSequence::FK_ConstructorOverloadFailed, |
| 9616 | OR_No_Viable_Function); |
| 9617 | |
| 9618 | // Try to give a meaningful diagnostic note for the problematic |
| 9619 | // constructor. |
| 9620 | const auto LastStep = Seq.step_end() - 1; |
| 9621 | assert(LastStep->Kind == |
| 9622 | InitializationSequence::SK_ConstructorInitialization); |
| 9623 | const FunctionDecl *Function = LastStep->Function.Function; |
| 9624 | auto Candidate = |
| 9625 | llvm::find_if(Seq.getFailedCandidateSet(), |
| 9626 | [Function](const OverloadCandidate &Candidate) -> bool { |
| 9627 | return Candidate.Viable && |
| 9628 | Candidate.Function == Function && |
| 9629 | Candidate.Conversions.size() > 0; |
| 9630 | }); |
| 9631 | if (Candidate != Seq.getFailedCandidateSet().end() && |
| 9632 | Function->getNumParams() > 0) { |
| 9633 | Candidate->Viable = false; |
| 9634 | Candidate->FailureKind = ovl_fail_bad_conversion; |
| 9635 | Candidate->Conversions[0].setBad(BadConversionSequence::no_conversion, |
| 9636 | InitE, |
| 9637 | Function->getParamDecl(0)->getType()); |
| 9638 | } |
| 9639 | } |
| 9640 | CurrentParameterCopyTypes.push_back(Entity.getType()); |
| 9641 | } |
| 9642 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 9643 | ExprResult Result = Seq.Perform(*this, Entity, Kind, InitE); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9644 | |
Alex Lorenz | de69ff9 | 2017-05-16 10:23:58 +0000 | [diff] [blame] | 9645 | if (ShouldTrackCopy) |
| 9646 | CurrentParameterCopyTypes.pop_back(); |
| 9647 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9648 | return Result; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 9649 | } |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9650 | |
Richard Smith | 1363e8f | 2017-09-07 07:22:36 +0000 | [diff] [blame] | 9651 | /// Determine whether RD is, or is derived from, a specialization of CTD. |
| 9652 | static bool isOrIsDerivedFromSpecializationOf(CXXRecordDecl *RD, |
| 9653 | ClassTemplateDecl *CTD) { |
| 9654 | auto NotSpecialization = [&] (const CXXRecordDecl *Candidate) { |
| 9655 | auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Candidate); |
| 9656 | return !CTSD || !declaresSameEntity(CTSD->getSpecializedTemplate(), CTD); |
| 9657 | }; |
| 9658 | return !(NotSpecialization(RD) && RD->forallBases(NotSpecialization)); |
| 9659 | } |
| 9660 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9661 | QualType Sema::DeduceTemplateSpecializationFromInitializer( |
| 9662 | TypeSourceInfo *TSInfo, const InitializedEntity &Entity, |
| 9663 | const InitializationKind &Kind, MultiExprArg Inits) { |
| 9664 | auto *DeducedTST = dyn_cast<DeducedTemplateSpecializationType>( |
| 9665 | TSInfo->getType()->getContainedDeducedType()); |
| 9666 | assert(DeducedTST && "not a deduced template specialization type"); |
| 9667 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9668 | auto TemplateName = DeducedTST->getTemplateName(); |
Richard Smith | cff4201 | 2018-09-28 03:18:53 +0000 | [diff] [blame] | 9669 | if (TemplateName.isDependent()) |
| 9670 | return Context.DependentTy; |
| 9671 | |
| 9672 | // We can only perform deduction for class templates. |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9673 | auto *Template = |
| 9674 | dyn_cast_or_null<ClassTemplateDecl>(TemplateName.getAsTemplateDecl()); |
| 9675 | if (!Template) { |
| 9676 | Diag(Kind.getLocation(), |
| 9677 | diag::err_deduced_non_class_template_specialization_type) |
| 9678 | << (int)getTemplateNameKindForDiagnostics(TemplateName) << TemplateName; |
| 9679 | if (auto *TD = TemplateName.getAsTemplateDecl()) |
| 9680 | Diag(TD->getLocation(), diag::note_template_decl_here); |
| 9681 | return QualType(); |
| 9682 | } |
| 9683 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9684 | // Can't deduce from dependent arguments. |
Richard Smith | 8eeb16f | 2018-09-10 20:31:03 +0000 | [diff] [blame] | 9685 | if (Expr::hasAnyTypeDependentArguments(Inits)) { |
| 9686 | Diag(TSInfo->getTypeLoc().getBeginLoc(), |
| 9687 | diag::warn_cxx14_compat_class_template_argument_deduction) |
| 9688 | << TSInfo->getTypeLoc().getSourceRange() << 0; |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9689 | return Context.DependentTy; |
Richard Smith | 8eeb16f | 2018-09-10 20:31:03 +0000 | [diff] [blame] | 9690 | } |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9691 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9692 | // FIXME: Perform "exact type" matching first, per CWG discussion? |
| 9693 | // Or implement this via an implied 'T(T) -> T' deduction guide? |
| 9694 | |
| 9695 | // FIXME: Do we need/want a std::initializer_list<T> special case? |
| 9696 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9697 | // Look up deduction guides, including those synthesized from constructors. |
| 9698 | // |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9699 | // C++1z [over.match.class.deduct]p1: |
| 9700 | // A set of functions and function templates is formed comprising: |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9701 | // - For each constructor of the class template designated by the |
| 9702 | // template-name, a function template [...] |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9703 | // - For each deduction-guide, a function or function template [...] |
| 9704 | DeclarationNameInfo NameInfo( |
| 9705 | Context.DeclarationNames.getCXXDeductionGuideName(Template), |
| 9706 | TSInfo->getTypeLoc().getEndLoc()); |
| 9707 | LookupResult Guides(*this, NameInfo, LookupOrdinaryName); |
| 9708 | LookupQualifiedName(Guides, Template->getDeclContext()); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9709 | |
| 9710 | // FIXME: Do not diagnose inaccessible deduction guides. The standard isn't |
| 9711 | // clear on this, but they're not found by name so access does not apply. |
| 9712 | Guides.suppressDiagnostics(); |
| 9713 | |
| 9714 | // Figure out if this is list-initialization. |
| 9715 | InitListExpr *ListInit = |
| 9716 | (Inits.size() == 1 && Kind.getKind() != InitializationKind::IK_Direct) |
| 9717 | ? dyn_cast<InitListExpr>(Inits[0]) |
| 9718 | : nullptr; |
| 9719 | |
| 9720 | // C++1z [over.match.class.deduct]p1: |
| 9721 | // Initialization and overload resolution are performed as described in |
| 9722 | // [dcl.init] and [over.match.ctor], [over.match.copy], or [over.match.list] |
| 9723 | // (as appropriate for the type of initialization performed) for an object |
| 9724 | // of a hypothetical class type, where the selected functions and function |
| 9725 | // templates are considered to be the constructors of that class type |
| 9726 | // |
| 9727 | // Since we know we're initializing a class type of a type unrelated to that |
| 9728 | // of the initializer, this reduces to something fairly reasonable. |
| 9729 | OverloadCandidateSet Candidates(Kind.getLocation(), |
| 9730 | OverloadCandidateSet::CSK_Normal); |
| 9731 | OverloadCandidateSet::iterator Best; |
Eric Fiselier | 73b51ae | 2019-01-17 21:44:24 +0000 | [diff] [blame] | 9732 | |
| 9733 | bool HasAnyDeductionGuide = false; |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 9734 | bool AllowExplicit = !Kind.isCopyInit() || ListInit; |
Eric Fiselier | 73b51ae | 2019-01-17 21:44:24 +0000 | [diff] [blame] | 9735 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9736 | auto tryToResolveOverload = |
| 9737 | [&](bool OnlyListConstructors) -> OverloadingResult { |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 9738 | Candidates.clear(OverloadCandidateSet::CSK_Normal); |
Eric Fiselier | 73b51ae | 2019-01-17 21:44:24 +0000 | [diff] [blame] | 9739 | HasAnyDeductionGuide = false; |
| 9740 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9741 | for (auto I = Guides.begin(), E = Guides.end(); I != E; ++I) { |
| 9742 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9743 | if (D->isInvalidDecl()) |
| 9744 | continue; |
| 9745 | |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9746 | auto *TD = dyn_cast<FunctionTemplateDecl>(D); |
| 9747 | auto *GD = dyn_cast_or_null<CXXDeductionGuideDecl>( |
| 9748 | TD ? TD->getTemplatedDecl() : dyn_cast<FunctionDecl>(D)); |
| 9749 | if (!GD) |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9750 | continue; |
| 9751 | |
Eric Fiselier | 73b51ae | 2019-01-17 21:44:24 +0000 | [diff] [blame] | 9752 | if (!GD->isImplicit()) |
| 9753 | HasAnyDeductionGuide = true; |
| 9754 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9755 | // C++ [over.match.ctor]p1: (non-list copy-initialization from non-class) |
| 9756 | // For copy-initialization, the candidate functions are all the |
| 9757 | // converting constructors (12.3.1) of that class. |
| 9758 | // C++ [over.match.copy]p1: (non-list copy-initialization from class) |
| 9759 | // The converting constructors of T are candidate functions. |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 9760 | if (!AllowExplicit) { |
Richard Smith | afe4aa8 | 2017-02-10 02:19:05 +0000 | [diff] [blame] | 9761 | // Only consider converting constructors. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9762 | if (GD->isExplicit()) |
Richard Smith | afe4aa8 | 2017-02-10 02:19:05 +0000 | [diff] [blame] | 9763 | continue; |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9764 | |
| 9765 | // When looking for a converting constructor, deduction guides that |
Richard Smith | afe4aa8 | 2017-02-10 02:19:05 +0000 | [diff] [blame] | 9766 | // could never be called with one argument are not interesting to |
| 9767 | // check or note. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9768 | if (GD->getMinRequiredArguments() > 1 || |
| 9769 | (GD->getNumParams() == 0 && !GD->isVariadic())) |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9770 | continue; |
| 9771 | } |
| 9772 | |
| 9773 | // C++ [over.match.list]p1.1: (first phase list initialization) |
| 9774 | // Initially, the candidate functions are the initializer-list |
| 9775 | // constructors of the class T |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9776 | if (OnlyListConstructors && !isInitListConstructor(GD)) |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9777 | continue; |
| 9778 | |
| 9779 | // C++ [over.match.list]p1.2: (second phase list initialization) |
| 9780 | // the candidate functions are all the constructors of the class T |
| 9781 | // C++ [over.match.ctor]p1: (all other cases) |
| 9782 | // the candidate functions are all the constructors of the class of |
| 9783 | // the object being initialized |
| 9784 | |
| 9785 | // C++ [over.best.ics]p4: |
| 9786 | // When [...] the constructor [...] is a candidate by |
| 9787 | // - [over.match.copy] (in all cases) |
| 9788 | // FIXME: The "second phase of [over.match.list] case can also |
| 9789 | // theoretically happen here, but it's not clear whether we can |
| 9790 | // ever have a parameter of the right type. |
| 9791 | bool SuppressUserConversions = Kind.isCopyInit(); |
| 9792 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9793 | if (TD) |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9794 | AddTemplateOverloadCandidate(TD, I.getPair(), /*ExplicitArgs*/ nullptr, |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 9795 | Inits, Candidates, SuppressUserConversions, |
| 9796 | /*PartialOverloading*/ false, |
| 9797 | AllowExplicit); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9798 | else |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9799 | AddOverloadCandidate(GD, I.getPair(), Inits, Candidates, |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 9800 | SuppressUserConversions, |
| 9801 | /*PartialOverloading*/ false, AllowExplicit); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9802 | } |
| 9803 | return Candidates.BestViableFunction(*this, Kind.getLocation(), Best); |
| 9804 | }; |
| 9805 | |
| 9806 | OverloadingResult Result = OR_No_Viable_Function; |
| 9807 | |
| 9808 | // C++11 [over.match.list]p1, per DR1467: for list-initialization, first |
| 9809 | // try initializer-list constructors. |
| 9810 | if (ListInit) { |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9811 | bool TryListConstructors = true; |
| 9812 | |
| 9813 | // Try list constructors unless the list is empty and the class has one or |
| 9814 | // more default constructors, in which case those constructors win. |
| 9815 | if (!ListInit->getNumInits()) { |
| 9816 | for (NamedDecl *D : Guides) { |
| 9817 | auto *FD = dyn_cast<FunctionDecl>(D->getUnderlyingDecl()); |
| 9818 | if (FD && FD->getMinRequiredArguments() == 0) { |
| 9819 | TryListConstructors = false; |
| 9820 | break; |
| 9821 | } |
| 9822 | } |
Richard Smith | 1363e8f | 2017-09-07 07:22:36 +0000 | [diff] [blame] | 9823 | } else if (ListInit->getNumInits() == 1) { |
| 9824 | // C++ [over.match.class.deduct]: |
| 9825 | // As an exception, the first phase in [over.match.list] (considering |
| 9826 | // initializer-list constructors) is omitted if the initializer list |
| 9827 | // consists of a single expression of type cv U, where U is a |
| 9828 | // specialization of C or a class derived from a specialization of C. |
| 9829 | Expr *E = ListInit->getInit(0); |
| 9830 | auto *RD = E->getType()->getAsCXXRecordDecl(); |
| 9831 | if (!isa<InitListExpr>(E) && RD && |
Erik Pilkington | dd0b344 | 2018-07-26 23:40:42 +0000 | [diff] [blame] | 9832 | isCompleteType(Kind.getLocation(), E->getType()) && |
Richard Smith | 1363e8f | 2017-09-07 07:22:36 +0000 | [diff] [blame] | 9833 | isOrIsDerivedFromSpecializationOf(RD, Template)) |
| 9834 | TryListConstructors = false; |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9835 | } |
| 9836 | |
| 9837 | if (TryListConstructors) |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9838 | Result = tryToResolveOverload(/*OnlyListConstructor*/true); |
| 9839 | // Then unwrap the initializer list and try again considering all |
| 9840 | // constructors. |
| 9841 | Inits = MultiExprArg(ListInit->getInits(), ListInit->getNumInits()); |
| 9842 | } |
| 9843 | |
| 9844 | // If list-initialization fails, or if we're doing any other kind of |
| 9845 | // initialization, we (eventually) consider constructors. |
| 9846 | if (Result == OR_No_Viable_Function) |
| 9847 | Result = tryToResolveOverload(/*OnlyListConstructor*/false); |
| 9848 | |
| 9849 | switch (Result) { |
| 9850 | case OR_Ambiguous: |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9851 | // FIXME: For list-initialization candidates, it'd usually be better to |
| 9852 | // list why they were not viable when given the initializer list itself as |
| 9853 | // an argument. |
David Blaikie | 5e32805 | 2019-05-03 00:44:50 +0000 | [diff] [blame] | 9854 | Candidates.NoteCandidates( |
| 9855 | PartialDiagnosticAt( |
| 9856 | Kind.getLocation(), |
| 9857 | PDiag(diag::err_deduced_class_template_ctor_ambiguous) |
| 9858 | << TemplateName), |
| 9859 | *this, OCD_ViableCandidates, Inits); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9860 | return QualType(); |
| 9861 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9862 | case OR_No_Viable_Function: { |
| 9863 | CXXRecordDecl *Primary = |
| 9864 | cast<ClassTemplateDecl>(Template)->getTemplatedDecl(); |
| 9865 | bool Complete = |
| 9866 | isCompleteType(Kind.getLocation(), Context.getTypeDeclType(Primary)); |
David Blaikie | 5e32805 | 2019-05-03 00:44:50 +0000 | [diff] [blame] | 9867 | Candidates.NoteCandidates( |
| 9868 | PartialDiagnosticAt( |
| 9869 | Kind.getLocation(), |
| 9870 | PDiag(Complete ? diag::err_deduced_class_template_ctor_no_viable |
| 9871 | : diag::err_deduced_class_template_incomplete) |
| 9872 | << TemplateName << !Guides.empty()), |
| 9873 | *this, OCD_AllCandidates, Inits); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9874 | return QualType(); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9875 | } |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9876 | |
| 9877 | case OR_Deleted: { |
| 9878 | Diag(Kind.getLocation(), diag::err_deduced_class_template_deleted) |
| 9879 | << TemplateName; |
| 9880 | NoteDeletedFunction(Best->Function); |
| 9881 | return QualType(); |
| 9882 | } |
| 9883 | |
| 9884 | case OR_Success: |
| 9885 | // C++ [over.match.list]p1: |
| 9886 | // In copy-list-initialization, if an explicit constructor is chosen, the |
| 9887 | // initialization is ill-formed. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9888 | if (Kind.isCopyInit() && ListInit && |
| 9889 | cast<CXXDeductionGuideDecl>(Best->Function)->isExplicit()) { |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9890 | bool IsDeductionGuide = !Best->Function->isImplicit(); |
| 9891 | Diag(Kind.getLocation(), diag::err_deduced_class_template_explicit) |
| 9892 | << TemplateName << IsDeductionGuide; |
| 9893 | Diag(Best->Function->getLocation(), |
| 9894 | diag::note_explicit_ctor_deduction_guide_here) |
| 9895 | << IsDeductionGuide; |
| 9896 | return QualType(); |
| 9897 | } |
| 9898 | |
| 9899 | // Make sure we didn't select an unusable deduction guide, and mark it |
| 9900 | // as referenced. |
| 9901 | DiagnoseUseOfDecl(Best->Function, Kind.getLocation()); |
| 9902 | MarkFunctionReferenced(Kind.getLocation(), Best->Function); |
| 9903 | break; |
| 9904 | } |
| 9905 | |
| 9906 | // C++ [dcl.type.class.deduct]p1: |
| 9907 | // The placeholder is replaced by the return type of the function selected |
| 9908 | // by overload resolution for class template deduction. |
Richard Smith | 8eeb16f | 2018-09-10 20:31:03 +0000 | [diff] [blame] | 9909 | QualType DeducedType = |
| 9910 | SubstAutoType(TSInfo->getType(), Best->Function->getReturnType()); |
| 9911 | Diag(TSInfo->getTypeLoc().getBeginLoc(), |
| 9912 | diag::warn_cxx14_compat_class_template_argument_deduction) |
| 9913 | << TSInfo->getTypeLoc().getSourceRange() << 1 << DeducedType; |
Eric Fiselier | 73b51ae | 2019-01-17 21:44:24 +0000 | [diff] [blame] | 9914 | |
| 9915 | // Warn if CTAD was used on a type that does not have any user-defined |
| 9916 | // deduction guides. |
| 9917 | if (!HasAnyDeductionGuide) { |
| 9918 | Diag(TSInfo->getTypeLoc().getBeginLoc(), |
| 9919 | diag::warn_ctad_maybe_unsupported) |
| 9920 | << TemplateName; |
| 9921 | Diag(Template->getLocation(), diag::note_suppress_ctad_maybe_unsupported); |
| 9922 | } |
| 9923 | |
Richard Smith | 8eeb16f | 2018-09-10 20:31:03 +0000 | [diff] [blame] | 9924 | return DeducedType; |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9925 | } |