Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1 | //===--- SemaInit.cpp - Semantic Analysis for Initializers ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Sebastian Redl | 26bcc94 | 2011-09-24 17:47:39 +0000 | [diff] [blame] | 10 | // This file implements semantic analysis for initializers. |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 11 | // |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Douglas Gregor | c3a6ade | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 14 | #include "clang/Sema/Initialization.h" |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 98cee2f | 2009-05-27 16:10:08 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprCXX.h" |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 18 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeLoc.h" |
James Molloy | 9eef265 | 2014-06-20 14:35:13 +0000 | [diff] [blame] | 20 | #include "clang/Basic/TargetInfo.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "clang/Sema/Designator.h" |
| 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" |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 28 | #include <map> |
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 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 35 | /// \brief Check whether T is compatible with a wide character type (wchar_t, |
| 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, |
| 52 | SIF_Other |
| 53 | }; |
| 54 | |
| 55 | /// \brief Check whether the array of type AT can be initialized by the Init |
| 56 | /// expression by means of string initialization. Returns SIF_None if so, |
| 57 | /// otherwise returns a StringInitFailureKind that describes why the |
| 58 | /// initialization would not work. |
| 59 | static StringInitFailureKind IsStringInit(Expr *Init, const ArrayType *AT, |
| 60 | ASTContext &Context) { |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 61 | if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT)) |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 62 | return SIF_Other; |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 63 | |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 64 | // See if this is a string literal or @encode. |
| 65 | Init = Init->IgnoreParens(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 66 | |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 67 | // Handle @encode, which is a narrow string. |
| 68 | if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType()) |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 69 | return SIF_None; |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 70 | |
| 71 | // Otherwise we can only handle string literals. |
| 72 | StringLiteral *SL = dyn_cast<StringLiteral>(Init); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 73 | if (!SL) |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 74 | return SIF_Other; |
Eli Friedman | 42a8465 | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 75 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 76 | const QualType ElemTy = |
| 77 | Context.getCanonicalType(AT->getElementType()).getUnqualifiedType(); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 78 | |
| 79 | switch (SL->getKind()) { |
| 80 | case StringLiteral::Ascii: |
| 81 | case StringLiteral::UTF8: |
| 82 | // char array can be initialized with a narrow string. |
| 83 | // Only allow char x[] = "foo"; not char x[] = L"foo"; |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 84 | if (ElemTy->isCharType()) |
| 85 | return SIF_None; |
| 86 | if (IsWideCharCompatible(ElemTy, Context)) |
| 87 | return SIF_NarrowStringIntoWideChar; |
| 88 | return SIF_Other; |
| 89 | // C99 6.7.8p15 (with correction from DR343), or C11 6.7.9p15: |
| 90 | // "An array with element type compatible with a qualified or unqualified |
| 91 | // version of wchar_t, char16_t, or char32_t may be initialized by a wide |
| 92 | // string literal with the corresponding encoding prefix (L, u, or U, |
| 93 | // respectively), optionally enclosed in braces. |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 94 | case StringLiteral::UTF16: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 95 | if (Context.typesAreCompatible(Context.Char16Ty, ElemTy)) |
| 96 | return SIF_None; |
| 97 | if (ElemTy->isCharType()) |
| 98 | return SIF_WideStringIntoChar; |
| 99 | if (IsWideCharCompatible(ElemTy, Context)) |
| 100 | return SIF_IncompatWideStringIntoWideChar; |
| 101 | return SIF_Other; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 102 | case StringLiteral::UTF32: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 103 | if (Context.typesAreCompatible(Context.Char32Ty, ElemTy)) |
| 104 | return SIF_None; |
| 105 | if (ElemTy->isCharType()) |
| 106 | return SIF_WideStringIntoChar; |
| 107 | if (IsWideCharCompatible(ElemTy, Context)) |
| 108 | return SIF_IncompatWideStringIntoWideChar; |
| 109 | return SIF_Other; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 110 | case StringLiteral::Wide: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 111 | if (Context.typesAreCompatible(Context.getWideCharType(), ElemTy)) |
| 112 | return SIF_None; |
| 113 | if (ElemTy->isCharType()) |
| 114 | return SIF_WideStringIntoChar; |
| 115 | if (IsWideCharCompatible(ElemTy, Context)) |
| 116 | return SIF_IncompatWideStringIntoWideChar; |
| 117 | return SIF_Other; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 118 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 119 | |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 120 | llvm_unreachable("missed a StringLiteral kind?"); |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Hans Wennborg | 950f318 | 2013-05-16 09:22:40 +0000 | [diff] [blame] | 123 | static StringInitFailureKind IsStringInit(Expr *init, QualType declType, |
| 124 | ASTContext &Context) { |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 125 | const ArrayType *arrayType = Context.getAsArrayType(declType); |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 126 | if (!arrayType) |
Hans Wennborg | 950f318 | 2013-05-16 09:22:40 +0000 | [diff] [blame] | 127 | return SIF_Other; |
| 128 | return IsStringInit(init, arrayType, Context); |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 131 | /// Update the type of a string literal, including any surrounding parentheses, |
| 132 | /// to match the type of the object which it is initializing. |
| 133 | static void updateStringLiteralType(Expr *E, QualType Ty) { |
Richard Smith | d74b1606 | 2013-05-06 00:35:47 +0000 | [diff] [blame] | 134 | while (true) { |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 135 | E->setType(Ty); |
Richard Smith | d74b1606 | 2013-05-06 00:35:47 +0000 | [diff] [blame] | 136 | if (isa<StringLiteral>(E) || isa<ObjCEncodeExpr>(E)) |
| 137 | break; |
| 138 | else if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) |
| 139 | E = PE->getSubExpr(); |
| 140 | else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) |
| 141 | E = UO->getSubExpr(); |
| 142 | else if (GenericSelectionExpr *GSE = dyn_cast<GenericSelectionExpr>(E)) |
| 143 | E = GSE->getResultExpr(); |
| 144 | else |
| 145 | llvm_unreachable("unexpected expr in string literal init"); |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 146 | } |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 147 | } |
| 148 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 149 | static void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT, |
| 150 | Sema &S) { |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 151 | // Get the length of the string as parsed. |
Ben Langmuir | 577b393 | 2015-01-26 19:04:10 +0000 | [diff] [blame^] | 152 | auto *ConstantArrayTy = |
| 153 | cast<ConstantArrayType>(Str->getType()->getUnqualifiedDesugaredType()); |
| 154 | uint64_t StrLength = ConstantArrayTy->getSize().getZExtValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 156 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | // 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] | 158 | // being initialized to a string literal. |
Benjamin Kramer | e073177 | 2012-08-04 17:00:46 +0000 | [diff] [blame] | 159 | llvm::APInt ConstVal(32, StrLength); |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 160 | // Return a new array type (C99 6.7.8p22). |
John McCall | c5b8225 | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 161 | DeclT = S.Context.getConstantArrayType(IAT->getElementType(), |
| 162 | ConstVal, |
| 163 | ArrayType::Normal, 0); |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 164 | updateStringLiteralType(Str, DeclT); |
Chris Lattner | 94e6c4b | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 165 | return; |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 166 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 168 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 169 | |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 170 | // We have an array of character type with known size. However, |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 171 | // the size may be smaller or larger than the string we are initializing. |
| 172 | // FIXME: Avoid truncation for 64-bit length strings. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 173 | if (S.getLangOpts().CPlusPlus) { |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 174 | if (StringLiteral *SL = dyn_cast<StringLiteral>(Str->IgnoreParens())) { |
Anders Carlsson | d162fb8 | 2011-04-14 00:41:11 +0000 | [diff] [blame] | 175 | // For Pascal strings it's OK to strip off the terminating null character, |
| 176 | // so the example below is valid: |
| 177 | // |
| 178 | // unsigned char a[2] = "\pa"; |
| 179 | if (SL->isPascal()) |
| 180 | StrLength--; |
| 181 | } |
| 182 | |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 183 | // [dcl.init.string]p2 |
| 184 | if (StrLength > CAT->getSize().getZExtValue()) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 185 | S.Diag(Str->getLocStart(), |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 186 | diag::err_initializer_string_for_char_array_too_long) |
| 187 | << Str->getSourceRange(); |
| 188 | } else { |
| 189 | // C99 6.7.8p14. |
| 190 | if (StrLength-1 > CAT->getSize().getZExtValue()) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 191 | S.Diag(Str->getLocStart(), |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 192 | diag::ext_initializer_string_for_char_array_too_long) |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 193 | << Str->getSourceRange(); |
| 194 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 196 | // Set the type to the actual size that we are initializing. If we have |
| 197 | // something like: |
| 198 | // char x[1] = "foo"; |
| 199 | // then this will set the string literal's type to char[1]. |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 200 | updateStringLiteralType(Str, DeclT); |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 203 | //===----------------------------------------------------------------------===// |
| 204 | // Semantic checking for initializer lists. |
| 205 | //===----------------------------------------------------------------------===// |
| 206 | |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 207 | /// @brief Semantic checking for initializer lists. |
| 208 | /// |
| 209 | /// The InitListChecker class contains a set of routines that each |
| 210 | /// handle the initialization of a certain kind of entity, e.g., |
| 211 | /// arrays, vectors, struct/union types, scalars, etc. The |
| 212 | /// InitListChecker itself performs a recursive walk of the subobject |
| 213 | /// structure of the type to be initialized, while stepping through |
| 214 | /// the initializer list one element at a time. The IList and Index |
| 215 | /// parameters to each of the Check* routines contain the active |
| 216 | /// (syntactic) initializer list and the index into that initializer |
| 217 | /// list that represents the current initializer. Each routine is |
| 218 | /// responsible for moving that Index forward as it consumes elements. |
| 219 | /// |
| 220 | /// Each Check* routine also has a StructuredList/StructuredIndex |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 221 | /// arguments, which contains the current "structured" (semantic) |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 222 | /// initializer list and the index into that initializer list where we |
| 223 | /// are copying initializers as we map them over to the semantic |
| 224 | /// list. Once we have completed our recursive walk of the subobject |
| 225 | /// structure, we will have constructed a full semantic initializer |
| 226 | /// list. |
| 227 | /// |
| 228 | /// C99 designators cause changes in the initializer list traversal, |
| 229 | /// because they make the initialization "jump" into a specific |
| 230 | /// subobject and then continue the initialization from that |
| 231 | /// point. CheckDesignatedInitializer() recursively steps into the |
| 232 | /// designated subobject and manages backing out the recursion to |
| 233 | /// initialize the subobjects after the one designated. |
Chris Lattner | 9ececce | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 234 | namespace { |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 235 | class InitListChecker { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 236 | Sema &SemaRef; |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 237 | bool hadError; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 238 | bool VerifyOnly; // no diagnostics, no structure building |
Benjamin Kramer | 6b441d6 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 239 | llvm::DenseMap<InitListExpr *, InitListExpr *> SyntacticToSemantic; |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 240 | InitListExpr *FullyStructuredList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 241 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 242 | void CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 243 | InitListExpr *ParentIList, QualType T, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 244 | unsigned &Index, InitListExpr *StructuredList, |
Eli Friedman | c616c5f | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 245 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 246 | void CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 247 | InitListExpr *IList, QualType &T, |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 248 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 249 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 250 | void CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 251 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 252 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 253 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 254 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 255 | unsigned &StructuredIndex, |
| 256 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 257 | void CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 258 | InitListExpr *IList, QualType ElemType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 259 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 260 | InitListExpr *StructuredList, |
| 261 | unsigned &StructuredIndex); |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 262 | void CheckComplexType(const InitializedEntity &Entity, |
| 263 | InitListExpr *IList, QualType DeclType, |
| 264 | unsigned &Index, |
| 265 | InitListExpr *StructuredList, |
| 266 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 267 | void CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 268 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 269 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 270 | InitListExpr *StructuredList, |
| 271 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 272 | void CheckReferenceType(const InitializedEntity &Entity, |
| 273 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 274 | unsigned &Index, |
| 275 | InitListExpr *StructuredList, |
| 276 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 277 | void CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 278 | InitListExpr *IList, QualType DeclType, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 279 | InitListExpr *StructuredList, |
| 280 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 281 | void CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 73eb7cd | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 282 | InitListExpr *IList, QualType DeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 283 | RecordDecl::field_iterator Field, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 284 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 285 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 286 | unsigned &StructuredIndex, |
| 287 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 288 | void CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 289 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 290 | llvm::APSInt elementIndex, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 291 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 292 | InitListExpr *StructuredList, |
| 293 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 294 | bool CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 295 | InitListExpr *IList, DesignatedInitExpr *DIE, |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 296 | unsigned DesigIdx, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 297 | QualType &CurrentObjectType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 298 | RecordDecl::field_iterator *NextField, |
| 299 | llvm::APSInt *NextElementIndex, |
| 300 | unsigned &Index, |
| 301 | InitListExpr *StructuredList, |
| 302 | unsigned &StructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 303 | bool FinishSubobjectInit, |
| 304 | bool TopLevelObject); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 305 | InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 306 | QualType CurrentObjectType, |
| 307 | InitListExpr *StructuredList, |
| 308 | unsigned StructuredIndex, |
| 309 | SourceRange InitRange); |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 310 | void UpdateStructuredListElement(InitListExpr *StructuredList, |
| 311 | unsigned &StructuredIndex, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 312 | Expr *expr); |
| 313 | int numArrayElements(QualType DeclType); |
| 314 | int numStructUnionElements(QualType DeclType); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 315 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 316 | static ExprResult PerformEmptyInit(Sema &SemaRef, |
| 317 | SourceLocation Loc, |
| 318 | const InitializedEntity &Entity, |
| 319 | bool VerifyOnly); |
| 320 | void FillInEmptyInitForField(unsigned Init, FieldDecl *Field, |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 321 | const InitializedEntity &ParentEntity, |
| 322 | InitListExpr *ILE, bool &RequiresSecondPass); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 323 | void FillInEmptyInitializations(const InitializedEntity &Entity, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 324 | InitListExpr *ILE, bool &RequiresSecondPass); |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 325 | bool CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 326 | Expr *InitExpr, FieldDecl *Field, |
| 327 | bool TopLevelObject); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 328 | void CheckEmptyInitializable(const InitializedEntity &Entity, |
| 329 | SourceLocation Loc); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 330 | |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 331 | public: |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 332 | InitListChecker(Sema &S, const InitializedEntity &Entity, |
Richard Smith | de22923 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 333 | InitListExpr *IL, QualType &T, bool VerifyOnly); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 334 | bool HadError() { return hadError; } |
| 335 | |
| 336 | // @brief Retrieves the fully-structured initializer list used for |
| 337 | // semantic analysis and code generation. |
| 338 | InitListExpr *getFullyStructuredList() const { return FullyStructuredList; } |
| 339 | }; |
Chris Lattner | 9ececce | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 340 | } // end anonymous namespace |
Chris Lattner | d9ae05b | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 341 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 342 | ExprResult InitListChecker::PerformEmptyInit(Sema &SemaRef, |
| 343 | SourceLocation Loc, |
| 344 | const InitializedEntity &Entity, |
| 345 | bool VerifyOnly) { |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 346 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 347 | true); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 348 | MultiExprArg SubInit; |
| 349 | Expr *InitExpr; |
| 350 | InitListExpr DummyInitList(SemaRef.Context, Loc, None, Loc); |
| 351 | |
| 352 | // C++ [dcl.init.aggr]p7: |
| 353 | // If there are fewer initializer-clauses in the list than there are |
| 354 | // members in the aggregate, then each member not explicitly initialized |
| 355 | // ... |
Nico Weber | bcb70ee | 2014-07-02 23:51:09 +0000 | [diff] [blame] | 356 | bool EmptyInitList = SemaRef.getLangOpts().CPlusPlus11 && |
| 357 | Entity.getType()->getBaseElementTypeUnsafe()->isRecordType(); |
| 358 | if (EmptyInitList) { |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 359 | // C++1y / DR1070: |
| 360 | // shall be initialized [...] from an empty initializer list. |
| 361 | // |
| 362 | // We apply the resolution of this DR to C++11 but not C++98, since C++98 |
| 363 | // does not have useful semantics for initialization from an init list. |
| 364 | // We treat this as copy-initialization, because aggregate initialization |
| 365 | // always performs copy-initialization on its elements. |
| 366 | // |
| 367 | // Only do this if we're initializing a class type, to avoid filling in |
| 368 | // the initializer list where possible. |
| 369 | InitExpr = VerifyOnly ? &DummyInitList : new (SemaRef.Context) |
| 370 | InitListExpr(SemaRef.Context, Loc, None, Loc); |
| 371 | InitExpr->setType(SemaRef.Context.VoidTy); |
| 372 | SubInit = InitExpr; |
| 373 | Kind = InitializationKind::CreateCopy(Loc, Loc); |
| 374 | } else { |
| 375 | // C++03: |
| 376 | // shall be value-initialized. |
| 377 | } |
| 378 | |
| 379 | InitializationSequence InitSeq(SemaRef, Entity, Kind, SubInit); |
Nico Weber | bcb70ee | 2014-07-02 23:51:09 +0000 | [diff] [blame] | 380 | // libstdc++4.6 marks the vector default constructor as explicit in |
| 381 | // _GLIBCXX_DEBUG mode, so recover using the C++03 logic in that case. |
| 382 | // stlport does so too. Look for std::__debug for libstdc++, and for |
| 383 | // std:: for stlport. This is effectively a compiler-side implementation of |
| 384 | // LWG2193. |
| 385 | if (!InitSeq && EmptyInitList && InitSeq.getFailureKind() == |
| 386 | InitializationSequence::FK_ExplicitConstructor) { |
| 387 | OverloadCandidateSet::iterator Best; |
| 388 | OverloadingResult O = |
| 389 | InitSeq.getFailedCandidateSet() |
| 390 | .BestViableFunction(SemaRef, Kind.getLocation(), Best); |
| 391 | (void)O; |
| 392 | assert(O == OR_Success && "Inconsistent overload resolution"); |
| 393 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 394 | CXXRecordDecl *R = CtorDecl->getParent(); |
| 395 | |
| 396 | if (CtorDecl->getMinRequiredArguments() == 0 && |
| 397 | CtorDecl->isExplicit() && R->getDeclName() && |
| 398 | SemaRef.SourceMgr.isInSystemHeader(CtorDecl->getLocation())) { |
| 399 | |
| 400 | |
| 401 | bool IsInStd = false; |
| 402 | for (NamespaceDecl *ND = dyn_cast<NamespaceDecl>(R->getDeclContext()); |
Nico Weber | 5752ad0 | 2014-07-03 00:38:25 +0000 | [diff] [blame] | 403 | ND && !IsInStd; ND = dyn_cast<NamespaceDecl>(ND->getParent())) { |
Nico Weber | bcb70ee | 2014-07-02 23:51:09 +0000 | [diff] [blame] | 404 | if (SemaRef.getStdNamespace()->InEnclosingNamespaceSetOf(ND)) |
| 405 | IsInStd = true; |
| 406 | } |
| 407 | |
| 408 | if (IsInStd && llvm::StringSwitch<bool>(R->getName()) |
| 409 | .Cases("basic_string", "deque", "forward_list", true) |
| 410 | .Cases("list", "map", "multimap", "multiset", true) |
| 411 | .Cases("priority_queue", "queue", "set", "stack", true) |
| 412 | .Cases("unordered_map", "unordered_set", "vector", true) |
| 413 | .Default(false)) { |
| 414 | InitSeq.InitializeFrom( |
| 415 | SemaRef, Entity, |
| 416 | InitializationKind::CreateValue(Loc, Loc, Loc, true), |
| 417 | MultiExprArg(), /*TopLevelOfInitList=*/false); |
| 418 | // Emit a warning for this. System header warnings aren't shown |
| 419 | // by default, but people working on system headers should see it. |
| 420 | if (!VerifyOnly) { |
| 421 | SemaRef.Diag(CtorDecl->getLocation(), |
| 422 | diag::warn_invalid_initializer_from_system_header); |
| 423 | SemaRef.Diag(Entity.getDecl()->getLocation(), |
| 424 | diag::note_used_in_initialization_here); |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | } |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 429 | if (!InitSeq) { |
| 430 | if (!VerifyOnly) { |
| 431 | InitSeq.Diagnose(SemaRef, Entity, Kind, SubInit); |
| 432 | if (Entity.getKind() == InitializedEntity::EK_Member) |
| 433 | SemaRef.Diag(Entity.getDecl()->getLocation(), |
| 434 | diag::note_in_omitted_aggregate_initializer) |
| 435 | << /*field*/1 << Entity.getDecl(); |
| 436 | else if (Entity.getKind() == InitializedEntity::EK_ArrayElement) |
| 437 | SemaRef.Diag(Loc, diag::note_in_omitted_aggregate_initializer) |
| 438 | << /*array element*/0 << Entity.getElementIndex(); |
| 439 | } |
| 440 | return ExprError(); |
| 441 | } |
| 442 | |
| 443 | return VerifyOnly ? ExprResult(static_cast<Expr *>(nullptr)) |
| 444 | : InitSeq.Perform(SemaRef, Entity, Kind, SubInit); |
| 445 | } |
| 446 | |
| 447 | void InitListChecker::CheckEmptyInitializable(const InitializedEntity &Entity, |
| 448 | SourceLocation Loc) { |
| 449 | assert(VerifyOnly && |
| 450 | "CheckEmptyInitializable is only inteded for verification mode."); |
| 451 | if (PerformEmptyInit(SemaRef, Loc, Entity, /*VerifyOnly*/true).isInvalid()) |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 452 | hadError = true; |
| 453 | } |
| 454 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 455 | void InitListChecker::FillInEmptyInitForField(unsigned Init, FieldDecl *Field, |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 456 | const InitializedEntity &ParentEntity, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 457 | InitListExpr *ILE, |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 458 | bool &RequiresSecondPass) { |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 459 | SourceLocation Loc = ILE->getLocEnd(); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 460 | unsigned NumInits = ILE->getNumInits(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 461 | InitializedEntity MemberEntity |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 462 | = InitializedEntity::InitializeMember(Field, &ParentEntity); |
| 463 | if (Init >= NumInits || !ILE->getInit(Init)) { |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 464 | // C++1y [dcl.init.aggr]p7: |
| 465 | // If there are fewer initializer-clauses in the list than there are |
| 466 | // members in the aggregate, then each member not explicitly initialized |
| 467 | // shall be initialized from its brace-or-equal-initializer [...] |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 468 | if (Field->hasInClassInitializer()) { |
Reid Kleckner | d60b82f | 2014-11-17 23:36:45 +0000 | [diff] [blame] | 469 | ExprResult DIE = SemaRef.BuildCXXDefaultInitExpr(Loc, Field); |
| 470 | if (DIE.isInvalid()) { |
| 471 | hadError = true; |
| 472 | return; |
| 473 | } |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 474 | if (Init < NumInits) |
Reid Kleckner | d60b82f | 2014-11-17 23:36:45 +0000 | [diff] [blame] | 475 | ILE->setInit(Init, DIE.get()); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 476 | else { |
Reid Kleckner | d60b82f | 2014-11-17 23:36:45 +0000 | [diff] [blame] | 477 | ILE->updateInit(SemaRef.Context, Init, DIE.get()); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 478 | RequiresSecondPass = true; |
| 479 | } |
| 480 | return; |
| 481 | } |
| 482 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 483 | if (Field->getType()->isReferenceType()) { |
| 484 | // C++ [dcl.init.aggr]p9: |
| 485 | // If an incomplete or empty initializer-list leaves a |
| 486 | // member of reference type uninitialized, the program is |
| 487 | // ill-formed. |
| 488 | SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized) |
| 489 | << Field->getType() |
| 490 | << ILE->getSyntacticForm()->getSourceRange(); |
| 491 | SemaRef.Diag(Field->getLocation(), |
| 492 | diag::note_uninit_reference_member); |
| 493 | hadError = true; |
| 494 | return; |
| 495 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 496 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 497 | ExprResult MemberInit = PerformEmptyInit(SemaRef, Loc, MemberEntity, |
| 498 | /*VerifyOnly*/false); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 499 | if (MemberInit.isInvalid()) { |
| 500 | hadError = true; |
| 501 | return; |
| 502 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 503 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 504 | if (hadError) { |
| 505 | // Do nothing |
| 506 | } else if (Init < NumInits) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 507 | ILE->setInit(Init, MemberInit.getAs<Expr>()); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 508 | } else if (!isa<ImplicitValueInitExpr>(MemberInit.get())) { |
| 509 | // Empty initialization requires a constructor call, so |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 510 | // extend the initializer list to include the constructor |
| 511 | // call and make a note that we'll need to take another pass |
| 512 | // through the initializer list. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 513 | ILE->updateInit(SemaRef.Context, Init, MemberInit.getAs<Expr>()); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 514 | RequiresSecondPass = true; |
| 515 | } |
| 516 | } else if (InitListExpr *InnerILE |
| 517 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 518 | FillInEmptyInitializations(MemberEntity, InnerILE, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 519 | RequiresSecondPass); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 522 | /// Recursively replaces NULL values within the given initializer list |
| 523 | /// with expressions that perform value-initialization of the |
| 524 | /// appropriate type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 525 | void |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 526 | InitListChecker::FillInEmptyInitializations(const InitializedEntity &Entity, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 527 | InitListExpr *ILE, |
| 528 | bool &RequiresSecondPass) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 529 | assert((ILE->getType() != SemaRef.Context.VoidTy) && |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 530 | "Should not have void type"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 531 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 532 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 533 | const RecordDecl *RDecl = RType->getDecl(); |
| 534 | if (RDecl->isUnion() && ILE->getInitializedFieldInUnion()) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 535 | FillInEmptyInitForField(0, ILE->getInitializedFieldInUnion(), |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 536 | Entity, ILE, RequiresSecondPass); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 537 | else if (RDecl->isUnion() && isa<CXXRecordDecl>(RDecl) && |
| 538 | cast<CXXRecordDecl>(RDecl)->hasInClassInitializer()) { |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 539 | for (auto *Field : RDecl->fields()) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 540 | if (Field->hasInClassInitializer()) { |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 541 | FillInEmptyInitForField(0, Field, Entity, ILE, RequiresSecondPass); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 542 | break; |
| 543 | } |
| 544 | } |
| 545 | } else { |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 546 | unsigned Init = 0; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 547 | for (auto *Field : RDecl->fields()) { |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 548 | if (Field->isUnnamedBitfield()) |
| 549 | continue; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 550 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 551 | if (hadError) |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 552 | return; |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 553 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 554 | FillInEmptyInitForField(Init, Field, Entity, ILE, RequiresSecondPass); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 555 | if (hadError) |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 556 | return; |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 557 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 558 | ++Init; |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 559 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 560 | // Only look at the first initialization of a union. |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 561 | if (RDecl->isUnion()) |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 562 | break; |
| 563 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 567 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 568 | |
| 569 | QualType ElementType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 570 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 571 | InitializedEntity ElementEntity = Entity; |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 572 | unsigned NumInits = ILE->getNumInits(); |
| 573 | unsigned NumElements = NumInits; |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 574 | if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 575 | ElementType = AType->getElementType(); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 576 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) |
| 577 | NumElements = CAType->getSize().getZExtValue(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 578 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 579 | 0, Entity); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 580 | } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 581 | ElementType = VType->getElementType(); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 582 | NumElements = VType->getNumElements(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 583 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 584 | 0, Entity); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 585 | } else |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 586 | ElementType = ILE->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 587 | |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 588 | for (unsigned Init = 0; Init != NumElements; ++Init) { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 589 | if (hadError) |
| 590 | return; |
| 591 | |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 592 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement || |
| 593 | ElementEntity.getKind() == InitializedEntity::EK_VectorElement) |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 594 | ElementEntity.setElementIndex(Init); |
| 595 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 596 | Expr *InitExpr = (Init < NumInits ? ILE->getInit(Init) : nullptr); |
Argyrios Kyrtzidis | d4590a5d | 2011-10-21 23:02:22 +0000 | [diff] [blame] | 597 | if (!InitExpr && !ILE->hasArrayFiller()) { |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 598 | ExprResult ElementInit = PerformEmptyInit(SemaRef, ILE->getLocEnd(), |
| 599 | ElementEntity, |
| 600 | /*VerifyOnly*/false); |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 601 | if (ElementInit.isInvalid()) { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 602 | hadError = true; |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 603 | return; |
| 604 | } |
| 605 | |
| 606 | if (hadError) { |
| 607 | // Do nothing |
| 608 | } else if (Init < NumInits) { |
Argyrios Kyrtzidis | 446bcf2 | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 609 | // For arrays, just set the expression used for value-initialization |
| 610 | // of the "holes" in the array. |
| 611 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 612 | ILE->setArrayFiller(ElementInit.getAs<Expr>()); |
Argyrios Kyrtzidis | 446bcf2 | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 613 | else |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 614 | ILE->setInit(Init, ElementInit.getAs<Expr>()); |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 615 | } else { |
| 616 | // For arrays, just set the expression used for value-initialization |
| 617 | // of the rest of elements and exit. |
| 618 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 619 | ILE->setArrayFiller(ElementInit.getAs<Expr>()); |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 620 | return; |
| 621 | } |
| 622 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 623 | if (!isa<ImplicitValueInitExpr>(ElementInit.get())) { |
| 624 | // Empty initialization requires a constructor call, so |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 625 | // extend the initializer list to include the constructor |
| 626 | // call and make a note that we'll need to take another pass |
| 627 | // through the initializer list. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 628 | ILE->updateInit(SemaRef.Context, Init, ElementInit.getAs<Expr>()); |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 629 | RequiresSecondPass = true; |
| 630 | } |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 631 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 632 | } else if (InitListExpr *InnerILE |
Argyrios Kyrtzidis | d4590a5d | 2011-10-21 23:02:22 +0000 | [diff] [blame] | 633 | = dyn_cast_or_null<InitListExpr>(InitExpr)) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 634 | FillInEmptyInitializations(ElementEntity, InnerILE, RequiresSecondPass); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | |
Chris Lattner | d9ae05b | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 638 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 639 | InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 640 | InitListExpr *IL, QualType &T, |
Richard Smith | de22923 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 641 | bool VerifyOnly) |
| 642 | : SemaRef(S), VerifyOnly(VerifyOnly) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 643 | hadError = false; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 644 | |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 645 | FullyStructuredList = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 646 | getStructuredSubobjectInit(IL, 0, T, nullptr, 0, IL->getSourceRange()); |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 647 | CheckExplicitInitList(Entity, IL, T, FullyStructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 648 | /*TopLevelObject=*/true); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 649 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 650 | if (!hadError && !VerifyOnly) { |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 651 | bool RequiresSecondPass = false; |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 652 | FillInEmptyInitializations(Entity, FullyStructuredList, RequiresSecondPass); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 653 | if (RequiresSecondPass && !hadError) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 654 | FillInEmptyInitializations(Entity, FullyStructuredList, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 655 | RequiresSecondPass); |
| 656 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 660 | // FIXME: use a proper constant |
| 661 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 662 | if (const ConstantArrayType *CAT = |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 663 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 664 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 665 | } |
| 666 | return maxElements; |
| 667 | } |
| 668 | |
| 669 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 670 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 671 | int InitializableMembers = 0; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 672 | for (const auto *Field : structDecl->fields()) |
Douglas Gregor | 556e586 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 673 | if (!Field->isUnnamedBitfield()) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 674 | ++InitializableMembers; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 675 | |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 676 | if (structDecl->isUnion()) |
Eli Friedman | 0e56c82 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 677 | return std::min(InitializableMembers, 1); |
| 678 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 679 | } |
| 680 | |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 681 | /// Check whether the range of the initializer \p ParentIList from element |
| 682 | /// \p Index onwards can be used to initialize an object of type \p T. Update |
| 683 | /// \p Index to indicate how many elements of the list were consumed. |
| 684 | /// |
| 685 | /// This also fills in \p StructuredList, from element \p StructuredIndex |
| 686 | /// onwards, with the fully-braced, desugared form of the initialization. |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 687 | void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 688 | InitListExpr *ParentIList, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 689 | QualType T, unsigned &Index, |
| 690 | InitListExpr *StructuredList, |
Eli Friedman | c616c5f | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 691 | unsigned &StructuredIndex) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 692 | int maxElements = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 693 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 694 | if (T->isArrayType()) |
| 695 | maxElements = numArrayElements(T); |
Douglas Gregor | 8385a06 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 696 | else if (T->isRecordType()) |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 697 | maxElements = numStructUnionElements(T); |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 698 | else if (T->isVectorType()) |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 699 | maxElements = T->getAs<VectorType>()->getNumElements(); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 700 | else |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 701 | llvm_unreachable("CheckImplicitInitList(): Illegal type"); |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 702 | |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 703 | if (maxElements == 0) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 704 | if (!VerifyOnly) |
| 705 | SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(), |
| 706 | diag::err_implicit_empty_initializer); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 707 | ++Index; |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 708 | hadError = true; |
| 709 | return; |
| 710 | } |
| 711 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 712 | // Build a structured initializer list corresponding to this subobject. |
| 713 | InitListExpr *StructuredSubobjectInitList |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 714 | = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList, |
| 715 | StructuredIndex, |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 716 | SourceRange(ParentIList->getInit(Index)->getLocStart(), |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 717 | ParentIList->getSourceRange().getEnd())); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 718 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 719 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 720 | // Check the element types and build the structural subobject. |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 721 | unsigned StartIndex = Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 722 | CheckListElementTypes(Entity, ParentIList, T, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 723 | /*SubobjectIsDesignatorContext=*/false, Index, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 724 | StructuredSubobjectInitList, |
Eli Friedman | c616c5f | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 725 | StructuredSubobjectInitIndex); |
Sebastian Redl | 8b6412a | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 726 | |
Richard Smith | de22923 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 727 | if (!VerifyOnly) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 728 | StructuredSubobjectInitList->setType(T); |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 729 | |
Sebastian Redl | 8b6412a | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 730 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 731 | // Update the structured sub-object initializer so that it's ending |
| 732 | // range corresponds with the end of the last initializer it used. |
| 733 | if (EndIndex < ParentIList->getNumInits()) { |
| 734 | SourceLocation EndLoc |
| 735 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 736 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 737 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 738 | |
Sebastian Redl | 8b6412a | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 739 | // Complain about missing braces. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 740 | if (T->isArrayType() || T->isRecordType()) { |
| 741 | SemaRef.Diag(StructuredSubobjectInitList->getLocStart(), |
Richard Smith | de22923 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 742 | diag::warn_missing_braces) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 743 | << StructuredSubobjectInitList->getSourceRange() |
| 744 | << FixItHint::CreateInsertion( |
| 745 | StructuredSubobjectInitList->getLocStart(), "{") |
| 746 | << FixItHint::CreateInsertion( |
| 747 | SemaRef.getLocForEndOfToken( |
| 748 | StructuredSubobjectInitList->getLocEnd()), |
| 749 | "}"); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 750 | } |
Tanya Lattner | 5029d56 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 751 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 754 | /// Check whether the initializer \p IList (that was written with explicit |
| 755 | /// braces) can be used to initialize an object of type \p T. |
| 756 | /// |
| 757 | /// This also fills in \p StructuredList with the fully-braced, desugared |
| 758 | /// form of the initialization. |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 759 | void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 760 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 761 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 762 | bool TopLevelObject) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 763 | if (!VerifyOnly) { |
| 764 | SyntacticToSemantic[IList] = StructuredList; |
| 765 | StructuredList->setSyntacticForm(IList); |
| 766 | } |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 767 | |
| 768 | unsigned Index = 0, StructuredIndex = 0; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 769 | CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 770 | Index, StructuredList, StructuredIndex, TopLevelObject); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 771 | if (!VerifyOnly) { |
Eli Friedman | 91f5ae5 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 772 | QualType ExprTy = T; |
| 773 | if (!ExprTy->isArrayType()) |
| 774 | ExprTy = ExprTy.getNonLValueExprType(SemaRef.Context); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 775 | IList->setType(ExprTy); |
| 776 | StructuredList->setType(ExprTy); |
| 777 | } |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 778 | if (hadError) |
| 779 | return; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 780 | |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 781 | if (Index < IList->getNumInits()) { |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 782 | // We have leftover initializers |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 783 | if (VerifyOnly) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 784 | if (SemaRef.getLangOpts().CPlusPlus || |
| 785 | (SemaRef.getLangOpts().OpenCL && |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 786 | IList->getType()->isVectorType())) { |
| 787 | hadError = true; |
| 788 | } |
| 789 | return; |
| 790 | } |
| 791 | |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 792 | if (StructuredIndex == 1 && |
Hans Wennborg | 950f318 | 2013-05-16 09:22:40 +0000 | [diff] [blame] | 793 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context) == |
| 794 | SIF_None) { |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 795 | unsigned DK = diag::ext_excess_initializers_in_char_array_initializer; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 796 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 797 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 798 | hadError = true; |
| 799 | } |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 800 | // Special-case |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 801 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 802 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d0e48ea | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 803 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | d42a0fb | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 804 | // Don't complain for incomplete types, since we'll get an error |
| 805 | // elsewhere |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 806 | QualType CurrentObjectType = StructuredList->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 807 | int initKind = |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 808 | CurrentObjectType->isArrayType()? 0 : |
| 809 | CurrentObjectType->isVectorType()? 1 : |
| 810 | CurrentObjectType->isScalarType()? 2 : |
| 811 | CurrentObjectType->isUnionType()? 3 : |
| 812 | 4; |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 813 | |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 814 | unsigned DK = diag::ext_excess_initializers; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 815 | if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 816 | DK = diag::err_excess_initializers; |
| 817 | hadError = true; |
| 818 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 819 | if (SemaRef.getLangOpts().OpenCL && initKind == 1) { |
Nate Begeman | 425038c | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 820 | DK = diag::err_excess_initializers; |
| 821 | hadError = true; |
| 822 | } |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 823 | |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 824 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 825 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 826 | } |
| 827 | } |
Eli Friedman | 6fcdec2 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 828 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 829 | if (!VerifyOnly && T->isScalarType() && IList->getNumInits() == 1 && |
| 830 | !TopLevelObject) |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 831 | SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 832 | << IList->getSourceRange() |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 833 | << FixItHint::CreateRemoval(IList->getLocStart()) |
| 834 | << FixItHint::CreateRemoval(IList->getLocEnd()); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 837 | void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 838 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 839 | QualType &DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 840 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 841 | unsigned &Index, |
| 842 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 843 | unsigned &StructuredIndex, |
| 844 | bool TopLevelObject) { |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 845 | if (DeclType->isAnyComplexType() && SubobjectIsDesignatorContext) { |
| 846 | // Explicitly braced initializer for complex type can be real+imaginary |
| 847 | // parts. |
| 848 | CheckComplexType(Entity, IList, DeclType, Index, |
| 849 | StructuredList, StructuredIndex); |
| 850 | } else if (DeclType->isScalarType()) { |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 851 | CheckScalarType(Entity, IList, DeclType, Index, |
| 852 | StructuredList, StructuredIndex); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 853 | } else if (DeclType->isVectorType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 854 | CheckVectorType(Entity, IList, DeclType, Index, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 855 | StructuredList, StructuredIndex); |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 856 | } else if (DeclType->isRecordType()) { |
| 857 | assert(DeclType->isAggregateType() && |
| 858 | "non-aggregate records should be handed in CheckSubElementType"); |
| 859 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
| 860 | CheckStructUnionTypes(Entity, IList, DeclType, RD->field_begin(), |
| 861 | SubobjectIsDesignatorContext, Index, |
| 862 | StructuredList, StructuredIndex, |
| 863 | TopLevelObject); |
| 864 | } else if (DeclType->isArrayType()) { |
| 865 | llvm::APSInt Zero( |
| 866 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
| 867 | false); |
| 868 | CheckArrayType(Entity, IList, DeclType, Zero, |
| 869 | SubobjectIsDesignatorContext, Index, |
| 870 | StructuredList, StructuredIndex); |
Steve Naroff | eaf5853 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 871 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 872 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 873 | ++Index; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 874 | if (!VerifyOnly) |
| 875 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 876 | << DeclType; |
Eli Friedman | d0e48ea | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 877 | hadError = true; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 878 | } else if (DeclType->isReferenceType()) { |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 879 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 880 | StructuredList, StructuredIndex); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 881 | } else if (DeclType->isObjCObjectType()) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 882 | if (!VerifyOnly) |
| 883 | SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class) |
| 884 | << DeclType; |
Douglas Gregor | 50ec46d | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 885 | hadError = true; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 886 | } else { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 887 | if (!VerifyOnly) |
| 888 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 889 | << DeclType; |
Douglas Gregor | 50ec46d | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 890 | hadError = true; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 891 | } |
| 892 | } |
| 893 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 894 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 895 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 896 | QualType ElemType, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 897 | unsigned &Index, |
| 898 | InitListExpr *StructuredList, |
| 899 | unsigned &StructuredIndex) { |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 900 | Expr *expr = IList->getInit(Index); |
Richard Smith | 72752e8 | 2013-05-31 02:56:17 +0000 | [diff] [blame] | 901 | |
| 902 | if (ElemType->isReferenceType()) |
| 903 | return CheckReferenceType(Entity, IList, ElemType, Index, |
| 904 | StructuredList, StructuredIndex); |
| 905 | |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 906 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 907 | if (!ElemType->isRecordType() || ElemType->isAggregateType()) { |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 908 | InitListExpr *InnerStructuredList |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 909 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 910 | StructuredList, StructuredIndex, |
| 911 | SubInitList->getSourceRange()); |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 912 | CheckExplicitInitList(Entity, SubInitList, ElemType, |
| 913 | InnerStructuredList); |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 914 | ++StructuredIndex; |
| 915 | ++Index; |
| 916 | return; |
| 917 | } |
| 918 | assert(SemaRef.getLangOpts().CPlusPlus && |
| 919 | "non-aggregate records are only possible in C++"); |
| 920 | // C++ initialization is handled later. |
Richard Smith | c4158e86 | 2014-07-18 04:47:25 +0000 | [diff] [blame] | 921 | } else if (isa<ImplicitValueInitExpr>(expr)) { |
Richard Smith | 8aa561b | 2014-07-17 23:12:06 +0000 | [diff] [blame] | 922 | // This happens during template instantiation when we see an InitListExpr |
| 923 | // that we've already checked once. |
Richard Smith | c4158e86 | 2014-07-18 04:47:25 +0000 | [diff] [blame] | 924 | assert(SemaRef.Context.hasSameType(expr->getType(), ElemType) && |
Richard Smith | 8aa561b | 2014-07-17 23:12:06 +0000 | [diff] [blame] | 925 | "found implicit initialization for the wrong type"); |
| 926 | if (!VerifyOnly) |
| 927 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 928 | ++Index; |
| 929 | return; |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 930 | } |
| 931 | |
Eli Friedman | 4628cf7 | 2013-08-19 22:12:56 +0000 | [diff] [blame] | 932 | // FIXME: Need to handle atomic aggregate types with implicit init lists. |
| 933 | if (ElemType->isScalarType() || ElemType->isAtomicType()) |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 934 | return CheckScalarType(Entity, IList, ElemType, Index, |
| 935 | StructuredList, StructuredIndex); |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 936 | |
Eli Friedman | 4628cf7 | 2013-08-19 22:12:56 +0000 | [diff] [blame] | 937 | assert((ElemType->isRecordType() || ElemType->isVectorType() || |
| 938 | ElemType->isArrayType()) && "Unexpected type"); |
| 939 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 940 | if (const ArrayType *arrayType = SemaRef.Context.getAsArrayType(ElemType)) { |
| 941 | // arrayType can be incomplete if we're initializing a flexible |
| 942 | // array member. There's nothing we can do with the completed |
| 943 | // type here, though. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 944 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 945 | if (IsStringInit(expr, arrayType, SemaRef.Context) == SIF_None) { |
Eli Friedman | d8d7a37 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 946 | if (!VerifyOnly) { |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 947 | CheckStringInit(expr, ElemType, arrayType, SemaRef); |
| 948 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
Eli Friedman | d8d7a37 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 949 | } |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 950 | ++Index; |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 951 | return; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 952 | } |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 953 | |
| 954 | // Fall through for subaggregate initialization. |
| 955 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 956 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 957 | // C++ [dcl.init.aggr]p12: |
| 958 | // All implicit type conversions (clause 4) are considered when |
Sebastian Redl | 26bcc94 | 2011-09-24 17:47:39 +0000 | [diff] [blame] | 959 | // initializing the aggregate member with an initializer from |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 960 | // an initializer-list. If the initializer can initialize a |
| 961 | // member, the member is initialized. [...] |
| 962 | |
| 963 | // FIXME: Better EqualLoc? |
| 964 | InitializationKind Kind = |
| 965 | InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation()); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 966 | InitializationSequence Seq(SemaRef, Entity, Kind, expr); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 967 | |
| 968 | if (Seq) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 969 | if (!VerifyOnly) { |
Richard Smith | 0f8ede1 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 970 | ExprResult Result = |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 971 | Seq.Perform(SemaRef, Entity, Kind, expr); |
Richard Smith | 0f8ede1 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 972 | if (Result.isInvalid()) |
| 973 | hadError = true; |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 974 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 975 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 976 | Result.getAs<Expr>()); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 977 | } |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 978 | ++Index; |
| 979 | return; |
| 980 | } |
| 981 | |
| 982 | // Fall through for subaggregate initialization |
| 983 | } else { |
| 984 | // C99 6.7.8p13: |
| 985 | // |
| 986 | // The initializer for a structure or union object that has |
| 987 | // automatic storage duration shall be either an initializer |
| 988 | // list as described below, or a single expression that has |
| 989 | // compatible structure or union type. In the latter case, the |
| 990 | // initial value of the object, including unnamed members, is |
| 991 | // that of the expression. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 992 | ExprResult ExprRes = expr; |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 993 | if ((ElemType->isRecordType() || ElemType->isVectorType()) && |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 994 | SemaRef.CheckSingleAssignmentConstraints(ElemType, ExprRes, |
| 995 | !VerifyOnly) |
Eli Friedman | b2a8d46 | 2013-09-17 04:07:04 +0000 | [diff] [blame] | 996 | != Sema::Incompatible) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 997 | if (ExprRes.isInvalid()) |
| 998 | hadError = true; |
| 999 | else { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1000 | ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.get()); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 1001 | if (ExprRes.isInvalid()) |
| 1002 | hadError = true; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1003 | } |
| 1004 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1005 | ExprRes.getAs<Expr>()); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1006 | ++Index; |
| 1007 | return; |
| 1008 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1009 | ExprRes.get(); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1010 | // Fall through for subaggregate initialization |
| 1011 | } |
| 1012 | |
| 1013 | // C++ [dcl.init.aggr]p12: |
| 1014 | // |
| 1015 | // [...] Otherwise, if the member is itself a non-empty |
| 1016 | // subaggregate, brace elision is assumed and the initializer is |
| 1017 | // considered for the initialization of the first member of |
| 1018 | // the subaggregate. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1019 | if (!SemaRef.getLangOpts().OpenCL && |
Tanya Lattner | 8355938 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 1020 | (ElemType->isAggregateType() || ElemType->isVectorType())) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1021 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
| 1022 | StructuredIndex); |
| 1023 | ++StructuredIndex; |
| 1024 | } else { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1025 | if (!VerifyOnly) { |
| 1026 | // We cannot initialize this element, so let |
| 1027 | // PerformCopyInitialization produce the appropriate diagnostic. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1028 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), expr, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1029 | /*TopLevelOfInitList=*/true); |
| 1030 | } |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1031 | hadError = true; |
| 1032 | ++Index; |
| 1033 | ++StructuredIndex; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1034 | } |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 1037 | void InitListChecker::CheckComplexType(const InitializedEntity &Entity, |
| 1038 | InitListExpr *IList, QualType DeclType, |
| 1039 | unsigned &Index, |
| 1040 | InitListExpr *StructuredList, |
| 1041 | unsigned &StructuredIndex) { |
| 1042 | assert(Index == 0 && "Index in explicit init list must be zero"); |
| 1043 | |
| 1044 | // As an extension, clang supports complex initializers, which initialize |
| 1045 | // a complex number component-wise. When an explicit initializer list for |
| 1046 | // a complex number contains two two initializers, this extension kicks in: |
| 1047 | // it exepcts the initializer list to contain two elements convertible to |
| 1048 | // the element type of the complex type. The first element initializes |
| 1049 | // the real part, and the second element intitializes the imaginary part. |
| 1050 | |
| 1051 | if (IList->getNumInits() != 2) |
| 1052 | return CheckScalarType(Entity, IList, DeclType, Index, StructuredList, |
| 1053 | StructuredIndex); |
| 1054 | |
| 1055 | // This is an extension in C. (The builtin _Complex type does not exist |
| 1056 | // in the C++ standard.) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1057 | if (!SemaRef.getLangOpts().CPlusPlus && !VerifyOnly) |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 1058 | SemaRef.Diag(IList->getLocStart(), diag::ext_complex_component_init) |
| 1059 | << IList->getSourceRange(); |
| 1060 | |
| 1061 | // Initialize the complex number. |
| 1062 | QualType elementType = DeclType->getAs<ComplexType>()->getElementType(); |
| 1063 | InitializedEntity ElementEntity = |
| 1064 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
| 1065 | |
| 1066 | for (unsigned i = 0; i < 2; ++i) { |
| 1067 | ElementEntity.setElementIndex(Index); |
| 1068 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1069 | StructuredList, StructuredIndex); |
| 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1074 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1075 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1076 | unsigned &Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1077 | InitListExpr *StructuredList, |
| 1078 | unsigned &StructuredIndex) { |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1079 | if (Index >= IList->getNumInits()) { |
Richard Smith | c823973 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 1080 | if (!VerifyOnly) |
| 1081 | SemaRef.Diag(IList->getLocStart(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1082 | SemaRef.getLangOpts().CPlusPlus11 ? |
Richard Smith | c823973 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 1083 | diag::warn_cxx98_compat_empty_scalar_initializer : |
| 1084 | diag::err_empty_scalar_initializer) |
| 1085 | << IList->getSourceRange(); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1086 | hadError = !SemaRef.getLangOpts().CPlusPlus11; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1087 | ++Index; |
| 1088 | ++StructuredIndex; |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 1089 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1090 | } |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1091 | |
| 1092 | Expr *expr = IList->getInit(Index); |
| 1093 | if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) { |
Richard Smith | fe9d2c0 | 2013-11-19 03:41:32 +0000 | [diff] [blame] | 1094 | // FIXME: This is invalid, and accepting it causes overload resolution |
| 1095 | // to pick the wrong overload in some corner cases. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1096 | if (!VerifyOnly) |
| 1097 | SemaRef.Diag(SubIList->getLocStart(), |
Richard Smith | fe9d2c0 | 2013-11-19 03:41:32 +0000 | [diff] [blame] | 1098 | diag::ext_many_braces_around_scalar_init) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1099 | << SubIList->getSourceRange(); |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1100 | |
| 1101 | CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList, |
| 1102 | StructuredIndex); |
| 1103 | return; |
| 1104 | } else if (isa<DesignatedInitExpr>(expr)) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1105 | if (!VerifyOnly) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1106 | SemaRef.Diag(expr->getLocStart(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1107 | diag::err_designator_for_scalar_init) |
| 1108 | << DeclType << expr->getSourceRange(); |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1109 | hadError = true; |
| 1110 | ++Index; |
| 1111 | ++StructuredIndex; |
| 1112 | return; |
| 1113 | } |
| 1114 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1115 | if (VerifyOnly) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1116 | if (!SemaRef.CanPerformCopyInitialization(Entity,expr)) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1117 | hadError = true; |
| 1118 | ++Index; |
| 1119 | return; |
| 1120 | } |
| 1121 | |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1122 | ExprResult Result = |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1123 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), expr, |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 1124 | /*TopLevelOfInitList=*/true); |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1125 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1126 | Expr *ResultExpr = nullptr; |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1127 | |
| 1128 | if (Result.isInvalid()) |
| 1129 | hadError = true; // types weren't compatible. |
| 1130 | else { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1131 | ResultExpr = Result.getAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1132 | |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1133 | if (ResultExpr != expr) { |
| 1134 | // The type was promoted, update initializer list. |
| 1135 | IList->setInit(Index, ResultExpr); |
| 1136 | } |
| 1137 | } |
| 1138 | if (hadError) |
| 1139 | ++StructuredIndex; |
| 1140 | else |
| 1141 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 1142 | ++Index; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1145 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 1146 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1147 | unsigned &Index, |
| 1148 | InitListExpr *StructuredList, |
| 1149 | unsigned &StructuredIndex) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1150 | if (Index >= IList->getNumInits()) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1151 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 1152 | // general, it would be useful to pass location information down the stack, |
| 1153 | // so that we know the location (or decl) of the "current object" being |
| 1154 | // initialized. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1155 | if (!VerifyOnly) |
| 1156 | SemaRef.Diag(IList->getLocStart(), |
| 1157 | diag::err_init_reference_member_uninitialized) |
| 1158 | << DeclType |
| 1159 | << IList->getSourceRange(); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1160 | hadError = true; |
| 1161 | ++Index; |
| 1162 | ++StructuredIndex; |
| 1163 | return; |
| 1164 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1165 | |
| 1166 | Expr *expr = IList->getInit(Index); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1167 | if (isa<InitListExpr>(expr) && !SemaRef.getLangOpts().CPlusPlus11) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1168 | if (!VerifyOnly) |
| 1169 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
| 1170 | << DeclType << IList->getSourceRange(); |
| 1171 | hadError = true; |
| 1172 | ++Index; |
| 1173 | ++StructuredIndex; |
| 1174 | return; |
| 1175 | } |
| 1176 | |
| 1177 | if (VerifyOnly) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1178 | if (!SemaRef.CanPerformCopyInitialization(Entity,expr)) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1179 | hadError = true; |
| 1180 | ++Index; |
| 1181 | return; |
| 1182 | } |
| 1183 | |
| 1184 | ExprResult Result = |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1185 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), expr, |
| 1186 | /*TopLevelOfInitList=*/true); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1187 | |
| 1188 | if (Result.isInvalid()) |
| 1189 | hadError = true; |
| 1190 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1191 | expr = Result.getAs<Expr>(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1192 | IList->setInit(Index, expr); |
| 1193 | |
| 1194 | if (hadError) |
| 1195 | ++StructuredIndex; |
| 1196 | else |
| 1197 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 1198 | ++Index; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1201 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1202 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1203 | unsigned &Index, |
| 1204 | InitListExpr *StructuredList, |
| 1205 | unsigned &StructuredIndex) { |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1206 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 1207 | unsigned maxElements = VT->getNumElements(); |
| 1208 | unsigned numEltsInit = 0; |
| 1209 | QualType elementType = VT->getElementType(); |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1210 | |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1211 | if (Index >= IList->getNumInits()) { |
| 1212 | // Make sure the element type can be value-initialized. |
| 1213 | if (VerifyOnly) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1214 | CheckEmptyInitializable( |
| 1215 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity), |
| 1216 | IList->getLocEnd()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1217 | return; |
| 1218 | } |
| 1219 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1220 | if (!SemaRef.getLangOpts().OpenCL) { |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1221 | // If the initializing element is a vector, try to copy-initialize |
| 1222 | // instead of breaking it apart (which is doomed to failure anyway). |
| 1223 | Expr *Init = IList->getInit(Index); |
| 1224 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1225 | if (VerifyOnly) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1226 | if (!SemaRef.CanPerformCopyInitialization(Entity, Init)) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1227 | hadError = true; |
| 1228 | ++Index; |
| 1229 | return; |
| 1230 | } |
| 1231 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1232 | ExprResult Result = |
| 1233 | SemaRef.PerformCopyInitialization(Entity, Init->getLocStart(), Init, |
| 1234 | /*TopLevelOfInitList=*/true); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1235 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1236 | Expr *ResultExpr = nullptr; |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1237 | if (Result.isInvalid()) |
| 1238 | hadError = true; // types weren't compatible. |
| 1239 | else { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1240 | ResultExpr = Result.getAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1241 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1242 | if (ResultExpr != Init) { |
| 1243 | // The type was promoted, update initializer list. |
| 1244 | IList->setInit(Index, ResultExpr); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1245 | } |
| 1246 | } |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1247 | if (hadError) |
| 1248 | ++StructuredIndex; |
| 1249 | else |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1250 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1251 | ResultExpr); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1252 | ++Index; |
| 1253 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1254 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1255 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1256 | InitializedEntity ElementEntity = |
| 1257 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1258 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1259 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 1260 | // Don't attempt to go past the end of the init list |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1261 | if (Index >= IList->getNumInits()) { |
| 1262 | if (VerifyOnly) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1263 | CheckEmptyInitializable(ElementEntity, IList->getLocEnd()); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1264 | break; |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1265 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1266 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1267 | ElementEntity.setElementIndex(Index); |
| 1268 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1269 | StructuredList, StructuredIndex); |
| 1270 | } |
James Molloy | 9eef265 | 2014-06-20 14:35:13 +0000 | [diff] [blame] | 1271 | |
| 1272 | if (VerifyOnly) |
| 1273 | return; |
| 1274 | |
| 1275 | bool isBigEndian = SemaRef.Context.getTargetInfo().isBigEndian(); |
| 1276 | const VectorType *T = Entity.getType()->getAs<VectorType>(); |
| 1277 | if (isBigEndian && (T->getVectorKind() == VectorType::NeonVector || |
| 1278 | T->getVectorKind() == VectorType::NeonPolyVector)) { |
| 1279 | // The ability to use vector initializer lists is a GNU vector extension |
| 1280 | // and is unrelated to the NEON intrinsics in arm_neon.h. On little |
| 1281 | // endian machines it works fine, however on big endian machines it |
| 1282 | // exhibits surprising behaviour: |
| 1283 | // |
| 1284 | // uint32x2_t x = {42, 64}; |
| 1285 | // return vget_lane_u32(x, 0); // Will return 64. |
| 1286 | // |
| 1287 | // Because of this, explicitly call out that it is non-portable. |
| 1288 | // |
| 1289 | SemaRef.Diag(IList->getLocStart(), |
| 1290 | diag::warn_neon_vector_initializer_non_portable); |
| 1291 | |
| 1292 | const char *typeCode; |
| 1293 | unsigned typeSize = SemaRef.Context.getTypeSize(elementType); |
| 1294 | |
| 1295 | if (elementType->isFloatingType()) |
| 1296 | typeCode = "f"; |
| 1297 | else if (elementType->isSignedIntegerType()) |
| 1298 | typeCode = "s"; |
| 1299 | else if (elementType->isUnsignedIntegerType()) |
| 1300 | typeCode = "u"; |
| 1301 | else |
| 1302 | llvm_unreachable("Invalid element type!"); |
| 1303 | |
| 1304 | SemaRef.Diag(IList->getLocStart(), |
| 1305 | SemaRef.Context.getTypeSize(VT) > 64 ? |
| 1306 | diag::note_neon_vector_initializer_non_portable_q : |
| 1307 | diag::note_neon_vector_initializer_non_portable) |
| 1308 | << typeCode << typeSize; |
| 1309 | } |
| 1310 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1311 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1312 | } |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1313 | |
| 1314 | InitializedEntity ElementEntity = |
| 1315 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1316 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1317 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 1318 | for (unsigned i = 0; i < maxElements; ++i) { |
| 1319 | // Don't attempt to go past the end of the init list |
| 1320 | if (Index >= IList->getNumInits()) |
| 1321 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1322 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1323 | ElementEntity.setElementIndex(Index); |
| 1324 | |
| 1325 | QualType IType = IList->getInit(Index)->getType(); |
| 1326 | if (!IType->isVectorType()) { |
| 1327 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1328 | StructuredList, StructuredIndex); |
| 1329 | ++numEltsInit; |
| 1330 | } else { |
| 1331 | QualType VecType; |
| 1332 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 1333 | unsigned numIElts = IVT->getNumElements(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1334 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1335 | if (IType->isExtVectorType()) |
| 1336 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 1337 | else |
| 1338 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1339 | IVT->getVectorKind()); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1340 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 1341 | StructuredList, StructuredIndex); |
| 1342 | numEltsInit += numIElts; |
| 1343 | } |
| 1344 | } |
| 1345 | |
| 1346 | // OpenCL requires all elements to be initialized. |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1347 | if (numEltsInit != maxElements) { |
| 1348 | if (!VerifyOnly) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1349 | SemaRef.Diag(IList->getLocStart(), |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1350 | diag::err_vector_incorrect_num_initializers) |
| 1351 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
| 1352 | hadError = true; |
| 1353 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1354 | } |
| 1355 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1356 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1357 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1358 | llvm::APSInt elementIndex, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1359 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1360 | unsigned &Index, |
| 1361 | InitListExpr *StructuredList, |
| 1362 | unsigned &StructuredIndex) { |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1363 | const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType); |
| 1364 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1365 | // Check for the special-case of initializing an array with a string. |
| 1366 | if (Index < IList->getNumInits()) { |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1367 | if (IsStringInit(IList->getInit(Index), arrayType, SemaRef.Context) == |
| 1368 | SIF_None) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1369 | // We place the string literal directly into the resulting |
| 1370 | // initializer list. This is the only place where the structure |
| 1371 | // of the structured initializer list doesn't match exactly, |
| 1372 | // because doing so would involve allocating one character |
| 1373 | // constant for each string. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1374 | if (!VerifyOnly) { |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1375 | CheckStringInit(IList->getInit(Index), DeclType, arrayType, SemaRef); |
| 1376 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1377 | IList->getInit(Index)); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1378 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
| 1379 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1380 | ++Index; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1381 | return; |
| 1382 | } |
| 1383 | } |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1384 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1385 | // Check for VLAs; in standard C it would be possible to check this |
| 1386 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 1387 | // them in all sorts of strange places). |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1388 | if (!VerifyOnly) |
| 1389 | SemaRef.Diag(VAT->getSizeExpr()->getLocStart(), |
| 1390 | diag::err_variable_object_no_init) |
| 1391 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1392 | hadError = true; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1393 | ++Index; |
| 1394 | ++StructuredIndex; |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1395 | return; |
| 1396 | } |
| 1397 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1398 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1399 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 1400 | elementIndex.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1401 | bool maxElementsKnown = false; |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1402 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1403 | maxElements = CAT->getSize(); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1404 | elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1405 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1406 | maxElementsKnown = true; |
| 1407 | } |
| 1408 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1409 | QualType elementType = arrayType->getElementType(); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1410 | while (Index < IList->getNumInits()) { |
| 1411 | Expr *Init = IList->getInit(Index); |
| 1412 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1413 | // If we're not the subobject that matches up with the '{' for |
| 1414 | // the designator, we shouldn't be handling the |
| 1415 | // designator. Return immediately. |
| 1416 | if (!SubobjectIsDesignatorContext) |
| 1417 | return; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1418 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1419 | // Handle this designated initializer. elementIndex will be |
| 1420 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1421 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1422 | DeclType, nullptr, &elementIndex, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1423 | StructuredList, StructuredIndex, true, |
| 1424 | false)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1425 | hadError = true; |
| 1426 | continue; |
| 1427 | } |
| 1428 | |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1429 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1430 | maxElements = maxElements.extend(elementIndex.getBitWidth()); |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1431 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1432 | elementIndex = elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1433 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1434 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1435 | // If the array is of incomplete type, keep track of the number of |
| 1436 | // elements in the initializer. |
| 1437 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1438 | maxElements = elementIndex; |
| 1439 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1440 | continue; |
| 1441 | } |
| 1442 | |
| 1443 | // If we know the maximum number of elements, and we've already |
| 1444 | // hit it, stop consuming elements in the initializer list. |
| 1445 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1446 | break; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1447 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1448 | InitializedEntity ElementEntity = |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1449 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1450 | Entity); |
| 1451 | // Check this element. |
| 1452 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1453 | StructuredList, StructuredIndex); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1454 | ++elementIndex; |
| 1455 | |
| 1456 | // If the array is of incomplete type, keep track of the number of |
| 1457 | // elements in the initializer. |
| 1458 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1459 | maxElements = elementIndex; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1460 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1461 | if (!hadError && DeclType->isIncompleteArrayType() && !VerifyOnly) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1462 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1463 | // be calculated here. |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1464 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1465 | if (maxElements == Zero) { |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1466 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1467 | // but is supported by GNU. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1468 | SemaRef.Diag(IList->getLocStart(), |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1469 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1470 | } |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1471 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1472 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1473 | ArrayType::Normal, 0); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1474 | } |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1475 | if (!hadError && VerifyOnly) { |
| 1476 | // Check if there are any members of the array that get value-initialized. |
| 1477 | // If so, check if doing that is possible. |
| 1478 | // FIXME: This needs to detect holes left by designated initializers too. |
| 1479 | if (maxElementsKnown && elementIndex < maxElements) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1480 | CheckEmptyInitializable(InitializedEntity::InitializeElement( |
| 1481 | SemaRef.Context, 0, Entity), |
| 1482 | IList->getLocEnd()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1483 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1486 | bool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 1487 | Expr *InitExpr, |
| 1488 | FieldDecl *Field, |
| 1489 | bool TopLevelObject) { |
| 1490 | // Handle GNU flexible array initializers. |
| 1491 | unsigned FlexArrayDiag; |
| 1492 | if (isa<InitListExpr>(InitExpr) && |
| 1493 | cast<InitListExpr>(InitExpr)->getNumInits() == 0) { |
| 1494 | // Empty flexible array init always allowed as an extension |
| 1495 | FlexArrayDiag = diag::ext_flexible_array_init; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1496 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1497 | // Disallow flexible array init in C++; it is not required for gcc |
| 1498 | // compatibility, and it needs work to IRGen correctly in general. |
| 1499 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1500 | } else if (!TopLevelObject) { |
| 1501 | // Disallow flexible array init on non-top-level object |
| 1502 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1503 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
| 1504 | // Disallow flexible array init on anything which is not a variable. |
| 1505 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1506 | } else if (cast<VarDecl>(Entity.getDecl())->hasLocalStorage()) { |
| 1507 | // Disallow flexible array init on local variables. |
| 1508 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1509 | } else { |
| 1510 | // Allow other cases. |
| 1511 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1512 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1513 | |
| 1514 | if (!VerifyOnly) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1515 | SemaRef.Diag(InitExpr->getLocStart(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1516 | FlexArrayDiag) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1517 | << InitExpr->getLocStart(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1518 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1519 | << Field; |
| 1520 | } |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1521 | |
| 1522 | return FlexArrayDiag != diag::ext_flexible_array_init; |
| 1523 | } |
| 1524 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1525 | void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 73eb7cd | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 1526 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1527 | QualType DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1528 | RecordDecl::field_iterator Field, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1529 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1530 | unsigned &Index, |
| 1531 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1532 | unsigned &StructuredIndex, |
| 1533 | bool TopLevelObject) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1534 | RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1535 | |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1536 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1537 | // confusion, we forgo checking the intializer for the entire record. |
| 1538 | if (structDecl->isInvalidDecl()) { |
Richard Smith | 845aa66 | 2012-09-28 21:23:50 +0000 | [diff] [blame] | 1539 | // Assume it was supposed to consume a single initializer. |
| 1540 | ++Index; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1541 | hadError = true; |
| 1542 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1543 | } |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1544 | |
| 1545 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1546 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1547 | |
| 1548 | // If there's a default initializer, use it. |
| 1549 | if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->hasInClassInitializer()) { |
| 1550 | if (VerifyOnly) |
| 1551 | return; |
| 1552 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1553 | Field != FieldEnd; ++Field) { |
| 1554 | if (Field->hasInClassInitializer()) { |
| 1555 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1556 | // FIXME: Actually build a CXXDefaultInitExpr? |
| 1557 | return; |
| 1558 | } |
| 1559 | } |
| 1560 | } |
| 1561 | |
Reid Kleckner | 6d829bd | 2014-11-12 21:30:23 +0000 | [diff] [blame] | 1562 | // Value-initialize the first member of the union that isn't an unnamed |
| 1563 | // bitfield. |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1564 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1565 | Field != FieldEnd; ++Field) { |
Reid Kleckner | 6d829bd | 2014-11-12 21:30:23 +0000 | [diff] [blame] | 1566 | if (!Field->isUnnamedBitfield()) { |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1567 | if (VerifyOnly) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1568 | CheckEmptyInitializable( |
| 1569 | InitializedEntity::InitializeMember(*Field, &Entity), |
| 1570 | IList->getLocEnd()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1571 | else |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1572 | StructuredList->setInitializedFieldInUnion(*Field); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1573 | break; |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1574 | } |
| 1575 | } |
| 1576 | return; |
| 1577 | } |
| 1578 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1579 | // If structDecl is a forward declaration, this loop won't do |
| 1580 | // anything except look at designated initializers; That's okay, |
| 1581 | // because an error should get printed out elsewhere. It might be |
| 1582 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1583 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1584 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1585 | bool InitializedSomething = false; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1586 | bool CheckForMissingFields = true; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1587 | while (Index < IList->getNumInits()) { |
| 1588 | Expr *Init = IList->getInit(Index); |
| 1589 | |
| 1590 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1591 | // If we're not the subobject that matches up with the '{' for |
| 1592 | // the designator, we shouldn't be handling the |
| 1593 | // designator. Return immediately. |
| 1594 | if (!SubobjectIsDesignatorContext) |
| 1595 | return; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1596 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1597 | // Handle this designated initializer. Field will be updated to |
| 1598 | // the next field that we'll be initializing. |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1599 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1600 | DeclType, &Field, nullptr, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1601 | StructuredList, StructuredIndex, |
| 1602 | true, TopLevelObject)) |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1603 | hadError = true; |
| 1604 | |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1605 | InitializedSomething = true; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1606 | |
| 1607 | // Disable check for missing fields when designators are used. |
| 1608 | // This matches gcc behaviour. |
| 1609 | CheckForMissingFields = false; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1610 | continue; |
| 1611 | } |
| 1612 | |
| 1613 | if (Field == FieldEnd) { |
| 1614 | // We've run out of fields. We're done. |
| 1615 | break; |
| 1616 | } |
| 1617 | |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1618 | // We've already initialized a member of a union. We're done. |
| 1619 | if (InitializedSomething && DeclType->isUnionType()) |
| 1620 | break; |
| 1621 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1622 | // If we've hit the flexible array member at the end, we're done. |
| 1623 | if (Field->getType()->isIncompleteArrayType()) |
| 1624 | break; |
| 1625 | |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1626 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1627 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1628 | ++Field; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1629 | continue; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1630 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1631 | |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1632 | // Make sure we can use this declaration. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1633 | bool InvalidUse; |
| 1634 | if (VerifyOnly) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1635 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1636 | else |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1637 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1638 | IList->getInit(Index)->getLocStart()); |
| 1639 | if (InvalidUse) { |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1640 | ++Index; |
| 1641 | ++Field; |
| 1642 | hadError = true; |
| 1643 | continue; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1644 | } |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1645 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1646 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1647 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1648 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 1649 | StructuredList, StructuredIndex); |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1650 | InitializedSomething = true; |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1651 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1652 | if (DeclType->isUnionType() && !VerifyOnly) { |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1653 | // Initialize the first field within the union. |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1654 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1655 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1656 | |
| 1657 | ++Field; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1658 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1659 | |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1660 | // Emit warnings for missing struct field initializers. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1661 | if (!VerifyOnly && InitializedSomething && CheckForMissingFields && |
| 1662 | Field != FieldEnd && !Field->getType()->isIncompleteArrayType() && |
| 1663 | !DeclType->isUnionType()) { |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1664 | // It is possible we have one or more unnamed bitfields remaining. |
| 1665 | // Find first (if any) named field and emit warning. |
| 1666 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 1667 | it != end; ++it) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1668 | if (!it->isUnnamedBitfield() && !it->hasInClassInitializer()) { |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1669 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
Aaron Ballman | b9bb201 | 2014-01-03 14:54:10 +0000 | [diff] [blame] | 1670 | diag::warn_missing_field_initializers) << *it; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1671 | break; |
| 1672 | } |
| 1673 | } |
| 1674 | } |
| 1675 | |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1676 | // Check that any remaining fields can be value-initialized. |
| 1677 | if (VerifyOnly && Field != FieldEnd && !DeclType->isUnionType() && |
| 1678 | !Field->getType()->isIncompleteArrayType()) { |
| 1679 | // FIXME: Should check for holes left by designated initializers too. |
| 1680 | for (; Field != FieldEnd && !hadError; ++Field) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1681 | if (!Field->isUnnamedBitfield() && !Field->hasInClassInitializer()) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1682 | CheckEmptyInitializable( |
| 1683 | InitializedEntity::InitializeMember(*Field, &Entity), |
| 1684 | IList->getLocEnd()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1685 | } |
| 1686 | } |
| 1687 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1688 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1689 | Index >= IList->getNumInits()) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1690 | return; |
| 1691 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1692 | if (CheckFlexibleArrayInit(Entity, IList->getInit(Index), *Field, |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1693 | TopLevelObject)) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1694 | hadError = true; |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1695 | ++Index; |
| 1696 | return; |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1699 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1700 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1701 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1702 | if (isa<InitListExpr>(IList->getInit(Index))) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1703 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1704 | StructuredList, StructuredIndex); |
| 1705 | else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1706 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 1707 | StructuredList, StructuredIndex); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1708 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1709 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1710 | /// \brief Expand a field designator that refers to a member of an |
| 1711 | /// anonymous struct or union into a series of field designators that |
| 1712 | /// refers to the field within the appropriate subobject. |
| 1713 | /// |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1714 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1715 | DesignatedInitExpr *DIE, |
| 1716 | unsigned DesigIdx, |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1717 | IndirectFieldDecl *IndirectField) { |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1718 | typedef DesignatedInitExpr::Designator Designator; |
| 1719 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1720 | // Build the replacement designators. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1721 | SmallVector<Designator, 4> Replacements; |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1722 | for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), |
| 1723 | PE = IndirectField->chain_end(); PI != PE; ++PI) { |
| 1724 | if (PI + 1 == PE) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1725 | Replacements.push_back(Designator((IdentifierInfo *)nullptr, |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1726 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 1727 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 1728 | else |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1729 | Replacements.push_back(Designator((IdentifierInfo *)nullptr, |
| 1730 | SourceLocation(), SourceLocation())); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1731 | assert(isa<FieldDecl>(*PI)); |
| 1732 | Replacements.back().setField(cast<FieldDecl>(*PI)); |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1733 | } |
| 1734 | |
| 1735 | // Expand the current designator into the set of replacement |
| 1736 | // designators, so we have a full subobject path down to where the |
| 1737 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 03e8bdc | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 1738 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1739 | &Replacements[0] + Replacements.size()); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1740 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1741 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1742 | static DesignatedInitExpr *CloneDesignatedInitExpr(Sema &SemaRef, |
| 1743 | DesignatedInitExpr *DIE) { |
| 1744 | unsigned NumIndexExprs = DIE->getNumSubExprs() - 1; |
| 1745 | SmallVector<Expr*, 4> IndexExprs(NumIndexExprs); |
| 1746 | for (unsigned I = 0; I < NumIndexExprs; ++I) |
| 1747 | IndexExprs[I] = DIE->getSubExpr(I + 1); |
| 1748 | return DesignatedInitExpr::Create(SemaRef.Context, DIE->designators_begin(), |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 1749 | DIE->size(), IndexExprs, |
| 1750 | DIE->getEqualOrColonLoc(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1751 | DIE->usesGNUSyntax(), DIE->getInit()); |
| 1752 | } |
| 1753 | |
Kaelyn Uhrain | b02c5e9 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1754 | namespace { |
| 1755 | |
| 1756 | // Callback to only accept typo corrections that are for field members of |
| 1757 | // the given struct or union. |
| 1758 | class FieldInitializerValidatorCCC : public CorrectionCandidateCallback { |
| 1759 | public: |
| 1760 | explicit FieldInitializerValidatorCCC(RecordDecl *RD) |
| 1761 | : Record(RD) {} |
| 1762 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1763 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
Kaelyn Uhrain | b02c5e9 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1764 | FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>(); |
| 1765 | return FD && FD->getDeclContext()->getRedeclContext()->Equals(Record); |
| 1766 | } |
| 1767 | |
| 1768 | private: |
| 1769 | RecordDecl *Record; |
| 1770 | }; |
| 1771 | |
| 1772 | } |
| 1773 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1774 | /// @brief Check the well-formedness of a C99 designated initializer. |
| 1775 | /// |
| 1776 | /// Determines whether the designated initializer @p DIE, which |
| 1777 | /// resides at the given @p Index within the initializer list @p |
| 1778 | /// IList, is well-formed for a current object of type @p DeclType |
| 1779 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1780 | /// within the current subobject is returned in either |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1781 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1782 | /// |
| 1783 | /// @param IList The initializer list in which this designated |
| 1784 | /// initializer occurs. |
| 1785 | /// |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1786 | /// @param DIE The designated initializer expression. |
| 1787 | /// |
| 1788 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1789 | /// |
Dmitri Gribenko | adba9be | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 1790 | /// @param CurrentObjectType The type of the "current object" (C99 6.7.8p17), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1791 | /// into which the designation in @p DIE should refer. |
| 1792 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1793 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 1794 | /// a field, this will be set to the field declaration corresponding |
| 1795 | /// to the field named by the designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1796 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1797 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 1798 | /// DIE is an array designator or GNU array-range designator, this |
| 1799 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1800 | /// |
| 1801 | /// @param Index Index into @p IList where the designated initializer |
| 1802 | /// @p DIE occurs. |
| 1803 | /// |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1804 | /// @param StructuredList The initializer list expression that |
| 1805 | /// describes all of the subobject initializers in the order they'll |
| 1806 | /// actually be initialized. |
| 1807 | /// |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1808 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1809 | bool |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1810 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1811 | InitListExpr *IList, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1812 | DesignatedInitExpr *DIE, |
| 1813 | unsigned DesigIdx, |
| 1814 | QualType &CurrentObjectType, |
| 1815 | RecordDecl::field_iterator *NextField, |
| 1816 | llvm::APSInt *NextElementIndex, |
| 1817 | unsigned &Index, |
| 1818 | InitListExpr *StructuredList, |
| 1819 | unsigned &StructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1820 | bool FinishSubobjectInit, |
| 1821 | bool TopLevelObject) { |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1822 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1823 | // Check the actual initialization for the designated object type. |
| 1824 | bool prevHadError = hadError; |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1825 | |
| 1826 | // Temporarily remove the designator expression from the |
| 1827 | // initializer list that the child calls see, so that we don't try |
| 1828 | // to re-process the designator. |
| 1829 | unsigned OldIndex = Index; |
| 1830 | IList->setInit(OldIndex, DIE->getInit()); |
| 1831 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1832 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1833 | StructuredList, StructuredIndex); |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1834 | |
| 1835 | // Restore the designated initializer expression in the syntactic |
| 1836 | // form of the initializer list. |
| 1837 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 1838 | DIE->setInit(IList->getInit(OldIndex)); |
| 1839 | IList->setInit(OldIndex, DIE); |
| 1840 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1841 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1844 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1845 | bool IsFirstDesignator = (DesigIdx == 0); |
| 1846 | if (!VerifyOnly) { |
| 1847 | assert((IsFirstDesignator || StructuredList) && |
| 1848 | "Need a non-designated initializer list to start from"); |
| 1849 | |
| 1850 | // Determine the structural initializer list that corresponds to the |
| 1851 | // current subobject. |
Benjamin Kramer | 6b441d6 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 1852 | StructuredList = IsFirstDesignator? SyntacticToSemantic.lookup(IList) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1853 | : getStructuredSubobjectInit(IList, Index, CurrentObjectType, |
| 1854 | StructuredList, StructuredIndex, |
Erik Verbruggen | 11a2ecc | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1855 | SourceRange(D->getLocStart(), |
| 1856 | DIE->getLocEnd())); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1857 | assert(StructuredList && "Expected a structured initializer list"); |
| 1858 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1859 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1860 | if (D->isFieldDesignator()) { |
| 1861 | // C99 6.7.8p7: |
| 1862 | // |
| 1863 | // If a designator has the form |
| 1864 | // |
| 1865 | // . identifier |
| 1866 | // |
| 1867 | // then the current object (defined below) shall have |
| 1868 | // structure or union type and the identifier shall be the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1869 | // name of a member of that type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1870 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1871 | if (!RT) { |
| 1872 | SourceLocation Loc = D->getDotLoc(); |
| 1873 | if (Loc.isInvalid()) |
| 1874 | Loc = D->getFieldLoc(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1875 | if (!VerifyOnly) |
| 1876 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1877 | << SemaRef.getLangOpts().CPlusPlus << CurrentObjectType; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1878 | ++Index; |
| 1879 | return true; |
| 1880 | } |
| 1881 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1882 | FieldDecl *KnownField = D->getField(); |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 1883 | if (!KnownField) { |
| 1884 | IdentifierInfo *FieldName = D->getFieldName(); |
| 1885 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
| 1886 | for (NamedDecl *ND : Lookup) { |
| 1887 | if (auto *FD = dyn_cast<FieldDecl>(ND)) { |
| 1888 | KnownField = FD; |
| 1889 | break; |
| 1890 | } |
| 1891 | if (auto *IFD = dyn_cast<IndirectFieldDecl>(ND)) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1892 | // In verify mode, don't modify the original. |
| 1893 | if (VerifyOnly) |
| 1894 | DIE = CloneDesignatedInitExpr(SemaRef, DIE); |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 1895 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IFD); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1896 | D = DIE->getDesignator(DesigIdx); |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 1897 | KnownField = cast<FieldDecl>(*IFD->chain_begin()); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1898 | break; |
| 1899 | } |
| 1900 | } |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 1901 | if (!KnownField) { |
| 1902 | if (VerifyOnly) { |
| 1903 | ++Index; |
| 1904 | return true; // No typo correction when just trying this out. |
| 1905 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1906 | |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 1907 | // Name lookup found something, but it wasn't a field. |
| 1908 | if (!Lookup.empty()) { |
| 1909 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
| 1910 | << FieldName; |
| 1911 | SemaRef.Diag(Lookup.front()->getLocation(), |
| 1912 | diag::note_field_designator_found); |
| 1913 | ++Index; |
| 1914 | return true; |
| 1915 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1916 | |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 1917 | // Name lookup didn't find anything. |
| 1918 | // Determine whether this was a typo for another field name. |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1919 | if (TypoCorrection Corrected = SemaRef.CorrectTypo( |
| 1920 | DeclarationNameInfo(FieldName, D->getFieldLoc()), |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 1921 | Sema::LookupMemberName, /*Scope=*/nullptr, /*SS=*/nullptr, |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 1922 | llvm::make_unique<FieldInitializerValidatorCCC>(RT->getDecl()), |
| 1923 | Sema::CTK_ErrorRecovery, RT->getDecl())) { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1924 | SemaRef.diagnoseTypo( |
| 1925 | Corrected, |
| 1926 | SemaRef.PDiag(diag::err_field_designator_unknown_suggest) |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 1927 | << FieldName << CurrentObjectType); |
| 1928 | KnownField = Corrected.getCorrectionDeclAs<FieldDecl>(); |
Benjamin Kramer | afe718f | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1929 | hadError = true; |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1930 | } else { |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 1931 | // Typo correction didn't find anything. |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1932 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 1933 | << FieldName << CurrentObjectType; |
| 1934 | ++Index; |
| 1935 | return true; |
| 1936 | } |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1937 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1938 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1939 | |
David Majnemer | 58e4ea9 | 2014-08-23 01:48:50 +0000 | [diff] [blame] | 1940 | unsigned FieldIndex = 0; |
| 1941 | for (auto *FI : RT->getDecl()->fields()) { |
| 1942 | if (FI->isUnnamedBitfield()) |
| 1943 | continue; |
| 1944 | if (KnownField == FI) |
| 1945 | break; |
| 1946 | ++FieldIndex; |
| 1947 | } |
| 1948 | |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 1949 | RecordDecl::field_iterator Field = |
| 1950 | RecordDecl::field_iterator(DeclContext::decl_iterator(KnownField)); |
| 1951 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1952 | // All of the fields of a union are located at the same place in |
| 1953 | // the initializer list. |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1954 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1955 | FieldIndex = 0; |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 1956 | if (!VerifyOnly) { |
| 1957 | FieldDecl *CurrentField = StructuredList->getInitializedFieldInUnion(); |
| 1958 | if (CurrentField && CurrentField != *Field) { |
| 1959 | assert(StructuredList->getNumInits() == 1 |
| 1960 | && "A union should never have more than one initializer!"); |
| 1961 | |
| 1962 | // we're about to throw away an initializer, emit warning |
| 1963 | SemaRef.Diag(D->getFieldLoc(), |
| 1964 | diag::warn_initializer_overrides) |
| 1965 | << D->getSourceRange(); |
| 1966 | Expr *ExistingInit = StructuredList->getInit(0); |
| 1967 | SemaRef.Diag(ExistingInit->getLocStart(), |
| 1968 | diag::note_previous_initializer) |
| 1969 | << /*FIXME:has side effects=*/0 |
| 1970 | << ExistingInit->getSourceRange(); |
| 1971 | |
| 1972 | // remove existing initializer |
| 1973 | StructuredList->resizeInits(SemaRef.Context, 0); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1974 | StructuredList->setInitializedFieldInUnion(nullptr); |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 1975 | } |
| 1976 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1977 | StructuredList->setInitializedFieldInUnion(*Field); |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 1978 | } |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1979 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1980 | |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1981 | // Make sure we can use this declaration. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1982 | bool InvalidUse; |
| 1983 | if (VerifyOnly) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1984 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1985 | else |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1986 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, D->getFieldLoc()); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1987 | if (InvalidUse) { |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1988 | ++Index; |
| 1989 | return true; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1990 | } |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1991 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1992 | if (!VerifyOnly) { |
| 1993 | // Update the designator with the field declaration. |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1994 | D->setField(*Field); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1995 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1996 | // Make sure that our non-designated initializer list has space |
| 1997 | // for a subobject corresponding to this field. |
| 1998 | if (FieldIndex >= StructuredList->getNumInits()) |
| 1999 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
| 2000 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2001 | |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2002 | // This designator names a flexible array member. |
| 2003 | if (Field->getType()->isIncompleteArrayType()) { |
| 2004 | bool Invalid = false; |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 2005 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2006 | // We can't designate an object within the flexible array |
| 2007 | // member (because GCC doesn't allow it). |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2008 | if (!VerifyOnly) { |
| 2009 | DesignatedInitExpr::Designator *NextD |
| 2010 | = DIE->getDesignator(DesigIdx + 1); |
Erik Verbruggen | 11a2ecc | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 2011 | SemaRef.Diag(NextD->getLocStart(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2012 | diag::err_designator_into_flexible_array_member) |
Erik Verbruggen | 11a2ecc | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 2013 | << SourceRange(NextD->getLocStart(), |
| 2014 | DIE->getLocEnd()); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2015 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2016 | << *Field; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2017 | } |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2018 | Invalid = true; |
| 2019 | } |
| 2020 | |
Chris Lattner | 001b29c | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 2021 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 2022 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2023 | // The initializer is not an initializer list. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2024 | if (!VerifyOnly) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2025 | SemaRef.Diag(DIE->getInit()->getLocStart(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2026 | diag::err_flexible_array_init_needs_braces) |
| 2027 | << DIE->getInit()->getSourceRange(); |
| 2028 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2029 | << *Field; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2030 | } |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2031 | Invalid = true; |
| 2032 | } |
| 2033 | |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 2034 | // Check GNU flexible array initializer. |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2035 | if (!Invalid && CheckFlexibleArrayInit(Entity, DIE->getInit(), *Field, |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 2036 | TopLevelObject)) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2037 | Invalid = true; |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2038 | |
| 2039 | if (Invalid) { |
| 2040 | ++Index; |
| 2041 | return true; |
| 2042 | } |
| 2043 | |
| 2044 | // Initialize the array. |
| 2045 | bool prevHadError = hadError; |
| 2046 | unsigned newStructuredIndex = FieldIndex; |
| 2047 | unsigned OldIndex = Index; |
| 2048 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2049 | |
| 2050 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2051 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2052 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2053 | StructuredList, newStructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2054 | |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2055 | IList->setInit(OldIndex, DIE); |
| 2056 | if (hadError && !prevHadError) { |
| 2057 | ++Field; |
| 2058 | ++FieldIndex; |
| 2059 | if (NextField) |
| 2060 | *NextField = Field; |
| 2061 | StructuredIndex = FieldIndex; |
| 2062 | return true; |
| 2063 | } |
| 2064 | } else { |
| 2065 | // Recurse to check later designated subobjects. |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 2066 | QualType FieldType = Field->getType(); |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2067 | unsigned newStructuredIndex = FieldIndex; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2068 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2069 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2070 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2071 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2072 | FieldType, nullptr, nullptr, Index, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2073 | StructuredList, newStructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2074 | true, false)) |
| 2075 | return true; |
| 2076 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2077 | |
| 2078 | // Find the position of the next field to be initialized in this |
| 2079 | // subobject. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2080 | ++Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2081 | ++FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2082 | |
| 2083 | // If this the first designator, our caller will continue checking |
| 2084 | // the rest of this struct/class/union subobject. |
| 2085 | if (IsFirstDesignator) { |
| 2086 | if (NextField) |
| 2087 | *NextField = Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2088 | StructuredIndex = FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2089 | return false; |
| 2090 | } |
| 2091 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2092 | if (!FinishSubobjectInit) |
| 2093 | return false; |
| 2094 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2095 | // We've already initialized something in the union; we're done. |
| 2096 | if (RT->getDecl()->isUnion()) |
| 2097 | return hadError; |
| 2098 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2099 | // Check the remaining fields within this class/struct/union subobject. |
| 2100 | bool prevHadError = hadError; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2101 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2102 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2103 | StructuredList, FieldIndex); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2104 | return hadError && !prevHadError; |
| 2105 | } |
| 2106 | |
| 2107 | // C99 6.7.8p6: |
| 2108 | // |
| 2109 | // If a designator has the form |
| 2110 | // |
| 2111 | // [ constant-expression ] |
| 2112 | // |
| 2113 | // then the current object (defined below) shall have array |
| 2114 | // type and the expression shall be an integer constant |
| 2115 | // expression. If the array is of unknown size, any |
| 2116 | // nonnegative value is valid. |
| 2117 | // |
| 2118 | // Additionally, cope with the GNU extension that permits |
| 2119 | // designators of the form |
| 2120 | // |
| 2121 | // [ constant-expression ... constant-expression ] |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 2122 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2123 | if (!AT) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2124 | if (!VerifyOnly) |
| 2125 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
| 2126 | << CurrentObjectType; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2127 | ++Index; |
| 2128 | return true; |
| 2129 | } |
| 2130 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2131 | Expr *IndexExpr = nullptr; |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2132 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 2133 | if (D->isArrayDesignator()) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2134 | IndexExpr = DIE->getArrayIndex(*D); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2135 | DesignatedStartIndex = IndexExpr->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2136 | DesignatedEndIndex = DesignatedStartIndex; |
| 2137 | } else { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2138 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2139 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2140 | DesignatedStartIndex = |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2141 | DIE->getArrayRangeStart(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2142 | DesignatedEndIndex = |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2143 | DIE->getArrayRangeEnd(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2144 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2145 | |
Chris Lattner | b0ed51d | 2011-02-19 22:28:58 +0000 | [diff] [blame] | 2146 | // Codegen can't handle evaluating array range designators that have side |
| 2147 | // effects, because we replicate the AST value for each initialized element. |
| 2148 | // As such, set the sawArrayRangeDesignator() bit if we initialize multiple |
| 2149 | // elements with something that has a side effect, so codegen can emit an |
| 2150 | // "error unsupported" error instead of miscompiling the app. |
| 2151 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2152 | DIE->getInit()->HasSideEffects(SemaRef.Context) && !VerifyOnly) |
Douglas Gregor | bf7207a | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 2153 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2154 | } |
| 2155 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2156 | if (isa<ConstantArrayType>(AT)) { |
| 2157 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2158 | DesignatedStartIndex |
| 2159 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2160 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2161 | DesignatedEndIndex |
| 2162 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2163 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 2164 | if (DesignatedEndIndex >= MaxElements) { |
Eli Friedman | ed0f916 | 2011-09-26 18:53:43 +0000 | [diff] [blame] | 2165 | if (!VerifyOnly) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2166 | SemaRef.Diag(IndexExpr->getLocStart(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2167 | diag::err_array_designator_too_large) |
| 2168 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
| 2169 | << IndexExpr->getSourceRange(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2170 | ++Index; |
| 2171 | return true; |
| 2172 | } |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2173 | } else { |
| 2174 | // Make sure the bit-widths and signedness match. |
| 2175 | if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2176 | DesignatedEndIndex |
| 2177 | = DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth()); |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2178 | else if (DesignatedStartIndex.getBitWidth() < |
| 2179 | DesignatedEndIndex.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2180 | DesignatedStartIndex |
| 2181 | = DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2182 | DesignatedStartIndex.setIsUnsigned(true); |
| 2183 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2184 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2185 | |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2186 | if (!VerifyOnly && StructuredList->isStringLiteralInit()) { |
| 2187 | // We're modifying a string literal init; we have to decompose the string |
| 2188 | // so we can modify the individual characters. |
| 2189 | ASTContext &Context = SemaRef.Context; |
| 2190 | Expr *SubExpr = StructuredList->getInit(0)->IgnoreParens(); |
| 2191 | |
| 2192 | // Compute the character type |
| 2193 | QualType CharTy = AT->getElementType(); |
| 2194 | |
| 2195 | // Compute the type of the integer literals. |
| 2196 | QualType PromotedCharTy = CharTy; |
| 2197 | if (CharTy->isPromotableIntegerType()) |
| 2198 | PromotedCharTy = Context.getPromotedIntegerType(CharTy); |
| 2199 | unsigned PromotedCharTyWidth = Context.getTypeSize(PromotedCharTy); |
| 2200 | |
| 2201 | if (StringLiteral *SL = dyn_cast<StringLiteral>(SubExpr)) { |
| 2202 | // Get the length of the string. |
| 2203 | uint64_t StrLen = SL->getLength(); |
| 2204 | if (cast<ConstantArrayType>(AT)->getSize().ult(StrLen)) |
| 2205 | StrLen = cast<ConstantArrayType>(AT)->getSize().getZExtValue(); |
| 2206 | StructuredList->resizeInits(Context, StrLen); |
| 2207 | |
| 2208 | // Build a literal for each character in the string, and put them into |
| 2209 | // the init list. |
| 2210 | for (unsigned i = 0, e = StrLen; i != e; ++i) { |
| 2211 | llvm::APInt CodeUnit(PromotedCharTyWidth, SL->getCodeUnit(i)); |
| 2212 | Expr *Init = new (Context) IntegerLiteral( |
Eli Friedman | 6cc05f7 | 2013-06-11 22:26:34 +0000 | [diff] [blame] | 2213 | Context, CodeUnit, PromotedCharTy, SubExpr->getExprLoc()); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2214 | if (CharTy != PromotedCharTy) |
| 2215 | Init = ImplicitCastExpr::Create(Context, CharTy, CK_IntegralCast, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2216 | Init, nullptr, VK_RValue); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2217 | StructuredList->updateInit(Context, i, Init); |
| 2218 | } |
| 2219 | } else { |
| 2220 | ObjCEncodeExpr *E = cast<ObjCEncodeExpr>(SubExpr); |
| 2221 | std::string Str; |
| 2222 | Context.getObjCEncodingForType(E->getEncodedType(), Str); |
| 2223 | |
| 2224 | // Get the length of the string. |
| 2225 | uint64_t StrLen = Str.size(); |
| 2226 | if (cast<ConstantArrayType>(AT)->getSize().ult(StrLen)) |
| 2227 | StrLen = cast<ConstantArrayType>(AT)->getSize().getZExtValue(); |
| 2228 | StructuredList->resizeInits(Context, StrLen); |
| 2229 | |
| 2230 | // Build a literal for each character in the string, and put them into |
| 2231 | // the init list. |
| 2232 | for (unsigned i = 0, e = StrLen; i != e; ++i) { |
| 2233 | llvm::APInt CodeUnit(PromotedCharTyWidth, Str[i]); |
| 2234 | Expr *Init = new (Context) IntegerLiteral( |
Eli Friedman | 6cc05f7 | 2013-06-11 22:26:34 +0000 | [diff] [blame] | 2235 | Context, CodeUnit, PromotedCharTy, SubExpr->getExprLoc()); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2236 | if (CharTy != PromotedCharTy) |
| 2237 | Init = ImplicitCastExpr::Create(Context, CharTy, CK_IntegralCast, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2238 | Init, nullptr, VK_RValue); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2239 | StructuredList->updateInit(Context, i, Init); |
| 2240 | } |
| 2241 | } |
| 2242 | } |
| 2243 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2244 | // Make sure that our non-designated initializer list has space |
| 2245 | // for a subobject corresponding to this array element. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2246 | if (!VerifyOnly && |
| 2247 | DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2248 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2249 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2250 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2251 | // Repeatedly perform subobject initializations in the range |
| 2252 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2253 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2254 | // Move to the next designator |
| 2255 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2256 | unsigned OldIndex = Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2257 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2258 | InitializedEntity ElementEntity = |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2259 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2260 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2261 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 2262 | // Recurse to check later designated subobjects. |
| 2263 | QualType ElementType = AT->getElementType(); |
| 2264 | Index = OldIndex; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2265 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2266 | ElementEntity.setElementIndex(ElementIndex); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2267 | if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2268 | ElementType, nullptr, nullptr, Index, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2269 | StructuredList, ElementIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2270 | (DesignatedStartIndex == DesignatedEndIndex), |
| 2271 | false)) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2272 | return true; |
| 2273 | |
| 2274 | // Move to the next index in the array that we'll be initializing. |
| 2275 | ++DesignatedStartIndex; |
| 2276 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2277 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2278 | |
| 2279 | // If this the first designator, our caller will continue checking |
| 2280 | // the rest of this array subobject. |
| 2281 | if (IsFirstDesignator) { |
| 2282 | if (NextElementIndex) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2283 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2284 | StructuredIndex = ElementIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2285 | return false; |
| 2286 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2287 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2288 | if (!FinishSubobjectInit) |
| 2289 | return false; |
| 2290 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2291 | // Check the remaining elements within this array subobject. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2292 | bool prevHadError = hadError; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2293 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 2294 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2295 | StructuredList, ElementIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2296 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2297 | } |
| 2298 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2299 | // Get the structured initializer list for a subobject of type |
| 2300 | // @p CurrentObjectType. |
| 2301 | InitListExpr * |
| 2302 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 2303 | QualType CurrentObjectType, |
| 2304 | InitListExpr *StructuredList, |
| 2305 | unsigned StructuredIndex, |
| 2306 | SourceRange InitRange) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2307 | if (VerifyOnly) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2308 | return nullptr; // No structured list in verification-only mode. |
| 2309 | Expr *ExistingInit = nullptr; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2310 | if (!StructuredList) |
Benjamin Kramer | 6b441d6 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 2311 | ExistingInit = SyntacticToSemantic.lookup(IList); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2312 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 2313 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2314 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2315 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
| 2316 | return Result; |
| 2317 | |
| 2318 | if (ExistingInit) { |
| 2319 | // We are creating an initializer list that initializes the |
| 2320 | // subobjects of the current object, but there was already an |
| 2321 | // initialization that completely initialized the current |
| 2322 | // subobject, e.g., by a compound literal: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2323 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2324 | // struct X { int a, b; }; |
| 2325 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2326 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2327 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 2328 | // designated initializer re-initializes the whole |
| 2329 | // subobject [0], overwriting previous initializers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2330 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2331 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2332 | << InitRange; |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2333 | SemaRef.Diag(ExistingInit->getLocStart(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2334 | diag::note_previous_initializer) |
Douglas Gregor | e6af7a0 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2335 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2336 | << ExistingInit->getSourceRange(); |
| 2337 | } |
| 2338 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2339 | InitListExpr *Result |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2340 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
Dmitri Gribenko | 78852e9 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 2341 | InitRange.getBegin(), None, |
Ted Kremenek | 013041e | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 2342 | InitRange.getEnd()); |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2343 | |
Eli Friedman | 91f5ae5 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 2344 | QualType ResultType = CurrentObjectType; |
| 2345 | if (!ResultType->isArrayType()) |
| 2346 | ResultType = ResultType.getNonLValueExprType(SemaRef.Context); |
| 2347 | Result->setType(ResultType); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2348 | |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2349 | // Pre-allocate storage for the structured initializer list. |
| 2350 | unsigned NumElements = 0; |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2351 | unsigned NumInits = 0; |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2352 | bool GotNumInits = false; |
| 2353 | if (!StructuredList) { |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2354 | NumInits = IList->getNumInits(); |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2355 | GotNumInits = true; |
| 2356 | } else if (Index < IList->getNumInits()) { |
| 2357 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) { |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2358 | NumInits = SubList->getNumInits(); |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2359 | GotNumInits = true; |
| 2360 | } |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2361 | } |
| 2362 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2363 | if (const ArrayType *AType |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2364 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 2365 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 2366 | NumElements = CAType->getSize().getZExtValue(); |
| 2367 | // Simple heuristic so that we don't allocate a very large |
| 2368 | // initializer with many empty entries at the end. |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2369 | if (GotNumInits && NumElements > NumInits) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2370 | NumElements = 0; |
| 2371 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2372 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2373 | NumElements = VType->getNumElements(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2374 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2375 | RecordDecl *RDecl = RType->getDecl(); |
| 2376 | if (RDecl->isUnion()) |
| 2377 | NumElements = 1; |
| 2378 | else |
Aaron Ballman | 62e47c4 | 2014-03-10 13:43:55 +0000 | [diff] [blame] | 2379 | NumElements = std::distance(RDecl->field_begin(), RDecl->field_end()); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2380 | } |
| 2381 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2382 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2383 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2384 | // Link this new initializer list into the structured initializer |
| 2385 | // lists. |
| 2386 | if (StructuredList) |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2387 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2388 | else { |
| 2389 | Result->setSyntacticForm(IList); |
| 2390 | SyntacticToSemantic[IList] = Result; |
| 2391 | } |
| 2392 | |
| 2393 | return Result; |
| 2394 | } |
| 2395 | |
| 2396 | /// Update the initializer at index @p StructuredIndex within the |
| 2397 | /// structured initializer list to the value @p expr. |
| 2398 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 2399 | unsigned &StructuredIndex, |
| 2400 | Expr *expr) { |
| 2401 | // No structured initializer list to update |
| 2402 | if (!StructuredList) |
| 2403 | return; |
| 2404 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2405 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 2406 | StructuredIndex, expr)) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2407 | // This initializer overwrites a previous initializer. Warn. |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2408 | SemaRef.Diag(expr->getLocStart(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2409 | diag::warn_initializer_overrides) |
| 2410 | << expr->getSourceRange(); |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2411 | SemaRef.Diag(PrevInit->getLocStart(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2412 | diag::note_previous_initializer) |
Douglas Gregor | e6af7a0 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2413 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2414 | << PrevInit->getSourceRange(); |
| 2415 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2416 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2417 | ++StructuredIndex; |
| 2418 | } |
| 2419 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2420 | /// Check that the given Index expression is a valid array designator |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2421 | /// value. This is essentially just a wrapper around |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2422 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2423 | /// and produces a reasonable diagnostic if there is a |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2424 | /// failure. Returns the index expression, possibly with an implicit cast |
| 2425 | /// added, on success. If everything went okay, Value will receive the |
| 2426 | /// value of the constant expression. |
| 2427 | static ExprResult |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2428 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2429 | SourceLocation Loc = Index->getLocStart(); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2430 | |
| 2431 | // Make sure this is an integer constant expression. |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2432 | ExprResult Result = S.VerifyIntegerConstantExpression(Index, &Value); |
| 2433 | if (Result.isInvalid()) |
| 2434 | return Result; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2435 | |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2436 | if (Value.isSigned() && Value.isNegative()) |
| 2437 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2438 | << Value.toString(10) << Index->getSourceRange(); |
| 2439 | |
Douglas Gregor | 51650d3 | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 2440 | Value.setIsUnsigned(true); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2441 | return Result; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2442 | } |
| 2443 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2444 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 2445 | SourceLocation Loc, |
| 2446 | bool GNUSyntax, |
| 2447 | ExprResult Init) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2448 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 2449 | |
| 2450 | bool Invalid = false; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2451 | SmallVector<ASTDesignator, 32> Designators; |
| 2452 | SmallVector<Expr *, 32> InitExpressions; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2453 | |
| 2454 | // Build designators and check array designator expressions. |
| 2455 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 2456 | const Designator &D = Desig.getDesignator(Idx); |
| 2457 | switch (D.getKind()) { |
| 2458 | case Designator::FieldDesignator: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2459 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2460 | D.getFieldLoc())); |
| 2461 | break; |
| 2462 | |
| 2463 | case Designator::ArrayDesignator: { |
| 2464 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 2465 | llvm::APSInt IndexValue; |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2466 | if (!Index->isTypeDependent() && !Index->isValueDependent()) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2467 | Index = CheckArrayDesignatorExpr(*this, Index, IndexValue).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2468 | if (!Index) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2469 | Invalid = true; |
| 2470 | else { |
| 2471 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2472 | D.getLBracketLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2473 | D.getRBracketLoc())); |
| 2474 | InitExpressions.push_back(Index); |
| 2475 | } |
| 2476 | break; |
| 2477 | } |
| 2478 | |
| 2479 | case Designator::ArrayRangeDesignator: { |
| 2480 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 2481 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 2482 | llvm::APSInt StartValue; |
| 2483 | llvm::APSInt EndValue; |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2484 | bool StartDependent = StartIndex->isTypeDependent() || |
| 2485 | StartIndex->isValueDependent(); |
| 2486 | bool EndDependent = EndIndex->isTypeDependent() || |
| 2487 | EndIndex->isValueDependent(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2488 | if (!StartDependent) |
| 2489 | StartIndex = |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2490 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2491 | if (!EndDependent) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2492 | EndIndex = CheckArrayDesignatorExpr(*this, EndIndex, EndValue).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2493 | |
| 2494 | if (!StartIndex || !EndIndex) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2495 | Invalid = true; |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2496 | else { |
| 2497 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2498 | if (StartDependent || EndDependent) { |
| 2499 | // Nothing to compute. |
| 2500 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2501 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2502 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2503 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2504 | |
Douglas Gregor | 0f9d400 | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 2505 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2506 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2507 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2508 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 2509 | Invalid = true; |
| 2510 | } else { |
| 2511 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2512 | D.getLBracketLoc(), |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2513 | D.getEllipsisLoc(), |
| 2514 | D.getRBracketLoc())); |
| 2515 | InitExpressions.push_back(StartIndex); |
| 2516 | InitExpressions.push_back(EndIndex); |
| 2517 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2518 | } |
| 2519 | break; |
| 2520 | } |
| 2521 | } |
| 2522 | } |
| 2523 | |
| 2524 | if (Invalid || Init.isInvalid()) |
| 2525 | return ExprError(); |
| 2526 | |
| 2527 | // Clear out the expressions within the designation. |
| 2528 | Desig.ClearExprs(*this); |
| 2529 | |
| 2530 | DesignatedInitExpr *DIE |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2531 | = DesignatedInitExpr::Create(Context, |
| 2532 | Designators.data(), Designators.size(), |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2533 | InitExpressions, Loc, GNUSyntax, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2534 | Init.getAs<Expr>()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2535 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2536 | if (!getLangOpts().C99) |
Douglas Gregor | c124e59 | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 2537 | Diag(DIE->getLocStart(), diag::ext_designated_init) |
| 2538 | << DIE->getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2539 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2540 | return DIE; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2541 | } |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 2542 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2543 | //===----------------------------------------------------------------------===// |
| 2544 | // Initialization entity |
| 2545 | //===----------------------------------------------------------------------===// |
| 2546 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2547 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2548 | const InitializedEntity &Parent) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2549 | : Parent(&Parent), Index(Index) |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2550 | { |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2551 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 2552 | Kind = EK_ArrayElement; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2553 | Type = AT->getElementType(); |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2554 | } else if (const VectorType *VT = Parent.getType()->getAs<VectorType>()) { |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2555 | Kind = EK_VectorElement; |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2556 | Type = VT->getElementType(); |
| 2557 | } else { |
| 2558 | const ComplexType *CT = Parent.getType()->getAs<ComplexType>(); |
| 2559 | assert(CT && "Unexpected type"); |
| 2560 | Kind = EK_ComplexElement; |
| 2561 | Type = CT->getElementType(); |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2562 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2563 | } |
| 2564 | |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2565 | InitializedEntity |
| 2566 | InitializedEntity::InitializeBase(ASTContext &Context, |
| 2567 | const CXXBaseSpecifier *Base, |
| 2568 | bool IsInheritedVirtualBase) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2569 | InitializedEntity Result; |
| 2570 | Result.Kind = EK_Base; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2571 | Result.Parent = nullptr; |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2572 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 2573 | if (IsInheritedVirtualBase) |
| 2574 | Result.Base |= 0x01; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2575 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2576 | Result.Type = Base->getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2577 | return Result; |
| 2578 | } |
| 2579 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2580 | DeclarationName InitializedEntity::getName() const { |
| 2581 | switch (getKind()) { |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2582 | case EK_Parameter: |
| 2583 | case EK_Parameter_CF_Audited: { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2584 | ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2585 | return (D ? D->getDeclName() : DeclarationName()); |
| 2586 | } |
Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 2587 | |
| 2588 | case EK_Variable: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2589 | case EK_Member: |
| 2590 | return VariableOrMember->getDeclName(); |
| 2591 | |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2592 | case EK_LambdaCapture: |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 2593 | return DeclarationName(Capture.VarID); |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2594 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2595 | case EK_Result: |
| 2596 | case EK_Exception: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2597 | case EK_New: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2598 | case EK_Temporary: |
| 2599 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2600 | case EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2601 | case EK_ArrayElement: |
| 2602 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2603 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2604 | case EK_BlockElement: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 2605 | case EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 2606 | case EK_RelatedResult: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2607 | return DeclarationName(); |
| 2608 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2609 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2610 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2611 | } |
| 2612 | |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2613 | DeclaratorDecl *InitializedEntity::getDecl() const { |
| 2614 | switch (getKind()) { |
| 2615 | case EK_Variable: |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2616 | case EK_Member: |
| 2617 | return VariableOrMember; |
| 2618 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2619 | case EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2620 | case EK_Parameter_CF_Audited: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2621 | return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2622 | |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2623 | case EK_Result: |
| 2624 | case EK_Exception: |
| 2625 | case EK_New: |
| 2626 | case EK_Temporary: |
| 2627 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2628 | case EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2629 | case EK_ArrayElement: |
| 2630 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2631 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2632 | case EK_BlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2633 | case EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 2634 | case EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 2635 | case EK_RelatedResult: |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2636 | return nullptr; |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2637 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2638 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2639 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2640 | } |
| 2641 | |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2642 | bool InitializedEntity::allowsNRVO() const { |
| 2643 | switch (getKind()) { |
| 2644 | case EK_Result: |
| 2645 | case EK_Exception: |
| 2646 | return LocAndNRVO.NRVO; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2647 | |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2648 | case EK_Variable: |
| 2649 | case EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2650 | case EK_Parameter_CF_Audited: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2651 | case EK_Member: |
| 2652 | case EK_New: |
| 2653 | case EK_Temporary: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 2654 | case EK_CompoundLiteralInit: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2655 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2656 | case EK_Delegating: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2657 | case EK_ArrayElement: |
| 2658 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2659 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2660 | case EK_BlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2661 | case EK_LambdaCapture: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 2662 | case EK_RelatedResult: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2663 | break; |
| 2664 | } |
| 2665 | |
| 2666 | return false; |
| 2667 | } |
| 2668 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2669 | unsigned InitializedEntity::dumpImpl(raw_ostream &OS) const { |
Richard Smith | e3b28bc | 2013-06-12 21:51:50 +0000 | [diff] [blame] | 2670 | assert(getParent() != this); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2671 | unsigned Depth = getParent() ? getParent()->dumpImpl(OS) : 0; |
| 2672 | for (unsigned I = 0; I != Depth; ++I) |
| 2673 | OS << "`-"; |
| 2674 | |
| 2675 | switch (getKind()) { |
| 2676 | case EK_Variable: OS << "Variable"; break; |
| 2677 | case EK_Parameter: OS << "Parameter"; break; |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2678 | case EK_Parameter_CF_Audited: OS << "CF audited function Parameter"; |
| 2679 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2680 | case EK_Result: OS << "Result"; break; |
| 2681 | case EK_Exception: OS << "Exception"; break; |
| 2682 | case EK_Member: OS << "Member"; break; |
| 2683 | case EK_New: OS << "New"; break; |
| 2684 | case EK_Temporary: OS << "Temporary"; break; |
| 2685 | case EK_CompoundLiteralInit: OS << "CompoundLiteral";break; |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 2686 | case EK_RelatedResult: OS << "RelatedResult"; break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2687 | case EK_Base: OS << "Base"; break; |
| 2688 | case EK_Delegating: OS << "Delegating"; break; |
| 2689 | case EK_ArrayElement: OS << "ArrayElement " << Index; break; |
| 2690 | case EK_VectorElement: OS << "VectorElement " << Index; break; |
| 2691 | case EK_ComplexElement: OS << "ComplexElement " << Index; break; |
| 2692 | case EK_BlockElement: OS << "Block"; break; |
| 2693 | case EK_LambdaCapture: |
| 2694 | OS << "LambdaCapture "; |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 2695 | OS << DeclarationName(Capture.VarID); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2696 | break; |
| 2697 | } |
| 2698 | |
| 2699 | if (Decl *D = getDecl()) { |
| 2700 | OS << " "; |
| 2701 | cast<NamedDecl>(D)->printQualifiedName(OS); |
| 2702 | } |
| 2703 | |
| 2704 | OS << " '" << getType().getAsString() << "'\n"; |
| 2705 | |
| 2706 | return Depth + 1; |
| 2707 | } |
| 2708 | |
| 2709 | void InitializedEntity::dump() const { |
| 2710 | dumpImpl(llvm::errs()); |
| 2711 | } |
| 2712 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2713 | //===----------------------------------------------------------------------===// |
| 2714 | // Initialization sequence |
| 2715 | //===----------------------------------------------------------------------===// |
| 2716 | |
| 2717 | void InitializationSequence::Step::Destroy() { |
| 2718 | switch (Kind) { |
| 2719 | case SK_ResolveAddressOfOverloadedFunction: |
| 2720 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2721 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2722 | case SK_CastDerivedToBaseLValue: |
| 2723 | case SK_BindReference: |
| 2724 | case SK_BindReferenceToTemporary: |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2725 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2726 | case SK_UserConversion: |
| 2727 | case SK_QualificationConversionRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2728 | case SK_QualificationConversionXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2729 | case SK_QualificationConversionLValue: |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 2730 | case SK_AtomicConversion: |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 2731 | case SK_LValueToRValue: |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2732 | case SK_ListInitialization: |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2733 | case SK_UnwrapInitList: |
| 2734 | case SK_RewrapInitList: |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2735 | case SK_ConstructorInitialization: |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 2736 | case SK_ConstructorInitializationFromList: |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2737 | case SK_ZeroInitialization: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2738 | case SK_CAssignment: |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2739 | case SK_StringInit: |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2740 | case SK_ObjCObjectConversion: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2741 | case SK_ArrayInit: |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 2742 | case SK_ParenthesizedArrayInit: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2743 | case SK_PassByIndirectCopyRestore: |
| 2744 | case SK_PassByIndirectRestore: |
| 2745 | case SK_ProduceObjCObject: |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2746 | case SK_StdInitializerList: |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 2747 | case SK_StdInitializerListConstructorCall: |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 2748 | case SK_OCLSamplerInit: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 2749 | case SK_OCLZeroEvent: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2750 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2751 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2752 | case SK_ConversionSequence: |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 2753 | case SK_ConversionSequenceNoNarrowing: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2754 | delete ICS; |
| 2755 | } |
| 2756 | } |
| 2757 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2758 | bool InitializationSequence::isDirectReferenceBinding() const { |
Sebastian Redl | 112aa82 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 2759 | return !Steps.empty() && Steps.back().Kind == SK_BindReference; |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2760 | } |
| 2761 | |
| 2762 | bool InitializationSequence::isAmbiguous() const { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 2763 | if (!Failed()) |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2764 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2765 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2766 | switch (getFailureKind()) { |
| 2767 | case FK_TooManyInitsForReference: |
| 2768 | case FK_ArrayNeedsInitList: |
| 2769 | case FK_ArrayNeedsInitListOrStringLiteral: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 2770 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 2771 | case FK_NarrowStringIntoWideCharArray: |
| 2772 | case FK_WideStringIntoCharArray: |
| 2773 | case FK_IncompatWideStringIntoWideChar: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2774 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 2775 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 2776 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 2777 | case FK_RValueReferenceBindingToLValue: |
| 2778 | case FK_ReferenceInitDropsQualifiers: |
| 2779 | case FK_ReferenceInitFailed: |
| 2780 | case FK_ConversionFailed: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2781 | case FK_ConversionFromPropertyFailed: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2782 | case FK_TooManyInitsForScalar: |
| 2783 | case FK_ReferenceBindingToInitList: |
| 2784 | case FK_InitListBadDestinationType: |
| 2785 | case FK_DefaultInitOfConst: |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2786 | case FK_Incomplete: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2787 | case FK_ArrayTypeMismatch: |
| 2788 | case FK_NonConstantArrayInit: |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2789 | case FK_ListInitializationFailed: |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 2790 | case FK_VariableLengthArrayHasInitializer: |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2791 | case FK_PlaceholderType: |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 2792 | case FK_ExplicitConstructor: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2793 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2794 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2795 | case FK_ReferenceInitOverloadFailed: |
| 2796 | case FK_UserConversionOverloadFailed: |
| 2797 | case FK_ConstructorOverloadFailed: |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2798 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2799 | return FailedOverloadResult == OR_Ambiguous; |
| 2800 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2801 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2802 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2803 | } |
| 2804 | |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 2805 | bool InitializationSequence::isConstructorInitialization() const { |
| 2806 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 2807 | } |
| 2808 | |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2809 | void |
| 2810 | InitializationSequence |
| 2811 | ::AddAddressOverloadResolutionStep(FunctionDecl *Function, |
| 2812 | DeclAccessPair Found, |
| 2813 | bool HadMultipleCandidates) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2814 | Step S; |
| 2815 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 2816 | S.Type = Function->getType(); |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2817 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2818 | S.Function.Function = Function; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2819 | S.Function.FoundDecl = Found; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2820 | Steps.push_back(S); |
| 2821 | } |
| 2822 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2823 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2824 | ExprValueKind VK) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2825 | Step S; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2826 | switch (VK) { |
| 2827 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 2828 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 2829 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2830 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2831 | S.Type = BaseType; |
| 2832 | Steps.push_back(S); |
| 2833 | } |
| 2834 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2835 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2836 | bool BindingTemporary) { |
| 2837 | Step S; |
| 2838 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 2839 | S.Type = T; |
| 2840 | Steps.push_back(S); |
| 2841 | } |
| 2842 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2843 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 2844 | Step S; |
| 2845 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 2846 | S.Type = T; |
| 2847 | Steps.push_back(S); |
| 2848 | } |
| 2849 | |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2850 | void |
| 2851 | InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
| 2852 | DeclAccessPair FoundDecl, |
| 2853 | QualType T, |
| 2854 | bool HadMultipleCandidates) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2855 | Step S; |
| 2856 | S.Kind = SK_UserConversion; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2857 | S.Type = T; |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2858 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2859 | S.Function.Function = Function; |
| 2860 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2861 | Steps.push_back(S); |
| 2862 | } |
| 2863 | |
| 2864 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2865 | ExprValueKind VK) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2866 | Step S; |
John McCall | 7a1da89 | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 2867 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2868 | switch (VK) { |
| 2869 | case VK_RValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2870 | S.Kind = SK_QualificationConversionRValue; |
| 2871 | break; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2872 | case VK_XValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2873 | S.Kind = SK_QualificationConversionXValue; |
| 2874 | break; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2875 | case VK_LValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2876 | S.Kind = SK_QualificationConversionLValue; |
| 2877 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2878 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2879 | S.Type = Ty; |
| 2880 | Steps.push_back(S); |
| 2881 | } |
| 2882 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 2883 | void InitializationSequence::AddAtomicConversionStep(QualType Ty) { |
| 2884 | Step S; |
| 2885 | S.Kind = SK_AtomicConversion; |
| 2886 | S.Type = Ty; |
| 2887 | Steps.push_back(S); |
| 2888 | } |
| 2889 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 2890 | void InitializationSequence::AddLValueToRValueStep(QualType Ty) { |
| 2891 | assert(!Ty.hasQualifiers() && "rvalues may not have qualifiers"); |
| 2892 | |
| 2893 | Step S; |
| 2894 | S.Kind = SK_LValueToRValue; |
| 2895 | S.Type = Ty; |
| 2896 | Steps.push_back(S); |
| 2897 | } |
| 2898 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2899 | void InitializationSequence::AddConversionSequenceStep( |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 2900 | const ImplicitConversionSequence &ICS, QualType T, |
| 2901 | bool TopLevelOfInitList) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2902 | Step S; |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 2903 | S.Kind = TopLevelOfInitList ? SK_ConversionSequenceNoNarrowing |
| 2904 | : SK_ConversionSequence; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2905 | S.Type = T; |
| 2906 | S.ICS = new ImplicitConversionSequence(ICS); |
| 2907 | Steps.push_back(S); |
| 2908 | } |
| 2909 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2910 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 2911 | Step S; |
| 2912 | S.Kind = SK_ListInitialization; |
| 2913 | S.Type = T; |
| 2914 | Steps.push_back(S); |
| 2915 | } |
| 2916 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2917 | void |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2918 | InitializationSequence |
| 2919 | ::AddConstructorInitializationStep(CXXConstructorDecl *Constructor, |
| 2920 | AccessSpecifier Access, |
| 2921 | QualType T, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2922 | bool HadMultipleCandidates, |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2923 | bool FromInitList, bool AsInitList) { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2924 | Step S; |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 2925 | S.Kind = FromInitList ? AsInitList ? SK_StdInitializerListConstructorCall |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 2926 | : SK_ConstructorInitializationFromList |
| 2927 | : SK_ConstructorInitialization; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2928 | S.Type = T; |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2929 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2930 | S.Function.Function = Constructor; |
| 2931 | S.Function.FoundDecl = DeclAccessPair::make(Constructor, Access); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2932 | Steps.push_back(S); |
| 2933 | } |
| 2934 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2935 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 2936 | Step S; |
| 2937 | S.Kind = SK_ZeroInitialization; |
| 2938 | S.Type = T; |
| 2939 | Steps.push_back(S); |
| 2940 | } |
| 2941 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2942 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 2943 | Step S; |
| 2944 | S.Kind = SK_CAssignment; |
| 2945 | S.Type = T; |
| 2946 | Steps.push_back(S); |
| 2947 | } |
| 2948 | |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2949 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 2950 | Step S; |
| 2951 | S.Kind = SK_StringInit; |
| 2952 | S.Type = T; |
| 2953 | Steps.push_back(S); |
| 2954 | } |
| 2955 | |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2956 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 2957 | Step S; |
| 2958 | S.Kind = SK_ObjCObjectConversion; |
| 2959 | S.Type = T; |
| 2960 | Steps.push_back(S); |
| 2961 | } |
| 2962 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2963 | void InitializationSequence::AddArrayInitStep(QualType T) { |
| 2964 | Step S; |
| 2965 | S.Kind = SK_ArrayInit; |
| 2966 | S.Type = T; |
| 2967 | Steps.push_back(S); |
| 2968 | } |
| 2969 | |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 2970 | void InitializationSequence::AddParenthesizedArrayInitStep(QualType T) { |
| 2971 | Step S; |
| 2972 | S.Kind = SK_ParenthesizedArrayInit; |
| 2973 | S.Type = T; |
| 2974 | Steps.push_back(S); |
| 2975 | } |
| 2976 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2977 | void InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type, |
| 2978 | bool shouldCopy) { |
| 2979 | Step s; |
| 2980 | s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore |
| 2981 | : SK_PassByIndirectRestore); |
| 2982 | s.Type = type; |
| 2983 | Steps.push_back(s); |
| 2984 | } |
| 2985 | |
| 2986 | void InitializationSequence::AddProduceObjCObjectStep(QualType T) { |
| 2987 | Step S; |
| 2988 | S.Kind = SK_ProduceObjCObject; |
| 2989 | S.Type = T; |
| 2990 | Steps.push_back(S); |
| 2991 | } |
| 2992 | |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2993 | void InitializationSequence::AddStdInitializerListConstructionStep(QualType T) { |
| 2994 | Step S; |
| 2995 | S.Kind = SK_StdInitializerList; |
| 2996 | S.Type = T; |
| 2997 | Steps.push_back(S); |
| 2998 | } |
| 2999 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 3000 | void InitializationSequence::AddOCLSamplerInitStep(QualType T) { |
| 3001 | Step S; |
| 3002 | S.Kind = SK_OCLSamplerInit; |
| 3003 | S.Type = T; |
| 3004 | Steps.push_back(S); |
| 3005 | } |
| 3006 | |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 3007 | void InitializationSequence::AddOCLZeroEventStep(QualType T) { |
| 3008 | Step S; |
| 3009 | S.Kind = SK_OCLZeroEvent; |
| 3010 | S.Type = T; |
| 3011 | Steps.push_back(S); |
| 3012 | } |
| 3013 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3014 | void InitializationSequence::RewrapReferenceInitList(QualType T, |
| 3015 | InitListExpr *Syntactic) { |
| 3016 | assert(Syntactic->getNumInits() == 1 && |
| 3017 | "Can only rewrap trivial init lists."); |
| 3018 | Step S; |
| 3019 | S.Kind = SK_UnwrapInitList; |
| 3020 | S.Type = Syntactic->getInit(0)->getType(); |
| 3021 | Steps.insert(Steps.begin(), S); |
| 3022 | |
| 3023 | S.Kind = SK_RewrapInitList; |
| 3024 | S.Type = T; |
| 3025 | S.WrappingSyntacticList = Syntactic; |
| 3026 | Steps.push_back(S); |
| 3027 | } |
| 3028 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3029 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3030 | OverloadingResult Result) { |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 3031 | setSequenceKind(FailedSequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3032 | this->Failure = Failure; |
| 3033 | this->FailedOverloadResult = Result; |
| 3034 | } |
| 3035 | |
| 3036 | //===----------------------------------------------------------------------===// |
| 3037 | // Attempt initialization |
| 3038 | //===----------------------------------------------------------------------===// |
| 3039 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3040 | static void MaybeProduceObjCObject(Sema &S, |
| 3041 | InitializationSequence &Sequence, |
| 3042 | const InitializedEntity &Entity) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3043 | if (!S.getLangOpts().ObjCAutoRefCount) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3044 | |
| 3045 | /// When initializing a parameter, produce the value if it's marked |
| 3046 | /// __attribute__((ns_consumed)). |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3047 | if (Entity.isParameterKind()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3048 | if (!Entity.isParameterConsumed()) |
| 3049 | return; |
| 3050 | |
| 3051 | assert(Entity.getType()->isObjCRetainableType() && |
| 3052 | "consuming an object of unretainable type?"); |
| 3053 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 3054 | |
| 3055 | /// When initializing a return value, if the return type is a |
| 3056 | /// retainable type, then returns need to immediately retain the |
| 3057 | /// object. If an autorelease is required, it will be done at the |
| 3058 | /// last instant. |
| 3059 | } else if (Entity.getKind() == InitializedEntity::EK_Result) { |
| 3060 | if (!Entity.getType()->isObjCRetainableType()) |
| 3061 | return; |
| 3062 | |
| 3063 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 3064 | } |
| 3065 | } |
| 3066 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3067 | static void TryListInitialization(Sema &S, |
| 3068 | const InitializedEntity &Entity, |
| 3069 | const InitializationKind &Kind, |
| 3070 | InitListExpr *InitList, |
| 3071 | InitializationSequence &Sequence); |
| 3072 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3073 | /// \brief When initializing from init list via constructor, handle |
| 3074 | /// initialization of an object of type std::initializer_list<T>. |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3075 | /// |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3076 | /// \return true if we have handled initialization of an object of type |
| 3077 | /// std::initializer_list<T>, false otherwise. |
| 3078 | static bool TryInitializerListConstruction(Sema &S, |
| 3079 | InitListExpr *List, |
| 3080 | QualType DestType, |
| 3081 | InitializationSequence &Sequence) { |
| 3082 | QualType E; |
| 3083 | if (!S.isStdInitializerList(DestType, &E)) |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3084 | return false; |
| 3085 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3086 | if (S.RequireCompleteType(List->getExprLoc(), E, 0)) { |
| 3087 | Sequence.setIncompleteTypeFailure(E); |
| 3088 | return true; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3089 | } |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3090 | |
| 3091 | // Try initializing a temporary array from the init list. |
| 3092 | QualType ArrayType = S.Context.getConstantArrayType( |
| 3093 | E.withConst(), llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 3094 | List->getNumInits()), |
| 3095 | clang::ArrayType::Normal, 0); |
| 3096 | InitializedEntity HiddenArray = |
| 3097 | InitializedEntity::InitializeTemporary(ArrayType); |
| 3098 | InitializationKind Kind = |
| 3099 | InitializationKind::CreateDirectList(List->getExprLoc()); |
| 3100 | TryListInitialization(S, HiddenArray, Kind, List, Sequence); |
| 3101 | if (Sequence) |
| 3102 | Sequence.AddStdInitializerListConstructionStep(DestType); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3103 | return true; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3104 | } |
| 3105 | |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3106 | static OverloadingResult |
| 3107 | ResolveConstructorOverload(Sema &S, SourceLocation DeclLoc, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3108 | MultiExprArg Args, |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3109 | OverloadCandidateSet &CandidateSet, |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3110 | ArrayRef<NamedDecl *> Ctors, |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3111 | OverloadCandidateSet::iterator &Best, |
| 3112 | bool CopyInitializing, bool AllowExplicit, |
Sebastian Redl | c7b718e | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 3113 | bool OnlyListConstructors, bool InitListSyntax) { |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3114 | CandidateSet.clear(); |
| 3115 | |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3116 | for (ArrayRef<NamedDecl *>::iterator |
| 3117 | Con = Ctors.begin(), ConEnd = Ctors.end(); Con != ConEnd; ++Con) { |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3118 | NamedDecl *D = *Con; |
| 3119 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 3120 | bool SuppressUserConversions = false; |
| 3121 | |
| 3122 | // Find the constructor (which may be a template). |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3123 | CXXConstructorDecl *Constructor = nullptr; |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3124 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
| 3125 | if (ConstructorTmpl) |
| 3126 | Constructor = cast<CXXConstructorDecl>( |
| 3127 | ConstructorTmpl->getTemplatedDecl()); |
| 3128 | else { |
| 3129 | Constructor = cast<CXXConstructorDecl>(D); |
| 3130 | |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3131 | // C++11 [over.best.ics]p4: |
| 3132 | // However, when considering the argument of a constructor or |
| 3133 | // user-defined conversion function that is a candidate: |
| 3134 | // -- by 13.3.1.3 when invoked for the copying/moving of a temporary |
| 3135 | // in the second step of a class copy-initialization, |
| 3136 | // -- by 13.3.1.7 when passing the initializer list as a single |
| 3137 | // argument or when the initializer list has exactly one elementand |
| 3138 | // a conversion to some class X or reference to (possibly |
| 3139 | // cv-qualified) X is considered for the first parameter of a |
| 3140 | // constructor of X, or |
| 3141 | // -- by 13.3.1.4, 13.3.1.5, or 13.3.1.6 in all cases, |
| 3142 | // only standard conversion sequences and ellipsis conversion sequences |
| 3143 | // are considered. |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3144 | if ((CopyInitializing || (InitListSyntax && Args.size() == 1)) && |
Sebastian Redl | c7b718e | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 3145 | Constructor->isCopyOrMoveConstructor()) |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3146 | SuppressUserConversions = true; |
| 3147 | } |
| 3148 | |
| 3149 | if (!Constructor->isInvalidDecl() && |
| 3150 | (AllowExplicit || !Constructor->isExplicit()) && |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3151 | (!OnlyListConstructors || S.isInitListConstructor(Constructor))) { |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3152 | if (ConstructorTmpl) |
| 3153 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3154 | /*ExplicitArgs*/ nullptr, Args, |
Sebastian Redl | c7b718e | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 3155 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3156 | else { |
| 3157 | // C++ [over.match.copy]p1: |
| 3158 | // - When initializing a temporary to be bound to the first parameter |
| 3159 | // of a constructor that takes a reference to possibly cv-qualified |
| 3160 | // T as its first argument, called with a single argument in the |
| 3161 | // context of direct-initialization, explicit conversion functions |
| 3162 | // are also considered. |
| 3163 | bool AllowExplicitConv = AllowExplicit && !CopyInitializing && |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3164 | Args.size() == 1 && |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3165 | Constructor->isCopyOrMoveConstructor(); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3166 | S.AddOverloadCandidate(Constructor, FoundDecl, Args, CandidateSet, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3167 | SuppressUserConversions, |
| 3168 | /*PartialOverloading=*/false, |
| 3169 | /*AllowExplicit=*/AllowExplicitConv); |
| 3170 | } |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3171 | } |
| 3172 | } |
| 3173 | |
| 3174 | // Perform overload resolution and return the result. |
| 3175 | return CandidateSet.BestViableFunction(S, DeclLoc, Best); |
| 3176 | } |
| 3177 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3178 | /// \brief Attempt initialization by constructor (C++ [dcl.init]), which |
| 3179 | /// enumerates the constructors of the initialized entity and performs overload |
| 3180 | /// resolution to select the best. |
Sebastian Redl | 88e4d49 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3181 | /// If InitListSyntax is true, this is list-initialization of a non-aggregate |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3182 | /// class type. |
| 3183 | static void TryConstructorInitialization(Sema &S, |
| 3184 | const InitializedEntity &Entity, |
| 3185 | const InitializationKind &Kind, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3186 | MultiExprArg Args, QualType DestType, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3187 | InitializationSequence &Sequence, |
Sebastian Redl | 88e4d49 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3188 | bool InitListSyntax = false) { |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3189 | assert((!InitListSyntax || (Args.size() == 1 && isa<InitListExpr>(Args[0]))) && |
Sebastian Redl | 88e4d49 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3190 | "InitListSyntax must come with a single initializer list argument."); |
| 3191 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3192 | // The type we're constructing needs to be complete. |
| 3193 | if (S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 85f3423 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 3194 | Sequence.setIncompleteTypeFailure(DestType); |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3195 | return; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3196 | } |
| 3197 | |
| 3198 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
| 3199 | assert(DestRecordType && "Constructor initialization requires record type"); |
| 3200 | CXXRecordDecl *DestRecordDecl |
| 3201 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
| 3202 | |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3203 | // Build the candidate set directly in the initialization sequence |
| 3204 | // structure, so that it will persist if we fail. |
| 3205 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3206 | |
| 3207 | // Determine whether we are allowed to call explicit constructors or |
| 3208 | // explicit conversion operators. |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3209 | bool AllowExplicit = Kind.AllowExplicit() || InitListSyntax; |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3210 | bool CopyInitialization = Kind.getKind() == InitializationKind::IK_Copy; |
Sebastian Redl | 88e4d49 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3211 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3212 | // - Otherwise, if T is a class type, constructors are considered. The |
| 3213 | // applicable constructors are enumerated, and the best one is chosen |
| 3214 | // through overload resolution. |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3215 | DeclContext::lookup_result R = S.LookupConstructors(DestRecordDecl); |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3216 | // The container holding the constructors can under certain conditions |
| 3217 | // be changed while iterating (e.g. because of deserialization). |
| 3218 | // To be safe we copy the lookup results to a new container. |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3219 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3220 | |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3221 | OverloadingResult Result = OR_No_Viable_Function; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3222 | OverloadCandidateSet::iterator Best; |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3223 | bool AsInitializerList = false; |
| 3224 | |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 3225 | // C++14 DR 1467 [over.match.list]p1: |
| 3226 | // When objects of non-aggregate type T are list-initialized, such that |
| 3227 | // 8.5.4 [dcl.init.list] specifies that overload resolution is performed |
| 3228 | // according to the rules in this section, overload resolution selects |
| 3229 | // the constructor in two phases: |
| 3230 | // |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3231 | // C++11 [over.match.list]p1: |
| 3232 | // When objects of non-aggregate type T are list-initialized, overload |
| 3233 | // resolution selects the constructor in two phases: |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 3234 | // |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3235 | // - Initially, the candidate functions are the initializer-list |
| 3236 | // constructors of the class T and the argument list consists of the |
| 3237 | // initializer list as a single argument. |
| 3238 | if (InitListSyntax) { |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3239 | InitListExpr *ILE = cast<InitListExpr>(Args[0]); |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3240 | AsInitializerList = true; |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3241 | |
| 3242 | // If the initializer list has no elements and T has a default constructor, |
| 3243 | // the first phase is omitted. |
Richard Smith | 2be35f5 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 3244 | if (ILE->getNumInits() != 0 || !DestRecordDecl->hasDefaultConstructor()) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3245 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3246 | CandidateSet, Ctors, Best, |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3247 | CopyInitialization, AllowExplicit, |
| 3248 | /*OnlyListConstructor=*/true, |
| 3249 | InitListSyntax); |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3250 | |
| 3251 | // Time to unwrap the init list. |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3252 | Args = MultiExprArg(ILE->getInits(), ILE->getNumInits()); |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3253 | } |
| 3254 | |
| 3255 | // C++11 [over.match.list]p1: |
| 3256 | // - If no viable initializer-list constructor is found, overload resolution |
| 3257 | // is performed again, where the candidate functions are all the |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3258 | // constructors of the class T and the argument list consists of the |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3259 | // elements of the initializer list. |
| 3260 | if (Result == OR_No_Viable_Function) { |
| 3261 | AsInitializerList = false; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3262 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3263 | CandidateSet, Ctors, Best, |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3264 | CopyInitialization, AllowExplicit, |
Sebastian Redl | c7b718e | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 3265 | /*OnlyListConstructors=*/false, |
| 3266 | InitListSyntax); |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3267 | } |
| 3268 | if (Result) { |
Sebastian Redl | 88e4d49 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3269 | Sequence.SetOverloadFailure(InitListSyntax ? |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3270 | InitializationSequence::FK_ListConstructorOverloadFailed : |
| 3271 | InitializationSequence::FK_ConstructorOverloadFailed, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3272 | Result); |
| 3273 | return; |
| 3274 | } |
| 3275 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3276 | // C++11 [dcl.init]p6: |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3277 | // If a program calls for the default initialization of an object |
| 3278 | // of a const-qualified type T, T shall be a class type with a |
| 3279 | // user-provided default constructor. |
| 3280 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 3281 | Entity.getType().isConstQualified() && |
Aaron Ballman | 899b9c6 | 2012-07-31 22:40:31 +0000 | [diff] [blame] | 3282 | !cast<CXXConstructorDecl>(Best->Function)->isUserProvided()) { |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3283 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 3284 | return; |
| 3285 | } |
| 3286 | |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3287 | // C++11 [over.match.list]p1: |
| 3288 | // In copy-list-initialization, if an explicit constructor is chosen, the |
| 3289 | // initializer is ill-formed. |
| 3290 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 3291 | if (InitListSyntax && !Kind.AllowExplicit() && CtorDecl->isExplicit()) { |
| 3292 | Sequence.SetFailed(InitializationSequence::FK_ExplicitConstructor); |
| 3293 | return; |
| 3294 | } |
| 3295 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3296 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 3297 | // subsumed by the initialization. |
| 3298 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3299 | Sequence.AddConstructorInitializationStep(CtorDecl, |
| 3300 | Best->FoundDecl.getAccess(), |
| 3301 | DestType, HadMultipleCandidates, |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3302 | InitListSyntax, AsInitializerList); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3303 | } |
| 3304 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3305 | static bool |
| 3306 | ResolveOverloadedFunctionForReferenceBinding(Sema &S, |
| 3307 | Expr *Initializer, |
| 3308 | QualType &SourceType, |
| 3309 | QualType &UnqualifiedSourceType, |
| 3310 | QualType UnqualifiedTargetType, |
| 3311 | InitializationSequence &Sequence) { |
| 3312 | if (S.Context.getCanonicalType(UnqualifiedSourceType) == |
| 3313 | S.Context.OverloadTy) { |
| 3314 | DeclAccessPair Found; |
| 3315 | bool HadMultipleCandidates = false; |
| 3316 | if (FunctionDecl *Fn |
| 3317 | = S.ResolveAddressOfOverloadedFunction(Initializer, |
| 3318 | UnqualifiedTargetType, |
| 3319 | false, Found, |
| 3320 | &HadMultipleCandidates)) { |
| 3321 | Sequence.AddAddressOverloadResolutionStep(Fn, Found, |
| 3322 | HadMultipleCandidates); |
| 3323 | SourceType = Fn->getType(); |
| 3324 | UnqualifiedSourceType = SourceType.getUnqualifiedType(); |
| 3325 | } else if (!UnqualifiedTargetType->isRecordType()) { |
| 3326 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3327 | return true; |
| 3328 | } |
| 3329 | } |
| 3330 | return false; |
| 3331 | } |
| 3332 | |
| 3333 | static void TryReferenceInitializationCore(Sema &S, |
| 3334 | const InitializedEntity &Entity, |
| 3335 | const InitializationKind &Kind, |
| 3336 | Expr *Initializer, |
| 3337 | QualType cv1T1, QualType T1, |
| 3338 | Qualifiers T1Quals, |
| 3339 | QualType cv2T2, QualType T2, |
| 3340 | Qualifiers T2Quals, |
| 3341 | InitializationSequence &Sequence); |
| 3342 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3343 | static void TryValueInitialization(Sema &S, |
| 3344 | const InitializedEntity &Entity, |
| 3345 | const InitializationKind &Kind, |
| 3346 | InitializationSequence &Sequence, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3347 | InitListExpr *InitList = nullptr); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3348 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3349 | /// \brief Attempt list initialization of a reference. |
| 3350 | static void TryReferenceListInitialization(Sema &S, |
| 3351 | const InitializedEntity &Entity, |
| 3352 | const InitializationKind &Kind, |
| 3353 | InitListExpr *InitList, |
Richard Smith | faadef7 | 2013-06-08 00:02:08 +0000 | [diff] [blame] | 3354 | InitializationSequence &Sequence) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3355 | // First, catch C++03 where this isn't possible. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3356 | if (!S.getLangOpts().CPlusPlus11) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3357 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 3358 | return; |
| 3359 | } |
| 3360 | |
| 3361 | QualType DestType = Entity.getType(); |
| 3362 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 3363 | Qualifiers T1Quals; |
| 3364 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
| 3365 | |
| 3366 | // Reference initialization via an initializer list works thus: |
| 3367 | // If the initializer list consists of a single element that is |
| 3368 | // reference-related to the referenced type, bind directly to that element |
| 3369 | // (possibly creating temporaries). |
| 3370 | // Otherwise, initialize a temporary with the initializer list and |
| 3371 | // bind to that. |
| 3372 | if (InitList->getNumInits() == 1) { |
| 3373 | Expr *Initializer = InitList->getInit(0); |
| 3374 | QualType cv2T2 = Initializer->getType(); |
| 3375 | Qualifiers T2Quals; |
| 3376 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
| 3377 | |
| 3378 | // If this fails, creating a temporary wouldn't work either. |
| 3379 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 3380 | T1, Sequence)) |
| 3381 | return; |
| 3382 | |
| 3383 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3384 | bool dummy1, dummy2, dummy3; |
| 3385 | Sema::ReferenceCompareResult RefRelationship |
| 3386 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, dummy1, |
| 3387 | dummy2, dummy3); |
| 3388 | if (RefRelationship >= Sema::Ref_Related) { |
| 3389 | // Try to bind the reference here. |
| 3390 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 3391 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 3392 | if (Sequence) |
| 3393 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
| 3394 | return; |
| 3395 | } |
Richard Smith | 03d9393 | 2013-01-15 07:58:29 +0000 | [diff] [blame] | 3396 | |
| 3397 | // Update the initializer if we've resolved an overloaded function. |
| 3398 | if (Sequence.step_begin() != Sequence.step_end()) |
| 3399 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3400 | } |
| 3401 | |
| 3402 | // Not reference-related. Create a temporary and bind to that. |
| 3403 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3404 | |
| 3405 | TryListInitialization(S, TempEntity, Kind, InitList, Sequence); |
| 3406 | if (Sequence) { |
| 3407 | if (DestType->isRValueReferenceType() || |
| 3408 | (T1Quals.hasConst() && !T1Quals.hasVolatile())) |
| 3409 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3410 | else |
| 3411 | Sequence.SetFailed( |
| 3412 | InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
| 3413 | } |
| 3414 | } |
| 3415 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3416 | /// \brief Attempt list initialization (C++0x [dcl.init.list]) |
| 3417 | static void TryListInitialization(Sema &S, |
| 3418 | const InitializedEntity &Entity, |
| 3419 | const InitializationKind &Kind, |
| 3420 | InitListExpr *InitList, |
| 3421 | InitializationSequence &Sequence) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3422 | QualType DestType = Entity.getType(); |
| 3423 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3424 | // C++ doesn't allow scalar initialization with more than one argument. |
| 3425 | // But C99 complex numbers are scalars and it makes sense there. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3426 | if (S.getLangOpts().CPlusPlus && DestType->isScalarType() && |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3427 | !DestType->isAnyComplexType() && InitList->getNumInits() > 1) { |
| 3428 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 3429 | return; |
| 3430 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3431 | if (DestType->isReferenceType()) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3432 | TryReferenceListInitialization(S, Entity, Kind, InitList, Sequence); |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3433 | return; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3434 | } |
Sebastian Redl | 4f28b58 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3435 | |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 3436 | if (DestType->isRecordType() && |
| 3437 | S.RequireCompleteType(InitList->getLocStart(), DestType, 0)) { |
| 3438 | Sequence.setIncompleteTypeFailure(DestType); |
| 3439 | return; |
| 3440 | } |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3441 | |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 3442 | // C++14 DR1467 [dcl.init.list]p3: |
| 3443 | // - If T is a class type and the initializer list has a single element of |
| 3444 | // type cv U, where U is T or a class derived from T, the object is |
| 3445 | // initialized from that element (by copy-initialization for |
| 3446 | // copy-list-initialization, or by direct-initialization for |
| 3447 | // direct-list-initialization). |
| 3448 | // - Otherwise, if T is a character array and the initializer list has a |
| 3449 | // single element that is an appropriately-typed string literal |
| 3450 | // (8.5.2 [dcl.init.string]), initialization is performed as described |
| 3451 | // in that section. |
| 3452 | // - Otherwise, If T is an aggregate, [...] (continue below). |
| 3453 | if (S.getLangOpts().CPlusPlus14 && InitList->getNumInits() == 1) { |
| 3454 | if (DestType->isRecordType()) { |
| 3455 | QualType InitType = InitList->getInit(0)->getType(); |
| 3456 | if (S.Context.hasSameUnqualifiedType(InitType, DestType) || |
| 3457 | S.IsDerivedFrom(InitType, DestType)) { |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3458 | Expr *InitListAsExpr = InitList; |
| 3459 | TryConstructorInitialization(S, Entity, Kind, InitListAsExpr, DestType, |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3460 | Sequence, /*InitListSyntax*/true); |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 3461 | return; |
| 3462 | } |
| 3463 | } |
| 3464 | if (const ArrayType *DestAT = S.Context.getAsArrayType(DestType)) { |
| 3465 | Expr *SubInit[1] = {InitList->getInit(0)}; |
| 3466 | if (!isa<VariableArrayType>(DestAT) && |
| 3467 | IsStringInit(SubInit[0], DestAT, S.Context) == SIF_None) { |
| 3468 | InitializationKind SubKind = |
| 3469 | Kind.getKind() == InitializationKind::IK_DirectList |
| 3470 | ? InitializationKind::CreateDirect(Kind.getLocation(), |
| 3471 | InitList->getLBraceLoc(), |
| 3472 | InitList->getRBraceLoc()) |
| 3473 | : Kind; |
| 3474 | Sequence.InitializeFrom(S, Entity, SubKind, SubInit, |
| 3475 | /*TopLevelOfInitList*/ true); |
| 3476 | |
| 3477 | // TryStringLiteralInitialization() (in InitializeFrom()) will fail if |
| 3478 | // the element is not an appropriately-typed string literal, in which |
| 3479 | // case we should proceed as in C++11 (below). |
| 3480 | if (Sequence) { |
| 3481 | Sequence.RewrapReferenceInitList(Entity.getType(), InitList); |
| 3482 | return; |
| 3483 | } |
| 3484 | } |
Sebastian Redl | 4f28b58 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3485 | } |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3486 | } |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 3487 | |
| 3488 | // C++11 [dcl.init.list]p3: |
| 3489 | // - If T is an aggregate, aggregate initialization is performed. |
| 3490 | if (DestType->isRecordType() && !DestType->isAggregateType()) { |
| 3491 | if (S.getLangOpts().CPlusPlus11) { |
| 3492 | // - Otherwise, if the initializer list has no elements and T is a |
| 3493 | // class type with a default constructor, the object is |
| 3494 | // value-initialized. |
| 3495 | if (InitList->getNumInits() == 0) { |
| 3496 | CXXRecordDecl *RD = DestType->getAsCXXRecordDecl(); |
| 3497 | if (RD->hasDefaultConstructor()) { |
| 3498 | TryValueInitialization(S, Entity, Kind, Sequence, InitList); |
| 3499 | return; |
| 3500 | } |
| 3501 | } |
| 3502 | |
| 3503 | // - Otherwise, if T is a specialization of std::initializer_list<E>, |
| 3504 | // an initializer_list object constructed [...] |
| 3505 | if (TryInitializerListConstruction(S, InitList, DestType, Sequence)) |
| 3506 | return; |
| 3507 | |
| 3508 | // - Otherwise, if T is a class type, constructors are considered. |
| 3509 | Expr *InitListAsExpr = InitList; |
| 3510 | TryConstructorInitialization(S, Entity, Kind, InitListAsExpr, DestType, |
| 3511 | Sequence, /*InitListSyntax*/ true); |
| 3512 | } else |
| 3513 | Sequence.SetFailed(InitializationSequence::FK_InitListBadDestinationType); |
| 3514 | return; |
| 3515 | } |
| 3516 | |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 3517 | if (S.getLangOpts().CPlusPlus && !DestType->isAggregateType() && |
| 3518 | InitList->getNumInits() == 1 && |
| 3519 | InitList->getInit(0)->getType()->isRecordType()) { |
| 3520 | // - Otherwise, if the initializer list has a single element of type E |
| 3521 | // [...references are handled above...], the object or reference is |
| 3522 | // initialized from that element; if a narrowing conversion is required |
| 3523 | // to convert the element to T, the program is ill-formed. |
| 3524 | // |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 3525 | // C++14 DR1467: |
| 3526 | // - Otherwise, if the initializer list has a single element of type E |
| 3527 | // [...references are handled above...], the object or reference is |
| 3528 | // initialized from that element (by copy-initialization for |
| 3529 | // copy-list-initialization, or by direct-initialization for |
| 3530 | // direct-list-initialization); if a narrowing conversion is required |
| 3531 | // to convert the element to T, the program is ill-formed. |
| 3532 | // |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 3533 | // Per core-24034, this is direct-initialization if we were performing |
| 3534 | // direct-list-initialization and copy-initialization otherwise. |
| 3535 | // We can't use InitListChecker for this, because it always performs |
| 3536 | // copy-initialization. This only matters if we might use an 'explicit' |
| 3537 | // conversion operator, so we only need to handle the cases where the source |
| 3538 | // is of record type. |
| 3539 | InitializationKind SubKind = |
| 3540 | Kind.getKind() == InitializationKind::IK_DirectList |
| 3541 | ? InitializationKind::CreateDirect(Kind.getLocation(), |
| 3542 | InitList->getLBraceLoc(), |
| 3543 | InitList->getRBraceLoc()) |
| 3544 | : Kind; |
| 3545 | Expr *SubInit[1] = { InitList->getInit(0) }; |
| 3546 | Sequence.InitializeFrom(S, Entity, SubKind, SubInit, |
| 3547 | /*TopLevelOfInitList*/true); |
| 3548 | if (Sequence) |
| 3549 | Sequence.RewrapReferenceInitList(Entity.getType(), InitList); |
| 3550 | return; |
| 3551 | } |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3552 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3553 | InitListChecker CheckInitList(S, Entity, InitList, |
Richard Smith | de22923 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 3554 | DestType, /*VerifyOnly=*/true); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3555 | if (CheckInitList.HadError()) { |
| 3556 | Sequence.SetFailed(InitializationSequence::FK_ListInitializationFailed); |
| 3557 | return; |
| 3558 | } |
| 3559 | |
| 3560 | // Add the list initialization step with the built init list. |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3561 | Sequence.AddListInitializationStep(DestType); |
| 3562 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3563 | |
| 3564 | /// \brief Try a reference initialization that involves calling a conversion |
| 3565 | /// function. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3566 | static OverloadingResult TryRefInitWithConversionFunction(Sema &S, |
| 3567 | const InitializedEntity &Entity, |
| 3568 | const InitializationKind &Kind, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3569 | Expr *Initializer, |
| 3570 | bool AllowRValues, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3571 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3572 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3573 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 3574 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 3575 | QualType cv2T2 = Initializer->getType(); |
| 3576 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 3577 | |
| 3578 | bool DerivedToBase; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3579 | bool ObjCConversion; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3580 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3581 | assert(!S.CompareReferenceRelationship(Initializer->getLocStart(), |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3582 | T1, T2, DerivedToBase, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3583 | ObjCConversion, |
| 3584 | ObjCLifetimeConversion) && |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3585 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 8abbc65 | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 3586 | (void)DerivedToBase; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3587 | (void)ObjCConversion; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3588 | (void)ObjCLifetimeConversion; |
| 3589 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3590 | // Build the candidate set directly in the initialization sequence |
| 3591 | // structure, so that it will persist if we fail. |
| 3592 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3593 | CandidateSet.clear(); |
| 3594 | |
| 3595 | // Determine whether we are allowed to call explicit constructors or |
| 3596 | // explicit conversion operators. |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3597 | bool AllowExplicit = Kind.AllowExplicit(); |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3598 | bool AllowExplicitConvs = Kind.allowExplicitConversionFunctionsInRefBinding(); |
| 3599 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3600 | const RecordType *T1RecordType = nullptr; |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 3601 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
| 3602 | !S.RequireCompleteType(Kind.getLocation(), T1, 0)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3603 | // The type we're converting to is a class type. Enumerate its constructors |
| 3604 | // to see if there is a suitable conversion. |
| 3605 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3606 | |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3607 | DeclContext::lookup_result R = S.LookupConstructors(T1RecordDecl); |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3608 | // The container holding the constructors can under certain conditions |
| 3609 | // be changed while iterating (e.g. because of deserialization). |
| 3610 | // To be safe we copy the lookup results to a new container. |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3611 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 3612 | for (SmallVectorImpl<NamedDecl *>::iterator |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3613 | CI = Ctors.begin(), CE = Ctors.end(); CI != CE; ++CI) { |
| 3614 | NamedDecl *D = *CI; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3615 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 3616 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3617 | // Find the constructor (which may be a template). |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3618 | CXXConstructorDecl *Constructor = nullptr; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3619 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3620 | if (ConstructorTmpl) |
| 3621 | Constructor = cast<CXXConstructorDecl>( |
| 3622 | ConstructorTmpl->getTemplatedDecl()); |
| 3623 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3624 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3625 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3626 | if (!Constructor->isInvalidDecl() && |
| 3627 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 3628 | if (ConstructorTmpl) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3629 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3630 | /*ExplicitArgs*/ nullptr, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3631 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | dfbdfbb | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 3632 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3633 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3634 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3635 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | dfbdfbb | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 3636 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3637 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3638 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3639 | } |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3640 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 3641 | return OR_No_Viable_Function; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3642 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3643 | const RecordType *T2RecordType = nullptr; |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 3644 | if ((T2RecordType = T2->getAs<RecordType>()) && |
| 3645 | !S.RequireCompleteType(Kind.getLocation(), T2, 0)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3646 | // The type we're converting from is a class type, enumerate its conversion |
| 3647 | // functions. |
| 3648 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 3649 | |
Argyrios Kyrtzidis | a6567c4 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 3650 | std::pair<CXXRecordDecl::conversion_iterator, |
| 3651 | CXXRecordDecl::conversion_iterator> |
| 3652 | Conversions = T2RecordDecl->getVisibleConversionFunctions(); |
| 3653 | for (CXXRecordDecl::conversion_iterator |
| 3654 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3655 | NamedDecl *D = *I; |
| 3656 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3657 | if (isa<UsingShadowDecl>(D)) |
| 3658 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3659 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3660 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3661 | CXXConversionDecl *Conv; |
| 3662 | if (ConvTemplate) |
| 3663 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3664 | else |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3665 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3666 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3667 | // If the conversion function doesn't return a reference type, |
| 3668 | // it can't be considered for this conversion unless we're allowed to |
| 3669 | // consider rvalues. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3670 | // FIXME: Do we need to make sure that we only consider conversion |
| 3671 | // candidates with reference-compatible results? That might be needed to |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3672 | // break recursion. |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3673 | if ((AllowExplicitConvs || !Conv->isExplicit()) && |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3674 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 3675 | if (ConvTemplate) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3676 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3677 | ActingDC, Initializer, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 3678 | DestType, CandidateSet, |
| 3679 | /*AllowObjCConversionOnExplicit=*/ |
| 3680 | false); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3681 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3682 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 3683 | Initializer, DestType, CandidateSet, |
| 3684 | /*AllowObjCConversionOnExplicit=*/false); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3685 | } |
| 3686 | } |
| 3687 | } |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3688 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 3689 | return OR_No_Viable_Function; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3690 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3691 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3692 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3693 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3694 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3695 | if (OverloadingResult Result |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3696 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3697 | return Result; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3698 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3699 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 3700 | // This is the overload that will be used for this initialization step if we |
| 3701 | // use this initialization. Mark it as referenced. |
| 3702 | Function->setReferenced(); |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3703 | |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3704 | // Compute the returned type of the conversion. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3705 | if (isa<CXXConversionDecl>(Function)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3706 | T2 = Function->getReturnType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3707 | else |
| 3708 | T2 = cv1T1; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3709 | |
| 3710 | // Add the user-defined conversion step. |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3711 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3712 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3713 | T2.getNonLValueExprType(S.Context), |
| 3714 | HadMultipleCandidates); |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3715 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3716 | // Determine whether we need to perform derived-to-base or |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3717 | // cv-qualification adjustments. |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3718 | ExprValueKind VK = VK_RValue; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3719 | if (T2->isLValueReferenceType()) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3720 | VK = VK_LValue; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3721 | else if (const RValueReferenceType *RRef = T2->getAs<RValueReferenceType>()) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3722 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3723 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3724 | bool NewDerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3725 | bool NewObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3726 | bool NewObjCLifetimeConversion = false; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3727 | Sema::ReferenceCompareResult NewRefRelationship |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3728 | = S.CompareReferenceRelationship(DeclLoc, T1, |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 3729 | T2.getNonLValueExprType(S.Context), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3730 | NewDerivedToBase, NewObjCConversion, |
| 3731 | NewObjCLifetimeConversion); |
Douglas Gregor | 1ce52ca | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 3732 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
| 3733 | // If the type we've converted to is not reference-related to the |
| 3734 | // type we're looking for, then there is another conversion step |
| 3735 | // we need to perform to produce a temporary of the right type |
| 3736 | // that we'll be binding to. |
| 3737 | ImplicitConversionSequence ICS; |
| 3738 | ICS.setStandard(); |
| 3739 | ICS.Standard = Best->FinalConversion; |
| 3740 | T2 = ICS.Standard.getToType(2); |
| 3741 | Sequence.AddConversionSequenceStep(ICS, T2); |
| 3742 | } else if (NewDerivedToBase) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3743 | Sequence.AddDerivedToBaseCastStep( |
| 3744 | S.Context.getQualifiedType(T1, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3745 | T2.getNonReferenceType().getQualifiers()), |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3746 | VK); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3747 | else if (NewObjCConversion) |
| 3748 | Sequence.AddObjCObjectConversionStep( |
| 3749 | S.Context.getQualifiedType(T1, |
| 3750 | T2.getNonReferenceType().getQualifiers())); |
| 3751 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3752 | if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers()) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3753 | Sequence.AddQualificationConversionStep(cv1T1, VK); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3754 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3755 | Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType()); |
| 3756 | return OR_Success; |
| 3757 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3758 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 3759 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 3760 | const InitializedEntity &Entity, |
| 3761 | Expr *CurInitExpr); |
| 3762 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3763 | /// \brief Attempt reference initialization (C++0x [dcl.init.ref]) |
| 3764 | static void TryReferenceInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3765 | const InitializedEntity &Entity, |
| 3766 | const InitializationKind &Kind, |
| 3767 | Expr *Initializer, |
| 3768 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3769 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3770 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3771 | Qualifiers T1Quals; |
| 3772 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3773 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3774 | Qualifiers T2Quals; |
| 3775 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3776 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3777 | // If the initializer is the address of an overloaded function, try |
| 3778 | // to resolve the overloaded function. If all goes well, T2 is the |
| 3779 | // type of the resulting function. |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3780 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 3781 | T1, Sequence)) |
| 3782 | return; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3783 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3784 | // Delegate everything else to a subfunction. |
| 3785 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 3786 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 3787 | } |
| 3788 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3789 | /// Converts the target of reference initialization so that it has the |
| 3790 | /// appropriate qualifiers and value kind. |
| 3791 | /// |
| 3792 | /// In this case, 'x' is an 'int' lvalue, but it needs to be 'const int'. |
| 3793 | /// \code |
| 3794 | /// int x; |
| 3795 | /// const int &r = x; |
| 3796 | /// \endcode |
| 3797 | /// |
| 3798 | /// In this case the reference is binding to a bitfield lvalue, which isn't |
| 3799 | /// valid. Perform a load to create a lifetime-extended temporary instead. |
| 3800 | /// \code |
| 3801 | /// const int &r = someStruct.bitfield; |
| 3802 | /// \endcode |
| 3803 | static ExprValueKind |
| 3804 | convertQualifiersAndValueKindIfNecessary(Sema &S, |
| 3805 | InitializationSequence &Sequence, |
| 3806 | Expr *Initializer, |
| 3807 | QualType cv1T1, |
| 3808 | Qualifiers T1Quals, |
| 3809 | Qualifiers T2Quals, |
| 3810 | bool IsLValueRef) { |
John McCall | d25db7e | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 3811 | bool IsNonAddressableType = Initializer->refersToBitField() || |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3812 | Initializer->refersToVectorElement(); |
| 3813 | |
| 3814 | if (IsNonAddressableType) { |
| 3815 | // C++11 [dcl.init.ref]p5: [...] Otherwise, the reference shall be an |
| 3816 | // lvalue reference to a non-volatile const type, or the reference shall be |
| 3817 | // an rvalue reference. |
| 3818 | // |
| 3819 | // If not, we can't make a temporary and bind to that. Give up and allow the |
| 3820 | // error to be diagnosed later. |
| 3821 | if (IsLValueRef && (!T1Quals.hasConst() || T1Quals.hasVolatile())) { |
| 3822 | assert(Initializer->isGLValue()); |
| 3823 | return Initializer->getValueKind(); |
| 3824 | } |
| 3825 | |
| 3826 | // Force a load so we can materialize a temporary. |
| 3827 | Sequence.AddLValueToRValueStep(cv1T1.getUnqualifiedType()); |
| 3828 | return VK_RValue; |
| 3829 | } |
| 3830 | |
| 3831 | if (T1Quals != T2Quals) { |
| 3832 | Sequence.AddQualificationConversionStep(cv1T1, |
| 3833 | Initializer->getValueKind()); |
| 3834 | } |
| 3835 | |
| 3836 | return Initializer->getValueKind(); |
| 3837 | } |
| 3838 | |
| 3839 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3840 | /// \brief Reference initialization without resolving overloaded functions. |
| 3841 | static void TryReferenceInitializationCore(Sema &S, |
| 3842 | const InitializedEntity &Entity, |
| 3843 | const InitializationKind &Kind, |
| 3844 | Expr *Initializer, |
| 3845 | QualType cv1T1, QualType T1, |
| 3846 | Qualifiers T1Quals, |
| 3847 | QualType cv2T2, QualType T2, |
| 3848 | Qualifiers T2Quals, |
| 3849 | InitializationSequence &Sequence) { |
| 3850 | QualType DestType = Entity.getType(); |
| 3851 | SourceLocation DeclLoc = Initializer->getLocStart(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3852 | // Compute some basic properties of the types and the initializer. |
| 3853 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 3854 | bool isRValueRef = !isLValueRef; |
| 3855 | bool DerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3856 | bool ObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3857 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3858 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3859 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3860 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3861 | ObjCConversion, ObjCLifetimeConversion); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3862 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3863 | // C++0x [dcl.init.ref]p5: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3864 | // 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] | 3865 | // "cv2 T2" as follows: |
| 3866 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3867 | // - If the reference is an lvalue reference and the initializer |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3868 | // expression |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3869 | // Note the analogous bullet points for rvalue refs to functions. Because |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3870 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 3871 | // like lvalue refs. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3872 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3873 | bool T1Function = T1->isFunctionType(); |
| 3874 | if (isLValueRef || T1Function) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3875 | if (InitCategory.isLValue() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3876 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3877 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3878 | RefRelationship == Sema::Ref_Related))) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3879 | // - 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] | 3880 | // reference-compatible with "cv2 T2," or |
| 3881 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3882 | // Per C++ [over.best.ics]p2, we don't diagnose whether the lvalue is a |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3883 | // bit-field when we're determining whether the reference initialization |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3884 | // can occur. However, we do pay attention to whether it is a bit-field |
| 3885 | // to decide whether we're actually binding to a temporary created from |
| 3886 | // the bit-field. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3887 | if (DerivedToBase) |
| 3888 | Sequence.AddDerivedToBaseCastStep( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3889 | S.Context.getQualifiedType(T1, T2Quals), |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3890 | VK_LValue); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3891 | else if (ObjCConversion) |
| 3892 | Sequence.AddObjCObjectConversionStep( |
| 3893 | S.Context.getQualifiedType(T1, T2Quals)); |
| 3894 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3895 | ExprValueKind ValueKind = |
| 3896 | convertQualifiersAndValueKindIfNecessary(S, Sequence, Initializer, |
| 3897 | cv1T1, T1Quals, T2Quals, |
| 3898 | isLValueRef); |
| 3899 | Sequence.AddReferenceBindingStep(cv1T1, ValueKind == VK_RValue); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3900 | return; |
| 3901 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3902 | |
| 3903 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3904 | // reference-related to T2, and can be implicitly converted to an |
| 3905 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 3906 | // with "cv3 T3" (this conversion is selected by enumerating the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3907 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 3908 | // one through overload resolution (13.3)), |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3909 | // 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] | 3910 | // an rvalue. DR1287 removed the "implicitly" here. |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3911 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 3912 | (isLValueRef || InitCategory.isRValue())) { |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3913 | ConvOvlResult = TryRefInitWithConversionFunction( |
| 3914 | S, Entity, Kind, Initializer, /*AllowRValues*/isRValueRef, Sequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3915 | if (ConvOvlResult == OR_Success) |
| 3916 | return; |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3917 | if (ConvOvlResult != OR_No_Viable_Function) |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3918 | Sequence.SetOverloadFailure( |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3919 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3920 | ConvOvlResult); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3921 | } |
| 3922 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3923 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3924 | // - Otherwise, the reference shall be an lvalue reference to a |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3925 | // 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] | 3926 | // shall be an rvalue reference. |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3927 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) { |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3928 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3929 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3930 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3931 | Sequence.SetOverloadFailure( |
| 3932 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3933 | ConvOvlResult); |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3934 | else |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3935 | Sequence.SetFailed(InitCategory.isLValue() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3936 | ? (RefRelationship == Sema::Ref_Related |
| 3937 | ? InitializationSequence::FK_ReferenceInitDropsQualifiers |
| 3938 | : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated) |
| 3939 | : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3940 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3941 | return; |
| 3942 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3943 | |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3944 | // - If the initializer expression |
| 3945 | // - is an xvalue, class prvalue, array prvalue, or function lvalue and |
| 3946 | // "cv1 T1" is reference-compatible with "cv2 T2" |
| 3947 | // Note: functions are handled below. |
| 3948 | if (!T1Function && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3949 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3950 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3951 | RefRelationship == Sema::Ref_Related)) && |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3952 | (InitCategory.isXValue() || |
| 3953 | (InitCategory.isPRValue() && T2->isRecordType()) || |
| 3954 | (InitCategory.isPRValue() && T2->isArrayType()))) { |
| 3955 | ExprValueKind ValueKind = InitCategory.isXValue()? VK_XValue : VK_RValue; |
| 3956 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3957 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 3958 | // compiler the freedom to perform a copy here or bind to the |
| 3959 | // object, while C++0x requires that we bind directly to the |
| 3960 | // object. Hence, we always bind to the object without making an |
| 3961 | // extra copy. However, in C++03 requires that we check for the |
| 3962 | // presence of a suitable copy constructor: |
| 3963 | // |
| 3964 | // The constructor that would be used to make the copy shall |
| 3965 | // be callable whether or not the copy is actually done. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3966 | if (!S.getLangOpts().CPlusPlus11 && !S.getLangOpts().MicrosoftExt) |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3967 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3968 | else if (S.getLangOpts().CPlusPlus11) |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 3969 | CheckCXX98CompatAccessibleCopy(S, Entity, Initializer); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3970 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3971 | |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3972 | if (DerivedToBase) |
| 3973 | Sequence.AddDerivedToBaseCastStep(S.Context.getQualifiedType(T1, T2Quals), |
| 3974 | ValueKind); |
| 3975 | else if (ObjCConversion) |
| 3976 | Sequence.AddObjCObjectConversionStep( |
| 3977 | S.Context.getQualifiedType(T1, T2Quals)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3978 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3979 | ValueKind = convertQualifiersAndValueKindIfNecessary(S, Sequence, |
| 3980 | Initializer, cv1T1, |
| 3981 | T1Quals, T2Quals, |
| 3982 | isLValueRef); |
| 3983 | |
| 3984 | Sequence.AddReferenceBindingStep(cv1T1, ValueKind == VK_RValue); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3985 | return; |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3986 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3987 | |
| 3988 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3989 | // reference-related to T2, and can be implicitly converted to an |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3990 | // xvalue, class prvalue, or function lvalue of type "cv3 T3", |
| 3991 | // where "cv1 T1" is reference-compatible with "cv3 T3", |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3992 | // |
| 3993 | // DR1287 removes the "implicitly" here. |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3994 | if (T2->isRecordType()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3995 | if (RefRelationship == Sema::Ref_Incompatible) { |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3996 | ConvOvlResult = TryRefInitWithConversionFunction( |
| 3997 | S, Entity, Kind, Initializer, /*AllowRValues*/true, Sequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3998 | if (ConvOvlResult) |
| 3999 | Sequence.SetOverloadFailure( |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 4000 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4001 | ConvOvlResult); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4002 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4003 | return; |
| 4004 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4005 | |
Douglas Gregor | 6fa6ab0 | 2013-03-26 23:59:23 +0000 | [diff] [blame] | 4006 | if ((RefRelationship == Sema::Ref_Compatible || |
| 4007 | RefRelationship == Sema::Ref_Compatible_With_Added_Qualification) && |
| 4008 | isRValueRef && InitCategory.isLValue()) { |
| 4009 | Sequence.SetFailed( |
| 4010 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 4011 | return; |
| 4012 | } |
| 4013 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4014 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 4015 | return; |
| 4016 | } |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4017 | |
| 4018 | // - Otherwise, a temporary of type "cv1 T1" is created and initialized |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4019 | // from the initializer expression using the rules for a non-reference |
Richard Smith | 2eabf78 | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 4020 | // copy-initialization (8.5). The reference is then bound to the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4021 | // temporary. [...] |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 4022 | |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 4023 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 4024 | |
Richard Smith | 2eabf78 | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 4025 | // FIXME: Why do we use an implicit conversion here rather than trying |
| 4026 | // copy-initialization? |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4027 | ImplicitConversionSequence ICS |
| 4028 | = S.TryImplicitConversion(Initializer, TempEntity.getType(), |
Richard Smith | 2eabf78 | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 4029 | /*SuppressUserConversions=*/false, |
| 4030 | /*AllowExplicit=*/false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4031 | /*FIXME:InOverloadResolution=*/false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4032 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 4033 | /*AllowObjCWritebackConversion=*/false); |
| 4034 | |
| 4035 | if (ICS.isBad()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4036 | // FIXME: Use the conversion function set stored in ICS to turn |
| 4037 | // this into an overloading ambiguity diagnostic. However, we need |
| 4038 | // to keep that set as an OverloadCandidateSet rather than as some |
| 4039 | // other kind of set. |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4040 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 4041 | Sequence.SetOverloadFailure( |
| 4042 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4043 | ConvOvlResult); |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 4044 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 4045 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4046 | else |
| 4047 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4048 | return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4049 | } else { |
| 4050 | Sequence.AddConversionSequenceStep(ICS, TempEntity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4051 | } |
| 4052 | |
| 4053 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 4054 | // same cv-qualification as, or greater cv-qualification |
| 4055 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 4056 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 4057 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4058 | if (RefRelationship == Sema::Ref_Related && |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 4059 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4060 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 4061 | return; |
| 4062 | } |
| 4063 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4064 | // [...] 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] | 4065 | // reference, the initializer expression shall not be an lvalue. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4066 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 4067 | InitCategory.isLValue()) { |
| 4068 | Sequence.SetFailed( |
| 4069 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 4070 | return; |
| 4071 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4072 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4073 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 4074 | return; |
| 4075 | } |
| 4076 | |
| 4077 | /// \brief Attempt character array initialization from a string literal |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4078 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 4079 | static void TryStringLiteralInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4080 | const InitializedEntity &Entity, |
| 4081 | const InitializationKind &Kind, |
| 4082 | Expr *Initializer, |
| 4083 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4084 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4085 | } |
| 4086 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4087 | /// \brief Attempt value initialization (C++ [dcl.init]p7). |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4088 | static void TryValueInitialization(Sema &S, |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4089 | const InitializedEntity &Entity, |
| 4090 | const InitializationKind &Kind, |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4091 | InitializationSequence &Sequence, |
| 4092 | InitListExpr *InitList) { |
| 4093 | assert((!InitList || InitList->getNumInits() == 0) && |
| 4094 | "Shouldn't use value-init for non-empty init lists"); |
| 4095 | |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4096 | // C++98 [dcl.init]p5, C++11 [dcl.init]p7: |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4097 | // |
| 4098 | // To value-initialize an object of type T means: |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4099 | QualType T = Entity.getType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4100 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4101 | // -- if T is an array type, then each element is value-initialized; |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4102 | T = S.Context.getBaseElementType(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4103 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4104 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 4105 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4106 | bool NeedZeroInitialization = true; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4107 | if (!S.getLangOpts().CPlusPlus11) { |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4108 | // C++98: |
| 4109 | // -- if T is a class type (clause 9) with a user-declared constructor |
| 4110 | // (12.1), then the default constructor for T is called (and the |
| 4111 | // initialization is ill-formed if T has no accessible default |
| 4112 | // constructor); |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4113 | if (ClassDecl->hasUserDeclaredConstructor()) |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4114 | NeedZeroInitialization = false; |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4115 | } else { |
| 4116 | // C++11: |
| 4117 | // -- if T is a class type (clause 9) with either no default constructor |
| 4118 | // (12.1 [class.ctor]) or a default constructor that is user-provided |
| 4119 | // or deleted, then the object is default-initialized; |
| 4120 | CXXConstructorDecl *CD = S.LookupDefaultConstructor(ClassDecl); |
| 4121 | if (!CD || !CD->getCanonicalDecl()->isDefaulted() || CD->isDeleted()) |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4122 | NeedZeroInitialization = false; |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4123 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4124 | |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4125 | // -- if T is a (possibly cv-qualified) non-union class type without a |
| 4126 | // user-provided or deleted default constructor, then the object is |
| 4127 | // zero-initialized and, if T has a non-trivial default constructor, |
| 4128 | // default-initialized; |
Richard Smith | fb26652 | 2012-10-18 00:44:17 +0000 | [diff] [blame] | 4129 | // The 'non-union' here was removed by DR1502. The 'non-trivial default |
| 4130 | // constructor' part was removed by DR1507. |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4131 | if (NeedZeroInitialization) |
| 4132 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 4133 | |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 4134 | // C++03: |
| 4135 | // -- if T is a non-union class type without a user-declared constructor, |
| 4136 | // then every non-static data member and base class component of T is |
| 4137 | // value-initialized; |
| 4138 | // [...] A program that calls for [...] value-initialization of an |
| 4139 | // entity of reference type is ill-formed. |
| 4140 | // |
| 4141 | // C++11 doesn't need this handling, because value-initialization does not |
| 4142 | // occur recursively there, and the implicit default constructor is |
| 4143 | // defined as deleted in the problematic cases. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4144 | if (!S.getLangOpts().CPlusPlus11 && |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 4145 | ClassDecl->hasUninitializedReferenceMember()) { |
| 4146 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForReference); |
| 4147 | return; |
| 4148 | } |
| 4149 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4150 | // If this is list-value-initialization, pass the empty init list on when |
| 4151 | // building the constructor call. This affects the semantics of a few |
| 4152 | // things (such as whether an explicit default constructor can be called). |
| 4153 | Expr *InitListAsExpr = InitList; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4154 | MultiExprArg Args(&InitListAsExpr, InitList ? 1 : 0); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4155 | bool InitListSyntax = InitList; |
| 4156 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4157 | return TryConstructorInitialization(S, Entity, Kind, Args, T, Sequence, |
| 4158 | InitListSyntax); |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4159 | } |
| 4160 | } |
| 4161 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4162 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4163 | } |
| 4164 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4165 | /// \brief Attempt default initialization (C++ [dcl.init]p6). |
| 4166 | static void TryDefaultInitialization(Sema &S, |
| 4167 | const InitializedEntity &Entity, |
| 4168 | const InitializationKind &Kind, |
| 4169 | InitializationSequence &Sequence) { |
| 4170 | assert(Kind.getKind() == InitializationKind::IK_Default); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4171 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4172 | // C++ [dcl.init]p6: |
| 4173 | // To default-initialize an object of type T means: |
| 4174 | // - if T is an array type, each element is default-initialized; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4175 | QualType DestType = S.Context.getBaseElementType(Entity.getType()); |
| 4176 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4177 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 4178 | // constructor for T is called (and the initialization is ill-formed if |
| 4179 | // T has no accessible default constructor); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4180 | if (DestType->isRecordType() && S.getLangOpts().CPlusPlus) { |
Dmitri Gribenko | 78852e9 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 4181 | TryConstructorInitialization(S, Entity, Kind, None, DestType, Sequence); |
Chandler Carruth | c926240 | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 4182 | return; |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4183 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4184 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4185 | // - otherwise, no initialization is performed. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4186 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4187 | // If a program calls for the default initialization of an object of |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4188 | // 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] | 4189 | // default constructor. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4190 | if (DestType.isConstQualified() && S.getLangOpts().CPlusPlus) { |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4191 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4192 | return; |
| 4193 | } |
| 4194 | |
| 4195 | // If the destination type has a lifetime property, zero-initialize it. |
| 4196 | if (DestType.getQualifiers().hasObjCLifetime()) { |
| 4197 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 4198 | return; |
| 4199 | } |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4200 | } |
| 4201 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4202 | /// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]), |
| 4203 | /// which enumerates all conversion functions and performs overload resolution |
| 4204 | /// to select the best. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4205 | static void TryUserDefinedConversion(Sema &S, |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 4206 | QualType DestType, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4207 | const InitializationKind &Kind, |
| 4208 | Expr *Initializer, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4209 | InitializationSequence &Sequence, |
| 4210 | bool TopLevelOfInitList) { |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4211 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 4212 | QualType SourceType = Initializer->getType(); |
| 4213 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 4214 | "Must have a class type to perform a user-defined conversion"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4215 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4216 | // Build the candidate set directly in the initialization sequence |
| 4217 | // structure, so that it will persist if we fail. |
| 4218 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 4219 | CandidateSet.clear(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4220 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4221 | // Determine whether we are allowed to call explicit constructors or |
| 4222 | // explicit conversion operators. |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 4223 | bool AllowExplicit = Kind.AllowExplicit(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4224 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4225 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 4226 | // The type we're converting to is a class type. Enumerate its constructors |
| 4227 | // to see if there is a suitable conversion. |
| 4228 | CXXRecordDecl *DestRecordDecl |
| 4229 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4230 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4231 | // Try to complete the type we're converting to. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4232 | if (!S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4233 | DeclContext::lookup_result R = S.LookupConstructors(DestRecordDecl); |
David Blaikie | 12be639 | 2012-10-18 16:57:32 +0000 | [diff] [blame] | 4234 | // The container holding the constructors can under certain conditions |
| 4235 | // be changed while iterating. To be safe we copy the lookup results |
| 4236 | // to a new container. |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4237 | SmallVector<NamedDecl*, 8> CopyOfCon(R.begin(), R.end()); |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 4238 | for (SmallVectorImpl<NamedDecl *>::iterator |
David Blaikie | 12be639 | 2012-10-18 16:57:32 +0000 | [diff] [blame] | 4239 | Con = CopyOfCon.begin(), ConEnd = CopyOfCon.end(); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4240 | Con != ConEnd; ++Con) { |
| 4241 | NamedDecl *D = *Con; |
| 4242 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4243 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4244 | // Find the constructor (which may be a template). |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4245 | CXXConstructorDecl *Constructor = nullptr; |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4246 | FunctionTemplateDecl *ConstructorTmpl |
| 4247 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4248 | if (ConstructorTmpl) |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4249 | Constructor = cast<CXXConstructorDecl>( |
| 4250 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 4251 | else |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4252 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4253 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4254 | if (!Constructor->isInvalidDecl() && |
| 4255 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 4256 | if (ConstructorTmpl) |
| 4257 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4258 | /*ExplicitArgs*/ nullptr, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4259 | Initializer, CandidateSet, |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 4260 | /*SuppressUserConversions=*/true); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4261 | else |
| 4262 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4263 | Initializer, CandidateSet, |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 4264 | /*SuppressUserConversions=*/true); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4265 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4266 | } |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4267 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4268 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4269 | |
| 4270 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 4271 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4272 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 4273 | // The type we're converting from is a class type, enumerate its conversion |
| 4274 | // functions. |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4275 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4276 | // We can only enumerate the conversion functions for a complete type; if |
| 4277 | // the type isn't complete, simply skip this step. |
| 4278 | if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) { |
| 4279 | CXXRecordDecl *SourceRecordDecl |
| 4280 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4281 | |
Argyrios Kyrtzidis | a6567c4 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 4282 | std::pair<CXXRecordDecl::conversion_iterator, |
| 4283 | CXXRecordDecl::conversion_iterator> |
| 4284 | Conversions = SourceRecordDecl->getVisibleConversionFunctions(); |
| 4285 | for (CXXRecordDecl::conversion_iterator |
| 4286 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4287 | NamedDecl *D = *I; |
| 4288 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 4289 | if (isa<UsingShadowDecl>(D)) |
| 4290 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4291 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4292 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 4293 | CXXConversionDecl *Conv; |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4294 | if (ConvTemplate) |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4295 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4296 | else |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4297 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4298 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4299 | if (AllowExplicit || !Conv->isExplicit()) { |
| 4300 | if (ConvTemplate) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4301 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4302 | ActingDC, Initializer, DestType, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 4303 | CandidateSet, AllowExplicit); |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4304 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4305 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 4306 | Initializer, DestType, CandidateSet, |
| 4307 | AllowExplicit); |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4308 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4309 | } |
| 4310 | } |
| 4311 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4312 | |
| 4313 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4314 | OverloadCandidateSet::iterator Best; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4315 | if (OverloadingResult Result |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4316 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4317 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4318 | InitializationSequence::FK_UserConversionOverloadFailed, |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4319 | Result); |
| 4320 | return; |
| 4321 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4322 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4323 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 4324 | Function->setReferenced(); |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4325 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4326 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4327 | if (isa<CXXConstructorDecl>(Function)) { |
| 4328 | // Add the user-defined conversion step. Any cv-qualification conversion is |
Richard Smith | b24f067 | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 4329 | // subsumed by the initialization. Per DR5, the created temporary is of the |
| 4330 | // cv-unqualified type of the destination. |
| 4331 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
| 4332 | DestType.getUnqualifiedType(), |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4333 | HadMultipleCandidates); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4334 | return; |
| 4335 | } |
| 4336 | |
| 4337 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 4338 | QualType ConvType = Function->getCallResultType(); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4339 | if (ConvType->getAs<RecordType>()) { |
Richard Smith | b24f067 | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 4340 | // If we're converting to a class type, there may be an copy of |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4341 | // the resulting temporary object (possible to create an object of |
| 4342 | // a base class type). That copy is not a separate conversion, so |
| 4343 | // we just make a note of the actual destination type (possibly a |
| 4344 | // base class of the type returned by the conversion function) and |
| 4345 | // let the user-defined conversion step handle the conversion. |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4346 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType, |
| 4347 | HadMultipleCandidates); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4348 | return; |
| 4349 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4350 | |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4351 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType, |
| 4352 | HadMultipleCandidates); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4353 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4354 | // If the conversion following the call to the conversion function |
| 4355 | // is interesting, add it as a separate step. |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4356 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 4357 | Best->FinalConversion.Third) { |
| 4358 | ImplicitConversionSequence ICS; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4359 | ICS.setStandard(); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4360 | ICS.Standard = Best->FinalConversion; |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4361 | Sequence.AddConversionSequenceStep(ICS, DestType, TopLevelOfInitList); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4362 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4363 | } |
| 4364 | |
Richard Smith | f032001b | 2013-06-20 02:18:31 +0000 | [diff] [blame] | 4365 | /// An egregious hack for compatibility with libstdc++-4.2: in <tr1/hashtable>, |
| 4366 | /// a function with a pointer return type contains a 'return false;' statement. |
| 4367 | /// In C++11, 'false' is not a null pointer, so this breaks the build of any |
| 4368 | /// code using that header. |
| 4369 | /// |
| 4370 | /// Work around this by treating 'return false;' as zero-initializing the result |
| 4371 | /// if it's used in a pointer-returning function in a system header. |
| 4372 | static bool isLibstdcxxPointerReturnFalseHack(Sema &S, |
| 4373 | const InitializedEntity &Entity, |
| 4374 | const Expr *Init) { |
| 4375 | return S.getLangOpts().CPlusPlus11 && |
| 4376 | Entity.getKind() == InitializedEntity::EK_Result && |
| 4377 | Entity.getType()->isPointerType() && |
| 4378 | isa<CXXBoolLiteralExpr>(Init) && |
| 4379 | !cast<CXXBoolLiteralExpr>(Init)->getValue() && |
| 4380 | S.getSourceManager().isInSystemHeader(Init->getExprLoc()); |
| 4381 | } |
| 4382 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4383 | /// The non-zero enum values here are indexes into diagnostic alternatives. |
| 4384 | enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar }; |
| 4385 | |
| 4386 | /// Determines whether this expression is an acceptable ICR source. |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4387 | static InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e, |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4388 | bool isAddressOf, bool &isWeakAccess) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4389 | // Skip parens. |
| 4390 | e = e->IgnoreParens(); |
| 4391 | |
| 4392 | // Skip address-of nodes. |
| 4393 | if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 4394 | if (op->getOpcode() == UO_AddrOf) |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4395 | return isInvalidICRSource(C, op->getSubExpr(), /*addressof*/ true, |
| 4396 | isWeakAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4397 | |
| 4398 | // Skip certain casts. |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4399 | } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 4400 | switch (ce->getCastKind()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4401 | case CK_Dependent: |
| 4402 | case CK_BitCast: |
| 4403 | case CK_LValueBitCast: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4404 | case CK_NoOp: |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4405 | return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf, isWeakAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4406 | |
| 4407 | case CK_ArrayToPointerDecay: |
| 4408 | return IIK_nonscalar; |
| 4409 | |
| 4410 | case CK_NullToPointer: |
| 4411 | return IIK_okay; |
| 4412 | |
| 4413 | default: |
| 4414 | break; |
| 4415 | } |
| 4416 | |
| 4417 | // 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] | 4418 | } else if (isa<DeclRefExpr>(e)) { |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4419 | // set isWeakAccess to true, to mean that there will be an implicit |
| 4420 | // load which requires a cleanup. |
| 4421 | if (e->getType().getObjCLifetime() == Qualifiers::OCL_Weak) |
| 4422 | isWeakAccess = true; |
| 4423 | |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4424 | if (!isAddressOf) return IIK_nonlocal; |
| 4425 | |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4426 | VarDecl *var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl()); |
| 4427 | if (!var) return IIK_nonlocal; |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4428 | |
| 4429 | return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4430 | |
| 4431 | // If we have a conditional operator, check both sides. |
| 4432 | } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) { |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4433 | if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf, |
| 4434 | isWeakAccess)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4435 | return iik; |
| 4436 | |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4437 | return isInvalidICRSource(C, cond->getRHS(), isAddressOf, isWeakAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4438 | |
| 4439 | // These are never scalar. |
| 4440 | } else if (isa<ArraySubscriptExpr>(e)) { |
| 4441 | return IIK_nonscalar; |
| 4442 | |
| 4443 | // Otherwise, it needs to be a null pointer constant. |
| 4444 | } else { |
| 4445 | return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull) |
| 4446 | ? IIK_okay : IIK_nonlocal); |
| 4447 | } |
| 4448 | |
| 4449 | return IIK_nonlocal; |
| 4450 | } |
| 4451 | |
| 4452 | /// Check whether the given expression is a valid operand for an |
| 4453 | /// indirect copy/restore. |
| 4454 | static void checkIndirectCopyRestoreSource(Sema &S, Expr *src) { |
| 4455 | assert(src->isRValue()); |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4456 | bool isWeakAccess = false; |
| 4457 | InvalidICRKind iik = isInvalidICRSource(S.Context, src, false, isWeakAccess); |
| 4458 | // If isWeakAccess to true, there will be an implicit |
| 4459 | // load which requires a cleanup. |
| 4460 | if (S.getLangOpts().ObjCAutoRefCount && isWeakAccess) |
| 4461 | S.ExprNeedsCleanups = true; |
| 4462 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4463 | if (iik == IIK_okay) return; |
| 4464 | |
| 4465 | S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback) |
| 4466 | << ((unsigned) iik - 1) // shift index into diagnostic explanations |
| 4467 | << src->getSourceRange(); |
| 4468 | } |
| 4469 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4470 | /// \brief Determine whether we have compatible array types for the |
| 4471 | /// purposes of GNU by-copy array initialization. |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4472 | static bool hasCompatibleArrayTypes(ASTContext &Context, const ArrayType *Dest, |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4473 | const ArrayType *Source) { |
| 4474 | // If the source and destination array types are equivalent, we're |
| 4475 | // done. |
| 4476 | if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0))) |
| 4477 | return true; |
| 4478 | |
| 4479 | // Make sure that the element types are the same. |
| 4480 | if (!Context.hasSameType(Dest->getElementType(), Source->getElementType())) |
| 4481 | return false; |
| 4482 | |
| 4483 | // The only mismatch we allow is when the destination is an |
| 4484 | // incomplete array type and the source is a constant array type. |
| 4485 | return Source->isConstantArrayType() && Dest->isIncompleteArrayType(); |
| 4486 | } |
| 4487 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4488 | static bool tryObjCWritebackConversion(Sema &S, |
| 4489 | InitializationSequence &Sequence, |
| 4490 | const InitializedEntity &Entity, |
| 4491 | Expr *Initializer) { |
| 4492 | bool ArrayDecay = false; |
| 4493 | QualType ArgType = Initializer->getType(); |
| 4494 | QualType ArgPointee; |
| 4495 | if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) { |
| 4496 | ArrayDecay = true; |
| 4497 | ArgPointee = ArgArrayType->getElementType(); |
| 4498 | ArgType = S.Context.getPointerType(ArgPointee); |
| 4499 | } |
| 4500 | |
| 4501 | // Handle write-back conversion. |
| 4502 | QualType ConvertedArgType; |
| 4503 | if (!S.isObjCWritebackConversion(ArgType, Entity.getType(), |
| 4504 | ConvertedArgType)) |
| 4505 | return false; |
| 4506 | |
| 4507 | // We should copy unless we're passing to an argument explicitly |
| 4508 | // marked 'out'. |
| 4509 | bool ShouldCopy = true; |
| 4510 | if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 4511 | ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 4512 | |
| 4513 | // Do we need an lvalue conversion? |
| 4514 | if (ArrayDecay || Initializer->isGLValue()) { |
| 4515 | ImplicitConversionSequence ICS; |
| 4516 | ICS.setStandard(); |
| 4517 | ICS.Standard.setAsIdentityConversion(); |
| 4518 | |
| 4519 | QualType ResultType; |
| 4520 | if (ArrayDecay) { |
| 4521 | ICS.Standard.First = ICK_Array_To_Pointer; |
| 4522 | ResultType = S.Context.getPointerType(ArgPointee); |
| 4523 | } else { |
| 4524 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
| 4525 | ResultType = Initializer->getType().getNonLValueExprType(S.Context); |
| 4526 | } |
| 4527 | |
| 4528 | Sequence.AddConversionSequenceStep(ICS, ResultType); |
| 4529 | } |
| 4530 | |
| 4531 | Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
| 4532 | return true; |
| 4533 | } |
| 4534 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4535 | static bool TryOCLSamplerInitialization(Sema &S, |
| 4536 | InitializationSequence &Sequence, |
| 4537 | QualType DestType, |
| 4538 | Expr *Initializer) { |
| 4539 | if (!S.getLangOpts().OpenCL || !DestType->isSamplerT() || |
| 4540 | !Initializer->isIntegerConstantExpr(S.getASTContext())) |
| 4541 | return false; |
| 4542 | |
| 4543 | Sequence.AddOCLSamplerInitStep(DestType); |
| 4544 | return true; |
| 4545 | } |
| 4546 | |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4547 | // |
| 4548 | // OpenCL 1.2 spec, s6.12.10 |
| 4549 | // |
| 4550 | // The event argument can also be used to associate the |
| 4551 | // async_work_group_copy with a previous async copy allowing |
| 4552 | // an event to be shared by multiple async copies; otherwise |
| 4553 | // event should be zero. |
| 4554 | // |
| 4555 | static bool TryOCLZeroEventInitialization(Sema &S, |
| 4556 | InitializationSequence &Sequence, |
| 4557 | QualType DestType, |
| 4558 | Expr *Initializer) { |
| 4559 | if (!S.getLangOpts().OpenCL || !DestType->isEventT() || |
| 4560 | !Initializer->isIntegerConstantExpr(S.getASTContext()) || |
| 4561 | (Initializer->EvaluateKnownConstInt(S.getASTContext()) != 0)) |
| 4562 | return false; |
| 4563 | |
| 4564 | Sequence.AddOCLZeroEventStep(DestType); |
| 4565 | return true; |
| 4566 | } |
| 4567 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4568 | InitializationSequence::InitializationSequence(Sema &S, |
| 4569 | const InitializedEntity &Entity, |
| 4570 | const InitializationKind &Kind, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4571 | MultiExprArg Args, |
| 4572 | bool TopLevelOfInitList) |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 4573 | : FailedCandidateSet(Kind.getLocation(), OverloadCandidateSet::CSK_Normal) { |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 4574 | InitializeFrom(S, Entity, Kind, Args, TopLevelOfInitList); |
| 4575 | } |
| 4576 | |
| 4577 | void InitializationSequence::InitializeFrom(Sema &S, |
| 4578 | const InitializedEntity &Entity, |
| 4579 | const InitializationKind &Kind, |
| 4580 | MultiExprArg Args, |
| 4581 | bool TopLevelOfInitList) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4582 | ASTContext &Context = S.Context; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4583 | |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 4584 | // Eliminate non-overload placeholder types in the arguments. We |
| 4585 | // need to do this before checking whether types are dependent |
| 4586 | // because lowering a pseudo-object expression might well give us |
| 4587 | // something of dependent type. |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4588 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 4589 | if (Args[I]->getType()->isNonOverloadPlaceholderType()) { |
| 4590 | // FIXME: should we be doing this here? |
| 4591 | ExprResult result = S.CheckPlaceholderExpr(Args[I]); |
| 4592 | if (result.isInvalid()) { |
| 4593 | SetFailed(FK_PlaceholderType); |
| 4594 | return; |
| 4595 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4596 | Args[I] = result.get(); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 4597 | } |
| 4598 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4599 | // C++0x [dcl.init]p16: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4600 | // The semantics of initializers are as follows. The destination type is |
| 4601 | // the type of the object or reference being initialized and the source |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4602 | // 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] | 4603 | // 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] | 4604 | // parenthesized list of expressions. |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4605 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4606 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4607 | if (DestType->isDependentType() || |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4608 | Expr::hasAnyTypeDependentArguments(Args)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4609 | SequenceKind = DependentSequence; |
| 4610 | return; |
| 4611 | } |
| 4612 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4613 | // Almost everything is a normal sequence. |
| 4614 | setSequenceKind(NormalSequence); |
| 4615 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4616 | QualType SourceType; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4617 | Expr *Initializer = nullptr; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4618 | if (Args.size() == 1) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4619 | Initializer = Args[0]; |
Fariborz Jahanian | 283bf89 | 2013-12-18 21:04:43 +0000 | [diff] [blame] | 4620 | if (S.getLangOpts().ObjC1) { |
| 4621 | if (S.CheckObjCBridgeRelatedConversions(Initializer->getLocStart(), |
| 4622 | DestType, Initializer->getType(), |
| 4623 | Initializer) || |
| 4624 | S.ConversionToObjCStringLiteralCheck(DestType, Initializer)) |
| 4625 | Args[0] = Initializer; |
Fariborz Jahanian | 283bf89 | 2013-12-18 21:04:43 +0000 | [diff] [blame] | 4626 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4627 | if (!isa<InitListExpr>(Initializer)) |
| 4628 | SourceType = Initializer->getType(); |
| 4629 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4630 | |
Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 4631 | // - If the initializer is a (non-parenthesized) braced-init-list, the |
| 4632 | // object is list-initialized (8.5.4). |
| 4633 | if (Kind.getKind() != InitializationKind::IK_Direct) { |
| 4634 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
| 4635 | TryListInitialization(S, Entity, Kind, InitList, *this); |
| 4636 | return; |
| 4637 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4638 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4639 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4640 | // - If the destination type is a reference type, see 8.5.3. |
| 4641 | if (DestType->isReferenceType()) { |
| 4642 | // C++0x [dcl.init.ref]p1: |
| 4643 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 4644 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 4645 | // by an object that can be converted into a T. |
| 4646 | // (Therefore, multiple arguments are not permitted.) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4647 | if (Args.size() != 1) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4648 | SetFailed(FK_TooManyInitsForReference); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4649 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4650 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4651 | return; |
| 4652 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4653 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4654 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4655 | if (Kind.getKind() == InitializationKind::IK_Value || |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4656 | (Kind.getKind() == InitializationKind::IK_Direct && Args.empty())) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4657 | TryValueInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4658 | return; |
| 4659 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4660 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4661 | // Handle default initialization. |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 4662 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4663 | TryDefaultInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4664 | return; |
| 4665 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4666 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 4667 | // - If the destination type is an array of characters, an array of |
| 4668 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 4669 | // initializer is a string literal, see 8.5.2. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4670 | // - Otherwise, if the destination type is an array, the program is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4671 | // ill-formed. |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4672 | if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) { |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 4673 | if (Initializer && isa<VariableArrayType>(DestAT)) { |
| 4674 | SetFailed(FK_VariableLengthArrayHasInitializer); |
| 4675 | return; |
| 4676 | } |
| 4677 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 4678 | if (Initializer) { |
| 4679 | switch (IsStringInit(Initializer, DestAT, Context)) { |
| 4680 | case SIF_None: |
| 4681 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
| 4682 | return; |
| 4683 | case SIF_NarrowStringIntoWideChar: |
| 4684 | SetFailed(FK_NarrowStringIntoWideCharArray); |
| 4685 | return; |
| 4686 | case SIF_WideStringIntoChar: |
| 4687 | SetFailed(FK_WideStringIntoCharArray); |
| 4688 | return; |
| 4689 | case SIF_IncompatWideStringIntoWideChar: |
| 4690 | SetFailed(FK_IncompatWideStringIntoWideChar); |
| 4691 | return; |
| 4692 | case SIF_Other: |
| 4693 | break; |
| 4694 | } |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 4695 | } |
| 4696 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4697 | // Note: as an GNU C extension, we allow initialization of an |
| 4698 | // array from a compound literal that creates an array of the same |
| 4699 | // type, so long as the initializer has no side effects. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4700 | if (!S.getLangOpts().CPlusPlus && Initializer && |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4701 | isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) && |
| 4702 | Initializer->getType()->isArrayType()) { |
| 4703 | const ArrayType *SourceAT |
| 4704 | = Context.getAsArrayType(Initializer->getType()); |
| 4705 | if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4706 | SetFailed(FK_ArrayTypeMismatch); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4707 | else if (Initializer->HasSideEffects(S.Context)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4708 | SetFailed(FK_NonConstantArrayInit); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4709 | else { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4710 | AddArrayInitStep(DestType); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4711 | } |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 4712 | } |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4713 | // Note: as a GNU C++ extension, we allow list-initialization of a |
| 4714 | // class member of array type from a parenthesized initializer list. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4715 | else if (S.getLangOpts().CPlusPlus && |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 4716 | Entity.getKind() == InitializedEntity::EK_Member && |
| 4717 | Initializer && isa<InitListExpr>(Initializer)) { |
| 4718 | TryListInitialization(S, Entity, Kind, cast<InitListExpr>(Initializer), |
| 4719 | *this); |
| 4720 | AddParenthesizedArrayInitStep(DestType); |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 4721 | } else if (DestAT->getElementType()->isCharType()) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4722 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 4723 | else if (IsWideCharCompatible(DestAT->getElementType(), Context)) |
| 4724 | SetFailed(FK_ArrayNeedsInitListOrWideStringLiteral); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4725 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4726 | SetFailed(FK_ArrayNeedsInitList); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4727 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4728 | return; |
| 4729 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4730 | |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4731 | // Determine whether we should consider writeback conversions for |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4732 | // Objective-C ARC. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4733 | bool allowObjCWritebackConversion = S.getLangOpts().ObjCAutoRefCount && |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 4734 | Entity.isParameterKind(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4735 | |
| 4736 | // We're at the end of the line for C: it's either a write-back conversion |
| 4737 | // 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] | 4738 | if (!S.getLangOpts().CPlusPlus) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4739 | // If allowed, check whether this is an Objective-C writeback conversion. |
| 4740 | if (allowObjCWritebackConversion && |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4741 | tryObjCWritebackConversion(S, *this, Entity, Initializer)) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4742 | return; |
| 4743 | } |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4744 | |
| 4745 | if (TryOCLSamplerInitialization(S, *this, DestType, Initializer)) |
| 4746 | return; |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4747 | |
| 4748 | if (TryOCLZeroEventInitialization(S, *this, DestType, Initializer)) |
| 4749 | return; |
| 4750 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4751 | // Handle initialization in C |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4752 | AddCAssignmentStep(DestType); |
| 4753 | MaybeProduceObjCObject(S, *this, Entity); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4754 | return; |
| 4755 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4756 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4757 | assert(S.getLangOpts().CPlusPlus); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4758 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4759 | // - If the destination type is a (possibly cv-qualified) class type: |
| 4760 | if (DestType->isRecordType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4761 | // - If the initialization is direct-initialization, or if it is |
| 4762 | // copy-initialization where the cv-unqualified version of the |
| 4763 | // 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] | 4764 | // class of the destination, constructors are considered. [...] |
| 4765 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 4766 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 4767 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
| 4768 | S.IsDerivedFrom(SourceType, DestType)))) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4769 | TryConstructorInitialization(S, Entity, Kind, Args, |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 4770 | DestType, *this); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4771 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4772 | // user-defined conversion sequences that can convert from the source |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4773 | // type to the destination type or (when a conversion function is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4774 | // used) to a derived class thereof are enumerated as described in |
| 4775 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 4776 | // (13.3). |
| 4777 | else |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 4778 | TryUserDefinedConversion(S, DestType, Kind, Initializer, *this, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4779 | TopLevelOfInitList); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4780 | return; |
| 4781 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4782 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4783 | if (Args.size() > 1) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4784 | SetFailed(FK_TooManyInitsForScalar); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4785 | return; |
| 4786 | } |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4787 | assert(Args.size() == 1 && "Zero-argument case handled above"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4788 | |
| 4789 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4790 | // type, conversion functions are considered. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4791 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 4792 | // For a conversion to _Atomic(T) from either T or a class type derived |
| 4793 | // from T, initialize the T object then convert to _Atomic type. |
| 4794 | bool NeedAtomicConversion = false; |
| 4795 | if (const AtomicType *Atomic = DestType->getAs<AtomicType>()) { |
| 4796 | if (Context.hasSameUnqualifiedType(SourceType, Atomic->getValueType()) || |
| 4797 | S.IsDerivedFrom(SourceType, Atomic->getValueType())) { |
| 4798 | DestType = Atomic->getValueType(); |
| 4799 | NeedAtomicConversion = true; |
| 4800 | } |
| 4801 | } |
| 4802 | |
| 4803 | TryUserDefinedConversion(S, DestType, Kind, Initializer, *this, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4804 | TopLevelOfInitList); |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4805 | MaybeProduceObjCObject(S, *this, Entity); |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 4806 | if (!Failed() && NeedAtomicConversion) |
| 4807 | AddAtomicConversionStep(Entity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4808 | return; |
| 4809 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4810 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4811 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4812 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4813 | // conversions (Clause 4) will be used, if necessary, to convert the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4814 | // initializer expression to the cv-unqualified version of the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4815 | // destination type; no user-defined conversions are considered. |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 4816 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4817 | ImplicitConversionSequence ICS |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 4818 | = S.TryImplicitConversion(Initializer, DestType, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4819 | /*SuppressUserConversions*/true, |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 4820 | /*AllowExplicitConversions*/ false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4821 | /*InOverloadResolution*/ false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4822 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 4823 | allowObjCWritebackConversion); |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 4824 | |
| 4825 | if (ICS.isStandard() && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4826 | ICS.Standard.Second == ICK_Writeback_Conversion) { |
| 4827 | // Objective-C ARC writeback conversion. |
| 4828 | |
| 4829 | // We should copy unless we're passing to an argument explicitly |
| 4830 | // marked 'out'. |
| 4831 | bool ShouldCopy = true; |
| 4832 | if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 4833 | ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 4834 | |
| 4835 | // If there was an lvalue adjustment, add it as a separate conversion. |
| 4836 | if (ICS.Standard.First == ICK_Array_To_Pointer || |
| 4837 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 4838 | ImplicitConversionSequence LvalueICS; |
| 4839 | LvalueICS.setStandard(); |
| 4840 | LvalueICS.Standard.setAsIdentityConversion(); |
| 4841 | LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0)); |
| 4842 | LvalueICS.Standard.First = ICS.Standard.First; |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4843 | AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4844 | } |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4845 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 4846 | AddPassByIndirectCopyRestoreStep(DestType, ShouldCopy); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4847 | } else if (ICS.isBad()) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4848 | DeclAccessPair dap; |
Richard Smith | f032001b | 2013-06-20 02:18:31 +0000 | [diff] [blame] | 4849 | if (isLibstdcxxPointerReturnFalseHack(S, Entity, Initializer)) { |
| 4850 | AddZeroInitializationStep(Entity.getType()); |
| 4851 | } else if (Initializer->getType() == Context.OverloadTy && |
| 4852 | !S.ResolveAddressOfOverloadedFunction(Initializer, DestType, |
| 4853 | false, dap)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4854 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 4855 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4856 | SetFailed(InitializationSequence::FK_ConversionFailed); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4857 | } else { |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 4858 | AddConversionSequenceStep(ICS, DestType, TopLevelOfInitList); |
John McCall | fa27234 | 2011-06-16 23:24:51 +0000 | [diff] [blame] | 4859 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4860 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 4861 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4862 | } |
| 4863 | |
| 4864 | InitializationSequence::~InitializationSequence() { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4865 | for (SmallVectorImpl<Step>::iterator Step = Steps.begin(), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4866 | StepEnd = Steps.end(); |
| 4867 | Step != StepEnd; ++Step) |
| 4868 | Step->Destroy(); |
| 4869 | } |
| 4870 | |
| 4871 | //===----------------------------------------------------------------------===// |
| 4872 | // Perform initialization |
| 4873 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4874 | static Sema::AssignmentAction |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 4875 | getAssignmentAction(const InitializedEntity &Entity, bool Diagnose = false) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4876 | switch(Entity.getKind()) { |
| 4877 | case InitializedEntity::EK_Variable: |
| 4878 | case InitializedEntity::EK_New: |
Douglas Gregor | 6dd3a6a | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 4879 | case InitializedEntity::EK_Exception: |
| 4880 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4881 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4882 | return Sema::AA_Initializing; |
| 4883 | |
| 4884 | case InitializedEntity::EK_Parameter: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4885 | if (Entity.getDecl() && |
Douglas Gregor | 6b7f12c | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 4886 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 4887 | return Sema::AA_Sending; |
| 4888 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4889 | return Sema::AA_Passing; |
| 4890 | |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 4891 | case InitializedEntity::EK_Parameter_CF_Audited: |
| 4892 | if (Entity.getDecl() && |
| 4893 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 4894 | return Sema::AA_Sending; |
| 4895 | |
| 4896 | return !Diagnose ? Sema::AA_Passing : Sema::AA_Passing_CFAudited; |
| 4897 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4898 | case InitializedEntity::EK_Result: |
| 4899 | return Sema::AA_Returning; |
| 4900 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4901 | case InitializedEntity::EK_Temporary: |
Fariborz Jahanian | 14e9541 | 2013-07-11 19:13:34 +0000 | [diff] [blame] | 4902 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4903 | // FIXME: Can we tell apart casting vs. converting? |
| 4904 | return Sema::AA_Casting; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4905 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4906 | case InitializedEntity::EK_Member: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4907 | case InitializedEntity::EK_ArrayElement: |
| 4908 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4909 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4910 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4911 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4912 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4913 | return Sema::AA_Initializing; |
| 4914 | } |
| 4915 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4916 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4917 | } |
| 4918 | |
Richard Smith | 27874d6 | 2013-01-08 00:08:23 +0000 | [diff] [blame] | 4919 | /// \brief Whether we should bind a created object as a temporary when |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4920 | /// initializing the given entity. |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4921 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4922 | switch (Entity.getKind()) { |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4923 | case InitializedEntity::EK_ArrayElement: |
| 4924 | case InitializedEntity::EK_Member: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4925 | case InitializedEntity::EK_Result: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4926 | case InitializedEntity::EK_New: |
| 4927 | case InitializedEntity::EK_Variable: |
| 4928 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4929 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4930 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4931 | case InitializedEntity::EK_ComplexElement: |
Anders Carlsson | fcd764a | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 4932 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4933 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4934 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4935 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4936 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4937 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4938 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 4939 | case InitializedEntity::EK_Parameter_CF_Audited: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4940 | case InitializedEntity::EK_Temporary: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 4941 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4942 | return true; |
| 4943 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4944 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4945 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 4946 | } |
| 4947 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4948 | /// \brief Whether the given entity, when initialized with an object |
| 4949 | /// created for that initialization, requires destruction. |
| 4950 | static bool shouldDestroyTemporary(const InitializedEntity &Entity) { |
| 4951 | switch (Entity.getKind()) { |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4952 | case InitializedEntity::EK_Result: |
| 4953 | case InitializedEntity::EK_New: |
| 4954 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4955 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4956 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4957 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4958 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4959 | case InitializedEntity::EK_LambdaCapture: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4960 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4961 | |
Richard Smith | 27874d6 | 2013-01-08 00:08:23 +0000 | [diff] [blame] | 4962 | case InitializedEntity::EK_Member: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4963 | case InitializedEntity::EK_Variable: |
| 4964 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 4965 | case InitializedEntity::EK_Parameter_CF_Audited: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4966 | case InitializedEntity::EK_Temporary: |
| 4967 | case InitializedEntity::EK_ArrayElement: |
| 4968 | case InitializedEntity::EK_Exception: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4969 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 4970 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4971 | return true; |
| 4972 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4973 | |
| 4974 | llvm_unreachable("missed an InitializedEntity kind?"); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4975 | } |
| 4976 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4977 | /// \brief Look for copy and move constructors and constructor templates, for |
| 4978 | /// copying an object via direct-initialization (per C++11 [dcl.init]p16). |
| 4979 | static void LookupCopyAndMoveConstructors(Sema &S, |
| 4980 | OverloadCandidateSet &CandidateSet, |
| 4981 | CXXRecordDecl *Class, |
| 4982 | Expr *CurInitExpr) { |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4983 | DeclContext::lookup_result R = S.LookupConstructors(Class); |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4984 | // The container holding the constructors can under certain conditions |
| 4985 | // be changed while iterating (e.g. because of deserialization). |
| 4986 | // To be safe we copy the lookup results to a new container. |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4987 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 4988 | for (SmallVectorImpl<NamedDecl *>::iterator |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4989 | CI = Ctors.begin(), CE = Ctors.end(); CI != CE; ++CI) { |
| 4990 | NamedDecl *D = *CI; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4991 | CXXConstructorDecl *Constructor = nullptr; |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4992 | |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4993 | if ((Constructor = dyn_cast<CXXConstructorDecl>(D))) { |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4994 | // Handle copy/moveconstructors, only. |
| 4995 | if (!Constructor || Constructor->isInvalidDecl() || |
| 4996 | !Constructor->isCopyOrMoveConstructor() || |
| 4997 | !Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
| 4998 | continue; |
| 4999 | |
| 5000 | DeclAccessPair FoundDecl |
| 5001 | = DeclAccessPair::make(Constructor, Constructor->getAccess()); |
| 5002 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5003 | CurInitExpr, CandidateSet); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5004 | continue; |
| 5005 | } |
| 5006 | |
| 5007 | // Handle constructor templates. |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 5008 | FunctionTemplateDecl *ConstructorTmpl = cast<FunctionTemplateDecl>(D); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5009 | if (ConstructorTmpl->isInvalidDecl()) |
| 5010 | continue; |
| 5011 | |
| 5012 | Constructor = cast<CXXConstructorDecl>( |
| 5013 | ConstructorTmpl->getTemplatedDecl()); |
| 5014 | if (!Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
| 5015 | continue; |
| 5016 | |
| 5017 | // FIXME: Do we need to limit this to copy-constructor-like |
| 5018 | // candidates? |
| 5019 | DeclAccessPair FoundDecl |
| 5020 | = DeclAccessPair::make(ConstructorTmpl, ConstructorTmpl->getAccess()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5021 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, nullptr, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5022 | CurInitExpr, CandidateSet, true); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5023 | } |
| 5024 | } |
| 5025 | |
| 5026 | /// \brief Get the location at which initialization diagnostics should appear. |
| 5027 | static SourceLocation getInitializationLoc(const InitializedEntity &Entity, |
| 5028 | Expr *Initializer) { |
| 5029 | switch (Entity.getKind()) { |
| 5030 | case InitializedEntity::EK_Result: |
| 5031 | return Entity.getReturnLoc(); |
| 5032 | |
| 5033 | case InitializedEntity::EK_Exception: |
| 5034 | return Entity.getThrowLoc(); |
| 5035 | |
| 5036 | case InitializedEntity::EK_Variable: |
| 5037 | return Entity.getDecl()->getLocation(); |
| 5038 | |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 5039 | case InitializedEntity::EK_LambdaCapture: |
| 5040 | return Entity.getCaptureLoc(); |
| 5041 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5042 | case InitializedEntity::EK_ArrayElement: |
| 5043 | case InitializedEntity::EK_Member: |
| 5044 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5045 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5046 | case InitializedEntity::EK_Temporary: |
| 5047 | case InitializedEntity::EK_New: |
| 5048 | case InitializedEntity::EK_Base: |
| 5049 | case InitializedEntity::EK_Delegating: |
| 5050 | case InitializedEntity::EK_VectorElement: |
| 5051 | case InitializedEntity::EK_ComplexElement: |
| 5052 | case InitializedEntity::EK_BlockElement: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5053 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5054 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5055 | return Initializer->getLocStart(); |
| 5056 | } |
| 5057 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 5058 | } |
| 5059 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5060 | /// \brief Make a (potentially elidable) temporary copy of the object |
| 5061 | /// provided by the given initializer by calling the appropriate copy |
| 5062 | /// constructor. |
| 5063 | /// |
| 5064 | /// \param S The Sema object used for type-checking. |
| 5065 | /// |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 5066 | /// \param T The type of the temporary object, which must either be |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5067 | /// the type of the initializer expression or a superclass thereof. |
| 5068 | /// |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5069 | /// \param Entity The entity being initialized. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5070 | /// |
| 5071 | /// \param CurInit The initializer expression. |
| 5072 | /// |
| 5073 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 5074 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 5075 | /// an rvalue. |
| 5076 | /// |
| 5077 | /// \returns An expression that copies the initializer expression into |
| 5078 | /// a temporary object, or an error expression if a copy could not be |
| 5079 | /// created. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5080 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 5081 | QualType T, |
| 5082 | const InitializedEntity &Entity, |
| 5083 | ExprResult CurInit, |
| 5084 | bool IsExtraneousCopy) { |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5085 | // Determine which class type we're copying to. |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 5086 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5087 | CXXRecordDecl *Class = nullptr; |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5088 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5089 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 5090 | if (!Class) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5091 | return CurInit; |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5092 | |
Douglas Gregor | 5d36900 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 5093 | // C++0x [class.copy]p32: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5094 | // When certain criteria are met, an implementation is allowed to |
| 5095 | // omit the copy/move construction of a class object, even if the |
| 5096 | // copy/move constructor and/or destructor for the object have |
| 5097 | // side effects. [...] |
| 5098 | // - when a temporary class object that has not been bound to a |
| 5099 | // reference (12.2) would be copied/moved to a class object |
| 5100 | // with the same cv-unqualified type, the copy/move operation |
| 5101 | // can be omitted by constructing the temporary object |
| 5102 | // directly into the target of the omitted copy/move |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5103 | // |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5104 | // Note that the other three bullets are handled elsewhere. Copy |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 5105 | // elision for return statements and throw expressions are handled as part |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5106 | // of constructor initialization, while copy elision for exception handlers |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 5107 | // is handled by the run-time. |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 5108 | bool Elidable = CurInitExpr->isTemporaryObject(S.Context, Class); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5109 | SourceLocation Loc = getInitializationLoc(Entity, CurInit.get()); |
Douglas Gregor | d5c231e | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 5110 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5111 | // Make sure that the type we are copying is complete. |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5112 | if (S.RequireCompleteType(Loc, T, diag::err_temp_copy_incomplete)) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5113 | return CurInit; |
Douglas Gregor | d5c231e | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 5114 | |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 5115 | // Perform overload resolution using the class's copy/move constructors. |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5116 | // Only consider constructors and constructor templates. Per |
| 5117 | // C++0x [dcl.init]p16, second bullet to class types, this initialization |
| 5118 | // is direct-initialization. |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 5119 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5120 | LookupCopyAndMoveConstructors(S, CandidateSet, Class, CurInitExpr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5121 | |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5122 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 5123 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5124 | OverloadCandidateSet::iterator Best; |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 5125 | switch (CandidateSet.BestViableFunction(S, Loc, Best)) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5126 | case OR_Success: |
| 5127 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5128 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5129 | case OR_No_Viable_Function: |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 5130 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 5131 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 5132 | : diag::err_temp_copy_no_viable) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 5133 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5134 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5135 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 5136 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5137 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5138 | return CurInit; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5139 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5140 | case OR_Ambiguous: |
| 5141 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 5142 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5143 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5144 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5145 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5146 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5147 | case OR_Deleted: |
| 5148 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 5149 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5150 | << CurInitExpr->getSourceRange(); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5151 | S.NoteDeletedFunction(Best->Function); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5152 | return ExprError(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5153 | } |
| 5154 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5155 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 5156 | SmallVector<Expr*, 8> ConstructorArgs; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5157 | CurInit.get(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5158 | |
Anders Carlsson | a01874b | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 5159 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 5160 | Best->FoundDecl.getAccess(), IsExtraneousCopy); |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5161 | |
| 5162 | if (IsExtraneousCopy) { |
| 5163 | // If this is a totally extraneous copy for C++03 reference |
| 5164 | // binding purposes, just return the original initialization |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 5165 | // expression. We don't generate an (elided) copy operation here |
| 5166 | // because doing so would require us to pass down a flag to avoid |
| 5167 | // infinite recursion, where each step adds another extraneous, |
| 5168 | // elidable copy. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5169 | |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 5170 | // Instantiate the default arguments of any extra parameters in |
| 5171 | // the selected copy constructor, as if we were going to create a |
| 5172 | // proper call to the copy constructor. |
| 5173 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 5174 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 5175 | if (S.RequireCompleteType(Loc, Parm->getType(), |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5176 | diag::err_call_incomplete_argument)) |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 5177 | break; |
| 5178 | |
| 5179 | // Build the default argument expression; we don't actually care |
| 5180 | // if this succeeds or not, because this routine will complain |
| 5181 | // if there was a problem. |
| 5182 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 5183 | } |
| 5184 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5185 | return CurInitExpr; |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5186 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5187 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5188 | // Determine the arguments required to actually perform the |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5189 | // constructor call (we might have derived-to-base conversions, or |
| 5190 | // the copy constructor may have default arguments). |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5191 | if (S.CompleteConstructorCall(Constructor, CurInitExpr, Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5192 | return ExprError(); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5193 | |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 5194 | // Actually perform the constructor call. |
| 5195 | CurInit = S.BuildCXXConstructExpr(Loc, T, Constructor, Elidable, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5196 | ConstructorArgs, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5197 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5198 | /*ListInit*/ false, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 5199 | /*StdInitListInit*/ false, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 5200 | /*ZeroInit*/ false, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5201 | CXXConstructExpr::CK_Complete, |
| 5202 | SourceRange()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5203 | |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 5204 | // If we're supposed to bind temporaries, do so. |
| 5205 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5206 | CurInit = S.MaybeBindToTemporary(CurInit.getAs<Expr>()); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5207 | return CurInit; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5208 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5209 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5210 | /// \brief Check whether elidable copy construction for binding a reference to |
| 5211 | /// a temporary would have succeeded if we were building in C++98 mode, for |
| 5212 | /// -Wc++98-compat. |
| 5213 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 5214 | const InitializedEntity &Entity, |
| 5215 | Expr *CurInitExpr) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5216 | assert(S.getLangOpts().CPlusPlus11); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5217 | |
| 5218 | const RecordType *Record = CurInitExpr->getType()->getAs<RecordType>(); |
| 5219 | if (!Record) |
| 5220 | return; |
| 5221 | |
| 5222 | SourceLocation Loc = getInitializationLoc(Entity, CurInitExpr); |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 5223 | if (S.Diags.isIgnored(diag::warn_cxx98_compat_temp_copy, Loc)) |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5224 | return; |
| 5225 | |
| 5226 | // Find constructors which would have been considered. |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 5227 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5228 | LookupCopyAndMoveConstructors( |
| 5229 | S, CandidateSet, cast<CXXRecordDecl>(Record->getDecl()), CurInitExpr); |
| 5230 | |
| 5231 | // Perform overload resolution. |
| 5232 | OverloadCandidateSet::iterator Best; |
| 5233 | OverloadingResult OR = CandidateSet.BestViableFunction(S, Loc, Best); |
| 5234 | |
| 5235 | PartialDiagnostic Diag = S.PDiag(diag::warn_cxx98_compat_temp_copy) |
| 5236 | << OR << (int)Entity.getKind() << CurInitExpr->getType() |
| 5237 | << CurInitExpr->getSourceRange(); |
| 5238 | |
| 5239 | switch (OR) { |
| 5240 | case OR_Success: |
| 5241 | S.CheckConstructorAccess(Loc, cast<CXXConstructorDecl>(Best->Function), |
John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 5242 | Entity, Best->FoundDecl.getAccess(), Diag); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5243 | // FIXME: Check default arguments as far as that's possible. |
| 5244 | break; |
| 5245 | |
| 5246 | case OR_No_Viable_Function: |
| 5247 | S.Diag(Loc, Diag); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5248 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5249 | break; |
| 5250 | |
| 5251 | case OR_Ambiguous: |
| 5252 | S.Diag(Loc, Diag); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5253 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5254 | break; |
| 5255 | |
| 5256 | case OR_Deleted: |
| 5257 | S.Diag(Loc, Diag); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5258 | S.NoteDeletedFunction(Best->Function); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5259 | break; |
| 5260 | } |
| 5261 | } |
| 5262 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5263 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 5264 | const InitializedEntity &Entity) { |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5265 | if (Entity.isParameterKind() && Entity.getDecl()) { |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5266 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 5267 | return; |
| 5268 | |
| 5269 | if (Entity.getDecl()->getDeclName()) |
| 5270 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 5271 | << Entity.getDecl()->getDeclName(); |
| 5272 | else |
| 5273 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 5274 | } |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5275 | else if (Entity.getKind() == InitializedEntity::EK_RelatedResult && |
| 5276 | Entity.getMethodDecl()) |
| 5277 | S.Diag(Entity.getMethodDecl()->getLocation(), |
| 5278 | diag::note_method_return_type_change) |
| 5279 | << Entity.getMethodDecl()->getDeclName(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5280 | } |
| 5281 | |
Sebastian Redl | 112aa82 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 5282 | static bool isReferenceBinding(const InitializationSequence::Step &s) { |
| 5283 | return s.Kind == InitializationSequence::SK_BindReference || |
| 5284 | s.Kind == InitializationSequence::SK_BindReferenceToTemporary; |
| 5285 | } |
| 5286 | |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5287 | /// Returns true if the parameters describe a constructor initialization of |
| 5288 | /// an explicit temporary object, e.g. "Point(x, y)". |
| 5289 | static bool isExplicitTemporary(const InitializedEntity &Entity, |
| 5290 | const InitializationKind &Kind, |
| 5291 | unsigned NumArgs) { |
| 5292 | switch (Entity.getKind()) { |
| 5293 | case InitializedEntity::EK_Temporary: |
| 5294 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5295 | case InitializedEntity::EK_RelatedResult: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5296 | break; |
| 5297 | default: |
| 5298 | return false; |
| 5299 | } |
| 5300 | |
| 5301 | switch (Kind.getKind()) { |
| 5302 | case InitializationKind::IK_DirectList: |
| 5303 | return true; |
| 5304 | // FIXME: Hack to work around cast weirdness. |
| 5305 | case InitializationKind::IK_Direct: |
| 5306 | case InitializationKind::IK_Value: |
| 5307 | return NumArgs != 1; |
| 5308 | default: |
| 5309 | return false; |
| 5310 | } |
| 5311 | } |
| 5312 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5313 | static ExprResult |
| 5314 | PerformConstructorInitialization(Sema &S, |
| 5315 | const InitializedEntity &Entity, |
| 5316 | const InitializationKind &Kind, |
| 5317 | MultiExprArg Args, |
| 5318 | const InitializationSequence::Step& Step, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5319 | bool &ConstructorInitRequiresZeroInit, |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 5320 | bool IsListInitialization, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 5321 | bool IsStdInitListInitialization, |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 5322 | SourceLocation LBraceLoc, |
| 5323 | SourceLocation RBraceLoc) { |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5324 | unsigned NumArgs = Args.size(); |
| 5325 | CXXConstructorDecl *Constructor |
| 5326 | = cast<CXXConstructorDecl>(Step.Function.Function); |
| 5327 | bool HadMultipleCandidates = Step.Function.HadMultipleCandidates; |
| 5328 | |
| 5329 | // Build a call to the selected constructor. |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 5330 | SmallVector<Expr*, 8> ConstructorArgs; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5331 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 5332 | ? Kind.getEqualLoc() |
| 5333 | : Kind.getLocation(); |
| 5334 | |
| 5335 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 5336 | // Force even a trivial, implicit default constructor to be |
| 5337 | // semantically checked. We do this explicitly because we don't build |
| 5338 | // the definition for completely trivial constructors. |
Matt Beaumont-Gay | 47ff122 | 2012-02-24 08:37:56 +0000 | [diff] [blame] | 5339 | assert(Constructor->getParent() && "No parent class for constructor."); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5340 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Douglas Gregor | f704ade | 2012-02-24 07:48:37 +0000 | [diff] [blame] | 5341 | Constructor->isTrivial() && !Constructor->isUsed(false)) |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5342 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 5343 | } |
| 5344 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5345 | ExprResult CurInit((Expr *)nullptr); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5346 | |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5347 | // C++ [over.match.copy]p1: |
| 5348 | // - When initializing a temporary to be bound to the first parameter |
| 5349 | // of a constructor that takes a reference to possibly cv-qualified |
| 5350 | // T as its first argument, called with a single argument in the |
| 5351 | // context of direct-initialization, explicit conversion functions |
| 5352 | // are also considered. |
| 5353 | bool AllowExplicitConv = Kind.AllowExplicit() && !Kind.isCopyInit() && |
| 5354 | Args.size() == 1 && |
| 5355 | Constructor->isCopyOrMoveConstructor(); |
| 5356 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5357 | // Determine the arguments required to actually perform the constructor |
| 5358 | // call. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5359 | if (S.CompleteConstructorCall(Constructor, Args, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5360 | Loc, ConstructorArgs, |
Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 5361 | AllowExplicitConv, |
| 5362 | IsListInitialization)) |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5363 | return ExprError(); |
| 5364 | |
| 5365 | |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5366 | if (isExplicitTemporary(Entity, Kind, NumArgs)) { |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5367 | // An explicitly-constructed temporary, e.g., X(1, 2). |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 5368 | S.MarkFunctionReferenced(Loc, Constructor); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5369 | if (S.DiagnoseUseOfDecl(Constructor, Loc)) |
| 5370 | return ExprError(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5371 | |
| 5372 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 5373 | if (!TSInfo) |
| 5374 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 5375 | SourceRange ParenOrBraceRange = |
| 5376 | (Kind.getKind() == InitializationKind::IK_DirectList) |
| 5377 | ? SourceRange(LBraceLoc, RBraceLoc) |
| 5378 | : Kind.getParenRange(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5379 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5380 | CurInit = new (S.Context) CXXTemporaryObjectExpr( |
| 5381 | S.Context, Constructor, TSInfo, ConstructorArgs, ParenOrBraceRange, |
| 5382 | HadMultipleCandidates, IsListInitialization, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 5383 | IsStdInitListInitialization, ConstructorInitRequiresZeroInit); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5384 | } else { |
| 5385 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 5386 | CXXConstructExpr::CK_Complete; |
| 5387 | |
| 5388 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 5389 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
| 5390 | CXXConstructExpr::CK_VirtualBase : |
| 5391 | CXXConstructExpr::CK_NonVirtualBase; |
| 5392 | } else if (Entity.getKind() == InitializedEntity::EK_Delegating) { |
| 5393 | ConstructKind = CXXConstructExpr::CK_Delegating; |
| 5394 | } |
| 5395 | |
Peter Collingbourne | b8d17e7 | 2014-02-22 02:59:41 +0000 | [diff] [blame] | 5396 | // Only get the parenthesis or brace range if it is a list initialization or |
| 5397 | // direct construction. |
| 5398 | SourceRange ParenOrBraceRange; |
| 5399 | if (IsListInitialization) |
| 5400 | ParenOrBraceRange = SourceRange(LBraceLoc, RBraceLoc); |
| 5401 | else if (Kind.getKind() == InitializationKind::IK_Direct) |
| 5402 | ParenOrBraceRange = Kind.getParenRange(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5403 | |
| 5404 | // If the entity allows NRVO, mark the construction as elidable |
| 5405 | // unconditionally. |
| 5406 | if (Entity.allowsNRVO()) |
| 5407 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 5408 | Constructor, /*Elidable=*/true, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5409 | ConstructorArgs, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5410 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5411 | IsListInitialization, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 5412 | IsStdInitListInitialization, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5413 | ConstructorInitRequiresZeroInit, |
| 5414 | ConstructKind, |
Peter Collingbourne | b8d17e7 | 2014-02-22 02:59:41 +0000 | [diff] [blame] | 5415 | ParenOrBraceRange); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5416 | else |
| 5417 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 5418 | Constructor, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5419 | ConstructorArgs, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5420 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5421 | IsListInitialization, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 5422 | IsStdInitListInitialization, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5423 | ConstructorInitRequiresZeroInit, |
| 5424 | ConstructKind, |
Peter Collingbourne | b8d17e7 | 2014-02-22 02:59:41 +0000 | [diff] [blame] | 5425 | ParenOrBraceRange); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5426 | } |
| 5427 | if (CurInit.isInvalid()) |
| 5428 | return ExprError(); |
| 5429 | |
| 5430 | // Only check access if all of that succeeded. |
| 5431 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
| 5432 | Step.Function.FoundDecl.getAccess()); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5433 | if (S.DiagnoseUseOfDecl(Step.Function.FoundDecl, Loc)) |
| 5434 | return ExprError(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5435 | |
| 5436 | if (shouldBindAsTemporary(Entity)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5437 | CurInit = S.MaybeBindToTemporary(CurInit.get()); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5438 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5439 | return CurInit; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5440 | } |
| 5441 | |
Richard Smith | eb3cad5 | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5442 | /// Determine whether the specified InitializedEntity definitely has a lifetime |
| 5443 | /// longer than the current full-expression. Conservatively returns false if |
| 5444 | /// it's unclear. |
| 5445 | static bool |
| 5446 | InitializedEntityOutlivesFullExpression(const InitializedEntity &Entity) { |
| 5447 | const InitializedEntity *Top = &Entity; |
| 5448 | while (Top->getParent()) |
| 5449 | Top = Top->getParent(); |
| 5450 | |
| 5451 | switch (Top->getKind()) { |
| 5452 | case InitializedEntity::EK_Variable: |
| 5453 | case InitializedEntity::EK_Result: |
| 5454 | case InitializedEntity::EK_Exception: |
| 5455 | case InitializedEntity::EK_Member: |
| 5456 | case InitializedEntity::EK_New: |
| 5457 | case InitializedEntity::EK_Base: |
| 5458 | case InitializedEntity::EK_Delegating: |
| 5459 | return true; |
| 5460 | |
| 5461 | case InitializedEntity::EK_ArrayElement: |
| 5462 | case InitializedEntity::EK_VectorElement: |
| 5463 | case InitializedEntity::EK_BlockElement: |
| 5464 | case InitializedEntity::EK_ComplexElement: |
| 5465 | // Could not determine what the full initialization is. Assume it might not |
| 5466 | // outlive the full-expression. |
| 5467 | return false; |
| 5468 | |
| 5469 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5470 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | eb3cad5 | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5471 | case InitializedEntity::EK_Temporary: |
| 5472 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5473 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5474 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | eb3cad5 | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5475 | // The entity being initialized might not outlive the full-expression. |
| 5476 | return false; |
| 5477 | } |
| 5478 | |
| 5479 | llvm_unreachable("unknown entity kind"); |
| 5480 | } |
| 5481 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5482 | /// Determine the declaration which an initialized entity ultimately refers to, |
| 5483 | /// for the purpose of lifetime-extending a temporary bound to a reference in |
| 5484 | /// the initialization of \p Entity. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5485 | static const InitializedEntity *getEntityForTemporaryLifetimeExtension( |
| 5486 | const InitializedEntity *Entity, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5487 | const InitializedEntity *FallbackDecl = nullptr) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5488 | // C++11 [class.temporary]p5: |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5489 | switch (Entity->getKind()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5490 | case InitializedEntity::EK_Variable: |
| 5491 | // The temporary [...] persists for the lifetime of the reference |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5492 | return Entity; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5493 | |
| 5494 | case InitializedEntity::EK_Member: |
| 5495 | // For subobjects, we look at the complete object. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5496 | if (Entity->getParent()) |
| 5497 | return getEntityForTemporaryLifetimeExtension(Entity->getParent(), |
| 5498 | Entity); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5499 | |
| 5500 | // except: |
| 5501 | // -- A temporary bound to a reference member in a constructor's |
| 5502 | // ctor-initializer persists until the constructor exits. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5503 | return Entity; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5504 | |
| 5505 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5506 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5507 | // -- A temporary bound to a reference parameter in a function call |
| 5508 | // persists until the completion of the full-expression containing |
| 5509 | // the call. |
| 5510 | case InitializedEntity::EK_Result: |
| 5511 | // -- The lifetime of a temporary bound to the returned value in a |
| 5512 | // function return statement is not extended; the temporary is |
| 5513 | // destroyed at the end of the full-expression in the return statement. |
| 5514 | case InitializedEntity::EK_New: |
| 5515 | // -- A temporary bound to a reference in a new-initializer persists |
| 5516 | // until the completion of the full-expression containing the |
| 5517 | // new-initializer. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5518 | return nullptr; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5519 | |
| 5520 | case InitializedEntity::EK_Temporary: |
| 5521 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5522 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5523 | // We don't yet know the storage duration of the surrounding temporary. |
| 5524 | // Assume it's got full-expression duration for now, it will patch up our |
| 5525 | // storage duration if that's not correct. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5526 | return nullptr; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5527 | |
| 5528 | case InitializedEntity::EK_ArrayElement: |
| 5529 | // For subobjects, we look at the complete object. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5530 | return getEntityForTemporaryLifetimeExtension(Entity->getParent(), |
| 5531 | FallbackDecl); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5532 | |
| 5533 | case InitializedEntity::EK_Base: |
| 5534 | case InitializedEntity::EK_Delegating: |
| 5535 | // We can reach this case for aggregate initialization in a constructor: |
| 5536 | // struct A { int &&r; }; |
| 5537 | // struct B : A { B() : A{0} {} }; |
| 5538 | // In this case, use the innermost field decl as the context. |
| 5539 | return FallbackDecl; |
| 5540 | |
| 5541 | case InitializedEntity::EK_BlockElement: |
| 5542 | case InitializedEntity::EK_LambdaCapture: |
| 5543 | case InitializedEntity::EK_Exception: |
| 5544 | case InitializedEntity::EK_VectorElement: |
| 5545 | case InitializedEntity::EK_ComplexElement: |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5546 | return nullptr; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5547 | } |
Benjamin Kramer | cabc882 | 2013-06-05 15:37:50 +0000 | [diff] [blame] | 5548 | llvm_unreachable("unknown entity kind"); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5549 | } |
| 5550 | |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5551 | static void performLifetimeExtension(Expr *Init, |
| 5552 | const InitializedEntity *ExtendingEntity); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5553 | |
| 5554 | /// Update a glvalue expression that is used as the initializer of a reference |
| 5555 | /// to note that its lifetime is extended. |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5556 | /// \return \c true if any temporary had its lifetime extended. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5557 | static bool |
| 5558 | performReferenceExtension(Expr *Init, |
| 5559 | const InitializedEntity *ExtendingEntity) { |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5560 | // Walk past any constructs which we can lifetime-extend across. |
| 5561 | Expr *Old; |
| 5562 | do { |
| 5563 | Old = Init; |
| 5564 | |
Richard Smith | dbc8249 | 2015-01-10 01:28:13 +0000 | [diff] [blame] | 5565 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
| 5566 | if (ILE->getNumInits() == 1 && ILE->isGLValue()) { |
| 5567 | // This is just redundant braces around an initializer. Step over it. |
| 5568 | Init = ILE->getInit(0); |
| 5569 | } |
| 5570 | } |
| 5571 | |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5572 | // Step over any subobject adjustments; we may have a materialized |
| 5573 | // temporary inside them. |
| 5574 | SmallVector<const Expr *, 2> CommaLHSs; |
| 5575 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
| 5576 | Init = const_cast<Expr *>( |
| 5577 | Init->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments)); |
| 5578 | |
| 5579 | // Per current approach for DR1376, look through casts to reference type |
| 5580 | // when performing lifetime extension. |
| 5581 | if (CastExpr *CE = dyn_cast<CastExpr>(Init)) |
| 5582 | if (CE->getSubExpr()->isGLValue()) |
| 5583 | Init = CE->getSubExpr(); |
| 5584 | |
| 5585 | // FIXME: Per DR1213, subscripting on an array temporary produces an xvalue. |
| 5586 | // It's unclear if binding a reference to that xvalue extends the array |
| 5587 | // temporary. |
| 5588 | } while (Init != Old); |
| 5589 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5590 | if (MaterializeTemporaryExpr *ME = dyn_cast<MaterializeTemporaryExpr>(Init)) { |
| 5591 | // Update the storage duration of the materialized temporary. |
| 5592 | // FIXME: Rebuild the expression instead of mutating it. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5593 | ME->setExtendingDecl(ExtendingEntity->getDecl(), |
| 5594 | ExtendingEntity->allocateManglingNumber()); |
| 5595 | performLifetimeExtension(ME->GetTemporaryExpr(), ExtendingEntity); |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5596 | return true; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5597 | } |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5598 | |
| 5599 | return false; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5600 | } |
| 5601 | |
| 5602 | /// Update a prvalue expression that is going to be materialized as a |
| 5603 | /// lifetime-extended temporary. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5604 | static void performLifetimeExtension(Expr *Init, |
| 5605 | const InitializedEntity *ExtendingEntity) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5606 | // Dig out the expression which constructs the extended temporary. |
| 5607 | SmallVector<const Expr *, 2> CommaLHSs; |
| 5608 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
| 5609 | Init = const_cast<Expr *>( |
| 5610 | Init->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments)); |
| 5611 | |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 5612 | if (CXXBindTemporaryExpr *BTE = dyn_cast<CXXBindTemporaryExpr>(Init)) |
| 5613 | Init = BTE->getSubExpr(); |
| 5614 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5615 | if (CXXStdInitializerListExpr *ILE = |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5616 | dyn_cast<CXXStdInitializerListExpr>(Init)) { |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5617 | performReferenceExtension(ILE->getSubExpr(), ExtendingEntity); |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5618 | return; |
| 5619 | } |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5620 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5621 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5622 | if (ILE->getType()->isArrayType()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5623 | for (unsigned I = 0, N = ILE->getNumInits(); I != N; ++I) |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5624 | performLifetimeExtension(ILE->getInit(I), ExtendingEntity); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5625 | return; |
| 5626 | } |
| 5627 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5628 | if (CXXRecordDecl *RD = ILE->getType()->getAsCXXRecordDecl()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5629 | assert(RD->isAggregate() && "aggregate init on non-aggregate"); |
| 5630 | |
| 5631 | // If we lifetime-extend a braced initializer which is initializing an |
| 5632 | // aggregate, and that aggregate contains reference members which are |
| 5633 | // bound to temporaries, those temporaries are also lifetime-extended. |
| 5634 | if (RD->isUnion() && ILE->getInitializedFieldInUnion() && |
| 5635 | ILE->getInitializedFieldInUnion()->getType()->isReferenceType()) |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5636 | performReferenceExtension(ILE->getInit(0), ExtendingEntity); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5637 | else { |
| 5638 | unsigned Index = 0; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5639 | for (const auto *I : RD->fields()) { |
Richard Smith | 0bca59d | 2013-07-01 06:08:20 +0000 | [diff] [blame] | 5640 | if (Index >= ILE->getNumInits()) |
| 5641 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5642 | if (I->isUnnamedBitfield()) |
| 5643 | continue; |
Richard Smith | 8d7f11d | 2013-06-27 22:54:33 +0000 | [diff] [blame] | 5644 | Expr *SubInit = ILE->getInit(Index); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5645 | if (I->getType()->isReferenceType()) |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5646 | performReferenceExtension(SubInit, ExtendingEntity); |
Richard Smith | 8d7f11d | 2013-06-27 22:54:33 +0000 | [diff] [blame] | 5647 | else if (isa<InitListExpr>(SubInit) || |
| 5648 | isa<CXXStdInitializerListExpr>(SubInit)) |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5649 | // This may be either aggregate-initialization of a member or |
| 5650 | // initialization of a std::initializer_list object. Either way, |
| 5651 | // we should recursively lifetime-extend that initializer. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5652 | performLifetimeExtension(SubInit, ExtendingEntity); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5653 | ++Index; |
| 5654 | } |
| 5655 | } |
| 5656 | } |
| 5657 | } |
| 5658 | } |
| 5659 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5660 | static void warnOnLifetimeExtension(Sema &S, const InitializedEntity &Entity, |
| 5661 | const Expr *Init, bool IsInitializerList, |
| 5662 | const ValueDecl *ExtendingDecl) { |
| 5663 | // Warn if a field lifetime-extends a temporary. |
| 5664 | if (isa<FieldDecl>(ExtendingDecl)) { |
| 5665 | if (IsInitializerList) { |
| 5666 | S.Diag(Init->getExprLoc(), diag::warn_dangling_std_initializer_list) |
| 5667 | << /*at end of constructor*/true; |
| 5668 | return; |
| 5669 | } |
| 5670 | |
| 5671 | bool IsSubobjectMember = false; |
| 5672 | for (const InitializedEntity *Ent = Entity.getParent(); Ent; |
| 5673 | Ent = Ent->getParent()) { |
| 5674 | if (Ent->getKind() != InitializedEntity::EK_Base) { |
| 5675 | IsSubobjectMember = true; |
| 5676 | break; |
| 5677 | } |
| 5678 | } |
| 5679 | S.Diag(Init->getExprLoc(), |
| 5680 | diag::warn_bind_ref_member_to_temporary) |
| 5681 | << ExtendingDecl << Init->getSourceRange() |
| 5682 | << IsSubobjectMember << IsInitializerList; |
| 5683 | if (IsSubobjectMember) |
| 5684 | S.Diag(ExtendingDecl->getLocation(), |
| 5685 | diag::note_ref_subobject_of_member_declared_here); |
| 5686 | else |
| 5687 | S.Diag(ExtendingDecl->getLocation(), |
| 5688 | diag::note_ref_or_ptr_member_declared_here) |
| 5689 | << /*is pointer*/false; |
| 5690 | } |
| 5691 | } |
| 5692 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5693 | static void DiagnoseNarrowingInInitList(Sema &S, |
| 5694 | const ImplicitConversionSequence &ICS, |
| 5695 | QualType PreNarrowingType, |
| 5696 | QualType EntityType, |
| 5697 | const Expr *PostInit); |
| 5698 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5699 | ExprResult |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5700 | InitializationSequence::Perform(Sema &S, |
| 5701 | const InitializedEntity &Entity, |
| 5702 | const InitializationKind &Kind, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5703 | MultiExprArg Args, |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5704 | QualType *ResultType) { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 5705 | if (Failed()) { |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5706 | Diagnose(S, Entity, Kind, Args); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5707 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5708 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5709 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5710 | if (getKind() == DependentSequence) { |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5711 | // If the declaration is a non-dependent, incomplete array type |
| 5712 | // that has an initializer, then its type will be completed once |
| 5713 | // the initializer is instantiated. |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5714 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5715 | Args.size() == 1) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5716 | QualType DeclType = Entity.getType(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5717 | if (const IncompleteArrayType *ArrayT |
| 5718 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 5719 | // FIXME: We don't currently have the ability to accurately |
| 5720 | // compute the length of an initializer list without |
| 5721 | // performing full type-checking of the initializer list |
| 5722 | // (since we have to determine where braces are implicitly |
| 5723 | // introduced and such). So, we fall back to making the array |
| 5724 | // type a dependently-sized array type with no specified |
| 5725 | // bound. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5726 | if (isa<InitListExpr>((Expr *)Args[0])) { |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5727 | SourceRange Brackets; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5728 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5729 | // Scavange the location of the brackets from the entity, if we can. |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5730 | if (DeclaratorDecl *DD = Entity.getDecl()) { |
| 5731 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 5732 | TypeLoc TL = TInfo->getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 5733 | if (IncompleteArrayTypeLoc ArrayLoc = |
| 5734 | TL.getAs<IncompleteArrayTypeLoc>()) |
| 5735 | Brackets = ArrayLoc.getBracketsRange(); |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5736 | } |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5737 | } |
| 5738 | |
| 5739 | *ResultType |
| 5740 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5741 | /*NumElts=*/nullptr, |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5742 | ArrayT->getSizeModifier(), |
| 5743 | ArrayT->getIndexTypeCVRQualifiers(), |
| 5744 | Brackets); |
| 5745 | } |
| 5746 | |
| 5747 | } |
| 5748 | } |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 5749 | if (Kind.getKind() == InitializationKind::IK_Direct && |
| 5750 | !Kind.isExplicitCast()) { |
| 5751 | // Rebuild the ParenListExpr. |
| 5752 | SourceRange ParenRange = Kind.getParenRange(); |
| 5753 | return S.ActOnParenListExpr(ParenRange.getBegin(), ParenRange.getEnd(), |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5754 | Args); |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 5755 | } |
Manuel Klimek | f2b4b69 | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 5756 | assert(Kind.getKind() == InitializationKind::IK_Copy || |
Douglas Gregor | bf13895 | 2012-04-04 04:06:51 +0000 | [diff] [blame] | 5757 | Kind.isExplicitCast() || |
| 5758 | Kind.getKind() == InitializationKind::IK_DirectList); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5759 | return ExprResult(Args[0]); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5760 | } |
| 5761 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5762 | // No steps means no initialization. |
| 5763 | if (Steps.empty()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5764 | return ExprResult((Expr *)nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5765 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5766 | if (S.getLangOpts().CPlusPlus11 && Entity.getType()->isReferenceType() && |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5767 | Args.size() == 1 && isa<InitListExpr>(Args[0]) && |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5768 | !Entity.isParameterKind()) { |
Richard Smith | 2b349ae | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5769 | // Produce a C++98 compatibility warning if we are initializing a reference |
| 5770 | // from an initializer list. For parameters, we produce a better warning |
| 5771 | // elsewhere. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5772 | Expr *Init = Args[0]; |
Richard Smith | 2b349ae | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5773 | S.Diag(Init->getLocStart(), diag::warn_cxx98_compat_reference_list_init) |
| 5774 | << Init->getSourceRange(); |
| 5775 | } |
| 5776 | |
Richard Smith | eb3cad5 | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5777 | // Diagnose cases where we initialize a pointer to an array temporary, and the |
| 5778 | // pointer obviously outlives the temporary. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5779 | if (Args.size() == 1 && Args[0]->getType()->isArrayType() && |
Richard Smith | eb3cad5 | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5780 | Entity.getType()->isPointerType() && |
| 5781 | InitializedEntityOutlivesFullExpression(Entity)) { |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5782 | Expr *Init = Args[0]; |
Richard Smith | eb3cad5 | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5783 | Expr::LValueClassification Kind = Init->ClassifyLValue(S.Context); |
| 5784 | if (Kind == Expr::LV_ClassTemporary || Kind == Expr::LV_ArrayTemporary) |
| 5785 | S.Diag(Init->getLocStart(), diag::warn_temporary_array_to_pointer_decay) |
| 5786 | << Init->getSourceRange(); |
| 5787 | } |
| 5788 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5789 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 5790 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 5791 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 5792 | // and we want latter when it makes sense. |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5793 | if (ResultType) |
Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 5794 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5795 | Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5796 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5797 | ExprResult CurInit((Expr *)nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5798 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5799 | // For initialization steps that start with a single initializer, |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5800 | // grab the only argument out the Args and place it into the "current" |
| 5801 | // initializer. |
| 5802 | switch (Steps.front().Kind) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5803 | case SK_ResolveAddressOfOverloadedFunction: |
| 5804 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5805 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5806 | case SK_CastDerivedToBaseLValue: |
| 5807 | case SK_BindReference: |
| 5808 | case SK_BindReferenceToTemporary: |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5809 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5810 | case SK_UserConversion: |
| 5811 | case SK_QualificationConversionLValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5812 | case SK_QualificationConversionXValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5813 | case SK_QualificationConversionRValue: |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5814 | case SK_AtomicConversion: |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 5815 | case SK_LValueToRValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5816 | case SK_ConversionSequence: |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5817 | case SK_ConversionSequenceNoNarrowing: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5818 | case SK_ListInitialization: |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5819 | case SK_UnwrapInitList: |
| 5820 | case SK_RewrapInitList: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5821 | case SK_CAssignment: |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5822 | case SK_StringInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5823 | case SK_ObjCObjectConversion: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5824 | case SK_ArrayInit: |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5825 | case SK_ParenthesizedArrayInit: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5826 | case SK_PassByIndirectCopyRestore: |
| 5827 | case SK_PassByIndirectRestore: |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5828 | case SK_ProduceObjCObject: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5829 | case SK_StdInitializerList: |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5830 | case SK_OCLSamplerInit: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5831 | case SK_OCLZeroEvent: { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5832 | assert(Args.size() == 1); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5833 | CurInit = Args[0]; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5834 | if (!CurInit.get()) return ExprError(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5835 | break; |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5836 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5837 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5838 | case SK_ConstructorInitialization: |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 5839 | case SK_ConstructorInitializationFromList: |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 5840 | case SK_StdInitializerListConstructorCall: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5841 | case SK_ZeroInitialization: |
| 5842 | break; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5843 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5844 | |
| 5845 | // Walk through the computed steps for the initialization sequence, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5846 | // performing the specified conversions along the way. |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5847 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5848 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 5849 | Step != StepEnd; ++Step) { |
| 5850 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5851 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5852 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5853 | QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5854 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5855 | switch (Step->Kind) { |
| 5856 | case SK_ResolveAddressOfOverloadedFunction: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5857 | // Overload resolution determined which function invoke; update the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5858 | // initializer to reflect that choice. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5859 | S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5860 | if (S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation())) |
| 5861 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5862 | CurInit = S.FixOverloadedFunctionReference(CurInit, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5863 | Step->Function.FoundDecl, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5864 | Step->Function.Function); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5865 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5866 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5867 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5868 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5869 | case SK_CastDerivedToBaseLValue: { |
| 5870 | // We have a derived-to-base cast that produces either an rvalue or an |
| 5871 | // lvalue. Perform that cast. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5872 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5873 | CXXCastPath BasePath; |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 5874 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5875 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 5876 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
| 5877 | if (S.CheckDerivedToBaseConversion(SourceType, Step->Type, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5878 | CurInit.get()->getLocStart(), |
| 5879 | CurInit.get()->getSourceRange(), |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 5880 | &BasePath, IgnoreBaseAccess)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5881 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5882 | |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5883 | ExprValueKind VK = |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5884 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5885 | VK_LValue : |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5886 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5887 | VK_XValue : |
| 5888 | VK_RValue); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5889 | CurInit = |
| 5890 | ImplicitCastExpr::Create(S.Context, Step->Type, CK_DerivedToBase, |
| 5891 | CurInit.get(), &BasePath, VK); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5892 | break; |
| 5893 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5894 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5895 | case SK_BindReference: |
John McCall | d25db7e | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5896 | // References cannot bind to bit-fields (C++ [dcl.init.ref]p5). |
| 5897 | if (CurInit.get()->refersToBitField()) { |
| 5898 | // We don't necessarily have an unambiguous source bit-field. |
| 5899 | FieldDecl *BitField = CurInit.get()->getSourceBitField(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5900 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5901 | << Entity.getType().isVolatileQualified() |
John McCall | d25db7e | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5902 | << (BitField ? BitField->getDeclName() : DeclarationName()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5903 | << (BitField != nullptr) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5904 | << CurInit.get()->getSourceRange(); |
John McCall | d25db7e | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5905 | if (BitField) |
| 5906 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
| 5907 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5908 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5909 | } |
Anders Carlsson | a91be64 | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 5910 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5911 | if (CurInit.get()->refersToVectorElement()) { |
John McCall | c17ae44 | 2010-02-02 19:02:38 +0000 | [diff] [blame] | 5912 | // References cannot bind to vector elements. |
Anders Carlsson | 8abde4b | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 5913 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 5914 | << Entity.getType().isVolatileQualified() |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5915 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5916 | PrintInitLocationNote(S, Entity); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5917 | return ExprError(); |
Anders Carlsson | 8abde4b | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 5918 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5919 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5920 | // Reference binding does not have any corresponding ASTs. |
| 5921 | |
| 5922 | // Check exception specifications |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5923 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5924 | return ExprError(); |
Anders Carlsson | ab0ddb5 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 5925 | |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5926 | // Even though we didn't materialize a temporary, the binding may still |
| 5927 | // extend the lifetime of a temporary. This happens if we bind a reference |
| 5928 | // to the result of a cast to reference type. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5929 | if (const InitializedEntity *ExtendingEntity = |
| 5930 | getEntityForTemporaryLifetimeExtension(&Entity)) |
| 5931 | if (performReferenceExtension(CurInit.get(), ExtendingEntity)) |
| 5932 | warnOnLifetimeExtension(S, Entity, CurInit.get(), |
| 5933 | /*IsInitializerList=*/false, |
| 5934 | ExtendingEntity->getDecl()); |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5935 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5936 | break; |
Anders Carlsson | ab0ddb5 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 5937 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5938 | case SK_BindReferenceToTemporary: { |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 5939 | // Make sure the "temporary" is actually an rvalue. |
| 5940 | assert(CurInit.get()->isRValue() && "not a temporary"); |
| 5941 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5942 | // Check exception specifications |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5943 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5944 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5945 | |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 5946 | // Materialize the temporary into memory. |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 5947 | MaterializeTemporaryExpr *MTE = new (S.Context) MaterializeTemporaryExpr( |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5948 | Entity.getType().getNonReferenceType(), CurInit.get(), |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5949 | Entity.getType()->isLValueReferenceType()); |
| 5950 | |
| 5951 | // Maybe lifetime-extend the temporary's subobjects to match the |
| 5952 | // entity's lifetime. |
| 5953 | if (const InitializedEntity *ExtendingEntity = |
| 5954 | getEntityForTemporaryLifetimeExtension(&Entity)) |
| 5955 | if (performReferenceExtension(MTE, ExtendingEntity)) |
| 5956 | warnOnLifetimeExtension(S, Entity, CurInit.get(), /*IsInitializerList=*/false, |
| 5957 | ExtendingEntity->getDecl()); |
Douglas Gregor | 58df509 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 5958 | |
| 5959 | // If we're binding to an Objective-C object that has lifetime, we |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 5960 | // need cleanups. Likewise if we're extending this temporary to automatic |
| 5961 | // storage duration -- we need to register its cleanup during the |
| 5962 | // full-expression's cleanups. |
| 5963 | if ((S.getLangOpts().ObjCAutoRefCount && |
| 5964 | MTE->getType()->isObjCLifetimeType()) || |
| 5965 | (MTE->getStorageDuration() == SD_Automatic && |
| 5966 | MTE->getType().isDestructedType())) |
Douglas Gregor | 58df509 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 5967 | S.ExprNeedsCleanups = true; |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 5968 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5969 | CurInit = MTE; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5970 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5971 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5972 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5973 | case SK_ExtraneousCopyToTemporary: |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5974 | CurInit = CopyObject(S, Step->Type, Entity, CurInit, |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5975 | /*IsExtraneousCopy=*/true); |
| 5976 | break; |
| 5977 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5978 | case SK_UserConversion: { |
| 5979 | // We have a user-defined conversion that invokes either a constructor |
| 5980 | // or a conversion function. |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5981 | CastKind CastKind; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5982 | bool IsCopy = false; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5983 | FunctionDecl *Fn = Step->Function.Function; |
| 5984 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5985 | bool HadMultipleCandidates = Step->Function.HadMultipleCandidates; |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5986 | bool CreatedObject = false; |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5987 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5988 | // Build a call to the selected constructor. |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 5989 | SmallVector<Expr*, 8> ConstructorArgs; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5990 | SourceLocation Loc = CurInit.get()->getLocStart(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5991 | CurInit.get(); // Ownership transferred into MultiExprArg, below. |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5992 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5993 | // Determine the arguments required to actually perform the constructor |
| 5994 | // call. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5995 | Expr *Arg = CurInit.get(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5996 | if (S.CompleteConstructorCall(Constructor, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5997 | MultiExprArg(&Arg, 1), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5998 | Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5999 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6000 | |
Richard Smith | b24f067 | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 6001 | // Build an expression that constructs a temporary. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6002 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6003 | ConstructorArgs, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 6004 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6005 | /*ListInit*/ false, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6006 | /*StdInitListInit*/ false, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 6007 | /*ZeroInit*/ false, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 6008 | CXXConstructExpr::CK_Complete, |
| 6009 | SourceRange()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6010 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6011 | return ExprError(); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 6012 | |
Anders Carlsson | a01874b | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 6013 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, Entity, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6014 | FoundFn.getAccess()); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 6015 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 6016 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6017 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6018 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6019 | QualType Class = S.Context.getTypeDeclType(Constructor->getParent()); |
| 6020 | if (S.Context.hasSameUnqualifiedType(SourceType, Class) || |
| 6021 | S.IsDerivedFrom(SourceType, Class)) |
| 6022 | IsCopy = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6023 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 6024 | CreatedObject = true; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6025 | } else { |
| 6026 | // Build a call to the conversion function. |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 6027 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6028 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), nullptr, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6029 | FoundFn); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 6030 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 6031 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6032 | |
| 6033 | // FIXME: Should we move this initialization into a separate |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6034 | // derived-to-base conversion? I believe the answer is "no", because |
| 6035 | // we don't want to turn off access control here for c-style casts. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6036 | ExprResult CurInitExprRes = |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6037 | S.PerformObjectArgumentInitialization(CurInit.get(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6038 | /*Qualifier=*/nullptr, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6039 | FoundFn, Conversion); |
| 6040 | if(CurInitExprRes.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6041 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6042 | CurInit = CurInitExprRes; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6043 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6044 | // Build the actual call to the conversion function. |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 6045 | CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion, |
| 6046 | HadMultipleCandidates); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6047 | if (CurInit.isInvalid() || !CurInit.get()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6048 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6049 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6050 | CastKind = CK_UserDefinedConversion; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6051 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6052 | CreatedObject = Conversion->getReturnType()->isRecordType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6053 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6054 | |
Sebastian Redl | 112aa82 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 6055 | bool RequiresCopy = !IsCopy && !isReferenceBinding(Steps.back()); |
Abramo Bagnara | b0cf297 | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 6056 | bool MaybeBindToTemp = RequiresCopy || shouldBindAsTemporary(Entity); |
| 6057 | |
| 6058 | if (!MaybeBindToTemp && CreatedObject && shouldDestroyTemporary(Entity)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6059 | QualType T = CurInit.get()->getType(); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 6060 | if (const RecordType *Record = T->getAs<RecordType>()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6061 | CXXDestructorDecl *Destructor |
Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 6062 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6063 | S.CheckDestructorAccess(CurInit.get()->getLocStart(), Destructor, |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 6064 | S.PDiag(diag::err_access_dtor_temp) << T); |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 6065 | S.MarkFunctionReferenced(CurInit.get()->getLocStart(), Destructor); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 6066 | if (S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getLocStart())) |
| 6067 | return ExprError(); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 6068 | } |
| 6069 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6070 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6071 | CurInit = ImplicitCastExpr::Create(S.Context, CurInit.get()->getType(), |
| 6072 | CastKind, CurInit.get(), nullptr, |
| 6073 | CurInit.get()->getValueKind()); |
Abramo Bagnara | b0cf297 | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 6074 | if (MaybeBindToTemp) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6075 | CurInit = S.MaybeBindToTemporary(CurInit.getAs<Expr>()); |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 6076 | if (RequiresCopy) |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 6077 | CurInit = CopyObject(S, Entity.getType().getNonReferenceType(), Entity, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6078 | CurInit, /*IsExtraneousCopy=*/false); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6079 | break; |
| 6080 | } |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6081 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6082 | case SK_QualificationConversionLValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6083 | case SK_QualificationConversionXValue: |
| 6084 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6085 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 6086 | ExprValueKind VK = |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6087 | Step->Kind == SK_QualificationConversionLValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 6088 | VK_LValue : |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6089 | (Step->Kind == SK_QualificationConversionXValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 6090 | VK_XValue : |
| 6091 | VK_RValue); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6092 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, CK_NoOp, VK); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6093 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6094 | } |
| 6095 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 6096 | case SK_AtomicConversion: { |
| 6097 | assert(CurInit.get()->isRValue() && "cannot convert glvalue to atomic"); |
| 6098 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
| 6099 | CK_NonAtomicToAtomic, VK_RValue); |
| 6100 | break; |
| 6101 | } |
| 6102 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 6103 | case SK_LValueToRValue: { |
| 6104 | assert(CurInit.get()->isGLValue() && "cannot load from a prvalue"); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6105 | CurInit = ImplicitCastExpr::Create(S.Context, Step->Type, |
| 6106 | CK_LValueToRValue, CurInit.get(), |
| 6107 | /*BasePath=*/nullptr, VK_RValue); |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 6108 | break; |
| 6109 | } |
| 6110 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 6111 | case SK_ConversionSequence: |
| 6112 | case SK_ConversionSequenceNoNarrowing: { |
| 6113 | Sema::CheckedConversionKind CCK |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6114 | = Kind.isCStyleCast()? Sema::CCK_CStyleCast |
| 6115 | : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 6116 | : Kind.isExplicitCast()? Sema::CCK_OtherCast |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6117 | : Sema::CCK_ImplicitConversion; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6118 | ExprResult CurInitExprRes = |
| 6119 | S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6120 | getAssignmentAction(Entity), CCK); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6121 | if (CurInitExprRes.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6122 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6123 | CurInit = CurInitExprRes; |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 6124 | |
| 6125 | if (Step->Kind == SK_ConversionSequenceNoNarrowing && |
| 6126 | S.getLangOpts().CPlusPlus && !CurInit.get()->isValueDependent()) |
| 6127 | DiagnoseNarrowingInInitList(S, *Step->ICS, SourceType, Entity.getType(), |
| 6128 | CurInit.get()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6129 | break; |
Douglas Gregor | 5c8ffab | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 6130 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6131 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6132 | case SK_ListInitialization: { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6133 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6134 | // If we're not initializing the top-level entity, we need to create an |
| 6135 | // InitializeTemporary entity for our target type. |
| 6136 | QualType Ty = Step->Type; |
| 6137 | bool IsTemporary = !S.Context.hasSameType(Entity.getType(), Ty); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6138 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(Ty); |
Richard Smith | d712d0d | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 6139 | InitializedEntity InitEntity = IsTemporary ? TempEntity : Entity; |
| 6140 | InitListChecker PerformInitList(S, InitEntity, |
Richard Smith | de22923 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 6141 | InitList, Ty, /*VerifyOnly=*/false); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6142 | if (PerformInitList.HadError()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6143 | return ExprError(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6144 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6145 | // Hack: We must update *ResultType if available in order to set the |
| 6146 | // bounds of arrays, e.g. in 'int ar[] = {1, 2, 3};'. |
| 6147 | // Worst case: 'const int (&arref)[] = {1, 2, 3};'. |
| 6148 | if (ResultType && |
| 6149 | ResultType->getNonReferenceType()->isIncompleteArrayType()) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6150 | if ((*ResultType)->isRValueReferenceType()) |
| 6151 | Ty = S.Context.getRValueReferenceType(Ty); |
| 6152 | else if ((*ResultType)->isLValueReferenceType()) |
| 6153 | Ty = S.Context.getLValueReferenceType(Ty, |
| 6154 | (*ResultType)->getAs<LValueReferenceType>()->isSpelledAsLValue()); |
| 6155 | *ResultType = Ty; |
| 6156 | } |
| 6157 | |
| 6158 | InitListExpr *StructuredInitList = |
| 6159 | PerformInitList.getFullyStructuredList(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6160 | CurInit.get(); |
Richard Smith | d712d0d | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 6161 | CurInit = shouldBindAsTemporary(InitEntity) |
| 6162 | ? S.MaybeBindToTemporary(StructuredInitList) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6163 | : StructuredInitList; |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6164 | break; |
| 6165 | } |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6166 | |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 6167 | case SK_ConstructorInitializationFromList: { |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 6168 | // When an initializer list is passed for a parameter of type "reference |
| 6169 | // to object", we don't get an EK_Temporary entity, but instead an |
| 6170 | // EK_Parameter entity with reference type. |
Sebastian Redl | 99f6616 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 6171 | // FIXME: This is a hack. What we really should do is create a user |
| 6172 | // conversion step for this case, but this makes it considerably more |
| 6173 | // complicated. For now, this will do. |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 6174 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 6175 | Entity.getType().getNonReferenceType()); |
| 6176 | bool UseTemporary = Entity.getType()->isReferenceType(); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 6177 | assert(Args.size() == 1 && "expected a single argument for list init"); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6178 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Richard Smith | 2b349ae | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 6179 | S.Diag(InitList->getExprLoc(), diag::warn_cxx98_compat_ctor_list_init) |
| 6180 | << InitList->getSourceRange(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6181 | MultiExprArg Arg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 6182 | CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity : |
| 6183 | Entity, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6184 | Kind, Arg, *Step, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6185 | ConstructorInitRequiresZeroInit, |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 6186 | /*IsListInitialization*/true, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6187 | /*IsStdInitListInit*/false, |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 6188 | InitList->getLBraceLoc(), |
| 6189 | InitList->getRBraceLoc()); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6190 | break; |
| 6191 | } |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6192 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6193 | case SK_UnwrapInitList: |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6194 | CurInit = cast<InitListExpr>(CurInit.get())->getInit(0); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6195 | break; |
| 6196 | |
| 6197 | case SK_RewrapInitList: { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6198 | Expr *E = CurInit.get(); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6199 | InitListExpr *Syntactic = Step->WrappingSyntacticList; |
| 6200 | InitListExpr *ILE = new (S.Context) InitListExpr(S.Context, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 6201 | Syntactic->getLBraceLoc(), E, Syntactic->getRBraceLoc()); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6202 | ILE->setSyntacticForm(Syntactic); |
| 6203 | ILE->setType(E->getType()); |
| 6204 | ILE->setValueKind(E->getValueKind()); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6205 | CurInit = ILE; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6206 | break; |
| 6207 | } |
| 6208 | |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 6209 | case SK_ConstructorInitialization: |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6210 | case SK_StdInitializerListConstructorCall: { |
Sebastian Redl | 99f6616 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 6211 | // When an initializer list is passed for a parameter of type "reference |
| 6212 | // to object", we don't get an EK_Temporary entity, but instead an |
| 6213 | // EK_Parameter entity with reference type. |
| 6214 | // FIXME: This is a hack. What we really should do is create a user |
| 6215 | // conversion step for this case, but this makes it considerably more |
| 6216 | // complicated. For now, this will do. |
| 6217 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 6218 | Entity.getType().getNonReferenceType()); |
| 6219 | bool UseTemporary = Entity.getType()->isReferenceType(); |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6220 | bool IsStdInitListInit = |
| 6221 | Step->Kind == SK_StdInitializerListConstructorCall; |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 6222 | CurInit = PerformConstructorInitialization( |
| 6223 | S, UseTemporary ? TempEntity : Entity, Kind, Args, *Step, |
| 6224 | ConstructorInitRequiresZeroInit, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6225 | /*IsListInitialization*/IsStdInitListInit, |
| 6226 | /*IsStdInitListInitialization*/IsStdInitListInit, |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 6227 | /*LBraceLoc*/SourceLocation(), |
| 6228 | /*RBraceLoc*/SourceLocation()); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6229 | break; |
Sebastian Redl | 99f6616 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 6230 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6231 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 6232 | case SK_ZeroInitialization: { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6233 | step_iterator NextStep = Step; |
| 6234 | ++NextStep; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6235 | if (NextStep != StepEnd && |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 6236 | (NextStep->Kind == SK_ConstructorInitialization || |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 6237 | NextStep->Kind == SK_ConstructorInitializationFromList)) { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6238 | // The need for zero-initialization is recorded directly into |
| 6239 | // the call to the object's constructor within the next step. |
| 6240 | ConstructorInitRequiresZeroInit = true; |
| 6241 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6242 | S.getLangOpts().CPlusPlus && |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6243 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 6244 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 6245 | if (!TSInfo) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6246 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 6247 | Kind.getRange().getBegin()); |
| 6248 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6249 | CurInit = new (S.Context) CXXScalarValueInitExpr( |
| 6250 | TSInfo->getType().getNonLValueExprType(S.Context), TSInfo, |
| 6251 | Kind.getRange().getEnd()); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6252 | } else { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6253 | CurInit = new (S.Context) ImplicitValueInitExpr(Step->Type); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6254 | } |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 6255 | break; |
| 6256 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6257 | |
| 6258 | case SK_CAssignment: { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6259 | QualType SourceType = CurInit.get()->getType(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6260 | ExprResult Result = CurInit; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6261 | Sema::AssignConvertType ConvTy = |
Fariborz Jahanian | 25eef19 | 2013-07-31 21:40:51 +0000 | [diff] [blame] | 6262 | S.CheckSingleAssignmentConstraints(Step->Type, Result, true, |
| 6263 | Entity.getKind() == InitializedEntity::EK_Parameter_CF_Audited); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6264 | if (Result.isInvalid()) |
| 6265 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6266 | CurInit = Result; |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 6267 | |
| 6268 | // If this is a call, allow conversion to a transparent union. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6269 | ExprResult CurInitExprRes = CurInit; |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 6270 | if (ConvTy != Sema::Compatible && |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6271 | Entity.isParameterKind() && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6272 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes) |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 6273 | == Sema::Compatible) |
| 6274 | ConvTy = Sema::Compatible; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6275 | if (CurInitExprRes.isInvalid()) |
| 6276 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6277 | CurInit = CurInitExprRes; |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 6278 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6279 | bool Complained; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6280 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 6281 | Step->Type, SourceType, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6282 | CurInit.get(), |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 6283 | getAssignmentAction(Entity, true), |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6284 | &Complained)) { |
| 6285 | PrintInitLocationNote(S, Entity); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6286 | return ExprError(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6287 | } else if (Complained) |
| 6288 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6289 | break; |
| 6290 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 6291 | |
| 6292 | case SK_StringInit: { |
| 6293 | QualType Ty = Step->Type; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6294 | CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty, |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 6295 | S.Context.getAsArrayType(Ty), S); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 6296 | break; |
| 6297 | } |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6298 | |
| 6299 | case SK_ObjCObjectConversion: |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6300 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6301 | CK_ObjCObjectLValueCast, |
Eli Friedman | be4b363 | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 6302 | CurInit.get()->getValueKind()); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6303 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6304 | |
| 6305 | case SK_ArrayInit: |
| 6306 | // Okay: we checked everything before creating this step. Note that |
| 6307 | // this is a GNU extension. |
| 6308 | S.Diag(Kind.getLocation(), diag::ext_array_init_copy) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6309 | << Step->Type << CurInit.get()->getType() |
| 6310 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6311 | |
| 6312 | // If the destination type is an incomplete array type, update the |
| 6313 | // type accordingly. |
| 6314 | if (ResultType) { |
| 6315 | if (const IncompleteArrayType *IncompleteDest |
| 6316 | = S.Context.getAsIncompleteArrayType(Step->Type)) { |
| 6317 | if (const ConstantArrayType *ConstantSource |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6318 | = S.Context.getAsConstantArrayType(CurInit.get()->getType())) { |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6319 | *ResultType = S.Context.getConstantArrayType( |
| 6320 | IncompleteDest->getElementType(), |
| 6321 | ConstantSource->getSize(), |
| 6322 | ArrayType::Normal, 0); |
| 6323 | } |
| 6324 | } |
| 6325 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6326 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6327 | |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 6328 | case SK_ParenthesizedArrayInit: |
| 6329 | // Okay: we checked everything before creating this step. Note that |
| 6330 | // this is a GNU extension. |
| 6331 | S.Diag(Kind.getLocation(), diag::ext_array_init_parens) |
| 6332 | << CurInit.get()->getSourceRange(); |
| 6333 | break; |
| 6334 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6335 | case SK_PassByIndirectCopyRestore: |
| 6336 | case SK_PassByIndirectRestore: |
| 6337 | checkIndirectCopyRestoreSource(S, CurInit.get()); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6338 | CurInit = new (S.Context) ObjCIndirectCopyRestoreExpr( |
| 6339 | CurInit.get(), Step->Type, |
| 6340 | Step->Kind == SK_PassByIndirectCopyRestore); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6341 | break; |
| 6342 | |
| 6343 | case SK_ProduceObjCObject: |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6344 | CurInit = |
| 6345 | ImplicitCastExpr::Create(S.Context, Step->Type, CK_ARCProduceObject, |
| 6346 | CurInit.get(), nullptr, VK_RValue); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6347 | break; |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6348 | |
| 6349 | case SK_StdInitializerList: { |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6350 | S.Diag(CurInit.get()->getExprLoc(), |
| 6351 | diag::warn_cxx98_compat_initializer_list_init) |
| 6352 | << CurInit.get()->getSourceRange(); |
Sebastian Redl | 249dee5 | 2012-03-05 19:35:43 +0000 | [diff] [blame] | 6353 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6354 | // Materialize the temporary into memory. |
| 6355 | MaterializeTemporaryExpr *MTE = new (S.Context) |
| 6356 | MaterializeTemporaryExpr(CurInit.get()->getType(), CurInit.get(), |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 6357 | /*BoundToLvalueReference=*/false); |
| 6358 | |
| 6359 | // Maybe lifetime-extend the array temporary's subobjects to match the |
| 6360 | // entity's lifetime. |
| 6361 | if (const InitializedEntity *ExtendingEntity = |
| 6362 | getEntityForTemporaryLifetimeExtension(&Entity)) |
| 6363 | if (performReferenceExtension(MTE, ExtendingEntity)) |
| 6364 | warnOnLifetimeExtension(S, Entity, CurInit.get(), |
| 6365 | /*IsInitializerList=*/true, |
| 6366 | ExtendingEntity->getDecl()); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6367 | |
| 6368 | // Wrap it in a construction of a std::initializer_list<T>. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6369 | CurInit = new (S.Context) CXXStdInitializerListExpr(Step->Type, MTE); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6370 | |
| 6371 | // Bind the result, in case the library has given initializer_list a |
| 6372 | // non-trivial destructor. |
| 6373 | if (shouldBindAsTemporary(Entity)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6374 | CurInit = S.MaybeBindToTemporary(CurInit.get()); |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6375 | break; |
| 6376 | } |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6377 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6378 | case SK_OCLSamplerInit: { |
| 6379 | assert(Step->Type->isSamplerT() && |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 6380 | "Sampler initialization on non-sampler type."); |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6381 | |
| 6382 | QualType SourceType = CurInit.get()->getType(); |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6383 | |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6384 | if (Entity.isParameterKind()) { |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6385 | if (!SourceType->isSamplerT()) |
| 6386 | S.Diag(Kind.getLocation(), diag::err_sampler_argument_required) |
| 6387 | << SourceType; |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6388 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6389 | llvm_unreachable("Invalid EntityKind!"); |
| 6390 | } |
| 6391 | |
| 6392 | break; |
| 6393 | } |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6394 | case SK_OCLZeroEvent: { |
| 6395 | assert(Step->Type->isEventT() && |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 6396 | "Event initialization on non-event type."); |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6397 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6398 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6399 | CK_ZeroToOCLEvent, |
| 6400 | CurInit.get()->getValueKind()); |
| 6401 | break; |
| 6402 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6403 | } |
| 6404 | } |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 6405 | |
| 6406 | // Diagnose non-fatal problems with the completed initialization. |
| 6407 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 6408 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 6409 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 6410 | cast<FieldDecl>(Entity.getDecl()), |
| 6411 | CurInit.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6412 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6413 | return CurInit; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6414 | } |
| 6415 | |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6416 | /// Somewhere within T there is an uninitialized reference subobject. |
| 6417 | /// Dig it out and diagnose it. |
Benjamin Kramer | 3e35026 | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 6418 | static bool DiagnoseUninitializedReference(Sema &S, SourceLocation Loc, |
| 6419 | QualType T) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6420 | if (T->isReferenceType()) { |
| 6421 | S.Diag(Loc, diag::err_reference_without_init) |
| 6422 | << T.getNonReferenceType(); |
| 6423 | return true; |
| 6424 | } |
| 6425 | |
| 6426 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 6427 | if (!RD || !RD->hasUninitializedReferenceMember()) |
| 6428 | return false; |
| 6429 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6430 | for (const auto *FI : RD->fields()) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6431 | if (FI->isUnnamedBitfield()) |
| 6432 | continue; |
| 6433 | |
| 6434 | if (DiagnoseUninitializedReference(S, FI->getLocation(), FI->getType())) { |
| 6435 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 6436 | return true; |
| 6437 | } |
| 6438 | } |
| 6439 | |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 6440 | for (const auto &BI : RD->bases()) { |
| 6441 | if (DiagnoseUninitializedReference(S, BI.getLocStart(), BI.getType())) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6442 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 6443 | return true; |
| 6444 | } |
| 6445 | } |
| 6446 | |
| 6447 | return false; |
| 6448 | } |
| 6449 | |
| 6450 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6451 | //===----------------------------------------------------------------------===// |
| 6452 | // Diagnose initialization failures |
| 6453 | //===----------------------------------------------------------------------===// |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 6454 | |
| 6455 | /// Emit notes associated with an initialization that failed due to a |
| 6456 | /// "simple" conversion failure. |
| 6457 | static void emitBadConversionNotes(Sema &S, const InitializedEntity &entity, |
| 6458 | Expr *op) { |
| 6459 | QualType destType = entity.getType(); |
| 6460 | if (destType.getNonReferenceType()->isObjCObjectPointerType() && |
| 6461 | op->getType()->isObjCObjectPointerType()) { |
| 6462 | |
| 6463 | // Emit a possible note about the conversion failing because the |
| 6464 | // operand is a message send with a related result type. |
| 6465 | S.EmitRelatedResultTypeNote(op); |
| 6466 | |
| 6467 | // Emit a possible note about a return failing because we're |
| 6468 | // expecting a related result type. |
| 6469 | if (entity.getKind() == InitializedEntity::EK_Result) |
| 6470 | S.EmitRelatedResultTypeNoteForReturn(destType); |
| 6471 | } |
| 6472 | } |
| 6473 | |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 6474 | static void diagnoseListInit(Sema &S, const InitializedEntity &Entity, |
| 6475 | InitListExpr *InitList) { |
| 6476 | QualType DestType = Entity.getType(); |
| 6477 | |
| 6478 | QualType E; |
| 6479 | if (S.getLangOpts().CPlusPlus11 && S.isStdInitializerList(DestType, &E)) { |
| 6480 | QualType ArrayType = S.Context.getConstantArrayType( |
| 6481 | E.withConst(), |
| 6482 | llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 6483 | InitList->getNumInits()), |
| 6484 | clang::ArrayType::Normal, 0); |
| 6485 | InitializedEntity HiddenArray = |
| 6486 | InitializedEntity::InitializeTemporary(ArrayType); |
| 6487 | return diagnoseListInit(S, HiddenArray, InitList); |
| 6488 | } |
| 6489 | |
Richard Smith | 8d082d1 | 2014-09-04 22:13:39 +0000 | [diff] [blame] | 6490 | if (DestType->isReferenceType()) { |
| 6491 | // A list-initialization failure for a reference means that we tried to |
| 6492 | // create a temporary of the inner type (per [dcl.init.list]p3.6) and the |
| 6493 | // inner initialization failed. |
| 6494 | QualType T = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 6495 | diagnoseListInit(S, InitializedEntity::InitializeTemporary(T), InitList); |
| 6496 | SourceLocation Loc = InitList->getLocStart(); |
| 6497 | if (auto *D = Entity.getDecl()) |
| 6498 | Loc = D->getLocation(); |
| 6499 | S.Diag(Loc, diag::note_in_reference_temporary_list_initializer) << T; |
| 6500 | return; |
| 6501 | } |
| 6502 | |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 6503 | InitListChecker DiagnoseInitList(S, Entity, InitList, DestType, |
| 6504 | /*VerifyOnly=*/false); |
| 6505 | assert(DiagnoseInitList.HadError() && |
| 6506 | "Inconsistent init list check result."); |
| 6507 | } |
| 6508 | |
Nico Weber | 9386c82 | 2014-07-23 05:16:10 +0000 | [diff] [blame] | 6509 | /// Prints a fixit for adding a null initializer for |Entity|. Call this only |
| 6510 | /// right after emitting a diagnostic. |
| 6511 | static void maybeEmitZeroInitializationFixit(Sema &S, |
| 6512 | InitializationSequence &Sequence, |
| 6513 | const InitializedEntity &Entity) { |
| 6514 | if (Entity.getKind() != InitializedEntity::EK_Variable) |
| 6515 | return; |
| 6516 | |
| 6517 | VarDecl *VD = cast<VarDecl>(Entity.getDecl()); |
| 6518 | if (VD->getInit() || VD->getLocEnd().isMacroID()) |
| 6519 | return; |
| 6520 | |
| 6521 | QualType VariableTy = VD->getType().getCanonicalType(); |
| 6522 | SourceLocation Loc = S.getLocForEndOfToken(VD->getLocEnd()); |
| 6523 | std::string Init = S.getFixItZeroInitializerForType(VariableTy, Loc); |
| 6524 | |
| 6525 | S.Diag(Loc, diag::note_add_initializer) |
| 6526 | << VD << FixItHint::CreateInsertion(Loc, Init); |
| 6527 | } |
| 6528 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6529 | bool InitializationSequence::Diagnose(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6530 | const InitializedEntity &Entity, |
| 6531 | const InitializationKind &Kind, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6532 | ArrayRef<Expr *> Args) { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 6533 | if (!Failed()) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6534 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6535 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 6536 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6537 | switch (Failure) { |
| 6538 | case FK_TooManyInitsForReference: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6539 | // FIXME: Customize for the initialized entity? |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6540 | if (Args.empty()) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6541 | // Dig out the reference subobject which is uninitialized and diagnose it. |
| 6542 | // If this is value-initialization, this could be nested some way within |
| 6543 | // the target type. |
| 6544 | assert(Kind.getKind() == InitializationKind::IK_Value || |
| 6545 | DestType->isReferenceType()); |
| 6546 | bool Diagnosed = |
| 6547 | DiagnoseUninitializedReference(S, Kind.getLocation(), DestType); |
| 6548 | assert(Diagnosed && "couldn't find uninitialized reference to diagnose"); |
| 6549 | (void)Diagnosed; |
| 6550 | } else // FIXME: diagnostic below could be better! |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6551 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6552 | << SourceRange(Args.front()->getLocStart(), Args.back()->getLocEnd()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6553 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6554 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6555 | case FK_ArrayNeedsInitList: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 6556 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 0; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6557 | break; |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 6558 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 6559 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 1; |
| 6560 | break; |
| 6561 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 6562 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 2; |
| 6563 | break; |
| 6564 | case FK_NarrowStringIntoWideCharArray: |
| 6565 | S.Diag(Kind.getLocation(), diag::err_array_init_narrow_string_into_wchar); |
| 6566 | break; |
| 6567 | case FK_WideStringIntoCharArray: |
| 6568 | S.Diag(Kind.getLocation(), diag::err_array_init_wide_string_into_char); |
| 6569 | break; |
| 6570 | case FK_IncompatWideStringIntoWideChar: |
| 6571 | S.Diag(Kind.getLocation(), |
| 6572 | diag::err_array_init_incompat_wide_string_into_wchar); |
| 6573 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6574 | case FK_ArrayTypeMismatch: |
| 6575 | case FK_NonConstantArrayInit: |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 6576 | S.Diag(Kind.getLocation(), |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6577 | (Failure == FK_ArrayTypeMismatch |
| 6578 | ? diag::err_array_init_different_type |
| 6579 | : diag::err_array_init_non_constant_array)) |
| 6580 | << DestType.getNonReferenceType() |
| 6581 | << Args[0]->getType() |
| 6582 | << Args[0]->getSourceRange(); |
| 6583 | break; |
| 6584 | |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 6585 | case FK_VariableLengthArrayHasInitializer: |
| 6586 | S.Diag(Kind.getLocation(), diag::err_variable_object_no_init) |
| 6587 | << Args[0]->getSourceRange(); |
| 6588 | break; |
| 6589 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6590 | case FK_AddressOfOverloadFailed: { |
| 6591 | DeclAccessPair Found; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6592 | S.ResolveAddressOfOverloadedFunction(Args[0], |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6593 | DestType.getNonReferenceType(), |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6594 | true, |
| 6595 | Found); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6596 | break; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6597 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6598 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6599 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 6600 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6601 | switch (FailedOverloadResult) { |
| 6602 | case OR_Ambiguous: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6603 | if (Failure == FK_UserConversionOverloadFailed) |
| 6604 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
| 6605 | << Args[0]->getType() << DestType |
| 6606 | << Args[0]->getSourceRange(); |
| 6607 | else |
| 6608 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
| 6609 | << DestType << Args[0]->getType() |
| 6610 | << Args[0]->getSourceRange(); |
| 6611 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6612 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6613 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6614 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6615 | case OR_No_Viable_Function: |
Larisse Voufo | 70bb43a | 2013-06-27 03:36:30 +0000 | [diff] [blame] | 6616 | if (!S.RequireCompleteType(Kind.getLocation(), |
Larisse Voufo | 64cf3ef | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 6617 | DestType.getNonReferenceType(), |
| 6618 | diag::err_typecheck_nonviable_condition_incomplete, |
| 6619 | Args[0]->getType(), Args[0]->getSourceRange())) |
| 6620 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
| 6621 | << Args[0]->getType() << Args[0]->getSourceRange() |
| 6622 | << DestType.getNonReferenceType(); |
| 6623 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6624 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6625 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6626 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6627 | case OR_Deleted: { |
| 6628 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
| 6629 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 6630 | << Args[0]->getSourceRange(); |
| 6631 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6632 | OverloadingResult Ovl |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 6633 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best, |
| 6634 | true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6635 | if (Ovl == OR_Deleted) { |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6636 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6637 | } else { |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 6638 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6639 | } |
| 6640 | break; |
| 6641 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6642 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6643 | case OR_Success: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 6644 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6645 | } |
| 6646 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6647 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6648 | case FK_NonConstLValueReferenceBindingToTemporary: |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6649 | if (isa<InitListExpr>(Args[0])) { |
| 6650 | S.Diag(Kind.getLocation(), |
| 6651 | diag::err_lvalue_reference_bind_to_initlist) |
| 6652 | << DestType.getNonReferenceType().isVolatileQualified() |
| 6653 | << DestType.getNonReferenceType() |
| 6654 | << Args[0]->getSourceRange(); |
| 6655 | break; |
| 6656 | } |
| 6657 | // Intentional fallthrough |
| 6658 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6659 | case FK_NonConstLValueReferenceBindingToUnrelated: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6660 | S.Diag(Kind.getLocation(), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6661 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 6662 | ? diag::err_lvalue_reference_bind_to_temporary |
| 6663 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | d1e0864 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 6664 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6665 | << DestType.getNonReferenceType() |
| 6666 | << Args[0]->getType() |
| 6667 | << Args[0]->getSourceRange(); |
| 6668 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6669 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6670 | case FK_RValueReferenceBindingToLValue: |
| 6671 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
Douglas Gregor | bed28f7 | 2011-01-21 01:04:33 +0000 | [diff] [blame] | 6672 | << DestType.getNonReferenceType() << Args[0]->getType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6673 | << Args[0]->getSourceRange(); |
| 6674 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6675 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6676 | case FK_ReferenceInitDropsQualifiers: |
| 6677 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
| 6678 | << DestType.getNonReferenceType() |
| 6679 | << Args[0]->getType() |
| 6680 | << Args[0]->getSourceRange(); |
| 6681 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6682 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6683 | case FK_ReferenceInitFailed: |
| 6684 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 6685 | << DestType.getNonReferenceType() |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 6686 | << Args[0]->isLValue() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6687 | << Args[0]->getType() |
| 6688 | << Args[0]->getSourceRange(); |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 6689 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6690 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6691 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 6692 | case FK_ConversionFailed: { |
| 6693 | QualType FromType = Args[0]->getType(); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 6694 | PartialDiagnostic PDiag = S.PDiag(diag::err_init_conversion_failed) |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6695 | << (int)Entity.getKind() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6696 | << DestType |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 6697 | << Args[0]->isLValue() |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 6698 | << FromType |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6699 | << Args[0]->getSourceRange(); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 6700 | S.HandleFunctionTypeMismatch(PDiag, FromType, DestType); |
| 6701 | S.Diag(Kind.getLocation(), PDiag); |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 6702 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6703 | break; |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 6704 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6705 | |
| 6706 | case FK_ConversionFromPropertyFailed: |
| 6707 | // No-op. This error has already been reported. |
| 6708 | break; |
| 6709 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6710 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6711 | SourceRange R; |
| 6712 | |
| 6713 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0])) |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 6714 | R = SourceRange(InitList->getInit(0)->getLocEnd(), |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6715 | InitList->getLocEnd()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6716 | else |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6717 | R = SourceRange(Args.front()->getLocEnd(), Args.back()->getLocEnd()); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6718 | |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 6719 | R.setBegin(S.getLocForEndOfToken(R.getBegin())); |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 6720 | if (Kind.isCStyleOrFunctionalCast()) |
| 6721 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 6722 | << R; |
| 6723 | else |
| 6724 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 6725 | << /*scalar=*/2 << R; |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6726 | break; |
| 6727 | } |
| 6728 | |
| 6729 | case FK_ReferenceBindingToInitList: |
| 6730 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 6731 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 6732 | break; |
| 6733 | |
| 6734 | case FK_InitListBadDestinationType: |
| 6735 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 6736 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 6737 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6738 | |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6739 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6740 | case FK_ConstructorOverloadFailed: { |
| 6741 | SourceRange ArgsRange; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6742 | if (Args.size()) |
| 6743 | ArgsRange = SourceRange(Args.front()->getLocStart(), |
| 6744 | Args.back()->getLocEnd()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6745 | |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6746 | if (Failure == FK_ListConstructorOverloadFailed) { |
Nico Weber | 9709ebf | 2014-07-08 23:54:25 +0000 | [diff] [blame] | 6747 | assert(Args.size() == 1 && |
| 6748 | "List construction from other than 1 argument."); |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6749 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6750 | Args = MultiExprArg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6751 | } |
| 6752 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6753 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 6754 | // bad. |
| 6755 | switch (FailedOverloadResult) { |
| 6756 | case OR_Ambiguous: |
| 6757 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 6758 | << DestType << ArgsRange; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6759 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6760 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6761 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6762 | case OR_No_Viable_Function: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6763 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 6764 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 6765 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 6766 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 6767 | // This is implicit default initialization of a member or |
| 6768 | // base within a constructor. If no viable function was |
| 6769 | // found, notify the user that she needs to explicitly |
| 6770 | // initialize this base/member. |
| 6771 | CXXConstructorDecl *Constructor |
| 6772 | = cast<CXXConstructorDecl>(S.CurContext); |
| 6773 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 6774 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 6775 | << (Constructor->getInheritedConstructor() ? 2 : |
| 6776 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6777 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 6778 | << /*base=*/0 |
| 6779 | << Entity.getType(); |
| 6780 | |
| 6781 | RecordDecl *BaseDecl |
| 6782 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 6783 | ->getDecl(); |
| 6784 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 6785 | << S.Context.getTagDeclType(BaseDecl); |
| 6786 | } else { |
| 6787 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 6788 | << (Constructor->getInheritedConstructor() ? 2 : |
| 6789 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6790 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 6791 | << /*member=*/1 |
| 6792 | << Entity.getName(); |
Alp Toker | 2afa878 | 2014-05-28 12:20:14 +0000 | [diff] [blame] | 6793 | S.Diag(Entity.getDecl()->getLocation(), |
| 6794 | diag::note_member_declared_at); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6795 | |
| 6796 | if (const RecordType *Record |
| 6797 | = Entity.getType()->getAs<RecordType>()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6798 | S.Diag(Record->getDecl()->getLocation(), |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6799 | diag::note_previous_decl) |
| 6800 | << S.Context.getTagDeclType(Record->getDecl()); |
| 6801 | } |
| 6802 | break; |
| 6803 | } |
| 6804 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6805 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 6806 | << DestType << ArgsRange; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6807 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6808 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6809 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6810 | case OR_Deleted: { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6811 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6812 | OverloadingResult Ovl |
| 6813 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6814 | if (Ovl != OR_Deleted) { |
| 6815 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 6816 | << true << DestType << ArgsRange; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6817 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6818 | break; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6819 | } |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6820 | |
| 6821 | // If this is a defaulted or implicitly-declared function, then |
| 6822 | // it was implicitly deleted. Make it clear that the deletion was |
| 6823 | // implicit. |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6824 | if (S.isImplicitlyDeleted(Best->Function)) |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6825 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_special_init) |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6826 | << S.getSpecialMember(cast<CXXMethodDecl>(Best->Function)) |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6827 | << DestType << ArgsRange; |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6828 | else |
| 6829 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 6830 | << true << DestType << ArgsRange; |
| 6831 | |
| 6832 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6833 | break; |
| 6834 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6835 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6836 | case OR_Success: |
| 6837 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6838 | } |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6839 | } |
David Blaikie | 60deeee | 2012-01-17 08:24:58 +0000 | [diff] [blame] | 6840 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6841 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6842 | case FK_DefaultInitOfConst: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6843 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 6844 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 6845 | // This is implicit default-initialization of a const member in |
| 6846 | // a constructor. Complain that it needs to be explicitly |
| 6847 | // initialized. |
| 6848 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 6849 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 6850 | << (Constructor->getInheritedConstructor() ? 2 : |
| 6851 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6852 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 6853 | << /*const=*/1 |
| 6854 | << Entity.getName(); |
| 6855 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 6856 | << Entity.getName(); |
| 6857 | } else { |
| 6858 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
Nico Weber | 9386c82 | 2014-07-23 05:16:10 +0000 | [diff] [blame] | 6859 | << DestType << (bool)DestType->getAs<RecordType>(); |
| 6860 | maybeEmitZeroInitializationFixit(S, *this, Entity); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6861 | } |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6862 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6863 | |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6864 | case FK_Incomplete: |
Douglas Gregor | 85f3423 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 6865 | S.RequireCompleteType(Kind.getLocation(), FailedIncompleteType, |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6866 | diag::err_init_incomplete_type); |
| 6867 | break; |
| 6868 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6869 | case FK_ListInitializationFailed: { |
| 6870 | // Run the init list checker again to emit diagnostics. |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 6871 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
| 6872 | diagnoseListInit(S, Entity, InitList); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6873 | break; |
| 6874 | } |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 6875 | |
| 6876 | case FK_PlaceholderType: { |
| 6877 | // FIXME: Already diagnosed! |
| 6878 | break; |
| 6879 | } |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6880 | |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 6881 | case FK_ExplicitConstructor: { |
| 6882 | S.Diag(Kind.getLocation(), diag::err_selected_explicit_constructor) |
| 6883 | << Args[0]->getSourceRange(); |
| 6884 | OverloadCandidateSet::iterator Best; |
| 6885 | OverloadingResult Ovl |
| 6886 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Matt Beaumont-Gay | 5dcce09 | 2012-04-02 19:05:35 +0000 | [diff] [blame] | 6887 | (void)Ovl; |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 6888 | assert(Ovl == OR_Success && "Inconsistent overload resolution"); |
| 6889 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 6890 | S.Diag(CtorDecl->getLocation(), diag::note_constructor_declared_here); |
| 6891 | break; |
| 6892 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6893 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6894 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6895 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6896 | return true; |
| 6897 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6898 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6899 | void InitializationSequence::dump(raw_ostream &OS) const { |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6900 | switch (SequenceKind) { |
| 6901 | case FailedSequence: { |
| 6902 | OS << "Failed sequence: "; |
| 6903 | switch (Failure) { |
| 6904 | case FK_TooManyInitsForReference: |
| 6905 | OS << "too many initializers for reference"; |
| 6906 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6907 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6908 | case FK_ArrayNeedsInitList: |
| 6909 | OS << "array requires initializer list"; |
| 6910 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6911 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6912 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 6913 | OS << "array requires initializer list or string literal"; |
| 6914 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6915 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 6916 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 6917 | OS << "array requires initializer list or wide string literal"; |
| 6918 | break; |
| 6919 | |
| 6920 | case FK_NarrowStringIntoWideCharArray: |
| 6921 | OS << "narrow string into wide char array"; |
| 6922 | break; |
| 6923 | |
| 6924 | case FK_WideStringIntoCharArray: |
| 6925 | OS << "wide string into char array"; |
| 6926 | break; |
| 6927 | |
| 6928 | case FK_IncompatWideStringIntoWideChar: |
| 6929 | OS << "incompatible wide string into wide char array"; |
| 6930 | break; |
| 6931 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6932 | case FK_ArrayTypeMismatch: |
| 6933 | OS << "array type mismatch"; |
| 6934 | break; |
| 6935 | |
| 6936 | case FK_NonConstantArrayInit: |
| 6937 | OS << "non-constant array initializer"; |
| 6938 | break; |
| 6939 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6940 | case FK_AddressOfOverloadFailed: |
| 6941 | OS << "address of overloaded function failed"; |
| 6942 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6943 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6944 | case FK_ReferenceInitOverloadFailed: |
| 6945 | OS << "overload resolution for reference initialization failed"; |
| 6946 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6947 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6948 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 6949 | OS << "non-const lvalue reference bound to temporary"; |
| 6950 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6951 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6952 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 6953 | OS << "non-const lvalue reference bound to unrelated type"; |
| 6954 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6955 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6956 | case FK_RValueReferenceBindingToLValue: |
| 6957 | OS << "rvalue reference bound to an lvalue"; |
| 6958 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6959 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6960 | case FK_ReferenceInitDropsQualifiers: |
| 6961 | OS << "reference initialization drops qualifiers"; |
| 6962 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6963 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6964 | case FK_ReferenceInitFailed: |
| 6965 | OS << "reference initialization failed"; |
| 6966 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6967 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6968 | case FK_ConversionFailed: |
| 6969 | OS << "conversion failed"; |
| 6970 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6971 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6972 | case FK_ConversionFromPropertyFailed: |
| 6973 | OS << "conversion from property failed"; |
| 6974 | break; |
| 6975 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6976 | case FK_TooManyInitsForScalar: |
| 6977 | OS << "too many initializers for scalar"; |
| 6978 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6979 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6980 | case FK_ReferenceBindingToInitList: |
| 6981 | OS << "referencing binding to initializer list"; |
| 6982 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6983 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6984 | case FK_InitListBadDestinationType: |
| 6985 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 6986 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6987 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6988 | case FK_UserConversionOverloadFailed: |
| 6989 | OS << "overloading failed for user-defined conversion"; |
| 6990 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6991 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6992 | case FK_ConstructorOverloadFailed: |
| 6993 | OS << "constructor overloading failed"; |
| 6994 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6995 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6996 | case FK_DefaultInitOfConst: |
| 6997 | OS << "default initialization of a const variable"; |
| 6998 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6999 | |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 7000 | case FK_Incomplete: |
| 7001 | OS << "initialization of incomplete type"; |
| 7002 | break; |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 7003 | |
| 7004 | case FK_ListInitializationFailed: |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 7005 | OS << "list initialization checker failure"; |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 7006 | break; |
| 7007 | |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 7008 | case FK_VariableLengthArrayHasInitializer: |
| 7009 | OS << "variable length array has an initializer"; |
| 7010 | break; |
| 7011 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 7012 | case FK_PlaceholderType: |
| 7013 | OS << "initializer expression isn't contextually valid"; |
| 7014 | break; |
Nick Lewycky | 097f47c | 2011-12-22 20:21:32 +0000 | [diff] [blame] | 7015 | |
| 7016 | case FK_ListConstructorOverloadFailed: |
| 7017 | OS << "list constructor overloading failed"; |
| 7018 | break; |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 7019 | |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 7020 | case FK_ExplicitConstructor: |
| 7021 | OS << "list copy initialization chose explicit constructor"; |
| 7022 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7023 | } |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7024 | OS << '\n'; |
| 7025 | return; |
| 7026 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7027 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7028 | case DependentSequence: |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 7029 | OS << "Dependent sequence\n"; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7030 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7031 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 7032 | case NormalSequence: |
| 7033 | OS << "Normal sequence: "; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7034 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7035 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7036 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7037 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 7038 | if (S != step_begin()) { |
| 7039 | OS << " -> "; |
| 7040 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7041 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7042 | switch (S->Kind) { |
| 7043 | case SK_ResolveAddressOfOverloadedFunction: |
| 7044 | OS << "resolve address of overloaded function"; |
| 7045 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7046 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7047 | case SK_CastDerivedToBaseRValue: |
| 7048 | OS << "derived-to-base case (rvalue" << S->Type.getAsString() << ")"; |
| 7049 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7050 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7051 | case SK_CastDerivedToBaseXValue: |
| 7052 | OS << "derived-to-base case (xvalue" << S->Type.getAsString() << ")"; |
| 7053 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7054 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7055 | case SK_CastDerivedToBaseLValue: |
| 7056 | OS << "derived-to-base case (lvalue" << S->Type.getAsString() << ")"; |
| 7057 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7058 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7059 | case SK_BindReference: |
| 7060 | OS << "bind reference to lvalue"; |
| 7061 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7062 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7063 | case SK_BindReferenceToTemporary: |
| 7064 | OS << "bind reference to a temporary"; |
| 7065 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7066 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 7067 | case SK_ExtraneousCopyToTemporary: |
| 7068 | OS << "extraneous C++03 copy to temporary"; |
| 7069 | break; |
| 7070 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7071 | case SK_UserConversion: |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 7072 | OS << "user-defined conversion via " << *S->Function.Function; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7073 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7074 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7075 | case SK_QualificationConversionRValue: |
| 7076 | OS << "qualification conversion (rvalue)"; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7077 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7078 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7079 | case SK_QualificationConversionXValue: |
| 7080 | OS << "qualification conversion (xvalue)"; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7081 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7082 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7083 | case SK_QualificationConversionLValue: |
| 7084 | OS << "qualification conversion (lvalue)"; |
| 7085 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7086 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 7087 | case SK_AtomicConversion: |
| 7088 | OS << "non-atomic-to-atomic conversion"; |
| 7089 | break; |
| 7090 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 7091 | case SK_LValueToRValue: |
| 7092 | OS << "load (lvalue to rvalue)"; |
| 7093 | break; |
| 7094 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7095 | case SK_ConversionSequence: |
| 7096 | OS << "implicit conversion sequence ("; |
Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 7097 | S->ICS->dump(); // FIXME: use OS |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7098 | OS << ")"; |
| 7099 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7100 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7101 | case SK_ConversionSequenceNoNarrowing: |
| 7102 | OS << "implicit conversion sequence with narrowing prohibited ("; |
Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 7103 | S->ICS->dump(); // FIXME: use OS |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7104 | OS << ")"; |
| 7105 | break; |
| 7106 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7107 | case SK_ListInitialization: |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 7108 | OS << "list aggregate initialization"; |
| 7109 | break; |
| 7110 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7111 | case SK_UnwrapInitList: |
| 7112 | OS << "unwrap reference initializer list"; |
| 7113 | break; |
| 7114 | |
| 7115 | case SK_RewrapInitList: |
| 7116 | OS << "rewrap reference initializer list"; |
| 7117 | break; |
| 7118 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7119 | case SK_ConstructorInitialization: |
| 7120 | OS << "constructor initialization"; |
| 7121 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7122 | |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7123 | case SK_ConstructorInitializationFromList: |
| 7124 | OS << "list initialization via constructor"; |
| 7125 | break; |
| 7126 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7127 | case SK_ZeroInitialization: |
| 7128 | OS << "zero initialization"; |
| 7129 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7130 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7131 | case SK_CAssignment: |
| 7132 | OS << "C assignment"; |
| 7133 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7134 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7135 | case SK_StringInit: |
| 7136 | OS << "string initialization"; |
| 7137 | break; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 7138 | |
| 7139 | case SK_ObjCObjectConversion: |
| 7140 | OS << "Objective-C object conversion"; |
| 7141 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7142 | |
| 7143 | case SK_ArrayInit: |
| 7144 | OS << "array initialization"; |
| 7145 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7146 | |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 7147 | case SK_ParenthesizedArrayInit: |
| 7148 | OS << "parenthesized array initialization"; |
| 7149 | break; |
| 7150 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7151 | case SK_PassByIndirectCopyRestore: |
| 7152 | OS << "pass by indirect copy and restore"; |
| 7153 | break; |
| 7154 | |
| 7155 | case SK_PassByIndirectRestore: |
| 7156 | OS << "pass by indirect restore"; |
| 7157 | break; |
| 7158 | |
| 7159 | case SK_ProduceObjCObject: |
| 7160 | OS << "Objective-C object retension"; |
| 7161 | break; |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 7162 | |
| 7163 | case SK_StdInitializerList: |
| 7164 | OS << "std::initializer_list from initializer list"; |
| 7165 | break; |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7166 | |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 7167 | case SK_StdInitializerListConstructorCall: |
| 7168 | OS << "list initialization from std::initializer_list"; |
| 7169 | break; |
| 7170 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 7171 | case SK_OCLSamplerInit: |
| 7172 | OS << "OpenCL sampler_t from integer constant"; |
| 7173 | break; |
| 7174 | |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7175 | case SK_OCLZeroEvent: |
| 7176 | OS << "OpenCL event_t from zero"; |
| 7177 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7178 | } |
Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 7179 | |
| 7180 | OS << " [" << S->Type.getAsString() << ']'; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7181 | } |
Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 7182 | |
| 7183 | OS << '\n'; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7184 | } |
| 7185 | |
| 7186 | void InitializationSequence::dump() const { |
| 7187 | dump(llvm::errs()); |
| 7188 | } |
| 7189 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7190 | static void DiagnoseNarrowingInInitList(Sema &S, |
| 7191 | const ImplicitConversionSequence &ICS, |
| 7192 | QualType PreNarrowingType, |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7193 | QualType EntityType, |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7194 | const Expr *PostInit) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7195 | const StandardConversionSequence *SCS = nullptr; |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7196 | switch (ICS.getKind()) { |
| 7197 | case ImplicitConversionSequence::StandardConversion: |
| 7198 | SCS = &ICS.Standard; |
| 7199 | break; |
| 7200 | case ImplicitConversionSequence::UserDefinedConversion: |
| 7201 | SCS = &ICS.UserDefined.After; |
| 7202 | break; |
| 7203 | case ImplicitConversionSequence::AmbiguousConversion: |
| 7204 | case ImplicitConversionSequence::EllipsisConversion: |
| 7205 | case ImplicitConversionSequence::BadConversion: |
| 7206 | return; |
| 7207 | } |
| 7208 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7209 | // C++11 [dcl.init.list]p7: Check whether this is a narrowing conversion. |
| 7210 | APValue ConstantValue; |
Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 7211 | QualType ConstantType; |
| 7212 | switch (SCS->getNarrowingKind(S.Context, PostInit, ConstantValue, |
| 7213 | ConstantType)) { |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7214 | case NK_Not_Narrowing: |
| 7215 | // No narrowing occurred. |
| 7216 | return; |
| 7217 | |
| 7218 | case NK_Type_Narrowing: |
| 7219 | // This was a floating-to-integer conversion, which is always considered a |
| 7220 | // narrowing conversion even if the value is a constant and can be |
| 7221 | // represented exactly as an integer. |
| 7222 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 16e1b07 | 2013-11-12 02:41:45 +0000 | [diff] [blame] | 7223 | (S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11) |
| 7224 | ? diag::warn_init_list_type_narrowing |
| 7225 | : diag::ext_init_list_type_narrowing) |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7226 | << PostInit->getSourceRange() |
| 7227 | << PreNarrowingType.getLocalUnqualifiedType() |
| 7228 | << EntityType.getLocalUnqualifiedType(); |
| 7229 | break; |
| 7230 | |
| 7231 | case NK_Constant_Narrowing: |
| 7232 | // A constant value was narrowed. |
| 7233 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 16e1b07 | 2013-11-12 02:41:45 +0000 | [diff] [blame] | 7234 | (S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11) |
| 7235 | ? diag::warn_init_list_constant_narrowing |
| 7236 | : diag::ext_init_list_constant_narrowing) |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7237 | << PostInit->getSourceRange() |
Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 7238 | << ConstantValue.getAsString(S.getASTContext(), ConstantType) |
Jeffrey Yasskin | 7423138 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 7239 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7240 | break; |
| 7241 | |
| 7242 | case NK_Variable_Narrowing: |
| 7243 | // A variable's value may have been narrowed. |
| 7244 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 16e1b07 | 2013-11-12 02:41:45 +0000 | [diff] [blame] | 7245 | (S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11) |
| 7246 | ? diag::warn_init_list_variable_narrowing |
| 7247 | : diag::ext_init_list_variable_narrowing) |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7248 | << PostInit->getSourceRange() |
| 7249 | << PreNarrowingType.getLocalUnqualifiedType() |
Jeffrey Yasskin | 7423138 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 7250 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7251 | break; |
| 7252 | } |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7253 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 7254 | SmallString<128> StaticCast; |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7255 | llvm::raw_svector_ostream OS(StaticCast); |
| 7256 | OS << "static_cast<"; |
| 7257 | if (const TypedefType *TT = EntityType->getAs<TypedefType>()) { |
| 7258 | // It's important to use the typedef's name if there is one so that the |
| 7259 | // fixit doesn't break code using types like int64_t. |
| 7260 | // |
| 7261 | // FIXME: This will break if the typedef requires qualification. But |
| 7262 | // getQualifiedNameAsString() includes non-machine-parsable components. |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 7263 | OS << *TT->getDecl(); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7264 | } else if (const BuiltinType *BT = EntityType->getAs<BuiltinType>()) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7265 | OS << BT->getName(S.getLangOpts()); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7266 | else { |
| 7267 | // Oops, we didn't find the actual type of the variable. Don't emit a fixit |
| 7268 | // with a broken cast. |
| 7269 | return; |
| 7270 | } |
| 7271 | OS << ">("; |
Alp Toker | b086903 | 2014-05-17 01:13:18 +0000 | [diff] [blame] | 7272 | S.Diag(PostInit->getLocStart(), diag::note_init_list_narrowing_silence) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 7273 | << PostInit->getSourceRange() |
| 7274 | << FixItHint::CreateInsertion(PostInit->getLocStart(), OS.str()) |
| 7275 | << FixItHint::CreateInsertion( |
| 7276 | S.getLocForEndOfToken(PostInit->getLocEnd()), ")"); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7277 | } |
| 7278 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7279 | //===----------------------------------------------------------------------===// |
| 7280 | // Initialization helper functions |
| 7281 | //===----------------------------------------------------------------------===// |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 7282 | bool |
| 7283 | Sema::CanPerformCopyInitialization(const InitializedEntity &Entity, |
| 7284 | ExprResult Init) { |
| 7285 | if (Init.isInvalid()) |
| 7286 | return false; |
| 7287 | |
| 7288 | Expr *InitE = Init.get(); |
| 7289 | assert(InitE && "No initialization expression"); |
| 7290 | |
Douglas Gregor | f4cc61d | 2012-07-31 22:15:04 +0000 | [diff] [blame] | 7291 | InitializationKind Kind |
| 7292 | = InitializationKind::CreateCopy(InitE->getLocStart(), SourceLocation()); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 7293 | InitializationSequence Seq(*this, Entity, Kind, InitE); |
Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 7294 | return !Seq.Failed(); |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 7295 | } |
| 7296 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7297 | ExprResult |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7298 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 7299 | SourceLocation EqualLoc, |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7300 | ExprResult Init, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 7301 | bool TopLevelOfInitList, |
| 7302 | bool AllowExplicit) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7303 | if (Init.isInvalid()) |
| 7304 | return ExprError(); |
| 7305 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 7306 | Expr *InitE = Init.get(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7307 | assert(InitE && "No initialization expression?"); |
| 7308 | |
| 7309 | if (EqualLoc.isInvalid()) |
| 7310 | EqualLoc = InitE->getLocStart(); |
| 7311 | |
| 7312 | InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(), |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 7313 | EqualLoc, |
| 7314 | AllowExplicit); |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7315 | InitializationSequence Seq(*this, Entity, Kind, InitE, TopLevelOfInitList); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7316 | Init.get(); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7317 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 7318 | ExprResult Result = Seq.Perform(*this, Entity, Kind, InitE); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7319 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7320 | return Result; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7321 | } |