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. |
| 2211 | class FieldInitializerValidatorCCC : public CorrectionCandidateCallback { |
| 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 | |
| 2221 | private: |
| 2222 | RecordDecl *Record; |
| 2223 | }; |
| 2224 | |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 2225 | } // end anonymous namespace |
Kaelyn Uhrain | b02c5e9 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 2226 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2227 | /// Check the well-formedness of a C99 designated initializer. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2228 | /// |
| 2229 | /// Determines whether the designated initializer @p DIE, which |
| 2230 | /// resides at the given @p Index within the initializer list @p |
| 2231 | /// IList, is well-formed for a current object of type @p DeclType |
| 2232 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2233 | /// within the current subobject is returned in either |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2234 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2235 | /// |
| 2236 | /// @param IList The initializer list in which this designated |
| 2237 | /// initializer occurs. |
| 2238 | /// |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 2239 | /// @param DIE The designated initializer expression. |
| 2240 | /// |
| 2241 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2242 | /// |
Dmitri Gribenko | adba9be | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 2243 | /// @param CurrentObjectType The type of the "current object" (C99 6.7.8p17), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2244 | /// into which the designation in @p DIE should refer. |
| 2245 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2246 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 2247 | /// a field, this will be set to the field declaration corresponding |
| 2248 | /// to the field named by the designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2249 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2250 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 2251 | /// DIE is an array designator or GNU array-range designator, this |
| 2252 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2253 | /// |
| 2254 | /// @param Index Index into @p IList where the designated initializer |
| 2255 | /// @p DIE occurs. |
| 2256 | /// |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2257 | /// @param StructuredList The initializer list expression that |
| 2258 | /// describes all of the subobject initializers in the order they'll |
| 2259 | /// actually be initialized. |
| 2260 | /// |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2261 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2262 | bool |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2263 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2264 | InitListExpr *IList, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2265 | DesignatedInitExpr *DIE, |
| 2266 | unsigned DesigIdx, |
| 2267 | QualType &CurrentObjectType, |
| 2268 | RecordDecl::field_iterator *NextField, |
| 2269 | llvm::APSInt *NextElementIndex, |
| 2270 | unsigned &Index, |
| 2271 | InitListExpr *StructuredList, |
| 2272 | unsigned &StructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2273 | bool FinishSubobjectInit, |
| 2274 | bool TopLevelObject) { |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 2275 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2276 | // Check the actual initialization for the designated object type. |
| 2277 | bool prevHadError = hadError; |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 2278 | |
| 2279 | // Temporarily remove the designator expression from the |
| 2280 | // initializer list that the child calls see, so that we don't try |
| 2281 | // to re-process the designator. |
| 2282 | unsigned OldIndex = Index; |
| 2283 | IList->setInit(OldIndex, DIE->getInit()); |
| 2284 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2285 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2286 | StructuredList, StructuredIndex); |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 2287 | |
| 2288 | // Restore the designated initializer expression in the syntactic |
| 2289 | // form of the initializer list. |
| 2290 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 2291 | DIE->setInit(IList->getInit(OldIndex)); |
| 2292 | IList->setInit(OldIndex, DIE); |
| 2293 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2294 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2295 | } |
| 2296 | |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 2297 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2298 | bool IsFirstDesignator = (DesigIdx == 0); |
| 2299 | if (!VerifyOnly) { |
| 2300 | assert((IsFirstDesignator || StructuredList) && |
| 2301 | "Need a non-designated initializer list to start from"); |
| 2302 | |
| 2303 | // Determine the structural initializer list that corresponds to the |
| 2304 | // current subobject. |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2305 | if (IsFirstDesignator) |
| 2306 | StructuredList = SyntacticToSemantic.lookup(IList); |
| 2307 | else { |
| 2308 | Expr *ExistingInit = StructuredIndex < StructuredList->getNumInits() ? |
| 2309 | StructuredList->getInit(StructuredIndex) : nullptr; |
| 2310 | if (!ExistingInit && StructuredList->hasArrayFiller()) |
| 2311 | ExistingInit = StructuredList->getArrayFiller(); |
| 2312 | |
| 2313 | if (!ExistingInit) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2314 | StructuredList = getStructuredSubobjectInit( |
| 2315 | IList, Index, CurrentObjectType, StructuredList, StructuredIndex, |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2316 | SourceRange(D->getBeginLoc(), DIE->getEndLoc())); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2317 | else if (InitListExpr *Result = dyn_cast<InitListExpr>(ExistingInit)) |
| 2318 | StructuredList = Result; |
| 2319 | else { |
| 2320 | if (DesignatedInitUpdateExpr *E = |
| 2321 | dyn_cast<DesignatedInitUpdateExpr>(ExistingInit)) |
| 2322 | StructuredList = E->getUpdater(); |
| 2323 | else { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2324 | DesignatedInitUpdateExpr *DIUE = new (SemaRef.Context) |
| 2325 | DesignatedInitUpdateExpr(SemaRef.Context, D->getBeginLoc(), |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2326 | ExistingInit, DIE->getEndLoc()); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2327 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, DIUE); |
| 2328 | StructuredList = DIUE->getUpdater(); |
| 2329 | } |
| 2330 | |
| 2331 | // We need to check on source range validity because the previous |
| 2332 | // initializer does not have to be an explicit initializer. e.g., |
| 2333 | // |
| 2334 | // struct P { int a, b; }; |
| 2335 | // struct PP { struct P p } l = { { .a = 2 }, .p.b = 3 }; |
| 2336 | // |
| 2337 | // There is an overwrite taking place because the first braced initializer |
| 2338 | // list "{ .a = 2 }" already provides value for .p.b (which is zero). |
| 2339 | if (ExistingInit->getSourceRange().isValid()) { |
| 2340 | // We are creating an initializer list that initializes the |
| 2341 | // subobjects of the current object, but there was already an |
| 2342 | // initialization that completely initialized the current |
| 2343 | // subobject, e.g., by a compound literal: |
| 2344 | // |
| 2345 | // struct X { int a, b; }; |
| 2346 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
| 2347 | // |
| 2348 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 2349 | // designated initializer re-initializes the whole |
| 2350 | // subobject [0], overwriting previous initializers. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2351 | SemaRef.Diag(D->getBeginLoc(), |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2352 | diag::warn_subobject_initializer_overrides) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2353 | << SourceRange(D->getBeginLoc(), DIE->getEndLoc()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2354 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2355 | SemaRef.Diag(ExistingInit->getBeginLoc(), |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2356 | diag::note_previous_initializer) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2357 | << /*FIXME:has side effects=*/0 << ExistingInit->getSourceRange(); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2358 | } |
| 2359 | } |
| 2360 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2361 | assert(StructuredList && "Expected a structured initializer list"); |
| 2362 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2363 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2364 | if (D->isFieldDesignator()) { |
| 2365 | // C99 6.7.8p7: |
| 2366 | // |
| 2367 | // If a designator has the form |
| 2368 | // |
| 2369 | // . identifier |
| 2370 | // |
| 2371 | // then the current object (defined below) shall have |
| 2372 | // structure or union type and the identifier shall be the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2373 | // name of a member of that type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2374 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2375 | if (!RT) { |
| 2376 | SourceLocation Loc = D->getDotLoc(); |
| 2377 | if (Loc.isInvalid()) |
| 2378 | Loc = D->getFieldLoc(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2379 | if (!VerifyOnly) |
| 2380 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2381 | << SemaRef.getLangOpts().CPlusPlus << CurrentObjectType; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2382 | ++Index; |
| 2383 | return true; |
| 2384 | } |
| 2385 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2386 | FieldDecl *KnownField = D->getField(); |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2387 | if (!KnownField) { |
| 2388 | IdentifierInfo *FieldName = D->getFieldName(); |
| 2389 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
| 2390 | for (NamedDecl *ND : Lookup) { |
| 2391 | if (auto *FD = dyn_cast<FieldDecl>(ND)) { |
| 2392 | KnownField = FD; |
| 2393 | break; |
| 2394 | } |
| 2395 | if (auto *IFD = dyn_cast<IndirectFieldDecl>(ND)) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2396 | // In verify mode, don't modify the original. |
| 2397 | if (VerifyOnly) |
| 2398 | DIE = CloneDesignatedInitExpr(SemaRef, DIE); |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2399 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IFD); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2400 | D = DIE->getDesignator(DesigIdx); |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2401 | KnownField = cast<FieldDecl>(*IFD->chain_begin()); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 2402 | break; |
| 2403 | } |
| 2404 | } |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2405 | if (!KnownField) { |
| 2406 | if (VerifyOnly) { |
| 2407 | ++Index; |
| 2408 | return true; // No typo correction when just trying this out. |
| 2409 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2410 | |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2411 | // Name lookup found something, but it wasn't a field. |
| 2412 | if (!Lookup.empty()) { |
| 2413 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
| 2414 | << FieldName; |
| 2415 | SemaRef.Diag(Lookup.front()->getLocation(), |
| 2416 | diag::note_field_designator_found); |
| 2417 | ++Index; |
| 2418 | return true; |
| 2419 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2420 | |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2421 | // Name lookup didn't find anything. |
| 2422 | // Determine whether this was a typo for another field name. |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 2423 | if (TypoCorrection Corrected = SemaRef.CorrectTypo( |
| 2424 | DeclarationNameInfo(FieldName, D->getFieldLoc()), |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2425 | Sema::LookupMemberName, /*Scope=*/nullptr, /*SS=*/nullptr, |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 2426 | llvm::make_unique<FieldInitializerValidatorCCC>(RT->getDecl()), |
| 2427 | Sema::CTK_ErrorRecovery, RT->getDecl())) { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 2428 | SemaRef.diagnoseTypo( |
| 2429 | Corrected, |
| 2430 | SemaRef.PDiag(diag::err_field_designator_unknown_suggest) |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2431 | << FieldName << CurrentObjectType); |
| 2432 | KnownField = Corrected.getCorrectionDeclAs<FieldDecl>(); |
Benjamin Kramer | afe718f | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 2433 | hadError = true; |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 2434 | } else { |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2435 | // Typo correction didn't find anything. |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 2436 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 2437 | << FieldName << CurrentObjectType; |
| 2438 | ++Index; |
| 2439 | return true; |
| 2440 | } |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 2441 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2442 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2443 | |
David Majnemer | 58e4ea9 | 2014-08-23 01:48:50 +0000 | [diff] [blame] | 2444 | unsigned FieldIndex = 0; |
Akira Hatanaka | 8eccb9b | 2017-01-17 19:35:54 +0000 | [diff] [blame] | 2445 | |
| 2446 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) |
| 2447 | FieldIndex = CXXRD->getNumBases(); |
| 2448 | |
David Majnemer | 58e4ea9 | 2014-08-23 01:48:50 +0000 | [diff] [blame] | 2449 | for (auto *FI : RT->getDecl()->fields()) { |
| 2450 | if (FI->isUnnamedBitfield()) |
| 2451 | continue; |
Richard Smith | fe1bc70 | 2016-04-08 19:57:40 +0000 | [diff] [blame] | 2452 | if (declaresSameEntity(KnownField, FI)) { |
| 2453 | KnownField = FI; |
David Majnemer | 58e4ea9 | 2014-08-23 01:48:50 +0000 | [diff] [blame] | 2454 | break; |
Richard Smith | fe1bc70 | 2016-04-08 19:57:40 +0000 | [diff] [blame] | 2455 | } |
David Majnemer | 58e4ea9 | 2014-08-23 01:48:50 +0000 | [diff] [blame] | 2456 | ++FieldIndex; |
| 2457 | } |
| 2458 | |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 2459 | RecordDecl::field_iterator Field = |
| 2460 | RecordDecl::field_iterator(DeclContext::decl_iterator(KnownField)); |
| 2461 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2462 | // All of the fields of a union are located at the same place in |
| 2463 | // the initializer list. |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 2464 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2465 | FieldIndex = 0; |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2466 | if (!VerifyOnly) { |
| 2467 | FieldDecl *CurrentField = StructuredList->getInitializedFieldInUnion(); |
Richard Smith | fe1bc70 | 2016-04-08 19:57:40 +0000 | [diff] [blame] | 2468 | if (CurrentField && !declaresSameEntity(CurrentField, *Field)) { |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2469 | assert(StructuredList->getNumInits() == 1 |
| 2470 | && "A union should never have more than one initializer!"); |
| 2471 | |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2472 | Expr *ExistingInit = StructuredList->getInit(0); |
Vassil Vassilev | 1a1678e | 2017-04-14 08:48:08 +0000 | [diff] [blame] | 2473 | if (ExistingInit) { |
| 2474 | // We're about to throw away an initializer, emit warning. |
| 2475 | SemaRef.Diag(D->getFieldLoc(), |
| 2476 | diag::warn_initializer_overrides) |
| 2477 | << D->getSourceRange(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2478 | SemaRef.Diag(ExistingInit->getBeginLoc(), |
Vassil Vassilev | 1a1678e | 2017-04-14 08:48:08 +0000 | [diff] [blame] | 2479 | diag::note_previous_initializer) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2480 | << /*FIXME:has side effects=*/0 |
| 2481 | << ExistingInit->getSourceRange(); |
Vassil Vassilev | 1a1678e | 2017-04-14 08:48:08 +0000 | [diff] [blame] | 2482 | } |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2483 | |
| 2484 | // remove existing initializer |
| 2485 | StructuredList->resizeInits(SemaRef.Context, 0); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2486 | StructuredList->setInitializedFieldInUnion(nullptr); |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2487 | } |
| 2488 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2489 | StructuredList->setInitializedFieldInUnion(*Field); |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2490 | } |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 2491 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2492 | |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2493 | // Make sure we can use this declaration. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2494 | bool InvalidUse; |
| 2495 | if (VerifyOnly) |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 2496 | InvalidUse = !SemaRef.CanUseDecl(*Field, TreatUnavailableAsInvalid); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2497 | else |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2498 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, D->getFieldLoc()); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2499 | if (InvalidUse) { |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2500 | ++Index; |
| 2501 | return true; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2502 | } |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2503 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2504 | if (!VerifyOnly) { |
| 2505 | // Update the designator with the field declaration. |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2506 | D->setField(*Field); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2507 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2508 | // Make sure that our non-designated initializer list has space |
| 2509 | // for a subobject corresponding to this field. |
| 2510 | if (FieldIndex >= StructuredList->getNumInits()) |
| 2511 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
| 2512 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2513 | |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2514 | // This designator names a flexible array member. |
| 2515 | if (Field->getType()->isIncompleteArrayType()) { |
| 2516 | bool Invalid = false; |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 2517 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2518 | // We can't designate an object within the flexible array |
| 2519 | // member (because GCC doesn't allow it). |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2520 | if (!VerifyOnly) { |
| 2521 | DesignatedInitExpr::Designator *NextD |
| 2522 | = DIE->getDesignator(DesigIdx + 1); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2523 | SemaRef.Diag(NextD->getBeginLoc(), |
| 2524 | diag::err_designator_into_flexible_array_member) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2525 | << SourceRange(NextD->getBeginLoc(), DIE->getEndLoc()); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2526 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2527 | << *Field; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2528 | } |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2529 | Invalid = true; |
| 2530 | } |
| 2531 | |
Chris Lattner | 001b29c | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 2532 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 2533 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2534 | // The initializer is not an initializer list. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2535 | if (!VerifyOnly) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2536 | SemaRef.Diag(DIE->getInit()->getBeginLoc(), |
| 2537 | diag::err_flexible_array_init_needs_braces) |
| 2538 | << DIE->getInit()->getSourceRange(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2539 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2540 | << *Field; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2541 | } |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2542 | Invalid = true; |
| 2543 | } |
| 2544 | |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 2545 | // Check GNU flexible array initializer. |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2546 | if (!Invalid && CheckFlexibleArrayInit(Entity, DIE->getInit(), *Field, |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 2547 | TopLevelObject)) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2548 | Invalid = true; |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2549 | |
| 2550 | if (Invalid) { |
| 2551 | ++Index; |
| 2552 | return true; |
| 2553 | } |
| 2554 | |
| 2555 | // Initialize the array. |
| 2556 | bool prevHadError = hadError; |
| 2557 | unsigned newStructuredIndex = FieldIndex; |
| 2558 | unsigned OldIndex = Index; |
| 2559 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2560 | |
| 2561 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2562 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2563 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2564 | StructuredList, newStructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2565 | |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2566 | IList->setInit(OldIndex, DIE); |
| 2567 | if (hadError && !prevHadError) { |
| 2568 | ++Field; |
| 2569 | ++FieldIndex; |
| 2570 | if (NextField) |
| 2571 | *NextField = Field; |
| 2572 | StructuredIndex = FieldIndex; |
| 2573 | return true; |
| 2574 | } |
| 2575 | } else { |
| 2576 | // Recurse to check later designated subobjects. |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 2577 | QualType FieldType = Field->getType(); |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2578 | unsigned newStructuredIndex = FieldIndex; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2579 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2580 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2581 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2582 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2583 | FieldType, nullptr, nullptr, Index, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2584 | StructuredList, newStructuredIndex, |
Alexey Bataev | 86a489e | 2016-01-25 05:14:03 +0000 | [diff] [blame] | 2585 | FinishSubobjectInit, false)) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2586 | return true; |
| 2587 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2588 | |
| 2589 | // Find the position of the next field to be initialized in this |
| 2590 | // subobject. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2591 | ++Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2592 | ++FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2593 | |
| 2594 | // If this the first designator, our caller will continue checking |
| 2595 | // the rest of this struct/class/union subobject. |
| 2596 | if (IsFirstDesignator) { |
| 2597 | if (NextField) |
| 2598 | *NextField = Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2599 | StructuredIndex = FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2600 | return false; |
| 2601 | } |
| 2602 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2603 | if (!FinishSubobjectInit) |
| 2604 | return false; |
| 2605 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2606 | // We've already initialized something in the union; we're done. |
| 2607 | if (RT->getDecl()->isUnion()) |
| 2608 | return hadError; |
| 2609 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2610 | // Check the remaining fields within this class/struct/union subobject. |
| 2611 | bool prevHadError = hadError; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2612 | |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 2613 | auto NoBases = |
| 2614 | CXXRecordDecl::base_class_range(CXXRecordDecl::base_class_iterator(), |
| 2615 | CXXRecordDecl::base_class_iterator()); |
| 2616 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, NoBases, Field, |
| 2617 | false, Index, StructuredList, FieldIndex); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2618 | return hadError && !prevHadError; |
| 2619 | } |
| 2620 | |
| 2621 | // C99 6.7.8p6: |
| 2622 | // |
| 2623 | // If a designator has the form |
| 2624 | // |
| 2625 | // [ constant-expression ] |
| 2626 | // |
| 2627 | // then the current object (defined below) shall have array |
| 2628 | // type and the expression shall be an integer constant |
| 2629 | // expression. If the array is of unknown size, any |
| 2630 | // nonnegative value is valid. |
| 2631 | // |
| 2632 | // Additionally, cope with the GNU extension that permits |
| 2633 | // designators of the form |
| 2634 | // |
| 2635 | // [ constant-expression ... constant-expression ] |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 2636 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2637 | if (!AT) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2638 | if (!VerifyOnly) |
| 2639 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
| 2640 | << CurrentObjectType; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2641 | ++Index; |
| 2642 | return true; |
| 2643 | } |
| 2644 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2645 | Expr *IndexExpr = nullptr; |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2646 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 2647 | if (D->isArrayDesignator()) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2648 | IndexExpr = DIE->getArrayIndex(*D); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2649 | DesignatedStartIndex = IndexExpr->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2650 | DesignatedEndIndex = DesignatedStartIndex; |
| 2651 | } else { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2652 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2653 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2654 | DesignatedStartIndex = |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2655 | DIE->getArrayRangeStart(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2656 | DesignatedEndIndex = |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2657 | DIE->getArrayRangeEnd(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2658 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2659 | |
Chris Lattner | b0ed51d | 2011-02-19 22:28:58 +0000 | [diff] [blame] | 2660 | // Codegen can't handle evaluating array range designators that have side |
| 2661 | // effects, because we replicate the AST value for each initialized element. |
| 2662 | // As such, set the sawArrayRangeDesignator() bit if we initialize multiple |
| 2663 | // elements with something that has a side effect, so codegen can emit an |
| 2664 | // "error unsupported" error instead of miscompiling the app. |
| 2665 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2666 | DIE->getInit()->HasSideEffects(SemaRef.Context) && !VerifyOnly) |
Douglas Gregor | bf7207a | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 2667 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2668 | } |
| 2669 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2670 | if (isa<ConstantArrayType>(AT)) { |
| 2671 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2672 | DesignatedStartIndex |
| 2673 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2674 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2675 | DesignatedEndIndex |
| 2676 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2677 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 2678 | if (DesignatedEndIndex >= MaxElements) { |
Eli Friedman | ed0f916 | 2011-09-26 18:53:43 +0000 | [diff] [blame] | 2679 | if (!VerifyOnly) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2680 | SemaRef.Diag(IndexExpr->getBeginLoc(), |
| 2681 | diag::err_array_designator_too_large) |
| 2682 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
| 2683 | << IndexExpr->getSourceRange(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2684 | ++Index; |
| 2685 | return true; |
| 2686 | } |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2687 | } else { |
Argyrios Kyrtzidis | 4746c2f | 2015-07-27 23:16:53 +0000 | [diff] [blame] | 2688 | unsigned DesignatedIndexBitWidth = |
| 2689 | ConstantArrayType::getMaxSizeBits(SemaRef.Context); |
| 2690 | DesignatedStartIndex = |
| 2691 | DesignatedStartIndex.extOrTrunc(DesignatedIndexBitWidth); |
| 2692 | DesignatedEndIndex = |
| 2693 | DesignatedEndIndex.extOrTrunc(DesignatedIndexBitWidth); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2694 | DesignatedStartIndex.setIsUnsigned(true); |
| 2695 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2696 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2697 | |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2698 | if (!VerifyOnly && StructuredList->isStringLiteralInit()) { |
| 2699 | // We're modifying a string literal init; we have to decompose the string |
| 2700 | // so we can modify the individual characters. |
| 2701 | ASTContext &Context = SemaRef.Context; |
| 2702 | Expr *SubExpr = StructuredList->getInit(0)->IgnoreParens(); |
| 2703 | |
| 2704 | // Compute the character type |
| 2705 | QualType CharTy = AT->getElementType(); |
| 2706 | |
| 2707 | // Compute the type of the integer literals. |
| 2708 | QualType PromotedCharTy = CharTy; |
| 2709 | if (CharTy->isPromotableIntegerType()) |
| 2710 | PromotedCharTy = Context.getPromotedIntegerType(CharTy); |
| 2711 | unsigned PromotedCharTyWidth = Context.getTypeSize(PromotedCharTy); |
| 2712 | |
| 2713 | if (StringLiteral *SL = dyn_cast<StringLiteral>(SubExpr)) { |
| 2714 | // Get the length of the string. |
| 2715 | uint64_t StrLen = SL->getLength(); |
| 2716 | if (cast<ConstantArrayType>(AT)->getSize().ult(StrLen)) |
| 2717 | StrLen = cast<ConstantArrayType>(AT)->getSize().getZExtValue(); |
| 2718 | StructuredList->resizeInits(Context, StrLen); |
| 2719 | |
| 2720 | // Build a literal for each character in the string, and put them into |
| 2721 | // the init list. |
| 2722 | for (unsigned i = 0, e = StrLen; i != e; ++i) { |
| 2723 | llvm::APInt CodeUnit(PromotedCharTyWidth, SL->getCodeUnit(i)); |
| 2724 | Expr *Init = new (Context) IntegerLiteral( |
Eli Friedman | 6cc05f7 | 2013-06-11 22:26:34 +0000 | [diff] [blame] | 2725 | Context, CodeUnit, PromotedCharTy, SubExpr->getExprLoc()); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2726 | if (CharTy != PromotedCharTy) |
| 2727 | Init = ImplicitCastExpr::Create(Context, CharTy, CK_IntegralCast, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2728 | Init, nullptr, VK_RValue); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2729 | StructuredList->updateInit(Context, i, Init); |
| 2730 | } |
| 2731 | } else { |
| 2732 | ObjCEncodeExpr *E = cast<ObjCEncodeExpr>(SubExpr); |
| 2733 | std::string Str; |
| 2734 | Context.getObjCEncodingForType(E->getEncodedType(), Str); |
| 2735 | |
| 2736 | // Get the length of the string. |
| 2737 | uint64_t StrLen = Str.size(); |
| 2738 | if (cast<ConstantArrayType>(AT)->getSize().ult(StrLen)) |
| 2739 | StrLen = cast<ConstantArrayType>(AT)->getSize().getZExtValue(); |
| 2740 | StructuredList->resizeInits(Context, StrLen); |
| 2741 | |
| 2742 | // Build a literal for each character in the string, and put them into |
| 2743 | // the init list. |
| 2744 | for (unsigned i = 0, e = StrLen; i != e; ++i) { |
| 2745 | llvm::APInt CodeUnit(PromotedCharTyWidth, Str[i]); |
| 2746 | Expr *Init = new (Context) IntegerLiteral( |
Eli Friedman | 6cc05f7 | 2013-06-11 22:26:34 +0000 | [diff] [blame] | 2747 | Context, CodeUnit, PromotedCharTy, SubExpr->getExprLoc()); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2748 | if (CharTy != PromotedCharTy) |
| 2749 | Init = ImplicitCastExpr::Create(Context, CharTy, CK_IntegralCast, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2750 | Init, nullptr, VK_RValue); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2751 | StructuredList->updateInit(Context, i, Init); |
| 2752 | } |
| 2753 | } |
| 2754 | } |
| 2755 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2756 | // Make sure that our non-designated initializer list has space |
| 2757 | // for a subobject corresponding to this array element. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2758 | if (!VerifyOnly && |
| 2759 | DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2760 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2761 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2762 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2763 | // Repeatedly perform subobject initializations in the range |
| 2764 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2765 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2766 | // Move to the next designator |
| 2767 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2768 | unsigned OldIndex = Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2769 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2770 | InitializedEntity ElementEntity = |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2771 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2772 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2773 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 2774 | // Recurse to check later designated subobjects. |
| 2775 | QualType ElementType = AT->getElementType(); |
| 2776 | Index = OldIndex; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2777 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2778 | ElementEntity.setElementIndex(ElementIndex); |
Alexey Bataev | 86a489e | 2016-01-25 05:14:03 +0000 | [diff] [blame] | 2779 | if (CheckDesignatedInitializer( |
| 2780 | ElementEntity, IList, DIE, DesigIdx + 1, ElementType, nullptr, |
| 2781 | nullptr, Index, StructuredList, ElementIndex, |
| 2782 | FinishSubobjectInit && (DesignatedStartIndex == DesignatedEndIndex), |
| 2783 | false)) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2784 | return true; |
| 2785 | |
| 2786 | // Move to the next index in the array that we'll be initializing. |
| 2787 | ++DesignatedStartIndex; |
| 2788 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2789 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2790 | |
| 2791 | // If this the first designator, our caller will continue checking |
| 2792 | // the rest of this array subobject. |
| 2793 | if (IsFirstDesignator) { |
| 2794 | if (NextElementIndex) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2795 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2796 | StructuredIndex = ElementIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2797 | return false; |
| 2798 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2799 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2800 | if (!FinishSubobjectInit) |
| 2801 | return false; |
| 2802 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2803 | // Check the remaining elements within this array subobject. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2804 | bool prevHadError = hadError; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2805 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 2806 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2807 | StructuredList, ElementIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2808 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2809 | } |
| 2810 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2811 | // Get the structured initializer list for a subobject of type |
| 2812 | // @p CurrentObjectType. |
| 2813 | InitListExpr * |
| 2814 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 2815 | QualType CurrentObjectType, |
| 2816 | InitListExpr *StructuredList, |
| 2817 | unsigned StructuredIndex, |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2818 | SourceRange InitRange, |
| 2819 | bool IsFullyOverwritten) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2820 | if (VerifyOnly) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2821 | return nullptr; // No structured list in verification-only mode. |
| 2822 | Expr *ExistingInit = nullptr; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2823 | if (!StructuredList) |
Benjamin Kramer | 6b441d6 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 2824 | ExistingInit = SyntacticToSemantic.lookup(IList); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2825 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 2826 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2827 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2828 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2829 | // There might have already been initializers for subobjects of the current |
| 2830 | // object, but a subsequent initializer list will overwrite the entirety |
| 2831 | // of the current object. (See DR 253 and C99 6.7.8p21). e.g., |
| 2832 | // |
| 2833 | // struct P { char x[6]; }; |
| 2834 | // struct P l = { .x[2] = 'x', .x = { [0] = 'f' } }; |
| 2835 | // |
| 2836 | // The first designated initializer is ignored, and l.x is just "f". |
| 2837 | if (!IsFullyOverwritten) |
| 2838 | return Result; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2839 | |
| 2840 | if (ExistingInit) { |
| 2841 | // We are creating an initializer list that initializes the |
| 2842 | // subobjects of the current object, but there was already an |
| 2843 | // initialization that completely initialized the current |
| 2844 | // subobject, e.g., by a compound literal: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2845 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2846 | // struct X { int a, b; }; |
| 2847 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2848 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2849 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 2850 | // designated initializer re-initializes the whole |
| 2851 | // subobject [0], overwriting previous initializers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2852 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2853 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2854 | << InitRange; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2855 | SemaRef.Diag(ExistingInit->getBeginLoc(), diag::note_previous_initializer) |
| 2856 | << /*FIXME:has side effects=*/0 << ExistingInit->getSourceRange(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2857 | } |
| 2858 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2859 | InitListExpr *Result |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2860 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
Dmitri Gribenko | 78852e9 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 2861 | InitRange.getBegin(), None, |
Ted Kremenek | 013041e | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 2862 | InitRange.getEnd()); |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2863 | |
Eli Friedman | 91f5ae5 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 2864 | QualType ResultType = CurrentObjectType; |
| 2865 | if (!ResultType->isArrayType()) |
| 2866 | ResultType = ResultType.getNonLValueExprType(SemaRef.Context); |
| 2867 | Result->setType(ResultType); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2868 | |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2869 | // Pre-allocate storage for the structured initializer list. |
| 2870 | unsigned NumElements = 0; |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2871 | unsigned NumInits = 0; |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2872 | bool GotNumInits = false; |
| 2873 | if (!StructuredList) { |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2874 | NumInits = IList->getNumInits(); |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2875 | GotNumInits = true; |
| 2876 | } else if (Index < IList->getNumInits()) { |
| 2877 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) { |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2878 | NumInits = SubList->getNumInits(); |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2879 | GotNumInits = true; |
| 2880 | } |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2881 | } |
| 2882 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2883 | if (const ArrayType *AType |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2884 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 2885 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 2886 | NumElements = CAType->getSize().getZExtValue(); |
| 2887 | // Simple heuristic so that we don't allocate a very large |
| 2888 | // initializer with many empty entries at the end. |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2889 | if (GotNumInits && NumElements > NumInits) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2890 | NumElements = 0; |
| 2891 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2892 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2893 | NumElements = VType->getNumElements(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2894 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2895 | RecordDecl *RDecl = RType->getDecl(); |
| 2896 | if (RDecl->isUnion()) |
| 2897 | NumElements = 1; |
| 2898 | else |
Aaron Ballman | 62e47c4 | 2014-03-10 13:43:55 +0000 | [diff] [blame] | 2899 | NumElements = std::distance(RDecl->field_begin(), RDecl->field_end()); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2900 | } |
| 2901 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2902 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2903 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2904 | // Link this new initializer list into the structured initializer |
| 2905 | // lists. |
| 2906 | if (StructuredList) |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2907 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2908 | else { |
| 2909 | Result->setSyntacticForm(IList); |
| 2910 | SyntacticToSemantic[IList] = Result; |
| 2911 | } |
| 2912 | |
| 2913 | return Result; |
| 2914 | } |
| 2915 | |
| 2916 | /// Update the initializer at index @p StructuredIndex within the |
| 2917 | /// structured initializer list to the value @p expr. |
| 2918 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 2919 | unsigned &StructuredIndex, |
| 2920 | Expr *expr) { |
| 2921 | // No structured initializer list to update |
| 2922 | if (!StructuredList) |
| 2923 | return; |
| 2924 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2925 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 2926 | StructuredIndex, expr)) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2927 | // This initializer overwrites a previous initializer. Warn. |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2928 | // We need to check on source range validity because the previous |
| 2929 | // initializer does not have to be an explicit initializer. |
| 2930 | // struct P { int a, b; }; |
| 2931 | // struct PP { struct P p } l = { { .a = 2 }, .p.b = 3 }; |
| 2932 | // There is an overwrite taking place because the first braced initializer |
| 2933 | // list "{ .a = 2 }' already provides value for .p.b (which is zero). |
| 2934 | if (PrevInit->getSourceRange().isValid()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2935 | SemaRef.Diag(expr->getBeginLoc(), diag::warn_initializer_overrides) |
| 2936 | << expr->getSourceRange(); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2937 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2938 | SemaRef.Diag(PrevInit->getBeginLoc(), diag::note_previous_initializer) |
| 2939 | << /*FIXME:has side effects=*/0 << PrevInit->getSourceRange(); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2940 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2941 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2942 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2943 | ++StructuredIndex; |
| 2944 | } |
| 2945 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2946 | /// Check that the given Index expression is a valid array designator |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2947 | /// value. This is essentially just a wrapper around |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2948 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2949 | /// and produces a reasonable diagnostic if there is a |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2950 | /// failure. Returns the index expression, possibly with an implicit cast |
| 2951 | /// added, on success. If everything went okay, Value will receive the |
| 2952 | /// value of the constant expression. |
| 2953 | static ExprResult |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2954 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2955 | SourceLocation Loc = Index->getBeginLoc(); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2956 | |
| 2957 | // Make sure this is an integer constant expression. |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2958 | ExprResult Result = S.VerifyIntegerConstantExpression(Index, &Value); |
| 2959 | if (Result.isInvalid()) |
| 2960 | return Result; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2961 | |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2962 | if (Value.isSigned() && Value.isNegative()) |
| 2963 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2964 | << Value.toString(10) << Index->getSourceRange(); |
| 2965 | |
Douglas Gregor | 51650d3 | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 2966 | Value.setIsUnsigned(true); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2967 | return Result; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2968 | } |
| 2969 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2970 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 2971 | SourceLocation Loc, |
| 2972 | bool GNUSyntax, |
| 2973 | ExprResult Init) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2974 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 2975 | |
| 2976 | bool Invalid = false; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2977 | SmallVector<ASTDesignator, 32> Designators; |
| 2978 | SmallVector<Expr *, 32> InitExpressions; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2979 | |
| 2980 | // Build designators and check array designator expressions. |
| 2981 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 2982 | const Designator &D = Desig.getDesignator(Idx); |
| 2983 | switch (D.getKind()) { |
| 2984 | case Designator::FieldDesignator: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2985 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2986 | D.getFieldLoc())); |
| 2987 | break; |
| 2988 | |
| 2989 | case Designator::ArrayDesignator: { |
| 2990 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 2991 | llvm::APSInt IndexValue; |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2992 | if (!Index->isTypeDependent() && !Index->isValueDependent()) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2993 | Index = CheckArrayDesignatorExpr(*this, Index, IndexValue).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2994 | if (!Index) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2995 | Invalid = true; |
| 2996 | else { |
| 2997 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2998 | D.getLBracketLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2999 | D.getRBracketLoc())); |
| 3000 | InitExpressions.push_back(Index); |
| 3001 | } |
| 3002 | break; |
| 3003 | } |
| 3004 | |
| 3005 | case Designator::ArrayRangeDesignator: { |
| 3006 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 3007 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 3008 | llvm::APSInt StartValue; |
| 3009 | llvm::APSInt EndValue; |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 3010 | bool StartDependent = StartIndex->isTypeDependent() || |
| 3011 | StartIndex->isValueDependent(); |
| 3012 | bool EndDependent = EndIndex->isTypeDependent() || |
| 3013 | EndIndex->isValueDependent(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3014 | if (!StartDependent) |
| 3015 | StartIndex = |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3016 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3017 | if (!EndDependent) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3018 | EndIndex = CheckArrayDesignatorExpr(*this, EndIndex, EndValue).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3019 | |
| 3020 | if (!StartIndex || !EndIndex) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3021 | Invalid = true; |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 3022 | else { |
| 3023 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 3024 | if (StartDependent || EndDependent) { |
| 3025 | // Nothing to compute. |
| 3026 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3027 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 3028 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3029 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 3030 | |
Douglas Gregor | 0f9d400 | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 3031 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 3032 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3033 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 3034 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 3035 | Invalid = true; |
| 3036 | } else { |
| 3037 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3038 | D.getLBracketLoc(), |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 3039 | D.getEllipsisLoc(), |
| 3040 | D.getRBracketLoc())); |
| 3041 | InitExpressions.push_back(StartIndex); |
| 3042 | InitExpressions.push_back(EndIndex); |
| 3043 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3044 | } |
| 3045 | break; |
| 3046 | } |
| 3047 | } |
| 3048 | } |
| 3049 | |
| 3050 | if (Invalid || Init.isInvalid()) |
| 3051 | return ExprError(); |
| 3052 | |
| 3053 | // Clear out the expressions within the designation. |
| 3054 | Desig.ClearExprs(*this); |
| 3055 | |
| 3056 | DesignatedInitExpr *DIE |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 3057 | = DesignatedInitExpr::Create(Context, |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 3058 | Designators, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3059 | InitExpressions, Loc, GNUSyntax, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3060 | Init.getAs<Expr>()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3061 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3062 | if (!getLangOpts().C99) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3063 | Diag(DIE->getBeginLoc(), diag::ext_designated_init) |
| 3064 | << DIE->getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3065 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3066 | return DIE; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3067 | } |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 3068 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3069 | //===----------------------------------------------------------------------===// |
| 3070 | // Initialization entity |
| 3071 | //===----------------------------------------------------------------------===// |
| 3072 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3073 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 3074 | const InitializedEntity &Parent) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3075 | : Parent(&Parent), Index(Index) |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 3076 | { |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3077 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 3078 | Kind = EK_ArrayElement; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3079 | Type = AT->getElementType(); |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3080 | } else if (const VectorType *VT = Parent.getType()->getAs<VectorType>()) { |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3081 | Kind = EK_VectorElement; |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3082 | Type = VT->getElementType(); |
| 3083 | } else { |
| 3084 | const ComplexType *CT = Parent.getType()->getAs<ComplexType>(); |
| 3085 | assert(CT && "Unexpected type"); |
| 3086 | Kind = EK_ComplexElement; |
| 3087 | Type = CT->getElementType(); |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3088 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3089 | } |
| 3090 | |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 3091 | InitializedEntity |
| 3092 | InitializedEntity::InitializeBase(ASTContext &Context, |
| 3093 | const CXXBaseSpecifier *Base, |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 3094 | bool IsInheritedVirtualBase, |
| 3095 | const InitializedEntity *Parent) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3096 | InitializedEntity Result; |
| 3097 | Result.Kind = EK_Base; |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 3098 | Result.Parent = Parent; |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 3099 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 3100 | if (IsInheritedVirtualBase) |
| 3101 | Result.Base |= 0x01; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3102 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3103 | Result.Type = Base->getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3104 | return Result; |
| 3105 | } |
| 3106 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3107 | DeclarationName InitializedEntity::getName() const { |
| 3108 | switch (getKind()) { |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3109 | case EK_Parameter: |
| 3110 | case EK_Parameter_CF_Audited: { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3111 | ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 3112 | return (D ? D->getDeclName() : DeclarationName()); |
| 3113 | } |
Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3114 | |
| 3115 | case EK_Variable: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3116 | case EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3117 | case EK_Binding: |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3118 | return Variable.VariableOrMember->getDeclName(); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3119 | |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 3120 | case EK_LambdaCapture: |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 3121 | return DeclarationName(Capture.VarID); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3122 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3123 | case EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 3124 | case EK_StmtExprResult: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3125 | case EK_Exception: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3126 | case EK_New: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3127 | case EK_Temporary: |
| 3128 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3129 | case EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3130 | case EK_ArrayElement: |
| 3131 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3132 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3133 | case EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3134 | case EK_LambdaToBlockConversionBlockElement: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 3135 | case EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 3136 | case EK_RelatedResult: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3137 | return DeclarationName(); |
| 3138 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3139 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3140 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3141 | } |
| 3142 | |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3143 | ValueDecl *InitializedEntity::getDecl() const { |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3144 | switch (getKind()) { |
| 3145 | case EK_Variable: |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3146 | case EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3147 | case EK_Binding: |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3148 | return Variable.VariableOrMember; |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3149 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3150 | case EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3151 | case EK_Parameter_CF_Audited: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3152 | return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 3153 | |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3154 | case EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 3155 | case EK_StmtExprResult: |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3156 | case EK_Exception: |
| 3157 | case EK_New: |
| 3158 | case EK_Temporary: |
| 3159 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3160 | case EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3161 | case EK_ArrayElement: |
| 3162 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3163 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3164 | case EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3165 | case EK_LambdaToBlockConversionBlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 3166 | case EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 3167 | case EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 3168 | case EK_RelatedResult: |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3169 | return nullptr; |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3170 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3171 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3172 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3173 | } |
| 3174 | |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3175 | bool InitializedEntity::allowsNRVO() const { |
| 3176 | switch (getKind()) { |
| 3177 | case EK_Result: |
| 3178 | case EK_Exception: |
| 3179 | return LocAndNRVO.NRVO; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3180 | |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 3181 | case EK_StmtExprResult: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3182 | case EK_Variable: |
| 3183 | case EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3184 | case EK_Parameter_CF_Audited: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3185 | case EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3186 | case EK_Binding: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3187 | case EK_New: |
| 3188 | case EK_Temporary: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 3189 | case EK_CompoundLiteralInit: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3190 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3191 | case EK_Delegating: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3192 | case EK_ArrayElement: |
| 3193 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 3194 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3195 | case EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3196 | case EK_LambdaToBlockConversionBlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 3197 | case EK_LambdaCapture: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 3198 | case EK_RelatedResult: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3199 | break; |
| 3200 | } |
| 3201 | |
| 3202 | return false; |
| 3203 | } |
| 3204 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3205 | unsigned InitializedEntity::dumpImpl(raw_ostream &OS) const { |
Richard Smith | e3b28bc | 2013-06-12 21:51:50 +0000 | [diff] [blame] | 3206 | assert(getParent() != this); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3207 | unsigned Depth = getParent() ? getParent()->dumpImpl(OS) : 0; |
| 3208 | for (unsigned I = 0; I != Depth; ++I) |
| 3209 | OS << "`-"; |
| 3210 | |
| 3211 | switch (getKind()) { |
| 3212 | case EK_Variable: OS << "Variable"; break; |
| 3213 | case EK_Parameter: OS << "Parameter"; break; |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3214 | case EK_Parameter_CF_Audited: OS << "CF audited function Parameter"; |
| 3215 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3216 | case EK_Result: OS << "Result"; break; |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 3217 | case EK_StmtExprResult: OS << "StmtExprResult"; break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3218 | case EK_Exception: OS << "Exception"; break; |
| 3219 | case EK_Member: OS << "Member"; break; |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3220 | case EK_Binding: OS << "Binding"; break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3221 | case EK_New: OS << "New"; break; |
| 3222 | case EK_Temporary: OS << "Temporary"; break; |
| 3223 | case EK_CompoundLiteralInit: OS << "CompoundLiteral";break; |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 3224 | case EK_RelatedResult: OS << "RelatedResult"; break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3225 | case EK_Base: OS << "Base"; break; |
| 3226 | case EK_Delegating: OS << "Delegating"; break; |
| 3227 | case EK_ArrayElement: OS << "ArrayElement " << Index; break; |
| 3228 | case EK_VectorElement: OS << "VectorElement " << Index; break; |
| 3229 | case EK_ComplexElement: OS << "ComplexElement " << Index; break; |
| 3230 | case EK_BlockElement: OS << "Block"; break; |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3231 | case EK_LambdaToBlockConversionBlockElement: |
| 3232 | OS << "Block (lambda)"; |
| 3233 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3234 | case EK_LambdaCapture: |
| 3235 | OS << "LambdaCapture "; |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 3236 | OS << DeclarationName(Capture.VarID); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3237 | break; |
| 3238 | } |
| 3239 | |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3240 | if (auto *D = getDecl()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3241 | OS << " "; |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3242 | D->printQualifiedName(OS); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3243 | } |
| 3244 | |
| 3245 | OS << " '" << getType().getAsString() << "'\n"; |
| 3246 | |
| 3247 | return Depth + 1; |
| 3248 | } |
| 3249 | |
Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 3250 | LLVM_DUMP_METHOD void InitializedEntity::dump() const { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3251 | dumpImpl(llvm::errs()); |
| 3252 | } |
| 3253 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3254 | //===----------------------------------------------------------------------===// |
| 3255 | // Initialization sequence |
| 3256 | //===----------------------------------------------------------------------===// |
| 3257 | |
| 3258 | void InitializationSequence::Step::Destroy() { |
| 3259 | switch (Kind) { |
| 3260 | case SK_ResolveAddressOfOverloadedFunction: |
| 3261 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3262 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3263 | case SK_CastDerivedToBaseLValue: |
| 3264 | case SK_BindReference: |
| 3265 | case SK_BindReferenceToTemporary: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 3266 | case SK_FinalCopy: |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3267 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3268 | case SK_UserConversion: |
| 3269 | case SK_QualificationConversionRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3270 | case SK_QualificationConversionXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3271 | case SK_QualificationConversionLValue: |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 3272 | case SK_AtomicConversion: |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3273 | case SK_LValueToRValue: |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3274 | case SK_ListInitialization: |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3275 | case SK_UnwrapInitList: |
| 3276 | case SK_RewrapInitList: |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3277 | case SK_ConstructorInitialization: |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 3278 | case SK_ConstructorInitializationFromList: |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3279 | case SK_ZeroInitialization: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3280 | case SK_CAssignment: |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3281 | case SK_StringInit: |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3282 | case SK_ObjCObjectConversion: |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3283 | case SK_ArrayLoopIndex: |
| 3284 | case SK_ArrayLoopInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3285 | case SK_ArrayInit: |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 3286 | case SK_GNUArrayInit: |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 3287 | case SK_ParenthesizedArrayInit: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3288 | case SK_PassByIndirectCopyRestore: |
| 3289 | case SK_PassByIndirectRestore: |
| 3290 | case SK_ProduceObjCObject: |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 3291 | case SK_StdInitializerList: |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3292 | case SK_StdInitializerListConstructorCall: |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 3293 | case SK_OCLSamplerInit: |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 3294 | case SK_OCLZeroOpaqueType: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3295 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3296 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3297 | case SK_ConversionSequence: |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 3298 | case SK_ConversionSequenceNoNarrowing: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3299 | delete ICS; |
| 3300 | } |
| 3301 | } |
| 3302 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3303 | bool InitializationSequence::isDirectReferenceBinding() const { |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 3304 | // There can be some lvalue adjustments after the SK_BindReference step. |
| 3305 | for (auto I = Steps.rbegin(); I != Steps.rend(); ++I) { |
| 3306 | if (I->Kind == SK_BindReference) |
| 3307 | return true; |
| 3308 | if (I->Kind == SK_BindReferenceToTemporary) |
| 3309 | return false; |
| 3310 | } |
| 3311 | return false; |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3312 | } |
| 3313 | |
| 3314 | bool InitializationSequence::isAmbiguous() const { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 3315 | if (!Failed()) |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3316 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3317 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3318 | switch (getFailureKind()) { |
| 3319 | case FK_TooManyInitsForReference: |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 3320 | case FK_ParenthesizedListInitForReference: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3321 | case FK_ArrayNeedsInitList: |
| 3322 | case FK_ArrayNeedsInitListOrStringLiteral: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 3323 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 3324 | case FK_NarrowStringIntoWideCharArray: |
| 3325 | case FK_WideStringIntoCharArray: |
| 3326 | case FK_IncompatWideStringIntoWideChar: |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 3327 | case FK_PlainStringIntoUTF8Char: |
| 3328 | case FK_UTF8StringIntoPlainChar: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3329 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 3330 | case FK_NonConstLValueReferenceBindingToTemporary: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 3331 | case FK_NonConstLValueReferenceBindingToBitfield: |
| 3332 | case FK_NonConstLValueReferenceBindingToVectorElement: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3333 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 3334 | case FK_RValueReferenceBindingToLValue: |
| 3335 | case FK_ReferenceInitDropsQualifiers: |
| 3336 | case FK_ReferenceInitFailed: |
| 3337 | case FK_ConversionFailed: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3338 | case FK_ConversionFromPropertyFailed: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3339 | case FK_TooManyInitsForScalar: |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 3340 | case FK_ParenthesizedListInitForScalar: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3341 | case FK_ReferenceBindingToInitList: |
| 3342 | case FK_InitListBadDestinationType: |
| 3343 | case FK_DefaultInitOfConst: |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 3344 | case FK_Incomplete: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3345 | case FK_ArrayTypeMismatch: |
| 3346 | case FK_NonConstantArrayInit: |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 3347 | case FK_ListInitializationFailed: |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 3348 | case FK_VariableLengthArrayHasInitializer: |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3349 | case FK_PlaceholderType: |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3350 | case FK_ExplicitConstructor: |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 3351 | case FK_AddressOfUnaddressableFunction: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3352 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3353 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3354 | case FK_ReferenceInitOverloadFailed: |
| 3355 | case FK_UserConversionOverloadFailed: |
| 3356 | case FK_ConstructorOverloadFailed: |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3357 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3358 | return FailedOverloadResult == OR_Ambiguous; |
| 3359 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3360 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3361 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3362 | } |
| 3363 | |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 3364 | bool InitializationSequence::isConstructorInitialization() const { |
| 3365 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 3366 | } |
| 3367 | |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3368 | void |
| 3369 | InitializationSequence |
| 3370 | ::AddAddressOverloadResolutionStep(FunctionDecl *Function, |
| 3371 | DeclAccessPair Found, |
| 3372 | bool HadMultipleCandidates) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3373 | Step S; |
| 3374 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 3375 | S.Type = Function->getType(); |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3376 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3377 | S.Function.Function = Function; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3378 | S.Function.FoundDecl = Found; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3379 | Steps.push_back(S); |
| 3380 | } |
| 3381 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3382 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3383 | ExprValueKind VK) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3384 | Step S; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3385 | switch (VK) { |
| 3386 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 3387 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 3388 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3389 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3390 | S.Type = BaseType; |
| 3391 | Steps.push_back(S); |
| 3392 | } |
| 3393 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3394 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3395 | bool BindingTemporary) { |
| 3396 | Step S; |
| 3397 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 3398 | S.Type = T; |
| 3399 | Steps.push_back(S); |
| 3400 | } |
| 3401 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 3402 | void InitializationSequence::AddFinalCopy(QualType T) { |
| 3403 | Step S; |
| 3404 | S.Kind = SK_FinalCopy; |
| 3405 | S.Type = T; |
| 3406 | Steps.push_back(S); |
| 3407 | } |
| 3408 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3409 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 3410 | Step S; |
| 3411 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 3412 | S.Type = T; |
| 3413 | Steps.push_back(S); |
| 3414 | } |
| 3415 | |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3416 | void |
| 3417 | InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
| 3418 | DeclAccessPair FoundDecl, |
| 3419 | QualType T, |
| 3420 | bool HadMultipleCandidates) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3421 | Step S; |
| 3422 | S.Kind = SK_UserConversion; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3423 | S.Type = T; |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3424 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3425 | S.Function.Function = Function; |
| 3426 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3427 | Steps.push_back(S); |
| 3428 | } |
| 3429 | |
| 3430 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3431 | ExprValueKind VK) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3432 | Step S; |
John McCall | 7a1da89 | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 3433 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3434 | switch (VK) { |
| 3435 | case VK_RValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3436 | S.Kind = SK_QualificationConversionRValue; |
| 3437 | break; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3438 | case VK_XValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3439 | S.Kind = SK_QualificationConversionXValue; |
| 3440 | break; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3441 | case VK_LValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3442 | S.Kind = SK_QualificationConversionLValue; |
| 3443 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3444 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3445 | S.Type = Ty; |
| 3446 | Steps.push_back(S); |
| 3447 | } |
| 3448 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 3449 | void InitializationSequence::AddAtomicConversionStep(QualType Ty) { |
| 3450 | Step S; |
| 3451 | S.Kind = SK_AtomicConversion; |
| 3452 | S.Type = Ty; |
| 3453 | Steps.push_back(S); |
| 3454 | } |
| 3455 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3456 | void InitializationSequence::AddLValueToRValueStep(QualType Ty) { |
| 3457 | assert(!Ty.hasQualifiers() && "rvalues may not have qualifiers"); |
| 3458 | |
| 3459 | Step S; |
| 3460 | S.Kind = SK_LValueToRValue; |
| 3461 | S.Type = Ty; |
| 3462 | Steps.push_back(S); |
| 3463 | } |
| 3464 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3465 | void InitializationSequence::AddConversionSequenceStep( |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 3466 | const ImplicitConversionSequence &ICS, QualType T, |
| 3467 | bool TopLevelOfInitList) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3468 | Step S; |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 3469 | S.Kind = TopLevelOfInitList ? SK_ConversionSequenceNoNarrowing |
| 3470 | : SK_ConversionSequence; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3471 | S.Type = T; |
| 3472 | S.ICS = new ImplicitConversionSequence(ICS); |
| 3473 | Steps.push_back(S); |
| 3474 | } |
| 3475 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3476 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 3477 | Step S; |
| 3478 | S.Kind = SK_ListInitialization; |
| 3479 | S.Type = T; |
| 3480 | Steps.push_back(S); |
| 3481 | } |
| 3482 | |
Richard Smith | 55c2888 | 2016-05-12 23:45:49 +0000 | [diff] [blame] | 3483 | void InitializationSequence::AddConstructorInitializationStep( |
| 3484 | DeclAccessPair FoundDecl, CXXConstructorDecl *Constructor, QualType T, |
| 3485 | bool HadMultipleCandidates, bool FromInitList, bool AsInitList) { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3486 | Step S; |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 3487 | S.Kind = FromInitList ? AsInitList ? SK_StdInitializerListConstructorCall |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 3488 | : SK_ConstructorInitializationFromList |
| 3489 | : SK_ConstructorInitialization; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3490 | S.Type = T; |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3491 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3492 | S.Function.Function = Constructor; |
Richard Smith | 55c2888 | 2016-05-12 23:45:49 +0000 | [diff] [blame] | 3493 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3494 | Steps.push_back(S); |
| 3495 | } |
| 3496 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3497 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 3498 | Step S; |
| 3499 | S.Kind = SK_ZeroInitialization; |
| 3500 | S.Type = T; |
| 3501 | Steps.push_back(S); |
| 3502 | } |
| 3503 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3504 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 3505 | Step S; |
| 3506 | S.Kind = SK_CAssignment; |
| 3507 | S.Type = T; |
| 3508 | Steps.push_back(S); |
| 3509 | } |
| 3510 | |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3511 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 3512 | Step S; |
| 3513 | S.Kind = SK_StringInit; |
| 3514 | S.Type = T; |
| 3515 | Steps.push_back(S); |
| 3516 | } |
| 3517 | |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3518 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 3519 | Step S; |
| 3520 | S.Kind = SK_ObjCObjectConversion; |
| 3521 | S.Type = T; |
| 3522 | Steps.push_back(S); |
| 3523 | } |
| 3524 | |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 3525 | void InitializationSequence::AddArrayInitStep(QualType T, bool IsGNUExtension) { |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3526 | Step S; |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 3527 | S.Kind = IsGNUExtension ? SK_GNUArrayInit : SK_ArrayInit; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3528 | S.Type = T; |
| 3529 | Steps.push_back(S); |
| 3530 | } |
| 3531 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3532 | void InitializationSequence::AddArrayInitLoopStep(QualType T, QualType EltT) { |
| 3533 | Step S; |
| 3534 | S.Kind = SK_ArrayLoopIndex; |
| 3535 | S.Type = EltT; |
| 3536 | Steps.insert(Steps.begin(), S); |
| 3537 | |
| 3538 | S.Kind = SK_ArrayLoopInit; |
| 3539 | S.Type = T; |
| 3540 | Steps.push_back(S); |
| 3541 | } |
| 3542 | |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 3543 | void InitializationSequence::AddParenthesizedArrayInitStep(QualType T) { |
| 3544 | Step S; |
| 3545 | S.Kind = SK_ParenthesizedArrayInit; |
| 3546 | S.Type = T; |
| 3547 | Steps.push_back(S); |
| 3548 | } |
| 3549 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3550 | void InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type, |
| 3551 | bool shouldCopy) { |
| 3552 | Step s; |
| 3553 | s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore |
| 3554 | : SK_PassByIndirectRestore); |
| 3555 | s.Type = type; |
| 3556 | Steps.push_back(s); |
| 3557 | } |
| 3558 | |
| 3559 | void InitializationSequence::AddProduceObjCObjectStep(QualType T) { |
| 3560 | Step S; |
| 3561 | S.Kind = SK_ProduceObjCObject; |
| 3562 | S.Type = T; |
| 3563 | Steps.push_back(S); |
| 3564 | } |
| 3565 | |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 3566 | void InitializationSequence::AddStdInitializerListConstructionStep(QualType T) { |
| 3567 | Step S; |
| 3568 | S.Kind = SK_StdInitializerList; |
| 3569 | S.Type = T; |
| 3570 | Steps.push_back(S); |
| 3571 | } |
| 3572 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 3573 | void InitializationSequence::AddOCLSamplerInitStep(QualType T) { |
| 3574 | Step S; |
| 3575 | S.Kind = SK_OCLSamplerInit; |
| 3576 | S.Type = T; |
| 3577 | Steps.push_back(S); |
| 3578 | } |
| 3579 | |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 3580 | void InitializationSequence::AddOCLZeroOpaqueTypeStep(QualType T) { |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 3581 | Step S; |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 3582 | S.Kind = SK_OCLZeroOpaqueType; |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 3583 | S.Type = T; |
| 3584 | Steps.push_back(S); |
| 3585 | } |
| 3586 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3587 | void InitializationSequence::RewrapReferenceInitList(QualType T, |
| 3588 | InitListExpr *Syntactic) { |
| 3589 | assert(Syntactic->getNumInits() == 1 && |
| 3590 | "Can only rewrap trivial init lists."); |
| 3591 | Step S; |
| 3592 | S.Kind = SK_UnwrapInitList; |
| 3593 | S.Type = Syntactic->getInit(0)->getType(); |
| 3594 | Steps.insert(Steps.begin(), S); |
| 3595 | |
| 3596 | S.Kind = SK_RewrapInitList; |
| 3597 | S.Type = T; |
| 3598 | S.WrappingSyntacticList = Syntactic; |
| 3599 | Steps.push_back(S); |
| 3600 | } |
| 3601 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3602 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3603 | OverloadingResult Result) { |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 3604 | setSequenceKind(FailedSequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3605 | this->Failure = Failure; |
| 3606 | this->FailedOverloadResult = Result; |
| 3607 | } |
| 3608 | |
| 3609 | //===----------------------------------------------------------------------===// |
| 3610 | // Attempt initialization |
| 3611 | //===----------------------------------------------------------------------===// |
| 3612 | |
Nico Weber | 337d5aa | 2015-04-17 08:32:38 +0000 | [diff] [blame] | 3613 | /// Tries to add a zero initializer. Returns true if that worked. |
| 3614 | static bool |
| 3615 | maybeRecoverWithZeroInitialization(Sema &S, InitializationSequence &Sequence, |
| 3616 | const InitializedEntity &Entity) { |
| 3617 | if (Entity.getKind() != InitializedEntity::EK_Variable) |
| 3618 | return false; |
| 3619 | |
| 3620 | VarDecl *VD = cast<VarDecl>(Entity.getDecl()); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 3621 | if (VD->getInit() || VD->getEndLoc().isMacroID()) |
Nico Weber | 337d5aa | 2015-04-17 08:32:38 +0000 | [diff] [blame] | 3622 | return false; |
| 3623 | |
| 3624 | QualType VariableTy = VD->getType().getCanonicalType(); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 3625 | SourceLocation Loc = S.getLocForEndOfToken(VD->getEndLoc()); |
Nico Weber | 337d5aa | 2015-04-17 08:32:38 +0000 | [diff] [blame] | 3626 | std::string Init = S.getFixItZeroInitializerForType(VariableTy, Loc); |
| 3627 | if (!Init.empty()) { |
| 3628 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3629 | Sequence.SetZeroInitializationFixit(Init, Loc); |
| 3630 | return true; |
| 3631 | } |
| 3632 | return false; |
| 3633 | } |
| 3634 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3635 | static void MaybeProduceObjCObject(Sema &S, |
| 3636 | InitializationSequence &Sequence, |
| 3637 | const InitializedEntity &Entity) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3638 | if (!S.getLangOpts().ObjCAutoRefCount) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3639 | |
| 3640 | /// When initializing a parameter, produce the value if it's marked |
| 3641 | /// __attribute__((ns_consumed)). |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3642 | if (Entity.isParameterKind()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3643 | if (!Entity.isParameterConsumed()) |
| 3644 | return; |
| 3645 | |
| 3646 | assert(Entity.getType()->isObjCRetainableType() && |
| 3647 | "consuming an object of unretainable type?"); |
| 3648 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 3649 | |
| 3650 | /// When initializing a return value, if the return type is a |
| 3651 | /// retainable type, then returns need to immediately retain the |
| 3652 | /// object. If an autorelease is required, it will be done at the |
| 3653 | /// last instant. |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 3654 | } else if (Entity.getKind() == InitializedEntity::EK_Result || |
| 3655 | Entity.getKind() == InitializedEntity::EK_StmtExprResult) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3656 | if (!Entity.getType()->isObjCRetainableType()) |
| 3657 | return; |
| 3658 | |
| 3659 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 3660 | } |
| 3661 | } |
| 3662 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3663 | static void TryListInitialization(Sema &S, |
| 3664 | const InitializedEntity &Entity, |
| 3665 | const InitializationKind &Kind, |
| 3666 | InitListExpr *InitList, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 3667 | InitializationSequence &Sequence, |
| 3668 | bool TreatUnavailableAsInvalid); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3669 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3670 | /// When initializing from init list via constructor, handle |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3671 | /// initialization of an object of type std::initializer_list<T>. |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3672 | /// |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3673 | /// \return true if we have handled initialization of an object of type |
| 3674 | /// std::initializer_list<T>, false otherwise. |
| 3675 | static bool TryInitializerListConstruction(Sema &S, |
| 3676 | InitListExpr *List, |
| 3677 | QualType DestType, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 3678 | InitializationSequence &Sequence, |
| 3679 | bool TreatUnavailableAsInvalid) { |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3680 | QualType E; |
| 3681 | if (!S.isStdInitializerList(DestType, &E)) |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3682 | return false; |
| 3683 | |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3684 | if (!S.isCompleteType(List->getExprLoc(), E)) { |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3685 | Sequence.setIncompleteTypeFailure(E); |
| 3686 | return true; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3687 | } |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3688 | |
| 3689 | // Try initializing a temporary array from the init list. |
| 3690 | QualType ArrayType = S.Context.getConstantArrayType( |
| 3691 | E.withConst(), llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 3692 | List->getNumInits()), |
| 3693 | clang::ArrayType::Normal, 0); |
| 3694 | InitializedEntity HiddenArray = |
| 3695 | InitializedEntity::InitializeTemporary(ArrayType); |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 3696 | InitializationKind Kind = InitializationKind::CreateDirectList( |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 3697 | List->getExprLoc(), List->getBeginLoc(), List->getEndLoc()); |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 3698 | TryListInitialization(S, HiddenArray, Kind, List, Sequence, |
| 3699 | TreatUnavailableAsInvalid); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3700 | if (Sequence) |
| 3701 | Sequence.AddStdInitializerListConstructionStep(DestType); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3702 | return true; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3703 | } |
| 3704 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3705 | /// Determine if the constructor has the signature of a copy or move |
| 3706 | /// constructor for the type T of the class in which it was found. That is, |
| 3707 | /// determine if its first parameter is of type T or reference to (possibly |
| 3708 | /// cv-qualified) T. |
| 3709 | static bool hasCopyOrMoveCtorParam(ASTContext &Ctx, |
| 3710 | const ConstructorInfo &Info) { |
| 3711 | if (Info.Constructor->getNumParams() == 0) |
| 3712 | return false; |
| 3713 | |
| 3714 | QualType ParmT = |
| 3715 | Info.Constructor->getParamDecl(0)->getType().getNonReferenceType(); |
| 3716 | QualType ClassT = |
| 3717 | Ctx.getRecordType(cast<CXXRecordDecl>(Info.FoundDecl->getDeclContext())); |
| 3718 | |
| 3719 | return Ctx.hasSameUnqualifiedType(ParmT, ClassT); |
| 3720 | } |
| 3721 | |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3722 | static OverloadingResult |
| 3723 | ResolveConstructorOverload(Sema &S, SourceLocation DeclLoc, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3724 | MultiExprArg Args, |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3725 | OverloadCandidateSet &CandidateSet, |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3726 | QualType DestType, |
Richard Smith | 40c7806 | 2015-02-21 02:31:57 +0000 | [diff] [blame] | 3727 | DeclContext::lookup_result Ctors, |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3728 | OverloadCandidateSet::iterator &Best, |
| 3729 | bool CopyInitializing, bool AllowExplicit, |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3730 | bool OnlyListConstructors, bool IsListInit, |
| 3731 | bool SecondStepOfCopyInit = false) { |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3732 | CandidateSet.clear(OverloadCandidateSet::CSK_InitByConstructor); |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3733 | |
Richard Smith | 40c7806 | 2015-02-21 02:31:57 +0000 | [diff] [blame] | 3734 | for (NamedDecl *D : Ctors) { |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3735 | auto Info = getConstructorInfo(D); |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3736 | if (!Info.Constructor || Info.Constructor->isInvalidDecl()) |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3737 | continue; |
| 3738 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3739 | if (!AllowExplicit && Info.Constructor->isExplicit()) |
| 3740 | continue; |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3741 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3742 | if (OnlyListConstructors && !S.isInitListConstructor(Info.Constructor)) |
| 3743 | continue; |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3744 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3745 | // C++11 [over.best.ics]p4: |
| 3746 | // ... and the constructor or user-defined conversion function is a |
| 3747 | // candidate by |
| 3748 | // - 13.3.1.3, when the argument is the temporary in the second step |
| 3749 | // of a class copy-initialization, or |
| 3750 | // - 13.3.1.4, 13.3.1.5, or 13.3.1.6 (in all cases), [not handled here] |
| 3751 | // - the second phase of 13.3.1.7 when the initializer list has exactly |
| 3752 | // one element that is itself an initializer list, and the target is |
| 3753 | // the first parameter of a constructor of class X, and the conversion |
| 3754 | // is to X or reference to (possibly cv-qualified X), |
| 3755 | // user-defined conversion sequences are not considered. |
| 3756 | bool SuppressUserConversions = |
| 3757 | SecondStepOfCopyInit || |
| 3758 | (IsListInit && Args.size() == 1 && isa<InitListExpr>(Args[0]) && |
| 3759 | hasCopyOrMoveCtorParam(S.Context, Info)); |
| 3760 | |
| 3761 | if (Info.ConstructorTmpl) |
| 3762 | S.AddTemplateOverloadCandidate(Info.ConstructorTmpl, Info.FoundDecl, |
| 3763 | /*ExplicitArgs*/ nullptr, Args, |
| 3764 | CandidateSet, SuppressUserConversions); |
| 3765 | else { |
| 3766 | // C++ [over.match.copy]p1: |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3767 | // - When initializing a temporary to be bound to the first parameter |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3768 | // of a constructor [for type T] that takes a reference to possibly |
| 3769 | // cv-qualified T as its first argument, called with a single |
| 3770 | // argument in the context of direct-initialization, explicit |
| 3771 | // conversion functions are also considered. |
| 3772 | // FIXME: What if a constructor template instantiates to such a signature? |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3773 | bool AllowExplicitConv = AllowExplicit && !CopyInitializing && |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 3774 | Args.size() == 1 && |
| 3775 | hasCopyOrMoveCtorParam(S.Context, Info); |
| 3776 | S.AddOverloadCandidate(Info.Constructor, Info.FoundDecl, Args, |
| 3777 | CandidateSet, SuppressUserConversions, |
| 3778 | /*PartialOverloading=*/false, |
| 3779 | /*AllowExplicit=*/AllowExplicitConv); |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3780 | } |
| 3781 | } |
| 3782 | |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3783 | // FIXME: Work around a bug in C++17 guaranteed copy elision. |
| 3784 | // |
| 3785 | // When initializing an object of class type T by constructor |
| 3786 | // ([over.match.ctor]) or by list-initialization ([over.match.list]) |
| 3787 | // from a single expression of class type U, conversion functions of |
| 3788 | // U that convert to the non-reference type cv T are candidates. |
| 3789 | // Explicit conversion functions are only candidates during |
| 3790 | // direct-initialization. |
| 3791 | // |
| 3792 | // Note: SecondStepOfCopyInit is only ever true in this case when |
| 3793 | // evaluating whether to produce a C++98 compatibility warning. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 3794 | if (S.getLangOpts().CPlusPlus17 && Args.size() == 1 && |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3795 | !SecondStepOfCopyInit) { |
| 3796 | Expr *Initializer = Args[0]; |
| 3797 | auto *SourceRD = Initializer->getType()->getAsCXXRecordDecl(); |
| 3798 | if (SourceRD && S.isCompleteType(DeclLoc, Initializer->getType())) { |
| 3799 | const auto &Conversions = SourceRD->getVisibleConversionFunctions(); |
| 3800 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
| 3801 | NamedDecl *D = *I; |
| 3802 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3803 | D = D->getUnderlyingDecl(); |
| 3804 | |
| 3805 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3806 | CXXConversionDecl *Conv; |
| 3807 | if (ConvTemplate) |
| 3808 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3809 | else |
| 3810 | Conv = cast<CXXConversionDecl>(D); |
| 3811 | |
| 3812 | if ((AllowExplicit && !CopyInitializing) || !Conv->isExplicit()) { |
| 3813 | if (ConvTemplate) |
| 3814 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
| 3815 | ActingDC, Initializer, DestType, |
| 3816 | CandidateSet, AllowExplicit, |
| 3817 | /*AllowResultConversion*/false); |
| 3818 | else |
| 3819 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Initializer, |
| 3820 | DestType, CandidateSet, AllowExplicit, |
| 3821 | /*AllowResultConversion*/false); |
| 3822 | } |
| 3823 | } |
| 3824 | } |
| 3825 | } |
| 3826 | |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3827 | // Perform overload resolution and return the result. |
| 3828 | return CandidateSet.BestViableFunction(S, DeclLoc, Best); |
| 3829 | } |
| 3830 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3831 | /// Attempt initialization by constructor (C++ [dcl.init]), which |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3832 | /// enumerates the constructors of the initialized entity and performs overload |
| 3833 | /// resolution to select the best. |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3834 | /// \param DestType The destination class type. |
| 3835 | /// \param DestArrayType The destination type, which is either DestType or |
| 3836 | /// a (possibly multidimensional) array of DestType. |
NAKAMURA Takumi | ffcc98a | 2015-02-05 23:12:13 +0000 | [diff] [blame] | 3837 | /// \param IsListInit Is this list-initialization? |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3838 | /// \param IsInitListCopy Is this non-list-initialization resulting from a |
| 3839 | /// list-initialization from {x} where x is the same |
| 3840 | /// type as the entity? |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3841 | static void TryConstructorInitialization(Sema &S, |
| 3842 | const InitializedEntity &Entity, |
| 3843 | const InitializationKind &Kind, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3844 | MultiExprArg Args, QualType DestType, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 3845 | QualType DestArrayType, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3846 | InitializationSequence &Sequence, |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3847 | bool IsListInit = false, |
| 3848 | bool IsInitListCopy = false) { |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3849 | assert(((!IsListInit && !IsInitListCopy) || |
| 3850 | (Args.size() == 1 && isa<InitListExpr>(Args[0]))) && |
| 3851 | "IsListInit/IsInitListCopy must come with a single initializer list " |
| 3852 | "argument."); |
| 3853 | InitListExpr *ILE = |
| 3854 | (IsListInit || IsInitListCopy) ? cast<InitListExpr>(Args[0]) : nullptr; |
| 3855 | MultiExprArg UnwrappedArgs = |
| 3856 | ILE ? MultiExprArg(ILE->getInits(), ILE->getNumInits()) : Args; |
Sebastian Redl | 88e4d49 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3857 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3858 | // The type we're constructing needs to be complete. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3859 | if (!S.isCompleteType(Kind.getLocation(), DestType)) { |
Douglas Gregor | 85f3423 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 3860 | Sequence.setIncompleteTypeFailure(DestType); |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3861 | return; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3862 | } |
| 3863 | |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 3864 | // C++17 [dcl.init]p17: |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3865 | // - If the initializer expression is a prvalue and the cv-unqualified |
| 3866 | // version of the source type is the same class as the class of the |
| 3867 | // destination, the initializer expression is used to initialize the |
| 3868 | // destination object. |
| 3869 | // Per DR (no number yet), this does not apply when initializing a base |
| 3870 | // class or delegating to another constructor from a mem-initializer. |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3871 | // ObjC++: Lambda captured by the block in the lambda to block conversion |
| 3872 | // should avoid copy elision. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 3873 | if (S.getLangOpts().CPlusPlus17 && |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3874 | Entity.getKind() != InitializedEntity::EK_Base && |
| 3875 | Entity.getKind() != InitializedEntity::EK_Delegating && |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 3876 | Entity.getKind() != |
| 3877 | InitializedEntity::EK_LambdaToBlockConversionBlockElement && |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3878 | UnwrappedArgs.size() == 1 && UnwrappedArgs[0]->isRValue() && |
| 3879 | S.Context.hasSameUnqualifiedType(UnwrappedArgs[0]->getType(), DestType)) { |
| 3880 | // Convert qualifications if necessary. |
Richard Smith | 16d3150 | 2016-12-21 01:31:56 +0000 | [diff] [blame] | 3881 | Sequence.AddQualificationConversionStep(DestType, VK_RValue); |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3882 | if (ILE) |
| 3883 | Sequence.RewrapReferenceInitList(DestType, ILE); |
| 3884 | return; |
| 3885 | } |
| 3886 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3887 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
| 3888 | assert(DestRecordType && "Constructor initialization requires record type"); |
| 3889 | CXXRecordDecl *DestRecordDecl |
| 3890 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
| 3891 | |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3892 | // Build the candidate set directly in the initialization sequence |
| 3893 | // structure, so that it will persist if we fail. |
| 3894 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3895 | |
| 3896 | // Determine whether we are allowed to call explicit constructors or |
| 3897 | // explicit conversion operators. |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3898 | bool AllowExplicit = Kind.AllowExplicit() || IsListInit; |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3899 | bool CopyInitialization = Kind.getKind() == InitializationKind::IK_Copy; |
Sebastian Redl | 88e4d49 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3900 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3901 | // - Otherwise, if T is a class type, constructors are considered. The |
| 3902 | // applicable constructors are enumerated, and the best one is chosen |
| 3903 | // through overload resolution. |
Richard Smith | 40c7806 | 2015-02-21 02:31:57 +0000 | [diff] [blame] | 3904 | DeclContext::lookup_result Ctors = S.LookupConstructors(DestRecordDecl); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3905 | |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3906 | OverloadingResult Result = OR_No_Viable_Function; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3907 | OverloadCandidateSet::iterator Best; |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3908 | bool AsInitializerList = false; |
| 3909 | |
Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 3910 | // C++11 [over.match.list]p1, per DR1467: |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 3911 | // When objects of non-aggregate type T are list-initialized, such that |
| 3912 | // 8.5.4 [dcl.init.list] specifies that overload resolution is performed |
| 3913 | // according to the rules in this section, overload resolution selects |
| 3914 | // the constructor in two phases: |
| 3915 | // |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3916 | // - Initially, the candidate functions are the initializer-list |
| 3917 | // constructors of the class T and the argument list consists of the |
| 3918 | // initializer list as a single argument. |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3919 | if (IsListInit) { |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3920 | AsInitializerList = true; |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3921 | |
| 3922 | // If the initializer list has no elements and T has a default constructor, |
| 3923 | // the first phase is omitted. |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3924 | if (!(UnwrappedArgs.empty() && DestRecordDecl->hasDefaultConstructor())) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3925 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3926 | CandidateSet, DestType, Ctors, Best, |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3927 | CopyInitialization, AllowExplicit, |
Larisse Voufo | bcf327a | 2015-02-10 02:20:14 +0000 | [diff] [blame] | 3928 | /*OnlyListConstructor=*/true, |
| 3929 | IsListInit); |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3930 | } |
| 3931 | |
| 3932 | // C++11 [over.match.list]p1: |
| 3933 | // - If no viable initializer-list constructor is found, overload resolution |
| 3934 | // is performed again, where the candidate functions are all the |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3935 | // constructors of the class T and the argument list consists of the |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3936 | // elements of the initializer list. |
| 3937 | if (Result == OR_No_Viable_Function) { |
| 3938 | AsInitializerList = false; |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3939 | Result = ResolveConstructorOverload(S, Kind.getLocation(), UnwrappedArgs, |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3940 | CandidateSet, DestType, Ctors, Best, |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3941 | CopyInitialization, AllowExplicit, |
Larisse Voufo | bcf327a | 2015-02-10 02:20:14 +0000 | [diff] [blame] | 3942 | /*OnlyListConstructors=*/false, |
| 3943 | IsListInit); |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3944 | } |
| 3945 | if (Result) { |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3946 | Sequence.SetOverloadFailure(IsListInit ? |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3947 | InitializationSequence::FK_ListConstructorOverloadFailed : |
| 3948 | InitializationSequence::FK_ConstructorOverloadFailed, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3949 | Result); |
| 3950 | return; |
| 3951 | } |
| 3952 | |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3953 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 3954 | |
| 3955 | // In C++17, ResolveConstructorOverload can select a conversion function |
| 3956 | // instead of a constructor. |
| 3957 | if (auto *CD = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 3958 | // Add the user-defined conversion step that calls the conversion function. |
| 3959 | QualType ConvType = CD->getConversionType(); |
| 3960 | assert(S.Context.hasSameUnqualifiedType(ConvType, DestType) && |
| 3961 | "should not have selected this conversion function"); |
| 3962 | Sequence.AddUserConversionStep(CD, Best->FoundDecl, ConvType, |
| 3963 | HadMultipleCandidates); |
| 3964 | if (!S.Context.hasSameType(ConvType, DestType)) |
| 3965 | Sequence.AddQualificationConversionStep(DestType, VK_RValue); |
| 3966 | if (IsListInit) |
| 3967 | Sequence.RewrapReferenceInitList(Entity.getType(), ILE); |
| 3968 | return; |
| 3969 | } |
| 3970 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3971 | // C++11 [dcl.init]p6: |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3972 | // If a program calls for the default initialization of an object |
| 3973 | // of a const-qualified type T, T shall be a class type with a |
| 3974 | // user-provided default constructor. |
Nico Weber | 6a6376b | 2016-02-19 01:52:46 +0000 | [diff] [blame] | 3975 | // C++ core issue 253 proposal: |
| 3976 | // If the implicit default constructor initializes all subobjects, no |
| 3977 | // initializer should be required. |
| 3978 | // The 253 proposal is for example needed to process libstdc++ headers in 5.x. |
| 3979 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3980 | if (Kind.getKind() == InitializationKind::IK_Default && |
Nico Weber | 6a6376b | 2016-02-19 01:52:46 +0000 | [diff] [blame] | 3981 | Entity.getType().isConstQualified()) { |
| 3982 | if (!CtorDecl->getParent()->allowConstDefaultInit()) { |
| 3983 | if (!maybeRecoverWithZeroInitialization(S, Sequence, Entity)) |
| 3984 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 3985 | return; |
| 3986 | } |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3987 | } |
| 3988 | |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3989 | // C++11 [over.match.list]p1: |
| 3990 | // In copy-list-initialization, if an explicit constructor is chosen, the |
| 3991 | // initializer is ill-formed. |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3992 | if (IsListInit && !Kind.AllowExplicit() && CtorDecl->isExplicit()) { |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3993 | Sequence.SetFailed(InitializationSequence::FK_ExplicitConstructor); |
| 3994 | return; |
| 3995 | } |
| 3996 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3997 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 3998 | // subsumed by the initialization. |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3999 | Sequence.AddConstructorInitializationStep( |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 4000 | Best->FoundDecl, CtorDecl, DestArrayType, HadMultipleCandidates, |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 4001 | IsListInit | IsInitListCopy, AsInitializerList); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4002 | } |
| 4003 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4004 | static bool |
| 4005 | ResolveOverloadedFunctionForReferenceBinding(Sema &S, |
| 4006 | Expr *Initializer, |
| 4007 | QualType &SourceType, |
| 4008 | QualType &UnqualifiedSourceType, |
| 4009 | QualType UnqualifiedTargetType, |
| 4010 | InitializationSequence &Sequence) { |
| 4011 | if (S.Context.getCanonicalType(UnqualifiedSourceType) == |
| 4012 | S.Context.OverloadTy) { |
| 4013 | DeclAccessPair Found; |
| 4014 | bool HadMultipleCandidates = false; |
| 4015 | if (FunctionDecl *Fn |
| 4016 | = S.ResolveAddressOfOverloadedFunction(Initializer, |
| 4017 | UnqualifiedTargetType, |
| 4018 | false, Found, |
| 4019 | &HadMultipleCandidates)) { |
| 4020 | Sequence.AddAddressOverloadResolutionStep(Fn, Found, |
| 4021 | HadMultipleCandidates); |
| 4022 | SourceType = Fn->getType(); |
| 4023 | UnqualifiedSourceType = SourceType.getUnqualifiedType(); |
| 4024 | } else if (!UnqualifiedTargetType->isRecordType()) { |
| 4025 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 4026 | return true; |
| 4027 | } |
| 4028 | } |
| 4029 | return false; |
| 4030 | } |
| 4031 | |
| 4032 | static void TryReferenceInitializationCore(Sema &S, |
| 4033 | const InitializedEntity &Entity, |
| 4034 | const InitializationKind &Kind, |
| 4035 | Expr *Initializer, |
| 4036 | QualType cv1T1, QualType T1, |
| 4037 | Qualifiers T1Quals, |
| 4038 | QualType cv2T2, QualType T2, |
| 4039 | Qualifiers T2Quals, |
| 4040 | InitializationSequence &Sequence); |
| 4041 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4042 | static void TryValueInitialization(Sema &S, |
| 4043 | const InitializedEntity &Entity, |
| 4044 | const InitializationKind &Kind, |
| 4045 | InitializationSequence &Sequence, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4046 | InitListExpr *InitList = nullptr); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4047 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4048 | /// Attempt list initialization of a reference. |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4049 | static void TryReferenceListInitialization(Sema &S, |
| 4050 | const InitializedEntity &Entity, |
| 4051 | const InitializationKind &Kind, |
| 4052 | InitListExpr *InitList, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4053 | InitializationSequence &Sequence, |
| 4054 | bool TreatUnavailableAsInvalid) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4055 | // First, catch C++03 where this isn't possible. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4056 | if (!S.getLangOpts().CPlusPlus11) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4057 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 4058 | return; |
| 4059 | } |
David Majnemer | 9370dc2 | 2015-04-26 07:35:03 +0000 | [diff] [blame] | 4060 | // Can't reference initialize a compound literal. |
| 4061 | if (Entity.getKind() == InitializedEntity::EK_CompoundLiteralInit) { |
| 4062 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 4063 | return; |
| 4064 | } |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4065 | |
| 4066 | QualType DestType = Entity.getType(); |
| 4067 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 4068 | Qualifiers T1Quals; |
| 4069 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
| 4070 | |
| 4071 | // Reference initialization via an initializer list works thus: |
| 4072 | // If the initializer list consists of a single element that is |
| 4073 | // reference-related to the referenced type, bind directly to that element |
| 4074 | // (possibly creating temporaries). |
| 4075 | // Otherwise, initialize a temporary with the initializer list and |
| 4076 | // bind to that. |
| 4077 | if (InitList->getNumInits() == 1) { |
| 4078 | Expr *Initializer = InitList->getInit(0); |
| 4079 | QualType cv2T2 = Initializer->getType(); |
| 4080 | Qualifiers T2Quals; |
| 4081 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
| 4082 | |
| 4083 | // If this fails, creating a temporary wouldn't work either. |
| 4084 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 4085 | T1, Sequence)) |
| 4086 | return; |
| 4087 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4088 | SourceLocation DeclLoc = Initializer->getBeginLoc(); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4089 | bool dummy1, dummy2, dummy3; |
| 4090 | Sema::ReferenceCompareResult RefRelationship |
| 4091 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, dummy1, |
| 4092 | dummy2, dummy3); |
| 4093 | if (RefRelationship >= Sema::Ref_Related) { |
| 4094 | // Try to bind the reference here. |
| 4095 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 4096 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 4097 | if (Sequence) |
| 4098 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
| 4099 | return; |
| 4100 | } |
Richard Smith | 03d9393 | 2013-01-15 07:58:29 +0000 | [diff] [blame] | 4101 | |
| 4102 | // Update the initializer if we've resolved an overloaded function. |
| 4103 | if (Sequence.step_begin() != Sequence.step_end()) |
| 4104 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4105 | } |
| 4106 | |
| 4107 | // Not reference-related. Create a temporary and bind to that. |
| 4108 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 4109 | |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4110 | TryListInitialization(S, TempEntity, Kind, InitList, Sequence, |
| 4111 | TreatUnavailableAsInvalid); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4112 | if (Sequence) { |
| 4113 | if (DestType->isRValueReferenceType() || |
| 4114 | (T1Quals.hasConst() && !T1Quals.hasVolatile())) |
| 4115 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 4116 | else |
| 4117 | Sequence.SetFailed( |
| 4118 | InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
| 4119 | } |
| 4120 | } |
| 4121 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4122 | /// Attempt list initialization (C++0x [dcl.init.list]) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4123 | static void TryListInitialization(Sema &S, |
| 4124 | const InitializedEntity &Entity, |
| 4125 | const InitializationKind &Kind, |
| 4126 | InitListExpr *InitList, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4127 | InitializationSequence &Sequence, |
| 4128 | bool TreatUnavailableAsInvalid) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4129 | QualType DestType = Entity.getType(); |
| 4130 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4131 | // C++ doesn't allow scalar initialization with more than one argument. |
| 4132 | // But C99 complex numbers are scalars and it makes sense there. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4133 | if (S.getLangOpts().CPlusPlus && DestType->isScalarType() && |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4134 | !DestType->isAnyComplexType() && InitList->getNumInits() > 1) { |
| 4135 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 4136 | return; |
| 4137 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4138 | if (DestType->isReferenceType()) { |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4139 | TryReferenceListInitialization(S, Entity, Kind, InitList, Sequence, |
| 4140 | TreatUnavailableAsInvalid); |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4141 | return; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4142 | } |
Sebastian Redl | 4f28b58 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 4143 | |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4144 | if (DestType->isRecordType() && |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4145 | !S.isCompleteType(InitList->getBeginLoc(), DestType)) { |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4146 | Sequence.setIncompleteTypeFailure(DestType); |
| 4147 | return; |
| 4148 | } |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4149 | |
Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 4150 | // C++11 [dcl.init.list]p3, per DR1467: |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4151 | // - If T is a class type and the initializer list has a single element of |
| 4152 | // type cv U, where U is T or a class derived from T, the object is |
| 4153 | // initialized from that element (by copy-initialization for |
| 4154 | // copy-list-initialization, or by direct-initialization for |
| 4155 | // direct-list-initialization). |
| 4156 | // - Otherwise, if T is a character array and the initializer list has a |
| 4157 | // single element that is an appropriately-typed string literal |
| 4158 | // (8.5.2 [dcl.init.string]), initialization is performed as described |
| 4159 | // in that section. |
Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 4160 | // - Otherwise, if T is an aggregate, [...] (continue below). |
| 4161 | if (S.getLangOpts().CPlusPlus11 && InitList->getNumInits() == 1) { |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4162 | if (DestType->isRecordType()) { |
| 4163 | QualType InitType = InitList->getInit(0)->getType(); |
| 4164 | if (S.Context.hasSameUnqualifiedType(InitType, DestType) || |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4165 | S.IsDerivedFrom(InitList->getBeginLoc(), InitType, DestType)) { |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 4166 | Expr *InitListAsExpr = InitList; |
| 4167 | TryConstructorInitialization(S, Entity, Kind, InitListAsExpr, DestType, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 4168 | DestType, Sequence, |
| 4169 | /*InitListSyntax*/false, |
| 4170 | /*IsInitListCopy*/true); |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4171 | return; |
| 4172 | } |
| 4173 | } |
| 4174 | if (const ArrayType *DestAT = S.Context.getAsArrayType(DestType)) { |
| 4175 | Expr *SubInit[1] = {InitList->getInit(0)}; |
| 4176 | if (!isa<VariableArrayType>(DestAT) && |
| 4177 | IsStringInit(SubInit[0], DestAT, S.Context) == SIF_None) { |
| 4178 | InitializationKind SubKind = |
| 4179 | Kind.getKind() == InitializationKind::IK_DirectList |
| 4180 | ? InitializationKind::CreateDirect(Kind.getLocation(), |
| 4181 | InitList->getLBraceLoc(), |
| 4182 | InitList->getRBraceLoc()) |
| 4183 | : Kind; |
| 4184 | Sequence.InitializeFrom(S, Entity, SubKind, SubInit, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4185 | /*TopLevelOfInitList*/ true, |
| 4186 | TreatUnavailableAsInvalid); |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4187 | |
| 4188 | // TryStringLiteralInitialization() (in InitializeFrom()) will fail if |
| 4189 | // the element is not an appropriately-typed string literal, in which |
| 4190 | // case we should proceed as in C++11 (below). |
| 4191 | if (Sequence) { |
| 4192 | Sequence.RewrapReferenceInitList(Entity.getType(), InitList); |
| 4193 | return; |
| 4194 | } |
| 4195 | } |
Sebastian Redl | 4f28b58 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 4196 | } |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4197 | } |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4198 | |
| 4199 | // C++11 [dcl.init.list]p3: |
| 4200 | // - If T is an aggregate, aggregate initialization is performed. |
Faisal Vali | 30622bb | 2015-12-07 02:37:44 +0000 | [diff] [blame] | 4201 | if ((DestType->isRecordType() && !DestType->isAggregateType()) || |
| 4202 | (S.getLangOpts().CPlusPlus11 && |
| 4203 | S.isStdInitializerList(DestType, nullptr))) { |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4204 | if (S.getLangOpts().CPlusPlus11) { |
| 4205 | // - Otherwise, if the initializer list has no elements and T is a |
| 4206 | // class type with a default constructor, the object is |
| 4207 | // value-initialized. |
| 4208 | if (InitList->getNumInits() == 0) { |
| 4209 | CXXRecordDecl *RD = DestType->getAsCXXRecordDecl(); |
| 4210 | if (RD->hasDefaultConstructor()) { |
| 4211 | TryValueInitialization(S, Entity, Kind, Sequence, InitList); |
| 4212 | return; |
| 4213 | } |
| 4214 | } |
| 4215 | |
| 4216 | // - Otherwise, if T is a specialization of std::initializer_list<E>, |
| 4217 | // an initializer_list object constructed [...] |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4218 | if (TryInitializerListConstruction(S, InitList, DestType, Sequence, |
| 4219 | TreatUnavailableAsInvalid)) |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4220 | return; |
| 4221 | |
| 4222 | // - Otherwise, if T is a class type, constructors are considered. |
| 4223 | Expr *InitListAsExpr = InitList; |
| 4224 | TryConstructorInitialization(S, Entity, Kind, InitListAsExpr, DestType, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 4225 | DestType, Sequence, /*InitListSyntax*/true); |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4226 | } else |
| 4227 | Sequence.SetFailed(InitializationSequence::FK_InitListBadDestinationType); |
| 4228 | return; |
| 4229 | } |
| 4230 | |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 4231 | if (S.getLangOpts().CPlusPlus && !DestType->isAggregateType() && |
Richard Smith | ed63886 | 2016-03-28 06:08:37 +0000 | [diff] [blame] | 4232 | InitList->getNumInits() == 1) { |
| 4233 | Expr *E = InitList->getInit(0); |
| 4234 | |
| 4235 | // - Otherwise, if T is an enumeration with a fixed underlying type, |
| 4236 | // the initializer-list has a single element v, and the initialization |
| 4237 | // is direct-list-initialization, the object is initialized with the |
| 4238 | // value T(v); if a narrowing conversion is required to convert v to |
| 4239 | // the underlying type of T, the program is ill-formed. |
| 4240 | auto *ET = DestType->getAs<EnumType>(); |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 4241 | if (S.getLangOpts().CPlusPlus17 && |
Richard Smith | ed63886 | 2016-03-28 06:08:37 +0000 | [diff] [blame] | 4242 | Kind.getKind() == InitializationKind::IK_DirectList && |
| 4243 | ET && ET->getDecl()->isFixed() && |
| 4244 | !S.Context.hasSameUnqualifiedType(E->getType(), DestType) && |
| 4245 | (E->getType()->isIntegralOrEnumerationType() || |
| 4246 | E->getType()->isFloatingType())) { |
| 4247 | // There are two ways that T(v) can work when T is an enumeration type. |
| 4248 | // If there is either an implicit conversion sequence from v to T or |
| 4249 | // a conversion function that can convert from v to T, then we use that. |
| 4250 | // Otherwise, if v is of integral, enumeration, or floating-point type, |
| 4251 | // it is converted to the enumeration type via its underlying type. |
| 4252 | // There is no overlap possible between these two cases (except when the |
| 4253 | // source value is already of the destination type), and the first |
| 4254 | // case is handled by the general case for single-element lists below. |
| 4255 | ImplicitConversionSequence ICS; |
| 4256 | ICS.setStandard(); |
| 4257 | ICS.Standard.setAsIdentityConversion(); |
Vedant Kumar | f4217f8 | 2017-02-16 01:20:00 +0000 | [diff] [blame] | 4258 | if (!E->isRValue()) |
| 4259 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
Richard Smith | ed63886 | 2016-03-28 06:08:37 +0000 | [diff] [blame] | 4260 | // If E is of a floating-point type, then the conversion is ill-formed |
| 4261 | // due to narrowing, but go through the motions in order to produce the |
| 4262 | // right diagnostic. |
| 4263 | ICS.Standard.Second = E->getType()->isFloatingType() |
| 4264 | ? ICK_Floating_Integral |
| 4265 | : ICK_Integral_Conversion; |
| 4266 | ICS.Standard.setFromType(E->getType()); |
| 4267 | ICS.Standard.setToType(0, E->getType()); |
| 4268 | ICS.Standard.setToType(1, DestType); |
| 4269 | ICS.Standard.setToType(2, DestType); |
| 4270 | Sequence.AddConversionSequenceStep(ICS, ICS.Standard.getToType(2), |
| 4271 | /*TopLevelOfInitList*/true); |
| 4272 | Sequence.RewrapReferenceInitList(Entity.getType(), InitList); |
| 4273 | return; |
| 4274 | } |
| 4275 | |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 4276 | // - Otherwise, if the initializer list has a single element of type E |
| 4277 | // [...references are handled above...], the object or reference is |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4278 | // initialized from that element (by copy-initialization for |
| 4279 | // copy-list-initialization, or by direct-initialization for |
| 4280 | // direct-list-initialization); if a narrowing conversion is required |
| 4281 | // to convert the element to T, the program is ill-formed. |
| 4282 | // |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 4283 | // Per core-24034, this is direct-initialization if we were performing |
| 4284 | // direct-list-initialization and copy-initialization otherwise. |
| 4285 | // We can't use InitListChecker for this, because it always performs |
| 4286 | // copy-initialization. This only matters if we might use an 'explicit' |
| 4287 | // conversion operator, so we only need to handle the cases where the source |
| 4288 | // is of record type. |
Richard Smith | ed63886 | 2016-03-28 06:08:37 +0000 | [diff] [blame] | 4289 | if (InitList->getInit(0)->getType()->isRecordType()) { |
| 4290 | InitializationKind SubKind = |
| 4291 | Kind.getKind() == InitializationKind::IK_DirectList |
| 4292 | ? InitializationKind::CreateDirect(Kind.getLocation(), |
| 4293 | InitList->getLBraceLoc(), |
| 4294 | InitList->getRBraceLoc()) |
| 4295 | : Kind; |
| 4296 | Expr *SubInit[1] = { InitList->getInit(0) }; |
| 4297 | Sequence.InitializeFrom(S, Entity, SubKind, SubInit, |
| 4298 | /*TopLevelOfInitList*/true, |
| 4299 | TreatUnavailableAsInvalid); |
| 4300 | if (Sequence) |
| 4301 | Sequence.RewrapReferenceInitList(Entity.getType(), InitList); |
| 4302 | return; |
| 4303 | } |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 4304 | } |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4305 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4306 | InitListChecker CheckInitList(S, Entity, InitList, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 4307 | DestType, /*VerifyOnly=*/true, TreatUnavailableAsInvalid); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4308 | if (CheckInitList.HadError()) { |
| 4309 | Sequence.SetFailed(InitializationSequence::FK_ListInitializationFailed); |
| 4310 | return; |
| 4311 | } |
| 4312 | |
| 4313 | // Add the list initialization step with the built init list. |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4314 | Sequence.AddListInitializationStep(DestType); |
| 4315 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4316 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4317 | /// Try a reference initialization that involves calling a conversion |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4318 | /// function. |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4319 | static OverloadingResult TryRefInitWithConversionFunction( |
| 4320 | Sema &S, const InitializedEntity &Entity, const InitializationKind &Kind, |
| 4321 | Expr *Initializer, bool AllowRValues, bool IsLValueRef, |
| 4322 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4323 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4324 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 4325 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 4326 | QualType cv2T2 = Initializer->getType(); |
| 4327 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 4328 | |
| 4329 | bool DerivedToBase; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4330 | bool ObjCConversion; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4331 | bool ObjCLifetimeConversion; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4332 | assert(!S.CompareReferenceRelationship(Initializer->getBeginLoc(), T1, T2, |
| 4333 | DerivedToBase, ObjCConversion, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4334 | ObjCLifetimeConversion) && |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4335 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 8abbc65 | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 4336 | (void)DerivedToBase; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4337 | (void)ObjCConversion; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4338 | (void)ObjCLifetimeConversion; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4339 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4340 | // Build the candidate set directly in the initialization sequence |
| 4341 | // structure, so that it will persist if we fail. |
| 4342 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 4343 | CandidateSet.clear(OverloadCandidateSet::CSK_InitByUserDefinedConversion); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4344 | |
Richard Smith | b368ea8 | 2018-07-02 23:25:22 +0000 | [diff] [blame] | 4345 | // Determine whether we are allowed to call explicit conversion operators. |
| 4346 | // Note that none of [over.match.copy], [over.match.conv], nor |
| 4347 | // [over.match.ref] permit an explicit constructor to be chosen when |
| 4348 | // initializing a reference, not even for direct-initialization. |
| 4349 | bool AllowExplicitCtors = false; |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4350 | bool AllowExplicitConvs = Kind.allowExplicitConversionFunctionsInRefBinding(); |
| 4351 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4352 | const RecordType *T1RecordType = nullptr; |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 4353 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4354 | S.isCompleteType(Kind.getLocation(), T1)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4355 | // The type we're converting to is a class type. Enumerate its constructors |
| 4356 | // to see if there is a suitable conversion. |
| 4357 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 4358 | |
Richard Smith | 40c7806 | 2015-02-21 02:31:57 +0000 | [diff] [blame] | 4359 | for (NamedDecl *D : S.LookupConstructors(T1RecordDecl)) { |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4360 | auto Info = getConstructorInfo(D); |
| 4361 | if (!Info.Constructor) |
| 4362 | continue; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4363 | |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4364 | if (!Info.Constructor->isInvalidDecl() && |
Richard Smith | b368ea8 | 2018-07-02 23:25:22 +0000 | [diff] [blame] | 4365 | Info.Constructor->isConvertingConstructor(AllowExplicitCtors)) { |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4366 | if (Info.ConstructorTmpl) |
| 4367 | S.AddTemplateOverloadCandidate(Info.ConstructorTmpl, Info.FoundDecl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4368 | /*ExplicitArgs*/ nullptr, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4369 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | dfbdfbb | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 4370 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4371 | else |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4372 | S.AddOverloadCandidate(Info.Constructor, Info.FoundDecl, |
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 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4376 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4377 | } |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 4378 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 4379 | return OR_No_Viable_Function; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4380 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4381 | const RecordType *T2RecordType = nullptr; |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 4382 | if ((T2RecordType = T2->getAs<RecordType>()) && |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4383 | S.isCompleteType(Kind.getLocation(), T2)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4384 | // The type we're converting from is a class type, enumerate its conversion |
| 4385 | // functions. |
| 4386 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 4387 | |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 4388 | const auto &Conversions = T2RecordDecl->getVisibleConversionFunctions(); |
| 4389 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4390 | NamedDecl *D = *I; |
| 4391 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 4392 | if (isa<UsingShadowDecl>(D)) |
| 4393 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4394 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4395 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 4396 | CXXConversionDecl *Conv; |
| 4397 | if (ConvTemplate) |
| 4398 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 4399 | else |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4400 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4401 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4402 | // If the conversion function doesn't return a reference type, |
| 4403 | // it can't be considered for this conversion unless we're allowed to |
| 4404 | // consider rvalues. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4405 | // FIXME: Do we need to make sure that we only consider conversion |
| 4406 | // candidates with reference-compatible results? That might be needed to |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4407 | // break recursion. |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4408 | if ((AllowExplicitConvs || !Conv->isExplicit()) && |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4409 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 4410 | if (ConvTemplate) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4411 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4412 | ActingDC, Initializer, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 4413 | DestType, CandidateSet, |
| 4414 | /*AllowObjCConversionOnExplicit=*/ |
| 4415 | false); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4416 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4417 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 4418 | Initializer, DestType, CandidateSet, |
| 4419 | /*AllowObjCConversionOnExplicit=*/false); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4420 | } |
| 4421 | } |
| 4422 | } |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 4423 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 4424 | return OR_No_Viable_Function; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4425 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4426 | SourceLocation DeclLoc = Initializer->getBeginLoc(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4427 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4428 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4429 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4430 | if (OverloadingResult Result |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 4431 | = CandidateSet.BestViableFunction(S, DeclLoc, Best)) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4432 | return Result; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 4433 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4434 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 4435 | // This is the overload that will be used for this initialization step if we |
| 4436 | // use this initialization. Mark it as referenced. |
| 4437 | Function->setReferenced(); |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4438 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4439 | // Compute the returned type and value kind of the conversion. |
| 4440 | QualType cv3T3; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4441 | if (isa<CXXConversionDecl>(Function)) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4442 | cv3T3 = Function->getReturnType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4443 | else |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4444 | cv3T3 = T1; |
| 4445 | |
| 4446 | ExprValueKind VK = VK_RValue; |
| 4447 | if (cv3T3->isLValueReferenceType()) |
| 4448 | VK = VK_LValue; |
| 4449 | else if (const auto *RRef = cv3T3->getAs<RValueReferenceType>()) |
| 4450 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
| 4451 | cv3T3 = cv3T3.getNonLValueExprType(S.Context); |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 4452 | |
| 4453 | // Add the user-defined conversion step. |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4454 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4455 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, cv3T3, |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4456 | HadMultipleCandidates); |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 4457 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4458 | // Determine whether we'll need to perform derived-to-base adjustments or |
| 4459 | // other conversions. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4460 | bool NewDerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4461 | bool NewObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4462 | bool NewObjCLifetimeConversion = false; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4463 | Sema::ReferenceCompareResult NewRefRelationship |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4464 | = S.CompareReferenceRelationship(DeclLoc, T1, cv3T3, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4465 | NewDerivedToBase, NewObjCConversion, |
| 4466 | NewObjCLifetimeConversion); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4467 | |
| 4468 | // Add the final conversion sequence, if necessary. |
Douglas Gregor | 1ce52ca | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 4469 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4470 | assert(!isa<CXXConstructorDecl>(Function) && |
| 4471 | "should not have conversion after constructor"); |
| 4472 | |
Douglas Gregor | 1ce52ca | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 4473 | ImplicitConversionSequence ICS; |
| 4474 | ICS.setStandard(); |
| 4475 | ICS.Standard = Best->FinalConversion; |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4476 | Sequence.AddConversionSequenceStep(ICS, ICS.Standard.getToType(2)); |
| 4477 | |
| 4478 | // Every implicit conversion results in a prvalue, except for a glvalue |
| 4479 | // derived-to-base conversion, which we handle below. |
| 4480 | cv3T3 = ICS.Standard.getToType(2); |
| 4481 | VK = VK_RValue; |
| 4482 | } |
| 4483 | |
| 4484 | // If the converted initializer is a prvalue, its type T4 is adjusted to |
| 4485 | // type "cv1 T4" and the temporary materialization conversion is applied. |
| 4486 | // |
| 4487 | // We adjust the cv-qualifications to match the reference regardless of |
| 4488 | // whether we have a prvalue so that the AST records the change. In this |
| 4489 | // case, T4 is "cv3 T3". |
| 4490 | QualType cv1T4 = S.Context.getQualifiedType(cv3T3, cv1T1.getQualifiers()); |
| 4491 | if (cv1T4.getQualifiers() != cv3T3.getQualifiers()) |
| 4492 | Sequence.AddQualificationConversionStep(cv1T4, VK); |
| 4493 | Sequence.AddReferenceBindingStep(cv1T4, VK == VK_RValue); |
| 4494 | VK = IsLValueRef ? VK_LValue : VK_XValue; |
| 4495 | |
| 4496 | if (NewDerivedToBase) |
| 4497 | Sequence.AddDerivedToBaseCastStep(cv1T1, VK); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4498 | else if (NewObjCConversion) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4499 | Sequence.AddObjCObjectConversionStep(cv1T1); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4500 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4501 | return OR_Success; |
| 4502 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4503 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4504 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 4505 | const InitializedEntity &Entity, |
| 4506 | Expr *CurInitExpr); |
| 4507 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4508 | /// Attempt reference initialization (C++0x [dcl.init.ref]) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4509 | static void TryReferenceInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4510 | const InitializedEntity &Entity, |
| 4511 | const InitializationKind &Kind, |
| 4512 | Expr *Initializer, |
| 4513 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4514 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4515 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 4516 | Qualifiers T1Quals; |
| 4517 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4518 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 4519 | Qualifiers T2Quals; |
| 4520 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4521 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4522 | // If the initializer is the address of an overloaded function, try |
| 4523 | // to resolve the overloaded function. If all goes well, T2 is the |
| 4524 | // type of the resulting function. |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4525 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 4526 | T1, Sequence)) |
| 4527 | return; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4528 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4529 | // Delegate everything else to a subfunction. |
| 4530 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 4531 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 4532 | } |
| 4533 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4534 | /// Determine whether an expression is a non-referenceable glvalue (one to |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 4535 | /// which a reference can never bind). Attempting to bind a reference to |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4536 | /// such a glvalue will always create a temporary. |
| 4537 | static bool isNonReferenceableGLValue(Expr *E) { |
| 4538 | return E->refersToBitField() || E->refersToVectorElement(); |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 4539 | } |
| 4540 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4541 | /// Reference initialization without resolving overloaded functions. |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4542 | static void TryReferenceInitializationCore(Sema &S, |
| 4543 | const InitializedEntity &Entity, |
| 4544 | const InitializationKind &Kind, |
| 4545 | Expr *Initializer, |
| 4546 | QualType cv1T1, QualType T1, |
| 4547 | Qualifiers T1Quals, |
| 4548 | QualType cv2T2, QualType T2, |
| 4549 | Qualifiers T2Quals, |
| 4550 | InitializationSequence &Sequence) { |
| 4551 | QualType DestType = Entity.getType(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4552 | SourceLocation DeclLoc = Initializer->getBeginLoc(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4553 | // Compute some basic properties of the types and the initializer. |
| 4554 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 4555 | bool isRValueRef = !isLValueRef; |
| 4556 | bool DerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4557 | bool ObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4558 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4559 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4560 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4561 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4562 | ObjCConversion, ObjCLifetimeConversion); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4563 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4564 | // C++0x [dcl.init.ref]p5: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4565 | // 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] | 4566 | // "cv2 T2" as follows: |
| 4567 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4568 | // - If the reference is an lvalue reference and the initializer |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4569 | // expression |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4570 | // Note the analogous bullet points for rvalue refs to functions. Because |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4571 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 4572 | // like lvalue refs. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4573 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4574 | bool T1Function = T1->isFunctionType(); |
| 4575 | if (isLValueRef || T1Function) { |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4576 | if (InitCategory.isLValue() && !isNonReferenceableGLValue(Initializer) && |
Richard Smith | ce76629 | 2016-10-21 23:01:55 +0000 | [diff] [blame] | 4577 | (RefRelationship == Sema::Ref_Compatible || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4578 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4579 | RefRelationship == Sema::Ref_Related))) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4580 | // - 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] | 4581 | // reference-compatible with "cv2 T2," or |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4582 | if (T1Quals != T2Quals) |
| 4583 | // Convert to cv1 T2. This should only add qualifiers unless this is a |
| 4584 | // c-style cast. The removal of qualifiers in that case notionally |
| 4585 | // happens after the reference binding, but that doesn't matter. |
| 4586 | Sequence.AddQualificationConversionStep( |
| 4587 | S.Context.getQualifiedType(T2, T1Quals), |
| 4588 | Initializer->getValueKind()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4589 | if (DerivedToBase) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4590 | Sequence.AddDerivedToBaseCastStep(cv1T1, VK_LValue); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4591 | else if (ObjCConversion) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4592 | Sequence.AddObjCObjectConversionStep(cv1T1); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4593 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4594 | // We only create a temporary here when binding a reference to a |
| 4595 | // bit-field or vector element. Those cases are't supposed to be |
| 4596 | // handled by this bullet, but the outcome is the same either way. |
| 4597 | Sequence.AddReferenceBindingStep(cv1T1, false); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4598 | return; |
| 4599 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4600 | |
| 4601 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 4602 | // reference-related to T2, and can be implicitly converted to an |
| 4603 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 4604 | // with "cv3 T3" (this conversion is selected by enumerating the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4605 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 4606 | // one through overload resolution (13.3)), |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4607 | // 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] | 4608 | // an rvalue. DR1287 removed the "implicitly" here. |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4609 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 4610 | (isLValueRef || InitCategory.isRValue())) { |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4611 | ConvOvlResult = TryRefInitWithConversionFunction( |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4612 | S, Entity, Kind, Initializer, /*AllowRValues*/ isRValueRef, |
| 4613 | /*IsLValueRef*/ isLValueRef, Sequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4614 | if (ConvOvlResult == OR_Success) |
| 4615 | return; |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4616 | if (ConvOvlResult != OR_No_Viable_Function) |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4617 | Sequence.SetOverloadFailure( |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4618 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4619 | ConvOvlResult); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4620 | } |
| 4621 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4622 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4623 | // - Otherwise, the reference shall be an lvalue reference to a |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4624 | // 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] | 4625 | // shall be an rvalue reference. |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 4626 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) { |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 4627 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 4628 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 4629 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4630 | Sequence.SetOverloadFailure( |
| 4631 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4632 | ConvOvlResult); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4633 | else if (!InitCategory.isLValue()) |
| 4634 | Sequence.SetFailed( |
| 4635 | InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
| 4636 | else { |
| 4637 | InitializationSequence::FailureKind FK; |
| 4638 | switch (RefRelationship) { |
| 4639 | case Sema::Ref_Compatible: |
| 4640 | if (Initializer->refersToBitField()) |
| 4641 | FK = InitializationSequence:: |
| 4642 | FK_NonConstLValueReferenceBindingToBitfield; |
| 4643 | else if (Initializer->refersToVectorElement()) |
| 4644 | FK = InitializationSequence:: |
| 4645 | FK_NonConstLValueReferenceBindingToVectorElement; |
| 4646 | else |
| 4647 | llvm_unreachable("unexpected kind of compatible initializer"); |
| 4648 | break; |
| 4649 | case Sema::Ref_Related: |
| 4650 | FK = InitializationSequence::FK_ReferenceInitDropsQualifiers; |
| 4651 | break; |
| 4652 | case Sema::Ref_Incompatible: |
| 4653 | FK = InitializationSequence:: |
| 4654 | FK_NonConstLValueReferenceBindingToUnrelated; |
| 4655 | break; |
| 4656 | } |
| 4657 | Sequence.SetFailed(FK); |
| 4658 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4659 | return; |
| 4660 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4661 | |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4662 | // - If the initializer expression |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4663 | // - is an |
| 4664 | // [<=14] xvalue (but not a bit-field), class prvalue, array prvalue, or |
| 4665 | // [1z] rvalue (but not a bit-field) or |
| 4666 | // function lvalue and "cv1 T1" is reference-compatible with "cv2 T2" |
| 4667 | // |
| 4668 | // Note: functions are handled above and below rather than here... |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4669 | if (!T1Function && |
Richard Smith | ce76629 | 2016-10-21 23:01:55 +0000 | [diff] [blame] | 4670 | (RefRelationship == Sema::Ref_Compatible || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4671 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4672 | RefRelationship == Sema::Ref_Related)) && |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4673 | ((InitCategory.isXValue() && !isNonReferenceableGLValue(Initializer)) || |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 4674 | (InitCategory.isPRValue() && |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 4675 | (S.getLangOpts().CPlusPlus17 || T2->isRecordType() || |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 4676 | T2->isArrayType())))) { |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4677 | ExprValueKind ValueKind = InitCategory.isXValue() ? VK_XValue : VK_RValue; |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4678 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4679 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 4680 | // compiler the freedom to perform a copy here or bind to the |
| 4681 | // object, while C++0x requires that we bind directly to the |
| 4682 | // object. Hence, we always bind to the object without making an |
| 4683 | // extra copy. However, in C++03 requires that we check for the |
| 4684 | // presence of a suitable copy constructor: |
| 4685 | // |
| 4686 | // The constructor that would be used to make the copy shall |
| 4687 | // be callable whether or not the copy is actually done. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4688 | if (!S.getLangOpts().CPlusPlus11 && !S.getLangOpts().MicrosoftExt) |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4689 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4690 | else if (S.getLangOpts().CPlusPlus11) |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4691 | CheckCXX98CompatAccessibleCopy(S, Entity, Initializer); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4692 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4693 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4694 | // C++1z [dcl.init.ref]/5.2.1.2: |
| 4695 | // If the converted initializer is a prvalue, its type T4 is adjusted |
| 4696 | // to type "cv1 T4" and the temporary materialization conversion is |
| 4697 | // applied. |
Anastasia Stulova | d1986d1 | 2019-01-14 11:44:22 +0000 | [diff] [blame] | 4698 | // Postpone address space conversions to after the temporary materialization |
| 4699 | // conversion to allow creating temporaries in the alloca address space. |
Anastasia Stulova | e368e4d | 2019-02-05 11:32:58 +0000 | [diff] [blame] | 4700 | auto T1QualsIgnoreAS = T1Quals; |
| 4701 | auto T2QualsIgnoreAS = T2Quals; |
| 4702 | if (T1Quals.getAddressSpace() != T2Quals.getAddressSpace()) { |
| 4703 | T1QualsIgnoreAS.removeAddressSpace(); |
| 4704 | T2QualsIgnoreAS.removeAddressSpace(); |
| 4705 | } |
| 4706 | QualType cv1T4 = S.Context.getQualifiedType(cv2T2, T1QualsIgnoreAS); |
| 4707 | if (T1QualsIgnoreAS != T2QualsIgnoreAS) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4708 | Sequence.AddQualificationConversionStep(cv1T4, ValueKind); |
| 4709 | Sequence.AddReferenceBindingStep(cv1T4, ValueKind == VK_RValue); |
| 4710 | ValueKind = isLValueRef ? VK_LValue : VK_XValue; |
Anastasia Stulova | e368e4d | 2019-02-05 11:32:58 +0000 | [diff] [blame] | 4711 | // Add addr space conversion if required. |
| 4712 | if (T1Quals.getAddressSpace() != T2Quals.getAddressSpace()) { |
| 4713 | auto T4Quals = cv1T4.getQualifiers(); |
| 4714 | T4Quals.addAddressSpace(T1Quals.getAddressSpace()); |
| 4715 | QualType cv1T4WithAS = S.Context.getQualifiedType(T2, T4Quals); |
| 4716 | Sequence.AddQualificationConversionStep(cv1T4WithAS, ValueKind); |
Anastasia Stulova | d1986d1 | 2019-01-14 11:44:22 +0000 | [diff] [blame] | 4717 | } |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4718 | |
| 4719 | // In any case, the reference is bound to the resulting glvalue (or to |
| 4720 | // an appropriate base class subobject). |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4721 | if (DerivedToBase) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4722 | Sequence.AddDerivedToBaseCastStep(cv1T1, ValueKind); |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4723 | else if (ObjCConversion) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4724 | Sequence.AddObjCObjectConversionStep(cv1T1); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4725 | return; |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4726 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4727 | |
| 4728 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 4729 | // reference-related to T2, and can be implicitly converted to an |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4730 | // xvalue, class prvalue, or function lvalue of type "cv3 T3", |
| 4731 | // where "cv1 T1" is reference-compatible with "cv3 T3", |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4732 | // |
| 4733 | // DR1287 removes the "implicitly" here. |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 4734 | if (T2->isRecordType()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4735 | if (RefRelationship == Sema::Ref_Incompatible) { |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4736 | ConvOvlResult = TryRefInitWithConversionFunction( |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4737 | S, Entity, Kind, Initializer, /*AllowRValues*/ true, |
| 4738 | /*IsLValueRef*/ isLValueRef, Sequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4739 | if (ConvOvlResult) |
| 4740 | Sequence.SetOverloadFailure( |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4741 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4742 | ConvOvlResult); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4743 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4744 | return; |
| 4745 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4746 | |
Richard Smith | ce76629 | 2016-10-21 23:01:55 +0000 | [diff] [blame] | 4747 | if (RefRelationship == Sema::Ref_Compatible && |
Douglas Gregor | 6fa6ab0 | 2013-03-26 23:59:23 +0000 | [diff] [blame] | 4748 | isRValueRef && InitCategory.isLValue()) { |
| 4749 | Sequence.SetFailed( |
| 4750 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 4751 | return; |
| 4752 | } |
| 4753 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4754 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 4755 | return; |
| 4756 | } |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4757 | |
| 4758 | // - Otherwise, a temporary of type "cv1 T1" is created and initialized |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4759 | // from the initializer expression using the rules for a non-reference |
Richard Smith | 2eabf78 | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 4760 | // copy-initialization (8.5). The reference is then bound to the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4761 | // temporary. [...] |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 4762 | |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 4763 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 4764 | |
Richard Smith | 2eabf78 | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 4765 | // FIXME: Why do we use an implicit conversion here rather than trying |
| 4766 | // copy-initialization? |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4767 | ImplicitConversionSequence ICS |
| 4768 | = S.TryImplicitConversion(Initializer, TempEntity.getType(), |
Richard Smith | 2eabf78 | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 4769 | /*SuppressUserConversions=*/false, |
| 4770 | /*AllowExplicit=*/false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4771 | /*FIXME:InOverloadResolution=*/false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4772 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 4773 | /*AllowObjCWritebackConversion=*/false); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4774 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4775 | if (ICS.isBad()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4776 | // FIXME: Use the conversion function set stored in ICS to turn |
| 4777 | // this into an overloading ambiguity diagnostic. However, we need |
| 4778 | // to keep that set as an OverloadCandidateSet rather than as some |
| 4779 | // other kind of set. |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4780 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 4781 | Sequence.SetOverloadFailure( |
| 4782 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4783 | ConvOvlResult); |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 4784 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 4785 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4786 | else |
| 4787 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4788 | return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4789 | } else { |
| 4790 | Sequence.AddConversionSequenceStep(ICS, TempEntity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4791 | } |
| 4792 | |
| 4793 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 4794 | // same cv-qualification as, or greater cv-qualification |
| 4795 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 4796 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 4797 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4798 | if (RefRelationship == Sema::Ref_Related && |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 4799 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4800 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 4801 | return; |
| 4802 | } |
| 4803 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4804 | // [...] 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] | 4805 | // reference, the initializer expression shall not be an lvalue. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4806 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 4807 | InitCategory.isLValue()) { |
| 4808 | Sequence.SetFailed( |
| 4809 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 4810 | return; |
| 4811 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4812 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4813 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4814 | } |
| 4815 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4816 | /// Attempt character array initialization from a string literal |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4817 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 4818 | static void TryStringLiteralInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4819 | const InitializedEntity &Entity, |
| 4820 | const InitializationKind &Kind, |
| 4821 | Expr *Initializer, |
| 4822 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4823 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4824 | } |
| 4825 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4826 | /// Attempt value initialization (C++ [dcl.init]p7). |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4827 | static void TryValueInitialization(Sema &S, |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4828 | const InitializedEntity &Entity, |
| 4829 | const InitializationKind &Kind, |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4830 | InitializationSequence &Sequence, |
| 4831 | InitListExpr *InitList) { |
| 4832 | assert((!InitList || InitList->getNumInits() == 0) && |
| 4833 | "Shouldn't use value-init for non-empty init lists"); |
| 4834 | |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4835 | // C++98 [dcl.init]p5, C++11 [dcl.init]p7: |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4836 | // |
| 4837 | // To value-initialize an object of type T means: |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4838 | QualType T = Entity.getType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4839 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4840 | // -- if T is an array type, then each element is value-initialized; |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4841 | T = S.Context.getBaseElementType(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4842 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4843 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 4844 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4845 | bool NeedZeroInitialization = true; |
Richard Smith | 505ef81 | 2016-12-21 01:57:02 +0000 | [diff] [blame] | 4846 | // C++98: |
| 4847 | // -- if T is a class type (clause 9) with a user-declared constructor |
| 4848 | // (12.1), then the default constructor for T is called (and the |
| 4849 | // initialization is ill-formed if T has no accessible default |
| 4850 | // constructor); |
| 4851 | // C++11: |
| 4852 | // -- if T is a class type (clause 9) with either no default constructor |
| 4853 | // (12.1 [class.ctor]) or a default constructor that is user-provided |
| 4854 | // or deleted, then the object is default-initialized; |
| 4855 | // |
| 4856 | // Note that the C++11 rule is the same as the C++98 rule if there are no |
| 4857 | // defaulted or deleted constructors, so we just use it unconditionally. |
| 4858 | CXXConstructorDecl *CD = S.LookupDefaultConstructor(ClassDecl); |
| 4859 | if (!CD || !CD->getCanonicalDecl()->isDefaulted() || CD->isDeleted()) |
| 4860 | NeedZeroInitialization = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4861 | |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4862 | // -- if T is a (possibly cv-qualified) non-union class type without a |
| 4863 | // user-provided or deleted default constructor, then the object is |
| 4864 | // zero-initialized and, if T has a non-trivial default constructor, |
| 4865 | // default-initialized; |
Richard Smith | fb26652 | 2012-10-18 00:44:17 +0000 | [diff] [blame] | 4866 | // The 'non-union' here was removed by DR1502. The 'non-trivial default |
| 4867 | // constructor' part was removed by DR1507. |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4868 | if (NeedZeroInitialization) |
| 4869 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 4870 | |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 4871 | // C++03: |
| 4872 | // -- if T is a non-union class type without a user-declared constructor, |
| 4873 | // then every non-static data member and base class component of T is |
| 4874 | // value-initialized; |
| 4875 | // [...] A program that calls for [...] value-initialization of an |
| 4876 | // entity of reference type is ill-formed. |
| 4877 | // |
| 4878 | // C++11 doesn't need this handling, because value-initialization does not |
| 4879 | // occur recursively there, and the implicit default constructor is |
| 4880 | // defined as deleted in the problematic cases. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4881 | if (!S.getLangOpts().CPlusPlus11 && |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 4882 | ClassDecl->hasUninitializedReferenceMember()) { |
| 4883 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForReference); |
| 4884 | return; |
| 4885 | } |
| 4886 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4887 | // If this is list-value-initialization, pass the empty init list on when |
| 4888 | // building the constructor call. This affects the semantics of a few |
| 4889 | // things (such as whether an explicit default constructor can be called). |
| 4890 | Expr *InitListAsExpr = InitList; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4891 | MultiExprArg Args(&InitListAsExpr, InitList ? 1 : 0); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4892 | bool InitListSyntax = InitList; |
| 4893 | |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 4894 | // FIXME: Instead of creating a CXXConstructExpr of array type here, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 4895 | // wrap a class-typed CXXConstructExpr in an ArrayInitLoopExpr. |
| 4896 | return TryConstructorInitialization( |
| 4897 | S, Entity, Kind, Args, T, Entity.getType(), Sequence, InitListSyntax); |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4898 | } |
| 4899 | } |
| 4900 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4901 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4902 | } |
| 4903 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4904 | /// Attempt default initialization (C++ [dcl.init]p6). |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4905 | static void TryDefaultInitialization(Sema &S, |
| 4906 | const InitializedEntity &Entity, |
| 4907 | const InitializationKind &Kind, |
| 4908 | InitializationSequence &Sequence) { |
| 4909 | assert(Kind.getKind() == InitializationKind::IK_Default); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4910 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4911 | // C++ [dcl.init]p6: |
| 4912 | // To default-initialize an object of type T means: |
| 4913 | // - if T is an array type, each element is default-initialized; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4914 | QualType DestType = S.Context.getBaseElementType(Entity.getType()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4915 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4916 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 4917 | // constructor for T is called (and the initialization is ill-formed if |
| 4918 | // T has no accessible default constructor); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4919 | if (DestType->isRecordType() && S.getLangOpts().CPlusPlus) { |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 4920 | TryConstructorInitialization(S, Entity, Kind, None, DestType, |
| 4921 | Entity.getType(), Sequence); |
Chandler Carruth | c926240 | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 4922 | return; |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4923 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4924 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4925 | // - otherwise, no initialization is performed. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4926 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4927 | // If a program calls for the default initialization of an object of |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4928 | // 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] | 4929 | // default constructor. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4930 | if (DestType.isConstQualified() && S.getLangOpts().CPlusPlus) { |
Nico Weber | 337d5aa | 2015-04-17 08:32:38 +0000 | [diff] [blame] | 4931 | if (!maybeRecoverWithZeroInitialization(S, Sequence, Entity)) |
| 4932 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4933 | return; |
| 4934 | } |
| 4935 | |
| 4936 | // If the destination type has a lifetime property, zero-initialize it. |
| 4937 | if (DestType.getQualifiers().hasObjCLifetime()) { |
| 4938 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 4939 | return; |
| 4940 | } |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4941 | } |
| 4942 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4943 | /// Attempt a user-defined conversion between two types (C++ [dcl.init]), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4944 | /// which enumerates all conversion functions and performs overload resolution |
| 4945 | /// to select the best. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4946 | static void TryUserDefinedConversion(Sema &S, |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 4947 | QualType DestType, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4948 | const InitializationKind &Kind, |
| 4949 | Expr *Initializer, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4950 | InitializationSequence &Sequence, |
| 4951 | bool TopLevelOfInitList) { |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4952 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 4953 | QualType SourceType = Initializer->getType(); |
| 4954 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 4955 | "Must have a class type to perform a user-defined conversion"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4956 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4957 | // Build the candidate set directly in the initialization sequence |
| 4958 | // structure, so that it will persist if we fail. |
| 4959 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 4960 | CandidateSet.clear(OverloadCandidateSet::CSK_InitByUserDefinedConversion); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4961 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4962 | // Determine whether we are allowed to call explicit constructors or |
| 4963 | // explicit conversion operators. |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 4964 | bool AllowExplicit = Kind.AllowExplicit(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4965 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4966 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 4967 | // The type we're converting to is a class type. Enumerate its constructors |
| 4968 | // to see if there is a suitable conversion. |
| 4969 | CXXRecordDecl *DestRecordDecl |
| 4970 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4971 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4972 | // Try to complete the type we're converting to. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4973 | if (S.isCompleteType(Kind.getLocation(), DestType)) { |
Richard Smith | 776e9c3 | 2017-02-01 03:28:59 +0000 | [diff] [blame] | 4974 | for (NamedDecl *D : S.LookupConstructors(DestRecordDecl)) { |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4975 | auto Info = getConstructorInfo(D); |
| 4976 | if (!Info.Constructor) |
| 4977 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4978 | |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4979 | if (!Info.Constructor->isInvalidDecl() && |
| 4980 | Info.Constructor->isConvertingConstructor(AllowExplicit)) { |
| 4981 | if (Info.ConstructorTmpl) |
| 4982 | S.AddTemplateOverloadCandidate(Info.ConstructorTmpl, Info.FoundDecl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4983 | /*ExplicitArgs*/ nullptr, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4984 | Initializer, CandidateSet, |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 4985 | /*SuppressUserConversions=*/true); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4986 | else |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4987 | S.AddOverloadCandidate(Info.Constructor, Info.FoundDecl, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4988 | Initializer, CandidateSet, |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 4989 | /*SuppressUserConversions=*/true); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4990 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4991 | } |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4992 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4993 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4994 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4995 | SourceLocation DeclLoc = Initializer->getBeginLoc(); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4996 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4997 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 4998 | // The type we're converting from is a class type, enumerate its conversion |
| 4999 | // functions. |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5000 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 5001 | // We can only enumerate the conversion functions for a complete type; if |
| 5002 | // the type isn't complete, simply skip this step. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 5003 | if (S.isCompleteType(DeclLoc, SourceType)) { |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 5004 | CXXRecordDecl *SourceRecordDecl |
| 5005 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5006 | |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 5007 | const auto &Conversions = |
| 5008 | SourceRecordDecl->getVisibleConversionFunctions(); |
| 5009 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 5010 | NamedDecl *D = *I; |
| 5011 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 5012 | if (isa<UsingShadowDecl>(D)) |
| 5013 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5014 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 5015 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 5016 | CXXConversionDecl *Conv; |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5017 | if (ConvTemplate) |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 5018 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5019 | else |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 5020 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5021 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 5022 | if (AllowExplicit || !Conv->isExplicit()) { |
| 5023 | if (ConvTemplate) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5024 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 5025 | ActingDC, Initializer, DestType, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 5026 | CandidateSet, AllowExplicit); |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 5027 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5028 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 5029 | Initializer, DestType, CandidateSet, |
| 5030 | AllowExplicit); |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 5031 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5032 | } |
| 5033 | } |
| 5034 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5035 | |
| 5036 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5037 | OverloadCandidateSet::iterator Best; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5038 | if (OverloadingResult Result |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 5039 | = CandidateSet.BestViableFunction(S, DeclLoc, Best)) { |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5040 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5041 | InitializationSequence::FK_UserConversionOverloadFailed, |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5042 | Result); |
| 5043 | return; |
| 5044 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5045 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5046 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 5047 | Function->setReferenced(); |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 5048 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5049 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5050 | if (isa<CXXConstructorDecl>(Function)) { |
| 5051 | // Add the user-defined conversion step. Any cv-qualification conversion is |
Richard Smith | b24f067 | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 5052 | // subsumed by the initialization. Per DR5, the created temporary is of the |
| 5053 | // cv-unqualified type of the destination. |
| 5054 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
| 5055 | DestType.getUnqualifiedType(), |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 5056 | HadMultipleCandidates); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5057 | |
| 5058 | // C++14 and before: |
| 5059 | // - if the function is a constructor, the call initializes a temporary |
| 5060 | // of the cv-unqualified version of the destination type. The [...] |
| 5061 | // temporary [...] is then used to direct-initialize, according to the |
| 5062 | // rules above, the object that is the destination of the |
| 5063 | // copy-initialization. |
| 5064 | // Note that this just performs a simple object copy from the temporary. |
| 5065 | // |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 5066 | // C++17: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5067 | // - if the function is a constructor, the call is a prvalue of the |
| 5068 | // cv-unqualified version of the destination type whose return object |
| 5069 | // is initialized by the constructor. The call is used to |
| 5070 | // direct-initialize, according to the rules above, the object that |
| 5071 | // is the destination of the copy-initialization. |
| 5072 | // Therefore we need to do nothing further. |
| 5073 | // |
| 5074 | // FIXME: Mark this copy as extraneous. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 5075 | if (!S.getLangOpts().CPlusPlus17) |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5076 | Sequence.AddFinalCopy(DestType); |
Richard Smith | 16d3150 | 2016-12-21 01:31:56 +0000 | [diff] [blame] | 5077 | else if (DestType.hasQualifiers()) |
| 5078 | Sequence.AddQualificationConversionStep(DestType, VK_RValue); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5079 | return; |
| 5080 | } |
| 5081 | |
| 5082 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 5083 | QualType ConvType = Function->getCallResultType(); |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 5084 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType, |
| 5085 | HadMultipleCandidates); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5086 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5087 | if (ConvType->getAs<RecordType>()) { |
| 5088 | // The call is used to direct-initialize [...] the object that is the |
| 5089 | // destination of the copy-initialization. |
| 5090 | // |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 5091 | // 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] | 5092 | // - If the initializer expression is a prvalue and the cv-unqualified |
| 5093 | // version of the source type is the same as the class of the |
| 5094 | // destination [... do not make an extra copy] |
| 5095 | // |
| 5096 | // FIXME: Mark this copy as extraneous. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 5097 | if (!S.getLangOpts().CPlusPlus17 || |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5098 | Function->getReturnType()->isReferenceType() || |
| 5099 | !S.Context.hasSameUnqualifiedType(ConvType, DestType)) |
| 5100 | Sequence.AddFinalCopy(DestType); |
Richard Smith | 16d3150 | 2016-12-21 01:31:56 +0000 | [diff] [blame] | 5101 | else if (!S.Context.hasSameType(ConvType, DestType)) |
| 5102 | Sequence.AddQualificationConversionStep(DestType, VK_RValue); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5103 | return; |
| 5104 | } |
| 5105 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5106 | // If the conversion following the call to the conversion function |
| 5107 | // is interesting, add it as a separate step. |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5108 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 5109 | Best->FinalConversion.Third) { |
| 5110 | ImplicitConversionSequence ICS; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5111 | ICS.setStandard(); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5112 | ICS.Standard = Best->FinalConversion; |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5113 | Sequence.AddConversionSequenceStep(ICS, DestType, TopLevelOfInitList); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5114 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5115 | } |
| 5116 | |
Richard Smith | f032001b | 2013-06-20 02:18:31 +0000 | [diff] [blame] | 5117 | /// An egregious hack for compatibility with libstdc++-4.2: in <tr1/hashtable>, |
| 5118 | /// a function with a pointer return type contains a 'return false;' statement. |
| 5119 | /// In C++11, 'false' is not a null pointer, so this breaks the build of any |
| 5120 | /// code using that header. |
| 5121 | /// |
| 5122 | /// Work around this by treating 'return false;' as zero-initializing the result |
| 5123 | /// if it's used in a pointer-returning function in a system header. |
| 5124 | static bool isLibstdcxxPointerReturnFalseHack(Sema &S, |
| 5125 | const InitializedEntity &Entity, |
| 5126 | const Expr *Init) { |
| 5127 | return S.getLangOpts().CPlusPlus11 && |
| 5128 | Entity.getKind() == InitializedEntity::EK_Result && |
| 5129 | Entity.getType()->isPointerType() && |
| 5130 | isa<CXXBoolLiteralExpr>(Init) && |
| 5131 | !cast<CXXBoolLiteralExpr>(Init)->getValue() && |
| 5132 | S.getSourceManager().isInSystemHeader(Init->getExprLoc()); |
| 5133 | } |
| 5134 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5135 | /// The non-zero enum values here are indexes into diagnostic alternatives. |
| 5136 | enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar }; |
| 5137 | |
| 5138 | /// Determines whether this expression is an acceptable ICR source. |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 5139 | static InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e, |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5140 | bool isAddressOf, bool &isWeakAccess) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5141 | // Skip parens. |
| 5142 | e = e->IgnoreParens(); |
| 5143 | |
| 5144 | // Skip address-of nodes. |
| 5145 | if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 5146 | if (op->getOpcode() == UO_AddrOf) |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5147 | return isInvalidICRSource(C, op->getSubExpr(), /*addressof*/ true, |
| 5148 | isWeakAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5149 | |
| 5150 | // Skip certain casts. |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 5151 | } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 5152 | switch (ce->getCastKind()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5153 | case CK_Dependent: |
| 5154 | case CK_BitCast: |
| 5155 | case CK_LValueBitCast: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5156 | case CK_NoOp: |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5157 | return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf, isWeakAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5158 | |
| 5159 | case CK_ArrayToPointerDecay: |
| 5160 | return IIK_nonscalar; |
| 5161 | |
| 5162 | case CK_NullToPointer: |
| 5163 | return IIK_okay; |
| 5164 | |
| 5165 | default: |
| 5166 | break; |
| 5167 | } |
| 5168 | |
| 5169 | // 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] | 5170 | } else if (isa<DeclRefExpr>(e)) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5171 | // set isWeakAccess to true, to mean that there will be an implicit |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5172 | // load which requires a cleanup. |
| 5173 | if (e->getType().getObjCLifetime() == Qualifiers::OCL_Weak) |
| 5174 | isWeakAccess = true; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5175 | |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 5176 | if (!isAddressOf) return IIK_nonlocal; |
| 5177 | |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5178 | VarDecl *var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl()); |
| 5179 | if (!var) return IIK_nonlocal; |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 5180 | |
| 5181 | return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5182 | |
| 5183 | // If we have a conditional operator, check both sides. |
| 5184 | } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) { |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5185 | if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf, |
| 5186 | isWeakAccess)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5187 | return iik; |
| 5188 | |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5189 | return isInvalidICRSource(C, cond->getRHS(), isAddressOf, isWeakAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5190 | |
| 5191 | // These are never scalar. |
| 5192 | } else if (isa<ArraySubscriptExpr>(e)) { |
| 5193 | return IIK_nonscalar; |
| 5194 | |
| 5195 | // Otherwise, it needs to be a null pointer constant. |
| 5196 | } else { |
| 5197 | return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull) |
| 5198 | ? IIK_okay : IIK_nonlocal); |
| 5199 | } |
| 5200 | |
| 5201 | return IIK_nonlocal; |
| 5202 | } |
| 5203 | |
| 5204 | /// Check whether the given expression is a valid operand for an |
| 5205 | /// indirect copy/restore. |
| 5206 | static void checkIndirectCopyRestoreSource(Sema &S, Expr *src) { |
| 5207 | assert(src->isRValue()); |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5208 | bool isWeakAccess = false; |
| 5209 | InvalidICRKind iik = isInvalidICRSource(S.Context, src, false, isWeakAccess); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5210 | // If isWeakAccess to true, there will be an implicit |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 5211 | // load which requires a cleanup. |
| 5212 | if (S.getLangOpts().ObjCAutoRefCount && isWeakAccess) |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 5213 | S.Cleanup.setExprNeedsCleanups(true); |
| 5214 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5215 | if (iik == IIK_okay) return; |
| 5216 | |
| 5217 | S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback) |
| 5218 | << ((unsigned) iik - 1) // shift index into diagnostic explanations |
| 5219 | << src->getSourceRange(); |
| 5220 | } |
| 5221 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5222 | /// Determine whether we have compatible array types for the |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5223 | /// purposes of GNU by-copy array initialization. |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 5224 | static bool hasCompatibleArrayTypes(ASTContext &Context, const ArrayType *Dest, |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5225 | const ArrayType *Source) { |
| 5226 | // If the source and destination array types are equivalent, we're |
| 5227 | // done. |
| 5228 | if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0))) |
| 5229 | return true; |
| 5230 | |
| 5231 | // Make sure that the element types are the same. |
| 5232 | if (!Context.hasSameType(Dest->getElementType(), Source->getElementType())) |
| 5233 | return false; |
| 5234 | |
| 5235 | // The only mismatch we allow is when the destination is an |
| 5236 | // incomplete array type and the source is a constant array type. |
| 5237 | return Source->isConstantArrayType() && Dest->isIncompleteArrayType(); |
| 5238 | } |
| 5239 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5240 | static bool tryObjCWritebackConversion(Sema &S, |
| 5241 | InitializationSequence &Sequence, |
| 5242 | const InitializedEntity &Entity, |
| 5243 | Expr *Initializer) { |
| 5244 | bool ArrayDecay = false; |
| 5245 | QualType ArgType = Initializer->getType(); |
| 5246 | QualType ArgPointee; |
| 5247 | if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) { |
| 5248 | ArrayDecay = true; |
| 5249 | ArgPointee = ArgArrayType->getElementType(); |
| 5250 | ArgType = S.Context.getPointerType(ArgPointee); |
| 5251 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5252 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5253 | // Handle write-back conversion. |
| 5254 | QualType ConvertedArgType; |
| 5255 | if (!S.isObjCWritebackConversion(ArgType, Entity.getType(), |
| 5256 | ConvertedArgType)) |
| 5257 | return false; |
| 5258 | |
| 5259 | // We should copy unless we're passing to an argument explicitly |
| 5260 | // marked 'out'. |
| 5261 | bool ShouldCopy = true; |
| 5262 | if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 5263 | ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 5264 | |
| 5265 | // Do we need an lvalue conversion? |
| 5266 | if (ArrayDecay || Initializer->isGLValue()) { |
| 5267 | ImplicitConversionSequence ICS; |
| 5268 | ICS.setStandard(); |
| 5269 | ICS.Standard.setAsIdentityConversion(); |
| 5270 | |
| 5271 | QualType ResultType; |
| 5272 | if (ArrayDecay) { |
| 5273 | ICS.Standard.First = ICK_Array_To_Pointer; |
| 5274 | ResultType = S.Context.getPointerType(ArgPointee); |
| 5275 | } else { |
| 5276 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
| 5277 | ResultType = Initializer->getType().getNonLValueExprType(S.Context); |
| 5278 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5279 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5280 | Sequence.AddConversionSequenceStep(ICS, ResultType); |
| 5281 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5282 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5283 | Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
| 5284 | return true; |
| 5285 | } |
| 5286 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5287 | static bool TryOCLSamplerInitialization(Sema &S, |
| 5288 | InitializationSequence &Sequence, |
| 5289 | QualType DestType, |
| 5290 | Expr *Initializer) { |
| 5291 | if (!S.getLangOpts().OpenCL || !DestType->isSamplerT() || |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 5292 | (!Initializer->isIntegerConstantExpr(S.Context) && |
| 5293 | !Initializer->getType()->isSamplerT())) |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5294 | return false; |
| 5295 | |
| 5296 | Sequence.AddOCLSamplerInitStep(DestType); |
| 5297 | return true; |
| 5298 | } |
| 5299 | |
Andrew Savonichev | 3fee351 | 2018-11-08 11:25:41 +0000 | [diff] [blame] | 5300 | static bool IsZeroInitializer(Expr *Initializer, Sema &S) { |
| 5301 | return Initializer->isIntegerConstantExpr(S.getASTContext()) && |
| 5302 | (Initializer->EvaluateKnownConstInt(S.getASTContext()) == 0); |
| 5303 | } |
| 5304 | |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 5305 | static bool TryOCLZeroOpaqueTypeInitialization(Sema &S, |
| 5306 | InitializationSequence &Sequence, |
| 5307 | QualType DestType, |
| 5308 | Expr *Initializer) { |
| 5309 | if (!S.getLangOpts().OpenCL) |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5310 | return false; |
| 5311 | |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 5312 | // |
| 5313 | // OpenCL 1.2 spec, s6.12.10 |
| 5314 | // |
| 5315 | // The event argument can also be used to associate the |
| 5316 | // async_work_group_copy with a previous async copy allowing |
| 5317 | // an event to be shared by multiple async copies; otherwise |
| 5318 | // event should be zero. |
| 5319 | // |
| 5320 | if (DestType->isEventT() || DestType->isQueueT()) { |
Andrew Savonichev | 3fee351 | 2018-11-08 11:25:41 +0000 | [diff] [blame] | 5321 | if (!IsZeroInitializer(Initializer, S)) |
| 5322 | return false; |
| 5323 | |
| 5324 | Sequence.AddOCLZeroOpaqueTypeStep(DestType); |
| 5325 | return true; |
| 5326 | } |
| 5327 | |
| 5328 | // We should allow zero initialization for all types defined in the |
| 5329 | // cl_intel_device_side_avc_motion_estimation extension, except |
| 5330 | // intel_sub_group_avc_mce_payload_t and intel_sub_group_avc_mce_result_t. |
| 5331 | if (S.getOpenCLOptions().isEnabled( |
| 5332 | "cl_intel_device_side_avc_motion_estimation") && |
| 5333 | DestType->isOCLIntelSubgroupAVCType()) { |
| 5334 | if (DestType->isOCLIntelSubgroupAVCMcePayloadType() || |
| 5335 | DestType->isOCLIntelSubgroupAVCMceResultType()) |
| 5336 | return false; |
| 5337 | if (!IsZeroInitializer(Initializer, S)) |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 5338 | return false; |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5339 | |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 5340 | Sequence.AddOCLZeroOpaqueTypeStep(DestType); |
| 5341 | return true; |
| 5342 | } |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 5343 | |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 5344 | return false; |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 5345 | } |
| 5346 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5347 | InitializationSequence::InitializationSequence(Sema &S, |
| 5348 | const InitializedEntity &Entity, |
| 5349 | const InitializationKind &Kind, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5350 | MultiExprArg Args, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 5351 | bool TopLevelOfInitList, |
| 5352 | bool TreatUnavailableAsInvalid) |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 5353 | : FailedCandidateSet(Kind.getLocation(), OverloadCandidateSet::CSK_Normal) { |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 5354 | InitializeFrom(S, Entity, Kind, Args, TopLevelOfInitList, |
| 5355 | TreatUnavailableAsInvalid); |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 5356 | } |
| 5357 | |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 5358 | /// Tries to get a FunctionDecl out of `E`. If it succeeds and we can take the |
| 5359 | /// address of that function, this returns true. Otherwise, it returns false. |
| 5360 | static bool isExprAnUnaddressableFunction(Sema &S, const Expr *E) { |
| 5361 | auto *DRE = dyn_cast<DeclRefExpr>(E); |
| 5362 | if (!DRE || !isa<FunctionDecl>(DRE->getDecl())) |
| 5363 | return false; |
| 5364 | |
| 5365 | return !S.checkAddressOfFunctionIsAvailable( |
| 5366 | cast<FunctionDecl>(DRE->getDecl())); |
| 5367 | } |
| 5368 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5369 | /// Determine whether we can perform an elementwise array copy for this kind |
| 5370 | /// of entity. |
| 5371 | static bool canPerformArrayCopy(const InitializedEntity &Entity) { |
| 5372 | switch (Entity.getKind()) { |
| 5373 | case InitializedEntity::EK_LambdaCapture: |
| 5374 | // C++ [expr.prim.lambda]p24: |
| 5375 | // For array members, the array elements are direct-initialized in |
| 5376 | // increasing subscript order. |
| 5377 | return true; |
| 5378 | |
| 5379 | case InitializedEntity::EK_Variable: |
| 5380 | // C++ [dcl.decomp]p1: |
| 5381 | // [...] each element is copy-initialized or direct-initialized from the |
| 5382 | // corresponding element of the assignment-expression [...] |
| 5383 | return isa<DecompositionDecl>(Entity.getDecl()); |
| 5384 | |
| 5385 | case InitializedEntity::EK_Member: |
| 5386 | // C++ [class.copy.ctor]p14: |
| 5387 | // - if the member is an array, each element is direct-initialized with |
| 5388 | // the corresponding subobject of x |
| 5389 | return Entity.isImplicitMemberInitializer(); |
| 5390 | |
| 5391 | case InitializedEntity::EK_ArrayElement: |
| 5392 | // All the above cases are intended to apply recursively, even though none |
| 5393 | // of them actually say that. |
| 5394 | if (auto *E = Entity.getParent()) |
| 5395 | return canPerformArrayCopy(*E); |
| 5396 | break; |
| 5397 | |
| 5398 | default: |
| 5399 | break; |
| 5400 | } |
| 5401 | |
| 5402 | return false; |
| 5403 | } |
| 5404 | |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 5405 | void InitializationSequence::InitializeFrom(Sema &S, |
| 5406 | const InitializedEntity &Entity, |
| 5407 | const InitializationKind &Kind, |
| 5408 | MultiExprArg Args, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 5409 | bool TopLevelOfInitList, |
| 5410 | bool TreatUnavailableAsInvalid) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5411 | ASTContext &Context = S.Context; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5412 | |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 5413 | // Eliminate non-overload placeholder types in the arguments. We |
| 5414 | // need to do this before checking whether types are dependent |
| 5415 | // because lowering a pseudo-object expression might well give us |
| 5416 | // something of dependent type. |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5417 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 5418 | if (Args[I]->getType()->isNonOverloadPlaceholderType()) { |
| 5419 | // FIXME: should we be doing this here? |
| 5420 | ExprResult result = S.CheckPlaceholderExpr(Args[I]); |
| 5421 | if (result.isInvalid()) { |
| 5422 | SetFailed(FK_PlaceholderType); |
| 5423 | return; |
| 5424 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5425 | Args[I] = result.get(); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 5426 | } |
| 5427 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5428 | // C++0x [dcl.init]p16: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5429 | // The semantics of initializers are as follows. The destination type is |
| 5430 | // the type of the object or reference being initialized and the source |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5431 | // 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] | 5432 | // 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] | 5433 | // parenthesized list of expressions. |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5434 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5435 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5436 | if (DestType->isDependentType() || |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5437 | Expr::hasAnyTypeDependentArguments(Args)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5438 | SequenceKind = DependentSequence; |
| 5439 | return; |
| 5440 | } |
| 5441 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5442 | // Almost everything is a normal sequence. |
| 5443 | setSequenceKind(NormalSequence); |
| 5444 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5445 | QualType SourceType; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5446 | Expr *Initializer = nullptr; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5447 | if (Args.size() == 1) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5448 | Initializer = Args[0]; |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 5449 | if (S.getLangOpts().ObjC) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5450 | if (S.CheckObjCBridgeRelatedConversions(Initializer->getBeginLoc(), |
Fariborz Jahanian | 283bf89 | 2013-12-18 21:04:43 +0000 | [diff] [blame] | 5451 | DestType, Initializer->getType(), |
| 5452 | Initializer) || |
| 5453 | S.ConversionToObjCStringLiteralCheck(DestType, Initializer)) |
| 5454 | Args[0] = Initializer; |
Fariborz Jahanian | 283bf89 | 2013-12-18 21:04:43 +0000 | [diff] [blame] | 5455 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5456 | if (!isa<InitListExpr>(Initializer)) |
| 5457 | SourceType = Initializer->getType(); |
| 5458 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5459 | |
Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 5460 | // - If the initializer is a (non-parenthesized) braced-init-list, the |
| 5461 | // object is list-initialized (8.5.4). |
| 5462 | if (Kind.getKind() != InitializationKind::IK_Direct) { |
| 5463 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 5464 | TryListInitialization(S, Entity, Kind, InitList, *this, |
| 5465 | TreatUnavailableAsInvalid); |
Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 5466 | return; |
| 5467 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5468 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5469 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5470 | // - If the destination type is a reference type, see 8.5.3. |
| 5471 | if (DestType->isReferenceType()) { |
| 5472 | // C++0x [dcl.init.ref]p1: |
| 5473 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 5474 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 5475 | // by an object that can be converted into a T. |
| 5476 | // (Therefore, multiple arguments are not permitted.) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5477 | if (Args.size() != 1) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5478 | SetFailed(FK_TooManyInitsForReference); |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 5479 | // C++17 [dcl.init.ref]p5: |
| 5480 | // A reference [...] is initialized by an expression [...] as follows: |
| 5481 | // If the initializer is not an expression, presumably we should reject, |
| 5482 | // but the standard fails to actually say so. |
| 5483 | else if (isa<InitListExpr>(Args[0])) |
| 5484 | SetFailed(FK_ParenthesizedListInitForReference); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5485 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5486 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5487 | return; |
| 5488 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5489 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5490 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5491 | if (Kind.getKind() == InitializationKind::IK_Value || |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5492 | (Kind.getKind() == InitializationKind::IK_Direct && Args.empty())) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5493 | TryValueInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5494 | return; |
| 5495 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5496 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5497 | // Handle default initialization. |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 5498 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5499 | TryDefaultInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5500 | return; |
| 5501 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5502 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 5503 | // - If the destination type is an array of characters, an array of |
| 5504 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 5505 | // initializer is a string literal, see 8.5.2. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5506 | // - Otherwise, if the destination type is an array, the program is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5507 | // ill-formed. |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5508 | if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) { |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 5509 | if (Initializer && isa<VariableArrayType>(DestAT)) { |
| 5510 | SetFailed(FK_VariableLengthArrayHasInitializer); |
| 5511 | return; |
| 5512 | } |
| 5513 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 5514 | if (Initializer) { |
| 5515 | switch (IsStringInit(Initializer, DestAT, Context)) { |
| 5516 | case SIF_None: |
| 5517 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
| 5518 | return; |
| 5519 | case SIF_NarrowStringIntoWideChar: |
| 5520 | SetFailed(FK_NarrowStringIntoWideCharArray); |
| 5521 | return; |
| 5522 | case SIF_WideStringIntoChar: |
| 5523 | SetFailed(FK_WideStringIntoCharArray); |
| 5524 | return; |
| 5525 | case SIF_IncompatWideStringIntoWideChar: |
| 5526 | SetFailed(FK_IncompatWideStringIntoWideChar); |
| 5527 | return; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 5528 | case SIF_PlainStringIntoUTF8Char: |
| 5529 | SetFailed(FK_PlainStringIntoUTF8Char); |
| 5530 | return; |
| 5531 | case SIF_UTF8StringIntoPlainChar: |
| 5532 | SetFailed(FK_UTF8StringIntoPlainChar); |
| 5533 | return; |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 5534 | case SIF_Other: |
| 5535 | break; |
| 5536 | } |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 5537 | } |
| 5538 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5539 | // Some kinds of initialization permit an array to be initialized from |
| 5540 | // another array of the same type, and perform elementwise initialization. |
| 5541 | if (Initializer && isa<ConstantArrayType>(DestAT) && |
| 5542 | S.Context.hasSameUnqualifiedType(Initializer->getType(), |
| 5543 | Entity.getType()) && |
| 5544 | canPerformArrayCopy(Entity)) { |
| 5545 | // If source is a prvalue, use it directly. |
| 5546 | if (Initializer->getValueKind() == VK_RValue) { |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 5547 | AddArrayInitStep(DestType, /*IsGNUExtension*/false); |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5548 | return; |
| 5549 | } |
| 5550 | |
| 5551 | // Emit element-at-a-time copy loop. |
| 5552 | InitializedEntity Element = |
| 5553 | InitializedEntity::InitializeElement(S.Context, 0, Entity); |
| 5554 | QualType InitEltT = |
| 5555 | Context.getAsArrayType(Initializer->getType())->getElementType(); |
Richard Smith | 30e304e | 2016-12-14 00:03:17 +0000 | [diff] [blame] | 5556 | OpaqueValueExpr OVE(Initializer->getExprLoc(), InitEltT, |
| 5557 | Initializer->getValueKind(), |
| 5558 | Initializer->getObjectKind()); |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5559 | Expr *OVEAsExpr = &OVE; |
| 5560 | InitializeFrom(S, Element, Kind, OVEAsExpr, TopLevelOfInitList, |
| 5561 | TreatUnavailableAsInvalid); |
| 5562 | if (!Failed()) |
| 5563 | AddArrayInitLoopStep(Entity.getType(), InitEltT); |
| 5564 | return; |
| 5565 | } |
| 5566 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5567 | // Note: as an GNU C extension, we allow initialization of an |
| 5568 | // array from a compound literal that creates an array of the same |
| 5569 | // type, so long as the initializer has no side effects. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5570 | if (!S.getLangOpts().CPlusPlus && Initializer && |
Eli Friedman | 88fccbd | 2019-02-11 22:54:27 +0000 | [diff] [blame] | 5571 | isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) && |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5572 | Initializer->getType()->isArrayType()) { |
| 5573 | const ArrayType *SourceAT |
| 5574 | = Context.getAsArrayType(Initializer->getType()); |
| 5575 | if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5576 | SetFailed(FK_ArrayTypeMismatch); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5577 | else if (Initializer->HasSideEffects(S.Context)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5578 | SetFailed(FK_NonConstantArrayInit); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5579 | else { |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 5580 | AddArrayInitStep(DestType, /*IsGNUExtension*/true); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5581 | } |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5582 | } |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5583 | // Note: as a GNU C++ extension, we allow list-initialization of a |
| 5584 | // class member of array type from a parenthesized initializer list. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5585 | else if (S.getLangOpts().CPlusPlus && |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5586 | Entity.getKind() == InitializedEntity::EK_Member && |
| 5587 | Initializer && isa<InitListExpr>(Initializer)) { |
| 5588 | TryListInitialization(S, Entity, Kind, cast<InitListExpr>(Initializer), |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 5589 | *this, TreatUnavailableAsInvalid); |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5590 | AddParenthesizedArrayInitStep(DestType); |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 5591 | } else if (DestAT->getElementType()->isCharType()) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5592 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 5593 | else if (IsWideCharCompatible(DestAT->getElementType(), Context)) |
| 5594 | SetFailed(FK_ArrayNeedsInitListOrWideStringLiteral); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5595 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5596 | SetFailed(FK_ArrayNeedsInitList); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5597 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5598 | return; |
| 5599 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5600 | |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 5601 | // Determine whether we should consider writeback conversions for |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5602 | // Objective-C ARC. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5603 | bool allowObjCWritebackConversion = S.getLangOpts().ObjCAutoRefCount && |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5604 | Entity.isParameterKind(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5605 | |
| 5606 | // We're at the end of the line for C: it's either a write-back conversion |
| 5607 | // 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] | 5608 | if (!S.getLangOpts().CPlusPlus) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5609 | // If allowed, check whether this is an Objective-C writeback conversion. |
| 5610 | if (allowObjCWritebackConversion && |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5611 | tryObjCWritebackConversion(S, *this, Entity, Initializer)) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5612 | return; |
| 5613 | } |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5614 | |
| 5615 | if (TryOCLSamplerInitialization(S, *this, DestType, Initializer)) |
| 5616 | return; |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5617 | |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 5618 | if (TryOCLZeroOpaqueTypeInitialization(S, *this, DestType, Initializer)) |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5619 | return; |
| 5620 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5621 | // Handle initialization in C |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5622 | AddCAssignmentStep(DestType); |
| 5623 | MaybeProduceObjCObject(S, *this, Entity); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5624 | return; |
| 5625 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5626 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5627 | assert(S.getLangOpts().CPlusPlus); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 5628 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5629 | // - If the destination type is a (possibly cv-qualified) class type: |
| 5630 | if (DestType->isRecordType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5631 | // - If the initialization is direct-initialization, or if it is |
| 5632 | // copy-initialization where the cv-unqualified version of the |
| 5633 | // 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] | 5634 | // class of the destination, constructors are considered. [...] |
| 5635 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 5636 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 5637 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5638 | S.IsDerivedFrom(Initializer->getBeginLoc(), SourceType, DestType)))) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5639 | TryConstructorInitialization(S, Entity, Kind, Args, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 5640 | DestType, DestType, *this); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5641 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5642 | // user-defined conversion sequences that can convert from the source |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5643 | // type to the destination type or (when a conversion function is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5644 | // used) to a derived class thereof are enumerated as described in |
| 5645 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 5646 | // (13.3). |
| 5647 | else |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5648 | TryUserDefinedConversion(S, DestType, Kind, Initializer, *this, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5649 | TopLevelOfInitList); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5650 | return; |
| 5651 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5652 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 5653 | assert(Args.size() >= 1 && "Zero-argument case handled above"); |
| 5654 | |
| 5655 | // The remaining cases all need a source type. |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5656 | if (Args.size() > 1) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5657 | SetFailed(FK_TooManyInitsForScalar); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5658 | return; |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 5659 | } else if (isa<InitListExpr>(Args[0])) { |
| 5660 | SetFailed(FK_ParenthesizedListInitForScalar); |
| 5661 | return; |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5662 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5663 | |
| 5664 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5665 | // type, conversion functions are considered. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5666 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5667 | // For a conversion to _Atomic(T) from either T or a class type derived |
| 5668 | // from T, initialize the T object then convert to _Atomic type. |
| 5669 | bool NeedAtomicConversion = false; |
| 5670 | if (const AtomicType *Atomic = DestType->getAs<AtomicType>()) { |
| 5671 | if (Context.hasSameUnqualifiedType(SourceType, Atomic->getValueType()) || |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5672 | S.IsDerivedFrom(Initializer->getBeginLoc(), SourceType, |
Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 5673 | Atomic->getValueType())) { |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5674 | DestType = Atomic->getValueType(); |
| 5675 | NeedAtomicConversion = true; |
| 5676 | } |
| 5677 | } |
| 5678 | |
| 5679 | TryUserDefinedConversion(S, DestType, Kind, Initializer, *this, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5680 | TopLevelOfInitList); |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5681 | MaybeProduceObjCObject(S, *this, Entity); |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5682 | if (!Failed() && NeedAtomicConversion) |
| 5683 | AddAtomicConversionStep(Entity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5684 | return; |
| 5685 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5686 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5687 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5688 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5689 | // conversions (Clause 4) will be used, if necessary, to convert the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5690 | // initializer expression to the cv-unqualified version of the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5691 | // destination type; no user-defined conversions are considered. |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5692 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5693 | ImplicitConversionSequence ICS |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5694 | = S.TryImplicitConversion(Initializer, DestType, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5695 | /*SuppressUserConversions*/true, |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 5696 | /*AllowExplicitConversions*/ false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 5697 | /*InOverloadResolution*/ false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5698 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 5699 | allowObjCWritebackConversion); |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5700 | |
| 5701 | if (ICS.isStandard() && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5702 | ICS.Standard.Second == ICK_Writeback_Conversion) { |
| 5703 | // Objective-C ARC writeback conversion. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5704 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5705 | // We should copy unless we're passing to an argument explicitly |
| 5706 | // marked 'out'. |
| 5707 | bool ShouldCopy = true; |
| 5708 | if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 5709 | ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5710 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5711 | // If there was an lvalue adjustment, add it as a separate conversion. |
| 5712 | if (ICS.Standard.First == ICK_Array_To_Pointer || |
| 5713 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 5714 | ImplicitConversionSequence LvalueICS; |
| 5715 | LvalueICS.setStandard(); |
| 5716 | LvalueICS.Standard.setAsIdentityConversion(); |
| 5717 | LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0)); |
| 5718 | LvalueICS.Standard.First = ICS.Standard.First; |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5719 | AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5720 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5721 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5722 | AddPassByIndirectCopyRestoreStep(DestType, ShouldCopy); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5723 | } else if (ICS.isBad()) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 5724 | DeclAccessPair dap; |
Richard Smith | f032001b | 2013-06-20 02:18:31 +0000 | [diff] [blame] | 5725 | if (isLibstdcxxPointerReturnFalseHack(S, Entity, Initializer)) { |
| 5726 | AddZeroInitializationStep(Entity.getType()); |
| 5727 | } else if (Initializer->getType() == Context.OverloadTy && |
| 5728 | !S.ResolveAddressOfOverloadedFunction(Initializer, DestType, |
| 5729 | false, dap)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5730 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 5731 | else if (Initializer->getType()->isFunctionType() && |
| 5732 | isExprAnUnaddressableFunction(S, Initializer)) |
| 5733 | SetFailed(InitializationSequence::FK_AddressOfUnaddressableFunction); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 5734 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5735 | SetFailed(InitializationSequence::FK_ConversionFailed); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5736 | } else { |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5737 | AddConversionSequenceStep(ICS, DestType, TopLevelOfInitList); |
John McCall | fa27234 | 2011-06-16 23:24:51 +0000 | [diff] [blame] | 5738 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 5739 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 5740 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5741 | } |
| 5742 | |
| 5743 | InitializationSequence::~InitializationSequence() { |
Davide Italiano | 67bb9f7 | 2015-07-01 21:51:58 +0000 | [diff] [blame] | 5744 | for (auto &S : Steps) |
| 5745 | S.Destroy(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5746 | } |
| 5747 | |
| 5748 | //===----------------------------------------------------------------------===// |
| 5749 | // Perform initialization |
| 5750 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5751 | static Sema::AssignmentAction |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 5752 | getAssignmentAction(const InitializedEntity &Entity, bool Diagnose = false) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5753 | switch(Entity.getKind()) { |
| 5754 | case InitializedEntity::EK_Variable: |
| 5755 | case InitializedEntity::EK_New: |
Douglas Gregor | 6dd3a6a | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 5756 | case InitializedEntity::EK_Exception: |
| 5757 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 5758 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5759 | return Sema::AA_Initializing; |
| 5760 | |
| 5761 | case InitializedEntity::EK_Parameter: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5762 | if (Entity.getDecl() && |
Douglas Gregor | 6b7f12c | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 5763 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 5764 | return Sema::AA_Sending; |
| 5765 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5766 | return Sema::AA_Passing; |
| 5767 | |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 5768 | case InitializedEntity::EK_Parameter_CF_Audited: |
| 5769 | if (Entity.getDecl() && |
| 5770 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 5771 | return Sema::AA_Sending; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5772 | |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 5773 | return !Diagnose ? Sema::AA_Passing : Sema::AA_Passing_CFAudited; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5774 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5775 | case InitializedEntity::EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 5776 | case InitializedEntity::EK_StmtExprResult: // FIXME: Not quite right. |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5777 | return Sema::AA_Returning; |
| 5778 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5779 | case InitializedEntity::EK_Temporary: |
Fariborz Jahanian | 14e9541 | 2013-07-11 19:13:34 +0000 | [diff] [blame] | 5780 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5781 | // FIXME: Can we tell apart casting vs. converting? |
| 5782 | return Sema::AA_Casting; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5783 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5784 | case InitializedEntity::EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 5785 | case InitializedEntity::EK_Binding: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 5786 | case InitializedEntity::EK_ArrayElement: |
| 5787 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 5788 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 5789 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 5790 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 5791 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5792 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5793 | return Sema::AA_Initializing; |
| 5794 | } |
| 5795 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 5796 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5797 | } |
| 5798 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5799 | /// Whether we should bind a created object as a temporary when |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5800 | /// initializing the given entity. |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5801 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5802 | switch (Entity.getKind()) { |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 5803 | case InitializedEntity::EK_ArrayElement: |
| 5804 | case InitializedEntity::EK_Member: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5805 | case InitializedEntity::EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 5806 | case InitializedEntity::EK_StmtExprResult: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5807 | case InitializedEntity::EK_New: |
| 5808 | case InitializedEntity::EK_Variable: |
| 5809 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 5810 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 5811 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 5812 | case InitializedEntity::EK_ComplexElement: |
Anders Carlsson | fcd764a | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 5813 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 5814 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 5815 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 5816 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5817 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5818 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5819 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5820 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5821 | case InitializedEntity::EK_Parameter_CF_Audited: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5822 | case InitializedEntity::EK_Temporary: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5823 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 5824 | case InitializedEntity::EK_Binding: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5825 | return true; |
| 5826 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5827 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5828 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 5829 | } |
| 5830 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5831 | /// Whether the given entity, when initialized with an object |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5832 | /// created for that initialization, requires destruction. |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 5833 | static bool shouldDestroyEntity(const InitializedEntity &Entity) { |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5834 | switch (Entity.getKind()) { |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5835 | case InitializedEntity::EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 5836 | case InitializedEntity::EK_StmtExprResult: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5837 | case InitializedEntity::EK_New: |
| 5838 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 5839 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5840 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 5841 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 5842 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 5843 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 5844 | case InitializedEntity::EK_LambdaCapture: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5845 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5846 | |
Richard Smith | 27874d6 | 2013-01-08 00:08:23 +0000 | [diff] [blame] | 5847 | case InitializedEntity::EK_Member: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 5848 | case InitializedEntity::EK_Binding: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5849 | case InitializedEntity::EK_Variable: |
| 5850 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5851 | case InitializedEntity::EK_Parameter_CF_Audited: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5852 | case InitializedEntity::EK_Temporary: |
| 5853 | case InitializedEntity::EK_ArrayElement: |
| 5854 | case InitializedEntity::EK_Exception: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5855 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5856 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5857 | return true; |
| 5858 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5859 | |
| 5860 | llvm_unreachable("missed an InitializedEntity kind?"); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5861 | } |
| 5862 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5863 | /// Get the location at which initialization diagnostics should appear. |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5864 | static SourceLocation getInitializationLoc(const InitializedEntity &Entity, |
| 5865 | Expr *Initializer) { |
| 5866 | switch (Entity.getKind()) { |
| 5867 | case InitializedEntity::EK_Result: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 5868 | case InitializedEntity::EK_StmtExprResult: |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5869 | return Entity.getReturnLoc(); |
| 5870 | |
| 5871 | case InitializedEntity::EK_Exception: |
| 5872 | return Entity.getThrowLoc(); |
| 5873 | |
| 5874 | case InitializedEntity::EK_Variable: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 5875 | case InitializedEntity::EK_Binding: |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5876 | return Entity.getDecl()->getLocation(); |
| 5877 | |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 5878 | case InitializedEntity::EK_LambdaCapture: |
| 5879 | return Entity.getCaptureLoc(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5880 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5881 | case InitializedEntity::EK_ArrayElement: |
| 5882 | case InitializedEntity::EK_Member: |
| 5883 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5884 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5885 | case InitializedEntity::EK_Temporary: |
| 5886 | case InitializedEntity::EK_New: |
| 5887 | case InitializedEntity::EK_Base: |
| 5888 | case InitializedEntity::EK_Delegating: |
| 5889 | case InitializedEntity::EK_VectorElement: |
| 5890 | case InitializedEntity::EK_ComplexElement: |
| 5891 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 5892 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5893 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5894 | case InitializedEntity::EK_RelatedResult: |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5895 | return Initializer->getBeginLoc(); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5896 | } |
| 5897 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 5898 | } |
| 5899 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5900 | /// Make a (potentially elidable) temporary copy of the object |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5901 | /// provided by the given initializer by calling the appropriate copy |
| 5902 | /// constructor. |
| 5903 | /// |
| 5904 | /// \param S The Sema object used for type-checking. |
| 5905 | /// |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 5906 | /// \param T The type of the temporary object, which must either be |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5907 | /// the type of the initializer expression or a superclass thereof. |
| 5908 | /// |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5909 | /// \param Entity The entity being initialized. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5910 | /// |
| 5911 | /// \param CurInit The initializer expression. |
| 5912 | /// |
| 5913 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 5914 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 5915 | /// an rvalue. |
| 5916 | /// |
| 5917 | /// \returns An expression that copies the initializer expression into |
| 5918 | /// a temporary object, or an error expression if a copy could not be |
| 5919 | /// created. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5920 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 5921 | QualType T, |
| 5922 | const InitializedEntity &Entity, |
| 5923 | ExprResult CurInit, |
| 5924 | bool IsExtraneousCopy) { |
Fariborz Jahanian | 36f7f13 | 2015-01-28 22:08:10 +0000 | [diff] [blame] | 5925 | if (CurInit.isInvalid()) |
| 5926 | return CurInit; |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5927 | // Determine which class type we're copying to. |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 5928 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5929 | CXXRecordDecl *Class = nullptr; |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5930 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5931 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 5932 | if (!Class) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5933 | return CurInit; |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5934 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5935 | SourceLocation Loc = getInitializationLoc(Entity, CurInit.get()); |
Douglas Gregor | d5c231e | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 5936 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5937 | // Make sure that the type we are copying is complete. |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5938 | if (S.RequireCompleteType(Loc, T, diag::err_temp_copy_incomplete)) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5939 | return CurInit; |
Douglas Gregor | d5c231e | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 5940 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5941 | // Perform overload resolution using the class's constructors. Per |
| 5942 | // C++11 [dcl.init]p16, second bullet for class types, this initialization |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5943 | // is direct-initialization. |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 5944 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5945 | DeclContext::lookup_result Ctors = S.LookupConstructors(Class); |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5946 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5947 | OverloadCandidateSet::iterator Best; |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5948 | switch (ResolveConstructorOverload( |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 5949 | S, Loc, CurInitExpr, CandidateSet, T, Ctors, Best, |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5950 | /*CopyInitializing=*/false, /*AllowExplicit=*/true, |
| 5951 | /*OnlyListConstructors=*/false, /*IsListInit=*/false, |
| 5952 | /*SecondStepOfCopyInit=*/true)) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5953 | case OR_Success: |
| 5954 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5955 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5956 | case OR_No_Viable_Function: |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 5957 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 5958 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 5959 | : diag::err_temp_copy_no_viable) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 5960 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5961 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5962 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 5963 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5964 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5965 | return CurInit; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5966 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5967 | case OR_Ambiguous: |
| 5968 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 5969 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5970 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5971 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5972 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5973 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5974 | case OR_Deleted: |
| 5975 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 5976 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5977 | << CurInitExpr->getSourceRange(); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5978 | S.NoteDeletedFunction(Best->Function); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5979 | return ExprError(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5980 | } |
| 5981 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 5982 | bool HadMultipleCandidates = CandidateSet.size() > 1; |
| 5983 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5984 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 5985 | SmallVector<Expr*, 8> ConstructorArgs; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5986 | CurInit.get(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5987 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 5988 | S.CheckConstructorAccess(Loc, Constructor, Best->FoundDecl, Entity, |
| 5989 | IsExtraneousCopy); |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5990 | |
| 5991 | if (IsExtraneousCopy) { |
| 5992 | // If this is a totally extraneous copy for C++03 reference |
| 5993 | // binding purposes, just return the original initialization |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 5994 | // expression. We don't generate an (elided) copy operation here |
| 5995 | // because doing so would require us to pass down a flag to avoid |
| 5996 | // infinite recursion, where each step adds another extraneous, |
| 5997 | // elidable copy. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5998 | |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 5999 | // Instantiate the default arguments of any extra parameters in |
| 6000 | // the selected copy constructor, as if we were going to create a |
| 6001 | // proper call to the copy constructor. |
| 6002 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 6003 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 6004 | if (S.RequireCompleteType(Loc, Parm->getType(), |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 6005 | diag::err_call_incomplete_argument)) |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 6006 | break; |
| 6007 | |
| 6008 | // Build the default argument expression; we don't actually care |
| 6009 | // if this succeeds or not, because this routine will complain |
| 6010 | // if there was a problem. |
| 6011 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 6012 | } |
| 6013 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6014 | return CurInitExpr; |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 6015 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6016 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 6017 | // Determine the arguments required to actually perform the |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 6018 | // constructor call (we might have derived-to-base conversions, or |
| 6019 | // the copy constructor may have default arguments). |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6020 | if (S.CompleteConstructorCall(Constructor, CurInitExpr, Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6021 | return ExprError(); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 6022 | |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 6023 | // C++0x [class.copy]p32: |
| 6024 | // When certain criteria are met, an implementation is allowed to |
| 6025 | // omit the copy/move construction of a class object, even if the |
| 6026 | // copy/move constructor and/or destructor for the object have |
| 6027 | // side effects. [...] |
| 6028 | // - when a temporary class object that has not been bound to a |
| 6029 | // reference (12.2) would be copied/moved to a class object |
| 6030 | // with the same cv-unqualified type, the copy/move operation |
| 6031 | // can be omitted by constructing the temporary object |
| 6032 | // directly into the target of the omitted copy/move |
| 6033 | // |
| 6034 | // Note that the other three bullets are handled elsewhere. Copy |
| 6035 | // elision for return statements and throw expressions are handled as part |
| 6036 | // of constructor initialization, while copy elision for exception handlers |
| 6037 | // is handled by the run-time. |
| 6038 | // |
| 6039 | // FIXME: If the function parameter is not the same type as the temporary, we |
| 6040 | // should still be able to elide the copy, but we don't have a way to |
| 6041 | // represent in the AST how much should be elided in this case. |
| 6042 | bool Elidable = |
| 6043 | CurInitExpr->isTemporaryObject(S.Context, Class) && |
| 6044 | S.Context.hasSameUnqualifiedType( |
| 6045 | Best->Function->getParamDecl(0)->getType().getNonReferenceType(), |
| 6046 | CurInitExpr->getType()); |
| 6047 | |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 6048 | // Actually perform the constructor call. |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 6049 | CurInit = S.BuildCXXConstructExpr(Loc, T, Best->FoundDecl, Constructor, |
| 6050 | Elidable, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6051 | ConstructorArgs, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 6052 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6053 | /*ListInit*/ false, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6054 | /*StdInitListInit*/ false, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 6055 | /*ZeroInit*/ false, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 6056 | CXXConstructExpr::CK_Complete, |
| 6057 | SourceRange()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6058 | |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 6059 | // If we're supposed to bind temporaries, do so. |
| 6060 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6061 | CurInit = S.MaybeBindToTemporary(CurInit.getAs<Expr>()); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6062 | return CurInit; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6063 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6064 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6065 | /// Check whether elidable copy construction for binding a reference to |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6066 | /// a temporary would have succeeded if we were building in C++98 mode, for |
| 6067 | /// -Wc++98-compat. |
| 6068 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 6069 | const InitializedEntity &Entity, |
| 6070 | Expr *CurInitExpr) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6071 | assert(S.getLangOpts().CPlusPlus11); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6072 | |
| 6073 | const RecordType *Record = CurInitExpr->getType()->getAs<RecordType>(); |
| 6074 | if (!Record) |
| 6075 | return; |
| 6076 | |
| 6077 | SourceLocation Loc = getInitializationLoc(Entity, CurInitExpr); |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 6078 | if (S.Diags.isIgnored(diag::warn_cxx98_compat_temp_copy, Loc)) |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6079 | return; |
| 6080 | |
| 6081 | // Find constructors which would have been considered. |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 6082 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 6083 | DeclContext::lookup_result Ctors = |
| 6084 | S.LookupConstructors(cast<CXXRecordDecl>(Record->getDecl())); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6085 | |
| 6086 | // Perform overload resolution. |
| 6087 | OverloadCandidateSet::iterator Best; |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 6088 | OverloadingResult OR = ResolveConstructorOverload( |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 6089 | S, Loc, CurInitExpr, CandidateSet, CurInitExpr->getType(), Ctors, Best, |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 6090 | /*CopyInitializing=*/false, /*AllowExplicit=*/true, |
| 6091 | /*OnlyListConstructors=*/false, /*IsListInit=*/false, |
| 6092 | /*SecondStepOfCopyInit=*/true); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6093 | |
| 6094 | PartialDiagnostic Diag = S.PDiag(diag::warn_cxx98_compat_temp_copy) |
| 6095 | << OR << (int)Entity.getKind() << CurInitExpr->getType() |
| 6096 | << CurInitExpr->getSourceRange(); |
| 6097 | |
| 6098 | switch (OR) { |
| 6099 | case OR_Success: |
| 6100 | S.CheckConstructorAccess(Loc, cast<CXXConstructorDecl>(Best->Function), |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6101 | Best->FoundDecl, Entity, Diag); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6102 | // FIXME: Check default arguments as far as that's possible. |
| 6103 | break; |
| 6104 | |
| 6105 | case OR_No_Viable_Function: |
| 6106 | S.Diag(Loc, Diag); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6107 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6108 | break; |
| 6109 | |
| 6110 | case OR_Ambiguous: |
| 6111 | S.Diag(Loc, Diag); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6112 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6113 | break; |
| 6114 | |
| 6115 | case OR_Deleted: |
| 6116 | S.Diag(Loc, Diag); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6117 | S.NoteDeletedFunction(Best->Function); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 6118 | break; |
| 6119 | } |
| 6120 | } |
| 6121 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6122 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 6123 | const InitializedEntity &Entity) { |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6124 | if (Entity.isParameterKind() && Entity.getDecl()) { |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6125 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 6126 | return; |
| 6127 | |
| 6128 | if (Entity.getDecl()->getDeclName()) |
| 6129 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 6130 | << Entity.getDecl()->getDeclName(); |
| 6131 | else |
| 6132 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 6133 | } |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 6134 | else if (Entity.getKind() == InitializedEntity::EK_RelatedResult && |
| 6135 | Entity.getMethodDecl()) |
| 6136 | S.Diag(Entity.getMethodDecl()->getLocation(), |
| 6137 | diag::note_method_return_type_change) |
| 6138 | << Entity.getMethodDecl()->getDeclName(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6139 | } |
| 6140 | |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 6141 | /// Returns true if the parameters describe a constructor initialization of |
| 6142 | /// an explicit temporary object, e.g. "Point(x, y)". |
| 6143 | static bool isExplicitTemporary(const InitializedEntity &Entity, |
| 6144 | const InitializationKind &Kind, |
| 6145 | unsigned NumArgs) { |
| 6146 | switch (Entity.getKind()) { |
| 6147 | case InitializedEntity::EK_Temporary: |
| 6148 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 6149 | case InitializedEntity::EK_RelatedResult: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 6150 | break; |
| 6151 | default: |
| 6152 | return false; |
| 6153 | } |
| 6154 | |
| 6155 | switch (Kind.getKind()) { |
| 6156 | case InitializationKind::IK_DirectList: |
| 6157 | return true; |
| 6158 | // FIXME: Hack to work around cast weirdness. |
| 6159 | case InitializationKind::IK_Direct: |
| 6160 | case InitializationKind::IK_Value: |
| 6161 | return NumArgs != 1; |
| 6162 | default: |
| 6163 | return false; |
| 6164 | } |
| 6165 | } |
| 6166 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6167 | static ExprResult |
| 6168 | PerformConstructorInitialization(Sema &S, |
| 6169 | const InitializedEntity &Entity, |
| 6170 | const InitializationKind &Kind, |
| 6171 | MultiExprArg Args, |
| 6172 | const InitializationSequence::Step& Step, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6173 | bool &ConstructorInitRequiresZeroInit, |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 6174 | bool IsListInitialization, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6175 | bool IsStdInitListInitialization, |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 6176 | SourceLocation LBraceLoc, |
| 6177 | SourceLocation RBraceLoc) { |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6178 | unsigned NumArgs = Args.size(); |
| 6179 | CXXConstructorDecl *Constructor |
| 6180 | = cast<CXXConstructorDecl>(Step.Function.Function); |
| 6181 | bool HadMultipleCandidates = Step.Function.HadMultipleCandidates; |
| 6182 | |
| 6183 | // Build a call to the selected constructor. |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 6184 | SmallVector<Expr*, 8> ConstructorArgs; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6185 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 6186 | ? Kind.getEqualLoc() |
| 6187 | : Kind.getLocation(); |
| 6188 | |
| 6189 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 6190 | // Force even a trivial, implicit default constructor to be |
| 6191 | // semantically checked. We do this explicitly because we don't build |
| 6192 | // the definition for completely trivial constructors. |
Matt Beaumont-Gay | 47ff122 | 2012-02-24 08:37:56 +0000 | [diff] [blame] | 6193 | assert(Constructor->getParent() && "No parent class for constructor."); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6194 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Douglas Gregor | f704ade | 2012-02-24 07:48:37 +0000 | [diff] [blame] | 6195 | Constructor->isTrivial() && !Constructor->isUsed(false)) |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6196 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 6197 | } |
| 6198 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6199 | ExprResult CurInit((Expr *)nullptr); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6200 | |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6201 | // C++ [over.match.copy]p1: |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6202 | // - When initializing a temporary to be bound to the first parameter |
| 6203 | // of a constructor that takes a reference to possibly cv-qualified |
| 6204 | // T as its first argument, called with a single argument in the |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6205 | // context of direct-initialization, explicit conversion functions |
| 6206 | // are also considered. |
Richard Smith | 7c2bcc9 | 2016-09-07 02:14:33 +0000 | [diff] [blame] | 6207 | bool AllowExplicitConv = |
| 6208 | Kind.AllowExplicit() && !Kind.isCopyInit() && Args.size() == 1 && |
| 6209 | hasCopyOrMoveCtorParam(S.Context, |
| 6210 | getConstructorInfo(Step.Function.FoundDecl)); |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6211 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6212 | // Determine the arguments required to actually perform the constructor |
| 6213 | // call. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6214 | if (S.CompleteConstructorCall(Constructor, Args, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6215 | Loc, ConstructorArgs, |
Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 6216 | AllowExplicitConv, |
| 6217 | IsListInitialization)) |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6218 | return ExprError(); |
| 6219 | |
| 6220 | |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 6221 | if (isExplicitTemporary(Entity, Kind, NumArgs)) { |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6222 | // An explicitly-constructed temporary, e.g., X(1, 2). |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 6223 | if (S.DiagnoseUseOfDecl(Constructor, Loc)) |
| 6224 | return ExprError(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6225 | |
| 6226 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 6227 | if (!TSInfo) |
| 6228 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
Vedant Kumar | c9a9531 | 2018-11-19 20:10:21 +0000 | [diff] [blame] | 6229 | SourceRange ParenOrBraceRange = |
| 6230 | (Kind.getKind() == InitializationKind::IK_DirectList) |
| 6231 | ? SourceRange(LBraceLoc, RBraceLoc) |
| 6232 | : Kind.getParenOrBraceRange(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6233 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6234 | if (auto *Shadow = dyn_cast<ConstructorUsingShadowDecl>( |
Richard Smith | 80a4702 | 2016-06-29 01:10:27 +0000 | [diff] [blame] | 6235 | Step.Function.FoundDecl.getDecl())) { |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6236 | Constructor = S.findInheritingConstructor(Loc, Constructor, Shadow); |
Richard Smith | 80a4702 | 2016-06-29 01:10:27 +0000 | [diff] [blame] | 6237 | if (S.DiagnoseUseOfDecl(Constructor, Loc)) |
| 6238 | return ExprError(); |
| 6239 | } |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6240 | S.MarkFunctionReferenced(Loc, Constructor); |
| 6241 | |
Bruno Ricci | ddb8f6b | 2018-12-22 14:39:30 +0000 | [diff] [blame] | 6242 | CurInit = CXXTemporaryObjectExpr::Create( |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 6243 | S.Context, Constructor, |
| 6244 | Entity.getType().getNonLValueExprType(S.Context), TSInfo, |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 6245 | ConstructorArgs, ParenOrBraceRange, HadMultipleCandidates, |
| 6246 | IsListInitialization, IsStdInitListInitialization, |
| 6247 | ConstructorInitRequiresZeroInit); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6248 | } else { |
| 6249 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 6250 | CXXConstructExpr::CK_Complete; |
| 6251 | |
| 6252 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 6253 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
| 6254 | CXXConstructExpr::CK_VirtualBase : |
| 6255 | CXXConstructExpr::CK_NonVirtualBase; |
| 6256 | } else if (Entity.getKind() == InitializedEntity::EK_Delegating) { |
| 6257 | ConstructKind = CXXConstructExpr::CK_Delegating; |
| 6258 | } |
| 6259 | |
Peter Collingbourne | b8d17e7 | 2014-02-22 02:59:41 +0000 | [diff] [blame] | 6260 | // Only get the parenthesis or brace range if it is a list initialization or |
| 6261 | // direct construction. |
| 6262 | SourceRange ParenOrBraceRange; |
| 6263 | if (IsListInitialization) |
| 6264 | ParenOrBraceRange = SourceRange(LBraceLoc, RBraceLoc); |
| 6265 | else if (Kind.getKind() == InitializationKind::IK_Direct) |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 6266 | ParenOrBraceRange = Kind.getParenOrBraceRange(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6267 | |
| 6268 | // If the entity allows NRVO, mark the construction as elidable |
| 6269 | // unconditionally. |
| 6270 | if (Entity.allowsNRVO()) |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 6271 | CurInit = S.BuildCXXConstructExpr(Loc, Step.Type, |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 6272 | Step.Function.FoundDecl, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6273 | Constructor, /*Elidable=*/true, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6274 | ConstructorArgs, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6275 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6276 | IsListInitialization, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6277 | IsStdInitListInitialization, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6278 | ConstructorInitRequiresZeroInit, |
| 6279 | ConstructKind, |
Peter Collingbourne | b8d17e7 | 2014-02-22 02:59:41 +0000 | [diff] [blame] | 6280 | ParenOrBraceRange); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6281 | else |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 6282 | CurInit = S.BuildCXXConstructExpr(Loc, Step.Type, |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 6283 | Step.Function.FoundDecl, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6284 | Constructor, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6285 | ConstructorArgs, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6286 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6287 | IsListInitialization, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6288 | IsStdInitListInitialization, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6289 | ConstructorInitRequiresZeroInit, |
| 6290 | ConstructKind, |
Peter Collingbourne | b8d17e7 | 2014-02-22 02:59:41 +0000 | [diff] [blame] | 6291 | ParenOrBraceRange); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6292 | } |
| 6293 | if (CurInit.isInvalid()) |
| 6294 | return ExprError(); |
| 6295 | |
| 6296 | // Only check access if all of that succeeded. |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6297 | S.CheckConstructorAccess(Loc, Constructor, Step.Function.FoundDecl, Entity); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 6298 | if (S.DiagnoseUseOfDecl(Step.Function.FoundDecl, Loc)) |
| 6299 | return ExprError(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6300 | |
| 6301 | if (shouldBindAsTemporary(Entity)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6302 | CurInit = S.MaybeBindToTemporary(CurInit.get()); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6303 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6304 | return CurInit; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6305 | } |
| 6306 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6307 | namespace { |
| 6308 | enum LifetimeKind { |
| 6309 | /// The lifetime of a temporary bound to this entity ends at the end of the |
| 6310 | /// full-expression, and that's (probably) fine. |
| 6311 | LK_FullExpression, |
| 6312 | |
| 6313 | /// The lifetime of a temporary bound to this entity is extended to the |
| 6314 | /// lifeitme of the entity itself. |
| 6315 | LK_Extended, |
| 6316 | |
| 6317 | /// The lifetime of a temporary bound to this entity probably ends too soon, |
| 6318 | /// because the entity is allocated in a new-expression. |
| 6319 | LK_New, |
| 6320 | |
| 6321 | /// The lifetime of a temporary bound to this entity ends too soon, because |
| 6322 | /// the entity is a return object. |
| 6323 | LK_Return, |
| 6324 | |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 6325 | /// The lifetime of a temporary bound to this entity ends too soon, because |
| 6326 | /// the entity is the result of a statement expression. |
| 6327 | LK_StmtExprResult, |
| 6328 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6329 | /// This is a mem-initializer: if it would extend a temporary (other than via |
| 6330 | /// a default member initializer), the program is ill-formed. |
| 6331 | LK_MemInitializer, |
| 6332 | }; |
| 6333 | using LifetimeResult = |
| 6334 | llvm::PointerIntPair<const InitializedEntity *, 3, LifetimeKind>; |
| 6335 | } |
| 6336 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6337 | /// Determine the declaration which an initialized entity ultimately refers to, |
| 6338 | /// for the purpose of lifetime-extending a temporary bound to a reference in |
| 6339 | /// the initialization of \p Entity. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6340 | static LifetimeResult getEntityLifetime( |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 6341 | const InitializedEntity *Entity, |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6342 | const InitializedEntity *InitField = nullptr) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6343 | // C++11 [class.temporary]p5: |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 6344 | switch (Entity->getKind()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6345 | case InitializedEntity::EK_Variable: |
| 6346 | // The temporary [...] persists for the lifetime of the reference |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6347 | return {Entity, LK_Extended}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6348 | |
| 6349 | case InitializedEntity::EK_Member: |
| 6350 | // For subobjects, we look at the complete object. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 6351 | if (Entity->getParent()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6352 | return getEntityLifetime(Entity->getParent(), Entity); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6353 | |
| 6354 | // except: |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6355 | // C++17 [class.base.init]p8: |
| 6356 | // A temporary expression bound to a reference member in a |
| 6357 | // mem-initializer is ill-formed. |
| 6358 | // C++17 [class.base.init]p11: |
| 6359 | // A temporary expression bound to a reference member from a |
| 6360 | // default member initializer is ill-formed. |
| 6361 | // |
| 6362 | // The context of p11 and its example suggest that it's only the use of a |
| 6363 | // default member initializer from a constructor that makes the program |
| 6364 | // ill-formed, not its mere existence, and that it can even be used by |
| 6365 | // aggregate initialization. |
| 6366 | return {Entity, Entity->isDefaultMemberInitializer() ? LK_Extended |
| 6367 | : LK_MemInitializer}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6368 | |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 6369 | case InitializedEntity::EK_Binding: |
Richard Smith | 3997b1b | 2016-08-12 01:55:21 +0000 | [diff] [blame] | 6370 | // Per [dcl.decomp]p3, the binding is treated as a variable of reference |
| 6371 | // type. |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6372 | return {Entity, LK_Extended}; |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 6373 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6374 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6375 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6376 | // -- A temporary bound to a reference parameter in a function call |
| 6377 | // persists until the completion of the full-expression containing |
| 6378 | // the call. |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6379 | return {nullptr, LK_FullExpression}; |
| 6380 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6381 | case InitializedEntity::EK_Result: |
| 6382 | // -- The lifetime of a temporary bound to the returned value in a |
| 6383 | // function return statement is not extended; the temporary is |
| 6384 | // destroyed at the end of the full-expression in the return statement. |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6385 | return {nullptr, LK_Return}; |
| 6386 | |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 6387 | case InitializedEntity::EK_StmtExprResult: |
| 6388 | // FIXME: Should we lifetime-extend through the result of a statement |
| 6389 | // expression? |
| 6390 | return {nullptr, LK_StmtExprResult}; |
| 6391 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6392 | case InitializedEntity::EK_New: |
| 6393 | // -- A temporary bound to a reference in a new-initializer persists |
| 6394 | // until the completion of the full-expression containing the |
| 6395 | // new-initializer. |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6396 | return {nullptr, LK_New}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6397 | |
| 6398 | case InitializedEntity::EK_Temporary: |
| 6399 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 6400 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6401 | // We don't yet know the storage duration of the surrounding temporary. |
| 6402 | // Assume it's got full-expression duration for now, it will patch up our |
| 6403 | // storage duration if that's not correct. |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6404 | return {nullptr, LK_FullExpression}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6405 | |
| 6406 | case InitializedEntity::EK_ArrayElement: |
| 6407 | // For subobjects, we look at the complete object. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6408 | return getEntityLifetime(Entity->getParent(), InitField); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6409 | |
| 6410 | case InitializedEntity::EK_Base: |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 6411 | // For subobjects, we look at the complete object. |
| 6412 | if (Entity->getParent()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6413 | return getEntityLifetime(Entity->getParent(), InitField); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6414 | return {InitField, LK_MemInitializer}; |
| 6415 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6416 | case InitializedEntity::EK_Delegating: |
| 6417 | // We can reach this case for aggregate initialization in a constructor: |
| 6418 | // struct A { int &&r; }; |
| 6419 | // struct B : A { B() : A{0} {} }; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6420 | // In this case, use the outermost field decl as the context. |
| 6421 | return {InitField, LK_MemInitializer}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6422 | |
| 6423 | case InitializedEntity::EK_BlockElement: |
Alex Lorenz | b4791c7 | 2017-04-06 12:53:43 +0000 | [diff] [blame] | 6424 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6425 | case InitializedEntity::EK_LambdaCapture: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6426 | case InitializedEntity::EK_VectorElement: |
| 6427 | case InitializedEntity::EK_ComplexElement: |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6428 | return {nullptr, LK_FullExpression}; |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6429 | |
| 6430 | case InitializedEntity::EK_Exception: |
| 6431 | // FIXME: Can we diagnose lifetime problems with exceptions? |
| 6432 | return {nullptr, LK_FullExpression}; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6433 | } |
Benjamin Kramer | cabc882 | 2013-06-05 15:37:50 +0000 | [diff] [blame] | 6434 | llvm_unreachable("unknown entity kind"); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6435 | } |
| 6436 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6437 | namespace { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6438 | enum ReferenceKind { |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6439 | /// Lifetime would be extended by a reference binding to a temporary. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6440 | RK_ReferenceBinding, |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6441 | /// Lifetime would be extended by a std::initializer_list object binding to |
| 6442 | /// its backing array. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6443 | RK_StdInitializerList, |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6444 | }; |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6445 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6446 | /// A temporary or local variable. This will be one of: |
| 6447 | /// * A MaterializeTemporaryExpr. |
| 6448 | /// * A DeclRefExpr whose declaration is a local. |
| 6449 | /// * An AddrLabelExpr. |
| 6450 | /// * A BlockExpr for a block with captures. |
| 6451 | using Local = Expr*; |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6452 | |
| 6453 | /// Expressions we stepped over when looking for the local state. Any steps |
| 6454 | /// that would inhibit lifetime extension or take us out of subexpressions of |
| 6455 | /// the initializer are included. |
| 6456 | struct IndirectLocalPathEntry { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6457 | enum EntryKind { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6458 | DefaultInit, |
| 6459 | AddressOf, |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6460 | VarInit, |
| 6461 | LValToRVal, |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6462 | LifetimeBoundCall, |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6463 | } Kind; |
| 6464 | Expr *E; |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6465 | const Decl *D = nullptr; |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6466 | IndirectLocalPathEntry() {} |
| 6467 | IndirectLocalPathEntry(EntryKind K, Expr *E) : Kind(K), E(E) {} |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6468 | IndirectLocalPathEntry(EntryKind K, Expr *E, const Decl *D) |
| 6469 | : Kind(K), E(E), D(D) {} |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6470 | }; |
| 6471 | |
| 6472 | using IndirectLocalPath = llvm::SmallVectorImpl<IndirectLocalPathEntry>; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6473 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6474 | struct RevertToOldSizeRAII { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6475 | IndirectLocalPath &Path; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6476 | unsigned OldSize = Path.size(); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6477 | RevertToOldSizeRAII(IndirectLocalPath &Path) : Path(Path) {} |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6478 | ~RevertToOldSizeRAII() { Path.resize(OldSize); } |
| 6479 | }; |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6480 | |
| 6481 | using LocalVisitor = llvm::function_ref<bool(IndirectLocalPath &Path, Local L, |
| 6482 | ReferenceKind RK)>; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6483 | } |
| 6484 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6485 | static bool isVarOnPath(IndirectLocalPath &Path, VarDecl *VD) { |
| 6486 | for (auto E : Path) |
| 6487 | if (E.Kind == IndirectLocalPathEntry::VarInit && E.D == VD) |
| 6488 | return true; |
| 6489 | return false; |
| 6490 | } |
| 6491 | |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6492 | static bool pathContainsInit(IndirectLocalPath &Path) { |
Fangrui Song | 3117b17 | 2018-10-20 17:53:42 +0000 | [diff] [blame] | 6493 | return llvm::any_of(Path, [=](IndirectLocalPathEntry E) { |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6494 | return E.Kind == IndirectLocalPathEntry::DefaultInit || |
| 6495 | E.Kind == IndirectLocalPathEntry::VarInit; |
| 6496 | }); |
| 6497 | } |
| 6498 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6499 | static void visitLocalsRetainedByInitializer(IndirectLocalPath &Path, |
| 6500 | Expr *Init, LocalVisitor Visit, |
| 6501 | bool RevisitSubinits); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6502 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6503 | static void visitLocalsRetainedByReferenceBinding(IndirectLocalPath &Path, |
| 6504 | Expr *Init, ReferenceKind RK, |
| 6505 | LocalVisitor Visit); |
| 6506 | |
| 6507 | static bool implicitObjectParamIsLifetimeBound(const FunctionDecl *FD) { |
| 6508 | const TypeSourceInfo *TSI = FD->getTypeSourceInfo(); |
| 6509 | if (!TSI) |
| 6510 | return false; |
Martin Storsjo | d03fa99 | 2018-08-02 18:12:08 +0000 | [diff] [blame] | 6511 | // Don't declare this variable in the second operand of the for-statement; |
| 6512 | // GCC miscompiles that by ending its lifetime before evaluating the |
| 6513 | // third operand. See gcc.gnu.org/PR86769. |
| 6514 | AttributedTypeLoc ATL; |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6515 | for (TypeLoc TL = TSI->getTypeLoc(); |
Martin Storsjo | d03fa99 | 2018-08-02 18:12:08 +0000 | [diff] [blame] | 6516 | (ATL = TL.getAsAdjusted<AttributedTypeLoc>()); |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6517 | TL = ATL.getModifiedLoc()) { |
Richard Smith | e43e2b3 | 2018-08-20 21:47:29 +0000 | [diff] [blame] | 6518 | if (ATL.getAttrAs<LifetimeBoundAttr>()) |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6519 | return true; |
| 6520 | } |
| 6521 | return false; |
| 6522 | } |
| 6523 | |
| 6524 | static void visitLifetimeBoundArguments(IndirectLocalPath &Path, Expr *Call, |
| 6525 | LocalVisitor Visit) { |
| 6526 | const FunctionDecl *Callee; |
| 6527 | ArrayRef<Expr*> Args; |
| 6528 | |
| 6529 | if (auto *CE = dyn_cast<CallExpr>(Call)) { |
| 6530 | Callee = CE->getDirectCallee(); |
| 6531 | Args = llvm::makeArrayRef(CE->getArgs(), CE->getNumArgs()); |
| 6532 | } else { |
| 6533 | auto *CCE = cast<CXXConstructExpr>(Call); |
| 6534 | Callee = CCE->getConstructor(); |
| 6535 | Args = llvm::makeArrayRef(CCE->getArgs(), CCE->getNumArgs()); |
| 6536 | } |
| 6537 | if (!Callee) |
| 6538 | return; |
| 6539 | |
| 6540 | Expr *ObjectArg = nullptr; |
| 6541 | if (isa<CXXOperatorCallExpr>(Call) && Callee->isCXXInstanceMember()) { |
| 6542 | ObjectArg = Args[0]; |
| 6543 | Args = Args.slice(1); |
| 6544 | } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(Call)) { |
| 6545 | ObjectArg = MCE->getImplicitObjectArgument(); |
| 6546 | } |
| 6547 | |
| 6548 | auto VisitLifetimeBoundArg = [&](const Decl *D, Expr *Arg) { |
| 6549 | Path.push_back({IndirectLocalPathEntry::LifetimeBoundCall, Arg, D}); |
| 6550 | if (Arg->isGLValue()) |
| 6551 | visitLocalsRetainedByReferenceBinding(Path, Arg, RK_ReferenceBinding, |
| 6552 | Visit); |
| 6553 | else |
| 6554 | visitLocalsRetainedByInitializer(Path, Arg, Visit, true); |
| 6555 | Path.pop_back(); |
| 6556 | }; |
| 6557 | |
| 6558 | if (ObjectArg && implicitObjectParamIsLifetimeBound(Callee)) |
| 6559 | VisitLifetimeBoundArg(Callee, ObjectArg); |
| 6560 | |
| 6561 | for (unsigned I = 0, |
| 6562 | N = std::min<unsigned>(Callee->getNumParams(), Args.size()); |
| 6563 | I != N; ++I) { |
| 6564 | if (Callee->getParamDecl(I)->hasAttr<LifetimeBoundAttr>()) |
| 6565 | VisitLifetimeBoundArg(Callee->getParamDecl(I), Args[I]); |
| 6566 | } |
| 6567 | } |
| 6568 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6569 | /// Visit the locals that would be reachable through a reference bound to the |
| 6570 | /// glvalue expression \c Init. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6571 | static void visitLocalsRetainedByReferenceBinding(IndirectLocalPath &Path, |
| 6572 | Expr *Init, ReferenceKind RK, |
| 6573 | LocalVisitor Visit) { |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6574 | RevertToOldSizeRAII RAII(Path); |
| 6575 | |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 6576 | // Walk past any constructs which we can lifetime-extend across. |
| 6577 | Expr *Old; |
| 6578 | do { |
| 6579 | Old = Init; |
| 6580 | |
Bill Wendling | 7c44da2 | 2018-10-31 03:48:47 +0000 | [diff] [blame] | 6581 | if (auto *FE = dyn_cast<FullExpr>(Init)) |
| 6582 | Init = FE->getSubExpr(); |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6583 | |
Richard Smith | dbc8249 | 2015-01-10 01:28:13 +0000 | [diff] [blame] | 6584 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6585 | // If this is just redundant braces around an initializer, step over it. |
| 6586 | if (ILE->isTransparent()) |
Richard Smith | dbc8249 | 2015-01-10 01:28:13 +0000 | [diff] [blame] | 6587 | Init = ILE->getInit(0); |
Richard Smith | dbc8249 | 2015-01-10 01:28:13 +0000 | [diff] [blame] | 6588 | } |
| 6589 | |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 6590 | // Step over any subobject adjustments; we may have a materialized |
| 6591 | // temporary inside them. |
Richard Smith | 4baaa5a | 2016-12-03 01:14:32 +0000 | [diff] [blame] | 6592 | Init = const_cast<Expr *>(Init->skipRValueSubobjectAdjustments()); |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 6593 | |
| 6594 | // Per current approach for DR1376, look through casts to reference type |
| 6595 | // when performing lifetime extension. |
| 6596 | if (CastExpr *CE = dyn_cast<CastExpr>(Init)) |
| 6597 | if (CE->getSubExpr()->isGLValue()) |
| 6598 | Init = CE->getSubExpr(); |
| 6599 | |
Richard Smith | b3189a1 | 2016-12-05 07:49:14 +0000 | [diff] [blame] | 6600 | // Per the current approach for DR1299, look through array element access |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6601 | // on array glvalues when performing lifetime extension. |
| 6602 | if (auto *ASE = dyn_cast<ArraySubscriptExpr>(Init)) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6603 | Init = ASE->getBase(); |
| 6604 | auto *ICE = dyn_cast<ImplicitCastExpr>(Init); |
| 6605 | if (ICE && ICE->getCastKind() == CK_ArrayToPointerDecay) |
| 6606 | Init = ICE->getSubExpr(); |
| 6607 | else |
| 6608 | // We can't lifetime extend through this but we might still find some |
| 6609 | // retained temporaries. |
| 6610 | return visitLocalsRetainedByInitializer(Path, Init, Visit, true); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6611 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6612 | |
| 6613 | // Step into CXXDefaultInitExprs so we can diagnose cases where a |
| 6614 | // constructor inherits one as an implicit mem-initializer. |
| 6615 | if (auto *DIE = dyn_cast<CXXDefaultInitExpr>(Init)) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6616 | Path.push_back( |
| 6617 | {IndirectLocalPathEntry::DefaultInit, DIE, DIE->getField()}); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6618 | Init = DIE->getExpr(); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6619 | } |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 6620 | } while (Init != Old); |
| 6621 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6622 | if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(Init)) { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6623 | if (Visit(Path, Local(MTE), RK)) |
| 6624 | visitLocalsRetainedByInitializer(Path, MTE->GetTemporaryExpr(), Visit, |
| 6625 | true); |
| 6626 | } |
| 6627 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6628 | if (isa<CallExpr>(Init)) |
| 6629 | return visitLifetimeBoundArguments(Path, Init, Visit); |
| 6630 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6631 | switch (Init->getStmtClass()) { |
| 6632 | case Stmt::DeclRefExprClass: { |
| 6633 | // If we find the name of a local non-reference parameter, we could have a |
| 6634 | // lifetime problem. |
| 6635 | auto *DRE = cast<DeclRefExpr>(Init); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6636 | auto *VD = dyn_cast<VarDecl>(DRE->getDecl()); |
| 6637 | if (VD && VD->hasLocalStorage() && |
| 6638 | !DRE->refersToEnclosingVariableOrCapture()) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6639 | if (!VD->getType()->isReferenceType()) { |
| 6640 | Visit(Path, Local(DRE), RK); |
| 6641 | } else if (isa<ParmVarDecl>(DRE->getDecl())) { |
| 6642 | // The lifetime of a reference parameter is unknown; assume it's OK |
| 6643 | // for now. |
| 6644 | break; |
| 6645 | } else if (VD->getInit() && !isVarOnPath(Path, VD)) { |
| 6646 | Path.push_back({IndirectLocalPathEntry::VarInit, DRE, VD}); |
| 6647 | visitLocalsRetainedByReferenceBinding(Path, VD->getInit(), |
| 6648 | RK_ReferenceBinding, Visit); |
| 6649 | } |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6650 | } |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6651 | break; |
| 6652 | } |
| 6653 | |
| 6654 | case Stmt::UnaryOperatorClass: { |
| 6655 | // The only unary operator that make sense to handle here |
| 6656 | // is Deref. All others don't resolve to a "name." This includes |
| 6657 | // handling all sorts of rvalues passed to a unary operator. |
| 6658 | const UnaryOperator *U = cast<UnaryOperator>(Init); |
| 6659 | if (U->getOpcode() == UO_Deref) |
| 6660 | visitLocalsRetainedByInitializer(Path, U->getSubExpr(), Visit, true); |
| 6661 | break; |
| 6662 | } |
| 6663 | |
| 6664 | case Stmt::OMPArraySectionExprClass: { |
| 6665 | visitLocalsRetainedByInitializer( |
| 6666 | Path, cast<OMPArraySectionExpr>(Init)->getBase(), Visit, true); |
| 6667 | break; |
| 6668 | } |
| 6669 | |
| 6670 | case Stmt::ConditionalOperatorClass: |
| 6671 | case Stmt::BinaryConditionalOperatorClass: { |
| 6672 | auto *C = cast<AbstractConditionalOperator>(Init); |
| 6673 | if (!C->getTrueExpr()->getType()->isVoidType()) |
| 6674 | visitLocalsRetainedByReferenceBinding(Path, C->getTrueExpr(), RK, Visit); |
| 6675 | if (!C->getFalseExpr()->getType()->isVoidType()) |
| 6676 | visitLocalsRetainedByReferenceBinding(Path, C->getFalseExpr(), RK, Visit); |
| 6677 | break; |
| 6678 | } |
| 6679 | |
| 6680 | // FIXME: Visit the left-hand side of an -> or ->*. |
| 6681 | |
| 6682 | default: |
| 6683 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6684 | } |
| 6685 | } |
| 6686 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6687 | /// Visit the locals that would be reachable through an object initialized by |
| 6688 | /// the prvalue expression \c Init. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6689 | static void visitLocalsRetainedByInitializer(IndirectLocalPath &Path, |
| 6690 | Expr *Init, LocalVisitor Visit, |
| 6691 | bool RevisitSubinits) { |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6692 | RevertToOldSizeRAII RAII(Path); |
| 6693 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6694 | Expr *Old; |
| 6695 | do { |
| 6696 | Old = Init; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6697 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6698 | // Step into CXXDefaultInitExprs so we can diagnose cases where a |
| 6699 | // constructor inherits one as an implicit mem-initializer. |
| 6700 | if (auto *DIE = dyn_cast<CXXDefaultInitExpr>(Init)) { |
| 6701 | Path.push_back({IndirectLocalPathEntry::DefaultInit, DIE, DIE->getField()}); |
| 6702 | Init = DIE->getExpr(); |
| 6703 | } |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6704 | |
Bill Wendling | 7c44da2 | 2018-10-31 03:48:47 +0000 | [diff] [blame] | 6705 | if (auto *FE = dyn_cast<FullExpr>(Init)) |
| 6706 | Init = FE->getSubExpr(); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6707 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6708 | // Dig out the expression which constructs the extended temporary. |
| 6709 | Init = const_cast<Expr *>(Init->skipRValueSubobjectAdjustments()); |
| 6710 | |
| 6711 | if (CXXBindTemporaryExpr *BTE = dyn_cast<CXXBindTemporaryExpr>(Init)) |
| 6712 | Init = BTE->getSubExpr(); |
| 6713 | |
| 6714 | Init = Init->IgnoreParens(); |
| 6715 | |
| 6716 | // Step over value-preserving rvalue casts. |
| 6717 | if (auto *CE = dyn_cast<CastExpr>(Init)) { |
| 6718 | switch (CE->getCastKind()) { |
| 6719 | case CK_LValueToRValue: |
| 6720 | // If we can match the lvalue to a const object, we can look at its |
| 6721 | // initializer. |
| 6722 | Path.push_back({IndirectLocalPathEntry::LValToRVal, CE}); |
| 6723 | return visitLocalsRetainedByReferenceBinding( |
| 6724 | Path, Init, RK_ReferenceBinding, |
| 6725 | [&](IndirectLocalPath &Path, Local L, ReferenceKind RK) -> bool { |
| 6726 | if (auto *DRE = dyn_cast<DeclRefExpr>(L)) { |
| 6727 | auto *VD = dyn_cast<VarDecl>(DRE->getDecl()); |
| 6728 | if (VD && VD->getType().isConstQualified() && VD->getInit() && |
| 6729 | !isVarOnPath(Path, VD)) { |
| 6730 | Path.push_back({IndirectLocalPathEntry::VarInit, DRE, VD}); |
| 6731 | visitLocalsRetainedByInitializer(Path, VD->getInit(), Visit, true); |
| 6732 | } |
| 6733 | } else if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(L)) { |
| 6734 | if (MTE->getType().isConstQualified()) |
| 6735 | visitLocalsRetainedByInitializer(Path, MTE->GetTemporaryExpr(), |
| 6736 | Visit, true); |
| 6737 | } |
| 6738 | return false; |
| 6739 | }); |
| 6740 | |
| 6741 | // We assume that objects can be retained by pointers cast to integers, |
| 6742 | // but not if the integer is cast to floating-point type or to _Complex. |
| 6743 | // We assume that casts to 'bool' do not preserve enough information to |
| 6744 | // retain a local object. |
| 6745 | case CK_NoOp: |
| 6746 | case CK_BitCast: |
| 6747 | case CK_BaseToDerived: |
| 6748 | case CK_DerivedToBase: |
| 6749 | case CK_UncheckedDerivedToBase: |
| 6750 | case CK_Dynamic: |
| 6751 | case CK_ToUnion: |
| 6752 | case CK_UserDefinedConversion: |
| 6753 | case CK_ConstructorConversion: |
| 6754 | case CK_IntegralToPointer: |
| 6755 | case CK_PointerToIntegral: |
| 6756 | case CK_VectorSplat: |
| 6757 | case CK_IntegralCast: |
| 6758 | case CK_CPointerToObjCPointerCast: |
| 6759 | case CK_BlockPointerToObjCPointerCast: |
| 6760 | case CK_AnyPointerToBlockPointerCast: |
| 6761 | case CK_AddressSpaceConversion: |
| 6762 | break; |
| 6763 | |
| 6764 | case CK_ArrayToPointerDecay: |
| 6765 | // Model array-to-pointer decay as taking the address of the array |
| 6766 | // lvalue. |
| 6767 | Path.push_back({IndirectLocalPathEntry::AddressOf, CE}); |
| 6768 | return visitLocalsRetainedByReferenceBinding(Path, CE->getSubExpr(), |
| 6769 | RK_ReferenceBinding, Visit); |
| 6770 | |
| 6771 | default: |
| 6772 | return; |
| 6773 | } |
| 6774 | |
| 6775 | Init = CE->getSubExpr(); |
| 6776 | } |
| 6777 | } while (Old != Init); |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 6778 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6779 | // C++17 [dcl.init.list]p6: |
| 6780 | // initializing an initializer_list object from the array extends the |
| 6781 | // lifetime of the array exactly like binding a reference to a temporary. |
| 6782 | if (auto *ILE = dyn_cast<CXXStdInitializerListExpr>(Init)) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6783 | return visitLocalsRetainedByReferenceBinding(Path, ILE->getSubExpr(), |
| 6784 | RK_StdInitializerList, Visit); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6785 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6786 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6787 | // We already visited the elements of this initializer list while |
| 6788 | // performing the initialization. Don't visit them again unless we've |
| 6789 | // changed the lifetime of the initialized entity. |
| 6790 | if (!RevisitSubinits) |
| 6791 | return; |
| 6792 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6793 | if (ILE->isTransparent()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6794 | return visitLocalsRetainedByInitializer(Path, ILE->getInit(0), Visit, |
| 6795 | RevisitSubinits); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6796 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6797 | if (ILE->getType()->isArrayType()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6798 | for (unsigned I = 0, N = ILE->getNumInits(); I != N; ++I) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6799 | visitLocalsRetainedByInitializer(Path, ILE->getInit(I), Visit, |
| 6800 | RevisitSubinits); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6801 | return; |
| 6802 | } |
| 6803 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6804 | if (CXXRecordDecl *RD = ILE->getType()->getAsCXXRecordDecl()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6805 | assert(RD->isAggregate() && "aggregate init on non-aggregate"); |
| 6806 | |
| 6807 | // If we lifetime-extend a braced initializer which is initializing an |
| 6808 | // aggregate, and that aggregate contains reference members which are |
| 6809 | // bound to temporaries, those temporaries are also lifetime-extended. |
| 6810 | if (RD->isUnion() && ILE->getInitializedFieldInUnion() && |
| 6811 | ILE->getInitializedFieldInUnion()->getType()->isReferenceType()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6812 | visitLocalsRetainedByReferenceBinding(Path, ILE->getInit(0), |
| 6813 | RK_ReferenceBinding, Visit); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6814 | else { |
| 6815 | unsigned Index = 0; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6816 | for (const auto *I : RD->fields()) { |
Richard Smith | 0bca59d | 2013-07-01 06:08:20 +0000 | [diff] [blame] | 6817 | if (Index >= ILE->getNumInits()) |
| 6818 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6819 | if (I->isUnnamedBitfield()) |
| 6820 | continue; |
Richard Smith | 8d7f11d | 2013-06-27 22:54:33 +0000 | [diff] [blame] | 6821 | Expr *SubInit = ILE->getInit(Index); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6822 | if (I->getType()->isReferenceType()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6823 | visitLocalsRetainedByReferenceBinding(Path, SubInit, |
| 6824 | RK_ReferenceBinding, Visit); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6825 | else |
| 6826 | // This might be either aggregate-initialization of a member or |
| 6827 | // initialization of a std::initializer_list object. Regardless, |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6828 | // we should recursively lifetime-extend that initializer. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6829 | visitLocalsRetainedByInitializer(Path, SubInit, Visit, |
| 6830 | RevisitSubinits); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6831 | ++Index; |
| 6832 | } |
| 6833 | } |
| 6834 | } |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6835 | return; |
| 6836 | } |
| 6837 | |
Richard Smith | b3d203f | 2018-10-19 19:01:34 +0000 | [diff] [blame] | 6838 | // The lifetime of an init-capture is that of the closure object constructed |
| 6839 | // by a lambda-expression. |
| 6840 | if (auto *LE = dyn_cast<LambdaExpr>(Init)) { |
| 6841 | for (Expr *E : LE->capture_inits()) { |
| 6842 | if (!E) |
| 6843 | continue; |
| 6844 | if (E->isGLValue()) |
| 6845 | visitLocalsRetainedByReferenceBinding(Path, E, RK_ReferenceBinding, |
| 6846 | Visit); |
| 6847 | else |
| 6848 | visitLocalsRetainedByInitializer(Path, E, Visit, true); |
| 6849 | } |
| 6850 | } |
| 6851 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6852 | if (isa<CallExpr>(Init) || isa<CXXConstructExpr>(Init)) |
| 6853 | return visitLifetimeBoundArguments(Path, Init, Visit); |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6854 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6855 | switch (Init->getStmtClass()) { |
| 6856 | case Stmt::UnaryOperatorClass: { |
| 6857 | auto *UO = cast<UnaryOperator>(Init); |
| 6858 | // If the initializer is the address of a local, we could have a lifetime |
| 6859 | // problem. |
| 6860 | if (UO->getOpcode() == UO_AddrOf) { |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6861 | // If this is &rvalue, then it's ill-formed and we have already diagnosed |
| 6862 | // it. Don't produce a redundant warning about the lifetime of the |
| 6863 | // temporary. |
| 6864 | if (isa<MaterializeTemporaryExpr>(UO->getSubExpr())) |
| 6865 | return; |
| 6866 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6867 | Path.push_back({IndirectLocalPathEntry::AddressOf, UO}); |
| 6868 | visitLocalsRetainedByReferenceBinding(Path, UO->getSubExpr(), |
| 6869 | RK_ReferenceBinding, Visit); |
| 6870 | } |
| 6871 | break; |
| 6872 | } |
| 6873 | |
| 6874 | case Stmt::BinaryOperatorClass: { |
| 6875 | // Handle pointer arithmetic. |
| 6876 | auto *BO = cast<BinaryOperator>(Init); |
| 6877 | BinaryOperatorKind BOK = BO->getOpcode(); |
| 6878 | if (!BO->getType()->isPointerType() || (BOK != BO_Add && BOK != BO_Sub)) |
| 6879 | break; |
| 6880 | |
| 6881 | if (BO->getLHS()->getType()->isPointerType()) |
| 6882 | visitLocalsRetainedByInitializer(Path, BO->getLHS(), Visit, true); |
| 6883 | else if (BO->getRHS()->getType()->isPointerType()) |
| 6884 | visitLocalsRetainedByInitializer(Path, BO->getRHS(), Visit, true); |
| 6885 | break; |
| 6886 | } |
| 6887 | |
| 6888 | case Stmt::ConditionalOperatorClass: |
| 6889 | case Stmt::BinaryConditionalOperatorClass: { |
| 6890 | auto *C = cast<AbstractConditionalOperator>(Init); |
| 6891 | // In C++, we can have a throw-expression operand, which has 'void' type |
| 6892 | // and isn't interesting from a lifetime perspective. |
| 6893 | if (!C->getTrueExpr()->getType()->isVoidType()) |
| 6894 | visitLocalsRetainedByInitializer(Path, C->getTrueExpr(), Visit, true); |
| 6895 | if (!C->getFalseExpr()->getType()->isVoidType()) |
| 6896 | visitLocalsRetainedByInitializer(Path, C->getFalseExpr(), Visit, true); |
| 6897 | break; |
| 6898 | } |
| 6899 | |
| 6900 | case Stmt::BlockExprClass: |
| 6901 | if (cast<BlockExpr>(Init)->getBlockDecl()->hasCaptures()) { |
| 6902 | // This is a local block, whose lifetime is that of the function. |
| 6903 | Visit(Path, Local(cast<BlockExpr>(Init)), RK_ReferenceBinding); |
| 6904 | } |
| 6905 | break; |
| 6906 | |
| 6907 | case Stmt::AddrLabelExprClass: |
| 6908 | // We want to warn if the address of a label would escape the function. |
| 6909 | Visit(Path, Local(cast<AddrLabelExpr>(Init)), RK_ReferenceBinding); |
| 6910 | break; |
| 6911 | |
| 6912 | default: |
| 6913 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 6914 | } |
| 6915 | } |
| 6916 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6917 | /// Determine whether this is an indirect path to a temporary that we are |
| 6918 | /// supposed to lifetime-extend along (but don't). |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6919 | static bool shouldLifetimeExtendThroughPath(const IndirectLocalPath &Path) { |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6920 | for (auto Elem : Path) { |
Richard Smith | f66e4f7 | 2018-07-23 22:56:45 +0000 | [diff] [blame] | 6921 | if (Elem.Kind != IndirectLocalPathEntry::DefaultInit) |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6922 | return false; |
| 6923 | } |
| 6924 | return true; |
| 6925 | } |
| 6926 | |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 6927 | /// Find the range for the first interesting entry in the path at or after I. |
| 6928 | static SourceRange nextPathEntryRange(const IndirectLocalPath &Path, unsigned I, |
| 6929 | Expr *E) { |
| 6930 | for (unsigned N = Path.size(); I != N; ++I) { |
| 6931 | switch (Path[I].Kind) { |
| 6932 | case IndirectLocalPathEntry::AddressOf: |
| 6933 | case IndirectLocalPathEntry::LValToRVal: |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6934 | case IndirectLocalPathEntry::LifetimeBoundCall: |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 6935 | // These exist primarily to mark the path as not permitting or |
| 6936 | // supporting lifetime extension. |
| 6937 | break; |
| 6938 | |
| 6939 | case IndirectLocalPathEntry::DefaultInit: |
| 6940 | case IndirectLocalPathEntry::VarInit: |
| 6941 | return Path[I].E->getSourceRange(); |
| 6942 | } |
| 6943 | } |
| 6944 | return E->getSourceRange(); |
| 6945 | } |
| 6946 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6947 | void Sema::checkInitializerLifetime(const InitializedEntity &Entity, |
| 6948 | Expr *Init) { |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6949 | LifetimeResult LR = getEntityLifetime(&Entity); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6950 | LifetimeKind LK = LR.getInt(); |
| 6951 | const InitializedEntity *ExtendingEntity = LR.getPointer(); |
| 6952 | |
| 6953 | // If this entity doesn't have an interesting lifetime, don't bother looking |
| 6954 | // for temporaries within its initializer. |
| 6955 | if (LK == LK_FullExpression) |
| 6956 | return; |
| 6957 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6958 | auto TemporaryVisitor = [&](IndirectLocalPath &Path, Local L, |
| 6959 | ReferenceKind RK) -> bool { |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 6960 | SourceRange DiagRange = nextPathEntryRange(Path, 0, L); |
| 6961 | SourceLocation DiagLoc = DiagRange.getBegin(); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 6962 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6963 | switch (LK) { |
| 6964 | case LK_FullExpression: |
| 6965 | llvm_unreachable("already handled this"); |
| 6966 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6967 | case LK_Extended: { |
| 6968 | auto *MTE = dyn_cast<MaterializeTemporaryExpr>(L); |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6969 | if (!MTE) { |
| 6970 | // The initialized entity has lifetime beyond the full-expression, |
| 6971 | // and the local entity does too, so don't warn. |
| 6972 | // |
| 6973 | // FIXME: We should consider warning if a static / thread storage |
| 6974 | // duration variable retains an automatic storage duration local. |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6975 | return false; |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6976 | } |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 6977 | |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6978 | // Lifetime-extend the temporary. |
| 6979 | if (Path.empty()) { |
| 6980 | // Update the storage duration of the materialized temporary. |
| 6981 | // FIXME: Rebuild the expression instead of mutating it. |
| 6982 | MTE->setExtendingDecl(ExtendingEntity->getDecl(), |
| 6983 | ExtendingEntity->allocateManglingNumber()); |
| 6984 | // Also visit the temporaries lifetime-extended by this initializer. |
| 6985 | return true; |
| 6986 | } |
| 6987 | |
| 6988 | if (shouldLifetimeExtendThroughPath(Path)) { |
| 6989 | // We're supposed to lifetime-extend the temporary along this path (per |
| 6990 | // the resolution of DR1815), but we don't support that yet. |
| 6991 | // |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6992 | // FIXME: Properly handle this situation. Perhaps the easiest approach |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6993 | // would be to clone the initializer expression on each use that would |
| 6994 | // lifetime extend its temporaries. |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6995 | Diag(DiagLoc, diag::warn_unsupported_lifetime_extension) |
| 6996 | << RK << DiagRange; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 6997 | } else { |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 6998 | // If the path goes through the initialization of a variable or field, |
| 6999 | // it can't possibly reach a temporary created in this full-expression. |
| 7000 | // We will have already diagnosed any problems with the initializer. |
| 7001 | if (pathContainsInit(Path)) |
| 7002 | return false; |
| 7003 | |
| 7004 | Diag(DiagLoc, diag::warn_dangling_variable) |
Richard Smith | ad5bbcc | 2018-08-01 01:03:33 +0000 | [diff] [blame] | 7005 | << RK << !Entity.getParent() |
| 7006 | << ExtendingEntity->getDecl()->isImplicit() |
| 7007 | << ExtendingEntity->getDecl() << Init->isGLValue() << DiagRange; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7008 | } |
| 7009 | break; |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7010 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7011 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7012 | case LK_MemInitializer: { |
George Burgess IV | 06df229 | 2018-07-24 02:10:53 +0000 | [diff] [blame] | 7013 | if (isa<MaterializeTemporaryExpr>(L)) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7014 | // Under C++ DR1696, if a mem-initializer (or a default member |
| 7015 | // initializer used by the absence of one) would lifetime-extend a |
| 7016 | // temporary, the program is ill-formed. |
| 7017 | if (auto *ExtendingDecl = |
| 7018 | ExtendingEntity ? ExtendingEntity->getDecl() : nullptr) { |
| 7019 | bool IsSubobjectMember = ExtendingEntity != &Entity; |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 7020 | Diag(DiagLoc, shouldLifetimeExtendThroughPath(Path) |
| 7021 | ? diag::err_dangling_member |
| 7022 | : diag::warn_dangling_member) |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7023 | << ExtendingDecl << IsSubobjectMember << RK << DiagRange; |
| 7024 | // Don't bother adding a note pointing to the field if we're inside |
| 7025 | // its default member initializer; our primary diagnostic points to |
| 7026 | // the same place in that case. |
| 7027 | if (Path.empty() || |
| 7028 | Path.back().Kind != IndirectLocalPathEntry::DefaultInit) { |
| 7029 | Diag(ExtendingDecl->getLocation(), |
| 7030 | diag::note_lifetime_extending_member_declared_here) |
| 7031 | << RK << IsSubobjectMember; |
| 7032 | } |
| 7033 | } else { |
| 7034 | // We have a mem-initializer but no particular field within it; this |
| 7035 | // is either a base class or a delegating initializer directly |
| 7036 | // initializing the base-class from something that doesn't live long |
| 7037 | // enough. |
| 7038 | // |
| 7039 | // FIXME: Warn on this. |
| 7040 | return false; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7041 | } |
| 7042 | } else { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7043 | // Paths via a default initializer can only occur during error recovery |
| 7044 | // (there's no other way that a default initializer can refer to a |
| 7045 | // local). Don't produce a bogus warning on those cases. |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 7046 | if (pathContainsInit(Path)) |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7047 | return false; |
| 7048 | |
| 7049 | auto *DRE = dyn_cast<DeclRefExpr>(L); |
| 7050 | auto *VD = DRE ? dyn_cast<VarDecl>(DRE->getDecl()) : nullptr; |
| 7051 | if (!VD) { |
| 7052 | // A member was initialized to a local block. |
| 7053 | // FIXME: Warn on this. |
| 7054 | return false; |
| 7055 | } |
| 7056 | |
| 7057 | if (auto *Member = |
| 7058 | ExtendingEntity ? ExtendingEntity->getDecl() : nullptr) { |
| 7059 | bool IsPointer = Member->getType()->isAnyPointerType(); |
| 7060 | Diag(DiagLoc, IsPointer ? diag::warn_init_ptr_member_to_parameter_addr |
| 7061 | : diag::warn_bind_ref_member_to_parameter) |
| 7062 | << Member << VD << isa<ParmVarDecl>(VD) << DiagRange; |
| 7063 | Diag(Member->getLocation(), |
| 7064 | diag::note_ref_or_ptr_member_declared_here) |
| 7065 | << (unsigned)IsPointer; |
| 7066 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7067 | } |
| 7068 | break; |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7069 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7070 | |
| 7071 | case LK_New: |
George Burgess IV | 06df229 | 2018-07-24 02:10:53 +0000 | [diff] [blame] | 7072 | if (isa<MaterializeTemporaryExpr>(L)) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7073 | Diag(DiagLoc, RK == RK_ReferenceBinding |
| 7074 | ? diag::warn_new_dangling_reference |
| 7075 | : diag::warn_new_dangling_initializer_list) |
Richard Smith | 0e3102d | 2018-07-24 00:55:08 +0000 | [diff] [blame] | 7076 | << !Entity.getParent() << DiagRange; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7077 | } else { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7078 | // We can't determine if the allocation outlives the local declaration. |
| 7079 | return false; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7080 | } |
| 7081 | break; |
| 7082 | |
| 7083 | case LK_Return: |
Richard Smith | 67af95b | 2018-07-23 19:19:08 +0000 | [diff] [blame] | 7084 | case LK_StmtExprResult: |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7085 | if (auto *DRE = dyn_cast<DeclRefExpr>(L)) { |
| 7086 | // We can't determine if the local variable outlives the statement |
| 7087 | // expression. |
| 7088 | if (LK == LK_StmtExprResult) |
| 7089 | return false; |
| 7090 | Diag(DiagLoc, diag::warn_ret_stack_addr_ref) |
| 7091 | << Entity.getType()->isReferenceType() << DRE->getDecl() |
| 7092 | << isa<ParmVarDecl>(DRE->getDecl()) << DiagRange; |
| 7093 | } else if (isa<BlockExpr>(L)) { |
| 7094 | Diag(DiagLoc, diag::err_ret_local_block) << DiagRange; |
| 7095 | } else if (isa<AddrLabelExpr>(L)) { |
Reid Kleckner | 4c33d19 | 2018-08-17 22:11:31 +0000 | [diff] [blame] | 7096 | // Don't warn when returning a label from a statement expression. |
| 7097 | // Leaving the scope doesn't end its lifetime. |
| 7098 | if (LK == LK_StmtExprResult) |
| 7099 | return false; |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7100 | Diag(DiagLoc, diag::warn_ret_addr_label) << DiagRange; |
| 7101 | } else { |
| 7102 | Diag(DiagLoc, diag::warn_ret_local_temp_addr_ref) |
| 7103 | << Entity.getType()->isReferenceType() << DiagRange; |
| 7104 | } |
| 7105 | break; |
Florian Hahn | 0aa117d | 2018-07-17 09:23:31 +0000 | [diff] [blame] | 7106 | } |
| 7107 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7108 | for (unsigned I = 0; I != Path.size(); ++I) { |
| 7109 | auto Elem = Path[I]; |
| 7110 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7111 | switch (Elem.Kind) { |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7112 | case IndirectLocalPathEntry::AddressOf: |
| 7113 | case IndirectLocalPathEntry::LValToRVal: |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 7114 | // These exist primarily to mark the path as not permitting or |
| 7115 | // supporting lifetime extension. |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7116 | break; |
| 7117 | |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 7118 | case IndirectLocalPathEntry::LifetimeBoundCall: |
| 7119 | // FIXME: Consider adding a note for this. |
| 7120 | break; |
| 7121 | |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7122 | case IndirectLocalPathEntry::DefaultInit: { |
| 7123 | auto *FD = cast<FieldDecl>(Elem.D); |
| 7124 | Diag(FD->getLocation(), diag::note_init_with_default_member_initalizer) |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 7125 | << FD << nextPathEntryRange(Path, I + 1, L); |
Richard Smith | afe48f9 | 2018-07-23 21:21:22 +0000 | [diff] [blame] | 7126 | break; |
| 7127 | } |
| 7128 | |
| 7129 | case IndirectLocalPathEntry::VarInit: |
| 7130 | const VarDecl *VD = cast<VarDecl>(Elem.D); |
| 7131 | Diag(VD->getLocation(), diag::note_local_var_initializer) |
Richard Smith | ad5bbcc | 2018-08-01 01:03:33 +0000 | [diff] [blame] | 7132 | << VD->getType()->isReferenceType() |
| 7133 | << VD->isImplicit() << VD->getDeclName() |
Richard Smith | 6a32c05 | 2018-07-23 21:21:24 +0000 | [diff] [blame] | 7134 | << nextPathEntryRange(Path, I + 1, L); |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7135 | break; |
Florian Hahn | 0aa117d | 2018-07-17 09:23:31 +0000 | [diff] [blame] | 7136 | } |
| 7137 | } |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7138 | |
| 7139 | // We didn't lifetime-extend, so don't go any further; we don't need more |
| 7140 | // warnings or errors on inner temporaries within this one's initializer. |
| 7141 | return false; |
| 7142 | }; |
| 7143 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7144 | llvm::SmallVector<IndirectLocalPathEntry, 8> Path; |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7145 | if (Init->isGLValue()) |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7146 | visitLocalsRetainedByReferenceBinding(Path, Init, RK_ReferenceBinding, |
| 7147 | TemporaryVisitor); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7148 | else |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 7149 | visitLocalsRetainedByInitializer(Path, Init, TemporaryVisitor, false); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 7150 | } |
| 7151 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7152 | static void DiagnoseNarrowingInInitList(Sema &S, |
| 7153 | const ImplicitConversionSequence &ICS, |
| 7154 | QualType PreNarrowingType, |
| 7155 | QualType EntityType, |
| 7156 | const Expr *PostInit); |
| 7157 | |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7158 | /// Provide warnings when std::move is used on construction. |
| 7159 | static void CheckMoveOnConstruction(Sema &S, const Expr *InitExpr, |
| 7160 | bool IsReturnStmt) { |
| 7161 | if (!InitExpr) |
| 7162 | return; |
| 7163 | |
Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 7164 | if (S.inTemplateInstantiation()) |
Richard Trieu | 6093d14 | 2015-07-29 17:03:34 +0000 | [diff] [blame] | 7165 | return; |
| 7166 | |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7167 | QualType DestType = InitExpr->getType(); |
| 7168 | if (!DestType->isRecordType()) |
| 7169 | return; |
| 7170 | |
| 7171 | unsigned DiagID = 0; |
| 7172 | if (IsReturnStmt) { |
| 7173 | const CXXConstructExpr *CCE = |
| 7174 | dyn_cast<CXXConstructExpr>(InitExpr->IgnoreParens()); |
| 7175 | if (!CCE || CCE->getNumArgs() != 1) |
| 7176 | return; |
| 7177 | |
| 7178 | if (!CCE->getConstructor()->isCopyOrMoveConstructor()) |
| 7179 | return; |
| 7180 | |
| 7181 | InitExpr = CCE->getArg(0)->IgnoreImpCasts(); |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7182 | } |
| 7183 | |
| 7184 | // Find the std::move call and get the argument. |
| 7185 | const CallExpr *CE = dyn_cast<CallExpr>(InitExpr->IgnoreParens()); |
Nico Weber | 192184c | 2018-06-20 15:57:38 +0000 | [diff] [blame] | 7186 | if (!CE || !CE->isCallToStdMove()) |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7187 | return; |
| 7188 | |
| 7189 | const Expr *Arg = CE->getArg(0)->IgnoreImplicit(); |
| 7190 | |
| 7191 | if (IsReturnStmt) { |
| 7192 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg->IgnoreParenImpCasts()); |
| 7193 | if (!DRE || DRE->refersToEnclosingVariableOrCapture()) |
| 7194 | return; |
| 7195 | |
| 7196 | const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()); |
| 7197 | if (!VD || !VD->hasLocalStorage()) |
| 7198 | return; |
| 7199 | |
Alex Lorenz | bbe51d8 | 2017-11-07 21:40:11 +0000 | [diff] [blame] | 7200 | // __block variables are not moved implicitly. |
| 7201 | if (VD->hasAttr<BlocksAttr>()) |
| 7202 | return; |
| 7203 | |
Richard Trieu | 8d4006a | 2015-07-28 19:06:16 +0000 | [diff] [blame] | 7204 | QualType SourceType = VD->getType(); |
| 7205 | if (!SourceType->isRecordType()) |
Richard Trieu | 1d4911bc | 2015-05-18 19:54:08 +0000 | [diff] [blame] | 7206 | return; |
| 7207 | |
Richard Trieu | 8d4006a | 2015-07-28 19:06:16 +0000 | [diff] [blame] | 7208 | if (!S.Context.hasSameUnqualifiedType(DestType, SourceType)) { |
Richard Trieu | 1993dc8 | 2015-07-29 23:47:19 +0000 | [diff] [blame] | 7209 | return; |
Richard Trieu | 8d4006a | 2015-07-28 19:06:16 +0000 | [diff] [blame] | 7210 | } |
| 7211 | |
Davide Italiano | 7842c3f | 2015-07-18 01:15:19 +0000 | [diff] [blame] | 7212 | // If we're returning a function parameter, copy elision |
| 7213 | // is not possible. |
| 7214 | if (isa<ParmVarDecl>(VD)) |
| 7215 | DiagID = diag::warn_redundant_move_on_return; |
Richard Trieu | 1993dc8 | 2015-07-29 23:47:19 +0000 | [diff] [blame] | 7216 | else |
| 7217 | DiagID = diag::warn_pessimizing_move_on_return; |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7218 | } else { |
| 7219 | DiagID = diag::warn_pessimizing_move_on_initialization; |
| 7220 | const Expr *ArgStripped = Arg->IgnoreImplicit()->IgnoreParens(); |
| 7221 | if (!ArgStripped->isRValue() || !ArgStripped->getType()->isRecordType()) |
| 7222 | return; |
| 7223 | } |
| 7224 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7225 | S.Diag(CE->getBeginLoc(), DiagID); |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7226 | |
| 7227 | // Get all the locations for a fix-it. Don't emit the fix-it if any location |
| 7228 | // is within a macro. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7229 | SourceLocation CallBegin = CE->getCallee()->getBeginLoc(); |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7230 | if (CallBegin.isMacroID()) |
| 7231 | return; |
| 7232 | SourceLocation RParen = CE->getRParenLoc(); |
| 7233 | if (RParen.isMacroID()) |
| 7234 | return; |
| 7235 | SourceLocation LParen; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7236 | SourceLocation ArgLoc = Arg->getBeginLoc(); |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7237 | |
| 7238 | // Special testing for the argument location. Since the fix-it needs the |
| 7239 | // location right before the argument, the argument location can be in a |
| 7240 | // macro only if it is at the beginning of the macro. |
| 7241 | while (ArgLoc.isMacroID() && |
| 7242 | S.getSourceManager().isAtStartOfImmediateMacroExpansion(ArgLoc)) { |
Richard Smith | b5f8171 | 2018-04-30 05:25:48 +0000 | [diff] [blame] | 7243 | ArgLoc = S.getSourceManager().getImmediateExpansionRange(ArgLoc).getBegin(); |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7244 | } |
| 7245 | |
| 7246 | if (LParen.isMacroID()) |
| 7247 | return; |
| 7248 | |
| 7249 | LParen = ArgLoc.getLocWithOffset(-1); |
| 7250 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7251 | S.Diag(CE->getBeginLoc(), diag::note_remove_move) |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 7252 | << FixItHint::CreateRemoval(SourceRange(CallBegin, LParen)) |
| 7253 | << FixItHint::CreateRemoval(SourceRange(RParen, RParen)); |
| 7254 | } |
| 7255 | |
Nick Lewycky | 2eeddfb | 2016-05-14 17:44:14 +0000 | [diff] [blame] | 7256 | static void CheckForNullPointerDereference(Sema &S, const Expr *E) { |
| 7257 | // Check to see if we are dereferencing a null pointer. If so, this is |
| 7258 | // undefined behavior, so warn about it. This only handles the pattern |
| 7259 | // "*null", which is a very syntactic check. |
| 7260 | if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E->IgnoreParenCasts())) |
| 7261 | if (UO->getOpcode() == UO_Deref && |
| 7262 | UO->getSubExpr()->IgnoreParenCasts()-> |
| 7263 | isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull)) { |
| 7264 | S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
| 7265 | S.PDiag(diag::warn_binding_null_to_reference) |
| 7266 | << UO->getSubExpr()->getSourceRange()); |
| 7267 | } |
| 7268 | } |
| 7269 | |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 7270 | MaterializeTemporaryExpr * |
| 7271 | Sema::CreateMaterializeTemporaryExpr(QualType T, Expr *Temporary, |
| 7272 | bool BoundToLvalueReference) { |
| 7273 | auto MTE = new (Context) |
| 7274 | MaterializeTemporaryExpr(T, Temporary, BoundToLvalueReference); |
| 7275 | |
| 7276 | // Order an ExprWithCleanups for lifetime marks. |
| 7277 | // |
| 7278 | // TODO: It'll be good to have a single place to check the access of the |
| 7279 | // destructor and generate ExprWithCleanups for various uses. Currently these |
| 7280 | // are done in both CreateMaterializeTemporaryExpr and MaybeBindToTemporary, |
| 7281 | // but there may be a chance to merge them. |
| 7282 | Cleanup.setExprNeedsCleanups(false); |
| 7283 | return MTE; |
| 7284 | } |
| 7285 | |
Richard Smith | 4baaa5a | 2016-12-03 01:14:32 +0000 | [diff] [blame] | 7286 | ExprResult Sema::TemporaryMaterializationConversion(Expr *E) { |
| 7287 | // In C++98, we don't want to implicitly create an xvalue. |
| 7288 | // FIXME: This means that AST consumers need to deal with "prvalues" that |
| 7289 | // denote materialized temporaries. Maybe we should add another ValueKind |
| 7290 | // for "xvalue pretending to be a prvalue" for C++98 support. |
| 7291 | if (!E->isRValue() || !getLangOpts().CPlusPlus11) |
| 7292 | return E; |
| 7293 | |
| 7294 | // C++1z [conv.rval]/1: T shall be a complete type. |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7295 | // FIXME: Does this ever matter (can we form a prvalue of incomplete type)? |
| 7296 | // 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] | 7297 | QualType T = E->getType(); |
| 7298 | if (RequireCompleteType(E->getExprLoc(), T, diag::err_incomplete_type)) |
| 7299 | return ExprError(); |
| 7300 | |
| 7301 | return CreateMaterializeTemporaryExpr(E->getType(), E, false); |
| 7302 | } |
| 7303 | |
Anastasia Stulova | 0430794 | 2018-11-16 16:22:56 +0000 | [diff] [blame] | 7304 | ExprResult Sema::PerformQualificationConversion(Expr *E, QualType Ty, |
| 7305 | ExprValueKind VK, |
| 7306 | CheckedConversionKind CCK) { |
| 7307 | CastKind CK = (Ty.getAddressSpace() != E->getType().getAddressSpace()) |
| 7308 | ? CK_AddressSpaceConversion |
| 7309 | : CK_NoOp; |
| 7310 | return ImpCastExprToType(E, Ty, CK, VK, /*BasePath=*/nullptr, CCK); |
| 7311 | } |
| 7312 | |
| 7313 | ExprResult InitializationSequence::Perform(Sema &S, |
| 7314 | const InitializedEntity &Entity, |
| 7315 | const InitializationKind &Kind, |
| 7316 | MultiExprArg Args, |
| 7317 | QualType *ResultType) { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 7318 | if (Failed()) { |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 7319 | Diagnose(S, Entity, Kind, Args); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7320 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7321 | } |
Nico Weber | 337d5aa | 2015-04-17 08:32:38 +0000 | [diff] [blame] | 7322 | if (!ZeroInitializationFixit.empty()) { |
| 7323 | unsigned DiagID = diag::err_default_init_const; |
| 7324 | if (Decl *D = Entity.getDecl()) |
| 7325 | if (S.getLangOpts().MSVCCompat && D->hasAttr<SelectAnyAttr>()) |
| 7326 | DiagID = diag::ext_default_init_const; |
| 7327 | |
| 7328 | // The initialization would have succeeded with this fixit. Since the fixit |
| 7329 | // is on the error, we need to build a valid AST in this case, so this isn't |
| 7330 | // handled in the Failed() branch above. |
| 7331 | QualType DestType = Entity.getType(); |
| 7332 | S.Diag(Kind.getLocation(), DiagID) |
| 7333 | << DestType << (bool)DestType->getAs<RecordType>() |
| 7334 | << FixItHint::CreateInsertion(ZeroInitializationFixitLoc, |
| 7335 | ZeroInitializationFixit); |
| 7336 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7337 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 7338 | if (getKind() == DependentSequence) { |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7339 | // If the declaration is a non-dependent, incomplete array type |
| 7340 | // that has an initializer, then its type will be completed once |
| 7341 | // the initializer is instantiated. |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7342 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7343 | Args.size() == 1) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7344 | QualType DeclType = Entity.getType(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7345 | if (const IncompleteArrayType *ArrayT |
| 7346 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 7347 | // FIXME: We don't currently have the ability to accurately |
| 7348 | // compute the length of an initializer list without |
| 7349 | // performing full type-checking of the initializer list |
| 7350 | // (since we have to determine where braces are implicitly |
| 7351 | // introduced and such). So, we fall back to making the array |
| 7352 | // type a dependently-sized array type with no specified |
| 7353 | // bound. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7354 | if (isa<InitListExpr>((Expr *)Args[0])) { |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7355 | SourceRange Brackets; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7356 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7357 | // Scavange the location of the brackets from the entity, if we can. |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 7358 | if (auto *DD = dyn_cast_or_null<DeclaratorDecl>(Entity.getDecl())) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7359 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 7360 | TypeLoc TL = TInfo->getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7361 | if (IncompleteArrayTypeLoc ArrayLoc = |
| 7362 | TL.getAs<IncompleteArrayTypeLoc>()) |
| 7363 | Brackets = ArrayLoc.getBracketsRange(); |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7364 | } |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7365 | } |
| 7366 | |
| 7367 | *ResultType |
| 7368 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7369 | /*NumElts=*/nullptr, |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7370 | ArrayT->getSizeModifier(), |
| 7371 | ArrayT->getIndexTypeCVRQualifiers(), |
| 7372 | Brackets); |
| 7373 | } |
| 7374 | |
| 7375 | } |
| 7376 | } |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 7377 | if (Kind.getKind() == InitializationKind::IK_Direct && |
| 7378 | !Kind.isExplicitCast()) { |
| 7379 | // Rebuild the ParenListExpr. |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 7380 | SourceRange ParenRange = Kind.getParenOrBraceRange(); |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 7381 | return S.ActOnParenListExpr(ParenRange.getBegin(), ParenRange.getEnd(), |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7382 | Args); |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 7383 | } |
Manuel Klimek | f2b4b69 | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 7384 | assert(Kind.getKind() == InitializationKind::IK_Copy || |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7385 | Kind.isExplicitCast() || |
Douglas Gregor | bf13895 | 2012-04-04 04:06:51 +0000 | [diff] [blame] | 7386 | Kind.getKind() == InitializationKind::IK_DirectList); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7387 | return ExprResult(Args[0]); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7388 | } |
| 7389 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 7390 | // No steps means no initialization. |
| 7391 | if (Steps.empty()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7392 | return ExprResult((Expr *)nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7393 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7394 | if (S.getLangOpts().CPlusPlus11 && Entity.getType()->isReferenceType() && |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7395 | Args.size() == 1 && isa<InitListExpr>(Args[0]) && |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 7396 | !Entity.isParameterKind()) { |
Richard Smith | 2b349ae | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 7397 | // Produce a C++98 compatibility warning if we are initializing a reference |
| 7398 | // from an initializer list. For parameters, we produce a better warning |
| 7399 | // elsewhere. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7400 | Expr *Init = Args[0]; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7401 | S.Diag(Init->getBeginLoc(), diag::warn_cxx98_compat_reference_list_init) |
| 7402 | << Init->getSourceRange(); |
Richard Smith | 2b349ae | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 7403 | } |
| 7404 | |
Egor Churaev | 3bccec5 | 2017-04-05 12:47:10 +0000 | [diff] [blame] | 7405 | // OpenCL v2.0 s6.13.11.1. atomic variables can be initialized in global scope |
| 7406 | QualType ETy = Entity.getType(); |
| 7407 | Qualifiers TyQualifiers = ETy.getQualifiers(); |
| 7408 | bool HasGlobalAS = TyQualifiers.hasAddressSpace() && |
| 7409 | TyQualifiers.getAddressSpace() == LangAS::opencl_global; |
| 7410 | |
| 7411 | if (S.getLangOpts().OpenCLVersion >= 200 && |
| 7412 | ETy->isAtomicType() && !HasGlobalAS && |
| 7413 | Entity.getKind() == InitializedEntity::EK_Variable && Args.size() > 0) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7414 | S.Diag(Args[0]->getBeginLoc(), diag::err_opencl_atomic_init) |
| 7415 | << 1 |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 7416 | << SourceRange(Entity.getDecl()->getBeginLoc(), Args[0]->getEndLoc()); |
Egor Churaev | 3bccec5 | 2017-04-05 12:47:10 +0000 | [diff] [blame] | 7417 | return ExprError(); |
| 7418 | } |
| 7419 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7420 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 7421 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 7422 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 7423 | // and we want latter when it makes sense. |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7424 | if (ResultType) |
Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 7425 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 7426 | Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7427 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7428 | ExprResult CurInit((Expr *)nullptr); |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7429 | SmallVector<Expr*, 4> ArrayLoopCommonExprs; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7430 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7431 | // For initialization steps that start with a single initializer, |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 7432 | // grab the only argument out the Args and place it into the "current" |
| 7433 | // initializer. |
| 7434 | switch (Steps.front().Kind) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7435 | case SK_ResolveAddressOfOverloadedFunction: |
| 7436 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7437 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7438 | case SK_CastDerivedToBaseLValue: |
| 7439 | case SK_BindReference: |
| 7440 | case SK_BindReferenceToTemporary: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7441 | case SK_FinalCopy: |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 7442 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7443 | case SK_UserConversion: |
| 7444 | case SK_QualificationConversionLValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7445 | case SK_QualificationConversionXValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7446 | case SK_QualificationConversionRValue: |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 7447 | case SK_AtomicConversion: |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 7448 | case SK_LValueToRValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7449 | case SK_ConversionSequence: |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7450 | case SK_ConversionSequenceNoNarrowing: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7451 | case SK_ListInitialization: |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7452 | case SK_UnwrapInitList: |
| 7453 | case SK_RewrapInitList: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7454 | case SK_CAssignment: |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 7455 | case SK_StringInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7456 | case SK_ObjCObjectConversion: |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7457 | case SK_ArrayLoopIndex: |
| 7458 | case SK_ArrayLoopInit: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7459 | case SK_ArrayInit: |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 7460 | case SK_GNUArrayInit: |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 7461 | case SK_ParenthesizedArrayInit: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7462 | case SK_PassByIndirectCopyRestore: |
| 7463 | case SK_PassByIndirectRestore: |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 7464 | case SK_ProduceObjCObject: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7465 | case SK_StdInitializerList: |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 7466 | case SK_OCLSamplerInit: |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 7467 | case SK_OCLZeroOpaqueType: { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7468 | assert(Args.size() == 1); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7469 | CurInit = Args[0]; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7470 | if (!CurInit.get()) return ExprError(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7471 | break; |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7472 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7473 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7474 | case SK_ConstructorInitialization: |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7475 | case SK_ConstructorInitializationFromList: |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 7476 | case SK_StdInitializerListConstructorCall: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7477 | case SK_ZeroInitialization: |
| 7478 | break; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7479 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7480 | |
Richard Smith | d6a1508 | 2017-01-07 00:48:55 +0000 | [diff] [blame] | 7481 | // Promote from an unevaluated context to an unevaluated list context in |
| 7482 | // C++11 list-initialization; we need to instantiate entities usable in |
| 7483 | // constant expressions here in order to perform narrowing checks =( |
| 7484 | EnterExpressionEvaluationContext Evaluated( |
| 7485 | S, EnterExpressionEvaluationContext::InitList, |
| 7486 | CurInit.get() && isa<InitListExpr>(CurInit.get())); |
| 7487 | |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7488 | // C++ [class.abstract]p2: |
| 7489 | // no objects of an abstract class can be created except as subobjects |
| 7490 | // of a class derived from it |
| 7491 | auto checkAbstractType = [&](QualType T) -> bool { |
| 7492 | if (Entity.getKind() == InitializedEntity::EK_Base || |
| 7493 | Entity.getKind() == InitializedEntity::EK_Delegating) |
| 7494 | return false; |
| 7495 | return S.RequireNonAbstractType(Kind.getLocation(), T, |
| 7496 | diag::err_allocation_of_abstract_type); |
| 7497 | }; |
| 7498 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7499 | // Walk through the computed steps for the initialization sequence, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7500 | // performing the specified conversions along the way. |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7501 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7502 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 7503 | Step != StepEnd; ++Step) { |
| 7504 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7505 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7506 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7507 | QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7508 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7509 | switch (Step->Kind) { |
| 7510 | case SK_ResolveAddressOfOverloadedFunction: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7511 | // Overload resolution determined which function invoke; update the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7512 | // initializer to reflect that choice. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7513 | S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 7514 | if (S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation())) |
| 7515 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7516 | CurInit = S.FixOverloadedFunctionReference(CurInit, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7517 | Step->Function.FoundDecl, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7518 | Step->Function.Function); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7519 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7520 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7521 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7522 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7523 | case SK_CastDerivedToBaseLValue: { |
| 7524 | // We have a derived-to-base cast that produces either an rvalue or an |
| 7525 | // lvalue. Perform that cast. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7526 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 7527 | CXXCastPath BasePath; |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 7528 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7529 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 7530 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7531 | if (S.CheckDerivedToBaseConversion( |
| 7532 | SourceType, Step->Type, CurInit.get()->getBeginLoc(), |
| 7533 | CurInit.get()->getSourceRange(), &BasePath, IgnoreBaseAccess)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7534 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7535 | |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7536 | ExprValueKind VK = |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7537 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7538 | VK_LValue : |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7539 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7540 | VK_XValue : |
| 7541 | VK_RValue); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7542 | CurInit = |
| 7543 | ImplicitCastExpr::Create(S.Context, Step->Type, CK_DerivedToBase, |
| 7544 | CurInit.get(), &BasePath, VK); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7545 | break; |
| 7546 | } |
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_BindReference: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7549 | // Reference binding does not have any corresponding ASTs. |
| 7550 | |
| 7551 | // Check exception specifications |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7552 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7553 | return ExprError(); |
Anders Carlsson | ab0ddb5 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 7554 | |
George Burgess IV | cfd48d9 | 2017-04-13 23:47:08 +0000 | [diff] [blame] | 7555 | // We don't check for e.g. function pointers here, since address |
| 7556 | // availability checks should only occur when the function first decays |
| 7557 | // into a pointer or reference. |
| 7558 | if (CurInit.get()->getType()->isFunctionProtoType()) { |
| 7559 | if (auto *DRE = dyn_cast<DeclRefExpr>(CurInit.get()->IgnoreParens())) { |
| 7560 | if (auto *FD = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
| 7561 | if (!S.checkAddressOfFunctionIsAvailable(FD, /*Complain=*/true, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7562 | DRE->getBeginLoc())) |
George Burgess IV | cfd48d9 | 2017-04-13 23:47:08 +0000 | [diff] [blame] | 7563 | return ExprError(); |
| 7564 | } |
| 7565 | } |
| 7566 | } |
| 7567 | |
Nick Lewycky | 2eeddfb | 2016-05-14 17:44:14 +0000 | [diff] [blame] | 7568 | CheckForNullPointerDereference(S, CurInit.get()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7569 | break; |
Anders Carlsson | ab0ddb5 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 7570 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 7571 | case SK_BindReferenceToTemporary: { |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 7572 | // Make sure the "temporary" is actually an rvalue. |
| 7573 | assert(CurInit.get()->isRValue() && "not a temporary"); |
| 7574 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7575 | // Check exception specifications |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7576 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7577 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7578 | |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 7579 | // Materialize the temporary into memory. |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 7580 | MaterializeTemporaryExpr *MTE = S.CreateMaterializeTemporaryExpr( |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7581 | Step->Type, CurInit.get(), Entity.getType()->isLValueReferenceType()); |
Richard Smith | d87aab9 | 2018-07-17 22:24:09 +0000 | [diff] [blame] | 7582 | CurInit = MTE; |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 7583 | |
Brian Kelley | 762f928 | 2017-03-29 18:16:38 +0000 | [diff] [blame] | 7584 | // If we're extending this temporary to automatic storage duration -- we |
| 7585 | // need to register its cleanup during the full-expression's cleanups. |
| 7586 | if (MTE->getStorageDuration() == SD_Automatic && |
| 7587 | MTE->getType().isDestructedType()) |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 7588 | S.Cleanup.setExprNeedsCleanups(true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7589 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 7590 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7591 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7592 | case SK_FinalCopy: |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7593 | if (checkAbstractType(Step->Type)) |
| 7594 | return ExprError(); |
| 7595 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7596 | // If the overall initialization is initializing a temporary, we already |
| 7597 | // bound our argument if it was necessary to do so. If not (if we're |
| 7598 | // ultimately initializing a non-temporary), our argument needs to be |
| 7599 | // bound since it's initializing a function parameter. |
| 7600 | // FIXME: This is a mess. Rationalize temporary destruction. |
| 7601 | if (!shouldBindAsTemporary(Entity)) |
| 7602 | CurInit = S.MaybeBindToTemporary(CurInit.get()); |
| 7603 | CurInit = CopyObject(S, Step->Type, Entity, CurInit, |
| 7604 | /*IsExtraneousCopy=*/false); |
| 7605 | break; |
| 7606 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 7607 | case SK_ExtraneousCopyToTemporary: |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7608 | CurInit = CopyObject(S, Step->Type, Entity, CurInit, |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 7609 | /*IsExtraneousCopy=*/true); |
| 7610 | break; |
| 7611 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7612 | case SK_UserConversion: { |
| 7613 | // We have a user-defined conversion that invokes either a constructor |
| 7614 | // or a conversion function. |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 7615 | CastKind CastKind; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7616 | FunctionDecl *Fn = Step->Function.Function; |
| 7617 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 7618 | bool HadMultipleCandidates = Step->Function.HadMultipleCandidates; |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 7619 | bool CreatedObject = false; |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 7620 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7621 | // Build a call to the selected constructor. |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 7622 | SmallVector<Expr*, 8> ConstructorArgs; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7623 | SourceLocation Loc = CurInit.get()->getBeginLoc(); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 7624 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7625 | // Determine the arguments required to actually perform the constructor |
| 7626 | // call. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7627 | Expr *Arg = CurInit.get(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7628 | if (S.CompleteConstructorCall(Constructor, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7629 | MultiExprArg(&Arg, 1), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7630 | Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7631 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7632 | |
Richard Smith | b24f067 | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 7633 | // Build an expression that constructs a temporary. |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 7634 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, |
| 7635 | FoundFn, Constructor, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7636 | ConstructorArgs, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 7637 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 7638 | /*ListInit*/ false, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 7639 | /*StdInitListInit*/ false, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 7640 | /*ZeroInit*/ false, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 7641 | CXXConstructExpr::CK_Complete, |
| 7642 | SourceRange()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7643 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7644 | return ExprError(); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 7645 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 7646 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, FoundFn, |
| 7647 | Entity); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 7648 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 7649 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7650 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7651 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 7652 | CreatedObject = true; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7653 | } else { |
| 7654 | // Build a call to the conversion function. |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 7655 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7656 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), nullptr, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7657 | FoundFn); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 7658 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 7659 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7660 | |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 7661 | CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion, |
| 7662 | HadMultipleCandidates); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7663 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7664 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7665 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7666 | CastKind = CK_UserDefinedConversion; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7667 | CreatedObject = Conversion->getReturnType()->isRecordType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7668 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7669 | |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7670 | if (CreatedObject && checkAbstractType(CurInit.get()->getType())) |
| 7671 | return ExprError(); |
| 7672 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7673 | CurInit = ImplicitCastExpr::Create(S.Context, CurInit.get()->getType(), |
| 7674 | CastKind, CurInit.get(), nullptr, |
| 7675 | CurInit.get()->getValueKind()); |
Abramo Bagnara | b0cf297 | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 7676 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 7677 | if (shouldBindAsTemporary(Entity)) |
| 7678 | // The overall entity is temporary, so this expression should be |
| 7679 | // destroyed at the end of its full-expression. |
| 7680 | CurInit = S.MaybeBindToTemporary(CurInit.getAs<Expr>()); |
| 7681 | else if (CreatedObject && shouldDestroyEntity(Entity)) { |
| 7682 | // The object outlasts the full-expression, but we need to prepare for |
| 7683 | // a destructor being run on it. |
| 7684 | // FIXME: It makes no sense to do this here. This should happen |
| 7685 | // regardless of how we initialized the entity. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7686 | QualType T = CurInit.get()->getType(); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 7687 | if (const RecordType *Record = T->getAs<RecordType>()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7688 | CXXDestructorDecl *Destructor |
Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 7689 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7690 | S.CheckDestructorAccess(CurInit.get()->getBeginLoc(), Destructor, |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 7691 | S.PDiag(diag::err_access_dtor_temp) << T); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7692 | S.MarkFunctionReferenced(CurInit.get()->getBeginLoc(), Destructor); |
| 7693 | if (S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getBeginLoc())) |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 7694 | return ExprError(); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 7695 | } |
| 7696 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7697 | break; |
| 7698 | } |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7699 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7700 | case SK_QualificationConversionLValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7701 | case SK_QualificationConversionXValue: |
| 7702 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7703 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7704 | ExprValueKind VK = |
Anastasia Stulova | 0430794 | 2018-11-16 16:22:56 +0000 | [diff] [blame] | 7705 | Step->Kind == SK_QualificationConversionLValue |
| 7706 | ? VK_LValue |
| 7707 | : (Step->Kind == SK_QualificationConversionXValue ? VK_XValue |
| 7708 | : VK_RValue); |
| 7709 | CurInit = S.PerformQualificationConversion(CurInit.get(), Step->Type, VK); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7710 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7711 | } |
| 7712 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 7713 | case SK_AtomicConversion: { |
| 7714 | assert(CurInit.get()->isRValue() && "cannot convert glvalue to atomic"); |
| 7715 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
| 7716 | CK_NonAtomicToAtomic, VK_RValue); |
| 7717 | break; |
| 7718 | } |
| 7719 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 7720 | case SK_LValueToRValue: { |
| 7721 | assert(CurInit.get()->isGLValue() && "cannot load from a prvalue"); |
Richard Smith | 3501895 | 2018-11-03 02:23:33 +0000 | [diff] [blame] | 7722 | CurInit = ImplicitCastExpr::Create(S.Context, Step->Type, |
| 7723 | CK_LValueToRValue, CurInit.get(), |
| 7724 | /*BasePath=*/nullptr, VK_RValue); |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 7725 | break; |
| 7726 | } |
| 7727 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7728 | case SK_ConversionSequence: |
| 7729 | case SK_ConversionSequenceNoNarrowing: { |
Leonard Chan | ad7ac96 | 2018-12-06 01:05:54 +0000 | [diff] [blame] | 7730 | if (const auto *FromPtrType = |
| 7731 | CurInit.get()->getType()->getAs<PointerType>()) { |
| 7732 | if (const auto *ToPtrType = Step->Type->getAs<PointerType>()) { |
| 7733 | if (FromPtrType->getPointeeType()->hasAttr(attr::NoDeref) && |
| 7734 | !ToPtrType->getPointeeType()->hasAttr(attr::NoDeref)) { |
| 7735 | S.Diag(CurInit.get()->getExprLoc(), |
| 7736 | diag::warn_noderef_to_dereferenceable_pointer) |
| 7737 | << CurInit.get()->getSourceRange(); |
| 7738 | } |
| 7739 | } |
| 7740 | } |
| 7741 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7742 | Sema::CheckedConversionKind CCK |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7743 | = Kind.isCStyleCast()? Sema::CCK_CStyleCast |
| 7744 | : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 7745 | : Kind.isExplicitCast()? Sema::CCK_OtherCast |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7746 | : Sema::CCK_ImplicitConversion; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7747 | ExprResult CurInitExprRes = |
| 7748 | S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7749 | getAssignmentAction(Entity), CCK); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7750 | if (CurInitExprRes.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7751 | return ExprError(); |
Roger Ferrer Ibanez | 722a4db | 2016-08-12 08:04:13 +0000 | [diff] [blame] | 7752 | |
| 7753 | S.DiscardMisalignedMemberAddress(Step->Type.getTypePtr(), CurInit.get()); |
| 7754 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7755 | CurInit = CurInitExprRes; |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7756 | |
| 7757 | if (Step->Kind == SK_ConversionSequenceNoNarrowing && |
Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 7758 | S.getLangOpts().CPlusPlus) |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7759 | DiagnoseNarrowingInInitList(S, *Step->ICS, SourceType, Entity.getType(), |
| 7760 | CurInit.get()); |
Roger Ferrer Ibanez | 722a4db | 2016-08-12 08:04:13 +0000 | [diff] [blame] | 7761 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7762 | break; |
Douglas Gregor | 5c8ffab | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 7763 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7764 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7765 | case SK_ListInitialization: { |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7766 | if (checkAbstractType(Step->Type)) |
| 7767 | return ExprError(); |
| 7768 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7769 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 7770 | // If we're not initializing the top-level entity, we need to create an |
| 7771 | // InitializeTemporary entity for our target type. |
| 7772 | QualType Ty = Step->Type; |
| 7773 | bool IsTemporary = !S.Context.hasSameType(Entity.getType(), Ty); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7774 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(Ty); |
Richard Smith | d712d0d | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 7775 | InitializedEntity InitEntity = IsTemporary ? TempEntity : Entity; |
| 7776 | InitListChecker PerformInitList(S, InitEntity, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 7777 | InitList, Ty, /*VerifyOnly=*/false, |
| 7778 | /*TreatUnavailableAsInvalid=*/false); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 7779 | if (PerformInitList.HadError()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7780 | return ExprError(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7781 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 7782 | // Hack: We must update *ResultType if available in order to set the |
| 7783 | // bounds of arrays, e.g. in 'int ar[] = {1, 2, 3};'. |
| 7784 | // Worst case: 'const int (&arref)[] = {1, 2, 3};'. |
| 7785 | if (ResultType && |
| 7786 | ResultType->getNonReferenceType()->isIncompleteArrayType()) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7787 | if ((*ResultType)->isRValueReferenceType()) |
| 7788 | Ty = S.Context.getRValueReferenceType(Ty); |
| 7789 | else if ((*ResultType)->isLValueReferenceType()) |
| 7790 | Ty = S.Context.getLValueReferenceType(Ty, |
| 7791 | (*ResultType)->getAs<LValueReferenceType>()->isSpelledAsLValue()); |
| 7792 | *ResultType = Ty; |
| 7793 | } |
| 7794 | |
| 7795 | InitListExpr *StructuredInitList = |
| 7796 | PerformInitList.getFullyStructuredList(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7797 | CurInit.get(); |
Richard Smith | d712d0d | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 7798 | CurInit = shouldBindAsTemporary(InitEntity) |
| 7799 | ? S.MaybeBindToTemporary(StructuredInitList) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7800 | : StructuredInitList; |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 7801 | break; |
| 7802 | } |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 7803 | |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7804 | case SK_ConstructorInitializationFromList: { |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7805 | if (checkAbstractType(Step->Type)) |
| 7806 | return ExprError(); |
| 7807 | |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 7808 | // When an initializer list is passed for a parameter of type "reference |
| 7809 | // to object", we don't get an EK_Temporary entity, but instead an |
| 7810 | // EK_Parameter entity with reference type. |
Sebastian Redl | 99f6616 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 7811 | // FIXME: This is a hack. What we really should do is create a user |
| 7812 | // conversion step for this case, but this makes it considerably more |
| 7813 | // complicated. For now, this will do. |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 7814 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 7815 | Entity.getType().getNonReferenceType()); |
| 7816 | bool UseTemporary = Entity.getType()->isReferenceType(); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 7817 | assert(Args.size() == 1 && "expected a single argument for list init"); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7818 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Richard Smith | 2b349ae | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 7819 | S.Diag(InitList->getExprLoc(), diag::warn_cxx98_compat_ctor_list_init) |
| 7820 | << InitList->getSourceRange(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 7821 | MultiExprArg Arg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 7822 | CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity : |
| 7823 | Entity, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7824 | Kind, Arg, *Step, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 7825 | ConstructorInitRequiresZeroInit, |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7826 | /*IsListInitialization*/true, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 7827 | /*IsStdInitListInit*/false, |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 7828 | InitList->getLBraceLoc(), |
| 7829 | InitList->getRBraceLoc()); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 7830 | break; |
| 7831 | } |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 7832 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7833 | case SK_UnwrapInitList: |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7834 | CurInit = cast<InitListExpr>(CurInit.get())->getInit(0); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7835 | break; |
| 7836 | |
| 7837 | case SK_RewrapInitList: { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7838 | Expr *E = CurInit.get(); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7839 | InitListExpr *Syntactic = Step->WrappingSyntacticList; |
| 7840 | InitListExpr *ILE = new (S.Context) InitListExpr(S.Context, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 7841 | Syntactic->getLBraceLoc(), E, Syntactic->getRBraceLoc()); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7842 | ILE->setSyntacticForm(Syntactic); |
| 7843 | ILE->setType(E->getType()); |
| 7844 | ILE->setValueKind(E->getValueKind()); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7845 | CurInit = ILE; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7846 | break; |
| 7847 | } |
| 7848 | |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7849 | case SK_ConstructorInitialization: |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 7850 | case SK_StdInitializerListConstructorCall: { |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 7851 | if (checkAbstractType(Step->Type)) |
| 7852 | return ExprError(); |
| 7853 | |
Sebastian Redl | 99f6616 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 7854 | // When an initializer list is passed for a parameter of type "reference |
| 7855 | // to object", we don't get an EK_Temporary entity, but instead an |
| 7856 | // EK_Parameter entity with reference type. |
| 7857 | // FIXME: This is a hack. What we really should do is create a user |
| 7858 | // conversion step for this case, but this makes it considerably more |
| 7859 | // complicated. For now, this will do. |
| 7860 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 7861 | Entity.getType().getNonReferenceType()); |
| 7862 | bool UseTemporary = Entity.getType()->isReferenceType(); |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 7863 | bool IsStdInitListInit = |
| 7864 | Step->Kind == SK_StdInitializerListConstructorCall; |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7865 | Expr *Source = CurInit.get(); |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 7866 | SourceRange Range = Kind.hasParenOrBraceRange() |
| 7867 | ? Kind.getParenOrBraceRange() |
| 7868 | : SourceRange(); |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7869 | CurInit = PerformConstructorInitialization( |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7870 | S, UseTemporary ? TempEntity : Entity, Kind, |
| 7871 | Source ? MultiExprArg(Source) : Args, *Step, |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7872 | ConstructorInitRequiresZeroInit, |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7873 | /*IsListInitialization*/ IsStdInitListInit, |
| 7874 | /*IsStdInitListInitialization*/ IsStdInitListInit, |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 7875 | /*LBraceLoc*/ Range.getBegin(), |
| 7876 | /*RBraceLoc*/ Range.getEnd()); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 7877 | break; |
Sebastian Redl | 99f6616 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 7878 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7879 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 7880 | case SK_ZeroInitialization: { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7881 | step_iterator NextStep = Step; |
| 7882 | ++NextStep; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7883 | if (NextStep != StepEnd && |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 7884 | (NextStep->Kind == SK_ConstructorInitialization || |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7885 | NextStep->Kind == SK_ConstructorInitializationFromList)) { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7886 | // The need for zero-initialization is recorded directly into |
| 7887 | // the call to the object's constructor within the next step. |
| 7888 | ConstructorInitRequiresZeroInit = true; |
| 7889 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7890 | S.getLangOpts().CPlusPlus && |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7891 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 7892 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 7893 | if (!TSInfo) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7894 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 7895 | Kind.getRange().getBegin()); |
| 7896 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7897 | CurInit = new (S.Context) CXXScalarValueInitExpr( |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 7898 | Entity.getType().getNonLValueExprType(S.Context), TSInfo, |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7899 | Kind.getRange().getEnd()); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7900 | } else { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7901 | CurInit = new (S.Context) ImplicitValueInitExpr(Step->Type); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7902 | } |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 7903 | break; |
| 7904 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7905 | |
| 7906 | case SK_CAssignment: { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7907 | QualType SourceType = CurInit.get()->getType(); |
Leonard Chan | ad7ac96 | 2018-12-06 01:05:54 +0000 | [diff] [blame] | 7908 | |
George Burgess IV | 5f21c71 | 2015-10-12 19:57:04 +0000 | [diff] [blame] | 7909 | // Save off the initial CurInit in case we need to emit a diagnostic |
| 7910 | ExprResult InitialCurInit = CurInit; |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7911 | ExprResult Result = CurInit; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7912 | Sema::AssignConvertType ConvTy = |
Fariborz Jahanian | 25eef19 | 2013-07-31 21:40:51 +0000 | [diff] [blame] | 7913 | S.CheckSingleAssignmentConstraints(Step->Type, Result, true, |
| 7914 | Entity.getKind() == InitializedEntity::EK_Parameter_CF_Audited); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7915 | if (Result.isInvalid()) |
| 7916 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7917 | CurInit = Result; |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 7918 | |
| 7919 | // If this is a call, allow conversion to a transparent union. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7920 | ExprResult CurInitExprRes = CurInit; |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 7921 | if (ConvTy != Sema::Compatible && |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 7922 | Entity.isParameterKind() && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7923 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes) |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 7924 | == Sema::Compatible) |
| 7925 | ConvTy = Sema::Compatible; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7926 | if (CurInitExprRes.isInvalid()) |
| 7927 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7928 | CurInit = CurInitExprRes; |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 7929 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 7930 | bool Complained; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7931 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 7932 | Step->Type, SourceType, |
George Burgess IV | 5f21c71 | 2015-10-12 19:57:04 +0000 | [diff] [blame] | 7933 | InitialCurInit.get(), |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 7934 | getAssignmentAction(Entity, true), |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 7935 | &Complained)) { |
| 7936 | PrintInitLocationNote(S, Entity); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7937 | return ExprError(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 7938 | } else if (Complained) |
| 7939 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7940 | break; |
| 7941 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 7942 | |
| 7943 | case SK_StringInit: { |
| 7944 | QualType Ty = Step->Type; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7945 | CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty, |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 7946 | S.Context.getAsArrayType(Ty), S); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 7947 | break; |
| 7948 | } |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 7949 | |
| 7950 | case SK_ObjCObjectConversion: |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7951 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7952 | CK_ObjCObjectLValueCast, |
Eli Friedman | be4b363 | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 7953 | CurInit.get()->getValueKind()); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 7954 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7955 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7956 | case SK_ArrayLoopIndex: { |
| 7957 | Expr *Cur = CurInit.get(); |
| 7958 | Expr *BaseExpr = new (S.Context) |
| 7959 | OpaqueValueExpr(Cur->getExprLoc(), Cur->getType(), |
| 7960 | Cur->getValueKind(), Cur->getObjectKind(), Cur); |
| 7961 | Expr *IndexExpr = |
| 7962 | new (S.Context) ArrayInitIndexExpr(S.Context.getSizeType()); |
| 7963 | CurInit = S.CreateBuiltinArraySubscriptExpr( |
| 7964 | BaseExpr, Kind.getLocation(), IndexExpr, Kind.getLocation()); |
| 7965 | ArrayLoopCommonExprs.push_back(BaseExpr); |
| 7966 | break; |
| 7967 | } |
| 7968 | |
| 7969 | case SK_ArrayLoopInit: { |
| 7970 | assert(!ArrayLoopCommonExprs.empty() && |
| 7971 | "mismatched SK_ArrayLoopIndex and SK_ArrayLoopInit"); |
| 7972 | Expr *Common = ArrayLoopCommonExprs.pop_back_val(); |
| 7973 | CurInit = new (S.Context) ArrayInitLoopExpr(Step->Type, Common, |
| 7974 | CurInit.get()); |
| 7975 | break; |
| 7976 | } |
| 7977 | |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 7978 | case SK_GNUArrayInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7979 | // Okay: we checked everything before creating this step. Note that |
| 7980 | // this is a GNU extension. |
| 7981 | S.Diag(Kind.getLocation(), diag::ext_array_init_copy) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7982 | << Step->Type << CurInit.get()->getType() |
| 7983 | << CurInit.get()->getSourceRange(); |
Eli Friedman | 88fccbd | 2019-02-11 22:54:27 +0000 | [diff] [blame] | 7984 | updateGNUCompoundLiteralRValue(CurInit.get()); |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 7985 | LLVM_FALLTHROUGH; |
| 7986 | case SK_ArrayInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7987 | // If the destination type is an incomplete array type, update the |
| 7988 | // type accordingly. |
| 7989 | if (ResultType) { |
| 7990 | if (const IncompleteArrayType *IncompleteDest |
| 7991 | = S.Context.getAsIncompleteArrayType(Step->Type)) { |
| 7992 | if (const ConstantArrayType *ConstantSource |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7993 | = S.Context.getAsConstantArrayType(CurInit.get()->getType())) { |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7994 | *ResultType = S.Context.getConstantArrayType( |
| 7995 | IncompleteDest->getElementType(), |
| 7996 | ConstantSource->getSize(), |
| 7997 | ArrayType::Normal, 0); |
| 7998 | } |
| 7999 | } |
| 8000 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8001 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8002 | |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 8003 | case SK_ParenthesizedArrayInit: |
| 8004 | // Okay: we checked everything before creating this step. Note that |
| 8005 | // this is a GNU extension. |
| 8006 | S.Diag(Kind.getLocation(), diag::ext_array_init_parens) |
| 8007 | << CurInit.get()->getSourceRange(); |
| 8008 | break; |
| 8009 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8010 | case SK_PassByIndirectCopyRestore: |
| 8011 | case SK_PassByIndirectRestore: |
| 8012 | checkIndirectCopyRestoreSource(S, CurInit.get()); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 8013 | CurInit = new (S.Context) ObjCIndirectCopyRestoreExpr( |
| 8014 | CurInit.get(), Step->Type, |
| 8015 | Step->Kind == SK_PassByIndirectCopyRestore); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8016 | break; |
| 8017 | |
| 8018 | case SK_ProduceObjCObject: |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 8019 | CurInit = |
| 8020 | ImplicitCastExpr::Create(S.Context, Step->Type, CK_ARCProduceObject, |
| 8021 | CurInit.get(), nullptr, VK_RValue); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8022 | break; |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 8023 | |
| 8024 | case SK_StdInitializerList: { |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 8025 | S.Diag(CurInit.get()->getExprLoc(), |
| 8026 | diag::warn_cxx98_compat_initializer_list_init) |
| 8027 | << CurInit.get()->getSourceRange(); |
Sebastian Redl | 249dee5 | 2012-03-05 19:35:43 +0000 | [diff] [blame] | 8028 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 8029 | // Materialize the temporary into memory. |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 8030 | MaterializeTemporaryExpr *MTE = S.CreateMaterializeTemporaryExpr( |
| 8031 | CurInit.get()->getType(), CurInit.get(), |
| 8032 | /*BoundToLvalueReference=*/false); |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 8033 | |
Florian Hahn | 0aa117d | 2018-07-17 09:23:31 +0000 | [diff] [blame] | 8034 | // Wrap it in a construction of a std::initializer_list<T>. |
| 8035 | CurInit = new (S.Context) CXXStdInitializerListExpr(Step->Type, MTE); |
Richard Smith | 0a9969b | 2018-07-17 00:11:41 +0000 | [diff] [blame] | 8036 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 8037 | // Bind the result, in case the library has given initializer_list a |
| 8038 | // non-trivial destructor. |
| 8039 | if (shouldBindAsTemporary(Entity)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 8040 | CurInit = S.MaybeBindToTemporary(CurInit.get()); |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 8041 | break; |
| 8042 | } |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 8043 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 8044 | case SK_OCLSamplerInit: { |
Raphael Isemann | b23ccec | 2018-12-10 12:37:46 +0000 | [diff] [blame] | 8045 | // Sampler initialization have 5 cases: |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 8046 | // 1. function argument passing |
| 8047 | // 1a. argument is a file-scope variable |
| 8048 | // 1b. argument is a function-scope variable |
| 8049 | // 1c. argument is one of caller function's parameters |
| 8050 | // 2. variable initialization |
| 8051 | // 2a. initializing a file-scope variable |
| 8052 | // 2b. initializing a function-scope variable |
| 8053 | // |
| 8054 | // For file-scope variables, since they cannot be initialized by function |
| 8055 | // call of __translate_sampler_initializer in LLVM IR, their references |
| 8056 | // need to be replaced by a cast from their literal initializers to |
| 8057 | // sampler type. Since sampler variables can only be used in function |
| 8058 | // calls as arguments, we only need to replace them when handling the |
| 8059 | // argument passing. |
| 8060 | assert(Step->Type->isSamplerT() && |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 8061 | "Sampler initialization on non-sampler type."); |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 8062 | Expr *Init = CurInit.get(); |
| 8063 | QualType SourceType = Init->getType(); |
| 8064 | // Case 1 |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 8065 | if (Entity.isParameterKind()) { |
Egor Churaev | a8d2451 | 2017-04-05 09:02:56 +0000 | [diff] [blame] | 8066 | if (!SourceType->isSamplerT() && !SourceType->isIntegerType()) { |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 8067 | S.Diag(Kind.getLocation(), diag::err_sampler_argument_required) |
| 8068 | << SourceType; |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 8069 | break; |
| 8070 | } else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Init)) { |
| 8071 | auto Var = cast<VarDecl>(DRE->getDecl()); |
| 8072 | // Case 1b and 1c |
| 8073 | // No cast from integer to sampler is needed. |
| 8074 | if (!Var->hasGlobalStorage()) { |
| 8075 | CurInit = ImplicitCastExpr::Create(S.Context, Step->Type, |
| 8076 | CK_LValueToRValue, Init, |
| 8077 | /*BasePath=*/nullptr, VK_RValue); |
| 8078 | break; |
| 8079 | } |
| 8080 | // Case 1a |
| 8081 | // For function call with a file-scope sampler variable as argument, |
| 8082 | // get the integer literal. |
| 8083 | // Do not diagnose if the file-scope variable does not have initializer |
| 8084 | // since this has already been diagnosed when parsing the variable |
| 8085 | // declaration. |
| 8086 | if (!Var->getInit() || !isa<ImplicitCastExpr>(Var->getInit())) |
| 8087 | break; |
| 8088 | Init = cast<ImplicitCastExpr>(const_cast<Expr*>( |
| 8089 | Var->getInit()))->getSubExpr(); |
| 8090 | SourceType = Init->getType(); |
| 8091 | } |
| 8092 | } else { |
| 8093 | // Case 2 |
| 8094 | // Check initializer is 32 bit integer constant. |
| 8095 | // If the initializer is taken from global variable, do not diagnose since |
| 8096 | // this has already been done when parsing the variable declaration. |
| 8097 | if (!Init->isConstantInitializer(S.Context, false)) |
| 8098 | break; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8099 | |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 8100 | if (!SourceType->isIntegerType() || |
| 8101 | 32 != S.Context.getIntWidth(SourceType)) { |
| 8102 | S.Diag(Kind.getLocation(), diag::err_sampler_initializer_not_integer) |
| 8103 | << SourceType; |
| 8104 | break; |
| 8105 | } |
| 8106 | |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 8107 | Expr::EvalResult EVResult; |
| 8108 | Init->EvaluateAsInt(EVResult, S.Context); |
| 8109 | llvm::APSInt Result = EVResult.Val.getInt(); |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 8110 | const uint64_t SamplerValue = Result.getLimitedValue(); |
| 8111 | // 32-bit value of sampler's initializer is interpreted as |
| 8112 | // bit-field with the following structure: |
| 8113 | // |unspecified|Filter|Addressing Mode| Normalized Coords| |
| 8114 | // |31 6|5 4|3 1| 0| |
| 8115 | // This structure corresponds to enum values of sampler properties |
| 8116 | // defined in SPIR spec v1.2 and also opencl-c.h |
| 8117 | unsigned AddressingMode = (0x0E & SamplerValue) >> 1; |
| 8118 | unsigned FilterMode = (0x30 & SamplerValue) >> 4; |
Andrew Savonichev | 3fee351 | 2018-11-08 11:25:41 +0000 | [diff] [blame] | 8119 | if (FilterMode != 1 && FilterMode != 2 && |
| 8120 | !S.getOpenCLOptions().isEnabled( |
| 8121 | "cl_intel_device_side_avc_motion_estimation")) |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 8122 | S.Diag(Kind.getLocation(), |
| 8123 | diag::warn_sampler_initializer_invalid_bits) |
| 8124 | << "Filter Mode"; |
| 8125 | if (AddressingMode > 4) |
| 8126 | S.Diag(Kind.getLocation(), |
| 8127 | diag::warn_sampler_initializer_invalid_bits) |
| 8128 | << "Addressing Mode"; |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 8129 | } |
| 8130 | |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 8131 | // Cases 1a, 2a and 2b |
| 8132 | // Insert cast from integer to sampler. |
| 8133 | CurInit = S.ImpCastExprToType(Init, S.Context.OCLSamplerTy, |
| 8134 | CK_IntToOCLSampler); |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 8135 | break; |
| 8136 | } |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 8137 | case SK_OCLZeroOpaqueType: { |
Andrew Savonichev | 3fee351 | 2018-11-08 11:25:41 +0000 | [diff] [blame] | 8138 | assert((Step->Type->isEventT() || Step->Type->isQueueT() || |
| 8139 | Step->Type->isOCLIntelSubgroupAVCType()) && |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 8140 | "Wrong type for initialization of OpenCL opaque type."); |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 8141 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 8142 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 8143 | CK_ZeroToOCLOpaqueType, |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 8144 | CurInit.get()->getValueKind()); |
| 8145 | break; |
| 8146 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8147 | } |
| 8148 | } |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 8149 | |
Richard Smith | ca975b2 | 2018-07-23 18:50:26 +0000 | [diff] [blame] | 8150 | // Check whether the initializer has a shorter lifetime than the initialized |
| 8151 | // entity, and if not, either lifetime-extend or warn as appropriate. |
| 8152 | if (auto *Init = CurInit.get()) |
| 8153 | S.checkInitializerLifetime(Entity, Init); |
| 8154 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 8155 | // Diagnose non-fatal problems with the completed initialization. |
| 8156 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 8157 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 8158 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 8159 | cast<FieldDecl>(Entity.getDecl()), |
| 8160 | CurInit.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8161 | |
Richard Trieu | ac3eca5 | 2015-04-29 01:52:17 +0000 | [diff] [blame] | 8162 | // Check for std::move on construction. |
| 8163 | if (const Expr *E = CurInit.get()) { |
| 8164 | CheckMoveOnConstruction(S, E, |
| 8165 | Entity.getKind() == InitializedEntity::EK_Result); |
| 8166 | } |
| 8167 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 8168 | return CurInit; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8169 | } |
| 8170 | |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 8171 | /// Somewhere within T there is an uninitialized reference subobject. |
| 8172 | /// Dig it out and diagnose it. |
Benjamin Kramer | 3e35026 | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 8173 | static bool DiagnoseUninitializedReference(Sema &S, SourceLocation Loc, |
| 8174 | QualType T) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 8175 | if (T->isReferenceType()) { |
| 8176 | S.Diag(Loc, diag::err_reference_without_init) |
| 8177 | << T.getNonReferenceType(); |
| 8178 | return true; |
| 8179 | } |
| 8180 | |
| 8181 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 8182 | if (!RD || !RD->hasUninitializedReferenceMember()) |
| 8183 | return false; |
| 8184 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 8185 | for (const auto *FI : RD->fields()) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 8186 | if (FI->isUnnamedBitfield()) |
| 8187 | continue; |
| 8188 | |
| 8189 | if (DiagnoseUninitializedReference(S, FI->getLocation(), FI->getType())) { |
| 8190 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 8191 | return true; |
| 8192 | } |
| 8193 | } |
| 8194 | |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 8195 | for (const auto &BI : RD->bases()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8196 | if (DiagnoseUninitializedReference(S, BI.getBeginLoc(), BI.getType())) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 8197 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 8198 | return true; |
| 8199 | } |
| 8200 | } |
| 8201 | |
| 8202 | return false; |
| 8203 | } |
| 8204 | |
| 8205 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8206 | //===----------------------------------------------------------------------===// |
| 8207 | // Diagnose initialization failures |
| 8208 | //===----------------------------------------------------------------------===// |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 8209 | |
| 8210 | /// Emit notes associated with an initialization that failed due to a |
| 8211 | /// "simple" conversion failure. |
| 8212 | static void emitBadConversionNotes(Sema &S, const InitializedEntity &entity, |
| 8213 | Expr *op) { |
| 8214 | QualType destType = entity.getType(); |
| 8215 | if (destType.getNonReferenceType()->isObjCObjectPointerType() && |
| 8216 | op->getType()->isObjCObjectPointerType()) { |
| 8217 | |
| 8218 | // Emit a possible note about the conversion failing because the |
| 8219 | // operand is a message send with a related result type. |
| 8220 | S.EmitRelatedResultTypeNote(op); |
| 8221 | |
| 8222 | // Emit a possible note about a return failing because we're |
| 8223 | // expecting a related result type. |
| 8224 | if (entity.getKind() == InitializedEntity::EK_Result) |
| 8225 | S.EmitRelatedResultTypeNoteForReturn(destType); |
| 8226 | } |
| 8227 | } |
| 8228 | |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 8229 | static void diagnoseListInit(Sema &S, const InitializedEntity &Entity, |
| 8230 | InitListExpr *InitList) { |
| 8231 | QualType DestType = Entity.getType(); |
| 8232 | |
| 8233 | QualType E; |
| 8234 | if (S.getLangOpts().CPlusPlus11 && S.isStdInitializerList(DestType, &E)) { |
| 8235 | QualType ArrayType = S.Context.getConstantArrayType( |
| 8236 | E.withConst(), |
| 8237 | llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 8238 | InitList->getNumInits()), |
| 8239 | clang::ArrayType::Normal, 0); |
| 8240 | InitializedEntity HiddenArray = |
| 8241 | InitializedEntity::InitializeTemporary(ArrayType); |
| 8242 | return diagnoseListInit(S, HiddenArray, InitList); |
| 8243 | } |
| 8244 | |
Richard Smith | 8d082d1 | 2014-09-04 22:13:39 +0000 | [diff] [blame] | 8245 | if (DestType->isReferenceType()) { |
| 8246 | // A list-initialization failure for a reference means that we tried to |
| 8247 | // create a temporary of the inner type (per [dcl.init.list]p3.6) and the |
| 8248 | // inner initialization failed. |
| 8249 | QualType T = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 8250 | diagnoseListInit(S, InitializedEntity::InitializeTemporary(T), InitList); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8251 | SourceLocation Loc = InitList->getBeginLoc(); |
Richard Smith | 8d082d1 | 2014-09-04 22:13:39 +0000 | [diff] [blame] | 8252 | if (auto *D = Entity.getDecl()) |
| 8253 | Loc = D->getLocation(); |
| 8254 | S.Diag(Loc, diag::note_in_reference_temporary_list_initializer) << T; |
| 8255 | return; |
| 8256 | } |
| 8257 | |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 8258 | InitListChecker DiagnoseInitList(S, Entity, InitList, DestType, |
Manman Ren | 073db02 | 2016-03-10 18:53:19 +0000 | [diff] [blame] | 8259 | /*VerifyOnly=*/false, |
| 8260 | /*TreatUnavailableAsInvalid=*/false); |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 8261 | assert(DiagnoseInitList.HadError() && |
| 8262 | "Inconsistent init list check result."); |
| 8263 | } |
| 8264 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8265 | bool InitializationSequence::Diagnose(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8266 | const InitializedEntity &Entity, |
| 8267 | const InitializationKind &Kind, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8268 | ArrayRef<Expr *> Args) { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 8269 | if (!Failed()) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8270 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8271 | |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8272 | // When we want to diagnose only one element of a braced-init-list, |
| 8273 | // we need to factor it out. |
| 8274 | Expr *OnlyArg; |
| 8275 | if (Args.size() == 1) { |
| 8276 | auto *List = dyn_cast<InitListExpr>(Args[0]); |
| 8277 | if (List && List->getNumInits() == 1) |
| 8278 | OnlyArg = List->getInit(0); |
| 8279 | else |
| 8280 | OnlyArg = Args[0]; |
| 8281 | } |
| 8282 | else |
| 8283 | OnlyArg = nullptr; |
| 8284 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 8285 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8286 | switch (Failure) { |
| 8287 | case FK_TooManyInitsForReference: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8288 | // FIXME: Customize for the initialized entity? |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8289 | if (Args.empty()) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 8290 | // Dig out the reference subobject which is uninitialized and diagnose it. |
| 8291 | // If this is value-initialization, this could be nested some way within |
| 8292 | // the target type. |
| 8293 | assert(Kind.getKind() == InitializationKind::IK_Value || |
| 8294 | DestType->isReferenceType()); |
| 8295 | bool Diagnosed = |
| 8296 | DiagnoseUninitializedReference(S, Kind.getLocation(), DestType); |
| 8297 | assert(Diagnosed && "couldn't find uninitialized reference to diagnose"); |
| 8298 | (void)Diagnosed; |
| 8299 | } else // FIXME: diagnostic below could be better! |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8300 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 8301 | << SourceRange(Args.front()->getBeginLoc(), Args.back()->getEndLoc()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8302 | break; |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 8303 | case FK_ParenthesizedListInitForReference: |
| 8304 | S.Diag(Kind.getLocation(), diag::err_list_init_in_parens) |
| 8305 | << 1 << Entity.getType() << Args[0]->getSourceRange(); |
| 8306 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8307 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8308 | case FK_ArrayNeedsInitList: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 8309 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 0; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8310 | break; |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 8311 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 8312 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 1; |
| 8313 | break; |
| 8314 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 8315 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 2; |
| 8316 | break; |
| 8317 | case FK_NarrowStringIntoWideCharArray: |
| 8318 | S.Diag(Kind.getLocation(), diag::err_array_init_narrow_string_into_wchar); |
| 8319 | break; |
| 8320 | case FK_WideStringIntoCharArray: |
| 8321 | S.Diag(Kind.getLocation(), diag::err_array_init_wide_string_into_char); |
| 8322 | break; |
| 8323 | case FK_IncompatWideStringIntoWideChar: |
| 8324 | S.Diag(Kind.getLocation(), |
| 8325 | diag::err_array_init_incompat_wide_string_into_wchar); |
| 8326 | break; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 8327 | case FK_PlainStringIntoUTF8Char: |
| 8328 | S.Diag(Kind.getLocation(), |
| 8329 | diag::err_array_init_plain_string_into_char8_t); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8330 | S.Diag(Args.front()->getBeginLoc(), |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 8331 | diag::note_array_init_plain_string_into_char8_t) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8332 | << FixItHint::CreateInsertion(Args.front()->getBeginLoc(), "u8"); |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 8333 | break; |
| 8334 | case FK_UTF8StringIntoPlainChar: |
| 8335 | S.Diag(Kind.getLocation(), |
Richard Smith | 28ddb91 | 2018-11-14 21:04:34 +0000 | [diff] [blame] | 8336 | diag::err_array_init_utf8_string_into_char) |
| 8337 | << S.getLangOpts().CPlusPlus2a; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 8338 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8339 | case FK_ArrayTypeMismatch: |
| 8340 | case FK_NonConstantArrayInit: |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 8341 | S.Diag(Kind.getLocation(), |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8342 | (Failure == FK_ArrayTypeMismatch |
| 8343 | ? diag::err_array_init_different_type |
| 8344 | : diag::err_array_init_non_constant_array)) |
| 8345 | << DestType.getNonReferenceType() |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8346 | << OnlyArg->getType() |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8347 | << Args[0]->getSourceRange(); |
| 8348 | break; |
| 8349 | |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 8350 | case FK_VariableLengthArrayHasInitializer: |
| 8351 | S.Diag(Kind.getLocation(), diag::err_variable_object_no_init) |
| 8352 | << Args[0]->getSourceRange(); |
| 8353 | break; |
| 8354 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8355 | case FK_AddressOfOverloadFailed: { |
| 8356 | DeclAccessPair Found; |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8357 | S.ResolveAddressOfOverloadedFunction(OnlyArg, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8358 | DestType.getNonReferenceType(), |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8359 | true, |
| 8360 | Found); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8361 | break; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8362 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8363 | |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 8364 | case FK_AddressOfUnaddressableFunction: { |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8365 | auto *FD = cast<FunctionDecl>(cast<DeclRefExpr>(OnlyArg)->getDecl()); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 8366 | S.checkAddressOfFunctionIsAvailable(FD, /*Complain=*/true, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8367 | OnlyArg->getBeginLoc()); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 8368 | break; |
| 8369 | } |
| 8370 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8371 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 8372 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8373 | switch (FailedOverloadResult) { |
| 8374 | case OR_Ambiguous: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8375 | if (Failure == FK_UserConversionOverloadFailed) |
| 8376 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8377 | << OnlyArg->getType() << DestType |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8378 | << Args[0]->getSourceRange(); |
| 8379 | else |
| 8380 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8381 | << DestType << OnlyArg->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8382 | << Args[0]->getSourceRange(); |
| 8383 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8384 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8385 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8386 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8387 | case OR_No_Viable_Function: |
Larisse Voufo | 70bb43a | 2013-06-27 03:36:30 +0000 | [diff] [blame] | 8388 | if (!S.RequireCompleteType(Kind.getLocation(), |
Larisse Voufo | 64cf3ef | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 8389 | DestType.getNonReferenceType(), |
| 8390 | diag::err_typecheck_nonviable_condition_incomplete, |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8391 | OnlyArg->getType(), Args[0]->getSourceRange())) |
Larisse Voufo | 64cf3ef | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 8392 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
Nick Lewycky | 08426e2 | 2015-08-25 22:18:46 +0000 | [diff] [blame] | 8393 | << (Entity.getKind() == InitializedEntity::EK_Result) |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8394 | << OnlyArg->getType() << Args[0]->getSourceRange() |
Larisse Voufo | 64cf3ef | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 8395 | << DestType.getNonReferenceType(); |
| 8396 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8397 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8398 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8399 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8400 | case OR_Deleted: { |
| 8401 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8402 | << OnlyArg->getType() << DestType.getNonReferenceType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8403 | << Args[0]->getSourceRange(); |
| 8404 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8405 | OverloadingResult Ovl |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 8406 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8407 | if (Ovl == OR_Deleted) { |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 8408 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8409 | } else { |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 8410 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8411 | } |
| 8412 | break; |
| 8413 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8414 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8415 | case OR_Success: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 8416 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8417 | } |
| 8418 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8419 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8420 | case FK_NonConstLValueReferenceBindingToTemporary: |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8421 | if (isa<InitListExpr>(Args[0])) { |
| 8422 | S.Diag(Kind.getLocation(), |
| 8423 | diag::err_lvalue_reference_bind_to_initlist) |
| 8424 | << DestType.getNonReferenceType().isVolatileQualified() |
| 8425 | << DestType.getNonReferenceType() |
| 8426 | << Args[0]->getSourceRange(); |
| 8427 | break; |
| 8428 | } |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 8429 | LLVM_FALLTHROUGH; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8430 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8431 | case FK_NonConstLValueReferenceBindingToUnrelated: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8432 | S.Diag(Kind.getLocation(), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8433 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 8434 | ? diag::err_lvalue_reference_bind_to_temporary |
| 8435 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | d1e0864 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 8436 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8437 | << DestType.getNonReferenceType() |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8438 | << OnlyArg->getType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8439 | << Args[0]->getSourceRange(); |
| 8440 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8441 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8442 | case FK_NonConstLValueReferenceBindingToBitfield: { |
| 8443 | // We don't necessarily have an unambiguous source bit-field. |
| 8444 | FieldDecl *BitField = Args[0]->getSourceBitField(); |
| 8445 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
| 8446 | << DestType.isVolatileQualified() |
| 8447 | << (BitField ? BitField->getDeclName() : DeclarationName()) |
| 8448 | << (BitField != nullptr) |
| 8449 | << Args[0]->getSourceRange(); |
| 8450 | if (BitField) |
| 8451 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
| 8452 | break; |
| 8453 | } |
| 8454 | |
| 8455 | case FK_NonConstLValueReferenceBindingToVectorElement: |
| 8456 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 8457 | << DestType.isVolatileQualified() |
| 8458 | << Args[0]->getSourceRange(); |
| 8459 | break; |
| 8460 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8461 | case FK_RValueReferenceBindingToLValue: |
| 8462 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8463 | << DestType.getNonReferenceType() << OnlyArg->getType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8464 | << Args[0]->getSourceRange(); |
| 8465 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8466 | |
Richard Trieu | f956a49 | 2015-05-16 01:27:03 +0000 | [diff] [blame] | 8467 | case FK_ReferenceInitDropsQualifiers: { |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8468 | QualType SourceType = OnlyArg->getType(); |
Richard Trieu | f956a49 | 2015-05-16 01:27:03 +0000 | [diff] [blame] | 8469 | QualType NonRefType = DestType.getNonReferenceType(); |
| 8470 | Qualifiers DroppedQualifiers = |
| 8471 | SourceType.getQualifiers() - NonRefType.getQualifiers(); |
| 8472 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8473 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
Richard Trieu | f956a49 | 2015-05-16 01:27:03 +0000 | [diff] [blame] | 8474 | << SourceType |
| 8475 | << NonRefType |
| 8476 | << DroppedQualifiers.getCVRQualifiers() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8477 | << Args[0]->getSourceRange(); |
| 8478 | break; |
Richard Trieu | f956a49 | 2015-05-16 01:27:03 +0000 | [diff] [blame] | 8479 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8480 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8481 | case FK_ReferenceInitFailed: |
| 8482 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 8483 | << DestType.getNonReferenceType() |
Eric Fiselier | 1147f71 | 2019-02-01 22:06:02 +0000 | [diff] [blame] | 8484 | << DestType.getNonReferenceType()->isIncompleteType() |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8485 | << OnlyArg->isLValue() |
| 8486 | << OnlyArg->getType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8487 | << Args[0]->getSourceRange(); |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 8488 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8489 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8490 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8491 | case FK_ConversionFailed: { |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8492 | QualType FromType = OnlyArg->getType(); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8493 | PartialDiagnostic PDiag = S.PDiag(diag::err_init_conversion_failed) |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8494 | << (int)Entity.getKind() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8495 | << DestType |
Nicolas Lesser | 8217a2a | 2018-07-12 17:43:49 +0000 | [diff] [blame] | 8496 | << OnlyArg->isLValue() |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8497 | << FromType |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8498 | << Args[0]->getSourceRange(); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8499 | S.HandleFunctionTypeMismatch(PDiag, FromType, DestType); |
| 8500 | S.Diag(Kind.getLocation(), PDiag); |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 8501 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8502 | break; |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8503 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8504 | |
| 8505 | case FK_ConversionFromPropertyFailed: |
| 8506 | // No-op. This error has already been reported. |
| 8507 | break; |
| 8508 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8509 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 8510 | SourceRange R; |
| 8511 | |
David Majnemer | bd38544 | 2015-04-10 04:52:06 +0000 | [diff] [blame] | 8512 | auto *InitList = dyn_cast<InitListExpr>(Args[0]); |
Benjamin Kramer | c4284e3 | 2015-09-23 16:03:53 +0000 | [diff] [blame] | 8513 | if (InitList && InitList->getNumInits() >= 1) { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 8514 | R = SourceRange(InitList->getInit(0)->getEndLoc(), InitList->getEndLoc()); |
Benjamin Kramer | c4284e3 | 2015-09-23 16:03:53 +0000 | [diff] [blame] | 8515 | } else { |
| 8516 | assert(Args.size() > 1 && "Expected multiple initializers!"); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 8517 | R = SourceRange(Args.front()->getEndLoc(), Args.back()->getEndLoc()); |
Benjamin Kramer | c4284e3 | 2015-09-23 16:03:53 +0000 | [diff] [blame] | 8518 | } |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8519 | |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 8520 | R.setBegin(S.getLocForEndOfToken(R.getBegin())); |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 8521 | if (Kind.isCStyleOrFunctionalCast()) |
| 8522 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 8523 | << R; |
| 8524 | else |
| 8525 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 8526 | << /*scalar=*/2 << R; |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8527 | break; |
| 8528 | } |
| 8529 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 8530 | case FK_ParenthesizedListInitForScalar: |
| 8531 | S.Diag(Kind.getLocation(), diag::err_list_init_in_parens) |
| 8532 | << 0 << Entity.getType() << Args[0]->getSourceRange(); |
| 8533 | break; |
| 8534 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 8535 | case FK_ReferenceBindingToInitList: |
| 8536 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 8537 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 8538 | break; |
| 8539 | |
| 8540 | case FK_InitListBadDestinationType: |
| 8541 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 8542 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 8543 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8544 | |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 8545 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8546 | case FK_ConstructorOverloadFailed: { |
| 8547 | SourceRange ArgsRange; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8548 | if (Args.size()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8549 | ArgsRange = |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 8550 | SourceRange(Args.front()->getBeginLoc(), Args.back()->getEndLoc()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8551 | |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 8552 | if (Failure == FK_ListConstructorOverloadFailed) { |
Nico Weber | 9709ebf | 2014-07-08 23:54:25 +0000 | [diff] [blame] | 8553 | assert(Args.size() == 1 && |
| 8554 | "List construction from other than 1 argument."); |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 8555 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8556 | Args = MultiExprArg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 8557 | } |
| 8558 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8559 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 8560 | // bad. |
| 8561 | switch (FailedOverloadResult) { |
| 8562 | case OR_Ambiguous: |
| 8563 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 8564 | << DestType << ArgsRange; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8565 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8566 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8567 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8568 | case OR_No_Viable_Function: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8569 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 8570 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 8571 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 8572 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 8573 | // This is implicit default initialization of a member or |
| 8574 | // base within a constructor. If no viable function was |
Nico Weber | a691689 | 2016-06-10 18:53:04 +0000 | [diff] [blame] | 8575 | // found, notify the user that they need to explicitly |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8576 | // initialize this base/member. |
| 8577 | CXXConstructorDecl *Constructor |
| 8578 | = cast<CXXConstructorDecl>(S.CurContext); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 8579 | const CXXRecordDecl *InheritedFrom = nullptr; |
| 8580 | if (auto Inherited = Constructor->getInheritedConstructor()) |
| 8581 | InheritedFrom = Inherited.getShadowDecl()->getNominatedBaseClass(); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8582 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 8583 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 8584 | << (InheritedFrom ? 2 : Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8585 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 8586 | << /*base=*/0 |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 8587 | << Entity.getType() |
| 8588 | << InheritedFrom; |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8589 | |
| 8590 | RecordDecl *BaseDecl |
| 8591 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 8592 | ->getDecl(); |
| 8593 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 8594 | << S.Context.getTagDeclType(BaseDecl); |
| 8595 | } else { |
| 8596 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 8597 | << (InheritedFrom ? 2 : Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8598 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 8599 | << /*member=*/1 |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 8600 | << Entity.getName() |
| 8601 | << InheritedFrom; |
Alp Toker | 2afa878 | 2014-05-28 12:20:14 +0000 | [diff] [blame] | 8602 | S.Diag(Entity.getDecl()->getLocation(), |
| 8603 | diag::note_member_declared_at); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8604 | |
| 8605 | if (const RecordType *Record |
| 8606 | = Entity.getType()->getAs<RecordType>()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8607 | S.Diag(Record->getDecl()->getLocation(), |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8608 | diag::note_previous_decl) |
| 8609 | << S.Context.getTagDeclType(Record->getDecl()); |
| 8610 | } |
| 8611 | break; |
| 8612 | } |
| 8613 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8614 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 8615 | << DestType << ArgsRange; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 8616 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8617 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8618 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8619 | case OR_Deleted: { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8620 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8621 | OverloadingResult Ovl |
| 8622 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 8623 | if (Ovl != OR_Deleted) { |
| 8624 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 8625 | << true << DestType << ArgsRange; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8626 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 8627 | break; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8628 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8629 | |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 8630 | // If this is a defaulted or implicitly-declared function, then |
| 8631 | // it was implicitly deleted. Make it clear that the deletion was |
| 8632 | // implicit. |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 8633 | if (S.isImplicitlyDeleted(Best->Function)) |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 8634 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_special_init) |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 8635 | << S.getSpecialMember(cast<CXXMethodDecl>(Best->Function)) |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 8636 | << DestType << ArgsRange; |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 8637 | else |
| 8638 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 8639 | << true << DestType << ArgsRange; |
| 8640 | |
| 8641 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8642 | break; |
| 8643 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8644 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8645 | case OR_Success: |
| 8646 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8647 | } |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 8648 | } |
David Blaikie | 60deeee | 2012-01-17 08:24:58 +0000 | [diff] [blame] | 8649 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8650 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 8651 | case FK_DefaultInitOfConst: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8652 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 8653 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 8654 | // This is implicit default-initialization of a const member in |
| 8655 | // a constructor. Complain that it needs to be explicitly |
| 8656 | // initialized. |
| 8657 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 8658 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 8659 | << (Constructor->getInheritedConstructor() ? 2 : |
| 8660 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8661 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 8662 | << /*const=*/1 |
| 8663 | << Entity.getName(); |
| 8664 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 8665 | << Entity.getName(); |
| 8666 | } else { |
| 8667 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
Nico Weber | 9386c82 | 2014-07-23 05:16:10 +0000 | [diff] [blame] | 8668 | << DestType << (bool)DestType->getAs<RecordType>(); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8669 | } |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 8670 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8671 | |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 8672 | case FK_Incomplete: |
Douglas Gregor | 85f3423 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 8673 | S.RequireCompleteType(Kind.getLocation(), FailedIncompleteType, |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 8674 | diag::err_init_incomplete_type); |
| 8675 | break; |
| 8676 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 8677 | case FK_ListInitializationFailed: { |
| 8678 | // Run the init list checker again to emit diagnostics. |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 8679 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
| 8680 | diagnoseListInit(S, Entity, InitList); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 8681 | break; |
| 8682 | } |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 8683 | |
| 8684 | case FK_PlaceholderType: { |
| 8685 | // FIXME: Already diagnosed! |
| 8686 | break; |
| 8687 | } |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 8688 | |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 8689 | case FK_ExplicitConstructor: { |
| 8690 | S.Diag(Kind.getLocation(), diag::err_selected_explicit_constructor) |
| 8691 | << Args[0]->getSourceRange(); |
| 8692 | OverloadCandidateSet::iterator Best; |
| 8693 | OverloadingResult Ovl |
| 8694 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Matt Beaumont-Gay | 5dcce09 | 2012-04-02 19:05:35 +0000 | [diff] [blame] | 8695 | (void)Ovl; |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 8696 | assert(Ovl == OR_Success && "Inconsistent overload resolution"); |
| 8697 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 8698 | S.Diag(CtorDecl->getLocation(), |
| 8699 | diag::note_explicit_ctor_deduction_guide_here) << false; |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 8700 | break; |
| 8701 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8702 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8703 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 8704 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 8705 | return true; |
| 8706 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 8707 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8708 | void InitializationSequence::dump(raw_ostream &OS) const { |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8709 | switch (SequenceKind) { |
| 8710 | case FailedSequence: { |
| 8711 | OS << "Failed sequence: "; |
| 8712 | switch (Failure) { |
| 8713 | case FK_TooManyInitsForReference: |
| 8714 | OS << "too many initializers for reference"; |
| 8715 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8716 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 8717 | case FK_ParenthesizedListInitForReference: |
| 8718 | OS << "parenthesized list init for reference"; |
| 8719 | break; |
| 8720 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8721 | case FK_ArrayNeedsInitList: |
| 8722 | OS << "array requires initializer list"; |
| 8723 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8724 | |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 8725 | case FK_AddressOfUnaddressableFunction: |
| 8726 | OS << "address of unaddressable function was taken"; |
| 8727 | break; |
| 8728 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8729 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 8730 | OS << "array requires initializer list or string literal"; |
| 8731 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8732 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 8733 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 8734 | OS << "array requires initializer list or wide string literal"; |
| 8735 | break; |
| 8736 | |
| 8737 | case FK_NarrowStringIntoWideCharArray: |
| 8738 | OS << "narrow string into wide char array"; |
| 8739 | break; |
| 8740 | |
| 8741 | case FK_WideStringIntoCharArray: |
| 8742 | OS << "wide string into char array"; |
| 8743 | break; |
| 8744 | |
| 8745 | case FK_IncompatWideStringIntoWideChar: |
| 8746 | OS << "incompatible wide string into wide char array"; |
| 8747 | break; |
| 8748 | |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 8749 | case FK_PlainStringIntoUTF8Char: |
| 8750 | OS << "plain string literal into char8_t array"; |
| 8751 | break; |
| 8752 | |
| 8753 | case FK_UTF8StringIntoPlainChar: |
| 8754 | OS << "u8 string literal into char array"; |
| 8755 | break; |
| 8756 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8757 | case FK_ArrayTypeMismatch: |
| 8758 | OS << "array type mismatch"; |
| 8759 | break; |
| 8760 | |
| 8761 | case FK_NonConstantArrayInit: |
| 8762 | OS << "non-constant array initializer"; |
| 8763 | break; |
| 8764 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8765 | case FK_AddressOfOverloadFailed: |
| 8766 | OS << "address of overloaded function failed"; |
| 8767 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8768 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8769 | case FK_ReferenceInitOverloadFailed: |
| 8770 | OS << "overload resolution for reference initialization failed"; |
| 8771 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8772 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8773 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 8774 | OS << "non-const lvalue reference bound to temporary"; |
| 8775 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8776 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8777 | case FK_NonConstLValueReferenceBindingToBitfield: |
| 8778 | OS << "non-const lvalue reference bound to bit-field"; |
| 8779 | break; |
| 8780 | |
| 8781 | case FK_NonConstLValueReferenceBindingToVectorElement: |
| 8782 | OS << "non-const lvalue reference bound to vector element"; |
| 8783 | break; |
| 8784 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8785 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 8786 | OS << "non-const lvalue reference bound to unrelated type"; |
| 8787 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8788 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8789 | case FK_RValueReferenceBindingToLValue: |
| 8790 | OS << "rvalue reference bound to an lvalue"; |
| 8791 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8792 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8793 | case FK_ReferenceInitDropsQualifiers: |
| 8794 | OS << "reference initialization drops qualifiers"; |
| 8795 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8796 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8797 | case FK_ReferenceInitFailed: |
| 8798 | OS << "reference initialization failed"; |
| 8799 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8800 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8801 | case FK_ConversionFailed: |
| 8802 | OS << "conversion failed"; |
| 8803 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8804 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8805 | case FK_ConversionFromPropertyFailed: |
| 8806 | OS << "conversion from property failed"; |
| 8807 | break; |
| 8808 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8809 | case FK_TooManyInitsForScalar: |
| 8810 | OS << "too many initializers for scalar"; |
| 8811 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8812 | |
Richard Smith | 49a6b6e | 2017-03-24 01:14:25 +0000 | [diff] [blame] | 8813 | case FK_ParenthesizedListInitForScalar: |
| 8814 | OS << "parenthesized list init for reference"; |
| 8815 | break; |
| 8816 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8817 | case FK_ReferenceBindingToInitList: |
| 8818 | OS << "referencing binding to initializer list"; |
| 8819 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8820 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8821 | case FK_InitListBadDestinationType: |
| 8822 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 8823 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8824 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8825 | case FK_UserConversionOverloadFailed: |
| 8826 | OS << "overloading failed for user-defined conversion"; |
| 8827 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8828 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8829 | case FK_ConstructorOverloadFailed: |
| 8830 | OS << "constructor overloading failed"; |
| 8831 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8832 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8833 | case FK_DefaultInitOfConst: |
| 8834 | OS << "default initialization of a const variable"; |
| 8835 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8836 | |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 8837 | case FK_Incomplete: |
| 8838 | OS << "initialization of incomplete type"; |
| 8839 | break; |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 8840 | |
| 8841 | case FK_ListInitializationFailed: |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 8842 | OS << "list initialization checker failure"; |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 8843 | break; |
| 8844 | |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 8845 | case FK_VariableLengthArrayHasInitializer: |
| 8846 | OS << "variable length array has an initializer"; |
| 8847 | break; |
| 8848 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 8849 | case FK_PlaceholderType: |
| 8850 | OS << "initializer expression isn't contextually valid"; |
| 8851 | break; |
Nick Lewycky | 097f47c | 2011-12-22 20:21:32 +0000 | [diff] [blame] | 8852 | |
| 8853 | case FK_ListConstructorOverloadFailed: |
| 8854 | OS << "list constructor overloading failed"; |
| 8855 | break; |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 8856 | |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 8857 | case FK_ExplicitConstructor: |
| 8858 | OS << "list copy initialization chose explicit constructor"; |
| 8859 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8860 | } |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8861 | OS << '\n'; |
| 8862 | return; |
| 8863 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8864 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8865 | case DependentSequence: |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 8866 | OS << "Dependent sequence\n"; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8867 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8868 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 8869 | case NormalSequence: |
| 8870 | OS << "Normal sequence: "; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8871 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8872 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8873 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8874 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 8875 | if (S != step_begin()) { |
| 8876 | OS << " -> "; |
| 8877 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8878 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8879 | switch (S->Kind) { |
| 8880 | case SK_ResolveAddressOfOverloadedFunction: |
| 8881 | OS << "resolve address of overloaded function"; |
| 8882 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8883 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8884 | case SK_CastDerivedToBaseRValue: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8885 | OS << "derived-to-base (rvalue)"; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8886 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8887 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 8888 | case SK_CastDerivedToBaseXValue: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8889 | OS << "derived-to-base (xvalue)"; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 8890 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8891 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8892 | case SK_CastDerivedToBaseLValue: |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8893 | OS << "derived-to-base (lvalue)"; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8894 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8895 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8896 | case SK_BindReference: |
| 8897 | OS << "bind reference to lvalue"; |
| 8898 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8899 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8900 | case SK_BindReferenceToTemporary: |
| 8901 | OS << "bind reference to a temporary"; |
| 8902 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8903 | |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 8904 | case SK_FinalCopy: |
| 8905 | OS << "final copy in class direct-initialization"; |
| 8906 | break; |
| 8907 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 8908 | case SK_ExtraneousCopyToTemporary: |
| 8909 | OS << "extraneous C++03 copy to temporary"; |
| 8910 | break; |
| 8911 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8912 | case SK_UserConversion: |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 8913 | OS << "user-defined conversion via " << *S->Function.Function; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8914 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 8915 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8916 | case SK_QualificationConversionRValue: |
| 8917 | OS << "qualification conversion (rvalue)"; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8918 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8919 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 8920 | case SK_QualificationConversionXValue: |
| 8921 | OS << "qualification conversion (xvalue)"; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8922 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 8923 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8924 | case SK_QualificationConversionLValue: |
| 8925 | OS << "qualification conversion (lvalue)"; |
| 8926 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8927 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 8928 | case SK_AtomicConversion: |
| 8929 | OS << "non-atomic-to-atomic conversion"; |
| 8930 | break; |
| 8931 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 8932 | case SK_LValueToRValue: |
| 8933 | OS << "load (lvalue to rvalue)"; |
| 8934 | break; |
| 8935 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8936 | case SK_ConversionSequence: |
| 8937 | OS << "implicit conversion sequence ("; |
Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 8938 | S->ICS->dump(); // FIXME: use OS |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8939 | OS << ")"; |
| 8940 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8941 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 8942 | case SK_ConversionSequenceNoNarrowing: |
| 8943 | OS << "implicit conversion sequence with narrowing prohibited ("; |
Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 8944 | S->ICS->dump(); // FIXME: use OS |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 8945 | OS << ")"; |
| 8946 | break; |
| 8947 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8948 | case SK_ListInitialization: |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 8949 | OS << "list aggregate initialization"; |
| 8950 | break; |
| 8951 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 8952 | case SK_UnwrapInitList: |
| 8953 | OS << "unwrap reference initializer list"; |
| 8954 | break; |
| 8955 | |
| 8956 | case SK_RewrapInitList: |
| 8957 | OS << "rewrap reference initializer list"; |
| 8958 | break; |
| 8959 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8960 | case SK_ConstructorInitialization: |
| 8961 | OS << "constructor initialization"; |
| 8962 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8963 | |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 8964 | case SK_ConstructorInitializationFromList: |
| 8965 | OS << "list initialization via constructor"; |
| 8966 | break; |
| 8967 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8968 | case SK_ZeroInitialization: |
| 8969 | OS << "zero initialization"; |
| 8970 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8971 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8972 | case SK_CAssignment: |
| 8973 | OS << "C assignment"; |
| 8974 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8975 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 8976 | case SK_StringInit: |
| 8977 | OS << "string initialization"; |
| 8978 | break; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 8979 | |
| 8980 | case SK_ObjCObjectConversion: |
| 8981 | OS << "Objective-C object conversion"; |
| 8982 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8983 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 8984 | case SK_ArrayLoopIndex: |
| 8985 | OS << "indexing for array initialization loop"; |
| 8986 | break; |
| 8987 | |
| 8988 | case SK_ArrayLoopInit: |
| 8989 | OS << "array initialization loop"; |
| 8990 | break; |
| 8991 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 8992 | case SK_ArrayInit: |
| 8993 | OS << "array initialization"; |
| 8994 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8995 | |
Richard Smith | 378b8c8 | 2016-12-14 03:22:16 +0000 | [diff] [blame] | 8996 | case SK_GNUArrayInit: |
| 8997 | OS << "array initialization (GNU extension)"; |
| 8998 | break; |
| 8999 | |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 9000 | case SK_ParenthesizedArrayInit: |
| 9001 | OS << "parenthesized array initialization"; |
| 9002 | break; |
| 9003 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9004 | case SK_PassByIndirectCopyRestore: |
| 9005 | OS << "pass by indirect copy and restore"; |
| 9006 | break; |
| 9007 | |
| 9008 | case SK_PassByIndirectRestore: |
| 9009 | OS << "pass by indirect restore"; |
| 9010 | break; |
| 9011 | |
| 9012 | case SK_ProduceObjCObject: |
| 9013 | OS << "Objective-C object retension"; |
| 9014 | break; |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 9015 | |
| 9016 | case SK_StdInitializerList: |
| 9017 | OS << "std::initializer_list from initializer list"; |
| 9018 | break; |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 9019 | |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 9020 | case SK_StdInitializerListConstructorCall: |
| 9021 | OS << "list initialization from std::initializer_list"; |
| 9022 | break; |
| 9023 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 9024 | case SK_OCLSamplerInit: |
| 9025 | OS << "OpenCL sampler_t from integer constant"; |
| 9026 | break; |
| 9027 | |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 9028 | case SK_OCLZeroOpaqueType: |
| 9029 | OS << "OpenCL opaque type from zero"; |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 9030 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9031 | } |
Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 9032 | |
| 9033 | OS << " [" << S->Type.getAsString() << ']'; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9034 | } |
Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 9035 | |
| 9036 | OS << '\n'; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 9037 | } |
| 9038 | |
| 9039 | void InitializationSequence::dump() const { |
| 9040 | dump(llvm::errs()); |
| 9041 | } |
| 9042 | |
Nico Weber | 3d7f00d | 2018-06-19 23:19:34 +0000 | [diff] [blame] | 9043 | static bool NarrowingErrs(const LangOptions &L) { |
| 9044 | return L.CPlusPlus11 && |
| 9045 | (!L.MicrosoftExt || L.isCompatibleWithMSVC(LangOptions::MSVC2015)); |
| 9046 | } |
| 9047 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 9048 | static void DiagnoseNarrowingInInitList(Sema &S, |
| 9049 | const ImplicitConversionSequence &ICS, |
| 9050 | QualType PreNarrowingType, |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9051 | QualType EntityType, |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9052 | const Expr *PostInit) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9053 | const StandardConversionSequence *SCS = nullptr; |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9054 | switch (ICS.getKind()) { |
| 9055 | case ImplicitConversionSequence::StandardConversion: |
| 9056 | SCS = &ICS.Standard; |
| 9057 | break; |
| 9058 | case ImplicitConversionSequence::UserDefinedConversion: |
| 9059 | SCS = &ICS.UserDefined.After; |
| 9060 | break; |
| 9061 | case ImplicitConversionSequence::AmbiguousConversion: |
| 9062 | case ImplicitConversionSequence::EllipsisConversion: |
| 9063 | case ImplicitConversionSequence::BadConversion: |
| 9064 | return; |
| 9065 | } |
| 9066 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9067 | // C++11 [dcl.init.list]p7: Check whether this is a narrowing conversion. |
| 9068 | APValue ConstantValue; |
Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 9069 | QualType ConstantType; |
| 9070 | switch (SCS->getNarrowingKind(S.Context, PostInit, ConstantValue, |
| 9071 | ConstantType)) { |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9072 | case NK_Not_Narrowing: |
Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 9073 | case NK_Dependent_Narrowing: |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9074 | // No narrowing occurred. |
| 9075 | return; |
| 9076 | |
| 9077 | case NK_Type_Narrowing: |
| 9078 | // This was a floating-to-integer conversion, which is always considered a |
| 9079 | // narrowing conversion even if the value is a constant and can be |
| 9080 | // represented exactly as an integer. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9081 | S.Diag(PostInit->getBeginLoc(), NarrowingErrs(S.getLangOpts()) |
Nico Weber | 3d7f00d | 2018-06-19 23:19:34 +0000 | [diff] [blame] | 9082 | ? diag::ext_init_list_type_narrowing |
| 9083 | : diag::warn_init_list_type_narrowing) |
| 9084 | << PostInit->getSourceRange() |
| 9085 | << PreNarrowingType.getLocalUnqualifiedType() |
| 9086 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9087 | break; |
| 9088 | |
| 9089 | case NK_Constant_Narrowing: |
| 9090 | // A constant value was narrowed. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9091 | S.Diag(PostInit->getBeginLoc(), |
Nico Weber | 3d7f00d | 2018-06-19 23:19:34 +0000 | [diff] [blame] | 9092 | NarrowingErrs(S.getLangOpts()) |
| 9093 | ? diag::ext_init_list_constant_narrowing |
| 9094 | : diag::warn_init_list_constant_narrowing) |
| 9095 | << PostInit->getSourceRange() |
| 9096 | << ConstantValue.getAsString(S.getASTContext(), ConstantType) |
| 9097 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9098 | break; |
| 9099 | |
| 9100 | case NK_Variable_Narrowing: |
| 9101 | // A variable's value may have been narrowed. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9102 | S.Diag(PostInit->getBeginLoc(), |
Nico Weber | 3d7f00d | 2018-06-19 23:19:34 +0000 | [diff] [blame] | 9103 | NarrowingErrs(S.getLangOpts()) |
| 9104 | ? diag::ext_init_list_variable_narrowing |
| 9105 | : diag::warn_init_list_variable_narrowing) |
| 9106 | << PostInit->getSourceRange() |
| 9107 | << PreNarrowingType.getLocalUnqualifiedType() |
| 9108 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9109 | break; |
| 9110 | } |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 9111 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 9112 | SmallString<128> StaticCast; |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 9113 | llvm::raw_svector_ostream OS(StaticCast); |
| 9114 | OS << "static_cast<"; |
| 9115 | if (const TypedefType *TT = EntityType->getAs<TypedefType>()) { |
| 9116 | // It's important to use the typedef's name if there is one so that the |
| 9117 | // fixit doesn't break code using types like int64_t. |
| 9118 | // |
| 9119 | // FIXME: This will break if the typedef requires qualification. But |
| 9120 | // getQualifiedNameAsString() includes non-machine-parsable components. |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 9121 | OS << *TT->getDecl(); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 9122 | } else if (const BuiltinType *BT = EntityType->getAs<BuiltinType>()) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9123 | OS << BT->getName(S.getLangOpts()); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 9124 | else { |
| 9125 | // Oops, we didn't find the actual type of the variable. Don't emit a fixit |
| 9126 | // with a broken cast. |
| 9127 | return; |
| 9128 | } |
| 9129 | OS << ">("; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9130 | S.Diag(PostInit->getBeginLoc(), diag::note_init_list_narrowing_silence) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 9131 | << PostInit->getSourceRange() |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9132 | << FixItHint::CreateInsertion(PostInit->getBeginLoc(), OS.str()) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 9133 | << FixItHint::CreateInsertion( |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 9134 | S.getLocForEndOfToken(PostInit->getEndLoc()), ")"); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 9135 | } |
| 9136 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 9137 | //===----------------------------------------------------------------------===// |
| 9138 | // Initialization helper functions |
| 9139 | //===----------------------------------------------------------------------===// |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 9140 | bool |
| 9141 | Sema::CanPerformCopyInitialization(const InitializedEntity &Entity, |
| 9142 | ExprResult Init) { |
| 9143 | if (Init.isInvalid()) |
| 9144 | return false; |
| 9145 | |
| 9146 | Expr *InitE = Init.get(); |
| 9147 | assert(InitE && "No initialization expression"); |
| 9148 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9149 | InitializationKind Kind = |
| 9150 | InitializationKind::CreateCopy(InitE->getBeginLoc(), SourceLocation()); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 9151 | InitializationSequence Seq(*this, Entity, Kind, InitE); |
Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 9152 | return !Seq.Failed(); |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 9153 | } |
| 9154 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9155 | ExprResult |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 9156 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 9157 | SourceLocation EqualLoc, |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 9158 | ExprResult Init, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 9159 | bool TopLevelOfInitList, |
| 9160 | bool AllowExplicit) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 9161 | if (Init.isInvalid()) |
| 9162 | return ExprError(); |
| 9163 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 9164 | Expr *InitE = Init.get(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 9165 | assert(InitE && "No initialization expression?"); |
| 9166 | |
| 9167 | if (EqualLoc.isInvalid()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9168 | EqualLoc = InitE->getBeginLoc(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 9169 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9170 | InitializationKind Kind = InitializationKind::CreateCopy( |
| 9171 | InitE->getBeginLoc(), EqualLoc, AllowExplicit); |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 9172 | InitializationSequence Seq(*this, Entity, Kind, InitE, TopLevelOfInitList); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 9173 | |
Alex Lorenz | de69ff9 | 2017-05-16 10:23:58 +0000 | [diff] [blame] | 9174 | // Prevent infinite recursion when performing parameter copy-initialization. |
| 9175 | const bool ShouldTrackCopy = |
| 9176 | Entity.isParameterKind() && Seq.isConstructorInitialization(); |
| 9177 | if (ShouldTrackCopy) { |
| 9178 | if (llvm::find(CurrentParameterCopyTypes, Entity.getType()) != |
| 9179 | CurrentParameterCopyTypes.end()) { |
| 9180 | Seq.SetOverloadFailure( |
| 9181 | InitializationSequence::FK_ConstructorOverloadFailed, |
| 9182 | OR_No_Viable_Function); |
| 9183 | |
| 9184 | // Try to give a meaningful diagnostic note for the problematic |
| 9185 | // constructor. |
| 9186 | const auto LastStep = Seq.step_end() - 1; |
| 9187 | assert(LastStep->Kind == |
| 9188 | InitializationSequence::SK_ConstructorInitialization); |
| 9189 | const FunctionDecl *Function = LastStep->Function.Function; |
| 9190 | auto Candidate = |
| 9191 | llvm::find_if(Seq.getFailedCandidateSet(), |
| 9192 | [Function](const OverloadCandidate &Candidate) -> bool { |
| 9193 | return Candidate.Viable && |
| 9194 | Candidate.Function == Function && |
| 9195 | Candidate.Conversions.size() > 0; |
| 9196 | }); |
| 9197 | if (Candidate != Seq.getFailedCandidateSet().end() && |
| 9198 | Function->getNumParams() > 0) { |
| 9199 | Candidate->Viable = false; |
| 9200 | Candidate->FailureKind = ovl_fail_bad_conversion; |
| 9201 | Candidate->Conversions[0].setBad(BadConversionSequence::no_conversion, |
| 9202 | InitE, |
| 9203 | Function->getParamDecl(0)->getType()); |
| 9204 | } |
| 9205 | } |
| 9206 | CurrentParameterCopyTypes.push_back(Entity.getType()); |
| 9207 | } |
| 9208 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 9209 | ExprResult Result = Seq.Perform(*this, Entity, Kind, InitE); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9210 | |
Alex Lorenz | de69ff9 | 2017-05-16 10:23:58 +0000 | [diff] [blame] | 9211 | if (ShouldTrackCopy) |
| 9212 | CurrentParameterCopyTypes.pop_back(); |
| 9213 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9214 | return Result; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 9215 | } |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9216 | |
Richard Smith | 1363e8f | 2017-09-07 07:22:36 +0000 | [diff] [blame] | 9217 | /// Determine whether RD is, or is derived from, a specialization of CTD. |
| 9218 | static bool isOrIsDerivedFromSpecializationOf(CXXRecordDecl *RD, |
| 9219 | ClassTemplateDecl *CTD) { |
| 9220 | auto NotSpecialization = [&] (const CXXRecordDecl *Candidate) { |
| 9221 | auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Candidate); |
| 9222 | return !CTSD || !declaresSameEntity(CTSD->getSpecializedTemplate(), CTD); |
| 9223 | }; |
| 9224 | return !(NotSpecialization(RD) && RD->forallBases(NotSpecialization)); |
| 9225 | } |
| 9226 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9227 | QualType Sema::DeduceTemplateSpecializationFromInitializer( |
| 9228 | TypeSourceInfo *TSInfo, const InitializedEntity &Entity, |
| 9229 | const InitializationKind &Kind, MultiExprArg Inits) { |
| 9230 | auto *DeducedTST = dyn_cast<DeducedTemplateSpecializationType>( |
| 9231 | TSInfo->getType()->getContainedDeducedType()); |
| 9232 | assert(DeducedTST && "not a deduced template specialization type"); |
| 9233 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9234 | auto TemplateName = DeducedTST->getTemplateName(); |
Richard Smith | cff4201 | 2018-09-28 03:18:53 +0000 | [diff] [blame] | 9235 | if (TemplateName.isDependent()) |
| 9236 | return Context.DependentTy; |
| 9237 | |
| 9238 | // We can only perform deduction for class templates. |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9239 | auto *Template = |
| 9240 | dyn_cast_or_null<ClassTemplateDecl>(TemplateName.getAsTemplateDecl()); |
| 9241 | if (!Template) { |
| 9242 | Diag(Kind.getLocation(), |
| 9243 | diag::err_deduced_non_class_template_specialization_type) |
| 9244 | << (int)getTemplateNameKindForDiagnostics(TemplateName) << TemplateName; |
| 9245 | if (auto *TD = TemplateName.getAsTemplateDecl()) |
| 9246 | Diag(TD->getLocation(), diag::note_template_decl_here); |
| 9247 | return QualType(); |
| 9248 | } |
| 9249 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9250 | // Can't deduce from dependent arguments. |
Richard Smith | 8eeb16f | 2018-09-10 20:31:03 +0000 | [diff] [blame] | 9251 | if (Expr::hasAnyTypeDependentArguments(Inits)) { |
| 9252 | Diag(TSInfo->getTypeLoc().getBeginLoc(), |
| 9253 | diag::warn_cxx14_compat_class_template_argument_deduction) |
| 9254 | << TSInfo->getTypeLoc().getSourceRange() << 0; |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9255 | return Context.DependentTy; |
Richard Smith | 8eeb16f | 2018-09-10 20:31:03 +0000 | [diff] [blame] | 9256 | } |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9257 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9258 | // FIXME: Perform "exact type" matching first, per CWG discussion? |
| 9259 | // Or implement this via an implied 'T(T) -> T' deduction guide? |
| 9260 | |
| 9261 | // FIXME: Do we need/want a std::initializer_list<T> special case? |
| 9262 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9263 | // Look up deduction guides, including those synthesized from constructors. |
| 9264 | // |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9265 | // C++1z [over.match.class.deduct]p1: |
| 9266 | // A set of functions and function templates is formed comprising: |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9267 | // - For each constructor of the class template designated by the |
| 9268 | // template-name, a function template [...] |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9269 | // - For each deduction-guide, a function or function template [...] |
| 9270 | DeclarationNameInfo NameInfo( |
| 9271 | Context.DeclarationNames.getCXXDeductionGuideName(Template), |
| 9272 | TSInfo->getTypeLoc().getEndLoc()); |
| 9273 | LookupResult Guides(*this, NameInfo, LookupOrdinaryName); |
| 9274 | LookupQualifiedName(Guides, Template->getDeclContext()); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9275 | |
| 9276 | // FIXME: Do not diagnose inaccessible deduction guides. The standard isn't |
| 9277 | // clear on this, but they're not found by name so access does not apply. |
| 9278 | Guides.suppressDiagnostics(); |
| 9279 | |
| 9280 | // Figure out if this is list-initialization. |
| 9281 | InitListExpr *ListInit = |
| 9282 | (Inits.size() == 1 && Kind.getKind() != InitializationKind::IK_Direct) |
| 9283 | ? dyn_cast<InitListExpr>(Inits[0]) |
| 9284 | : nullptr; |
| 9285 | |
| 9286 | // C++1z [over.match.class.deduct]p1: |
| 9287 | // Initialization and overload resolution are performed as described in |
| 9288 | // [dcl.init] and [over.match.ctor], [over.match.copy], or [over.match.list] |
| 9289 | // (as appropriate for the type of initialization performed) for an object |
| 9290 | // of a hypothetical class type, where the selected functions and function |
| 9291 | // templates are considered to be the constructors of that class type |
| 9292 | // |
| 9293 | // Since we know we're initializing a class type of a type unrelated to that |
| 9294 | // of the initializer, this reduces to something fairly reasonable. |
| 9295 | OverloadCandidateSet Candidates(Kind.getLocation(), |
| 9296 | OverloadCandidateSet::CSK_Normal); |
| 9297 | OverloadCandidateSet::iterator Best; |
Eric Fiselier | 73b51ae | 2019-01-17 21:44:24 +0000 | [diff] [blame] | 9298 | |
| 9299 | bool HasAnyDeductionGuide = false; |
| 9300 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9301 | auto tryToResolveOverload = |
| 9302 | [&](bool OnlyListConstructors) -> OverloadingResult { |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 9303 | Candidates.clear(OverloadCandidateSet::CSK_Normal); |
Eric Fiselier | 73b51ae | 2019-01-17 21:44:24 +0000 | [diff] [blame] | 9304 | HasAnyDeductionGuide = false; |
| 9305 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9306 | for (auto I = Guides.begin(), E = Guides.end(); I != E; ++I) { |
| 9307 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9308 | if (D->isInvalidDecl()) |
| 9309 | continue; |
| 9310 | |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9311 | auto *TD = dyn_cast<FunctionTemplateDecl>(D); |
| 9312 | auto *GD = dyn_cast_or_null<CXXDeductionGuideDecl>( |
| 9313 | TD ? TD->getTemplatedDecl() : dyn_cast<FunctionDecl>(D)); |
| 9314 | if (!GD) |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9315 | continue; |
| 9316 | |
Eric Fiselier | 73b51ae | 2019-01-17 21:44:24 +0000 | [diff] [blame] | 9317 | if (!GD->isImplicit()) |
| 9318 | HasAnyDeductionGuide = true; |
| 9319 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9320 | // C++ [over.match.ctor]p1: (non-list copy-initialization from non-class) |
| 9321 | // For copy-initialization, the candidate functions are all the |
| 9322 | // converting constructors (12.3.1) of that class. |
| 9323 | // C++ [over.match.copy]p1: (non-list copy-initialization from class) |
| 9324 | // The converting constructors of T are candidate functions. |
| 9325 | if (Kind.isCopyInit() && !ListInit) { |
Richard Smith | afe4aa8 | 2017-02-10 02:19:05 +0000 | [diff] [blame] | 9326 | // Only consider converting constructors. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9327 | if (GD->isExplicit()) |
Richard Smith | afe4aa8 | 2017-02-10 02:19:05 +0000 | [diff] [blame] | 9328 | continue; |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9329 | |
| 9330 | // When looking for a converting constructor, deduction guides that |
Richard Smith | afe4aa8 | 2017-02-10 02:19:05 +0000 | [diff] [blame] | 9331 | // could never be called with one argument are not interesting to |
| 9332 | // check or note. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9333 | if (GD->getMinRequiredArguments() > 1 || |
| 9334 | (GD->getNumParams() == 0 && !GD->isVariadic())) |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9335 | continue; |
| 9336 | } |
| 9337 | |
| 9338 | // C++ [over.match.list]p1.1: (first phase list initialization) |
| 9339 | // Initially, the candidate functions are the initializer-list |
| 9340 | // constructors of the class T |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9341 | if (OnlyListConstructors && !isInitListConstructor(GD)) |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9342 | continue; |
| 9343 | |
| 9344 | // C++ [over.match.list]p1.2: (second phase list initialization) |
| 9345 | // the candidate functions are all the constructors of the class T |
| 9346 | // C++ [over.match.ctor]p1: (all other cases) |
| 9347 | // the candidate functions are all the constructors of the class of |
| 9348 | // the object being initialized |
| 9349 | |
| 9350 | // C++ [over.best.ics]p4: |
| 9351 | // When [...] the constructor [...] is a candidate by |
| 9352 | // - [over.match.copy] (in all cases) |
| 9353 | // FIXME: The "second phase of [over.match.list] case can also |
| 9354 | // theoretically happen here, but it's not clear whether we can |
| 9355 | // ever have a parameter of the right type. |
| 9356 | bool SuppressUserConversions = Kind.isCopyInit(); |
| 9357 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9358 | if (TD) |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9359 | AddTemplateOverloadCandidate(TD, I.getPair(), /*ExplicitArgs*/ nullptr, |
| 9360 | Inits, Candidates, |
| 9361 | SuppressUserConversions); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9362 | else |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9363 | AddOverloadCandidate(GD, I.getPair(), Inits, Candidates, |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9364 | SuppressUserConversions); |
| 9365 | } |
| 9366 | return Candidates.BestViableFunction(*this, Kind.getLocation(), Best); |
| 9367 | }; |
| 9368 | |
| 9369 | OverloadingResult Result = OR_No_Viable_Function; |
| 9370 | |
| 9371 | // C++11 [over.match.list]p1, per DR1467: for list-initialization, first |
| 9372 | // try initializer-list constructors. |
| 9373 | if (ListInit) { |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9374 | bool TryListConstructors = true; |
| 9375 | |
| 9376 | // Try list constructors unless the list is empty and the class has one or |
| 9377 | // more default constructors, in which case those constructors win. |
| 9378 | if (!ListInit->getNumInits()) { |
| 9379 | for (NamedDecl *D : Guides) { |
| 9380 | auto *FD = dyn_cast<FunctionDecl>(D->getUnderlyingDecl()); |
| 9381 | if (FD && FD->getMinRequiredArguments() == 0) { |
| 9382 | TryListConstructors = false; |
| 9383 | break; |
| 9384 | } |
| 9385 | } |
Richard Smith | 1363e8f | 2017-09-07 07:22:36 +0000 | [diff] [blame] | 9386 | } else if (ListInit->getNumInits() == 1) { |
| 9387 | // C++ [over.match.class.deduct]: |
| 9388 | // As an exception, the first phase in [over.match.list] (considering |
| 9389 | // initializer-list constructors) is omitted if the initializer list |
| 9390 | // consists of a single expression of type cv U, where U is a |
| 9391 | // specialization of C or a class derived from a specialization of C. |
| 9392 | Expr *E = ListInit->getInit(0); |
| 9393 | auto *RD = E->getType()->getAsCXXRecordDecl(); |
| 9394 | if (!isa<InitListExpr>(E) && RD && |
Erik Pilkington | dd0b344 | 2018-07-26 23:40:42 +0000 | [diff] [blame] | 9395 | isCompleteType(Kind.getLocation(), E->getType()) && |
Richard Smith | 1363e8f | 2017-09-07 07:22:36 +0000 | [diff] [blame] | 9396 | isOrIsDerivedFromSpecializationOf(RD, Template)) |
| 9397 | TryListConstructors = false; |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9398 | } |
| 9399 | |
| 9400 | if (TryListConstructors) |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9401 | Result = tryToResolveOverload(/*OnlyListConstructor*/true); |
| 9402 | // Then unwrap the initializer list and try again considering all |
| 9403 | // constructors. |
| 9404 | Inits = MultiExprArg(ListInit->getInits(), ListInit->getNumInits()); |
| 9405 | } |
| 9406 | |
| 9407 | // If list-initialization fails, or if we're doing any other kind of |
| 9408 | // initialization, we (eventually) consider constructors. |
| 9409 | if (Result == OR_No_Viable_Function) |
| 9410 | Result = tryToResolveOverload(/*OnlyListConstructor*/false); |
| 9411 | |
| 9412 | switch (Result) { |
| 9413 | case OR_Ambiguous: |
| 9414 | Diag(Kind.getLocation(), diag::err_deduced_class_template_ctor_ambiguous) |
| 9415 | << TemplateName; |
| 9416 | // FIXME: For list-initialization candidates, it'd usually be better to |
| 9417 | // list why they were not viable when given the initializer list itself as |
| 9418 | // an argument. |
| 9419 | Candidates.NoteCandidates(*this, OCD_ViableCandidates, Inits); |
| 9420 | return QualType(); |
| 9421 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9422 | case OR_No_Viable_Function: { |
| 9423 | CXXRecordDecl *Primary = |
| 9424 | cast<ClassTemplateDecl>(Template)->getTemplatedDecl(); |
| 9425 | bool Complete = |
| 9426 | isCompleteType(Kind.getLocation(), Context.getTypeDeclType(Primary)); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9427 | Diag(Kind.getLocation(), |
| 9428 | Complete ? diag::err_deduced_class_template_ctor_no_viable |
| 9429 | : diag::err_deduced_class_template_incomplete) |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9430 | << TemplateName << !Guides.empty(); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9431 | Candidates.NoteCandidates(*this, OCD_AllCandidates, Inits); |
| 9432 | return QualType(); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 9433 | } |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9434 | |
| 9435 | case OR_Deleted: { |
| 9436 | Diag(Kind.getLocation(), diag::err_deduced_class_template_deleted) |
| 9437 | << TemplateName; |
| 9438 | NoteDeletedFunction(Best->Function); |
| 9439 | return QualType(); |
| 9440 | } |
| 9441 | |
| 9442 | case OR_Success: |
| 9443 | // C++ [over.match.list]p1: |
| 9444 | // In copy-list-initialization, if an explicit constructor is chosen, the |
| 9445 | // initialization is ill-formed. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 9446 | if (Kind.isCopyInit() && ListInit && |
| 9447 | cast<CXXDeductionGuideDecl>(Best->Function)->isExplicit()) { |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9448 | bool IsDeductionGuide = !Best->Function->isImplicit(); |
| 9449 | Diag(Kind.getLocation(), diag::err_deduced_class_template_explicit) |
| 9450 | << TemplateName << IsDeductionGuide; |
| 9451 | Diag(Best->Function->getLocation(), |
| 9452 | diag::note_explicit_ctor_deduction_guide_here) |
| 9453 | << IsDeductionGuide; |
| 9454 | return QualType(); |
| 9455 | } |
| 9456 | |
| 9457 | // Make sure we didn't select an unusable deduction guide, and mark it |
| 9458 | // as referenced. |
| 9459 | DiagnoseUseOfDecl(Best->Function, Kind.getLocation()); |
| 9460 | MarkFunctionReferenced(Kind.getLocation(), Best->Function); |
| 9461 | break; |
| 9462 | } |
| 9463 | |
| 9464 | // C++ [dcl.type.class.deduct]p1: |
| 9465 | // The placeholder is replaced by the return type of the function selected |
| 9466 | // by overload resolution for class template deduction. |
Richard Smith | 8eeb16f | 2018-09-10 20:31:03 +0000 | [diff] [blame] | 9467 | QualType DeducedType = |
| 9468 | SubstAutoType(TSInfo->getType(), Best->Function->getReturnType()); |
| 9469 | Diag(TSInfo->getTypeLoc().getBeginLoc(), |
| 9470 | diag::warn_cxx14_compat_class_template_argument_deduction) |
| 9471 | << TSInfo->getTypeLoc().getSourceRange() << 1 << DeducedType; |
Eric Fiselier | 73b51ae | 2019-01-17 21:44:24 +0000 | [diff] [blame] | 9472 | |
| 9473 | // Warn if CTAD was used on a type that does not have any user-defined |
| 9474 | // deduction guides. |
| 9475 | if (!HasAnyDeductionGuide) { |
| 9476 | Diag(TSInfo->getTypeLoc().getBeginLoc(), |
| 9477 | diag::warn_ctad_maybe_unsupported) |
| 9478 | << TemplateName; |
| 9479 | Diag(Template->getLocation(), diag::note_suppress_ctad_maybe_unsupported); |
| 9480 | } |
| 9481 | |
Richard Smith | 8eeb16f | 2018-09-10 20:31:03 +0000 | [diff] [blame] | 9482 | return DeducedType; |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 9483 | } |