Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1 | //===--- SemaInit.cpp - Semantic Analysis for Initializers ----------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Sebastian Redl | 26bcc94 | 2011-09-24 17:47:39 +0000 | [diff] [blame] | 9 | // This file implements semantic analysis for initializers. |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 10 | // |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 13 | #include "clang/AST/ASTContext.h" |
John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 14 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 98cee2f | 2009-05-27 16:10:08 +0000 | [diff] [blame] | 15 | #include "clang/AST/ExprCXX.h" |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 16 | #include "clang/AST/ExprObjC.h" |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprOpenMP.h" |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 18 | #include "clang/AST/TypeLoc.h" |
James Molloy | 9eef265 | 2014-06-20 14:35:13 +0000 | [diff] [blame] | 19 | #include "clang/Basic/TargetInfo.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/Sema/Designator.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 21 | #include "clang/Sema/Initialization.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "clang/Sema/Lookup.h" |
| 23 | #include "clang/Sema/SemaInternal.h" |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/APInt.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 28 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 29 | using namespace clang; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 30 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | // Sema Initialization Checking |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 35 | /// Check whether T is compatible with a wide character type (wchar_t, |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 36 | /// char16_t or char32_t). |
| 37 | static bool IsWideCharCompatible(QualType T, ASTContext &Context) { |
| 38 | if (Context.typesAreCompatible(Context.getWideCharType(), T)) |
| 39 | return true; |
| 40 | if (Context.getLangOpts().CPlusPlus || Context.getLangOpts().C11) { |
| 41 | return Context.typesAreCompatible(Context.Char16Ty, T) || |
| 42 | Context.typesAreCompatible(Context.Char32Ty, T); |
| 43 | } |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | enum StringInitFailureKind { |
| 48 | SIF_None, |
| 49 | SIF_NarrowStringIntoWideChar, |
| 50 | SIF_WideStringIntoChar, |
| 51 | SIF_IncompatWideStringIntoWideChar, |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 52 | SIF_UTF8StringIntoPlainChar, |
| 53 | SIF_PlainStringIntoUTF8Char, |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 54 | SIF_Other |
| 55 | }; |
| 56 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 57 | /// Check whether the array of type AT can be initialized by the Init |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 58 | /// expression by means of string initialization. Returns SIF_None if so, |
| 59 | /// otherwise returns a StringInitFailureKind that describes why the |
| 60 | /// initialization would not work. |
| 61 | static StringInitFailureKind IsStringInit(Expr *Init, const ArrayType *AT, |
| 62 | ASTContext &Context) { |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 63 | if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT)) |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 64 | return SIF_Other; |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 65 | |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 66 | // See if this is a string literal or @encode. |
| 67 | Init = Init->IgnoreParens(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 68 | |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 69 | // Handle @encode, which is a narrow string. |
| 70 | if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType()) |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 71 | return SIF_None; |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 72 | |
| 73 | // Otherwise we can only handle string literals. |
| 74 | StringLiteral *SL = dyn_cast<StringLiteral>(Init); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 75 | if (!SL) |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 76 | return SIF_Other; |
Eli Friedman | 42a8465 | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 77 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 78 | const QualType ElemTy = |
| 79 | Context.getCanonicalType(AT->getElementType()).getUnqualifiedType(); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 80 | |
| 81 | switch (SL->getKind()) { |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 82 | case StringLiteral::UTF8: |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 83 | // char8_t array can be initialized with a UTF-8 string. |
| 84 | if (ElemTy->isChar8Type()) |
| 85 | return SIF_None; |
| 86 | LLVM_FALLTHROUGH; |
| 87 | case StringLiteral::Ascii: |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 88 | // char array can be initialized with a narrow string. |
| 89 | // Only allow char x[] = "foo"; not char x[] = L"foo"; |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 90 | if (ElemTy->isCharType()) |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 91 | return (SL->getKind() == StringLiteral::UTF8 && |
| 92 | Context.getLangOpts().Char8) |
| 93 | ? SIF_UTF8StringIntoPlainChar |
| 94 | : SIF_None; |
| 95 | if (ElemTy->isChar8Type()) |
| 96 | return SIF_PlainStringIntoUTF8Char; |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 97 | if (IsWideCharCompatible(ElemTy, Context)) |
| 98 | return SIF_NarrowStringIntoWideChar; |
| 99 | return SIF_Other; |
| 100 | // C99 6.7.8p15 (with correction from DR343), or C11 6.7.9p15: |
| 101 | // "An array with element type compatible with a qualified or unqualified |
| 102 | // version of wchar_t, char16_t, or char32_t may be initialized by a wide |
| 103 | // string literal with the corresponding encoding prefix (L, u, or U, |
| 104 | // respectively), optionally enclosed in braces. |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 105 | case StringLiteral::UTF16: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 106 | if (Context.typesAreCompatible(Context.Char16Ty, ElemTy)) |
| 107 | return SIF_None; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 108 | if (ElemTy->isCharType() || ElemTy->isChar8Type()) |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 109 | return SIF_WideStringIntoChar; |
| 110 | if (IsWideCharCompatible(ElemTy, Context)) |
| 111 | return SIF_IncompatWideStringIntoWideChar; |
| 112 | return SIF_Other; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 113 | case StringLiteral::UTF32: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 114 | if (Context.typesAreCompatible(Context.Char32Ty, ElemTy)) |
| 115 | return SIF_None; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 116 | if (ElemTy->isCharType() || ElemTy->isChar8Type()) |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 117 | return SIF_WideStringIntoChar; |
| 118 | if (IsWideCharCompatible(ElemTy, Context)) |
| 119 | return SIF_IncompatWideStringIntoWideChar; |
| 120 | return SIF_Other; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 121 | case StringLiteral::Wide: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 122 | if (Context.typesAreCompatible(Context.getWideCharType(), ElemTy)) |
| 123 | return SIF_None; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 124 | if (ElemTy->isCharType() || ElemTy->isChar8Type()) |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 125 | return SIF_WideStringIntoChar; |
| 126 | if (IsWideCharCompatible(ElemTy, Context)) |
| 127 | return SIF_IncompatWideStringIntoWideChar; |
| 128 | return SIF_Other; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 129 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 130 | |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 131 | llvm_unreachable("missed a StringLiteral kind?"); |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Hans Wennborg | 950f318 | 2013-05-16 09:22:40 +0000 | [diff] [blame] | 134 | static StringInitFailureKind IsStringInit(Expr *init, QualType declType, |
| 135 | ASTContext &Context) { |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 136 | const ArrayType *arrayType = Context.getAsArrayType(declType); |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 137 | if (!arrayType) |
Hans Wennborg | 950f318 | 2013-05-16 09:22:40 +0000 | [diff] [blame] | 138 | return SIF_Other; |
| 139 | return IsStringInit(init, arrayType, Context); |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 142 | /// Update the type of a string literal, including any surrounding parentheses, |
| 143 | /// to match the type of the object which it is initializing. |
| 144 | static void updateStringLiteralType(Expr *E, QualType Ty) { |
Richard Smith | d74b1606 | 2013-05-06 00:35:47 +0000 | [diff] [blame] | 145 | while (true) { |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 146 | E->setType(Ty); |
Eli Friedman | 3bf72d7 | 2019-02-08 21:18:46 +0000 | [diff] [blame] | 147 | E->setValueKind(VK_RValue); |
Eli Friedman | 88fccbd | 2019-02-11 22:54:27 +0000 | [diff] [blame] | 148 | if (isa<StringLiteral>(E) || isa<ObjCEncodeExpr>(E)) { |
Richard Smith | d74b1606 | 2013-05-06 00:35:47 +0000 | [diff] [blame] | 149 | break; |
Eli Friedman | 88fccbd | 2019-02-11 22:54:27 +0000 | [diff] [blame] | 150 | } else if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
Richard Smith | d74b1606 | 2013-05-06 00:35:47 +0000 | [diff] [blame] | 151 | E = PE->getSubExpr(); |
Eli Friedman | 88fccbd | 2019-02-11 22:54:27 +0000 | [diff] [blame] | 152 | } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
| 153 | assert(UO->getOpcode() == UO_Extension); |
Richard Smith | d74b1606 | 2013-05-06 00:35:47 +0000 | [diff] [blame] | 154 | E = UO->getSubExpr(); |
Eli Friedman | 88fccbd | 2019-02-11 22:54:27 +0000 | [diff] [blame] | 155 | } else if (GenericSelectionExpr *GSE = dyn_cast<GenericSelectionExpr>(E)) { |
Richard Smith | d74b1606 | 2013-05-06 00:35:47 +0000 | [diff] [blame] | 156 | E = GSE->getResultExpr(); |
Eli Friedman | 88fccbd | 2019-02-11 22:54:27 +0000 | [diff] [blame] | 157 | } else if (ChooseExpr *CE = dyn_cast<ChooseExpr>(E)) { |
| 158 | E = CE->getChosenSubExpr(); |
| 159 | } else { |
Richard Smith | d74b1606 | 2013-05-06 00:35:47 +0000 | [diff] [blame] | 160 | llvm_unreachable("unexpected expr in string literal init"); |
Eli Friedman | 88fccbd | 2019-02-11 22:54:27 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /// Fix a compound literal initializing an array so it's correctly marked |
| 166 | /// as an rvalue. |
| 167 | static void updateGNUCompoundLiteralRValue(Expr *E) { |
| 168 | while (true) { |
| 169 | E->setValueKind(VK_RValue); |
| 170 | if (isa<CompoundLiteralExpr>(E)) { |
| 171 | break; |
| 172 | } else if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
| 173 | E = PE->getSubExpr(); |
| 174 | } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
| 175 | assert(UO->getOpcode() == UO_Extension); |
| 176 | E = UO->getSubExpr(); |
| 177 | } else if (GenericSelectionExpr *GSE = dyn_cast<GenericSelectionExpr>(E)) { |
| 178 | E = GSE->getResultExpr(); |
| 179 | } else if (ChooseExpr *CE = dyn_cast<ChooseExpr>(E)) { |
| 180 | E = CE->getChosenSubExpr(); |
| 181 | } else { |
| 182 | llvm_unreachable("unexpected expr in array compound literal init"); |
| 183 | } |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 184 | } |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 185 | } |
| 186 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 187 | static void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT, |
| 188 | Sema &S) { |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 189 | // Get the length of the string as parsed. |
Ben Langmuir | 577b393 | 2015-01-26 19:04:10 +0000 | [diff] [blame] | 190 | auto *ConstantArrayTy = |
Ben Langmuir | 7b30f53 | 2015-01-26 20:01:17 +0000 | [diff] [blame] | 191 | cast<ConstantArrayType>(Str->getType()->getAsArrayTypeUnsafe()); |
Ben Langmuir | 577b393 | 2015-01-26 19:04:10 +0000 | [diff] [blame] | 192 | uint64_t StrLength = ConstantArrayTy->getSize().getZExtValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 193 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 194 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | // 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] | 196 | // being initialized to a string literal. |
Benjamin Kramer | e073177 | 2012-08-04 17:00:46 +0000 | [diff] [blame] | 197 | llvm::APInt ConstVal(32, StrLength); |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 198 | // Return a new array type (C99 6.7.8p22). |
John McCall | c5b8225 | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 199 | DeclT = S.Context.getConstantArrayType(IAT->getElementType(), |
| 200 | ConstVal, |
| 201 | ArrayType::Normal, 0); |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 202 | updateStringLiteralType(Str, DeclT); |
Chris Lattner | 94e6c4b | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 203 | return; |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 204 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 205 | |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 206 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 207 | |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 208 | // We have an array of character type with known size. However, |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 209 | // the size may be smaller or larger than the string we are initializing. |
| 210 | // FIXME: Avoid truncation for 64-bit length strings. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 211 | if (S.getLangOpts().CPlusPlus) { |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 212 | if (StringLiteral *SL = dyn_cast<StringLiteral>(Str->IgnoreParens())) { |
Anders Carlsson | d162fb8 | 2011-04-14 00:41:11 +0000 | [diff] [blame] | 213 | // For Pascal strings it's OK to strip off the terminating null character, |
| 214 | // so the example below is valid: |
| 215 | // |
| 216 | // unsigned char a[2] = "\pa"; |
| 217 | if (SL->isPascal()) |
| 218 | StrLength--; |
| 219 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 220 | |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 221 | // [dcl.init.string]p2 |
| 222 | if (StrLength > CAT->getSize().getZExtValue()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 223 | S.Diag(Str->getBeginLoc(), |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 224 | diag::err_initializer_string_for_char_array_too_long) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 225 | << Str->getSourceRange(); |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 226 | } else { |
| 227 | // C99 6.7.8p14. |
| 228 | if (StrLength-1 > CAT->getSize().getZExtValue()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 229 | S.Diag(Str->getBeginLoc(), |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 230 | diag::ext_initializer_string_for_char_array_too_long) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 231 | << Str->getSourceRange(); |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 232 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 233 | |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 234 | // Set the type to the actual size that we are initializing. If we have |
| 235 | // something like: |
| 236 | // char x[1] = "foo"; |
| 237 | // then this will set the string literal's type to char[1]. |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 238 | updateStringLiteralType(Str, DeclT); |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 241 | //===----------------------------------------------------------------------===// |
| 242 | // Semantic checking for initializer lists. |
| 243 | //===----------------------------------------------------------------------===// |
| 244 | |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 245 | namespace { |
| 246 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 247 | /// Semantic checking for initializer lists. |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 248 | /// |
| 249 | /// The InitListChecker class contains a set of routines that each |
| 250 | /// handle the initialization of a certain kind of entity, e.g., |
| 251 | /// arrays, vectors, struct/union types, scalars, etc. The |
| 252 | /// InitListChecker itself performs a recursive walk of the subobject |
| 253 | /// structure of the type to be initialized, while stepping through |
| 254 | /// the initializer list one element at a time. The IList and Index |
| 255 | /// parameters to each of the Check* routines contain the active |
| 256 | /// (syntactic) initializer list and the index into that initializer |
| 257 | /// list that represents the current initializer. Each routine is |
| 258 | /// responsible for moving that Index forward as it consumes elements. |
| 259 | /// |
| 260 | /// Each Check* routine also has a StructuredList/StructuredIndex |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 261 | /// arguments, which contains the current "structured" (semantic) |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 262 | /// initializer list and the index into that initializer list where we |
| 263 | /// are copying initializers as we map them over to the semantic |
| 264 | /// list. Once we have completed our recursive walk of the subobject |
| 265 | /// structure, we will have constructed a full semantic initializer |
| 266 | /// list. |
| 267 | /// |
| 268 | /// C99 designators cause changes in the initializer list traversal, |
| 269 | /// because they make the initialization "jump" into a specific |
| 270 | /// subobject and then continue the initialization from that |
| 271 | /// point. CheckDesignatedInitializer() recursively steps into the |
| 272 | /// designated subobject and manages backing out the recursion to |
| 273 | /// initialize the subobjects after the one designated. |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 274 | class InitListChecker { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 275 | Sema &SemaRef; |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 276 | bool hadError; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 277 | bool VerifyOnly; // no diagnostics, no structure building |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 278 | bool TreatUnavailableAsInvalid; // Used only in VerifyOnly mode. |
Benjamin Kramer | 6b441d6 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 279 | llvm::DenseMap<InitListExpr *, InitListExpr *> SyntacticToSemantic; |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 280 | InitListExpr *FullyStructuredList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 281 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 282 | void CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 283 | InitListExpr *ParentIList, QualType T, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 284 | unsigned &Index, InitListExpr *StructuredList, |
Eli Friedman | c616c5f | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 285 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 286 | void CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 287 | InitListExpr *IList, QualType &T, |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 288 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 289 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 290 | void CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 291 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 292 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 293 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 294 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 295 | unsigned &StructuredIndex, |
| 296 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 297 | void CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 298 | InitListExpr *IList, QualType ElemType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 299 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 300 | InitListExpr *StructuredList, |
| 301 | unsigned &StructuredIndex); |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 302 | void CheckComplexType(const InitializedEntity &Entity, |
| 303 | InitListExpr *IList, QualType DeclType, |
| 304 | unsigned &Index, |
| 305 | InitListExpr *StructuredList, |
| 306 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 307 | void CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 308 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 309 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 310 | InitListExpr *StructuredList, |
| 311 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 312 | void CheckReferenceType(const InitializedEntity &Entity, |
| 313 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 314 | unsigned &Index, |
| 315 | InitListExpr *StructuredList, |
| 316 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 317 | void CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 318 | InitListExpr *IList, QualType DeclType, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 319 | InitListExpr *StructuredList, |
| 320 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 321 | void CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 73eb7cd | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 322 | InitListExpr *IList, QualType DeclType, |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 323 | CXXRecordDecl::base_class_range Bases, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 324 | RecordDecl::field_iterator Field, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 325 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 326 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 327 | unsigned &StructuredIndex, |
| 328 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 329 | void CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 330 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 331 | llvm::APSInt elementIndex, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 332 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 333 | InitListExpr *StructuredList, |
| 334 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 335 | bool CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 336 | InitListExpr *IList, DesignatedInitExpr *DIE, |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 337 | unsigned DesigIdx, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 338 | QualType &CurrentObjectType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 339 | RecordDecl::field_iterator *NextField, |
| 340 | llvm::APSInt *NextElementIndex, |
| 341 | unsigned &Index, |
| 342 | InitListExpr *StructuredList, |
| 343 | unsigned &StructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 344 | bool FinishSubobjectInit, |
| 345 | bool TopLevelObject); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 346 | InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 347 | QualType CurrentObjectType, |
| 348 | InitListExpr *StructuredList, |
| 349 | unsigned StructuredIndex, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 350 | SourceRange InitRange, |
| 351 | bool IsFullyOverwritten = false); |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 352 | void UpdateStructuredListElement(InitListExpr *StructuredList, |
| 353 | unsigned &StructuredIndex, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 354 | Expr *expr); |
| 355 | int numArrayElements(QualType DeclType); |
| 356 | int numStructUnionElements(QualType DeclType); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 357 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 358 | static ExprResult PerformEmptyInit(Sema &SemaRef, |
| 359 | SourceLocation Loc, |
| 360 | const InitializedEntity &Entity, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 361 | bool VerifyOnly, |
| 362 | bool TreatUnavailableAsInvalid); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 363 | |
| 364 | // Explanation on the "FillWithNoInit" mode: |
| 365 | // |
| 366 | // Assume we have the following definitions (Case#1): |
| 367 | // struct P { char x[6][6]; } xp = { .x[1] = "bar" }; |
| 368 | // struct PP { struct P lp; } l = { .lp = xp, .lp.x[1][2] = 'f' }; |
| 369 | // |
| 370 | // l.lp.x[1][0..1] should not be filled with implicit initializers because the |
| 371 | // "base" initializer "xp" will provide values for them; l.lp.x[1] will be "baf". |
| 372 | // |
| 373 | // But if we have (Case#2): |
| 374 | // struct PP l = { .lp = xp, .lp.x[1] = { [2] = 'f' } }; |
| 375 | // |
| 376 | // l.lp.x[1][0..1] are implicitly initialized and do not use values from the |
| 377 | // "base" initializer; l.lp.x[1] will be "\0\0f\0\0\0". |
| 378 | // |
| 379 | // To distinguish Case#1 from Case#2, and also to avoid leaving many "holes" |
| 380 | // in the InitListExpr, the "holes" in Case#1 are filled not with empty |
| 381 | // initializers but with special "NoInitExpr" place holders, which tells the |
| 382 | // CodeGen not to generate any initializers for these parts. |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 383 | void FillInEmptyInitForBase(unsigned Init, const CXXBaseSpecifier &Base, |
| 384 | const InitializedEntity &ParentEntity, |
| 385 | InitListExpr *ILE, bool &RequiresSecondPass, |
| 386 | bool FillWithNoInit); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 387 | void FillInEmptyInitForField(unsigned Init, FieldDecl *Field, |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 388 | const InitializedEntity &ParentEntity, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 389 | InitListExpr *ILE, bool &RequiresSecondPass, |
| 390 | bool FillWithNoInit = false); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 391 | void FillInEmptyInitializations(const InitializedEntity &Entity, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 392 | InitListExpr *ILE, bool &RequiresSecondPass, |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 393 | InitListExpr *OuterILE, unsigned OuterIndex, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 394 | bool FillWithNoInit = false); |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 395 | bool CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 396 | Expr *InitExpr, FieldDecl *Field, |
| 397 | bool TopLevelObject); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 398 | void CheckEmptyInitializable(const InitializedEntity &Entity, |
| 399 | SourceLocation Loc); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 400 | |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 401 | public: |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 402 | InitListChecker(Sema &S, const InitializedEntity &Entity, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 403 | InitListExpr *IL, QualType &T, bool VerifyOnly, |
| 404 | bool TreatUnavailableAsInvalid); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 405 | bool HadError() { return hadError; } |
| 406 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 407 | // Retrieves the fully-structured initializer list used for |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 408 | // semantic analysis and code generation. |
| 409 | InitListExpr *getFullyStructuredList() const { return FullyStructuredList; } |
| 410 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 411 | |
Chris Lattner | 9ececce | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 412 | } // end anonymous namespace |
Chris Lattner | d9ae05b | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 413 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 414 | ExprResult InitListChecker::PerformEmptyInit(Sema &SemaRef, |
| 415 | SourceLocation Loc, |
| 416 | const InitializedEntity &Entity, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 417 | bool VerifyOnly, |
| 418 | bool TreatUnavailableAsInvalid) { |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 419 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 420 | true); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 421 | MultiExprArg SubInit; |
| 422 | Expr *InitExpr; |
| 423 | InitListExpr DummyInitList(SemaRef.Context, Loc, None, Loc); |
| 424 | |
| 425 | // C++ [dcl.init.aggr]p7: |
| 426 | // If there are fewer initializer-clauses in the list than there are |
| 427 | // members in the aggregate, then each member not explicitly initialized |
| 428 | // ... |
Nico Weber | bcb70ee | 2014-07-02 23:51:09 +0000 | [diff] [blame] | 429 | bool EmptyInitList = SemaRef.getLangOpts().CPlusPlus11 && |
| 430 | Entity.getType()->getBaseElementTypeUnsafe()->isRecordType(); |
| 431 | if (EmptyInitList) { |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 432 | // C++1y / DR1070: |
| 433 | // shall be initialized [...] from an empty initializer list. |
| 434 | // |
| 435 | // We apply the resolution of this DR to C++11 but not C++98, since C++98 |
| 436 | // does not have useful semantics for initialization from an init list. |
| 437 | // We treat this as copy-initialization, because aggregate initialization |
| 438 | // always performs copy-initialization on its elements. |
| 439 | // |
| 440 | // Only do this if we're initializing a class type, to avoid filling in |
| 441 | // the initializer list where possible. |
| 442 | InitExpr = VerifyOnly ? &DummyInitList : new (SemaRef.Context) |
| 443 | InitListExpr(SemaRef.Context, Loc, None, Loc); |
| 444 | InitExpr->setType(SemaRef.Context.VoidTy); |
| 445 | SubInit = InitExpr; |
| 446 | Kind = InitializationKind::CreateCopy(Loc, Loc); |
| 447 | } else { |
| 448 | // C++03: |
| 449 | // shall be value-initialized. |
| 450 | } |
| 451 | |
| 452 | InitializationSequence InitSeq(SemaRef, Entity, Kind, SubInit); |
Nico Weber | bcb70ee | 2014-07-02 23:51:09 +0000 | [diff] [blame] | 453 | // libstdc++4.6 marks the vector default constructor as explicit in |
| 454 | // _GLIBCXX_DEBUG mode, so recover using the C++03 logic in that case. |
| 455 | // stlport does so too. Look for std::__debug for libstdc++, and for |
| 456 | // std:: for stlport. This is effectively a compiler-side implementation of |
| 457 | // LWG2193. |
| 458 | if (!InitSeq && EmptyInitList && InitSeq.getFailureKind() == |
| 459 | InitializationSequence::FK_ExplicitConstructor) { |
| 460 | OverloadCandidateSet::iterator Best; |
| 461 | OverloadingResult O = |
| 462 | InitSeq.getFailedCandidateSet() |
| 463 | .BestViableFunction(SemaRef, Kind.getLocation(), Best); |
| 464 | (void)O; |
| 465 | assert(O == OR_Success && "Inconsistent overload resolution"); |
| 466 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 467 | CXXRecordDecl *R = CtorDecl->getParent(); |
| 468 | |
| 469 | if (CtorDecl->getMinRequiredArguments() == 0 && |
| 470 | CtorDecl->isExplicit() && R->getDeclName() && |
| 471 | SemaRef.SourceMgr.isInSystemHeader(CtorDecl->getLocation())) { |
Nico Weber | bcb70ee | 2014-07-02 23:51:09 +0000 | [diff] [blame] | 472 | bool IsInStd = false; |
| 473 | for (NamespaceDecl *ND = dyn_cast<NamespaceDecl>(R->getDeclContext()); |
Nico Weber | 5752ad0 | 2014-07-03 00:38:25 +0000 | [diff] [blame] | 474 | ND && !IsInStd; ND = dyn_cast<NamespaceDecl>(ND->getParent())) { |
Nico Weber | bcb70ee | 2014-07-02 23:51:09 +0000 | [diff] [blame] | 475 | if (SemaRef.getStdNamespace()->InEnclosingNamespaceSetOf(ND)) |
| 476 | IsInStd = true; |
| 477 | } |
| 478 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 479 | if (IsInStd && llvm::StringSwitch<bool>(R->getName()) |
Nico Weber | bcb70ee | 2014-07-02 23:51:09 +0000 | [diff] [blame] | 480 | .Cases("basic_string", "deque", "forward_list", true) |
| 481 | .Cases("list", "map", "multimap", "multiset", true) |
| 482 | .Cases("priority_queue", "queue", "set", "stack", true) |
| 483 | .Cases("unordered_map", "unordered_set", "vector", true) |
| 484 | .Default(false)) { |
| 485 | InitSeq.InitializeFrom( |
| 486 | SemaRef, Entity, |
| 487 | InitializationKind::CreateValue(Loc, Loc, Loc, true), |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 488 | MultiExprArg(), /*TopLevelOfInitList=*/false, |
| 489 | TreatUnavailableAsInvalid); |
Nico Weber | bcb70ee | 2014-07-02 23:51:09 +0000 | [diff] [blame] | 490 | // Emit a warning for this. System header warnings aren't shown |
| 491 | // by default, but people working on system headers should see it. |
| 492 | if (!VerifyOnly) { |
| 493 | SemaRef.Diag(CtorDecl->getLocation(), |
| 494 | diag::warn_invalid_initializer_from_system_header); |
David Majnemer | 9588a95 | 2015-08-21 06:44:10 +0000 | [diff] [blame] | 495 | if (Entity.getKind() == InitializedEntity::EK_Member) |
| 496 | SemaRef.Diag(Entity.getDecl()->getLocation(), |
| 497 | diag::note_used_in_initialization_here); |
| 498 | else if (Entity.getKind() == InitializedEntity::EK_ArrayElement) |
| 499 | SemaRef.Diag(Loc, diag::note_used_in_initialization_here); |
Nico Weber | bcb70ee | 2014-07-02 23:51:09 +0000 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | } |
| 503 | } |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 504 | if (!InitSeq) { |
| 505 | if (!VerifyOnly) { |
| 506 | InitSeq.Diagnose(SemaRef, Entity, Kind, SubInit); |
| 507 | if (Entity.getKind() == InitializedEntity::EK_Member) |
| 508 | SemaRef.Diag(Entity.getDecl()->getLocation(), |
| 509 | diag::note_in_omitted_aggregate_initializer) |
| 510 | << /*field*/1 << Entity.getDecl(); |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 511 | else if (Entity.getKind() == InitializedEntity::EK_ArrayElement) { |
| 512 | bool IsTrailingArrayNewMember = |
| 513 | Entity.getParent() && |
| 514 | Entity.getParent()->isVariableLengthArrayNew(); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 515 | SemaRef.Diag(Loc, diag::note_in_omitted_aggregate_initializer) |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 516 | << (IsTrailingArrayNewMember ? 2 : /*array element*/0) |
| 517 | << Entity.getElementIndex(); |
| 518 | } |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 519 | } |
| 520 | return ExprError(); |
| 521 | } |
| 522 | |
| 523 | return VerifyOnly ? ExprResult(static_cast<Expr *>(nullptr)) |
| 524 | : InitSeq.Perform(SemaRef, Entity, Kind, SubInit); |
| 525 | } |
| 526 | |
| 527 | void InitListChecker::CheckEmptyInitializable(const InitializedEntity &Entity, |
| 528 | SourceLocation Loc) { |
| 529 | assert(VerifyOnly && |
| 530 | "CheckEmptyInitializable is only inteded for verification mode."); |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 531 | if (PerformEmptyInit(SemaRef, Loc, Entity, /*VerifyOnly*/true, |
| 532 | TreatUnavailableAsInvalid).isInvalid()) |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 533 | hadError = true; |
| 534 | } |
| 535 | |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 536 | void InitListChecker::FillInEmptyInitForBase( |
| 537 | unsigned Init, const CXXBaseSpecifier &Base, |
| 538 | const InitializedEntity &ParentEntity, InitListExpr *ILE, |
| 539 | bool &RequiresSecondPass, bool FillWithNoInit) { |
| 540 | assert(Init < ILE->getNumInits() && "should have been expanded"); |
| 541 | |
| 542 | InitializedEntity BaseEntity = InitializedEntity::InitializeBase( |
| 543 | SemaRef.Context, &Base, false, &ParentEntity); |
| 544 | |
| 545 | if (!ILE->getInit(Init)) { |
| 546 | ExprResult BaseInit = |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 547 | FillWithNoInit |
| 548 | ? new (SemaRef.Context) NoInitExpr(Base.getType()) |
| 549 | : PerformEmptyInit(SemaRef, ILE->getEndLoc(), BaseEntity, |
| 550 | /*VerifyOnly*/ false, TreatUnavailableAsInvalid); |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 551 | if (BaseInit.isInvalid()) { |
| 552 | hadError = true; |
| 553 | return; |
| 554 | } |
| 555 | |
| 556 | ILE->setInit(Init, BaseInit.getAs<Expr>()); |
| 557 | } else if (InitListExpr *InnerILE = |
| 558 | dyn_cast<InitListExpr>(ILE->getInit(Init))) { |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 559 | FillInEmptyInitializations(BaseEntity, InnerILE, RequiresSecondPass, |
| 560 | ILE, Init, FillWithNoInit); |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 561 | } else if (DesignatedInitUpdateExpr *InnerDIUE = |
| 562 | dyn_cast<DesignatedInitUpdateExpr>(ILE->getInit(Init))) { |
| 563 | FillInEmptyInitializations(BaseEntity, InnerDIUE->getUpdater(), |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 564 | RequiresSecondPass, ILE, Init, |
| 565 | /*FillWithNoInit =*/true); |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 569 | void InitListChecker::FillInEmptyInitForField(unsigned Init, FieldDecl *Field, |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 570 | const InitializedEntity &ParentEntity, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 571 | InitListExpr *ILE, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 572 | bool &RequiresSecondPass, |
| 573 | bool FillWithNoInit) { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 574 | SourceLocation Loc = ILE->getEndLoc(); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 575 | unsigned NumInits = ILE->getNumInits(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 576 | InitializedEntity MemberEntity |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 577 | = InitializedEntity::InitializeMember(Field, &ParentEntity); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 578 | |
| 579 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) |
| 580 | if (!RType->getDecl()->isUnion()) |
| 581 | assert(Init < NumInits && "This ILE should have been expanded"); |
| 582 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 583 | if (Init >= NumInits || !ILE->getInit(Init)) { |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 584 | if (FillWithNoInit) { |
| 585 | Expr *Filler = new (SemaRef.Context) NoInitExpr(Field->getType()); |
| 586 | if (Init < NumInits) |
| 587 | ILE->setInit(Init, Filler); |
| 588 | else |
| 589 | ILE->updateInit(SemaRef.Context, Init, Filler); |
| 590 | return; |
| 591 | } |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 592 | // C++1y [dcl.init.aggr]p7: |
| 593 | // If there are fewer initializer-clauses in the list than there are |
| 594 | // members in the aggregate, then each member not explicitly initialized |
| 595 | // shall be initialized from its brace-or-equal-initializer [...] |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 596 | if (Field->hasInClassInitializer()) { |
Reid Kleckner | d60b82f | 2014-11-17 23:36:45 +0000 | [diff] [blame] | 597 | ExprResult DIE = SemaRef.BuildCXXDefaultInitExpr(Loc, Field); |
| 598 | if (DIE.isInvalid()) { |
| 599 | hadError = true; |
| 600 | return; |
| 601 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 602 | SemaRef.checkInitializerLifetime(MemberEntity, DIE.get()); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 603 | if (Init < NumInits) |
Reid Kleckner | d60b82f | 2014-11-17 23:36:45 +0000 | [diff] [blame] | 604 | ILE->setInit(Init, DIE.get()); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 605 | else { |
Reid Kleckner | d60b82f | 2014-11-17 23:36:45 +0000 | [diff] [blame] | 606 | ILE->updateInit(SemaRef.Context, Init, DIE.get()); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 607 | RequiresSecondPass = true; |
| 608 | } |
| 609 | return; |
| 610 | } |
| 611 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 612 | if (Field->getType()->isReferenceType()) { |
| 613 | // C++ [dcl.init.aggr]p9: |
| 614 | // If an incomplete or empty initializer-list leaves a |
| 615 | // member of reference type uninitialized, the program is |
| 616 | // ill-formed. |
| 617 | SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized) |
| 618 | << Field->getType() |
| 619 | << ILE->getSyntacticForm()->getSourceRange(); |
| 620 | SemaRef.Diag(Field->getLocation(), |
| 621 | diag::note_uninit_reference_member); |
| 622 | hadError = true; |
| 623 | return; |
| 624 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 625 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 626 | ExprResult MemberInit = PerformEmptyInit(SemaRef, Loc, MemberEntity, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 627 | /*VerifyOnly*/false, |
| 628 | TreatUnavailableAsInvalid); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 629 | if (MemberInit.isInvalid()) { |
| 630 | hadError = true; |
| 631 | return; |
| 632 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 633 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 634 | if (hadError) { |
| 635 | // Do nothing |
| 636 | } else if (Init < NumInits) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 637 | ILE->setInit(Init, MemberInit.getAs<Expr>()); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 638 | } else if (!isa<ImplicitValueInitExpr>(MemberInit.get())) { |
| 639 | // Empty initialization requires a constructor call, so |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 640 | // extend the initializer list to include the constructor |
| 641 | // call and make a note that we'll need to take another pass |
| 642 | // through the initializer list. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 643 | ILE->updateInit(SemaRef.Context, Init, MemberInit.getAs<Expr>()); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 644 | RequiresSecondPass = true; |
| 645 | } |
| 646 | } else if (InitListExpr *InnerILE |
| 647 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 648 | FillInEmptyInitializations(MemberEntity, InnerILE, |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 649 | RequiresSecondPass, ILE, Init, FillWithNoInit); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 650 | else if (DesignatedInitUpdateExpr *InnerDIUE |
| 651 | = dyn_cast<DesignatedInitUpdateExpr>(ILE->getInit(Init))) |
| 652 | FillInEmptyInitializations(MemberEntity, InnerDIUE->getUpdater(), |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 653 | RequiresSecondPass, ILE, Init, |
| 654 | /*FillWithNoInit =*/true); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 657 | /// Recursively replaces NULL values within the given initializer list |
| 658 | /// with expressions that perform value-initialization of the |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 659 | /// appropriate type, and finish off the InitListExpr formation. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 660 | void |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 661 | InitListChecker::FillInEmptyInitializations(const InitializedEntity &Entity, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 662 | InitListExpr *ILE, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 663 | bool &RequiresSecondPass, |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 664 | InitListExpr *OuterILE, |
| 665 | unsigned OuterIndex, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 666 | bool FillWithNoInit) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 667 | assert((ILE->getType() != SemaRef.Context.VoidTy) && |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 668 | "Should not have void type"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 669 | |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 670 | // If this is a nested initializer list, we might have changed its contents |
| 671 | // (and therefore some of its properties, such as instantiation-dependence) |
| 672 | // while filling it in. Inform the outer initializer list so that its state |
| 673 | // can be updated to match. |
| 674 | // FIXME: We should fully build the inner initializers before constructing |
| 675 | // the outer InitListExpr instead of mutating AST nodes after they have |
| 676 | // been used as subexpressions of other nodes. |
| 677 | struct UpdateOuterILEWithUpdatedInit { |
| 678 | InitListExpr *Outer; |
| 679 | unsigned OuterIndex; |
| 680 | ~UpdateOuterILEWithUpdatedInit() { |
| 681 | if (Outer) |
| 682 | Outer->setInit(OuterIndex, Outer->getInit(OuterIndex)); |
| 683 | } |
| 684 | } UpdateOuterRAII = {OuterILE, OuterIndex}; |
| 685 | |
Richard Smith | 382bc51 | 2017-02-23 22:41:47 +0000 | [diff] [blame] | 686 | // A transparent ILE is not performing aggregate initialization and should |
| 687 | // not be filled in. |
| 688 | if (ILE->isTransparent()) |
| 689 | return; |
| 690 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 691 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 692 | const RecordDecl *RDecl = RType->getDecl(); |
| 693 | if (RDecl->isUnion() && ILE->getInitializedFieldInUnion()) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 694 | FillInEmptyInitForField(0, ILE->getInitializedFieldInUnion(), |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 695 | Entity, ILE, RequiresSecondPass, FillWithNoInit); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 696 | else if (RDecl->isUnion() && isa<CXXRecordDecl>(RDecl) && |
| 697 | cast<CXXRecordDecl>(RDecl)->hasInClassInitializer()) { |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 698 | for (auto *Field : RDecl->fields()) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 699 | if (Field->hasInClassInitializer()) { |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 700 | FillInEmptyInitForField(0, Field, Entity, ILE, RequiresSecondPass, |
| 701 | FillWithNoInit); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 702 | break; |
| 703 | } |
| 704 | } |
| 705 | } else { |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 706 | // The fields beyond ILE->getNumInits() are default initialized, so in |
| 707 | // order to leave them uninitialized, the ILE is expanded and the extra |
| 708 | // fields are then filled with NoInitExpr. |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 709 | unsigned NumElems = numStructUnionElements(ILE->getType()); |
| 710 | if (RDecl->hasFlexibleArrayMember()) |
| 711 | ++NumElems; |
| 712 | if (ILE->getNumInits() < NumElems) |
| 713 | ILE->resizeInits(SemaRef.Context, NumElems); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 714 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 715 | unsigned Init = 0; |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 716 | |
| 717 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RDecl)) { |
| 718 | for (auto &Base : CXXRD->bases()) { |
| 719 | if (hadError) |
| 720 | return; |
| 721 | |
| 722 | FillInEmptyInitForBase(Init, Base, Entity, ILE, RequiresSecondPass, |
| 723 | FillWithNoInit); |
| 724 | ++Init; |
| 725 | } |
| 726 | } |
| 727 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 728 | for (auto *Field : RDecl->fields()) { |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 729 | if (Field->isUnnamedBitfield()) |
| 730 | continue; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 731 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 732 | if (hadError) |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 733 | return; |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 734 | |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 735 | FillInEmptyInitForField(Init, Field, Entity, ILE, RequiresSecondPass, |
| 736 | FillWithNoInit); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 737 | if (hadError) |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 738 | return; |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 739 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 740 | ++Init; |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 741 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 742 | // Only look at the first initialization of a union. |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 743 | if (RDecl->isUnion()) |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 744 | break; |
| 745 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 749 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 750 | |
| 751 | QualType ElementType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 752 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 753 | InitializedEntity ElementEntity = Entity; |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 754 | unsigned NumInits = ILE->getNumInits(); |
| 755 | unsigned NumElements = NumInits; |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 756 | if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 757 | ElementType = AType->getElementType(); |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 758 | if (const auto *CAType = dyn_cast<ConstantArrayType>(AType)) |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 759 | NumElements = CAType->getSize().getZExtValue(); |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 760 | // For an array new with an unknown bound, ask for one additional element |
| 761 | // in order to populate the array filler. |
| 762 | if (Entity.isVariableLengthArrayNew()) |
| 763 | ++NumElements; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 764 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 765 | 0, Entity); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 766 | } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 767 | ElementType = VType->getElementType(); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 768 | NumElements = VType->getNumElements(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 769 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 770 | 0, Entity); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 771 | } else |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 772 | ElementType = ILE->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 773 | |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 774 | for (unsigned Init = 0; Init != NumElements; ++Init) { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 775 | if (hadError) |
| 776 | return; |
| 777 | |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 778 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement || |
| 779 | ElementEntity.getKind() == InitializedEntity::EK_VectorElement) |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 780 | ElementEntity.setElementIndex(Init); |
| 781 | |
Richard Smith | 3e26863 | 2018-05-23 23:41:38 +0000 | [diff] [blame] | 782 | if (Init >= NumInits && ILE->hasArrayFiller()) |
| 783 | return; |
| 784 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 785 | Expr *InitExpr = (Init < NumInits ? ILE->getInit(Init) : nullptr); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 786 | if (!InitExpr && Init < NumInits && ILE->hasArrayFiller()) |
| 787 | ILE->setInit(Init, ILE->getArrayFiller()); |
| 788 | else if (!InitExpr && !ILE->hasArrayFiller()) { |
| 789 | Expr *Filler = nullptr; |
| 790 | |
| 791 | if (FillWithNoInit) |
| 792 | Filler = new (SemaRef.Context) NoInitExpr(ElementType); |
| 793 | else { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 794 | ExprResult ElementInit = |
| 795 | PerformEmptyInit(SemaRef, ILE->getEndLoc(), ElementEntity, |
| 796 | /*VerifyOnly*/ false, TreatUnavailableAsInvalid); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 797 | if (ElementInit.isInvalid()) { |
| 798 | hadError = true; |
| 799 | return; |
| 800 | } |
| 801 | |
| 802 | Filler = ElementInit.getAs<Expr>(); |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | if (hadError) { |
| 806 | // Do nothing |
| 807 | } else if (Init < NumInits) { |
Argyrios Kyrtzidis | 446bcf2 | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 808 | // For arrays, just set the expression used for value-initialization |
| 809 | // of the "holes" in the array. |
| 810 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 811 | ILE->setArrayFiller(Filler); |
Argyrios Kyrtzidis | 446bcf2 | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 812 | else |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 813 | ILE->setInit(Init, Filler); |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 814 | } else { |
| 815 | // For arrays, just set the expression used for value-initialization |
| 816 | // of the rest of elements and exit. |
| 817 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) { |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 818 | ILE->setArrayFiller(Filler); |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 819 | return; |
| 820 | } |
| 821 | |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 822 | if (!isa<ImplicitValueInitExpr>(Filler) && !isa<NoInitExpr>(Filler)) { |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 823 | // Empty initialization requires a constructor call, so |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 824 | // extend the initializer list to include the constructor |
| 825 | // call and make a note that we'll need to take another pass |
| 826 | // through the initializer list. |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 827 | ILE->updateInit(SemaRef.Context, Init, Filler); |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 828 | RequiresSecondPass = true; |
| 829 | } |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 830 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 831 | } else if (InitListExpr *InnerILE |
Argyrios Kyrtzidis | d4590a5d | 2011-10-21 23:02:22 +0000 | [diff] [blame] | 832 | = dyn_cast_or_null<InitListExpr>(InitExpr)) |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 833 | FillInEmptyInitializations(ElementEntity, InnerILE, RequiresSecondPass, |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 834 | ILE, Init, FillWithNoInit); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 835 | else if (DesignatedInitUpdateExpr *InnerDIUE |
| 836 | = dyn_cast_or_null<DesignatedInitUpdateExpr>(InitExpr)) |
| 837 | FillInEmptyInitializations(ElementEntity, InnerDIUE->getUpdater(), |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 838 | RequiresSecondPass, ILE, Init, |
| 839 | /*FillWithNoInit =*/true); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 840 | } |
| 841 | } |
| 842 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 843 | InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 844 | InitListExpr *IL, QualType &T, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 845 | bool VerifyOnly, |
| 846 | bool TreatUnavailableAsInvalid) |
| 847 | : SemaRef(S), VerifyOnly(VerifyOnly), |
| 848 | TreatUnavailableAsInvalid(TreatUnavailableAsInvalid) { |
Richard Smith | 520449d | 2015-02-05 06:15:50 +0000 | [diff] [blame] | 849 | // FIXME: Check that IL isn't already the semantic form of some other |
| 850 | // InitListExpr. If it is, we'd create a broken AST. |
| 851 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 852 | hadError = false; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 853 | |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 854 | FullyStructuredList = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 855 | getStructuredSubobjectInit(IL, 0, T, nullptr, 0, IL->getSourceRange()); |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 856 | CheckExplicitInitList(Entity, IL, T, FullyStructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 857 | /*TopLevelObject=*/true); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 858 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 859 | if (!hadError && !VerifyOnly) { |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 860 | bool RequiresSecondPass = false; |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 861 | FillInEmptyInitializations(Entity, FullyStructuredList, RequiresSecondPass, |
| 862 | /*OuterILE=*/nullptr, /*OuterIndex=*/0); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 863 | if (RequiresSecondPass && !hadError) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 864 | FillInEmptyInitializations(Entity, FullyStructuredList, |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 865 | RequiresSecondPass, nullptr, 0); |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 866 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 870 | // FIXME: use a proper constant |
| 871 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 872 | if (const ConstantArrayType *CAT = |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 873 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 874 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 875 | } |
| 876 | return maxElements; |
| 877 | } |
| 878 | |
| 879 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 880 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 881 | int InitializableMembers = 0; |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 882 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(structDecl)) |
| 883 | InitializableMembers += CXXRD->getNumBases(); |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 884 | for (const auto *Field : structDecl->fields()) |
Douglas Gregor | 556e586 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 885 | if (!Field->isUnnamedBitfield()) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 886 | ++InitializableMembers; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 887 | |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 888 | if (structDecl->isUnion()) |
Eli Friedman | 0e56c82 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 889 | return std::min(InitializableMembers, 1); |
| 890 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Richard Smith | 283e207 | 2017-10-03 20:36:00 +0000 | [diff] [blame] | 893 | /// Determine whether Entity is an entity for which it is idiomatic to elide |
| 894 | /// the braces in aggregate initialization. |
| 895 | static bool isIdiomaticBraceElisionEntity(const InitializedEntity &Entity) { |
| 896 | // Recursive initialization of the one and only field within an aggregate |
| 897 | // class is considered idiomatic. This case arises in particular for |
| 898 | // initialization of std::array, where the C++ standard suggests the idiom of |
| 899 | // |
| 900 | // std::array<T, N> arr = {1, 2, 3}; |
| 901 | // |
| 902 | // (where std::array is an aggregate struct containing a single array field. |
| 903 | |
| 904 | // FIXME: Should aggregate initialization of a struct with a single |
| 905 | // base class and no members also suppress the warning? |
| 906 | if (Entity.getKind() != InitializedEntity::EK_Member || !Entity.getParent()) |
| 907 | return false; |
| 908 | |
| 909 | auto *ParentRD = |
| 910 | Entity.getParent()->getType()->castAs<RecordType>()->getDecl(); |
| 911 | if (CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(ParentRD)) |
| 912 | if (CXXRD->getNumBases()) |
| 913 | return false; |
| 914 | |
| 915 | auto FieldIt = ParentRD->field_begin(); |
| 916 | assert(FieldIt != ParentRD->field_end() && |
| 917 | "no fields but have initializer for member?"); |
| 918 | return ++FieldIt == ParentRD->field_end(); |
| 919 | } |
| 920 | |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 921 | /// Check whether the range of the initializer \p ParentIList from element |
| 922 | /// \p Index onwards can be used to initialize an object of type \p T. Update |
| 923 | /// \p Index to indicate how many elements of the list were consumed. |
| 924 | /// |
| 925 | /// This also fills in \p StructuredList, from element \p StructuredIndex |
| 926 | /// onwards, with the fully-braced, desugared form of the initialization. |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 927 | void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 928 | InitListExpr *ParentIList, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 929 | QualType T, unsigned &Index, |
| 930 | InitListExpr *StructuredList, |
Eli Friedman | c616c5f | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 931 | unsigned &StructuredIndex) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 932 | int maxElements = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 933 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 934 | if (T->isArrayType()) |
| 935 | maxElements = numArrayElements(T); |
Douglas Gregor | 8385a06 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 936 | else if (T->isRecordType()) |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 937 | maxElements = numStructUnionElements(T); |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 938 | else if (T->isVectorType()) |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 939 | maxElements = T->getAs<VectorType>()->getNumElements(); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 940 | else |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 941 | llvm_unreachable("CheckImplicitInitList(): Illegal type"); |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 942 | |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 943 | if (maxElements == 0) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 944 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 945 | SemaRef.Diag(ParentIList->getInit(Index)->getBeginLoc(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 946 | diag::err_implicit_empty_initializer); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 947 | ++Index; |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 948 | hadError = true; |
| 949 | return; |
| 950 | } |
| 951 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 952 | // Build a structured initializer list corresponding to this subobject. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 953 | InitListExpr *StructuredSubobjectInitList = getStructuredSubobjectInit( |
| 954 | ParentIList, Index, T, StructuredList, StructuredIndex, |
| 955 | SourceRange(ParentIList->getInit(Index)->getBeginLoc(), |
| 956 | ParentIList->getSourceRange().getEnd())); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 957 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 958 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 959 | // Check the element types and build the structural subobject. |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 960 | unsigned StartIndex = Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 961 | CheckListElementTypes(Entity, ParentIList, T, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 962 | /*SubobjectIsDesignatorContext=*/false, Index, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 963 | StructuredSubobjectInitList, |
Eli Friedman | c616c5f | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 964 | StructuredSubobjectInitIndex); |
Sebastian Redl | 8b6412a | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 965 | |
Richard Smith | de22923 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 966 | if (!VerifyOnly) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 967 | StructuredSubobjectInitList->setType(T); |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 968 | |
Sebastian Redl | 8b6412a | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 969 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 970 | // Update the structured sub-object initializer so that it's ending |
| 971 | // range corresponds with the end of the last initializer it used. |
Reid Kleckner | 4a09e88 | 2015-12-09 23:18:38 +0000 | [diff] [blame] | 972 | if (EndIndex < ParentIList->getNumInits() && |
| 973 | ParentIList->getInit(EndIndex)) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 974 | SourceLocation EndLoc |
| 975 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 976 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 977 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 978 | |
Sebastian Redl | 8b6412a | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 979 | // Complain about missing braces. |
Daniel Marjamaki | 817a3bf | 2017-09-29 09:44:41 +0000 | [diff] [blame] | 980 | if ((T->isArrayType() || T->isRecordType()) && |
Richard Smith | 283e207 | 2017-10-03 20:36:00 +0000 | [diff] [blame] | 981 | !ParentIList->isIdiomaticZeroInitializer(SemaRef.getLangOpts()) && |
| 982 | !isIdiomaticBraceElisionEntity(Entity)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 983 | SemaRef.Diag(StructuredSubobjectInitList->getBeginLoc(), |
Richard Smith | de22923 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 984 | diag::warn_missing_braces) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 985 | << StructuredSubobjectInitList->getSourceRange() |
| 986 | << FixItHint::CreateInsertion( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 987 | StructuredSubobjectInitList->getBeginLoc(), "{") |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 988 | << FixItHint::CreateInsertion( |
| 989 | SemaRef.getLocForEndOfToken( |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 990 | StructuredSubobjectInitList->getEndLoc()), |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 991 | "}"); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 992 | } |
Richard Smith | 79c88c3 | 2018-09-26 19:00:16 +0000 | [diff] [blame] | 993 | |
| 994 | // Warn if this type won't be an aggregate in future versions of C++. |
| 995 | auto *CXXRD = T->getAsCXXRecordDecl(); |
| 996 | if (CXXRD && CXXRD->hasUserDeclaredConstructor()) { |
| 997 | SemaRef.Diag(StructuredSubobjectInitList->getBeginLoc(), |
| 998 | diag::warn_cxx2a_compat_aggregate_init_with_ctors) |
| 999 | << StructuredSubobjectInitList->getSourceRange() << T; |
| 1000 | } |
Tanya Lattner | 5029d56 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 1001 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
Richard Smith | 420fa12 | 2015-02-12 01:50:05 +0000 | [diff] [blame] | 1004 | /// Warn that \p Entity was of scalar type and was initialized by a |
| 1005 | /// single-element braced initializer list. |
| 1006 | static void warnBracedScalarInit(Sema &S, const InitializedEntity &Entity, |
| 1007 | SourceRange Braces) { |
| 1008 | // Don't warn during template instantiation. If the initialization was |
| 1009 | // non-dependent, we warned during the initial parse; otherwise, the |
| 1010 | // type might not be scalar in some uses of the template. |
Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 1011 | if (S.inTemplateInstantiation()) |
Richard Smith | 420fa12 | 2015-02-12 01:50:05 +0000 | [diff] [blame] | 1012 | return; |
| 1013 | |
| 1014 | unsigned DiagID = 0; |
| 1015 | |
| 1016 | switch (Entity.getKind()) { |
| 1017 | case InitializedEntity::EK_VectorElement: |
| 1018 | case InitializedEntity::EK_ComplexElement: |
| 1019 | case InitializedEntity::EK_ArrayElement: |
| 1020 | case InitializedEntity::EK_Parameter: |
| 1021 | case InitializedEntity::EK_Parameter_CF_Audited: |
| 1022 | case InitializedEntity::EK_Result: |
| 1023 | // Extra braces here are suspicious. |
| 1024 | DiagID = diag::warn_braces_around_scalar_init; |
| 1025 | break; |
| 1026 | |
| 1027 | case InitializedEntity::EK_Member: |
| 1028 | // Warn on aggregate initialization but not on ctor init list or |
| 1029 | // default member initializer. |
| 1030 | if (Entity.getParent()) |
| 1031 | DiagID = diag::warn_braces_around_scalar_init; |
| 1032 | break; |
| 1033 | |
| 1034 | case InitializedEntity::EK_Variable: |
| 1035 | case InitializedEntity::EK_LambdaCapture: |
| 1036 | // No warning, might be direct-list-initialization. |
| 1037 | // FIXME: Should we warn for copy-list-initialization in these cases? |
| 1038 | break; |
| 1039 | |
| 1040 | case InitializedEntity::EK_New: |
| 1041 | case InitializedEntity::EK_Temporary: |
| 1042 | case InitializedEntity::EK_CompoundLiteralInit: |
| 1043 | // No warning, braces are part of the syntax of the underlying construct. |
| 1044 | break; |
| 1045 | |
| 1046 | case InitializedEntity::EK_RelatedResult: |
| 1047 | // No warning, we already warned when initializing the result. |
| 1048 | break; |
| 1049 | |
| 1050 | case InitializedEntity::EK_Exception: |
| 1051 | case InitializedEntity::EK_Base: |
| 1052 | case InitializedEntity::EK_Delegating: |
| 1053 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 1054 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 1055 | case InitializedEntity::EK_Binding: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 1056 | case InitializedEntity::EK_StmtExprResult: |
Richard Smith | 420fa12 | 2015-02-12 01:50:05 +0000 | [diff] [blame] | 1057 | llvm_unreachable("unexpected braced scalar init"); |
| 1058 | } |
| 1059 | |
| 1060 | if (DiagID) { |
| 1061 | S.Diag(Braces.getBegin(), DiagID) |
| 1062 | << Braces |
| 1063 | << FixItHint::CreateRemoval(Braces.getBegin()) |
| 1064 | << FixItHint::CreateRemoval(Braces.getEnd()); |
| 1065 | } |
| 1066 | } |
| 1067 | |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 1068 | /// Check whether the initializer \p IList (that was written with explicit |
| 1069 | /// braces) can be used to initialize an object of type \p T. |
| 1070 | /// |
| 1071 | /// This also fills in \p StructuredList with the fully-braced, desugared |
| 1072 | /// form of the initialization. |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1073 | void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1074 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1075 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1076 | bool TopLevelObject) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1077 | if (!VerifyOnly) { |
| 1078 | SyntacticToSemantic[IList] = StructuredList; |
| 1079 | StructuredList->setSyntacticForm(IList); |
| 1080 | } |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 1081 | |
| 1082 | unsigned Index = 0, StructuredIndex = 0; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1083 | CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1084 | Index, StructuredList, StructuredIndex, TopLevelObject); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1085 | if (!VerifyOnly) { |
Eli Friedman | 91f5ae5 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 1086 | QualType ExprTy = T; |
| 1087 | if (!ExprTy->isArrayType()) |
| 1088 | ExprTy = ExprTy.getNonLValueExprType(SemaRef.Context); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1089 | IList->setType(ExprTy); |
| 1090 | StructuredList->setType(ExprTy); |
| 1091 | } |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1092 | if (hadError) |
| 1093 | return; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 1094 | |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1095 | if (Index < IList->getNumInits()) { |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 1096 | // We have leftover initializers |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1097 | if (VerifyOnly) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1098 | if (SemaRef.getLangOpts().CPlusPlus || |
| 1099 | (SemaRef.getLangOpts().OpenCL && |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1100 | IList->getType()->isVectorType())) { |
| 1101 | hadError = true; |
| 1102 | } |
| 1103 | return; |
| 1104 | } |
| 1105 | |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 1106 | if (StructuredIndex == 1 && |
Hans Wennborg | 950f318 | 2013-05-16 09:22:40 +0000 | [diff] [blame] | 1107 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context) == |
| 1108 | SIF_None) { |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 1109 | unsigned DK = diag::ext_excess_initializers_in_char_array_initializer; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1110 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 1111 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 1112 | hadError = true; |
| 1113 | } |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 1114 | // Special-case |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1115 | SemaRef.Diag(IList->getInit(Index)->getBeginLoc(), DK) |
| 1116 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d0e48ea | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 1117 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | d42a0fb | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 1118 | // Don't complain for incomplete types, since we'll get an error |
| 1119 | // elsewhere |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1120 | QualType CurrentObjectType = StructuredList->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1121 | int initKind = |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1122 | CurrentObjectType->isArrayType()? 0 : |
| 1123 | CurrentObjectType->isVectorType()? 1 : |
| 1124 | CurrentObjectType->isScalarType()? 2 : |
| 1125 | CurrentObjectType->isUnionType()? 3 : |
| 1126 | 4; |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 1127 | |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 1128 | unsigned DK = diag::ext_excess_initializers; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1129 | if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 1130 | DK = diag::err_excess_initializers; |
| 1131 | hadError = true; |
| 1132 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1133 | if (SemaRef.getLangOpts().OpenCL && initKind == 1) { |
Nate Begeman | 425038c | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 1134 | DK = diag::err_excess_initializers; |
| 1135 | hadError = true; |
| 1136 | } |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 1137 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1138 | SemaRef.Diag(IList->getInit(Index)->getBeginLoc(), DK) |
| 1139 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 1140 | } |
| 1141 | } |
Eli Friedman | 6fcdec2 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 1142 | |
Richard Smith | 79c88c3 | 2018-09-26 19:00:16 +0000 | [diff] [blame] | 1143 | if (!VerifyOnly) { |
| 1144 | if (T->isScalarType() && IList->getNumInits() == 1 && |
| 1145 | !isa<InitListExpr>(IList->getInit(0))) |
| 1146 | warnBracedScalarInit(SemaRef, Entity, IList->getSourceRange()); |
| 1147 | |
| 1148 | // Warn if this is a class type that won't be an aggregate in future |
| 1149 | // versions of C++. |
| 1150 | auto *CXXRD = T->getAsCXXRecordDecl(); |
| 1151 | if (CXXRD && CXXRD->hasUserDeclaredConstructor()) { |
| 1152 | // Don't warn if there's an equivalent default constructor that would be |
| 1153 | // used instead. |
| 1154 | bool HasEquivCtor = false; |
| 1155 | if (IList->getNumInits() == 0) { |
| 1156 | auto *CD = SemaRef.LookupDefaultConstructor(CXXRD); |
| 1157 | HasEquivCtor = CD && !CD->isDeleted(); |
| 1158 | } |
| 1159 | |
| 1160 | if (!HasEquivCtor) { |
| 1161 | SemaRef.Diag(IList->getBeginLoc(), |
| 1162 | diag::warn_cxx2a_compat_aggregate_init_with_ctors) |
| 1163 | << IList->getSourceRange() << T; |
| 1164 | } |
| 1165 | } |
| 1166 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1169 | void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1170 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1171 | QualType &DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1172 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1173 | unsigned &Index, |
| 1174 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1175 | unsigned &StructuredIndex, |
| 1176 | bool TopLevelObject) { |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 1177 | if (DeclType->isAnyComplexType() && SubobjectIsDesignatorContext) { |
| 1178 | // Explicitly braced initializer for complex type can be real+imaginary |
| 1179 | // parts. |
| 1180 | CheckComplexType(Entity, IList, DeclType, Index, |
| 1181 | StructuredList, StructuredIndex); |
| 1182 | } else if (DeclType->isScalarType()) { |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1183 | CheckScalarType(Entity, IList, DeclType, Index, |
| 1184 | StructuredList, StructuredIndex); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 1185 | } else if (DeclType->isVectorType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1186 | CheckVectorType(Entity, IList, DeclType, Index, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1187 | StructuredList, StructuredIndex); |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 1188 | } else if (DeclType->isRecordType()) { |
| 1189 | assert(DeclType->isAggregateType() && |
| 1190 | "non-aggregate records should be handed in CheckSubElementType"); |
| 1191 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 1192 | auto Bases = |
| 1193 | CXXRecordDecl::base_class_range(CXXRecordDecl::base_class_iterator(), |
| 1194 | CXXRecordDecl::base_class_iterator()); |
| 1195 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
| 1196 | Bases = CXXRD->bases(); |
| 1197 | CheckStructUnionTypes(Entity, IList, DeclType, Bases, RD->field_begin(), |
| 1198 | SubobjectIsDesignatorContext, Index, StructuredList, |
| 1199 | StructuredIndex, TopLevelObject); |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 1200 | } else if (DeclType->isArrayType()) { |
| 1201 | llvm::APSInt Zero( |
| 1202 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
| 1203 | false); |
| 1204 | CheckArrayType(Entity, IList, DeclType, Zero, |
| 1205 | SubobjectIsDesignatorContext, Index, |
| 1206 | StructuredList, StructuredIndex); |
Steve Naroff | eaf5853 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 1207 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 1208 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1209 | ++Index; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1210 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1211 | SemaRef.Diag(IList->getBeginLoc(), diag::err_illegal_initializer_type) |
| 1212 | << DeclType; |
Eli Friedman | d0e48ea | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 1213 | hadError = true; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1214 | } else if (DeclType->isReferenceType()) { |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1215 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 1216 | StructuredList, StructuredIndex); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1217 | } else if (DeclType->isObjCObjectType()) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1218 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1219 | SemaRef.Diag(IList->getBeginLoc(), diag::err_init_objc_class) << DeclType; |
Douglas Gregor | 50ec46d | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 1220 | hadError = true; |
Andrew Savonichev | 3fee351 | 2018-11-08 11:25:41 +0000 | [diff] [blame] | 1221 | } else if (DeclType->isOCLIntelSubgroupAVCType()) { |
| 1222 | // Checks for scalar type are sufficient for these types too. |
| 1223 | CheckScalarType(Entity, IList, DeclType, Index, StructuredList, |
| 1224 | StructuredIndex); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1225 | } else { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1226 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1227 | SemaRef.Diag(IList->getBeginLoc(), diag::err_illegal_initializer_type) |
| 1228 | << DeclType; |
Douglas Gregor | 50ec46d | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 1229 | hadError = true; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1230 | } |
| 1231 | } |
| 1232 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1233 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1234 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1235 | QualType ElemType, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1236 | unsigned &Index, |
| 1237 | InitListExpr *StructuredList, |
| 1238 | unsigned &StructuredIndex) { |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1239 | Expr *expr = IList->getInit(Index); |
Richard Smith | 72752e8 | 2013-05-31 02:56:17 +0000 | [diff] [blame] | 1240 | |
| 1241 | if (ElemType->isReferenceType()) |
| 1242 | return CheckReferenceType(Entity, IList, ElemType, Index, |
| 1243 | StructuredList, StructuredIndex); |
| 1244 | |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 1245 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1246 | if (SubInitList->getNumInits() == 1 && |
| 1247 | IsStringInit(SubInitList->getInit(0), ElemType, SemaRef.Context) == |
| 1248 | SIF_None) { |
| 1249 | expr = SubInitList->getInit(0); |
| 1250 | } else if (!SemaRef.getLangOpts().CPlusPlus) { |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 1251 | InitListExpr *InnerStructuredList |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 1252 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 1253 | StructuredList, StructuredIndex, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1254 | SubInitList->getSourceRange(), true); |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 1255 | CheckExplicitInitList(Entity, SubInitList, ElemType, |
| 1256 | InnerStructuredList); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1257 | |
| 1258 | if (!hadError && !VerifyOnly) { |
| 1259 | bool RequiresSecondPass = false; |
| 1260 | FillInEmptyInitializations(Entity, InnerStructuredList, |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 1261 | RequiresSecondPass, StructuredList, |
| 1262 | StructuredIndex); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1263 | if (RequiresSecondPass && !hadError) |
| 1264 | FillInEmptyInitializations(Entity, InnerStructuredList, |
Richard Smith | f3b4ca8 | 2018-02-07 22:25:16 +0000 | [diff] [blame] | 1265 | RequiresSecondPass, StructuredList, |
| 1266 | StructuredIndex); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1267 | } |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 1268 | ++StructuredIndex; |
| 1269 | ++Index; |
| 1270 | return; |
| 1271 | } |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 1272 | // C++ initialization is handled later. |
Richard Smith | c4158e86 | 2014-07-18 04:47:25 +0000 | [diff] [blame] | 1273 | } else if (isa<ImplicitValueInitExpr>(expr)) { |
Richard Smith | 8aa561b | 2014-07-17 23:12:06 +0000 | [diff] [blame] | 1274 | // This happens during template instantiation when we see an InitListExpr |
| 1275 | // that we've already checked once. |
Richard Smith | c4158e86 | 2014-07-18 04:47:25 +0000 | [diff] [blame] | 1276 | assert(SemaRef.Context.hasSameType(expr->getType(), ElemType) && |
Richard Smith | 8aa561b | 2014-07-17 23:12:06 +0000 | [diff] [blame] | 1277 | "found implicit initialization for the wrong type"); |
| 1278 | if (!VerifyOnly) |
| 1279 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 1280 | ++Index; |
| 1281 | return; |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 1282 | } |
| 1283 | |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1284 | if (SemaRef.getLangOpts().CPlusPlus) { |
| 1285 | // C++ [dcl.init.aggr]p2: |
| 1286 | // Each member is copy-initialized from the corresponding |
| 1287 | // initializer-clause. |
| 1288 | |
| 1289 | // FIXME: Better EqualLoc? |
| 1290 | InitializationKind Kind = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1291 | InitializationKind::CreateCopy(expr->getBeginLoc(), SourceLocation()); |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1292 | InitializationSequence Seq(SemaRef, Entity, Kind, expr, |
| 1293 | /*TopLevelOfInitList*/ true); |
| 1294 | |
| 1295 | // C++14 [dcl.init.aggr]p13: |
| 1296 | // If the assignment-expression can initialize a member, the member is |
| 1297 | // initialized. Otherwise [...] brace elision is assumed |
| 1298 | // |
| 1299 | // Brace elision is never performed if the element is not an |
| 1300 | // assignment-expression. |
| 1301 | if (Seq || isa<InitListExpr>(expr)) { |
| 1302 | if (!VerifyOnly) { |
| 1303 | ExprResult Result = |
| 1304 | Seq.Perform(SemaRef, Entity, Kind, expr); |
| 1305 | if (Result.isInvalid()) |
| 1306 | hadError = true; |
| 1307 | |
| 1308 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1309 | Result.getAs<Expr>()); |
Richard Smith | 40574cc | 2015-02-16 04:42:59 +0000 | [diff] [blame] | 1310 | } else if (!Seq) |
| 1311 | hadError = true; |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1312 | ++Index; |
| 1313 | return; |
| 1314 | } |
| 1315 | |
| 1316 | // Fall through for subaggregate initialization |
| 1317 | } else if (ElemType->isScalarType() || ElemType->isAtomicType()) { |
| 1318 | // FIXME: Need to handle atomic aggregate types with implicit init lists. |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1319 | return CheckScalarType(Entity, IList, ElemType, Index, |
| 1320 | StructuredList, StructuredIndex); |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1321 | } else if (const ArrayType *arrayType = |
| 1322 | SemaRef.Context.getAsArrayType(ElemType)) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1323 | // arrayType can be incomplete if we're initializing a flexible |
| 1324 | // array member. There's nothing we can do with the completed |
| 1325 | // type here, though. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1326 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1327 | if (IsStringInit(expr, arrayType, SemaRef.Context) == SIF_None) { |
Eli Friedman | d8d7a37 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 1328 | if (!VerifyOnly) { |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1329 | CheckStringInit(expr, ElemType, arrayType, SemaRef); |
| 1330 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
Eli Friedman | d8d7a37 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 1331 | } |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1332 | ++Index; |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1333 | return; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1334 | } |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1335 | |
| 1336 | // Fall through for subaggregate initialization. |
| 1337 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1338 | } else { |
Anastasia Stulova | db7a31c | 2016-07-05 11:31:24 +0000 | [diff] [blame] | 1339 | assert((ElemType->isRecordType() || ElemType->isVectorType() || |
Egor Churaev | 45fe70f | 2017-05-10 10:28:34 +0000 | [diff] [blame] | 1340 | ElemType->isOpenCLSpecificType()) && "Unexpected type"); |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1341 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1342 | // C99 6.7.8p13: |
| 1343 | // |
| 1344 | // The initializer for a structure or union object that has |
| 1345 | // automatic storage duration shall be either an initializer |
| 1346 | // list as described below, or a single expression that has |
| 1347 | // compatible structure or union type. In the latter case, the |
| 1348 | // initial value of the object, including unnamed members, is |
| 1349 | // that of the expression. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1350 | ExprResult ExprRes = expr; |
Richard Smith | 3c567fc | 2015-02-12 01:55:09 +0000 | [diff] [blame] | 1351 | if (SemaRef.CheckSingleAssignmentConstraints( |
| 1352 | ElemType, ExprRes, !VerifyOnly) != Sema::Incompatible) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1353 | if (ExprRes.isInvalid()) |
| 1354 | hadError = true; |
| 1355 | else { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1356 | ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.get()); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 1357 | if (ExprRes.isInvalid()) |
| 1358 | hadError = true; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1359 | } |
| 1360 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1361 | ExprRes.getAs<Expr>()); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1362 | ++Index; |
| 1363 | return; |
| 1364 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1365 | ExprRes.get(); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1366 | // Fall through for subaggregate initialization |
| 1367 | } |
| 1368 | |
| 1369 | // C++ [dcl.init.aggr]p12: |
| 1370 | // |
| 1371 | // [...] Otherwise, if the member is itself a non-empty |
| 1372 | // subaggregate, brace elision is assumed and the initializer is |
| 1373 | // considered for the initialization of the first member of |
| 1374 | // the subaggregate. |
Yaxun Liu | a91da4b | 2016-10-11 15:53:28 +0000 | [diff] [blame] | 1375 | // OpenCL vector initializer is handled elsewhere. |
| 1376 | if ((!SemaRef.getLangOpts().OpenCL && ElemType->isVectorType()) || |
| 1377 | ElemType->isAggregateType()) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1378 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
| 1379 | StructuredIndex); |
| 1380 | ++StructuredIndex; |
| 1381 | } else { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1382 | if (!VerifyOnly) { |
| 1383 | // We cannot initialize this element, so let |
| 1384 | // PerformCopyInitialization produce the appropriate diagnostic. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1385 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), expr, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1386 | /*TopLevelOfInitList=*/true); |
| 1387 | } |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1388 | hadError = true; |
| 1389 | ++Index; |
| 1390 | ++StructuredIndex; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1391 | } |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 1394 | void InitListChecker::CheckComplexType(const InitializedEntity &Entity, |
| 1395 | InitListExpr *IList, QualType DeclType, |
| 1396 | unsigned &Index, |
| 1397 | InitListExpr *StructuredList, |
| 1398 | unsigned &StructuredIndex) { |
| 1399 | assert(Index == 0 && "Index in explicit init list must be zero"); |
| 1400 | |
| 1401 | // As an extension, clang supports complex initializers, which initialize |
| 1402 | // a complex number component-wise. When an explicit initializer list for |
| 1403 | // a complex number contains two two initializers, this extension kicks in: |
| 1404 | // it exepcts the initializer list to contain two elements convertible to |
| 1405 | // the element type of the complex type. The first element initializes |
| 1406 | // the real part, and the second element intitializes the imaginary part. |
| 1407 | |
| 1408 | if (IList->getNumInits() != 2) |
| 1409 | return CheckScalarType(Entity, IList, DeclType, Index, StructuredList, |
| 1410 | StructuredIndex); |
| 1411 | |
| 1412 | // This is an extension in C. (The builtin _Complex type does not exist |
| 1413 | // in the C++ standard.) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1414 | if (!SemaRef.getLangOpts().CPlusPlus && !VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1415 | SemaRef.Diag(IList->getBeginLoc(), diag::ext_complex_component_init) |
| 1416 | << IList->getSourceRange(); |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 1417 | |
| 1418 | // Initialize the complex number. |
| 1419 | QualType elementType = DeclType->getAs<ComplexType>()->getElementType(); |
| 1420 | InitializedEntity ElementEntity = |
| 1421 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
| 1422 | |
| 1423 | for (unsigned i = 0; i < 2; ++i) { |
| 1424 | ElementEntity.setElementIndex(Index); |
| 1425 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1426 | StructuredList, StructuredIndex); |
| 1427 | } |
| 1428 | } |
| 1429 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1430 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1431 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1432 | unsigned &Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1433 | InitListExpr *StructuredList, |
| 1434 | unsigned &StructuredIndex) { |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1435 | if (Index >= IList->getNumInits()) { |
Richard Smith | c823973 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 1436 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1437 | SemaRef.Diag(IList->getBeginLoc(), |
| 1438 | SemaRef.getLangOpts().CPlusPlus11 |
| 1439 | ? diag::warn_cxx98_compat_empty_scalar_initializer |
| 1440 | : diag::err_empty_scalar_initializer) |
| 1441 | << IList->getSourceRange(); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1442 | hadError = !SemaRef.getLangOpts().CPlusPlus11; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1443 | ++Index; |
| 1444 | ++StructuredIndex; |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 1445 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1446 | } |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1447 | |
| 1448 | Expr *expr = IList->getInit(Index); |
| 1449 | if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) { |
Richard Smith | fe9d2c0 | 2013-11-19 03:41:32 +0000 | [diff] [blame] | 1450 | // FIXME: This is invalid, and accepting it causes overload resolution |
| 1451 | // to pick the wrong overload in some corner cases. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1452 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1453 | SemaRef.Diag(SubIList->getBeginLoc(), |
Richard Smith | fe9d2c0 | 2013-11-19 03:41:32 +0000 | [diff] [blame] | 1454 | diag::ext_many_braces_around_scalar_init) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1455 | << SubIList->getSourceRange(); |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1456 | |
| 1457 | CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList, |
| 1458 | StructuredIndex); |
| 1459 | return; |
| 1460 | } else if (isa<DesignatedInitExpr>(expr)) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1461 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1462 | SemaRef.Diag(expr->getBeginLoc(), diag::err_designator_for_scalar_init) |
| 1463 | << DeclType << expr->getSourceRange(); |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1464 | hadError = true; |
| 1465 | ++Index; |
| 1466 | ++StructuredIndex; |
| 1467 | return; |
| 1468 | } |
| 1469 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1470 | if (VerifyOnly) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1471 | if (!SemaRef.CanPerformCopyInitialization(Entity,expr)) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1472 | hadError = true; |
| 1473 | ++Index; |
| 1474 | return; |
| 1475 | } |
| 1476 | |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1477 | ExprResult Result = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1478 | SemaRef.PerformCopyInitialization(Entity, expr->getBeginLoc(), expr, |
| 1479 | /*TopLevelOfInitList=*/true); |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1480 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1481 | Expr *ResultExpr = nullptr; |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1482 | |
| 1483 | if (Result.isInvalid()) |
| 1484 | hadError = true; // types weren't compatible. |
| 1485 | else { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1486 | ResultExpr = Result.getAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1487 | |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1488 | if (ResultExpr != expr) { |
| 1489 | // The type was promoted, update initializer list. |
| 1490 | IList->setInit(Index, ResultExpr); |
| 1491 | } |
| 1492 | } |
| 1493 | if (hadError) |
| 1494 | ++StructuredIndex; |
| 1495 | else |
| 1496 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 1497 | ++Index; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1500 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 1501 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1502 | unsigned &Index, |
| 1503 | InitListExpr *StructuredList, |
| 1504 | unsigned &StructuredIndex) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1505 | if (Index >= IList->getNumInits()) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1506 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 1507 | // general, it would be useful to pass location information down the stack, |
| 1508 | // so that we know the location (or decl) of the "current object" being |
| 1509 | // initialized. |
Sebastian Redl | b49c46c | 2011-09-24 17:48: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 | diag::err_init_reference_member_uninitialized) |
| 1513 | << DeclType << IList->getSourceRange(); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1514 | hadError = true; |
| 1515 | ++Index; |
| 1516 | ++StructuredIndex; |
| 1517 | return; |
| 1518 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1519 | |
| 1520 | Expr *expr = IList->getInit(Index); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1521 | if (isa<InitListExpr>(expr) && !SemaRef.getLangOpts().CPlusPlus11) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1522 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1523 | SemaRef.Diag(IList->getBeginLoc(), diag::err_init_non_aggr_init_list) |
| 1524 | << DeclType << IList->getSourceRange(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1525 | hadError = true; |
| 1526 | ++Index; |
| 1527 | ++StructuredIndex; |
| 1528 | return; |
| 1529 | } |
| 1530 | |
| 1531 | if (VerifyOnly) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1532 | if (!SemaRef.CanPerformCopyInitialization(Entity,expr)) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1533 | hadError = true; |
| 1534 | ++Index; |
| 1535 | return; |
| 1536 | } |
| 1537 | |
| 1538 | ExprResult Result = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1539 | SemaRef.PerformCopyInitialization(Entity, expr->getBeginLoc(), expr, |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1540 | /*TopLevelOfInitList=*/true); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1541 | |
| 1542 | if (Result.isInvalid()) |
| 1543 | hadError = true; |
| 1544 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1545 | expr = Result.getAs<Expr>(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1546 | IList->setInit(Index, expr); |
| 1547 | |
| 1548 | if (hadError) |
| 1549 | ++StructuredIndex; |
| 1550 | else |
| 1551 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 1552 | ++Index; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1555 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1556 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1557 | unsigned &Index, |
| 1558 | InitListExpr *StructuredList, |
| 1559 | unsigned &StructuredIndex) { |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1560 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 1561 | unsigned maxElements = VT->getNumElements(); |
| 1562 | unsigned numEltsInit = 0; |
| 1563 | QualType elementType = VT->getElementType(); |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1564 | |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1565 | if (Index >= IList->getNumInits()) { |
| 1566 | // Make sure the element type can be value-initialized. |
| 1567 | if (VerifyOnly) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1568 | CheckEmptyInitializable( |
| 1569 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity), |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1570 | IList->getEndLoc()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1571 | return; |
| 1572 | } |
| 1573 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1574 | if (!SemaRef.getLangOpts().OpenCL) { |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1575 | // If the initializing element is a vector, try to copy-initialize |
| 1576 | // instead of breaking it apart (which is doomed to failure anyway). |
| 1577 | Expr *Init = IList->getInit(Index); |
| 1578 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1579 | if (VerifyOnly) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1580 | if (!SemaRef.CanPerformCopyInitialization(Entity, Init)) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1581 | hadError = true; |
| 1582 | ++Index; |
| 1583 | return; |
| 1584 | } |
| 1585 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1586 | ExprResult Result = |
| 1587 | SemaRef.PerformCopyInitialization(Entity, Init->getBeginLoc(), Init, |
| 1588 | /*TopLevelOfInitList=*/true); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1589 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1590 | Expr *ResultExpr = nullptr; |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1591 | if (Result.isInvalid()) |
| 1592 | hadError = true; // types weren't compatible. |
| 1593 | else { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1594 | ResultExpr = Result.getAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1595 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1596 | if (ResultExpr != Init) { |
| 1597 | // The type was promoted, update initializer list. |
| 1598 | IList->setInit(Index, ResultExpr); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1599 | } |
| 1600 | } |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1601 | if (hadError) |
| 1602 | ++StructuredIndex; |
| 1603 | else |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1604 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1605 | ResultExpr); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1606 | ++Index; |
| 1607 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1608 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1609 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1610 | InitializedEntity ElementEntity = |
| 1611 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1612 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1613 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 1614 | // Don't attempt to go past the end of the init list |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1615 | if (Index >= IList->getNumInits()) { |
| 1616 | if (VerifyOnly) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1617 | CheckEmptyInitializable(ElementEntity, IList->getEndLoc()); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1618 | break; |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1619 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1620 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1621 | ElementEntity.setElementIndex(Index); |
| 1622 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1623 | StructuredList, StructuredIndex); |
| 1624 | } |
James Molloy | 9eef265 | 2014-06-20 14:35:13 +0000 | [diff] [blame] | 1625 | |
| 1626 | if (VerifyOnly) |
| 1627 | return; |
| 1628 | |
| 1629 | bool isBigEndian = SemaRef.Context.getTargetInfo().isBigEndian(); |
| 1630 | const VectorType *T = Entity.getType()->getAs<VectorType>(); |
| 1631 | if (isBigEndian && (T->getVectorKind() == VectorType::NeonVector || |
| 1632 | T->getVectorKind() == VectorType::NeonPolyVector)) { |
| 1633 | // The ability to use vector initializer lists is a GNU vector extension |
| 1634 | // 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] | 1635 | // endian machines it works fine, however on big endian machines it |
James Molloy | 9eef265 | 2014-06-20 14:35:13 +0000 | [diff] [blame] | 1636 | // exhibits surprising behaviour: |
| 1637 | // |
| 1638 | // uint32x2_t x = {42, 64}; |
| 1639 | // return vget_lane_u32(x, 0); // Will return 64. |
| 1640 | // |
| 1641 | // Because of this, explicitly call out that it is non-portable. |
| 1642 | // |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1643 | SemaRef.Diag(IList->getBeginLoc(), |
James Molloy | 9eef265 | 2014-06-20 14:35:13 +0000 | [diff] [blame] | 1644 | diag::warn_neon_vector_initializer_non_portable); |
| 1645 | |
| 1646 | const char *typeCode; |
| 1647 | unsigned typeSize = SemaRef.Context.getTypeSize(elementType); |
| 1648 | |
| 1649 | if (elementType->isFloatingType()) |
| 1650 | typeCode = "f"; |
| 1651 | else if (elementType->isSignedIntegerType()) |
| 1652 | typeCode = "s"; |
| 1653 | else if (elementType->isUnsignedIntegerType()) |
| 1654 | typeCode = "u"; |
| 1655 | else |
| 1656 | llvm_unreachable("Invalid element type!"); |
| 1657 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1658 | SemaRef.Diag(IList->getBeginLoc(), |
| 1659 | SemaRef.Context.getTypeSize(VT) > 64 |
| 1660 | ? diag::note_neon_vector_initializer_non_portable_q |
| 1661 | : diag::note_neon_vector_initializer_non_portable) |
| 1662 | << typeCode << typeSize; |
James Molloy | 9eef265 | 2014-06-20 14:35:13 +0000 | [diff] [blame] | 1663 | } |
| 1664 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1665 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1666 | } |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1667 | |
| 1668 | InitializedEntity ElementEntity = |
| 1669 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1670 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1671 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 1672 | for (unsigned i = 0; i < maxElements; ++i) { |
| 1673 | // Don't attempt to go past the end of the init list |
| 1674 | if (Index >= IList->getNumInits()) |
| 1675 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1676 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1677 | ElementEntity.setElementIndex(Index); |
| 1678 | |
| 1679 | QualType IType = IList->getInit(Index)->getType(); |
| 1680 | if (!IType->isVectorType()) { |
| 1681 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1682 | StructuredList, StructuredIndex); |
| 1683 | ++numEltsInit; |
| 1684 | } else { |
| 1685 | QualType VecType; |
| 1686 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 1687 | unsigned numIElts = IVT->getNumElements(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1688 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1689 | if (IType->isExtVectorType()) |
| 1690 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 1691 | else |
| 1692 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1693 | IVT->getVectorKind()); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1694 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 1695 | StructuredList, StructuredIndex); |
| 1696 | numEltsInit += numIElts; |
| 1697 | } |
| 1698 | } |
| 1699 | |
| 1700 | // OpenCL requires all elements to be initialized. |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1701 | if (numEltsInit != maxElements) { |
| 1702 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1703 | SemaRef.Diag(IList->getBeginLoc(), |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1704 | diag::err_vector_incorrect_num_initializers) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1705 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1706 | hadError = true; |
| 1707 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1708 | } |
| 1709 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1710 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1711 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1712 | llvm::APSInt elementIndex, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1713 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1714 | unsigned &Index, |
| 1715 | InitListExpr *StructuredList, |
| 1716 | unsigned &StructuredIndex) { |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1717 | const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType); |
| 1718 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1719 | // Check for the special-case of initializing an array with a string. |
| 1720 | if (Index < IList->getNumInits()) { |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1721 | if (IsStringInit(IList->getInit(Index), arrayType, SemaRef.Context) == |
| 1722 | SIF_None) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1723 | // We place the string literal directly into the resulting |
| 1724 | // initializer list. This is the only place where the structure |
| 1725 | // of the structured initializer list doesn't match exactly, |
| 1726 | // because doing so would involve allocating one character |
| 1727 | // constant for each string. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1728 | if (!VerifyOnly) { |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1729 | CheckStringInit(IList->getInit(Index), DeclType, arrayType, SemaRef); |
| 1730 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1731 | IList->getInit(Index)); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1732 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
| 1733 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1734 | ++Index; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1735 | return; |
| 1736 | } |
| 1737 | } |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1738 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1739 | // Check for VLAs; in standard C it would be possible to check this |
| 1740 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 1741 | // them in all sorts of strange places). |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1742 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1743 | SemaRef.Diag(VAT->getSizeExpr()->getBeginLoc(), |
| 1744 | diag::err_variable_object_no_init) |
| 1745 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1746 | hadError = true; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1747 | ++Index; |
| 1748 | ++StructuredIndex; |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1749 | return; |
| 1750 | } |
| 1751 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1752 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1753 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 1754 | elementIndex.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1755 | bool maxElementsKnown = false; |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1756 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1757 | maxElements = CAT->getSize(); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1758 | elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1759 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1760 | maxElementsKnown = true; |
| 1761 | } |
| 1762 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1763 | QualType elementType = arrayType->getElementType(); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1764 | while (Index < IList->getNumInits()) { |
| 1765 | Expr *Init = IList->getInit(Index); |
| 1766 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1767 | // If we're not the subobject that matches up with the '{' for |
| 1768 | // the designator, we shouldn't be handling the |
| 1769 | // designator. Return immediately. |
| 1770 | if (!SubobjectIsDesignatorContext) |
| 1771 | return; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1772 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1773 | // Handle this designated initializer. elementIndex will be |
| 1774 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1775 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1776 | DeclType, nullptr, &elementIndex, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1777 | StructuredList, StructuredIndex, true, |
| 1778 | false)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1779 | hadError = true; |
| 1780 | continue; |
| 1781 | } |
| 1782 | |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1783 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1784 | maxElements = maxElements.extend(elementIndex.getBitWidth()); |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1785 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1786 | elementIndex = elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1787 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1788 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1789 | // If the array is of incomplete type, keep track of the number of |
| 1790 | // elements in the initializer. |
| 1791 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1792 | maxElements = elementIndex; |
| 1793 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1794 | continue; |
| 1795 | } |
| 1796 | |
| 1797 | // If we know the maximum number of elements, and we've already |
| 1798 | // hit it, stop consuming elements in the initializer list. |
| 1799 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1800 | break; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1801 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1802 | InitializedEntity ElementEntity = |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1803 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1804 | Entity); |
| 1805 | // Check this element. |
| 1806 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1807 | StructuredList, StructuredIndex); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1808 | ++elementIndex; |
| 1809 | |
| 1810 | // If the array is of incomplete type, keep track of the number of |
| 1811 | // elements in the initializer. |
| 1812 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1813 | maxElements = elementIndex; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1814 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1815 | if (!hadError && DeclType->isIncompleteArrayType() && !VerifyOnly) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1816 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1817 | // be calculated here. |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1818 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Richard Smith | 73edb6d | 2017-01-24 23:18:28 +0000 | [diff] [blame] | 1819 | if (maxElements == Zero && !Entity.isVariableLengthArrayNew()) { |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1820 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1821 | // but is supported by GNU. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1822 | SemaRef.Diag(IList->getBeginLoc(), diag::ext_typecheck_zero_array_size); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1823 | } |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1824 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1825 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1826 | ArrayType::Normal, 0); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1827 | } |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1828 | if (!hadError && VerifyOnly) { |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1829 | // If there are any members of the array that get value-initialized, check |
| 1830 | // that is possible. That happens if we know the bound and don't have |
| 1831 | // enough elements, or if we're performing an array new with an unknown |
| 1832 | // bound. |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1833 | // FIXME: This needs to detect holes left by designated initializers too. |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1834 | if ((maxElementsKnown && elementIndex < maxElements) || |
| 1835 | Entity.isVariableLengthArrayNew()) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1836 | CheckEmptyInitializable( |
| 1837 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity), |
| 1838 | IList->getEndLoc()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1839 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1842 | bool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 1843 | Expr *InitExpr, |
| 1844 | FieldDecl *Field, |
| 1845 | bool TopLevelObject) { |
| 1846 | // Handle GNU flexible array initializers. |
| 1847 | unsigned FlexArrayDiag; |
| 1848 | if (isa<InitListExpr>(InitExpr) && |
| 1849 | cast<InitListExpr>(InitExpr)->getNumInits() == 0) { |
| 1850 | // Empty flexible array init always allowed as an extension |
| 1851 | FlexArrayDiag = diag::ext_flexible_array_init; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1852 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1853 | // Disallow flexible array init in C++; it is not required for gcc |
| 1854 | // compatibility, and it needs work to IRGen correctly in general. |
| 1855 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1856 | } else if (!TopLevelObject) { |
| 1857 | // Disallow flexible array init on non-top-level object |
| 1858 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1859 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
| 1860 | // Disallow flexible array init on anything which is not a variable. |
| 1861 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1862 | } else if (cast<VarDecl>(Entity.getDecl())->hasLocalStorage()) { |
| 1863 | // Disallow flexible array init on local variables. |
| 1864 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1865 | } else { |
| 1866 | // Allow other cases. |
| 1867 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1868 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1869 | |
| 1870 | if (!VerifyOnly) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1871 | SemaRef.Diag(InitExpr->getBeginLoc(), FlexArrayDiag) |
| 1872 | << InitExpr->getBeginLoc(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1873 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1874 | << Field; |
| 1875 | } |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1876 | |
| 1877 | return FlexArrayDiag != diag::ext_flexible_array_init; |
| 1878 | } |
| 1879 | |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 1880 | /// Check if the type of a class element has an accessible destructor. |
| 1881 | /// |
| 1882 | /// Aggregate initialization requires a class element's destructor be |
| 1883 | /// accessible per 11.6.1 [dcl.init.aggr]: |
| 1884 | /// |
| 1885 | /// The destructor for each element of class type is potentially invoked |
| 1886 | /// (15.4 [class.dtor]) from the context where the aggregate initialization |
| 1887 | /// occurs. |
| 1888 | static bool hasAccessibleDestructor(QualType ElementType, SourceLocation Loc, |
| 1889 | Sema &SemaRef) { |
| 1890 | auto *CXXRD = ElementType->getAsCXXRecordDecl(); |
| 1891 | if (!CXXRD) |
| 1892 | return false; |
| 1893 | |
| 1894 | CXXDestructorDecl *Destructor = SemaRef.LookupDestructor(CXXRD); |
| 1895 | SemaRef.CheckDestructorAccess(Loc, Destructor, |
| 1896 | SemaRef.PDiag(diag::err_access_dtor_temp) |
| 1897 | << ElementType); |
| 1898 | SemaRef.MarkFunctionReferenced(Loc, Destructor); |
| 1899 | if (SemaRef.DiagnoseUseOfDecl(Destructor, Loc)) |
| 1900 | return true; |
| 1901 | return false; |
| 1902 | } |
| 1903 | |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 1904 | void InitListChecker::CheckStructUnionTypes( |
| 1905 | const InitializedEntity &Entity, InitListExpr *IList, QualType DeclType, |
| 1906 | CXXRecordDecl::base_class_range Bases, RecordDecl::field_iterator Field, |
| 1907 | bool SubobjectIsDesignatorContext, unsigned &Index, |
| 1908 | InitListExpr *StructuredList, unsigned &StructuredIndex, |
| 1909 | bool TopLevelObject) { |
| 1910 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1911 | |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1912 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1913 | // confusion, we forgo checking the intializer for the entire record. |
| 1914 | if (structDecl->isInvalidDecl()) { |
Richard Smith | 845aa66 | 2012-09-28 21:23:50 +0000 | [diff] [blame] | 1915 | // Assume it was supposed to consume a single initializer. |
| 1916 | ++Index; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1917 | hadError = true; |
| 1918 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1919 | } |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1920 | |
| 1921 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1922 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1923 | |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 1924 | if (!VerifyOnly) |
| 1925 | for (FieldDecl *FD : RD->fields()) { |
| 1926 | QualType ET = SemaRef.Context.getBaseElementType(FD->getType()); |
| 1927 | if (hasAccessibleDestructor(ET, IList->getEndLoc(), SemaRef)) { |
| 1928 | hadError = true; |
| 1929 | return; |
| 1930 | } |
| 1931 | } |
| 1932 | |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1933 | // If there's a default initializer, use it. |
| 1934 | if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->hasInClassInitializer()) { |
| 1935 | if (VerifyOnly) |
| 1936 | return; |
| 1937 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1938 | Field != FieldEnd; ++Field) { |
| 1939 | if (Field->hasInClassInitializer()) { |
| 1940 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1941 | // FIXME: Actually build a CXXDefaultInitExpr? |
| 1942 | return; |
| 1943 | } |
| 1944 | } |
| 1945 | } |
| 1946 | |
Reid Kleckner | 6d829bd | 2014-11-12 21:30:23 +0000 | [diff] [blame] | 1947 | // Value-initialize the first member of the union that isn't an unnamed |
| 1948 | // bitfield. |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1949 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1950 | Field != FieldEnd; ++Field) { |
Reid Kleckner | 6d829bd | 2014-11-12 21:30:23 +0000 | [diff] [blame] | 1951 | if (!Field->isUnnamedBitfield()) { |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1952 | if (VerifyOnly) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1953 | CheckEmptyInitializable( |
| 1954 | InitializedEntity::InitializeMember(*Field, &Entity), |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1955 | IList->getEndLoc()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1956 | else |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1957 | StructuredList->setInitializedFieldInUnion(*Field); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1958 | break; |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1959 | } |
| 1960 | } |
| 1961 | return; |
| 1962 | } |
| 1963 | |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 1964 | bool InitializedSomething = false; |
| 1965 | |
| 1966 | // If we have any base classes, they are initialized prior to the fields. |
| 1967 | for (auto &Base : Bases) { |
| 1968 | Expr *Init = Index < IList->getNumInits() ? IList->getInit(Index) : nullptr; |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 1969 | |
| 1970 | // Designated inits always initialize fields, so if we see one, all |
| 1971 | // remaining base classes have no explicit initializer. |
| 1972 | if (Init && isa<DesignatedInitExpr>(Init)) |
| 1973 | Init = nullptr; |
| 1974 | |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 1975 | SourceLocation InitLoc = Init ? Init->getBeginLoc() : IList->getEndLoc(); |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 1976 | InitializedEntity BaseEntity = InitializedEntity::InitializeBase( |
| 1977 | SemaRef.Context, &Base, false, &Entity); |
| 1978 | if (Init) { |
| 1979 | CheckSubElementType(BaseEntity, IList, Base.getType(), Index, |
| 1980 | StructuredList, StructuredIndex); |
| 1981 | InitializedSomething = true; |
| 1982 | } else if (VerifyOnly) { |
| 1983 | CheckEmptyInitializable(BaseEntity, InitLoc); |
| 1984 | } |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 1985 | |
| 1986 | if (!VerifyOnly) |
| 1987 | if (hasAccessibleDestructor(Base.getType(), InitLoc, SemaRef)) { |
| 1988 | hadError = true; |
| 1989 | return; |
| 1990 | } |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 1991 | } |
| 1992 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1993 | // If structDecl is a forward declaration, this loop won't do |
| 1994 | // anything except look at designated initializers; That's okay, |
| 1995 | // because an error should get printed out elsewhere. It might be |
| 1996 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1997 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1998 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Daniel Marjamaki | 817a3bf | 2017-09-29 09:44:41 +0000 | [diff] [blame] | 1999 | bool CheckForMissingFields = |
| 2000 | !IList->isIdiomaticZeroInitializer(SemaRef.getLangOpts()); |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 2001 | bool HasDesignatedInit = false; |
Daniel Marjamaki | 817a3bf | 2017-09-29 09:44:41 +0000 | [diff] [blame] | 2002 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2003 | while (Index < IList->getNumInits()) { |
| 2004 | Expr *Init = IList->getInit(Index); |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 2005 | SourceLocation InitLoc = Init->getBeginLoc(); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2006 | |
| 2007 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2008 | // If we're not the subobject that matches up with the '{' for |
| 2009 | // the designator, we shouldn't be handling the |
| 2010 | // designator. Return immediately. |
| 2011 | if (!SubobjectIsDesignatorContext) |
| 2012 | return; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2013 | |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 2014 | HasDesignatedInit = true; |
| 2015 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2016 | // Handle this designated initializer. Field will be updated to |
| 2017 | // the next field that we'll be initializing. |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2018 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2019 | DeclType, &Field, nullptr, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2020 | StructuredList, StructuredIndex, |
| 2021 | true, TopLevelObject)) |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2022 | hadError = true; |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 2023 | else if (!VerifyOnly) { |
| 2024 | // Find the field named by the designated initializer. |
| 2025 | RecordDecl::field_iterator F = RD->field_begin(); |
| 2026 | while (std::next(F) != Field) |
| 2027 | ++F; |
| 2028 | QualType ET = SemaRef.Context.getBaseElementType(F->getType()); |
| 2029 | if (hasAccessibleDestructor(ET, InitLoc, SemaRef)) { |
| 2030 | hadError = true; |
| 2031 | return; |
| 2032 | } |
| 2033 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2034 | |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 2035 | InitializedSomething = true; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 2036 | |
| 2037 | // Disable check for missing fields when designators are used. |
| 2038 | // This matches gcc behaviour. |
| 2039 | CheckForMissingFields = false; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2040 | continue; |
| 2041 | } |
| 2042 | |
| 2043 | if (Field == FieldEnd) { |
| 2044 | // We've run out of fields. We're done. |
| 2045 | break; |
| 2046 | } |
| 2047 | |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 2048 | // We've already initialized a member of a union. We're done. |
| 2049 | if (InitializedSomething && DeclType->isUnionType()) |
| 2050 | break; |
| 2051 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2052 | // If we've hit the flexible array member at the end, we're done. |
| 2053 | if (Field->getType()->isIncompleteArrayType()) |
| 2054 | break; |
| 2055 | |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 2056 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2057 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2058 | ++Field; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 2059 | continue; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 2060 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2061 | |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2062 | // Make sure we can use this declaration. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2063 | bool InvalidUse; |
| 2064 | if (VerifyOnly) |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 2065 | InvalidUse = !SemaRef.CanUseDecl(*Field, TreatUnavailableAsInvalid); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2066 | else |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2067 | InvalidUse = SemaRef.DiagnoseUseOfDecl( |
| 2068 | *Field, IList->getInit(Index)->getBeginLoc()); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2069 | if (InvalidUse) { |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2070 | ++Index; |
| 2071 | ++Field; |
| 2072 | hadError = true; |
| 2073 | continue; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2074 | } |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2075 | |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 2076 | if (!VerifyOnly) { |
| 2077 | QualType ET = SemaRef.Context.getBaseElementType(Field->getType()); |
| 2078 | if (hasAccessibleDestructor(ET, InitLoc, SemaRef)) { |
| 2079 | hadError = true; |
| 2080 | return; |
| 2081 | } |
| 2082 | } |
| 2083 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2084 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2085 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2086 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 2087 | StructuredList, StructuredIndex); |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 2088 | InitializedSomething = true; |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 2089 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2090 | if (DeclType->isUnionType() && !VerifyOnly) { |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 2091 | // Initialize the first field within the union. |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2092 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 2093 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2094 | |
| 2095 | ++Field; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 2096 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2097 | |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 2098 | // Emit warnings for missing struct field initializers. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2099 | if (!VerifyOnly && InitializedSomething && CheckForMissingFields && |
| 2100 | Field != FieldEnd && !Field->getType()->isIncompleteArrayType() && |
| 2101 | !DeclType->isUnionType()) { |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 2102 | // It is possible we have one or more unnamed bitfields remaining. |
| 2103 | // Find first (if any) named field and emit warning. |
| 2104 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 2105 | it != end; ++it) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 2106 | if (!it->isUnnamedBitfield() && !it->hasInClassInitializer()) { |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 2107 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
Aaron Ballman | b9bb201 | 2014-01-03 14:54:10 +0000 | [diff] [blame] | 2108 | diag::warn_missing_field_initializers) << *it; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 2109 | break; |
| 2110 | } |
| 2111 | } |
| 2112 | } |
| 2113 | |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 2114 | // Check that any remaining fields can be value-initialized. |
| 2115 | if (VerifyOnly && Field != FieldEnd && !DeclType->isUnionType() && |
| 2116 | !Field->getType()->isIncompleteArrayType()) { |
| 2117 | // FIXME: Should check for holes left by designated initializers too. |
| 2118 | for (; Field != FieldEnd && !hadError; ++Field) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 2119 | if (!Field->isUnnamedBitfield() && !Field->hasInClassInitializer()) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 2120 | CheckEmptyInitializable( |
| 2121 | InitializedEntity::InitializeMember(*Field, &Entity), |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2122 | IList->getEndLoc()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 2123 | } |
| 2124 | } |
| 2125 | |
Akira Hatanaka | 2ccb319 | 2018-09-07 02:38:01 +0000 | [diff] [blame] | 2126 | // Check that the types of the remaining fields have accessible destructors. |
| 2127 | if (!VerifyOnly) { |
| 2128 | // If the initializer expression has a designated initializer, check the |
| 2129 | // elements for which a designated initializer is not provided too. |
| 2130 | RecordDecl::field_iterator I = HasDesignatedInit ? RD->field_begin() |
| 2131 | : Field; |
| 2132 | for (RecordDecl::field_iterator E = RD->field_end(); I != E; ++I) { |
| 2133 | QualType ET = SemaRef.Context.getBaseElementType(I->getType()); |
| 2134 | if (hasAccessibleDestructor(ET, IList->getEndLoc(), SemaRef)) { |
| 2135 | hadError = true; |
| 2136 | return; |
| 2137 | } |
| 2138 | } |
| 2139 | } |
| 2140 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2141 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 2142 | Index >= IList->getNumInits()) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2143 | return; |
| 2144 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2145 | if (CheckFlexibleArrayInit(Entity, IList->getInit(Index), *Field, |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 2146 | TopLevelObject)) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2147 | hadError = true; |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 2148 | ++Index; |
| 2149 | return; |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2150 | } |
| 2151 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2152 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2153 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2154 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2155 | if (isa<InitListExpr>(IList->getInit(Index))) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2156 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2157 | StructuredList, StructuredIndex); |
| 2158 | else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2159 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 2160 | StructuredList, StructuredIndex); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 2161 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 2162 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2163 | /// Expand a field designator that refers to a member of an |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2164 | /// anonymous struct or union into a series of field designators that |
| 2165 | /// refers to the field within the appropriate subobject. |
| 2166 | /// |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2167 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2168 | DesignatedInitExpr *DIE, |
| 2169 | unsigned DesigIdx, |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2170 | IndirectFieldDecl *IndirectField) { |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2171 | typedef DesignatedInitExpr::Designator Designator; |
| 2172 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2173 | // Build the replacement designators. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2174 | SmallVector<Designator, 4> Replacements; |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2175 | for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), |
| 2176 | PE = IndirectField->chain_end(); PI != PE; ++PI) { |
| 2177 | if (PI + 1 == PE) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2178 | Replacements.push_back(Designator((IdentifierInfo *)nullptr, |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2179 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 2180 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 2181 | else |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2182 | Replacements.push_back(Designator((IdentifierInfo *)nullptr, |
| 2183 | SourceLocation(), SourceLocation())); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2184 | assert(isa<FieldDecl>(*PI)); |
| 2185 | Replacements.back().setField(cast<FieldDecl>(*PI)); |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2186 | } |
| 2187 | |
| 2188 | // Expand the current designator into the set of replacement |
| 2189 | // designators, so we have a full subobject path down to where the |
| 2190 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 03e8bdc | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 2191 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2192 | &Replacements[0] + Replacements.size()); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2193 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2194 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2195 | static DesignatedInitExpr *CloneDesignatedInitExpr(Sema &SemaRef, |
| 2196 | DesignatedInitExpr *DIE) { |
| 2197 | unsigned NumIndexExprs = DIE->getNumSubExprs() - 1; |
| 2198 | SmallVector<Expr*, 4> IndexExprs(NumIndexExprs); |
| 2199 | for (unsigned I = 0; I < NumIndexExprs; ++I) |
| 2200 | IndexExprs[I] = DIE->getSubExpr(I + 1); |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 2201 | return DesignatedInitExpr::Create(SemaRef.Context, DIE->designators(), |
| 2202 | IndexExprs, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2203 | DIE->getEqualOrColonLoc(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2204 | DIE->usesGNUSyntax(), DIE->getInit()); |
| 2205 | } |
| 2206 | |
Kaelyn Uhrain | b02c5e9 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 2207 | namespace { |
| 2208 | |
| 2209 | // Callback to only accept typo corrections that are for field members of |
| 2210 | // the given struct or union. |
Bruno Ricci | 70ad396 | 2019-03-25 17:08:51 +0000 | [diff] [blame] | 2211 | class FieldInitializerValidatorCCC final : public CorrectionCandidateCallback { |
Kaelyn Uhrain | b02c5e9 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 2212 | public: |
| 2213 | explicit FieldInitializerValidatorCCC(RecordDecl *RD) |
| 2214 | : Record(RD) {} |
| 2215 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 2216 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
Kaelyn Uhrain | b02c5e9 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 2217 | FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>(); |
| 2218 | return FD && FD->getDeclContext()->getRedeclContext()->Equals(Record); |
| 2219 | } |
| 2220 | |
Bruno Ricci | 70ad396 | 2019-03-25 17:08:51 +0000 | [diff] [blame] | 2221 | std::unique_ptr<CorrectionCandidateCallback> clone() override { |
| 2222 | return llvm::make_unique<FieldInitializerValidatorCCC>(*this); |
| 2223 | } |
| 2224 | |
Kaelyn Uhrain | b02c5e9 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 2225 | private: |
| 2226 | RecordDecl *Record; |
| 2227 | }; |
| 2228 | |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 2229 | } // end anonymous namespace |
Kaelyn Uhrain | b02c5e9 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 2230 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2231 | /// Check the well-formedness of a C99 designated initializer. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2232 | /// |
| 2233 | /// Determines whether the designated initializer @p DIE, which |
| 2234 | /// resides at the given @p Index within the initializer list @p |
| 2235 | /// IList, is well-formed for a current object of type @p DeclType |
| 2236 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2237 | /// within the current subobject is returned in either |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2238 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2239 | /// |
| 2240 | /// @param IList The initializer list in which this designated |
| 2241 | /// initializer occurs. |
| 2242 | /// |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 2243 | /// @param DIE The designated initializer expression. |
| 2244 | /// |
| 2245 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2246 | /// |
Dmitri Gribenko | adba9be | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 2247 | /// @param CurrentObjectType The type of the "current object" (C99 6.7.8p17), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2248 | /// into which the designation in @p DIE should refer. |
| 2249 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2250 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 2251 | /// a field, this will be set to the field declaration corresponding |
| 2252 | /// to the field named by the designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2253 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2254 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 2255 | /// DIE is an array designator or GNU array-range designator, this |
| 2256 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2257 | /// |
| 2258 | /// @param Index Index into @p IList where the designated initializer |
| 2259 | /// @p DIE occurs. |
| 2260 | /// |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2261 | /// @param StructuredList The initializer list expression that |
| 2262 | /// describes all of the subobject initializers in the order they'll |
| 2263 | /// actually be initialized. |
| 2264 | /// |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2265 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2266 | bool |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2267 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2268 | InitListExpr *IList, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2269 | DesignatedInitExpr *DIE, |
| 2270 | unsigned DesigIdx, |
| 2271 | QualType &CurrentObjectType, |
| 2272 | RecordDecl::field_iterator *NextField, |
| 2273 | llvm::APSInt *NextElementIndex, |
| 2274 | unsigned &Index, |
| 2275 | InitListExpr *StructuredList, |
| 2276 | unsigned &StructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2277 | bool FinishSubobjectInit, |
| 2278 | bool TopLevelObject) { |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 2279 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2280 | // Check the actual initialization for the designated object type. |
| 2281 | bool prevHadError = hadError; |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 2282 | |
| 2283 | // Temporarily remove the designator expression from the |
| 2284 | // initializer list that the child calls see, so that we don't try |
| 2285 | // to re-process the designator. |
| 2286 | unsigned OldIndex = Index; |
| 2287 | IList->setInit(OldIndex, DIE->getInit()); |
| 2288 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2289 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2290 | StructuredList, StructuredIndex); |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 2291 | |
| 2292 | // Restore the designated initializer expression in the syntactic |
| 2293 | // form of the initializer list. |
| 2294 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 2295 | DIE->setInit(IList->getInit(OldIndex)); |
| 2296 | IList->setInit(OldIndex, DIE); |
| 2297 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2298 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2299 | } |
| 2300 | |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 2301 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2302 | bool IsFirstDesignator = (DesigIdx == 0); |
| 2303 | if (!VerifyOnly) { |
| 2304 | assert((IsFirstDesignator || StructuredList) && |
| 2305 | "Need a non-designated initializer list to start from"); |
| 2306 | |
| 2307 | // Determine the structural initializer list that corresponds to the |
| 2308 | // current subobject. |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2309 | if (IsFirstDesignator) |
| 2310 | StructuredList = SyntacticToSemantic.lookup(IList); |
| 2311 | else { |
| 2312 | Expr *ExistingInit = StructuredIndex < StructuredList->getNumInits() ? |
| 2313 | StructuredList->getInit(StructuredIndex) : nullptr; |
| 2314 | if (!ExistingInit && StructuredList->hasArrayFiller()) |
| 2315 | ExistingInit = StructuredList->getArrayFiller(); |
| 2316 | |
| 2317 | if (!ExistingInit) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2318 | StructuredList = getStructuredSubobjectInit( |
| 2319 | IList, Index, CurrentObjectType, StructuredList, StructuredIndex, |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2320 | SourceRange(D->getBeginLoc(), DIE->getEndLoc())); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2321 | else if (InitListExpr *Result = dyn_cast<InitListExpr>(ExistingInit)) |
| 2322 | StructuredList = Result; |
| 2323 | else { |
| 2324 | if (DesignatedInitUpdateExpr *E = |
| 2325 | dyn_cast<DesignatedInitUpdateExpr>(ExistingInit)) |
| 2326 | StructuredList = E->getUpdater(); |
| 2327 | else { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2328 | DesignatedInitUpdateExpr *DIUE = new (SemaRef.Context) |
| 2329 | DesignatedInitUpdateExpr(SemaRef.Context, D->getBeginLoc(), |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2330 | ExistingInit, DIE->getEndLoc()); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2331 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, DIUE); |
| 2332 | StructuredList = DIUE->getUpdater(); |
| 2333 | } |
| 2334 | |
| 2335 | // We need to check on source range validity because the previous |
| 2336 | // initializer does not have to be an explicit initializer. e.g., |
| 2337 | // |
| 2338 | // struct P { int a, b; }; |
| 2339 | // struct PP { struct P p } l = { { .a = 2 }, .p.b = 3 }; |
| 2340 | // |
| 2341 | // There is an overwrite taking place because the first braced initializer |
| 2342 | // list "{ .a = 2 }" already provides value for .p.b (which is zero). |
| 2343 | if (ExistingInit->getSourceRange().isValid()) { |
| 2344 | // We are creating an initializer list that initializes the |
| 2345 | // subobjects of the current object, but there was already an |
| 2346 | // initialization that completely initialized the current |
| 2347 | // subobject, e.g., by a compound literal: |
| 2348 | // |
| 2349 | // struct X { int a, b; }; |
| 2350 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
| 2351 | // |
| 2352 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 2353 | // designated initializer re-initializes the whole |
| 2354 | // subobject [0], overwriting previous initializers. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2355 | SemaRef.Diag(D->getBeginLoc(), |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2356 | diag::warn_subobject_initializer_overrides) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2357 | << SourceRange(D->getBeginLoc(), DIE->getEndLoc()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2358 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2359 | SemaRef.Diag(ExistingInit->getBeginLoc(), |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2360 | diag::note_previous_initializer) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2361 | << /*FIXME:has side effects=*/0 << ExistingInit->getSourceRange(); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2362 | } |
| 2363 | } |
| 2364 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2365 | assert(StructuredList && "Expected a structured initializer list"); |
| 2366 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2367 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2368 | if (D->isFieldDesignator()) { |
| 2369 | // C99 6.7.8p7: |
| 2370 | // |
| 2371 | // If a designator has the form |
| 2372 | // |
| 2373 | // . identifier |
| 2374 | // |
| 2375 | // then the current object (defined below) shall have |
| 2376 | // structure or union type and the identifier shall be the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2377 | // name of a member of that type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2378 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2379 | if (!RT) { |
| 2380 | SourceLocation Loc = D->getDotLoc(); |
| 2381 | if (Loc.isInvalid()) |
| 2382 | Loc = D->getFieldLoc(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2383 | if (!VerifyOnly) |
| 2384 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2385 | << SemaRef.getLangOpts().CPlusPlus << CurrentObjectType; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2386 | ++Index; |
| 2387 | return true; |
| 2388 | } |
| 2389 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2390 | FieldDecl *KnownField = D->getField(); |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2391 | if (!KnownField) { |
| 2392 | IdentifierInfo *FieldName = D->getFieldName(); |
| 2393 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
| 2394 | for (NamedDecl *ND : Lookup) { |
| 2395 | if (auto *FD = dyn_cast<FieldDecl>(ND)) { |
| 2396 | KnownField = FD; |
| 2397 | break; |
| 2398 | } |
| 2399 | if (auto *IFD = dyn_cast<IndirectFieldDecl>(ND)) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2400 | // In verify mode, don't modify the original. |
| 2401 | if (VerifyOnly) |
| 2402 | DIE = CloneDesignatedInitExpr(SemaRef, DIE); |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2403 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IFD); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2404 | D = DIE->getDesignator(DesigIdx); |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2405 | KnownField = cast<FieldDecl>(*IFD->chain_begin()); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2406 | break; |
| 2407 | } |
| 2408 | } |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2409 | if (!KnownField) { |
| 2410 | if (VerifyOnly) { |
| 2411 | ++Index; |
| 2412 | return true; // No typo correction when just trying this out. |
| 2413 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2414 | |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2415 | // Name lookup found something, but it wasn't a field. |
| 2416 | if (!Lookup.empty()) { |
| 2417 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
| 2418 | << FieldName; |
| 2419 | SemaRef.Diag(Lookup.front()->getLocation(), |
| 2420 | diag::note_field_designator_found); |
| 2421 | ++Index; |
| 2422 | return true; |
| 2423 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2424 | |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2425 | // Name lookup didn't find anything. |
| 2426 | // Determine whether this was a typo for another field name. |
Bruno Ricci | 70ad396 | 2019-03-25 17:08:51 +0000 | [diff] [blame] | 2427 | FieldInitializerValidatorCCC CCC(RT->getDecl()); |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 2428 | if (TypoCorrection Corrected = SemaRef.CorrectTypo( |
| 2429 | DeclarationNameInfo(FieldName, D->getFieldLoc()), |
Bruno Ricci | 70ad396 | 2019-03-25 17:08:51 +0000 | [diff] [blame] | 2430 | Sema::LookupMemberName, /*Scope=*/nullptr, /*SS=*/nullptr, CCC, |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 2431 | Sema::CTK_ErrorRecovery, RT->getDecl())) { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 2432 | SemaRef.diagnoseTypo( |
| 2433 | Corrected, |
| 2434 | SemaRef.PDiag(diag::err_field_designator_unknown_suggest) |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2435 | << FieldName << CurrentObjectType); |
| 2436 | KnownField = Corrected.getCorrectionDeclAs<FieldDecl>(); |
Benjamin Kramer | afe718f | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 2437 | hadError = true; |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 2438 | } else { |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2439 | // Typo correction didn't find anything. |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 2440 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 2441 | << FieldName << CurrentObjectType; |
| 2442 | ++Index; |
| 2443 | return true; |
| 2444 | } |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 2445 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2446 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2447 | |
David Majnemer | 58e4ea9 | 2014-08-23 01:48:50 +0000 | [diff] [blame] | 2448 | unsigned FieldIndex = 0; |
Akira Hatanaka | 8eccb9b | 2017-01-17 19:35:54 +0000 | [diff] [blame] | 2449 | |
| 2450 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) |
| 2451 | FieldIndex = CXXRD->getNumBases(); |
| 2452 | |
David Majnemer | 58e4ea9 | 2014-08-23 01:48:50 +0000 | [diff] [blame] | 2453 | for (auto *FI : RT->getDecl()->fields()) { |
| 2454 | if (FI->isUnnamedBitfield()) |
| 2455 | continue; |
Richard Smith | fe1bc70 | 2016-04-08 19:57:40 +0000 | [diff] [blame] | 2456 | if (declaresSameEntity(KnownField, FI)) { |
| 2457 | KnownField = FI; |
David Majnemer | 58e4ea9 | 2014-08-23 01:48:50 +0000 | [diff] [blame] | 2458 | break; |
Richard Smith | fe1bc70 | 2016-04-08 19:57:40 +0000 | [diff] [blame] | 2459 | } |
David Majnemer | 58e4ea9 | 2014-08-23 01:48:50 +0000 | [diff] [blame] | 2460 | ++FieldIndex; |
| 2461 | } |
| 2462 | |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2463 | RecordDecl::field_iterator Field = |
| 2464 | RecordDecl::field_iterator(DeclContext::decl_iterator(KnownField)); |
| 2465 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2466 | // All of the fields of a union are located at the same place in |
| 2467 | // the initializer list. |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 2468 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2469 | FieldIndex = 0; |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2470 | if (!VerifyOnly) { |
| 2471 | FieldDecl *CurrentField = StructuredList->getInitializedFieldInUnion(); |
Richard Smith | fe1bc70 | 2016-04-08 19:57:40 +0000 | [diff] [blame] | 2472 | if (CurrentField && !declaresSameEntity(CurrentField, *Field)) { |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2473 | assert(StructuredList->getNumInits() == 1 |
| 2474 | && "A union should never have more than one initializer!"); |
| 2475 | |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2476 | Expr *ExistingInit = StructuredList->getInit(0); |
Vassil Vassilev | 1a1678e | 2017-04-14 08:48:08 +0000 | [diff] [blame] | 2477 | if (ExistingInit) { |
| 2478 | // We're about to throw away an initializer, emit warning. |
| 2479 | SemaRef.Diag(D->getFieldLoc(), |
| 2480 | diag::warn_initializer_overrides) |
| 2481 | << D->getSourceRange(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2482 | SemaRef.Diag(ExistingInit->getBeginLoc(), |
Vassil Vassilev | 1a1678e | 2017-04-14 08:48:08 +0000 | [diff] [blame] | 2483 | diag::note_previous_initializer) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2484 | << /*FIXME:has side effects=*/0 |
| 2485 | << ExistingInit->getSourceRange(); |
Vassil Vassilev | 1a1678e | 2017-04-14 08:48:08 +0000 | [diff] [blame] | 2486 | } |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2487 | |
| 2488 | // remove existing initializer |
| 2489 | StructuredList->resizeInits(SemaRef.Context, 0); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2490 | StructuredList->setInitializedFieldInUnion(nullptr); |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2491 | } |
| 2492 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2493 | StructuredList->setInitializedFieldInUnion(*Field); |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2494 | } |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 2495 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2496 | |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2497 | // Make sure we can use this declaration. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2498 | bool InvalidUse; |
| 2499 | if (VerifyOnly) |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 2500 | InvalidUse = !SemaRef.CanUseDecl(*Field, TreatUnavailableAsInvalid); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2501 | else |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2502 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, D->getFieldLoc()); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2503 | if (InvalidUse) { |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2504 | ++Index; |
| 2505 | return true; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2506 | } |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2507 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2508 | if (!VerifyOnly) { |
| 2509 | // Update the designator with the field declaration. |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2510 | D->setField(*Field); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2511 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2512 | // Make sure that our non-designated initializer list has space |
| 2513 | // for a subobject corresponding to this field. |
| 2514 | if (FieldIndex >= StructuredList->getNumInits()) |
| 2515 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
| 2516 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2517 | |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2518 | // This designator names a flexible array member. |
| 2519 | if (Field->getType()->isIncompleteArrayType()) { |
| 2520 | bool Invalid = false; |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 2521 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2522 | // We can't designate an object within the flexible array |
| 2523 | // member (because GCC doesn't allow it). |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2524 | if (!VerifyOnly) { |
| 2525 | DesignatedInitExpr::Designator *NextD |
| 2526 | = DIE->getDesignator(DesigIdx + 1); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2527 | SemaRef.Diag(NextD->getBeginLoc(), |
| 2528 | diag::err_designator_into_flexible_array_member) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2529 | << SourceRange(NextD->getBeginLoc(), DIE->getEndLoc()); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2530 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2531 | << *Field; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2532 | } |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2533 | Invalid = true; |
| 2534 | } |
| 2535 | |
Chris Lattner | 001b29c | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 2536 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 2537 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2538 | // The initializer is not an initializer list. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2539 | if (!VerifyOnly) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2540 | SemaRef.Diag(DIE->getInit()->getBeginLoc(), |
| 2541 | diag::err_flexible_array_init_needs_braces) |
| 2542 | << DIE->getInit()->getSourceRange(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2543 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2544 | << *Field; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2545 | } |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2546 | Invalid = true; |
| 2547 | } |
| 2548 | |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 2549 | // Check GNU flexible array initializer. |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2550 | if (!Invalid && CheckFlexibleArrayInit(Entity, DIE->getInit(), *Field, |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 2551 | TopLevelObject)) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2552 | Invalid = true; |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2553 | |
| 2554 | if (Invalid) { |
| 2555 | ++Index; |
| 2556 | return true; |
| 2557 | } |
| 2558 | |
| 2559 | // Initialize the array. |
| 2560 | bool prevHadError = hadError; |
| 2561 | unsigned newStructuredIndex = FieldIndex; |
| 2562 | unsigned OldIndex = Index; |
| 2563 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2564 | |
| 2565 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2566 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2567 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2568 | StructuredList, newStructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2569 | |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2570 | IList->setInit(OldIndex, DIE); |
| 2571 | if (hadError && !prevHadError) { |
| 2572 | ++Field; |
| 2573 | ++FieldIndex; |
| 2574 | if (NextField) |
| 2575 | *NextField = Field; |
| 2576 | StructuredIndex = FieldIndex; |
| 2577 | return true; |
| 2578 | } |
| 2579 | } else { |
| 2580 | // Recurse to check later designated subobjects. |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 2581 | QualType FieldType = Field->getType(); |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2582 | unsigned newStructuredIndex = FieldIndex; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2583 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2584 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2585 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2586 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2587 | FieldType, nullptr, nullptr, Index, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2588 | StructuredList, newStructuredIndex, |
Alexey Bataev | 86a489e | 2016-01-25 05:14:03 +0000 | [diff] [blame] | 2589 | FinishSubobjectInit, false)) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2590 | return true; |
| 2591 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2592 | |
| 2593 | // Find the position of the next field to be initialized in this |
| 2594 | // subobject. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2595 | ++Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2596 | ++FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2597 | |
| 2598 | // If this the first designator, our caller will continue checking |
| 2599 | // the rest of this struct/class/union subobject. |
| 2600 | if (IsFirstDesignator) { |
| 2601 | if (NextField) |
| 2602 | *NextField = Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2603 | StructuredIndex = FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2604 | return false; |
| 2605 | } |
| 2606 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2607 | if (!FinishSubobjectInit) |
| 2608 | return false; |
| 2609 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2610 | // We've already initialized something in the union; we're done. |
| 2611 | if (RT->getDecl()->isUnion()) |
| 2612 | return hadError; |
| 2613 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2614 | // Check the remaining fields within this class/struct/union subobject. |
| 2615 | bool prevHadError = hadError; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2616 | |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 2617 | auto NoBases = |
| 2618 | CXXRecordDecl::base_class_range(CXXRecordDecl::base_class_iterator(), |
| 2619 | CXXRecordDecl::base_class_iterator()); |
| 2620 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, NoBases, Field, |
| 2621 | false, Index, StructuredList, FieldIndex); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2622 | return hadError && !prevHadError; |
| 2623 | } |
| 2624 | |
| 2625 | // C99 6.7.8p6: |
| 2626 | // |
| 2627 | // If a designator has the form |
| 2628 | // |
| 2629 | // [ constant-expression ] |
| 2630 | // |
| 2631 | // then the current object (defined below) shall have array |
| 2632 | // type and the expression shall be an integer constant |
| 2633 | // expression. If the array is of unknown size, any |
| 2634 | // nonnegative value is valid. |
| 2635 | // |
| 2636 | // Additionally, cope with the GNU extension that permits |
| 2637 | // designators of the form |
| 2638 | // |
| 2639 | // [ constant-expression ... constant-expression ] |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 2640 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2641 | if (!AT) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2642 | if (!VerifyOnly) |
| 2643 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
| 2644 | << CurrentObjectType; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2645 | ++Index; |
| 2646 | return true; |
| 2647 | } |
| 2648 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2649 | Expr *IndexExpr = nullptr; |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2650 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 2651 | if (D->isArrayDesignator()) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2652 | IndexExpr = DIE->getArrayIndex(*D); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2653 | DesignatedStartIndex = IndexExpr->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2654 | DesignatedEndIndex = DesignatedStartIndex; |
| 2655 | } else { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2656 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2657 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2658 | DesignatedStartIndex = |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2659 | DIE->getArrayRangeStart(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2660 | DesignatedEndIndex = |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2661 | DIE->getArrayRangeEnd(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2662 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2663 | |
Chris Lattner | b0ed51d | 2011-02-19 22:28:58 +0000 | [diff] [blame] | 2664 | // Codegen can't handle evaluating array range designators that have side |
| 2665 | // effects, because we replicate the AST value for each initialized element. |
| 2666 | // As such, set the sawArrayRangeDesignator() bit if we initialize multiple |
| 2667 | // elements with something that has a side effect, so codegen can emit an |
| 2668 | // "error unsupported" error instead of miscompiling the app. |
| 2669 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2670 | DIE->getInit()->HasSideEffects(SemaRef.Context) && !VerifyOnly) |
Douglas Gregor | bf7207a | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 2671 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2672 | } |
| 2673 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2674 | if (isa<ConstantArrayType>(AT)) { |
| 2675 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2676 | DesignatedStartIndex |
| 2677 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2678 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2679 | DesignatedEndIndex |
| 2680 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2681 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 2682 | if (DesignatedEndIndex >= MaxElements) { |
Eli Friedman | ed0f916 | 2011-09-26 18:53:43 +0000 | [diff] [blame] | 2683 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2684 | SemaRef.Diag(IndexExpr->getBeginLoc(), |
| 2685 | diag::err_array_designator_too_large) |
| 2686 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
| 2687 | << IndexExpr->getSourceRange(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2688 | ++Index; |
| 2689 | return true; |
| 2690 | } |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2691 | } else { |
Argyrios Kyrtzidis | 4746c2f | 2015-07-27 23:16:53 +0000 | [diff] [blame] | 2692 | unsigned DesignatedIndexBitWidth = |
| 2693 | ConstantArrayType::getMaxSizeBits(SemaRef.Context); |
| 2694 | DesignatedStartIndex = |
| 2695 | DesignatedStartIndex.extOrTrunc(DesignatedIndexBitWidth); |
| 2696 | DesignatedEndIndex = |
| 2697 | DesignatedEndIndex.extOrTrunc(DesignatedIndexBitWidth); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2698 | DesignatedStartIndex.setIsUnsigned(true); |
| 2699 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2700 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2701 | |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2702 | if (!VerifyOnly && StructuredList->isStringLiteralInit()) { |
| 2703 | // We're modifying a string literal init; we have to decompose the string |
| 2704 | // so we can modify the individual characters. |
| 2705 | ASTContext &Context = SemaRef.Context; |
| 2706 | Expr *SubExpr = StructuredList->getInit(0)->IgnoreParens(); |
| 2707 | |
| 2708 | // Compute the character type |
| 2709 | QualType CharTy = AT->getElementType(); |
| 2710 | |
| 2711 | // Compute the type of the integer literals. |
| 2712 | QualType PromotedCharTy = CharTy; |
| 2713 | if (CharTy->isPromotableIntegerType()) |
| 2714 | PromotedCharTy = Context.getPromotedIntegerType(CharTy); |
| 2715 | unsigned PromotedCharTyWidth = Context.getTypeSize(PromotedCharTy); |
| 2716 | |
| 2717 | if (StringLiteral *SL = dyn_cast<StringLiteral>(SubExpr)) { |
| 2718 | // Get the length of the string. |
| 2719 | uint64_t StrLen = SL->getLength(); |
| 2720 | if (cast<ConstantArrayType>(AT)->getSize().ult(StrLen)) |
| 2721 | StrLen = cast<ConstantArrayType>(AT)->getSize().getZExtValue(); |
| 2722 | StructuredList->resizeInits(Context, StrLen); |
| 2723 | |
| 2724 | // Build a literal for each character in the string, and put them into |
| 2725 | // the init list. |
| 2726 | for (unsigned i = 0, e = StrLen; i != e; ++i) { |
| 2727 | llvm::APInt CodeUnit(PromotedCharTyWidth, SL->getCodeUnit(i)); |
| 2728 | Expr *Init = new (Context) IntegerLiteral( |
Eli Friedman | 6cc05f7 | 2013-06-11 22:26:34 +0000 | [diff] [blame] | 2729 | Context, CodeUnit, PromotedCharTy, SubExpr->getExprLoc()); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2730 | if (CharTy != PromotedCharTy) |
| 2731 | Init = ImplicitCastExpr::Create(Context, CharTy, CK_IntegralCast, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2732 | Init, nullptr, VK_RValue); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2733 | StructuredList->updateInit(Context, i, Init); |
| 2734 | } |
| 2735 | } else { |
| 2736 | ObjCEncodeExpr *E = cast<ObjCEncodeExpr>(SubExpr); |
| 2737 | std::string Str; |
| 2738 | Context.getObjCEncodingForType(E->getEncodedType(), Str); |
| 2739 | |
| 2740 | // Get the length of the string. |
| 2741 | uint64_t StrLen = Str.size(); |
| 2742 | if (cast<ConstantArrayType>(AT)->getSize().ult(StrLen)) |
| 2743 | StrLen = cast<ConstantArrayType>(AT)->getSize().getZExtValue(); |
| 2744 | StructuredList->resizeInits(Context, StrLen); |
| 2745 | |
| 2746 | // Build a literal for each character in the string, and put them into |
| 2747 | // the init list. |
| 2748 | for (unsigned i = 0, e = StrLen; i != e; ++i) { |
| 2749 | llvm::APInt CodeUnit(PromotedCharTyWidth, Str[i]); |
| 2750 | Expr *Init = new (Context) IntegerLiteral( |
Eli Friedman | 6cc05f7 | 2013-06-11 22:26:34 +0000 | [diff] [blame] | 2751 | Context, CodeUnit, PromotedCharTy, SubExpr->getExprLoc()); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2752 | if (CharTy != PromotedCharTy) |
| 2753 | Init = ImplicitCastExpr::Create(Context, CharTy, CK_IntegralCast, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2754 | Init, nullptr, VK_RValue); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2755 | StructuredList->updateInit(Context, i, Init); |
| 2756 | } |
| 2757 | } |
| 2758 | } |
| 2759 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2760 | // Make sure that our non-designated initializer list has space |
| 2761 | // for a subobject corresponding to this array element. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2762 | if (!VerifyOnly && |
| 2763 | DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2764 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2765 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2766 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2767 | // Repeatedly perform subobject initializations in the range |
| 2768 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2769 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2770 | // Move to the next designator |
| 2771 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2772 | unsigned OldIndex = Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2773 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2774 | InitializedEntity ElementEntity = |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2775 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2776 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2777 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 2778 | // Recurse to check later designated subobjects. |
| 2779 | QualType ElementType = AT->getElementType(); |
| 2780 | Index = OldIndex; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2781 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2782 | ElementEntity.setElementIndex(ElementIndex); |
Alexey Bataev | 86a489e | 2016-01-25 05:14:03 +0000 | [diff] [blame] | 2783 | if (CheckDesignatedInitializer( |
| 2784 | ElementEntity, IList, DIE, DesigIdx + 1, ElementType, nullptr, |
| 2785 | nullptr, Index, StructuredList, ElementIndex, |
| 2786 | FinishSubobjectInit && (DesignatedStartIndex == DesignatedEndIndex), |
| 2787 | false)) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2788 | return true; |
| 2789 | |
| 2790 | // Move to the next index in the array that we'll be initializing. |
| 2791 | ++DesignatedStartIndex; |
| 2792 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2793 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2794 | |
| 2795 | // If this the first designator, our caller will continue checking |
| 2796 | // the rest of this array subobject. |
| 2797 | if (IsFirstDesignator) { |
| 2798 | if (NextElementIndex) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2799 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2800 | StructuredIndex = ElementIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2801 | return false; |
| 2802 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2803 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2804 | if (!FinishSubobjectInit) |
| 2805 | return false; |
| 2806 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2807 | // Check the remaining elements within this array subobject. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2808 | bool prevHadError = hadError; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2809 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 2810 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2811 | StructuredList, ElementIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2812 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2813 | } |
| 2814 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2815 | // Get the structured initializer list for a subobject of type |
| 2816 | // @p CurrentObjectType. |
| 2817 | InitListExpr * |
| 2818 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 2819 | QualType CurrentObjectType, |
| 2820 | InitListExpr *StructuredList, |
| 2821 | unsigned StructuredIndex, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2822 | SourceRange InitRange, |
| 2823 | bool IsFullyOverwritten) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2824 | if (VerifyOnly) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2825 | return nullptr; // No structured list in verification-only mode. |
| 2826 | Expr *ExistingInit = nullptr; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2827 | if (!StructuredList) |
Benjamin Kramer | 6b441d6 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 2828 | ExistingInit = SyntacticToSemantic.lookup(IList); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2829 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 2830 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2831 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2832 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2833 | // There might have already been initializers for subobjects of the current |
| 2834 | // object, but a subsequent initializer list will overwrite the entirety |
| 2835 | // of the current object. (See DR 253 and C99 6.7.8p21). e.g., |
| 2836 | // |
| 2837 | // struct P { char x[6]; }; |
| 2838 | // struct P l = { .x[2] = 'x', .x = { [0] = 'f' } }; |
| 2839 | // |
| 2840 | // The first designated initializer is ignored, and l.x is just "f". |
| 2841 | if (!IsFullyOverwritten) |
| 2842 | return Result; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2843 | |
| 2844 | if (ExistingInit) { |
| 2845 | // We are creating an initializer list that initializes the |
| 2846 | // subobjects of the current object, but there was already an |
| 2847 | // initialization that completely initialized the current |
| 2848 | // subobject, e.g., by a compound literal: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2849 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2850 | // struct X { int a, b; }; |
| 2851 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2852 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2853 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 2854 | // designated initializer re-initializes the whole |
| 2855 | // subobject [0], overwriting previous initializers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2856 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2857 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2858 | << InitRange; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2859 | SemaRef.Diag(ExistingInit->getBeginLoc(), diag::note_previous_initializer) |
| 2860 | << /*FIXME:has side effects=*/0 << ExistingInit->getSourceRange(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2861 | } |
| 2862 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2863 | InitListExpr *Result |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2864 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
Dmitri Gribenko | 78852e9 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 2865 | InitRange.getBegin(), None, |
Ted Kremenek | 013041e | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 2866 | InitRange.getEnd()); |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2867 | |
Eli Friedman | 91f5ae5 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 2868 | QualType ResultType = CurrentObjectType; |
| 2869 | if (!ResultType->isArrayType()) |
| 2870 | ResultType = ResultType.getNonLValueExprType(SemaRef.Context); |
| 2871 | Result->setType(ResultType); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2872 | |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2873 | // Pre-allocate storage for the structured initializer list. |
| 2874 | unsigned NumElements = 0; |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2875 | unsigned NumInits = 0; |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2876 | bool GotNumInits = false; |
| 2877 | if (!StructuredList) { |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2878 | NumInits = IList->getNumInits(); |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2879 | GotNumInits = true; |
| 2880 | } else if (Index < IList->getNumInits()) { |
| 2881 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) { |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2882 | NumInits = SubList->getNumInits(); |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2883 | GotNumInits = true; |
| 2884 | } |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2885 | } |
| 2886 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2887 | if (const ArrayType *AType |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2888 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 2889 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 2890 | NumElements = CAType->getSize().getZExtValue(); |
| 2891 | // Simple heuristic so that we don't allocate a very large |
| 2892 | // initializer with many empty entries at the end. |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2893 | if (GotNumInits && NumElements > NumInits) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2894 | NumElements = 0; |
| 2895 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2896 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2897 | NumElements = VType->getNumElements(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2898 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2899 | RecordDecl *RDecl = RType->getDecl(); |
| 2900 | if (RDecl->isUnion()) |
| 2901 | NumElements = 1; |
| 2902 | else |
Aaron Ballman | 62e47c4 | 2014-03-10 13:43:55 +0000 | [diff] [blame] | 2903 | NumElements = std::distance(RDecl->field_begin(), RDecl->field_end()); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2904 | } |
| 2905 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2906 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2907 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2908 | // Link this new initializer list into the structured initializer |
| 2909 | // lists. |
| 2910 | if (StructuredList) |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2911 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2912 | else { |
| 2913 | Result->setSyntacticForm(IList); |
| 2914 | SyntacticToSemantic[IList] = Result; |
| 2915 | } |
| 2916 | |
| 2917 | return Result; |
| 2918 | } |
| 2919 | |
| 2920 | /// Update the initializer at index @p StructuredIndex within the |
| 2921 | /// structured initializer list to the value @p expr. |
| 2922 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 2923 | unsigned &StructuredIndex, |
| 2924 | Expr *expr) { |
| 2925 | // No structured initializer list to update |
| 2926 | if (!StructuredList) |
| 2927 | return; |
| 2928 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2929 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 2930 | StructuredIndex, expr)) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2931 | // This initializer overwrites a previous initializer. Warn. |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2932 | // We need to check on source range validity because the previous |
| 2933 | // initializer does not have to be an explicit initializer. |
| 2934 | // struct P { int a, b; }; |
| 2935 | // struct PP { struct P p } l = { { .a = 2 }, .p.b = 3 }; |
| 2936 | // There is an overwrite taking place because the first braced initializer |
| 2937 | // list "{ .a = 2 }' already provides value for .p.b (which is zero). |
| 2938 | if (PrevInit->getSourceRange().isValid()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2939 | SemaRef.Diag(expr->getBeginLoc(), diag::warn_initializer_overrides) |
| 2940 | << expr->getSourceRange(); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2941 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2942 | SemaRef.Diag(PrevInit->getBeginLoc(), diag::note_previous_initializer) |
| 2943 | << /*FIXME:has side effects=*/0 << PrevInit->getSourceRange(); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2944 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2945 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2946 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2947 | ++StructuredIndex; |
| 2948 | } |
| 2949 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2950 | /// Check that the given Index expression is a valid array designator |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2951 | /// value. This is essentially just a wrapper around |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2952 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2953 | /// and produces a reasonable diagnostic if there is a |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2954 | /// failure. Returns the index expression, possibly with an implicit cast |
| 2955 | /// added, on success. If everything went okay, Value will receive the |
| 2956 | /// value of the constant expression. |
| 2957 | static ExprResult |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2958 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2959 | SourceLocation Loc = Index->getBeginLoc(); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2960 | |
| 2961 | // Make sure this is an integer constant expression. |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2962 | ExprResult Result = S.VerifyIntegerConstantExpression(Index, &Value); |
| 2963 | if (Result.isInvalid()) |
| 2964 | return Result; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2965 | |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2966 | if (Value.isSigned() && Value.isNegative()) |
| 2967 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2968 | << Value.toString(10) << Index->getSourceRange(); |
| 2969 | |
Douglas Gregor | 51650d3 | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 2970 | Value.setIsUnsigned(true); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2971 | return Result; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2972 | } |
| 2973 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2974 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 2975 | SourceLocation Loc, |
| 2976 | bool GNUSyntax, |
| 2977 | ExprResult Init) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2978 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 2979 | |
| 2980 | bool Invalid = false; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2981 | SmallVector<ASTDesignator, 32> Designators; |
| 2982 | SmallVector<Expr *, 32> InitExpressions; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2983 | |
| 2984 | // Build designators and check array designator expressions. |
| 2985 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 2986 | const Designator &D = Desig.getDesignator(Idx); |
| 2987 | switch (D.getKind()) { |
| 2988 | case Designator::FieldDesignator: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2989 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2990 | D.getFieldLoc())); |
| 2991 | break; |
| 2992 | |
| 2993 | case Designator::ArrayDesignator: { |
| 2994 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 2995 | llvm::APSInt IndexValue; |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2996 | if (!Index->isTypeDependent() && !Index->isValueDependent()) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2997 | Index = CheckArrayDesignatorExpr(*this, Index, IndexValue).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2998 | if (!Index) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2999 | Invalid = true; |
| 3000 | else { |
| 3001 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3002 | D.getLBracketLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3003 | D.getRBracketLoc())); |
| 3004 | InitExpressions.push_back(Index); |
| 3005 | } |
| 3006 | break; |
| 3007 | } |
| 3008 | |
| 3009 | case Designator::ArrayRangeDesignator: { |
| 3010 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 3011 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 3012 | llvm::APSInt StartValue; |
| 3013 | llvm::APSInt EndValue; |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 3014 | bool StartDependent = StartIndex->isTypeDependent() || |
| 3015 | StartIndex->isValueDependent(); |
| 3016 | bool EndDependent = EndIndex->isTypeDependent() || |
| 3017 | EndIndex->isValueDependent(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3018 | if (!StartDependent) |
| 3019 | StartIndex = |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3020 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3021 | if (!EndDependent) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3022 | EndIndex = CheckArrayDesignatorExpr(*this, EndIndex, EndValue).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3023 | |
| 3024 | if (!StartIndex || !EndIndex) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3025 | Invalid = true; |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 3026 | else { |
| 3027 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 3028 | if (StartDependent || EndDependent) { |
| 3029 | // Nothing to compute. |
| 3030 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3031 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 3032 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3033 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 3034 | |
Douglas Gregor | 0f9d400 | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 3035 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 3036 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3037 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 3038 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 3039 | Invalid = true; |
| 3040 | } else { |
| 3041 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3042 | D.getLBracketLoc(), |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 3043 | D.getEllipsisLoc(), |
| 3044 | D.getRBracketLoc())); |
| 3045 | InitExpressions.push_back(StartIndex); |
| 3046 | InitExpressions.push_back(EndIndex); |
| 3047 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3048 | } |
| 3049 | break; |
| 3050 | } |
| 3051 | } |
| 3052 | } |
| 3053 | |
| 3054 | if (Invalid || Init.isInvalid()) |
| 3055 | return ExprError(); |
| 3056 | |
| 3057 | // Clear out the expressions within the designation. |
| 3058 | Desig.ClearExprs(*this); |
| 3059 | |
| 3060 | DesignatedInitExpr *DIE |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 3061 | = DesignatedInitExpr::Create(Context, |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 3062 | Designators, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3063 | InitExpressions, Loc, GNUSyntax, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3064 | Init.getAs<Expr>()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3065 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3066 | if (!getLangOpts().C99) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3067 | Diag(DIE->getBeginLoc(), diag::ext_designated_init) |
| 3068 | << DIE->getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3069 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3070 | return DIE; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3071 | } |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 3072 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3073 | //===----------------------------------------------------------------------===// |
| 3074 | // Initialization entity |
| 3075 | //===----------------------------------------------------------------------===// |
| 3076 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3077 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 3078 | const InitializedEntity &Parent) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3079 | : Parent(&Parent), Index(Index) |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 3080 | { |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3081 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 3082 | Kind = EK_ArrayElement; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3083 | Type = AT->getElementType(); |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3084 | } else if (const VectorType *VT = Parent.getType()->getAs<VectorType>()) { |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3085 | Kind = EK_VectorElement; |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3086 | Type = VT->getElementType(); |
| 3087 | } else { |
| 3088 | const ComplexType *CT = Parent.getType()->getAs<ComplexType>(); |
| 3089 | assert(CT && "Unexpected type"); |
| 3090 | Kind = EK_ComplexElement; |
| 3091 | Type = CT->getElementType(); |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3092 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3093 | } |
| 3094 | |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 3095 | InitializedEntity |
| 3096 | InitializedEntity::InitializeBase(ASTContext &Context, |
| 3097 | const CXXBaseSpecifier *Base, |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 3098 | bool IsInheritedVirtualBase, |
| 3099 | const InitializedEntity *Parent) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3100 | InitializedEntity Result; |
| 3101 | Result.Kind = EK_Base; |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 3102 | Result.Parent = Parent; |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 3103 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 3104 | if (IsInheritedVirtualBase) |
| 3105 | Result.Base |= 0x01; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3106 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3107 | Result.Type = Base->getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3108 | return Result; |
| 3109 | } |
| 3110 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3111 | DeclarationName InitializedEntity::getName() const { |
| 3112 | switch (getKind()) { |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3113 | case EK_Parameter: |
| 3114 | case EK_Parameter_CF_Audited: { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3115 | ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 3116 | return (D ? D->getDeclName() : DeclarationName()); |
| 3117 | } |
Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3118 | |
| 3119 | case EK_Variable: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3120 | case EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3121 | case EK_Binding: |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3122 | return Variable.VariableOrMember->getDeclName(); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3123 | |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 3124 | case EK_LambdaCapture: |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 3125 | return DeclarationName(Capture.VarID); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3126 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3127 | case EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 3128 | case EK_StmtExprResult: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3129 | case EK_Exception: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3130 | case EK_New: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3131 | case EK_Temporary: |
| 3132 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3133 | case EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3134 | case EK_ArrayElement: |
| 3135 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3136 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3137 | case EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3138 | case EK_LambdaToBlockConversionBlockElement: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 3139 | case EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 3140 | case EK_RelatedResult: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3141 | return DeclarationName(); |
| 3142 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3143 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3144 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3145 | } |
| 3146 | |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3147 | ValueDecl *InitializedEntity::getDecl() const { |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3148 | switch (getKind()) { |
| 3149 | case EK_Variable: |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3150 | case EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3151 | case EK_Binding: |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3152 | return Variable.VariableOrMember; |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3153 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3154 | case EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3155 | case EK_Parameter_CF_Audited: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3156 | return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 3157 | |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3158 | case EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 3159 | case EK_StmtExprResult: |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3160 | case EK_Exception: |
| 3161 | case EK_New: |
| 3162 | case EK_Temporary: |
| 3163 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3164 | case EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3165 | case EK_ArrayElement: |
| 3166 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3167 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3168 | case EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3169 | case EK_LambdaToBlockConversionBlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 3170 | case EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 3171 | case EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 3172 | case EK_RelatedResult: |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3173 | return nullptr; |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3174 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3175 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3176 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3177 | } |
| 3178 | |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3179 | bool InitializedEntity::allowsNRVO() const { |
| 3180 | switch (getKind()) { |
| 3181 | case EK_Result: |
| 3182 | case EK_Exception: |
| 3183 | return LocAndNRVO.NRVO; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3184 | |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 3185 | case EK_StmtExprResult: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3186 | case EK_Variable: |
| 3187 | case EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3188 | case EK_Parameter_CF_Audited: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3189 | case EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3190 | case EK_Binding: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3191 | case EK_New: |
| 3192 | case EK_Temporary: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 3193 | case EK_CompoundLiteralInit: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3194 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3195 | case EK_Delegating: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3196 | case EK_ArrayElement: |
| 3197 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3198 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3199 | case EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3200 | case EK_LambdaToBlockConversionBlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 3201 | case EK_LambdaCapture: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 3202 | case EK_RelatedResult: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3203 | break; |
| 3204 | } |
| 3205 | |
| 3206 | return false; |
| 3207 | } |
| 3208 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3209 | unsigned InitializedEntity::dumpImpl(raw_ostream &OS) const { |
Richard Smith | e3b28bc | 2013-06-12 21:51:50 +0000 | [diff] [blame] | 3210 | assert(getParent() != this); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3211 | unsigned Depth = getParent() ? getParent()->dumpImpl(OS) : 0; |
| 3212 | for (unsigned I = 0; I != Depth; ++I) |
| 3213 | OS << "`-"; |
| 3214 | |
| 3215 | switch (getKind()) { |
| 3216 | case EK_Variable: OS << "Variable"; break; |
| 3217 | case EK_Parameter: OS << "Parameter"; break; |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3218 | case EK_Parameter_CF_Audited: OS << "CF audited function Parameter"; |
| 3219 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3220 | case EK_Result: OS << "Result"; break; |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 3221 | case EK_StmtExprResult: OS << "StmtExprResult"; break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3222 | case EK_Exception: OS << "Exception"; break; |
| 3223 | case EK_Member: OS << "Member"; break; |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3224 | case EK_Binding: OS << "Binding"; break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3225 | case EK_New: OS << "New"; break; |
| 3226 | case EK_Temporary: OS << "Temporary"; break; |
| 3227 | case EK_CompoundLiteralInit: OS << "CompoundLiteral";break; |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 3228 | case EK_RelatedResult: OS << "RelatedResult"; break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3229 | case EK_Base: OS << "Base"; break; |
| 3230 | case EK_Delegating: OS << "Delegating"; break; |
| 3231 | case EK_ArrayElement: OS << "ArrayElement " << Index; break; |
| 3232 | case EK_VectorElement: OS << "VectorElement " << Index; break; |
| 3233 | case EK_ComplexElement: OS << "ComplexElement " << Index; break; |
| 3234 | case EK_BlockElement: OS << "Block"; break; |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3235 | case EK_LambdaToBlockConversionBlockElement: |
| 3236 | OS << "Block (lambda)"; |
| 3237 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3238 | case EK_LambdaCapture: |
| 3239 | OS << "LambdaCapture "; |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 3240 | OS << DeclarationName(Capture.VarID); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3241 | break; |
| 3242 | } |
| 3243 | |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3244 | if (auto *D = getDecl()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3245 | OS << " "; |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3246 | D->printQualifiedName(OS); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3247 | } |
| 3248 | |
| 3249 | OS << " '" << getType().getAsString() << "'\n"; |
| 3250 | |
| 3251 | return Depth + 1; |
| 3252 | } |
| 3253 | |
Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 3254 | LLVM_DUMP_METHOD void InitializedEntity::dump() const { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3255 | dumpImpl(llvm::errs()); |
| 3256 | } |
| 3257 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3258 | //===----------------------------------------------------------------------===// |
| 3259 | // Initialization sequence |
| 3260 | //===----------------------------------------------------------------------===// |
| 3261 | |
| 3262 | void InitializationSequence::Step::Destroy() { |
| 3263 | switch (Kind) { |
| 3264 | case SK_ResolveAddressOfOverloadedFunction: |
| 3265 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3266 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3267 | case SK_CastDerivedToBaseLValue: |
| 3268 | case SK_BindReference: |
| 3269 | case SK_BindReferenceToTemporary: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 3270 | case SK_FinalCopy: |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3271 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3272 | case SK_UserConversion: |
| 3273 | case SK_QualificationConversionRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3274 | case SK_QualificationConversionXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3275 | case SK_QualificationConversionLValue: |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 3276 | case SK_AtomicConversion: |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3277 | case SK_LValueToRValue: |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3278 | case SK_ListInitialization: |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3279 | case SK_UnwrapInitList: |
| 3280 | case SK_RewrapInitList: |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3281 | case SK_ConstructorInitialization: |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 3282 | case SK_ConstructorInitializationFromList: |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3283 | case SK_ZeroInitialization: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3284 | case SK_CAssignment: |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3285 | case SK_StringInit: |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3286 | case SK_ObjCObjectConversion: |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3287 | case SK_ArrayLoopIndex: |
| 3288 | case SK_ArrayLoopInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3289 | case SK_ArrayInit: |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 3290 | case SK_GNUArrayInit: |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 3291 | case SK_ParenthesizedArrayInit: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3292 | case SK_PassByIndirectCopyRestore: |
| 3293 | case SK_PassByIndirectRestore: |
| 3294 | case SK_ProduceObjCObject: |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 3295 | case SK_StdInitializerList: |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3296 | case SK_StdInitializerListConstructorCall: |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 3297 | case SK_OCLSamplerInit: |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 3298 | case SK_OCLZeroOpaqueType: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3299 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3300 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3301 | case SK_ConversionSequence: |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 3302 | case SK_ConversionSequenceNoNarrowing: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3303 | delete ICS; |
| 3304 | } |
| 3305 | } |
| 3306 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3307 | bool InitializationSequence::isDirectReferenceBinding() const { |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 3308 | // There can be some lvalue adjustments after the SK_BindReference step. |
| 3309 | for (auto I = Steps.rbegin(); I != Steps.rend(); ++I) { |
| 3310 | if (I->Kind == SK_BindReference) |
| 3311 | return true; |
| 3312 | if (I->Kind == SK_BindReferenceToTemporary) |
| 3313 | return false; |
| 3314 | } |
| 3315 | return false; |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3316 | } |
| 3317 | |
| 3318 | bool InitializationSequence::isAmbiguous() const { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 3319 | if (!Failed()) |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3320 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3321 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3322 | switch (getFailureKind()) { |
| 3323 | case FK_TooManyInitsForReference: |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 3324 | case FK_ParenthesizedListInitForReference: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3325 | case FK_ArrayNeedsInitList: |
| 3326 | case FK_ArrayNeedsInitListOrStringLiteral: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 3327 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 3328 | case FK_NarrowStringIntoWideCharArray: |
| 3329 | case FK_WideStringIntoCharArray: |
| 3330 | case FK_IncompatWideStringIntoWideChar: |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 3331 | case FK_PlainStringIntoUTF8Char: |
| 3332 | case FK_UTF8StringIntoPlainChar: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3333 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 3334 | case FK_NonConstLValueReferenceBindingToTemporary: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 3335 | case FK_NonConstLValueReferenceBindingToBitfield: |
| 3336 | case FK_NonConstLValueReferenceBindingToVectorElement: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3337 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 3338 | case FK_RValueReferenceBindingToLValue: |
| 3339 | case FK_ReferenceInitDropsQualifiers: |
| 3340 | case FK_ReferenceInitFailed: |
| 3341 | case FK_ConversionFailed: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3342 | case FK_ConversionFromPropertyFailed: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3343 | case FK_TooManyInitsForScalar: |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 3344 | case FK_ParenthesizedListInitForScalar: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3345 | case FK_ReferenceBindingToInitList: |
| 3346 | case FK_InitListBadDestinationType: |
| 3347 | case FK_DefaultInitOfConst: |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 3348 | case FK_Incomplete: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3349 | case FK_ArrayTypeMismatch: |
| 3350 | case FK_NonConstantArrayInit: |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 3351 | case FK_ListInitializationFailed: |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 3352 | case FK_VariableLengthArrayHasInitializer: |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3353 | case FK_PlaceholderType: |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3354 | case FK_ExplicitConstructor: |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 3355 | case FK_AddressOfUnaddressableFunction: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3356 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3357 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3358 | case FK_ReferenceInitOverloadFailed: |
| 3359 | case FK_UserConversionOverloadFailed: |
| 3360 | case FK_ConstructorOverloadFailed: |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3361 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3362 | return FailedOverloadResult == OR_Ambiguous; |
| 3363 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3364 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3365 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3366 | } |
| 3367 | |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 3368 | bool InitializationSequence::isConstructorInitialization() const { |
| 3369 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 3370 | } |
| 3371 | |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3372 | void |
| 3373 | InitializationSequence |
| 3374 | ::AddAddressOverloadResolutionStep(FunctionDecl *Function, |
| 3375 | DeclAccessPair Found, |
| 3376 | bool HadMultipleCandidates) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3377 | Step S; |
| 3378 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 3379 | S.Type = Function->getType(); |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3380 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3381 | S.Function.Function = Function; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3382 | S.Function.FoundDecl = Found; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3383 | Steps.push_back(S); |
| 3384 | } |
| 3385 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3386 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3387 | ExprValueKind VK) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3388 | Step S; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3389 | switch (VK) { |
| 3390 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 3391 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 3392 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3393 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3394 | S.Type = BaseType; |
| 3395 | Steps.push_back(S); |
| 3396 | } |
| 3397 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3398 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3399 | bool BindingTemporary) { |
| 3400 | Step S; |
| 3401 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 3402 | S.Type = T; |
| 3403 | Steps.push_back(S); |
| 3404 | } |
| 3405 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 3406 | void InitializationSequence::AddFinalCopy(QualType T) { |
| 3407 | Step S; |
| 3408 | S.Kind = SK_FinalCopy; |
| 3409 | S.Type = T; |
| 3410 | Steps.push_back(S); |
| 3411 | } |
| 3412 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3413 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 3414 | Step S; |
| 3415 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 3416 | S.Type = T; |
| 3417 | Steps.push_back(S); |
| 3418 | } |
| 3419 | |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3420 | void |
| 3421 | InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
| 3422 | DeclAccessPair FoundDecl, |
| 3423 | QualType T, |
| 3424 | bool HadMultipleCandidates) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3425 | Step S; |
| 3426 | S.Kind = SK_UserConversion; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3427 | S.Type = T; |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3428 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3429 | S.Function.Function = Function; |
| 3430 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3431 | Steps.push_back(S); |
| 3432 | } |
| 3433 | |
| 3434 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3435 | ExprValueKind VK) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3436 | Step S; |
John McCall | 7a1da89 | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 3437 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3438 | switch (VK) { |
| 3439 | case VK_RValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3440 | S.Kind = SK_QualificationConversionRValue; |
| 3441 | break; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3442 | case VK_XValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3443 | S.Kind = SK_QualificationConversionXValue; |
| 3444 | break; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3445 | case VK_LValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3446 | S.Kind = SK_QualificationConversionLValue; |
| 3447 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3448 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3449 | S.Type = Ty; |
| 3450 | Steps.push_back(S); |
| 3451 | } |
| 3452 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 3453 | void InitializationSequence::AddAtomicConversionStep(QualType Ty) { |
| 3454 | Step S; |
| 3455 | S.Kind = SK_AtomicConversion; |
| 3456 | S.Type = Ty; |
| 3457 | Steps.push_back(S); |
| 3458 | } |
| 3459 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3460 | void InitializationSequence::AddLValueToRValueStep(QualType Ty) { |
| 3461 | assert(!Ty.hasQualifiers() && "rvalues may not have qualifiers"); |
| 3462 | |
| 3463 | Step S; |
| 3464 | S.Kind = SK_LValueToRValue; |
| 3465 | S.Type = Ty; |
| 3466 | Steps.push_back(S); |
| 3467 | } |
| 3468 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3469 | void InitializationSequence::AddConversionSequenceStep( |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 3470 | const ImplicitConversionSequence &ICS, QualType T, |
| 3471 | bool TopLevelOfInitList) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3472 | Step S; |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 3473 | S.Kind = TopLevelOfInitList ? SK_ConversionSequenceNoNarrowing |
| 3474 | : SK_ConversionSequence; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3475 | S.Type = T; |
| 3476 | S.ICS = new ImplicitConversionSequence(ICS); |
| 3477 | Steps.push_back(S); |
| 3478 | } |
| 3479 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3480 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 3481 | Step S; |
| 3482 | S.Kind = SK_ListInitialization; |
| 3483 | S.Type = T; |
| 3484 | Steps.push_back(S); |
| 3485 | } |
| 3486 | |
Richard Smith | 55c2888 | 2016-05-12 23:45:49 +0000 | [diff] [blame] | 3487 | void InitializationSequence::AddConstructorInitializationStep( |
| 3488 | DeclAccessPair FoundDecl, CXXConstructorDecl *Constructor, QualType T, |
| 3489 | bool HadMultipleCandidates, bool FromInitList, bool AsInitList) { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3490 | Step S; |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3491 | S.Kind = FromInitList ? AsInitList ? SK_StdInitializerListConstructorCall |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 3492 | : SK_ConstructorInitializationFromList |
| 3493 | : SK_ConstructorInitialization; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3494 | S.Type = T; |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3495 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3496 | S.Function.Function = Constructor; |
Richard Smith | 55c2888 | 2016-05-12 23:45:49 +0000 | [diff] [blame] | 3497 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3498 | Steps.push_back(S); |
| 3499 | } |
| 3500 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3501 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 3502 | Step S; |
| 3503 | S.Kind = SK_ZeroInitialization; |
| 3504 | S.Type = T; |
| 3505 | Steps.push_back(S); |
| 3506 | } |
| 3507 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3508 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 3509 | Step S; |
| 3510 | S.Kind = SK_CAssignment; |
| 3511 | S.Type = T; |
| 3512 | Steps.push_back(S); |
| 3513 | } |
| 3514 | |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3515 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 3516 | Step S; |
| 3517 | S.Kind = SK_StringInit; |
| 3518 | S.Type = T; |
| 3519 | Steps.push_back(S); |
| 3520 | } |
| 3521 | |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3522 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 3523 | Step S; |
| 3524 | S.Kind = SK_ObjCObjectConversion; |
| 3525 | S.Type = T; |
| 3526 | Steps.push_back(S); |
| 3527 | } |
| 3528 | |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 3529 | void InitializationSequence::AddArrayInitStep(QualType T, bool IsGNUExtension) { |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3530 | Step S; |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 3531 | S.Kind = IsGNUExtension ? SK_GNUArrayInit : SK_ArrayInit; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3532 | S.Type = T; |
| 3533 | Steps.push_back(S); |
| 3534 | } |
| 3535 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3536 | void InitializationSequence::AddArrayInitLoopStep(QualType T, QualType EltT) { |
| 3537 | Step S; |
| 3538 | S.Kind = SK_ArrayLoopIndex; |
| 3539 | S.Type = EltT; |
| 3540 | Steps.insert(Steps.begin(), S); |
| 3541 | |
| 3542 | S.Kind = SK_ArrayLoopInit; |
| 3543 | S.Type = T; |
| 3544 | Steps.push_back(S); |
| 3545 | } |
| 3546 | |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 3547 | void InitializationSequence::AddParenthesizedArrayInitStep(QualType T) { |
| 3548 | Step S; |
| 3549 | S.Kind = SK_ParenthesizedArrayInit; |
| 3550 | S.Type = T; |
| 3551 | Steps.push_back(S); |
| 3552 | } |
| 3553 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3554 | void InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type, |
| 3555 | bool shouldCopy) { |
| 3556 | Step s; |
| 3557 | s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore |
| 3558 | : SK_PassByIndirectRestore); |
| 3559 | s.Type = type; |
| 3560 | Steps.push_back(s); |
| 3561 | } |
| 3562 | |
| 3563 | void InitializationSequence::AddProduceObjCObjectStep(QualType T) { |
| 3564 | Step S; |
| 3565 | S.Kind = SK_ProduceObjCObject; |
| 3566 | S.Type = T; |
| 3567 | Steps.push_back(S); |
| 3568 | } |
| 3569 | |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 3570 | void InitializationSequence::AddStdInitializerListConstructionStep(QualType T) { |
| 3571 | Step S; |
| 3572 | S.Kind = SK_StdInitializerList; |
| 3573 | S.Type = T; |
| 3574 | Steps.push_back(S); |
| 3575 | } |
| 3576 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 3577 | void InitializationSequence::AddOCLSamplerInitStep(QualType T) { |
| 3578 | Step S; |
| 3579 | S.Kind = SK_OCLSamplerInit; |
| 3580 | S.Type = T; |
| 3581 | Steps.push_back(S); |
| 3582 | } |
| 3583 | |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 3584 | void InitializationSequence::AddOCLZeroOpaqueTypeStep(QualType T) { |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 3585 | Step S; |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 3586 | S.Kind = SK_OCLZeroOpaqueType; |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 3587 | S.Type = T; |
| 3588 | Steps.push_back(S); |
| 3589 | } |
| 3590 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3591 | void InitializationSequence::RewrapReferenceInitList(QualType T, |
| 3592 | InitListExpr *Syntactic) { |
| 3593 | assert(Syntactic->getNumInits() == 1 && |
| 3594 | "Can only rewrap trivial init lists."); |
| 3595 | Step S; |
| 3596 | S.Kind = SK_UnwrapInitList; |
| 3597 | S.Type = Syntactic->getInit(0)->getType(); |
| 3598 | Steps.insert(Steps.begin(), S); |
| 3599 | |
| 3600 | S.Kind = SK_RewrapInitList; |
| 3601 | S.Type = T; |
| 3602 | S.WrappingSyntacticList = Syntactic; |
| 3603 | Steps.push_back(S); |
| 3604 | } |
| 3605 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3606 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3607 | OverloadingResult Result) { |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 3608 | setSequenceKind(FailedSequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3609 | this->Failure = Failure; |
| 3610 | this->FailedOverloadResult = Result; |
| 3611 | } |
| 3612 | |
| 3613 | //===----------------------------------------------------------------------===// |
| 3614 | // Attempt initialization |
| 3615 | //===----------------------------------------------------------------------===// |
| 3616 | |
Nico Weber | 337d5aa | 2015-04-17 08:32:38 +0000 | [diff] [blame] | 3617 | /// Tries to add a zero initializer. Returns true if that worked. |
| 3618 | static bool |
| 3619 | maybeRecoverWithZeroInitialization(Sema &S, InitializationSequence &Sequence, |
| 3620 | const InitializedEntity &Entity) { |
| 3621 | if (Entity.getKind() != InitializedEntity::EK_Variable) |
| 3622 | return false; |
| 3623 | |
| 3624 | VarDecl *VD = cast<VarDecl>(Entity.getDecl()); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 3625 | if (VD->getInit() || VD->getEndLoc().isMacroID()) |
Nico Weber | 337d5aa | 2015-04-17 08:32:38 +0000 | [diff] [blame] | 3626 | return false; |
| 3627 | |
| 3628 | QualType VariableTy = VD->getType().getCanonicalType(); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 3629 | SourceLocation Loc = S.getLocForEndOfToken(VD->getEndLoc()); |
Nico Weber | 337d5aa | 2015-04-17 08:32:38 +0000 | [diff] [blame] | 3630 | std::string Init = S.getFixItZeroInitializerForType(VariableTy, Loc); |
| 3631 | if (!Init.empty()) { |
| 3632 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3633 | Sequence.SetZeroInitializationFixit(Init, Loc); |
| 3634 | return true; |
| 3635 | } |
| 3636 | return false; |
| 3637 | } |
| 3638 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3639 | static void MaybeProduceObjCObject(Sema &S, |
| 3640 | InitializationSequence &Sequence, |
| 3641 | const InitializedEntity &Entity) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3642 | if (!S.getLangOpts().ObjCAutoRefCount) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3643 | |
| 3644 | /// When initializing a parameter, produce the value if it's marked |
| 3645 | /// __attribute__((ns_consumed)). |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3646 | if (Entity.isParameterKind()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3647 | if (!Entity.isParameterConsumed()) |
| 3648 | return; |
| 3649 | |
| 3650 | assert(Entity.getType()->isObjCRetainableType() && |
| 3651 | "consuming an object of unretainable type?"); |
| 3652 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 3653 | |
| 3654 | /// When initializing a return value, if the return type is a |
| 3655 | /// retainable type, then returns need to immediately retain the |
| 3656 | /// object. If an autorelease is required, it will be done at the |
| 3657 | /// last instant. |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 3658 | } else if (Entity.getKind() == InitializedEntity::EK_Result || |
| 3659 | Entity.getKind() == InitializedEntity::EK_StmtExprResult) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3660 | if (!Entity.getType()->isObjCRetainableType()) |
| 3661 | return; |
| 3662 | |
| 3663 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 3664 | } |
| 3665 | } |
| 3666 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3667 | static void TryListInitialization(Sema &S, |
| 3668 | const InitializedEntity &Entity, |
| 3669 | const InitializationKind &Kind, |
| 3670 | InitListExpr *InitList, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 3671 | InitializationSequence &Sequence, |
| 3672 | bool TreatUnavailableAsInvalid); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3673 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3674 | /// When initializing from init list via constructor, handle |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3675 | /// initialization of an object of type std::initializer_list<T>. |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3676 | /// |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3677 | /// \return true if we have handled initialization of an object of type |
| 3678 | /// std::initializer_list<T>, false otherwise. |
| 3679 | static bool TryInitializerListConstruction(Sema &S, |
| 3680 | InitListExpr *List, |
| 3681 | QualType DestType, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 3682 | InitializationSequence &Sequence, |
| 3683 | bool TreatUnavailableAsInvalid) { |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3684 | QualType E; |
| 3685 | if (!S.isStdInitializerList(DestType, &E)) |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3686 | return false; |
| 3687 | |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3688 | if (!S.isCompleteType(List->getExprLoc(), E)) { |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3689 | Sequence.setIncompleteTypeFailure(E); |
| 3690 | return true; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3691 | } |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3692 | |
| 3693 | // Try initializing a temporary array from the init list. |
| 3694 | QualType ArrayType = S.Context.getConstantArrayType( |
| 3695 | E.withConst(), llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 3696 | List->getNumInits()), |
| 3697 | clang::ArrayType::Normal, 0); |
| 3698 | InitializedEntity HiddenArray = |
| 3699 | InitializedEntity::InitializeTemporary(ArrayType); |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 3700 | InitializationKind Kind = InitializationKind::CreateDirectList( |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 3701 | List->getExprLoc(), List->getBeginLoc(), List->getEndLoc()); |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 3702 | TryListInitialization(S, HiddenArray, Kind, List, Sequence, |
| 3703 | TreatUnavailableAsInvalid); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3704 | if (Sequence) |
| 3705 | Sequence.AddStdInitializerListConstructionStep(DestType); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3706 | return true; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3707 | } |
| 3708 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3709 | /// Determine if the constructor has the signature of a copy or move |
| 3710 | /// constructor for the type T of the class in which it was found. That is, |
| 3711 | /// determine if its first parameter is of type T or reference to (possibly |
| 3712 | /// cv-qualified) T. |
| 3713 | static bool hasCopyOrMoveCtorParam(ASTContext &Ctx, |
| 3714 | const ConstructorInfo &Info) { |
| 3715 | if (Info.Constructor->getNumParams() == 0) |
| 3716 | return false; |
| 3717 | |
| 3718 | QualType ParmT = |
| 3719 | Info.Constructor->getParamDecl(0)->getType().getNonReferenceType(); |
| 3720 | QualType ClassT = |
| 3721 | Ctx.getRecordType(cast<CXXRecordDecl>(Info.FoundDecl->getDeclContext())); |
| 3722 | |
| 3723 | return Ctx.hasSameUnqualifiedType(ParmT, ClassT); |
| 3724 | } |
| 3725 | |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3726 | static OverloadingResult |
| 3727 | ResolveConstructorOverload(Sema &S, SourceLocation DeclLoc, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3728 | MultiExprArg Args, |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3729 | OverloadCandidateSet &CandidateSet, |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3730 | QualType DestType, |
Richard Smith | 40c7806 | 2015-02-21 02:31:57 +0000 | [diff] [blame] | 3731 | DeclContext::lookup_result Ctors, |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3732 | OverloadCandidateSet::iterator &Best, |
| 3733 | bool CopyInitializing, bool AllowExplicit, |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3734 | bool OnlyListConstructors, bool IsListInit, |
| 3735 | bool SecondStepOfCopyInit = false) { |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3736 | CandidateSet.clear(OverloadCandidateSet::CSK_InitByConstructor); |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3737 | |
Richard Smith | 40c7806 | 2015-02-21 02:31:57 +0000 | [diff] [blame] | 3738 | for (NamedDecl *D : Ctors) { |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3739 | auto Info = getConstructorInfo(D); |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3740 | if (!Info.Constructor || Info.Constructor->isInvalidDecl()) |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3741 | continue; |
| 3742 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3743 | if (!AllowExplicit && Info.Constructor->isExplicit()) |
| 3744 | continue; |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3745 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3746 | if (OnlyListConstructors && !S.isInitListConstructor(Info.Constructor)) |
| 3747 | continue; |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3748 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3749 | // C++11 [over.best.ics]p4: |
| 3750 | // ... and the constructor or user-defined conversion function is a |
| 3751 | // candidate by |
| 3752 | // - 13.3.1.3, when the argument is the temporary in the second step |
| 3753 | // of a class copy-initialization, or |
| 3754 | // - 13.3.1.4, 13.3.1.5, or 13.3.1.6 (in all cases), [not handled here] |
| 3755 | // - the second phase of 13.3.1.7 when the initializer list has exactly |
| 3756 | // one element that is itself an initializer list, and the target is |
| 3757 | // the first parameter of a constructor of class X, and the conversion |
| 3758 | // is to X or reference to (possibly cv-qualified X), |
| 3759 | // user-defined conversion sequences are not considered. |
| 3760 | bool SuppressUserConversions = |
| 3761 | SecondStepOfCopyInit || |
| 3762 | (IsListInit && Args.size() == 1 && isa<InitListExpr>(Args[0]) && |
| 3763 | hasCopyOrMoveCtorParam(S.Context, Info)); |
| 3764 | |
| 3765 | if (Info.ConstructorTmpl) |
| 3766 | S.AddTemplateOverloadCandidate(Info.ConstructorTmpl, Info.FoundDecl, |
| 3767 | /*ExplicitArgs*/ nullptr, Args, |
| 3768 | CandidateSet, SuppressUserConversions); |
| 3769 | else { |
| 3770 | // C++ [over.match.copy]p1: |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3771 | // - When initializing a temporary to be bound to the first parameter |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3772 | // of a constructor [for type T] that takes a reference to possibly |
| 3773 | // cv-qualified T as its first argument, called with a single |
| 3774 | // argument in the context of direct-initialization, explicit |
| 3775 | // conversion functions are also considered. |
| 3776 | // FIXME: What if a constructor template instantiates to such a signature? |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3777 | bool AllowExplicitConv = AllowExplicit && !CopyInitializing && |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3778 | Args.size() == 1 && |
| 3779 | hasCopyOrMoveCtorParam(S.Context, Info); |
| 3780 | S.AddOverloadCandidate(Info.Constructor, Info.FoundDecl, Args, |
| 3781 | CandidateSet, SuppressUserConversions, |
| 3782 | /*PartialOverloading=*/false, |
| 3783 | /*AllowExplicit=*/AllowExplicitConv); |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3784 | } |
| 3785 | } |
| 3786 | |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3787 | // FIXME: Work around a bug in C++17 guaranteed copy elision. |
| 3788 | // |
| 3789 | // When initializing an object of class type T by constructor |
| 3790 | // ([over.match.ctor]) or by list-initialization ([over.match.list]) |
| 3791 | // from a single expression of class type U, conversion functions of |
| 3792 | // U that convert to the non-reference type cv T are candidates. |
| 3793 | // Explicit conversion functions are only candidates during |
| 3794 | // direct-initialization. |
| 3795 | // |
| 3796 | // Note: SecondStepOfCopyInit is only ever true in this case when |
| 3797 | // evaluating whether to produce a C++98 compatibility warning. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 3798 | if (S.getLangOpts().CPlusPlus17 && Args.size() == 1 && |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3799 | !SecondStepOfCopyInit) { |
| 3800 | Expr *Initializer = Args[0]; |
| 3801 | auto *SourceRD = Initializer->getType()->getAsCXXRecordDecl(); |
| 3802 | if (SourceRD && S.isCompleteType(DeclLoc, Initializer->getType())) { |
| 3803 | const auto &Conversions = SourceRD->getVisibleConversionFunctions(); |
| 3804 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
| 3805 | NamedDecl *D = *I; |
| 3806 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3807 | D = D->getUnderlyingDecl(); |
| 3808 | |
| 3809 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3810 | CXXConversionDecl *Conv; |
| 3811 | if (ConvTemplate) |
| 3812 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3813 | else |
| 3814 | Conv = cast<CXXConversionDecl>(D); |
| 3815 | |
| 3816 | if ((AllowExplicit && !CopyInitializing) || !Conv->isExplicit()) { |
| 3817 | if (ConvTemplate) |
| 3818 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
| 3819 | ActingDC, Initializer, DestType, |
| 3820 | CandidateSet, AllowExplicit, |
| 3821 | /*AllowResultConversion*/false); |
| 3822 | else |
| 3823 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Initializer, |
| 3824 | DestType, CandidateSet, AllowExplicit, |
| 3825 | /*AllowResultConversion*/false); |
| 3826 | } |
| 3827 | } |
| 3828 | } |
| 3829 | } |
| 3830 | |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3831 | // Perform overload resolution and return the result. |
| 3832 | return CandidateSet.BestViableFunction(S, DeclLoc, Best); |
| 3833 | } |
| 3834 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3835 | /// Attempt initialization by constructor (C++ [dcl.init]), which |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3836 | /// enumerates the constructors of the initialized entity and performs overload |
| 3837 | /// resolution to select the best. |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3838 | /// \param DestType The destination class type. |
| 3839 | /// \param DestArrayType The destination type, which is either DestType or |
| 3840 | /// a (possibly multidimensional) array of DestType. |
NAKAMURA Takumi | ffcc98a | 2015-02-05 23:12:13 +0000 | [diff] [blame] | 3841 | /// \param IsListInit Is this list-initialization? |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3842 | /// \param IsInitListCopy Is this non-list-initialization resulting from a |
| 3843 | /// list-initialization from {x} where x is the same |
| 3844 | /// type as the entity? |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3845 | static void TryConstructorInitialization(Sema &S, |
| 3846 | const InitializedEntity &Entity, |
| 3847 | const InitializationKind &Kind, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3848 | MultiExprArg Args, QualType DestType, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3849 | QualType DestArrayType, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3850 | InitializationSequence &Sequence, |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3851 | bool IsListInit = false, |
| 3852 | bool IsInitListCopy = false) { |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3853 | assert(((!IsListInit && !IsInitListCopy) || |
| 3854 | (Args.size() == 1 && isa<InitListExpr>(Args[0]))) && |
| 3855 | "IsListInit/IsInitListCopy must come with a single initializer list " |
| 3856 | "argument."); |
| 3857 | InitListExpr *ILE = |
| 3858 | (IsListInit || IsInitListCopy) ? cast<InitListExpr>(Args[0]) : nullptr; |
| 3859 | MultiExprArg UnwrappedArgs = |
| 3860 | ILE ? MultiExprArg(ILE->getInits(), ILE->getNumInits()) : Args; |
Sebastian Redl | 88e4d49 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3861 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3862 | // The type we're constructing needs to be complete. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3863 | if (!S.isCompleteType(Kind.getLocation(), DestType)) { |
Douglas Gregor | 85f3423 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 3864 | Sequence.setIncompleteTypeFailure(DestType); |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3865 | return; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3866 | } |
| 3867 | |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 3868 | // C++17 [dcl.init]p17: |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3869 | // - If the initializer expression is a prvalue and the cv-unqualified |
| 3870 | // version of the source type is the same class as the class of the |
| 3871 | // destination, the initializer expression is used to initialize the |
| 3872 | // destination object. |
| 3873 | // Per DR (no number yet), this does not apply when initializing a base |
| 3874 | // class or delegating to another constructor from a mem-initializer. |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3875 | // ObjC++: Lambda captured by the block in the lambda to block conversion |
| 3876 | // should avoid copy elision. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 3877 | if (S.getLangOpts().CPlusPlus17 && |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3878 | Entity.getKind() != InitializedEntity::EK_Base && |
| 3879 | Entity.getKind() != InitializedEntity::EK_Delegating && |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3880 | Entity.getKind() != |
| 3881 | InitializedEntity::EK_LambdaToBlockConversionBlockElement && |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3882 | UnwrappedArgs.size() == 1 && UnwrappedArgs[0]->isRValue() && |
| 3883 | S.Context.hasSameUnqualifiedType(UnwrappedArgs[0]->getType(), DestType)) { |
| 3884 | // Convert qualifications if necessary. |
Richard Smith | 16d3150 | 2016-12-21 01:31:56 +0000 | [diff] [blame] | 3885 | Sequence.AddQualificationConversionStep(DestType, VK_RValue); |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3886 | if (ILE) |
| 3887 | Sequence.RewrapReferenceInitList(DestType, ILE); |
| 3888 | return; |
| 3889 | } |
| 3890 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3891 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
| 3892 | assert(DestRecordType && "Constructor initialization requires record type"); |
| 3893 | CXXRecordDecl *DestRecordDecl |
| 3894 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
| 3895 | |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3896 | // Build the candidate set directly in the initialization sequence |
| 3897 | // structure, so that it will persist if we fail. |
| 3898 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3899 | |
| 3900 | // Determine whether we are allowed to call explicit constructors or |
| 3901 | // explicit conversion operators. |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3902 | bool AllowExplicit = Kind.AllowExplicit() || IsListInit; |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3903 | bool CopyInitialization = Kind.getKind() == InitializationKind::IK_Copy; |
Sebastian Redl | 88e4d49 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3904 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3905 | // - Otherwise, if T is a class type, constructors are considered. The |
| 3906 | // applicable constructors are enumerated, and the best one is chosen |
| 3907 | // through overload resolution. |
Richard Smith | 40c7806 | 2015-02-21 02:31:57 +0000 | [diff] [blame] | 3908 | DeclContext::lookup_result Ctors = S.LookupConstructors(DestRecordDecl); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3909 | |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3910 | OverloadingResult Result = OR_No_Viable_Function; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3911 | OverloadCandidateSet::iterator Best; |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3912 | bool AsInitializerList = false; |
| 3913 | |
Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 3914 | // C++11 [over.match.list]p1, per DR1467: |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 3915 | // When objects of non-aggregate type T are list-initialized, such that |
| 3916 | // 8.5.4 [dcl.init.list] specifies that overload resolution is performed |
| 3917 | // according to the rules in this section, overload resolution selects |
| 3918 | // the constructor in two phases: |
| 3919 | // |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3920 | // - Initially, the candidate functions are the initializer-list |
| 3921 | // constructors of the class T and the argument list consists of the |
| 3922 | // initializer list as a single argument. |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3923 | if (IsListInit) { |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3924 | AsInitializerList = true; |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3925 | |
| 3926 | // If the initializer list has no elements and T has a default constructor, |
| 3927 | // the first phase is omitted. |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3928 | if (!(UnwrappedArgs.empty() && DestRecordDecl->hasDefaultConstructor())) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3929 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3930 | CandidateSet, DestType, Ctors, Best, |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3931 | CopyInitialization, AllowExplicit, |
Larisse Voufo | bcf327a | 2015-02-10 02:20:14 +0000 | [diff] [blame] | 3932 | /*OnlyListConstructor=*/true, |
| 3933 | IsListInit); |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3934 | } |
| 3935 | |
| 3936 | // C++11 [over.match.list]p1: |
| 3937 | // - If no viable initializer-list constructor is found, overload resolution |
| 3938 | // is performed again, where the candidate functions are all the |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3939 | // constructors of the class T and the argument list consists of the |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3940 | // elements of the initializer list. |
| 3941 | if (Result == OR_No_Viable_Function) { |
| 3942 | AsInitializerList = false; |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3943 | Result = ResolveConstructorOverload(S, Kind.getLocation(), UnwrappedArgs, |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3944 | CandidateSet, DestType, Ctors, Best, |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3945 | CopyInitialization, AllowExplicit, |
Larisse Voufo | bcf327a | 2015-02-10 02:20:14 +0000 | [diff] [blame] | 3946 | /*OnlyListConstructors=*/false, |
| 3947 | IsListInit); |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3948 | } |
| 3949 | if (Result) { |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3950 | Sequence.SetOverloadFailure(IsListInit ? |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3951 | InitializationSequence::FK_ListConstructorOverloadFailed : |
| 3952 | InitializationSequence::FK_ConstructorOverloadFailed, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3953 | Result); |
| 3954 | return; |
| 3955 | } |
| 3956 | |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3957 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 3958 | |
| 3959 | // In C++17, ResolveConstructorOverload can select a conversion function |
| 3960 | // instead of a constructor. |
| 3961 | if (auto *CD = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 3962 | // Add the user-defined conversion step that calls the conversion function. |
| 3963 | QualType ConvType = CD->getConversionType(); |
| 3964 | assert(S.Context.hasSameUnqualifiedType(ConvType, DestType) && |
| 3965 | "should not have selected this conversion function"); |
| 3966 | Sequence.AddUserConversionStep(CD, Best->FoundDecl, ConvType, |
| 3967 | HadMultipleCandidates); |
| 3968 | if (!S.Context.hasSameType(ConvType, DestType)) |
| 3969 | Sequence.AddQualificationConversionStep(DestType, VK_RValue); |
| 3970 | if (IsListInit) |
| 3971 | Sequence.RewrapReferenceInitList(Entity.getType(), ILE); |
| 3972 | return; |
| 3973 | } |
| 3974 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3975 | // C++11 [dcl.init]p6: |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3976 | // If a program calls for the default initialization of an object |
| 3977 | // of a const-qualified type T, T shall be a class type with a |
| 3978 | // user-provided default constructor. |
Nico Weber | 6a6376b | 2016-02-19 01:52:46 +0000 | [diff] [blame] | 3979 | // C++ core issue 253 proposal: |
| 3980 | // If the implicit default constructor initializes all subobjects, no |
| 3981 | // initializer should be required. |
| 3982 | // The 253 proposal is for example needed to process libstdc++ headers in 5.x. |
| 3983 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3984 | if (Kind.getKind() == InitializationKind::IK_Default && |
Nico Weber | 6a6376b | 2016-02-19 01:52:46 +0000 | [diff] [blame] | 3985 | Entity.getType().isConstQualified()) { |
| 3986 | if (!CtorDecl->getParent()->allowConstDefaultInit()) { |
| 3987 | if (!maybeRecoverWithZeroInitialization(S, Sequence, Entity)) |
| 3988 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 3989 | return; |
| 3990 | } |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3991 | } |
| 3992 | |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3993 | // C++11 [over.match.list]p1: |
| 3994 | // In copy-list-initialization, if an explicit constructor is chosen, the |
| 3995 | // initializer is ill-formed. |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3996 | if (IsListInit && !Kind.AllowExplicit() && CtorDecl->isExplicit()) { |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3997 | Sequence.SetFailed(InitializationSequence::FK_ExplicitConstructor); |
| 3998 | return; |
| 3999 | } |
| 4000 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4001 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 4002 | // subsumed by the initialization. |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 4003 | Sequence.AddConstructorInitializationStep( |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 4004 | Best->FoundDecl, CtorDecl, DestArrayType, HadMultipleCandidates, |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 4005 | IsListInit | IsInitListCopy, AsInitializerList); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4006 | } |
| 4007 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4008 | static bool |
| 4009 | ResolveOverloadedFunctionForReferenceBinding(Sema &S, |
| 4010 | Expr *Initializer, |
| 4011 | QualType &SourceType, |
| 4012 | QualType &UnqualifiedSourceType, |
| 4013 | QualType UnqualifiedTargetType, |
| 4014 | InitializationSequence &Sequence) { |
| 4015 | if (S.Context.getCanonicalType(UnqualifiedSourceType) == |
| 4016 | S.Context.OverloadTy) { |
| 4017 | DeclAccessPair Found; |
| 4018 | bool HadMultipleCandidates = false; |
| 4019 | if (FunctionDecl *Fn |
| 4020 | = S.ResolveAddressOfOverloadedFunction(Initializer, |
| 4021 | UnqualifiedTargetType, |
| 4022 | false, Found, |
| 4023 | &HadMultipleCandidates)) { |
| 4024 | Sequence.AddAddressOverloadResolutionStep(Fn, Found, |
| 4025 | HadMultipleCandidates); |
| 4026 | SourceType = Fn->getType(); |
| 4027 | UnqualifiedSourceType = SourceType.getUnqualifiedType(); |
| 4028 | } else if (!UnqualifiedTargetType->isRecordType()) { |
| 4029 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 4030 | return true; |
| 4031 | } |
| 4032 | } |
| 4033 | return false; |
| 4034 | } |
| 4035 | |
| 4036 | static void TryReferenceInitializationCore(Sema &S, |
| 4037 | const InitializedEntity &Entity, |
| 4038 | const InitializationKind &Kind, |
| 4039 | Expr *Initializer, |
| 4040 | QualType cv1T1, QualType T1, |
| 4041 | Qualifiers T1Quals, |
| 4042 | QualType cv2T2, QualType T2, |
| 4043 | Qualifiers T2Quals, |
| 4044 | InitializationSequence &Sequence); |
| 4045 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4046 | static void TryValueInitialization(Sema &S, |
| 4047 | const InitializedEntity &Entity, |
| 4048 | const InitializationKind &Kind, |
| 4049 | InitializationSequence &Sequence, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4050 | InitListExpr *InitList = nullptr); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4051 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4052 | /// Attempt list initialization of a reference. |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4053 | static void TryReferenceListInitialization(Sema &S, |
| 4054 | const InitializedEntity &Entity, |
| 4055 | const InitializationKind &Kind, |
| 4056 | InitListExpr *InitList, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4057 | InitializationSequence &Sequence, |
| 4058 | bool TreatUnavailableAsInvalid) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4059 | // First, catch C++03 where this isn't possible. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4060 | if (!S.getLangOpts().CPlusPlus11) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4061 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 4062 | return; |
| 4063 | } |
David Majnemer | 9370dc2 | 2015-04-26 07:35:03 +0000 | [diff] [blame] | 4064 | // Can't reference initialize a compound literal. |
| 4065 | if (Entity.getKind() == InitializedEntity::EK_CompoundLiteralInit) { |
| 4066 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 4067 | return; |
| 4068 | } |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4069 | |
| 4070 | QualType DestType = Entity.getType(); |
| 4071 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 4072 | Qualifiers T1Quals; |
| 4073 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
| 4074 | |
| 4075 | // Reference initialization via an initializer list works thus: |
| 4076 | // If the initializer list consists of a single element that is |
| 4077 | // reference-related to the referenced type, bind directly to that element |
| 4078 | // (possibly creating temporaries). |
| 4079 | // Otherwise, initialize a temporary with the initializer list and |
| 4080 | // bind to that. |
| 4081 | if (InitList->getNumInits() == 1) { |
| 4082 | Expr *Initializer = InitList->getInit(0); |
| 4083 | QualType cv2T2 = Initializer->getType(); |
| 4084 | Qualifiers T2Quals; |
| 4085 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
| 4086 | |
| 4087 | // If this fails, creating a temporary wouldn't work either. |
| 4088 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 4089 | T1, Sequence)) |
| 4090 | return; |
| 4091 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4092 | SourceLocation DeclLoc = Initializer->getBeginLoc(); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4093 | bool dummy1, dummy2, dummy3; |
| 4094 | Sema::ReferenceCompareResult RefRelationship |
| 4095 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, dummy1, |
| 4096 | dummy2, dummy3); |
| 4097 | if (RefRelationship >= Sema::Ref_Related) { |
| 4098 | // Try to bind the reference here. |
| 4099 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 4100 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 4101 | if (Sequence) |
| 4102 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
| 4103 | return; |
| 4104 | } |
Richard Smith | 03d9393 | 2013-01-15 07:58:29 +0000 | [diff] [blame] | 4105 | |
| 4106 | // Update the initializer if we've resolved an overloaded function. |
| 4107 | if (Sequence.step_begin() != Sequence.step_end()) |
| 4108 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4109 | } |
| 4110 | |
| 4111 | // Not reference-related. Create a temporary and bind to that. |
| 4112 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 4113 | |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4114 | TryListInitialization(S, TempEntity, Kind, InitList, Sequence, |
| 4115 | TreatUnavailableAsInvalid); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4116 | if (Sequence) { |
| 4117 | if (DestType->isRValueReferenceType() || |
| 4118 | (T1Quals.hasConst() && !T1Quals.hasVolatile())) |
| 4119 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 4120 | else |
| 4121 | Sequence.SetFailed( |
| 4122 | InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
| 4123 | } |
| 4124 | } |
| 4125 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4126 | /// Attempt list initialization (C++0x [dcl.init.list]) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4127 | static void TryListInitialization(Sema &S, |
| 4128 | const InitializedEntity &Entity, |
| 4129 | const InitializationKind &Kind, |
| 4130 | InitListExpr *InitList, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4131 | InitializationSequence &Sequence, |
| 4132 | bool TreatUnavailableAsInvalid) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4133 | QualType DestType = Entity.getType(); |
| 4134 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4135 | // C++ doesn't allow scalar initialization with more than one argument. |
| 4136 | // But C99 complex numbers are scalars and it makes sense there. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4137 | if (S.getLangOpts().CPlusPlus && DestType->isScalarType() && |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4138 | !DestType->isAnyComplexType() && InitList->getNumInits() > 1) { |
| 4139 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 4140 | return; |
| 4141 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4142 | if (DestType->isReferenceType()) { |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4143 | TryReferenceListInitialization(S, Entity, Kind, InitList, Sequence, |
| 4144 | TreatUnavailableAsInvalid); |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4145 | return; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4146 | } |
Sebastian Redl | 4f28b58 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 4147 | |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4148 | if (DestType->isRecordType() && |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4149 | !S.isCompleteType(InitList->getBeginLoc(), DestType)) { |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4150 | Sequence.setIncompleteTypeFailure(DestType); |
| 4151 | return; |
| 4152 | } |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4153 | |
Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 4154 | // C++11 [dcl.init.list]p3, per DR1467: |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4155 | // - If T is a class type and the initializer list has a single element of |
| 4156 | // type cv U, where U is T or a class derived from T, the object is |
| 4157 | // initialized from that element (by copy-initialization for |
| 4158 | // copy-list-initialization, or by direct-initialization for |
| 4159 | // direct-list-initialization). |
| 4160 | // - Otherwise, if T is a character array and the initializer list has a |
| 4161 | // single element that is an appropriately-typed string literal |
| 4162 | // (8.5.2 [dcl.init.string]), initialization is performed as described |
| 4163 | // in that section. |
Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 4164 | // - Otherwise, if T is an aggregate, [...] (continue below). |
| 4165 | if (S.getLangOpts().CPlusPlus11 && InitList->getNumInits() == 1) { |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4166 | if (DestType->isRecordType()) { |
| 4167 | QualType InitType = InitList->getInit(0)->getType(); |
| 4168 | if (S.Context.hasSameUnqualifiedType(InitType, DestType) || |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4169 | S.IsDerivedFrom(InitList->getBeginLoc(), InitType, DestType)) { |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 4170 | Expr *InitListAsExpr = InitList; |
| 4171 | TryConstructorInitialization(S, Entity, Kind, InitListAsExpr, DestType, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 4172 | DestType, Sequence, |
| 4173 | /*InitListSyntax*/false, |
| 4174 | /*IsInitListCopy*/true); |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4175 | return; |
| 4176 | } |
| 4177 | } |
| 4178 | if (const ArrayType *DestAT = S.Context.getAsArrayType(DestType)) { |
| 4179 | Expr *SubInit[1] = {InitList->getInit(0)}; |
| 4180 | if (!isa<VariableArrayType>(DestAT) && |
| 4181 | IsStringInit(SubInit[0], DestAT, S.Context) == SIF_None) { |
| 4182 | InitializationKind SubKind = |
| 4183 | Kind.getKind() == InitializationKind::IK_DirectList |
| 4184 | ? InitializationKind::CreateDirect(Kind.getLocation(), |
| 4185 | InitList->getLBraceLoc(), |
| 4186 | InitList->getRBraceLoc()) |
| 4187 | : Kind; |
| 4188 | Sequence.InitializeFrom(S, Entity, SubKind, SubInit, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4189 | /*TopLevelOfInitList*/ true, |
| 4190 | TreatUnavailableAsInvalid); |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4191 | |
| 4192 | // TryStringLiteralInitialization() (in InitializeFrom()) will fail if |
| 4193 | // the element is not an appropriately-typed string literal, in which |
| 4194 | // case we should proceed as in C++11 (below). |
| 4195 | if (Sequence) { |
| 4196 | Sequence.RewrapReferenceInitList(Entity.getType(), InitList); |
| 4197 | return; |
| 4198 | } |
| 4199 | } |
Sebastian Redl | 4f28b58 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 4200 | } |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4201 | } |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4202 | |
| 4203 | // C++11 [dcl.init.list]p3: |
| 4204 | // - If T is an aggregate, aggregate initialization is performed. |
Faisal Vali | 30622bb | 2015-12-07 02:37:44 +0000 | [diff] [blame] | 4205 | if ((DestType->isRecordType() && !DestType->isAggregateType()) || |
| 4206 | (S.getLangOpts().CPlusPlus11 && |
| 4207 | S.isStdInitializerList(DestType, nullptr))) { |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4208 | if (S.getLangOpts().CPlusPlus11) { |
| 4209 | // - Otherwise, if the initializer list has no elements and T is a |
| 4210 | // class type with a default constructor, the object is |
| 4211 | // value-initialized. |
| 4212 | if (InitList->getNumInits() == 0) { |
| 4213 | CXXRecordDecl *RD = DestType->getAsCXXRecordDecl(); |
| 4214 | if (RD->hasDefaultConstructor()) { |
| 4215 | TryValueInitialization(S, Entity, Kind, Sequence, InitList); |
| 4216 | return; |
| 4217 | } |
| 4218 | } |
| 4219 | |
| 4220 | // - Otherwise, if T is a specialization of std::initializer_list<E>, |
| 4221 | // an initializer_list object constructed [...] |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4222 | if (TryInitializerListConstruction(S, InitList, DestType, Sequence, |
| 4223 | TreatUnavailableAsInvalid)) |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4224 | return; |
| 4225 | |
| 4226 | // - Otherwise, if T is a class type, constructors are considered. |
| 4227 | Expr *InitListAsExpr = InitList; |
| 4228 | TryConstructorInitialization(S, Entity, Kind, InitListAsExpr, DestType, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 4229 | DestType, Sequence, /*InitListSyntax*/true); |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4230 | } else |
| 4231 | Sequence.SetFailed(InitializationSequence::FK_InitListBadDestinationType); |
| 4232 | return; |
| 4233 | } |
| 4234 | |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 4235 | if (S.getLangOpts().CPlusPlus && !DestType->isAggregateType() && |
Richard Smith | ed63886 | 2016-03-28 06:08:37 +0000 | [diff] [blame] | 4236 | InitList->getNumInits() == 1) { |
| 4237 | Expr *E = InitList->getInit(0); |
| 4238 | |
| 4239 | // - Otherwise, if T is an enumeration with a fixed underlying type, |
| 4240 | // the initializer-list has a single element v, and the initialization |
| 4241 | // is direct-list-initialization, the object is initialized with the |
| 4242 | // value T(v); if a narrowing conversion is required to convert v to |
| 4243 | // the underlying type of T, the program is ill-formed. |
| 4244 | auto *ET = DestType->getAs<EnumType>(); |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 4245 | if (S.getLangOpts().CPlusPlus17 && |
Richard Smith | ed63886 | 2016-03-28 06:08:37 +0000 | [diff] [blame] | 4246 | Kind.getKind() == InitializationKind::IK_DirectList && |
| 4247 | ET && ET->getDecl()->isFixed() && |
| 4248 | !S.Context.hasSameUnqualifiedType(E->getType(), DestType) && |
| 4249 | (E->getType()->isIntegralOrEnumerationType() || |
| 4250 | E->getType()->isFloatingType())) { |
| 4251 | // There are two ways that T(v) can work when T is an enumeration type. |
| 4252 | // If there is either an implicit conversion sequence from v to T or |
| 4253 | // a conversion function that can convert from v to T, then we use that. |
| 4254 | // Otherwise, if v is of integral, enumeration, or floating-point type, |
| 4255 | // it is converted to the enumeration type via its underlying type. |
| 4256 | // There is no overlap possible between these two cases (except when the |
| 4257 | // source value is already of the destination type), and the first |
| 4258 | // case is handled by the general case for single-element lists below. |
| 4259 | ImplicitConversionSequence ICS; |
| 4260 | ICS.setStandard(); |
| 4261 | ICS.Standard.setAsIdentityConversion(); |
Vedant Kumar | f4217f8 | 2017-02-16 01:20:00 +0000 | [diff] [blame] | 4262 | if (!E->isRValue()) |
| 4263 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
Richard Smith | ed63886 | 2016-03-28 06:08:37 +0000 | [diff] [blame] | 4264 | // If E is of a floating-point type, then the conversion is ill-formed |
| 4265 | // due to narrowing, but go through the motions in order to produce the |
| 4266 | // right diagnostic. |
| 4267 | ICS.Standard.Second = E->getType()->isFloatingType() |
| 4268 | ? ICK_Floating_Integral |
| 4269 | : ICK_Integral_Conversion; |
| 4270 | ICS.Standard.setFromType(E->getType()); |
| 4271 | ICS.Standard.setToType(0, E->getType()); |
| 4272 | ICS.Standard.setToType(1, DestType); |
| 4273 | ICS.Standard.setToType(2, DestType); |
| 4274 | Sequence.AddConversionSequenceStep(ICS, ICS.Standard.getToType(2), |
| 4275 | /*TopLevelOfInitList*/true); |
| 4276 | Sequence.RewrapReferenceInitList(Entity.getType(), InitList); |
| 4277 | return; |
| 4278 | } |
| 4279 | |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 4280 | // - Otherwise, if the initializer list has a single element of type E |
| 4281 | // [...references are handled above...], the object or reference is |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4282 | // initialized from that element (by copy-initialization for |
| 4283 | // copy-list-initialization, or by direct-initialization for |
| 4284 | // direct-list-initialization); if a narrowing conversion is required |
| 4285 | // to convert the element to T, the program is ill-formed. |
| 4286 | // |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 4287 | // Per core-24034, this is direct-initialization if we were performing |
| 4288 | // direct-list-initialization and copy-initialization otherwise. |
| 4289 | // We can't use InitListChecker for this, because it always performs |
| 4290 | // copy-initialization. This only matters if we might use an 'explicit' |
| 4291 | // conversion operator, so we only need to handle the cases where the source |
| 4292 | // is of record type. |
Richard Smith | ed63886 | 2016-03-28 06:08:37 +0000 | [diff] [blame] | 4293 | if (InitList->getInit(0)->getType()->isRecordType()) { |
| 4294 | InitializationKind SubKind = |
| 4295 | Kind.getKind() == InitializationKind::IK_DirectList |
| 4296 | ? InitializationKind::CreateDirect(Kind.getLocation(), |
| 4297 | InitList->getLBraceLoc(), |
| 4298 | InitList->getRBraceLoc()) |
| 4299 | : Kind; |
| 4300 | Expr *SubInit[1] = { InitList->getInit(0) }; |
| 4301 | Sequence.InitializeFrom(S, Entity, SubKind, SubInit, |
| 4302 | /*TopLevelOfInitList*/true, |
| 4303 | TreatUnavailableAsInvalid); |
| 4304 | if (Sequence) |
| 4305 | Sequence.RewrapReferenceInitList(Entity.getType(), InitList); |
| 4306 | return; |
| 4307 | } |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 4308 | } |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4309 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4310 | InitListChecker CheckInitList(S, Entity, InitList, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4311 | DestType, /*VerifyOnly=*/true, TreatUnavailableAsInvalid); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4312 | if (CheckInitList.HadError()) { |
| 4313 | Sequence.SetFailed(InitializationSequence::FK_ListInitializationFailed); |
| 4314 | return; |
| 4315 | } |
| 4316 | |
| 4317 | // Add the list initialization step with the built init list. |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4318 | Sequence.AddListInitializationStep(DestType); |
| 4319 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4320 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4321 | /// Try a reference initialization that involves calling a conversion |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4322 | /// function. |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4323 | static OverloadingResult TryRefInitWithConversionFunction( |
| 4324 | Sema &S, const InitializedEntity &Entity, const InitializationKind &Kind, |
| 4325 | Expr *Initializer, bool AllowRValues, bool IsLValueRef, |
| 4326 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4327 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4328 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 4329 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 4330 | QualType cv2T2 = Initializer->getType(); |
| 4331 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 4332 | |
| 4333 | bool DerivedToBase; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4334 | bool ObjCConversion; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4335 | bool ObjCLifetimeConversion; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4336 | assert(!S.CompareReferenceRelationship(Initializer->getBeginLoc(), T1, T2, |
| 4337 | DerivedToBase, ObjCConversion, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4338 | ObjCLifetimeConversion) && |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4339 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 8abbc65 | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 4340 | (void)DerivedToBase; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4341 | (void)ObjCConversion; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4342 | (void)ObjCLifetimeConversion; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4343 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4344 | // Build the candidate set directly in the initialization sequence |
| 4345 | // structure, so that it will persist if we fail. |
| 4346 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 4347 | CandidateSet.clear(OverloadCandidateSet::CSK_InitByUserDefinedConversion); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4348 | |
Richard Smith | b368ea8 | 2018-07-02 23:25:22 +0000 | [diff] [blame] | 4349 | // Determine whether we are allowed to call explicit conversion operators. |
| 4350 | // Note that none of [over.match.copy], [over.match.conv], nor |
| 4351 | // [over.match.ref] permit an explicit constructor to be chosen when |
| 4352 | // initializing a reference, not even for direct-initialization. |
| 4353 | bool AllowExplicitCtors = false; |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4354 | bool AllowExplicitConvs = Kind.allowExplicitConversionFunctionsInRefBinding(); |
| 4355 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4356 | const RecordType *T1RecordType = nullptr; |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 4357 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4358 | S.isCompleteType(Kind.getLocation(), T1)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4359 | // The type we're converting to is a class type. Enumerate its constructors |
| 4360 | // to see if there is a suitable conversion. |
| 4361 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 4362 | |
Richard Smith | 40c7806 | 2015-02-21 02:31:57 +0000 | [diff] [blame] | 4363 | for (NamedDecl *D : S.LookupConstructors(T1RecordDecl)) { |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4364 | auto Info = getConstructorInfo(D); |
| 4365 | if (!Info.Constructor) |
| 4366 | continue; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4367 | |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4368 | if (!Info.Constructor->isInvalidDecl() && |
Richard Smith | b368ea8 | 2018-07-02 23:25:22 +0000 | [diff] [blame] | 4369 | Info.Constructor->isConvertingConstructor(AllowExplicitCtors)) { |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4370 | if (Info.ConstructorTmpl) |
| 4371 | S.AddTemplateOverloadCandidate(Info.ConstructorTmpl, Info.FoundDecl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4372 | /*ExplicitArgs*/ nullptr, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4373 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | dfbdfbb | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 4374 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4375 | else |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4376 | S.AddOverloadCandidate(Info.Constructor, Info.FoundDecl, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4377 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | dfbdfbb | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 4378 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4379 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4380 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4381 | } |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 4382 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 4383 | return OR_No_Viable_Function; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4384 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4385 | const RecordType *T2RecordType = nullptr; |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 4386 | if ((T2RecordType = T2->getAs<RecordType>()) && |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4387 | S.isCompleteType(Kind.getLocation(), T2)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4388 | // The type we're converting from is a class type, enumerate its conversion |
| 4389 | // functions. |
| 4390 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 4391 | |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 4392 | const auto &Conversions = T2RecordDecl->getVisibleConversionFunctions(); |
| 4393 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4394 | NamedDecl *D = *I; |
| 4395 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 4396 | if (isa<UsingShadowDecl>(D)) |
| 4397 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4398 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4399 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 4400 | CXXConversionDecl *Conv; |
| 4401 | if (ConvTemplate) |
| 4402 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 4403 | else |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4404 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4405 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4406 | // If the conversion function doesn't return a reference type, |
| 4407 | // it can't be considered for this conversion unless we're allowed to |
| 4408 | // consider rvalues. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4409 | // FIXME: Do we need to make sure that we only consider conversion |
| 4410 | // candidates with reference-compatible results? That might be needed to |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4411 | // break recursion. |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4412 | if ((AllowExplicitConvs || !Conv->isExplicit()) && |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4413 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 4414 | if (ConvTemplate) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4415 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4416 | ActingDC, Initializer, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 4417 | DestType, CandidateSet, |
| 4418 | /*AllowObjCConversionOnExplicit=*/ |
| 4419 | false); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4420 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4421 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 4422 | Initializer, DestType, CandidateSet, |
| 4423 | /*AllowObjCConversionOnExplicit=*/false); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4424 | } |
| 4425 | } |
| 4426 | } |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 4427 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 4428 | return OR_No_Viable_Function; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4429 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4430 | SourceLocation DeclLoc = Initializer->getBeginLoc(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4431 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4432 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4433 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4434 | if (OverloadingResult Result |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 4435 | = CandidateSet.BestViableFunction(S, DeclLoc, Best)) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4436 | return Result; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 4437 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4438 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 4439 | // This is the overload that will be used for this initialization step if we |
| 4440 | // use this initialization. Mark it as referenced. |
| 4441 | Function->setReferenced(); |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4442 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4443 | // Compute the returned type and value kind of the conversion. |
| 4444 | QualType cv3T3; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4445 | if (isa<CXXConversionDecl>(Function)) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4446 | cv3T3 = Function->getReturnType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4447 | else |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4448 | cv3T3 = T1; |
| 4449 | |
| 4450 | ExprValueKind VK = VK_RValue; |
| 4451 | if (cv3T3->isLValueReferenceType()) |
| 4452 | VK = VK_LValue; |
| 4453 | else if (const auto *RRef = cv3T3->getAs<RValueReferenceType>()) |
| 4454 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
| 4455 | cv3T3 = cv3T3.getNonLValueExprType(S.Context); |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 4456 | |
| 4457 | // Add the user-defined conversion step. |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4458 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4459 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, cv3T3, |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4460 | HadMultipleCandidates); |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 4461 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4462 | // Determine whether we'll need to perform derived-to-base adjustments or |
| 4463 | // other conversions. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4464 | bool NewDerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4465 | bool NewObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4466 | bool NewObjCLifetimeConversion = false; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4467 | Sema::ReferenceCompareResult NewRefRelationship |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4468 | = S.CompareReferenceRelationship(DeclLoc, T1, cv3T3, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4469 | NewDerivedToBase, NewObjCConversion, |
| 4470 | NewObjCLifetimeConversion); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4471 | |
| 4472 | // Add the final conversion sequence, if necessary. |
Douglas Gregor | 1ce52ca | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 4473 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4474 | assert(!isa<CXXConstructorDecl>(Function) && |
| 4475 | "should not have conversion after constructor"); |
| 4476 | |
Douglas Gregor | 1ce52ca | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 4477 | ImplicitConversionSequence ICS; |
| 4478 | ICS.setStandard(); |
| 4479 | ICS.Standard = Best->FinalConversion; |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4480 | Sequence.AddConversionSequenceStep(ICS, ICS.Standard.getToType(2)); |
| 4481 | |
| 4482 | // Every implicit conversion results in a prvalue, except for a glvalue |
| 4483 | // derived-to-base conversion, which we handle below. |
| 4484 | cv3T3 = ICS.Standard.getToType(2); |
| 4485 | VK = VK_RValue; |
| 4486 | } |
| 4487 | |
| 4488 | // If the converted initializer is a prvalue, its type T4 is adjusted to |
| 4489 | // type "cv1 T4" and the temporary materialization conversion is applied. |
| 4490 | // |
| 4491 | // We adjust the cv-qualifications to match the reference regardless of |
| 4492 | // whether we have a prvalue so that the AST records the change. In this |
| 4493 | // case, T4 is "cv3 T3". |
| 4494 | QualType cv1T4 = S.Context.getQualifiedType(cv3T3, cv1T1.getQualifiers()); |
| 4495 | if (cv1T4.getQualifiers() != cv3T3.getQualifiers()) |
| 4496 | Sequence.AddQualificationConversionStep(cv1T4, VK); |
| 4497 | Sequence.AddReferenceBindingStep(cv1T4, VK == VK_RValue); |
| 4498 | VK = IsLValueRef ? VK_LValue : VK_XValue; |
| 4499 | |
| 4500 | if (NewDerivedToBase) |
| 4501 | Sequence.AddDerivedToBaseCastStep(cv1T1, VK); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4502 | else if (NewObjCConversion) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4503 | Sequence.AddObjCObjectConversionStep(cv1T1); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4504 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4505 | return OR_Success; |
| 4506 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4507 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4508 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 4509 | const InitializedEntity &Entity, |
| 4510 | Expr *CurInitExpr); |
| 4511 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4512 | /// Attempt reference initialization (C++0x [dcl.init.ref]) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4513 | static void TryReferenceInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4514 | const InitializedEntity &Entity, |
| 4515 | const InitializationKind &Kind, |
| 4516 | Expr *Initializer, |
| 4517 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4518 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4519 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 4520 | Qualifiers T1Quals; |
| 4521 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4522 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 4523 | Qualifiers T2Quals; |
| 4524 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4525 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4526 | // If the initializer is the address of an overloaded function, try |
| 4527 | // to resolve the overloaded function. If all goes well, T2 is the |
| 4528 | // type of the resulting function. |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4529 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 4530 | T1, Sequence)) |
| 4531 | return; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4532 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4533 | // Delegate everything else to a subfunction. |
| 4534 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 4535 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 4536 | } |
| 4537 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4538 | /// Determine whether an expression is a non-referenceable glvalue (one to |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 4539 | /// which a reference can never bind). Attempting to bind a reference to |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4540 | /// such a glvalue will always create a temporary. |
| 4541 | static bool isNonReferenceableGLValue(Expr *E) { |
| 4542 | return E->refersToBitField() || E->refersToVectorElement(); |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 4543 | } |
| 4544 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4545 | /// Reference initialization without resolving overloaded functions. |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4546 | static void TryReferenceInitializationCore(Sema &S, |
| 4547 | const InitializedEntity &Entity, |
| 4548 | const InitializationKind &Kind, |
| 4549 | Expr *Initializer, |
| 4550 | QualType cv1T1, QualType T1, |
| 4551 | Qualifiers T1Quals, |
| 4552 | QualType cv2T2, QualType T2, |
| 4553 | Qualifiers T2Quals, |
| 4554 | InitializationSequence &Sequence) { |
| 4555 | QualType DestType = Entity.getType(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4556 | SourceLocation DeclLoc = Initializer->getBeginLoc(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4557 | // Compute some basic properties of the types and the initializer. |
| 4558 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 4559 | bool isRValueRef = !isLValueRef; |
| 4560 | bool DerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4561 | bool ObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4562 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4563 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4564 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4565 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4566 | ObjCConversion, ObjCLifetimeConversion); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4567 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4568 | // C++0x [dcl.init.ref]p5: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4569 | // 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] | 4570 | // "cv2 T2" as follows: |
| 4571 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4572 | // - If the reference is an lvalue reference and the initializer |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4573 | // expression |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4574 | // Note the analogous bullet points for rvalue refs to functions. Because |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4575 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 4576 | // like lvalue refs. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4577 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4578 | bool T1Function = T1->isFunctionType(); |
| 4579 | if (isLValueRef || T1Function) { |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4580 | if (InitCategory.isLValue() && !isNonReferenceableGLValue(Initializer) && |
Richard Smith | ce76629 | 2016-10-21 23:01:55 +0000 | [diff] [blame] | 4581 | (RefRelationship == Sema::Ref_Compatible || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4582 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4583 | RefRelationship == Sema::Ref_Related))) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4584 | // - 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] | 4585 | // reference-compatible with "cv2 T2," or |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4586 | if (T1Quals != T2Quals) |
| 4587 | // Convert to cv1 T2. This should only add qualifiers unless this is a |
| 4588 | // c-style cast. The removal of qualifiers in that case notionally |
| 4589 | // happens after the reference binding, but that doesn't matter. |
| 4590 | Sequence.AddQualificationConversionStep( |
| 4591 | S.Context.getQualifiedType(T2, T1Quals), |
| 4592 | Initializer->getValueKind()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4593 | if (DerivedToBase) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4594 | Sequence.AddDerivedToBaseCastStep(cv1T1, VK_LValue); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4595 | else if (ObjCConversion) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4596 | Sequence.AddObjCObjectConversionStep(cv1T1); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4597 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4598 | // We only create a temporary here when binding a reference to a |
| 4599 | // bit-field or vector element. Those cases are't supposed to be |
| 4600 | // handled by this bullet, but the outcome is the same either way. |
| 4601 | Sequence.AddReferenceBindingStep(cv1T1, false); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4602 | return; |
| 4603 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4604 | |
| 4605 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 4606 | // reference-related to T2, and can be implicitly converted to an |
| 4607 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 4608 | // with "cv3 T3" (this conversion is selected by enumerating the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4609 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 4610 | // one through overload resolution (13.3)), |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4611 | // 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] | 4612 | // an rvalue. DR1287 removed the "implicitly" here. |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4613 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 4614 | (isLValueRef || InitCategory.isRValue())) { |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4615 | ConvOvlResult = TryRefInitWithConversionFunction( |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4616 | S, Entity, Kind, Initializer, /*AllowRValues*/ isRValueRef, |
| 4617 | /*IsLValueRef*/ isLValueRef, Sequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4618 | if (ConvOvlResult == OR_Success) |
| 4619 | return; |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4620 | if (ConvOvlResult != OR_No_Viable_Function) |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4621 | Sequence.SetOverloadFailure( |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4622 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4623 | ConvOvlResult); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4624 | } |
| 4625 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4626 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4627 | // - Otherwise, the reference shall be an lvalue reference to a |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4628 | // 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] | 4629 | // shall be an rvalue reference. |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 4630 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) { |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 4631 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 4632 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 4633 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4634 | Sequence.SetOverloadFailure( |
| 4635 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4636 | ConvOvlResult); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4637 | else if (!InitCategory.isLValue()) |
| 4638 | Sequence.SetFailed( |
| 4639 | InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
| 4640 | else { |
| 4641 | InitializationSequence::FailureKind FK; |
| 4642 | switch (RefRelationship) { |
| 4643 | case Sema::Ref_Compatible: |
| 4644 | if (Initializer->refersToBitField()) |
| 4645 | FK = InitializationSequence:: |
| 4646 | FK_NonConstLValueReferenceBindingToBitfield; |
| 4647 | else if (Initializer->refersToVectorElement()) |
| 4648 | FK = InitializationSequence:: |
| 4649 | FK_NonConstLValueReferenceBindingToVectorElement; |
| 4650 | else |
| 4651 | llvm_unreachable("unexpected kind of compatible initializer"); |
| 4652 | break; |
| 4653 | case Sema::Ref_Related: |
| 4654 | FK = InitializationSequence::FK_ReferenceInitDropsQualifiers; |
| 4655 | break; |
| 4656 | case Sema::Ref_Incompatible: |
| 4657 | FK = InitializationSequence:: |
| 4658 | FK_NonConstLValueReferenceBindingToUnrelated; |
| 4659 | break; |
| 4660 | } |
| 4661 | Sequence.SetFailed(FK); |
| 4662 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4663 | return; |
| 4664 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4665 | |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4666 | // - If the initializer expression |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4667 | // - is an |
| 4668 | // [<=14] xvalue (but not a bit-field), class prvalue, array prvalue, or |
| 4669 | // [1z] rvalue (but not a bit-field) or |
| 4670 | // function lvalue and "cv1 T1" is reference-compatible with "cv2 T2" |
| 4671 | // |
| 4672 | // Note: functions are handled above and below rather than here... |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4673 | if (!T1Function && |
Richard Smith | ce76629 | 2016-10-21 23:01:55 +0000 | [diff] [blame] | 4674 | (RefRelationship == Sema::Ref_Compatible || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4675 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4676 | RefRelationship == Sema::Ref_Related)) && |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4677 | ((InitCategory.isXValue() && !isNonReferenceableGLValue(Initializer)) || |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 4678 | (InitCategory.isPRValue() && |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 4679 | (S.getLangOpts().CPlusPlus17 || T2->isRecordType() || |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 4680 | T2->isArrayType())))) { |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4681 | ExprValueKind ValueKind = InitCategory.isXValue() ? VK_XValue : VK_RValue; |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4682 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4683 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 4684 | // compiler the freedom to perform a copy here or bind to the |
| 4685 | // object, while C++0x requires that we bind directly to the |
| 4686 | // object. Hence, we always bind to the object without making an |
| 4687 | // extra copy. However, in C++03 requires that we check for the |
| 4688 | // presence of a suitable copy constructor: |
| 4689 | // |
| 4690 | // The constructor that would be used to make the copy shall |
| 4691 | // be callable whether or not the copy is actually done. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4692 | if (!S.getLangOpts().CPlusPlus11 && !S.getLangOpts().MicrosoftExt) |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4693 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4694 | else if (S.getLangOpts().CPlusPlus11) |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4695 | CheckCXX98CompatAccessibleCopy(S, Entity, Initializer); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4696 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4697 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4698 | // C++1z [dcl.init.ref]/5.2.1.2: |
| 4699 | // If the converted initializer is a prvalue, its type T4 is adjusted |
| 4700 | // to type "cv1 T4" and the temporary materialization conversion is |
| 4701 | // applied. |
Anastasia Stulova | d1986d1 | 2019-01-14 11:44:22 +0000 | [diff] [blame] | 4702 | // Postpone address space conversions to after the temporary materialization |
| 4703 | // conversion to allow creating temporaries in the alloca address space. |
Anastasia Stulova | e368e4d | 2019-02-05 11:32:58 +0000 | [diff] [blame] | 4704 | auto T1QualsIgnoreAS = T1Quals; |
| 4705 | auto T2QualsIgnoreAS = T2Quals; |
| 4706 | if (T1Quals.getAddressSpace() != T2Quals.getAddressSpace()) { |
| 4707 | T1QualsIgnoreAS.removeAddressSpace(); |
| 4708 | T2QualsIgnoreAS.removeAddressSpace(); |
| 4709 | } |
| 4710 | QualType cv1T4 = S.Context.getQualifiedType(cv2T2, T1QualsIgnoreAS); |
| 4711 | if (T1QualsIgnoreAS != T2QualsIgnoreAS) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4712 | Sequence.AddQualificationConversionStep(cv1T4, ValueKind); |
| 4713 | Sequence.AddReferenceBindingStep(cv1T4, ValueKind == VK_RValue); |
| 4714 | ValueKind = isLValueRef ? VK_LValue : VK_XValue; |
Anastasia Stulova | e368e4d | 2019-02-05 11:32:58 +0000 | [diff] [blame] | 4715 | // Add addr space conversion if required. |
| 4716 | if (T1Quals.getAddressSpace() != T2Quals.getAddressSpace()) { |
| 4717 | auto T4Quals = cv1T4.getQualifiers(); |
| 4718 | T4Quals.addAddressSpace(T1Quals.getAddressSpace()); |
| 4719 | QualType cv1T4WithAS = S.Context.getQualifiedType(T2, T4Quals); |
| 4720 | Sequence.AddQualificationConversionStep(cv1T4WithAS, ValueKind); |
Anastasia Stulova | d1986d1 | 2019-01-14 11:44:22 +0000 | [diff] [blame] | 4721 | } |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4722 | |
| 4723 | // In any case, the reference is bound to the resulting glvalue (or to |
| 4724 | // an appropriate base class subobject). |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4725 | if (DerivedToBase) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4726 | Sequence.AddDerivedToBaseCastStep(cv1T1, ValueKind); |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4727 | else if (ObjCConversion) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4728 | Sequence.AddObjCObjectConversionStep(cv1T1); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4729 | return; |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4730 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4731 | |
| 4732 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 4733 | // reference-related to T2, and can be implicitly converted to an |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4734 | // xvalue, class prvalue, or function lvalue of type "cv3 T3", |
| 4735 | // where "cv1 T1" is reference-compatible with "cv3 T3", |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4736 | // |
| 4737 | // DR1287 removes the "implicitly" here. |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4738 | if (T2->isRecordType()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4739 | if (RefRelationship == Sema::Ref_Incompatible) { |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4740 | ConvOvlResult = TryRefInitWithConversionFunction( |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4741 | S, Entity, Kind, Initializer, /*AllowRValues*/ true, |
| 4742 | /*IsLValueRef*/ isLValueRef, Sequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4743 | if (ConvOvlResult) |
| 4744 | Sequence.SetOverloadFailure( |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4745 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4746 | ConvOvlResult); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4747 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4748 | return; |
| 4749 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4750 | |
Richard Smith | ce76629 | 2016-10-21 23:01:55 +0000 | [diff] [blame] | 4751 | if (RefRelationship == Sema::Ref_Compatible && |
Douglas Gregor | 6fa6ab0 | 2013-03-26 23:59:23 +0000 | [diff] [blame] | 4752 | isRValueRef && InitCategory.isLValue()) { |
| 4753 | Sequence.SetFailed( |
| 4754 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 4755 | return; |
| 4756 | } |
| 4757 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4758 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 4759 | return; |
| 4760 | } |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4761 | |
| 4762 | // - Otherwise, a temporary of type "cv1 T1" is created and initialized |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4763 | // from the initializer expression using the rules for a non-reference |
Richard Smith | 2eabf78 | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 4764 | // copy-initialization (8.5). The reference is then bound to the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4765 | // temporary. [...] |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 4766 | |
Anastasia Stulova | d3ae87e | 2019-03-06 13:02:41 +0000 | [diff] [blame] | 4767 | // Ignore address space of reference type at this point and perform address |
| 4768 | // space conversion after the reference binding step. |
| 4769 | QualType cv1T1IgnoreAS = |
| 4770 | T1Quals.hasAddressSpace() |
| 4771 | ? S.Context.getQualifiedType(T1, T1Quals.withoutAddressSpace()) |
| 4772 | : cv1T1; |
| 4773 | |
| 4774 | InitializedEntity TempEntity = |
| 4775 | InitializedEntity::InitializeTemporary(cv1T1IgnoreAS); |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 4776 | |
Richard Smith | 2eabf78 | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 4777 | // FIXME: Why do we use an implicit conversion here rather than trying |
| 4778 | // copy-initialization? |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4779 | ImplicitConversionSequence ICS |
| 4780 | = S.TryImplicitConversion(Initializer, TempEntity.getType(), |
Richard Smith | 2eabf78 | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 4781 | /*SuppressUserConversions=*/false, |
| 4782 | /*AllowExplicit=*/false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4783 | /*FIXME:InOverloadResolution=*/false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4784 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 4785 | /*AllowObjCWritebackConversion=*/false); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4786 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4787 | if (ICS.isBad()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4788 | // FIXME: Use the conversion function set stored in ICS to turn |
| 4789 | // this into an overloading ambiguity diagnostic. However, we need |
| 4790 | // to keep that set as an OverloadCandidateSet rather than as some |
| 4791 | // other kind of set. |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4792 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 4793 | Sequence.SetOverloadFailure( |
| 4794 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4795 | ConvOvlResult); |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 4796 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 4797 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4798 | else |
| 4799 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4800 | return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4801 | } else { |
| 4802 | Sequence.AddConversionSequenceStep(ICS, TempEntity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4803 | } |
| 4804 | |
| 4805 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 4806 | // same cv-qualification as, or greater cv-qualification |
| 4807 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 4808 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 4809 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
Anastasia Stulova | d3ae87e | 2019-03-06 13:02:41 +0000 | [diff] [blame] | 4810 | if ((RefRelationship == Sema::Ref_Related && |
| 4811 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) || |
| 4812 | !T1Quals.isAddressSpaceSupersetOf(T2Quals)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4813 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 4814 | return; |
| 4815 | } |
| 4816 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4817 | // [...] 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] | 4818 | // reference, the initializer expression shall not be an lvalue. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4819 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 4820 | InitCategory.isLValue()) { |
| 4821 | Sequence.SetFailed( |
| 4822 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 4823 | return; |
| 4824 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4825 | |
Anastasia Stulova | d3ae87e | 2019-03-06 13:02:41 +0000 | [diff] [blame] | 4826 | Sequence.AddReferenceBindingStep(cv1T1IgnoreAS, /*bindingTemporary=*/true); |
| 4827 | |
| 4828 | if (T1Quals.hasAddressSpace()) |
| 4829 | Sequence.AddQualificationConversionStep(cv1T1, isLValueRef ? VK_LValue |
| 4830 | : VK_XValue); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4831 | } |
| 4832 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4833 | /// Attempt character array initialization from a string literal |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4834 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 4835 | static void TryStringLiteralInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4836 | const InitializedEntity &Entity, |
| 4837 | const InitializationKind &Kind, |
| 4838 | Expr *Initializer, |
| 4839 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4840 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4841 | } |
| 4842 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4843 | /// Attempt value initialization (C++ [dcl.init]p7). |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4844 | static void TryValueInitialization(Sema &S, |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4845 | const InitializedEntity &Entity, |
| 4846 | const InitializationKind &Kind, |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4847 | InitializationSequence &Sequence, |
| 4848 | InitListExpr *InitList) { |
| 4849 | assert((!InitList || InitList->getNumInits() == 0) && |
| 4850 | "Shouldn't use value-init for non-empty init lists"); |
| 4851 | |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4852 | // C++98 [dcl.init]p5, C++11 [dcl.init]p7: |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4853 | // |
| 4854 | // To value-initialize an object of type T means: |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4855 | QualType T = Entity.getType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4856 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4857 | // -- if T is an array type, then each element is value-initialized; |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4858 | T = S.Context.getBaseElementType(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4859 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4860 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 4861 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4862 | bool NeedZeroInitialization = true; |
Richard Smith | 505ef81 | 2016-12-21 01:57:02 +0000 | [diff] [blame] | 4863 | // C++98: |
| 4864 | // -- if T is a class type (clause 9) with a user-declared constructor |
| 4865 | // (12.1), then the default constructor for T is called (and the |
| 4866 | // initialization is ill-formed if T has no accessible default |
| 4867 | // constructor); |
| 4868 | // C++11: |
| 4869 | // -- if T is a class type (clause 9) with either no default constructor |
| 4870 | // (12.1 [class.ctor]) or a default constructor that is user-provided |
| 4871 | // or deleted, then the object is default-initialized; |
| 4872 | // |
| 4873 | // Note that the C++11 rule is the same as the C++98 rule if there are no |
| 4874 | // defaulted or deleted constructors, so we just use it unconditionally. |
| 4875 | CXXConstructorDecl *CD = S.LookupDefaultConstructor(ClassDecl); |
| 4876 | if (!CD || !CD->getCanonicalDecl()->isDefaulted() || CD->isDeleted()) |
| 4877 | NeedZeroInitialization = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4878 | |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4879 | // -- if T is a (possibly cv-qualified) non-union class type without a |
| 4880 | // user-provided or deleted default constructor, then the object is |
| 4881 | // zero-initialized and, if T has a non-trivial default constructor, |
| 4882 | // default-initialized; |
Richard Smith | fb26652 | 2012-10-18 00:44:17 +0000 | [diff] [blame] | 4883 | // The 'non-union' here was removed by DR1502. The 'non-trivial default |
| 4884 | // constructor' part was removed by DR1507. |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4885 | if (NeedZeroInitialization) |
| 4886 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 4887 | |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 4888 | // C++03: |
| 4889 | // -- if T is a non-union class type without a user-declared constructor, |
| 4890 | // then every non-static data member and base class component of T is |
| 4891 | // value-initialized; |
| 4892 | // [...] A program that calls for [...] value-initialization of an |
| 4893 | // entity of reference type is ill-formed. |
| 4894 | // |
| 4895 | // C++11 doesn't need this handling, because value-initialization does not |
| 4896 | // occur recursively there, and the implicit default constructor is |
| 4897 | // defined as deleted in the problematic cases. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4898 | if (!S.getLangOpts().CPlusPlus11 && |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 4899 | ClassDecl->hasUninitializedReferenceMember()) { |
| 4900 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForReference); |
| 4901 | return; |
| 4902 | } |
| 4903 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4904 | // If this is list-value-initialization, pass the empty init list on when |
| 4905 | // building the constructor call. This affects the semantics of a few |
| 4906 | // things (such as whether an explicit default constructor can be called). |
| 4907 | Expr *InitListAsExpr = InitList; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4908 | MultiExprArg Args(&InitListAsExpr, InitList ? 1 : 0); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4909 | bool InitListSyntax = InitList; |
| 4910 | |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 4911 | // FIXME: Instead of creating a CXXConstructExpr of array type here, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 4912 | // wrap a class-typed CXXConstructExpr in an ArrayInitLoopExpr. |
| 4913 | return TryConstructorInitialization( |
| 4914 | S, Entity, Kind, Args, T, Entity.getType(), Sequence, InitListSyntax); |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4915 | } |
| 4916 | } |
| 4917 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4918 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4919 | } |
| 4920 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4921 | /// Attempt default initialization (C++ [dcl.init]p6). |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4922 | static void TryDefaultInitialization(Sema &S, |
| 4923 | const InitializedEntity &Entity, |
| 4924 | const InitializationKind &Kind, |
| 4925 | InitializationSequence &Sequence) { |
| 4926 | assert(Kind.getKind() == InitializationKind::IK_Default); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4927 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4928 | // C++ [dcl.init]p6: |
| 4929 | // To default-initialize an object of type T means: |
| 4930 | // - if T is an array type, each element is default-initialized; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4931 | QualType DestType = S.Context.getBaseElementType(Entity.getType()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4932 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4933 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 4934 | // constructor for T is called (and the initialization is ill-formed if |
| 4935 | // T has no accessible default constructor); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4936 | if (DestType->isRecordType() && S.getLangOpts().CPlusPlus) { |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 4937 | TryConstructorInitialization(S, Entity, Kind, None, DestType, |
| 4938 | Entity.getType(), Sequence); |
Chandler Carruth | c926240 | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 4939 | return; |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4940 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4941 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4942 | // - otherwise, no initialization is performed. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4943 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4944 | // If a program calls for the default initialization of an object of |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4945 | // 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] | 4946 | // default constructor. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4947 | if (DestType.isConstQualified() && S.getLangOpts().CPlusPlus) { |
Nico Weber | 337d5aa | 2015-04-17 08:32:38 +0000 | [diff] [blame] | 4948 | if (!maybeRecoverWithZeroInitialization(S, Sequence, Entity)) |
| 4949 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4950 | return; |
| 4951 | } |
| 4952 | |
| 4953 | // If the destination type has a lifetime property, zero-initialize it. |
| 4954 | if (DestType.getQualifiers().hasObjCLifetime()) { |
| 4955 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 4956 | return; |
| 4957 | } |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4958 | } |
| 4959 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4960 | /// Attempt a user-defined conversion between two types (C++ [dcl.init]), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4961 | /// which enumerates all conversion functions and performs overload resolution |
| 4962 | /// to select the best. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4963 | static void TryUserDefinedConversion(Sema &S, |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 4964 | QualType DestType, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4965 | const InitializationKind &Kind, |
| 4966 | Expr *Initializer, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4967 | InitializationSequence &Sequence, |
| 4968 | bool TopLevelOfInitList) { |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4969 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 4970 | QualType SourceType = Initializer->getType(); |
| 4971 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 4972 | "Must have a class type to perform a user-defined conversion"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4973 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4974 | // Build the candidate set directly in the initialization sequence |
| 4975 | // structure, so that it will persist if we fail. |
| 4976 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 4977 | CandidateSet.clear(OverloadCandidateSet::CSK_InitByUserDefinedConversion); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4978 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4979 | // Determine whether we are allowed to call explicit constructors or |
| 4980 | // explicit conversion operators. |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 4981 | bool AllowExplicit = Kind.AllowExplicit(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4982 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4983 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 4984 | // The type we're converting to is a class type. Enumerate its constructors |
| 4985 | // to see if there is a suitable conversion. |
| 4986 | CXXRecordDecl *DestRecordDecl |
| 4987 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4988 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4989 | // Try to complete the type we're converting to. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4990 | if (S.isCompleteType(Kind.getLocation(), DestType)) { |
Richard Smith | 776e9c3 | 2017-02-01 03:28:59 +0000 | [diff] [blame] | 4991 | for (NamedDecl *D : S.LookupConstructors(DestRecordDecl)) { |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4992 | auto Info = getConstructorInfo(D); |
| 4993 | if (!Info.Constructor) |
| 4994 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4995 | |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4996 | if (!Info.Constructor->isInvalidDecl() && |
| 4997 | Info.Constructor->isConvertingConstructor(AllowExplicit)) { |
| 4998 | if (Info.ConstructorTmpl) |
| 4999 | S.AddTemplateOverloadCandidate(Info.ConstructorTmpl, Info.FoundDecl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5000 | /*ExplicitArgs*/ nullptr, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5001 | Initializer, CandidateSet, |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 5002 | /*SuppressUserConversions=*/true); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 5003 | else |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 5004 | S.AddOverloadCandidate(Info.Constructor, Info.FoundDecl, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5005 | Initializer, CandidateSet, |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 5006 | /*SuppressUserConversions=*/true); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 5007 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5008 | } |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 5009 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5010 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5011 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5012 | SourceLocation DeclLoc = Initializer->getBeginLoc(); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5013 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5014 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 5015 | // The type we're converting from is a class type, enumerate its conversion |
| 5016 | // functions. |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5017 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 5018 | // We can only enumerate the conversion functions for a complete type; if |
| 5019 | // the type isn't complete, simply skip this step. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 5020 | if (S.isCompleteType(DeclLoc, SourceType)) { |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 5021 | CXXRecordDecl *SourceRecordDecl |
| 5022 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5023 | |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 5024 | const auto &Conversions = |
| 5025 | SourceRecordDecl->getVisibleConversionFunctions(); |
| 5026 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 5027 | NamedDecl *D = *I; |
| 5028 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 5029 | if (isa<UsingShadowDecl>(D)) |
| 5030 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5031 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 5032 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 5033 | CXXConversionDecl *Conv; |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5034 | if (ConvTemplate) |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 5035 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5036 | else |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 5037 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5038 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 5039 | if (AllowExplicit || !Conv->isExplicit()) { |
| 5040 | if (ConvTemplate) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5041 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 5042 | ActingDC, Initializer, DestType, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 5043 | CandidateSet, AllowExplicit); |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 5044 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5045 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 5046 | Initializer, DestType, CandidateSet, |
| 5047 | AllowExplicit); |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 5048 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5049 | } |
| 5050 | } |
| 5051 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5052 | |
| 5053 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5054 | OverloadCandidateSet::iterator Best; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5055 | if (OverloadingResult Result |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 5056 | = CandidateSet.BestViableFunction(S, DeclLoc, Best)) { |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5057 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5058 | InitializationSequence::FK_UserConversionOverloadFailed, |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5059 | Result); |
| 5060 | return; |
| 5061 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5062 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5063 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 5064 | Function->setReferenced(); |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 5065 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5066 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5067 | if (isa<CXXConstructorDecl>(Function)) { |
| 5068 | // Add the user-defined conversion step. Any cv-qualification conversion is |
Richard Smith | b24f067 | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 5069 | // subsumed by the initialization. Per DR5, the created temporary is of the |
| 5070 | // cv-unqualified type of the destination. |
| 5071 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
| 5072 | DestType.getUnqualifiedType(), |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 5073 | HadMultipleCandidates); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5074 | |
| 5075 | // C++14 and before: |
| 5076 | // - if the function is a constructor, the call initializes a temporary |
| 5077 | // of the cv-unqualified version of the destination type. The [...] |
| 5078 | // temporary [...] is then used to direct-initialize, according to the |
| 5079 | // rules above, the object that is the destination of the |
| 5080 | // copy-initialization. |
| 5081 | // Note that this just performs a simple object copy from the temporary. |
| 5082 | // |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 5083 | // C++17: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5084 | // - if the function is a constructor, the call is a prvalue of the |
| 5085 | // cv-unqualified version of the destination type whose return object |
| 5086 | // is initialized by the constructor. The call is used to |
| 5087 | // direct-initialize, according to the rules above, the object that |
| 5088 | // is the destination of the copy-initialization. |
| 5089 | // Therefore we need to do nothing further. |
| 5090 | // |
| 5091 | // FIXME: Mark this copy as extraneous. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 5092 | if (!S.getLangOpts().CPlusPlus17) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5093 | Sequence.AddFinalCopy(DestType); |
Richard Smith | 16d3150 | 2016-12-21 01:31:56 +0000 | [diff] [blame] | 5094 | else if (DestType.hasQualifiers()) |
| 5095 | Sequence.AddQualificationConversionStep(DestType, VK_RValue); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5096 | return; |
| 5097 | } |
| 5098 | |
| 5099 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 5100 | QualType ConvType = Function->getCallResultType(); |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 5101 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType, |
| 5102 | HadMultipleCandidates); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5103 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5104 | if (ConvType->getAs<RecordType>()) { |
| 5105 | // The call is used to direct-initialize [...] the object that is the |
| 5106 | // destination of the copy-initialization. |
| 5107 | // |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 5108 | // 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] | 5109 | // - If the initializer expression is a prvalue and the cv-unqualified |
| 5110 | // version of the source type is the same as the class of the |
| 5111 | // destination [... do not make an extra copy] |
| 5112 | // |
| 5113 | // FIXME: Mark this copy as extraneous. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 5114 | if (!S.getLangOpts().CPlusPlus17 || |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5115 | Function->getReturnType()->isReferenceType() || |
| 5116 | !S.Context.hasSameUnqualifiedType(ConvType, DestType)) |
| 5117 | Sequence.AddFinalCopy(DestType); |
Richard Smith | 16d3150 | 2016-12-21 01:31:56 +0000 | [diff] [blame] | 5118 | else if (!S.Context.hasSameType(ConvType, DestType)) |
| 5119 | Sequence.AddQualificationConversionStep(DestType, VK_RValue); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5120 | return; |
| 5121 | } |
| 5122 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5123 | // If the conversion following the call to the conversion function |
| 5124 | // is interesting, add it as a separate step. |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5125 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 5126 | Best->FinalConversion.Third) { |
| 5127 | ImplicitConversionSequence ICS; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5128 | ICS.setStandard(); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5129 | ICS.Standard = Best->FinalConversion; |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5130 | Sequence.AddConversionSequenceStep(ICS, DestType, TopLevelOfInitList); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5131 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5132 | } |
| 5133 | |
Richard Smith | f032001b | 2013-06-20 02:18:31 +0000 | [diff] [blame] | 5134 | /// An egregious hack for compatibility with libstdc++-4.2: in <tr1/hashtable>, |
| 5135 | /// a function with a pointer return type contains a 'return false;' statement. |
| 5136 | /// In C++11, 'false' is not a null pointer, so this breaks the build of any |
| 5137 | /// code using that header. |
| 5138 | /// |
| 5139 | /// Work around this by treating 'return false;' as zero-initializing the result |
| 5140 | /// if it's used in a pointer-returning function in a system header. |
| 5141 | static bool isLibstdcxxPointerReturnFalseHack(Sema &S, |
| 5142 | const InitializedEntity &Entity, |
| 5143 | const Expr *Init) { |
| 5144 | return S.getLangOpts().CPlusPlus11 && |
| 5145 | Entity.getKind() == InitializedEntity::EK_Result && |
| 5146 | Entity.getType()->isPointerType() && |
| 5147 | isa<CXXBoolLiteralExpr>(Init) && |
| 5148 | !cast<CXXBoolLiteralExpr>(Init)->getValue() && |
| 5149 | S.getSourceManager().isInSystemHeader(Init->getExprLoc()); |
| 5150 | } |
| 5151 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5152 | /// The non-zero enum values here are indexes into diagnostic alternatives. |
| 5153 | enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar }; |
| 5154 | |
| 5155 | /// Determines whether this expression is an acceptable ICR source. |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 5156 | static InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e, |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5157 | bool isAddressOf, bool &isWeakAccess) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5158 | // Skip parens. |
| 5159 | e = e->IgnoreParens(); |
| 5160 | |
| 5161 | // Skip address-of nodes. |
| 5162 | if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 5163 | if (op->getOpcode() == UO_AddrOf) |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5164 | return isInvalidICRSource(C, op->getSubExpr(), /*addressof*/ true, |
| 5165 | isWeakAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5166 | |
| 5167 | // Skip certain casts. |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 5168 | } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 5169 | switch (ce->getCastKind()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5170 | case CK_Dependent: |
| 5171 | case CK_BitCast: |
| 5172 | case CK_LValueBitCast: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5173 | case CK_NoOp: |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5174 | return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf, isWeakAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5175 | |
| 5176 | case CK_ArrayToPointerDecay: |
| 5177 | return IIK_nonscalar; |
| 5178 | |
| 5179 | case CK_NullToPointer: |
| 5180 | return IIK_okay; |
| 5181 | |
| 5182 | default: |
| 5183 | break; |
| 5184 | } |
| 5185 | |
| 5186 | // 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] | 5187 | } else if (isa<DeclRefExpr>(e)) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5188 | // set isWeakAccess to true, to mean that there will be an implicit |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5189 | // load which requires a cleanup. |
| 5190 | if (e->getType().getObjCLifetime() == Qualifiers::OCL_Weak) |
| 5191 | isWeakAccess = true; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5192 | |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 5193 | if (!isAddressOf) return IIK_nonlocal; |
| 5194 | |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5195 | VarDecl *var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl()); |
| 5196 | if (!var) return IIK_nonlocal; |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 5197 | |
| 5198 | return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5199 | |
| 5200 | // If we have a conditional operator, check both sides. |
| 5201 | } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) { |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5202 | if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf, |
| 5203 | isWeakAccess)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5204 | return iik; |
| 5205 | |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5206 | return isInvalidICRSource(C, cond->getRHS(), isAddressOf, isWeakAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5207 | |
| 5208 | // These are never scalar. |
| 5209 | } else if (isa<ArraySubscriptExpr>(e)) { |
| 5210 | return IIK_nonscalar; |
| 5211 | |
| 5212 | // Otherwise, it needs to be a null pointer constant. |
| 5213 | } else { |
| 5214 | return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull) |
| 5215 | ? IIK_okay : IIK_nonlocal); |
| 5216 | } |
| 5217 | |
| 5218 | return IIK_nonlocal; |
| 5219 | } |
| 5220 | |
| 5221 | /// Check whether the given expression is a valid operand for an |
| 5222 | /// indirect copy/restore. |
| 5223 | static void checkIndirectCopyRestoreSource(Sema &S, Expr *src) { |
| 5224 | assert(src->isRValue()); |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5225 | bool isWeakAccess = false; |
| 5226 | InvalidICRKind iik = isInvalidICRSource(S.Context, src, false, isWeakAccess); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5227 | // If isWeakAccess to true, there will be an implicit |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5228 | // load which requires a cleanup. |
| 5229 | if (S.getLangOpts().ObjCAutoRefCount && isWeakAccess) |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 5230 | S.Cleanup.setExprNeedsCleanups(true); |
| 5231 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5232 | if (iik == IIK_okay) return; |
| 5233 | |
| 5234 | S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback) |
| 5235 | << ((unsigned) iik - 1) // shift index into diagnostic explanations |
| 5236 | << src->getSourceRange(); |
| 5237 | } |
| 5238 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5239 | /// Determine whether we have compatible array types for the |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5240 | /// purposes of GNU by-copy array initialization. |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 5241 | static bool hasCompatibleArrayTypes(ASTContext &Context, const ArrayType *Dest, |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5242 | const ArrayType *Source) { |
| 5243 | // If the source and destination array types are equivalent, we're |
| 5244 | // done. |
| 5245 | if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0))) |
| 5246 | return true; |
| 5247 | |
| 5248 | // Make sure that the element types are the same. |
| 5249 | if (!Context.hasSameType(Dest->getElementType(), Source->getElementType())) |
| 5250 | return false; |
| 5251 | |
| 5252 | // The only mismatch we allow is when the destination is an |
| 5253 | // incomplete array type and the source is a constant array type. |
| 5254 | return Source->isConstantArrayType() && Dest->isIncompleteArrayType(); |
| 5255 | } |
| 5256 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5257 | static bool tryObjCWritebackConversion(Sema &S, |
| 5258 | InitializationSequence &Sequence, |
| 5259 | const InitializedEntity &Entity, |
| 5260 | Expr *Initializer) { |
| 5261 | bool ArrayDecay = false; |
| 5262 | QualType ArgType = Initializer->getType(); |
| 5263 | QualType ArgPointee; |
| 5264 | if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) { |
| 5265 | ArrayDecay = true; |
| 5266 | ArgPointee = ArgArrayType->getElementType(); |
| 5267 | ArgType = S.Context.getPointerType(ArgPointee); |
| 5268 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5269 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5270 | // Handle write-back conversion. |
| 5271 | QualType ConvertedArgType; |
| 5272 | if (!S.isObjCWritebackConversion(ArgType, Entity.getType(), |
| 5273 | ConvertedArgType)) |
| 5274 | return false; |
| 5275 | |
| 5276 | // We should copy unless we're passing to an argument explicitly |
| 5277 | // marked 'out'. |
| 5278 | bool ShouldCopy = true; |
| 5279 | if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 5280 | ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 5281 | |
| 5282 | // Do we need an lvalue conversion? |
| 5283 | if (ArrayDecay || Initializer->isGLValue()) { |
| 5284 | ImplicitConversionSequence ICS; |
| 5285 | ICS.setStandard(); |
| 5286 | ICS.Standard.setAsIdentityConversion(); |
| 5287 | |
| 5288 | QualType ResultType; |
| 5289 | if (ArrayDecay) { |
| 5290 | ICS.Standard.First = ICK_Array_To_Pointer; |
| 5291 | ResultType = S.Context.getPointerType(ArgPointee); |
| 5292 | } else { |
| 5293 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
| 5294 | ResultType = Initializer->getType().getNonLValueExprType(S.Context); |
| 5295 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5296 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5297 | Sequence.AddConversionSequenceStep(ICS, ResultType); |
| 5298 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5299 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5300 | Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
| 5301 | return true; |
| 5302 | } |
| 5303 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5304 | static bool TryOCLSamplerInitialization(Sema &S, |
| 5305 | InitializationSequence &Sequence, |
| 5306 | QualType DestType, |
| 5307 | Expr *Initializer) { |
| 5308 | if (!S.getLangOpts().OpenCL || !DestType->isSamplerT() || |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 5309 | (!Initializer->isIntegerConstantExpr(S.Context) && |
| 5310 | !Initializer->getType()->isSamplerT())) |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5311 | return false; |
| 5312 | |
| 5313 | Sequence.AddOCLSamplerInitStep(DestType); |
| 5314 | return true; |
| 5315 | } |
| 5316 | |
Andrew Savonichev | 3fee351 | 2018-11-08 11:25:41 +0000 | [diff] [blame] | 5317 | static bool IsZeroInitializer(Expr *Initializer, Sema &S) { |
| 5318 | return Initializer->isIntegerConstantExpr(S.getASTContext()) && |
| 5319 | (Initializer->EvaluateKnownConstInt(S.getASTContext()) == 0); |
| 5320 | } |
| 5321 | |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 5322 | static bool TryOCLZeroOpaqueTypeInitialization(Sema &S, |
| 5323 | InitializationSequence &Sequence, |
| 5324 | QualType DestType, |
| 5325 | Expr *Initializer) { |
| 5326 | if (!S.getLangOpts().OpenCL) |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5327 | return false; |
| 5328 | |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 5329 | // |
| 5330 | // OpenCL 1.2 spec, s6.12.10 |
| 5331 | // |
| 5332 | // The event argument can also be used to associate the |
| 5333 | // async_work_group_copy with a previous async copy allowing |
| 5334 | // an event to be shared by multiple async copies; otherwise |
| 5335 | // event should be zero. |
| 5336 | // |
| 5337 | if (DestType->isEventT() || DestType->isQueueT()) { |
Andrew Savonichev | 3fee351 | 2018-11-08 11:25:41 +0000 | [diff] [blame] | 5338 | if (!IsZeroInitializer(Initializer, S)) |
| 5339 | return false; |
| 5340 | |
| 5341 | Sequence.AddOCLZeroOpaqueTypeStep(DestType); |
| 5342 | return true; |
| 5343 | } |
| 5344 | |
| 5345 | // We should allow zero initialization for all types defined in the |
| 5346 | // cl_intel_device_side_avc_motion_estimation extension, except |
| 5347 | // intel_sub_group_avc_mce_payload_t and intel_sub_group_avc_mce_result_t. |
| 5348 | if (S.getOpenCLOptions().isEnabled( |
| 5349 | "cl_intel_device_side_avc_motion_estimation") && |
| 5350 | DestType->isOCLIntelSubgroupAVCType()) { |
| 5351 | if (DestType->isOCLIntelSubgroupAVCMcePayloadType() || |
| 5352 | DestType->isOCLIntelSubgroupAVCMceResultType()) |
| 5353 | return false; |
| 5354 | if (!IsZeroInitializer(Initializer, S)) |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 5355 | return false; |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5356 | |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 5357 | Sequence.AddOCLZeroOpaqueTypeStep(DestType); |
| 5358 | return true; |
| 5359 | } |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 5360 | |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 5361 | return false; |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 5362 | } |
| 5363 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5364 | InitializationSequence::InitializationSequence(Sema &S, |
| 5365 | const InitializedEntity &Entity, |
| 5366 | const InitializationKind &Kind, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5367 | MultiExprArg Args, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 5368 | bool TopLevelOfInitList, |
| 5369 | bool TreatUnavailableAsInvalid) |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 5370 | : FailedCandidateSet(Kind.getLocation(), OverloadCandidateSet::CSK_Normal) { |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 5371 | InitializeFrom(S, Entity, Kind, Args, TopLevelOfInitList, |
| 5372 | TreatUnavailableAsInvalid); |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 5373 | } |
| 5374 | |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 5375 | /// Tries to get a FunctionDecl out of `E`. If it succeeds and we can take the |
| 5376 | /// address of that function, this returns true. Otherwise, it returns false. |
| 5377 | static bool isExprAnUnaddressableFunction(Sema &S, const Expr *E) { |
| 5378 | auto *DRE = dyn_cast<DeclRefExpr>(E); |
| 5379 | if (!DRE || !isa<FunctionDecl>(DRE->getDecl())) |
| 5380 | return false; |
| 5381 | |
| 5382 | return !S.checkAddressOfFunctionIsAvailable( |
| 5383 | cast<FunctionDecl>(DRE->getDecl())); |
| 5384 | } |
| 5385 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5386 | /// Determine whether we can perform an elementwise array copy for this kind |
| 5387 | /// of entity. |
| 5388 | static bool canPerformArrayCopy(const InitializedEntity &Entity) { |
| 5389 | switch (Entity.getKind()) { |
| 5390 | case InitializedEntity::EK_LambdaCapture: |
| 5391 | // C++ [expr.prim.lambda]p24: |
| 5392 | // For array members, the array elements are direct-initialized in |
| 5393 | // increasing subscript order. |
| 5394 | return true; |
| 5395 | |
| 5396 | case InitializedEntity::EK_Variable: |
| 5397 | // C++ [dcl.decomp]p1: |
| 5398 | // [...] each element is copy-initialized or direct-initialized from the |
| 5399 | // corresponding element of the assignment-expression [...] |
| 5400 | return isa<DecompositionDecl>(Entity.getDecl()); |
| 5401 | |
| 5402 | case InitializedEntity::EK_Member: |
| 5403 | // C++ [class.copy.ctor]p14: |
| 5404 | // - if the member is an array, each element is direct-initialized with |
| 5405 | // the corresponding subobject of x |
| 5406 | return Entity.isImplicitMemberInitializer(); |
| 5407 | |
| 5408 | case InitializedEntity::EK_ArrayElement: |
| 5409 | // All the above cases are intended to apply recursively, even though none |
| 5410 | // of them actually say that. |
| 5411 | if (auto *E = Entity.getParent()) |
| 5412 | return canPerformArrayCopy(*E); |
| 5413 | break; |
| 5414 | |
| 5415 | default: |
| 5416 | break; |
| 5417 | } |
| 5418 | |
| 5419 | return false; |
| 5420 | } |
| 5421 | |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 5422 | void InitializationSequence::InitializeFrom(Sema &S, |
| 5423 | const InitializedEntity &Entity, |
| 5424 | const InitializationKind &Kind, |
| 5425 | MultiExprArg Args, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 5426 | bool TopLevelOfInitList, |
| 5427 | bool TreatUnavailableAsInvalid) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5428 | ASTContext &Context = S.Context; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5429 | |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 5430 | // Eliminate non-overload placeholder types in the arguments. We |
| 5431 | // need to do this before checking whether types are dependent |
| 5432 | // because lowering a pseudo-object expression might well give us |
| 5433 | // something of dependent type. |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5434 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 5435 | if (Args[I]->getType()->isNonOverloadPlaceholderType()) { |
| 5436 | // FIXME: should we be doing this here? |
| 5437 | ExprResult result = S.CheckPlaceholderExpr(Args[I]); |
| 5438 | if (result.isInvalid()) { |
| 5439 | SetFailed(FK_PlaceholderType); |
| 5440 | return; |
| 5441 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5442 | Args[I] = result.get(); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 5443 | } |
| 5444 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5445 | // C++0x [dcl.init]p16: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5446 | // The semantics of initializers are as follows. The destination type is |
| 5447 | // the type of the object or reference being initialized and the source |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5448 | // 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] | 5449 | // 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] | 5450 | // parenthesized list of expressions. |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5451 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5452 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5453 | if (DestType->isDependentType() || |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5454 | Expr::hasAnyTypeDependentArguments(Args)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5455 | SequenceKind = DependentSequence; |
| 5456 | return; |
| 5457 | } |
| 5458 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5459 | // Almost everything is a normal sequence. |
| 5460 | setSequenceKind(NormalSequence); |
| 5461 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5462 | QualType SourceType; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5463 | Expr *Initializer = nullptr; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5464 | if (Args.size() == 1) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5465 | Initializer = Args[0]; |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 5466 | if (S.getLangOpts().ObjC) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5467 | if (S.CheckObjCBridgeRelatedConversions(Initializer->getBeginLoc(), |
Fariborz Jahanian | 283bf89 | 2013-12-18 21:04:43 +0000 | [diff] [blame] | 5468 | DestType, Initializer->getType(), |
| 5469 | Initializer) || |
| 5470 | S.ConversionToObjCStringLiteralCheck(DestType, Initializer)) |
| 5471 | Args[0] = Initializer; |
Fariborz Jahanian | 283bf89 | 2013-12-18 21:04:43 +0000 | [diff] [blame] | 5472 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5473 | if (!isa<InitListExpr>(Initializer)) |
| 5474 | SourceType = Initializer->getType(); |
| 5475 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5476 | |
Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 5477 | // - If the initializer is a (non-parenthesized) braced-init-list, the |
| 5478 | // object is list-initialized (8.5.4). |
| 5479 | if (Kind.getKind() != InitializationKind::IK_Direct) { |
| 5480 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 5481 | TryListInitialization(S, Entity, Kind, InitList, *this, |
| 5482 | TreatUnavailableAsInvalid); |
Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 5483 | return; |
| 5484 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5485 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5486 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5487 | // - If the destination type is a reference type, see 8.5.3. |
| 5488 | if (DestType->isReferenceType()) { |
| 5489 | // C++0x [dcl.init.ref]p1: |
| 5490 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 5491 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 5492 | // by an object that can be converted into a T. |
| 5493 | // (Therefore, multiple arguments are not permitted.) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5494 | if (Args.size() != 1) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5495 | SetFailed(FK_TooManyInitsForReference); |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 5496 | // C++17 [dcl.init.ref]p5: |
| 5497 | // A reference [...] is initialized by an expression [...] as follows: |
| 5498 | // If the initializer is not an expression, presumably we should reject, |
| 5499 | // but the standard fails to actually say so. |
| 5500 | else if (isa<InitListExpr>(Args[0])) |
| 5501 | SetFailed(FK_ParenthesizedListInitForReference); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5502 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5503 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5504 | return; |
| 5505 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5506 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5507 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5508 | if (Kind.getKind() == InitializationKind::IK_Value || |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5509 | (Kind.getKind() == InitializationKind::IK_Direct && Args.empty())) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5510 | TryValueInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5511 | return; |
| 5512 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5513 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5514 | // Handle default initialization. |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 5515 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5516 | TryDefaultInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5517 | return; |
| 5518 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5519 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 5520 | // - If the destination type is an array of characters, an array of |
| 5521 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 5522 | // initializer is a string literal, see 8.5.2. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5523 | // - Otherwise, if the destination type is an array, the program is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5524 | // ill-formed. |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5525 | if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) { |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 5526 | if (Initializer && isa<VariableArrayType>(DestAT)) { |
| 5527 | SetFailed(FK_VariableLengthArrayHasInitializer); |
| 5528 | return; |
| 5529 | } |
| 5530 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 5531 | if (Initializer) { |
| 5532 | switch (IsStringInit(Initializer, DestAT, Context)) { |
| 5533 | case SIF_None: |
| 5534 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
| 5535 | return; |
| 5536 | case SIF_NarrowStringIntoWideChar: |
| 5537 | SetFailed(FK_NarrowStringIntoWideCharArray); |
| 5538 | return; |
| 5539 | case SIF_WideStringIntoChar: |
| 5540 | SetFailed(FK_WideStringIntoCharArray); |
| 5541 | return; |
| 5542 | case SIF_IncompatWideStringIntoWideChar: |
| 5543 | SetFailed(FK_IncompatWideStringIntoWideChar); |
| 5544 | return; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 5545 | case SIF_PlainStringIntoUTF8Char: |
| 5546 | SetFailed(FK_PlainStringIntoUTF8Char); |
| 5547 | return; |
| 5548 | case SIF_UTF8StringIntoPlainChar: |
| 5549 | SetFailed(FK_UTF8StringIntoPlainChar); |
| 5550 | return; |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 5551 | case SIF_Other: |
| 5552 | break; |
| 5553 | } |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 5554 | } |
| 5555 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5556 | // Some kinds of initialization permit an array to be initialized from |
| 5557 | // another array of the same type, and perform elementwise initialization. |
| 5558 | if (Initializer && isa<ConstantArrayType>(DestAT) && |
| 5559 | S.Context.hasSameUnqualifiedType(Initializer->getType(), |
| 5560 | Entity.getType()) && |
| 5561 | canPerformArrayCopy(Entity)) { |
| 5562 | // If source is a prvalue, use it directly. |
| 5563 | if (Initializer->getValueKind() == VK_RValue) { |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 5564 | AddArrayInitStep(DestType, /*IsGNUExtension*/false); |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5565 | return; |
| 5566 | } |
| 5567 | |
| 5568 | // Emit element-at-a-time copy loop. |
| 5569 | InitializedEntity Element = |
| 5570 | InitializedEntity::InitializeElement(S.Context, 0, Entity); |
| 5571 | QualType InitEltT = |
| 5572 | Context.getAsArrayType(Initializer->getType())->getElementType(); |
Richard Smith | 30e304e | 2016-12-14 00:03:17 +0000 | [diff] [blame] | 5573 | OpaqueValueExpr OVE(Initializer->getExprLoc(), InitEltT, |
| 5574 | Initializer->getValueKind(), |
| 5575 | Initializer->getObjectKind()); |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5576 | Expr *OVEAsExpr = &OVE; |
| 5577 | InitializeFrom(S, Element, Kind, OVEAsExpr, TopLevelOfInitList, |
| 5578 | TreatUnavailableAsInvalid); |
| 5579 | if (!Failed()) |
| 5580 | AddArrayInitLoopStep(Entity.getType(), InitEltT); |
| 5581 | return; |
| 5582 | } |
| 5583 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5584 | // Note: as an GNU C extension, we allow initialization of an |
| 5585 | // array from a compound literal that creates an array of the same |
| 5586 | // type, so long as the initializer has no side effects. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5587 | if (!S.getLangOpts().CPlusPlus && Initializer && |
Eli Friedman | 88fccbd | 2019-02-11 22:54:27 +0000 | [diff] [blame] | 5588 | isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) && |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5589 | Initializer->getType()->isArrayType()) { |
| 5590 | const ArrayType *SourceAT |
| 5591 | = Context.getAsArrayType(Initializer->getType()); |
| 5592 | if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5593 | SetFailed(FK_ArrayTypeMismatch); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5594 | else if (Initializer->HasSideEffects(S.Context)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5595 | SetFailed(FK_NonConstantArrayInit); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5596 | else { |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 5597 | AddArrayInitStep(DestType, /*IsGNUExtension*/true); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5598 | } |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5599 | } |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5600 | // Note: as a GNU C++ extension, we allow list-initialization of a |
| 5601 | // class member of array type from a parenthesized initializer list. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5602 | else if (S.getLangOpts().CPlusPlus && |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5603 | Entity.getKind() == InitializedEntity::EK_Member && |
| 5604 | Initializer && isa<InitListExpr>(Initializer)) { |
| 5605 | TryListInitialization(S, Entity, Kind, cast<InitListExpr>(Initializer), |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 5606 | *this, TreatUnavailableAsInvalid); |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5607 | AddParenthesizedArrayInitStep(DestType); |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 5608 | } else if (DestAT->getElementType()->isCharType()) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5609 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 5610 | else if (IsWideCharCompatible(DestAT->getElementType(), Context)) |
| 5611 | SetFailed(FK_ArrayNeedsInitListOrWideStringLiteral); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5612 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5613 | SetFailed(FK_ArrayNeedsInitList); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5614 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5615 | return; |
| 5616 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5617 | |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 5618 | // Determine whether we should consider writeback conversions for |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5619 | // Objective-C ARC. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5620 | bool allowObjCWritebackConversion = S.getLangOpts().ObjCAutoRefCount && |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5621 | Entity.isParameterKind(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5622 | |
| 5623 | // We're at the end of the line for C: it's either a write-back conversion |
| 5624 | // 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] | 5625 | if (!S.getLangOpts().CPlusPlus) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5626 | // If allowed, check whether this is an Objective-C writeback conversion. |
| 5627 | if (allowObjCWritebackConversion && |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5628 | tryObjCWritebackConversion(S, *this, Entity, Initializer)) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5629 | return; |
| 5630 | } |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5631 | |
| 5632 | if (TryOCLSamplerInitialization(S, *this, DestType, Initializer)) |
| 5633 | return; |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5634 | |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 5635 | if (TryOCLZeroOpaqueTypeInitialization(S, *this, DestType, Initializer)) |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5636 | return; |
| 5637 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5638 | // Handle initialization in C |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5639 | AddCAssignmentStep(DestType); |
| 5640 | MaybeProduceObjCObject(S, *this, Entity); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5641 | return; |
| 5642 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5643 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5644 | assert(S.getLangOpts().CPlusPlus); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 5645 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5646 | // - If the destination type is a (possibly cv-qualified) class type: |
| 5647 | if (DestType->isRecordType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5648 | // - If the initialization is direct-initialization, or if it is |
| 5649 | // copy-initialization where the cv-unqualified version of the |
| 5650 | // 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] | 5651 | // class of the destination, constructors are considered. [...] |
| 5652 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 5653 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 5654 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5655 | S.IsDerivedFrom(Initializer->getBeginLoc(), SourceType, DestType)))) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5656 | TryConstructorInitialization(S, Entity, Kind, Args, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5657 | DestType, DestType, *this); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5658 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5659 | // user-defined conversion sequences that can convert from the source |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5660 | // type to the destination type or (when a conversion function is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5661 | // used) to a derived class thereof are enumerated as described in |
| 5662 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 5663 | // (13.3). |
| 5664 | else |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5665 | TryUserDefinedConversion(S, DestType, Kind, Initializer, *this, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5666 | TopLevelOfInitList); |
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 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 5670 | assert(Args.size() >= 1 && "Zero-argument case handled above"); |
| 5671 | |
| 5672 | // The remaining cases all need a source type. |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5673 | if (Args.size() > 1) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5674 | SetFailed(FK_TooManyInitsForScalar); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5675 | return; |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 5676 | } else if (isa<InitListExpr>(Args[0])) { |
| 5677 | SetFailed(FK_ParenthesizedListInitForScalar); |
| 5678 | return; |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5679 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5680 | |
| 5681 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5682 | // type, conversion functions are considered. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5683 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5684 | // For a conversion to _Atomic(T) from either T or a class type derived |
| 5685 | // from T, initialize the T object then convert to _Atomic type. |
| 5686 | bool NeedAtomicConversion = false; |
| 5687 | if (const AtomicType *Atomic = DestType->getAs<AtomicType>()) { |
| 5688 | if (Context.hasSameUnqualifiedType(SourceType, Atomic->getValueType()) || |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5689 | S.IsDerivedFrom(Initializer->getBeginLoc(), SourceType, |
Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 5690 | Atomic->getValueType())) { |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5691 | DestType = Atomic->getValueType(); |
| 5692 | NeedAtomicConversion = true; |
| 5693 | } |
| 5694 | } |
| 5695 | |
| 5696 | TryUserDefinedConversion(S, DestType, Kind, Initializer, *this, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5697 | TopLevelOfInitList); |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5698 | MaybeProduceObjCObject(S, *this, Entity); |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5699 | if (!Failed() && NeedAtomicConversion) |
| 5700 | AddAtomicConversionStep(Entity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5701 | return; |
| 5702 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5703 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5704 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5705 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5706 | // conversions (Clause 4) will be used, if necessary, to convert the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5707 | // initializer expression to the cv-unqualified version of the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5708 | // destination type; no user-defined conversions are considered. |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5709 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5710 | ImplicitConversionSequence ICS |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5711 | = S.TryImplicitConversion(Initializer, DestType, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5712 | /*SuppressUserConversions*/true, |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 5713 | /*AllowExplicitConversions*/ false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 5714 | /*InOverloadResolution*/ false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5715 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 5716 | allowObjCWritebackConversion); |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5717 | |
| 5718 | if (ICS.isStandard() && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5719 | ICS.Standard.Second == ICK_Writeback_Conversion) { |
| 5720 | // Objective-C ARC writeback conversion. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5721 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5722 | // We should copy unless we're passing to an argument explicitly |
| 5723 | // marked 'out'. |
| 5724 | bool ShouldCopy = true; |
| 5725 | if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 5726 | ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5727 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5728 | // If there was an lvalue adjustment, add it as a separate conversion. |
| 5729 | if (ICS.Standard.First == ICK_Array_To_Pointer || |
| 5730 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 5731 | ImplicitConversionSequence LvalueICS; |
| 5732 | LvalueICS.setStandard(); |
| 5733 | LvalueICS.Standard.setAsIdentityConversion(); |
| 5734 | LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0)); |
| 5735 | LvalueICS.Standard.First = ICS.Standard.First; |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5736 | AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5737 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5738 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5739 | AddPassByIndirectCopyRestoreStep(DestType, ShouldCopy); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5740 | } else if (ICS.isBad()) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 5741 | DeclAccessPair dap; |
Richard Smith | f032001b | 2013-06-20 02:18:31 +0000 | [diff] [blame] | 5742 | if (isLibstdcxxPointerReturnFalseHack(S, Entity, Initializer)) { |
| 5743 | AddZeroInitializationStep(Entity.getType()); |
| 5744 | } else if (Initializer->getType() == Context.OverloadTy && |
| 5745 | !S.ResolveAddressOfOverloadedFunction(Initializer, DestType, |
| 5746 | false, dap)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5747 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 5748 | else if (Initializer->getType()->isFunctionType() && |
| 5749 | isExprAnUnaddressableFunction(S, Initializer)) |
| 5750 | SetFailed(InitializationSequence::FK_AddressOfUnaddressableFunction); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 5751 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5752 | SetFailed(InitializationSequence::FK_ConversionFailed); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5753 | } else { |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5754 | AddConversionSequenceStep(ICS, DestType, TopLevelOfInitList); |
John McCall | fa27234 | 2011-06-16 23:24:51 +0000 | [diff] [blame] | 5755 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5756 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 5757 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5758 | } |
| 5759 | |
| 5760 | InitializationSequence::~InitializationSequence() { |
Davide Italiano | 67bb9f7 | 2015-07-01 21:51:58 +0000 | [diff] [blame] | 5761 | for (auto &S : Steps) |
| 5762 | S.Destroy(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5763 | } |
| 5764 | |
| 5765 | //===----------------------------------------------------------------------===// |
| 5766 | // Perform initialization |
| 5767 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5768 | static Sema::AssignmentAction |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 5769 | getAssignmentAction(const InitializedEntity &Entity, bool Diagnose = false) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5770 | switch(Entity.getKind()) { |
| 5771 | case InitializedEntity::EK_Variable: |
| 5772 | case InitializedEntity::EK_New: |
Douglas Gregor | 6dd3a6a | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 5773 | case InitializedEntity::EK_Exception: |
| 5774 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 5775 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5776 | return Sema::AA_Initializing; |
| 5777 | |
| 5778 | case InitializedEntity::EK_Parameter: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5779 | if (Entity.getDecl() && |
Douglas Gregor | 6b7f12c | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 5780 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 5781 | return Sema::AA_Sending; |
| 5782 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5783 | return Sema::AA_Passing; |
| 5784 | |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 5785 | case InitializedEntity::EK_Parameter_CF_Audited: |
| 5786 | if (Entity.getDecl() && |
| 5787 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 5788 | return Sema::AA_Sending; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5789 | |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 5790 | return !Diagnose ? Sema::AA_Passing : Sema::AA_Passing_CFAudited; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5791 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5792 | case InitializedEntity::EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 5793 | case InitializedEntity::EK_StmtExprResult: // FIXME: Not quite right. |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5794 | return Sema::AA_Returning; |
| 5795 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5796 | case InitializedEntity::EK_Temporary: |
Fariborz Jahanian | 14e9541 | 2013-07-11 19:13:34 +0000 | [diff] [blame] | 5797 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5798 | // FIXME: Can we tell apart casting vs. converting? |
| 5799 | return Sema::AA_Casting; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5800 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5801 | case InitializedEntity::EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 5802 | case InitializedEntity::EK_Binding: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 5803 | case InitializedEntity::EK_ArrayElement: |
| 5804 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 5805 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 5806 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 5807 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 5808 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5809 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5810 | return Sema::AA_Initializing; |
| 5811 | } |
| 5812 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 5813 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5814 | } |
| 5815 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5816 | /// Whether we should bind a created object as a temporary when |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5817 | /// initializing the given entity. |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5818 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5819 | switch (Entity.getKind()) { |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 5820 | case InitializedEntity::EK_ArrayElement: |
| 5821 | case InitializedEntity::EK_Member: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5822 | case InitializedEntity::EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 5823 | case InitializedEntity::EK_StmtExprResult: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5824 | case InitializedEntity::EK_New: |
| 5825 | case InitializedEntity::EK_Variable: |
| 5826 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 5827 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 5828 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 5829 | case InitializedEntity::EK_ComplexElement: |
Anders Carlsson | fcd764a | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 5830 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 5831 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 5832 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 5833 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5834 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5835 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5836 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5837 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5838 | case InitializedEntity::EK_Parameter_CF_Audited: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5839 | case InitializedEntity::EK_Temporary: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5840 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 5841 | case InitializedEntity::EK_Binding: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5842 | return true; |
| 5843 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5844 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5845 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 5846 | } |
| 5847 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5848 | /// Whether the given entity, when initialized with an object |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5849 | /// created for that initialization, requires destruction. |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5850 | static bool shouldDestroyEntity(const InitializedEntity &Entity) { |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5851 | switch (Entity.getKind()) { |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5852 | case InitializedEntity::EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 5853 | case InitializedEntity::EK_StmtExprResult: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5854 | case InitializedEntity::EK_New: |
| 5855 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 5856 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5857 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 5858 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 5859 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 5860 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 5861 | case InitializedEntity::EK_LambdaCapture: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5862 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5863 | |
Richard Smith | 27874d6 | 2013-01-08 00:08:23 +0000 | [diff] [blame] | 5864 | case InitializedEntity::EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 5865 | case InitializedEntity::EK_Binding: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5866 | case InitializedEntity::EK_Variable: |
| 5867 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5868 | case InitializedEntity::EK_Parameter_CF_Audited: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5869 | case InitializedEntity::EK_Temporary: |
| 5870 | case InitializedEntity::EK_ArrayElement: |
| 5871 | case InitializedEntity::EK_Exception: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5872 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5873 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5874 | return true; |
| 5875 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5876 | |
| 5877 | llvm_unreachable("missed an InitializedEntity kind?"); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5878 | } |
| 5879 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5880 | /// Get the location at which initialization diagnostics should appear. |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5881 | static SourceLocation getInitializationLoc(const InitializedEntity &Entity, |
| 5882 | Expr *Initializer) { |
| 5883 | switch (Entity.getKind()) { |
| 5884 | case InitializedEntity::EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 5885 | case InitializedEntity::EK_StmtExprResult: |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5886 | return Entity.getReturnLoc(); |
| 5887 | |
| 5888 | case InitializedEntity::EK_Exception: |
| 5889 | return Entity.getThrowLoc(); |
| 5890 | |
| 5891 | case InitializedEntity::EK_Variable: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 5892 | case InitializedEntity::EK_Binding: |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5893 | return Entity.getDecl()->getLocation(); |
| 5894 | |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 5895 | case InitializedEntity::EK_LambdaCapture: |
| 5896 | return Entity.getCaptureLoc(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5897 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5898 | case InitializedEntity::EK_ArrayElement: |
| 5899 | case InitializedEntity::EK_Member: |
| 5900 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5901 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5902 | case InitializedEntity::EK_Temporary: |
| 5903 | case InitializedEntity::EK_New: |
| 5904 | case InitializedEntity::EK_Base: |
| 5905 | case InitializedEntity::EK_Delegating: |
| 5906 | case InitializedEntity::EK_VectorElement: |
| 5907 | case InitializedEntity::EK_ComplexElement: |
| 5908 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 5909 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5910 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5911 | case InitializedEntity::EK_RelatedResult: |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5912 | return Initializer->getBeginLoc(); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5913 | } |
| 5914 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 5915 | } |
| 5916 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5917 | /// Make a (potentially elidable) temporary copy of the object |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5918 | /// provided by the given initializer by calling the appropriate copy |
| 5919 | /// constructor. |
| 5920 | /// |
| 5921 | /// \param S The Sema object used for type-checking. |
| 5922 | /// |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 5923 | /// \param T The type of the temporary object, which must either be |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5924 | /// the type of the initializer expression or a superclass thereof. |
| 5925 | /// |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5926 | /// \param Entity The entity being initialized. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5927 | /// |
| 5928 | /// \param CurInit The initializer expression. |
| 5929 | /// |
| 5930 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 5931 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 5932 | /// an rvalue. |
| 5933 | /// |
| 5934 | /// \returns An expression that copies the initializer expression into |
| 5935 | /// a temporary object, or an error expression if a copy could not be |
| 5936 | /// created. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5937 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 5938 | QualType T, |
| 5939 | const InitializedEntity &Entity, |
| 5940 | ExprResult CurInit, |
| 5941 | bool IsExtraneousCopy) { |
Fariborz Jahanian | 36f7f13 | 2015-01-28 22:08:10 +0000 | [diff] [blame] | 5942 | if (CurInit.isInvalid()) |
| 5943 | return CurInit; |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5944 | // Determine which class type we're copying to. |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 5945 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5946 | CXXRecordDecl *Class = nullptr; |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5947 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5948 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 5949 | if (!Class) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5950 | return CurInit; |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5951 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5952 | SourceLocation Loc = getInitializationLoc(Entity, CurInit.get()); |
Douglas Gregor | d5c231e | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 5953 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5954 | // Make sure that the type we are copying is complete. |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5955 | if (S.RequireCompleteType(Loc, T, diag::err_temp_copy_incomplete)) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5956 | return CurInit; |
Douglas Gregor | d5c231e | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 5957 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5958 | // Perform overload resolution using the class's constructors. Per |
| 5959 | // C++11 [dcl.init]p16, second bullet for class types, this initialization |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5960 | // is direct-initialization. |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 5961 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5962 | DeclContext::lookup_result Ctors = S.LookupConstructors(Class); |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5963 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5964 | OverloadCandidateSet::iterator Best; |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5965 | switch (ResolveConstructorOverload( |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 5966 | S, Loc, CurInitExpr, CandidateSet, T, Ctors, Best, |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5967 | /*CopyInitializing=*/false, /*AllowExplicit=*/true, |
| 5968 | /*OnlyListConstructors=*/false, /*IsListInit=*/false, |
| 5969 | /*SecondStepOfCopyInit=*/true)) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5970 | case OR_Success: |
| 5971 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5972 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5973 | case OR_No_Viable_Function: |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 5974 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 5975 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 5976 | : diag::err_temp_copy_no_viable) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 5977 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5978 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5979 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 5980 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5981 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5982 | return CurInit; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5983 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5984 | case OR_Ambiguous: |
| 5985 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 5986 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5987 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5988 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5989 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5990 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5991 | case OR_Deleted: |
| 5992 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 5993 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5994 | << CurInitExpr->getSourceRange(); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5995 | S.NoteDeletedFunction(Best->Function); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5996 | return ExprError(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5997 | } |
| 5998 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5999 | bool HadMultipleCandidates = CandidateSet.size() > 1; |
| 6000 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 6001 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 6002 | SmallVector<Expr*, 8> ConstructorArgs; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6003 | CurInit.get(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 6004 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6005 | S.CheckConstructorAccess(Loc, Constructor, Best->FoundDecl, Entity, |
| 6006 | IsExtraneousCopy); |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 6007 | |
| 6008 | if (IsExtraneousCopy) { |
| 6009 | // If this is a totally extraneous copy for C++03 reference |
| 6010 | // binding purposes, just return the original initialization |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 6011 | // expression. We don't generate an (elided) copy operation here |
| 6012 | // because doing so would require us to pass down a flag to avoid |
| 6013 | // infinite recursion, where each step adds another extraneous, |
| 6014 | // elidable copy. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 6015 | |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 6016 | // Instantiate the default arguments of any extra parameters in |
| 6017 | // the selected copy constructor, as if we were going to create a |
| 6018 | // proper call to the copy constructor. |
| 6019 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 6020 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 6021 | if (S.RequireCompleteType(Loc, Parm->getType(), |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 6022 | diag::err_call_incomplete_argument)) |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 6023 | break; |
| 6024 | |
| 6025 | // Build the default argument expression; we don't actually care |
| 6026 | // if this succeeds or not, because this routine will complain |
| 6027 | // if there was a problem. |
| 6028 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 6029 | } |
| 6030 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6031 | return CurInitExpr; |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 6032 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6033 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 6034 | // Determine the arguments required to actually perform the |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 6035 | // constructor call (we might have derived-to-base conversions, or |
| 6036 | // the copy constructor may have default arguments). |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6037 | if (S.CompleteConstructorCall(Constructor, CurInitExpr, Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6038 | return ExprError(); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 6039 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 6040 | // C++0x [class.copy]p32: |
| 6041 | // When certain criteria are met, an implementation is allowed to |
| 6042 | // omit the copy/move construction of a class object, even if the |
| 6043 | // copy/move constructor and/or destructor for the object have |
| 6044 | // side effects. [...] |
| 6045 | // - when a temporary class object that has not been bound to a |
| 6046 | // reference (12.2) would be copied/moved to a class object |
| 6047 | // with the same cv-unqualified type, the copy/move operation |
| 6048 | // can be omitted by constructing the temporary object |
| 6049 | // directly into the target of the omitted copy/move |
| 6050 | // |
| 6051 | // Note that the other three bullets are handled elsewhere. Copy |
| 6052 | // elision for return statements and throw expressions are handled as part |
| 6053 | // of constructor initialization, while copy elision for exception handlers |
| 6054 | // is handled by the run-time. |
| 6055 | // |
| 6056 | // FIXME: If the function parameter is not the same type as the temporary, we |
| 6057 | // should still be able to elide the copy, but we don't have a way to |
| 6058 | // represent in the AST how much should be elided in this case. |
| 6059 | bool Elidable = |
| 6060 | CurInitExpr->isTemporaryObject(S.Context, Class) && |
| 6061 | S.Context.hasSameUnqualifiedType( |
| 6062 | Best->Function->getParamDecl(0)->getType().getNonReferenceType(), |
| 6063 | CurInitExpr->getType()); |
| 6064 | |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 6065 | // Actually perform the constructor call. |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 6066 | CurInit = S.BuildCXXConstructExpr(Loc, T, Best->FoundDecl, Constructor, |
| 6067 | Elidable, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6068 | ConstructorArgs, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 6069 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6070 | /*ListInit*/ false, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6071 | /*StdInitListInit*/ false, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 6072 | /*ZeroInit*/ false, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 6073 | CXXConstructExpr::CK_Complete, |
| 6074 | SourceRange()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6075 | |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 6076 | // If we're supposed to bind temporaries, do so. |
| 6077 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6078 | CurInit = S.MaybeBindToTemporary(CurInit.getAs<Expr>()); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6079 | return CurInit; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6080 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6081 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6082 | /// Check whether elidable copy construction for binding a reference to |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6083 | /// a temporary would have succeeded if we were building in C++98 mode, for |
| 6084 | /// -Wc++98-compat. |
| 6085 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 6086 | const InitializedEntity &Entity, |
| 6087 | Expr *CurInitExpr) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6088 | assert(S.getLangOpts().CPlusPlus11); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6089 | |
| 6090 | const RecordType *Record = CurInitExpr->getType()->getAs<RecordType>(); |
| 6091 | if (!Record) |
| 6092 | return; |
| 6093 | |
| 6094 | SourceLocation Loc = getInitializationLoc(Entity, CurInitExpr); |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 6095 | if (S.Diags.isIgnored(diag::warn_cxx98_compat_temp_copy, Loc)) |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6096 | return; |
| 6097 | |
| 6098 | // Find constructors which would have been considered. |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 6099 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 6100 | DeclContext::lookup_result Ctors = |
| 6101 | S.LookupConstructors(cast<CXXRecordDecl>(Record->getDecl())); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6102 | |
| 6103 | // Perform overload resolution. |
| 6104 | OverloadCandidateSet::iterator Best; |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 6105 | OverloadingResult OR = ResolveConstructorOverload( |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 6106 | S, Loc, CurInitExpr, CandidateSet, CurInitExpr->getType(), Ctors, Best, |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 6107 | /*CopyInitializing=*/false, /*AllowExplicit=*/true, |
| 6108 | /*OnlyListConstructors=*/false, /*IsListInit=*/false, |
| 6109 | /*SecondStepOfCopyInit=*/true); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6110 | |
| 6111 | PartialDiagnostic Diag = S.PDiag(diag::warn_cxx98_compat_temp_copy) |
| 6112 | << OR << (int)Entity.getKind() << CurInitExpr->getType() |
| 6113 | << CurInitExpr->getSourceRange(); |
| 6114 | |
| 6115 | switch (OR) { |
| 6116 | case OR_Success: |
| 6117 | S.CheckConstructorAccess(Loc, cast<CXXConstructorDecl>(Best->Function), |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6118 | Best->FoundDecl, Entity, Diag); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6119 | // FIXME: Check default arguments as far as that's possible. |
| 6120 | break; |
| 6121 | |
| 6122 | case OR_No_Viable_Function: |
| 6123 | S.Diag(Loc, Diag); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6124 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6125 | break; |
| 6126 | |
| 6127 | case OR_Ambiguous: |
| 6128 | S.Diag(Loc, Diag); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6129 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6130 | break; |
| 6131 | |
| 6132 | case OR_Deleted: |
| 6133 | S.Diag(Loc, Diag); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6134 | S.NoteDeletedFunction(Best->Function); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6135 | break; |
| 6136 | } |
| 6137 | } |
| 6138 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6139 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 6140 | const InitializedEntity &Entity) { |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6141 | if (Entity.isParameterKind() && Entity.getDecl()) { |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6142 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 6143 | return; |
| 6144 | |
| 6145 | if (Entity.getDecl()->getDeclName()) |
| 6146 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 6147 | << Entity.getDecl()->getDeclName(); |
| 6148 | else |
| 6149 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 6150 | } |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 6151 | else if (Entity.getKind() == InitializedEntity::EK_RelatedResult && |
| 6152 | Entity.getMethodDecl()) |
| 6153 | S.Diag(Entity.getMethodDecl()->getLocation(), |
| 6154 | diag::note_method_return_type_change) |
| 6155 | << Entity.getMethodDecl()->getDeclName(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6156 | } |
| 6157 | |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 6158 | /// Returns true if the parameters describe a constructor initialization of |
| 6159 | /// an explicit temporary object, e.g. "Point(x, y)". |
| 6160 | static bool isExplicitTemporary(const InitializedEntity &Entity, |
| 6161 | const InitializationKind &Kind, |
| 6162 | unsigned NumArgs) { |
| 6163 | switch (Entity.getKind()) { |
| 6164 | case InitializedEntity::EK_Temporary: |
| 6165 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 6166 | case InitializedEntity::EK_RelatedResult: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 6167 | break; |
| 6168 | default: |
| 6169 | return false; |
| 6170 | } |
| 6171 | |
| 6172 | switch (Kind.getKind()) { |
| 6173 | case InitializationKind::IK_DirectList: |
| 6174 | return true; |
| 6175 | // FIXME: Hack to work around cast weirdness. |
| 6176 | case InitializationKind::IK_Direct: |
| 6177 | case InitializationKind::IK_Value: |
| 6178 | return NumArgs != 1; |
| 6179 | default: |
| 6180 | return false; |
| 6181 | } |
| 6182 | } |
| 6183 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6184 | static ExprResult |
| 6185 | PerformConstructorInitialization(Sema &S, |
| 6186 | const InitializedEntity &Entity, |
| 6187 | const InitializationKind &Kind, |
| 6188 | MultiExprArg Args, |
| 6189 | const InitializationSequence::Step& Step, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6190 | bool &ConstructorInitRequiresZeroInit, |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 6191 | bool IsListInitialization, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6192 | bool IsStdInitListInitialization, |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 6193 | SourceLocation LBraceLoc, |
| 6194 | SourceLocation RBraceLoc) { |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6195 | unsigned NumArgs = Args.size(); |
| 6196 | CXXConstructorDecl *Constructor |
| 6197 | = cast<CXXConstructorDecl>(Step.Function.Function); |
| 6198 | bool HadMultipleCandidates = Step.Function.HadMultipleCandidates; |
| 6199 | |
| 6200 | // Build a call to the selected constructor. |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 6201 | SmallVector<Expr*, 8> ConstructorArgs; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6202 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 6203 | ? Kind.getEqualLoc() |
| 6204 | : Kind.getLocation(); |
| 6205 | |
| 6206 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 6207 | // Force even a trivial, implicit default constructor to be |
| 6208 | // semantically checked. We do this explicitly because we don't build |
| 6209 | // the definition for completely trivial constructors. |
Matt Beaumont-Gay | 47ff122 | 2012-02-24 08:37:56 +0000 | [diff] [blame] | 6210 | assert(Constructor->getParent() && "No parent class for constructor."); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6211 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Douglas Gregor | f704ade | 2012-02-24 07:48:37 +0000 | [diff] [blame] | 6212 | Constructor->isTrivial() && !Constructor->isUsed(false)) |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6213 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 6214 | } |
| 6215 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6216 | ExprResult CurInit((Expr *)nullptr); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6217 | |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6218 | // C++ [over.match.copy]p1: |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6219 | // - When initializing a temporary to be bound to the first parameter |
| 6220 | // of a constructor that takes a reference to possibly cv-qualified |
| 6221 | // T as its first argument, called with a single argument in the |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6222 | // context of direct-initialization, explicit conversion functions |
| 6223 | // are also considered. |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 6224 | bool AllowExplicitConv = |
| 6225 | Kind.AllowExplicit() && !Kind.isCopyInit() && Args.size() == 1 && |
| 6226 | hasCopyOrMoveCtorParam(S.Context, |
| 6227 | getConstructorInfo(Step.Function.FoundDecl)); |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6228 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6229 | // Determine the arguments required to actually perform the constructor |
| 6230 | // call. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6231 | if (S.CompleteConstructorCall(Constructor, Args, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6232 | Loc, ConstructorArgs, |
Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 6233 | AllowExplicitConv, |
| 6234 | IsListInitialization)) |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6235 | return ExprError(); |
| 6236 | |
| 6237 | |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 6238 | if (isExplicitTemporary(Entity, Kind, NumArgs)) { |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6239 | // An explicitly-constructed temporary, e.g., X(1, 2). |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 6240 | if (S.DiagnoseUseOfDecl(Constructor, Loc)) |
| 6241 | return ExprError(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6242 | |
| 6243 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 6244 | if (!TSInfo) |
| 6245 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
Vedant Kumar | c9a9531 | 2018-11-19 20:10:21 +0000 | [diff] [blame] | 6246 | SourceRange ParenOrBraceRange = |
| 6247 | (Kind.getKind() == InitializationKind::IK_DirectList) |
| 6248 | ? SourceRange(LBraceLoc, RBraceLoc) |
| 6249 | : Kind.getParenOrBraceRange(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6250 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6251 | if (auto *Shadow = dyn_cast<ConstructorUsingShadowDecl>( |
Richard Smith | 80a4702 | 2016-06-29 01:10:27 +0000 | [diff] [blame] | 6252 | Step.Function.FoundDecl.getDecl())) { |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6253 | Constructor = S.findInheritingConstructor(Loc, Constructor, Shadow); |
Richard Smith | 80a4702 | 2016-06-29 01:10:27 +0000 | [diff] [blame] | 6254 | if (S.DiagnoseUseOfDecl(Constructor, Loc)) |
| 6255 | return ExprError(); |
| 6256 | } |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6257 | S.MarkFunctionReferenced(Loc, Constructor); |
| 6258 | |
Bruno Ricci | ddb8f6b | 2018-12-22 14:39:30 +0000 | [diff] [blame] | 6259 | CurInit = CXXTemporaryObjectExpr::Create( |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 6260 | S.Context, Constructor, |
| 6261 | Entity.getType().getNonLValueExprType(S.Context), TSInfo, |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 6262 | ConstructorArgs, ParenOrBraceRange, HadMultipleCandidates, |
| 6263 | IsListInitialization, IsStdInitListInitialization, |
| 6264 | ConstructorInitRequiresZeroInit); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6265 | } else { |
| 6266 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 6267 | CXXConstructExpr::CK_Complete; |
| 6268 | |
| 6269 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 6270 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
| 6271 | CXXConstructExpr::CK_VirtualBase : |
| 6272 | CXXConstructExpr::CK_NonVirtualBase; |
| 6273 | } else if (Entity.getKind() == InitializedEntity::EK_Delegating) { |
| 6274 | ConstructKind = CXXConstructExpr::CK_Delegating; |
| 6275 | } |
| 6276 | |
Peter Collingbourne | b8d17e7 | 2014-02-22 02:59:41 +0000 | [diff] [blame] | 6277 | // Only get the parenthesis or brace range if it is a list initialization or |
| 6278 | // direct construction. |
| 6279 | SourceRange ParenOrBraceRange; |
| 6280 | if (IsListInitialization) |
| 6281 | ParenOrBraceRange = SourceRange(LBraceLoc, RBraceLoc); |
| 6282 | else if (Kind.getKind() == InitializationKind::IK_Direct) |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 6283 | ParenOrBraceRange = Kind.getParenOrBraceRange(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6284 | |
| 6285 | // If the entity allows NRVO, mark the construction as elidable |
| 6286 | // unconditionally. |
| 6287 | if (Entity.allowsNRVO()) |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 6288 | CurInit = S.BuildCXXConstructExpr(Loc, Step.Type, |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 6289 | Step.Function.FoundDecl, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6290 | Constructor, /*Elidable=*/true, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6291 | ConstructorArgs, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6292 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6293 | IsListInitialization, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6294 | IsStdInitListInitialization, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6295 | ConstructorInitRequiresZeroInit, |
| 6296 | ConstructKind, |
Peter Collingbourne | b8d17e7 | 2014-02-22 02:59:41 +0000 | [diff] [blame] | 6297 | ParenOrBraceRange); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6298 | else |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 6299 | CurInit = S.BuildCXXConstructExpr(Loc, Step.Type, |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 6300 | Step.Function.FoundDecl, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6301 | Constructor, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6302 | ConstructorArgs, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6303 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6304 | IsListInitialization, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6305 | IsStdInitListInitialization, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6306 | ConstructorInitRequiresZeroInit, |
| 6307 | ConstructKind, |
Peter Collingbourne | b8d17e7 | 2014-02-22 02:59:41 +0000 | [diff] [blame] | 6308 | ParenOrBraceRange); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6309 | } |
| 6310 | if (CurInit.isInvalid()) |
| 6311 | return ExprError(); |
| 6312 | |
| 6313 | // Only check access if all of that succeeded. |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6314 | S.CheckConstructorAccess(Loc, Constructor, Step.Function.FoundDecl, Entity); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 6315 | if (S.DiagnoseUseOfDecl(Step.Function.FoundDecl, Loc)) |
| 6316 | return ExprError(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6317 | |
| 6318 | if (shouldBindAsTemporary(Entity)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6319 | CurInit = S.MaybeBindToTemporary(CurInit.get()); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6320 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6321 | return CurInit; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6322 | } |
| 6323 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6324 | namespace { |
| 6325 | enum LifetimeKind { |
| 6326 | /// The lifetime of a temporary bound to this entity ends at the end of the |
| 6327 | /// full-expression, and that's (probably) fine. |
| 6328 | LK_FullExpression, |
| 6329 | |
| 6330 | /// The lifetime of a temporary bound to this entity is extended to the |
| 6331 | /// lifeitme of the entity itself. |
| 6332 | LK_Extended, |
| 6333 | |
| 6334 | /// The lifetime of a temporary bound to this entity probably ends too soon, |
| 6335 | /// because the entity is allocated in a new-expression. |
| 6336 | LK_New, |
| 6337 | |
| 6338 | /// The lifetime of a temporary bound to this entity ends too soon, because |
| 6339 | /// the entity is a return object. |
| 6340 | LK_Return, |
| 6341 | |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 6342 | /// The lifetime of a temporary bound to this entity ends too soon, because |
| 6343 | /// the entity is the result of a statement expression. |
| 6344 | LK_StmtExprResult, |
| 6345 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6346 | /// This is a mem-initializer: if it would extend a temporary (other than via |
| 6347 | /// a default member initializer), the program is ill-formed. |
| 6348 | LK_MemInitializer, |
| 6349 | }; |
| 6350 | using LifetimeResult = |
| 6351 | llvm::PointerIntPair<const InitializedEntity *, 3, LifetimeKind>; |
| 6352 | } |
| 6353 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6354 | /// Determine the declaration which an initialized entity ultimately refers to, |
| 6355 | /// for the purpose of lifetime-extending a temporary bound to a reference in |
| 6356 | /// the initialization of \p Entity. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6357 | static LifetimeResult getEntityLifetime( |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 6358 | const InitializedEntity *Entity, |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6359 | const InitializedEntity *InitField = nullptr) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6360 | // C++11 [class.temporary]p5: |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 6361 | switch (Entity->getKind()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6362 | case InitializedEntity::EK_Variable: |
| 6363 | // The temporary [...] persists for the lifetime of the reference |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6364 | return {Entity, LK_Extended}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6365 | |
| 6366 | case InitializedEntity::EK_Member: |
| 6367 | // For subobjects, we look at the complete object. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 6368 | if (Entity->getParent()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6369 | return getEntityLifetime(Entity->getParent(), Entity); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6370 | |
| 6371 | // except: |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6372 | // C++17 [class.base.init]p8: |
| 6373 | // A temporary expression bound to a reference member in a |
| 6374 | // mem-initializer is ill-formed. |
| 6375 | // C++17 [class.base.init]p11: |
| 6376 | // A temporary expression bound to a reference member from a |
| 6377 | // default member initializer is ill-formed. |
| 6378 | // |
| 6379 | // The context of p11 and its example suggest that it's only the use of a |
| 6380 | // default member initializer from a constructor that makes the program |
| 6381 | // ill-formed, not its mere existence, and that it can even be used by |
| 6382 | // aggregate initialization. |
| 6383 | return {Entity, Entity->isDefaultMemberInitializer() ? LK_Extended |
| 6384 | : LK_MemInitializer}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6385 | |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 6386 | case InitializedEntity::EK_Binding: |
Richard Smith | 3997b1b | 2016-08-12 01:55:21 +0000 | [diff] [blame] | 6387 | // Per [dcl.decomp]p3, the binding is treated as a variable of reference |
| 6388 | // type. |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6389 | return {Entity, LK_Extended}; |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 6390 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6391 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6392 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6393 | // -- A temporary bound to a reference parameter in a function call |
| 6394 | // persists until the completion of the full-expression containing |
| 6395 | // the call. |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6396 | return {nullptr, LK_FullExpression}; |
| 6397 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6398 | case InitializedEntity::EK_Result: |
| 6399 | // -- The lifetime of a temporary bound to the returned value in a |
| 6400 | // function return statement is not extended; the temporary is |
| 6401 | // destroyed at the end of the full-expression in the return statement. |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6402 | return {nullptr, LK_Return}; |
| 6403 | |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 6404 | case InitializedEntity::EK_StmtExprResult: |
| 6405 | // FIXME: Should we lifetime-extend through the result of a statement |
| 6406 | // expression? |
| 6407 | return {nullptr, LK_StmtExprResult}; |
| 6408 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6409 | case InitializedEntity::EK_New: |
| 6410 | // -- A temporary bound to a reference in a new-initializer persists |
| 6411 | // until the completion of the full-expression containing the |
| 6412 | // new-initializer. |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6413 | return {nullptr, LK_New}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6414 | |
| 6415 | case InitializedEntity::EK_Temporary: |
| 6416 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 6417 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6418 | // We don't yet know the storage duration of the surrounding temporary. |
| 6419 | // Assume it's got full-expression duration for now, it will patch up our |
| 6420 | // storage duration if that's not correct. |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6421 | return {nullptr, LK_FullExpression}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6422 | |
| 6423 | case InitializedEntity::EK_ArrayElement: |
| 6424 | // For subobjects, we look at the complete object. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6425 | return getEntityLifetime(Entity->getParent(), InitField); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6426 | |
| 6427 | case InitializedEntity::EK_Base: |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 6428 | // For subobjects, we look at the complete object. |
| 6429 | if (Entity->getParent()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6430 | return getEntityLifetime(Entity->getParent(), InitField); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6431 | return {InitField, LK_MemInitializer}; |
| 6432 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6433 | case InitializedEntity::EK_Delegating: |
| 6434 | // We can reach this case for aggregate initialization in a constructor: |
| 6435 | // struct A { int &&r; }; |
| 6436 | // struct B : A { B() : A{0} {} }; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6437 | // In this case, use the outermost field decl as the context. |
| 6438 | return {InitField, LK_MemInitializer}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6439 | |
| 6440 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 6441 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6442 | case InitializedEntity::EK_LambdaCapture: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6443 | case InitializedEntity::EK_VectorElement: |
| 6444 | case InitializedEntity::EK_ComplexElement: |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6445 | return {nullptr, LK_FullExpression}; |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6446 | |
| 6447 | case InitializedEntity::EK_Exception: |
| 6448 | // FIXME: Can we diagnose lifetime problems with exceptions? |
| 6449 | return {nullptr, LK_FullExpression}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6450 | } |
Benjamin Kramer | cabc882 | 2013-06-05 15:37:50 +0000 | [diff] [blame] | 6451 | llvm_unreachable("unknown entity kind"); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6452 | } |
| 6453 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6454 | namespace { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6455 | enum ReferenceKind { |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6456 | /// Lifetime would be extended by a reference binding to a temporary. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6457 | RK_ReferenceBinding, |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6458 | /// Lifetime would be extended by a std::initializer_list object binding to |
| 6459 | /// its backing array. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6460 | RK_StdInitializerList, |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6461 | }; |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6462 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6463 | /// A temporary or local variable. This will be one of: |
| 6464 | /// * A MaterializeTemporaryExpr. |
| 6465 | /// * A DeclRefExpr whose declaration is a local. |
| 6466 | /// * An AddrLabelExpr. |
| 6467 | /// * A BlockExpr for a block with captures. |
| 6468 | using Local = Expr*; |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6469 | |
| 6470 | /// Expressions we stepped over when looking for the local state. Any steps |
| 6471 | /// that would inhibit lifetime extension or take us out of subexpressions of |
| 6472 | /// the initializer are included. |
| 6473 | struct IndirectLocalPathEntry { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6474 | enum EntryKind { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6475 | DefaultInit, |
| 6476 | AddressOf, |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6477 | VarInit, |
| 6478 | LValToRVal, |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6479 | LifetimeBoundCall, |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6480 | } Kind; |
| 6481 | Expr *E; |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6482 | const Decl *D = nullptr; |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6483 | IndirectLocalPathEntry() {} |
| 6484 | IndirectLocalPathEntry(EntryKind K, Expr *E) : Kind(K), E(E) {} |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6485 | IndirectLocalPathEntry(EntryKind K, Expr *E, const Decl *D) |
| 6486 | : Kind(K), E(E), D(D) {} |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6487 | }; |
| 6488 | |
| 6489 | using IndirectLocalPath = llvm::SmallVectorImpl<IndirectLocalPathEntry>; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6490 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6491 | struct RevertToOldSizeRAII { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6492 | IndirectLocalPath &Path; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6493 | unsigned OldSize = Path.size(); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6494 | RevertToOldSizeRAII(IndirectLocalPath &Path) : Path(Path) {} |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6495 | ~RevertToOldSizeRAII() { Path.resize(OldSize); } |
| 6496 | }; |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6497 | |
| 6498 | using LocalVisitor = llvm::function_ref<bool(IndirectLocalPath &Path, Local L, |
| 6499 | ReferenceKind RK)>; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6500 | } |
| 6501 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6502 | static bool isVarOnPath(IndirectLocalPath &Path, VarDecl *VD) { |
| 6503 | for (auto E : Path) |
| 6504 | if (E.Kind == IndirectLocalPathEntry::VarInit && E.D == VD) |
| 6505 | return true; |
| 6506 | return false; |
| 6507 | } |
| 6508 | |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6509 | static bool pathContainsInit(IndirectLocalPath &Path) { |
Fangrui Song | 3117b17 | 2018-10-20 17:53:42 +0000 | [diff] [blame] | 6510 | return llvm::any_of(Path, [=](IndirectLocalPathEntry E) { |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6511 | return E.Kind == IndirectLocalPathEntry::DefaultInit || |
| 6512 | E.Kind == IndirectLocalPathEntry::VarInit; |
| 6513 | }); |
| 6514 | } |
| 6515 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6516 | static void visitLocalsRetainedByInitializer(IndirectLocalPath &Path, |
| 6517 | Expr *Init, LocalVisitor Visit, |
| 6518 | bool RevisitSubinits); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6519 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6520 | static void visitLocalsRetainedByReferenceBinding(IndirectLocalPath &Path, |
| 6521 | Expr *Init, ReferenceKind RK, |
| 6522 | LocalVisitor Visit); |
| 6523 | |
| 6524 | static bool implicitObjectParamIsLifetimeBound(const FunctionDecl *FD) { |
| 6525 | const TypeSourceInfo *TSI = FD->getTypeSourceInfo(); |
| 6526 | if (!TSI) |
| 6527 | return false; |
Martin Storsjo | d03fa99 | 2018-08-02 18:12:08 +0000 | [diff] [blame] | 6528 | // Don't declare this variable in the second operand of the for-statement; |
| 6529 | // GCC miscompiles that by ending its lifetime before evaluating the |
| 6530 | // third operand. See gcc.gnu.org/PR86769. |
| 6531 | AttributedTypeLoc ATL; |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6532 | for (TypeLoc TL = TSI->getTypeLoc(); |
Martin Storsjo | d03fa99 | 2018-08-02 18:12:08 +0000 | [diff] [blame] | 6533 | (ATL = TL.getAsAdjusted<AttributedTypeLoc>()); |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6534 | TL = ATL.getModifiedLoc()) { |
Richard Smith | e43e2b3 | 2018-08-20 21:47:29 +0000 | [diff] [blame] | 6535 | if (ATL.getAttrAs<LifetimeBoundAttr>()) |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6536 | return true; |
| 6537 | } |
| 6538 | return false; |
| 6539 | } |
| 6540 | |
| 6541 | static void visitLifetimeBoundArguments(IndirectLocalPath &Path, Expr *Call, |
| 6542 | LocalVisitor Visit) { |
| 6543 | const FunctionDecl *Callee; |
| 6544 | ArrayRef<Expr*> Args; |
| 6545 | |
| 6546 | if (auto *CE = dyn_cast<CallExpr>(Call)) { |
| 6547 | Callee = CE->getDirectCallee(); |
| 6548 | Args = llvm::makeArrayRef(CE->getArgs(), CE->getNumArgs()); |
| 6549 | } else { |
| 6550 | auto *CCE = cast<CXXConstructExpr>(Call); |
| 6551 | Callee = CCE->getConstructor(); |
| 6552 | Args = llvm::makeArrayRef(CCE->getArgs(), CCE->getNumArgs()); |
| 6553 | } |
| 6554 | if (!Callee) |
| 6555 | return; |
| 6556 | |
| 6557 | Expr *ObjectArg = nullptr; |
| 6558 | if (isa<CXXOperatorCallExpr>(Call) && Callee->isCXXInstanceMember()) { |
| 6559 | ObjectArg = Args[0]; |
| 6560 | Args = Args.slice(1); |
| 6561 | } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(Call)) { |
| 6562 | ObjectArg = MCE->getImplicitObjectArgument(); |
| 6563 | } |
| 6564 | |
| 6565 | auto VisitLifetimeBoundArg = [&](const Decl *D, Expr *Arg) { |
| 6566 | Path.push_back({IndirectLocalPathEntry::LifetimeBoundCall, Arg, D}); |
| 6567 | if (Arg->isGLValue()) |
| 6568 | visitLocalsRetainedByReferenceBinding(Path, Arg, RK_ReferenceBinding, |
| 6569 | Visit); |
| 6570 | else |
| 6571 | visitLocalsRetainedByInitializer(Path, Arg, Visit, true); |
| 6572 | Path.pop_back(); |
| 6573 | }; |
| 6574 | |
| 6575 | if (ObjectArg && implicitObjectParamIsLifetimeBound(Callee)) |
| 6576 | VisitLifetimeBoundArg(Callee, ObjectArg); |
| 6577 | |
| 6578 | for (unsigned I = 0, |
| 6579 | N = std::min<unsigned>(Callee->getNumParams(), Args.size()); |
| 6580 | I != N; ++I) { |
| 6581 | if (Callee->getParamDecl(I)->hasAttr<LifetimeBoundAttr>()) |
| 6582 | VisitLifetimeBoundArg(Callee->getParamDecl(I), Args[I]); |
| 6583 | } |
| 6584 | } |
| 6585 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6586 | /// Visit the locals that would be reachable through a reference bound to the |
| 6587 | /// glvalue expression \c Init. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6588 | static void visitLocalsRetainedByReferenceBinding(IndirectLocalPath &Path, |
| 6589 | Expr *Init, ReferenceKind RK, |
| 6590 | LocalVisitor Visit) { |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6591 | RevertToOldSizeRAII RAII(Path); |
| 6592 | |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 6593 | // Walk past any constructs which we can lifetime-extend across. |
| 6594 | Expr *Old; |
| 6595 | do { |
| 6596 | Old = Init; |
| 6597 | |
Bill Wendling | 7c44da2 | 2018-10-31 03:48:47 +0000 | [diff] [blame] | 6598 | if (auto *FE = dyn_cast<FullExpr>(Init)) |
| 6599 | Init = FE->getSubExpr(); |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6600 | |
Richard Smith | dbc8249 | 2015-01-10 01:28:13 +0000 | [diff] [blame] | 6601 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6602 | // If this is just redundant braces around an initializer, step over it. |
| 6603 | if (ILE->isTransparent()) |
Richard Smith | dbc8249 | 2015-01-10 01:28:13 +0000 | [diff] [blame] | 6604 | Init = ILE->getInit(0); |
Richard Smith | dbc8249 | 2015-01-10 01:28:13 +0000 | [diff] [blame] | 6605 | } |
| 6606 | |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 6607 | // Step over any subobject adjustments; we may have a materialized |
| 6608 | // temporary inside them. |
Richard Smith | 4baaa5a | 2016-12-03 01:14:32 +0000 | [diff] [blame] | 6609 | Init = const_cast<Expr *>(Init->skipRValueSubobjectAdjustments()); |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 6610 | |
| 6611 | // Per current approach for DR1376, look through casts to reference type |
| 6612 | // when performing lifetime extension. |
| 6613 | if (CastExpr *CE = dyn_cast<CastExpr>(Init)) |
| 6614 | if (CE->getSubExpr()->isGLValue()) |
| 6615 | Init = CE->getSubExpr(); |
| 6616 | |
Richard Smith | b3189a1 | 2016-12-05 07:49:14 +0000 | [diff] [blame] | 6617 | // Per the current approach for DR1299, look through array element access |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6618 | // on array glvalues when performing lifetime extension. |
| 6619 | if (auto *ASE = dyn_cast<ArraySubscriptExpr>(Init)) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6620 | Init = ASE->getBase(); |
| 6621 | auto *ICE = dyn_cast<ImplicitCastExpr>(Init); |
| 6622 | if (ICE && ICE->getCastKind() == CK_ArrayToPointerDecay) |
| 6623 | Init = ICE->getSubExpr(); |
| 6624 | else |
| 6625 | // We can't lifetime extend through this but we might still find some |
| 6626 | // retained temporaries. |
| 6627 | return visitLocalsRetainedByInitializer(Path, Init, Visit, true); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6628 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6629 | |
| 6630 | // Step into CXXDefaultInitExprs so we can diagnose cases where a |
| 6631 | // constructor inherits one as an implicit mem-initializer. |
| 6632 | if (auto *DIE = dyn_cast<CXXDefaultInitExpr>(Init)) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6633 | Path.push_back( |
| 6634 | {IndirectLocalPathEntry::DefaultInit, DIE, DIE->getField()}); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6635 | Init = DIE->getExpr(); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6636 | } |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 6637 | } while (Init != Old); |
| 6638 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6639 | if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(Init)) { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6640 | if (Visit(Path, Local(MTE), RK)) |
| 6641 | visitLocalsRetainedByInitializer(Path, MTE->GetTemporaryExpr(), Visit, |
| 6642 | true); |
| 6643 | } |
| 6644 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6645 | if (isa<CallExpr>(Init)) |
| 6646 | return visitLifetimeBoundArguments(Path, Init, Visit); |
| 6647 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6648 | switch (Init->getStmtClass()) { |
| 6649 | case Stmt::DeclRefExprClass: { |
| 6650 | // If we find the name of a local non-reference parameter, we could have a |
| 6651 | // lifetime problem. |
| 6652 | auto *DRE = cast<DeclRefExpr>(Init); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6653 | auto *VD = dyn_cast<VarDecl>(DRE->getDecl()); |
| 6654 | if (VD && VD->hasLocalStorage() && |
| 6655 | !DRE->refersToEnclosingVariableOrCapture()) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6656 | if (!VD->getType()->isReferenceType()) { |
| 6657 | Visit(Path, Local(DRE), RK); |
| 6658 | } else if (isa<ParmVarDecl>(DRE->getDecl())) { |
| 6659 | // The lifetime of a reference parameter is unknown; assume it's OK |
| 6660 | // for now. |
| 6661 | break; |
| 6662 | } else if (VD->getInit() && !isVarOnPath(Path, VD)) { |
| 6663 | Path.push_back({IndirectLocalPathEntry::VarInit, DRE, VD}); |
| 6664 | visitLocalsRetainedByReferenceBinding(Path, VD->getInit(), |
| 6665 | RK_ReferenceBinding, Visit); |
| 6666 | } |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6667 | } |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6668 | break; |
| 6669 | } |
| 6670 | |
| 6671 | case Stmt::UnaryOperatorClass: { |
| 6672 | // The only unary operator that make sense to handle here |
| 6673 | // is Deref. All others don't resolve to a "name." This includes |
| 6674 | // handling all sorts of rvalues passed to a unary operator. |
| 6675 | const UnaryOperator *U = cast<UnaryOperator>(Init); |
| 6676 | if (U->getOpcode() == UO_Deref) |
| 6677 | visitLocalsRetainedByInitializer(Path, U->getSubExpr(), Visit, true); |
| 6678 | break; |
| 6679 | } |
| 6680 | |
| 6681 | case Stmt::OMPArraySectionExprClass: { |
| 6682 | visitLocalsRetainedByInitializer( |
| 6683 | Path, cast<OMPArraySectionExpr>(Init)->getBase(), Visit, true); |
| 6684 | break; |
| 6685 | } |
| 6686 | |
| 6687 | case Stmt::ConditionalOperatorClass: |
| 6688 | case Stmt::BinaryConditionalOperatorClass: { |
| 6689 | auto *C = cast<AbstractConditionalOperator>(Init); |
| 6690 | if (!C->getTrueExpr()->getType()->isVoidType()) |
| 6691 | visitLocalsRetainedByReferenceBinding(Path, C->getTrueExpr(), RK, Visit); |
| 6692 | if (!C->getFalseExpr()->getType()->isVoidType()) |
| 6693 | visitLocalsRetainedByReferenceBinding(Path, C->getFalseExpr(), RK, Visit); |
| 6694 | break; |
| 6695 | } |
| 6696 | |
| 6697 | // FIXME: Visit the left-hand side of an -> or ->*. |
| 6698 | |
| 6699 | default: |
| 6700 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6701 | } |
| 6702 | } |
| 6703 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6704 | /// Visit the locals that would be reachable through an object initialized by |
| 6705 | /// the prvalue expression \c Init. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6706 | static void visitLocalsRetainedByInitializer(IndirectLocalPath &Path, |
| 6707 | Expr *Init, LocalVisitor Visit, |
| 6708 | bool RevisitSubinits) { |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6709 | RevertToOldSizeRAII RAII(Path); |
| 6710 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6711 | Expr *Old; |
| 6712 | do { |
| 6713 | Old = Init; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6714 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6715 | // Step into CXXDefaultInitExprs so we can diagnose cases where a |
| 6716 | // constructor inherits one as an implicit mem-initializer. |
| 6717 | if (auto *DIE = dyn_cast<CXXDefaultInitExpr>(Init)) { |
| 6718 | Path.push_back({IndirectLocalPathEntry::DefaultInit, DIE, DIE->getField()}); |
| 6719 | Init = DIE->getExpr(); |
| 6720 | } |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6721 | |
Bill Wendling | 7c44da2 | 2018-10-31 03:48:47 +0000 | [diff] [blame] | 6722 | if (auto *FE = dyn_cast<FullExpr>(Init)) |
| 6723 | Init = FE->getSubExpr(); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6724 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6725 | // Dig out the expression which constructs the extended temporary. |
| 6726 | Init = const_cast<Expr *>(Init->skipRValueSubobjectAdjustments()); |
| 6727 | |
| 6728 | if (CXXBindTemporaryExpr *BTE = dyn_cast<CXXBindTemporaryExpr>(Init)) |
| 6729 | Init = BTE->getSubExpr(); |
| 6730 | |
| 6731 | Init = Init->IgnoreParens(); |
| 6732 | |
| 6733 | // Step over value-preserving rvalue casts. |
| 6734 | if (auto *CE = dyn_cast<CastExpr>(Init)) { |
| 6735 | switch (CE->getCastKind()) { |
| 6736 | case CK_LValueToRValue: |
| 6737 | // If we can match the lvalue to a const object, we can look at its |
| 6738 | // initializer. |
| 6739 | Path.push_back({IndirectLocalPathEntry::LValToRVal, CE}); |
| 6740 | return visitLocalsRetainedByReferenceBinding( |
| 6741 | Path, Init, RK_ReferenceBinding, |
| 6742 | [&](IndirectLocalPath &Path, Local L, ReferenceKind RK) -> bool { |
| 6743 | if (auto *DRE = dyn_cast<DeclRefExpr>(L)) { |
| 6744 | auto *VD = dyn_cast<VarDecl>(DRE->getDecl()); |
| 6745 | if (VD && VD->getType().isConstQualified() && VD->getInit() && |
| 6746 | !isVarOnPath(Path, VD)) { |
| 6747 | Path.push_back({IndirectLocalPathEntry::VarInit, DRE, VD}); |
| 6748 | visitLocalsRetainedByInitializer(Path, VD->getInit(), Visit, true); |
| 6749 | } |
| 6750 | } else if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(L)) { |
| 6751 | if (MTE->getType().isConstQualified()) |
| 6752 | visitLocalsRetainedByInitializer(Path, MTE->GetTemporaryExpr(), |
| 6753 | Visit, true); |
| 6754 | } |
| 6755 | return false; |
| 6756 | }); |
| 6757 | |
| 6758 | // We assume that objects can be retained by pointers cast to integers, |
| 6759 | // but not if the integer is cast to floating-point type or to _Complex. |
| 6760 | // We assume that casts to 'bool' do not preserve enough information to |
| 6761 | // retain a local object. |
| 6762 | case CK_NoOp: |
| 6763 | case CK_BitCast: |
| 6764 | case CK_BaseToDerived: |
| 6765 | case CK_DerivedToBase: |
| 6766 | case CK_UncheckedDerivedToBase: |
| 6767 | case CK_Dynamic: |
| 6768 | case CK_ToUnion: |
| 6769 | case CK_UserDefinedConversion: |
| 6770 | case CK_ConstructorConversion: |
| 6771 | case CK_IntegralToPointer: |
| 6772 | case CK_PointerToIntegral: |
| 6773 | case CK_VectorSplat: |
| 6774 | case CK_IntegralCast: |
| 6775 | case CK_CPointerToObjCPointerCast: |
| 6776 | case CK_BlockPointerToObjCPointerCast: |
| 6777 | case CK_AnyPointerToBlockPointerCast: |
| 6778 | case CK_AddressSpaceConversion: |
| 6779 | break; |
| 6780 | |
| 6781 | case CK_ArrayToPointerDecay: |
| 6782 | // Model array-to-pointer decay as taking the address of the array |
| 6783 | // lvalue. |
| 6784 | Path.push_back({IndirectLocalPathEntry::AddressOf, CE}); |
| 6785 | return visitLocalsRetainedByReferenceBinding(Path, CE->getSubExpr(), |
| 6786 | RK_ReferenceBinding, Visit); |
| 6787 | |
| 6788 | default: |
| 6789 | return; |
| 6790 | } |
| 6791 | |
| 6792 | Init = CE->getSubExpr(); |
| 6793 | } |
| 6794 | } while (Old != Init); |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 6795 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6796 | // C++17 [dcl.init.list]p6: |
| 6797 | // initializing an initializer_list object from the array extends the |
| 6798 | // lifetime of the array exactly like binding a reference to a temporary. |
| 6799 | if (auto *ILE = dyn_cast<CXXStdInitializerListExpr>(Init)) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6800 | return visitLocalsRetainedByReferenceBinding(Path, ILE->getSubExpr(), |
| 6801 | RK_StdInitializerList, Visit); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6802 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6803 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6804 | // We already visited the elements of this initializer list while |
| 6805 | // performing the initialization. Don't visit them again unless we've |
| 6806 | // changed the lifetime of the initialized entity. |
| 6807 | if (!RevisitSubinits) |
| 6808 | return; |
| 6809 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6810 | if (ILE->isTransparent()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6811 | return visitLocalsRetainedByInitializer(Path, ILE->getInit(0), Visit, |
| 6812 | RevisitSubinits); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6813 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6814 | if (ILE->getType()->isArrayType()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6815 | for (unsigned I = 0, N = ILE->getNumInits(); I != N; ++I) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6816 | visitLocalsRetainedByInitializer(Path, ILE->getInit(I), Visit, |
| 6817 | RevisitSubinits); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6818 | return; |
| 6819 | } |
| 6820 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6821 | if (CXXRecordDecl *RD = ILE->getType()->getAsCXXRecordDecl()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6822 | assert(RD->isAggregate() && "aggregate init on non-aggregate"); |
| 6823 | |
| 6824 | // If we lifetime-extend a braced initializer which is initializing an |
| 6825 | // aggregate, and that aggregate contains reference members which are |
| 6826 | // bound to temporaries, those temporaries are also lifetime-extended. |
| 6827 | if (RD->isUnion() && ILE->getInitializedFieldInUnion() && |
| 6828 | ILE->getInitializedFieldInUnion()->getType()->isReferenceType()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6829 | visitLocalsRetainedByReferenceBinding(Path, ILE->getInit(0), |
| 6830 | RK_ReferenceBinding, Visit); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6831 | else { |
| 6832 | unsigned Index = 0; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6833 | for (const auto *I : RD->fields()) { |
Richard Smith | 0bca59d | 2013-07-01 06:08:20 +0000 | [diff] [blame] | 6834 | if (Index >= ILE->getNumInits()) |
| 6835 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6836 | if (I->isUnnamedBitfield()) |
| 6837 | continue; |
Richard Smith | 8d7f11d | 2013-06-27 22:54:33 +0000 | [diff] [blame] | 6838 | Expr *SubInit = ILE->getInit(Index); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6839 | if (I->getType()->isReferenceType()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6840 | visitLocalsRetainedByReferenceBinding(Path, SubInit, |
| 6841 | RK_ReferenceBinding, Visit); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6842 | else |
| 6843 | // This might be either aggregate-initialization of a member or |
| 6844 | // initialization of a std::initializer_list object. Regardless, |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6845 | // we should recursively lifetime-extend that initializer. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6846 | visitLocalsRetainedByInitializer(Path, SubInit, Visit, |
| 6847 | RevisitSubinits); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6848 | ++Index; |
| 6849 | } |
| 6850 | } |
| 6851 | } |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6852 | return; |
| 6853 | } |
| 6854 | |
Richard Smith | b3d203f | 2018-10-19 19:01:34 +0000 | [diff] [blame] | 6855 | // The lifetime of an init-capture is that of the closure object constructed |
| 6856 | // by a lambda-expression. |
| 6857 | if (auto *LE = dyn_cast<LambdaExpr>(Init)) { |
| 6858 | for (Expr *E : LE->capture_inits()) { |
| 6859 | if (!E) |
| 6860 | continue; |
| 6861 | if (E->isGLValue()) |
| 6862 | visitLocalsRetainedByReferenceBinding(Path, E, RK_ReferenceBinding, |
| 6863 | Visit); |
| 6864 | else |
| 6865 | visitLocalsRetainedByInitializer(Path, E, Visit, true); |
| 6866 | } |
| 6867 | } |
| 6868 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6869 | if (isa<CallExpr>(Init) || isa<CXXConstructExpr>(Init)) |
| 6870 | return visitLifetimeBoundArguments(Path, Init, Visit); |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6871 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6872 | switch (Init->getStmtClass()) { |
| 6873 | case Stmt::UnaryOperatorClass: { |
| 6874 | auto *UO = cast<UnaryOperator>(Init); |
| 6875 | // If the initializer is the address of a local, we could have a lifetime |
| 6876 | // problem. |
| 6877 | if (UO->getOpcode() == UO_AddrOf) { |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6878 | // If this is &rvalue, then it's ill-formed and we have already diagnosed |
| 6879 | // it. Don't produce a redundant warning about the lifetime of the |
| 6880 | // temporary. |
| 6881 | if (isa<MaterializeTemporaryExpr>(UO->getSubExpr())) |
| 6882 | return; |
| 6883 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6884 | Path.push_back({IndirectLocalPathEntry::AddressOf, UO}); |
| 6885 | visitLocalsRetainedByReferenceBinding(Path, UO->getSubExpr(), |
| 6886 | RK_ReferenceBinding, Visit); |
| 6887 | } |
| 6888 | break; |
| 6889 | } |
| 6890 | |
| 6891 | case Stmt::BinaryOperatorClass: { |
| 6892 | // Handle pointer arithmetic. |
| 6893 | auto *BO = cast<BinaryOperator>(Init); |
| 6894 | BinaryOperatorKind BOK = BO->getOpcode(); |
| 6895 | if (!BO->getType()->isPointerType() || (BOK != BO_Add && BOK != BO_Sub)) |
| 6896 | break; |
| 6897 | |
| 6898 | if (BO->getLHS()->getType()->isPointerType()) |
| 6899 | visitLocalsRetainedByInitializer(Path, BO->getLHS(), Visit, true); |
| 6900 | else if (BO->getRHS()->getType()->isPointerType()) |
| 6901 | visitLocalsRetainedByInitializer(Path, BO->getRHS(), Visit, true); |
| 6902 | break; |
| 6903 | } |
| 6904 | |
| 6905 | case Stmt::ConditionalOperatorClass: |
| 6906 | case Stmt::BinaryConditionalOperatorClass: { |
| 6907 | auto *C = cast<AbstractConditionalOperator>(Init); |
| 6908 | // In C++, we can have a throw-expression operand, which has 'void' type |
| 6909 | // and isn't interesting from a lifetime perspective. |
| 6910 | if (!C->getTrueExpr()->getType()->isVoidType()) |
| 6911 | visitLocalsRetainedByInitializer(Path, C->getTrueExpr(), Visit, true); |
| 6912 | if (!C->getFalseExpr()->getType()->isVoidType()) |
| 6913 | visitLocalsRetainedByInitializer(Path, C->getFalseExpr(), Visit, true); |
| 6914 | break; |
| 6915 | } |
| 6916 | |
| 6917 | case Stmt::BlockExprClass: |
| 6918 | if (cast<BlockExpr>(Init)->getBlockDecl()->hasCaptures()) { |
| 6919 | // This is a local block, whose lifetime is that of the function. |
| 6920 | Visit(Path, Local(cast<BlockExpr>(Init)), RK_ReferenceBinding); |
| 6921 | } |
| 6922 | break; |
| 6923 | |
| 6924 | case Stmt::AddrLabelExprClass: |
| 6925 | // We want to warn if the address of a label would escape the function. |
| 6926 | Visit(Path, Local(cast<AddrLabelExpr>(Init)), RK_ReferenceBinding); |
| 6927 | break; |
| 6928 | |
| 6929 | default: |
| 6930 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6931 | } |
| 6932 | } |
| 6933 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6934 | /// Determine whether this is an indirect path to a temporary that we are |
| 6935 | /// supposed to lifetime-extend along (but don't). |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6936 | static bool shouldLifetimeExtendThroughPath(const IndirectLocalPath &Path) { |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6937 | for (auto Elem : Path) { |
Richard Smith | f66e4f7 | 2018-07-23 22:56:45 +0000 | [diff] [blame] | 6938 | if (Elem.Kind != IndirectLocalPathEntry::DefaultInit) |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6939 | return false; |
| 6940 | } |
| 6941 | return true; |
| 6942 | } |
| 6943 | |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 6944 | /// Find the range for the first interesting entry in the path at or after I. |
| 6945 | static SourceRange nextPathEntryRange(const IndirectLocalPath &Path, unsigned I, |
| 6946 | Expr *E) { |
| 6947 | for (unsigned N = Path.size(); I != N; ++I) { |
| 6948 | switch (Path[I].Kind) { |
| 6949 | case IndirectLocalPathEntry::AddressOf: |
| 6950 | case IndirectLocalPathEntry::LValToRVal: |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6951 | case IndirectLocalPathEntry::LifetimeBoundCall: |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 6952 | // These exist primarily to mark the path as not permitting or |
| 6953 | // supporting lifetime extension. |
| 6954 | break; |
| 6955 | |
| 6956 | case IndirectLocalPathEntry::DefaultInit: |
| 6957 | case IndirectLocalPathEntry::VarInit: |
| 6958 | return Path[I].E->getSourceRange(); |
| 6959 | } |
| 6960 | } |
| 6961 | return E->getSourceRange(); |
| 6962 | } |
| 6963 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6964 | void Sema::checkInitializerLifetime(const InitializedEntity &Entity, |
| 6965 | Expr *Init) { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6966 | LifetimeResult LR = getEntityLifetime(&Entity); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6967 | LifetimeKind LK = LR.getInt(); |
| 6968 | const InitializedEntity *ExtendingEntity = LR.getPointer(); |
| 6969 | |
| 6970 | // If this entity doesn't have an interesting lifetime, don't bother looking |
| 6971 | // for temporaries within its initializer. |
| 6972 | if (LK == LK_FullExpression) |
| 6973 | return; |
| 6974 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6975 | auto TemporaryVisitor = [&](IndirectLocalPath &Path, Local L, |
| 6976 | ReferenceKind RK) -> bool { |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 6977 | SourceRange DiagRange = nextPathEntryRange(Path, 0, L); |
| 6978 | SourceLocation DiagLoc = DiagRange.getBegin(); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6979 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6980 | switch (LK) { |
| 6981 | case LK_FullExpression: |
| 6982 | llvm_unreachable("already handled this"); |
| 6983 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6984 | case LK_Extended: { |
| 6985 | auto *MTE = dyn_cast<MaterializeTemporaryExpr>(L); |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6986 | if (!MTE) { |
| 6987 | // The initialized entity has lifetime beyond the full-expression, |
| 6988 | // and the local entity does too, so don't warn. |
| 6989 | // |
| 6990 | // FIXME: We should consider warning if a static / thread storage |
| 6991 | // duration variable retains an automatic storage duration local. |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6992 | return false; |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6993 | } |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6994 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6995 | // Lifetime-extend the temporary. |
| 6996 | if (Path.empty()) { |
| 6997 | // Update the storage duration of the materialized temporary. |
| 6998 | // FIXME: Rebuild the expression instead of mutating it. |
| 6999 | MTE->setExtendingDecl(ExtendingEntity->getDecl(), |
| 7000 | ExtendingEntity->allocateManglingNumber()); |
| 7001 | // Also visit the temporaries lifetime-extended by this initializer. |
| 7002 | return true; |
| 7003 | } |
| 7004 | |
| 7005 | if (shouldLifetimeExtendThroughPath(Path)) { |
| 7006 | // We're supposed to lifetime-extend the temporary along this path (per |
| 7007 | // the resolution of DR1815), but we don't support that yet. |
| 7008 | // |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 7009 | // FIXME: Properly handle this situation. Perhaps the easiest approach |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7010 | // would be to clone the initializer expression on each use that would |
| 7011 | // lifetime extend its temporaries. |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 7012 | Diag(DiagLoc, diag::warn_unsupported_lifetime_extension) |
| 7013 | << RK << DiagRange; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7014 | } else { |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 7015 | // If the path goes through the initialization of a variable or field, |
| 7016 | // it can't possibly reach a temporary created in this full-expression. |
| 7017 | // We will have already diagnosed any problems with the initializer. |
| 7018 | if (pathContainsInit(Path)) |
| 7019 | return false; |
| 7020 | |
| 7021 | Diag(DiagLoc, diag::warn_dangling_variable) |
Richard Smith | ad5bbcc | 2018-08-01 01:03:33 +0000 | [diff] [blame] | 7022 | << RK << !Entity.getParent() |
| 7023 | << ExtendingEntity->getDecl()->isImplicit() |
| 7024 | << ExtendingEntity->getDecl() << Init->isGLValue() << DiagRange; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7025 | } |
| 7026 | break; |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7027 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7028 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7029 | case LK_MemInitializer: { |
George Burgess IV | 06df229 | 2018-07-24 02:10:53 +0000 | [diff] [blame] | 7030 | if (isa<MaterializeTemporaryExpr>(L)) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7031 | // Under C++ DR1696, if a mem-initializer (or a default member |
| 7032 | // initializer used by the absence of one) would lifetime-extend a |
| 7033 | // temporary, the program is ill-formed. |
| 7034 | if (auto *ExtendingDecl = |
| 7035 | ExtendingEntity ? ExtendingEntity->getDecl() : nullptr) { |
| 7036 | bool IsSubobjectMember = ExtendingEntity != &Entity; |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 7037 | Diag(DiagLoc, shouldLifetimeExtendThroughPath(Path) |
| 7038 | ? diag::err_dangling_member |
| 7039 | : diag::warn_dangling_member) |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7040 | << ExtendingDecl << IsSubobjectMember << RK << DiagRange; |
| 7041 | // Don't bother adding a note pointing to the field if we're inside |
| 7042 | // its default member initializer; our primary diagnostic points to |
| 7043 | // the same place in that case. |
| 7044 | if (Path.empty() || |
| 7045 | Path.back().Kind != IndirectLocalPathEntry::DefaultInit) { |
| 7046 | Diag(ExtendingDecl->getLocation(), |
| 7047 | diag::note_lifetime_extending_member_declared_here) |
| 7048 | << RK << IsSubobjectMember; |
| 7049 | } |
| 7050 | } else { |
| 7051 | // We have a mem-initializer but no particular field within it; this |
| 7052 | // is either a base class or a delegating initializer directly |
| 7053 | // initializing the base-class from something that doesn't live long |
| 7054 | // enough. |
| 7055 | // |
| 7056 | // FIXME: Warn on this. |
| 7057 | return false; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7058 | } |
| 7059 | } else { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7060 | // Paths via a default initializer can only occur during error recovery |
| 7061 | // (there's no other way that a default initializer can refer to a |
| 7062 | // local). Don't produce a bogus warning on those cases. |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 7063 | if (pathContainsInit(Path)) |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7064 | return false; |
| 7065 | |
| 7066 | auto *DRE = dyn_cast<DeclRefExpr>(L); |
| 7067 | auto *VD = DRE ? dyn_cast<VarDecl>(DRE->getDecl()) : nullptr; |
| 7068 | if (!VD) { |
| 7069 | // A member was initialized to a local block. |
| 7070 | // FIXME: Warn on this. |
| 7071 | return false; |
| 7072 | } |
| 7073 | |
| 7074 | if (auto *Member = |
| 7075 | ExtendingEntity ? ExtendingEntity->getDecl() : nullptr) { |
| 7076 | bool IsPointer = Member->getType()->isAnyPointerType(); |
| 7077 | Diag(DiagLoc, IsPointer ? diag::warn_init_ptr_member_to_parameter_addr |
| 7078 | : diag::warn_bind_ref_member_to_parameter) |
| 7079 | << Member << VD << isa<ParmVarDecl>(VD) << DiagRange; |
| 7080 | Diag(Member->getLocation(), |
| 7081 | diag::note_ref_or_ptr_member_declared_here) |
| 7082 | << (unsigned)IsPointer; |
| 7083 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7084 | } |
| 7085 | break; |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7086 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7087 | |
| 7088 | case LK_New: |
George Burgess IV | 06df229 | 2018-07-24 02:10:53 +0000 | [diff] [blame] | 7089 | if (isa<MaterializeTemporaryExpr>(L)) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7090 | Diag(DiagLoc, RK == RK_ReferenceBinding |
| 7091 | ? diag::warn_new_dangling_reference |
| 7092 | : diag::warn_new_dangling_initializer_list) |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 7093 | << !Entity.getParent() << DiagRange; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7094 | } else { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7095 | // We can't determine if the allocation outlives the local declaration. |
| 7096 | return false; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7097 | } |
| 7098 | break; |
| 7099 | |
| 7100 | case LK_Return: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 7101 | case LK_StmtExprResult: |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7102 | if (auto *DRE = dyn_cast<DeclRefExpr>(L)) { |
| 7103 | // We can't determine if the local variable outlives the statement |
| 7104 | // expression. |
| 7105 | if (LK == LK_StmtExprResult) |
| 7106 | return false; |
| 7107 | Diag(DiagLoc, diag::warn_ret_stack_addr_ref) |
| 7108 | << Entity.getType()->isReferenceType() << DRE->getDecl() |
| 7109 | << isa<ParmVarDecl>(DRE->getDecl()) << DiagRange; |
| 7110 | } else if (isa<BlockExpr>(L)) { |
| 7111 | Diag(DiagLoc, diag::err_ret_local_block) << DiagRange; |
| 7112 | } else if (isa<AddrLabelExpr>(L)) { |
Reid Kleckner | 4c33d19 | 2018-08-17 22:11:31 +0000 | [diff] [blame] | 7113 | // Don't warn when returning a label from a statement expression. |
| 7114 | // Leaving the scope doesn't end its lifetime. |
| 7115 | if (LK == LK_StmtExprResult) |
| 7116 | return false; |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7117 | Diag(DiagLoc, diag::warn_ret_addr_label) << DiagRange; |
| 7118 | } else { |
| 7119 | Diag(DiagLoc, diag::warn_ret_local_temp_addr_ref) |
| 7120 | << Entity.getType()->isReferenceType() << DiagRange; |
| 7121 | } |
| 7122 | break; |
Florian Hahn | 0aa117d | 2018-07-17 09:23:31 +0000 | [diff] [blame] | 7123 | } |
| 7124 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7125 | for (unsigned I = 0; I != Path.size(); ++I) { |
| 7126 | auto Elem = Path[I]; |
| 7127 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7128 | switch (Elem.Kind) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7129 | case IndirectLocalPathEntry::AddressOf: |
| 7130 | case IndirectLocalPathEntry::LValToRVal: |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 7131 | // These exist primarily to mark the path as not permitting or |
| 7132 | // supporting lifetime extension. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7133 | break; |
| 7134 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 7135 | case IndirectLocalPathEntry::LifetimeBoundCall: |
| 7136 | // FIXME: Consider adding a note for this. |
| 7137 | break; |
| 7138 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7139 | case IndirectLocalPathEntry::DefaultInit: { |
| 7140 | auto *FD = cast<FieldDecl>(Elem.D); |
| 7141 | Diag(FD->getLocation(), diag::note_init_with_default_member_initalizer) |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 7142 | << FD << nextPathEntryRange(Path, I + 1, L); |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7143 | break; |
| 7144 | } |
| 7145 | |
| 7146 | case IndirectLocalPathEntry::VarInit: |
| 7147 | const VarDecl *VD = cast<VarDecl>(Elem.D); |
| 7148 | Diag(VD->getLocation(), diag::note_local_var_initializer) |
Richard Smith | ad5bbcc | 2018-08-01 01:03:33 +0000 | [diff] [blame] | 7149 | << VD->getType()->isReferenceType() |
| 7150 | << VD->isImplicit() << VD->getDeclName() |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 7151 | << nextPathEntryRange(Path, I + 1, L); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7152 | break; |
Florian Hahn | 0aa117d | 2018-07-17 09:23:31 +0000 | [diff] [blame] | 7153 | } |
| 7154 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7155 | |
| 7156 | // We didn't lifetime-extend, so don't go any further; we don't need more |
| 7157 | // warnings or errors on inner temporaries within this one's initializer. |
| 7158 | return false; |
| 7159 | }; |
| 7160 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7161 | llvm::SmallVector<IndirectLocalPathEntry, 8> Path; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7162 | if (Init->isGLValue()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7163 | visitLocalsRetainedByReferenceBinding(Path, Init, RK_ReferenceBinding, |
| 7164 | TemporaryVisitor); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7165 | else |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7166 | visitLocalsRetainedByInitializer(Path, Init, TemporaryVisitor, false); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 7167 | } |
| 7168 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7169 | static void DiagnoseNarrowingInInitList(Sema &S, |
| 7170 | const ImplicitConversionSequence &ICS, |
| 7171 | QualType PreNarrowingType, |
| 7172 | QualType EntityType, |
| 7173 | const Expr *PostInit); |
| 7174 | |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7175 | /// Provide warnings when std::move is used on construction. |
| 7176 | static void CheckMoveOnConstruction(Sema &S, const Expr *InitExpr, |
| 7177 | bool IsReturnStmt) { |
| 7178 | if (!InitExpr) |
| 7179 | return; |
| 7180 | |
Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 7181 | if (S.inTemplateInstantiation()) |
Richard Trieu | 6093d14 | 2015-07-29 17:03:34 +0000 | [diff] [blame] | 7182 | return; |
| 7183 | |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7184 | QualType DestType = InitExpr->getType(); |
| 7185 | if (!DestType->isRecordType()) |
| 7186 | return; |
| 7187 | |
| 7188 | unsigned DiagID = 0; |
| 7189 | if (IsReturnStmt) { |
| 7190 | const CXXConstructExpr *CCE = |
| 7191 | dyn_cast<CXXConstructExpr>(InitExpr->IgnoreParens()); |
| 7192 | if (!CCE || CCE->getNumArgs() != 1) |
| 7193 | return; |
| 7194 | |
| 7195 | if (!CCE->getConstructor()->isCopyOrMoveConstructor()) |
| 7196 | return; |
| 7197 | |
| 7198 | InitExpr = CCE->getArg(0)->IgnoreImpCasts(); |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7199 | } |
| 7200 | |
| 7201 | // Find the std::move call and get the argument. |
| 7202 | const CallExpr *CE = dyn_cast<CallExpr>(InitExpr->IgnoreParens()); |
Nico Weber | 192184c | 2018-06-20 15:57:38 +0000 | [diff] [blame] | 7203 | if (!CE || !CE->isCallToStdMove()) |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7204 | return; |
| 7205 | |
| 7206 | const Expr *Arg = CE->getArg(0)->IgnoreImplicit(); |
| 7207 | |
| 7208 | if (IsReturnStmt) { |
| 7209 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg->IgnoreParenImpCasts()); |
| 7210 | if (!DRE || DRE->refersToEnclosingVariableOrCapture()) |
| 7211 | return; |
| 7212 | |
| 7213 | const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()); |
| 7214 | if (!VD || !VD->hasLocalStorage()) |
| 7215 | return; |
| 7216 | |
Alex Lorenz | bbe51d8 | 2017-11-07 21:40:11 +0000 | [diff] [blame] | 7217 | // __block variables are not moved implicitly. |
| 7218 | if (VD->hasAttr<BlocksAttr>()) |
| 7219 | return; |
| 7220 | |
Richard Trieu | 8d4006a | 2015-07-28 19:06:16 +0000 | [diff] [blame] | 7221 | QualType SourceType = VD->getType(); |
| 7222 | if (!SourceType->isRecordType()) |
Richard Trieu | 1d4911bc | 2015-05-18 19:54:08 +0000 | [diff] [blame] | 7223 | return; |
| 7224 | |
Richard Trieu | 8d4006a | 2015-07-28 19:06:16 +0000 | [diff] [blame] | 7225 | if (!S.Context.hasSameUnqualifiedType(DestType, SourceType)) { |
Richard Trieu | 1993dc8 | 2015-07-29 23:47:19 +0000 | [diff] [blame] | 7226 | return; |
Richard Trieu | 8d4006a | 2015-07-28 19:06:16 +0000 | [diff] [blame] | 7227 | } |
| 7228 | |
Davide Italiano | 7842c3f | 2015-07-18 01:15:19 +0000 | [diff] [blame] | 7229 | // If we're returning a function parameter, copy elision |
| 7230 | // is not possible. |
| 7231 | if (isa<ParmVarDecl>(VD)) |
| 7232 | DiagID = diag::warn_redundant_move_on_return; |
Richard Trieu | 1993dc8 | 2015-07-29 23:47:19 +0000 | [diff] [blame] | 7233 | else |
| 7234 | DiagID = diag::warn_pessimizing_move_on_return; |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7235 | } else { |
| 7236 | DiagID = diag::warn_pessimizing_move_on_initialization; |
| 7237 | const Expr *ArgStripped = Arg->IgnoreImplicit()->IgnoreParens(); |
| 7238 | if (!ArgStripped->isRValue() || !ArgStripped->getType()->isRecordType()) |
| 7239 | return; |
| 7240 | } |
| 7241 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7242 | S.Diag(CE->getBeginLoc(), DiagID); |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7243 | |
| 7244 | // Get all the locations for a fix-it. Don't emit the fix-it if any location |
| 7245 | // is within a macro. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7246 | SourceLocation CallBegin = CE->getCallee()->getBeginLoc(); |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7247 | if (CallBegin.isMacroID()) |
| 7248 | return; |
| 7249 | SourceLocation RParen = CE->getRParenLoc(); |
| 7250 | if (RParen.isMacroID()) |
| 7251 | return; |
| 7252 | SourceLocation LParen; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7253 | SourceLocation ArgLoc = Arg->getBeginLoc(); |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7254 | |
| 7255 | // Special testing for the argument location. Since the fix-it needs the |
| 7256 | // location right before the argument, the argument location can be in a |
| 7257 | // macro only if it is at the beginning of the macro. |
| 7258 | while (ArgLoc.isMacroID() && |
| 7259 | S.getSourceManager().isAtStartOfImmediateMacroExpansion(ArgLoc)) { |
Richard Smith | b5f8171 | 2018-04-30 05:25:48 +0000 | [diff] [blame] | 7260 | ArgLoc = S.getSourceManager().getImmediateExpansionRange(ArgLoc).getBegin(); |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7261 | } |
| 7262 | |
| 7263 | if (LParen.isMacroID()) |
| 7264 | return; |
| 7265 | |
| 7266 | LParen = ArgLoc.getLocWithOffset(-1); |
| 7267 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7268 | S.Diag(CE->getBeginLoc(), diag::note_remove_move) |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7269 | << FixItHint::CreateRemoval(SourceRange(CallBegin, LParen)) |
| 7270 | << FixItHint::CreateRemoval(SourceRange(RParen, RParen)); |
| 7271 | } |
| 7272 | |
Nick Lewycky | 2eeddfb | 2016-05-14 17:44:14 +0000 | [diff] [blame] | 7273 | static void CheckForNullPointerDereference(Sema &S, const Expr *E) { |
| 7274 | // Check to see if we are dereferencing a null pointer. If so, this is |
| 7275 | // undefined behavior, so warn about it. This only handles the pattern |
| 7276 | // "*null", which is a very syntactic check. |
| 7277 | if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E->IgnoreParenCasts())) |
| 7278 | if (UO->getOpcode() == UO_Deref && |
| 7279 | UO->getSubExpr()->IgnoreParenCasts()-> |
| 7280 | isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull)) { |
| 7281 | S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
| 7282 | S.PDiag(diag::warn_binding_null_to_reference) |
| 7283 | << UO->getSubExpr()->getSourceRange()); |
| 7284 | } |
| 7285 | } |
| 7286 | |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 7287 | MaterializeTemporaryExpr * |
| 7288 | Sema::CreateMaterializeTemporaryExpr(QualType T, Expr *Temporary, |
| 7289 | bool BoundToLvalueReference) { |
| 7290 | auto MTE = new (Context) |
| 7291 | MaterializeTemporaryExpr(T, Temporary, BoundToLvalueReference); |
| 7292 | |
| 7293 | // Order an ExprWithCleanups for lifetime marks. |
| 7294 | // |
| 7295 | // TODO: It'll be good to have a single place to check the access of the |
| 7296 | // destructor and generate ExprWithCleanups for various uses. Currently these |
| 7297 | // are done in both CreateMaterializeTemporaryExpr and MaybeBindToTemporary, |
| 7298 | // but there may be a chance to merge them. |
| 7299 | Cleanup.setExprNeedsCleanups(false); |
| 7300 | return MTE; |
| 7301 | } |
| 7302 | |
Richard Smith | 4baaa5a | 2016-12-03 01:14:32 +0000 | [diff] [blame] | 7303 | ExprResult Sema::TemporaryMaterializationConversion(Expr *E) { |
| 7304 | // In C++98, we don't want to implicitly create an xvalue. |
| 7305 | // FIXME: This means that AST consumers need to deal with "prvalues" that |
| 7306 | // denote materialized temporaries. Maybe we should add another ValueKind |
| 7307 | // for "xvalue pretending to be a prvalue" for C++98 support. |
| 7308 | if (!E->isRValue() || !getLangOpts().CPlusPlus11) |
| 7309 | return E; |
| 7310 | |
| 7311 | // C++1z [conv.rval]/1: T shall be a complete type. |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7312 | // FIXME: Does this ever matter (can we form a prvalue of incomplete type)? |
| 7313 | // 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] | 7314 | QualType T = E->getType(); |
| 7315 | if (RequireCompleteType(E->getExprLoc(), T, diag::err_incomplete_type)) |
| 7316 | return ExprError(); |
| 7317 | |
| 7318 | return CreateMaterializeTemporaryExpr(E->getType(), E, false); |
| 7319 | } |
| 7320 | |
Anastasia Stulova | 0430794 | 2018-11-16 16:22:56 +0000 | [diff] [blame] | 7321 | ExprResult Sema::PerformQualificationConversion(Expr *E, QualType Ty, |
| 7322 | ExprValueKind VK, |
| 7323 | CheckedConversionKind CCK) { |
Anastasia Stulova | 094c726 | 2019-04-04 10:48:36 +0000 | [diff] [blame] | 7324 | |
| 7325 | CastKind CK = CK_NoOp; |
| 7326 | |
| 7327 | if (VK == VK_RValue) { |
| 7328 | auto PointeeTy = Ty->getPointeeType(); |
| 7329 | auto ExprPointeeTy = E->getType()->getPointeeType(); |
| 7330 | if (!PointeeTy.isNull() && |
| 7331 | PointeeTy.getAddressSpace() != ExprPointeeTy.getAddressSpace()) |
| 7332 | CK = CK_AddressSpaceConversion; |
| 7333 | } else if (Ty.getAddressSpace() != E->getType().getAddressSpace()) { |
| 7334 | CK = CK_AddressSpaceConversion; |
| 7335 | } |
| 7336 | |
Anastasia Stulova | 0430794 | 2018-11-16 16:22:56 +0000 | [diff] [blame] | 7337 | return ImpCastExprToType(E, Ty, CK, VK, /*BasePath=*/nullptr, CCK); |
| 7338 | } |
| 7339 | |
| 7340 | ExprResult InitializationSequence::Perform(Sema &S, |
| 7341 | const InitializedEntity &Entity, |
| 7342 | const InitializationKind &Kind, |
| 7343 | MultiExprArg Args, |
| 7344 | QualType *ResultType) { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 7345 | if (Failed()) { |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 7346 | Diagnose(S, Entity, Kind, Args); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7347 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7348 | } |
Nico Weber | 337d5aa | 2015-04-17 08:32:38 +0000 | [diff] [blame] | 7349 | if (!ZeroInitializationFixit.empty()) { |
| 7350 | unsigned DiagID = diag::err_default_init_const; |
| 7351 | if (Decl *D = Entity.getDecl()) |
| 7352 | if (S.getLangOpts().MSVCCompat && D->hasAttr<SelectAnyAttr>()) |
| 7353 | DiagID = diag::ext_default_init_const; |
| 7354 | |
| 7355 | // The initialization would have succeeded with this fixit. Since the fixit |
| 7356 | // is on the error, we need to build a valid AST in this case, so this isn't |
| 7357 | // handled in the Failed() branch above. |
| 7358 | QualType DestType = Entity.getType(); |
| 7359 | S.Diag(Kind.getLocation(), DiagID) |
| 7360 | << DestType << (bool)DestType->getAs<RecordType>() |
| 7361 | << FixItHint::CreateInsertion(ZeroInitializationFixitLoc, |
| 7362 | ZeroInitializationFixit); |
| 7363 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7364 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 7365 | if (getKind() == DependentSequence) { |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7366 | // If the declaration is a non-dependent, incomplete array type |
| 7367 | // that has an initializer, then its type will be completed once |
| 7368 | // the initializer is instantiated. |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7369 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7370 | Args.size() == 1) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7371 | QualType DeclType = Entity.getType(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7372 | if (const IncompleteArrayType *ArrayT |
| 7373 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 7374 | // FIXME: We don't currently have the ability to accurately |
| 7375 | // compute the length of an initializer list without |
| 7376 | // performing full type-checking of the initializer list |
| 7377 | // (since we have to determine where braces are implicitly |
| 7378 | // introduced and such). So, we fall back to making the array |
| 7379 | // type a dependently-sized array type with no specified |
| 7380 | // bound. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7381 | if (isa<InitListExpr>((Expr *)Args[0])) { |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7382 | SourceRange Brackets; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7383 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7384 | // Scavange the location of the brackets from the entity, if we can. |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 7385 | if (auto *DD = dyn_cast_or_null<DeclaratorDecl>(Entity.getDecl())) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7386 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 7387 | TypeLoc TL = TInfo->getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7388 | if (IncompleteArrayTypeLoc ArrayLoc = |
| 7389 | TL.getAs<IncompleteArrayTypeLoc>()) |
| 7390 | Brackets = ArrayLoc.getBracketsRange(); |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7391 | } |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7392 | } |
| 7393 | |
| 7394 | *ResultType |
| 7395 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7396 | /*NumElts=*/nullptr, |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7397 | ArrayT->getSizeModifier(), |
| 7398 | ArrayT->getIndexTypeCVRQualifiers(), |
| 7399 | Brackets); |
| 7400 | } |
| 7401 | |
| 7402 | } |
| 7403 | } |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 7404 | if (Kind.getKind() == InitializationKind::IK_Direct && |
| 7405 | !Kind.isExplicitCast()) { |
| 7406 | // Rebuild the ParenListExpr. |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 7407 | SourceRange ParenRange = Kind.getParenOrBraceRange(); |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 7408 | return S.ActOnParenListExpr(ParenRange.getBegin(), ParenRange.getEnd(), |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7409 | Args); |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 7410 | } |
Manuel Klimek | f2b4b69 | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 7411 | assert(Kind.getKind() == InitializationKind::IK_Copy || |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7412 | Kind.isExplicitCast() || |
Douglas Gregor | bf13895 | 2012-04-04 04:06:51 +0000 | [diff] [blame] | 7413 | Kind.getKind() == InitializationKind::IK_DirectList); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7414 | return ExprResult(Args[0]); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7415 | } |
| 7416 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 7417 | // No steps means no initialization. |
| 7418 | if (Steps.empty()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7419 | return ExprResult((Expr *)nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7420 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7421 | if (S.getLangOpts().CPlusPlus11 && Entity.getType()->isReferenceType() && |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7422 | Args.size() == 1 && isa<InitListExpr>(Args[0]) && |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 7423 | !Entity.isParameterKind()) { |
Richard Smith | 2b349ae | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 7424 | // Produce a C++98 compatibility warning if we are initializing a reference |
| 7425 | // from an initializer list. For parameters, we produce a better warning |
| 7426 | // elsewhere. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7427 | Expr *Init = Args[0]; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7428 | S.Diag(Init->getBeginLoc(), diag::warn_cxx98_compat_reference_list_init) |
| 7429 | << Init->getSourceRange(); |
Richard Smith | 2b349ae | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 7430 | } |
| 7431 | |
Egor Churaev | 3bccec5 | 2017-04-05 12:47:10 +0000 | [diff] [blame] | 7432 | // OpenCL v2.0 s6.13.11.1. atomic variables can be initialized in global scope |
| 7433 | QualType ETy = Entity.getType(); |
| 7434 | Qualifiers TyQualifiers = ETy.getQualifiers(); |
| 7435 | bool HasGlobalAS = TyQualifiers.hasAddressSpace() && |
| 7436 | TyQualifiers.getAddressSpace() == LangAS::opencl_global; |
| 7437 | |
| 7438 | if (S.getLangOpts().OpenCLVersion >= 200 && |
| 7439 | ETy->isAtomicType() && !HasGlobalAS && |
| 7440 | Entity.getKind() == InitializedEntity::EK_Variable && Args.size() > 0) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7441 | S.Diag(Args[0]->getBeginLoc(), diag::err_opencl_atomic_init) |
| 7442 | << 1 |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 7443 | << SourceRange(Entity.getDecl()->getBeginLoc(), Args[0]->getEndLoc()); |
Egor Churaev | 3bccec5 | 2017-04-05 12:47:10 +0000 | [diff] [blame] | 7444 | return ExprError(); |
| 7445 | } |
| 7446 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7447 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 7448 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 7449 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 7450 | // and we want latter when it makes sense. |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7451 | if (ResultType) |
Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 7452 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7453 | Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7454 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7455 | ExprResult CurInit((Expr *)nullptr); |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7456 | SmallVector<Expr*, 4> ArrayLoopCommonExprs; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7457 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7458 | // For initialization steps that start with a single initializer, |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 7459 | // grab the only argument out the Args and place it into the "current" |
| 7460 | // initializer. |
| 7461 | switch (Steps.front().Kind) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7462 | case SK_ResolveAddressOfOverloadedFunction: |
| 7463 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7464 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7465 | case SK_CastDerivedToBaseLValue: |
| 7466 | case SK_BindReference: |
| 7467 | case SK_BindReferenceToTemporary: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7468 | case SK_FinalCopy: |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 7469 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7470 | case SK_UserConversion: |
| 7471 | case SK_QualificationConversionLValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7472 | case SK_QualificationConversionXValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7473 | case SK_QualificationConversionRValue: |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 7474 | case SK_AtomicConversion: |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 7475 | case SK_LValueToRValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7476 | case SK_ConversionSequence: |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7477 | case SK_ConversionSequenceNoNarrowing: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7478 | case SK_ListInitialization: |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7479 | case SK_UnwrapInitList: |
| 7480 | case SK_RewrapInitList: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7481 | case SK_CAssignment: |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 7482 | case SK_StringInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7483 | case SK_ObjCObjectConversion: |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7484 | case SK_ArrayLoopIndex: |
| 7485 | case SK_ArrayLoopInit: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7486 | case SK_ArrayInit: |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 7487 | case SK_GNUArrayInit: |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 7488 | case SK_ParenthesizedArrayInit: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7489 | case SK_PassByIndirectCopyRestore: |
| 7490 | case SK_PassByIndirectRestore: |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 7491 | case SK_ProduceObjCObject: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7492 | case SK_StdInitializerList: |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 7493 | case SK_OCLSamplerInit: |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 7494 | case SK_OCLZeroOpaqueType: { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7495 | assert(Args.size() == 1); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7496 | CurInit = Args[0]; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7497 | if (!CurInit.get()) return ExprError(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7498 | break; |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7499 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7500 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7501 | case SK_ConstructorInitialization: |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7502 | case SK_ConstructorInitializationFromList: |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 7503 | case SK_StdInitializerListConstructorCall: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7504 | case SK_ZeroInitialization: |
| 7505 | break; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7506 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7507 | |
Richard Smith | d6a1508 | 2017-01-07 00:48:55 +0000 | [diff] [blame] | 7508 | // Promote from an unevaluated context to an unevaluated list context in |
| 7509 | // C++11 list-initialization; we need to instantiate entities usable in |
| 7510 | // constant expressions here in order to perform narrowing checks =( |
| 7511 | EnterExpressionEvaluationContext Evaluated( |
| 7512 | S, EnterExpressionEvaluationContext::InitList, |
| 7513 | CurInit.get() && isa<InitListExpr>(CurInit.get())); |
| 7514 | |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7515 | // C++ [class.abstract]p2: |
| 7516 | // no objects of an abstract class can be created except as subobjects |
| 7517 | // of a class derived from it |
| 7518 | auto checkAbstractType = [&](QualType T) -> bool { |
| 7519 | if (Entity.getKind() == InitializedEntity::EK_Base || |
| 7520 | Entity.getKind() == InitializedEntity::EK_Delegating) |
| 7521 | return false; |
| 7522 | return S.RequireNonAbstractType(Kind.getLocation(), T, |
| 7523 | diag::err_allocation_of_abstract_type); |
| 7524 | }; |
| 7525 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7526 | // Walk through the computed steps for the initialization sequence, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7527 | // performing the specified conversions along the way. |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7528 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7529 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 7530 | Step != StepEnd; ++Step) { |
| 7531 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7532 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7533 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7534 | QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7535 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7536 | switch (Step->Kind) { |
| 7537 | case SK_ResolveAddressOfOverloadedFunction: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7538 | // Overload resolution determined which function invoke; update the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7539 | // initializer to reflect that choice. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7540 | S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 7541 | if (S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation())) |
| 7542 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7543 | CurInit = S.FixOverloadedFunctionReference(CurInit, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7544 | Step->Function.FoundDecl, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7545 | Step->Function.Function); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7546 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7547 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7548 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7549 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7550 | case SK_CastDerivedToBaseLValue: { |
| 7551 | // We have a derived-to-base cast that produces either an rvalue or an |
| 7552 | // lvalue. Perform that cast. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7553 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 7554 | CXXCastPath BasePath; |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 7555 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7556 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 7557 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7558 | if (S.CheckDerivedToBaseConversion( |
| 7559 | SourceType, Step->Type, CurInit.get()->getBeginLoc(), |
| 7560 | CurInit.get()->getSourceRange(), &BasePath, IgnoreBaseAccess)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7561 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7562 | |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7563 | ExprValueKind VK = |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7564 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7565 | VK_LValue : |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7566 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7567 | VK_XValue : |
| 7568 | VK_RValue); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7569 | CurInit = |
| 7570 | ImplicitCastExpr::Create(S.Context, Step->Type, CK_DerivedToBase, |
| 7571 | CurInit.get(), &BasePath, VK); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7572 | break; |
| 7573 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7574 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7575 | case SK_BindReference: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7576 | // Reference binding does not have any corresponding ASTs. |
| 7577 | |
| 7578 | // Check exception specifications |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7579 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7580 | return ExprError(); |
Anders Carlsson | ab0ddb5 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 7581 | |
George Burgess IV | cfd48d9 | 2017-04-13 23:47:08 +0000 | [diff] [blame] | 7582 | // We don't check for e.g. function pointers here, since address |
| 7583 | // availability checks should only occur when the function first decays |
| 7584 | // into a pointer or reference. |
| 7585 | if (CurInit.get()->getType()->isFunctionProtoType()) { |
| 7586 | if (auto *DRE = dyn_cast<DeclRefExpr>(CurInit.get()->IgnoreParens())) { |
| 7587 | if (auto *FD = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
| 7588 | if (!S.checkAddressOfFunctionIsAvailable(FD, /*Complain=*/true, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7589 | DRE->getBeginLoc())) |
George Burgess IV | cfd48d9 | 2017-04-13 23:47:08 +0000 | [diff] [blame] | 7590 | return ExprError(); |
| 7591 | } |
| 7592 | } |
| 7593 | } |
| 7594 | |
Nick Lewycky | 2eeddfb | 2016-05-14 17:44:14 +0000 | [diff] [blame] | 7595 | CheckForNullPointerDereference(S, CurInit.get()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7596 | break; |
Anders Carlsson | ab0ddb5 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 7597 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 7598 | case SK_BindReferenceToTemporary: { |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 7599 | // Make sure the "temporary" is actually an rvalue. |
| 7600 | assert(CurInit.get()->isRValue() && "not a temporary"); |
| 7601 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7602 | // Check exception specifications |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7603 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7604 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7605 | |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 7606 | // Materialize the temporary into memory. |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 7607 | MaterializeTemporaryExpr *MTE = S.CreateMaterializeTemporaryExpr( |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7608 | Step->Type, CurInit.get(), Entity.getType()->isLValueReferenceType()); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7609 | CurInit = MTE; |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 7610 | |
Brian Kelley | 762f928 | 2017-03-29 18:16:38 +0000 | [diff] [blame] | 7611 | // If we're extending this temporary to automatic storage duration -- we |
| 7612 | // need to register its cleanup during the full-expression's cleanups. |
| 7613 | if (MTE->getStorageDuration() == SD_Automatic && |
| 7614 | MTE->getType().isDestructedType()) |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 7615 | S.Cleanup.setExprNeedsCleanups(true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7616 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 7617 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7618 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7619 | case SK_FinalCopy: |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7620 | if (checkAbstractType(Step->Type)) |
| 7621 | return ExprError(); |
| 7622 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7623 | // If the overall initialization is initializing a temporary, we already |
| 7624 | // bound our argument if it was necessary to do so. If not (if we're |
| 7625 | // ultimately initializing a non-temporary), our argument needs to be |
| 7626 | // bound since it's initializing a function parameter. |
| 7627 | // FIXME: This is a mess. Rationalize temporary destruction. |
| 7628 | if (!shouldBindAsTemporary(Entity)) |
| 7629 | CurInit = S.MaybeBindToTemporary(CurInit.get()); |
| 7630 | CurInit = CopyObject(S, Step->Type, Entity, CurInit, |
| 7631 | /*IsExtraneousCopy=*/false); |
| 7632 | break; |
| 7633 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 7634 | case SK_ExtraneousCopyToTemporary: |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7635 | CurInit = CopyObject(S, Step->Type, Entity, CurInit, |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 7636 | /*IsExtraneousCopy=*/true); |
| 7637 | break; |
| 7638 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7639 | case SK_UserConversion: { |
| 7640 | // We have a user-defined conversion that invokes either a constructor |
| 7641 | // or a conversion function. |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 7642 | CastKind CastKind; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7643 | FunctionDecl *Fn = Step->Function.Function; |
| 7644 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 7645 | bool HadMultipleCandidates = Step->Function.HadMultipleCandidates; |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 7646 | bool CreatedObject = false; |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 7647 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7648 | // Build a call to the selected constructor. |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 7649 | SmallVector<Expr*, 8> ConstructorArgs; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7650 | SourceLocation Loc = CurInit.get()->getBeginLoc(); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 7651 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7652 | // Determine the arguments required to actually perform the constructor |
| 7653 | // call. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7654 | Expr *Arg = CurInit.get(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7655 | if (S.CompleteConstructorCall(Constructor, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7656 | MultiExprArg(&Arg, 1), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7657 | Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7658 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7659 | |
Richard Smith | b24f067 | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 7660 | // Build an expression that constructs a temporary. |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 7661 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, |
| 7662 | FoundFn, Constructor, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7663 | ConstructorArgs, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 7664 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 7665 | /*ListInit*/ false, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 7666 | /*StdInitListInit*/ false, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 7667 | /*ZeroInit*/ false, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 7668 | CXXConstructExpr::CK_Complete, |
| 7669 | SourceRange()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7670 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7671 | return ExprError(); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 7672 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 7673 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, FoundFn, |
| 7674 | Entity); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 7675 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 7676 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7677 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7678 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 7679 | CreatedObject = true; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7680 | } else { |
| 7681 | // Build a call to the conversion function. |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 7682 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7683 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), nullptr, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7684 | FoundFn); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 7685 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 7686 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7687 | |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 7688 | CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion, |
| 7689 | HadMultipleCandidates); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7690 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7691 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7692 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7693 | CastKind = CK_UserDefinedConversion; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7694 | CreatedObject = Conversion->getReturnType()->isRecordType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7695 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7696 | |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7697 | if (CreatedObject && checkAbstractType(CurInit.get()->getType())) |
| 7698 | return ExprError(); |
| 7699 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7700 | CurInit = ImplicitCastExpr::Create(S.Context, CurInit.get()->getType(), |
| 7701 | CastKind, CurInit.get(), nullptr, |
| 7702 | CurInit.get()->getValueKind()); |
Abramo Bagnara | b0cf297 | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 7703 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7704 | if (shouldBindAsTemporary(Entity)) |
| 7705 | // The overall entity is temporary, so this expression should be |
| 7706 | // destroyed at the end of its full-expression. |
| 7707 | CurInit = S.MaybeBindToTemporary(CurInit.getAs<Expr>()); |
| 7708 | else if (CreatedObject && shouldDestroyEntity(Entity)) { |
| 7709 | // The object outlasts the full-expression, but we need to prepare for |
| 7710 | // a destructor being run on it. |
| 7711 | // FIXME: It makes no sense to do this here. This should happen |
| 7712 | // regardless of how we initialized the entity. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7713 | QualType T = CurInit.get()->getType(); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 7714 | if (const RecordType *Record = T->getAs<RecordType>()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7715 | CXXDestructorDecl *Destructor |
Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 7716 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7717 | S.CheckDestructorAccess(CurInit.get()->getBeginLoc(), Destructor, |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 7718 | S.PDiag(diag::err_access_dtor_temp) << T); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7719 | S.MarkFunctionReferenced(CurInit.get()->getBeginLoc(), Destructor); |
| 7720 | if (S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getBeginLoc())) |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 7721 | return ExprError(); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 7722 | } |
| 7723 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7724 | break; |
| 7725 | } |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7726 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7727 | case SK_QualificationConversionLValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7728 | case SK_QualificationConversionXValue: |
| 7729 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7730 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7731 | ExprValueKind VK = |
Anastasia Stulova | 0430794 | 2018-11-16 16:22:56 +0000 | [diff] [blame] | 7732 | Step->Kind == SK_QualificationConversionLValue |
| 7733 | ? VK_LValue |
| 7734 | : (Step->Kind == SK_QualificationConversionXValue ? VK_XValue |
| 7735 | : VK_RValue); |
| 7736 | CurInit = S.PerformQualificationConversion(CurInit.get(), Step->Type, VK); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7737 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7738 | } |
| 7739 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 7740 | case SK_AtomicConversion: { |
| 7741 | assert(CurInit.get()->isRValue() && "cannot convert glvalue to atomic"); |
| 7742 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
| 7743 | CK_NonAtomicToAtomic, VK_RValue); |
| 7744 | break; |
| 7745 | } |
| 7746 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 7747 | case SK_LValueToRValue: { |
| 7748 | assert(CurInit.get()->isGLValue() && "cannot load from a prvalue"); |
Richard Smith | 3501895 | 2018-11-03 02:23:33 +0000 | [diff] [blame] | 7749 | CurInit = ImplicitCastExpr::Create(S.Context, Step->Type, |
| 7750 | CK_LValueToRValue, CurInit.get(), |
| 7751 | /*BasePath=*/nullptr, VK_RValue); |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 7752 | break; |
| 7753 | } |
| 7754 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7755 | case SK_ConversionSequence: |
| 7756 | case SK_ConversionSequenceNoNarrowing: { |
Leonard Chan | ad7ac96 | 2018-12-06 01:05:54 +0000 | [diff] [blame] | 7757 | if (const auto *FromPtrType = |
| 7758 | CurInit.get()->getType()->getAs<PointerType>()) { |
| 7759 | if (const auto *ToPtrType = Step->Type->getAs<PointerType>()) { |
| 7760 | if (FromPtrType->getPointeeType()->hasAttr(attr::NoDeref) && |
| 7761 | !ToPtrType->getPointeeType()->hasAttr(attr::NoDeref)) { |
| 7762 | S.Diag(CurInit.get()->getExprLoc(), |
| 7763 | diag::warn_noderef_to_dereferenceable_pointer) |
| 7764 | << CurInit.get()->getSourceRange(); |
| 7765 | } |
| 7766 | } |
| 7767 | } |
| 7768 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7769 | Sema::CheckedConversionKind CCK |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7770 | = Kind.isCStyleCast()? Sema::CCK_CStyleCast |
| 7771 | : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 7772 | : Kind.isExplicitCast()? Sema::CCK_OtherCast |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7773 | : Sema::CCK_ImplicitConversion; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7774 | ExprResult CurInitExprRes = |
| 7775 | S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7776 | getAssignmentAction(Entity), CCK); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7777 | if (CurInitExprRes.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7778 | return ExprError(); |
Roger Ferrer Ibanez | 722a4db | 2016-08-12 08:04:13 +0000 | [diff] [blame] | 7779 | |
| 7780 | S.DiscardMisalignedMemberAddress(Step->Type.getTypePtr(), CurInit.get()); |
| 7781 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7782 | CurInit = CurInitExprRes; |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7783 | |
| 7784 | if (Step->Kind == SK_ConversionSequenceNoNarrowing && |
Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 7785 | S.getLangOpts().CPlusPlus) |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7786 | DiagnoseNarrowingInInitList(S, *Step->ICS, SourceType, Entity.getType(), |
| 7787 | CurInit.get()); |
Roger Ferrer Ibanez | 722a4db | 2016-08-12 08:04:13 +0000 | [diff] [blame] | 7788 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7789 | break; |
Douglas Gregor | 5c8ffab | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 7790 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7791 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7792 | case SK_ListInitialization: { |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7793 | if (checkAbstractType(Step->Type)) |
| 7794 | return ExprError(); |
| 7795 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7796 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 7797 | // If we're not initializing the top-level entity, we need to create an |
| 7798 | // InitializeTemporary entity for our target type. |
| 7799 | QualType Ty = Step->Type; |
| 7800 | bool IsTemporary = !S.Context.hasSameType(Entity.getType(), Ty); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7801 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(Ty); |
Richard Smith | d712d0d | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 7802 | InitializedEntity InitEntity = IsTemporary ? TempEntity : Entity; |
| 7803 | InitListChecker PerformInitList(S, InitEntity, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 7804 | InitList, Ty, /*VerifyOnly=*/false, |
| 7805 | /*TreatUnavailableAsInvalid=*/false); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 7806 | if (PerformInitList.HadError()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7807 | return ExprError(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7808 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 7809 | // Hack: We must update *ResultType if available in order to set the |
| 7810 | // bounds of arrays, e.g. in 'int ar[] = {1, 2, 3};'. |
| 7811 | // Worst case: 'const int (&arref)[] = {1, 2, 3};'. |
| 7812 | if (ResultType && |
| 7813 | ResultType->getNonReferenceType()->isIncompleteArrayType()) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7814 | if ((*ResultType)->isRValueReferenceType()) |
| 7815 | Ty = S.Context.getRValueReferenceType(Ty); |
| 7816 | else if ((*ResultType)->isLValueReferenceType()) |
| 7817 | Ty = S.Context.getLValueReferenceType(Ty, |
| 7818 | (*ResultType)->getAs<LValueReferenceType>()->isSpelledAsLValue()); |
| 7819 | *ResultType = Ty; |
| 7820 | } |
| 7821 | |
| 7822 | InitListExpr *StructuredInitList = |
| 7823 | PerformInitList.getFullyStructuredList(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7824 | CurInit.get(); |
Richard Smith | d712d0d | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 7825 | CurInit = shouldBindAsTemporary(InitEntity) |
| 7826 | ? S.MaybeBindToTemporary(StructuredInitList) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7827 | : StructuredInitList; |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7828 | break; |
| 7829 | } |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 7830 | |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7831 | case SK_ConstructorInitializationFromList: { |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7832 | if (checkAbstractType(Step->Type)) |
| 7833 | return ExprError(); |
| 7834 | |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 7835 | // When an initializer list is passed for a parameter of type "reference |
| 7836 | // to object", we don't get an EK_Temporary entity, but instead an |
| 7837 | // EK_Parameter entity with reference type. |
Sebastian Redl | 99f6616 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 7838 | // FIXME: This is a hack. What we really should do is create a user |
| 7839 | // conversion step for this case, but this makes it considerably more |
| 7840 | // complicated. For now, this will do. |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 7841 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 7842 | Entity.getType().getNonReferenceType()); |
| 7843 | bool UseTemporary = Entity.getType()->isReferenceType(); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 7844 | assert(Args.size() == 1 && "expected a single argument for list init"); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7845 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Richard Smith | 2b349ae | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 7846 | S.Diag(InitList->getExprLoc(), diag::warn_cxx98_compat_ctor_list_init) |
| 7847 | << InitList->getSourceRange(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 7848 | MultiExprArg Arg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 7849 | CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity : |
| 7850 | Entity, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7851 | Kind, Arg, *Step, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 7852 | ConstructorInitRequiresZeroInit, |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7853 | /*IsListInitialization*/true, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 7854 | /*IsStdInitListInit*/false, |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 7855 | InitList->getLBraceLoc(), |
| 7856 | InitList->getRBraceLoc()); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 7857 | break; |
| 7858 | } |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 7859 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7860 | case SK_UnwrapInitList: |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7861 | CurInit = cast<InitListExpr>(CurInit.get())->getInit(0); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7862 | break; |
| 7863 | |
| 7864 | case SK_RewrapInitList: { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7865 | Expr *E = CurInit.get(); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7866 | InitListExpr *Syntactic = Step->WrappingSyntacticList; |
| 7867 | InitListExpr *ILE = new (S.Context) InitListExpr(S.Context, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 7868 | Syntactic->getLBraceLoc(), E, Syntactic->getRBraceLoc()); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7869 | ILE->setSyntacticForm(Syntactic); |
| 7870 | ILE->setType(E->getType()); |
| 7871 | ILE->setValueKind(E->getValueKind()); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7872 | CurInit = ILE; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7873 | break; |
| 7874 | } |
| 7875 | |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7876 | case SK_ConstructorInitialization: |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 7877 | case SK_StdInitializerListConstructorCall: { |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7878 | if (checkAbstractType(Step->Type)) |
| 7879 | return ExprError(); |
| 7880 | |
Sebastian Redl | 99f6616 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 7881 | // When an initializer list is passed for a parameter of type "reference |
| 7882 | // to object", we don't get an EK_Temporary entity, but instead an |
| 7883 | // EK_Parameter entity with reference type. |
| 7884 | // FIXME: This is a hack. What we really should do is create a user |
| 7885 | // conversion step for this case, but this makes it considerably more |
| 7886 | // complicated. For now, this will do. |
| 7887 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 7888 | Entity.getType().getNonReferenceType()); |
| 7889 | bool UseTemporary = Entity.getType()->isReferenceType(); |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 7890 | bool IsStdInitListInit = |
| 7891 | Step->Kind == SK_StdInitializerListConstructorCall; |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7892 | Expr *Source = CurInit.get(); |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 7893 | SourceRange Range = Kind.hasParenOrBraceRange() |
| 7894 | ? Kind.getParenOrBraceRange() |
| 7895 | : SourceRange(); |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7896 | CurInit = PerformConstructorInitialization( |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7897 | S, UseTemporary ? TempEntity : Entity, Kind, |
| 7898 | Source ? MultiExprArg(Source) : Args, *Step, |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7899 | ConstructorInitRequiresZeroInit, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7900 | /*IsListInitialization*/ IsStdInitListInit, |
| 7901 | /*IsStdInitListInitialization*/ IsStdInitListInit, |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 7902 | /*LBraceLoc*/ Range.getBegin(), |
| 7903 | /*RBraceLoc*/ Range.getEnd()); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 7904 | break; |
Sebastian Redl | 99f6616 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 7905 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7906 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 7907 | case SK_ZeroInitialization: { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7908 | step_iterator NextStep = Step; |
| 7909 | ++NextStep; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7910 | if (NextStep != StepEnd && |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 7911 | (NextStep->Kind == SK_ConstructorInitialization || |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7912 | NextStep->Kind == SK_ConstructorInitializationFromList)) { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7913 | // The need for zero-initialization is recorded directly into |
| 7914 | // the call to the object's constructor within the next step. |
| 7915 | ConstructorInitRequiresZeroInit = true; |
| 7916 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7917 | S.getLangOpts().CPlusPlus && |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7918 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 7919 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 7920 | if (!TSInfo) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7921 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 7922 | Kind.getRange().getBegin()); |
| 7923 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7924 | CurInit = new (S.Context) CXXScalarValueInitExpr( |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 7925 | Entity.getType().getNonLValueExprType(S.Context), TSInfo, |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7926 | Kind.getRange().getEnd()); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7927 | } else { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7928 | CurInit = new (S.Context) ImplicitValueInitExpr(Step->Type); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7929 | } |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 7930 | break; |
| 7931 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7932 | |
| 7933 | case SK_CAssignment: { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7934 | QualType SourceType = CurInit.get()->getType(); |
Leonard Chan | ad7ac96 | 2018-12-06 01:05:54 +0000 | [diff] [blame] | 7935 | |
George Burgess IV | 5f21c71 | 2015-10-12 19:57:04 +0000 | [diff] [blame] | 7936 | // Save off the initial CurInit in case we need to emit a diagnostic |
| 7937 | ExprResult InitialCurInit = CurInit; |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7938 | ExprResult Result = CurInit; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7939 | Sema::AssignConvertType ConvTy = |
Fariborz Jahanian | 25eef19 | 2013-07-31 21:40:51 +0000 | [diff] [blame] | 7940 | S.CheckSingleAssignmentConstraints(Step->Type, Result, true, |
| 7941 | Entity.getKind() == InitializedEntity::EK_Parameter_CF_Audited); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7942 | if (Result.isInvalid()) |
| 7943 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7944 | CurInit = Result; |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 7945 | |
| 7946 | // If this is a call, allow conversion to a transparent union. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7947 | ExprResult CurInitExprRes = CurInit; |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 7948 | if (ConvTy != Sema::Compatible && |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 7949 | Entity.isParameterKind() && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7950 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes) |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 7951 | == Sema::Compatible) |
| 7952 | ConvTy = Sema::Compatible; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7953 | if (CurInitExprRes.isInvalid()) |
| 7954 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7955 | CurInit = CurInitExprRes; |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 7956 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 7957 | bool Complained; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7958 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 7959 | Step->Type, SourceType, |
George Burgess IV | 5f21c71 | 2015-10-12 19:57:04 +0000 | [diff] [blame] | 7960 | InitialCurInit.get(), |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 7961 | getAssignmentAction(Entity, true), |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 7962 | &Complained)) { |
| 7963 | PrintInitLocationNote(S, Entity); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7964 | return ExprError(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 7965 | } else if (Complained) |
| 7966 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7967 | break; |
| 7968 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 7969 | |
| 7970 | case SK_StringInit: { |
| 7971 | QualType Ty = Step->Type; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7972 | CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty, |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 7973 | S.Context.getAsArrayType(Ty), S); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 7974 | break; |
| 7975 | } |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 7976 | |
| 7977 | case SK_ObjCObjectConversion: |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7978 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7979 | CK_ObjCObjectLValueCast, |
Eli Friedman | be4b363 | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 7980 | CurInit.get()->getValueKind()); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 7981 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7982 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7983 | case SK_ArrayLoopIndex: { |
| 7984 | Expr *Cur = CurInit.get(); |
| 7985 | Expr *BaseExpr = new (S.Context) |
| 7986 | OpaqueValueExpr(Cur->getExprLoc(), Cur->getType(), |
| 7987 | Cur->getValueKind(), Cur->getObjectKind(), Cur); |
| 7988 | Expr *IndexExpr = |
| 7989 | new (S.Context) ArrayInitIndexExpr(S.Context.getSizeType()); |
| 7990 | CurInit = S.CreateBuiltinArraySubscriptExpr( |
| 7991 | BaseExpr, Kind.getLocation(), IndexExpr, Kind.getLocation()); |
| 7992 | ArrayLoopCommonExprs.push_back(BaseExpr); |
| 7993 | break; |
| 7994 | } |
| 7995 | |
| 7996 | case SK_ArrayLoopInit: { |
| 7997 | assert(!ArrayLoopCommonExprs.empty() && |
| 7998 | "mismatched SK_ArrayLoopIndex and SK_ArrayLoopInit"); |
| 7999 | Expr *Common = ArrayLoopCommonExprs.pop_back_val(); |
| 8000 | CurInit = new (S.Context) ArrayInitLoopExpr(Step->Type, Common, |
| 8001 | CurInit.get()); |
| 8002 | break; |
| 8003 | } |
| 8004 | |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 8005 | case SK_GNUArrayInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8006 | // Okay: we checked everything before creating this step. Note that |
| 8007 | // this is a GNU extension. |
| 8008 | S.Diag(Kind.getLocation(), diag::ext_array_init_copy) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8009 | << Step->Type << CurInit.get()->getType() |
| 8010 | << CurInit.get()->getSourceRange(); |
Eli Friedman | 88fccbd | 2019-02-11 22:54:27 +0000 | [diff] [blame] | 8011 | updateGNUCompoundLiteralRValue(CurInit.get()); |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 8012 | LLVM_FALLTHROUGH; |
| 8013 | case SK_ArrayInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8014 | // If the destination type is an incomplete array type, update the |
| 8015 | // type accordingly. |
| 8016 | if (ResultType) { |
| 8017 | if (const IncompleteArrayType *IncompleteDest |
| 8018 | = S.Context.getAsIncompleteArrayType(Step->Type)) { |
| 8019 | if (const ConstantArrayType *ConstantSource |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8020 | = S.Context.getAsConstantArrayType(CurInit.get()->getType())) { |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8021 | *ResultType = S.Context.getConstantArrayType( |
| 8022 | IncompleteDest->getElementType(), |
| 8023 | ConstantSource->getSize(), |
| 8024 | ArrayType::Normal, 0); |
| 8025 | } |
| 8026 | } |
| 8027 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8028 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8029 | |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 8030 | case SK_ParenthesizedArrayInit: |
| 8031 | // Okay: we checked everything before creating this step. Note that |
| 8032 | // this is a GNU extension. |
| 8033 | S.Diag(Kind.getLocation(), diag::ext_array_init_parens) |
| 8034 | << CurInit.get()->getSourceRange(); |
| 8035 | break; |
| 8036 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8037 | case SK_PassByIndirectCopyRestore: |
| 8038 | case SK_PassByIndirectRestore: |
| 8039 | checkIndirectCopyRestoreSource(S, CurInit.get()); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 8040 | CurInit = new (S.Context) ObjCIndirectCopyRestoreExpr( |
| 8041 | CurInit.get(), Step->Type, |
| 8042 | Step->Kind == SK_PassByIndirectCopyRestore); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8043 | break; |
| 8044 | |
| 8045 | case SK_ProduceObjCObject: |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 8046 | CurInit = |
| 8047 | ImplicitCastExpr::Create(S.Context, Step->Type, CK_ARCProduceObject, |
| 8048 | CurInit.get(), nullptr, VK_RValue); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8049 | break; |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 8050 | |
| 8051 | case SK_StdInitializerList: { |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 8052 | S.Diag(CurInit.get()->getExprLoc(), |
| 8053 | diag::warn_cxx98_compat_initializer_list_init) |
| 8054 | << CurInit.get()->getSourceRange(); |
Sebastian Redl | 249dee5 | 2012-03-05 19:35:43 +0000 | [diff] [blame] | 8055 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 8056 | // Materialize the temporary into memory. |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 8057 | MaterializeTemporaryExpr *MTE = S.CreateMaterializeTemporaryExpr( |
| 8058 | CurInit.get()->getType(), CurInit.get(), |
| 8059 | /*BoundToLvalueReference=*/false); |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 8060 | |
Florian Hahn | 0aa117d | 2018-07-17 09:23:31 +0000 | [diff] [blame] | 8061 | // Wrap it in a construction of a std::initializer_list<T>. |
| 8062 | CurInit = new (S.Context) CXXStdInitializerListExpr(Step->Type, MTE); |
Richard Smith | 0a9969b | 2018-07-17 00:11:41 +0000 | [diff] [blame] | 8063 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 8064 | // Bind the result, in case the library has given initializer_list a |
| 8065 | // non-trivial destructor. |
| 8066 | if (shouldBindAsTemporary(Entity)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 8067 | CurInit = S.MaybeBindToTemporary(CurInit.get()); |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 8068 | break; |
| 8069 | } |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 8070 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 8071 | case SK_OCLSamplerInit: { |
Raphael Isemann | b23ccec | 2018-12-10 12:37:46 +0000 | [diff] [blame] | 8072 | // Sampler initialization have 5 cases: |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 8073 | // 1. function argument passing |
| 8074 | // 1a. argument is a file-scope variable |
| 8075 | // 1b. argument is a function-scope variable |
| 8076 | // 1c. argument is one of caller function's parameters |
| 8077 | // 2. variable initialization |
| 8078 | // 2a. initializing a file-scope variable |
| 8079 | // 2b. initializing a function-scope variable |
| 8080 | // |
| 8081 | // For file-scope variables, since they cannot be initialized by function |
| 8082 | // call of __translate_sampler_initializer in LLVM IR, their references |
| 8083 | // need to be replaced by a cast from their literal initializers to |
| 8084 | // sampler type. Since sampler variables can only be used in function |
| 8085 | // calls as arguments, we only need to replace them when handling the |
| 8086 | // argument passing. |
| 8087 | assert(Step->Type->isSamplerT() && |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 8088 | "Sampler initialization on non-sampler type."); |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 8089 | Expr *Init = CurInit.get(); |
| 8090 | QualType SourceType = Init->getType(); |
| 8091 | // Case 1 |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 8092 | if (Entity.isParameterKind()) { |
Egor Churaev | a8d2451 | 2017-04-05 09:02:56 +0000 | [diff] [blame] | 8093 | if (!SourceType->isSamplerT() && !SourceType->isIntegerType()) { |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 8094 | S.Diag(Kind.getLocation(), diag::err_sampler_argument_required) |
| 8095 | << SourceType; |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 8096 | break; |
| 8097 | } else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Init)) { |
| 8098 | auto Var = cast<VarDecl>(DRE->getDecl()); |
| 8099 | // Case 1b and 1c |
| 8100 | // No cast from integer to sampler is needed. |
| 8101 | if (!Var->hasGlobalStorage()) { |
| 8102 | CurInit = ImplicitCastExpr::Create(S.Context, Step->Type, |
| 8103 | CK_LValueToRValue, Init, |
| 8104 | /*BasePath=*/nullptr, VK_RValue); |
| 8105 | break; |
| 8106 | } |
| 8107 | // Case 1a |
| 8108 | // For function call with a file-scope sampler variable as argument, |
| 8109 | // get the integer literal. |
| 8110 | // Do not diagnose if the file-scope variable does not have initializer |
| 8111 | // since this has already been diagnosed when parsing the variable |
| 8112 | // declaration. |
| 8113 | if (!Var->getInit() || !isa<ImplicitCastExpr>(Var->getInit())) |
| 8114 | break; |
| 8115 | Init = cast<ImplicitCastExpr>(const_cast<Expr*>( |
| 8116 | Var->getInit()))->getSubExpr(); |
| 8117 | SourceType = Init->getType(); |
| 8118 | } |
| 8119 | } else { |
| 8120 | // Case 2 |
| 8121 | // Check initializer is 32 bit integer constant. |
| 8122 | // If the initializer is taken from global variable, do not diagnose since |
| 8123 | // this has already been done when parsing the variable declaration. |
| 8124 | if (!Init->isConstantInitializer(S.Context, false)) |
| 8125 | break; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8126 | |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 8127 | if (!SourceType->isIntegerType() || |
| 8128 | 32 != S.Context.getIntWidth(SourceType)) { |
| 8129 | S.Diag(Kind.getLocation(), diag::err_sampler_initializer_not_integer) |
| 8130 | << SourceType; |
| 8131 | break; |
| 8132 | } |
| 8133 | |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 8134 | Expr::EvalResult EVResult; |
| 8135 | Init->EvaluateAsInt(EVResult, S.Context); |
| 8136 | llvm::APSInt Result = EVResult.Val.getInt(); |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 8137 | const uint64_t SamplerValue = Result.getLimitedValue(); |
| 8138 | // 32-bit value of sampler's initializer is interpreted as |
| 8139 | // bit-field with the following structure: |
| 8140 | // |unspecified|Filter|Addressing Mode| Normalized Coords| |
| 8141 | // |31 6|5 4|3 1| 0| |
| 8142 | // This structure corresponds to enum values of sampler properties |
| 8143 | // defined in SPIR spec v1.2 and also opencl-c.h |
| 8144 | unsigned AddressingMode = (0x0E & SamplerValue) >> 1; |
| 8145 | unsigned FilterMode = (0x30 & SamplerValue) >> 4; |
Andrew Savonichev | 3fee351 | 2018-11-08 11:25:41 +0000 | [diff] [blame] | 8146 | if (FilterMode != 1 && FilterMode != 2 && |
| 8147 | !S.getOpenCLOptions().isEnabled( |
| 8148 | "cl_intel_device_side_avc_motion_estimation")) |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 8149 | S.Diag(Kind.getLocation(), |
| 8150 | diag::warn_sampler_initializer_invalid_bits) |
| 8151 | << "Filter Mode"; |
| 8152 | if (AddressingMode > 4) |
| 8153 | S.Diag(Kind.getLocation(), |
| 8154 | diag::warn_sampler_initializer_invalid_bits) |
| 8155 | << "Addressing Mode"; |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 8156 | } |
| 8157 | |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 8158 | // Cases 1a, 2a and 2b |
| 8159 | // Insert cast from integer to sampler. |
| 8160 | CurInit = S.ImpCastExprToType(Init, S.Context.OCLSamplerTy, |
| 8161 | CK_IntToOCLSampler); |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 8162 | break; |
| 8163 | } |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 8164 | case SK_OCLZeroOpaqueType: { |
Andrew Savonichev | 3fee351 | 2018-11-08 11:25:41 +0000 | [diff] [blame] | 8165 | assert((Step->Type->isEventT() || Step->Type->isQueueT() || |
| 8166 | Step->Type->isOCLIntelSubgroupAVCType()) && |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 8167 | "Wrong type for initialization of OpenCL opaque type."); |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 8168 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 8169 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 8170 | CK_ZeroToOCLOpaqueType, |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 8171 | CurInit.get()->getValueKind()); |
| 8172 | break; |
| 8173 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8174 | } |
| 8175 | } |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 8176 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 8177 | // Check whether the initializer has a shorter lifetime than the initialized |
| 8178 | // entity, and if not, either lifetime-extend or warn as appropriate. |
| 8179 | if (auto *Init = CurInit.get()) |
| 8180 | S.checkInitializerLifetime(Entity, Init); |
| 8181 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 8182 | // Diagnose non-fatal problems with the completed initialization. |
| 8183 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 8184 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 8185 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 8186 | cast<FieldDecl>(Entity.getDecl()), |
| 8187 | CurInit.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8188 | |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 8189 | // Check for std::move on construction. |
| 8190 | if (const Expr *E = CurInit.get()) { |
| 8191 | CheckMoveOnConstruction(S, E, |
| 8192 | Entity.getKind() == InitializedEntity::EK_Result); |
| 8193 | } |
| 8194 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 8195 | return CurInit; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8196 | } |
| 8197 | |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 8198 | /// Somewhere within T there is an uninitialized reference subobject. |
| 8199 | /// Dig it out and diagnose it. |
Benjamin Kramer | 3e35026 | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 8200 | static bool DiagnoseUninitializedReference(Sema &S, SourceLocation Loc, |
| 8201 | QualType T) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 8202 | if (T->isReferenceType()) { |
| 8203 | S.Diag(Loc, diag::err_reference_without_init) |
| 8204 | << T.getNonReferenceType(); |
| 8205 | return true; |
| 8206 | } |
| 8207 | |
| 8208 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 8209 | if (!RD || !RD->hasUninitializedReferenceMember()) |
| 8210 | return false; |
| 8211 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 8212 | for (const auto *FI : RD->fields()) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 8213 | if (FI->isUnnamedBitfield()) |
| 8214 | continue; |
| 8215 | |
| 8216 | if (DiagnoseUninitializedReference(S, FI->getLocation(), FI->getType())) { |
| 8217 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 8218 | return true; |
| 8219 | } |
| 8220 | } |
| 8221 | |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 8222 | for (const auto &BI : RD->bases()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8223 | if (DiagnoseUninitializedReference(S, BI.getBeginLoc(), BI.getType())) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 8224 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 8225 | return true; |
| 8226 | } |
| 8227 | } |
| 8228 | |
| 8229 | return false; |
| 8230 | } |
| 8231 | |
| 8232 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8233 | //===----------------------------------------------------------------------===// |
| 8234 | // Diagnose initialization failures |
| 8235 | //===----------------------------------------------------------------------===// |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 8236 | |
| 8237 | /// Emit notes associated with an initialization that failed due to a |
| 8238 | /// "simple" conversion failure. |
| 8239 | static void emitBadConversionNotes(Sema &S, const InitializedEntity &entity, |
| 8240 | Expr *op) { |
| 8241 | QualType destType = entity.getType(); |
| 8242 | if (destType.getNonReferenceType()->isObjCObjectPointerType() && |
| 8243 | op->getType()->isObjCObjectPointerType()) { |
| 8244 | |
| 8245 | // Emit a possible note about the conversion failing because the |
| 8246 | // operand is a message send with a related result type. |
| 8247 | S.EmitRelatedResultTypeNote(op); |
| 8248 | |
| 8249 | // Emit a possible note about a return failing because we're |
| 8250 | // expecting a related result type. |
| 8251 | if (entity.getKind() == InitializedEntity::EK_Result) |
| 8252 | S.EmitRelatedResultTypeNoteForReturn(destType); |
| 8253 | } |
| 8254 | } |
| 8255 | |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 8256 | static void diagnoseListInit(Sema &S, const InitializedEntity &Entity, |
| 8257 | InitListExpr *InitList) { |
| 8258 | QualType DestType = Entity.getType(); |
| 8259 | |
| 8260 | QualType E; |
| 8261 | if (S.getLangOpts().CPlusPlus11 && S.isStdInitializerList(DestType, &E)) { |
| 8262 | QualType ArrayType = S.Context.getConstantArrayType( |
| 8263 | E.withConst(), |
| 8264 | llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 8265 | InitList->getNumInits()), |
| 8266 | clang::ArrayType::Normal, 0); |
| 8267 | InitializedEntity HiddenArray = |
| 8268 | InitializedEntity::InitializeTemporary(ArrayType); |
| 8269 | return diagnoseListInit(S, HiddenArray, InitList); |
| 8270 | } |
| 8271 | |
Richard Smith | 8d082d1 | 2014-09-04 22:13:39 +0000 | [diff] [blame] | 8272 | if (DestType->isReferenceType()) { |
| 8273 | // A list-initialization failure for a reference means that we tried to |
| 8274 | // create a temporary of the inner type (per [dcl.init.list]p3.6) and the |
| 8275 | // inner initialization failed. |
| 8276 | QualType T = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 8277 | diagnoseListInit(S, InitializedEntity::InitializeTemporary(T), InitList); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8278 | SourceLocation Loc = InitList->getBeginLoc(); |
Richard Smith | 8d082d1 | 2014-09-04 22:13:39 +0000 | [diff] [blame] | 8279 | if (auto *D = Entity.getDecl()) |
| 8280 | Loc = D->getLocation(); |
| 8281 | S.Diag(Loc, diag::note_in_reference_temporary_list_initializer) << T; |
| 8282 | return; |
| 8283 | } |
| 8284 | |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 8285 | InitListChecker DiagnoseInitList(S, Entity, InitList, DestType, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 8286 | /*VerifyOnly=*/false, |
| 8287 | /*TreatUnavailableAsInvalid=*/false); |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 8288 | assert(DiagnoseInitList.HadError() && |
| 8289 | "Inconsistent init list check result."); |
| 8290 | } |
| 8291 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8292 | bool InitializationSequence::Diagnose(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8293 | const InitializedEntity &Entity, |
| 8294 | const InitializationKind &Kind, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8295 | ArrayRef<Expr *> Args) { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 8296 | if (!Failed()) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8297 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8298 | |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8299 | // When we want to diagnose only one element of a braced-init-list, |
| 8300 | // we need to factor it out. |
| 8301 | Expr *OnlyArg; |
| 8302 | if (Args.size() == 1) { |
| 8303 | auto *List = dyn_cast<InitListExpr>(Args[0]); |
| 8304 | if (List && List->getNumInits() == 1) |
| 8305 | OnlyArg = List->getInit(0); |
| 8306 | else |
| 8307 | OnlyArg = Args[0]; |
| 8308 | } |
| 8309 | else |
| 8310 | OnlyArg = nullptr; |
| 8311 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 8312 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8313 | switch (Failure) { |
| 8314 | case FK_TooManyInitsForReference: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8315 | // FIXME: Customize for the initialized entity? |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8316 | if (Args.empty()) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 8317 | // Dig out the reference subobject which is uninitialized and diagnose it. |
| 8318 | // If this is value-initialization, this could be nested some way within |
| 8319 | // the target type. |
| 8320 | assert(Kind.getKind() == InitializationKind::IK_Value || |
| 8321 | DestType->isReferenceType()); |
| 8322 | bool Diagnosed = |
| 8323 | DiagnoseUninitializedReference(S, Kind.getLocation(), DestType); |
| 8324 | assert(Diagnosed && "couldn't find uninitialized reference to diagnose"); |
| 8325 | (void)Diagnosed; |
| 8326 | } else // FIXME: diagnostic below could be better! |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8327 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 8328 | << SourceRange(Args.front()->getBeginLoc(), Args.back()->getEndLoc()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8329 | break; |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 8330 | case FK_ParenthesizedListInitForReference: |
| 8331 | S.Diag(Kind.getLocation(), diag::err_list_init_in_parens) |
| 8332 | << 1 << Entity.getType() << Args[0]->getSourceRange(); |
| 8333 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8334 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8335 | case FK_ArrayNeedsInitList: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 8336 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 0; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8337 | break; |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 8338 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 8339 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 1; |
| 8340 | break; |
| 8341 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 8342 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 2; |
| 8343 | break; |
| 8344 | case FK_NarrowStringIntoWideCharArray: |
| 8345 | S.Diag(Kind.getLocation(), diag::err_array_init_narrow_string_into_wchar); |
| 8346 | break; |
| 8347 | case FK_WideStringIntoCharArray: |
| 8348 | S.Diag(Kind.getLocation(), diag::err_array_init_wide_string_into_char); |
| 8349 | break; |
| 8350 | case FK_IncompatWideStringIntoWideChar: |
| 8351 | S.Diag(Kind.getLocation(), |
| 8352 | diag::err_array_init_incompat_wide_string_into_wchar); |
| 8353 | break; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 8354 | case FK_PlainStringIntoUTF8Char: |
| 8355 | S.Diag(Kind.getLocation(), |
| 8356 | diag::err_array_init_plain_string_into_char8_t); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8357 | S.Diag(Args.front()->getBeginLoc(), |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 8358 | diag::note_array_init_plain_string_into_char8_t) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8359 | << FixItHint::CreateInsertion(Args.front()->getBeginLoc(), "u8"); |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 8360 | break; |
| 8361 | case FK_UTF8StringIntoPlainChar: |
| 8362 | S.Diag(Kind.getLocation(), |
Richard Smith | 28ddb91 | 2018-11-14 21:04:34 +0000 | [diff] [blame] | 8363 | diag::err_array_init_utf8_string_into_char) |
| 8364 | << S.getLangOpts().CPlusPlus2a; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 8365 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8366 | case FK_ArrayTypeMismatch: |
| 8367 | case FK_NonConstantArrayInit: |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 8368 | S.Diag(Kind.getLocation(), |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8369 | (Failure == FK_ArrayTypeMismatch |
| 8370 | ? diag::err_array_init_different_type |
| 8371 | : diag::err_array_init_non_constant_array)) |
| 8372 | << DestType.getNonReferenceType() |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8373 | << OnlyArg->getType() |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8374 | << Args[0]->getSourceRange(); |
| 8375 | break; |
| 8376 | |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 8377 | case FK_VariableLengthArrayHasInitializer: |
| 8378 | S.Diag(Kind.getLocation(), diag::err_variable_object_no_init) |
| 8379 | << Args[0]->getSourceRange(); |
| 8380 | break; |
| 8381 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8382 | case FK_AddressOfOverloadFailed: { |
| 8383 | DeclAccessPair Found; |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8384 | S.ResolveAddressOfOverloadedFunction(OnlyArg, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8385 | DestType.getNonReferenceType(), |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8386 | true, |
| 8387 | Found); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8388 | break; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8389 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8390 | |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 8391 | case FK_AddressOfUnaddressableFunction: { |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8392 | auto *FD = cast<FunctionDecl>(cast<DeclRefExpr>(OnlyArg)->getDecl()); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 8393 | S.checkAddressOfFunctionIsAvailable(FD, /*Complain=*/true, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8394 | OnlyArg->getBeginLoc()); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 8395 | break; |
| 8396 | } |
| 8397 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8398 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 8399 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8400 | switch (FailedOverloadResult) { |
| 8401 | case OR_Ambiguous: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8402 | if (Failure == FK_UserConversionOverloadFailed) |
| 8403 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8404 | << OnlyArg->getType() << DestType |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8405 | << Args[0]->getSourceRange(); |
| 8406 | else |
| 8407 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8408 | << DestType << OnlyArg->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8409 | << Args[0]->getSourceRange(); |
| 8410 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8411 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8412 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8413 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8414 | case OR_No_Viable_Function: |
Larisse Voufo | 70bb43a | 2013-06-27 03:36:30 +0000 | [diff] [blame] | 8415 | if (!S.RequireCompleteType(Kind.getLocation(), |
Larisse Voufo | 64cf3ef | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 8416 | DestType.getNonReferenceType(), |
| 8417 | diag::err_typecheck_nonviable_condition_incomplete, |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8418 | OnlyArg->getType(), Args[0]->getSourceRange())) |
Larisse Voufo | 64cf3ef | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 8419 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
Nick Lewycky | 08426e2 | 2015-08-25 22:18:46 +0000 | [diff] [blame] | 8420 | << (Entity.getKind() == InitializedEntity::EK_Result) |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8421 | << OnlyArg->getType() << Args[0]->getSourceRange() |
Larisse Voufo | 64cf3ef | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 8422 | << DestType.getNonReferenceType(); |
| 8423 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8424 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8425 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8426 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8427 | case OR_Deleted: { |
| 8428 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8429 | << OnlyArg->getType() << DestType.getNonReferenceType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8430 | << Args[0]->getSourceRange(); |
| 8431 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8432 | OverloadingResult Ovl |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 8433 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8434 | if (Ovl == OR_Deleted) { |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 8435 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8436 | } else { |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 8437 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8438 | } |
| 8439 | break; |
| 8440 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8441 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8442 | case OR_Success: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 8443 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8444 | } |
| 8445 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8446 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8447 | case FK_NonConstLValueReferenceBindingToTemporary: |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8448 | if (isa<InitListExpr>(Args[0])) { |
| 8449 | S.Diag(Kind.getLocation(), |
| 8450 | diag::err_lvalue_reference_bind_to_initlist) |
| 8451 | << DestType.getNonReferenceType().isVolatileQualified() |
| 8452 | << DestType.getNonReferenceType() |
| 8453 | << Args[0]->getSourceRange(); |
| 8454 | break; |
| 8455 | } |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 8456 | LLVM_FALLTHROUGH; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8457 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8458 | case FK_NonConstLValueReferenceBindingToUnrelated: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8459 | S.Diag(Kind.getLocation(), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8460 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 8461 | ? diag::err_lvalue_reference_bind_to_temporary |
| 8462 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | d1e0864 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 8463 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8464 | << DestType.getNonReferenceType() |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8465 | << OnlyArg->getType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8466 | << Args[0]->getSourceRange(); |
| 8467 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8468 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8469 | case FK_NonConstLValueReferenceBindingToBitfield: { |
| 8470 | // We don't necessarily have an unambiguous source bit-field. |
| 8471 | FieldDecl *BitField = Args[0]->getSourceBitField(); |
| 8472 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
| 8473 | << DestType.isVolatileQualified() |
| 8474 | << (BitField ? BitField->getDeclName() : DeclarationName()) |
| 8475 | << (BitField != nullptr) |
| 8476 | << Args[0]->getSourceRange(); |
| 8477 | if (BitField) |
| 8478 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
| 8479 | break; |
| 8480 | } |
| 8481 | |
| 8482 | case FK_NonConstLValueReferenceBindingToVectorElement: |
| 8483 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 8484 | << DestType.isVolatileQualified() |
| 8485 | << Args[0]->getSourceRange(); |
| 8486 | break; |
| 8487 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8488 | case FK_RValueReferenceBindingToLValue: |
| 8489 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8490 | << DestType.getNonReferenceType() << OnlyArg->getType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8491 | << Args[0]->getSourceRange(); |
| 8492 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8493 | |
Richard Trieu | f956a49 | 2015-05-16 01:27:03 +0000 | [diff] [blame] | 8494 | case FK_ReferenceInitDropsQualifiers: { |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8495 | QualType SourceType = OnlyArg->getType(); |
Richard Trieu | f956a49 | 2015-05-16 01:27:03 +0000 | [diff] [blame] | 8496 | QualType NonRefType = DestType.getNonReferenceType(); |
| 8497 | Qualifiers DroppedQualifiers = |
| 8498 | SourceType.getQualifiers() - NonRefType.getQualifiers(); |
| 8499 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8500 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
Richard Trieu | f956a49 | 2015-05-16 01:27:03 +0000 | [diff] [blame] | 8501 | << SourceType |
| 8502 | << NonRefType |
| 8503 | << DroppedQualifiers.getCVRQualifiers() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8504 | << Args[0]->getSourceRange(); |
| 8505 | break; |
Richard Trieu | f956a49 | 2015-05-16 01:27:03 +0000 | [diff] [blame] | 8506 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8507 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8508 | case FK_ReferenceInitFailed: |
| 8509 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 8510 | << DestType.getNonReferenceType() |
Eric Fiselier | 1147f71 | 2019-02-01 22:06:02 +0000 | [diff] [blame] | 8511 | << DestType.getNonReferenceType()->isIncompleteType() |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8512 | << OnlyArg->isLValue() |
| 8513 | << OnlyArg->getType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8514 | << Args[0]->getSourceRange(); |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 8515 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8516 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8517 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8518 | case FK_ConversionFailed: { |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8519 | QualType FromType = OnlyArg->getType(); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8520 | PartialDiagnostic PDiag = S.PDiag(diag::err_init_conversion_failed) |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8521 | << (int)Entity.getKind() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8522 | << DestType |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8523 | << OnlyArg->isLValue() |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8524 | << FromType |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8525 | << Args[0]->getSourceRange(); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8526 | S.HandleFunctionTypeMismatch(PDiag, FromType, DestType); |
| 8527 | S.Diag(Kind.getLocation(), PDiag); |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 8528 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8529 | break; |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8530 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8531 | |
| 8532 | case FK_ConversionFromPropertyFailed: |
| 8533 | // No-op. This error has already been reported. |
| 8534 | break; |
| 8535 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8536 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 8537 | SourceRange R; |
| 8538 | |
David Majnemer | bd38544 | 2015-04-10 04:52:06 +0000 | [diff] [blame] | 8539 | auto *InitList = dyn_cast<InitListExpr>(Args[0]); |
Benjamin Kramer | c4284e3 | 2015-09-23 16:03:53 +0000 | [diff] [blame] | 8540 | if (InitList && InitList->getNumInits() >= 1) { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 8541 | R = SourceRange(InitList->getInit(0)->getEndLoc(), InitList->getEndLoc()); |
Benjamin Kramer | c4284e3 | 2015-09-23 16:03:53 +0000 | [diff] [blame] | 8542 | } else { |
| 8543 | assert(Args.size() > 1 && "Expected multiple initializers!"); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 8544 | R = SourceRange(Args.front()->getEndLoc(), Args.back()->getEndLoc()); |
Benjamin Kramer | c4284e3 | 2015-09-23 16:03:53 +0000 | [diff] [blame] | 8545 | } |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8546 | |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 8547 | R.setBegin(S.getLocForEndOfToken(R.getBegin())); |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 8548 | if (Kind.isCStyleOrFunctionalCast()) |
| 8549 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 8550 | << R; |
| 8551 | else |
| 8552 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 8553 | << /*scalar=*/2 << R; |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8554 | break; |
| 8555 | } |
| 8556 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 8557 | case FK_ParenthesizedListInitForScalar: |
| 8558 | S.Diag(Kind.getLocation(), diag::err_list_init_in_parens) |
| 8559 | << 0 << Entity.getType() << Args[0]->getSourceRange(); |
| 8560 | break; |
| 8561 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8562 | case FK_ReferenceBindingToInitList: |
| 8563 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 8564 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 8565 | break; |
| 8566 | |
| 8567 | case FK_InitListBadDestinationType: |
| 8568 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 8569 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 8570 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8571 | |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 8572 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8573 | case FK_ConstructorOverloadFailed: { |
| 8574 | SourceRange ArgsRange; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8575 | if (Args.size()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8576 | ArgsRange = |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 8577 | SourceRange(Args.front()->getBeginLoc(), Args.back()->getEndLoc()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8578 | |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 8579 | if (Failure == FK_ListConstructorOverloadFailed) { |
Nico Weber | 9709ebf | 2014-07-08 23:54:25 +0000 | [diff] [blame] | 8580 | assert(Args.size() == 1 && |
| 8581 | "List construction from other than 1 argument."); |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 8582 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8583 | Args = MultiExprArg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 8584 | } |
| 8585 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8586 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 8587 | // bad. |
| 8588 | switch (FailedOverloadResult) { |
| 8589 | case OR_Ambiguous: |
| 8590 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 8591 | << DestType << ArgsRange; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8592 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8593 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8594 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8595 | case OR_No_Viable_Function: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8596 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 8597 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 8598 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 8599 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 8600 | // This is implicit default initialization of a member or |
| 8601 | // base within a constructor. If no viable function was |
Nico Weber | a691689 | 2016-06-10 18:53:04 +0000 | [diff] [blame] | 8602 | // found, notify the user that they need to explicitly |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8603 | // initialize this base/member. |
| 8604 | CXXConstructorDecl *Constructor |
| 8605 | = cast<CXXConstructorDecl>(S.CurContext); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 8606 | const CXXRecordDecl *InheritedFrom = nullptr; |
| 8607 | if (auto Inherited = Constructor->getInheritedConstructor()) |
| 8608 | InheritedFrom = Inherited.getShadowDecl()->getNominatedBaseClass(); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8609 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 8610 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 8611 | << (InheritedFrom ? 2 : Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8612 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 8613 | << /*base=*/0 |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 8614 | << Entity.getType() |
| 8615 | << InheritedFrom; |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8616 | |
| 8617 | RecordDecl *BaseDecl |
| 8618 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 8619 | ->getDecl(); |
| 8620 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 8621 | << S.Context.getTagDeclType(BaseDecl); |
| 8622 | } else { |
| 8623 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 8624 | << (InheritedFrom ? 2 : Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8625 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 8626 | << /*member=*/1 |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 8627 | << Entity.getName() |
| 8628 | << InheritedFrom; |
Alp Toker | 2afa878 | 2014-05-28 12:20:14 +0000 | [diff] [blame] | 8629 | S.Diag(Entity.getDecl()->getLocation(), |
| 8630 | diag::note_member_declared_at); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8631 | |
| 8632 | if (const RecordType *Record |
| 8633 | = Entity.getType()->getAs<RecordType>()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8634 | S.Diag(Record->getDecl()->getLocation(), |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8635 | diag::note_previous_decl) |
| 8636 | << S.Context.getTagDeclType(Record->getDecl()); |
| 8637 | } |
| 8638 | break; |
| 8639 | } |
| 8640 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8641 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 8642 | << DestType << ArgsRange; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8643 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8644 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8645 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8646 | case OR_Deleted: { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8647 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8648 | OverloadingResult Ovl |
| 8649 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 8650 | if (Ovl != OR_Deleted) { |
| 8651 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
Erik Pilkington | 13ee62f | 2019-03-20 19:26:33 +0000 | [diff] [blame] | 8652 | << DestType << ArgsRange; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8653 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 8654 | break; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8655 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8656 | |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 8657 | // If this is a defaulted or implicitly-declared function, then |
| 8658 | // it was implicitly deleted. Make it clear that the deletion was |
| 8659 | // implicit. |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 8660 | if (S.isImplicitlyDeleted(Best->Function)) |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 8661 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_special_init) |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 8662 | << S.getSpecialMember(cast<CXXMethodDecl>(Best->Function)) |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 8663 | << DestType << ArgsRange; |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 8664 | else |
| 8665 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
Erik Pilkington | 13ee62f | 2019-03-20 19:26:33 +0000 | [diff] [blame] | 8666 | << DestType << ArgsRange; |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 8667 | |
| 8668 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8669 | break; |
| 8670 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8671 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8672 | case OR_Success: |
| 8673 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8674 | } |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8675 | } |
David Blaikie | 60deeee | 2012-01-17 08:24:58 +0000 | [diff] [blame] | 8676 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8677 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 8678 | case FK_DefaultInitOfConst: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8679 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 8680 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 8681 | // This is implicit default-initialization of a const member in |
| 8682 | // a constructor. Complain that it needs to be explicitly |
| 8683 | // initialized. |
| 8684 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 8685 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 8686 | << (Constructor->getInheritedConstructor() ? 2 : |
| 8687 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8688 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 8689 | << /*const=*/1 |
| 8690 | << Entity.getName(); |
| 8691 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 8692 | << Entity.getName(); |
| 8693 | } else { |
| 8694 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
Nico Weber | 9386c82 | 2014-07-23 05:16:10 +0000 | [diff] [blame] | 8695 | << DestType << (bool)DestType->getAs<RecordType>(); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8696 | } |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 8697 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8698 | |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 8699 | case FK_Incomplete: |
Douglas Gregor | 85f3423 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 8700 | S.RequireCompleteType(Kind.getLocation(), FailedIncompleteType, |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 8701 | diag::err_init_incomplete_type); |
| 8702 | break; |
| 8703 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 8704 | case FK_ListInitializationFailed: { |
| 8705 | // Run the init list checker again to emit diagnostics. |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 8706 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
| 8707 | diagnoseListInit(S, Entity, InitList); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 8708 | break; |
| 8709 | } |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 8710 | |
| 8711 | case FK_PlaceholderType: { |
| 8712 | // FIXME: Already diagnosed! |
| 8713 | break; |
| 8714 | } |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 8715 | |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 8716 | case FK_ExplicitConstructor: { |
| 8717 | S.Diag(Kind.getLocation(), diag::err_selected_explicit_constructor) |
| 8718 | << Args[0]->getSourceRange(); |
| 8719 | OverloadCandidateSet::iterator Best; |
| 8720 | OverloadingResult Ovl |
| 8721 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Matt Beaumont-Gay | 5dcce09 | 2012-04-02 19:05:35 +0000 | [diff] [blame] | 8722 | (void)Ovl; |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 8723 | assert(Ovl == OR_Success && "Inconsistent overload resolution"); |
| 8724 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 8725 | S.Diag(CtorDecl->getLocation(), |
| 8726 | diag::note_explicit_ctor_deduction_guide_here) << false; |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 8727 | break; |
| 8728 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8729 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8730 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 8731 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8732 | return true; |
| 8733 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8734 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8735 | void InitializationSequence::dump(raw_ostream &OS) const { |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8736 | switch (SequenceKind) { |
| 8737 | case FailedSequence: { |
| 8738 | OS << "Failed sequence: "; |
| 8739 | switch (Failure) { |
| 8740 | case FK_TooManyInitsForReference: |
| 8741 | OS << "too many initializers for reference"; |
| 8742 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8743 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 8744 | case FK_ParenthesizedListInitForReference: |
| 8745 | OS << "parenthesized list init for reference"; |
| 8746 | break; |
| 8747 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8748 | case FK_ArrayNeedsInitList: |
| 8749 | OS << "array requires initializer list"; |
| 8750 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8751 | |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 8752 | case FK_AddressOfUnaddressableFunction: |
| 8753 | OS << "address of unaddressable function was taken"; |
| 8754 | break; |
| 8755 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8756 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 8757 | OS << "array requires initializer list or string literal"; |
| 8758 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8759 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 8760 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 8761 | OS << "array requires initializer list or wide string literal"; |
| 8762 | break; |
| 8763 | |
| 8764 | case FK_NarrowStringIntoWideCharArray: |
| 8765 | OS << "narrow string into wide char array"; |
| 8766 | break; |
| 8767 | |
| 8768 | case FK_WideStringIntoCharArray: |
| 8769 | OS << "wide string into char array"; |
| 8770 | break; |
| 8771 | |
| 8772 | case FK_IncompatWideStringIntoWideChar: |
| 8773 | OS << "incompatible wide string into wide char array"; |
| 8774 | break; |
| 8775 | |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 8776 | case FK_PlainStringIntoUTF8Char: |
| 8777 | OS << "plain string literal into char8_t array"; |
| 8778 | break; |
| 8779 | |
| 8780 | case FK_UTF8StringIntoPlainChar: |
| 8781 | OS << "u8 string literal into char array"; |
| 8782 | break; |
| 8783 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8784 | case FK_ArrayTypeMismatch: |
| 8785 | OS << "array type mismatch"; |
| 8786 | break; |
| 8787 | |
| 8788 | case FK_NonConstantArrayInit: |
| 8789 | OS << "non-constant array initializer"; |
| 8790 | break; |
| 8791 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8792 | case FK_AddressOfOverloadFailed: |
| 8793 | OS << "address of overloaded function failed"; |
| 8794 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8795 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8796 | case FK_ReferenceInitOverloadFailed: |
| 8797 | OS << "overload resolution for reference initialization failed"; |
| 8798 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8799 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8800 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 8801 | OS << "non-const lvalue reference bound to temporary"; |
| 8802 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8803 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8804 | case FK_NonConstLValueReferenceBindingToBitfield: |
| 8805 | OS << "non-const lvalue reference bound to bit-field"; |
| 8806 | break; |
| 8807 | |
| 8808 | case FK_NonConstLValueReferenceBindingToVectorElement: |
| 8809 | OS << "non-const lvalue reference bound to vector element"; |
| 8810 | break; |
| 8811 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8812 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 8813 | OS << "non-const lvalue reference bound to unrelated type"; |
| 8814 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8815 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8816 | case FK_RValueReferenceBindingToLValue: |
| 8817 | OS << "rvalue reference bound to an lvalue"; |
| 8818 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8819 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8820 | case FK_ReferenceInitDropsQualifiers: |
| 8821 | OS << "reference initialization drops qualifiers"; |
| 8822 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8823 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8824 | case FK_ReferenceInitFailed: |
| 8825 | OS << "reference initialization failed"; |
| 8826 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8827 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8828 | case FK_ConversionFailed: |
| 8829 | OS << "conversion failed"; |
| 8830 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8831 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8832 | case FK_ConversionFromPropertyFailed: |
| 8833 | OS << "conversion from property failed"; |
| 8834 | break; |
| 8835 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8836 | case FK_TooManyInitsForScalar: |
| 8837 | OS << "too many initializers for scalar"; |
| 8838 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8839 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 8840 | case FK_ParenthesizedListInitForScalar: |
| 8841 | OS << "parenthesized list init for reference"; |
| 8842 | break; |
| 8843 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8844 | case FK_ReferenceBindingToInitList: |
| 8845 | OS << "referencing binding to initializer list"; |
| 8846 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8847 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8848 | case FK_InitListBadDestinationType: |
| 8849 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 8850 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8851 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8852 | case FK_UserConversionOverloadFailed: |
| 8853 | OS << "overloading failed for user-defined conversion"; |
| 8854 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8855 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8856 | case FK_ConstructorOverloadFailed: |
| 8857 | OS << "constructor overloading failed"; |
| 8858 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8859 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8860 | case FK_DefaultInitOfConst: |
| 8861 | OS << "default initialization of a const variable"; |
| 8862 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8863 | |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 8864 | case FK_Incomplete: |
| 8865 | OS << "initialization of incomplete type"; |
| 8866 | break; |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 8867 | |
| 8868 | case FK_ListInitializationFailed: |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 8869 | OS << "list initialization checker failure"; |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 8870 | break; |
| 8871 | |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 8872 | case FK_VariableLengthArrayHasInitializer: |
| 8873 | OS << "variable length array has an initializer"; |
| 8874 | break; |
| 8875 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 8876 | case FK_PlaceholderType: |
| 8877 | OS << "initializer expression isn't contextually valid"; |
| 8878 | break; |
Nick Lewycky | 097f47c | 2011-12-22 20:21:32 +0000 | [diff] [blame] | 8879 | |
| 8880 | case FK_ListConstructorOverloadFailed: |
| 8881 | OS << "list constructor overloading failed"; |
| 8882 | break; |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 8883 | |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 8884 | case FK_ExplicitConstructor: |
| 8885 | OS << "list copy initialization chose explicit constructor"; |
| 8886 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8887 | } |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8888 | OS << '\n'; |
| 8889 | return; |
| 8890 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8891 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8892 | case DependentSequence: |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 8893 | OS << "Dependent sequence\n"; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8894 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8895 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 8896 | case NormalSequence: |
| 8897 | OS << "Normal sequence: "; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8898 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8899 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8900 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8901 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 8902 | if (S != step_begin()) { |
| 8903 | OS << " -> "; |
| 8904 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8905 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8906 | switch (S->Kind) { |
| 8907 | case SK_ResolveAddressOfOverloadedFunction: |
| 8908 | OS << "resolve address of overloaded function"; |
| 8909 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8910 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8911 | case SK_CastDerivedToBaseRValue: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8912 | OS << "derived-to-base (rvalue)"; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8913 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8914 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 8915 | case SK_CastDerivedToBaseXValue: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8916 | OS << "derived-to-base (xvalue)"; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 8917 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8918 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8919 | case SK_CastDerivedToBaseLValue: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8920 | OS << "derived-to-base (lvalue)"; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8921 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8922 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8923 | case SK_BindReference: |
| 8924 | OS << "bind reference to lvalue"; |
| 8925 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8926 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8927 | case SK_BindReferenceToTemporary: |
| 8928 | OS << "bind reference to a temporary"; |
| 8929 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8930 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8931 | case SK_FinalCopy: |
| 8932 | OS << "final copy in class direct-initialization"; |
| 8933 | break; |
| 8934 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 8935 | case SK_ExtraneousCopyToTemporary: |
| 8936 | OS << "extraneous C++03 copy to temporary"; |
| 8937 | break; |
| 8938 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8939 | case SK_UserConversion: |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 8940 | OS << "user-defined conversion via " << *S->Function.Function; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8941 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 8942 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8943 | case SK_QualificationConversionRValue: |
| 8944 | OS << "qualification conversion (rvalue)"; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8945 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8946 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 8947 | case SK_QualificationConversionXValue: |
| 8948 | OS << "qualification conversion (xvalue)"; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8949 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 8950 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8951 | case SK_QualificationConversionLValue: |
| 8952 | OS << "qualification conversion (lvalue)"; |
| 8953 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8954 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 8955 | case SK_AtomicConversion: |
| 8956 | OS << "non-atomic-to-atomic conversion"; |
| 8957 | break; |
| 8958 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 8959 | case SK_LValueToRValue: |
| 8960 | OS << "load (lvalue to rvalue)"; |
| 8961 | break; |
| 8962 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8963 | case SK_ConversionSequence: |
| 8964 | OS << "implicit conversion sequence ("; |
Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 8965 | S->ICS->dump(); // FIXME: use OS |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8966 | OS << ")"; |
| 8967 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8968 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 8969 | case SK_ConversionSequenceNoNarrowing: |
| 8970 | OS << "implicit conversion sequence with narrowing prohibited ("; |
Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 8971 | S->ICS->dump(); // FIXME: use OS |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 8972 | OS << ")"; |
| 8973 | break; |
| 8974 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8975 | case SK_ListInitialization: |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 8976 | OS << "list aggregate initialization"; |
| 8977 | break; |
| 8978 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8979 | case SK_UnwrapInitList: |
| 8980 | OS << "unwrap reference initializer list"; |
| 8981 | break; |
| 8982 | |
| 8983 | case SK_RewrapInitList: |
| 8984 | OS << "rewrap reference initializer list"; |
| 8985 | break; |
| 8986 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8987 | case SK_ConstructorInitialization: |
| 8988 | OS << "constructor initialization"; |
| 8989 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8990 | |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 8991 | case SK_ConstructorInitializationFromList: |
| 8992 | OS << "list initialization via constructor"; |
| 8993 | break; |
| 8994 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8995 | case SK_ZeroInitialization: |
| 8996 | OS << "zero initialization"; |
| 8997 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8998 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8999 | case SK_CAssignment: |
| 9000 | OS << "C assignment"; |
| 9001 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9002 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9003 | case SK_StringInit: |
| 9004 | OS << "string initialization"; |
| 9005 | break; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 9006 | |
| 9007 | case SK_ObjCObjectConversion: |
| 9008 | OS << "Objective-C object conversion"; |
| 9009 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 9010 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 9011 | case SK_ArrayLoopIndex: |
| 9012 | OS << "indexing for array initialization loop"; |
| 9013 | break; |
| 9014 | |
| 9015 | case SK_ArrayLoopInit: |
| 9016 | OS << "array initialization loop"; |
| 9017 | break; |
| 9018 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 9019 | case SK_ArrayInit: |
| 9020 | OS << "array initialization"; |
| 9021 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9022 | |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 9023 | case SK_GNUArrayInit: |
| 9024 | OS << "array initialization (GNU extension)"; |
| 9025 | break; |
| 9026 | |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 9027 | case SK_ParenthesizedArrayInit: |
| 9028 | OS << "parenthesized array initialization"; |
| 9029 | break; |
| 9030 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9031 | case SK_PassByIndirectCopyRestore: |
| 9032 | OS << "pass by indirect copy and restore"; |
| 9033 | break; |
| 9034 | |
| 9035 | case SK_PassByIndirectRestore: |
| 9036 | OS << "pass by indirect restore"; |
| 9037 | break; |
| 9038 | |
| 9039 | case SK_ProduceObjCObject: |
| 9040 | OS << "Objective-C object retension"; |
| 9041 | break; |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 9042 | |
| 9043 | case SK_StdInitializerList: |
| 9044 | OS << "std::initializer_list from initializer list"; |
| 9045 | break; |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 9046 | |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 9047 | case SK_StdInitializerListConstructorCall: |
| 9048 | OS << "list initialization from std::initializer_list"; |
| 9049 | break; |
| 9050 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 9051 | case SK_OCLSamplerInit: |
| 9052 | OS << "OpenCL sampler_t from integer constant"; |
| 9053 | break; |
| 9054 | |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 9055 | case SK_OCLZeroOpaqueType: |
| 9056 | OS << "OpenCL opaque type from zero"; |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 9057 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9058 | } |
Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 9059 | |
| 9060 | OS << " [" << S->Type.getAsString() << ']'; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9061 | } |
Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 9062 | |
| 9063 | OS << '\n'; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9064 | } |
| 9065 | |
| 9066 | void InitializationSequence::dump() const { |
| 9067 | dump(llvm::errs()); |
| 9068 | } |
| 9069 | |
Nico Weber | 3d7f00d | 2018-06-19 23:19:34 +0000 | [diff] [blame] | 9070 | static bool NarrowingErrs(const LangOptions &L) { |
| 9071 | return L.CPlusPlus11 && |
| 9072 | (!L.MicrosoftExt || L.isCompatibleWithMSVC(LangOptions::MSVC2015)); |
| 9073 | } |
| 9074 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 9075 | static void DiagnoseNarrowingInInitList(Sema &S, |
| 9076 | const ImplicitConversionSequence &ICS, |
| 9077 | QualType PreNarrowingType, |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9078 | QualType EntityType, |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9079 | const Expr *PostInit) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9080 | const StandardConversionSequence *SCS = nullptr; |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9081 | switch (ICS.getKind()) { |
| 9082 | case ImplicitConversionSequence::StandardConversion: |
| 9083 | SCS = &ICS.Standard; |
| 9084 | break; |
| 9085 | case ImplicitConversionSequence::UserDefinedConversion: |
| 9086 | SCS = &ICS.UserDefined.After; |
| 9087 | break; |
| 9088 | case ImplicitConversionSequence::AmbiguousConversion: |
| 9089 | case ImplicitConversionSequence::EllipsisConversion: |
| 9090 | case ImplicitConversionSequence::BadConversion: |
| 9091 | return; |
| 9092 | } |
| 9093 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9094 | // C++11 [dcl.init.list]p7: Check whether this is a narrowing conversion. |
| 9095 | APValue ConstantValue; |
Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 9096 | QualType ConstantType; |
| 9097 | switch (SCS->getNarrowingKind(S.Context, PostInit, ConstantValue, |
| 9098 | ConstantType)) { |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9099 | case NK_Not_Narrowing: |
Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 9100 | case NK_Dependent_Narrowing: |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9101 | // No narrowing occurred. |
| 9102 | return; |
| 9103 | |
| 9104 | case NK_Type_Narrowing: |
| 9105 | // This was a floating-to-integer conversion, which is always considered a |
| 9106 | // narrowing conversion even if the value is a constant and can be |
| 9107 | // represented exactly as an integer. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9108 | S.Diag(PostInit->getBeginLoc(), NarrowingErrs(S.getLangOpts()) |
Nico Weber | 3d7f00d | 2018-06-19 23:19:34 +0000 | [diff] [blame] | 9109 | ? diag::ext_init_list_type_narrowing |
| 9110 | : diag::warn_init_list_type_narrowing) |
| 9111 | << PostInit->getSourceRange() |
| 9112 | << PreNarrowingType.getLocalUnqualifiedType() |
| 9113 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9114 | break; |
| 9115 | |
| 9116 | case NK_Constant_Narrowing: |
| 9117 | // A constant value was narrowed. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9118 | S.Diag(PostInit->getBeginLoc(), |
Nico Weber | 3d7f00d | 2018-06-19 23:19:34 +0000 | [diff] [blame] | 9119 | NarrowingErrs(S.getLangOpts()) |
| 9120 | ? diag::ext_init_list_constant_narrowing |
| 9121 | : diag::warn_init_list_constant_narrowing) |
| 9122 | << PostInit->getSourceRange() |
| 9123 | << ConstantValue.getAsString(S.getASTContext(), ConstantType) |
| 9124 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9125 | break; |
| 9126 | |
| 9127 | case NK_Variable_Narrowing: |
| 9128 | // A variable's value may have been narrowed. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9129 | S.Diag(PostInit->getBeginLoc(), |
Nico Weber | 3d7f00d | 2018-06-19 23:19:34 +0000 | [diff] [blame] | 9130 | NarrowingErrs(S.getLangOpts()) |
| 9131 | ? diag::ext_init_list_variable_narrowing |
| 9132 | : diag::warn_init_list_variable_narrowing) |
| 9133 | << PostInit->getSourceRange() |
| 9134 | << PreNarrowingType.getLocalUnqualifiedType() |
| 9135 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9136 | break; |
| 9137 | } |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 9138 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 9139 | SmallString<128> StaticCast; |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 9140 | llvm::raw_svector_ostream OS(StaticCast); |
| 9141 | OS << "static_cast<"; |
| 9142 | if (const TypedefType *TT = EntityType->getAs<TypedefType>()) { |
| 9143 | // It's important to use the typedef's name if there is one so that the |
| 9144 | // fixit doesn't break code using types like int64_t. |
| 9145 | // |
| 9146 | // FIXME: This will break if the typedef requires qualification. But |
| 9147 | // getQualifiedNameAsString() includes non-machine-parsable components. |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 9148 | OS << *TT->getDecl(); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 9149 | } else if (const BuiltinType *BT = EntityType->getAs<BuiltinType>()) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9150 | OS << BT->getName(S.getLangOpts()); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 9151 | else { |
| 9152 | // Oops, we didn't find the actual type of the variable. Don't emit a fixit |
| 9153 | // with a broken cast. |
| 9154 | return; |
| 9155 | } |
| 9156 | OS << ">("; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9157 | S.Diag(PostInit->getBeginLoc(), diag::note_init_list_narrowing_silence) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 9158 | << PostInit->getSourceRange() |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9159 | << FixItHint::CreateInsertion(PostInit->getBeginLoc(), OS.str()) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 9160 | << FixItHint::CreateInsertion( |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 9161 | S.getLocForEndOfToken(PostInit->getEndLoc()), ")"); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 9162 | } |
| 9163 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 9164 | //===----------------------------------------------------------------------===// |
| 9165 | // Initialization helper functions |
| 9166 | //===----------------------------------------------------------------------===// |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 9167 | bool |
| 9168 | Sema::CanPerformCopyInitialization(const InitializedEntity &Entity, |
| 9169 | ExprResult Init) { |
| 9170 | if (Init.isInvalid()) |
| 9171 | return false; |
| 9172 | |
| 9173 | Expr *InitE = Init.get(); |
| 9174 | assert(InitE && "No initialization expression"); |
| 9175 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9176 | InitializationKind Kind = |
| 9177 | InitializationKind::CreateCopy(InitE->getBeginLoc(), SourceLocation()); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 9178 | InitializationSequence Seq(*this, Entity, Kind, InitE); |
Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 9179 | return !Seq.Failed(); |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 9180 | } |
| 9181 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9182 | ExprResult |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 9183 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 9184 | SourceLocation EqualLoc, |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 9185 | ExprResult Init, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 9186 | bool TopLevelOfInitList, |
| 9187 | bool AllowExplicit) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 9188 | if (Init.isInvalid()) |
| 9189 | return ExprError(); |
| 9190 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 9191 | Expr *InitE = Init.get(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 9192 | assert(InitE && "No initialization expression?"); |
| 9193 | |
| 9194 | if (EqualLoc.isInvalid()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9195 | EqualLoc = InitE->getBeginLoc(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 9196 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9197 | InitializationKind Kind = InitializationKind::CreateCopy( |
| 9198 | InitE->getBeginLoc(), EqualLoc, AllowExplicit); |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 9199 | InitializationSequence Seq(*this, Entity, Kind, InitE, TopLevelOfInitList); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 9200 | |
Alex Lorenz | de69ff9 | 2017-05-16 10:23:58 +0000 | [diff] [blame] | 9201 | // Prevent infinite recursion when performing parameter copy-initialization. |
| 9202 | const bool ShouldTrackCopy = |
| 9203 | Entity.isParameterKind() && Seq.isConstructorInitialization(); |
| 9204 | if (ShouldTrackCopy) { |
| 9205 | if (llvm::find(CurrentParameterCopyTypes, Entity.getType()) != |
| 9206 | CurrentParameterCopyTypes.end()) { |
| 9207 | Seq.SetOverloadFailure( |
| 9208 | InitializationSequence::FK_ConstructorOverloadFailed, |
| 9209 | OR_No_Viable_Function); |
| 9210 | |
| 9211 | // Try to give a meaningful diagnostic note for the problematic |
| 9212 | // constructor. |
| 9213 | const auto LastStep = Seq.step_end() - 1; |
| 9214 | assert(LastStep->Kind == |
| 9215 | InitializationSequence::SK_ConstructorInitialization); |
| 9216 | const FunctionDecl *Function = LastStep->Function.Function; |
| 9217 | auto Candidate = |
| 9218 | llvm::find_if(Seq.getFailedCandidateSet(), |
| 9219 | [Function](const OverloadCandidate &Candidate) -> bool { |
| 9220 | return Candidate.Viable && |
| 9221 | Candidate.Function == Function && |
| 9222 | Candidate.Conversions.size() > 0; |
| 9223 | }); |
| 9224 | if (Candidate != Seq.getFailedCandidateSet().end() && |
| 9225 | Function->getNumParams() > 0) { |
| 9226 | Candidate->Viable = false; |
| 9227 | Candidate->FailureKind = ovl_fail_bad_conversion; |
| 9228 | Candidate->Conversions[0].setBad(BadConversionSequence::no_conversion, |
| 9229 | InitE, |
| 9230 | Function->getParamDecl(0)->getType()); |
| 9231 | } |
| 9232 | } |
| 9233 | CurrentParameterCopyTypes.push_back(Entity.getType()); |
| 9234 | } |
| 9235 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 9236 | ExprResult Result = Seq.Perform(*this, Entity, Kind, InitE); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9237 | |
Alex Lorenz | de69ff9 | 2017-05-16 10:23:58 +0000 | [diff] [blame] | 9238 | if (ShouldTrackCopy) |
| 9239 | CurrentParameterCopyTypes.pop_back(); |
| 9240 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9241 | return Result; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 9242 | } |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9243 | |
Richard Smith | 1363e8f | 2017-09-07 07:22:36 +0000 | [diff] [blame] | 9244 | /// Determine whether RD is, or is derived from, a specialization of CTD. |
| 9245 | static bool isOrIsDerivedFromSpecializationOf(CXXRecordDecl *RD, |
| 9246 | ClassTemplateDecl *CTD) { |
| 9247 | auto NotSpecialization = [&] (const CXXRecordDecl *Candidate) { |
| 9248 | auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Candidate); |
| 9249 | return !CTSD || !declaresSameEntity(CTSD->getSpecializedTemplate(), CTD); |
| 9250 | }; |
| 9251 | return !(NotSpecialization(RD) && RD->forallBases(NotSpecialization)); |
| 9252 | } |
| 9253 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9254 | QualType Sema::DeduceTemplateSpecializationFromInitializer( |
| 9255 | TypeSourceInfo *TSInfo, const InitializedEntity &Entity, |
| 9256 | const InitializationKind &Kind, MultiExprArg Inits) { |
| 9257 | auto *DeducedTST = dyn_cast<DeducedTemplateSpecializationType>( |
| 9258 | TSInfo->getType()->getContainedDeducedType()); |
| 9259 | assert(DeducedTST && "not a deduced template specialization type"); |
| 9260 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9261 | auto TemplateName = DeducedTST->getTemplateName(); |
Richard Smith | cff4201 | 2018-09-28 03:18:53 +0000 | [diff] [blame] | 9262 | if (TemplateName.isDependent()) |
| 9263 | return Context.DependentTy; |
| 9264 | |
| 9265 | // We can only perform deduction for class templates. |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9266 | auto *Template = |
| 9267 | dyn_cast_or_null<ClassTemplateDecl>(TemplateName.getAsTemplateDecl()); |
| 9268 | if (!Template) { |
| 9269 | Diag(Kind.getLocation(), |
| 9270 | diag::err_deduced_non_class_template_specialization_type) |
| 9271 | << (int)getTemplateNameKindForDiagnostics(TemplateName) << TemplateName; |
| 9272 | if (auto *TD = TemplateName.getAsTemplateDecl()) |
| 9273 | Diag(TD->getLocation(), diag::note_template_decl_here); |
| 9274 | return QualType(); |
| 9275 | } |
| 9276 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9277 | // Can't deduce from dependent arguments. |
Richard Smith | 8eeb16f | 2018-09-10 20:31:03 +0000 | [diff] [blame] | 9278 | if (Expr::hasAnyTypeDependentArguments(Inits)) { |
| 9279 | Diag(TSInfo->getTypeLoc().getBeginLoc(), |
| 9280 | diag::warn_cxx14_compat_class_template_argument_deduction) |
| 9281 | << TSInfo->getTypeLoc().getSourceRange() << 0; |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9282 | return Context.DependentTy; |
Richard Smith | 8eeb16f | 2018-09-10 20:31:03 +0000 | [diff] [blame] | 9283 | } |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9284 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9285 | // FIXME: Perform "exact type" matching first, per CWG discussion? |
| 9286 | // Or implement this via an implied 'T(T) -> T' deduction guide? |
| 9287 | |
| 9288 | // FIXME: Do we need/want a std::initializer_list<T> special case? |
| 9289 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9290 | // Look up deduction guides, including those synthesized from constructors. |
| 9291 | // |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9292 | // C++1z [over.match.class.deduct]p1: |
| 9293 | // A set of functions and function templates is formed comprising: |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9294 | // - For each constructor of the class template designated by the |
| 9295 | // template-name, a function template [...] |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9296 | // - For each deduction-guide, a function or function template [...] |
| 9297 | DeclarationNameInfo NameInfo( |
| 9298 | Context.DeclarationNames.getCXXDeductionGuideName(Template), |
| 9299 | TSInfo->getTypeLoc().getEndLoc()); |
| 9300 | LookupResult Guides(*this, NameInfo, LookupOrdinaryName); |
| 9301 | LookupQualifiedName(Guides, Template->getDeclContext()); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9302 | |
| 9303 | // FIXME: Do not diagnose inaccessible deduction guides. The standard isn't |
| 9304 | // clear on this, but they're not found by name so access does not apply. |
| 9305 | Guides.suppressDiagnostics(); |
| 9306 | |
| 9307 | // Figure out if this is list-initialization. |
| 9308 | InitListExpr *ListInit = |
| 9309 | (Inits.size() == 1 && Kind.getKind() != InitializationKind::IK_Direct) |
| 9310 | ? dyn_cast<InitListExpr>(Inits[0]) |
| 9311 | : nullptr; |
| 9312 | |
| 9313 | // C++1z [over.match.class.deduct]p1: |
| 9314 | // Initialization and overload resolution are performed as described in |
| 9315 | // [dcl.init] and [over.match.ctor], [over.match.copy], or [over.match.list] |
| 9316 | // (as appropriate for the type of initialization performed) for an object |
| 9317 | // of a hypothetical class type, where the selected functions and function |
| 9318 | // templates are considered to be the constructors of that class type |
| 9319 | // |
| 9320 | // Since we know we're initializing a class type of a type unrelated to that |
| 9321 | // of the initializer, this reduces to something fairly reasonable. |
| 9322 | OverloadCandidateSet Candidates(Kind.getLocation(), |
| 9323 | OverloadCandidateSet::CSK_Normal); |
| 9324 | OverloadCandidateSet::iterator Best; |
Eric Fiselier | 73b51ae | 2019-01-17 21:44:24 +0000 | [diff] [blame] | 9325 | |
| 9326 | bool HasAnyDeductionGuide = false; |
| 9327 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9328 | auto tryToResolveOverload = |
| 9329 | [&](bool OnlyListConstructors) -> OverloadingResult { |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 9330 | Candidates.clear(OverloadCandidateSet::CSK_Normal); |
Eric Fiselier | 73b51ae | 2019-01-17 21:44:24 +0000 | [diff] [blame] | 9331 | HasAnyDeductionGuide = false; |
| 9332 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9333 | for (auto I = Guides.begin(), E = Guides.end(); I != E; ++I) { |
| 9334 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9335 | if (D->isInvalidDecl()) |
| 9336 | continue; |
| 9337 | |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9338 | auto *TD = dyn_cast<FunctionTemplateDecl>(D); |
| 9339 | auto *GD = dyn_cast_or_null<CXXDeductionGuideDecl>( |
| 9340 | TD ? TD->getTemplatedDecl() : dyn_cast<FunctionDecl>(D)); |
| 9341 | if (!GD) |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9342 | continue; |
| 9343 | |
Eric Fiselier | 73b51ae | 2019-01-17 21:44:24 +0000 | [diff] [blame] | 9344 | if (!GD->isImplicit()) |
| 9345 | HasAnyDeductionGuide = true; |
| 9346 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9347 | // C++ [over.match.ctor]p1: (non-list copy-initialization from non-class) |
| 9348 | // For copy-initialization, the candidate functions are all the |
| 9349 | // converting constructors (12.3.1) of that class. |
| 9350 | // C++ [over.match.copy]p1: (non-list copy-initialization from class) |
| 9351 | // The converting constructors of T are candidate functions. |
| 9352 | if (Kind.isCopyInit() && !ListInit) { |
Richard Smith | afe4aa8 | 2017-02-10 02:19:05 +0000 | [diff] [blame] | 9353 | // Only consider converting constructors. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9354 | if (GD->isExplicit()) |
Richard Smith | afe4aa8 | 2017-02-10 02:19:05 +0000 | [diff] [blame] | 9355 | continue; |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9356 | |
| 9357 | // When looking for a converting constructor, deduction guides that |
Richard Smith | afe4aa8 | 2017-02-10 02:19:05 +0000 | [diff] [blame] | 9358 | // could never be called with one argument are not interesting to |
| 9359 | // check or note. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9360 | if (GD->getMinRequiredArguments() > 1 || |
| 9361 | (GD->getNumParams() == 0 && !GD->isVariadic())) |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9362 | continue; |
| 9363 | } |
| 9364 | |
| 9365 | // C++ [over.match.list]p1.1: (first phase list initialization) |
| 9366 | // Initially, the candidate functions are the initializer-list |
| 9367 | // constructors of the class T |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9368 | if (OnlyListConstructors && !isInitListConstructor(GD)) |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9369 | continue; |
| 9370 | |
| 9371 | // C++ [over.match.list]p1.2: (second phase list initialization) |
| 9372 | // the candidate functions are all the constructors of the class T |
| 9373 | // C++ [over.match.ctor]p1: (all other cases) |
| 9374 | // the candidate functions are all the constructors of the class of |
| 9375 | // the object being initialized |
| 9376 | |
| 9377 | // C++ [over.best.ics]p4: |
| 9378 | // When [...] the constructor [...] is a candidate by |
| 9379 | // - [over.match.copy] (in all cases) |
| 9380 | // FIXME: The "second phase of [over.match.list] case can also |
| 9381 | // theoretically happen here, but it's not clear whether we can |
| 9382 | // ever have a parameter of the right type. |
| 9383 | bool SuppressUserConversions = Kind.isCopyInit(); |
| 9384 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9385 | if (TD) |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9386 | AddTemplateOverloadCandidate(TD, I.getPair(), /*ExplicitArgs*/ nullptr, |
| 9387 | Inits, Candidates, |
| 9388 | SuppressUserConversions); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9389 | else |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9390 | AddOverloadCandidate(GD, I.getPair(), Inits, Candidates, |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9391 | SuppressUserConversions); |
| 9392 | } |
| 9393 | return Candidates.BestViableFunction(*this, Kind.getLocation(), Best); |
| 9394 | }; |
| 9395 | |
| 9396 | OverloadingResult Result = OR_No_Viable_Function; |
| 9397 | |
| 9398 | // C++11 [over.match.list]p1, per DR1467: for list-initialization, first |
| 9399 | // try initializer-list constructors. |
| 9400 | if (ListInit) { |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9401 | bool TryListConstructors = true; |
| 9402 | |
| 9403 | // Try list constructors unless the list is empty and the class has one or |
| 9404 | // more default constructors, in which case those constructors win. |
| 9405 | if (!ListInit->getNumInits()) { |
| 9406 | for (NamedDecl *D : Guides) { |
| 9407 | auto *FD = dyn_cast<FunctionDecl>(D->getUnderlyingDecl()); |
| 9408 | if (FD && FD->getMinRequiredArguments() == 0) { |
| 9409 | TryListConstructors = false; |
| 9410 | break; |
| 9411 | } |
| 9412 | } |
Richard Smith | 1363e8f | 2017-09-07 07:22:36 +0000 | [diff] [blame] | 9413 | } else if (ListInit->getNumInits() == 1) { |
| 9414 | // C++ [over.match.class.deduct]: |
| 9415 | // As an exception, the first phase in [over.match.list] (considering |
| 9416 | // initializer-list constructors) is omitted if the initializer list |
| 9417 | // consists of a single expression of type cv U, where U is a |
| 9418 | // specialization of C or a class derived from a specialization of C. |
| 9419 | Expr *E = ListInit->getInit(0); |
| 9420 | auto *RD = E->getType()->getAsCXXRecordDecl(); |
| 9421 | if (!isa<InitListExpr>(E) && RD && |
Erik Pilkington | dd0b344 | 2018-07-26 23:40:42 +0000 | [diff] [blame] | 9422 | isCompleteType(Kind.getLocation(), E->getType()) && |
Richard Smith | 1363e8f | 2017-09-07 07:22:36 +0000 | [diff] [blame] | 9423 | isOrIsDerivedFromSpecializationOf(RD, Template)) |
| 9424 | TryListConstructors = false; |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9425 | } |
| 9426 | |
| 9427 | if (TryListConstructors) |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9428 | Result = tryToResolveOverload(/*OnlyListConstructor*/true); |
| 9429 | // Then unwrap the initializer list and try again considering all |
| 9430 | // constructors. |
| 9431 | Inits = MultiExprArg(ListInit->getInits(), ListInit->getNumInits()); |
| 9432 | } |
| 9433 | |
| 9434 | // If list-initialization fails, or if we're doing any other kind of |
| 9435 | // initialization, we (eventually) consider constructors. |
| 9436 | if (Result == OR_No_Viable_Function) |
| 9437 | Result = tryToResolveOverload(/*OnlyListConstructor*/false); |
| 9438 | |
| 9439 | switch (Result) { |
| 9440 | case OR_Ambiguous: |
| 9441 | Diag(Kind.getLocation(), diag::err_deduced_class_template_ctor_ambiguous) |
| 9442 | << TemplateName; |
| 9443 | // FIXME: For list-initialization candidates, it'd usually be better to |
| 9444 | // list why they were not viable when given the initializer list itself as |
| 9445 | // an argument. |
| 9446 | Candidates.NoteCandidates(*this, OCD_ViableCandidates, Inits); |
| 9447 | return QualType(); |
| 9448 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9449 | case OR_No_Viable_Function: { |
| 9450 | CXXRecordDecl *Primary = |
| 9451 | cast<ClassTemplateDecl>(Template)->getTemplatedDecl(); |
| 9452 | bool Complete = |
| 9453 | isCompleteType(Kind.getLocation(), Context.getTypeDeclType(Primary)); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9454 | Diag(Kind.getLocation(), |
| 9455 | Complete ? diag::err_deduced_class_template_ctor_no_viable |
| 9456 | : diag::err_deduced_class_template_incomplete) |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9457 | << TemplateName << !Guides.empty(); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9458 | Candidates.NoteCandidates(*this, OCD_AllCandidates, Inits); |
| 9459 | return QualType(); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9460 | } |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9461 | |
| 9462 | case OR_Deleted: { |
| 9463 | Diag(Kind.getLocation(), diag::err_deduced_class_template_deleted) |
| 9464 | << TemplateName; |
| 9465 | NoteDeletedFunction(Best->Function); |
| 9466 | return QualType(); |
| 9467 | } |
| 9468 | |
| 9469 | case OR_Success: |
| 9470 | // C++ [over.match.list]p1: |
| 9471 | // In copy-list-initialization, if an explicit constructor is chosen, the |
| 9472 | // initialization is ill-formed. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9473 | if (Kind.isCopyInit() && ListInit && |
| 9474 | cast<CXXDeductionGuideDecl>(Best->Function)->isExplicit()) { |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9475 | bool IsDeductionGuide = !Best->Function->isImplicit(); |
| 9476 | Diag(Kind.getLocation(), diag::err_deduced_class_template_explicit) |
| 9477 | << TemplateName << IsDeductionGuide; |
| 9478 | Diag(Best->Function->getLocation(), |
| 9479 | diag::note_explicit_ctor_deduction_guide_here) |
| 9480 | << IsDeductionGuide; |
| 9481 | return QualType(); |
| 9482 | } |
| 9483 | |
| 9484 | // Make sure we didn't select an unusable deduction guide, and mark it |
| 9485 | // as referenced. |
| 9486 | DiagnoseUseOfDecl(Best->Function, Kind.getLocation()); |
| 9487 | MarkFunctionReferenced(Kind.getLocation(), Best->Function); |
| 9488 | break; |
| 9489 | } |
| 9490 | |
| 9491 | // C++ [dcl.type.class.deduct]p1: |
| 9492 | // The placeholder is replaced by the return type of the function selected |
| 9493 | // by overload resolution for class template deduction. |
Richard Smith | 8eeb16f | 2018-09-10 20:31:03 +0000 | [diff] [blame] | 9494 | QualType DeducedType = |
| 9495 | SubstAutoType(TSInfo->getType(), Best->Function->getReturnType()); |
| 9496 | Diag(TSInfo->getTypeLoc().getBeginLoc(), |
| 9497 | diag::warn_cxx14_compat_class_template_argument_deduction) |
| 9498 | << TSInfo->getTypeLoc().getSourceRange() << 1 << DeducedType; |
Eric Fiselier | 73b51ae | 2019-01-17 21:44:24 +0000 | [diff] [blame] | 9499 | |
| 9500 | // Warn if CTAD was used on a type that does not have any user-defined |
| 9501 | // deduction guides. |
| 9502 | if (!HasAnyDeductionGuide) { |
| 9503 | Diag(TSInfo->getTypeLoc().getBeginLoc(), |
| 9504 | diag::warn_ctad_maybe_unsupported) |
| 9505 | << TemplateName; |
| 9506 | Diag(Template->getLocation(), diag::note_suppress_ctad_maybe_unsupported); |
| 9507 | } |
| 9508 | |
Richard Smith | 8eeb16f | 2018-09-10 20:31:03 +0000 | [diff] [blame] | 9509 | return DeducedType; |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9510 | } |